Home | History | Annotate | Download | only in src
      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)
     37 struct 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   enum { kGnuStackSegmentAlignment = 0 };
     61 
     62   static inline const char* Machine() { return "ARM"; }
     63 
     64 # define ELF_R_SYM(val) ELF32_R_SYM(val)
     65 # define ELF_R_TYPE(val) ELF32_R_TYPE(val)
     66 # define ELF_R_INFO(sym, type) ELF32_R_INFO(sym, type)
     67 # define ELF_ST_TYPE(val) ELF32_ST_TYPE(val)
     68 };
     69 
     70 #elif defined(TARGET_ARM64)
     71 struct ELF {
     72   typedef Elf64_Addr Addr;
     73   typedef Elf64_Dyn Dyn;
     74   typedef Elf64_Ehdr Ehdr;
     75   typedef Elf64_Off Off;
     76   typedef Elf64_Phdr Phdr;
     77   typedef Elf64_Rel Rel;
     78   typedef Elf64_Rela Rela;
     79   typedef Elf64_Shdr Shdr;
     80   typedef Elf64_Sword Sword;
     81   typedef Elf64_Sxword Sxword;
     82   typedef Elf64_Sym Sym;
     83   typedef Elf64_Word Word;
     84   typedef Elf64_Xword Xword;
     85 
     86   static inline Ehdr* getehdr(Elf* elf) { return elf64_getehdr(elf); }
     87   static inline Phdr* getphdr(Elf* elf) { return elf64_getphdr(elf); }
     88   static inline Shdr* getshdr(Elf_Scn* scn) { return elf64_getshdr(scn); }
     89 
     90   enum { kMachine = EM_AARCH64 };
     91   enum { kFileClass = ELFCLASS64 };
     92   enum { kRelativeRelocationCode = R_AARCH64_RELATIVE };
     93   enum { kNoRelocationCode = R_AARCH64_NONE };
     94   enum { kGnuStackSegmentAlignment = 16 };
     95 
     96   static inline const char* Machine() { return "ARM64"; }
     97 
     98 # define ELF_R_SYM(val) ELF64_R_SYM(val)
     99 # define ELF_R_TYPE(val) ELF64_R_TYPE(val)
    100 # define ELF_R_INFO(sym, type) ELF64_R_INFO(sym, type)
    101 # define ELF_ST_TYPE(val) ELF64_ST_TYPE(val)
    102 };
    103 #endif
    104 
    105 #endif  // TOOLS_RELOCATION_PACKER_SRC_ELF_TRAITS_H_
    106