• 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 #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