elf_traits.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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#ifndef _ELF_TRAITS_H_
6#define _ELF_TRAITS_H_
7
8// NOTE: <stdint.h> is required here before <elf.h>. This is a NDK header bug.
9#include <stdint.h>
10#include <elf.h>
11
12// ELF is a traits structure used to provide convenient aliases for
13// 32/64 bit Elf types, depending on the target CPU bitness.
14#if __SIZEOF_POINTER__ == 4
15struct ELF {
16  typedef Elf32_Ehdr Ehdr;
17  typedef Elf32_Phdr Phdr;
18  typedef Elf32_Word Word;
19  typedef Elf32_Sword Sword;
20  // TODO(simonb): Elf32_Sxword missing from the NDK.
21  typedef int64_t Sxword;
22  typedef Elf32_Addr Addr;
23  typedef Elf32_Dyn Dyn;
24  typedef Elf32_Sym Sym;
25  typedef Elf32_Rel Rel;
26  typedef Elf32_Rela Rela;
27  typedef Elf32_auxv_t auxv_t;
28
29  enum { kElfClass = ELFCLASS32 };
30  enum { kElfBits = 32 };
31
32# ifndef ELF_R_TYPE
33# define ELF_R_TYPE ELF32_R_TYPE
34# endif
35
36# ifndef ELF_R_SYM
37# define ELF_R_SYM ELF32_R_SYM
38# endif
39
40# ifndef ELF_R_INFO
41# define ELF_R_INFO ELF32_R_INFO
42# endif
43};
44#elif __SIZEOF_POINTER__ == 8
45struct ELF {
46  typedef Elf64_Ehdr Ehdr;
47  typedef Elf64_Phdr Phdr;
48  typedef Elf64_Word Word;
49  typedef Elf64_Sword Sword;
50  typedef Elf64_Sxword Sxword;
51  typedef Elf64_Addr Addr;
52  typedef Elf64_Dyn Dyn;
53  typedef Elf64_Sym Sym;
54  typedef Elf64_Rel Rel;
55  typedef Elf64_Rela Rela;
56  typedef Elf64_auxv_t auxv_t;
57
58  enum { kElfClass = ELFCLASS64 };
59  enum { kElfBits = 64 };
60
61# ifndef ELF_R_TYPE
62# define ELF_R_TYPE ELF64_R_TYPE
63# endif
64
65# ifndef ELF_R_SYM
66# define ELF_R_SYM ELF64_R_SYM
67# endif
68
69// TODO(simonb): ELF64_R_INFO missing from the NDK.
70# ifndef ELF64_R_INFO
71# define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type))
72# endif
73
74# ifndef ELF_R_INFO
75# define ELF_R_INFO ELF64_R_INFO
76# endif
77};
78#else
79#error "Unsupported target CPU bitness"
80#endif
81
82#ifdef __arm__
83#define ELF_MACHINE EM_ARM
84#elif defined(__i386__)
85#define ELF_MACHINE EM_386
86#elif defined(__x86_64__)
87#define ELF_MACHINE EM_X86_64
88#elif defined(__mips__) && !defined(__LP64__)  // mips64el defines __mips__ too
89#define ELF_MACHINE EM_MIPS
90#elif defined(__aarch64__)
91#define ELF_MACHINE EM_AARCH64
92#else
93#error "Unsupported target CPU architecture"
94#endif
95
96#endif  // _ELF_TRAITS_H_
97