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 #define MAX_NAME_LEN 128
36 struct section *find_section_by_name(struct elf *elf, const char *name)
40 list_for_each_entry(sec, &elf->sections, list)
41 if (!strcmp(sec->name, name))
47 static struct section *find_section_by_index(struct elf *elf,
52 list_for_each_entry(sec, &elf->sections, list)
59 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
64 list_for_each_entry(sec, &elf->sections, list)
65 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
72 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
76 list_for_each_entry(sym, &sec->symbol_list, list)
77 if (sym->type != STT_SECTION &&
78 sym->offset == offset)
84 struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
89 list_for_each_entry(sec, &elf->sections, list)
90 list_for_each_entry(sym, &sec->symbol_list, list)
91 if (!strcmp(sym->name, name))
97 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
101 list_for_each_entry(sym, &sec->symbol_list, list)
102 if (sym->type != STT_SECTION &&
103 offset >= sym->offset && offset < sym->offset + sym->len)
109 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
118 for (o = offset; o < offset + len; o++)
119 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
120 if (rela->offset == o)
126 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
128 return find_rela_by_dest_range(sec, offset, 1);
131 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
135 list_for_each_entry(func, &sec->symbol_list, list)
136 if (func->type == STT_FUNC && offset >= func->offset &&
137 offset < func->offset + func->len)
143 static int read_sections(struct elf *elf)
147 size_t shstrndx, sections_nr;
150 if (elf_getshdrnum(elf->elf, §ions_nr)) {
151 WARN_ELF("elf_getshdrnum");
155 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
156 WARN_ELF("elf_getshdrstrndx");
160 for (i = 0; i < sections_nr; i++) {
161 sec = malloc(sizeof(*sec));
166 memset(sec, 0, sizeof(*sec));
168 INIT_LIST_HEAD(&sec->symbol_list);
169 INIT_LIST_HEAD(&sec->rela_list);
170 hash_init(sec->rela_hash);
171 hash_init(sec->symbol_hash);
173 list_add_tail(&sec->list, &elf->sections);
175 s = elf_getscn(elf->elf, i);
177 WARN_ELF("elf_getscn");
181 sec->idx = elf_ndxscn(s);
183 if (!gelf_getshdr(s, &sec->sh)) {
184 WARN_ELF("gelf_getshdr");
188 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
190 WARN_ELF("elf_strptr");
194 if (sec->sh.sh_size != 0) {
195 sec->data = elf_getdata(s, NULL);
197 WARN_ELF("elf_getdata");
200 if (sec->data->d_off != 0 ||
201 sec->data->d_size != sec->sh.sh_size) {
202 WARN("unexpected data attributes for %s",
207 sec->len = sec->sh.sh_size;
210 /* sanity check, one more call to elf_nextscn() should return NULL */
211 if (elf_nextscn(elf->elf, s)) {
212 WARN("section entry mismatch");
219 static int read_symbols(struct elf *elf)
221 struct section *symtab, *sec;
222 struct symbol *sym, *pfunc;
223 struct list_head *entry, *tmp;
227 symtab = find_section_by_name(elf, ".symtab");
230 * A missing symbol table is actually possible if it's an empty
231 * .o file. This can happen for thunk_64.o.
236 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
238 for (i = 0; i < symbols_nr; i++) {
239 sym = malloc(sizeof(*sym));
244 memset(sym, 0, sizeof(*sym));
248 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
249 WARN_ELF("gelf_getsym");
253 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
256 WARN_ELF("elf_strptr");
260 sym->type = GELF_ST_TYPE(sym->sym.st_info);
261 sym->bind = GELF_ST_BIND(sym->sym.st_info);
263 if (sym->sym.st_shndx > SHN_UNDEF &&
264 sym->sym.st_shndx < SHN_LORESERVE) {
265 sym->sec = find_section_by_index(elf,
268 WARN("couldn't find section for symbol %s",
272 if (sym->type == STT_SECTION) {
273 sym->name = sym->sec->name;
277 sym->sec = find_section_by_index(elf, 0);
279 sym->offset = sym->sym.st_value;
280 sym->len = sym->sym.st_size;
282 /* sorted insert into a per-section list */
283 entry = &sym->sec->symbol_list;
284 list_for_each_prev(tmp, &sym->sec->symbol_list) {
287 s = list_entry(tmp, struct symbol, list);
289 if (sym->offset > s->offset) {
294 if (sym->offset == s->offset && sym->len >= s->len) {
299 list_add(&sym->list, entry);
300 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
303 /* Create parent/child links for any cold subfunctions */
304 list_for_each_entry(sec, &elf->sections, list) {
305 list_for_each_entry(sym, &sec->symbol_list, list) {
306 char pname[MAX_NAME_LEN + 1];
308 if (sym->type != STT_FUNC)
310 sym->pfunc = sym->cfunc = sym;
311 coldstr = strstr(sym->name, ".cold");
315 pnamelen = coldstr - sym->name;
316 if (pnamelen > MAX_NAME_LEN) {
317 WARN("%s(): parent function name exceeds maximum length of %d characters",
318 sym->name, MAX_NAME_LEN);
322 strncpy(pname, sym->name, pnamelen);
323 pname[pnamelen] = '\0';
324 pfunc = find_symbol_by_name(elf, pname);
327 WARN("%s(): can't find parent function",
336 * Unfortunately, -fnoreorder-functions puts the child
337 * inside the parent. Remove the overlap so we can
338 * have sane assumptions.
340 * Note that pfunc->len now no longer matches
341 * pfunc->sym.st_size.
343 if (sym->sec == pfunc->sec &&
344 sym->offset >= pfunc->offset &&
345 sym->offset + sym->len == pfunc->offset + pfunc->len) {
346 pfunc->len -= sym->len;
358 static int read_relas(struct elf *elf)
365 list_for_each_entry(sec, &elf->sections, list) {
366 if (sec->sh.sh_type != SHT_RELA)
369 sec->base = find_section_by_name(elf, sec->name + 5);
371 WARN("can't find base section for rela section %s",
376 sec->base->rela = sec;
378 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
379 rela = malloc(sizeof(*rela));
384 memset(rela, 0, sizeof(*rela));
386 if (!gelf_getrela(sec->data, i, &rela->rela)) {
387 WARN_ELF("gelf_getrela");
391 rela->type = GELF_R_TYPE(rela->rela.r_info);
392 rela->addend = rela->rela.r_addend;
393 rela->offset = rela->rela.r_offset;
394 symndx = GELF_R_SYM(rela->rela.r_info);
395 rela->sym = find_symbol_by_index(elf, symndx);
396 rela->rela_sec = sec;
398 WARN("can't find rela entry symbol %d for %s",
403 list_add_tail(&rela->list, &sec->rela_list);
404 hash_add(sec->rela_hash, &rela->hash, rela->offset);
412 struct elf *elf_open(const char *name, int flags)
417 elf_version(EV_CURRENT);
419 elf = malloc(sizeof(*elf));
424 memset(elf, 0, sizeof(*elf));
426 INIT_LIST_HEAD(&elf->sections);
428 elf->fd = open(name, flags);
430 fprintf(stderr, "objtool: Can't open '%s': %s\n",
431 name, strerror(errno));
435 if ((flags & O_ACCMODE) == O_RDONLY)
436 cmd = ELF_C_READ_MMAP;
437 else if ((flags & O_ACCMODE) == O_RDWR)
442 elf->elf = elf_begin(elf->fd, cmd, NULL);
444 WARN_ELF("elf_begin");
448 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
449 WARN_ELF("gelf_getehdr");
453 if (read_sections(elf))
456 if (read_symbols(elf))
469 struct section *elf_create_section(struct elf *elf, const char *name,
470 size_t entsize, int nr)
472 struct section *sec, *shstrtab;
473 size_t size = entsize * nr;
477 sec = malloc(sizeof(*sec));
482 memset(sec, 0, sizeof(*sec));
484 INIT_LIST_HEAD(&sec->symbol_list);
485 INIT_LIST_HEAD(&sec->rela_list);
486 hash_init(sec->rela_hash);
487 hash_init(sec->symbol_hash);
489 list_add_tail(&sec->list, &elf->sections);
491 s = elf_newscn(elf->elf);
493 WARN_ELF("elf_newscn");
497 sec->name = strdup(name);
503 sec->idx = elf_ndxscn(s);
507 sec->data = elf_newdata(s);
509 WARN_ELF("elf_newdata");
513 sec->data->d_size = size;
514 sec->data->d_align = 1;
517 sec->data->d_buf = malloc(size);
518 if (!sec->data->d_buf) {
522 memset(sec->data->d_buf, 0, size);
525 if (!gelf_getshdr(s, &sec->sh)) {
526 WARN_ELF("gelf_getshdr");
530 sec->sh.sh_size = size;
531 sec->sh.sh_entsize = entsize;
532 sec->sh.sh_type = SHT_PROGBITS;
533 sec->sh.sh_addralign = 1;
534 sec->sh.sh_flags = SHF_ALLOC;
537 /* Add section name to .shstrtab (or .strtab for Clang) */
538 shstrtab = find_section_by_name(elf, ".shstrtab");
540 shstrtab = find_section_by_name(elf, ".strtab");
542 WARN("can't find .shstrtab or .strtab section");
546 s = elf_getscn(elf->elf, shstrtab->idx);
548 WARN_ELF("elf_getscn");
552 data = elf_newdata(s);
554 WARN_ELF("elf_newdata");
558 data->d_buf = sec->name;
559 data->d_size = strlen(name) + 1;
562 sec->sh.sh_name = shstrtab->len;
564 shstrtab->len += strlen(name) + 1;
565 shstrtab->changed = true;
570 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
575 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
580 strcpy(relaname, ".rela");
581 strcat(relaname, base->name);
583 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
591 sec->sh.sh_type = SHT_RELA;
592 sec->sh.sh_addralign = 8;
593 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
594 sec->sh.sh_info = base->idx;
595 sec->sh.sh_flags = SHF_INFO_LINK;
600 int elf_rebuild_rela_section(struct section *sec)
603 int nr, idx = 0, size;
607 list_for_each_entry(rela, &sec->rela_list, list)
610 size = nr * sizeof(*relas);
611 relas = malloc(size);
617 sec->data->d_buf = relas;
618 sec->data->d_size = size;
620 sec->sh.sh_size = size;
623 list_for_each_entry(rela, &sec->rela_list, list) {
624 relas[idx].r_offset = rela->offset;
625 relas[idx].r_addend = rela->addend;
626 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
633 int elf_write(struct elf *elf)
638 /* Update section headers for changed sections: */
639 list_for_each_entry(sec, &elf->sections, list) {
641 s = elf_getscn(elf->elf, sec->idx);
643 WARN_ELF("elf_getscn");
646 if (!gelf_update_shdr(s, &sec->sh)) {
647 WARN_ELF("gelf_update_shdr");
653 /* Make sure the new section header entries get updated properly. */
654 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
656 /* Write all changes to the file. */
657 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
658 WARN_ELF("elf_update");
665 void elf_close(struct elf *elf)
667 struct section *sec, *tmpsec;
668 struct symbol *sym, *tmpsym;
669 struct rela *rela, *tmprela;
677 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
678 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
679 list_del(&sym->list);
680 hash_del(&sym->hash);
683 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
684 list_del(&rela->list);
685 hash_del(&rela->hash);
688 list_del(&sec->list);