| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Target-specific ELF type traits. | 5 // Target-specific ELF type traits. |
| 6 | 6 |
| 7 #ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_ | 7 #ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_ |
| 8 #define TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_ | 8 #define TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_ |
| 9 | 9 |
| 10 #include "elf.h" | 10 #include "elf.h" |
| 11 #include "libelf.h" | 11 #include "libelf.h" |
| 12 | 12 |
| 13 // The TARGET_ macro controls which Elf types we expect and handle. | 13 // ELF is a traits structure used to provide convenient aliases for |
| 14 // Either TARGET_ARM or TARGET_ARM64 must be defined, but not both. | 14 // 32/64 bit Elf types and functions, depending on the target file. |
| 15 | 15 |
| 16 #if !defined(TARGET_ARM) && !defined(TARGET_ARM64) | 16 struct ELF32_traits { |
| 17 # error "Unsupported target, define one of TARGET_ARM or TARGET_ARM64" | |
| 18 #elif defined(TARGET_ARM) && defined(TARGET_ARM64) | |
| 19 # error "Define one of TARGET_ARM or TARGET_ARM64, but not both" | |
| 20 #endif | |
| 21 | |
| 22 // TODO(simonb): Eliminate these once AARCH64 appears reliably in elf.h. | |
| 23 #ifndef EM_AARCH64 | |
| 24 #define EM_AARCH64 183 | |
| 25 #endif | |
| 26 #ifndef R_AARCH64_RELATIVE | |
| 27 #define R_AARCH64_RELATIVE 1027 | |
| 28 #endif | |
| 29 #ifndef R_AARCH64_NONE | |
| 30 #define R_AARCH64_NONE 0 | |
| 31 #endif | |
| 32 | |
| 33 // ELF is a traits structure used to provide convenient aliases for | |
| 34 // 32/64 bit Elf types and functions, depending on the target specified. | |
| 35 | |
| 36 #if defined(TARGET_ARM) | |
| 37 struct ELF { | |
| 38 typedef Elf32_Addr Addr; | 17 typedef Elf32_Addr Addr; |
| 39 typedef Elf32_Dyn Dyn; | 18 typedef Elf32_Dyn Dyn; |
| 40 typedef Elf32_Ehdr Ehdr; | 19 typedef Elf32_Ehdr Ehdr; |
| 41 typedef Elf32_Off Off; | 20 typedef Elf32_Off Off; |
| 42 typedef Elf32_Phdr Phdr; | 21 typedef Elf32_Phdr Phdr; |
| 43 typedef Elf32_Rel Rel; | 22 typedef Elf32_Rel Rel; |
| 44 typedef Elf32_Rela Rela; | 23 typedef Elf32_Rela Rela; |
| 45 typedef Elf32_Shdr Shdr; | 24 typedef Elf32_Shdr Shdr; |
| 46 typedef Elf32_Sword Sword; | 25 typedef Elf32_Sword Sword; |
| 47 typedef Elf32_Sxword Sxword; | 26 typedef Elf32_Sxword Sxword; |
| 48 typedef Elf32_Sym Sym; | 27 typedef Elf32_Sym Sym; |
| 49 typedef Elf32_Word Word; | 28 typedef Elf32_Word Word; |
| 50 typedef Elf32_Xword Xword; | 29 typedef Elf32_Xword Xword; |
| 30 typedef Elf32_Half Half; |
| 51 | 31 |
| 52 static inline Ehdr* getehdr(Elf* elf) { return elf32_getehdr(elf); } | 32 static inline Ehdr* getehdr(Elf* elf) { return elf32_getehdr(elf); } |
| 53 static inline Phdr* getphdr(Elf* elf) { return elf32_getphdr(elf); } | 33 static inline Phdr* getphdr(Elf* elf) { return elf32_getphdr(elf); } |
| 54 static inline Shdr* getshdr(Elf_Scn* scn) { return elf32_getshdr(scn); } | 34 static inline Shdr* getshdr(Elf_Scn* scn) { return elf32_getshdr(scn); } |
| 55 | 35 static inline Word elf_r_type(Word info) { return ELF32_R_TYPE(info); } |
| 56 enum { kMachine = EM_ARM }; | 36 static inline int elf_st_type(uint8_t info) { return ELF32_ST_TYPE(info); } |
| 57 enum { kFileClass = ELFCLASS32 }; | 37 static inline Word elf_r_sym(Word info) { return ELF32_R_SYM(info); } |
| 58 enum { kRelativeRelocationCode = R_ARM_RELATIVE }; | |
| 59 enum { kNoRelocationCode = R_ARM_NONE }; | |
| 60 | |
| 61 static inline const char* Machine() { return "ARM"; } | |
| 62 | |
| 63 # define ELF_R_SYM(val) ELF32_R_SYM(val) | |
| 64 # define ELF_R_TYPE(val) ELF32_R_TYPE(val) | |
| 65 # define ELF_R_INFO(sym, type) ELF32_R_INFO(sym, type) | |
| 66 # define ELF_ST_TYPE(val) ELF32_ST_TYPE(val) | |
| 67 }; | 38 }; |
| 68 | 39 |
| 69 #elif defined(TARGET_ARM64) | 40 struct ELF64_traits { |
| 70 struct ELF { | |
| 71 typedef Elf64_Addr Addr; | 41 typedef Elf64_Addr Addr; |
| 72 typedef Elf64_Dyn Dyn; | 42 typedef Elf64_Dyn Dyn; |
| 73 typedef Elf64_Ehdr Ehdr; | 43 typedef Elf64_Ehdr Ehdr; |
| 74 typedef Elf64_Off Off; | 44 typedef Elf64_Off Off; |
| 75 typedef Elf64_Phdr Phdr; | 45 typedef Elf64_Phdr Phdr; |
| 76 typedef Elf64_Rel Rel; | 46 typedef Elf64_Rel Rel; |
| 77 typedef Elf64_Rela Rela; | 47 typedef Elf64_Rela Rela; |
| 78 typedef Elf64_Shdr Shdr; | 48 typedef Elf64_Shdr Shdr; |
| 79 typedef Elf64_Sword Sword; | 49 typedef Elf64_Sword Sword; |
| 80 typedef Elf64_Sxword Sxword; | 50 typedef Elf64_Sxword Sxword; |
| 81 typedef Elf64_Sym Sym; | 51 typedef Elf64_Sym Sym; |
| 82 typedef Elf64_Word Word; | 52 typedef Elf64_Word Word; |
| 83 typedef Elf64_Xword Xword; | 53 typedef Elf64_Xword Xword; |
| 54 typedef Elf64_Half Half; |
| 84 | 55 |
| 85 static inline Ehdr* getehdr(Elf* elf) { return elf64_getehdr(elf); } | 56 static inline Ehdr* getehdr(Elf* elf) { return elf64_getehdr(elf); } |
| 86 static inline Phdr* getphdr(Elf* elf) { return elf64_getphdr(elf); } | 57 static inline Phdr* getphdr(Elf* elf) { return elf64_getphdr(elf); } |
| 87 static inline Shdr* getshdr(Elf_Scn* scn) { return elf64_getshdr(scn); } | 58 static inline Shdr* getshdr(Elf_Scn* scn) { return elf64_getshdr(scn); } |
| 88 | 59 static inline Xword elf_r_type(Xword info) { return ELF64_R_TYPE(info); } |
| 89 enum { kMachine = EM_AARCH64 }; | 60 static inline int elf_st_type(uint8_t info) { return ELF64_ST_TYPE(info); } |
| 90 enum { kFileClass = ELFCLASS64 }; | 61 static inline Word elf_r_sym(Xword info) { return ELF64_R_SYM(info); } |
| 91 enum { kRelativeRelocationCode = R_AARCH64_RELATIVE }; | |
| 92 enum { kNoRelocationCode = R_AARCH64_NONE }; | |
| 93 | |
| 94 static inline const char* Machine() { return "ARM64"; } | |
| 95 | |
| 96 # define ELF_R_SYM(val) ELF64_R_SYM(val) | |
| 97 # define ELF_R_TYPE(val) ELF64_R_TYPE(val) | |
| 98 # define ELF_R_INFO(sym, type) ELF64_R_INFO(sym, type) | |
| 99 # define ELF_ST_TYPE(val) ELF64_ST_TYPE(val) | |
| 100 }; | 62 }; |
| 101 #endif | |
| 102 | 63 |
| 103 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_ | 64 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_ |
| OLD | NEW |