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>
34 struct section *find_section_by_name(struct elf *elf, const char *name)
38 list_for_each_entry(sec, &elf->sections, list)
39 if (!strcmp(sec->name, name))
45 static struct section *find_section_by_index(struct elf *elf,
50 list_for_each_entry(sec, &elf->sections, list)
57 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
62 list_for_each_entry(sec, &elf->sections, list)
63 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
70 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
74 list_for_each_entry(sym, &sec->symbol_list, list)
75 if (sym->type != STT_SECTION &&
76 sym->offset == offset)
82 struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
87 list_for_each_entry(sec, &elf->sections, list)
88 list_for_each_entry(sym, &sec->symbol_list, list)
89 if (!strcmp(sym->name, name))
95 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
99 list_for_each_entry(sym, &sec->symbol_list, list)
100 if (sym->type != STT_SECTION &&
101 offset >= sym->offset && offset < sym->offset + sym->len)
107 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
116 for (o = offset; o < offset + len; o++)
117 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
118 if (rela->offset == o)
124 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
126 return find_rela_by_dest_range(sec, offset, 1);
129 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
133 list_for_each_entry(func, &sec->symbol_list, list)
134 if (func->type == STT_FUNC && offset >= func->offset &&
135 offset < func->offset + func->len)
141 static int read_sections(struct elf *elf)
145 size_t shstrndx, sections_nr;
148 if (elf_getshdrnum(elf->elf, §ions_nr)) {
149 WARN_ELF("elf_getshdrnum");
153 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
154 WARN_ELF("elf_getshdrstrndx");
158 for (i = 0; i < sections_nr; i++) {
159 sec = malloc(sizeof(*sec));
164 memset(sec, 0, sizeof(*sec));
166 INIT_LIST_HEAD(&sec->symbol_list);
167 INIT_LIST_HEAD(&sec->rela_list);
168 hash_init(sec->rela_hash);
169 hash_init(sec->symbol_hash);
171 list_add_tail(&sec->list, &elf->sections);
173 s = elf_getscn(elf->elf, i);
175 WARN_ELF("elf_getscn");
179 sec->idx = elf_ndxscn(s);
181 if (!gelf_getshdr(s, &sec->sh)) {
182 WARN_ELF("gelf_getshdr");
186 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
188 WARN_ELF("elf_strptr");
192 if (sec->sh.sh_size != 0) {
193 sec->data = elf_getdata(s, NULL);
195 WARN_ELF("elf_getdata");
198 if (sec->data->d_off != 0 ||
199 sec->data->d_size != sec->sh.sh_size) {
200 WARN("unexpected data attributes for %s",
205 sec->len = sec->sh.sh_size;
208 /* sanity check, one more call to elf_nextscn() should return NULL */
209 if (elf_nextscn(elf->elf, s)) {
210 WARN("section entry mismatch");
217 static int read_symbols(struct elf *elf)
219 struct section *symtab, *sec;
220 struct symbol *sym, *pfunc;
221 struct list_head *entry, *tmp;
225 symtab = find_section_by_name(elf, ".symtab");
227 WARN("missing symbol table");
231 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
233 for (i = 0; i < symbols_nr; i++) {
234 sym = malloc(sizeof(*sym));
239 memset(sym, 0, sizeof(*sym));
243 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
244 WARN_ELF("gelf_getsym");
248 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
251 WARN_ELF("elf_strptr");
255 sym->type = GELF_ST_TYPE(sym->sym.st_info);
256 sym->bind = GELF_ST_BIND(sym->sym.st_info);
258 if (sym->sym.st_shndx > SHN_UNDEF &&
259 sym->sym.st_shndx < SHN_LORESERVE) {
260 sym->sec = find_section_by_index(elf,
263 WARN("couldn't find section for symbol %s",
267 if (sym->type == STT_SECTION) {
268 sym->name = sym->sec->name;
272 sym->sec = find_section_by_index(elf, 0);
274 sym->offset = sym->sym.st_value;
275 sym->len = sym->sym.st_size;
277 /* sorted insert into a per-section list */
278 entry = &sym->sec->symbol_list;
279 list_for_each_prev(tmp, &sym->sec->symbol_list) {
282 s = list_entry(tmp, struct symbol, list);
284 if (sym->offset > s->offset) {
289 if (sym->offset == s->offset && sym->len >= s->len) {
294 list_add(&sym->list, entry);
295 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
298 /* Create parent/child links for any cold subfunctions */
299 list_for_each_entry(sec, &elf->sections, list) {
300 list_for_each_entry(sym, &sec->symbol_list, list) {
301 if (sym->type != STT_FUNC)
303 sym->pfunc = sym->cfunc = sym;
304 coldstr = strstr(sym->name, ".cold.");
309 pfunc = find_symbol_by_name(elf, sym->name);
313 WARN("%s(): can't find parent function",
322 * Unfortunately, -fnoreorder-functions puts the child
323 * inside the parent. Remove the overlap so we can
324 * have sane assumptions.
326 * Note that pfunc->len now no longer matches
327 * pfunc->sym.st_size.
329 if (sym->sec == pfunc->sec &&
330 sym->offset >= pfunc->offset &&
331 sym->offset + sym->len == pfunc->offset + pfunc->len) {
332 pfunc->len -= sym->len;
344 static int read_relas(struct elf *elf)
351 list_for_each_entry(sec, &elf->sections, list) {
352 if (sec->sh.sh_type != SHT_RELA)
355 sec->base = find_section_by_name(elf, sec->name + 5);
357 WARN("can't find base section for rela section %s",
362 sec->base->rela = sec;
364 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
365 rela = malloc(sizeof(*rela));
370 memset(rela, 0, sizeof(*rela));
372 if (!gelf_getrela(sec->data, i, &rela->rela)) {
373 WARN_ELF("gelf_getrela");
377 rela->type = GELF_R_TYPE(rela->rela.r_info);
378 rela->addend = rela->rela.r_addend;
379 rela->offset = rela->rela.r_offset;
380 symndx = GELF_R_SYM(rela->rela.r_info);
381 rela->sym = find_symbol_by_index(elf, symndx);
383 WARN("can't find rela entry symbol %d for %s",
388 list_add_tail(&rela->list, &sec->rela_list);
389 hash_add(sec->rela_hash, &rela->hash, rela->offset);
397 struct elf *elf_open(const char *name, int flags)
402 elf_version(EV_CURRENT);
404 elf = malloc(sizeof(*elf));
409 memset(elf, 0, sizeof(*elf));
411 INIT_LIST_HEAD(&elf->sections);
413 elf->fd = open(name, flags);
415 fprintf(stderr, "objtool: Can't open '%s': %s\n",
416 name, strerror(errno));
420 if ((flags & O_ACCMODE) == O_RDONLY)
421 cmd = ELF_C_READ_MMAP;
422 else if ((flags & O_ACCMODE) == O_RDWR)
427 elf->elf = elf_begin(elf->fd, cmd, NULL);
429 WARN_ELF("elf_begin");
433 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
434 WARN_ELF("gelf_getehdr");
438 if (read_sections(elf))
441 if (read_symbols(elf))
454 struct section *elf_create_section(struct elf *elf, const char *name,
455 size_t entsize, int nr)
457 struct section *sec, *shstrtab;
458 size_t size = entsize * nr;
462 sec = malloc(sizeof(*sec));
467 memset(sec, 0, sizeof(*sec));
469 INIT_LIST_HEAD(&sec->symbol_list);
470 INIT_LIST_HEAD(&sec->rela_list);
471 hash_init(sec->rela_hash);
472 hash_init(sec->symbol_hash);
474 list_add_tail(&sec->list, &elf->sections);
476 s = elf_newscn(elf->elf);
478 WARN_ELF("elf_newscn");
482 sec->name = strdup(name);
488 sec->idx = elf_ndxscn(s);
492 sec->data = elf_newdata(s);
494 WARN_ELF("elf_newdata");
498 sec->data->d_size = size;
499 sec->data->d_align = 1;
502 sec->data->d_buf = malloc(size);
503 if (!sec->data->d_buf) {
507 memset(sec->data->d_buf, 0, size);
510 if (!gelf_getshdr(s, &sec->sh)) {
511 WARN_ELF("gelf_getshdr");
515 sec->sh.sh_size = size;
516 sec->sh.sh_entsize = entsize;
517 sec->sh.sh_type = SHT_PROGBITS;
518 sec->sh.sh_addralign = 1;
519 sec->sh.sh_flags = SHF_ALLOC;
522 /* Add section name to .shstrtab */
523 shstrtab = find_section_by_name(elf, ".shstrtab");
525 WARN("can't find .shstrtab section");
529 s = elf_getscn(elf->elf, shstrtab->idx);
531 WARN_ELF("elf_getscn");
535 data = elf_newdata(s);
537 WARN_ELF("elf_newdata");
541 data->d_buf = sec->name;
542 data->d_size = strlen(name) + 1;
545 sec->sh.sh_name = shstrtab->len;
547 shstrtab->len += strlen(name) + 1;
548 shstrtab->changed = true;
553 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
558 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
563 strcpy(relaname, ".rela");
564 strcat(relaname, base->name);
566 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
574 sec->sh.sh_type = SHT_RELA;
575 sec->sh.sh_addralign = 8;
576 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
577 sec->sh.sh_info = base->idx;
578 sec->sh.sh_flags = SHF_INFO_LINK;
583 int elf_rebuild_rela_section(struct section *sec)
586 int nr, idx = 0, size;
590 list_for_each_entry(rela, &sec->rela_list, list)
593 size = nr * sizeof(*relas);
594 relas = malloc(size);
600 sec->data->d_buf = relas;
601 sec->data->d_size = size;
603 sec->sh.sh_size = size;
606 list_for_each_entry(rela, &sec->rela_list, list) {
607 relas[idx].r_offset = rela->offset;
608 relas[idx].r_addend = rela->addend;
609 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
616 int elf_write(struct elf *elf)
621 /* Update section headers for changed sections: */
622 list_for_each_entry(sec, &elf->sections, list) {
624 s = elf_getscn(elf->elf, sec->idx);
626 WARN_ELF("elf_getscn");
629 if (!gelf_update_shdr(s, &sec->sh)) {
630 WARN_ELF("gelf_update_shdr");
636 /* Make sure the new section header entries get updated properly. */
637 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
639 /* Write all changes to the file. */
640 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
641 WARN_ELF("elf_update");
648 void elf_close(struct elf *elf)
650 struct section *sec, *tmpsec;
651 struct symbol *sym, *tmpsym;
652 struct rela *rela, *tmprela;
660 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
661 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
662 list_del(&sym->list);
663 hash_del(&sym->hash);
666 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
667 list_del(&rela->list);
668 hash_del(&rela->hash);
671 list_del(&sec->list);