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 rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
90 for (o = offset; o < offset + len; o++)
91 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
92 if (rela->offset == o)
98 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
100 return find_rela_by_dest_range(sec, offset, 1);
103 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
107 list_for_each_entry(func, &sec->symbol_list, list)
108 if (func->type == STT_FUNC && offset >= func->offset &&
109 offset < func->offset + func->len)
115 static int read_sections(struct elf *elf)
119 size_t shstrndx, sections_nr;
122 if (elf_getshdrnum(elf->elf, §ions_nr)) {
123 perror("elf_getshdrnum");
127 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
128 perror("elf_getshdrstrndx");
132 for (i = 0; i < sections_nr; i++) {
133 sec = malloc(sizeof(*sec));
138 memset(sec, 0, sizeof(*sec));
140 INIT_LIST_HEAD(&sec->symbol_list);
141 INIT_LIST_HEAD(&sec->rela_list);
142 hash_init(sec->rela_hash);
143 hash_init(sec->symbol_hash);
145 list_add_tail(&sec->list, &elf->sections);
147 s = elf_getscn(elf->elf, i);
149 perror("elf_getscn");
153 sec->idx = elf_ndxscn(s);
155 if (!gelf_getshdr(s, &sec->sh)) {
156 perror("gelf_getshdr");
160 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
162 perror("elf_strptr");
166 sec->elf_data = elf_getdata(s, NULL);
167 if (!sec->elf_data) {
168 perror("elf_getdata");
172 if (sec->elf_data->d_off != 0 ||
173 sec->elf_data->d_size != sec->sh.sh_size) {
174 WARN("unexpected data attributes for %s", sec->name);
178 sec->data = (unsigned long)sec->elf_data->d_buf;
179 sec->len = sec->elf_data->d_size;
182 /* sanity check, one more call to elf_nextscn() should return NULL */
183 if (elf_nextscn(elf->elf, s)) {
184 WARN("section entry mismatch");
191 static int read_symbols(struct elf *elf)
193 struct section *symtab;
195 struct list_head *entry, *tmp;
198 symtab = find_section_by_name(elf, ".symtab");
200 WARN("missing symbol table");
204 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
206 for (i = 0; i < symbols_nr; i++) {
207 sym = malloc(sizeof(*sym));
212 memset(sym, 0, sizeof(*sym));
216 if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
217 perror("gelf_getsym");
221 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
224 perror("elf_strptr");
228 sym->type = GELF_ST_TYPE(sym->sym.st_info);
229 sym->bind = GELF_ST_BIND(sym->sym.st_info);
231 if (sym->sym.st_shndx > SHN_UNDEF &&
232 sym->sym.st_shndx < SHN_LORESERVE) {
233 sym->sec = find_section_by_index(elf,
236 WARN("couldn't find section for symbol %s",
240 if (sym->type == STT_SECTION) {
241 sym->name = sym->sec->name;
245 sym->sec = find_section_by_index(elf, 0);
247 sym->offset = sym->sym.st_value;
248 sym->len = sym->sym.st_size;
250 /* sorted insert into a per-section list */
251 entry = &sym->sec->symbol_list;
252 list_for_each_prev(tmp, &sym->sec->symbol_list) {
255 s = list_entry(tmp, struct symbol, list);
257 if (sym->offset > s->offset) {
262 if (sym->offset == s->offset && sym->len >= s->len) {
267 list_add(&sym->list, entry);
268 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
278 static int read_relas(struct elf *elf)
285 list_for_each_entry(sec, &elf->sections, list) {
286 if (sec->sh.sh_type != SHT_RELA)
289 sec->base = find_section_by_name(elf, sec->name + 5);
291 WARN("can't find base section for rela section %s",
296 sec->base->rela = sec;
298 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
299 rela = malloc(sizeof(*rela));
304 memset(rela, 0, sizeof(*rela));
306 if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
307 perror("gelf_getrela");
311 rela->type = GELF_R_TYPE(rela->rela.r_info);
312 rela->addend = rela->rela.r_addend;
313 rela->offset = rela->rela.r_offset;
314 symndx = GELF_R_SYM(rela->rela.r_info);
315 rela->sym = find_symbol_by_index(elf, symndx);
317 WARN("can't find rela entry symbol %d for %s",
322 list_add_tail(&rela->list, &sec->rela_list);
323 hash_add(sec->rela_hash, &rela->hash, rela->offset);
331 struct elf *elf_open(const char *name)
335 elf_version(EV_CURRENT);
337 elf = malloc(sizeof(*elf));
342 memset(elf, 0, sizeof(*elf));
344 INIT_LIST_HEAD(&elf->sections);
346 elf->name = strdup(name);
352 elf->fd = open(name, O_RDONLY);
358 elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
364 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
365 perror("gelf_getehdr");
369 if (read_sections(elf))
372 if (read_symbols(elf))
385 void elf_close(struct elf *elf)
387 struct section *sec, *tmpsec;
388 struct symbol *sym, *tmpsym;
389 struct rela *rela, *tmprela;
391 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
392 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
393 list_del(&sym->list);
394 hash_del(&sym->hash);
397 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
398 list_del(&rela->list);
399 hash_del(&rela->hash);
402 list_del(&sec->list);