elf_traits.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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
3// found in the LICENSE file.
4
5// Target-specific ELF type traits.
6
7#ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_
8#define TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_
9
10#include "elf.h"
11#include "libelf.h"
12
13// The TARGET_ macro controls which Elf types we expect and handle.
14// Either TARGET_ARM or TARGET_ARM64 must be defined, but not both.
15
16#if !defined(TARGET_ARM) && !defined(TARGET_ARM64)
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)
37struct ELF {
38  typedef Elf32_Addr Addr;
39  typedef Elf32_Dyn Dyn;
40  typedef Elf32_Ehdr Ehdr;
41  typedef Elf32_Off Off;
42  typedef Elf32_Phdr Phdr;
43  typedef Elf32_Rel Rel;
44  typedef Elf32_Rela Rela;
45  typedef Elf32_Shdr Shdr;
46  typedef Elf32_Sword Sword;
47  typedef Elf32_Sxword Sxword;
48  typedef Elf32_Sym Sym;
49  typedef Elf32_Word Word;
50  typedef Elf32_Xword Xword;
51
52  static inline Ehdr* getehdr(Elf* elf) { return elf32_getehdr(elf); }
53  static inline Phdr* getphdr(Elf* elf) { return elf32_getphdr(elf); }
54  static inline Shdr* getshdr(Elf_Scn* scn) { return elf32_getshdr(scn); }
55
56  enum { kMachine = EM_ARM };
57  enum { kFileClass = ELFCLASS32 };
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};
68
69#elif defined(TARGET_ARM64)
70struct ELF {
71  typedef Elf64_Addr Addr;
72  typedef Elf64_Dyn Dyn;
73  typedef Elf64_Ehdr Ehdr;
74  typedef Elf64_Off Off;
75  typedef Elf64_Phdr Phdr;
76  typedef Elf64_Rel Rel;
77  typedef Elf64_Rela Rela;
78  typedef Elf64_Shdr Shdr;
79  typedef Elf64_Sword Sword;
80  typedef Elf64_Sxword Sxword;
81  typedef Elf64_Sym Sym;
82  typedef Elf64_Word Word;
83  typedef Elf64_Xword Xword;
84
85  static inline Ehdr* getehdr(Elf* elf) { return elf64_getehdr(elf); }
86  static inline Phdr* getphdr(Elf* elf) { return elf64_getphdr(elf); }
87  static inline Shdr* getshdr(Elf_Scn* scn) { return elf64_getshdr(scn); }
88
89  enum { kMachine = EM_AARCH64 };
90  enum { kFileClass = ELFCLASS64 };
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};
101#endif
102
103#endif  // TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_
104