xtensa: support DMA buffers in high memory
[cris-mirror.git] / tools / objtool / elf.c
blobc1c338661699788c8189becaab8465ed1bdcd775
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>
29 #include <errno.h>
31 #include "elf.h"
32 #include "warn.h"
34 struct section *find_section_by_name(struct elf *elf, const char *name)
36 struct section *sec;
38 list_for_each_entry(sec, &elf->sections, list)
39 if (!strcmp(sec->name, name))
40 return sec;
42 return NULL;
45 static struct section *find_section_by_index(struct elf *elf,
46 unsigned int idx)
48 struct section *sec;
50 list_for_each_entry(sec, &elf->sections, list)
51 if (sec->idx == idx)
52 return sec;
54 return NULL;
57 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
59 struct section *sec;
60 struct symbol *sym;
62 list_for_each_entry(sec, &elf->sections, list)
63 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
64 if (sym->idx == idx)
65 return sym;
67 return NULL;
70 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
72 struct symbol *sym;
74 list_for_each_entry(sym, &sec->symbol_list, list)
75 if (sym->type != STT_SECTION &&
76 sym->offset == offset)
77 return sym;
79 return NULL;
82 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
84 struct symbol *sym;
86 list_for_each_entry(sym, &sec->symbol_list, list)
87 if (sym->type != STT_SECTION &&
88 offset >= sym->offset && offset < sym->offset + sym->len)
89 return sym;
91 return NULL;
94 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
95 unsigned int len)
97 struct rela *rela;
98 unsigned long o;
100 if (!sec->rela)
101 return NULL;
103 for (o = offset; o < offset + len; o++)
104 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
105 if (rela->offset == o)
106 return rela;
108 return NULL;
111 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
113 return find_rela_by_dest_range(sec, offset, 1);
116 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
118 struct symbol *func;
120 list_for_each_entry(func, &sec->symbol_list, list)
121 if (func->type == STT_FUNC && offset >= func->offset &&
122 offset < func->offset + func->len)
123 return func;
125 return NULL;
128 static int read_sections(struct elf *elf)
130 Elf_Scn *s = NULL;
131 struct section *sec;
132 size_t shstrndx, sections_nr;
133 int i;
135 if (elf_getshdrnum(elf->elf, &sections_nr)) {
136 WARN_ELF("elf_getshdrnum");
137 return -1;
140 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
141 WARN_ELF("elf_getshdrstrndx");
142 return -1;
145 for (i = 0; i < sections_nr; i++) {
146 sec = malloc(sizeof(*sec));
147 if (!sec) {
148 perror("malloc");
149 return -1;
151 memset(sec, 0, sizeof(*sec));
153 INIT_LIST_HEAD(&sec->symbol_list);
154 INIT_LIST_HEAD(&sec->rela_list);
155 hash_init(sec->rela_hash);
156 hash_init(sec->symbol_hash);
158 list_add_tail(&sec->list, &elf->sections);
160 s = elf_getscn(elf->elf, i);
161 if (!s) {
162 WARN_ELF("elf_getscn");
163 return -1;
166 sec->idx = elf_ndxscn(s);
168 if (!gelf_getshdr(s, &sec->sh)) {
169 WARN_ELF("gelf_getshdr");
170 return -1;
173 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
174 if (!sec->name) {
175 WARN_ELF("elf_strptr");
176 return -1;
179 if (sec->sh.sh_size != 0) {
180 sec->data = elf_getdata(s, NULL);
181 if (!sec->data) {
182 WARN_ELF("elf_getdata");
183 return -1;
185 if (sec->data->d_off != 0 ||
186 sec->data->d_size != sec->sh.sh_size) {
187 WARN("unexpected data attributes for %s",
188 sec->name);
189 return -1;
192 sec->len = sec->sh.sh_size;
195 /* sanity check, one more call to elf_nextscn() should return NULL */
196 if (elf_nextscn(elf->elf, s)) {
197 WARN("section entry mismatch");
198 return -1;
201 return 0;
204 static int read_symbols(struct elf *elf)
206 struct section *symtab;
207 struct symbol *sym;
208 struct list_head *entry, *tmp;
209 int symbols_nr, i;
211 symtab = find_section_by_name(elf, ".symtab");
212 if (!symtab) {
213 WARN("missing symbol table");
214 return -1;
217 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
219 for (i = 0; i < symbols_nr; i++) {
220 sym = malloc(sizeof(*sym));
221 if (!sym) {
222 perror("malloc");
223 return -1;
225 memset(sym, 0, sizeof(*sym));
227 sym->idx = i;
229 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
230 WARN_ELF("gelf_getsym");
231 goto err;
234 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
235 sym->sym.st_name);
236 if (!sym->name) {
237 WARN_ELF("elf_strptr");
238 goto err;
241 sym->type = GELF_ST_TYPE(sym->sym.st_info);
242 sym->bind = GELF_ST_BIND(sym->sym.st_info);
244 if (sym->sym.st_shndx > SHN_UNDEF &&
245 sym->sym.st_shndx < SHN_LORESERVE) {
246 sym->sec = find_section_by_index(elf,
247 sym->sym.st_shndx);
248 if (!sym->sec) {
249 WARN("couldn't find section for symbol %s",
250 sym->name);
251 goto err;
253 if (sym->type == STT_SECTION) {
254 sym->name = sym->sec->name;
255 sym->sec->sym = sym;
257 } else
258 sym->sec = find_section_by_index(elf, 0);
260 sym->offset = sym->sym.st_value;
261 sym->len = sym->sym.st_size;
263 /* sorted insert into a per-section list */
264 entry = &sym->sec->symbol_list;
265 list_for_each_prev(tmp, &sym->sec->symbol_list) {
266 struct symbol *s;
268 s = list_entry(tmp, struct symbol, list);
270 if (sym->offset > s->offset) {
271 entry = tmp;
272 break;
275 if (sym->offset == s->offset && sym->len >= s->len) {
276 entry = tmp;
277 break;
280 list_add(&sym->list, entry);
281 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
284 return 0;
286 err:
287 free(sym);
288 return -1;
291 static int read_relas(struct elf *elf)
293 struct section *sec;
294 struct rela *rela;
295 int i;
296 unsigned int symndx;
298 list_for_each_entry(sec, &elf->sections, list) {
299 if (sec->sh.sh_type != SHT_RELA)
300 continue;
302 sec->base = find_section_by_name(elf, sec->name + 5);
303 if (!sec->base) {
304 WARN("can't find base section for rela section %s",
305 sec->name);
306 return -1;
309 sec->base->rela = sec;
311 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
312 rela = malloc(sizeof(*rela));
313 if (!rela) {
314 perror("malloc");
315 return -1;
317 memset(rela, 0, sizeof(*rela));
319 if (!gelf_getrela(sec->data, i, &rela->rela)) {
320 WARN_ELF("gelf_getrela");
321 return -1;
324 rela->type = GELF_R_TYPE(rela->rela.r_info);
325 rela->addend = rela->rela.r_addend;
326 rela->offset = rela->rela.r_offset;
327 symndx = GELF_R_SYM(rela->rela.r_info);
328 rela->sym = find_symbol_by_index(elf, symndx);
329 if (!rela->sym) {
330 WARN("can't find rela entry symbol %d for %s",
331 symndx, sec->name);
332 return -1;
335 list_add_tail(&rela->list, &sec->rela_list);
336 hash_add(sec->rela_hash, &rela->hash, rela->offset);
341 return 0;
344 struct elf *elf_open(const char *name, int flags)
346 struct elf *elf;
347 Elf_Cmd cmd;
349 elf_version(EV_CURRENT);
351 elf = malloc(sizeof(*elf));
352 if (!elf) {
353 perror("malloc");
354 return NULL;
356 memset(elf, 0, sizeof(*elf));
358 INIT_LIST_HEAD(&elf->sections);
360 elf->fd = open(name, flags);
361 if (elf->fd == -1) {
362 fprintf(stderr, "objtool: Can't open '%s': %s\n",
363 name, strerror(errno));
364 goto err;
367 if ((flags & O_ACCMODE) == O_RDONLY)
368 cmd = ELF_C_READ_MMAP;
369 else if ((flags & O_ACCMODE) == O_RDWR)
370 cmd = ELF_C_RDWR;
371 else /* O_WRONLY */
372 cmd = ELF_C_WRITE;
374 elf->elf = elf_begin(elf->fd, cmd, NULL);
375 if (!elf->elf) {
376 WARN_ELF("elf_begin");
377 goto err;
380 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
381 WARN_ELF("gelf_getehdr");
382 goto err;
385 if (read_sections(elf))
386 goto err;
388 if (read_symbols(elf))
389 goto err;
391 if (read_relas(elf))
392 goto err;
394 return elf;
396 err:
397 elf_close(elf);
398 return NULL;
401 struct section *elf_create_section(struct elf *elf, const char *name,
402 size_t entsize, int nr)
404 struct section *sec, *shstrtab;
405 size_t size = entsize * nr;
406 struct Elf_Scn *s;
407 Elf_Data *data;
409 sec = malloc(sizeof(*sec));
410 if (!sec) {
411 perror("malloc");
412 return NULL;
414 memset(sec, 0, sizeof(*sec));
416 INIT_LIST_HEAD(&sec->symbol_list);
417 INIT_LIST_HEAD(&sec->rela_list);
418 hash_init(sec->rela_hash);
419 hash_init(sec->symbol_hash);
421 list_add_tail(&sec->list, &elf->sections);
423 s = elf_newscn(elf->elf);
424 if (!s) {
425 WARN_ELF("elf_newscn");
426 return NULL;
429 sec->name = strdup(name);
430 if (!sec->name) {
431 perror("strdup");
432 return NULL;
435 sec->idx = elf_ndxscn(s);
436 sec->len = size;
437 sec->changed = true;
439 sec->data = elf_newdata(s);
440 if (!sec->data) {
441 WARN_ELF("elf_newdata");
442 return NULL;
445 sec->data->d_size = size;
446 sec->data->d_align = 1;
448 if (size) {
449 sec->data->d_buf = malloc(size);
450 if (!sec->data->d_buf) {
451 perror("malloc");
452 return NULL;
454 memset(sec->data->d_buf, 0, size);
457 if (!gelf_getshdr(s, &sec->sh)) {
458 WARN_ELF("gelf_getshdr");
459 return NULL;
462 sec->sh.sh_size = size;
463 sec->sh.sh_entsize = entsize;
464 sec->sh.sh_type = SHT_PROGBITS;
465 sec->sh.sh_addralign = 1;
466 sec->sh.sh_flags = SHF_ALLOC;
469 /* Add section name to .shstrtab */
470 shstrtab = find_section_by_name(elf, ".shstrtab");
471 if (!shstrtab) {
472 WARN("can't find .shstrtab section");
473 return NULL;
476 s = elf_getscn(elf->elf, shstrtab->idx);
477 if (!s) {
478 WARN_ELF("elf_getscn");
479 return NULL;
482 data = elf_newdata(s);
483 if (!data) {
484 WARN_ELF("elf_newdata");
485 return NULL;
488 data->d_buf = sec->name;
489 data->d_size = strlen(name) + 1;
490 data->d_align = 1;
492 sec->sh.sh_name = shstrtab->len;
494 shstrtab->len += strlen(name) + 1;
495 shstrtab->changed = true;
497 return sec;
500 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
502 char *relaname;
503 struct section *sec;
505 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
506 if (!relaname) {
507 perror("malloc");
508 return NULL;
510 strcpy(relaname, ".rela");
511 strcat(relaname, base->name);
513 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
514 free(relaname);
515 if (!sec)
516 return NULL;
518 base->rela = sec;
519 sec->base = base;
521 sec->sh.sh_type = SHT_RELA;
522 sec->sh.sh_addralign = 8;
523 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
524 sec->sh.sh_info = base->idx;
525 sec->sh.sh_flags = SHF_INFO_LINK;
527 return sec;
530 int elf_rebuild_rela_section(struct section *sec)
532 struct rela *rela;
533 int nr, idx = 0, size;
534 GElf_Rela *relas;
536 nr = 0;
537 list_for_each_entry(rela, &sec->rela_list, list)
538 nr++;
540 size = nr * sizeof(*relas);
541 relas = malloc(size);
542 if (!relas) {
543 perror("malloc");
544 return -1;
547 sec->data->d_buf = relas;
548 sec->data->d_size = size;
550 sec->sh.sh_size = size;
552 idx = 0;
553 list_for_each_entry(rela, &sec->rela_list, list) {
554 relas[idx].r_offset = rela->offset;
555 relas[idx].r_addend = rela->addend;
556 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
557 idx++;
560 return 0;
563 int elf_write(struct elf *elf)
565 struct section *sec;
566 Elf_Scn *s;
568 /* Update section headers for changed sections: */
569 list_for_each_entry(sec, &elf->sections, list) {
570 if (sec->changed) {
571 s = elf_getscn(elf->elf, sec->idx);
572 if (!s) {
573 WARN_ELF("elf_getscn");
574 return -1;
576 if (!gelf_update_shdr(s, &sec->sh)) {
577 WARN_ELF("gelf_update_shdr");
578 return -1;
583 /* Make sure the new section header entries get updated properly. */
584 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
586 /* Write all changes to the file. */
587 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
588 WARN_ELF("elf_update");
589 return -1;
592 return 0;
595 void elf_close(struct elf *elf)
597 struct section *sec, *tmpsec;
598 struct symbol *sym, *tmpsym;
599 struct rela *rela, *tmprela;
601 if (elf->elf)
602 elf_end(elf->elf);
604 if (elf->fd > 0)
605 close(elf->fd);
607 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
608 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
609 list_del(&sym->list);
610 hash_del(&sym->hash);
611 free(sym);
613 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
614 list_del(&rela->list);
615 hash_del(&rela->hash);
616 free(rela);
618 list_del(&sec->list);
619 free(sec);
622 free(elf);