blob: c1c338661699788c8189becaab8465ed1bdcd775 [file] [log] [blame]
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001/*
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>
Josh Poimboeuf385d11b2018-01-15 08:17:08 -060029#include <errno.h>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060030
31#include "elf.h"
32#include "warn.h"
33
34struct section *find_section_by_name(struct elf *elf, const char *name)
35{
36 struct section *sec;
37
38 list_for_each_entry(sec, &elf->sections, list)
39 if (!strcmp(sec->name, name))
40 return sec;
41
42 return NULL;
43}
44
45static struct section *find_section_by_index(struct elf *elf,
46 unsigned int idx)
47{
48 struct section *sec;
49
50 list_for_each_entry(sec, &elf->sections, list)
51 if (sec->idx == idx)
52 return sec;
53
54 return NULL;
55}
56
57static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
58{
59 struct section *sec;
60 struct symbol *sym;
61
62 list_for_each_entry(sec, &elf->sections, list)
Josh Poimboeuf042ba732016-03-09 00:07:00 -060063 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060064 if (sym->idx == idx)
65 return sym;
66
67 return NULL;
68}
69
70struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
71{
72 struct symbol *sym;
73
Josh Poimboeufa196e172016-03-09 00:06:57 -060074 list_for_each_entry(sym, &sec->symbol_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060075 if (sym->type != STT_SECTION &&
76 sym->offset == offset)
77 return sym;
78
79 return NULL;
80}
81
Josh Poimboeuf5c51f4a2017-03-02 16:57:23 -060082struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
83{
84 struct symbol *sym;
85
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)
89 return sym;
90
91 return NULL;
92}
93
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060094struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
95 unsigned int len)
96{
97 struct rela *rela;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060098 unsigned long o;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060099
100 if (!sec->rela)
101 return NULL;
102
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600103 for (o = offset; o < offset + len; o++)
104 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
105 if (rela->offset == o)
106 return rela;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600107
108 return NULL;
109}
110
111struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
112{
113 return find_rela_by_dest_range(sec, offset, 1);
114}
115
116struct symbol *find_containing_func(struct section *sec, unsigned long offset)
117{
118 struct symbol *func;
119
Josh Poimboeufa196e172016-03-09 00:06:57 -0600120 list_for_each_entry(func, &sec->symbol_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600121 if (func->type == STT_FUNC && offset >= func->offset &&
122 offset < func->offset + func->len)
123 return func;
124
125 return NULL;
126}
127
128static int read_sections(struct elf *elf)
129{
130 Elf_Scn *s = NULL;
131 struct section *sec;
132 size_t shstrndx, sections_nr;
133 int i;
134
135 if (elf_getshdrnum(elf->elf, &sections_nr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500136 WARN_ELF("elf_getshdrnum");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600137 return -1;
138 }
139
140 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500141 WARN_ELF("elf_getshdrstrndx");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600142 return -1;
143 }
144
145 for (i = 0; i < sections_nr; i++) {
146 sec = malloc(sizeof(*sec));
147 if (!sec) {
148 perror("malloc");
149 return -1;
150 }
151 memset(sec, 0, sizeof(*sec));
152
Josh Poimboeufa196e172016-03-09 00:06:57 -0600153 INIT_LIST_HEAD(&sec->symbol_list);
154 INIT_LIST_HEAD(&sec->rela_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600155 hash_init(sec->rela_hash);
156 hash_init(sec->symbol_hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600157
158 list_add_tail(&sec->list, &elf->sections);
159
160 s = elf_getscn(elf->elf, i);
161 if (!s) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500162 WARN_ELF("elf_getscn");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600163 return -1;
164 }
165
166 sec->idx = elf_ndxscn(s);
167
168 if (!gelf_getshdr(s, &sec->sh)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500169 WARN_ELF("gelf_getshdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600170 return -1;
171 }
172
173 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
174 if (!sec->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500175 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600176 return -1;
177 }
178
Petr Vandrovecdf968c92017-09-15 02:15:05 -0500179 if (sec->sh.sh_size != 0) {
180 sec->data = elf_getdata(s, NULL);
181 if (!sec->data) {
182 WARN_ELF("elf_getdata");
183 return -1;
184 }
185 if (sec->data->d_off != 0 ||
186 sec->data->d_size != sec->sh.sh_size) {
187 WARN("unexpected data attributes for %s",
188 sec->name);
189 return -1;
190 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600191 }
Petr Vandrovecdf968c92017-09-15 02:15:05 -0500192 sec->len = sec->sh.sh_size;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600193 }
194
195 /* sanity check, one more call to elf_nextscn() should return NULL */
196 if (elf_nextscn(elf->elf, s)) {
197 WARN("section entry mismatch");
198 return -1;
199 }
200
201 return 0;
202}
203
204static int read_symbols(struct elf *elf)
205{
206 struct section *symtab;
207 struct symbol *sym;
208 struct list_head *entry, *tmp;
209 int symbols_nr, i;
210
211 symtab = find_section_by_name(elf, ".symtab");
212 if (!symtab) {
213 WARN("missing symbol table");
214 return -1;
215 }
216
217 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
218
219 for (i = 0; i < symbols_nr; i++) {
220 sym = malloc(sizeof(*sym));
221 if (!sym) {
222 perror("malloc");
223 return -1;
224 }
225 memset(sym, 0, sizeof(*sym));
226
227 sym->idx = i;
228
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500229 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
230 WARN_ELF("gelf_getsym");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600231 goto err;
232 }
233
234 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
235 sym->sym.st_name);
236 if (!sym->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500237 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600238 goto err;
239 }
240
241 sym->type = GELF_ST_TYPE(sym->sym.st_info);
242 sym->bind = GELF_ST_BIND(sym->sym.st_info);
243
244 if (sym->sym.st_shndx > SHN_UNDEF &&
245 sym->sym.st_shndx < SHN_LORESERVE) {
246 sym->sec = find_section_by_index(elf,
247 sym->sym.st_shndx);
248 if (!sym->sec) {
249 WARN("couldn't find section for symbol %s",
250 sym->name);
251 goto err;
252 }
253 if (sym->type == STT_SECTION) {
254 sym->name = sym->sec->name;
255 sym->sec->sym = sym;
256 }
257 } else
258 sym->sec = find_section_by_index(elf, 0);
259
260 sym->offset = sym->sym.st_value;
261 sym->len = sym->sym.st_size;
262
263 /* sorted insert into a per-section list */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600264 entry = &sym->sec->symbol_list;
265 list_for_each_prev(tmp, &sym->sec->symbol_list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600266 struct symbol *s;
267
268 s = list_entry(tmp, struct symbol, list);
269
270 if (sym->offset > s->offset) {
271 entry = tmp;
272 break;
273 }
274
275 if (sym->offset == s->offset && sym->len >= s->len) {
276 entry = tmp;
277 break;
278 }
279 }
280 list_add(&sym->list, entry);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600281 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600282 }
283
284 return 0;
285
286err:
287 free(sym);
288 return -1;
289}
290
291static int read_relas(struct elf *elf)
292{
293 struct section *sec;
294 struct rela *rela;
295 int i;
296 unsigned int symndx;
297
298 list_for_each_entry(sec, &elf->sections, list) {
299 if (sec->sh.sh_type != SHT_RELA)
300 continue;
301
302 sec->base = find_section_by_name(elf, sec->name + 5);
303 if (!sec->base) {
304 WARN("can't find base section for rela section %s",
305 sec->name);
306 return -1;
307 }
308
309 sec->base->rela = sec;
310
311 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
312 rela = malloc(sizeof(*rela));
313 if (!rela) {
314 perror("malloc");
315 return -1;
316 }
317 memset(rela, 0, sizeof(*rela));
318
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500319 if (!gelf_getrela(sec->data, i, &rela->rela)) {
320 WARN_ELF("gelf_getrela");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600321 return -1;
322 }
323
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);
329 if (!rela->sym) {
330 WARN("can't find rela entry symbol %d for %s",
331 symndx, sec->name);
332 return -1;
333 }
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600334
335 list_add_tail(&rela->list, &sec->rela_list);
336 hash_add(sec->rela_hash, &rela->hash, rela->offset);
337
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600338 }
339 }
340
341 return 0;
342}
343
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500344struct elf *elf_open(const char *name, int flags)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600345{
346 struct elf *elf;
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500347 Elf_Cmd cmd;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600348
349 elf_version(EV_CURRENT);
350
351 elf = malloc(sizeof(*elf));
352 if (!elf) {
353 perror("malloc");
354 return NULL;
355 }
356 memset(elf, 0, sizeof(*elf));
357
358 INIT_LIST_HEAD(&elf->sections);
359
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500360 elf->fd = open(name, flags);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600361 if (elf->fd == -1) {
Josh Poimboeuf385d11b2018-01-15 08:17:08 -0600362 fprintf(stderr, "objtool: Can't open '%s': %s\n",
363 name, strerror(errno));
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600364 goto err;
365 }
366
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500367 if ((flags & O_ACCMODE) == O_RDONLY)
368 cmd = ELF_C_READ_MMAP;
369 else if ((flags & O_ACCMODE) == O_RDWR)
370 cmd = ELF_C_RDWR;
371 else /* O_WRONLY */
372 cmd = ELF_C_WRITE;
373
374 elf->elf = elf_begin(elf->fd, cmd, NULL);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600375 if (!elf->elf) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500376 WARN_ELF("elf_begin");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600377 goto err;
378 }
379
380 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500381 WARN_ELF("gelf_getehdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600382 goto err;
383 }
384
385 if (read_sections(elf))
386 goto err;
387
388 if (read_symbols(elf))
389 goto err;
390
391 if (read_relas(elf))
392 goto err;
393
394 return elf;
395
396err:
397 elf_close(elf);
398 return NULL;
399}
400
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500401struct section *elf_create_section(struct elf *elf, const char *name,
402 size_t entsize, int nr)
403{
404 struct section *sec, *shstrtab;
405 size_t size = entsize * nr;
406 struct Elf_Scn *s;
407 Elf_Data *data;
408
409 sec = malloc(sizeof(*sec));
410 if (!sec) {
411 perror("malloc");
412 return NULL;
413 }
414 memset(sec, 0, sizeof(*sec));
415
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);
420
421 list_add_tail(&sec->list, &elf->sections);
422
423 s = elf_newscn(elf->elf);
424 if (!s) {
425 WARN_ELF("elf_newscn");
426 return NULL;
427 }
428
429 sec->name = strdup(name);
430 if (!sec->name) {
431 perror("strdup");
432 return NULL;
433 }
434
435 sec->idx = elf_ndxscn(s);
436 sec->len = size;
437 sec->changed = true;
438
439 sec->data = elf_newdata(s);
440 if (!sec->data) {
441 WARN_ELF("elf_newdata");
442 return NULL;
443 }
444
445 sec->data->d_size = size;
446 sec->data->d_align = 1;
447
448 if (size) {
449 sec->data->d_buf = malloc(size);
450 if (!sec->data->d_buf) {
451 perror("malloc");
452 return NULL;
453 }
454 memset(sec->data->d_buf, 0, size);
455 }
456
457 if (!gelf_getshdr(s, &sec->sh)) {
458 WARN_ELF("gelf_getshdr");
459 return NULL;
460 }
461
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;
467
468
469 /* Add section name to .shstrtab */
470 shstrtab = find_section_by_name(elf, ".shstrtab");
471 if (!shstrtab) {
472 WARN("can't find .shstrtab section");
473 return NULL;
474 }
475
476 s = elf_getscn(elf->elf, shstrtab->idx);
477 if (!s) {
478 WARN_ELF("elf_getscn");
479 return NULL;
480 }
481
482 data = elf_newdata(s);
483 if (!data) {
484 WARN_ELF("elf_newdata");
485 return NULL;
486 }
487
488 data->d_buf = sec->name;
489 data->d_size = strlen(name) + 1;
490 data->d_align = 1;
491
492 sec->sh.sh_name = shstrtab->len;
493
494 shstrtab->len += strlen(name) + 1;
495 shstrtab->changed = true;
496
497 return sec;
498}
499
500struct section *elf_create_rela_section(struct elf *elf, struct section *base)
501{
502 char *relaname;
503 struct section *sec;
504
505 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
506 if (!relaname) {
507 perror("malloc");
508 return NULL;
509 }
510 strcpy(relaname, ".rela");
511 strcat(relaname, base->name);
512
513 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
Martin Kepplinger0998b7a2017-09-14 08:01:38 +0200514 free(relaname);
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500515 if (!sec)
516 return NULL;
517
518 base->rela = sec;
519 sec->base = base;
520
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;
526
527 return sec;
528}
529
530int elf_rebuild_rela_section(struct section *sec)
531{
532 struct rela *rela;
533 int nr, idx = 0, size;
534 GElf_Rela *relas;
535
536 nr = 0;
537 list_for_each_entry(rela, &sec->rela_list, list)
538 nr++;
539
540 size = nr * sizeof(*relas);
541 relas = malloc(size);
542 if (!relas) {
543 perror("malloc");
544 return -1;
545 }
546
547 sec->data->d_buf = relas;
548 sec->data->d_size = size;
549
550 sec->sh.sh_size = size;
551
552 idx = 0;
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);
557 idx++;
558 }
559
560 return 0;
561}
562
563int elf_write(struct elf *elf)
564{
565 struct section *sec;
566 Elf_Scn *s;
567
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500568 /* Update section headers for changed sections: */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500569 list_for_each_entry(sec, &elf->sections, list) {
570 if (sec->changed) {
571 s = elf_getscn(elf->elf, sec->idx);
572 if (!s) {
573 WARN_ELF("elf_getscn");
574 return -1;
575 }
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500576 if (!gelf_update_shdr(s, &sec->sh)) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500577 WARN_ELF("gelf_update_shdr");
578 return -1;
579 }
580 }
581 }
582
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500583 /* Make sure the new section header entries get updated properly. */
584 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
585
586 /* Write all changes to the file. */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500587 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
588 WARN_ELF("elf_update");
589 return -1;
590 }
591
592 return 0;
593}
594
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600595void elf_close(struct elf *elf)
596{
597 struct section *sec, *tmpsec;
598 struct symbol *sym, *tmpsym;
599 struct rela *rela, *tmprela;
600
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500601 if (elf->elf)
602 elf_end(elf->elf);
603
604 if (elf->fd > 0)
605 close(elf->fd);
606
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600607 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600608 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600609 list_del(&sym->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600610 hash_del(&sym->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600611 free(sym);
612 }
Josh Poimboeufa196e172016-03-09 00:06:57 -0600613 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600614 list_del(&rela->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600615 hash_del(&rela->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600616 free(rela);
617 }
618 list_del(&sec->list);
619 free(sec);
620 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500621
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600622 free(elf);
623}