Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 10 | /// This file declares classes for handling the YAML representation |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 11 | /// of ELF. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_OBJECTYAML_ELFYAML_H |
| 16 | #define LLVM_OBJECTYAML_ELFYAML_H |
| 17 | |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ObjectYAML/YAML.h" |
| 20 | #include "llvm/Support/YAMLTraits.h" |
| 21 | #include <cstdint> |
| 22 | #include <memory> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace llvm { |
| 26 | namespace ELFYAML { |
| 27 | |
| 28 | // These types are invariant across 32/64-bit ELF, so for simplicity just |
| 29 | // directly give them their exact sizes. We don't need to worry about |
| 30 | // endianness because these are just the types in the YAMLIO structures, |
| 31 | // and are appropriately converted to the necessary endianness when |
| 32 | // reading/generating binary object files. |
| 33 | // The naming of these types is intended to be ELF_PREFIX, where PREFIX is |
| 34 | // the common prefix of the respective constants. E.g. ELF_EM corresponds |
| 35 | // to the `e_machine` constants, like `EM_X86_64`. |
| 36 | // In the future, these would probably be better suited by C++11 enum |
| 37 | // class's with appropriate fixed underlying type. |
| 38 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET) |
| 39 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PT) |
| 40 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM) |
| 41 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS) |
| 42 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA) |
| 43 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI) |
| 44 | // Just use 64, since it can hold 32-bit values too. |
| 45 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF) |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 46 | // Just use 64, since it can hold 32-bit values too. |
| 47 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_DYNTAG) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF) |
| 49 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT) |
| 50 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL) |
| 51 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS) |
| 52 | // Just use 64, since it can hold 32-bit values too. |
| 53 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF) |
| 54 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN) |
| 55 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT) |
| 56 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV) |
| 57 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO) |
| 58 | |
| 59 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG) |
| 60 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP) |
| 61 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT) |
| 62 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE) |
| 63 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1) |
| 64 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA) |
| 65 | |
| 66 | // For now, hardcode 64 bits everywhere that 32 or 64 would be needed |
| 67 | // since 64-bit can hold 32-bit values too. |
| 68 | struct FileHeader { |
| 69 | ELF_ELFCLASS Class; |
| 70 | ELF_ELFDATA Data; |
| 71 | ELF_ELFOSABI OSABI; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 72 | llvm::yaml::Hex8 ABIVersion; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | ELF_ET Type; |
| 74 | ELF_EM Machine; |
| 75 | ELF_EF Flags; |
| 76 | llvm::yaml::Hex64 Entry; |
| 77 | }; |
| 78 | |
| 79 | struct SectionName { |
| 80 | StringRef Section; |
| 81 | }; |
| 82 | |
| 83 | struct ProgramHeader { |
| 84 | ELF_PT Type; |
| 85 | ELF_PF Flags; |
| 86 | llvm::yaml::Hex64 VAddr; |
| 87 | llvm::yaml::Hex64 PAddr; |
| 88 | Optional<llvm::yaml::Hex64> Align; |
| 89 | std::vector<SectionName> Sections; |
| 90 | }; |
| 91 | |
| 92 | struct Symbol { |
| 93 | StringRef Name; |
| 94 | ELF_STT Type; |
| 95 | StringRef Section; |
| 96 | Optional<ELF_SHN> Index; |
| 97 | llvm::yaml::Hex64 Value; |
| 98 | llvm::yaml::Hex64 Size; |
| 99 | uint8_t Other; |
| 100 | }; |
| 101 | |
| 102 | struct LocalGlobalWeakSymbols { |
| 103 | std::vector<Symbol> Local; |
| 104 | std::vector<Symbol> Global; |
| 105 | std::vector<Symbol> Weak; |
| 106 | }; |
| 107 | |
| 108 | struct SectionOrType { |
| 109 | StringRef sectionNameOrType; |
| 110 | }; |
| 111 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 112 | struct DynamicEntry { |
| 113 | ELF_DYNTAG Tag; |
| 114 | llvm::yaml::Hex64 Val; |
| 115 | }; |
| 116 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 117 | struct Section { |
| 118 | enum class SectionKind { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 119 | Dynamic, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 120 | Group, |
| 121 | RawContent, |
| 122 | Relocation, |
| 123 | NoBits, |
| 124 | MipsABIFlags |
| 125 | }; |
| 126 | SectionKind Kind; |
| 127 | StringRef Name; |
| 128 | ELF_SHT Type; |
| 129 | ELF_SHF Flags; |
| 130 | llvm::yaml::Hex64 Address; |
| 131 | StringRef Link; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 132 | llvm::yaml::Hex64 AddressAlign; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 133 | Optional<llvm::yaml::Hex64> EntSize; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 134 | |
| 135 | Section(SectionKind Kind) : Kind(Kind) {} |
| 136 | virtual ~Section(); |
| 137 | }; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 138 | |
| 139 | struct DynamicSection : Section { |
| 140 | std::vector<DynamicEntry> Entries; |
| 141 | |
| 142 | DynamicSection() : Section(SectionKind::Dynamic) {} |
| 143 | |
| 144 | static bool classof(const Section *S) { |
| 145 | return S->Kind == SectionKind::Dynamic; |
| 146 | } |
| 147 | }; |
| 148 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 149 | struct RawContentSection : Section { |
| 150 | yaml::BinaryRef Content; |
| 151 | llvm::yaml::Hex64 Size; |
| 152 | |
| 153 | RawContentSection() : Section(SectionKind::RawContent) {} |
| 154 | |
| 155 | static bool classof(const Section *S) { |
| 156 | return S->Kind == SectionKind::RawContent; |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | struct NoBitsSection : Section { |
| 161 | llvm::yaml::Hex64 Size; |
| 162 | |
| 163 | NoBitsSection() : Section(SectionKind::NoBits) {} |
| 164 | |
| 165 | static bool classof(const Section *S) { |
| 166 | return S->Kind == SectionKind::NoBits; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | struct Group : Section { |
| 171 | // Members of a group contain a flag and a list of section indices |
| 172 | // that are part of the group. |
| 173 | std::vector<SectionOrType> Members; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 174 | StringRef Signature; /* Info */ |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 175 | |
| 176 | Group() : Section(SectionKind::Group) {} |
| 177 | |
| 178 | static bool classof(const Section *S) { |
| 179 | return S->Kind == SectionKind::Group; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | struct Relocation { |
| 184 | llvm::yaml::Hex64 Offset; |
| 185 | int64_t Addend; |
| 186 | ELF_REL Type; |
| 187 | Optional<StringRef> Symbol; |
| 188 | }; |
| 189 | |
| 190 | struct RelocationSection : Section { |
| 191 | std::vector<Relocation> Relocations; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 192 | StringRef RelocatableSec; /* Info */ |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 193 | |
| 194 | RelocationSection() : Section(SectionKind::Relocation) {} |
| 195 | |
| 196 | static bool classof(const Section *S) { |
| 197 | return S->Kind == SectionKind::Relocation; |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | // Represents .MIPS.abiflags section |
| 202 | struct MipsABIFlags : Section { |
| 203 | llvm::yaml::Hex16 Version; |
| 204 | MIPS_ISA ISALevel; |
| 205 | llvm::yaml::Hex8 ISARevision; |
| 206 | MIPS_AFL_REG GPRSize; |
| 207 | MIPS_AFL_REG CPR1Size; |
| 208 | MIPS_AFL_REG CPR2Size; |
| 209 | MIPS_ABI_FP FpABI; |
| 210 | MIPS_AFL_EXT ISAExtension; |
| 211 | MIPS_AFL_ASE ASEs; |
| 212 | MIPS_AFL_FLAGS1 Flags1; |
| 213 | llvm::yaml::Hex32 Flags2; |
| 214 | |
| 215 | MipsABIFlags() : Section(SectionKind::MipsABIFlags) {} |
| 216 | |
| 217 | static bool classof(const Section *S) { |
| 218 | return S->Kind == SectionKind::MipsABIFlags; |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | struct Object { |
| 223 | FileHeader Header; |
| 224 | std::vector<ProgramHeader> ProgramHeaders; |
| 225 | std::vector<std::unique_ptr<Section>> Sections; |
| 226 | // Although in reality the symbols reside in a section, it is a lot |
| 227 | // cleaner and nicer if we read them from the YAML as a separate |
| 228 | // top-level key, which automatically ensures that invariants like there |
| 229 | // being a single SHT_SYMTAB section are upheld. |
| 230 | LocalGlobalWeakSymbols Symbols; |
| 231 | LocalGlobalWeakSymbols DynamicSymbols; |
| 232 | }; |
| 233 | |
| 234 | } // end namespace ELFYAML |
| 235 | } // end namespace llvm |
| 236 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 237 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 238 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader) |
| 239 | LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>) |
| 240 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol) |
| 241 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation) |
| 242 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType) |
| 243 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionName) |
| 244 | |
| 245 | namespace llvm { |
| 246 | namespace yaml { |
| 247 | |
| 248 | template <> |
| 249 | struct ScalarEnumerationTraits<ELFYAML::ELF_ET> { |
| 250 | static void enumeration(IO &IO, ELFYAML::ELF_ET &Value); |
| 251 | }; |
| 252 | |
| 253 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> { |
| 254 | static void enumeration(IO &IO, ELFYAML::ELF_PT &Value); |
| 255 | }; |
| 256 | |
| 257 | template <> |
| 258 | struct ScalarEnumerationTraits<ELFYAML::ELF_EM> { |
| 259 | static void enumeration(IO &IO, ELFYAML::ELF_EM &Value); |
| 260 | }; |
| 261 | |
| 262 | template <> |
| 263 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> { |
| 264 | static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value); |
| 265 | }; |
| 266 | |
| 267 | template <> |
| 268 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> { |
| 269 | static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value); |
| 270 | }; |
| 271 | |
| 272 | template <> |
| 273 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> { |
| 274 | static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value); |
| 275 | }; |
| 276 | |
| 277 | template <> |
| 278 | struct ScalarBitSetTraits<ELFYAML::ELF_EF> { |
| 279 | static void bitset(IO &IO, ELFYAML::ELF_EF &Value); |
| 280 | }; |
| 281 | |
| 282 | template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> { |
| 283 | static void bitset(IO &IO, ELFYAML::ELF_PF &Value); |
| 284 | }; |
| 285 | |
| 286 | template <> |
| 287 | struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> { |
| 288 | static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value); |
| 289 | }; |
| 290 | |
| 291 | template <> |
| 292 | struct ScalarBitSetTraits<ELFYAML::ELF_SHF> { |
| 293 | static void bitset(IO &IO, ELFYAML::ELF_SHF &Value); |
| 294 | }; |
| 295 | |
| 296 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> { |
| 297 | static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value); |
| 298 | }; |
| 299 | |
| 300 | template <> |
| 301 | struct ScalarEnumerationTraits<ELFYAML::ELF_STT> { |
| 302 | static void enumeration(IO &IO, ELFYAML::ELF_STT &Value); |
| 303 | }; |
| 304 | |
| 305 | template <> |
| 306 | struct ScalarEnumerationTraits<ELFYAML::ELF_STV> { |
| 307 | static void enumeration(IO &IO, ELFYAML::ELF_STV &Value); |
| 308 | }; |
| 309 | |
| 310 | template <> |
| 311 | struct ScalarBitSetTraits<ELFYAML::ELF_STO> { |
| 312 | static void bitset(IO &IO, ELFYAML::ELF_STO &Value); |
| 313 | }; |
| 314 | |
| 315 | template <> |
| 316 | struct ScalarEnumerationTraits<ELFYAML::ELF_REL> { |
| 317 | static void enumeration(IO &IO, ELFYAML::ELF_REL &Value); |
| 318 | }; |
| 319 | |
| 320 | template <> |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 321 | struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> { |
| 322 | static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value); |
| 323 | }; |
| 324 | |
| 325 | template <> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 326 | struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> { |
| 327 | static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value); |
| 328 | }; |
| 329 | |
| 330 | template <> |
| 331 | struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> { |
| 332 | static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value); |
| 333 | }; |
| 334 | |
| 335 | template <> |
| 336 | struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> { |
| 337 | static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value); |
| 338 | }; |
| 339 | |
| 340 | template <> |
| 341 | struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> { |
| 342 | static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value); |
| 343 | }; |
| 344 | |
| 345 | template <> |
| 346 | struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> { |
| 347 | static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value); |
| 348 | }; |
| 349 | |
| 350 | template <> |
| 351 | struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> { |
| 352 | static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value); |
| 353 | }; |
| 354 | |
| 355 | template <> |
| 356 | struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> { |
| 357 | static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value); |
| 358 | }; |
| 359 | |
| 360 | template <> |
| 361 | struct MappingTraits<ELFYAML::FileHeader> { |
| 362 | static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr); |
| 363 | }; |
| 364 | |
| 365 | template <> struct MappingTraits<ELFYAML::ProgramHeader> { |
| 366 | static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr); |
| 367 | }; |
| 368 | |
| 369 | template <> |
| 370 | struct MappingTraits<ELFYAML::Symbol> { |
| 371 | static void mapping(IO &IO, ELFYAML::Symbol &Symbol); |
| 372 | static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol); |
| 373 | }; |
| 374 | |
| 375 | template <> |
| 376 | struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> { |
| 377 | static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols); |
| 378 | }; |
| 379 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 380 | template <> struct MappingTraits<ELFYAML::DynamicEntry> { |
| 381 | static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel); |
| 382 | }; |
| 383 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 384 | template <> struct MappingTraits<ELFYAML::Relocation> { |
| 385 | static void mapping(IO &IO, ELFYAML::Relocation &Rel); |
| 386 | }; |
| 387 | |
| 388 | template <> |
| 389 | struct MappingTraits<std::unique_ptr<ELFYAML::Section>> { |
| 390 | static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section); |
| 391 | static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section); |
| 392 | }; |
| 393 | |
| 394 | template <> |
| 395 | struct MappingTraits<ELFYAML::Object> { |
| 396 | static void mapping(IO &IO, ELFYAML::Object &Object); |
| 397 | }; |
| 398 | |
| 399 | template <> struct MappingTraits<ELFYAML::SectionOrType> { |
| 400 | static void mapping(IO &IO, ELFYAML::SectionOrType §ionOrType); |
| 401 | }; |
| 402 | |
| 403 | template <> struct MappingTraits<ELFYAML::SectionName> { |
| 404 | static void mapping(IO &IO, ELFYAML::SectionName §ionName); |
| 405 | }; |
| 406 | |
| 407 | } // end namespace yaml |
| 408 | } // end namespace llvm |
| 409 | |
| 410 | #endif // LLVM_OBJECTYAML_ELFYAML_H |