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 symbol
*find_symbol_containing(struct section
*sec
, unsigned long offset
)
85 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
86 if (sym
->type
!= STT_SECTION
&&
87 offset
>= sym
->offset
&& offset
< sym
->offset
+ sym
->len
)
93 struct rela
*find_rela_by_dest_range(struct section
*sec
, unsigned long offset
,
102 for (o
= offset
; o
< offset
+ len
; o
++)
103 hash_for_each_possible(sec
->rela
->rela_hash
, rela
, hash
, o
)
104 if (rela
->offset
== o
)
110 struct rela
*find_rela_by_dest(struct section
*sec
, unsigned long offset
)
112 return find_rela_by_dest_range(sec
, offset
, 1);
115 struct symbol
*find_containing_func(struct section
*sec
, unsigned long offset
)
119 list_for_each_entry(func
, &sec
->symbol_list
, list
)
120 if (func
->type
== STT_FUNC
&& offset
>= func
->offset
&&
121 offset
< func
->offset
+ func
->len
)
127 static int read_sections(struct elf
*elf
)
131 size_t shstrndx
, sections_nr
;
134 if (elf_getshdrnum(elf
->elf
, §ions_nr
)) {
135 WARN_ELF("elf_getshdrnum");
139 if (elf_getshdrstrndx(elf
->elf
, &shstrndx
)) {
140 WARN_ELF("elf_getshdrstrndx");
144 for (i
= 0; i
< sections_nr
; i
++) {
145 sec
= malloc(sizeof(*sec
));
150 memset(sec
, 0, sizeof(*sec
));
152 INIT_LIST_HEAD(&sec
->symbol_list
);
153 INIT_LIST_HEAD(&sec
->rela_list
);
154 hash_init(sec
->rela_hash
);
155 hash_init(sec
->symbol_hash
);
157 list_add_tail(&sec
->list
, &elf
->sections
);
159 s
= elf_getscn(elf
->elf
, i
);
161 WARN_ELF("elf_getscn");
165 sec
->idx
= elf_ndxscn(s
);
167 if (!gelf_getshdr(s
, &sec
->sh
)) {
168 WARN_ELF("gelf_getshdr");
172 sec
->name
= elf_strptr(elf
->elf
, shstrndx
, sec
->sh
.sh_name
);
174 WARN_ELF("elf_strptr");
178 sec
->data
= elf_getdata(s
, NULL
);
180 WARN_ELF("elf_getdata");
184 if (sec
->data
->d_off
!= 0 ||
185 sec
->data
->d_size
!= sec
->sh
.sh_size
) {
186 WARN("unexpected data attributes for %s", sec
->name
);
190 sec
->len
= sec
->data
->d_size
;
193 /* sanity check, one more call to elf_nextscn() should return NULL */
194 if (elf_nextscn(elf
->elf
, s
)) {
195 WARN("section entry mismatch");
202 static int read_symbols(struct elf
*elf
)
204 struct section
*symtab
;
206 struct list_head
*entry
, *tmp
;
209 symtab
= find_section_by_name(elf
, ".symtab");
211 WARN("missing symbol table");
215 symbols_nr
= symtab
->sh
.sh_size
/ symtab
->sh
.sh_entsize
;
217 for (i
= 0; i
< symbols_nr
; i
++) {
218 sym
= malloc(sizeof(*sym
));
223 memset(sym
, 0, sizeof(*sym
));
227 if (!gelf_getsym(symtab
->data
, i
, &sym
->sym
)) {
228 WARN_ELF("gelf_getsym");
232 sym
->name
= elf_strptr(elf
->elf
, symtab
->sh
.sh_link
,
235 WARN_ELF("elf_strptr");
239 sym
->type
= GELF_ST_TYPE(sym
->sym
.st_info
);
240 sym
->bind
= GELF_ST_BIND(sym
->sym
.st_info
);
242 if (sym
->sym
.st_shndx
> SHN_UNDEF
&&
243 sym
->sym
.st_shndx
< SHN_LORESERVE
) {
244 sym
->sec
= find_section_by_index(elf
,
247 WARN("couldn't find section for symbol %s",
251 if (sym
->type
== STT_SECTION
) {
252 sym
->name
= sym
->sec
->name
;
256 sym
->sec
= find_section_by_index(elf
, 0);
258 sym
->offset
= sym
->sym
.st_value
;
259 sym
->len
= sym
->sym
.st_size
;
261 /* sorted insert into a per-section list */
262 entry
= &sym
->sec
->symbol_list
;
263 list_for_each_prev(tmp
, &sym
->sec
->symbol_list
) {
266 s
= list_entry(tmp
, struct symbol
, list
);
268 if (sym
->offset
> s
->offset
) {
273 if (sym
->offset
== s
->offset
&& sym
->len
>= s
->len
) {
278 list_add(&sym
->list
, entry
);
279 hash_add(sym
->sec
->symbol_hash
, &sym
->hash
, sym
->idx
);
289 static int read_relas(struct elf
*elf
)
296 list_for_each_entry(sec
, &elf
->sections
, list
) {
297 if (sec
->sh
.sh_type
!= SHT_RELA
)
300 sec
->base
= find_section_by_name(elf
, sec
->name
+ 5);
302 WARN("can't find base section for rela section %s",
307 sec
->base
->rela
= sec
;
309 for (i
= 0; i
< sec
->sh
.sh_size
/ sec
->sh
.sh_entsize
; i
++) {
310 rela
= malloc(sizeof(*rela
));
315 memset(rela
, 0, sizeof(*rela
));
317 if (!gelf_getrela(sec
->data
, i
, &rela
->rela
)) {
318 WARN_ELF("gelf_getrela");
322 rela
->type
= GELF_R_TYPE(rela
->rela
.r_info
);
323 rela
->addend
= rela
->rela
.r_addend
;
324 rela
->offset
= rela
->rela
.r_offset
;
325 symndx
= GELF_R_SYM(rela
->rela
.r_info
);
326 rela
->sym
= find_symbol_by_index(elf
, symndx
);
328 WARN("can't find rela entry symbol %d for %s",
333 list_add_tail(&rela
->list
, &sec
->rela_list
);
334 hash_add(sec
->rela_hash
, &rela
->hash
, rela
->offset
);
342 struct elf
*elf_open(const char *name
, int flags
)
347 elf_version(EV_CURRENT
);
349 elf
= malloc(sizeof(*elf
));
354 memset(elf
, 0, sizeof(*elf
));
356 INIT_LIST_HEAD(&elf
->sections
);
358 elf
->fd
= open(name
, flags
);
364 if ((flags
& O_ACCMODE
) == O_RDONLY
)
365 cmd
= ELF_C_READ_MMAP
;
366 else if ((flags
& O_ACCMODE
) == O_RDWR
)
371 elf
->elf
= elf_begin(elf
->fd
, cmd
, NULL
);
373 WARN_ELF("elf_begin");
377 if (!gelf_getehdr(elf
->elf
, &elf
->ehdr
)) {
378 WARN_ELF("gelf_getehdr");
382 if (read_sections(elf
))
385 if (read_symbols(elf
))
398 struct section
*elf_create_section(struct elf
*elf
, const char *name
,
399 size_t entsize
, int nr
)
401 struct section
*sec
, *shstrtab
;
402 size_t size
= entsize
* nr
;
406 sec
= malloc(sizeof(*sec
));
411 memset(sec
, 0, sizeof(*sec
));
413 INIT_LIST_HEAD(&sec
->symbol_list
);
414 INIT_LIST_HEAD(&sec
->rela_list
);
415 hash_init(sec
->rela_hash
);
416 hash_init(sec
->symbol_hash
);
418 list_add_tail(&sec
->list
, &elf
->sections
);
420 s
= elf_newscn(elf
->elf
);
422 WARN_ELF("elf_newscn");
426 sec
->name
= strdup(name
);
432 sec
->idx
= elf_ndxscn(s
);
436 sec
->data
= elf_newdata(s
);
438 WARN_ELF("elf_newdata");
442 sec
->data
->d_size
= size
;
443 sec
->data
->d_align
= 1;
446 sec
->data
->d_buf
= malloc(size
);
447 if (!sec
->data
->d_buf
) {
451 memset(sec
->data
->d_buf
, 0, size
);
454 if (!gelf_getshdr(s
, &sec
->sh
)) {
455 WARN_ELF("gelf_getshdr");
459 sec
->sh
.sh_size
= size
;
460 sec
->sh
.sh_entsize
= entsize
;
461 sec
->sh
.sh_type
= SHT_PROGBITS
;
462 sec
->sh
.sh_addralign
= 1;
463 sec
->sh
.sh_flags
= SHF_ALLOC
;
466 /* Add section name to .shstrtab */
467 shstrtab
= find_section_by_name(elf
, ".shstrtab");
469 WARN("can't find .shstrtab section");
473 s
= elf_getscn(elf
->elf
, shstrtab
->idx
);
475 WARN_ELF("elf_getscn");
479 data
= elf_newdata(s
);
481 WARN_ELF("elf_newdata");
485 data
->d_buf
= sec
->name
;
486 data
->d_size
= strlen(name
) + 1;
489 sec
->sh
.sh_name
= shstrtab
->len
;
491 shstrtab
->len
+= strlen(name
) + 1;
492 shstrtab
->changed
= true;
497 struct section
*elf_create_rela_section(struct elf
*elf
, struct section
*base
)
502 relaname
= malloc(strlen(base
->name
) + strlen(".rela") + 1);
507 strcpy(relaname
, ".rela");
508 strcat(relaname
, base
->name
);
510 sec
= elf_create_section(elf
, relaname
, sizeof(GElf_Rela
), 0);
517 sec
->sh
.sh_type
= SHT_RELA
;
518 sec
->sh
.sh_addralign
= 8;
519 sec
->sh
.sh_link
= find_section_by_name(elf
, ".symtab")->idx
;
520 sec
->sh
.sh_info
= base
->idx
;
521 sec
->sh
.sh_flags
= SHF_INFO_LINK
;
526 int elf_rebuild_rela_section(struct section
*sec
)
529 int nr
, idx
= 0, size
;
533 list_for_each_entry(rela
, &sec
->rela_list
, list
)
536 size
= nr
* sizeof(*relas
);
537 relas
= malloc(size
);
543 sec
->data
->d_buf
= relas
;
544 sec
->data
->d_size
= size
;
546 sec
->sh
.sh_size
= size
;
549 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
550 relas
[idx
].r_offset
= rela
->offset
;
551 relas
[idx
].r_addend
= rela
->addend
;
552 relas
[idx
].r_info
= GELF_R_INFO(rela
->sym
->idx
, rela
->type
);
559 int elf_write(struct elf
*elf
)
564 list_for_each_entry(sec
, &elf
->sections
, list
) {
566 s
= elf_getscn(elf
->elf
, sec
->idx
);
568 WARN_ELF("elf_getscn");
571 if (!gelf_update_shdr (s
, &sec
->sh
)) {
572 WARN_ELF("gelf_update_shdr");
578 if (elf_update(elf
->elf
, ELF_C_WRITE
) < 0) {
579 WARN_ELF("elf_update");
586 void elf_close(struct elf
*elf
)
588 struct section
*sec
, *tmpsec
;
589 struct symbol
*sym
, *tmpsym
;
590 struct rela
*rela
, *tmprela
;
598 list_for_each_entry_safe(sec
, tmpsec
, &elf
->sections
, list
) {
599 list_for_each_entry_safe(sym
, tmpsym
, &sec
->symbol_list
, list
) {
600 list_del(&sym
->list
);
601 hash_del(&sym
->hash
);
604 list_for_each_entry_safe(rela
, tmprela
, &sec
->rela_list
, list
) {
605 list_del(&rela
->list
);
606 hash_del(&rela
->hash
);
609 list_del(&sec
->list
);