blob: c4857fa3f1d18537c2bcbbc37c6f40b966ffd7e1 [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
Peter Zijlstra2a362ec2020-03-12 09:34:42 +010030static void rb_add(struct rb_root *tree, struct rb_node *node,
31 int (*cmp)(struct rb_node *, const struct rb_node *))
32{
33 struct rb_node **link = &tree->rb_node;
34 struct rb_node *parent = NULL;
35
36 while (*link) {
37 parent = *link;
38 if (cmp(node, parent) < 0)
39 link = &parent->rb_left;
40 else
41 link = &parent->rb_right;
42 }
43
44 rb_link_node(node, parent, link);
45 rb_insert_color(node, tree);
46}
47
48static struct rb_node *rb_find_first(struct rb_root *tree, const void *key,
49 int (*cmp)(const void *key, const struct rb_node *))
50{
51 struct rb_node *node = tree->rb_node;
52 struct rb_node *match = NULL;
53
54 while (node) {
55 int c = cmp(key, node);
56 if (c <= 0) {
57 if (!c)
58 match = node;
59 node = node->rb_left;
60 } else if (c > 0) {
61 node = node->rb_right;
62 }
63 }
64
65 return match;
66}
67
68static struct rb_node *rb_next_match(struct rb_node *node, const void *key,
69 int (*cmp)(const void *key, const struct rb_node *))
70{
71 node = rb_next(node);
72 if (node && cmp(key, node))
73 node = NULL;
74 return node;
75}
76
77#define rb_for_each(tree, node, key, cmp) \
78 for ((node) = rb_find_first((tree), (key), (cmp)); \
79 (node); (node) = rb_next_match((node), (key), (cmp)))
80
81static int symbol_to_offset(struct rb_node *a, const struct rb_node *b)
82{
83 struct symbol *sa = rb_entry(a, struct symbol, node);
84 struct symbol *sb = rb_entry(b, struct symbol, node);
85
86 if (sa->offset < sb->offset)
87 return -1;
88 if (sa->offset > sb->offset)
89 return 1;
90
91 if (sa->len < sb->len)
92 return -1;
93 if (sa->len > sb->len)
94 return 1;
95
96 sa->alias = sb;
97
98 return 0;
99}
100
101static int symbol_by_offset(const void *key, const struct rb_node *node)
102{
103 const struct symbol *s = rb_entry(node, struct symbol, node);
104 const unsigned long *o = key;
105
106 if (*o < s->offset)
107 return -1;
Julien Thierry7f9b34f2020-04-03 14:17:30 +0100108 if (*o >= s->offset + s->len)
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100109 return 1;
110
111 return 0;
112}
113
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600114struct section *find_section_by_name(struct elf *elf, const char *name)
115{
116 struct section *sec;
117
Peter Zijlstraae358192020-03-12 09:32:10 +0100118 hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600119 if (!strcmp(sec->name, name))
120 return sec;
121
122 return NULL;
123}
124
125static struct section *find_section_by_index(struct elf *elf,
126 unsigned int idx)
127{
128 struct section *sec;
129
Peter Zijlstra530389962020-03-10 18:43:35 +0100130 hash_for_each_possible(elf->section_hash, sec, hash, idx)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600131 if (sec->idx == idx)
132 return sec;
133
134 return NULL;
135}
136
137static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
138{
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600139 struct symbol *sym;
140
Peter Zijlstra65fb11a2020-03-10 18:39:45 +0100141 hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
142 if (sym->idx == idx)
143 return sym;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600144
145 return NULL;
146}
147
148struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
149{
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100150 struct rb_node *node;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600151
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100152 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
153 struct symbol *s = rb_entry(node, struct symbol, node);
154
155 if (s->offset == offset && s->type != STT_SECTION)
156 return s;
157 }
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600158
159 return NULL;
160}
161
162struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
163{
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100164 struct rb_node *node;
Josh Poimboeuf7acfe532020-02-17 21:41:54 -0600165
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100166 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
167 struct symbol *s = rb_entry(node, struct symbol, node);
168
169 if (s->offset == offset && s->type == STT_FUNC)
170 return s;
171 }
172
173 return NULL;
174}
175
176struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
177{
178 struct rb_node *node;
179
180 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
181 struct symbol *s = rb_entry(node, struct symbol, node);
182
183 if (s->type != STT_SECTION)
184 return s;
185 }
186
187 return NULL;
188}
189
Peter Zijlstra53d20722020-03-16 10:36:53 +0100190struct symbol *find_func_containing(struct section *sec, unsigned long offset)
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100191{
192 struct rb_node *node;
193
194 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
195 struct symbol *s = rb_entry(node, struct symbol, node);
196
197 if (s->type == STT_FUNC)
198 return s;
199 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600200
201 return NULL;
202}
203
Josh Poimboeuf13810432018-05-09 22:39:15 -0500204struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
205{
Josh Poimboeuf13810432018-05-09 22:39:15 -0500206 struct symbol *sym;
207
Peter Zijlstracdb3d052020-03-12 10:17:38 +0100208 hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
209 if (!strcmp(sym->name, name))
210 return sym;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500211
212 return NULL;
213}
214
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100215struct rela *find_rela_by_dest_range(struct elf *elf, struct section *sec,
216 unsigned long offset, unsigned int len)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600217{
Peter Zijlstra74b873e2020-03-12 11:30:50 +0100218 struct rela *rela, *r = NULL;
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600219 unsigned long o;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600220
221 if (!sec->rela)
222 return NULL;
223
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100224 sec = sec->rela;
225
Peter Zijlstra74b873e2020-03-12 11:30:50 +0100226 for_offset_range(o, offset, offset + len) {
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100227 hash_for_each_possible(elf->rela_hash, rela, hash,
228 sec_offset_hash(sec, o)) {
Peter Zijlstra74b873e2020-03-12 11:30:50 +0100229 if (rela->sec != sec)
230 continue;
231
232 if (rela->offset >= offset && rela->offset < offset + len) {
233 if (!r || rela->offset < r->offset)
234 r = rela;
235 }
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100236 }
Peter Zijlstra74b873e2020-03-12 11:30:50 +0100237 if (r)
238 return r;
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100239 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600240
241 return NULL;
242}
243
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100244struct rela *find_rela_by_dest(struct elf *elf, struct section *sec, unsigned long offset)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600245{
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100246 return find_rela_by_dest_range(elf, sec, offset, 1);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600247}
248
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600249static int read_sections(struct elf *elf)
250{
251 Elf_Scn *s = NULL;
252 struct section *sec;
253 size_t shstrndx, sections_nr;
254 int i;
255
256 if (elf_getshdrnum(elf->elf, &sections_nr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500257 WARN_ELF("elf_getshdrnum");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600258 return -1;
259 }
260
261 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500262 WARN_ELF("elf_getshdrstrndx");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600263 return -1;
264 }
265
266 for (i = 0; i < sections_nr; i++) {
267 sec = malloc(sizeof(*sec));
268 if (!sec) {
269 perror("malloc");
270 return -1;
271 }
272 memset(sec, 0, sizeof(*sec));
273
Josh Poimboeufa196e172016-03-09 00:06:57 -0600274 INIT_LIST_HEAD(&sec->symbol_list);
275 INIT_LIST_HEAD(&sec->rela_list);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600276
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600277 s = elf_getscn(elf->elf, i);
278 if (!s) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500279 WARN_ELF("elf_getscn");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600280 return -1;
281 }
282
283 sec->idx = elf_ndxscn(s);
284
285 if (!gelf_getshdr(s, &sec->sh)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500286 WARN_ELF("gelf_getshdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600287 return -1;
288 }
289
290 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
291 if (!sec->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500292 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600293 return -1;
294 }
295
Petr Vandrovecdf968c92017-09-15 02:15:05 -0500296 if (sec->sh.sh_size != 0) {
297 sec->data = elf_getdata(s, NULL);
298 if (!sec->data) {
299 WARN_ELF("elf_getdata");
300 return -1;
301 }
302 if (sec->data->d_off != 0 ||
303 sec->data->d_size != sec->sh.sh_size) {
304 WARN("unexpected data attributes for %s",
305 sec->name);
306 return -1;
307 }
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600308 }
Petr Vandrovecdf968c92017-09-15 02:15:05 -0500309 sec->len = sec->sh.sh_size;
Peter Zijlstra530389962020-03-10 18:43:35 +0100310
311 list_add_tail(&sec->list, &elf->sections);
312 hash_add(elf->section_hash, &sec->hash, sec->idx);
Peter Zijlstraae358192020-03-12 09:32:10 +0100313 hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600314 }
315
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100316 if (stats)
317 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
318
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600319 /* sanity check, one more call to elf_nextscn() should return NULL */
320 if (elf_nextscn(elf->elf, s)) {
321 WARN("section entry mismatch");
322 return -1;
323 }
324
325 return 0;
326}
327
328static int read_symbols(struct elf *elf)
329{
Josh Poimboeuf13810432018-05-09 22:39:15 -0500330 struct section *symtab, *sec;
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100331 struct symbol *sym, *pfunc;
332 struct list_head *entry;
333 struct rb_node *pnode;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600334 int symbols_nr, i;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500335 char *coldstr;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600336
337 symtab = find_section_by_name(elf, ".symtab");
338 if (!symtab) {
339 WARN("missing symbol table");
340 return -1;
341 }
342
343 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
344
345 for (i = 0; i < symbols_nr; i++) {
346 sym = malloc(sizeof(*sym));
347 if (!sym) {
348 perror("malloc");
349 return -1;
350 }
351 memset(sym, 0, sizeof(*sym));
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100352 sym->alias = sym;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600353
354 sym->idx = i;
355
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500356 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
357 WARN_ELF("gelf_getsym");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600358 goto err;
359 }
360
361 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
362 sym->sym.st_name);
363 if (!sym->name) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500364 WARN_ELF("elf_strptr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600365 goto err;
366 }
367
368 sym->type = GELF_ST_TYPE(sym->sym.st_info);
369 sym->bind = GELF_ST_BIND(sym->sym.st_info);
370
371 if (sym->sym.st_shndx > SHN_UNDEF &&
372 sym->sym.st_shndx < SHN_LORESERVE) {
373 sym->sec = find_section_by_index(elf,
374 sym->sym.st_shndx);
375 if (!sym->sec) {
376 WARN("couldn't find section for symbol %s",
377 sym->name);
378 goto err;
379 }
380 if (sym->type == STT_SECTION) {
381 sym->name = sym->sec->name;
382 sym->sec->sym = sym;
383 }
384 } else
385 sym->sec = find_section_by_index(elf, 0);
386
387 sym->offset = sym->sym.st_value;
388 sym->len = sym->sym.st_size;
389
Peter Zijlstra2a362ec2020-03-12 09:34:42 +0100390 rb_add(&sym->sec->symbol_tree, &sym->node, symbol_to_offset);
391 pnode = rb_prev(&sym->node);
392 if (pnode)
393 entry = &rb_entry(pnode, struct symbol, node)->list;
394 else
395 entry = &sym->sec->symbol_list;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600396 list_add(&sym->list, entry);
Peter Zijlstra65fb11a2020-03-10 18:39:45 +0100397 hash_add(elf->symbol_hash, &sym->hash, sym->idx);
Peter Zijlstracdb3d052020-03-12 10:17:38 +0100398 hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600399 }
400
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100401 if (stats)
402 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
403
Josh Poimboeuf13810432018-05-09 22:39:15 -0500404 /* Create parent/child links for any cold subfunctions */
405 list_for_each_entry(sec, &elf->sections, list) {
406 list_for_each_entry(sym, &sec->symbol_list, list) {
Artem Savkov22566c12018-11-20 11:52:16 -0600407 char pname[MAX_NAME_LEN + 1];
408 size_t pnamelen;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500409 if (sym->type != STT_FUNC)
410 continue;
411 sym->pfunc = sym->cfunc = sym;
Josh Poimboeufbcb6fb52018-10-31 21:57:30 -0500412 coldstr = strstr(sym->name, ".cold");
Josh Poimboeuf08b393d2018-06-27 17:03:45 -0500413 if (!coldstr)
414 continue;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500415
Artem Savkov22566c12018-11-20 11:52:16 -0600416 pnamelen = coldstr - sym->name;
417 if (pnamelen > MAX_NAME_LEN) {
418 WARN("%s(): parent function name exceeds maximum length of %d characters",
419 sym->name, MAX_NAME_LEN);
420 return -1;
421 }
422
423 strncpy(pname, sym->name, pnamelen);
424 pname[pnamelen] = '\0';
425 pfunc = find_symbol_by_name(elf, pname);
Josh Poimboeuf13810432018-05-09 22:39:15 -0500426
Josh Poimboeuf08b393d2018-06-27 17:03:45 -0500427 if (!pfunc) {
428 WARN("%s(): can't find parent function",
429 sym->name);
Artem Savkov0b9301fb2018-11-20 11:52:15 -0600430 return -1;
Josh Poimboeuf08b393d2018-06-27 17:03:45 -0500431 }
432
433 sym->pfunc = pfunc;
434 pfunc->cfunc = sym;
435
436 /*
437 * Unfortunately, -fnoreorder-functions puts the child
438 * inside the parent. Remove the overlap so we can
439 * have sane assumptions.
440 *
441 * Note that pfunc->len now no longer matches
442 * pfunc->sym.st_size.
443 */
444 if (sym->sec == pfunc->sec &&
445 sym->offset >= pfunc->offset &&
446 sym->offset + sym->len == pfunc->offset + pfunc->len) {
447 pfunc->len -= sym->len;
Josh Poimboeuf13810432018-05-09 22:39:15 -0500448 }
449 }
450 }
451
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600452 return 0;
453
454err:
455 free(sym);
456 return -1;
457}
458
459static int read_relas(struct elf *elf)
460{
461 struct section *sec;
462 struct rela *rela;
463 int i;
464 unsigned int symndx;
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100465 unsigned long nr_rela, max_rela = 0, tot_rela = 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600466
467 list_for_each_entry(sec, &elf->sections, list) {
468 if (sec->sh.sh_type != SHT_RELA)
469 continue;
470
471 sec->base = find_section_by_name(elf, sec->name + 5);
472 if (!sec->base) {
473 WARN("can't find base section for rela section %s",
474 sec->name);
475 return -1;
476 }
477
478 sec->base->rela = sec;
479
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100480 nr_rela = 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600481 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
482 rela = malloc(sizeof(*rela));
483 if (!rela) {
484 perror("malloc");
485 return -1;
486 }
487 memset(rela, 0, sizeof(*rela));
488
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500489 if (!gelf_getrela(sec->data, i, &rela->rela)) {
490 WARN_ELF("gelf_getrela");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600491 return -1;
492 }
493
494 rela->type = GELF_R_TYPE(rela->rela.r_info);
495 rela->addend = rela->rela.r_addend;
496 rela->offset = rela->rela.r_offset;
497 symndx = GELF_R_SYM(rela->rela.r_info);
498 rela->sym = find_symbol_by_index(elf, symndx);
Josh Poimboeufe7c2bc32019-07-17 20:36:53 -0500499 rela->sec = sec;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600500 if (!rela->sym) {
501 WARN("can't find rela entry symbol %d for %s",
502 symndx, sec->name);
503 return -1;
504 }
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600505
506 list_add_tail(&rela->list, &sec->rela_list);
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100507 hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100508 nr_rela++;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600509 }
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +0100510 max_rela = max(max_rela, nr_rela);
511 tot_rela += nr_rela;
512 }
513
514 if (stats) {
515 printf("max_rela: %lu\n", max_rela);
516 printf("tot_rela: %lu\n", tot_rela);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600517 }
518
519 return 0;
520}
521
Michael Forney8e144792019-07-10 16:20:11 -0500522struct elf *elf_read(const char *name, int flags)
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600523{
524 struct elf *elf;
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500525 Elf_Cmd cmd;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600526
527 elf_version(EV_CURRENT);
528
529 elf = malloc(sizeof(*elf));
530 if (!elf) {
531 perror("malloc");
532 return NULL;
533 }
534 memset(elf, 0, sizeof(*elf));
535
Peter Zijlstra65fb11a2020-03-10 18:39:45 +0100536 hash_init(elf->symbol_hash);
Peter Zijlstracdb3d052020-03-12 10:17:38 +0100537 hash_init(elf->symbol_name_hash);
Peter Zijlstra530389962020-03-10 18:43:35 +0100538 hash_init(elf->section_hash);
Peter Zijlstraae358192020-03-12 09:32:10 +0100539 hash_init(elf->section_name_hash);
Peter Zijlstra8b5fa6b2020-03-12 11:23:36 +0100540 hash_init(elf->rela_hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600541 INIT_LIST_HEAD(&elf->sections);
542
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500543 elf->fd = open(name, flags);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600544 if (elf->fd == -1) {
Josh Poimboeuf385d11b2018-01-15 08:17:08 -0600545 fprintf(stderr, "objtool: Can't open '%s': %s\n",
546 name, strerror(errno));
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600547 goto err;
548 }
549
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500550 if ((flags & O_ACCMODE) == O_RDONLY)
551 cmd = ELF_C_READ_MMAP;
552 else if ((flags & O_ACCMODE) == O_RDWR)
553 cmd = ELF_C_RDWR;
554 else /* O_WRONLY */
555 cmd = ELF_C_WRITE;
556
557 elf->elf = elf_begin(elf->fd, cmd, NULL);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600558 if (!elf->elf) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500559 WARN_ELF("elf_begin");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600560 goto err;
561 }
562
563 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500564 WARN_ELF("gelf_getehdr");
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600565 goto err;
566 }
567
568 if (read_sections(elf))
569 goto err;
570
571 if (read_symbols(elf))
572 goto err;
573
574 if (read_relas(elf))
575 goto err;
576
577 return elf;
578
579err:
580 elf_close(elf);
581 return NULL;
582}
583
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500584struct section *elf_create_section(struct elf *elf, const char *name,
585 size_t entsize, int nr)
586{
587 struct section *sec, *shstrtab;
588 size_t size = entsize * nr;
Michael Forney3c3ea502019-07-10 16:17:35 -0500589 Elf_Scn *s;
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500590 Elf_Data *data;
591
592 sec = malloc(sizeof(*sec));
593 if (!sec) {
594 perror("malloc");
595 return NULL;
596 }
597 memset(sec, 0, sizeof(*sec));
598
599 INIT_LIST_HEAD(&sec->symbol_list);
600 INIT_LIST_HEAD(&sec->rela_list);
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500601
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500602 s = elf_newscn(elf->elf);
603 if (!s) {
604 WARN_ELF("elf_newscn");
605 return NULL;
606 }
607
608 sec->name = strdup(name);
609 if (!sec->name) {
610 perror("strdup");
611 return NULL;
612 }
613
614 sec->idx = elf_ndxscn(s);
615 sec->len = size;
616 sec->changed = true;
617
618 sec->data = elf_newdata(s);
619 if (!sec->data) {
620 WARN_ELF("elf_newdata");
621 return NULL;
622 }
623
624 sec->data->d_size = size;
625 sec->data->d_align = 1;
626
627 if (size) {
628 sec->data->d_buf = malloc(size);
629 if (!sec->data->d_buf) {
630 perror("malloc");
631 return NULL;
632 }
633 memset(sec->data->d_buf, 0, size);
634 }
635
636 if (!gelf_getshdr(s, &sec->sh)) {
637 WARN_ELF("gelf_getshdr");
638 return NULL;
639 }
640
641 sec->sh.sh_size = size;
642 sec->sh.sh_entsize = entsize;
643 sec->sh.sh_type = SHT_PROGBITS;
644 sec->sh.sh_addralign = 1;
645 sec->sh.sh_flags = SHF_ALLOC;
646
647
Simon Ser6d77d3b2018-07-09 11:17:22 -0500648 /* Add section name to .shstrtab (or .strtab for Clang) */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500649 shstrtab = find_section_by_name(elf, ".shstrtab");
Simon Ser6d77d3b2018-07-09 11:17:22 -0500650 if (!shstrtab)
651 shstrtab = find_section_by_name(elf, ".strtab");
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500652 if (!shstrtab) {
Simon Ser6d77d3b2018-07-09 11:17:22 -0500653 WARN("can't find .shstrtab or .strtab section");
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500654 return NULL;
655 }
656
657 s = elf_getscn(elf->elf, shstrtab->idx);
658 if (!s) {
659 WARN_ELF("elf_getscn");
660 return NULL;
661 }
662
663 data = elf_newdata(s);
664 if (!data) {
665 WARN_ELF("elf_newdata");
666 return NULL;
667 }
668
669 data->d_buf = sec->name;
670 data->d_size = strlen(name) + 1;
671 data->d_align = 1;
672
673 sec->sh.sh_name = shstrtab->len;
674
675 shstrtab->len += strlen(name) + 1;
676 shstrtab->changed = true;
677
Peter Zijlstra530389962020-03-10 18:43:35 +0100678 list_add_tail(&sec->list, &elf->sections);
679 hash_add(elf->section_hash, &sec->hash, sec->idx);
Peter Zijlstraae358192020-03-12 09:32:10 +0100680 hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
Peter Zijlstra530389962020-03-10 18:43:35 +0100681
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500682 return sec;
683}
684
685struct section *elf_create_rela_section(struct elf *elf, struct section *base)
686{
687 char *relaname;
688 struct section *sec;
689
690 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
691 if (!relaname) {
692 perror("malloc");
693 return NULL;
694 }
695 strcpy(relaname, ".rela");
696 strcat(relaname, base->name);
697
698 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
Martin Kepplinger0998b7a2017-09-14 08:01:38 +0200699 free(relaname);
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500700 if (!sec)
701 return NULL;
702
703 base->rela = sec;
704 sec->base = base;
705
706 sec->sh.sh_type = SHT_RELA;
707 sec->sh.sh_addralign = 8;
708 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
709 sec->sh.sh_info = base->idx;
710 sec->sh.sh_flags = SHF_INFO_LINK;
711
712 return sec;
713}
714
715int elf_rebuild_rela_section(struct section *sec)
716{
717 struct rela *rela;
718 int nr, idx = 0, size;
719 GElf_Rela *relas;
720
721 nr = 0;
722 list_for_each_entry(rela, &sec->rela_list, list)
723 nr++;
724
725 size = nr * sizeof(*relas);
726 relas = malloc(size);
727 if (!relas) {
728 perror("malloc");
729 return -1;
730 }
731
732 sec->data->d_buf = relas;
733 sec->data->d_size = size;
734
735 sec->sh.sh_size = size;
736
737 idx = 0;
738 list_for_each_entry(rela, &sec->rela_list, list) {
739 relas[idx].r_offset = rela->offset;
740 relas[idx].r_addend = rela->addend;
741 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
742 idx++;
743 }
744
745 return 0;
746}
747
748int elf_write(struct elf *elf)
749{
750 struct section *sec;
751 Elf_Scn *s;
752
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500753 /* Update section headers for changed sections: */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500754 list_for_each_entry(sec, &elf->sections, list) {
755 if (sec->changed) {
756 s = elf_getscn(elf->elf, sec->idx);
757 if (!s) {
758 WARN_ELF("elf_getscn");
759 return -1;
760 }
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500761 if (!gelf_update_shdr(s, &sec->sh)) {
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500762 WARN_ELF("gelf_update_shdr");
763 return -1;
764 }
765 }
766 }
767
Josh Poimboeuf97dab2a2017-09-15 02:17:11 -0500768 /* Make sure the new section header entries get updated properly. */
769 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
770
771 /* Write all changes to the file. */
Josh Poimboeuf627fce12017-07-11 10:33:42 -0500772 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
773 WARN_ELF("elf_update");
774 return -1;
775 }
776
777 return 0;
778}
779
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600780void elf_close(struct elf *elf)
781{
782 struct section *sec, *tmpsec;
783 struct symbol *sym, *tmpsym;
784 struct rela *rela, *tmprela;
785
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500786 if (elf->elf)
787 elf_end(elf->elf);
788
789 if (elf->fd > 0)
790 close(elf->fd);
791
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600792 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
Josh Poimboeufa196e172016-03-09 00:06:57 -0600793 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600794 list_del(&sym->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600795 hash_del(&sym->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600796 free(sym);
797 }
Josh Poimboeufa196e172016-03-09 00:06:57 -0600798 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600799 list_del(&rela->list);
Josh Poimboeuf042ba732016-03-09 00:07:00 -0600800 hash_del(&rela->hash);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600801 free(rela);
802 }
803 list_del(&sec->list);
804 free(sec);
805 }
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500806
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600807 free(elf);
808}