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 #define MAX_NAME_LEN 128
36 struct section
*find_section_by_name(struct elf
*elf
, const char *name
)
40 list_for_each_entry(sec
, &elf
->sections
, list
)
41 if (!strcmp(sec
->name
, name
))
47 static struct section
*find_section_by_index(struct elf
*elf
,
52 list_for_each_entry(sec
, &elf
->sections
, list
)
59 static struct symbol
*find_symbol_by_index(struct elf
*elf
, unsigned int idx
)
64 list_for_each_entry(sec
, &elf
->sections
, list
)
65 hash_for_each_possible(sec
->symbol_hash
, sym
, hash
, idx
)
72 struct symbol
*find_symbol_by_offset(struct section
*sec
, unsigned long offset
)
76 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
77 if (sym
->type
!= STT_SECTION
&&
78 sym
->offset
== offset
)
84 struct symbol
*find_symbol_by_name(struct elf
*elf
, const char *name
)
89 list_for_each_entry(sec
, &elf
->sections
, list
)
90 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
91 if (!strcmp(sym
->name
, name
))
97 struct symbol
*find_symbol_containing(struct section
*sec
, unsigned long offset
)
101 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
102 if (sym
->type
!= STT_SECTION
&&
103 offset
>= sym
->offset
&& offset
< sym
->offset
+ sym
->len
)
109 struct rela
*find_rela_by_dest_range(struct section
*sec
, unsigned long offset
,
118 for (o
= offset
; o
< offset
+ len
; o
++)
119 hash_for_each_possible(sec
->rela
->rela_hash
, rela
, hash
, o
)
120 if (rela
->offset
== o
)
126 struct rela
*find_rela_by_dest(struct section
*sec
, unsigned long offset
)
128 return find_rela_by_dest_range(sec
, offset
, 1);
131 struct symbol
*find_containing_func(struct section
*sec
, unsigned long offset
)
135 list_for_each_entry(func
, &sec
->symbol_list
, list
)
136 if (func
->type
== STT_FUNC
&& offset
>= func
->offset
&&
137 offset
< func
->offset
+ func
->len
)
143 static int read_sections(struct elf
*elf
)
147 size_t shstrndx
, sections_nr
;
150 if (elf_getshdrnum(elf
->elf
, §ions_nr
)) {
151 WARN_ELF("elf_getshdrnum");
155 if (elf_getshdrstrndx(elf
->elf
, &shstrndx
)) {
156 WARN_ELF("elf_getshdrstrndx");
160 for (i
= 0; i
< sections_nr
; i
++) {
161 sec
= malloc(sizeof(*sec
));
166 memset(sec
, 0, sizeof(*sec
));
168 INIT_LIST_HEAD(&sec
->symbol_list
);
169 INIT_LIST_HEAD(&sec
->rela_list
);
170 hash_init(sec
->rela_hash
);
171 hash_init(sec
->symbol_hash
);
173 list_add_tail(&sec
->list
, &elf
->sections
);
175 s
= elf_getscn(elf
->elf
, i
);
177 WARN_ELF("elf_getscn");
181 sec
->idx
= elf_ndxscn(s
);
183 if (!gelf_getshdr(s
, &sec
->sh
)) {
184 WARN_ELF("gelf_getshdr");
188 sec
->name
= elf_strptr(elf
->elf
, shstrndx
, sec
->sh
.sh_name
);
190 WARN_ELF("elf_strptr");
194 if (sec
->sh
.sh_size
!= 0) {
195 sec
->data
= elf_getdata(s
, NULL
);
197 WARN_ELF("elf_getdata");
200 if (sec
->data
->d_off
!= 0 ||
201 sec
->data
->d_size
!= sec
->sh
.sh_size
) {
202 WARN("unexpected data attributes for %s",
207 sec
->len
= sec
->sh
.sh_size
;
210 /* sanity check, one more call to elf_nextscn() should return NULL */
211 if (elf_nextscn(elf
->elf
, s
)) {
212 WARN("section entry mismatch");
219 static int read_symbols(struct elf
*elf
)
221 struct section
*symtab
, *sec
;
222 struct symbol
*sym
, *pfunc
;
223 struct list_head
*entry
, *tmp
;
227 symtab
= find_section_by_name(elf
, ".symtab");
229 WARN("missing symbol table");
233 symbols_nr
= symtab
->sh
.sh_size
/ symtab
->sh
.sh_entsize
;
235 for (i
= 0; i
< symbols_nr
; i
++) {
236 sym
= malloc(sizeof(*sym
));
241 memset(sym
, 0, sizeof(*sym
));
245 if (!gelf_getsym(symtab
->data
, i
, &sym
->sym
)) {
246 WARN_ELF("gelf_getsym");
250 sym
->name
= elf_strptr(elf
->elf
, symtab
->sh
.sh_link
,
253 WARN_ELF("elf_strptr");
257 sym
->type
= GELF_ST_TYPE(sym
->sym
.st_info
);
258 sym
->bind
= GELF_ST_BIND(sym
->sym
.st_info
);
260 if (sym
->sym
.st_shndx
> SHN_UNDEF
&&
261 sym
->sym
.st_shndx
< SHN_LORESERVE
) {
262 sym
->sec
= find_section_by_index(elf
,
265 WARN("couldn't find section for symbol %s",
269 if (sym
->type
== STT_SECTION
) {
270 sym
->name
= sym
->sec
->name
;
274 sym
->sec
= find_section_by_index(elf
, 0);
276 sym
->offset
= sym
->sym
.st_value
;
277 sym
->len
= sym
->sym
.st_size
;
279 /* sorted insert into a per-section list */
280 entry
= &sym
->sec
->symbol_list
;
281 list_for_each_prev(tmp
, &sym
->sec
->symbol_list
) {
284 s
= list_entry(tmp
, struct symbol
, list
);
286 if (sym
->offset
> s
->offset
) {
291 if (sym
->offset
== s
->offset
&& sym
->len
>= s
->len
) {
296 list_add(&sym
->list
, entry
);
297 hash_add(sym
->sec
->symbol_hash
, &sym
->hash
, sym
->idx
);
300 /* Create parent/child links for any cold subfunctions */
301 list_for_each_entry(sec
, &elf
->sections
, list
) {
302 list_for_each_entry(sym
, &sec
->symbol_list
, list
) {
303 char pname
[MAX_NAME_LEN
+ 1];
305 if (sym
->type
!= STT_FUNC
)
307 sym
->pfunc
= sym
->cfunc
= sym
;
308 coldstr
= strstr(sym
->name
, ".cold.");
312 pnamelen
= coldstr
- sym
->name
;
313 if (pnamelen
> MAX_NAME_LEN
) {
314 WARN("%s(): parent function name exceeds maximum length of %d characters",
315 sym
->name
, MAX_NAME_LEN
);
319 strncpy(pname
, sym
->name
, pnamelen
);
320 pname
[pnamelen
] = '\0';
321 pfunc
= find_symbol_by_name(elf
, pname
);
324 WARN("%s(): can't find parent function",
333 * Unfortunately, -fnoreorder-functions puts the child
334 * inside the parent. Remove the overlap so we can
335 * have sane assumptions.
337 * Note that pfunc->len now no longer matches
338 * pfunc->sym.st_size.
340 if (sym
->sec
== pfunc
->sec
&&
341 sym
->offset
>= pfunc
->offset
&&
342 sym
->offset
+ sym
->len
== pfunc
->offset
+ pfunc
->len
) {
343 pfunc
->len
-= sym
->len
;
355 static int read_relas(struct elf
*elf
)
362 list_for_each_entry(sec
, &elf
->sections
, list
) {
363 if (sec
->sh
.sh_type
!= SHT_RELA
)
366 sec
->base
= find_section_by_name(elf
, sec
->name
+ 5);
368 WARN("can't find base section for rela section %s",
373 sec
->base
->rela
= sec
;
375 for (i
= 0; i
< sec
->sh
.sh_size
/ sec
->sh
.sh_entsize
; i
++) {
376 rela
= malloc(sizeof(*rela
));
381 memset(rela
, 0, sizeof(*rela
));
383 if (!gelf_getrela(sec
->data
, i
, &rela
->rela
)) {
384 WARN_ELF("gelf_getrela");
388 rela
->type
= GELF_R_TYPE(rela
->rela
.r_info
);
389 rela
->addend
= rela
->rela
.r_addend
;
390 rela
->offset
= rela
->rela
.r_offset
;
391 symndx
= GELF_R_SYM(rela
->rela
.r_info
);
392 rela
->sym
= find_symbol_by_index(elf
, symndx
);
394 WARN("can't find rela entry symbol %d for %s",
399 list_add_tail(&rela
->list
, &sec
->rela_list
);
400 hash_add(sec
->rela_hash
, &rela
->hash
, rela
->offset
);
408 struct elf
*elf_open(const char *name
, int flags
)
413 elf_version(EV_CURRENT
);
415 elf
= malloc(sizeof(*elf
));
420 memset(elf
, 0, sizeof(*elf
));
422 INIT_LIST_HEAD(&elf
->sections
);
424 elf
->fd
= open(name
, flags
);
426 fprintf(stderr
, "objtool: Can't open '%s': %s\n",
427 name
, strerror(errno
));
431 if ((flags
& O_ACCMODE
) == O_RDONLY
)
432 cmd
= ELF_C_READ_MMAP
;
433 else if ((flags
& O_ACCMODE
) == O_RDWR
)
438 elf
->elf
= elf_begin(elf
->fd
, cmd
, NULL
);
440 WARN_ELF("elf_begin");
444 if (!gelf_getehdr(elf
->elf
, &elf
->ehdr
)) {
445 WARN_ELF("gelf_getehdr");
449 if (read_sections(elf
))
452 if (read_symbols(elf
))
465 struct section
*elf_create_section(struct elf
*elf
, const char *name
,
466 size_t entsize
, int nr
)
468 struct section
*sec
, *shstrtab
;
469 size_t size
= entsize
* nr
;
473 sec
= malloc(sizeof(*sec
));
478 memset(sec
, 0, sizeof(*sec
));
480 INIT_LIST_HEAD(&sec
->symbol_list
);
481 INIT_LIST_HEAD(&sec
->rela_list
);
482 hash_init(sec
->rela_hash
);
483 hash_init(sec
->symbol_hash
);
485 list_add_tail(&sec
->list
, &elf
->sections
);
487 s
= elf_newscn(elf
->elf
);
489 WARN_ELF("elf_newscn");
493 sec
->name
= strdup(name
);
499 sec
->idx
= elf_ndxscn(s
);
503 sec
->data
= elf_newdata(s
);
505 WARN_ELF("elf_newdata");
509 sec
->data
->d_size
= size
;
510 sec
->data
->d_align
= 1;
513 sec
->data
->d_buf
= malloc(size
);
514 if (!sec
->data
->d_buf
) {
518 memset(sec
->data
->d_buf
, 0, size
);
521 if (!gelf_getshdr(s
, &sec
->sh
)) {
522 WARN_ELF("gelf_getshdr");
526 sec
->sh
.sh_size
= size
;
527 sec
->sh
.sh_entsize
= entsize
;
528 sec
->sh
.sh_type
= SHT_PROGBITS
;
529 sec
->sh
.sh_addralign
= 1;
530 sec
->sh
.sh_flags
= SHF_ALLOC
;
533 /* Add section name to .shstrtab */
534 shstrtab
= find_section_by_name(elf
, ".shstrtab");
536 WARN("can't find .shstrtab section");
540 s
= elf_getscn(elf
->elf
, shstrtab
->idx
);
542 WARN_ELF("elf_getscn");
546 data
= elf_newdata(s
);
548 WARN_ELF("elf_newdata");
552 data
->d_buf
= sec
->name
;
553 data
->d_size
= strlen(name
) + 1;
556 sec
->sh
.sh_name
= shstrtab
->len
;
558 shstrtab
->len
+= strlen(name
) + 1;
559 shstrtab
->changed
= true;
564 struct section
*elf_create_rela_section(struct elf
*elf
, struct section
*base
)
569 relaname
= malloc(strlen(base
->name
) + strlen(".rela") + 1);
574 strcpy(relaname
, ".rela");
575 strcat(relaname
, base
->name
);
577 sec
= elf_create_section(elf
, relaname
, sizeof(GElf_Rela
), 0);
585 sec
->sh
.sh_type
= SHT_RELA
;
586 sec
->sh
.sh_addralign
= 8;
587 sec
->sh
.sh_link
= find_section_by_name(elf
, ".symtab")->idx
;
588 sec
->sh
.sh_info
= base
->idx
;
589 sec
->sh
.sh_flags
= SHF_INFO_LINK
;
594 int elf_rebuild_rela_section(struct section
*sec
)
597 int nr
, idx
= 0, size
;
601 list_for_each_entry(rela
, &sec
->rela_list
, list
)
604 size
= nr
* sizeof(*relas
);
605 relas
= malloc(size
);
611 sec
->data
->d_buf
= relas
;
612 sec
->data
->d_size
= size
;
614 sec
->sh
.sh_size
= size
;
617 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
618 relas
[idx
].r_offset
= rela
->offset
;
619 relas
[idx
].r_addend
= rela
->addend
;
620 relas
[idx
].r_info
= GELF_R_INFO(rela
->sym
->idx
, rela
->type
);
627 int elf_write(struct elf
*elf
)
632 /* Update section headers for changed sections: */
633 list_for_each_entry(sec
, &elf
->sections
, list
) {
635 s
= elf_getscn(elf
->elf
, sec
->idx
);
637 WARN_ELF("elf_getscn");
640 if (!gelf_update_shdr(s
, &sec
->sh
)) {
641 WARN_ELF("gelf_update_shdr");
647 /* Make sure the new section header entries get updated properly. */
648 elf_flagelf(elf
->elf
, ELF_C_SET
, ELF_F_DIRTY
);
650 /* Write all changes to the file. */
651 if (elf_update(elf
->elf
, ELF_C_WRITE
) < 0) {
652 WARN_ELF("elf_update");
659 void elf_close(struct elf
*elf
)
661 struct section
*sec
, *tmpsec
;
662 struct symbol
*sym
, *tmpsym
;
663 struct rela
*rela
, *tmprela
;
671 list_for_each_entry_safe(sec
, tmpsec
, &elf
->sections
, list
) {
672 list_for_each_entry_safe(sym
, tmpsym
, &sec
->symbol_list
, list
) {
673 list_del(&sym
->list
);
674 hash_del(&sym
->hash
);
677 list_for_each_entry_safe(rela
, tmprela
, &sec
->rela_list
, list
) {
678 list_del(&rela
->list
);
679 hash_del(&rela
->hash
);
682 list_del(&sec
->list
);