netlink: access nlk groups safely in netlink bind and getname
[linux/fpc-iii.git] / tools / objtool / elf.c
blob6e9f980a7d26fdc4384fdb15d96baaca09846117
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 symbol *find_symbol_containing(struct section *sec, unsigned long offset)
83 struct symbol *sym;
85 list_for_each_entry(sym, &sec->symbol_list, list)
86 if (sym->type != STT_SECTION &&
87 offset >= sym->offset && offset < sym->offset + sym->len)
88 return sym;
90 return NULL;
93 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
94 unsigned int len)
96 struct rela *rela;
97 unsigned long o;
99 if (!sec->rela)
100 return NULL;
102 for (o = offset; o < offset + len; o++)
103 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
104 if (rela->offset == o)
105 return rela;
107 return NULL;
110 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
112 return find_rela_by_dest_range(sec, offset, 1);
115 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
117 struct symbol *func;
119 list_for_each_entry(func, &sec->symbol_list, list)
120 if (func->type == STT_FUNC && offset >= func->offset &&
121 offset < func->offset + func->len)
122 return func;
124 return NULL;
127 static int read_sections(struct elf *elf)
129 Elf_Scn *s = NULL;
130 struct section *sec;
131 size_t shstrndx, sections_nr;
132 int i;
134 if (elf_getshdrnum(elf->elf, &sections_nr)) {
135 WARN_ELF("elf_getshdrnum");
136 return -1;
139 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
140 WARN_ELF("elf_getshdrstrndx");
141 return -1;
144 for (i = 0; i < sections_nr; i++) {
145 sec = malloc(sizeof(*sec));
146 if (!sec) {
147 perror("malloc");
148 return -1;
150 memset(sec, 0, sizeof(*sec));
152 INIT_LIST_HEAD(&sec->symbol_list);
153 INIT_LIST_HEAD(&sec->rela_list);
154 hash_init(sec->rela_hash);
155 hash_init(sec->symbol_hash);
157 list_add_tail(&sec->list, &elf->sections);
159 s = elf_getscn(elf->elf, i);
160 if (!s) {
161 WARN_ELF("elf_getscn");
162 return -1;
165 sec->idx = elf_ndxscn(s);
167 if (!gelf_getshdr(s, &sec->sh)) {
168 WARN_ELF("gelf_getshdr");
169 return -1;
172 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
173 if (!sec->name) {
174 WARN_ELF("elf_strptr");
175 return -1;
178 sec->data = elf_getdata(s, NULL);
179 if (!sec->data) {
180 WARN_ELF("elf_getdata");
181 return -1;
184 if (sec->data->d_off != 0 ||
185 sec->data->d_size != sec->sh.sh_size) {
186 WARN("unexpected data attributes for %s", sec->name);
187 return -1;
190 sec->len = sec->data->d_size;
193 /* sanity check, one more call to elf_nextscn() should return NULL */
194 if (elf_nextscn(elf->elf, s)) {
195 WARN("section entry mismatch");
196 return -1;
199 return 0;
202 static int read_symbols(struct elf *elf)
204 struct section *symtab;
205 struct symbol *sym;
206 struct list_head *entry, *tmp;
207 int symbols_nr, i;
209 symtab = find_section_by_name(elf, ".symtab");
210 if (!symtab) {
211 WARN("missing symbol table");
212 return -1;
215 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
217 for (i = 0; i < symbols_nr; i++) {
218 sym = malloc(sizeof(*sym));
219 if (!sym) {
220 perror("malloc");
221 return -1;
223 memset(sym, 0, sizeof(*sym));
225 sym->idx = i;
227 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
228 WARN_ELF("gelf_getsym");
229 goto err;
232 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
233 sym->sym.st_name);
234 if (!sym->name) {
235 WARN_ELF("elf_strptr");
236 goto err;
239 sym->type = GELF_ST_TYPE(sym->sym.st_info);
240 sym->bind = GELF_ST_BIND(sym->sym.st_info);
242 if (sym->sym.st_shndx > SHN_UNDEF &&
243 sym->sym.st_shndx < SHN_LORESERVE) {
244 sym->sec = find_section_by_index(elf,
245 sym->sym.st_shndx);
246 if (!sym->sec) {
247 WARN("couldn't find section for symbol %s",
248 sym->name);
249 goto err;
251 if (sym->type == STT_SECTION) {
252 sym->name = sym->sec->name;
253 sym->sec->sym = sym;
255 } else
256 sym->sec = find_section_by_index(elf, 0);
258 sym->offset = sym->sym.st_value;
259 sym->len = sym->sym.st_size;
261 /* sorted insert into a per-section list */
262 entry = &sym->sec->symbol_list;
263 list_for_each_prev(tmp, &sym->sec->symbol_list) {
264 struct symbol *s;
266 s = list_entry(tmp, struct symbol, list);
268 if (sym->offset > s->offset) {
269 entry = tmp;
270 break;
273 if (sym->offset == s->offset && sym->len >= s->len) {
274 entry = tmp;
275 break;
278 list_add(&sym->list, entry);
279 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
282 return 0;
284 err:
285 free(sym);
286 return -1;
289 static int read_relas(struct elf *elf)
291 struct section *sec;
292 struct rela *rela;
293 int i;
294 unsigned int symndx;
296 list_for_each_entry(sec, &elf->sections, list) {
297 if (sec->sh.sh_type != SHT_RELA)
298 continue;
300 sec->base = find_section_by_name(elf, sec->name + 5);
301 if (!sec->base) {
302 WARN("can't find base section for rela section %s",
303 sec->name);
304 return -1;
307 sec->base->rela = sec;
309 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
310 rela = malloc(sizeof(*rela));
311 if (!rela) {
312 perror("malloc");
313 return -1;
315 memset(rela, 0, sizeof(*rela));
317 if (!gelf_getrela(sec->data, i, &rela->rela)) {
318 WARN_ELF("gelf_getrela");
319 return -1;
322 rela->type = GELF_R_TYPE(rela->rela.r_info);
323 rela->addend = rela->rela.r_addend;
324 rela->offset = rela->rela.r_offset;
325 symndx = GELF_R_SYM(rela->rela.r_info);
326 rela->sym = find_symbol_by_index(elf, symndx);
327 if (!rela->sym) {
328 WARN("can't find rela entry symbol %d for %s",
329 symndx, sec->name);
330 return -1;
333 list_add_tail(&rela->list, &sec->rela_list);
334 hash_add(sec->rela_hash, &rela->hash, rela->offset);
339 return 0;
342 struct elf *elf_open(const char *name, int flags)
344 struct elf *elf;
345 Elf_Cmd cmd;
347 elf_version(EV_CURRENT);
349 elf = malloc(sizeof(*elf));
350 if (!elf) {
351 perror("malloc");
352 return NULL;
354 memset(elf, 0, sizeof(*elf));
356 INIT_LIST_HEAD(&elf->sections);
358 elf->fd = open(name, flags);
359 if (elf->fd == -1) {
360 perror("open");
361 goto err;
364 if ((flags & O_ACCMODE) == O_RDONLY)
365 cmd = ELF_C_READ_MMAP;
366 else if ((flags & O_ACCMODE) == O_RDWR)
367 cmd = ELF_C_RDWR;
368 else /* O_WRONLY */
369 cmd = ELF_C_WRITE;
371 elf->elf = elf_begin(elf->fd, cmd, NULL);
372 if (!elf->elf) {
373 WARN_ELF("elf_begin");
374 goto err;
377 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
378 WARN_ELF("gelf_getehdr");
379 goto err;
382 if (read_sections(elf))
383 goto err;
385 if (read_symbols(elf))
386 goto err;
388 if (read_relas(elf))
389 goto err;
391 return elf;
393 err:
394 elf_close(elf);
395 return NULL;
398 struct section *elf_create_section(struct elf *elf, const char *name,
399 size_t entsize, int nr)
401 struct section *sec, *shstrtab;
402 size_t size = entsize * nr;
403 struct Elf_Scn *s;
404 Elf_Data *data;
406 sec = malloc(sizeof(*sec));
407 if (!sec) {
408 perror("malloc");
409 return NULL;
411 memset(sec, 0, sizeof(*sec));
413 INIT_LIST_HEAD(&sec->symbol_list);
414 INIT_LIST_HEAD(&sec->rela_list);
415 hash_init(sec->rela_hash);
416 hash_init(sec->symbol_hash);
418 list_add_tail(&sec->list, &elf->sections);
420 s = elf_newscn(elf->elf);
421 if (!s) {
422 WARN_ELF("elf_newscn");
423 return NULL;
426 sec->name = strdup(name);
427 if (!sec->name) {
428 perror("strdup");
429 return NULL;
432 sec->idx = elf_ndxscn(s);
433 sec->len = size;
434 sec->changed = true;
436 sec->data = elf_newdata(s);
437 if (!sec->data) {
438 WARN_ELF("elf_newdata");
439 return NULL;
442 sec->data->d_size = size;
443 sec->data->d_align = 1;
445 if (size) {
446 sec->data->d_buf = malloc(size);
447 if (!sec->data->d_buf) {
448 perror("malloc");
449 return NULL;
451 memset(sec->data->d_buf, 0, size);
454 if (!gelf_getshdr(s, &sec->sh)) {
455 WARN_ELF("gelf_getshdr");
456 return NULL;
459 sec->sh.sh_size = size;
460 sec->sh.sh_entsize = entsize;
461 sec->sh.sh_type = SHT_PROGBITS;
462 sec->sh.sh_addralign = 1;
463 sec->sh.sh_flags = SHF_ALLOC;
466 /* Add section name to .shstrtab */
467 shstrtab = find_section_by_name(elf, ".shstrtab");
468 if (!shstrtab) {
469 WARN("can't find .shstrtab section");
470 return NULL;
473 s = elf_getscn(elf->elf, shstrtab->idx);
474 if (!s) {
475 WARN_ELF("elf_getscn");
476 return NULL;
479 data = elf_newdata(s);
480 if (!data) {
481 WARN_ELF("elf_newdata");
482 return NULL;
485 data->d_buf = sec->name;
486 data->d_size = strlen(name) + 1;
487 data->d_align = 1;
489 sec->sh.sh_name = shstrtab->len;
491 shstrtab->len += strlen(name) + 1;
492 shstrtab->changed = true;
494 return sec;
497 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
499 char *relaname;
500 struct section *sec;
502 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
503 if (!relaname) {
504 perror("malloc");
505 return NULL;
507 strcpy(relaname, ".rela");
508 strcat(relaname, base->name);
510 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
511 if (!sec)
512 return NULL;
514 base->rela = sec;
515 sec->base = base;
517 sec->sh.sh_type = SHT_RELA;
518 sec->sh.sh_addralign = 8;
519 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
520 sec->sh.sh_info = base->idx;
521 sec->sh.sh_flags = SHF_INFO_LINK;
523 return sec;
526 int elf_rebuild_rela_section(struct section *sec)
528 struct rela *rela;
529 int nr, idx = 0, size;
530 GElf_Rela *relas;
532 nr = 0;
533 list_for_each_entry(rela, &sec->rela_list, list)
534 nr++;
536 size = nr * sizeof(*relas);
537 relas = malloc(size);
538 if (!relas) {
539 perror("malloc");
540 return -1;
543 sec->data->d_buf = relas;
544 sec->data->d_size = size;
546 sec->sh.sh_size = size;
548 idx = 0;
549 list_for_each_entry(rela, &sec->rela_list, list) {
550 relas[idx].r_offset = rela->offset;
551 relas[idx].r_addend = rela->addend;
552 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
553 idx++;
556 return 0;
559 int elf_write(struct elf *elf)
561 struct section *sec;
562 Elf_Scn *s;
564 list_for_each_entry(sec, &elf->sections, list) {
565 if (sec->changed) {
566 s = elf_getscn(elf->elf, sec->idx);
567 if (!s) {
568 WARN_ELF("elf_getscn");
569 return -1;
571 if (!gelf_update_shdr (s, &sec->sh)) {
572 WARN_ELF("gelf_update_shdr");
573 return -1;
578 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
579 WARN_ELF("elf_update");
580 return -1;
583 return 0;
586 void elf_close(struct elf *elf)
588 struct section *sec, *tmpsec;
589 struct symbol *sym, *tmpsym;
590 struct rela *rela, *tmprela;
592 if (elf->elf)
593 elf_end(elf->elf);
595 if (elf->fd > 0)
596 close(elf->fd);
598 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
599 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
600 list_del(&sym->list);
601 hash_del(&sym->hash);
602 free(sym);
604 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
605 list_del(&rela->list);
606 hash_del(&rela->hash);
607 free(rela);
609 list_del(&sec->list);
610 free(sec);
613 free(elf);