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