Merge remote-tracking branch 'spi/topic/core' into spi-next
[sfrench/cifs-2.6.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
30 #include "elf.h"
31 #include "warn.h"
32
33 struct section *find_section_by_name(struct elf *elf, const char *name)
34 {
35         struct section *sec;
36
37         list_for_each_entry(sec, &elf->sections, list)
38                 if (!strcmp(sec->name, name))
39                         return sec;
40
41         return NULL;
42 }
43
44 static struct section *find_section_by_index(struct elf *elf,
45                                              unsigned int idx)
46 {
47         struct section *sec;
48
49         list_for_each_entry(sec, &elf->sections, list)
50                 if (sec->idx == idx)
51                         return sec;
52
53         return NULL;
54 }
55
56 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
57 {
58         struct section *sec;
59         struct symbol *sym;
60
61         list_for_each_entry(sec, &elf->sections, list)
62                 hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
63                         if (sym->idx == idx)
64                                 return sym;
65
66         return NULL;
67 }
68
69 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
70 {
71         struct symbol *sym;
72
73         list_for_each_entry(sym, &sec->symbol_list, list)
74                 if (sym->type != STT_SECTION &&
75                     sym->offset == offset)
76                         return sym;
77
78         return NULL;
79 }
80
81 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
82 {
83         struct symbol *sym;
84
85         list_for_each_entry(sym, &sec->symbol_list, list)
86                 if (sym->type != STT_SECTION &&
87                     offset >= sym->offset && offset < sym->offset + sym->len)
88                         return sym;
89
90         return NULL;
91 }
92
93 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
94                                      unsigned int len)
95 {
96         struct rela *rela;
97         unsigned long o;
98
99         if (!sec->rela)
100                 return NULL;
101
102         for (o = offset; o < offset + len; o++)
103                 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
104                         if (rela->offset == o)
105                                 return rela;
106
107         return NULL;
108 }
109
110 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
111 {
112         return find_rela_by_dest_range(sec, offset, 1);
113 }
114
115 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
116 {
117         struct symbol *func;
118
119         list_for_each_entry(func, &sec->symbol_list, list)
120                 if (func->type == STT_FUNC && offset >= func->offset &&
121                     offset < func->offset + func->len)
122                         return func;
123
124         return NULL;
125 }
126
127 static int read_sections(struct elf *elf)
128 {
129         Elf_Scn *s = NULL;
130         struct section *sec;
131         size_t shstrndx, sections_nr;
132         int i;
133
134         if (elf_getshdrnum(elf->elf, &sections_nr)) {
135                 WARN_ELF("elf_getshdrnum");
136                 return -1;
137         }
138
139         if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
140                 WARN_ELF("elf_getshdrstrndx");
141                 return -1;
142         }
143
144         for (i = 0; i < sections_nr; i++) {
145                 sec = malloc(sizeof(*sec));
146                 if (!sec) {
147                         perror("malloc");
148                         return -1;
149                 }
150                 memset(sec, 0, sizeof(*sec));
151
152                 INIT_LIST_HEAD(&sec->symbol_list);
153                 INIT_LIST_HEAD(&sec->rela_list);
154                 hash_init(sec->rela_hash);
155                 hash_init(sec->symbol_hash);
156
157                 list_add_tail(&sec->list, &elf->sections);
158
159                 s = elf_getscn(elf->elf, i);
160                 if (!s) {
161                         WARN_ELF("elf_getscn");
162                         return -1;
163                 }
164
165                 sec->idx = elf_ndxscn(s);
166
167                 if (!gelf_getshdr(s, &sec->sh)) {
168                         WARN_ELF("gelf_getshdr");
169                         return -1;
170                 }
171
172                 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
173                 if (!sec->name) {
174                         WARN_ELF("elf_strptr");
175                         return -1;
176                 }
177
178                 if (sec->sh.sh_size != 0) {
179                         sec->data = elf_getdata(s, NULL);
180                         if (!sec->data) {
181                                 WARN_ELF("elf_getdata");
182                                 return -1;
183                         }
184                         if (sec->data->d_off != 0 ||
185                             sec->data->d_size != sec->sh.sh_size) {
186                                 WARN("unexpected data attributes for %s",
187                                      sec->name);
188                                 return -1;
189                         }
190                 }
191                 sec->len = sec->sh.sh_size;
192         }
193
194         /* sanity check, one more call to elf_nextscn() should return NULL */
195         if (elf_nextscn(elf->elf, s)) {
196                 WARN("section entry mismatch");
197                 return -1;
198         }
199
200         return 0;
201 }
202
203 static int read_symbols(struct elf *elf)
204 {
205         struct section *symtab;
206         struct symbol *sym;
207         struct list_head *entry, *tmp;
208         int symbols_nr, i;
209
210         symtab = find_section_by_name(elf, ".symtab");
211         if (!symtab) {
212                 WARN("missing symbol table");
213                 return -1;
214         }
215
216         symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
217
218         for (i = 0; i < symbols_nr; i++) {
219                 sym = malloc(sizeof(*sym));
220                 if (!sym) {
221                         perror("malloc");
222                         return -1;
223                 }
224                 memset(sym, 0, sizeof(*sym));
225
226                 sym->idx = i;
227
228                 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
229                         WARN_ELF("gelf_getsym");
230                         goto err;
231                 }
232
233                 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
234                                        sym->sym.st_name);
235                 if (!sym->name) {
236                         WARN_ELF("elf_strptr");
237                         goto err;
238                 }
239
240                 sym->type = GELF_ST_TYPE(sym->sym.st_info);
241                 sym->bind = GELF_ST_BIND(sym->sym.st_info);
242
243                 if (sym->sym.st_shndx > SHN_UNDEF &&
244                     sym->sym.st_shndx < SHN_LORESERVE) {
245                         sym->sec = find_section_by_index(elf,
246                                                          sym->sym.st_shndx);
247                         if (!sym->sec) {
248                                 WARN("couldn't find section for symbol %s",
249                                      sym->name);
250                                 goto err;
251                         }
252                         if (sym->type == STT_SECTION) {
253                                 sym->name = sym->sec->name;
254                                 sym->sec->sym = sym;
255                         }
256                 } else
257                         sym->sec = find_section_by_index(elf, 0);
258
259                 sym->offset = sym->sym.st_value;
260                 sym->len = sym->sym.st_size;
261
262                 /* sorted insert into a per-section list */
263                 entry = &sym->sec->symbol_list;
264                 list_for_each_prev(tmp, &sym->sec->symbol_list) {
265                         struct symbol *s;
266
267                         s = list_entry(tmp, struct symbol, list);
268
269                         if (sym->offset > s->offset) {
270                                 entry = tmp;
271                                 break;
272                         }
273
274                         if (sym->offset == s->offset && sym->len >= s->len) {
275                                 entry = tmp;
276                                 break;
277                         }
278                 }
279                 list_add(&sym->list, entry);
280                 hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
281         }
282
283         return 0;
284
285 err:
286         free(sym);
287         return -1;
288 }
289
290 static int read_relas(struct elf *elf)
291 {
292         struct section *sec;
293         struct rela *rela;
294         int i;
295         unsigned int symndx;
296
297         list_for_each_entry(sec, &elf->sections, list) {
298                 if (sec->sh.sh_type != SHT_RELA)
299                         continue;
300
301                 sec->base = find_section_by_name(elf, sec->name + 5);
302                 if (!sec->base) {
303                         WARN("can't find base section for rela section %s",
304                              sec->name);
305                         return -1;
306                 }
307
308                 sec->base->rela = sec;
309
310                 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
311                         rela = malloc(sizeof(*rela));
312                         if (!rela) {
313                                 perror("malloc");
314                                 return -1;
315                         }
316                         memset(rela, 0, sizeof(*rela));
317
318                         if (!gelf_getrela(sec->data, i, &rela->rela)) {
319                                 WARN_ELF("gelf_getrela");
320                                 return -1;
321                         }
322
323                         rela->type = GELF_R_TYPE(rela->rela.r_info);
324                         rela->addend = rela->rela.r_addend;
325                         rela->offset = rela->rela.r_offset;
326                         symndx = GELF_R_SYM(rela->rela.r_info);
327                         rela->sym = find_symbol_by_index(elf, symndx);
328                         if (!rela->sym) {
329                                 WARN("can't find rela entry symbol %d for %s",
330                                      symndx, sec->name);
331                                 return -1;
332                         }
333
334                         list_add_tail(&rela->list, &sec->rela_list);
335                         hash_add(sec->rela_hash, &rela->hash, rela->offset);
336
337                 }
338         }
339
340         return 0;
341 }
342
343 struct elf *elf_open(const char *name, int flags)
344 {
345         struct elf *elf;
346         Elf_Cmd cmd;
347
348         elf_version(EV_CURRENT);
349
350         elf = malloc(sizeof(*elf));
351         if (!elf) {
352                 perror("malloc");
353                 return NULL;
354         }
355         memset(elf, 0, sizeof(*elf));
356
357         INIT_LIST_HEAD(&elf->sections);
358
359         elf->fd = open(name, flags);
360         if (elf->fd == -1) {
361                 perror("open");
362                 goto err;
363         }
364
365         if ((flags & O_ACCMODE) == O_RDONLY)
366                 cmd = ELF_C_READ_MMAP;
367         else if ((flags & O_ACCMODE) == O_RDWR)
368                 cmd = ELF_C_RDWR;
369         else /* O_WRONLY */
370                 cmd = ELF_C_WRITE;
371
372         elf->elf = elf_begin(elf->fd, cmd, NULL);
373         if (!elf->elf) {
374                 WARN_ELF("elf_begin");
375                 goto err;
376         }
377
378         if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
379                 WARN_ELF("gelf_getehdr");
380                 goto err;
381         }
382
383         if (read_sections(elf))
384                 goto err;
385
386         if (read_symbols(elf))
387                 goto err;
388
389         if (read_relas(elf))
390                 goto err;
391
392         return elf;
393
394 err:
395         elf_close(elf);
396         return NULL;
397 }
398
399 struct section *elf_create_section(struct elf *elf, const char *name,
400                                    size_t entsize, int nr)
401 {
402         struct section *sec, *shstrtab;
403         size_t size = entsize * nr;
404         struct Elf_Scn *s;
405         Elf_Data *data;
406
407         sec = malloc(sizeof(*sec));
408         if (!sec) {
409                 perror("malloc");
410                 return NULL;
411         }
412         memset(sec, 0, sizeof(*sec));
413
414         INIT_LIST_HEAD(&sec->symbol_list);
415         INIT_LIST_HEAD(&sec->rela_list);
416         hash_init(sec->rela_hash);
417         hash_init(sec->symbol_hash);
418
419         list_add_tail(&sec->list, &elf->sections);
420
421         s = elf_newscn(elf->elf);
422         if (!s) {
423                 WARN_ELF("elf_newscn");
424                 return NULL;
425         }
426
427         sec->name = strdup(name);
428         if (!sec->name) {
429                 perror("strdup");
430                 return NULL;
431         }
432
433         sec->idx = elf_ndxscn(s);
434         sec->len = size;
435         sec->changed = true;
436
437         sec->data = elf_newdata(s);
438         if (!sec->data) {
439                 WARN_ELF("elf_newdata");
440                 return NULL;
441         }
442
443         sec->data->d_size = size;
444         sec->data->d_align = 1;
445
446         if (size) {
447                 sec->data->d_buf = malloc(size);
448                 if (!sec->data->d_buf) {
449                         perror("malloc");
450                         return NULL;
451                 }
452                 memset(sec->data->d_buf, 0, size);
453         }
454
455         if (!gelf_getshdr(s, &sec->sh)) {
456                 WARN_ELF("gelf_getshdr");
457                 return NULL;
458         }
459
460         sec->sh.sh_size = size;
461         sec->sh.sh_entsize = entsize;
462         sec->sh.sh_type = SHT_PROGBITS;
463         sec->sh.sh_addralign = 1;
464         sec->sh.sh_flags = SHF_ALLOC;
465
466
467         /* Add section name to .shstrtab */
468         shstrtab = find_section_by_name(elf, ".shstrtab");
469         if (!shstrtab) {
470                 WARN("can't find .shstrtab section");
471                 return NULL;
472         }
473
474         s = elf_getscn(elf->elf, shstrtab->idx);
475         if (!s) {
476                 WARN_ELF("elf_getscn");
477                 return NULL;
478         }
479
480         data = elf_newdata(s);
481         if (!data) {
482                 WARN_ELF("elf_newdata");
483                 return NULL;
484         }
485
486         data->d_buf = sec->name;
487         data->d_size = strlen(name) + 1;
488         data->d_align = 1;
489
490         sec->sh.sh_name = shstrtab->len;
491
492         shstrtab->len += strlen(name) + 1;
493         shstrtab->changed = true;
494
495         return sec;
496 }
497
498 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
499 {
500         char *relaname;
501         struct section *sec;
502
503         relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
504         if (!relaname) {
505                 perror("malloc");
506                 return NULL;
507         }
508         strcpy(relaname, ".rela");
509         strcat(relaname, base->name);
510
511         sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
512         free(relaname);
513         if (!sec)
514                 return NULL;
515
516         base->rela = sec;
517         sec->base = base;
518
519         sec->sh.sh_type = SHT_RELA;
520         sec->sh.sh_addralign = 8;
521         sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
522         sec->sh.sh_info = base->idx;
523         sec->sh.sh_flags = SHF_INFO_LINK;
524
525         return sec;
526 }
527
528 int elf_rebuild_rela_section(struct section *sec)
529 {
530         struct rela *rela;
531         int nr, idx = 0, size;
532         GElf_Rela *relas;
533
534         nr = 0;
535         list_for_each_entry(rela, &sec->rela_list, list)
536                 nr++;
537
538         size = nr * sizeof(*relas);
539         relas = malloc(size);
540         if (!relas) {
541                 perror("malloc");
542                 return -1;
543         }
544
545         sec->data->d_buf = relas;
546         sec->data->d_size = size;
547
548         sec->sh.sh_size = size;
549
550         idx = 0;
551         list_for_each_entry(rela, &sec->rela_list, list) {
552                 relas[idx].r_offset = rela->offset;
553                 relas[idx].r_addend = rela->addend;
554                 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
555                 idx++;
556         }
557
558         return 0;
559 }
560
561 int elf_write(struct elf *elf)
562 {
563         struct section *sec;
564         Elf_Scn *s;
565
566         /* Update section headers for changed sections: */
567         list_for_each_entry(sec, &elf->sections, list) {
568                 if (sec->changed) {
569                         s = elf_getscn(elf->elf, sec->idx);
570                         if (!s) {
571                                 WARN_ELF("elf_getscn");
572                                 return -1;
573                         }
574                         if (!gelf_update_shdr(s, &sec->sh)) {
575                                 WARN_ELF("gelf_update_shdr");
576                                 return -1;
577                         }
578                 }
579         }
580
581         /* Make sure the new section header entries get updated properly. */
582         elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
583
584         /* Write all changes to the file. */
585         if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
586                 WARN_ELF("elf_update");
587                 return -1;
588         }
589
590         return 0;
591 }
592
593 void elf_close(struct elf *elf)
594 {
595         struct section *sec, *tmpsec;
596         struct symbol *sym, *tmpsym;
597         struct rela *rela, *tmprela;
598
599         if (elf->elf)
600                 elf_end(elf->elf);
601
602         if (elf->fd > 0)
603                 close(elf->fd);
604
605         list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
606                 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
607                         list_del(&sym->list);
608                         hash_del(&sym->hash);
609                         free(sym);
610                 }
611                 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
612                         list_del(&rela->list);
613                         hash_del(&rela->hash);
614                         free(rela);
615                 }
616                 list_del(&sec->list);
617                 free(sec);
618         }
619
620         free(elf);
621 }