projects/linux·blob v7.2 · refs

1852 lines · 40.4 KiB

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