• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBELFFILE_ELF_ELF_UTILS_H_
18 #define ART_LIBELFFILE_ELF_ELF_UTILS_H_
19 
20 #include <elf.h>
21 
22 #include <sys/cdefs.h>
23 
24 #include <android-base/logging.h>
25 
26 namespace art {
27 
28 struct ElfTypes32 {
29   using Addr = Elf32_Addr;
30   using Off = Elf32_Off;
31   using Half = Elf32_Half;
32   using Word = Elf32_Word;
33   using Sword = Elf32_Sword;
34   using Ehdr = Elf32_Ehdr;
35   using Shdr = Elf32_Shdr;
36   using Sym = Elf32_Sym;
37   using Rel = Elf32_Rel;
38   using Rela = Elf32_Rela;
39   using Phdr = Elf32_Phdr;
40   using Dyn = Elf32_Dyn;
41 };
42 
43 struct ElfTypes64 {
44   using Addr = Elf64_Addr;
45   using Off = Elf64_Off;
46   using Half = Elf64_Half;
47   using Word = Elf64_Word;
48   using Sword = Elf64_Sword;
49   using Xword = Elf64_Xword;
50   using Sxword = Elf64_Sxword;
51   using Ehdr = Elf64_Ehdr;
52   using Shdr = Elf64_Shdr;
53   using Sym = Elf64_Sym;
54   using Rel = Elf64_Rel;
55   using Rela = Elf64_Rela;
56   using Phdr = Elf64_Phdr;
57   using Dyn = Elf64_Dyn;
58 };
59 
60 #define ELF_ST_BIND(x) ((x) >> 4)
61 #define ELF_ST_TYPE(x) ((x) & 0xf)
62 
63 // Architecture dependent flags for the ELF header.
64 #define EF_ARM_EABI_VER5 0x05000000
65 
66 #define EI_ABIVERSION 8
67 #define EM_ARM 40
68 #if !defined(STV_DEFAULT)
69 #define STV_DEFAULT 0
70 #endif
71 
72 #define EM_AARCH64 183
73 
74 #ifndef EM_RISCV
75 #define EM_RISCV 243
76 #endif
77 
78 #ifndef EF_RISCV_RVC
79 #define EF_RISCV_RVC 0x1
80 #define EF_RISCV_FLOAT_ABI_DOUBLE 0x4
81 #endif
82 
83 #define DT_BIND_NOW 24
84 #define DT_INIT_ARRAY 25
85 #define DT_FINI_ARRAY 26
86 #define DT_INIT_ARRAYSZ 27
87 #define DT_FINI_ARRAYSZ 28
88 #define DT_RUNPATH 29
89 #define DT_FLAGS 30
90 
91 // Patching section type
92 #define SHT_OAT_PATCH        SHT_LOUSER
93 
SetBindingAndType(Elf32_Sym * sym,unsigned char b,unsigned char t)94 static inline void SetBindingAndType(Elf32_Sym* sym, unsigned char b, unsigned char t) {
95   sym->st_info = (b << 4) + (t & 0x0f);
96 }
97 
IsDynamicSectionPointer(Elf32_Word d_tag,Elf32_Word e_machine ATTRIBUTE_UNUSED)98 static inline bool IsDynamicSectionPointer(Elf32_Word d_tag,
99                                            Elf32_Word e_machine ATTRIBUTE_UNUSED) {
100   // TODO: Remove the `e_machine` parameter from API (not needed after Mips target was removed).
101   switch (d_tag) {
102     // case 1: well known d_tag values that imply Elf32_Dyn.d_un contains an address in d_ptr
103     case DT_PLTGOT:
104     case DT_HASH:
105     case DT_STRTAB:
106     case DT_SYMTAB:
107     case DT_RELA:
108     case DT_INIT:
109     case DT_FINI:
110     case DT_REL:
111     case DT_DEBUG:
112     case DT_JMPREL: {
113       return true;
114     }
115     // d_val or ignored values
116     case DT_NULL:
117     case DT_NEEDED:
118     case DT_PLTRELSZ:
119     case DT_RELASZ:
120     case DT_RELAENT:
121     case DT_STRSZ:
122     case DT_SYMENT:
123     case DT_SONAME:
124     case DT_RPATH:
125     case DT_SYMBOLIC:
126     case DT_RELSZ:
127     case DT_RELENT:
128     case DT_PLTREL:
129     case DT_TEXTREL:
130     case DT_BIND_NOW:
131     case DT_INIT_ARRAYSZ:
132     case DT_FINI_ARRAYSZ:
133     case DT_RUNPATH:
134     case DT_FLAGS: {
135       return false;
136     }
137     // boundary values that should not be used
138     case DT_ENCODING:
139     case DT_LOOS:
140     case DT_HIOS:
141     case DT_LOPROC:
142     case DT_HIPROC: {
143       LOG(FATAL) << "Illegal d_tag value 0x" << std::hex << d_tag;
144       return false;
145     }
146     default: {
147       // case 2: "regular" DT_* ranges where even d_tag values imply an address in d_ptr
148       if ((DT_ENCODING  < d_tag && d_tag < DT_LOOS)
149           || (DT_LOOS   < d_tag && d_tag < DT_HIOS)
150           || (DT_LOPROC < d_tag && d_tag < DT_HIPROC)) {
151         if ((d_tag % 2) == 0) {
152           return true;
153         } else {
154           return false;
155         }
156       } else {
157         LOG(FATAL) << "Unknown d_tag value 0x" << std::hex << d_tag;
158         return false;
159       }
160     }
161   }
162 }
163 
164 }  // namespace art
165 
166 #endif  // ART_LIBELFFILE_ELF_ELF_UTILS_H_
167