blob: 1e89a5f8bfc9ccb6f8a0384b29349b26f99264b8 [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>
29
30#include "elf.h"
31#include "warn.h"
32
33struct section *find_section_by_name(struct elf *elf, const char *name)
34{
35 struct section *sec;
36
37 list_for_each_entry(sec, &elf->sections, list)
38 if (!strcmp(sec->name, name))
39 return sec;
40
41 return NULL;
42}
43
44static struct section *find_section_by_index(struct elf *elf,
45 unsigned int idx)
46{
47 struct section *sec;
48
49 list_for_each_entry(sec, &elf->sections, list)
50 if (sec->idx == idx)
51 return sec;
52
53 return NULL;
54}
55
56static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
57{
58 struct section *sec;
59 struct symbol *sym;
60
61 list_for_each_entry(sec, &elf->sections, list)
Josh Poimboeuf042ba732016-03-09 00:07:00 -060062 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060063 if (sym->idx == idx)
64 return sym;
65
66 return NULL;
67}
68
69struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
70{
71 struct symbol *sym;
72
Josh Poimboeufa196e172016-03-09 00:06:57 -060073 list_for_each_entry(sym, &sec->symbol_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060074 if (sym->type != STT_SECTION &&
75 sym->offset == offset)
76 return sym;
77
78 return NULL;
79}
80
Josh Poimboeuf5c51f4a2017-03-02 16:57:23 -060081struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
82{
83 struct symbol *sym;
84
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)
88 return sym;
89
90 return NULL;
91}
92
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060093struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
94 unsigned int len)
95{
96 struct rela *rela;
Josh Poimboeuf042ba732016-03-09 00:07:00 -060097 unsigned long o;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060098
99 if (!sec->rela)
100 return NULL;
101
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600102 for (o = offset; o < offset + len; o++)
103 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
104 if (rela->offset == o)
105 return rela;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600106
107 return NULL;
108}
109
110struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
111{
112 return find_rela_by_dest_range(sec, offset, 1);
113}
114
115struct symbol *find_containing_func(struct section *sec, unsigned long offset)
116{
117 struct symbol *func;
118
Josh Poimboeufa196e172016-03-09 00:06:57 -0600119 list_for_each_entry(func, &sec->symbol_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600120 if (func->type == STT_FUNC && offset >= func->offset &&
121 offset < func->offset + func->len)
122 return func;
123
124 return NULL;
125}
126
127static int read_sections(struct elf *elf)
128{
129 Elf_Scn *s = NULL;
130 struct section *sec;
131 size_t shstrndx, sections_nr;
132 int i;
133
134 if (elf_getshdrnum(elf->elf, &sections_nr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500135 WARN_ELF("elf_getshdrnum");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600136 return -1;
137 }
138
139 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500140 WARN_ELF("elf_getshdrstrndx");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600141 return -1;
142 }
143
144 for (i = 0; i < sections_nr; i++) {
145 sec = malloc(sizeof(*sec));
146 if (!sec) {
147 perror("malloc");
148 return -1;
149 }
150 memset(sec, 0, sizeof(*sec));
151
Josh Poimboeufa196e172016-03-09 00:06:57 -0600152 INIT_LIST_HEAD(&sec->symbol_list);
153 INIT_LIST_HEAD(&sec->rela_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600154 hash_init(sec->rela_hash);
155 hash_init(sec->symbol_hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600156
157 list_add_tail(&sec->list, &elf->sections);
158
159 s = elf_getscn(elf->elf, i);
160 if (!s) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500161 WARN_ELF("elf_getscn");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600162 return -1;
163 }
164
165 sec->idx = elf_ndxscn(s);
166
167 if (!gelf_getshdr(s, &sec->sh)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500168 WARN_ELF("gelf_getshdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600169 return -1;
170 }
171
172 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
173 if (!sec->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500174 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600175 return -1;
176 }
177
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500178 sec->data = elf_getdata(s, NULL);
179 if (!sec->data) {
180 WARN_ELF("elf_getdata");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600181 return -1;
182 }
183
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500184 if (sec->data->d_off != 0 ||
185 sec->data->d_size != sec->sh.sh_size) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600186 WARN("unexpected data attributes for %s", sec->name);
187 return -1;
188 }
189
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500190 sec->len = sec->data->d_size;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600191 }
192
193 /* sanity check, one more call to elf_nextscn() should return NULL */
194 if (elf_nextscn(elf->elf, s)) {
195 WARN("section entry mismatch");
196 return -1;
197 }
198
199 return 0;
200}
201
202static int read_symbols(struct elf *elf)
203{
204 struct section *symtab;
205 struct symbol *sym;
206 struct list_head *entry, *tmp;
207 int symbols_nr, i;
208
209 symtab = find_section_by_name(elf, ".symtab");
210 if (!symtab) {
211 WARN("missing symbol table");
212 return -1;
213 }
214
215 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
216
217 for (i = 0; i < symbols_nr; i++) {
218 sym = malloc(sizeof(*sym));
219 if (!sym) {
220 perror("malloc");
221 return -1;
222 }
223 memset(sym, 0, sizeof(*sym));
224
225 sym->idx = i;
226
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500227 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
228 WARN_ELF("gelf_getsym");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600229 goto err;
230 }
231
232 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
233 sym->sym.st_name);
234 if (!sym->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500235 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600236 goto err;
237 }
238
239 sym->type = GELF_ST_TYPE(sym->sym.st_info);
240 sym->bind = GELF_ST_BIND(sym->sym.st_info);
241
242 if (sym->sym.st_shndx > SHN_UNDEF &&
243 sym->sym.st_shndx < SHN_LORESERVE) {
244 sym->sec = find_section_by_index(elf,
245 sym->sym.st_shndx);
246 if (!sym->sec) {
247 WARN("couldn't find section for symbol %s",
248 sym->name);
249 goto err;
250 }
251 if (sym->type == STT_SECTION) {
252 sym->name = sym->sec->name;
253 sym->sec->sym = sym;
254 }
255 } else
256 sym->sec = find_section_by_index(elf, 0);
257
258 sym->offset = sym->sym.st_value;
259 sym->len = sym->sym.st_size;
260
261 /* sorted insert into a per-section list */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600262 entry = &sym->sec->symbol_list;
263 list_for_each_prev(tmp, &sym->sec->symbol_list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600264 struct symbol *s;
265
266 s = list_entry(tmp, struct symbol, list);
267
268 if (sym->offset > s->offset) {
269 entry = tmp;
270 break;
271 }
272
273 if (sym->offset == s->offset && sym->len >= s->len) {
274 entry = tmp;
275 break;
276 }
277 }
278 list_add(&sym->list, entry);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600279 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600280 }
281
282 return 0;
283
284err:
285 free(sym);
286 return -1;
287}
288
289static int read_relas(struct elf *elf)
290{
291 struct section *sec;
292 struct rela *rela;
293 int i;
294 unsigned int symndx;
295
296 list_for_each_entry(sec, &elf->sections, list) {
297 if (sec->sh.sh_type != SHT_RELA)
298 continue;
299
300 sec->base = find_section_by_name(elf, sec->name + 5);
301 if (!sec->base) {
302 WARN("can't find base section for rela section %s",
303 sec->name);
304 return -1;
305 }
306
307 sec->base->rela = sec;
308
309 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
310 rela = malloc(sizeof(*rela));
311 if (!rela) {
312 perror("malloc");
313 return -1;
314 }
315 memset(rela, 0, sizeof(*rela));
316
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500317 if (!gelf_getrela(sec->data, i, &rela->rela)) {
318 WARN_ELF("gelf_getrela");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600319 return -1;
320 }
321
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);
327 if (!rela->sym) {
328 WARN("can't find rela entry symbol %d for %s",
329 symndx, sec->name);
330 return -1;
331 }
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600332
333 list_add_tail(&rela->list, &sec->rela_list);
334 hash_add(sec->rela_hash, &rela->hash, rela->offset);
335
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600336 }
337 }
338
339 return 0;
340}
341
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500342struct elf *elf_open(const char *name, int flags)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600343{
344 struct elf *elf;
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500345 Elf_Cmd cmd;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600346
347 elf_version(EV_CURRENT);
348
349 elf = malloc(sizeof(*elf));
350 if (!elf) {
351 perror("malloc");
352 return NULL;
353 }
354 memset(elf, 0, sizeof(*elf));
355
356 INIT_LIST_HEAD(&elf->sections);
357
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500358 elf->fd = open(name, flags);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600359 if (elf->fd == -1) {
360 perror("open");
361 goto err;
362 }
363
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500364 if ((flags & O_ACCMODE) == O_RDONLY)
365 cmd = ELF_C_READ_MMAP;
366 else if ((flags & O_ACCMODE) == O_RDWR)
367 cmd = ELF_C_RDWR;
368 else /* O_WRONLY */
369 cmd = ELF_C_WRITE;
370
371 elf->elf = elf_begin(elf->fd, cmd, NULL);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600372 if (!elf->elf) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500373 WARN_ELF("elf_begin");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600374 goto err;
375 }
376
377 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500378 WARN_ELF("gelf_getehdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600379 goto err;
380 }
381
382 if (read_sections(elf))
383 goto err;
384
385 if (read_symbols(elf))
386 goto err;
387
388 if (read_relas(elf))
389 goto err;
390
391 return elf;
392
393err:
394 elf_close(elf);
395 return NULL;
396}
397
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500398struct section *elf_create_section(struct elf *elf, const char *name,
399 size_t entsize, int nr)
400{
401 struct section *sec, *shstrtab;
402 size_t size = entsize * nr;
403 struct Elf_Scn *s;
404 Elf_Data *data;
405
406 sec = malloc(sizeof(*sec));
407 if (!sec) {
408 perror("malloc");
409 return NULL;
410 }
411 memset(sec, 0, sizeof(*sec));
412
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);
417
418 list_add_tail(&sec->list, &elf->sections);
419
420 s = elf_newscn(elf->elf);
421 if (!s) {
422 WARN_ELF("elf_newscn");
423 return NULL;
424 }
425
426 sec->name = strdup(name);
427 if (!sec->name) {
428 perror("strdup");
429 return NULL;
430 }
431
432 sec->idx = elf_ndxscn(s);
433 sec->len = size;
434 sec->changed = true;
435
436 sec->data = elf_newdata(s);
437 if (!sec->data) {
438 WARN_ELF("elf_newdata");
439 return NULL;
440 }
441
442 sec->data->d_size = size;
443 sec->data->d_align = 1;
444
445 if (size) {
446 sec->data->d_buf = malloc(size);
447 if (!sec->data->d_buf) {
448 perror("malloc");
449 return NULL;
450 }
451 memset(sec->data->d_buf, 0, size);
452 }
453
454 if (!gelf_getshdr(s, &sec->sh)) {
455 WARN_ELF("gelf_getshdr");
456 return NULL;
457 }
458
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;
464
465
466 /* Add section name to .shstrtab */
467 shstrtab = find_section_by_name(elf, ".shstrtab");
468 if (!shstrtab) {
469 WARN("can't find .shstrtab section");
470 return NULL;
471 }
472
473 s = elf_getscn(elf->elf, shstrtab->idx);
474 if (!s) {
475 WARN_ELF("elf_getscn");
476 return NULL;
477 }
478
479 data = elf_newdata(s);
480 if (!data) {
481 WARN_ELF("elf_newdata");
482 return NULL;
483 }
484
485 data->d_buf = sec->name;
486 data->d_size = strlen(name) + 1;
487 data->d_align = 1;
488
489 sec->sh.sh_name = shstrtab->len;
490
491 shstrtab->len += strlen(name) + 1;
492 shstrtab->changed = true;
493
494 return sec;
495}
496
497struct section *elf_create_rela_section(struct elf *elf, struct section *base)
498{
499 char *relaname;
500 struct section *sec;
501
502 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
503 if (!relaname) {
504 perror("malloc");
505 return NULL;
506 }
507 strcpy(relaname, ".rela");
508 strcat(relaname, base->name);
509
510 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
Martin Kepplinger0998b7a2017-09-14 08:01:38 +0200511 free(relaname);
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500512 if (!sec)
513 return NULL;
514
515 base->rela = sec;
516 sec->base = base;
517
518 sec->sh.sh_type = SHT_RELA;
519 sec->sh.sh_addralign = 8;
520 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
521 sec->sh.sh_info = base->idx;
522 sec->sh.sh_flags = SHF_INFO_LINK;
523
524 return sec;
525}
526
527int elf_rebuild_rela_section(struct section *sec)
528{
529 struct rela *rela;
530 int nr, idx = 0, size;
531 GElf_Rela *relas;
532
533 nr = 0;
534 list_for_each_entry(rela, &sec->rela_list, list)
535 nr++;
536
537 size = nr * sizeof(*relas);
538 relas = malloc(size);
539 if (!relas) {
540 perror("malloc");
541 return -1;
542 }
543
544 sec->data->d_buf = relas;
545 sec->data->d_size = size;
546
547 sec->sh.sh_size = size;
548
549 idx = 0;
550 list_for_each_entry(rela, &sec->rela_list, list) {
551 relas[idx].r_offset = rela->offset;
552 relas[idx].r_addend = rela->addend;
553 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
554 idx++;
555 }
556
557 return 0;
558}
559
560int elf_write(struct elf *elf)
561{
562 struct section *sec;
563 Elf_Scn *s;
564
565 list_for_each_entry(sec, &elf->sections, list) {
566 if (sec->changed) {
567 s = elf_getscn(elf->elf, sec->idx);
568 if (!s) {
569 WARN_ELF("elf_getscn");
570 return -1;
571 }
572 if (!gelf_update_shdr (s, &sec->sh)) {
573 WARN_ELF("gelf_update_shdr");
574 return -1;
575 }
576 }
577 }
578
579 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
580 WARN_ELF("elf_update");
581 return -1;
582 }
583
584 return 0;
585}
586
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600587void elf_close(struct elf *elf)
588{
589 struct section *sec, *tmpsec;
590 struct symbol *sym, *tmpsym;
591 struct rela *rela, *tmprela;
592
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500593 if (elf->elf)
594 elf_end(elf->elf);
595
596 if (elf->fd > 0)
597 close(elf->fd);
598
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600599 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600600 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600601 list_del(&sym->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600602 hash_del(&sym->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600603 free(sym);
604 }
Josh Poimboeufa196e172016-03-09 00:06:57 -0600605 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600606 list_del(&rela->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600607 hash_del(&rela->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600608 free(rela);
609 }
610 list_del(&sec->list);
611 free(sec);
612 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500613
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600614 free(elf);
615}