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