include/linux/huge_mm.h: return NULL instead of false for pmd_trans_huge_lock()
[linux/fpc-iii.git] / tools / objtool / elf.c
blobe11f6b69cce67e05b16c31767fbf36a8f68f0af4
1 /*
2 * elf.c - ELF access library
4 * Adapted from kpatch (https://github.com/dynup/kpatch):
5 * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
6 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "elf.h"
31 #include "warn.h"
33 struct section *find_section_by_name(struct elf *elf, const char *name)
35 struct section *sec;
37 list_for_each_entry(sec, &elf->sections, list)
38 if (!strcmp(sec->name, name))
39 return sec;
41 return NULL;
44 static struct section *find_section_by_index(struct elf *elf,
45 unsigned int idx)
47 struct section *sec;
49 list_for_each_entry(sec, &elf->sections, list)
50 if (sec->idx == idx)
51 return sec;
53 return NULL;
56 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
58 struct section *sec;
59 struct symbol *sym;
61 list_for_each_entry(sec, &elf->sections, list)
62 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
63 if (sym->idx == idx)
64 return sym;
66 return NULL;
69 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
71 struct symbol *sym;
73 list_for_each_entry(sym, &sec->symbol_list, list)
74 if (sym->type != STT_SECTION &&
75 sym->offset == offset)
76 return sym;
78 return NULL;
81 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
82 unsigned int len)
84 struct rela *rela;
85 unsigned long o;
87 if (!sec->rela)
88 return NULL;
90 for (o = offset; o < offset + len; o++)
91 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
92 if (rela->offset == o)
93 return rela;
95 return NULL;
98 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
100 return find_rela_by_dest_range(sec, offset, 1);
103 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
105 struct symbol *func;
107 list_for_each_entry(func, &sec->symbol_list, list)
108 if (func->type == STT_FUNC && offset >= func->offset &&
109 offset < func->offset + func->len)
110 return func;
112 return NULL;
115 static int read_sections(struct elf *elf)
117 Elf_Scn *s = NULL;
118 struct section *sec;
119 size_t shstrndx, sections_nr;
120 int i;
122 if (elf_getshdrnum(elf->elf, &sections_nr)) {
123 perror("elf_getshdrnum");
124 return -1;
127 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
128 perror("elf_getshdrstrndx");
129 return -1;
132 for (i = 0; i < sections_nr; i++) {
133 sec = malloc(sizeof(*sec));
134 if (!sec) {
135 perror("malloc");
136 return -1;
138 memset(sec, 0, sizeof(*sec));
140 INIT_LIST_HEAD(&sec->symbol_list);
141 INIT_LIST_HEAD(&sec->rela_list);
142 hash_init(sec->rela_hash);
143 hash_init(sec->symbol_hash);
145 list_add_tail(&sec->list, &elf->sections);
147 s = elf_getscn(elf->elf, i);
148 if (!s) {
149 perror("elf_getscn");
150 return -1;
153 sec->idx = elf_ndxscn(s);
155 if (!gelf_getshdr(s, &sec->sh)) {
156 perror("gelf_getshdr");
157 return -1;
160 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
161 if (!sec->name) {
162 perror("elf_strptr");
163 return -1;
166 sec->elf_data = elf_getdata(s, NULL);
167 if (!sec->elf_data) {
168 perror("elf_getdata");
169 return -1;
172 if (sec->elf_data->d_off != 0 ||
173 sec->elf_data->d_size != sec->sh.sh_size) {
174 WARN("unexpected data attributes for %s", sec->name);
175 return -1;
178 sec->data = (unsigned long)sec->elf_data->d_buf;
179 sec->len = sec->elf_data->d_size;
182 /* sanity check, one more call to elf_nextscn() should return NULL */
183 if (elf_nextscn(elf->elf, s)) {
184 WARN("section entry mismatch");
185 return -1;
188 return 0;
191 static int read_symbols(struct elf *elf)
193 struct section *symtab;
194 struct symbol *sym;
195 struct list_head *entry, *tmp;
196 int symbols_nr, i;
198 symtab = find_section_by_name(elf, ".symtab");
199 if (!symtab) {
200 WARN("missing symbol table");
201 return -1;
204 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
206 for (i = 0; i < symbols_nr; i++) {
207 sym = malloc(sizeof(*sym));
208 if (!sym) {
209 perror("malloc");
210 return -1;
212 memset(sym, 0, sizeof(*sym));
214 sym->idx = i;
216 if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
217 perror("gelf_getsym");
218 goto err;
221 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
222 sym->sym.st_name);
223 if (!sym->name) {
224 perror("elf_strptr");
225 goto err;
228 sym->type = GELF_ST_TYPE(sym->sym.st_info);
229 sym->bind = GELF_ST_BIND(sym->sym.st_info);
231 if (sym->sym.st_shndx > SHN_UNDEF &&
232 sym->sym.st_shndx < SHN_LORESERVE) {
233 sym->sec = find_section_by_index(elf,
234 sym->sym.st_shndx);
235 if (!sym->sec) {
236 WARN("couldn't find section for symbol %s",
237 sym->name);
238 goto err;
240 if (sym->type == STT_SECTION) {
241 sym->name = sym->sec->name;
242 sym->sec->sym = sym;
244 } else
245 sym->sec = find_section_by_index(elf, 0);
247 sym->offset = sym->sym.st_value;
248 sym->len = sym->sym.st_size;
250 /* sorted insert into a per-section list */
251 entry = &sym->sec->symbol_list;
252 list_for_each_prev(tmp, &sym->sec->symbol_list) {
253 struct symbol *s;
255 s = list_entry(tmp, struct symbol, list);
257 if (sym->offset > s->offset) {
258 entry = tmp;
259 break;
262 if (sym->offset == s->offset && sym->len >= s->len) {
263 entry = tmp;
264 break;
267 list_add(&sym->list, entry);
268 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
271 return 0;
273 err:
274 free(sym);
275 return -1;
278 static int read_relas(struct elf *elf)
280 struct section *sec;
281 struct rela *rela;
282 int i;
283 unsigned int symndx;
285 list_for_each_entry(sec, &elf->sections, list) {
286 if (sec->sh.sh_type != SHT_RELA)
287 continue;
289 sec->base = find_section_by_name(elf, sec->name + 5);
290 if (!sec->base) {
291 WARN("can't find base section for rela section %s",
292 sec->name);
293 return -1;
296 sec->base->rela = sec;
298 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
299 rela = malloc(sizeof(*rela));
300 if (!rela) {
301 perror("malloc");
302 return -1;
304 memset(rela, 0, sizeof(*rela));
306 if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
307 perror("gelf_getrela");
308 return -1;
311 rela->type = GELF_R_TYPE(rela->rela.r_info);
312 rela->addend = rela->rela.r_addend;
313 rela->offset = rela->rela.r_offset;
314 symndx = GELF_R_SYM(rela->rela.r_info);
315 rela->sym = find_symbol_by_index(elf, symndx);
316 if (!rela->sym) {
317 WARN("can't find rela entry symbol %d for %s",
318 symndx, sec->name);
319 return -1;
322 list_add_tail(&rela->list, &sec->rela_list);
323 hash_add(sec->rela_hash, &rela->hash, rela->offset);
328 return 0;
331 struct elf *elf_open(const char *name)
333 struct elf *elf;
335 elf_version(EV_CURRENT);
337 elf = malloc(sizeof(*elf));
338 if (!elf) {
339 perror("malloc");
340 return NULL;
342 memset(elf, 0, sizeof(*elf));
344 INIT_LIST_HEAD(&elf->sections);
346 elf->name = strdup(name);
347 if (!elf->name) {
348 perror("strdup");
349 goto err;
352 elf->fd = open(name, O_RDONLY);
353 if (elf->fd == -1) {
354 perror("open");
355 goto err;
358 elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
359 if (!elf->elf) {
360 perror("elf_begin");
361 goto err;
364 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
365 perror("gelf_getehdr");
366 goto err;
369 if (read_sections(elf))
370 goto err;
372 if (read_symbols(elf))
373 goto err;
375 if (read_relas(elf))
376 goto err;
378 return elf;
380 err:
381 elf_close(elf);
382 return NULL;
385 void elf_close(struct elf *elf)
387 struct section *sec, *tmpsec;
388 struct symbol *sym, *tmpsym;
389 struct rela *rela, *tmprela;
391 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
392 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
393 list_del(&sym->list);
394 hash_del(&sym->hash);
395 free(sym);
397 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
398 list_del(&rela->list);
399 hash_del(&rela->hash);
400 free(rela);
402 list_del(&sec->list);
403 free(sec);
405 if (elf->name)
406 free(elf->name);
407 if (elf->fd > 0)
408 close(elf->fd);
409 if (elf->elf)
410 elf_end(elf->elf);
411 free(elf);