GNU Linux-libre 4.14.257-gnu1
[releases.git] / tools / objtool / elf.c
1 /*
2  * elf.c - ELF access library
3  *
4  * Adapted from kpatch (https://github.com/dynup/kpatch):
5  * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
6  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "elf.h"
32 #include "warn.h"
33
34 #define MAX_NAME_LEN 128
35
36 struct section *find_section_by_name(struct elf *elf, const char *name)
37 {
38         struct section *sec;
39
40         list_for_each_entry(sec, &elf->sections, list)
41                 if (!strcmp(sec->name, name))
42                         return sec;
43
44         return NULL;
45 }
46
47 static struct section *find_section_by_index(struct elf *elf,
48                                              unsigned int idx)
49 {
50         struct section *sec;
51
52         list_for_each_entry(sec, &elf->sections, list)
53                 if (sec->idx == idx)
54                         return sec;
55
56         return NULL;
57 }
58
59 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
60 {
61         struct section *sec;
62         struct symbol *sym;
63
64         list_for_each_entry(sec, &elf->sections, list)
65                 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
66                         if (sym->idx == idx)
67                                 return sym;
68
69         return NULL;
70 }
71
72 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
73 {
74         struct symbol *sym;
75
76         list_for_each_entry(sym, &sec->symbol_list, list)
77                 if (sym->type != STT_SECTION &&
78                     sym->offset == offset)
79                         return sym;
80
81         return NULL;
82 }
83
84 struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
85 {
86         struct section *sec;
87         struct symbol *sym;
88
89         list_for_each_entry(sec, &elf->sections, list)
90                 list_for_each_entry(sym, &sec->symbol_list, list)
91                         if (!strcmp(sym->name, name))
92                                 return sym;
93
94         return NULL;
95 }
96
97 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
98 {
99         struct symbol *sym;
100
101         list_for_each_entry(sym, &sec->symbol_list, list)
102                 if (sym->type != STT_SECTION &&
103                     offset >= sym->offset && offset < sym->offset + sym->len)
104                         return sym;
105
106         return NULL;
107 }
108
109 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
110                                      unsigned int len)
111 {
112         struct rela *rela;
113         unsigned long o;
114
115         if (!sec->rela)
116                 return NULL;
117
118         for (o = offset; o < offset + len; o++)
119                 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
120                         if (rela->offset == o)
121                                 return rela;
122
123         return NULL;
124 }
125
126 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
127 {
128         return find_rela_by_dest_range(sec, offset, 1);
129 }
130
131 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
132 {
133         struct symbol *func;
134
135         list_for_each_entry(func, &sec->symbol_list, list)
136                 if (func->type == STT_FUNC && offset >= func->offset &&
137                     offset < func->offset + func->len)
138                         return func;
139
140         return NULL;
141 }
142
143 static int read_sections(struct elf *elf)
144 {
145         Elf_Scn *s = NULL;
146         struct section *sec;
147         size_t shstrndx, sections_nr;
148         int i;
149
150         if (elf_getshdrnum(elf->elf, &sections_nr)) {
151                 WARN_ELF("elf_getshdrnum");
152                 return -1;
153         }
154
155         if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
156                 WARN_ELF("elf_getshdrstrndx");
157                 return -1;
158         }
159
160         for (i = 0; i < sections_nr; i++) {
161                 sec = malloc(sizeof(*sec));
162                 if (!sec) {
163                         perror("malloc");
164                         return -1;
165                 }
166                 memset(sec, 0, sizeof(*sec));
167
168                 INIT_LIST_HEAD(&sec->symbol_list);
169                 INIT_LIST_HEAD(&sec->rela_list);
170                 hash_init(sec->rela_hash);
171                 hash_init(sec->symbol_hash);
172
173                 list_add_tail(&sec->list, &elf->sections);
174
175                 s = elf_getscn(elf->elf, i);
176                 if (!s) {
177                         WARN_ELF("elf_getscn");
178                         return -1;
179                 }
180
181                 sec->idx = elf_ndxscn(s);
182
183                 if (!gelf_getshdr(s, &sec->sh)) {
184                         WARN_ELF("gelf_getshdr");
185                         return -1;
186                 }
187
188                 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
189                 if (!sec->name) {
190                         WARN_ELF("elf_strptr");
191                         return -1;
192                 }
193
194                 if (sec->sh.sh_size != 0) {
195                         sec->data = elf_getdata(s, NULL);
196                         if (!sec->data) {
197                                 WARN_ELF("elf_getdata");
198                                 return -1;
199                         }
200                         if (sec->data->d_off != 0 ||
201                             sec->data->d_size != sec->sh.sh_size) {
202                                 WARN("unexpected data attributes for %s",
203                                      sec->name);
204                                 return -1;
205                         }
206                 }
207                 sec->len = sec->sh.sh_size;
208         }
209
210         /* sanity check, one more call to elf_nextscn() should return NULL */
211         if (elf_nextscn(elf->elf, s)) {
212                 WARN("section entry mismatch");
213                 return -1;
214         }
215
216         return 0;
217 }
218
219 static int read_symbols(struct elf *elf)
220 {
221         struct section *symtab, *sec;
222         struct symbol *sym, *pfunc;
223         struct list_head *entry, *tmp;
224         int symbols_nr, i;
225         char *coldstr;
226
227         symtab = find_section_by_name(elf, ".symtab");
228         if (!symtab) {
229                 /*
230                  * A missing symbol table is actually possible if it's an empty
231                  * .o file.  This can happen for thunk_64.o.
232                  */
233                 return 0;
234         }
235
236         symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
237
238         for (i = 0; i < symbols_nr; i++) {
239                 sym = malloc(sizeof(*sym));
240                 if (!sym) {
241                         perror("malloc");
242                         return -1;
243                 }
244                 memset(sym, 0, sizeof(*sym));
245
246                 sym->idx = i;
247
248                 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
249                         WARN_ELF("gelf_getsym");
250                         goto err;
251                 }
252
253                 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
254                                        sym->sym.st_name);
255                 if (!sym->name) {
256                         WARN_ELF("elf_strptr");
257                         goto err;
258                 }
259
260                 sym->type = GELF_ST_TYPE(sym->sym.st_info);
261                 sym->bind = GELF_ST_BIND(sym->sym.st_info);
262
263                 if (sym->sym.st_shndx > SHN_UNDEF &&
264                     sym->sym.st_shndx < SHN_LORESERVE) {
265                         sym->sec = find_section_by_index(elf,
266                                                          sym->sym.st_shndx);
267                         if (!sym->sec) {
268                                 WARN("couldn't find section for symbol %s",
269                                      sym->name);
270                                 goto err;
271                         }
272                         if (sym->type == STT_SECTION) {
273                                 sym->name = sym->sec->name;
274                                 sym->sec->sym = sym;
275                         }
276                 } else
277                         sym->sec = find_section_by_index(elf, 0);
278
279                 sym->offset = sym->sym.st_value;
280                 sym->len = sym->sym.st_size;
281
282                 /* sorted insert into a per-section list */
283                 entry = &sym->sec->symbol_list;
284                 list_for_each_prev(tmp, &sym->sec->symbol_list) {
285                         struct symbol *s;
286
287                         s = list_entry(tmp, struct symbol, list);
288
289                         if (sym->offset > s->offset) {
290                                 entry = tmp;
291                                 break;
292                         }
293
294                         if (sym->offset == s->offset && sym->len >= s->len) {
295                                 entry = tmp;
296                                 break;
297                         }
298                 }
299                 list_add(&sym->list, entry);
300                 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
301         }
302
303         /* Create parent/child links for any cold subfunctions */
304         list_for_each_entry(sec, &elf->sections, list) {
305                 list_for_each_entry(sym, &sec->symbol_list, list) {
306                         char pname[MAX_NAME_LEN + 1];
307                         size_t pnamelen;
308                         if (sym->type != STT_FUNC)
309                                 continue;
310                         sym->pfunc = sym->cfunc = sym;
311                         coldstr = strstr(sym->name, ".cold");
312                         if (!coldstr)
313                                 continue;
314
315                         pnamelen = coldstr - sym->name;
316                         if (pnamelen > MAX_NAME_LEN) {
317                                 WARN("%s(): parent function name exceeds maximum length of %d characters",
318                                      sym->name, MAX_NAME_LEN);
319                                 return -1;
320                         }
321
322                         strncpy(pname, sym->name, pnamelen);
323                         pname[pnamelen] = '\0';
324                         pfunc = find_symbol_by_name(elf, pname);
325
326                         if (!pfunc) {
327                                 WARN("%s(): can't find parent function",
328                                      sym->name);
329                                 return -1;
330                         }
331
332                         sym->pfunc = pfunc;
333                         pfunc->cfunc = sym;
334
335                         /*
336                          * Unfortunately, -fnoreorder-functions puts the child
337                          * inside the parent.  Remove the overlap so we can
338                          * have sane assumptions.
339                          *
340                          * Note that pfunc->len now no longer matches
341                          * pfunc->sym.st_size.
342                          */
343                         if (sym->sec == pfunc->sec &&
344                             sym->offset >= pfunc->offset &&
345                             sym->offset + sym->len == pfunc->offset + pfunc->len) {
346                                 pfunc->len -= sym->len;
347                         }
348                 }
349         }
350
351         return 0;
352
353 err:
354         free(sym);
355         return -1;
356 }
357
358 static int read_relas(struct elf *elf)
359 {
360         struct section *sec;
361         struct rela *rela;
362         int i;
363         unsigned int symndx;
364
365         list_for_each_entry(sec, &elf->sections, list) {
366                 if (sec->sh.sh_type != SHT_RELA)
367                         continue;
368
369                 sec->base = find_section_by_name(elf, sec->name + 5);
370                 if (!sec->base) {
371                         WARN("can't find base section for rela section %s",
372                              sec->name);
373                         return -1;
374                 }
375
376                 sec->base->rela = sec;
377
378                 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
379                         rela = malloc(sizeof(*rela));
380                         if (!rela) {
381                                 perror("malloc");
382                                 return -1;
383                         }
384                         memset(rela, 0, sizeof(*rela));
385
386                         if (!gelf_getrela(sec->data, i, &rela->rela)) {
387                                 WARN_ELF("gelf_getrela");
388                                 return -1;
389                         }
390
391                         rela->type = GELF_R_TYPE(rela->rela.r_info);
392                         rela->addend = rela->rela.r_addend;
393                         rela->offset = rela->rela.r_offset;
394                         symndx = GELF_R_SYM(rela->rela.r_info);
395                         rela->sym = find_symbol_by_index(elf, symndx);
396                         rela->rela_sec = sec;
397                         if (!rela->sym) {
398                                 WARN("can't find rela entry symbol %d for %s",
399                                      symndx, sec->name);
400                                 return -1;
401                         }
402
403                         list_add_tail(&rela->list, &sec->rela_list);
404                         hash_add(sec->rela_hash, &rela->hash, rela->offset);
405
406                 }
407         }
408
409         return 0;
410 }
411
412 struct elf *elf_open(const char *name, int flags)
413 {
414         struct elf *elf;
415         Elf_Cmd cmd;
416
417         elf_version(EV_CURRENT);
418
419         elf = malloc(sizeof(*elf));
420         if (!elf) {
421                 perror("malloc");
422                 return NULL;
423         }
424         memset(elf, 0, sizeof(*elf));
425
426         INIT_LIST_HEAD(&elf->sections);
427
428         elf->fd = open(name, flags);
429         if (elf->fd == -1) {
430                 fprintf(stderr, "objtool: Can't open '%s': %s\n",
431                         name, strerror(errno));
432                 goto err;
433         }
434
435         if ((flags & O_ACCMODE) == O_RDONLY)
436                 cmd = ELF_C_READ_MMAP;
437         else if ((flags & O_ACCMODE) == O_RDWR)
438                 cmd = ELF_C_RDWR;
439         else /* O_WRONLY */
440                 cmd = ELF_C_WRITE;
441
442         elf->elf = elf_begin(elf->fd, cmd, NULL);
443         if (!elf->elf) {
444                 WARN_ELF("elf_begin");
445                 goto err;
446         }
447
448         if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
449                 WARN_ELF("gelf_getehdr");
450                 goto err;
451         }
452
453         if (read_sections(elf))
454                 goto err;
455
456         if (read_symbols(elf))
457                 goto err;
458
459         if (read_relas(elf))
460                 goto err;
461
462         return elf;
463
464 err:
465         elf_close(elf);
466         return NULL;
467 }
468
469 struct section *elf_create_section(struct elf *elf, const char *name,
470                                    size_t entsize, int nr)
471 {
472         struct section *sec, *shstrtab;
473         size_t size = entsize * nr;
474         struct Elf_Scn *s;
475         Elf_Data *data;
476
477         sec = malloc(sizeof(*sec));
478         if (!sec) {
479                 perror("malloc");
480                 return NULL;
481         }
482         memset(sec, 0, sizeof(*sec));
483
484         INIT_LIST_HEAD(&sec->symbol_list);
485         INIT_LIST_HEAD(&sec->rela_list);
486         hash_init(sec->rela_hash);
487         hash_init(sec->symbol_hash);
488
489         list_add_tail(&sec->list, &elf->sections);
490
491         s = elf_newscn(elf->elf);
492         if (!s) {
493                 WARN_ELF("elf_newscn");
494                 return NULL;
495         }
496
497         sec->name = strdup(name);
498         if (!sec->name) {
499                 perror("strdup");
500                 return NULL;
501         }
502
503         sec->idx = elf_ndxscn(s);
504         sec->len = size;
505         sec->changed = true;
506
507         sec->data = elf_newdata(s);
508         if (!sec->data) {
509                 WARN_ELF("elf_newdata");
510                 return NULL;
511         }
512
513         sec->data->d_size = size;
514         sec->data->d_align = 1;
515
516         if (size) {
517                 sec->data->d_buf = malloc(size);
518                 if (!sec->data->d_buf) {
519                         perror("malloc");
520                         return NULL;
521                 }
522                 memset(sec->data->d_buf, 0, size);
523         }
524
525         if (!gelf_getshdr(s, &sec->sh)) {
526                 WARN_ELF("gelf_getshdr");
527                 return NULL;
528         }
529
530         sec->sh.sh_size = size;
531         sec->sh.sh_entsize = entsize;
532         sec->sh.sh_type = SHT_PROGBITS;
533         sec->sh.sh_addralign = 1;
534         sec->sh.sh_flags = SHF_ALLOC;
535
536
537         /* Add section name to .shstrtab */
538         shstrtab = find_section_by_name(elf, ".shstrtab");
539         if (!shstrtab) {
540                 WARN("can't find .shstrtab section");
541                 return NULL;
542         }
543
544         s = elf_getscn(elf->elf, shstrtab->idx);
545         if (!s) {
546                 WARN_ELF("elf_getscn");
547                 return NULL;
548         }
549
550         data = elf_newdata(s);
551         if (!data) {
552                 WARN_ELF("elf_newdata");
553                 return NULL;
554         }
555
556         data->d_buf = sec->name;
557         data->d_size = strlen(name) + 1;
558         data->d_align = 1;
559
560         sec->sh.sh_name = shstrtab->len;
561
562         shstrtab->len += strlen(name) + 1;
563         shstrtab->changed = true;
564
565         return sec;
566 }
567
568 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
569 {
570         char *relaname;
571         struct section *sec;
572
573         relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
574         if (!relaname) {
575                 perror("malloc");
576                 return NULL;
577         }
578         strcpy(relaname, ".rela");
579         strcat(relaname, base->name);
580
581         sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
582         free(relaname);
583         if (!sec)
584                 return NULL;
585
586         base->rela = sec;
587         sec->base = base;
588
589         sec->sh.sh_type = SHT_RELA;
590         sec->sh.sh_addralign = 8;
591         sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
592         sec->sh.sh_info = base->idx;
593         sec->sh.sh_flags = SHF_INFO_LINK;
594
595         return sec;
596 }
597
598 int elf_rebuild_rela_section(struct section *sec)
599 {
600         struct rela *rela;
601         int nr, idx = 0, size;
602         GElf_Rela *relas;
603
604         nr = 0;
605         list_for_each_entry(rela, &sec->rela_list, list)
606                 nr++;
607
608         size = nr * sizeof(*relas);
609         relas = malloc(size);
610         if (!relas) {
611                 perror("malloc");
612                 return -1;
613         }
614
615         sec->data->d_buf = relas;
616         sec->data->d_size = size;
617
618         sec->sh.sh_size = size;
619
620         idx = 0;
621         list_for_each_entry(rela, &sec->rela_list, list) {
622                 relas[idx].r_offset = rela->offset;
623                 relas[idx].r_addend = rela->addend;
624                 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
625                 idx++;
626         }
627
628         return 0;
629 }
630
631 int elf_write(struct elf *elf)
632 {
633         struct section *sec;
634         Elf_Scn *s;
635
636         /* Update section headers for changed sections: */
637         list_for_each_entry(sec, &elf->sections, list) {
638                 if (sec->changed) {
639                         s = elf_getscn(elf->elf, sec->idx);
640                         if (!s) {
641                                 WARN_ELF("elf_getscn");
642                                 return -1;
643                         }
644                         if (!gelf_update_shdr(s, &sec->sh)) {
645                                 WARN_ELF("gelf_update_shdr");
646                                 return -1;
647                         }
648                 }
649         }
650
651         /* Make sure the new section header entries get updated properly. */
652         elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
653
654         /* Write all changes to the file. */
655         if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
656                 WARN_ELF("elf_update");
657                 return -1;
658         }
659
660         return 0;
661 }
662
663 void elf_close(struct elf *elf)
664 {
665         struct section *sec, *tmpsec;
666         struct symbol *sym, *tmpsym;
667         struct rela *rela, *tmprela;
668
669         if (elf->elf)
670                 elf_end(elf->elf);
671
672         if (elf->fd > 0)
673                 close(elf->fd);
674
675         list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
676                 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
677                         list_del(&sym->list);
678                         hash_del(&sym->hash);
679                         free(sym);
680                 }
681                 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
682                         list_del(&rela->list);
683                         hash_del(&rela->hash);
684                         free(rela);
685                 }
686                 list_del(&sec->list);
687                 free(sec);
688         }
689
690         free(elf);
691 }