blob: 3579e27953da720d3abc2f3403685f6bba9aebec [file] [log] [blame]
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001/*
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_RUNTIME_ELF_UTILS_H_
18#define ART_RUNTIME_ELF_UTILS_H_
19
Brian Carlstrom8758c642014-06-11 14:22:02 -070020#include <sys/cdefs.h>
21
Brian Carlstrome130ee62014-07-01 23:54:20 -070022// Explicitly include our own elf.h to avoid Linux and other dependencies.
23#include "./elf.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000024
Alex Light53cb16b2014-06-12 11:26:29 -070025#include "base/logging.h"
26
Ian Rogersc7dd2952014-10-21 23:31:19 -070027namespace art {
28
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000029// Architecture dependent flags for the ELF header.
30#define EF_ARM_EABI_VER5 0x05000000
31#define EF_MIPS_ABI_O32 0x00001000
32#define EF_MIPS_ARCH_32R2 0x70000000
Andreas Gampec5a3ea72015-01-13 16:41:53 -080033#define EF_MIPS_ARCH_32R6 0x90000000
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000034
35#define EI_ABIVERSION 8
36#define EM_ARM 40
37#define EF_MIPS_NOREORDER 1
38#define EF_MIPS_PIC 2
39#define EF_MIPS_CPIC 4
40#define STV_DEFAULT 0
41
Stuart Monteithb95a5342014-03-12 13:32:32 +000042#define EM_AARCH64 183
43
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000044#define DT_BIND_NOW 24
45#define DT_INIT_ARRAY 25
46#define DT_FINI_ARRAY 26
47#define DT_INIT_ARRAYSZ 27
48#define DT_FINI_ARRAYSZ 28
49#define DT_RUNPATH 29
50#define DT_FLAGS 30
51
52/* MIPS dependent d_tag field for Elf32_Dyn. */
53#define DT_MIPS_RLD_VERSION 0x70000001 /* Runtime Linker Interface ID */
54#define DT_MIPS_TIME_STAMP 0x70000002 /* Timestamp */
55#define DT_MIPS_ICHECKSUM 0x70000003 /* Cksum of ext. str. and com. sizes */
56#define DT_MIPS_IVERSION 0x70000004 /* Version string (string tbl index) */
57#define DT_MIPS_FLAGS 0x70000005 /* Flags */
58#define DT_MIPS_BASE_ADDRESS 0x70000006 /* Segment base address */
59#define DT_MIPS_CONFLICT 0x70000008 /* Adr of .conflict section */
60#define DT_MIPS_LIBLIST 0x70000009 /* Address of .liblist section */
61#define DT_MIPS_LOCAL_GOTNO 0x7000000a /* Number of local .GOT entries */
62#define DT_MIPS_CONFLICTNO 0x7000000b /* Number of .conflict entries */
63#define DT_MIPS_LIBLISTNO 0x70000010 /* Number of .liblist entries */
64#define DT_MIPS_SYMTABNO 0x70000011 /* Number of .dynsym entries */
65#define DT_MIPS_UNREFEXTNO 0x70000012 /* First external DYNSYM */
66#define DT_MIPS_GOTSYM 0x70000013 /* First GOT entry in .dynsym */
67#define DT_MIPS_HIPAGENO 0x70000014 /* Number of GOT page table entries */
68#define DT_MIPS_RLD_MAP 0x70000016 /* Address of debug map pointer */
69
Alex Light53cb16b2014-06-12 11:26:29 -070070// Patching section type
71#define SHT_OAT_PATCH SHT_LOUSER
72
Ian Rogersd4c4d952014-10-16 20:31:53 -070073static inline void SetBindingAndType(Elf32_Sym* sym, unsigned char b, unsigned char t) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000074 sym->st_info = (b << 4) + (t & 0x0f);
75}
76
Ian Rogersd4c4d952014-10-16 20:31:53 -070077static inline bool IsDynamicSectionPointer(Elf32_Word d_tag, Elf32_Word e_machine) {
Alex Light53cb16b2014-06-12 11:26:29 -070078 switch (d_tag) {
79 // case 1: well known d_tag values that imply Elf32_Dyn.d_un contains an address in d_ptr
80 case DT_PLTGOT:
81 case DT_HASH:
82 case DT_STRTAB:
83 case DT_SYMTAB:
84 case DT_RELA:
85 case DT_INIT:
86 case DT_FINI:
87 case DT_REL:
88 case DT_DEBUG:
89 case DT_JMPREL: {
90 return true;
91 }
92 // d_val or ignored values
93 case DT_NULL:
94 case DT_NEEDED:
95 case DT_PLTRELSZ:
96 case DT_RELASZ:
97 case DT_RELAENT:
98 case DT_STRSZ:
99 case DT_SYMENT:
100 case DT_SONAME:
101 case DT_RPATH:
102 case DT_SYMBOLIC:
103 case DT_RELSZ:
104 case DT_RELENT:
105 case DT_PLTREL:
106 case DT_TEXTREL:
107 case DT_BIND_NOW:
108 case DT_INIT_ARRAYSZ:
109 case DT_FINI_ARRAYSZ:
110 case DT_RUNPATH:
111 case DT_FLAGS: {
112 return false;
113 }
114 // boundary values that should not be used
115 case DT_ENCODING:
116 case DT_LOOS:
117 case DT_HIOS:
118 case DT_LOPROC:
119 case DT_HIPROC: {
120 LOG(FATAL) << "Illegal d_tag value 0x" << std::hex << d_tag;
121 return false;
122 }
123 default: {
124 // case 2: "regular" DT_* ranges where even d_tag values imply an address in d_ptr
125 if ((DT_ENCODING < d_tag && d_tag < DT_LOOS)
126 || (DT_LOOS < d_tag && d_tag < DT_HIOS)
127 || (DT_LOPROC < d_tag && d_tag < DT_HIPROC)) {
128 // Special case for MIPS which breaks the regular rules between DT_LOPROC and DT_HIPROC
129 if (e_machine == EM_MIPS) {
130 switch (d_tag) {
131 case DT_MIPS_RLD_VERSION:
132 case DT_MIPS_TIME_STAMP:
133 case DT_MIPS_ICHECKSUM:
134 case DT_MIPS_IVERSION:
135 case DT_MIPS_FLAGS:
136 case DT_MIPS_LOCAL_GOTNO:
137 case DT_MIPS_CONFLICTNO:
138 case DT_MIPS_LIBLISTNO:
139 case DT_MIPS_SYMTABNO:
140 case DT_MIPS_UNREFEXTNO:
141 case DT_MIPS_GOTSYM:
142 case DT_MIPS_HIPAGENO: {
143 return false;
144 }
145 case DT_MIPS_BASE_ADDRESS:
146 case DT_MIPS_CONFLICT:
147 case DT_MIPS_LIBLIST:
148 case DT_MIPS_RLD_MAP: {
149 return true;
150 }
151 default: {
152 LOG(FATAL) << "Unknown MIPS d_tag value 0x" << std::hex << d_tag;
153 return false;
154 }
155 }
156 } else if ((d_tag % 2) == 0) {
157 return true;
158 } else {
159 return false;
160 }
161 } else {
162 LOG(FATAL) << "Unknown d_tag value 0x" << std::hex << d_tag;
163 return false;
164 }
165 }
166 }
167}
168
Ian Rogersc7dd2952014-10-21 23:31:19 -0700169} // namespace art
170
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000171#endif // ART_RUNTIME_ELF_UTILS_H_