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