1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
8 */
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/mman.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <libgen.h>
20 #include <ctype.h>
21 #include <linux/align.h>
22 #include <linux/kernel.h>
23 #include <linux/interval_tree_generic.h>
24 #include <linux/log2.h>
25 #include <objtool/builtin.h>
26 #include <objtool/elf.h>
27 #include <objtool/warn.h>
28
str_hash(const char * str)29 static inline u32 str_hash(const char *str)
30 {
31 return jhash(str, strlen(str), 0);
32 }
33
34 #define __elf_table(name) (elf->name##_hash)
35 #define __elf_bits(name) (elf->name##_bits)
36
37 #define __elf_table_entry(name, key) \
38 __elf_table(name)[hash_min(key, __elf_bits(name))]
39
40 #define elf_hash_add(name, node, key) \
41 ({ \
42 struct elf_hash_node *__node = node; \
43 __node->next = __elf_table_entry(name, key); \
44 __elf_table_entry(name, key) = __node; \
45 })
46
__elf_hash_del(struct elf_hash_node * node,struct elf_hash_node ** head)47 static inline void __elf_hash_del(struct elf_hash_node *node,
48 struct elf_hash_node **head)
49 {
50 struct elf_hash_node *cur, *prev;
51
52 if (node == *head) {
53 *head = node->next;
54 return;
55 }
56
57 for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
58 if (cur == node) {
59 prev->next = cur->next;
60 break;
61 }
62 }
63 }
64
65 #define elf_hash_del(name, node, key) \
66 __elf_hash_del(node, &__elf_table_entry(name, key))
67
68 #define elf_list_entry(ptr, type, member) \
69 ({ \
70 typeof(ptr) __ptr = (ptr); \
71 __ptr ? container_of(__ptr, type, member) : NULL; \
72 })
73
74 #define elf_hash_for_each_possible(name, obj, member, key) \
75 for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
76 obj; \
77 obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
78
79 #define elf_alloc_hash(name, size) \
80 ({ \
81 __elf_bits(name) = max(10, ilog2(size)); \
82 __elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
83 PROT_READ|PROT_WRITE, \
84 MAP_PRIVATE|MAP_ANON, -1, 0); \
85 if (__elf_table(name) == (void *)-1L) { \
86 ERROR_GLIBC("mmap fail " #name); \
87 __elf_table(name) = NULL; \
88 } \
89 __elf_table(name); \
90 })
91
__sym_start(struct symbol * s)92 static inline unsigned long __sym_start(struct symbol *s)
93 {
94 return s->offset;
95 }
96
__sym_last(struct symbol * s)97 static inline unsigned long __sym_last(struct symbol *s)
98 {
99 return s->offset + (s->len ? s->len - 1 : 0);
100 }
101
102 INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
103 __sym_start, __sym_last, static inline __maybe_unused,
104 __sym)
105
106 #define __sym_for_each(_iter, _tree, _start, _end) \
107 for (_iter = __sym_iter_first((_tree), (_start), (_end)); \
108 _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
109
110 struct symbol_hole {
111 unsigned long key;
112 const struct symbol *sym;
113 };
114
115 /*
116 * Find the last symbol before @offset.
117 */
symbol_hole_by_offset(const void * key,const struct rb_node * node)118 static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
119 {
120 const struct symbol *s = rb_entry(node, struct symbol, node);
121 struct symbol_hole *sh = (void *)key;
122
123 if (sh->key < s->offset)
124 return -1;
125
126 if (sh->key >= s->offset + s->len) {
127 sh->sym = s;
128 return 1;
129 }
130
131 return 0;
132 }
133
find_section_by_name(const struct elf * elf,const char * name)134 struct section *find_section_by_name(const struct elf *elf, const char *name)
135 {
136 struct section *sec;
137
138 elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
139 if (!strcmp(sec->name, name))
140 return sec;
141 }
142
143 return NULL;
144 }
145
find_section_by_index(struct elf * elf,unsigned int idx)146 static struct section *find_section_by_index(struct elf *elf,
147 unsigned int idx)
148 {
149 struct section *sec;
150
151 elf_hash_for_each_possible(section, sec, hash, idx) {
152 if (sec->idx == idx)
153 return sec;
154 }
155
156 return NULL;
157 }
158
find_symbol_by_index(struct elf * elf,unsigned int idx)159 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
160 {
161 struct symbol *sym;
162
163 elf_hash_for_each_possible(symbol, sym, hash, idx) {
164 if (sym->idx == idx)
165 return sym;
166 }
167
168 return NULL;
169 }
170
find_symbol_by_offset(struct section * sec,unsigned long offset)171 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
172 {
173 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
174 struct symbol *sym;
175
176 __sym_for_each(sym, tree, offset, offset) {
177 if (sym->offset == offset && !is_sec_sym(sym))
178 return sym->alias;
179 }
180
181 return NULL;
182 }
183
find_func_by_offset(struct section * sec,unsigned long offset)184 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
185 {
186 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
187 struct symbol *func;
188
189 __sym_for_each(func, tree, offset, offset) {
190 if (func->offset == offset && is_func_sym(func))
191 return func->alias;
192 }
193
194 return NULL;
195 }
196
find_symbol_containing(const struct section * sec,unsigned long offset)197 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
198 {
199 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
200 struct symbol *sym = NULL, *tmp;
201
202 __sym_for_each(tmp, tree, offset, offset) {
203 if (tmp->len) {
204 if (!sym) {
205 sym = tmp;
206 continue;
207 }
208
209 if (sym->offset != tmp->offset || sym->len != tmp->len) {
210 /*
211 * In the rare case of overlapping symbols,
212 * pick the smaller one.
213 *
214 * TODO: outlaw overlapping symbols
215 */
216 if (tmp->len < sym->len)
217 sym = tmp;
218 }
219 }
220 }
221
222 return sym ? sym->alias : NULL;
223 }
224
225 /*
226 * Returns size of hole starting at @offset.
227 */
find_symbol_hole_containing(const struct section * sec,unsigned long offset)228 int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
229 {
230 struct symbol_hole hole = {
231 .key = offset,
232 .sym = NULL,
233 };
234 struct rb_node *n;
235 struct symbol *s;
236
237 /*
238 * Find the rightmost symbol for which @offset is after it.
239 */
240 n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
241
242 /* found a symbol that contains @offset */
243 if (n)
244 return 0; /* not a hole */
245
246 /*
247 * @offset >= sym->offset + sym->len, find symbol after it.
248 * When hole.sym is empty, use the first node to compute the hole.
249 * If there is no symbol in the section, the first node will be NULL,
250 * in which case, -1 is returned to skip the whole section.
251 */
252 if (hole.sym)
253 n = rb_next(&hole.sym->node);
254 else
255 n = rb_first_cached(&sec->symbol_tree);
256
257 if (!n)
258 return -1; /* until end of address space */
259
260 /* hole until start of next symbol */
261 s = rb_entry(n, struct symbol, node);
262 return s->offset - offset;
263 }
264
find_func_containing(struct section * sec,unsigned long offset)265 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
266 {
267 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
268 struct symbol *func;
269
270 __sym_for_each(func, tree, offset, offset) {
271 if (is_func_sym(func))
272 return func->alias;
273 }
274
275 return NULL;
276 }
277
find_symbol_by_name(const struct elf * elf,const char * name)278 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
279 {
280 struct symbol *sym;
281
282 elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
283 if (!strcmp(sym->name, name))
284 return sym;
285 }
286
287 return NULL;
288 }
289
290 /* Find local symbol with matching STT_FILE */
find_local_symbol_by_file_and_name(const struct elf * elf,struct symbol * file,const char * name)291 static struct symbol *find_local_symbol_by_file_and_name(const struct elf *elf,
292 struct symbol *file,
293 const char *name)
294 {
295 struct symbol *sym;
296
297 elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
298 if (sym->bind == STB_LOCAL && sym->file == file &&
299 !strcmp(sym->name, name)) {
300 return sym;
301 }
302 }
303
304 return NULL;
305 }
306
find_global_symbol_by_name(const struct elf * elf,const char * name)307 struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name)
308 {
309 struct symbol *sym;
310
311 elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
312 if (!strcmp(sym->name, name) && !is_local_sym(sym))
313 return sym;
314 }
315
316 return NULL;
317 }
318
find_reloc_by_dest_range(const struct elf * elf,struct section * sec,unsigned long offset,unsigned int len)319 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
320 unsigned long offset, unsigned int len)
321 {
322 struct reloc *reloc, *r = NULL;
323 struct section *rsec;
324 unsigned long o;
325
326 rsec = sec->rsec;
327 if (!rsec)
328 return NULL;
329
330 for_offset_range(o, offset, offset + len) {
331 elf_hash_for_each_possible(reloc, reloc, hash,
332 sec_offset_hash(rsec, o)) {
333 if (reloc->sec != rsec)
334 continue;
335
336 if (reloc_offset(reloc) >= offset &&
337 reloc_offset(reloc) < offset + len) {
338 if (!r || reloc_offset(reloc) < reloc_offset(r))
339 r = reloc;
340 }
341 }
342 if (r)
343 return r;
344 }
345
346 return NULL;
347 }
348
find_reloc_by_dest(const struct elf * elf,struct section * sec,unsigned long offset)349 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
350 {
351 return find_reloc_by_dest_range(elf, sec, offset, 1);
352 }
353
is_dwarf_section(struct section * sec)354 static bool is_dwarf_section(struct section *sec)
355 {
356 return !strncmp(sec->name, ".debug_", 7);
357 }
358
read_sections(struct elf * elf)359 static int read_sections(struct elf *elf)
360 {
361 Elf_Scn *s = NULL;
362 struct section *sec;
363 size_t shstrndx, sections_nr;
364 int i;
365
366 if (elf_getshdrnum(elf->elf, §ions_nr)) {
367 ERROR_ELF("elf_getshdrnum");
368 return -1;
369 }
370
371 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
372 ERROR_ELF("elf_getshdrstrndx");
373 return -1;
374 }
375
376 if (!elf_alloc_hash(section, sections_nr) ||
377 !elf_alloc_hash(section_name, sections_nr))
378 return -1;
379
380 elf->section_data = calloc(sections_nr, sizeof(*sec));
381 if (!elf->section_data) {
382 ERROR_GLIBC("calloc");
383 return -1;
384 }
385 for (i = 0; i < sections_nr; i++) {
386 sec = &elf->section_data[i];
387
388 INIT_LIST_HEAD(&sec->symbol_list);
389
390 s = elf_getscn(elf->elf, i);
391 if (!s) {
392 ERROR_ELF("elf_getscn");
393 return -1;
394 }
395
396 sec->idx = elf_ndxscn(s);
397
398 if (!gelf_getshdr(s, &sec->sh)) {
399 ERROR_ELF("gelf_getshdr");
400 return -1;
401 }
402
403 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
404 if (!sec->name) {
405 ERROR_ELF("elf_strptr");
406 return -1;
407 }
408
409 if (sec_size(sec) != 0 && !is_dwarf_section(sec)) {
410 sec->data = elf_getdata(s, NULL);
411 if (!sec->data) {
412 ERROR_ELF("elf_getdata");
413 return -1;
414 }
415 if (sec->data->d_off != 0 ||
416 sec->data->d_size != sec_size(sec)) {
417 ERROR("unexpected data attributes for %s", sec->name);
418 return -1;
419 }
420 }
421
422 list_add_tail(&sec->list, &elf->sections);
423 elf_hash_add(section, &sec->hash, sec->idx);
424 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
425
426 if (is_reloc_sec(sec))
427 elf->num_relocs += sec_num_entries(sec);
428 }
429
430 if (opts.stats) {
431 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
432 printf("section_bits: %d\n", elf->section_bits);
433 }
434
435 /* sanity check, one more call to elf_nextscn() should return NULL */
436 if (elf_nextscn(elf->elf, s)) {
437 ERROR("section entry mismatch");
438 return -1;
439 }
440
441 return 0;
442 }
443
demangle_name(struct symbol * sym)444 static const char *demangle_name(struct symbol *sym)
445 {
446 char *str;
447
448 if (!is_local_sym(sym))
449 return sym->name;
450
451 if (!is_func_sym(sym) && !is_object_sym(sym))
452 return sym->name;
453
454 if (!strstarts(sym->name, "__UNIQUE_ID_") && !strchr(sym->name, '.'))
455 return sym->name;
456
457 str = strdup(sym->name);
458 if (!str) {
459 ERROR_GLIBC("strdup");
460 return NULL;
461 }
462
463 for (int i = strlen(str) - 1; i >= 0; i--) {
464 char c = str[i];
465
466 if (!isdigit(c) && c != '.') {
467 str[i + 1] = '\0';
468 break;
469 }
470 }
471
472 return str;
473 }
474
elf_add_symbol(struct elf * elf,struct symbol * sym)475 static int elf_add_symbol(struct elf *elf, struct symbol *sym)
476 {
477 struct list_head *entry;
478 struct rb_node *pnode;
479 struct symbol *iter;
480
481 INIT_LIST_HEAD(&sym->pv_target);
482 sym->alias = sym;
483
484 sym->type = GELF_ST_TYPE(sym->sym.st_info);
485 sym->bind = GELF_ST_BIND(sym->sym.st_info);
486
487 if (is_file_sym(sym))
488 elf->num_files++;
489
490 sym->offset = sym->sym.st_value;
491 sym->len = sym->sym.st_size;
492
493 __sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
494 if (!is_undef_sym(iter) && iter->offset == sym->offset &&
495 iter->type == sym->type && iter->len == sym->len)
496 iter->alias = sym;
497 }
498
499 __sym_insert(sym, &sym->sec->symbol_tree);
500 pnode = rb_prev(&sym->node);
501 if (pnode)
502 entry = &rb_entry(pnode, struct symbol, node)->list;
503 else
504 entry = &sym->sec->symbol_list;
505 list_add(&sym->list, entry);
506
507 list_add_tail(&sym->global_list, &elf->symbols);
508 elf_hash_add(symbol, &sym->hash, sym->idx);
509 elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
510
511 if (is_func_sym(sym) &&
512 (strstarts(sym->name, "__pfx_") ||
513 strstarts(sym->name, "__cfi_") ||
514 strstarts(sym->name, "__pi___pfx_") ||
515 strstarts(sym->name, "__pi___cfi_")))
516 sym->prefix = 1;
517
518 if (strstarts(sym->name, ".klp.sym"))
519 sym->klp = 1;
520
521 if (!sym->klp && !is_sec_sym(sym) && strstr(sym->name, ".cold")) {
522 sym->cold = 1;
523
524 /*
525 * Clang doesn't mark cold subfunctions as STT_FUNC, which
526 * breaks several objtool assumptions. Fake it.
527 */
528 sym->type = STT_FUNC;
529 }
530
531 sym->pfunc = sym->cfunc = sym;
532
533 sym->demangled_name = demangle_name(sym);
534 if (!sym->demangled_name)
535 return -1;
536
537 return 0;
538 }
539
read_symbols(struct elf * elf)540 static int read_symbols(struct elf *elf)
541 {
542 struct section *symtab, *symtab_shndx, *sec;
543 struct symbol *sym, *pfunc, *file = NULL;
544 int symbols_nr, i;
545 char *coldstr;
546 Elf_Data *shndx_data = NULL;
547 Elf32_Word shndx;
548
549 symtab = find_section_by_name(elf, ".symtab");
550 if (symtab) {
551 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
552 if (symtab_shndx)
553 shndx_data = symtab_shndx->data;
554
555 symbols_nr = sec_num_entries(symtab);
556 } else {
557 /*
558 * A missing symbol table is actually possible if it's an empty
559 * .o file. This can happen for thunk_64.o. Make sure to at
560 * least allocate the symbol hash tables so we can do symbol
561 * lookups without crashing.
562 */
563 symbols_nr = 0;
564 }
565
566 if (!elf_alloc_hash(symbol, symbols_nr) ||
567 !elf_alloc_hash(symbol_name, symbols_nr))
568 return -1;
569
570 elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
571 if (!elf->symbol_data) {
572 ERROR_GLIBC("calloc");
573 return -1;
574 }
575
576 INIT_LIST_HEAD(&elf->symbols);
577
578 for (i = 0; i < symbols_nr; i++) {
579 sym = &elf->symbol_data[i];
580
581 sym->idx = i;
582
583 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
584 &shndx)) {
585 ERROR_ELF("gelf_getsymshndx");
586 return -1;
587 }
588
589 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
590 sym->sym.st_name);
591 if (!sym->name) {
592 ERROR_ELF("elf_strptr");
593 return -1;
594 }
595
596 if ((sym->sym.st_shndx > SHN_UNDEF &&
597 sym->sym.st_shndx < SHN_LORESERVE) ||
598 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
599 if (sym->sym.st_shndx != SHN_XINDEX)
600 shndx = sym->sym.st_shndx;
601
602 sym->sec = find_section_by_index(elf, shndx);
603 if (!sym->sec) {
604 ERROR("couldn't find section for symbol %s", sym->name);
605 return -1;
606 }
607 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
608 sym->name = sym->sec->name;
609 sym->sec->sym = sym;
610 }
611 } else
612 sym->sec = find_section_by_index(elf, 0);
613
614 if (elf_add_symbol(elf, sym))
615 return -1;
616
617 if (sym->type == STT_FILE)
618 file = sym;
619 else if (sym->bind == STB_LOCAL)
620 sym->file = file;
621 }
622
623 if (opts.stats) {
624 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
625 printf("symbol_bits: %d\n", elf->symbol_bits);
626 }
627
628 /* Create parent/child links for any cold subfunctions */
629 list_for_each_entry(sec, &elf->sections, list) {
630 sec_for_each_sym(sec, sym) {
631 char *pname;
632 size_t pnamelen;
633
634 if (!sym->cold)
635 continue;
636
637 coldstr = strstr(sym->name, ".cold");
638 if (!coldstr) {
639 ERROR("%s(): cold subfunction without \".cold\"?", sym->name);
640 return -1;
641 }
642
643 pnamelen = coldstr - sym->name;
644 pname = strndup(sym->name, pnamelen);
645 if (!pname) {
646 ERROR("%s(): failed to allocate memory", sym->name);
647 return -1;
648 }
649
650 pfunc = find_local_symbol_by_file_and_name(elf, sym->file, pname);
651 if (!pfunc)
652 pfunc = find_global_symbol_by_name(elf, pname);
653 free(pname);
654
655 if (!pfunc) {
656 ERROR("%s(): can't find parent function", sym->name);
657 return -1;
658 }
659
660 sym->pfunc = pfunc->alias;
661 pfunc->cfunc = sym;
662 pfunc->alias->cfunc = sym;
663
664 /*
665 * Unfortunately, -fnoreorder-functions puts the child
666 * inside the parent. Remove the overlap so we can
667 * have sane assumptions.
668 *
669 * Note that pfunc->len now no longer matches
670 * pfunc->sym.st_size.
671 */
672 if (sym->sec == pfunc->sec &&
673 sym->offset >= pfunc->offset &&
674 sym->offset + sym->len == pfunc->offset + pfunc->len) {
675 pfunc->len -= sym->len;
676 }
677 }
678 }
679
680 return 0;
681 }
682
mark_group_syms(struct elf * elf)683 static int mark_group_syms(struct elf *elf)
684 {
685 struct section *symtab, *sec;
686 struct symbol *sym;
687
688 symtab = find_section_by_name(elf, ".symtab");
689 if (!symtab) {
690 ERROR("no .symtab");
691 return -1;
692 }
693
694 for_each_sec(elf, sec) {
695 if (sec->sh.sh_type == SHT_GROUP &&
696 sec->sh.sh_link == symtab->idx) {
697 sym = find_symbol_by_index(elf, sec->sh.sh_info);
698 if (!sym) {
699 ERROR("%s: can't find SHT_GROUP signature symbol",
700 sec->name);
701 return -1;
702 }
703
704 sym->group_sec = sec;
705 }
706 }
707
708 return 0;
709 }
710
711 /*
712 * @sym's idx has changed. Update the relocs which reference it.
713 */
elf_update_sym_relocs(struct elf * elf,struct symbol * sym)714 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
715 {
716 struct reloc *reloc;
717
718 for (reloc = sym->relocs; reloc; reloc = sym_next_reloc(reloc))
719 set_reloc_sym(elf, reloc, reloc->sym->idx);
720
721 return 0;
722 }
723
724 /*
725 * The libelf API is terrible; gelf_update_sym*() takes a data block relative
726 * index value, *NOT* the symbol index. As such, iterate the data blocks and
727 * adjust index until it fits.
728 *
729 * If no data block is found, allow adding a new data block provided the index
730 * is only one past the end.
731 */
elf_update_symbol(struct elf * elf,struct section * symtab,struct section * symtab_shndx,struct symbol * sym)732 static int elf_update_symbol(struct elf *elf, struct section *symtab,
733 struct section *symtab_shndx, struct symbol *sym)
734 {
735 Elf32_Word shndx;
736 Elf_Data *symtab_data = NULL, *shndx_data = NULL;
737 Elf64_Xword entsize = symtab->sh.sh_entsize;
738 int max_idx, idx = sym->idx;
739 Elf_Scn *s, *t = NULL;
740 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
741 sym->sym.st_shndx != SHN_XINDEX;
742
743 shndx = is_special_shndx ? sym->sym.st_shndx : sym->sec->idx;
744
745 s = elf_getscn(elf->elf, symtab->idx);
746 if (!s) {
747 ERROR_ELF("elf_getscn");
748 return -1;
749 }
750
751 if (symtab_shndx) {
752 t = elf_getscn(elf->elf, symtab_shndx->idx);
753 if (!t) {
754 ERROR_ELF("elf_getscn");
755 return -1;
756 }
757 }
758
759 for (;;) {
760 /* get next data descriptor for the relevant sections */
761 symtab_data = elf_getdata(s, symtab_data);
762 if (t)
763 shndx_data = elf_getdata(t, shndx_data);
764
765 /* end-of-list */
766 if (!symtab_data) {
767 /*
768 * Over-allocate to avoid O(n^2) symbol creation
769 * behaviour. The down side is that libelf doesn't
770 * like this; see elf_truncate_section() for the fixup.
771 */
772 int num = max(1U, sym->idx/3);
773 void *buf;
774
775 if (idx) {
776 /* we don't do holes in symbol tables */
777 ERROR("index out of range");
778 return -1;
779 }
780
781 /* if @idx == 0, it's the next contiguous entry, create it */
782 symtab_data = elf_newdata(s);
783 if (t)
784 shndx_data = elf_newdata(t);
785
786 buf = calloc(num, entsize);
787 if (!buf) {
788 ERROR_GLIBC("calloc");
789 return -1;
790 }
791
792 symtab_data->d_buf = buf;
793 symtab_data->d_size = num * entsize;
794 symtab_data->d_align = 1;
795 symtab_data->d_type = ELF_T_SYM;
796
797 mark_sec_changed(elf, symtab, true);
798 symtab->truncate = true;
799
800 if (t) {
801 buf = calloc(num, sizeof(Elf32_Word));
802 if (!buf) {
803 ERROR_GLIBC("calloc");
804 return -1;
805 }
806
807 shndx_data->d_buf = buf;
808 shndx_data->d_size = num * sizeof(Elf32_Word);
809 shndx_data->d_align = sizeof(Elf32_Word);
810 shndx_data->d_type = ELF_T_WORD;
811
812 mark_sec_changed(elf, symtab_shndx, true);
813 symtab_shndx->truncate = true;
814 }
815
816 break;
817 }
818
819 /* empty blocks should not happen */
820 if (!symtab_data->d_size) {
821 ERROR("zero size data");
822 return -1;
823 }
824
825 /* is this the right block? */
826 max_idx = symtab_data->d_size / entsize;
827 if (idx < max_idx)
828 break;
829
830 /* adjust index and try again */
831 idx -= max_idx;
832 }
833
834 /* something went side-ways */
835 if (idx < 0) {
836 ERROR("negative index");
837 return -1;
838 }
839
840 /* setup extended section index magic and write the symbol */
841 if (shndx < SHN_LORESERVE || is_special_shndx) {
842 sym->sym.st_shndx = shndx;
843 if (!shndx_data)
844 shndx = 0;
845 } else {
846 sym->sym.st_shndx = SHN_XINDEX;
847 if (!shndx_data) {
848 ERROR("no .symtab_shndx");
849 return -1;
850 }
851 }
852
853 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
854 ERROR_ELF("gelf_update_symshndx");
855 return -1;
856 }
857
858 return 0;
859 }
860
elf_create_symbol(struct elf * elf,const char * name,struct section * sec,unsigned int bind,unsigned int type,unsigned long offset,size_t size)861 struct symbol *elf_create_symbol(struct elf *elf, const char *name,
862 struct section *sec, unsigned int bind,
863 unsigned int type, unsigned long offset,
864 size_t size)
865 {
866 struct section *symtab, *symtab_shndx;
867 Elf32_Word first_non_local, new_idx;
868 struct symbol *old, *sym;
869
870 sym = calloc(1, sizeof(*sym));
871 if (!sym) {
872 ERROR_GLIBC("calloc");
873 return NULL;
874 }
875
876 sym->name = strdup(name);
877 if (!sym->name) {
878 ERROR_GLIBC("strdup");
879 return NULL;
880 }
881
882 if (type != STT_SECTION) {
883 sym->sym.st_name = elf_add_string(elf, NULL, sym->name);
884 if (sym->sym.st_name == -1)
885 return NULL;
886 }
887
888 if (sec) {
889 sym->sec = sec;
890 } else {
891 sym->sec = find_section_by_index(elf, 0);
892 if (!sym->sec) {
893 ERROR("no NULL section");
894 return NULL;
895 }
896 }
897
898 sym->sym.st_info = GELF_ST_INFO(bind, type);
899 sym->sym.st_value = offset;
900 sym->sym.st_size = size;
901
902 symtab = find_section_by_name(elf, ".symtab");
903 if (!symtab) {
904 ERROR("no .symtab");
905 return NULL;
906 }
907
908 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
909
910 new_idx = sec_num_entries(symtab);
911
912 if (bind != STB_LOCAL)
913 goto non_local;
914
915 /*
916 * Move the first global symbol, as per sh_info, into a new, higher
917 * symbol index. This frees up a spot for a new local symbol.
918 */
919 first_non_local = symtab->sh.sh_info;
920 old = find_symbol_by_index(elf, first_non_local);
921 if (old) {
922
923 elf_hash_del(symbol, &old->hash, old->idx);
924 elf_hash_add(symbol, &old->hash, new_idx);
925 old->idx = new_idx;
926
927 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
928 ERROR("elf_update_symbol move");
929 return NULL;
930 }
931
932 if (elf_update_sym_relocs(elf, old))
933 return NULL;
934
935 if (old->group_sec) {
936 old->group_sec->sh.sh_info = new_idx;
937 mark_sec_changed(elf, old->group_sec, true);
938 }
939
940 new_idx = first_non_local;
941 }
942
943 /*
944 * Either way, we will add a LOCAL symbol.
945 */
946 symtab->sh.sh_info += 1;
947
948 non_local:
949 sym->idx = new_idx;
950 if (sym->idx && elf_update_symbol(elf, symtab, symtab_shndx, sym))
951 return NULL;
952
953 symtab->sh.sh_size += symtab->sh.sh_entsize;
954 mark_sec_changed(elf, symtab, true);
955
956 if (symtab_shndx) {
957 symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
958 mark_sec_changed(elf, symtab_shndx, true);
959 }
960
961 if (elf_add_symbol(elf, sym))
962 return NULL;
963
964 return sym;
965 }
966
elf_create_section_symbol(struct elf * elf,struct section * sec)967 struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec)
968 {
969 struct symbol *sym = calloc(1, sizeof(*sym));
970
971 sym = elf_create_symbol(elf, sec->name, sec, STB_LOCAL, STT_SECTION, 0, 0);
972 if (!sym)
973 return NULL;
974
975 sec->sym = sym;
976
977 return sym;
978 }
979
elf_init_reloc(struct elf * elf,struct section * rsec,unsigned int reloc_idx,unsigned long offset,struct symbol * sym,s64 addend,unsigned int type)980 struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
981 unsigned int reloc_idx, unsigned long offset,
982 struct symbol *sym, s64 addend, unsigned int type)
983 {
984 struct reloc *reloc, empty = { 0 };
985
986 if (reloc_idx >= sec_num_entries(rsec)) {
987 ERROR("%s: bad reloc_idx %u for %s with %d relocs",
988 __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
989 return NULL;
990 }
991
992 reloc = &rsec->relocs[reloc_idx];
993
994 if (memcmp(reloc, &empty, sizeof(empty))) {
995 ERROR("%s: %s: reloc %d already initialized!",
996 __func__, rsec->name, reloc_idx);
997 return NULL;
998 }
999
1000 reloc->sec = rsec;
1001 reloc->sym = sym;
1002
1003 set_reloc_offset(elf, reloc, offset);
1004 set_reloc_sym(elf, reloc, sym->idx);
1005 set_reloc_type(elf, reloc, type);
1006 set_reloc_addend(elf, reloc, addend);
1007
1008 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
1009 set_sym_next_reloc(reloc, sym->relocs);
1010 sym->relocs = reloc;
1011
1012 return reloc;
1013 }
1014
elf_init_reloc_text_sym(struct elf * elf,struct section * sec,unsigned long offset,unsigned int reloc_idx,struct section * insn_sec,unsigned long insn_off)1015 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
1016 unsigned long offset,
1017 unsigned int reloc_idx,
1018 struct section *insn_sec,
1019 unsigned long insn_off)
1020 {
1021 struct symbol *sym = insn_sec->sym;
1022 s64 addend = insn_off;
1023
1024 if (!is_text_sec(insn_sec)) {
1025 ERROR("bad call to %s() for data symbol %s", __func__, sym->name);
1026 return NULL;
1027 }
1028
1029 if (!sym) {
1030 /*
1031 * Due to how weak functions work, we must use section based
1032 * relocations. Symbol based relocations would result in the
1033 * weak and non-weak function annotations being overlaid on the
1034 * non-weak function after linking.
1035 */
1036 sym = elf_create_section_symbol(elf, insn_sec);
1037 if (!sym)
1038 return NULL;
1039 }
1040
1041 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
1042 elf_text_rela_type(elf));
1043 }
1044
elf_init_reloc_data_sym(struct elf * elf,struct section * sec,unsigned long offset,unsigned int reloc_idx,struct symbol * sym,s64 addend)1045 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
1046 unsigned long offset,
1047 unsigned int reloc_idx,
1048 struct symbol *sym,
1049 s64 addend)
1050 {
1051 if (is_text_sec(sec)) {
1052 ERROR("bad call to %s() for text symbol %s", __func__, sym->name);
1053 return NULL;
1054 }
1055
1056 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
1057 elf_data_rela_type(elf));
1058 }
1059
read_relocs(struct elf * elf)1060 static int read_relocs(struct elf *elf)
1061 {
1062 unsigned long nr_reloc, max_reloc = 0;
1063 struct section *rsec;
1064 struct reloc *reloc;
1065 unsigned int symndx;
1066 struct symbol *sym;
1067 int i;
1068
1069 if (!elf_alloc_hash(reloc, elf->num_relocs))
1070 return -1;
1071
1072 list_for_each_entry(rsec, &elf->sections, list) {
1073 if (!is_reloc_sec(rsec))
1074 continue;
1075
1076 rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
1077 if (!rsec->base) {
1078 ERROR("can't find base section for reloc section %s", rsec->name);
1079 return -1;
1080 }
1081
1082 rsec->base->rsec = rsec;
1083
1084 /* nr_alloc_relocs=0: libelf owns d_buf */
1085 rsec->nr_alloc_relocs = 0;
1086
1087 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
1088 if (!rsec->relocs) {
1089 ERROR_GLIBC("calloc");
1090 return -1;
1091 }
1092
1093 nr_reloc = 0;
1094 for (i = 0; i < sec_num_entries(rsec); i++) {
1095 reloc = &rsec->relocs[i];
1096
1097 reloc->sec = rsec;
1098 symndx = reloc_sym(reloc);
1099 reloc->sym = sym = find_symbol_by_index(elf, symndx);
1100 if (!reloc->sym) {
1101 ERROR("can't find reloc entry symbol %d for %s", symndx, rsec->name);
1102 return -1;
1103 }
1104
1105 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
1106 set_sym_next_reloc(reloc, sym->relocs);
1107 sym->relocs = reloc;
1108
1109 nr_reloc++;
1110 }
1111 max_reloc = max(max_reloc, nr_reloc);
1112 }
1113
1114 if (opts.stats) {
1115 printf("max_reloc: %lu\n", max_reloc);
1116 printf("num_relocs: %lu\n", elf->num_relocs);
1117 printf("reloc_bits: %d\n", elf->reloc_bits);
1118 }
1119
1120 return 0;
1121 }
1122
elf_open_read(const char * name,int flags)1123 struct elf *elf_open_read(const char *name, int flags)
1124 {
1125 struct elf *elf;
1126 Elf_Cmd cmd;
1127
1128 elf_version(EV_CURRENT);
1129
1130 elf = malloc(sizeof(*elf));
1131 if (!elf) {
1132 ERROR_GLIBC("malloc");
1133 return NULL;
1134 }
1135 memset(elf, 0, sizeof(*elf));
1136
1137 INIT_LIST_HEAD(&elf->sections);
1138
1139 elf->fd = open(name, flags);
1140 if (elf->fd == -1) {
1141 fprintf(stderr, "objtool: Can't open '%s': %s\n",
1142 name, strerror(errno));
1143 goto err;
1144 }
1145
1146 elf->name = strdup(name);
1147 if (!elf->name) {
1148 ERROR_GLIBC("strdup");
1149 return NULL;
1150 }
1151
1152 if ((flags & O_ACCMODE) == O_RDONLY)
1153 cmd = ELF_C_READ_MMAP;
1154 else if ((flags & O_ACCMODE) == O_RDWR)
1155 cmd = ELF_C_RDWR;
1156 else /* O_WRONLY */
1157 cmd = ELF_C_WRITE;
1158
1159 elf->elf = elf_begin(elf->fd, cmd, NULL);
1160 if (!elf->elf) {
1161 ERROR_ELF("elf_begin");
1162 goto err;
1163 }
1164
1165 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1166 ERROR_ELF("gelf_getehdr");
1167 goto err;
1168 }
1169
1170 if (read_sections(elf))
1171 goto err;
1172
1173 if (read_symbols(elf))
1174 goto err;
1175
1176 if (mark_group_syms(elf))
1177 goto err;
1178
1179 if (read_relocs(elf))
1180 goto err;
1181
1182 return elf;
1183
1184 err:
1185 elf_close(elf);
1186 return NULL;
1187 }
1188
elf_create_file(GElf_Ehdr * ehdr,const char * name)1189 struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
1190 {
1191 struct section *null, *symtab, *strtab, *shstrtab;
1192 char *dir, *base, *tmp_name;
1193 struct symbol *sym;
1194 struct elf *elf;
1195
1196 elf_version(EV_CURRENT);
1197
1198 elf = calloc(1, sizeof(*elf));
1199 if (!elf) {
1200 ERROR_GLIBC("calloc");
1201 return NULL;
1202 }
1203
1204 INIT_LIST_HEAD(&elf->sections);
1205
1206 dir = strdup(name);
1207 if (!dir) {
1208 ERROR_GLIBC("strdup");
1209 return NULL;
1210 }
1211
1212 dir = dirname(dir);
1213
1214 base = strdup(name);
1215 if (!base) {
1216 ERROR_GLIBC("strdup");
1217 return NULL;
1218 }
1219
1220 base = basename(base);
1221
1222 tmp_name = malloc(256);
1223 if (!tmp_name) {
1224 ERROR_GLIBC("malloc");
1225 return NULL;
1226 }
1227
1228 snprintf(tmp_name, 256, "%s/%s.XXXXXX", dir, base);
1229
1230 elf->fd = mkstemp(tmp_name);
1231 if (elf->fd == -1) {
1232 ERROR_GLIBC("can't create tmp file");
1233 exit(1);
1234 }
1235
1236 elf->tmp_name = tmp_name;
1237
1238 elf->name = strdup(name);
1239 if (!elf->name) {
1240 ERROR_GLIBC("strdup");
1241 return NULL;
1242 }
1243
1244 elf->elf = elf_begin(elf->fd, ELF_C_WRITE, NULL);
1245 if (!elf->elf) {
1246 ERROR_ELF("elf_begin");
1247 return NULL;
1248 }
1249
1250 if (!gelf_newehdr(elf->elf, ELFCLASS64)) {
1251 ERROR_ELF("gelf_newehdr");
1252 return NULL;
1253 }
1254
1255 memcpy(&elf->ehdr, ehdr, sizeof(elf->ehdr));
1256
1257 if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) {
1258 ERROR_ELF("gelf_update_ehdr");
1259 return NULL;
1260 }
1261
1262 INIT_LIST_HEAD(&elf->symbols);
1263
1264 if (!elf_alloc_hash(section, 1000) ||
1265 !elf_alloc_hash(section_name, 1000) ||
1266 !elf_alloc_hash(symbol, 10000) ||
1267 !elf_alloc_hash(symbol_name, 10000) ||
1268 !elf_alloc_hash(reloc, 100000))
1269 return NULL;
1270
1271 null = elf_create_section(elf, NULL, 0, 0, SHT_NULL, 0, 0);
1272 shstrtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0);
1273 strtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0);
1274
1275 if (!null || !shstrtab || !strtab)
1276 return NULL;
1277
1278 null->name = "";
1279 shstrtab->name = ".shstrtab";
1280 strtab->name = ".strtab";
1281
1282 null->sh.sh_name = elf_add_string(elf, shstrtab, null->name);
1283 shstrtab->sh.sh_name = elf_add_string(elf, shstrtab, shstrtab->name);
1284 strtab->sh.sh_name = elf_add_string(elf, shstrtab, strtab->name);
1285
1286 if (null->sh.sh_name == -1 || shstrtab->sh.sh_name == -1 || strtab->sh.sh_name == -1)
1287 return NULL;
1288
1289 elf_hash_add(section_name, &null->name_hash, str_hash(null->name));
1290 elf_hash_add(section_name, &strtab->name_hash, str_hash(strtab->name));
1291 elf_hash_add(section_name, &shstrtab->name_hash, str_hash(shstrtab->name));
1292
1293 if (elf_add_string(elf, strtab, "") == -1)
1294 return NULL;
1295
1296 symtab = elf_create_section(elf, ".symtab", 0x18, 0x18, SHT_SYMTAB, 0x8, 0);
1297 if (!symtab)
1298 return NULL;
1299
1300 symtab->sh.sh_link = strtab->idx;
1301 symtab->sh.sh_info = 1;
1302
1303 elf->ehdr.e_shstrndx = shstrtab->idx;
1304 if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) {
1305 ERROR_ELF("gelf_update_ehdr");
1306 return NULL;
1307 }
1308
1309 sym = calloc(1, sizeof(*sym));
1310 if (!sym) {
1311 ERROR_GLIBC("calloc");
1312 return NULL;
1313 }
1314
1315 sym->name = "";
1316 sym->sec = null;
1317 elf_add_symbol(elf, sym);
1318
1319 return elf;
1320 }
1321
elf_add_string(struct elf * elf,struct section * strtab,const char * str)1322 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
1323 {
1324 unsigned int offset;
1325
1326 if (!strtab)
1327 strtab = find_section_by_name(elf, ".strtab");
1328 if (!strtab) {
1329 ERROR("can't find .strtab section");
1330 return -1;
1331 }
1332
1333 if (!strtab->sh.sh_addralign) {
1334 ERROR("'%s': invalid sh_addralign", strtab->name);
1335 return -1;
1336 }
1337
1338 offset = ALIGN(strtab->sh.sh_size, strtab->sh.sh_addralign);
1339
1340 if (!elf_add_data(elf, strtab, str, strlen(str) + 1))
1341 return -1;
1342
1343 return offset;
1344 }
1345
elf_add_data(struct elf * elf,struct section * sec,const void * data,size_t size)1346 void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_t size)
1347 {
1348 unsigned long offset;
1349 Elf_Scn *s;
1350
1351 if (!sec->sh.sh_addralign) {
1352 ERROR("'%s': invalid sh_addralign", sec->name);
1353 return NULL;
1354 }
1355
1356 s = elf_getscn(elf->elf, sec->idx);
1357 if (!s) {
1358 ERROR_ELF("elf_getscn");
1359 return NULL;
1360 }
1361
1362 sec->data = elf_newdata(s);
1363 if (!sec->data) {
1364 ERROR_ELF("elf_newdata");
1365 return NULL;
1366 }
1367
1368 sec->data->d_buf = calloc(1, size);
1369 if (!sec->data->d_buf) {
1370 ERROR_GLIBC("calloc");
1371 return NULL;
1372 }
1373
1374 if (data)
1375 memcpy(sec->data->d_buf, data, size);
1376
1377 sec->data->d_size = size;
1378 sec->data->d_align = 1;
1379
1380 offset = ALIGN(sec->sh.sh_size, sec->sh.sh_addralign);
1381 sec->sh.sh_size = offset + size;
1382
1383 mark_sec_changed(elf, sec, true);
1384
1385 return sec->data->d_buf;
1386 }
1387
elf_create_section(struct elf * elf,const char * name,size_t size,size_t entsize,unsigned int type,unsigned int align,unsigned int flags)1388 struct section *elf_create_section(struct elf *elf, const char *name,
1389 size_t size, size_t entsize,
1390 unsigned int type, unsigned int align,
1391 unsigned int flags)
1392 {
1393 struct section *sec, *shstrtab;
1394 Elf_Scn *s;
1395
1396 if (name && find_section_by_name(elf, name)) {
1397 ERROR("section '%s' already exists", name);
1398 return NULL;
1399 }
1400
1401 sec = calloc(1, sizeof(*sec));
1402 if (!sec) {
1403 ERROR_GLIBC("calloc");
1404 return NULL;
1405 }
1406
1407 INIT_LIST_HEAD(&sec->symbol_list);
1408
1409 /* don't actually create the section, just the data structures */
1410 if (type == SHT_NULL)
1411 goto add;
1412
1413 s = elf_newscn(elf->elf);
1414 if (!s) {
1415 ERROR_ELF("elf_newscn");
1416 return NULL;
1417 }
1418
1419 sec->idx = elf_ndxscn(s);
1420
1421 if (size) {
1422 sec->data = elf_newdata(s);
1423 if (!sec->data) {
1424 ERROR_ELF("elf_newdata");
1425 return NULL;
1426 }
1427
1428 sec->data->d_size = size;
1429 sec->data->d_align = 1;
1430
1431 sec->data->d_buf = calloc(1, size);
1432 if (!sec->data->d_buf) {
1433 ERROR_GLIBC("calloc");
1434 return NULL;
1435 }
1436 }
1437
1438 if (!gelf_getshdr(s, &sec->sh)) {
1439 ERROR_ELF("gelf_getshdr");
1440 return NULL;
1441 }
1442
1443 sec->sh.sh_size = size;
1444 sec->sh.sh_entsize = entsize;
1445 sec->sh.sh_type = type;
1446 sec->sh.sh_addralign = align;
1447 sec->sh.sh_flags = flags;
1448
1449 if (name) {
1450 sec->name = strdup(name);
1451 if (!sec->name) {
1452 ERROR("strdup");
1453 return NULL;
1454 }
1455
1456 /* Add section name to .shstrtab (or .strtab for Clang) */
1457 shstrtab = find_section_by_name(elf, ".shstrtab");
1458 if (!shstrtab) {
1459 shstrtab = find_section_by_name(elf, ".strtab");
1460 if (!shstrtab) {
1461 ERROR("can't find .shstrtab or .strtab");
1462 return NULL;
1463 }
1464 }
1465 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1466 if (sec->sh.sh_name == -1)
1467 return NULL;
1468
1469 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
1470 }
1471
1472 add:
1473 list_add_tail(&sec->list, &elf->sections);
1474 elf_hash_add(section, &sec->hash, sec->idx);
1475
1476 mark_sec_changed(elf, sec, true);
1477
1478 return sec;
1479 }
1480
elf_alloc_reloc(struct elf * elf,struct section * rsec)1481 static int elf_alloc_reloc(struct elf *elf, struct section *rsec)
1482 {
1483 struct reloc *old_relocs, *old_relocs_end, *new_relocs;
1484 unsigned int nr_relocs_old = sec_num_entries(rsec);
1485 unsigned int nr_relocs_new = nr_relocs_old + 1;
1486 unsigned long nr_alloc;
1487 struct symbol *sym;
1488
1489 if (!rsec->data) {
1490 rsec->data = elf_newdata(elf_getscn(elf->elf, rsec->idx));
1491 if (!rsec->data) {
1492 ERROR_ELF("elf_newdata");
1493 return -1;
1494 }
1495
1496 rsec->data->d_align = 1;
1497 rsec->data->d_type = ELF_T_RELA;
1498 rsec->data->d_buf = NULL;
1499 }
1500
1501 rsec->data->d_size = nr_relocs_new * elf_rela_size(elf);
1502 rsec->sh.sh_size = rsec->data->d_size;
1503
1504 nr_alloc = max(64UL, roundup_pow_of_two(nr_relocs_new));
1505 if (nr_alloc <= rsec->nr_alloc_relocs)
1506 return 0;
1507
1508 if (rsec->data->d_buf && !rsec->nr_alloc_relocs) {
1509 void *orig_buf = rsec->data->d_buf;
1510
1511 /*
1512 * The original d_buf is owned by libelf so it can't be
1513 * realloced.
1514 */
1515 rsec->data->d_buf = malloc(nr_alloc * elf_rela_size(elf));
1516 if (!rsec->data->d_buf) {
1517 ERROR_GLIBC("malloc");
1518 return -1;
1519 }
1520 memcpy(rsec->data->d_buf, orig_buf,
1521 nr_relocs_old * elf_rela_size(elf));
1522 } else {
1523 rsec->data->d_buf = realloc(rsec->data->d_buf,
1524 nr_alloc * elf_rela_size(elf));
1525 if (!rsec->data->d_buf) {
1526 ERROR_GLIBC("realloc");
1527 return -1;
1528 }
1529 }
1530
1531 rsec->nr_alloc_relocs = nr_alloc;
1532
1533 old_relocs = rsec->relocs;
1534 new_relocs = calloc(nr_alloc, sizeof(struct reloc));
1535 if (!new_relocs) {
1536 ERROR_GLIBC("calloc");
1537 return -1;
1538 }
1539
1540 if (!old_relocs)
1541 goto done;
1542
1543 /*
1544 * The struct reloc's address has changed. Update all the symbols and
1545 * relocs which reference it.
1546 */
1547
1548 old_relocs_end = &old_relocs[nr_relocs_old];
1549 for_each_sym(elf, sym) {
1550 struct reloc *reloc;
1551
1552 reloc = sym->relocs;
1553 if (!reloc)
1554 continue;
1555
1556 if (reloc >= old_relocs && reloc < old_relocs_end)
1557 sym->relocs = &new_relocs[reloc - old_relocs];
1558
1559 while (1) {
1560 struct reloc *next_reloc = sym_next_reloc(reloc);
1561
1562 if (!next_reloc)
1563 break;
1564
1565 if (next_reloc >= old_relocs && next_reloc < old_relocs_end)
1566 set_sym_next_reloc(reloc, &new_relocs[next_reloc - old_relocs]);
1567
1568 reloc = next_reloc;
1569 }
1570 }
1571
1572 memcpy(new_relocs, old_relocs, nr_relocs_old * sizeof(struct reloc));
1573
1574 for (int i = 0; i < nr_relocs_old; i++) {
1575 struct reloc *old = &old_relocs[i];
1576 struct reloc *new = &new_relocs[i];
1577 u32 key = reloc_hash(old);
1578
1579 elf_hash_del(reloc, &old->hash, key);
1580 elf_hash_add(reloc, &new->hash, key);
1581 }
1582
1583 free(old_relocs);
1584 done:
1585 rsec->relocs = new_relocs;
1586 return 0;
1587 }
1588
elf_create_rela_section(struct elf * elf,struct section * sec,unsigned int nr_relocs)1589 struct section *elf_create_rela_section(struct elf *elf, struct section *sec,
1590 unsigned int nr_relocs)
1591 {
1592 struct section *rsec;
1593 char *rsec_name;
1594
1595 rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
1596 if (!rsec_name) {
1597 ERROR_GLIBC("malloc");
1598 return NULL;
1599 }
1600 strcpy(rsec_name, ".rela");
1601 strcat(rsec_name, sec->name);
1602
1603 rsec = elf_create_section(elf, rsec_name, nr_relocs * elf_rela_size(elf),
1604 elf_rela_size(elf), SHT_RELA, elf_addr_size(elf),
1605 SHF_INFO_LINK);
1606 free(rsec_name);
1607 if (!rsec)
1608 return NULL;
1609
1610 if (nr_relocs) {
1611 rsec->data->d_type = ELF_T_RELA;
1612
1613 rsec->nr_alloc_relocs = nr_relocs;
1614 rsec->relocs = calloc(nr_relocs, sizeof(struct reloc));
1615 if (!rsec->relocs) {
1616 ERROR_GLIBC("calloc");
1617 return NULL;
1618 }
1619 }
1620
1621 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1622 rsec->sh.sh_info = sec->idx;
1623
1624 sec->rsec = rsec;
1625 rsec->base = sec;
1626
1627 return rsec;
1628 }
1629
elf_create_reloc(struct elf * elf,struct section * sec,unsigned long offset,struct symbol * sym,s64 addend,unsigned int type)1630 struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
1631 unsigned long offset,
1632 struct symbol *sym, s64 addend,
1633 unsigned int type)
1634 {
1635 struct section *rsec = sec->rsec;
1636
1637 if (!rsec) {
1638 rsec = elf_create_rela_section(elf, sec, 0);
1639 if (!rsec)
1640 return NULL;
1641 }
1642
1643 if (find_reloc_by_dest(elf, sec, offset)) {
1644 ERROR_FUNC(sec, offset, "duplicate reloc");
1645 return NULL;
1646 }
1647
1648 if (elf_alloc_reloc(elf, rsec))
1649 return NULL;
1650
1651 mark_sec_changed(elf, rsec, true);
1652
1653 return elf_init_reloc(elf, rsec, sec_num_entries(rsec) - 1, offset, sym,
1654 addend, type);
1655 }
1656
elf_create_section_pair(struct elf * elf,const char * name,size_t entsize,unsigned int nr,unsigned int nr_relocs)1657 struct section *elf_create_section_pair(struct elf *elf, const char *name,
1658 size_t entsize, unsigned int nr,
1659 unsigned int nr_relocs)
1660 {
1661 struct section *sec;
1662
1663 sec = elf_create_section(elf, name, nr * entsize, entsize,
1664 SHT_PROGBITS, 1, SHF_ALLOC);
1665 if (!sec)
1666 return NULL;
1667
1668 if (!elf_create_rela_section(elf, sec, nr_relocs))
1669 return NULL;
1670
1671 return sec;
1672 }
1673
elf_write_insn(struct elf * elf,struct section * sec,unsigned long offset,unsigned int len,const char * insn)1674 int elf_write_insn(struct elf *elf, struct section *sec,
1675 unsigned long offset, unsigned int len,
1676 const char *insn)
1677 {
1678 Elf_Data *data = sec->data;
1679
1680 if (data->d_type != ELF_T_BYTE || data->d_off) {
1681 ERROR("write to unexpected data for section: %s", sec->name);
1682 return -1;
1683 }
1684
1685 memcpy(data->d_buf + offset, insn, len);
1686
1687 mark_sec_changed(elf, sec, true);
1688
1689 return 0;
1690 }
1691
1692 /*
1693 * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
1694 * do you:
1695 *
1696 * A) adhere to the section header and truncate the data, or
1697 * B) ignore the section header and write out all the data you've got?
1698 *
1699 * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
1700 */
elf_truncate_section(struct elf * elf,struct section * sec)1701 static int elf_truncate_section(struct elf *elf, struct section *sec)
1702 {
1703 u64 size = sec_size(sec);
1704 bool truncated = false;
1705 Elf_Data *data = NULL;
1706 Elf_Scn *s;
1707
1708 s = elf_getscn(elf->elf, sec->idx);
1709 if (!s) {
1710 ERROR_ELF("elf_getscn");
1711 return -1;
1712 }
1713
1714 for (;;) {
1715 /* get next data descriptor for the relevant section */
1716 data = elf_getdata(s, data);
1717 if (!data) {
1718 if (size) {
1719 ERROR("end of section data but non-zero size left\n");
1720 return -1;
1721 }
1722 return 0;
1723 }
1724
1725 if (truncated) {
1726 /* when we remove symbols */
1727 ERROR("truncated; but more data\n");
1728 return -1;
1729 }
1730
1731 if (!data->d_size) {
1732 ERROR("zero size data");
1733 return -1;
1734 }
1735
1736 if (data->d_size > size) {
1737 truncated = true;
1738 data->d_size = size;
1739 }
1740
1741 size -= data->d_size;
1742 }
1743 }
1744
elf_write(struct elf * elf)1745 int elf_write(struct elf *elf)
1746 {
1747 struct section *sec;
1748 Elf_Scn *s;
1749
1750 /* Update changed relocation sections and section headers: */
1751 list_for_each_entry(sec, &elf->sections, list) {
1752 if (sec->truncate && elf_truncate_section(elf, sec))
1753 return -1;
1754
1755 if (sec_changed(sec)) {
1756 s = elf_getscn(elf->elf, sec->idx);
1757 if (!s) {
1758 ERROR_ELF("elf_getscn");
1759 return -1;
1760 }
1761
1762 /* Note this also flags the section dirty */
1763 if (!gelf_update_shdr(s, &sec->sh)) {
1764 ERROR_ELF("gelf_update_shdr");
1765 return -1;
1766 }
1767
1768 mark_sec_changed(elf, sec, false);
1769 }
1770 }
1771
1772 /* Make sure the new section header entries get updated properly. */
1773 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1774
1775 /* Write all changes to the file. */
1776 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1777 ERROR_ELF("elf_update");
1778 return -1;
1779 }
1780
1781 elf->changed = false;
1782
1783 return 0;
1784 }
1785
elf_close(struct elf * elf)1786 int elf_close(struct elf *elf)
1787 {
1788 if (elf->elf)
1789 elf_end(elf->elf);
1790
1791 if (elf->fd > 0)
1792 close(elf->fd);
1793
1794 if (elf->tmp_name && rename(elf->tmp_name, elf->name))
1795 return -1;
1796
1797 /*
1798 * NOTE: All remaining allocations are leaked on purpose. Objtool is
1799 * about to exit anyway.
1800 */
1801 return 0;
1802 }
1803