2 * elf.c - ELF access library
4 * Adapted from kpatch (https://github.com/dynup/kpatch):
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 * Fallback for systems without this "read, mmaping if possible" cmd.
36 #ifndef ELF_C_READ_MMAP
37 #define ELF_C_READ_MMAP ELF_C_READ
40 struct section *find_section_by_name(struct elf *elf, const char *name)
44 list_for_each_entry(sec, &elf->sections, list)
45 if (!strcmp(sec->name, name))
51 static struct section *find_section_by_index(struct elf *elf,
56 list_for_each_entry(sec, &elf->sections, list)
63 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
68 list_for_each_entry(sec, &elf->sections, list)
69 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
76 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
80 list_for_each_entry(sym, &sec->symbol_list, list)
81 if (sym->type != STT_SECTION &&
82 sym->offset == offset)
88 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
92 list_for_each_entry(sym, &sec->symbol_list, list)
93 if (sym->type != STT_SECTION &&
94 offset >= sym->offset && offset < sym->offset + sym->len)
100 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
109 for (o = offset; o < offset + len; o++)
110 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
111 if (rela->offset == o)
117 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
119 return find_rela_by_dest_range(sec, offset, 1);
122 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
126 list_for_each_entry(func, &sec->symbol_list, list)
127 if (func->type == STT_FUNC && offset >= func->offset &&
128 offset < func->offset + func->len)
134 static int read_sections(struct elf *elf)
138 size_t shstrndx, sections_nr;
141 if (elf_getshdrnum(elf->elf, §ions_nr)) {
142 perror("elf_getshdrnum");
146 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
147 perror("elf_getshdrstrndx");
151 for (i = 0; i < sections_nr; i++) {
152 sec = malloc(sizeof(*sec));
157 memset(sec, 0, sizeof(*sec));
159 INIT_LIST_HEAD(&sec->symbol_list);
160 INIT_LIST_HEAD(&sec->rela_list);
161 hash_init(sec->rela_hash);
162 hash_init(sec->symbol_hash);
164 list_add_tail(&sec->list, &elf->sections);
166 s = elf_getscn(elf->elf, i);
168 perror("elf_getscn");
172 sec->idx = elf_ndxscn(s);
174 if (!gelf_getshdr(s, &sec->sh)) {
175 perror("gelf_getshdr");
179 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
181 perror("elf_strptr");
185 sec->elf_data = elf_getdata(s, NULL);
186 if (!sec->elf_data) {
187 perror("elf_getdata");
191 if (sec->elf_data->d_off != 0 ||
192 sec->elf_data->d_size != sec->sh.sh_size) {
193 WARN("unexpected data attributes for %s", sec->name);
197 sec->data = (unsigned long)sec->elf_data->d_buf;
198 sec->len = sec->elf_data->d_size;
201 /* sanity check, one more call to elf_nextscn() should return NULL */
202 if (elf_nextscn(elf->elf, s)) {
203 WARN("section entry mismatch");
210 static int read_symbols(struct elf *elf)
212 struct section *symtab;
214 struct list_head *entry, *tmp;
217 symtab = find_section_by_name(elf, ".symtab");
219 WARN("missing symbol table");
223 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
225 for (i = 0; i < symbols_nr; i++) {
226 sym = malloc(sizeof(*sym));
231 memset(sym, 0, sizeof(*sym));
235 if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
236 perror("gelf_getsym");
240 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
243 perror("elf_strptr");
247 sym->type = GELF_ST_TYPE(sym->sym.st_info);
248 sym->bind = GELF_ST_BIND(sym->sym.st_info);
250 if (sym->sym.st_shndx > SHN_UNDEF &&
251 sym->sym.st_shndx < SHN_LORESERVE) {
252 sym->sec = find_section_by_index(elf,
255 WARN("couldn't find section for symbol %s",
259 if (sym->type == STT_SECTION) {
260 sym->name = sym->sec->name;
264 sym->sec = find_section_by_index(elf, 0);
266 sym->offset = sym->sym.st_value;
267 sym->len = sym->sym.st_size;
269 /* sorted insert into a per-section list */
270 entry = &sym->sec->symbol_list;
271 list_for_each_prev(tmp, &sym->sec->symbol_list) {
274 s = list_entry(tmp, struct symbol, list);
276 if (sym->offset > s->offset) {
281 if (sym->offset == s->offset && sym->len >= s->len) {
286 list_add(&sym->list, entry);
287 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
297 static int read_relas(struct elf *elf)
304 list_for_each_entry(sec, &elf->sections, list) {
305 if (sec->sh.sh_type != SHT_RELA)
308 sec->base = find_section_by_name(elf, sec->name + 5);
310 WARN("can't find base section for rela section %s",
315 sec->base->rela = sec;
317 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
318 rela = malloc(sizeof(*rela));
323 memset(rela, 0, sizeof(*rela));
325 if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
326 perror("gelf_getrela");
330 rela->type = GELF_R_TYPE(rela->rela.r_info);
331 rela->addend = rela->rela.r_addend;
332 rela->offset = rela->rela.r_offset;
333 symndx = GELF_R_SYM(rela->rela.r_info);
334 rela->sym = find_symbol_by_index(elf, symndx);
336 WARN("can't find rela entry symbol %d for %s",
341 list_add_tail(&rela->list, &sec->rela_list);
342 hash_add(sec->rela_hash, &rela->hash, rela->offset);
350 struct elf *elf_open(const char *name)
354 elf_version(EV_CURRENT);
356 elf = malloc(sizeof(*elf));
361 memset(elf, 0, sizeof(*elf));
363 INIT_LIST_HEAD(&elf->sections);
365 elf->name = strdup(name);
371 elf->fd = open(name, O_RDONLY);
377 elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
383 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
384 perror("gelf_getehdr");
388 if (read_sections(elf))
391 if (read_symbols(elf))
404 void elf_close(struct elf *elf)
406 struct section *sec, *tmpsec;
407 struct symbol *sym, *tmpsym;
408 struct rela *rela, *tmprela;
410 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
411 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
412 list_del(&sym->list);
413 hash_del(&sym->hash);
416 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
417 list_del(&rela->list);
418 hash_del(&rela->hash);
421 list_del(&sec->list);