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