Eugene Zelenko | 1df42fa | 2017-04-24 23:21:38 +0000 | [diff] [blame] | 1 | //===- ELF.cpp - ELF object file implementation ---------------------------===// |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Object/ELF.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 10 | #include "llvm/BinaryFormat/ELF.h" |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 11 | #include "llvm/Support/LEB128.h" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 12 | |
Eugene Zelenko | 1df42fa | 2017-04-24 23:21:38 +0000 | [diff] [blame] | 13 | using namespace llvm; |
| 14 | using namespace object; |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 15 | |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 16 | #define STRINGIFY_ENUM_CASE(ns, name) \ |
| 17 | case ns::name: \ |
| 18 | return #name; |
| 19 | |
| 20 | #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name) |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 21 | |
Eugene Zelenko | 1df42fa | 2017-04-24 23:21:38 +0000 | [diff] [blame] | 22 | StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine, |
| 23 | uint32_t Type) { |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 24 | switch (Machine) { |
| 25 | case ELF::EM_X86_64: |
| 26 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 27 | #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 28 | default: |
| 29 | break; |
| 30 | } |
| 31 | break; |
| 32 | case ELF::EM_386: |
Michael Kuperstein | a3b79dd | 2015-11-04 11:21:50 +0000 | [diff] [blame] | 33 | case ELF::EM_IAMCU: |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 34 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 35 | #include "llvm/BinaryFormat/ELFRelocs/i386.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 36 | default: |
| 37 | break; |
| 38 | } |
| 39 | break; |
| 40 | case ELF::EM_MIPS: |
| 41 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 42 | #include "llvm/BinaryFormat/ELFRelocs/Mips.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 43 | default: |
| 44 | break; |
| 45 | } |
| 46 | break; |
| 47 | case ELF::EM_AARCH64: |
| 48 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 49 | #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 50 | default: |
| 51 | break; |
| 52 | } |
| 53 | break; |
| 54 | case ELF::EM_ARM: |
| 55 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 56 | #include "llvm/BinaryFormat/ELFRelocs/ARM.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 57 | default: |
| 58 | break; |
| 59 | } |
| 60 | break; |
Leslie Zhai | 49277d1 | 2017-09-13 01:49:49 +0000 | [diff] [blame] | 61 | case ELF::EM_ARC_COMPACT: |
| 62 | case ELF::EM_ARC_COMPACT2: |
| 63 | switch (Type) { |
| 64 | #include "llvm/BinaryFormat/ELFRelocs/ARC.def" |
| 65 | default: |
| 66 | break; |
| 67 | } |
| 68 | break; |
Dylan McKay | 35047ed | 2016-09-28 13:23:42 +0000 | [diff] [blame] | 69 | case ELF::EM_AVR: |
| 70 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 71 | #include "llvm/BinaryFormat/ELFRelocs/AVR.def" |
Dylan McKay | 35047ed | 2016-09-28 13:23:42 +0000 | [diff] [blame] | 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | break; |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 76 | case ELF::EM_HEXAGON: |
| 77 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 78 | #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | break; |
Jacques Pienaar | ea9f25a | 2016-03-01 21:21:42 +0000 | [diff] [blame] | 83 | case ELF::EM_LANAI: |
| 84 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 85 | #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" |
Jacques Pienaar | ea9f25a | 2016-03-01 21:21:42 +0000 | [diff] [blame] | 86 | default: |
| 87 | break; |
| 88 | } |
| 89 | break; |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 90 | case ELF::EM_PPC: |
| 91 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 92 | #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | break; |
| 97 | case ELF::EM_PPC64: |
| 98 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 99 | #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | break; |
Alex Bradbury | 1524f62 | 2016-11-01 16:59:37 +0000 | [diff] [blame] | 104 | case ELF::EM_RISCV: |
| 105 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 106 | #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" |
Alex Bradbury | 1524f62 | 2016-11-01 16:59:37 +0000 | [diff] [blame] | 107 | default: |
| 108 | break; |
| 109 | } |
| 110 | break; |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 111 | case ELF::EM_S390: |
| 112 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 113 | #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | break; |
Venkatraman Govindaraju | cdee0ed | 2014-01-26 03:21:28 +0000 | [diff] [blame] | 118 | case ELF::EM_SPARC: |
| 119 | case ELF::EM_SPARC32PLUS: |
| 120 | case ELF::EM_SPARCV9: |
| 121 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 122 | #include "llvm/BinaryFormat/ELFRelocs/Sparc.def" |
Venkatraman Govindaraju | cdee0ed | 2014-01-26 03:21:28 +0000 | [diff] [blame] | 123 | default: |
| 124 | break; |
| 125 | } |
| 126 | break; |
Tom Stellard | f8db61c | 2016-06-17 22:38:08 +0000 | [diff] [blame] | 127 | case ELF::EM_AMDGPU: |
| 128 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 129 | #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" |
Tom Stellard | f8db61c | 2016-06-17 22:38:08 +0000 | [diff] [blame] | 130 | default: |
| 131 | break; |
| 132 | } |
Adrian Prantl | 0e6694d | 2017-12-19 22:05:25 +0000 | [diff] [blame] | 133 | break; |
Alexei Starovoitov | cfb51f5 | 2016-07-15 22:27:55 +0000 | [diff] [blame] | 134 | case ELF::EM_BPF: |
| 135 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 136 | #include "llvm/BinaryFormat/ELFRelocs/BPF.def" |
Alexei Starovoitov | cfb51f5 | 2016-07-15 22:27:55 +0000 | [diff] [blame] | 137 | default: |
| 138 | break; |
| 139 | } |
Tom Stellard | f8db61c | 2016-06-17 22:38:08 +0000 | [diff] [blame] | 140 | break; |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 141 | case ELF::EM_MSP430: |
| 142 | switch (Type) { |
| 143 | #include "llvm/BinaryFormat/ELFRelocs/MSP430.def" |
| 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | break; |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 148 | default: |
| 149 | break; |
| 150 | } |
| 151 | return "Unknown"; |
| 152 | } |
| 153 | |
Tim Northover | 242785c | 2014-11-21 20:16:07 +0000 | [diff] [blame] | 154 | #undef ELF_RELOC |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 155 | |
Fangrui Song | d2ed5be | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 156 | uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) { |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 157 | switch (Machine) { |
| 158 | case ELF::EM_X86_64: |
| 159 | return ELF::R_X86_64_RELATIVE; |
| 160 | case ELF::EM_386: |
| 161 | case ELF::EM_IAMCU: |
| 162 | return ELF::R_386_RELATIVE; |
| 163 | case ELF::EM_MIPS: |
| 164 | break; |
| 165 | case ELF::EM_AARCH64: |
| 166 | return ELF::R_AARCH64_RELATIVE; |
| 167 | case ELF::EM_ARM: |
| 168 | return ELF::R_ARM_RELATIVE; |
| 169 | case ELF::EM_ARC_COMPACT: |
| 170 | case ELF::EM_ARC_COMPACT2: |
| 171 | return ELF::R_ARC_RELATIVE; |
| 172 | case ELF::EM_AVR: |
| 173 | break; |
| 174 | case ELF::EM_HEXAGON: |
| 175 | return ELF::R_HEX_RELATIVE; |
| 176 | case ELF::EM_LANAI: |
| 177 | break; |
| 178 | case ELF::EM_PPC: |
| 179 | break; |
| 180 | case ELF::EM_PPC64: |
| 181 | return ELF::R_PPC64_RELATIVE; |
| 182 | case ELF::EM_RISCV: |
| 183 | return ELF::R_RISCV_RELATIVE; |
| 184 | case ELF::EM_S390: |
| 185 | return ELF::R_390_RELATIVE; |
| 186 | case ELF::EM_SPARC: |
| 187 | case ELF::EM_SPARC32PLUS: |
| 188 | case ELF::EM_SPARCV9: |
| 189 | return ELF::R_SPARC_RELATIVE; |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 190 | case ELF::EM_AMDGPU: |
| 191 | break; |
| 192 | case ELF::EM_BPF: |
| 193 | break; |
| 194 | default: |
| 195 | break; |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 200 | StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) { |
| 201 | switch (Machine) { |
| 202 | case ELF::EM_ARM: |
| 203 | switch (Type) { |
| 204 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX); |
| 205 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP); |
| 206 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES); |
| 207 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY); |
| 208 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION); |
| 209 | } |
| 210 | break; |
| 211 | case ELF::EM_HEXAGON: |
| 212 | switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); } |
| 213 | break; |
| 214 | case ELF::EM_X86_64: |
| 215 | switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); } |
| 216 | break; |
| 217 | case ELF::EM_MIPS: |
| 218 | case ELF::EM_MIPS_RS3_LE: |
| 219 | switch (Type) { |
| 220 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO); |
| 221 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS); |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 222 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF); |
Fangrui Song | 61cd368 | 2019-02-21 10:19:08 +0000 | [diff] [blame] | 223 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS); |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 224 | } |
| 225 | break; |
| 226 | default: |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | switch (Type) { |
| 231 | STRINGIFY_ENUM_CASE(ELF, SHT_NULL); |
| 232 | STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS); |
| 233 | STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB); |
| 234 | STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB); |
| 235 | STRINGIFY_ENUM_CASE(ELF, SHT_RELA); |
| 236 | STRINGIFY_ENUM_CASE(ELF, SHT_HASH); |
| 237 | STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC); |
| 238 | STRINGIFY_ENUM_CASE(ELF, SHT_NOTE); |
| 239 | STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS); |
| 240 | STRINGIFY_ENUM_CASE(ELF, SHT_REL); |
| 241 | STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB); |
| 242 | STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM); |
| 243 | STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY); |
| 244 | STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY); |
| 245 | STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY); |
| 246 | STRINGIFY_ENUM_CASE(ELF, SHT_GROUP); |
| 247 | STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX); |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 248 | STRINGIFY_ENUM_CASE(ELF, SHT_RELR); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 249 | STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL); |
| 250 | STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA); |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 251 | STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR); |
Peter Collingbourne | f0e26e7 | 2017-06-14 18:52:12 +0000 | [diff] [blame] | 252 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB); |
Saleem Abdulrasool | b36fbbc | 2018-01-30 16:29:29 +0000 | [diff] [blame] | 253 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS); |
Michael J. Spencer | ae6eeae | 2018-06-02 16:33:01 +0000 | [diff] [blame] | 254 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE); |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 255 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG); |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 256 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES); |
| 257 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH); |
| 258 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef); |
| 259 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed); |
| 260 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym); |
| 261 | default: |
| 262 | return "Unknown"; |
| 263 | } |
| 264 | } |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 265 | |
| 266 | template <class ELFT> |
| 267 | Expected<std::vector<typename ELFT::Rela>> |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 268 | ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const { |
| 269 | // This function decodes the contents of an SHT_RELR packed relocation |
| 270 | // section. |
| 271 | // |
| 272 | // Proposal for adding SHT_RELR sections to generic-abi is here: |
| 273 | // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg |
| 274 | // |
| 275 | // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks |
| 276 | // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] |
| 277 | // |
| 278 | // i.e. start with an address, followed by any number of bitmaps. The address |
| 279 | // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 |
| 280 | // relocations each, at subsequent offsets following the last address entry. |
| 281 | // |
| 282 | // The bitmap entries must have 1 in the least significant bit. The assumption |
| 283 | // here is that an address cannot have 1 in lsb. Odd addresses are not |
| 284 | // supported. |
| 285 | // |
| 286 | // Excluding the least significant bit in the bitmap, each non-zero bit in |
| 287 | // the bitmap represents a relocation to be applied to a corresponding machine |
| 288 | // word that follows the base address word. The second least significant bit |
| 289 | // represents the machine word immediately following the initial address, and |
| 290 | // each bit that follows represents the next word, in linear order. As such, |
| 291 | // a single bitmap can encode up to 31 relocations in a 32-bit object, and |
| 292 | // 63 relocations in a 64-bit object. |
| 293 | // |
| 294 | // This encoding has a couple of interesting properties: |
| 295 | // 1. Looking at any entry, it is clear whether it's an address or a bitmap: |
| 296 | // even means address, odd means bitmap. |
| 297 | // 2. Just a simple list of addresses is a valid encoding. |
| 298 | |
| 299 | Elf_Rela Rela; |
| 300 | Rela.r_info = 0; |
| 301 | Rela.r_addend = 0; |
Fangrui Song | d2ed5be | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 302 | Rela.setType(getRelativeRelocationType(), false); |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 303 | std::vector<Elf_Rela> Relocs; |
| 304 | |
| 305 | // Word type: uint32_t for Elf32, and uint64_t for Elf64. |
| 306 | typedef typename ELFT::uint Word; |
| 307 | |
| 308 | // Word size in number of bytes. |
| 309 | const size_t WordSize = sizeof(Word); |
| 310 | |
| 311 | // Number of bits used for the relocation offsets bitmap. |
| 312 | // These many relative relocations can be encoded in a single entry. |
| 313 | const size_t NBits = 8*WordSize - 1; |
| 314 | |
| 315 | Word Base = 0; |
| 316 | for (const Elf_Relr &R : relrs) { |
| 317 | Word Entry = R; |
| 318 | if ((Entry&1) == 0) { |
| 319 | // Even entry: encodes the offset for next relocation. |
| 320 | Rela.r_offset = Entry; |
| 321 | Relocs.push_back(Rela); |
| 322 | // Set base offset for subsequent bitmap entries. |
| 323 | Base = Entry + WordSize; |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | // Odd entry: encodes bitmap for relocations starting at base. |
| 328 | Word Offset = Base; |
| 329 | while (Entry != 0) { |
| 330 | Entry >>= 1; |
| 331 | if ((Entry&1) != 0) { |
| 332 | Rela.r_offset = Offset; |
| 333 | Relocs.push_back(Rela); |
| 334 | } |
| 335 | Offset += WordSize; |
| 336 | } |
| 337 | |
| 338 | // Advance base offset by NBits words. |
| 339 | Base += NBits * WordSize; |
| 340 | } |
| 341 | |
| 342 | return Relocs; |
| 343 | } |
| 344 | |
| 345 | template <class ELFT> |
| 346 | Expected<std::vector<typename ELFT::Rela>> |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 347 | ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const { |
| 348 | // This function reads relocations in Android's packed relocation format, |
| 349 | // which is based on SLEB128 and delta encoding. |
| 350 | Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); |
| 351 | if (!ContentsOrErr) |
| 352 | return ContentsOrErr.takeError(); |
| 353 | const uint8_t *Cur = ContentsOrErr->begin(); |
| 354 | const uint8_t *End = ContentsOrErr->end(); |
| 355 | if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' || |
| 356 | Cur[2] != 'S' || Cur[3] != '2') |
| 357 | return createError("invalid packed relocation header"); |
| 358 | Cur += 4; |
| 359 | |
| 360 | const char *ErrStr = nullptr; |
| 361 | auto ReadSLEB = [&]() -> int64_t { |
| 362 | if (ErrStr) |
| 363 | return 0; |
| 364 | unsigned Len; |
| 365 | int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr); |
| 366 | Cur += Len; |
| 367 | return Result; |
| 368 | }; |
| 369 | |
| 370 | uint64_t NumRelocs = ReadSLEB(); |
| 371 | uint64_t Offset = ReadSLEB(); |
| 372 | uint64_t Addend = 0; |
| 373 | |
| 374 | if (ErrStr) |
| 375 | return createError(ErrStr); |
| 376 | |
| 377 | std::vector<Elf_Rela> Relocs; |
| 378 | Relocs.reserve(NumRelocs); |
| 379 | while (NumRelocs) { |
| 380 | uint64_t NumRelocsInGroup = ReadSLEB(); |
| 381 | if (NumRelocsInGroup > NumRelocs) |
| 382 | return createError("relocation group unexpectedly large"); |
| 383 | NumRelocs -= NumRelocsInGroup; |
| 384 | |
| 385 | uint64_t GroupFlags = ReadSLEB(); |
| 386 | bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG; |
| 387 | bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG; |
| 388 | bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG; |
| 389 | bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG; |
| 390 | |
| 391 | uint64_t GroupOffsetDelta; |
| 392 | if (GroupedByOffsetDelta) |
| 393 | GroupOffsetDelta = ReadSLEB(); |
| 394 | |
| 395 | uint64_t GroupRInfo; |
| 396 | if (GroupedByInfo) |
| 397 | GroupRInfo = ReadSLEB(); |
| 398 | |
| 399 | if (GroupedByAddend && GroupHasAddend) |
| 400 | Addend += ReadSLEB(); |
| 401 | |
Peter Collingbourne | 62e4fc4 | 2018-08-15 17:58:22 +0000 | [diff] [blame] | 402 | if (!GroupHasAddend) |
| 403 | Addend = 0; |
| 404 | |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 405 | for (uint64_t I = 0; I != NumRelocsInGroup; ++I) { |
| 406 | Elf_Rela R; |
| 407 | Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB(); |
| 408 | R.r_offset = Offset; |
| 409 | R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB(); |
Peter Collingbourne | 62e4fc4 | 2018-08-15 17:58:22 +0000 | [diff] [blame] | 410 | if (GroupHasAddend && !GroupedByAddend) |
| 411 | Addend += ReadSLEB(); |
| 412 | R.r_addend = Addend; |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 413 | Relocs.push_back(R); |
| 414 | |
| 415 | if (ErrStr) |
| 416 | return createError(ErrStr); |
| 417 | } |
| 418 | |
| 419 | if (ErrStr) |
| 420 | return createError(ErrStr); |
| 421 | } |
| 422 | |
| 423 | return Relocs; |
| 424 | } |
| 425 | |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 426 | template <class ELFT> |
| 427 | const char *ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch, |
| 428 | uint64_t Type) const { |
| 429 | #define DYNAMIC_STRINGIFY_ENUM(tag, value) \ |
| 430 | case value: \ |
| 431 | return #tag; |
| 432 | |
| 433 | #define DYNAMIC_TAG(n, v) |
| 434 | switch (Arch) { |
| 435 | case ELF::EM_HEXAGON: |
| 436 | switch (Type) { |
| 437 | #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 438 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 439 | #undef HEXAGON_DYNAMIC_TAG |
| 440 | } |
| 441 | |
| 442 | case ELF::EM_MIPS: |
| 443 | switch (Type) { |
| 444 | #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 445 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 446 | #undef MIPS_DYNAMIC_TAG |
| 447 | } |
| 448 | |
| 449 | case ELF::EM_PPC64: |
| 450 | switch (Type) { |
| 451 | #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 452 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 453 | #undef PPC64_DYNAMIC_TAG |
| 454 | } |
| 455 | } |
| 456 | #undef DYNAMIC_TAG |
| 457 | switch (Type) { |
| 458 | // Now handle all dynamic tags except the architecture specific ones |
| 459 | #define MIPS_DYNAMIC_TAG(name, value) |
| 460 | #define HEXAGON_DYNAMIC_TAG(name, value) |
| 461 | #define PPC64_DYNAMIC_TAG(name, value) |
| 462 | // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. |
| 463 | #define DYNAMIC_TAG_MARKER(name, value) |
| 464 | #define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 465 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 466 | #undef DYNAMIC_TAG |
| 467 | #undef MIPS_DYNAMIC_TAG |
| 468 | #undef HEXAGON_DYNAMIC_TAG |
| 469 | #undef PPC64_DYNAMIC_TAG |
| 470 | #undef DYNAMIC_TAG_MARKER |
| 471 | #undef DYNAMIC_STRINGIFY_ENUM |
| 472 | default: |
| 473 | return "unknown"; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | template <class ELFT> |
| 478 | const char *ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const { |
| 479 | return getDynamicTagAsString(getHeader()->e_machine, Type); |
| 480 | } |
| 481 | |
| 482 | template <class ELFT> |
| 483 | Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const { |
| 484 | ArrayRef<Elf_Dyn> Dyn; |
| 485 | size_t DynSecSize = 0; |
| 486 | |
| 487 | auto ProgramHeadersOrError = program_headers(); |
| 488 | if (!ProgramHeadersOrError) |
| 489 | return ProgramHeadersOrError.takeError(); |
| 490 | |
| 491 | for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) { |
| 492 | if (Phdr.p_type == ELF::PT_DYNAMIC) { |
| 493 | Dyn = makeArrayRef( |
| 494 | reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset), |
| 495 | Phdr.p_filesz / sizeof(Elf_Dyn)); |
| 496 | DynSecSize = Phdr.p_filesz; |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // If we can't find the dynamic section in the program headers, we just fall |
| 502 | // back on the sections. |
| 503 | if (Dyn.empty()) { |
| 504 | auto SectionsOrError = sections(); |
| 505 | if (!SectionsOrError) |
| 506 | return SectionsOrError.takeError(); |
| 507 | |
| 508 | for (const Elf_Shdr &Sec : *SectionsOrError) { |
| 509 | if (Sec.sh_type == ELF::SHT_DYNAMIC) { |
| 510 | Expected<ArrayRef<Elf_Dyn>> DynOrError = |
| 511 | getSectionContentsAsArray<Elf_Dyn>(&Sec); |
| 512 | if (!DynOrError) |
| 513 | return DynOrError.takeError(); |
| 514 | Dyn = *DynOrError; |
| 515 | DynSecSize = Sec.sh_size; |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if (!Dyn.data()) |
| 521 | return ArrayRef<Elf_Dyn>(); |
| 522 | } |
| 523 | |
| 524 | if (Dyn.empty()) |
| 525 | return createError("invalid empty dynamic section"); |
| 526 | |
| 527 | if (DynSecSize % sizeof(Elf_Dyn) != 0) |
| 528 | return createError("malformed dynamic section"); |
| 529 | |
| 530 | if (Dyn.back().d_tag != ELF::DT_NULL) |
| 531 | return createError("dynamic sections must be DT_NULL terminated"); |
| 532 | |
| 533 | return Dyn; |
| 534 | } |
| 535 | |
| 536 | template <class ELFT> |
| 537 | Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const { |
| 538 | auto ProgramHeadersOrError = program_headers(); |
| 539 | if (!ProgramHeadersOrError) |
| 540 | return ProgramHeadersOrError.takeError(); |
| 541 | |
| 542 | llvm::SmallVector<Elf_Phdr *, 4> LoadSegments; |
| 543 | |
| 544 | for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) |
| 545 | if (Phdr.p_type == ELF::PT_LOAD) |
| 546 | LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr)); |
| 547 | |
| 548 | const Elf_Phdr *const *I = |
| 549 | std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr, |
| 550 | [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) { |
| 551 | return VAddr < Phdr->p_vaddr; |
| 552 | }); |
| 553 | |
| 554 | if (I == LoadSegments.begin()) |
| 555 | return createError("Virtual address is not in any segment"); |
| 556 | --I; |
| 557 | const Elf_Phdr &Phdr = **I; |
| 558 | uint64_t Delta = VAddr - Phdr.p_vaddr; |
| 559 | if (Delta >= Phdr.p_filesz) |
| 560 | return createError("Virtual address is not in any segment"); |
| 561 | return base() + Phdr.p_offset + Delta; |
| 562 | } |
| 563 | |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 564 | template class llvm::object::ELFFile<ELF32LE>; |
| 565 | template class llvm::object::ELFFile<ELF32BE>; |
| 566 | template class llvm::object::ELFFile<ELF64LE>; |
| 567 | template class llvm::object::ELFFile<ELF64BE>; |