blob: b75d004f6482d8c049cbd29ae026a55427162e2d [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 * elf.c - ELF access library
3 *
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>
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <errno.h>
30
31#include "elf.h"
32#include "warn.h"
33
34#define MAX_NAME_LEN 128
35
36struct section *find_section_by_name(struct elf *elf, const char *name)
37{
38 struct section *sec;
39
40 list_for_each_entry(sec, &elf->sections, list)
41 if (!strcmp(sec->name, name))
42 return sec;
43
44 return NULL;
45}
46
47static struct section *find_section_by_index(struct elf *elf,
48 unsigned int idx)
49{
50 struct section *sec;
51
52 list_for_each_entry(sec, &elf->sections, list)
53 if (sec->idx == idx)
54 return sec;
55
56 return NULL;
57}
58
59static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
60{
61 struct section *sec;
62 struct symbol *sym;
63
64 list_for_each_entry(sec, &elf->sections, list)
65 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
66 if (sym->idx == idx)
67 return sym;
68
69 return NULL;
70}
71
72struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
73{
74 struct symbol *sym;
75
76 list_for_each_entry(sym, &sec->symbol_list, list)
77 if (sym->type != STT_SECTION &&
78 sym->offset == offset)
79 return sym;
80
81 return NULL;
82}
83
84struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
85{
86 struct section *sec;
87 struct symbol *sym;
88
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))
92 return sym;
93
94 return NULL;
95}
96
97struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
98{
99 struct symbol *sym;
100
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)
104 return sym;
105
106 return NULL;
107}
108
109struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
110 unsigned int len)
111{
112 struct rela *rela;
113 unsigned long o;
114
115 if (!sec->rela)
116 return NULL;
117
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)
121 return rela;
122
123 return NULL;
124}
125
126struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
127{
128 return find_rela_by_dest_range(sec, offset, 1);
129}
130
131struct symbol *find_containing_func(struct section *sec, unsigned long offset)
132{
133 struct symbol *func;
134
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)
138 return func;
139
140 return NULL;
141}
142
143static int read_sections(struct elf *elf)
144{
145 Elf_Scn *s = NULL;
146 struct section *sec;
147 size_t shstrndx, sections_nr;
148 int i;
149
150 if (elf_getshdrnum(elf->elf, &sections_nr)) {
151 WARN_ELF("elf_getshdrnum");
152 return -1;
153 }
154
155 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
156 WARN_ELF("elf_getshdrstrndx");
157 return -1;
158 }
159
160 for (i = 0; i < sections_nr; i++) {
161 sec = malloc(sizeof(*sec));
162 if (!sec) {
163 perror("malloc");
164 return -1;
165 }
166 memset(sec, 0, sizeof(*sec));
167
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);
172
173 list_add_tail(&sec->list, &elf->sections);
174
175 s = elf_getscn(elf->elf, i);
176 if (!s) {
177 WARN_ELF("elf_getscn");
178 return -1;
179 }
180
181 sec->idx = elf_ndxscn(s);
182
183 if (!gelf_getshdr(s, &sec->sh)) {
184 WARN_ELF("gelf_getshdr");
185 return -1;
186 }
187
188 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
189 if (!sec->name) {
190 WARN_ELF("elf_strptr");
191 return -1;
192 }
193
194 if (sec->sh.sh_size != 0) {
195 sec->data = elf_getdata(s, NULL);
196 if (!sec->data) {
197 WARN_ELF("elf_getdata");
198 return -1;
199 }
200 if (sec->data->d_off != 0 ||
201 sec->data->d_size != sec->sh.sh_size) {
202 WARN("unexpected data attributes for %s",
203 sec->name);
204 return -1;
205 }
206 }
207 sec->len = sec->sh.sh_size;
208 }
209
210 /* sanity check, one more call to elf_nextscn() should return NULL */
211 if (elf_nextscn(elf->elf, s)) {
212 WARN("section entry mismatch");
213 return -1;
214 }
215
216 return 0;
217}
218
219static int read_symbols(struct elf *elf)
220{
221 struct section *symtab, *sec;
222 struct symbol *sym, *pfunc;
223 struct list_head *entry, *tmp;
224 int symbols_nr, i;
225 char *coldstr;
226
227 symtab = find_section_by_name(elf, ".symtab");
228 if (!symtab) {
229 WARN("missing symbol table");
230 return -1;
231 }
232
233 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
234
235 for (i = 0; i < symbols_nr; i++) {
236 sym = malloc(sizeof(*sym));
237 if (!sym) {
238 perror("malloc");
239 return -1;
240 }
241 memset(sym, 0, sizeof(*sym));
242
243 sym->idx = i;
244
245 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
246 WARN_ELF("gelf_getsym");
247 goto err;
248 }
249
250 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
251 sym->sym.st_name);
252 if (!sym->name) {
253 WARN_ELF("elf_strptr");
254 goto err;
255 }
256
257 sym->type = GELF_ST_TYPE(sym->sym.st_info);
258 sym->bind = GELF_ST_BIND(sym->sym.st_info);
259
260 if (sym->sym.st_shndx > SHN_UNDEF &&
261 sym->sym.st_shndx < SHN_LORESERVE) {
262 sym->sec = find_section_by_index(elf,
263 sym->sym.st_shndx);
264 if (!sym->sec) {
265 WARN("couldn't find section for symbol %s",
266 sym->name);
267 goto err;
268 }
269 if (sym->type == STT_SECTION) {
270 sym->name = sym->sec->name;
271 sym->sec->sym = sym;
272 }
273 } else
274 sym->sec = find_section_by_index(elf, 0);
275
276 sym->offset = sym->sym.st_value;
277 sym->len = sym->sym.st_size;
278
279 /* sorted insert into a per-section list */
280 entry = &sym->sec->symbol_list;
281 list_for_each_prev(tmp, &sym->sec->symbol_list) {
282 struct symbol *s;
283
284 s = list_entry(tmp, struct symbol, list);
285
286 if (sym->offset > s->offset) {
287 entry = tmp;
288 break;
289 }
290
291 if (sym->offset == s->offset && sym->len >= s->len) {
292 entry = tmp;
293 break;
294 }
295 }
296 list_add(&sym->list, entry);
297 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
298 }
299
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];
304 size_t pnamelen;
305 if (sym->type != STT_FUNC)
306 continue;
307 sym->pfunc = sym->cfunc = sym;
308 coldstr = strstr(sym->name, ".cold.");
309 if (!coldstr)
310 continue;
311
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);
316 return -1;
317 }
318
319 strncpy(pname, sym->name, pnamelen);
320 pname[pnamelen] = '\0';
321 pfunc = find_symbol_by_name(elf, pname);
322
323 if (!pfunc) {
324 WARN("%s(): can't find parent function",
325 sym->name);
326 return -1;
327 }
328
329 sym->pfunc = pfunc;
330 pfunc->cfunc = sym;
331
332 /*
333 * Unfortunately, -fnoreorder-functions puts the child
334 * inside the parent. Remove the overlap so we can
335 * have sane assumptions.
336 *
337 * Note that pfunc->len now no longer matches
338 * pfunc->sym.st_size.
339 */
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;
344 }
345 }
346 }
347
348 return 0;
349
350err:
351 free(sym);
352 return -1;
353}
354
355static int read_relas(struct elf *elf)
356{
357 struct section *sec;
358 struct rela *rela;
359 int i;
360 unsigned int symndx;
361
362 list_for_each_entry(sec, &elf->sections, list) {
363 if (sec->sh.sh_type != SHT_RELA)
364 continue;
365
366 sec->base = find_section_by_name(elf, sec->name + 5);
367 if (!sec->base) {
368 WARN("can't find base section for rela section %s",
369 sec->name);
370 return -1;
371 }
372
373 sec->base->rela = sec;
374
375 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
376 rela = malloc(sizeof(*rela));
377 if (!rela) {
378 perror("malloc");
379 return -1;
380 }
381 memset(rela, 0, sizeof(*rela));
382
383 if (!gelf_getrela(sec->data, i, &rela->rela)) {
384 WARN_ELF("gelf_getrela");
385 return -1;
386 }
387
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);
393 if (!rela->sym) {
394 WARN("can't find rela entry symbol %d for %s",
395 symndx, sec->name);
396 return -1;
397 }
398
399 list_add_tail(&rela->list, &sec->rela_list);
400 hash_add(sec->rela_hash, &rela->hash, rela->offset);
401
402 }
403 }
404
405 return 0;
406}
407
408struct elf *elf_open(const char *name, int flags)
409{
410 struct elf *elf;
411 Elf_Cmd cmd;
412
413 elf_version(EV_CURRENT);
414
415 elf = malloc(sizeof(*elf));
416 if (!elf) {
417 perror("malloc");
418 return NULL;
419 }
420 memset(elf, 0, sizeof(*elf));
421
422 INIT_LIST_HEAD(&elf->sections);
423
424 elf->fd = open(name, flags);
425 if (elf->fd == -1) {
426 fprintf(stderr, "objtool: Can't open '%s': %s\n",
427 name, strerror(errno));
428 goto err;
429 }
430
431 if ((flags & O_ACCMODE) == O_RDONLY)
432 cmd = ELF_C_READ_MMAP;
433 else if ((flags & O_ACCMODE) == O_RDWR)
434 cmd = ELF_C_RDWR;
435 else /* O_WRONLY */
436 cmd = ELF_C_WRITE;
437
438 elf->elf = elf_begin(elf->fd, cmd, NULL);
439 if (!elf->elf) {
440 WARN_ELF("elf_begin");
441 goto err;
442 }
443
444 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
445 WARN_ELF("gelf_getehdr");
446 goto err;
447 }
448
449 if (read_sections(elf))
450 goto err;
451
452 if (read_symbols(elf))
453 goto err;
454
455 if (read_relas(elf))
456 goto err;
457
458 return elf;
459
460err:
461 elf_close(elf);
462 return NULL;
463}
464
465struct section *elf_create_section(struct elf *elf, const char *name,
466 size_t entsize, int nr)
467{
468 struct section *sec, *shstrtab;
469 size_t size = entsize * nr;
470 struct Elf_Scn *s;
471 Elf_Data *data;
472
473 sec = malloc(sizeof(*sec));
474 if (!sec) {
475 perror("malloc");
476 return NULL;
477 }
478 memset(sec, 0, sizeof(*sec));
479
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);
484
485 list_add_tail(&sec->list, &elf->sections);
486
487 s = elf_newscn(elf->elf);
488 if (!s) {
489 WARN_ELF("elf_newscn");
490 return NULL;
491 }
492
493 sec->name = strdup(name);
494 if (!sec->name) {
495 perror("strdup");
496 return NULL;
497 }
498
499 sec->idx = elf_ndxscn(s);
500 sec->len = size;
501 sec->changed = true;
502
503 sec->data = elf_newdata(s);
504 if (!sec->data) {
505 WARN_ELF("elf_newdata");
506 return NULL;
507 }
508
509 sec->data->d_size = size;
510 sec->data->d_align = 1;
511
512 if (size) {
513 sec->data->d_buf = malloc(size);
514 if (!sec->data->d_buf) {
515 perror("malloc");
516 return NULL;
517 }
518 memset(sec->data->d_buf, 0, size);
519 }
520
521 if (!gelf_getshdr(s, &sec->sh)) {
522 WARN_ELF("gelf_getshdr");
523 return NULL;
524 }
525
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;
531
532
533 /* Add section name to .shstrtab (or .strtab for Clang) */
534 shstrtab = find_section_by_name(elf, ".shstrtab");
535 if (!shstrtab)
536 shstrtab = find_section_by_name(elf, ".strtab");
537 if (!shstrtab) {
538 WARN("can't find .shstrtab or .strtab section");
539 return NULL;
540 }
541
542 s = elf_getscn(elf->elf, shstrtab->idx);
543 if (!s) {
544 WARN_ELF("elf_getscn");
545 return NULL;
546 }
547
548 data = elf_newdata(s);
549 if (!data) {
550 WARN_ELF("elf_newdata");
551 return NULL;
552 }
553
554 data->d_buf = sec->name;
555 data->d_size = strlen(name) + 1;
556 data->d_align = 1;
557
558 sec->sh.sh_name = shstrtab->len;
559
560 shstrtab->len += strlen(name) + 1;
561 shstrtab->changed = true;
562
563 return sec;
564}
565
566struct section *elf_create_rela_section(struct elf *elf, struct section *base)
567{
568 char *relaname;
569 struct section *sec;
570
571 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
572 if (!relaname) {
573 perror("malloc");
574 return NULL;
575 }
576 strcpy(relaname, ".rela");
577 strcat(relaname, base->name);
578
579 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
580 free(relaname);
581 if (!sec)
582 return NULL;
583
584 base->rela = sec;
585 sec->base = base;
586
587 sec->sh.sh_type = SHT_RELA;
588 sec->sh.sh_addralign = 8;
589 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
590 sec->sh.sh_info = base->idx;
591 sec->sh.sh_flags = SHF_INFO_LINK;
592
593 return sec;
594}
595
596int elf_rebuild_rela_section(struct section *sec)
597{
598 struct rela *rela;
599 int nr, idx = 0, size;
600 GElf_Rela *relas;
601
602 nr = 0;
603 list_for_each_entry(rela, &sec->rela_list, list)
604 nr++;
605
606 size = nr * sizeof(*relas);
607 relas = malloc(size);
608 if (!relas) {
609 perror("malloc");
610 return -1;
611 }
612
613 sec->data->d_buf = relas;
614 sec->data->d_size = size;
615
616 sec->sh.sh_size = size;
617
618 idx = 0;
619 list_for_each_entry(rela, &sec->rela_list, list) {
620 relas[idx].r_offset = rela->offset;
621 relas[idx].r_addend = rela->addend;
622 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
623 idx++;
624 }
625
626 return 0;
627}
628
629int elf_write(struct elf *elf)
630{
631 struct section *sec;
632 Elf_Scn *s;
633
634 /* Update section headers for changed sections: */
635 list_for_each_entry(sec, &elf->sections, list) {
636 if (sec->changed) {
637 s = elf_getscn(elf->elf, sec->idx);
638 if (!s) {
639 WARN_ELF("elf_getscn");
640 return -1;
641 }
642 if (!gelf_update_shdr(s, &sec->sh)) {
643 WARN_ELF("gelf_update_shdr");
644 return -1;
645 }
646 }
647 }
648
649 /* Make sure the new section header entries get updated properly. */
650 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
651
652 /* Write all changes to the file. */
653 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
654 WARN_ELF("elf_update");
655 return -1;
656 }
657
658 return 0;
659}
660
661void elf_close(struct elf *elf)
662{
663 struct section *sec, *tmpsec;
664 struct symbol *sym, *tmpsym;
665 struct rela *rela, *tmprela;
666
667 if (elf->elf)
668 elf_end(elf->elf);
669
670 if (elf->fd > 0)
671 close(elf->fd);
672
673 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
674 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
675 list_del(&sym->list);
676 hash_del(&sym->hash);
677 free(sym);
678 }
679 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
680 list_del(&rela->list);
681 hash_del(&rela->hash);
682 free(rela);
683 }
684 list_del(&sec->list);
685 free(sec);
686 }
687
688 free(elf);
689}