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>
33 struct section *find_section_by_name(struct elf *elf, const char *name)
37 list_for_each_entry(sec, &elf->sections, list)
38 if (!strcmp(sec->name, name))
44 static struct section *find_section_by_index(struct elf *elf,
49 list_for_each_entry(sec, &elf->sections, list)
56 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
61 list_for_each_entry(sec, &elf->sections, list)
62 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
69 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
73 list_for_each_entry(sym, &sec->symbol_list, list)
74 if (sym->type != STT_SECTION &&
75 sym->offset == offset)
81 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
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)
93 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
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)
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)
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)
127 static int read_sections(struct elf *elf)
131 size_t shstrndx, sections_nr;
134 if (elf_getshdrnum(elf->elf, §ions_nr)) {
135 WARN_ELF("elf_getshdrnum");
139 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
140 WARN_ELF("elf_getshdrstrndx");
144 for (i = 0; i < sections_nr; i++) {
145 sec = malloc(sizeof(*sec));
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);
161 WARN_ELF("elf_getscn");
165 sec->idx = elf_ndxscn(s);
167 if (!gelf_getshdr(s, &sec->sh)) {
168 WARN_ELF("gelf_getshdr");
172 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
174 WARN_ELF("elf_strptr");
178 if (sec->sh.sh_size != 0) {
179 sec->data = elf_getdata(s, NULL);
181 WARN_ELF("elf_getdata");
184 if (sec->data->d_off != 0 ||
185 sec->data->d_size != sec->sh.sh_size) {
186 WARN("unexpected data attributes for %s",
191 sec->len = sec->sh.sh_size;
194 /* sanity check, one more call to elf_nextscn() should return NULL */
195 if (elf_nextscn(elf->elf, s)) {
196 WARN("section entry mismatch");
203 static int read_symbols(struct elf *elf)
205 struct section *symtab;
207 struct list_head *entry, *tmp;
210 symtab = find_section_by_name(elf, ".symtab");
212 WARN("missing symbol table");
216 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
218 for (i = 0; i < symbols_nr; i++) {
219 sym = malloc(sizeof(*sym));
224 memset(sym, 0, sizeof(*sym));
228 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
229 WARN_ELF("gelf_getsym");
233 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
236 WARN_ELF("elf_strptr");
240 sym->type = GELF_ST_TYPE(sym->sym.st_info);
241 sym->bind = GELF_ST_BIND(sym->sym.st_info);
243 if (sym->sym.st_shndx > SHN_UNDEF &&
244 sym->sym.st_shndx < SHN_LORESERVE) {
245 sym->sec = find_section_by_index(elf,
248 WARN("couldn't find section for symbol %s",
252 if (sym->type == STT_SECTION) {
253 sym->name = sym->sec->name;
257 sym->sec = find_section_by_index(elf, 0);
259 sym->offset = sym->sym.st_value;
260 sym->len = sym->sym.st_size;
262 /* sorted insert into a per-section list */
263 entry = &sym->sec->symbol_list;
264 list_for_each_prev(tmp, &sym->sec->symbol_list) {
267 s = list_entry(tmp, struct symbol, list);
269 if (sym->offset > s->offset) {
274 if (sym->offset == s->offset && sym->len >= s->len) {
279 list_add(&sym->list, entry);
280 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
290 static int read_relas(struct elf *elf)
297 list_for_each_entry(sec, &elf->sections, list) {
298 if (sec->sh.sh_type != SHT_RELA)
301 sec->base = find_section_by_name(elf, sec->name + 5);
303 WARN("can't find base section for rela section %s",
308 sec->base->rela = sec;
310 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
311 rela = malloc(sizeof(*rela));
316 memset(rela, 0, sizeof(*rela));
318 if (!gelf_getrela(sec->data, i, &rela->rela)) {
319 WARN_ELF("gelf_getrela");
323 rela->type = GELF_R_TYPE(rela->rela.r_info);
324 rela->addend = rela->rela.r_addend;
325 rela->offset = rela->rela.r_offset;
326 symndx = GELF_R_SYM(rela->rela.r_info);
327 rela->sym = find_symbol_by_index(elf, symndx);
329 WARN("can't find rela entry symbol %d for %s",
334 list_add_tail(&rela->list, &sec->rela_list);
335 hash_add(sec->rela_hash, &rela->hash, rela->offset);
343 struct elf *elf_open(const char *name, int flags)
348 elf_version(EV_CURRENT);
350 elf = malloc(sizeof(*elf));
355 memset(elf, 0, sizeof(*elf));
357 INIT_LIST_HEAD(&elf->sections);
359 elf->fd = open(name, flags);
365 if ((flags & O_ACCMODE) == O_RDONLY)
366 cmd = ELF_C_READ_MMAP;
367 else if ((flags & O_ACCMODE) == O_RDWR)
372 elf->elf = elf_begin(elf->fd, cmd, NULL);
374 WARN_ELF("elf_begin");
378 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
379 WARN_ELF("gelf_getehdr");
383 if (read_sections(elf))
386 if (read_symbols(elf))
399 struct section *elf_create_section(struct elf *elf, const char *name,
400 size_t entsize, int nr)
402 struct section *sec, *shstrtab;
403 size_t size = entsize * nr;
407 sec = malloc(sizeof(*sec));
412 memset(sec, 0, sizeof(*sec));
414 INIT_LIST_HEAD(&sec->symbol_list);
415 INIT_LIST_HEAD(&sec->rela_list);
416 hash_init(sec->rela_hash);
417 hash_init(sec->symbol_hash);
419 list_add_tail(&sec->list, &elf->sections);
421 s = elf_newscn(elf->elf);
423 WARN_ELF("elf_newscn");
427 sec->name = strdup(name);
433 sec->idx = elf_ndxscn(s);
437 sec->data = elf_newdata(s);
439 WARN_ELF("elf_newdata");
443 sec->data->d_size = size;
444 sec->data->d_align = 1;
447 sec->data->d_buf = malloc(size);
448 if (!sec->data->d_buf) {
452 memset(sec->data->d_buf, 0, size);
455 if (!gelf_getshdr(s, &sec->sh)) {
456 WARN_ELF("gelf_getshdr");
460 sec->sh.sh_size = size;
461 sec->sh.sh_entsize = entsize;
462 sec->sh.sh_type = SHT_PROGBITS;
463 sec->sh.sh_addralign = 1;
464 sec->sh.sh_flags = SHF_ALLOC;
467 /* Add section name to .shstrtab */
468 shstrtab = find_section_by_name(elf, ".shstrtab");
470 WARN("can't find .shstrtab section");
474 s = elf_getscn(elf->elf, shstrtab->idx);
476 WARN_ELF("elf_getscn");
480 data = elf_newdata(s);
482 WARN_ELF("elf_newdata");
486 data->d_buf = sec->name;
487 data->d_size = strlen(name) + 1;
490 sec->sh.sh_name = shstrtab->len;
492 shstrtab->len += strlen(name) + 1;
493 shstrtab->changed = true;
498 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
503 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
508 strcpy(relaname, ".rela");
509 strcat(relaname, base->name);
511 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
519 sec->sh.sh_type = SHT_RELA;
520 sec->sh.sh_addralign = 8;
521 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
522 sec->sh.sh_info = base->idx;
523 sec->sh.sh_flags = SHF_INFO_LINK;
528 int elf_rebuild_rela_section(struct section *sec)
531 int nr, idx = 0, size;
535 list_for_each_entry(rela, &sec->rela_list, list)
538 size = nr * sizeof(*relas);
539 relas = malloc(size);
545 sec->data->d_buf = relas;
546 sec->data->d_size = size;
548 sec->sh.sh_size = size;
551 list_for_each_entry(rela, &sec->rela_list, list) {
552 relas[idx].r_offset = rela->offset;
553 relas[idx].r_addend = rela->addend;
554 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
561 int elf_write(struct elf *elf)
566 /* Update section headers for changed sections: */
567 list_for_each_entry(sec, &elf->sections, list) {
569 s = elf_getscn(elf->elf, sec->idx);
571 WARN_ELF("elf_getscn");
574 if (!gelf_update_shdr(s, &sec->sh)) {
575 WARN_ELF("gelf_update_shdr");
581 /* Make sure the new section header entries get updated properly. */
582 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
584 /* Write all changes to the file. */
585 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
586 WARN_ELF("elf_update");
593 void elf_close(struct elf *elf)
595 struct section *sec, *tmpsec;
596 struct symbol *sym, *tmpsym;
597 struct rela *rela, *tmprela;
605 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
606 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
607 list_del(&sym->list);
608 hash_del(&sym->hash);
611 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
612 list_del(&rela->list);
613 hash_del(&rela->hash);
616 list_del(&sec->list);