blob: 20fe40d5101d86615eed940a983a4189d1e4d60e [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06002/*
3 * elf.c - ELF access library
4 *
5 * Adapted from kpatch (https://github.com/dynup/kpatch):
6 * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
7 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06008 */
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
Josh Poimboeuf385d11b2018-01-15 08:17:08 -060017#include <errno.h>
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +010018#include "builtin.h"
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060019
20#include "elf.h"
21#include "warn.h"
22
Artem Savkov22566c12018-11-20 11:52:16 -060023#define MAX_NAME_LEN 128
24
Peter Zijlstraae358192020-03-12 09:32:10 +010025static inline u32 str_hash(const char *str)
26{
27 return jhash(str, strlen(str), 0);
28}
29
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060030struct section *find_section_by_name(struct elf *elf, const char *name)
31{
32 struct section *sec;
33
Peter Zijlstraae358192020-03-12 09:32:10 +010034 hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060035 if (!strcmp(sec->name, name))
36 return sec;
37
38 return NULL;
39}
40
41static struct section *find_section_by_index(struct elf *elf,
42 unsigned int idx)
43{
44 struct section *sec;
45
Peter Zijlstra530389962020-03-10 18:43:35 +010046 hash_for_each_possible(elf->section_hash, sec, hash, idx)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060047 if (sec->idx == idx)
48 return sec;
49
50 return NULL;
51}
52
53static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
54{
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060055 struct symbol *sym;
56
Peter Zijlstra65fb11a2020-03-10 18:39:45 +010057 hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
58 if (sym->idx == idx)
59 return sym;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060060
61 return NULL;
62}
63
64struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
65{
66 struct symbol *sym;
67
Josh Poimboeufa196e172016-03-09 00:06:57 -060068 list_for_each_entry(sym, &sec->symbol_list, list)
Josh Poimboeuf7acfe532020-02-17 21:41:54 -060069 if (sym->type != STT_SECTION && sym->offset == offset)
70 return sym;
71
72 return NULL;
73}
74
75struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
76{
77 struct symbol *sym;
78
79 list_for_each_entry(sym, &sec->symbol_list, list)
80 if (sym->type == STT_FUNC && sym->offset == offset)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060081 return sym;
82
83 return NULL;
84}
85
Josh Poimboeuf13810432018-05-09 22:39:15 -050086struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
87{
88 struct section *sec;
89 struct symbol *sym;
90
91 list_for_each_entry(sec, &elf->sections, list)
92 list_for_each_entry(sym, &sec->symbol_list, list)
93 if (!strcmp(sym->name, name))
94 return sym;
95
96 return NULL;
97}
98
Josh Poimboeuf5c51f4a2017-03-02 16:57:23 -060099struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
100{
101 struct symbol *sym;
102
103 list_for_each_entry(sym, &sec->symbol_list, list)
104 if (sym->type != STT_SECTION &&
105 offset >= sym->offset && offset < sym->offset + sym->len)
106 return sym;
107
108 return NULL;
109}
110
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600111struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
112 unsigned int len)
113{
114 struct rela *rela;
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600115 unsigned long o;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600116
117 if (!sec->rela)
118 return NULL;
119
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600120 for (o = offset; o < offset + len; o++)
121 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
122 if (rela->offset == o)
123 return rela;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600124
125 return NULL;
126}
127
128struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
129{
130 return find_rela_by_dest_range(sec, offset, 1);
131}
132
133struct symbol *find_containing_func(struct section *sec, unsigned long offset)
134{
135 struct symbol *func;
136
Josh Poimboeufa196e172016-03-09 00:06:57 -0600137 list_for_each_entry(func, &sec->symbol_list, list)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600138 if (func->type == STT_FUNC && offset >= func->offset &&
139 offset < func->offset + func->len)
140 return func;
141
142 return NULL;
143}
144
145static int read_sections(struct elf *elf)
146{
147 Elf_Scn *s = NULL;
148 struct section *sec;
149 size_t shstrndx, sections_nr;
150 int i;
151
152 if (elf_getshdrnum(elf->elf, &sections_nr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500153 WARN_ELF("elf_getshdrnum");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600154 return -1;
155 }
156
157 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500158 WARN_ELF("elf_getshdrstrndx");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600159 return -1;
160 }
161
162 for (i = 0; i < sections_nr; i++) {
163 sec = malloc(sizeof(*sec));
164 if (!sec) {
165 perror("malloc");
166 return -1;
167 }
168 memset(sec, 0, sizeof(*sec));
169
Josh Poimboeufa196e172016-03-09 00:06:57 -0600170 INIT_LIST_HEAD(&sec->symbol_list);
171 INIT_LIST_HEAD(&sec->rela_list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600172 hash_init(sec->rela_hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600173
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600174 s = elf_getscn(elf->elf, i);
175 if (!s) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500176 WARN_ELF("elf_getscn");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600177 return -1;
178 }
179
180 sec->idx = elf_ndxscn(s);
181
182 if (!gelf_getshdr(s, &sec->sh)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500183 WARN_ELF("gelf_getshdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600184 return -1;
185 }
186
187 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
188 if (!sec->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500189 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600190 return -1;
191 }
192
Petr Vandrovecdf968c92017-09-15 02:15:05 -0500193 if (sec->sh.sh_size != 0) {
194 sec->data = elf_getdata(s, NULL);
195 if (!sec->data) {
196 WARN_ELF("elf_getdata");
197 return -1;
198 }
199 if (sec->data->d_off != 0 ||
200 sec->data->d_size != sec->sh.sh_size) {
201 WARN("unexpected data attributes for %s",
202 sec->name);
203 return -1;
204 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600205 }
Petr Vandrovecdf968c92017-09-15 02:15:05 -0500206 sec->len = sec->sh.sh_size;
Peter Zijlstra530389962020-03-10 18:43:35 +0100207
208 list_add_tail(&sec->list, &elf->sections);
209 hash_add(elf->section_hash, &sec->hash, sec->idx);
Peter Zijlstraae358192020-03-12 09:32:10 +0100210 hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600211 }
212
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100213 if (stats)
214 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
215
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600216 /* sanity check, one more call to elf_nextscn() should return NULL */
217 if (elf_nextscn(elf->elf, s)) {
218 WARN("section entry mismatch");
219 return -1;
220 }
221
222 return 0;
223}
224
225static int read_symbols(struct elf *elf)
226{
Josh Poimboeuf13810432018-05-09 22:39:15 -0500227 struct section *symtab, *sec;
Peter Zijlstra09f30d82019-02-28 14:17:50 +0100228 struct symbol *sym, *pfunc, *alias;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600229 struct list_head *entry, *tmp;
230 int symbols_nr, i;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500231 char *coldstr;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600232
233 symtab = find_section_by_name(elf, ".symtab");
234 if (!symtab) {
235 WARN("missing symbol table");
236 return -1;
237 }
238
239 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
240
241 for (i = 0; i < symbols_nr; i++) {
242 sym = malloc(sizeof(*sym));
243 if (!sym) {
244 perror("malloc");
245 return -1;
246 }
247 memset(sym, 0, sizeof(*sym));
Peter Zijlstra09f30d82019-02-28 14:17:50 +0100248 alias = sym;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600249
250 sym->idx = i;
251
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500252 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
253 WARN_ELF("gelf_getsym");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600254 goto err;
255 }
256
257 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
258 sym->sym.st_name);
259 if (!sym->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500260 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600261 goto err;
262 }
263
264 sym->type = GELF_ST_TYPE(sym->sym.st_info);
265 sym->bind = GELF_ST_BIND(sym->sym.st_info);
266
267 if (sym->sym.st_shndx > SHN_UNDEF &&
268 sym->sym.st_shndx < SHN_LORESERVE) {
269 sym->sec = find_section_by_index(elf,
270 sym->sym.st_shndx);
271 if (!sym->sec) {
272 WARN("couldn't find section for symbol %s",
273 sym->name);
274 goto err;
275 }
276 if (sym->type == STT_SECTION) {
277 sym->name = sym->sec->name;
278 sym->sec->sym = sym;
279 }
280 } else
281 sym->sec = find_section_by_index(elf, 0);
282
283 sym->offset = sym->sym.st_value;
284 sym->len = sym->sym.st_size;
285
286 /* sorted insert into a per-section list */
Josh Poimboeufa196e172016-03-09 00:06:57 -0600287 entry = &sym->sec->symbol_list;
288 list_for_each_prev(tmp, &sym->sec->symbol_list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600289 struct symbol *s;
290
291 s = list_entry(tmp, struct symbol, list);
292
293 if (sym->offset > s->offset) {
294 entry = tmp;
295 break;
296 }
297
Peter Zijlstra09f30d82019-02-28 14:17:50 +0100298 if (sym->offset == s->offset) {
Josh Poimboeufe10cd8f2019-07-17 20:36:48 -0500299 if (sym->len && sym->len == s->len && alias == sym)
Peter Zijlstra09f30d82019-02-28 14:17:50 +0100300 alias = s;
301
302 if (sym->len >= s->len) {
303 entry = tmp;
304 break;
305 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600306 }
307 }
Peter Zijlstra09f30d82019-02-28 14:17:50 +0100308 sym->alias = alias;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600309 list_add(&sym->list, entry);
Peter Zijlstra65fb11a2020-03-10 18:39:45 +0100310 hash_add(elf->symbol_hash, &sym->hash, sym->idx);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600311 }
312
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100313 if (stats)
314 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
315
Josh Poimboeuf13810432018-05-09 22:39:15 -0500316 /* Create parent/child links for any cold subfunctions */
317 list_for_each_entry(sec, &elf->sections, list) {
318 list_for_each_entry(sym, &sec->symbol_list, list) {
Artem Savkov22566c12018-11-20 11:52:16 -0600319 char pname[MAX_NAME_LEN + 1];
320 size_t pnamelen;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500321 if (sym->type != STT_FUNC)
322 continue;
323 sym->pfunc = sym->cfunc = sym;
Josh Poimboeufbcb6fb52018-10-31 21:57:30 -0500324 coldstr = strstr(sym->name, ".cold");
Josh Poimboeuf08b393d2018-06-27 17:03:45 -0500325 if (!coldstr)
326 continue;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500327
Artem Savkov22566c12018-11-20 11:52:16 -0600328 pnamelen = coldstr - sym->name;
329 if (pnamelen > MAX_NAME_LEN) {
330 WARN("%s(): parent function name exceeds maximum length of %d characters",
331 sym->name, MAX_NAME_LEN);
332 return -1;
333 }
334
335 strncpy(pname, sym->name, pnamelen);
336 pname[pnamelen] = '\0';
337 pfunc = find_symbol_by_name(elf, pname);
Josh Poimboeuf13810432018-05-09 22:39:15 -0500338
Josh Poimboeuf08b393d2018-06-27 17:03:45 -0500339 if (!pfunc) {
340 WARN("%s(): can't find parent function",
341 sym->name);
Artem Savkov0b9301fb2018-11-20 11:52:15 -0600342 return -1;
Josh Poimboeuf08b393d2018-06-27 17:03:45 -0500343 }
344
345 sym->pfunc = pfunc;
346 pfunc->cfunc = sym;
347
348 /*
349 * Unfortunately, -fnoreorder-functions puts the child
350 * inside the parent. Remove the overlap so we can
351 * have sane assumptions.
352 *
353 * Note that pfunc->len now no longer matches
354 * pfunc->sym.st_size.
355 */
356 if (sym->sec == pfunc->sec &&
357 sym->offset >= pfunc->offset &&
358 sym->offset + sym->len == pfunc->offset + pfunc->len) {
359 pfunc->len -= sym->len;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500360 }
361 }
362 }
363
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600364 return 0;
365
366err:
367 free(sym);
368 return -1;
369}
370
371static int read_relas(struct elf *elf)
372{
373 struct section *sec;
374 struct rela *rela;
375 int i;
376 unsigned int symndx;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100377 unsigned long nr_rela, max_rela = 0, tot_rela = 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600378
379 list_for_each_entry(sec, &elf->sections, list) {
380 if (sec->sh.sh_type != SHT_RELA)
381 continue;
382
383 sec->base = find_section_by_name(elf, sec->name + 5);
384 if (!sec->base) {
385 WARN("can't find base section for rela section %s",
386 sec->name);
387 return -1;
388 }
389
390 sec->base->rela = sec;
391
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100392 nr_rela = 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600393 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
394 rela = malloc(sizeof(*rela));
395 if (!rela) {
396 perror("malloc");
397 return -1;
398 }
399 memset(rela, 0, sizeof(*rela));
400
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500401 if (!gelf_getrela(sec->data, i, &rela->rela)) {
402 WARN_ELF("gelf_getrela");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600403 return -1;
404 }
405
406 rela->type = GELF_R_TYPE(rela->rela.r_info);
407 rela->addend = rela->rela.r_addend;
408 rela->offset = rela->rela.r_offset;
409 symndx = GELF_R_SYM(rela->rela.r_info);
410 rela->sym = find_symbol_by_index(elf, symndx);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500411 rela->sec = sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600412 if (!rela->sym) {
413 WARN("can't find rela entry symbol %d for %s",
414 symndx, sec->name);
415 return -1;
416 }
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600417
418 list_add_tail(&rela->list, &sec->rela_list);
419 hash_add(sec->rela_hash, &rela->hash, rela->offset);
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100420 nr_rela++;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600421 }
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100422 max_rela = max(max_rela, nr_rela);
423 tot_rela += nr_rela;
424 }
425
426 if (stats) {
427 printf("max_rela: %lu\n", max_rela);
428 printf("tot_rela: %lu\n", tot_rela);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600429 }
430
431 return 0;
432}
433
Michael Forney8e144792019-07-10 16:20:11 -0500434struct elf *elf_read(const char *name, int flags)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600435{
436 struct elf *elf;
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500437 Elf_Cmd cmd;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600438
439 elf_version(EV_CURRENT);
440
441 elf = malloc(sizeof(*elf));
442 if (!elf) {
443 perror("malloc");
444 return NULL;
445 }
446 memset(elf, 0, sizeof(*elf));
447
Peter Zijlstra65fb11a2020-03-10 18:39:45 +0100448 hash_init(elf->symbol_hash);
Peter Zijlstra530389962020-03-10 18:43:35 +0100449 hash_init(elf->section_hash);
Peter Zijlstraae358192020-03-12 09:32:10 +0100450 hash_init(elf->section_name_hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600451 INIT_LIST_HEAD(&elf->sections);
452
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500453 elf->fd = open(name, flags);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600454 if (elf->fd == -1) {
Josh Poimboeuf385d11b2018-01-15 08:17:08 -0600455 fprintf(stderr, "objtool: Can't open '%s': %s\n",
456 name, strerror(errno));
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600457 goto err;
458 }
459
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500460 if ((flags & O_ACCMODE) == O_RDONLY)
461 cmd = ELF_C_READ_MMAP;
462 else if ((flags & O_ACCMODE) == O_RDWR)
463 cmd = ELF_C_RDWR;
464 else /* O_WRONLY */
465 cmd = ELF_C_WRITE;
466
467 elf->elf = elf_begin(elf->fd, cmd, NULL);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600468 if (!elf->elf) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500469 WARN_ELF("elf_begin");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600470 goto err;
471 }
472
473 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500474 WARN_ELF("gelf_getehdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600475 goto err;
476 }
477
478 if (read_sections(elf))
479 goto err;
480
481 if (read_symbols(elf))
482 goto err;
483
484 if (read_relas(elf))
485 goto err;
486
487 return elf;
488
489err:
490 elf_close(elf);
491 return NULL;
492}
493
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500494struct section *elf_create_section(struct elf *elf, const char *name,
495 size_t entsize, int nr)
496{
497 struct section *sec, *shstrtab;
498 size_t size = entsize * nr;
Michael Forney3c3ea502019-07-10 16:17:35 -0500499 Elf_Scn *s;
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500500 Elf_Data *data;
501
502 sec = malloc(sizeof(*sec));
503 if (!sec) {
504 perror("malloc");
505 return NULL;
506 }
507 memset(sec, 0, sizeof(*sec));
508
509 INIT_LIST_HEAD(&sec->symbol_list);
510 INIT_LIST_HEAD(&sec->rela_list);
511 hash_init(sec->rela_hash);
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500512
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500513 s = elf_newscn(elf->elf);
514 if (!s) {
515 WARN_ELF("elf_newscn");
516 return NULL;
517 }
518
519 sec->name = strdup(name);
520 if (!sec->name) {
521 perror("strdup");
522 return NULL;
523 }
524
525 sec->idx = elf_ndxscn(s);
526 sec->len = size;
527 sec->changed = true;
528
529 sec->data = elf_newdata(s);
530 if (!sec->data) {
531 WARN_ELF("elf_newdata");
532 return NULL;
533 }
534
535 sec->data->d_size = size;
536 sec->data->d_align = 1;
537
538 if (size) {
539 sec->data->d_buf = malloc(size);
540 if (!sec->data->d_buf) {
541 perror("malloc");
542 return NULL;
543 }
544 memset(sec->data->d_buf, 0, size);
545 }
546
547 if (!gelf_getshdr(s, &sec->sh)) {
548 WARN_ELF("gelf_getshdr");
549 return NULL;
550 }
551
552 sec->sh.sh_size = size;
553 sec->sh.sh_entsize = entsize;
554 sec->sh.sh_type = SHT_PROGBITS;
555 sec->sh.sh_addralign = 1;
556 sec->sh.sh_flags = SHF_ALLOC;
557
558
Simon Ser6d77d3b2018-07-09 11:17:22 -0500559 /* Add section name to .shstrtab (or .strtab for Clang) */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500560 shstrtab = find_section_by_name(elf, ".shstrtab");
Simon Ser6d77d3b2018-07-09 11:17:22 -0500561 if (!shstrtab)
562 shstrtab = find_section_by_name(elf, ".strtab");
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500563 if (!shstrtab) {
Simon Ser6d77d3b2018-07-09 11:17:22 -0500564 WARN("can't find .shstrtab or .strtab section");
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500565 return NULL;
566 }
567
568 s = elf_getscn(elf->elf, shstrtab->idx);
569 if (!s) {
570 WARN_ELF("elf_getscn");
571 return NULL;
572 }
573
574 data = elf_newdata(s);
575 if (!data) {
576 WARN_ELF("elf_newdata");
577 return NULL;
578 }
579
580 data->d_buf = sec->name;
581 data->d_size = strlen(name) + 1;
582 data->d_align = 1;
583
584 sec->sh.sh_name = shstrtab->len;
585
586 shstrtab->len += strlen(name) + 1;
587 shstrtab->changed = true;
588
Peter Zijlstra530389962020-03-10 18:43:35 +0100589 list_add_tail(&sec->list, &elf->sections);
590 hash_add(elf->section_hash, &sec->hash, sec->idx);
Peter Zijlstraae358192020-03-12 09:32:10 +0100591 hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
Peter Zijlstra530389962020-03-10 18:43:35 +0100592
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500593 return sec;
594}
595
596struct section *elf_create_rela_section(struct elf *elf, struct section *base)
597{
598 char *relaname;
599 struct section *sec;
600
601 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
602 if (!relaname) {
603 perror("malloc");
604 return NULL;
605 }
606 strcpy(relaname, ".rela");
607 strcat(relaname, base->name);
608
609 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
Martin Kepplinger0998b7a2017-09-14 08:01:38 +0200610 free(relaname);
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500611 if (!sec)
612 return NULL;
613
614 base->rela = sec;
615 sec->base = base;
616
617 sec->sh.sh_type = SHT_RELA;
618 sec->sh.sh_addralign = 8;
619 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
620 sec->sh.sh_info = base->idx;
621 sec->sh.sh_flags = SHF_INFO_LINK;
622
623 return sec;
624}
625
626int elf_rebuild_rela_section(struct section *sec)
627{
628 struct rela *rela;
629 int nr, idx = 0, size;
630 GElf_Rela *relas;
631
632 nr = 0;
633 list_for_each_entry(rela, &sec->rela_list, list)
634 nr++;
635
636 size = nr * sizeof(*relas);
637 relas = malloc(size);
638 if (!relas) {
639 perror("malloc");
640 return -1;
641 }
642
643 sec->data->d_buf = relas;
644 sec->data->d_size = size;
645
646 sec->sh.sh_size = size;
647
648 idx = 0;
649 list_for_each_entry(rela, &sec->rela_list, list) {
650 relas[idx].r_offset = rela->offset;
651 relas[idx].r_addend = rela->addend;
652 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
653 idx++;
654 }
655
656 return 0;
657}
658
659int elf_write(struct elf *elf)
660{
661 struct section *sec;
662 Elf_Scn *s;
663
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500664 /* Update section headers for changed sections: */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500665 list_for_each_entry(sec, &elf->sections, list) {
666 if (sec->changed) {
667 s = elf_getscn(elf->elf, sec->idx);
668 if (!s) {
669 WARN_ELF("elf_getscn");
670 return -1;
671 }
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500672 if (!gelf_update_shdr(s, &sec->sh)) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500673 WARN_ELF("gelf_update_shdr");
674 return -1;
675 }
676 }
677 }
678
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500679 /* Make sure the new section header entries get updated properly. */
680 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
681
682 /* Write all changes to the file. */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500683 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
684 WARN_ELF("elf_update");
685 return -1;
686 }
687
688 return 0;
689}
690
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600691void elf_close(struct elf *elf)
692{
693 struct section *sec, *tmpsec;
694 struct symbol *sym, *tmpsym;
695 struct rela *rela, *tmprela;
696
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500697 if (elf->elf)
698 elf_end(elf->elf);
699
700 if (elf->fd > 0)
701 close(elf->fd);
702
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600703 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600704 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600705 list_del(&sym->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600706 hash_del(&sym->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600707 free(sym);
708 }
Josh Poimboeufa196e172016-03-09 00:06:57 -0600709 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600710 list_del(&rela->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600711 hash_del(&rela->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600712 free(rela);
713 }
714 list_del(&sec->list);
715 free(sec);
716 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500717
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600718 free(elf);
719}