Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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) |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 62 | hash_for_each_possible(sec->symbol_hash, sym, hash, idx) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 73 | list_for_each_entry(sym, &sec->symbol_list, list) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 74 | if (sym->type != STT_SECTION && |
| 75 | sym->offset == offset) |
| 76 | return sym; |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Josh Poimboeuf | 5c51f4a | 2017-03-02 16:57:23 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 93 | struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset, |
| 94 | unsigned int len) |
| 95 | { |
| 96 | struct rela *rela; |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 97 | unsigned long o; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 98 | |
| 99 | if (!sec->rela) |
| 100 | return NULL; |
| 101 | |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 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; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 119 | list_for_each_entry(func, &sec->symbol_list, list) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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, §ions_nr)) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 135 | WARN_ELF("elf_getshdrnum"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | if (elf_getshdrstrndx(elf->elf, &shstrndx)) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 140 | WARN_ELF("elf_getshdrstrndx"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 152 | INIT_LIST_HEAD(&sec->symbol_list); |
| 153 | INIT_LIST_HEAD(&sec->rela_list); |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 154 | hash_init(sec->rela_hash); |
| 155 | hash_init(sec->symbol_hash); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 156 | |
| 157 | list_add_tail(&sec->list, &elf->sections); |
| 158 | |
| 159 | s = elf_getscn(elf->elf, i); |
| 160 | if (!s) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 161 | WARN_ELF("elf_getscn"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | sec->idx = elf_ndxscn(s); |
| 166 | |
| 167 | if (!gelf_getshdr(s, &sec->sh)) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 168 | WARN_ELF("gelf_getshdr"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); |
| 173 | if (!sec->name) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 174 | WARN_ELF("elf_strptr"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 175 | return -1; |
| 176 | } |
| 177 | |
Petr Vandrovec | df968c9 | 2017-09-15 02:15:05 -0500 | [diff] [blame^] | 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 | } |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 190 | } |
Petr Vandrovec | df968c9 | 2017-09-15 02:15:05 -0500 | [diff] [blame^] | 191 | sec->len = sec->sh.sh_size; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 228 | if (!gelf_getsym(symtab->data, i, &sym->sym)) { |
| 229 | WARN_ELF("gelf_getsym"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 236 | WARN_ELF("elf_strptr"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 */ |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 263 | entry = &sym->sec->symbol_list; |
| 264 | list_for_each_prev(tmp, &sym->sec->symbol_list) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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); |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 280 | hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 318 | if (!gelf_getrela(sec->data, i, &rela->rela)) { |
| 319 | WARN_ELF("gelf_getrela"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | } |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 333 | |
| 334 | list_add_tail(&rela->list, &sec->rela_list); |
| 335 | hash_add(sec->rela_hash, &rela->hash, rela->offset); |
| 336 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 343 | struct elf *elf_open(const char *name, int flags) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 344 | { |
| 345 | struct elf *elf; |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 346 | Elf_Cmd cmd; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 359 | elf->fd = open(name, flags); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 360 | if (elf->fd == -1) { |
| 361 | perror("open"); |
| 362 | goto err; |
| 363 | } |
| 364 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 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); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 373 | if (!elf->elf) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 374 | WARN_ELF("elf_begin"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 375 | goto err; |
| 376 | } |
| 377 | |
| 378 | if (!gelf_getehdr(elf->elf, &elf->ehdr)) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 379 | WARN_ELF("gelf_getehdr"); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 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); |
Martin Kepplinger | 0998b7a | 2017-09-14 08:01:38 +0200 | [diff] [blame] | 512 | free(relaname); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 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 | list_for_each_entry(sec, &elf->sections, list) { |
| 567 | if (sec->changed) { |
| 568 | s = elf_getscn(elf->elf, sec->idx); |
| 569 | if (!s) { |
| 570 | WARN_ELF("elf_getscn"); |
| 571 | return -1; |
| 572 | } |
| 573 | if (!gelf_update_shdr (s, &sec->sh)) { |
| 574 | WARN_ELF("gelf_update_shdr"); |
| 575 | return -1; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (elf_update(elf->elf, ELF_C_WRITE) < 0) { |
| 581 | WARN_ELF("elf_update"); |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 588 | void elf_close(struct elf *elf) |
| 589 | { |
| 590 | struct section *sec, *tmpsec; |
| 591 | struct symbol *sym, *tmpsym; |
| 592 | struct rela *rela, *tmprela; |
| 593 | |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 594 | if (elf->elf) |
| 595 | elf_end(elf->elf); |
| 596 | |
| 597 | if (elf->fd > 0) |
| 598 | close(elf->fd); |
| 599 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 600 | list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 601 | list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 602 | list_del(&sym->list); |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 603 | hash_del(&sym->hash); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 604 | free(sym); |
| 605 | } |
Josh Poimboeuf | a196e17 | 2016-03-09 00:06:57 -0600 | [diff] [blame] | 606 | list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 607 | list_del(&rela->list); |
Josh Poimboeuf | 042ba73 | 2016-03-09 00:07:00 -0600 | [diff] [blame] | 608 | hash_del(&rela->hash); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 609 | free(rela); |
| 610 | } |
| 611 | list_del(&sec->list); |
| 612 | free(sec); |
| 613 | } |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 614 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 615 | free(elf); |
| 616 | } |