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 list_for_each_entry(sym, &sec->symbols, list)
69 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
73 list_for_each_entry(sym, &sec->symbols, 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,
89 list_for_each_entry(rela, &sec->rela->relas, list)
90 if (rela->offset >= offset && rela->offset < offset + len)
96 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
98 return find_rela_by_dest_range(sec, offset, 1);
101 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
105 list_for_each_entry(func, &sec->symbols, list)
106 if (func->type == STT_FUNC && offset >= func->offset &&
107 offset < func->offset + func->len)
113 static int read_sections(struct elf *elf)
117 size_t shstrndx, sections_nr;
120 if (elf_getshdrnum(elf->elf, §ions_nr)) {
121 perror("elf_getshdrnum");
125 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
126 perror("elf_getshdrstrndx");
130 for (i = 0; i < sections_nr; i++) {
131 sec = malloc(sizeof(*sec));
136 memset(sec, 0, sizeof(*sec));
138 INIT_LIST_HEAD(&sec->symbols);
139 INIT_LIST_HEAD(&sec->relas);
141 list_add_tail(&sec->list, &elf->sections);
143 s = elf_getscn(elf->elf, i);
145 perror("elf_getscn");
149 sec->idx = elf_ndxscn(s);
151 if (!gelf_getshdr(s, &sec->sh)) {
152 perror("gelf_getshdr");
156 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
158 perror("elf_strptr");
162 sec->elf_data = elf_getdata(s, NULL);
163 if (!sec->elf_data) {
164 perror("elf_getdata");
168 if (sec->elf_data->d_off != 0 ||
169 sec->elf_data->d_size != sec->sh.sh_size) {
170 WARN("unexpected data attributes for %s", sec->name);
174 sec->data = (unsigned long)sec->elf_data->d_buf;
175 sec->len = sec->elf_data->d_size;
178 /* sanity check, one more call to elf_nextscn() should return NULL */
179 if (elf_nextscn(elf->elf, s)) {
180 WARN("section entry mismatch");
187 static int read_symbols(struct elf *elf)
189 struct section *symtab;
191 struct list_head *entry, *tmp;
194 symtab = find_section_by_name(elf, ".symtab");
196 WARN("missing symbol table");
200 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
202 for (i = 0; i < symbols_nr; i++) {
203 sym = malloc(sizeof(*sym));
208 memset(sym, 0, sizeof(*sym));
212 if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
213 perror("gelf_getsym");
217 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
220 perror("elf_strptr");
224 sym->type = GELF_ST_TYPE(sym->sym.st_info);
225 sym->bind = GELF_ST_BIND(sym->sym.st_info);
227 if (sym->sym.st_shndx > SHN_UNDEF &&
228 sym->sym.st_shndx < SHN_LORESERVE) {
229 sym->sec = find_section_by_index(elf,
232 WARN("couldn't find section for symbol %s",
236 if (sym->type == STT_SECTION) {
237 sym->name = sym->sec->name;
241 sym->sec = find_section_by_index(elf, 0);
243 sym->offset = sym->sym.st_value;
244 sym->len = sym->sym.st_size;
246 /* sorted insert into a per-section list */
247 entry = &sym->sec->symbols;
248 list_for_each_prev(tmp, &sym->sec->symbols) {
251 s = list_entry(tmp, struct symbol, list);
253 if (sym->offset > s->offset) {
258 if (sym->offset == s->offset && sym->len >= s->len) {
263 list_add(&sym->list, entry);
273 static int read_relas(struct elf *elf)
280 list_for_each_entry(sec, &elf->sections, list) {
281 if (sec->sh.sh_type != SHT_RELA)
284 sec->base = find_section_by_name(elf, sec->name + 5);
286 WARN("can't find base section for rela section %s",
291 sec->base->rela = sec;
293 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
294 rela = malloc(sizeof(*rela));
299 memset(rela, 0, sizeof(*rela));
301 list_add_tail(&rela->list, &sec->relas);
303 if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
304 perror("gelf_getrela");
308 rela->type = GELF_R_TYPE(rela->rela.r_info);
309 rela->addend = rela->rela.r_addend;
310 rela->offset = rela->rela.r_offset;
311 symndx = GELF_R_SYM(rela->rela.r_info);
312 rela->sym = find_symbol_by_index(elf, symndx);
314 WARN("can't find rela entry symbol %d for %s",
324 struct elf *elf_open(const char *name)
328 elf_version(EV_CURRENT);
330 elf = malloc(sizeof(*elf));
335 memset(elf, 0, sizeof(*elf));
337 INIT_LIST_HEAD(&elf->sections);
339 elf->name = strdup(name);
345 elf->fd = open(name, O_RDONLY);
351 elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
357 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
358 perror("gelf_getehdr");
362 if (read_sections(elf))
365 if (read_symbols(elf))
378 void elf_close(struct elf *elf)
380 struct section *sec, *tmpsec;
381 struct symbol *sym, *tmpsym;
382 struct rela *rela, *tmprela;
384 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
385 list_for_each_entry_safe(sym, tmpsym, &sec->symbols, list) {
386 list_del(&sym->list);
389 list_for_each_entry_safe(rela, tmprela, &sec->relas, list) {
390 list_del(&rela->list);
393 list_del(&sec->list);