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 <objtool/builtin.h>
20
21 #include <objtool/elf.h>
22 #include <objtool/warn.h>
23
24 #define MAX_NAME_LEN 128
25
str_hash(const char * str)26 static inline u32 str_hash(const char *str)
27 {
28 return jhash(str, strlen(str), 0);
29 }
30
31 #define __elf_table(name) (elf->name##_hash)
32 #define __elf_bits(name) (elf->name##_bits)
33
34 #define elf_hash_add(name, node, key) \
35 hlist_add_head(node, &__elf_table(name)[hash_min(key, __elf_bits(name))])
36
37 #define elf_hash_for_each_possible(name, obj, member, key) \
38 hlist_for_each_entry(obj, &__elf_table(name)[hash_min(key, __elf_bits(name))], member)
39
40 #define elf_alloc_hash(name, size) \
41 ({ \
42 __elf_bits(name) = max(10, ilog2(size)); \
43 __elf_table(name) = mmap(NULL, sizeof(struct hlist_head) << __elf_bits(name), \
44 PROT_READ|PROT_WRITE, \
45 MAP_PRIVATE|MAP_ANON, -1, 0); \
46 if (__elf_table(name) == (void *)-1L) { \
47 WARN("mmap fail " #name); \
48 __elf_table(name) = NULL; \
49 } \
50 __elf_table(name); \
51 })
52
symbol_to_offset(struct rb_node * a,const struct rb_node * b)53 static bool symbol_to_offset(struct rb_node *a, const struct rb_node *b)
54 {
55 struct symbol *sa = rb_entry(a, struct symbol, node);
56 struct symbol *sb = rb_entry(b, struct symbol, node);
57
58 if (sa->offset < sb->offset)
59 return true;
60 if (sa->offset > sb->offset)
61 return false;
62
63 if (sa->len < sb->len)
64 return true;
65 if (sa->len > sb->len)
66 return false;
67
68 sa->alias = sb;
69
70 return false;
71 }
72
symbol_by_offset(const void * key,const struct rb_node * node)73 static int symbol_by_offset(const void *key, const struct rb_node *node)
74 {
75 const struct symbol *s = rb_entry(node, struct symbol, node);
76 const unsigned long *o = key;
77
78 if (*o < s->offset)
79 return -1;
80 if (*o >= s->offset + s->len)
81 return 1;
82
83 return 0;
84 }
85
find_section_by_name(const struct elf * elf,const char * name)86 struct section *find_section_by_name(const struct elf *elf, const char *name)
87 {
88 struct section *sec;
89
90 elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
91 if (!strcmp(sec->name, name))
92 return sec;
93 }
94
95 return NULL;
96 }
97
find_section_by_index(struct elf * elf,unsigned int idx)98 static struct section *find_section_by_index(struct elf *elf,
99 unsigned int idx)
100 {
101 struct section *sec;
102
103 elf_hash_for_each_possible(section, sec, hash, idx) {
104 if (sec->idx == idx)
105 return sec;
106 }
107
108 return NULL;
109 }
110
find_symbol_by_index(struct elf * elf,unsigned int idx)111 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
112 {
113 struct symbol *sym;
114
115 elf_hash_for_each_possible(symbol, sym, hash, idx) {
116 if (sym->idx == idx)
117 return sym;
118 }
119
120 return NULL;
121 }
122
find_symbol_by_offset(struct section * sec,unsigned long offset)123 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
124 {
125 struct rb_node *node;
126
127 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
128 struct symbol *s = rb_entry(node, struct symbol, node);
129
130 if (s->offset == offset && s->type != STT_SECTION)
131 return s;
132 }
133
134 return NULL;
135 }
136
find_func_by_offset(struct section * sec,unsigned long offset)137 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
138 {
139 struct rb_node *node;
140
141 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
142 struct symbol *s = rb_entry(node, struct symbol, node);
143
144 if (s->offset == offset && s->type == STT_FUNC)
145 return s;
146 }
147
148 return NULL;
149 }
150
find_symbol_containing(const struct section * sec,unsigned long offset)151 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
152 {
153 struct rb_node *node;
154
155 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
156 struct symbol *s = rb_entry(node, struct symbol, node);
157
158 if (s->type != STT_SECTION)
159 return s;
160 }
161
162 return NULL;
163 }
164
find_func_containing(struct section * sec,unsigned long offset)165 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
166 {
167 struct rb_node *node;
168
169 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
170 struct symbol *s = rb_entry(node, struct symbol, node);
171
172 if (s->type == STT_FUNC)
173 return s;
174 }
175
176 return NULL;
177 }
178
find_symbol_by_name(const struct elf * elf,const char * name)179 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
180 {
181 struct symbol *sym;
182
183 elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
184 if (!strcmp(sym->name, name))
185 return sym;
186 }
187
188 return NULL;
189 }
190
find_reloc_by_dest_range(const struct elf * elf,struct section * sec,unsigned long offset,unsigned int len)191 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
192 unsigned long offset, unsigned int len)
193 {
194 struct reloc *reloc, *r = NULL;
195 unsigned long o;
196
197 if (!sec->reloc)
198 return NULL;
199
200 sec = sec->reloc;
201
202 for_offset_range(o, offset, offset + len) {
203 elf_hash_for_each_possible(reloc, reloc, hash,
204 sec_offset_hash(sec, o)) {
205 if (reloc->sec != sec)
206 continue;
207
208 if (reloc->offset >= offset && reloc->offset < offset + len) {
209 if (!r || reloc->offset < r->offset)
210 r = reloc;
211 }
212 }
213 if (r)
214 return r;
215 }
216
217 return NULL;
218 }
219
find_reloc_by_dest(const struct elf * elf,struct section * sec,unsigned long offset)220 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
221 {
222 return find_reloc_by_dest_range(elf, sec, offset, 1);
223 }
224
read_sections(struct elf * elf)225 static int read_sections(struct elf *elf)
226 {
227 Elf_Scn *s = NULL;
228 struct section *sec;
229 size_t shstrndx, sections_nr;
230 int i;
231
232 if (elf_getshdrnum(elf->elf, §ions_nr)) {
233 WARN_ELF("elf_getshdrnum");
234 return -1;
235 }
236
237 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
238 WARN_ELF("elf_getshdrstrndx");
239 return -1;
240 }
241
242 if (!elf_alloc_hash(section, sections_nr) ||
243 !elf_alloc_hash(section_name, sections_nr))
244 return -1;
245
246 for (i = 0; i < sections_nr; i++) {
247 sec = malloc(sizeof(*sec));
248 if (!sec) {
249 perror("malloc");
250 return -1;
251 }
252 memset(sec, 0, sizeof(*sec));
253
254 INIT_LIST_HEAD(&sec->symbol_list);
255 INIT_LIST_HEAD(&sec->reloc_list);
256
257 s = elf_getscn(elf->elf, i);
258 if (!s) {
259 WARN_ELF("elf_getscn");
260 return -1;
261 }
262
263 sec->idx = elf_ndxscn(s);
264
265 if (!gelf_getshdr(s, &sec->sh)) {
266 WARN_ELF("gelf_getshdr");
267 return -1;
268 }
269
270 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
271 if (!sec->name) {
272 WARN_ELF("elf_strptr");
273 return -1;
274 }
275
276 if (sec->sh.sh_size != 0) {
277 sec->data = elf_getdata(s, NULL);
278 if (!sec->data) {
279 WARN_ELF("elf_getdata");
280 return -1;
281 }
282 if (sec->data->d_off != 0 ||
283 sec->data->d_size != sec->sh.sh_size) {
284 WARN("unexpected data attributes for %s",
285 sec->name);
286 return -1;
287 }
288 }
289
290 if (sec->sh.sh_flags & SHF_EXECINSTR)
291 elf->text_size += sec->sh.sh_size;
292
293 list_add_tail(&sec->list, &elf->sections);
294 elf_hash_add(section, &sec->hash, sec->idx);
295 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
296 }
297
298 if (stats) {
299 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
300 printf("section_bits: %d\n", elf->section_bits);
301 }
302
303 /* sanity check, one more call to elf_nextscn() should return NULL */
304 if (elf_nextscn(elf->elf, s)) {
305 WARN("section entry mismatch");
306 return -1;
307 }
308
309 return 0;
310 }
311
elf_add_symbol(struct elf * elf,struct symbol * sym)312 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
313 {
314 struct list_head *entry;
315 struct rb_node *pnode;
316
317 sym->alias = sym;
318
319 sym->type = GELF_ST_TYPE(sym->sym.st_info);
320 sym->bind = GELF_ST_BIND(sym->sym.st_info);
321
322 sym->offset = sym->sym.st_value;
323 sym->len = sym->sym.st_size;
324
325 rb_add(&sym->node, &sym->sec->symbol_tree, symbol_to_offset);
326 pnode = rb_prev(&sym->node);
327 if (pnode)
328 entry = &rb_entry(pnode, struct symbol, node)->list;
329 else
330 entry = &sym->sec->symbol_list;
331 list_add(&sym->list, entry);
332 elf_hash_add(symbol, &sym->hash, sym->idx);
333 elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
334
335 /*
336 * Don't store empty STT_NOTYPE symbols in the rbtree. They
337 * can exist within a function, confusing the sorting.
338 */
339 if (!sym->len)
340 rb_erase(&sym->node, &sym->sec->symbol_tree);
341 }
342
read_symbols(struct elf * elf)343 static int read_symbols(struct elf *elf)
344 {
345 struct section *symtab, *symtab_shndx, *sec;
346 struct symbol *sym, *pfunc;
347 int symbols_nr, i;
348 char *coldstr;
349 Elf_Data *shndx_data = NULL;
350 Elf32_Word shndx;
351
352 symtab = find_section_by_name(elf, ".symtab");
353 if (symtab) {
354 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
355 if (symtab_shndx)
356 shndx_data = symtab_shndx->data;
357
358 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
359 } else {
360 /*
361 * A missing symbol table is actually possible if it's an empty
362 * .o file. This can happen for thunk_64.o. Make sure to at
363 * least allocate the symbol hash tables so we can do symbol
364 * lookups without crashing.
365 */
366 symbols_nr = 0;
367 }
368
369 if (!elf_alloc_hash(symbol, symbols_nr) ||
370 !elf_alloc_hash(symbol_name, symbols_nr))
371 return -1;
372
373 for (i = 0; i < symbols_nr; i++) {
374 sym = malloc(sizeof(*sym));
375 if (!sym) {
376 perror("malloc");
377 return -1;
378 }
379 memset(sym, 0, sizeof(*sym));
380
381 sym->idx = i;
382
383 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
384 &shndx)) {
385 WARN_ELF("gelf_getsymshndx");
386 goto err;
387 }
388
389 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
390 sym->sym.st_name);
391 if (!sym->name) {
392 WARN_ELF("elf_strptr");
393 goto err;
394 }
395
396 if ((sym->sym.st_shndx > SHN_UNDEF &&
397 sym->sym.st_shndx < SHN_LORESERVE) ||
398 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
399 if (sym->sym.st_shndx != SHN_XINDEX)
400 shndx = sym->sym.st_shndx;
401
402 sym->sec = find_section_by_index(elf, shndx);
403 if (!sym->sec) {
404 WARN("couldn't find section for symbol %s",
405 sym->name);
406 goto err;
407 }
408 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
409 sym->name = sym->sec->name;
410 sym->sec->sym = sym;
411 }
412 } else
413 sym->sec = find_section_by_index(elf, 0);
414
415 elf_add_symbol(elf, sym);
416 }
417
418 if (stats) {
419 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
420 printf("symbol_bits: %d\n", elf->symbol_bits);
421 }
422
423 /* Create parent/child links for any cold subfunctions */
424 list_for_each_entry(sec, &elf->sections, list) {
425 list_for_each_entry(sym, &sec->symbol_list, list) {
426 char pname[MAX_NAME_LEN + 1];
427 size_t pnamelen;
428 if (sym->type != STT_FUNC)
429 continue;
430
431 if (sym->pfunc == NULL)
432 sym->pfunc = sym;
433
434 if (sym->cfunc == NULL)
435 sym->cfunc = sym;
436
437 coldstr = strstr(sym->name, ".cold");
438 if (!coldstr)
439 continue;
440
441 pnamelen = coldstr - sym->name;
442 if (pnamelen > MAX_NAME_LEN) {
443 WARN("%s(): parent function name exceeds maximum length of %d characters",
444 sym->name, MAX_NAME_LEN);
445 return -1;
446 }
447
448 strncpy(pname, sym->name, pnamelen);
449 pname[pnamelen] = '\0';
450 pfunc = find_symbol_by_name(elf, pname);
451
452 if (!pfunc) {
453 WARN("%s(): can't find parent function",
454 sym->name);
455 return -1;
456 }
457
458 sym->pfunc = pfunc;
459 pfunc->cfunc = sym;
460
461 /*
462 * Unfortunately, -fnoreorder-functions puts the child
463 * inside the parent. Remove the overlap so we can
464 * have sane assumptions.
465 *
466 * Note that pfunc->len now no longer matches
467 * pfunc->sym.st_size.
468 */
469 if (sym->sec == pfunc->sec &&
470 sym->offset >= pfunc->offset &&
471 sym->offset + sym->len == pfunc->offset + pfunc->len) {
472 pfunc->len -= sym->len;
473 }
474 }
475 }
476
477 return 0;
478
479 err:
480 free(sym);
481 return -1;
482 }
483
484 static struct section *elf_create_reloc_section(struct elf *elf,
485 struct section *base,
486 int reltype);
487
elf_add_reloc(struct elf * elf,struct section * sec,unsigned long offset,unsigned int type,struct symbol * sym,s64 addend)488 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
489 unsigned int type, struct symbol *sym, s64 addend)
490 {
491 struct reloc *reloc;
492
493 if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA))
494 return -1;
495
496 reloc = malloc(sizeof(*reloc));
497 if (!reloc) {
498 perror("malloc");
499 return -1;
500 }
501 memset(reloc, 0, sizeof(*reloc));
502
503 reloc->sec = sec->reloc;
504 reloc->offset = offset;
505 reloc->type = type;
506 reloc->sym = sym;
507 reloc->addend = addend;
508
509 list_add_tail(&reloc->list, &sec->reloc->reloc_list);
510 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
511
512 sec->reloc->sh.sh_size += sec->reloc->sh.sh_entsize;
513 sec->reloc->changed = true;
514
515 return 0;
516 }
517
518 /*
519 * Ensure that any reloc section containing references to @sym is marked
520 * changed such that it will get re-generated in elf_rebuild_reloc_sections()
521 * with the new symbol index.
522 */
elf_dirty_reloc_sym(struct elf * elf,struct symbol * sym)523 static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym)
524 {
525 struct section *sec;
526
527 list_for_each_entry(sec, &elf->sections, list) {
528 struct reloc *reloc;
529
530 if (sec->changed)
531 continue;
532
533 list_for_each_entry(reloc, &sec->reloc_list, list) {
534 if (reloc->sym == sym) {
535 sec->changed = true;
536 break;
537 }
538 }
539 }
540 }
541
542 /*
543 * The libelf API is terrible; gelf_update_sym*() takes a data block relative
544 * index value, *NOT* the symbol index. As such, iterate the data blocks and
545 * adjust index until it fits.
546 *
547 * If no data block is found, allow adding a new data block provided the index
548 * is only one past the end.
549 */
elf_update_symbol(struct elf * elf,struct section * symtab,struct section * symtab_shndx,struct symbol * sym)550 static int elf_update_symbol(struct elf *elf, struct section *symtab,
551 struct section *symtab_shndx, struct symbol *sym)
552 {
553 Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
554 Elf_Data *symtab_data = NULL, *shndx_data = NULL;
555 Elf64_Xword entsize = symtab->sh.sh_entsize;
556 int max_idx, idx = sym->idx;
557 Elf_Scn *s, *t = NULL;
558 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
559 sym->sym.st_shndx != SHN_XINDEX;
560
561 if (is_special_shndx)
562 shndx = sym->sym.st_shndx;
563
564 s = elf_getscn(elf->elf, symtab->idx);
565 if (!s) {
566 WARN_ELF("elf_getscn");
567 return -1;
568 }
569
570 if (symtab_shndx) {
571 t = elf_getscn(elf->elf, symtab_shndx->idx);
572 if (!t) {
573 WARN_ELF("elf_getscn");
574 return -1;
575 }
576 }
577
578 for (;;) {
579 /* get next data descriptor for the relevant sections */
580 symtab_data = elf_getdata(s, symtab_data);
581 if (t)
582 shndx_data = elf_getdata(t, shndx_data);
583
584 /* end-of-list */
585 if (!symtab_data) {
586 void *buf;
587
588 if (idx) {
589 /* we don't do holes in symbol tables */
590 WARN("index out of range");
591 return -1;
592 }
593
594 /* if @idx == 0, it's the next contiguous entry, create it */
595 symtab_data = elf_newdata(s);
596 if (t)
597 shndx_data = elf_newdata(t);
598
599 buf = calloc(1, entsize);
600 if (!buf) {
601 WARN("malloc");
602 return -1;
603 }
604
605 symtab_data->d_buf = buf;
606 symtab_data->d_size = entsize;
607 symtab_data->d_align = 1;
608 symtab_data->d_type = ELF_T_SYM;
609
610 symtab->sh.sh_size += entsize;
611 symtab->changed = true;
612
613 if (t) {
614 shndx_data->d_buf = &sym->sec->idx;
615 shndx_data->d_size = sizeof(Elf32_Word);
616 shndx_data->d_align = sizeof(Elf32_Word);
617 shndx_data->d_type = ELF_T_WORD;
618
619 symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
620 symtab_shndx->changed = true;
621 }
622
623 break;
624 }
625
626 /* empty blocks should not happen */
627 if (!symtab_data->d_size) {
628 WARN("zero size data");
629 return -1;
630 }
631
632 /* is this the right block? */
633 max_idx = symtab_data->d_size / entsize;
634 if (idx < max_idx)
635 break;
636
637 /* adjust index and try again */
638 idx -= max_idx;
639 }
640
641 /* something went side-ways */
642 if (idx < 0) {
643 WARN("negative index");
644 return -1;
645 }
646
647 /* setup extended section index magic and write the symbol */
648 if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) {
649 sym->sym.st_shndx = shndx;
650 if (!shndx_data)
651 shndx = 0;
652 } else {
653 sym->sym.st_shndx = SHN_XINDEX;
654 if (!shndx_data) {
655 WARN("no .symtab_shndx");
656 return -1;
657 }
658 }
659
660 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
661 WARN_ELF("gelf_update_symshndx");
662 return -1;
663 }
664
665 return 0;
666 }
667
668 static struct symbol *
elf_create_section_symbol(struct elf * elf,struct section * sec)669 elf_create_section_symbol(struct elf *elf, struct section *sec)
670 {
671 struct section *symtab, *symtab_shndx;
672 Elf32_Word first_non_local, new_idx;
673 struct symbol *sym, *old;
674
675 symtab = find_section_by_name(elf, ".symtab");
676 if (symtab) {
677 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
678 } else {
679 WARN("no .symtab");
680 return NULL;
681 }
682
683 sym = calloc(1, sizeof(*sym));
684 if (!sym) {
685 perror("malloc");
686 return NULL;
687 }
688
689 sym->name = sec->name;
690 sym->sec = sec;
691
692 // st_name 0
693 sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
694 // st_other 0
695 // st_value 0
696 // st_size 0
697
698 /*
699 * Move the first global symbol, as per sh_info, into a new, higher
700 * symbol index. This fees up a spot for a new local symbol.
701 */
702 first_non_local = symtab->sh.sh_info;
703 new_idx = symtab->sh.sh_size / symtab->sh.sh_entsize;
704 old = find_symbol_by_index(elf, first_non_local);
705 if (old) {
706 old->idx = new_idx;
707
708 hlist_del(&old->hash);
709 elf_hash_add(symbol, &old->hash, old->idx);
710
711 elf_dirty_reloc_sym(elf, old);
712
713 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
714 WARN("elf_update_symbol move");
715 return NULL;
716 }
717
718 new_idx = first_non_local;
719 }
720
721 sym->idx = new_idx;
722 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
723 WARN("elf_update_symbol");
724 return NULL;
725 }
726
727 /*
728 * Either way, we added a LOCAL symbol.
729 */
730 symtab->sh.sh_info += 1;
731
732 elf_add_symbol(elf, sym);
733
734 return sym;
735 }
736
elf_add_reloc_to_insn(struct elf * elf,struct section * sec,unsigned long offset,unsigned int type,struct section * insn_sec,unsigned long insn_off)737 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
738 unsigned long offset, unsigned int type,
739 struct section *insn_sec, unsigned long insn_off)
740 {
741 struct symbol *sym = insn_sec->sym;
742 int addend = insn_off;
743
744 if (!sym) {
745 /*
746 * Due to how weak functions work, we must use section based
747 * relocations. Symbol based relocations would result in the
748 * weak and non-weak function annotations being overlaid on the
749 * non-weak function after linking.
750 */
751 sym = elf_create_section_symbol(elf, insn_sec);
752 if (!sym)
753 return -1;
754
755 insn_sec->sym = sym;
756 }
757
758 return elf_add_reloc(elf, sec, offset, type, sym, addend);
759 }
760
read_rel_reloc(struct section * sec,int i,struct reloc * reloc,unsigned int * symndx)761 static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
762 {
763 if (!gelf_getrel(sec->data, i, &reloc->rel)) {
764 WARN_ELF("gelf_getrel");
765 return -1;
766 }
767 reloc->type = GELF_R_TYPE(reloc->rel.r_info);
768 reloc->addend = 0;
769 reloc->offset = reloc->rel.r_offset;
770 *symndx = GELF_R_SYM(reloc->rel.r_info);
771 return 0;
772 }
773
read_rela_reloc(struct section * sec,int i,struct reloc * reloc,unsigned int * symndx)774 static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
775 {
776 if (!gelf_getrela(sec->data, i, &reloc->rela)) {
777 WARN_ELF("gelf_getrela");
778 return -1;
779 }
780 reloc->type = GELF_R_TYPE(reloc->rela.r_info);
781 reloc->addend = reloc->rela.r_addend;
782 reloc->offset = reloc->rela.r_offset;
783 *symndx = GELF_R_SYM(reloc->rela.r_info);
784 return 0;
785 }
786
read_relocs(struct elf * elf)787 static int read_relocs(struct elf *elf)
788 {
789 struct section *sec;
790 struct reloc *reloc;
791 int i;
792 unsigned int symndx;
793 unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
794
795 if (!elf_alloc_hash(reloc, elf->text_size / 16))
796 return -1;
797
798 list_for_each_entry(sec, &elf->sections, list) {
799 if ((sec->sh.sh_type != SHT_RELA) &&
800 (sec->sh.sh_type != SHT_REL))
801 continue;
802
803 sec->base = find_section_by_index(elf, sec->sh.sh_info);
804 if (!sec->base) {
805 WARN("can't find base section for reloc section %s",
806 sec->name);
807 return -1;
808 }
809
810 sec->base->reloc = sec;
811
812 nr_reloc = 0;
813 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
814 reloc = malloc(sizeof(*reloc));
815 if (!reloc) {
816 perror("malloc");
817 return -1;
818 }
819 memset(reloc, 0, sizeof(*reloc));
820 switch (sec->sh.sh_type) {
821 case SHT_REL:
822 if (read_rel_reloc(sec, i, reloc, &symndx))
823 return -1;
824 break;
825 case SHT_RELA:
826 if (read_rela_reloc(sec, i, reloc, &symndx))
827 return -1;
828 break;
829 default: return -1;
830 }
831
832 reloc->sec = sec;
833 reloc->idx = i;
834 reloc->sym = find_symbol_by_index(elf, symndx);
835 if (!reloc->sym) {
836 WARN("can't find reloc entry symbol %d for %s",
837 symndx, sec->name);
838 return -1;
839 }
840
841 list_add_tail(&reloc->list, &sec->reloc_list);
842 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
843
844 nr_reloc++;
845 }
846 max_reloc = max(max_reloc, nr_reloc);
847 tot_reloc += nr_reloc;
848 }
849
850 if (stats) {
851 printf("max_reloc: %lu\n", max_reloc);
852 printf("tot_reloc: %lu\n", tot_reloc);
853 printf("reloc_bits: %d\n", elf->reloc_bits);
854 }
855
856 return 0;
857 }
858
elf_open_read(const char * name,int flags)859 struct elf *elf_open_read(const char *name, int flags)
860 {
861 struct elf *elf;
862 Elf_Cmd cmd;
863
864 elf_version(EV_CURRENT);
865
866 elf = malloc(sizeof(*elf));
867 if (!elf) {
868 perror("malloc");
869 return NULL;
870 }
871 memset(elf, 0, offsetof(struct elf, sections));
872
873 INIT_LIST_HEAD(&elf->sections);
874
875 elf->fd = open(name, flags);
876 if (elf->fd == -1) {
877 fprintf(stderr, "objtool: Can't open '%s': %s\n",
878 name, strerror(errno));
879 goto err;
880 }
881
882 if ((flags & O_ACCMODE) == O_RDONLY)
883 cmd = ELF_C_READ_MMAP;
884 else if ((flags & O_ACCMODE) == O_RDWR)
885 cmd = ELF_C_RDWR;
886 else /* O_WRONLY */
887 cmd = ELF_C_WRITE;
888
889 elf->elf = elf_begin(elf->fd, cmd, NULL);
890 if (!elf->elf) {
891 WARN_ELF("elf_begin");
892 goto err;
893 }
894
895 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
896 WARN_ELF("gelf_getehdr");
897 goto err;
898 }
899
900 if (read_sections(elf))
901 goto err;
902
903 if (read_symbols(elf))
904 goto err;
905
906 if (read_relocs(elf))
907 goto err;
908
909 return elf;
910
911 err:
912 elf_close(elf);
913 return NULL;
914 }
915
elf_add_string(struct elf * elf,struct section * strtab,char * str)916 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
917 {
918 Elf_Data *data;
919 Elf_Scn *s;
920 int len;
921
922 if (!strtab)
923 strtab = find_section_by_name(elf, ".strtab");
924 if (!strtab) {
925 WARN("can't find .strtab section");
926 return -1;
927 }
928
929 s = elf_getscn(elf->elf, strtab->idx);
930 if (!s) {
931 WARN_ELF("elf_getscn");
932 return -1;
933 }
934
935 data = elf_newdata(s);
936 if (!data) {
937 WARN_ELF("elf_newdata");
938 return -1;
939 }
940
941 data->d_buf = str;
942 data->d_size = strlen(str) + 1;
943 data->d_align = 1;
944
945 len = strtab->sh.sh_size;
946 strtab->sh.sh_size += data->d_size;
947 strtab->changed = true;
948
949 return len;
950 }
951
elf_create_section(struct elf * elf,const char * name,unsigned int sh_flags,size_t entsize,int nr)952 struct section *elf_create_section(struct elf *elf, const char *name,
953 unsigned int sh_flags, size_t entsize, int nr)
954 {
955 struct section *sec, *shstrtab;
956 size_t size = entsize * nr;
957 Elf_Scn *s;
958
959 sec = malloc(sizeof(*sec));
960 if (!sec) {
961 perror("malloc");
962 return NULL;
963 }
964 memset(sec, 0, sizeof(*sec));
965
966 INIT_LIST_HEAD(&sec->symbol_list);
967 INIT_LIST_HEAD(&sec->reloc_list);
968
969 s = elf_newscn(elf->elf);
970 if (!s) {
971 WARN_ELF("elf_newscn");
972 return NULL;
973 }
974
975 sec->name = strdup(name);
976 if (!sec->name) {
977 perror("strdup");
978 return NULL;
979 }
980
981 sec->idx = elf_ndxscn(s);
982 sec->changed = true;
983
984 sec->data = elf_newdata(s);
985 if (!sec->data) {
986 WARN_ELF("elf_newdata");
987 return NULL;
988 }
989
990 sec->data->d_size = size;
991 sec->data->d_align = 1;
992
993 if (size) {
994 sec->data->d_buf = malloc(size);
995 if (!sec->data->d_buf) {
996 perror("malloc");
997 return NULL;
998 }
999 memset(sec->data->d_buf, 0, size);
1000 }
1001
1002 if (!gelf_getshdr(s, &sec->sh)) {
1003 WARN_ELF("gelf_getshdr");
1004 return NULL;
1005 }
1006
1007 sec->sh.sh_size = size;
1008 sec->sh.sh_entsize = entsize;
1009 sec->sh.sh_type = SHT_PROGBITS;
1010 sec->sh.sh_addralign = 1;
1011 sec->sh.sh_flags = SHF_ALLOC | sh_flags;
1012
1013 /* Add section name to .shstrtab (or .strtab for Clang) */
1014 shstrtab = find_section_by_name(elf, ".shstrtab");
1015 if (!shstrtab)
1016 shstrtab = find_section_by_name(elf, ".strtab");
1017 if (!shstrtab) {
1018 WARN("can't find .shstrtab or .strtab section");
1019 return NULL;
1020 }
1021 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1022 if (sec->sh.sh_name == -1)
1023 return NULL;
1024
1025 list_add_tail(&sec->list, &elf->sections);
1026 elf_hash_add(section, &sec->hash, sec->idx);
1027 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
1028
1029 elf->changed = true;
1030
1031 return sec;
1032 }
1033
elf_create_rel_reloc_section(struct elf * elf,struct section * base)1034 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
1035 {
1036 char *relocname;
1037 struct section *sec;
1038
1039 relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
1040 if (!relocname) {
1041 perror("malloc");
1042 return NULL;
1043 }
1044 strcpy(relocname, ".rel");
1045 strcat(relocname, base->name);
1046
1047 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0);
1048 free(relocname);
1049 if (!sec)
1050 return NULL;
1051
1052 base->reloc = sec;
1053 sec->base = base;
1054
1055 sec->sh.sh_type = SHT_REL;
1056 sec->sh.sh_addralign = 8;
1057 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1058 sec->sh.sh_info = base->idx;
1059 sec->sh.sh_flags = SHF_INFO_LINK;
1060
1061 return sec;
1062 }
1063
elf_create_rela_reloc_section(struct elf * elf,struct section * base)1064 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
1065 {
1066 char *relocname;
1067 struct section *sec;
1068
1069 relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
1070 if (!relocname) {
1071 perror("malloc");
1072 return NULL;
1073 }
1074 strcpy(relocname, ".rela");
1075 strcat(relocname, base->name);
1076
1077 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
1078 free(relocname);
1079 if (!sec)
1080 return NULL;
1081
1082 base->reloc = sec;
1083 sec->base = base;
1084
1085 sec->sh.sh_type = SHT_RELA;
1086 sec->sh.sh_addralign = 8;
1087 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1088 sec->sh.sh_info = base->idx;
1089 sec->sh.sh_flags = SHF_INFO_LINK;
1090
1091 return sec;
1092 }
1093
elf_create_reloc_section(struct elf * elf,struct section * base,int reltype)1094 static struct section *elf_create_reloc_section(struct elf *elf,
1095 struct section *base,
1096 int reltype)
1097 {
1098 switch (reltype) {
1099 case SHT_REL: return elf_create_rel_reloc_section(elf, base);
1100 case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
1101 default: return NULL;
1102 }
1103 }
1104
elf_rebuild_rel_reloc_section(struct section * sec)1105 static int elf_rebuild_rel_reloc_section(struct section *sec)
1106 {
1107 struct reloc *reloc;
1108 int idx = 0;
1109 void *buf;
1110
1111 /* Allocate a buffer for relocations */
1112 buf = malloc(sec->sh.sh_size);
1113 if (!buf) {
1114 perror("malloc");
1115 return -1;
1116 }
1117
1118 sec->data->d_buf = buf;
1119 sec->data->d_size = sec->sh.sh_size;
1120 sec->data->d_type = ELF_T_REL;
1121
1122 idx = 0;
1123 list_for_each_entry(reloc, &sec->reloc_list, list) {
1124 reloc->rel.r_offset = reloc->offset;
1125 reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1126 if (!gelf_update_rel(sec->data, idx, &reloc->rel)) {
1127 WARN_ELF("gelf_update_rel");
1128 return -1;
1129 }
1130 idx++;
1131 }
1132
1133 return 0;
1134 }
1135
elf_rebuild_rela_reloc_section(struct section * sec)1136 static int elf_rebuild_rela_reloc_section(struct section *sec)
1137 {
1138 struct reloc *reloc;
1139 int idx = 0;
1140 void *buf;
1141
1142 /* Allocate a buffer for relocations with addends */
1143 buf = malloc(sec->sh.sh_size);
1144 if (!buf) {
1145 perror("malloc");
1146 return -1;
1147 }
1148
1149 sec->data->d_buf = buf;
1150 sec->data->d_size = sec->sh.sh_size;
1151 sec->data->d_type = ELF_T_RELA;
1152
1153 idx = 0;
1154 list_for_each_entry(reloc, &sec->reloc_list, list) {
1155 reloc->rela.r_offset = reloc->offset;
1156 reloc->rela.r_addend = reloc->addend;
1157 reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1158 if (!gelf_update_rela(sec->data, idx, &reloc->rela)) {
1159 WARN_ELF("gelf_update_rela");
1160 return -1;
1161 }
1162 idx++;
1163 }
1164
1165 return 0;
1166 }
1167
elf_rebuild_reloc_section(struct elf * elf,struct section * sec)1168 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
1169 {
1170 switch (sec->sh.sh_type) {
1171 case SHT_REL: return elf_rebuild_rel_reloc_section(sec);
1172 case SHT_RELA: return elf_rebuild_rela_reloc_section(sec);
1173 default: return -1;
1174 }
1175 }
1176
elf_write_insn(struct elf * elf,struct section * sec,unsigned long offset,unsigned int len,const char * insn)1177 int elf_write_insn(struct elf *elf, struct section *sec,
1178 unsigned long offset, unsigned int len,
1179 const char *insn)
1180 {
1181 Elf_Data *data = sec->data;
1182
1183 if (data->d_type != ELF_T_BYTE || data->d_off) {
1184 WARN("write to unexpected data for section: %s", sec->name);
1185 return -1;
1186 }
1187
1188 memcpy(data->d_buf + offset, insn, len);
1189 elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
1190
1191 elf->changed = true;
1192
1193 return 0;
1194 }
1195
elf_write_reloc(struct elf * elf,struct reloc * reloc)1196 int elf_write_reloc(struct elf *elf, struct reloc *reloc)
1197 {
1198 struct section *sec = reloc->sec;
1199
1200 if (sec->sh.sh_type == SHT_REL) {
1201 reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1202 reloc->rel.r_offset = reloc->offset;
1203
1204 if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
1205 WARN_ELF("gelf_update_rel");
1206 return -1;
1207 }
1208 } else {
1209 reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1210 reloc->rela.r_addend = reloc->addend;
1211 reloc->rela.r_offset = reloc->offset;
1212
1213 if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
1214 WARN_ELF("gelf_update_rela");
1215 return -1;
1216 }
1217 }
1218
1219 elf->changed = true;
1220
1221 return 0;
1222 }
1223
elf_write(struct elf * elf)1224 int elf_write(struct elf *elf)
1225 {
1226 struct section *sec;
1227 Elf_Scn *s;
1228
1229 /* Update changed relocation sections and section headers: */
1230 list_for_each_entry(sec, &elf->sections, list) {
1231 if (sec->changed) {
1232 s = elf_getscn(elf->elf, sec->idx);
1233 if (!s) {
1234 WARN_ELF("elf_getscn");
1235 return -1;
1236 }
1237 if (!gelf_update_shdr(s, &sec->sh)) {
1238 WARN_ELF("gelf_update_shdr");
1239 return -1;
1240 }
1241
1242 if (sec->base &&
1243 elf_rebuild_reloc_section(elf, sec)) {
1244 WARN("elf_rebuild_reloc_section");
1245 return -1;
1246 }
1247
1248 sec->changed = false;
1249 elf->changed = true;
1250 }
1251 }
1252
1253 /* Make sure the new section header entries get updated properly. */
1254 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1255
1256 /* Write all changes to the file. */
1257 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1258 WARN_ELF("elf_update");
1259 return -1;
1260 }
1261
1262 elf->changed = false;
1263
1264 return 0;
1265 }
1266
elf_close(struct elf * elf)1267 void elf_close(struct elf *elf)
1268 {
1269 struct section *sec, *tmpsec;
1270 struct symbol *sym, *tmpsym;
1271 struct reloc *reloc, *tmpreloc;
1272
1273 if (elf->elf)
1274 elf_end(elf->elf);
1275
1276 if (elf->fd > 0)
1277 close(elf->fd);
1278
1279 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
1280 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
1281 list_del(&sym->list);
1282 hash_del(&sym->hash);
1283 free(sym);
1284 }
1285 list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
1286 list_del(&reloc->list);
1287 hash_del(&reloc->hash);
1288 free(reloc);
1289 }
1290 list_del(&sec->list);
1291 free(sec);
1292 }
1293
1294 free(elf);
1295 }
1296