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