1f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// found in the LICENSE file.
4f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#ifndef _ELF_TRAITS_H_
6f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define _ELF_TRAITS_H_
7f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// NOTE: <stdint.h> is required here before <elf.h>. This is a NDK header bug.
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include <stdint.h>
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include <elf.h>
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// ELF is a traits structure used to provide convenient aliases for
13f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// 32/64 bit Elf types, depending on the target CPU bitness.
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if __SIZEOF_POINTER__ == 4
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)struct ELF {
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Ehdr Ehdr;
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Phdr Phdr;
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Word Word;
19f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Sword Sword;
2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // TODO(simonb): Elf32_Sxword missing from the NDK.
2103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  typedef int64_t Sxword;
22f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Addr Addr;
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Dyn Dyn;
24f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Sym Sym;
25f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Rel Rel;
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_Rela Rela;
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf32_auxv_t auxv_t;
28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  enum { kElfClass = ELFCLASS32 };
30f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  enum { kElfBits = 32 };
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# ifndef ELF_R_TYPE
3303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# define ELF_R_TYPE ELF32_R_TYPE
3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# endif
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# ifndef ELF_R_SYM
3703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# define ELF_R_SYM ELF32_R_SYM
3803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# endif
3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
4003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# ifndef ELF_R_INFO
4103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# define ELF_R_INFO ELF32_R_INFO
4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# endif
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)};
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#elif __SIZEOF_POINTER__ == 8
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)struct ELF {
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Ehdr Ehdr;
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Phdr Phdr;
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Word Word;
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Sword Sword;
5003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  typedef Elf64_Sxword Sxword;
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Addr Addr;
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Dyn Dyn;
53f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Sym Sym;
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Rel Rel;
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_Rela Rela;
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  typedef Elf64_auxv_t auxv_t;
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
58f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  enum { kElfClass = ELFCLASS64 };
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  enum { kElfBits = 64 };
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# ifndef ELF_R_TYPE
6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# define ELF_R_TYPE ELF64_R_TYPE
6303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# endif
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
6503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# ifndef ELF_R_SYM
6603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# define ELF_R_SYM ELF64_R_SYM
6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# endif
6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
6903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// TODO(simonb): ELF64_R_INFO missing from the NDK.
7003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# ifndef ELF64_R_INFO
7103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type))
7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# endif
7303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
7403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# ifndef ELF_R_INFO
7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# define ELF_R_INFO ELF64_R_INFO
7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)# endif
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)};
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#else
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#error "Unsupported target CPU bitness"
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
82f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#ifdef __arm__
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define ELF_MACHINE EM_ARM
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#elif defined(__i386__)
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define ELF_MACHINE EM_386
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#elif defined(__x86_64__)
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define ELF_MACHINE EM_X86_64
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#elif defined(__mips__) && !defined(__LP64__)  // mips64el defines __mips__ too
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define ELF_MACHINE EM_MIPS
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#elif defined(__aarch64__)
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define ELF_MACHINE EM_AARCH64
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#else
93f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#error "Unsupported target CPU architecture"
94f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // _ELF_TRAITS_H_
97