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; |
Kazushi (Jam) Marukawa | 5921782 | 2020-05-28 10:07:21 +0200 | [diff] [blame] | 148 | case ELF::EM_VE: |
| 149 | switch (Type) { |
| 150 | #include "llvm/BinaryFormat/ELFRelocs/VE.def" |
| 151 | default: |
| 152 | break; |
| 153 | } |
| 154 | break; |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 155 | default: |
| 156 | break; |
| 157 | } |
| 158 | return "Unknown"; |
| 159 | } |
| 160 | |
Tim Northover | 242785c | 2014-11-21 20:16:07 +0000 | [diff] [blame] | 161 | #undef ELF_RELOC |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 162 | |
Fangrui Song | d2ed5be | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 163 | uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) { |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 164 | switch (Machine) { |
| 165 | case ELF::EM_X86_64: |
| 166 | return ELF::R_X86_64_RELATIVE; |
| 167 | case ELF::EM_386: |
| 168 | case ELF::EM_IAMCU: |
| 169 | return ELF::R_386_RELATIVE; |
| 170 | case ELF::EM_MIPS: |
| 171 | break; |
| 172 | case ELF::EM_AARCH64: |
| 173 | return ELF::R_AARCH64_RELATIVE; |
| 174 | case ELF::EM_ARM: |
| 175 | return ELF::R_ARM_RELATIVE; |
| 176 | case ELF::EM_ARC_COMPACT: |
| 177 | case ELF::EM_ARC_COMPACT2: |
| 178 | return ELF::R_ARC_RELATIVE; |
| 179 | case ELF::EM_AVR: |
| 180 | break; |
| 181 | case ELF::EM_HEXAGON: |
| 182 | return ELF::R_HEX_RELATIVE; |
| 183 | case ELF::EM_LANAI: |
| 184 | break; |
| 185 | case ELF::EM_PPC: |
| 186 | break; |
| 187 | case ELF::EM_PPC64: |
| 188 | return ELF::R_PPC64_RELATIVE; |
| 189 | case ELF::EM_RISCV: |
| 190 | return ELF::R_RISCV_RELATIVE; |
| 191 | case ELF::EM_S390: |
| 192 | return ELF::R_390_RELATIVE; |
| 193 | case ELF::EM_SPARC: |
| 194 | case ELF::EM_SPARC32PLUS: |
| 195 | case ELF::EM_SPARCV9: |
| 196 | return ELF::R_SPARC_RELATIVE; |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 197 | case ELF::EM_AMDGPU: |
| 198 | break; |
| 199 | case ELF::EM_BPF: |
| 200 | break; |
| 201 | default: |
| 202 | break; |
| 203 | } |
| 204 | return 0; |
| 205 | } |
| 206 | |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 207 | StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) { |
| 208 | switch (Machine) { |
| 209 | case ELF::EM_ARM: |
| 210 | switch (Type) { |
| 211 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX); |
| 212 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP); |
| 213 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES); |
| 214 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY); |
| 215 | STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION); |
| 216 | } |
| 217 | break; |
| 218 | case ELF::EM_HEXAGON: |
| 219 | switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); } |
| 220 | break; |
| 221 | case ELF::EM_X86_64: |
| 222 | switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); } |
| 223 | break; |
| 224 | case ELF::EM_MIPS: |
| 225 | case ELF::EM_MIPS_RS3_LE: |
| 226 | switch (Type) { |
| 227 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO); |
| 228 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS); |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 229 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF); |
Fangrui Song | 61cd368 | 2019-02-21 10:19:08 +0000 | [diff] [blame] | 230 | STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS); |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 231 | } |
| 232 | break; |
Kai Wang | 581ba35 | 2020-02-04 22:20:10 +0800 | [diff] [blame] | 233 | case ELF::EM_RISCV: |
| 234 | switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); } |
| 235 | break; |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 236 | default: |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | switch (Type) { |
| 241 | STRINGIFY_ENUM_CASE(ELF, SHT_NULL); |
| 242 | STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS); |
| 243 | STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB); |
| 244 | STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB); |
| 245 | STRINGIFY_ENUM_CASE(ELF, SHT_RELA); |
| 246 | STRINGIFY_ENUM_CASE(ELF, SHT_HASH); |
| 247 | STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC); |
| 248 | STRINGIFY_ENUM_CASE(ELF, SHT_NOTE); |
| 249 | STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS); |
| 250 | STRINGIFY_ENUM_CASE(ELF, SHT_REL); |
| 251 | STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB); |
| 252 | STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM); |
| 253 | STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY); |
| 254 | STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY); |
| 255 | STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY); |
| 256 | STRINGIFY_ENUM_CASE(ELF, SHT_GROUP); |
| 257 | STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX); |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 258 | STRINGIFY_ENUM_CASE(ELF, SHT_RELR); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 259 | STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL); |
| 260 | STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA); |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 261 | STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR); |
Peter Collingbourne | f0e26e7 | 2017-06-14 18:52:12 +0000 | [diff] [blame] | 262 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB); |
Saleem Abdulrasool | b36fbbc | 2018-01-30 16:29:29 +0000 | [diff] [blame] | 263 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS); |
Michael J. Spencer | ae6eeae | 2018-06-02 16:33:01 +0000 | [diff] [blame] | 264 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE); |
Peter Collingbourne | 3e22733 | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 265 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG); |
Ben Dunbobbin | 1d16515 | 2019-05-17 03:44:15 +0000 | [diff] [blame] | 266 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES); |
Peter Collingbourne | 31fda09 | 2019-05-29 03:29:01 +0000 | [diff] [blame] | 267 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART); |
Fangrui Song | 9d2504b | 2019-09-06 00:53:28 +0000 | [diff] [blame] | 268 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR); |
| 269 | STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR); |
Rafael Espindola | 3ba2573 | 2017-05-02 14:04:52 +0000 | [diff] [blame] | 270 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES); |
| 271 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH); |
| 272 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef); |
| 273 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed); |
| 274 | STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym); |
| 275 | default: |
| 276 | return "Unknown"; |
| 277 | } |
| 278 | } |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 279 | |
| 280 | template <class ELFT> |
Georgii Rymar | 6ae5b9e | 2020-08-05 16:30:28 +0300 | [diff] [blame] | 281 | std::vector<typename ELFT::Rel> |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 282 | ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const { |
| 283 | // This function decodes the contents of an SHT_RELR packed relocation |
| 284 | // section. |
| 285 | // |
| 286 | // Proposal for adding SHT_RELR sections to generic-abi is here: |
| 287 | // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg |
| 288 | // |
| 289 | // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks |
| 290 | // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] |
| 291 | // |
| 292 | // i.e. start with an address, followed by any number of bitmaps. The address |
| 293 | // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 |
| 294 | // relocations each, at subsequent offsets following the last address entry. |
| 295 | // |
| 296 | // The bitmap entries must have 1 in the least significant bit. The assumption |
| 297 | // here is that an address cannot have 1 in lsb. Odd addresses are not |
| 298 | // supported. |
| 299 | // |
| 300 | // Excluding the least significant bit in the bitmap, each non-zero bit in |
| 301 | // the bitmap represents a relocation to be applied to a corresponding machine |
| 302 | // word that follows the base address word. The second least significant bit |
| 303 | // represents the machine word immediately following the initial address, and |
| 304 | // each bit that follows represents the next word, in linear order. As such, |
| 305 | // a single bitmap can encode up to 31 relocations in a 32-bit object, and |
| 306 | // 63 relocations in a 64-bit object. |
| 307 | // |
| 308 | // This encoding has a couple of interesting properties: |
| 309 | // 1. Looking at any entry, it is clear whether it's an address or a bitmap: |
| 310 | // even means address, odd means bitmap. |
| 311 | // 2. Just a simple list of addresses is a valid encoding. |
| 312 | |
Georgii Rymar | 2a4df6a | 2020-07-15 14:56:55 +0300 | [diff] [blame] | 313 | Elf_Rel Rel; |
| 314 | Rel.r_info = 0; |
| 315 | Rel.setType(getRelativeRelocationType(), false); |
| 316 | std::vector<Elf_Rel> Relocs; |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 317 | |
| 318 | // Word type: uint32_t for Elf32, and uint64_t for Elf64. |
| 319 | typedef typename ELFT::uint Word; |
| 320 | |
| 321 | // Word size in number of bytes. |
| 322 | const size_t WordSize = sizeof(Word); |
| 323 | |
| 324 | // Number of bits used for the relocation offsets bitmap. |
| 325 | // These many relative relocations can be encoded in a single entry. |
| 326 | const size_t NBits = 8*WordSize - 1; |
| 327 | |
| 328 | Word Base = 0; |
| 329 | for (const Elf_Relr &R : relrs) { |
| 330 | Word Entry = R; |
| 331 | if ((Entry&1) == 0) { |
| 332 | // Even entry: encodes the offset for next relocation. |
Georgii Rymar | 2a4df6a | 2020-07-15 14:56:55 +0300 | [diff] [blame] | 333 | Rel.r_offset = Entry; |
| 334 | Relocs.push_back(Rel); |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 335 | // Set base offset for subsequent bitmap entries. |
| 336 | Base = Entry + WordSize; |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | // Odd entry: encodes bitmap for relocations starting at base. |
| 341 | Word Offset = Base; |
| 342 | while (Entry != 0) { |
| 343 | Entry >>= 1; |
| 344 | if ((Entry&1) != 0) { |
Georgii Rymar | 2a4df6a | 2020-07-15 14:56:55 +0300 | [diff] [blame] | 345 | Rel.r_offset = Offset; |
| 346 | Relocs.push_back(Rel); |
Jake Ehrlich | 0f440d8 | 2018-06-28 21:07:34 +0000 | [diff] [blame] | 347 | } |
| 348 | Offset += WordSize; |
| 349 | } |
| 350 | |
| 351 | // Advance base offset by NBits words. |
| 352 | Base += NBits * WordSize; |
| 353 | } |
| 354 | |
| 355 | return Relocs; |
| 356 | } |
| 357 | |
| 358 | template <class ELFT> |
| 359 | Expected<std::vector<typename ELFT::Rela>> |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 360 | ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const { |
| 361 | // This function reads relocations in Android's packed relocation format, |
| 362 | // which is based on SLEB128 and delta encoding. |
| 363 | Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); |
| 364 | if (!ContentsOrErr) |
| 365 | return ContentsOrErr.takeError(); |
| 366 | const uint8_t *Cur = ContentsOrErr->begin(); |
| 367 | const uint8_t *End = ContentsOrErr->end(); |
| 368 | if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' || |
| 369 | Cur[2] != 'S' || Cur[3] != '2') |
| 370 | return createError("invalid packed relocation header"); |
| 371 | Cur += 4; |
| 372 | |
| 373 | const char *ErrStr = nullptr; |
| 374 | auto ReadSLEB = [&]() -> int64_t { |
| 375 | if (ErrStr) |
| 376 | return 0; |
| 377 | unsigned Len; |
| 378 | int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr); |
| 379 | Cur += Len; |
| 380 | return Result; |
| 381 | }; |
| 382 | |
| 383 | uint64_t NumRelocs = ReadSLEB(); |
| 384 | uint64_t Offset = ReadSLEB(); |
| 385 | uint64_t Addend = 0; |
| 386 | |
| 387 | if (ErrStr) |
| 388 | return createError(ErrStr); |
| 389 | |
| 390 | std::vector<Elf_Rela> Relocs; |
| 391 | Relocs.reserve(NumRelocs); |
| 392 | while (NumRelocs) { |
| 393 | uint64_t NumRelocsInGroup = ReadSLEB(); |
| 394 | if (NumRelocsInGroup > NumRelocs) |
| 395 | return createError("relocation group unexpectedly large"); |
| 396 | NumRelocs -= NumRelocsInGroup; |
| 397 | |
| 398 | uint64_t GroupFlags = ReadSLEB(); |
| 399 | bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG; |
| 400 | bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG; |
| 401 | bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG; |
| 402 | bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG; |
| 403 | |
| 404 | uint64_t GroupOffsetDelta; |
| 405 | if (GroupedByOffsetDelta) |
| 406 | GroupOffsetDelta = ReadSLEB(); |
| 407 | |
| 408 | uint64_t GroupRInfo; |
| 409 | if (GroupedByInfo) |
| 410 | GroupRInfo = ReadSLEB(); |
| 411 | |
| 412 | if (GroupedByAddend && GroupHasAddend) |
| 413 | Addend += ReadSLEB(); |
| 414 | |
Peter Collingbourne | 62e4fc4 | 2018-08-15 17:58:22 +0000 | [diff] [blame] | 415 | if (!GroupHasAddend) |
| 416 | Addend = 0; |
| 417 | |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 418 | for (uint64_t I = 0; I != NumRelocsInGroup; ++I) { |
| 419 | Elf_Rela R; |
| 420 | Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB(); |
| 421 | R.r_offset = Offset; |
| 422 | R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB(); |
Peter Collingbourne | 62e4fc4 | 2018-08-15 17:58:22 +0000 | [diff] [blame] | 423 | if (GroupHasAddend && !GroupedByAddend) |
| 424 | Addend += ReadSLEB(); |
| 425 | R.r_addend = Addend; |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 426 | Relocs.push_back(R); |
| 427 | |
| 428 | if (ErrStr) |
| 429 | return createError(ErrStr); |
| 430 | } |
| 431 | |
| 432 | if (ErrStr) |
| 433 | return createError(ErrStr); |
| 434 | } |
| 435 | |
| 436 | return Relocs; |
| 437 | } |
| 438 | |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 439 | template <class ELFT> |
Xing GUO | b285878 | 2019-03-02 04:20:28 +0000 | [diff] [blame] | 440 | std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch, |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 441 | uint64_t Type) const { |
| 442 | #define DYNAMIC_STRINGIFY_ENUM(tag, value) \ |
| 443 | case value: \ |
| 444 | return #tag; |
| 445 | |
| 446 | #define DYNAMIC_TAG(n, v) |
| 447 | switch (Arch) { |
Peter Smith | 49d7221 | 2019-06-04 11:44:33 +0000 | [diff] [blame] | 448 | case ELF::EM_AARCH64: |
| 449 | switch (Type) { |
| 450 | #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 451 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 452 | #undef AARCH64_DYNAMIC_TAG |
| 453 | } |
| 454 | break; |
| 455 | |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 456 | case ELF::EM_HEXAGON: |
| 457 | switch (Type) { |
| 458 | #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 459 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 460 | #undef HEXAGON_DYNAMIC_TAG |
| 461 | } |
Jonas Hahnfeld | c64d73c | 2019-03-13 10:38:17 +0000 | [diff] [blame] | 462 | break; |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 463 | |
| 464 | case ELF::EM_MIPS: |
| 465 | switch (Type) { |
| 466 | #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 467 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 468 | #undef MIPS_DYNAMIC_TAG |
| 469 | } |
Jonas Hahnfeld | c64d73c | 2019-03-13 10:38:17 +0000 | [diff] [blame] | 470 | break; |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 471 | |
| 472 | case ELF::EM_PPC64: |
| 473 | switch (Type) { |
| 474 | #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) |
| 475 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 476 | #undef PPC64_DYNAMIC_TAG |
| 477 | } |
Jonas Hahnfeld | c64d73c | 2019-03-13 10:38:17 +0000 | [diff] [blame] | 478 | break; |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 479 | } |
| 480 | #undef DYNAMIC_TAG |
| 481 | switch (Type) { |
| 482 | // Now handle all dynamic tags except the architecture specific ones |
Peter Smith | 49d7221 | 2019-06-04 11:44:33 +0000 | [diff] [blame] | 483 | #define AARCH64_DYNAMIC_TAG(name, value) |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 484 | #define MIPS_DYNAMIC_TAG(name, value) |
| 485 | #define HEXAGON_DYNAMIC_TAG(name, value) |
| 486 | #define PPC64_DYNAMIC_TAG(name, value) |
| 487 | // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. |
| 488 | #define DYNAMIC_TAG_MARKER(name, value) |
Georgii Rymar | 301cb91 | 2019-12-23 14:54:36 +0300 | [diff] [blame] | 489 | #define DYNAMIC_TAG(name, value) case value: return #name; |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 490 | #include "llvm/BinaryFormat/DynamicTags.def" |
| 491 | #undef DYNAMIC_TAG |
Peter Smith | 49d7221 | 2019-06-04 11:44:33 +0000 | [diff] [blame] | 492 | #undef AARCH64_DYNAMIC_TAG |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 493 | #undef MIPS_DYNAMIC_TAG |
| 494 | #undef HEXAGON_DYNAMIC_TAG |
| 495 | #undef PPC64_DYNAMIC_TAG |
| 496 | #undef DYNAMIC_TAG_MARKER |
| 497 | #undef DYNAMIC_STRINGIFY_ENUM |
| 498 | default: |
Xing GUO | b285878 | 2019-03-02 04:20:28 +0000 | [diff] [blame] | 499 | return "<unknown:>0x" + utohexstr(Type, true); |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | template <class ELFT> |
Xing GUO | b285878 | 2019-03-02 04:20:28 +0000 | [diff] [blame] | 504 | std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const { |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 505 | return getDynamicTagAsString(getHeader()->e_machine, Type); |
| 506 | } |
| 507 | |
| 508 | template <class ELFT> |
| 509 | Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const { |
| 510 | ArrayRef<Elf_Dyn> Dyn; |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 511 | |
| 512 | auto ProgramHeadersOrError = program_headers(); |
| 513 | if (!ProgramHeadersOrError) |
| 514 | return ProgramHeadersOrError.takeError(); |
| 515 | |
| 516 | for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) { |
| 517 | if (Phdr.p_type == ELF::PT_DYNAMIC) { |
| 518 | Dyn = makeArrayRef( |
| 519 | reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset), |
| 520 | Phdr.p_filesz / sizeof(Elf_Dyn)); |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // If we can't find the dynamic section in the program headers, we just fall |
| 526 | // back on the sections. |
| 527 | if (Dyn.empty()) { |
| 528 | auto SectionsOrError = sections(); |
| 529 | if (!SectionsOrError) |
| 530 | return SectionsOrError.takeError(); |
| 531 | |
| 532 | for (const Elf_Shdr &Sec : *SectionsOrError) { |
| 533 | if (Sec.sh_type == ELF::SHT_DYNAMIC) { |
| 534 | Expected<ArrayRef<Elf_Dyn>> DynOrError = |
| 535 | getSectionContentsAsArray<Elf_Dyn>(&Sec); |
| 536 | if (!DynOrError) |
| 537 | return DynOrError.takeError(); |
| 538 | Dyn = *DynOrError; |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (!Dyn.data()) |
| 544 | return ArrayRef<Elf_Dyn>(); |
| 545 | } |
| 546 | |
| 547 | if (Dyn.empty()) |
George Rimar | d0921a4 | 2019-07-05 11:28:49 +0000 | [diff] [blame] | 548 | // TODO: this error is untested. |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 549 | return createError("invalid empty dynamic section"); |
| 550 | |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 551 | if (Dyn.back().d_tag != ELF::DT_NULL) |
George Rimar | d0921a4 | 2019-07-05 11:28:49 +0000 | [diff] [blame] | 552 | // TODO: this error is untested. |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 553 | return createError("dynamic sections must be DT_NULL terminated"); |
| 554 | |
| 555 | return Dyn; |
| 556 | } |
| 557 | |
| 558 | template <class ELFT> |
| 559 | Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const { |
| 560 | auto ProgramHeadersOrError = program_headers(); |
| 561 | if (!ProgramHeadersOrError) |
| 562 | return ProgramHeadersOrError.takeError(); |
| 563 | |
| 564 | llvm::SmallVector<Elf_Phdr *, 4> LoadSegments; |
| 565 | |
| 566 | for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) |
| 567 | if (Phdr.p_type == ELF::PT_LOAD) |
| 568 | LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr)); |
| 569 | |
| 570 | const Elf_Phdr *const *I = |
| 571 | std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr, |
| 572 | [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) { |
| 573 | return VAddr < Phdr->p_vaddr; |
| 574 | }); |
| 575 | |
| 576 | if (I == LoadSegments.begin()) |
George Rimar | d0921a4 | 2019-07-05 11:28:49 +0000 | [diff] [blame] | 577 | return createError("virtual address is not in any segment: 0x" + |
| 578 | Twine::utohexstr(VAddr)); |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 579 | --I; |
| 580 | const Elf_Phdr &Phdr = **I; |
| 581 | uint64_t Delta = VAddr - Phdr.p_vaddr; |
| 582 | if (Delta >= Phdr.p_filesz) |
George Rimar | d0921a4 | 2019-07-05 11:28:49 +0000 | [diff] [blame] | 583 | return createError("virtual address is not in any segment: 0x" + |
| 584 | Twine::utohexstr(VAddr)); |
Georgii Rymar | 30c1f9a | 2020-03-24 17:26:52 +0300 | [diff] [blame] | 585 | |
| 586 | uint64_t Offset = Phdr.p_offset + Delta; |
| 587 | if (Offset >= getBufSize()) |
| 588 | return createError("can't map virtual address 0x" + |
| 589 | Twine::utohexstr(VAddr) + " to the segment with index " + |
| 590 | Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) + |
| 591 | ": the segment ends at 0x" + |
| 592 | Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) + |
| 593 | ", which is greater than the file size (0x" + |
| 594 | Twine::utohexstr(getBufSize()) + ")"); |
| 595 | |
| 596 | return base() + Offset; |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Peter Collingbourne | 689e6c05 | 2017-10-25 03:37:12 +0000 | [diff] [blame] | 599 | template class llvm::object::ELFFile<ELF32LE>; |
| 600 | template class llvm::object::ELFFile<ELF32BE>; |
| 601 | template class llvm::object::ELFFile<ELF64LE>; |
| 602 | template class llvm::object::ELFFile<ELF64BE>; |