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