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 struct section
*find_section_by_name(struct elf
*elf
, const char *name
)
38 list_for_each_entry(sec
, &elf
->sections
, list
)
39 if (!strcmp(sec
->name
, name
))
45 static struct section
*find_section_by_index(struct elf
*elf
,
50 list_for_each_entry(sec
, &elf
->sections
, list
)
57 static struct symbol
*find_symbol_by_index(struct elf
*elf
, unsigned int idx
)
62 list_for_each_entry(sec
, &elf
->sections
, list
)
63 hash_for_each_possible(sec
->symbol_hash
, sym
, hash
, idx
)
70 struct symbol
*find_symbol_by_offset(struct section
*sec
, unsigned long offset
)
74 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
75 if (sym
->type
!= STT_SECTION
&&
76 sym
->offset
== offset
)
82 struct symbol
*find_symbol_containing(struct section
*sec
, unsigned long offset
)
86 list_for_each_entry(sym
, &sec
->symbol_list
, list
)
87 if (sym
->type
!= STT_SECTION
&&
88 offset
>= sym
->offset
&& offset
< sym
->offset
+ sym
->len
)
94 struct rela
*find_rela_by_dest_range(struct section
*sec
, unsigned long offset
,
103 for (o
= offset
; o
< offset
+ len
; o
++)
104 hash_for_each_possible(sec
->rela
->rela_hash
, rela
, hash
, o
)
105 if (rela
->offset
== o
)
111 struct rela
*find_rela_by_dest(struct section
*sec
, unsigned long offset
)
113 return find_rela_by_dest_range(sec
, offset
, 1);
116 struct symbol
*find_containing_func(struct section
*sec
, unsigned long offset
)
120 list_for_each_entry(func
, &sec
->symbol_list
, list
)
121 if (func
->type
== STT_FUNC
&& offset
>= func
->offset
&&
122 offset
< func
->offset
+ func
->len
)
128 static int read_sections(struct elf
*elf
)
132 size_t shstrndx
, sections_nr
;
135 if (elf_getshdrnum(elf
->elf
, §ions_nr
)) {
136 WARN_ELF("elf_getshdrnum");
140 if (elf_getshdrstrndx(elf
->elf
, &shstrndx
)) {
141 WARN_ELF("elf_getshdrstrndx");
145 for (i
= 0; i
< sections_nr
; i
++) {
146 sec
= malloc(sizeof(*sec
));
151 memset(sec
, 0, sizeof(*sec
));
153 INIT_LIST_HEAD(&sec
->symbol_list
);
154 INIT_LIST_HEAD(&sec
->rela_list
);
155 hash_init(sec
->rela_hash
);
156 hash_init(sec
->symbol_hash
);
158 list_add_tail(&sec
->list
, &elf
->sections
);
160 s
= elf_getscn(elf
->elf
, i
);
162 WARN_ELF("elf_getscn");
166 sec
->idx
= elf_ndxscn(s
);
168 if (!gelf_getshdr(s
, &sec
->sh
)) {
169 WARN_ELF("gelf_getshdr");
173 sec
->name
= elf_strptr(elf
->elf
, shstrndx
, sec
->sh
.sh_name
);
175 WARN_ELF("elf_strptr");
179 if (sec
->sh
.sh_size
!= 0) {
180 sec
->data
= elf_getdata(s
, NULL
);
182 WARN_ELF("elf_getdata");
185 if (sec
->data
->d_off
!= 0 ||
186 sec
->data
->d_size
!= sec
->sh
.sh_size
) {
187 WARN("unexpected data attributes for %s",
192 sec
->len
= sec
->sh
.sh_size
;
195 /* sanity check, one more call to elf_nextscn() should return NULL */
196 if (elf_nextscn(elf
->elf
, s
)) {
197 WARN("section entry mismatch");
204 static int read_symbols(struct elf
*elf
)
206 struct section
*symtab
;
208 struct list_head
*entry
, *tmp
;
211 symtab
= find_section_by_name(elf
, ".symtab");
213 WARN("missing symbol table");
217 symbols_nr
= symtab
->sh
.sh_size
/ symtab
->sh
.sh_entsize
;
219 for (i
= 0; i
< symbols_nr
; i
++) {
220 sym
= malloc(sizeof(*sym
));
225 memset(sym
, 0, sizeof(*sym
));
229 if (!gelf_getsym(symtab
->data
, i
, &sym
->sym
)) {
230 WARN_ELF("gelf_getsym");
234 sym
->name
= elf_strptr(elf
->elf
, symtab
->sh
.sh_link
,
237 WARN_ELF("elf_strptr");
241 sym
->type
= GELF_ST_TYPE(sym
->sym
.st_info
);
242 sym
->bind
= GELF_ST_BIND(sym
->sym
.st_info
);
244 if (sym
->sym
.st_shndx
> SHN_UNDEF
&&
245 sym
->sym
.st_shndx
< SHN_LORESERVE
) {
246 sym
->sec
= find_section_by_index(elf
,
249 WARN("couldn't find section for symbol %s",
253 if (sym
->type
== STT_SECTION
) {
254 sym
->name
= sym
->sec
->name
;
258 sym
->sec
= find_section_by_index(elf
, 0);
260 sym
->offset
= sym
->sym
.st_value
;
261 sym
->len
= sym
->sym
.st_size
;
263 /* sorted insert into a per-section list */
264 entry
= &sym
->sec
->symbol_list
;
265 list_for_each_prev(tmp
, &sym
->sec
->symbol_list
) {
268 s
= list_entry(tmp
, struct symbol
, list
);
270 if (sym
->offset
> s
->offset
) {
275 if (sym
->offset
== s
->offset
&& sym
->len
>= s
->len
) {
280 list_add(&sym
->list
, entry
);
281 hash_add(sym
->sec
->symbol_hash
, &sym
->hash
, sym
->idx
);
291 static int read_relas(struct elf
*elf
)
298 list_for_each_entry(sec
, &elf
->sections
, list
) {
299 if (sec
->sh
.sh_type
!= SHT_RELA
)
302 sec
->base
= find_section_by_name(elf
, sec
->name
+ 5);
304 WARN("can't find base section for rela section %s",
309 sec
->base
->rela
= sec
;
311 for (i
= 0; i
< sec
->sh
.sh_size
/ sec
->sh
.sh_entsize
; i
++) {
312 rela
= malloc(sizeof(*rela
));
317 memset(rela
, 0, sizeof(*rela
));
319 if (!gelf_getrela(sec
->data
, i
, &rela
->rela
)) {
320 WARN_ELF("gelf_getrela");
324 rela
->type
= GELF_R_TYPE(rela
->rela
.r_info
);
325 rela
->addend
= rela
->rela
.r_addend
;
326 rela
->offset
= rela
->rela
.r_offset
;
327 symndx
= GELF_R_SYM(rela
->rela
.r_info
);
328 rela
->sym
= find_symbol_by_index(elf
, symndx
);
330 WARN("can't find rela entry symbol %d for %s",
335 list_add_tail(&rela
->list
, &sec
->rela_list
);
336 hash_add(sec
->rela_hash
, &rela
->hash
, rela
->offset
);
344 struct elf
*elf_open(const char *name
, int flags
)
349 elf_version(EV_CURRENT
);
351 elf
= malloc(sizeof(*elf
));
356 memset(elf
, 0, sizeof(*elf
));
358 INIT_LIST_HEAD(&elf
->sections
);
360 elf
->fd
= open(name
, flags
);
362 fprintf(stderr
, "objtool: Can't open '%s': %s\n",
363 name
, strerror(errno
));
367 if ((flags
& O_ACCMODE
) == O_RDONLY
)
368 cmd
= ELF_C_READ_MMAP
;
369 else if ((flags
& O_ACCMODE
) == O_RDWR
)
374 elf
->elf
= elf_begin(elf
->fd
, cmd
, NULL
);
376 WARN_ELF("elf_begin");
380 if (!gelf_getehdr(elf
->elf
, &elf
->ehdr
)) {
381 WARN_ELF("gelf_getehdr");
385 if (read_sections(elf
))
388 if (read_symbols(elf
))
401 struct section
*elf_create_section(struct elf
*elf
, const char *name
,
402 size_t entsize
, int nr
)
404 struct section
*sec
, *shstrtab
;
405 size_t size
= entsize
* nr
;
409 sec
= malloc(sizeof(*sec
));
414 memset(sec
, 0, sizeof(*sec
));
416 INIT_LIST_HEAD(&sec
->symbol_list
);
417 INIT_LIST_HEAD(&sec
->rela_list
);
418 hash_init(sec
->rela_hash
);
419 hash_init(sec
->symbol_hash
);
421 list_add_tail(&sec
->list
, &elf
->sections
);
423 s
= elf_newscn(elf
->elf
);
425 WARN_ELF("elf_newscn");
429 sec
->name
= strdup(name
);
435 sec
->idx
= elf_ndxscn(s
);
439 sec
->data
= elf_newdata(s
);
441 WARN_ELF("elf_newdata");
445 sec
->data
->d_size
= size
;
446 sec
->data
->d_align
= 1;
449 sec
->data
->d_buf
= malloc(size
);
450 if (!sec
->data
->d_buf
) {
454 memset(sec
->data
->d_buf
, 0, size
);
457 if (!gelf_getshdr(s
, &sec
->sh
)) {
458 WARN_ELF("gelf_getshdr");
462 sec
->sh
.sh_size
= size
;
463 sec
->sh
.sh_entsize
= entsize
;
464 sec
->sh
.sh_type
= SHT_PROGBITS
;
465 sec
->sh
.sh_addralign
= 1;
466 sec
->sh
.sh_flags
= SHF_ALLOC
;
469 /* Add section name to .shstrtab */
470 shstrtab
= find_section_by_name(elf
, ".shstrtab");
472 WARN("can't find .shstrtab section");
476 s
= elf_getscn(elf
->elf
, shstrtab
->idx
);
478 WARN_ELF("elf_getscn");
482 data
= elf_newdata(s
);
484 WARN_ELF("elf_newdata");
488 data
->d_buf
= sec
->name
;
489 data
->d_size
= strlen(name
) + 1;
492 sec
->sh
.sh_name
= shstrtab
->len
;
494 shstrtab
->len
+= strlen(name
) + 1;
495 shstrtab
->changed
= true;
500 struct section
*elf_create_rela_section(struct elf
*elf
, struct section
*base
)
505 relaname
= malloc(strlen(base
->name
) + strlen(".rela") + 1);
510 strcpy(relaname
, ".rela");
511 strcat(relaname
, base
->name
);
513 sec
= elf_create_section(elf
, relaname
, sizeof(GElf_Rela
), 0);
521 sec
->sh
.sh_type
= SHT_RELA
;
522 sec
->sh
.sh_addralign
= 8;
523 sec
->sh
.sh_link
= find_section_by_name(elf
, ".symtab")->idx
;
524 sec
->sh
.sh_info
= base
->idx
;
525 sec
->sh
.sh_flags
= SHF_INFO_LINK
;
530 int elf_rebuild_rela_section(struct section
*sec
)
533 int nr
, idx
= 0, size
;
537 list_for_each_entry(rela
, &sec
->rela_list
, list
)
540 size
= nr
* sizeof(*relas
);
541 relas
= malloc(size
);
547 sec
->data
->d_buf
= relas
;
548 sec
->data
->d_size
= size
;
550 sec
->sh
.sh_size
= size
;
553 list_for_each_entry(rela
, &sec
->rela_list
, list
) {
554 relas
[idx
].r_offset
= rela
->offset
;
555 relas
[idx
].r_addend
= rela
->addend
;
556 relas
[idx
].r_info
= GELF_R_INFO(rela
->sym
->idx
, rela
->type
);
563 int elf_write(struct elf
*elf
)
568 /* Update section headers for changed sections: */
569 list_for_each_entry(sec
, &elf
->sections
, list
) {
571 s
= elf_getscn(elf
->elf
, sec
->idx
);
573 WARN_ELF("elf_getscn");
576 if (!gelf_update_shdr(s
, &sec
->sh
)) {
577 WARN_ELF("gelf_update_shdr");
583 /* Make sure the new section header entries get updated properly. */
584 elf_flagelf(elf
->elf
, ELF_C_SET
, ELF_F_DIRTY
);
586 /* Write all changes to the file. */
587 if (elf_update(elf
->elf
, ELF_C_WRITE
) < 0) {
588 WARN_ELF("elf_update");
595 void elf_close(struct elf
*elf
)
597 struct section
*sec
, *tmpsec
;
598 struct symbol
*sym
, *tmpsym
;
599 struct rela
*rela
, *tmprela
;
607 list_for_each_entry_safe(sec
, tmpsec
, &elf
->sections
, list
) {
608 list_for_each_entry_safe(sym
, tmpsym
, &sec
->symbol_list
, list
) {
609 list_del(&sym
->list
);
610 hash_del(&sym
->hash
);
613 list_for_each_entry_safe(rela
, tmprela
, &sec
->rela_list
, list
) {
614 list_del(&rela
->list
);
615 hash_del(&rela
->hash
);
618 list_del(&sec
->list
);