15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// found in the LICENSE file.
45f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Target-specific ELF type traits.
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "elf.h"
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "libelf.h"
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The TARGET_ macro controls which Elf types we expect and handle.
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Either TARGET_ARM or TARGET_ARM64 must be defined, but not both.
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if !defined(TARGET_ARM) && !defined(TARGET_ARM64)
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# error "Unsupported target, define one of TARGET_ARM or TARGET_ARM64"
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#elif defined(TARGET_ARM) && defined(TARGET_ARM64)
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# error "Define one of TARGET_ARM or TARGET_ARM64, but not both"
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// TODO(simonb): Eliminate these once AARCH64 appears reliably in elf.h.
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef EM_AARCH64
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define EM_AARCH64 183
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef R_AARCH64_RELATIVE
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define R_AARCH64_RELATIVE 1027
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef R_AARCH64_NONE
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define R_AARCH64_NONE 0
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// ELF is a traits structure used to provide convenient aliases for
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// 32/64 bit Elf types and functions, depending on the target specified.
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if defined(TARGET_ARM)
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)struct ELF {
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Addr Addr;
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Dyn Dyn;
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Ehdr Ehdr;
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Off Off;
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Phdr Phdr;
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Rel Rel;
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Rela Rela;
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Shdr Shdr;
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Sword Sword;
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Sxword Sxword;
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Sym Sym;
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Word Word;
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf32_Xword Xword;
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline Ehdr* getehdr(Elf* elf) { return elf32_getehdr(elf); }
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline Phdr* getphdr(Elf* elf) { return elf32_getphdr(elf); }
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline Shdr* getshdr(Elf_Scn* scn) { return elf32_getshdr(scn); }
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kMachine = EM_ARM };
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kFileClass = ELFCLASS32 };
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kRelativeRelocationCode = R_ARM_RELATIVE };
595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kNoRelocationCode = R_ARM_NONE };
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  enum { kGnuStackSegmentAlignment = 0 };
615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline const char* Machine() { return "ARM"; }
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_R_SYM(val) ELF32_R_SYM(val)
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_R_TYPE(val) ELF32_R_TYPE(val)
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_R_INFO(sym, type) ELF32_R_INFO(sym, type)
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_ST_TYPE(val) ELF32_ST_TYPE(val)
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)};
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#elif defined(TARGET_ARM64)
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)struct ELF {
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Addr Addr;
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Dyn Dyn;
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Ehdr Ehdr;
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Off Off;
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Phdr Phdr;
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Rel Rel;
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Rela Rela;
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Shdr Shdr;
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Sword Sword;
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Sxword Sxword;
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Sym Sym;
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Word Word;
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef Elf64_Xword Xword;
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline Ehdr* getehdr(Elf* elf) { return elf64_getehdr(elf); }
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline Phdr* getphdr(Elf* elf) { return elf64_getphdr(elf); }
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline Shdr* getshdr(Elf_Scn* scn) { return elf64_getshdr(scn); }
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kMachine = EM_AARCH64 };
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kFileClass = ELFCLASS64 };
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kRelativeRelocationCode = R_AARCH64_RELATIVE };
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  enum { kNoRelocationCode = R_AARCH64_NONE };
941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  enum { kGnuStackSegmentAlignment = 16 };
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static inline const char* Machine() { return "ARM64"; }
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_R_SYM(val) ELF64_R_SYM(val)
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_R_TYPE(val) ELF64_R_TYPE(val)
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_R_INFO(sym, type) ELF64_R_INFO(sym, type)
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)# define ELF_ST_TYPE(val) ELF64_ST_TYPE(val)
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)};
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif  // TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_
106