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