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   typedef Elf32_Addr Addr;
30   typedef Elf32_Off Off;
31   typedef Elf32_Half Half;
32   typedef Elf32_Word Word;
33   typedef Elf32_Sword Sword;
34   typedef Elf32_Ehdr Ehdr;
35   typedef Elf32_Shdr Shdr;
36   typedef Elf32_Sym Sym;
37   typedef Elf32_Rel Rel;
38   typedef Elf32_Rela Rela;
39   typedef Elf32_Phdr Phdr;
40   typedef Elf32_Dyn Dyn;
41 };
42 
43 struct ElfTypes64 {
44   typedef Elf64_Addr Addr;
45   typedef Elf64_Off Off;
46   typedef Elf64_Half Half;
47   typedef Elf64_Word Word;
48   typedef Elf64_Sword Sword;
49   typedef Elf64_Xword Xword;
50   typedef Elf64_Sxword Sxword;
51   typedef Elf64_Ehdr Ehdr;
52   typedef Elf64_Shdr Shdr;
53   typedef Elf64_Sym Sym;
54   typedef Elf64_Rel Rel;
55   typedef Elf64_Rela Rela;
56   typedef Elf64_Phdr Phdr;
57   typedef Elf64_Dyn 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 #define STV_DEFAULT 0
69 
70 #define EM_AARCH64 183
71 
72 #define DT_BIND_NOW 24
73 #define DT_INIT_ARRAY 25
74 #define DT_FINI_ARRAY 26
75 #define DT_INIT_ARRAYSZ 27
76 #define DT_FINI_ARRAYSZ 28
77 #define DT_RUNPATH 29
78 #define DT_FLAGS 30
79 
80 // Patching section type
81 #define SHT_OAT_PATCH        SHT_LOUSER
82 
SetBindingAndType(Elf32_Sym * sym,unsigned char b,unsigned char t)83 static inline void SetBindingAndType(Elf32_Sym* sym, unsigned char b, unsigned char t) {
84   sym->st_info = (b << 4) + (t & 0x0f);
85 }
86 
IsDynamicSectionPointer(Elf32_Word d_tag,Elf32_Word e_machine ATTRIBUTE_UNUSED)87 static inline bool IsDynamicSectionPointer(Elf32_Word d_tag,
88                                            Elf32_Word e_machine ATTRIBUTE_UNUSED) {
89   // TODO: Remove the `e_machine` parameter from API (not needed after Mips target was removed).
90   switch (d_tag) {
91     // case 1: well known d_tag values that imply Elf32_Dyn.d_un contains an address in d_ptr
92     case DT_PLTGOT:
93     case DT_HASH:
94     case DT_STRTAB:
95     case DT_SYMTAB:
96     case DT_RELA:
97     case DT_INIT:
98     case DT_FINI:
99     case DT_REL:
100     case DT_DEBUG:
101     case DT_JMPREL: {
102       return true;
103     }
104     // d_val or ignored values
105     case DT_NULL:
106     case DT_NEEDED:
107     case DT_PLTRELSZ:
108     case DT_RELASZ:
109     case DT_RELAENT:
110     case DT_STRSZ:
111     case DT_SYMENT:
112     case DT_SONAME:
113     case DT_RPATH:
114     case DT_SYMBOLIC:
115     case DT_RELSZ:
116     case DT_RELENT:
117     case DT_PLTREL:
118     case DT_TEXTREL:
119     case DT_BIND_NOW:
120     case DT_INIT_ARRAYSZ:
121     case DT_FINI_ARRAYSZ:
122     case DT_RUNPATH:
123     case DT_FLAGS: {
124       return false;
125     }
126     // boundary values that should not be used
127     case DT_ENCODING:
128     case DT_LOOS:
129     case DT_HIOS:
130     case DT_LOPROC:
131     case DT_HIPROC: {
132       LOG(FATAL) << "Illegal d_tag value 0x" << std::hex << d_tag;
133       return false;
134     }
135     default: {
136       // case 2: "regular" DT_* ranges where even d_tag values imply an address in d_ptr
137       if ((DT_ENCODING  < d_tag && d_tag < DT_LOOS)
138           || (DT_LOOS   < d_tag && d_tag < DT_HIOS)
139           || (DT_LOPROC < d_tag && d_tag < DT_HIPROC)) {
140         if ((d_tag % 2) == 0) {
141           return true;
142         } else {
143           return false;
144         }
145       } else {
146         LOG(FATAL) << "Unknown d_tag value 0x" << std::hex << d_tag;
147         return false;
148       }
149     }
150   }
151 }
152 
153 }  // namespace art
154 
155 #endif  // ART_LIBELFFILE_ELF_ELF_UTILS_H_
156