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>
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 #define WARN_ELF(format, ...) \
41 WARN(format ": %s", ##__VA_ARGS__, elf_errmsg(-1))
43 struct section
*find_section_by_name(struct elf
*elf
, const char *name
)
47 list_for_each_entry(sec
, &elf
->sections
, list
)
48 if (!strcmp(sec
->name
, name
))
54 static struct section
*find_section_by_index(struct elf
*elf
,
59 list_for_each_entry(sec
, &elf
->sections
, list
)
66 static struct symbol
*find_symbol_by_index(struct elf
*elf
, unsigned int idx
)
71 list_for_each_entry(sec
, &elf
->sections
, list
)
72 hash_for_each_possible(sec
->symbol_hash
, sym
, hash
, idx
)
79 struct symbol
*find_symbol_by_offset(struct section
*sec
, unsigned long offset
)
83 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
84 if (sym
->type
!= STT_SECTION
&&
85 sym
->offset
== offset
)
91 struct symbol
*find_symbol_containing(struct section
*sec
, unsigned long offset
)
95 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
96 if (sym
->type
!= STT_SECTION
&&
97 offset
>= sym
->offset
&& offset
< sym
->offset
+ sym
->len
)
103 struct rela
*find_rela_by_dest_range(struct section
*sec
, unsigned long offset
,
112 for (o
= offset
; o
< offset
+ len
; o
++)
113 hash_for_each_possible(sec
->rela
->rela_hash
, rela
, hash
, o
)
114 if (rela
->offset
== o
)
120 struct rela
*find_rela_by_dest(struct section
*sec
, unsigned long offset
)
122 return find_rela_by_dest_range(sec
, offset
, 1);
125 struct symbol
*find_containing_func(struct section
*sec
, unsigned long offset
)
129 list_for_each_entry(func
, &sec
->symbol_list
, list
)
130 if (func
->type
== STT_FUNC
&& offset
>= func
->offset
&&
131 offset
< func
->offset
+ func
->len
)
137 static int read_sections(struct elf
*elf
)
141 size_t shstrndx
, sections_nr
;
144 if (elf_getshdrnum(elf
->elf
, §ions_nr
)) {
145 WARN_ELF("elf_getshdrnum");
149 if (elf_getshdrstrndx(elf
->elf
, &shstrndx
)) {
150 WARN_ELF("elf_getshdrstrndx");
154 for (i
= 0; i
< sections_nr
; i
++) {
155 sec
= malloc(sizeof(*sec
));
160 memset(sec
, 0, sizeof(*sec
));
162 INIT_LIST_HEAD(&sec
->symbol_list
);
163 INIT_LIST_HEAD(&sec
->rela_list
);
164 hash_init(sec
->rela_hash
);
165 hash_init(sec
->symbol_hash
);
167 list_add_tail(&sec
->list
, &elf
->sections
);
169 s
= elf_getscn(elf
->elf
, i
);
171 WARN_ELF("elf_getscn");
175 sec
->idx
= elf_ndxscn(s
);
177 if (!gelf_getshdr(s
, &sec
->sh
)) {
178 WARN_ELF("gelf_getshdr");
182 sec
->name
= elf_strptr(elf
->elf
, shstrndx
, sec
->sh
.sh_name
);
184 WARN_ELF("elf_strptr");
188 sec
->data
= elf_getdata(s
, NULL
);
190 WARN_ELF("elf_getdata");
194 if (sec
->data
->d_off
!= 0 ||
195 sec
->data
->d_size
!= sec
->sh
.sh_size
) {
196 WARN("unexpected data attributes for %s", sec
->name
);
200 sec
->len
= sec
->data
->d_size
;
203 /* sanity check, one more call to elf_nextscn() should return NULL */
204 if (elf_nextscn(elf
->elf
, s
)) {
205 WARN("section entry mismatch");
212 static int read_symbols(struct elf
*elf
)
214 struct section
*symtab
;
216 struct list_head
*entry
, *tmp
;
219 symtab
= find_section_by_name(elf
, ".symtab");
221 WARN("missing symbol table");
225 symbols_nr
= symtab
->sh
.sh_size
/ symtab
->sh
.sh_entsize
;
227 for (i
= 0; i
< symbols_nr
; i
++) {
228 sym
= malloc(sizeof(*sym
));
233 memset(sym
, 0, sizeof(*sym
));
237 if (!gelf_getsym(symtab
->data
, i
, &sym
->sym
)) {
238 WARN_ELF("gelf_getsym");
242 sym
->name
= elf_strptr(elf
->elf
, symtab
->sh
.sh_link
,
245 WARN_ELF("elf_strptr");
249 sym
->type
= GELF_ST_TYPE(sym
->sym
.st_info
);
250 sym
->bind
= GELF_ST_BIND(sym
->sym
.st_info
);
252 if (sym
->sym
.st_shndx
> SHN_UNDEF
&&
253 sym
->sym
.st_shndx
< SHN_LORESERVE
) {
254 sym
->sec
= find_section_by_index(elf
,
257 WARN("couldn't find section for symbol %s",
261 if (sym
->type
== STT_SECTION
) {
262 sym
->name
= sym
->sec
->name
;
266 sym
->sec
= find_section_by_index(elf
, 0);
268 sym
->offset
= sym
->sym
.st_value
;
269 sym
->len
= sym
->sym
.st_size
;
271 /* sorted insert into a per-section list */
272 entry
= &sym
->sec
->symbol_list
;
273 list_for_each_prev(tmp
, &sym
->sec
->symbol_list
) {
276 s
= list_entry(tmp
, struct symbol
, list
);
278 if (sym
->offset
> s
->offset
) {
283 if (sym
->offset
== s
->offset
&& sym
->len
>= s
->len
) {
288 list_add(&sym
->list
, entry
);
289 hash_add(sym
->sec
->symbol_hash
, &sym
->hash
, sym
->idx
);
299 static int read_relas(struct elf
*elf
)
306 list_for_each_entry(sec
, &elf
->sections
, list
) {
307 if (sec
->sh
.sh_type
!= SHT_RELA
)
310 sec
->base
= find_section_by_name(elf
, sec
->name
+ 5);
312 WARN("can't find base section for rela section %s",
317 sec
->base
->rela
= sec
;
319 for (i
= 0; i
< sec
->sh
.sh_size
/ sec
->sh
.sh_entsize
; i
++) {
320 rela
= malloc(sizeof(*rela
));
325 memset(rela
, 0, sizeof(*rela
));
327 if (!gelf_getrela(sec
->data
, i
, &rela
->rela
)) {
328 WARN_ELF("gelf_getrela");
332 rela
->type
= GELF_R_TYPE(rela
->rela
.r_info
);
333 rela
->addend
= rela
->rela
.r_addend
;
334 rela
->offset
= rela
->rela
.r_offset
;
335 symndx
= GELF_R_SYM(rela
->rela
.r_info
);
336 rela
->sym
= find_symbol_by_index(elf
, symndx
);
338 WARN("can't find rela entry symbol %d for %s",
343 list_add_tail(&rela
->list
, &sec
->rela_list
);
344 hash_add(sec
->rela_hash
, &rela
->hash
, rela
->offset
);
352 struct elf
*elf_open(const char *name
)
356 elf_version(EV_CURRENT
);
358 elf
= malloc(sizeof(*elf
));
363 memset(elf
, 0, sizeof(*elf
));
365 INIT_LIST_HEAD(&elf
->sections
);
367 elf
->fd
= open(name
, O_RDONLY
);
373 elf
->elf
= elf_begin(elf
->fd
, ELF_C_READ_MMAP
, NULL
);
375 WARN_ELF("elf_begin");
379 if (!gelf_getehdr(elf
->elf
, &elf
->ehdr
)) {
380 WARN_ELF("gelf_getehdr");
384 if (read_sections(elf
))
387 if (read_symbols(elf
))
400 void elf_close(struct elf
*elf
)
402 struct section
*sec
, *tmpsec
;
403 struct symbol
*sym
, *tmpsym
;
404 struct rela
*rela
, *tmprela
;
412 list_for_each_entry_safe(sec
, tmpsec
, &elf
->sections
, list
) {
413 list_for_each_entry_safe(sym
, tmpsym
, &sec
->symbol_list
, list
) {
414 list_del(&sym
->list
);
415 hash_del(&sym
->hash
);
418 list_for_each_entry_safe(rela
, tmprela
, &sec
->rela_list
, list
) {
419 list_del(&rela
->list
);
420 hash_del(&rela
->hash
);
423 list_del(&sec
->list
);