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 rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
97 for (o = offset; o < offset + len; o++)
98 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
99 if (rela->offset == o)
105 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
107 return find_rela_by_dest_range(sec, offset, 1);
110 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
114 list_for_each_entry(func, &sec->symbol_list, list)
115 if (func->type == STT_FUNC && offset >= func->offset &&
116 offset < func->offset + func->len)
122 static int read_sections(struct elf *elf)
126 size_t shstrndx, sections_nr;
129 if (elf_getshdrnum(elf->elf, §ions_nr)) {
130 perror("elf_getshdrnum");
134 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
135 perror("elf_getshdrstrndx");
139 for (i = 0; i < sections_nr; i++) {
140 sec = malloc(sizeof(*sec));
145 memset(sec, 0, sizeof(*sec));
147 INIT_LIST_HEAD(&sec->symbol_list);
148 INIT_LIST_HEAD(&sec->rela_list);
149 hash_init(sec->rela_hash);
150 hash_init(sec->symbol_hash);
152 list_add_tail(&sec->list, &elf->sections);
154 s = elf_getscn(elf->elf, i);
156 perror("elf_getscn");
160 sec->idx = elf_ndxscn(s);
162 if (!gelf_getshdr(s, &sec->sh)) {
163 perror("gelf_getshdr");
167 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
169 perror("elf_strptr");
173 sec->elf_data = elf_getdata(s, NULL);
174 if (!sec->elf_data) {
175 perror("elf_getdata");
179 if (sec->elf_data->d_off != 0 ||
180 sec->elf_data->d_size != sec->sh.sh_size) {
181 WARN("unexpected data attributes for %s", sec->name);
185 sec->data = (unsigned long)sec->elf_data->d_buf;
186 sec->len = sec->elf_data->d_size;
189 /* sanity check, one more call to elf_nextscn() should return NULL */
190 if (elf_nextscn(elf->elf, s)) {
191 WARN("section entry mismatch");
198 static int read_symbols(struct elf *elf)
200 struct section *symtab;
202 struct list_head *entry, *tmp;
205 symtab = find_section_by_name(elf, ".symtab");
207 WARN("missing symbol table");
211 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
213 for (i = 0; i < symbols_nr; i++) {
214 sym = malloc(sizeof(*sym));
219 memset(sym, 0, sizeof(*sym));
223 if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
224 perror("gelf_getsym");
228 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
231 perror("elf_strptr");
235 sym->type = GELF_ST_TYPE(sym->sym.st_info);
236 sym->bind = GELF_ST_BIND(sym->sym.st_info);
238 if (sym->sym.st_shndx > SHN_UNDEF &&
239 sym->sym.st_shndx < SHN_LORESERVE) {
240 sym->sec = find_section_by_index(elf,
243 WARN("couldn't find section for symbol %s",
247 if (sym->type == STT_SECTION) {
248 sym->name = sym->sec->name;
252 sym->sec = find_section_by_index(elf, 0);
254 sym->offset = sym->sym.st_value;
255 sym->len = sym->sym.st_size;
257 /* sorted insert into a per-section list */
258 entry = &sym->sec->symbol_list;
259 list_for_each_prev(tmp, &sym->sec->symbol_list) {
262 s = list_entry(tmp, struct symbol, list);
264 if (sym->offset > s->offset) {
269 if (sym->offset == s->offset && sym->len >= s->len) {
274 list_add(&sym->list, entry);
275 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
285 static int read_relas(struct elf *elf)
292 list_for_each_entry(sec, &elf->sections, list) {
293 if (sec->sh.sh_type != SHT_RELA)
296 sec->base = find_section_by_name(elf, sec->name + 5);
298 WARN("can't find base section for rela section %s",
303 sec->base->rela = sec;
305 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
306 rela = malloc(sizeof(*rela));
311 memset(rela, 0, sizeof(*rela));
313 if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
314 perror("gelf_getrela");
318 rela->type = GELF_R_TYPE(rela->rela.r_info);
319 rela->addend = rela->rela.r_addend;
320 rela->offset = rela->rela.r_offset;
321 symndx = GELF_R_SYM(rela->rela.r_info);
322 rela->sym = find_symbol_by_index(elf, symndx);
324 WARN("can't find rela entry symbol %d for %s",
329 list_add_tail(&rela->list, &sec->rela_list);
330 hash_add(sec->rela_hash, &rela->hash, rela->offset);
338 struct elf *elf_open(const char *name)
342 elf_version(EV_CURRENT);
344 elf = malloc(sizeof(*elf));
349 memset(elf, 0, sizeof(*elf));
351 INIT_LIST_HEAD(&elf->sections);
353 elf->name = strdup(name);
359 elf->fd = open(name, O_RDONLY);
365 elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
371 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
372 perror("gelf_getehdr");
376 if (read_sections(elf))
379 if (read_symbols(elf))
392 void elf_close(struct elf *elf)
394 struct section *sec, *tmpsec;
395 struct symbol *sym, *tmpsym;
396 struct rela *rela, *tmprela;
398 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
399 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
400 list_del(&sym->list);
401 hash_del(&sym->hash);
404 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
405 list_del(&rela->list);
406 hash_del(&rela->hash);
409 list_del(&sec->list);