1 /* $NetBSD: elf_update.c,v 1.3 2016/02/20 02:43:42 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2006-2011 Joseph Koshy 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #if HAVE_NBTOOL_CONFIG_H 30 # include "nbtool_config.h" 31 #endif 32 33 #include <sys/param.h> 34 #include <sys/stat.h> 35 36 #include <assert.h> 37 #include <errno.h> 38 #include <gelf.h> 39 #include <libelf.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 #include "_libelf.h" 45 46 #if ELFTC_HAVE_MMAP 47 #include <sys/mman.h> 48 #endif 49 50 __RCSID("$NetBSD: elf_update.c,v 1.3 2016/02/20 02:43:42 christos Exp $"); 51 ELFTC_VCSID("Id: elf_update.c 3190 2015-05-04 15:23:08Z jkoshy "); 52 53 /* 54 * Layout strategy: 55 * 56 * - Case 1: ELF_F_LAYOUT is asserted 57 * In this case the application has full control over where the 58 * section header table, program header table, and section data 59 * will reside. The library only perform error checks. 60 * 61 * - Case 2: ELF_F_LAYOUT is not asserted 62 * 63 * The library will do the object layout using the following 64 * ordering: 65 * - The executable header is placed first, are required by the 66 * ELF specification. 67 * - The program header table is placed immediately following the 68 * executable header. 69 * - Section data, if any, is placed after the program header 70 * table, aligned appropriately. 71 * - The section header table, if needed, is placed last. 72 * 73 * There are two sub-cases to be taken care of: 74 * 75 * - Case 2a: e->e_cmd == ELF_C_READ or ELF_C_RDWR 76 * 77 * In this sub-case, the underlying ELF object may already have 78 * content in it, which the application may have modified. The 79 * library will retrieve content from the existing object as 80 * needed. 81 * 82 * - Case 2b: e->e_cmd == ELF_C_WRITE 83 * 84 * The ELF object is being created afresh in this sub-case; 85 * there is no pre-existing content in the underlying ELF 86 * object. 87 */ 88 89 /* 90 * The types of extents in an ELF object. 91 */ 92 enum elf_extent { 93 ELF_EXTENT_EHDR, 94 ELF_EXTENT_PHDR, 95 ELF_EXTENT_SECTION, 96 ELF_EXTENT_SHDR 97 }; 98 99 /* 100 * A extent descriptor, used when laying out an ELF object. 101 */ 102 struct _Elf_Extent { 103 SLIST_ENTRY(_Elf_Extent) ex_next; 104 uint64_t ex_start; /* Start of the region. */ 105 uint64_t ex_size; /* The size of the region. */ 106 enum elf_extent ex_type; /* Type of region. */ 107 void *ex_desc; /* Associated descriptor. */ 108 }; 109 110 SLIST_HEAD(_Elf_Extent_List, _Elf_Extent); 111 112 /* 113 * Compute the extents of a section, by looking at the data 114 * descriptors associated with it. The function returns 1 115 * if successful, or zero if an error was detected. 116 */ 117 static int 118 _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) 119 { 120 Elf_Data *d; 121 size_t fsz, msz; 122 int ec, elftype; 123 uint32_t sh_type; 124 uint64_t d_align; 125 Elf32_Shdr *shdr32; 126 Elf64_Shdr *shdr64; 127 struct _Libelf_Data *ld; 128 uint64_t scn_size, scn_alignment; 129 uint64_t sh_align, sh_entsize, sh_offset, sh_size; 130 131 ec = e->e_class; 132 133 shdr32 = &s->s_shdr.s_shdr32; 134 shdr64 = &s->s_shdr.s_shdr64; 135 if (ec == ELFCLASS32) { 136 sh_type = shdr32->sh_type; 137 sh_align = (uint64_t) shdr32->sh_addralign; 138 sh_entsize = (uint64_t) shdr32->sh_entsize; 139 sh_offset = (uint64_t) shdr32->sh_offset; 140 sh_size = (uint64_t) shdr32->sh_size; 141 } else { 142 sh_type = shdr64->sh_type; 143 sh_align = shdr64->sh_addralign; 144 sh_entsize = shdr64->sh_entsize; 145 sh_offset = shdr64->sh_offset; 146 sh_size = shdr64->sh_size; 147 } 148 149 assert(sh_type != SHT_NULL && sh_type != SHT_NOBITS); 150 151 elftype = _libelf_xlate_shtype(sh_type); 152 if (elftype > ELF_T_LAST) { 153 LIBELF_SET_ERROR(SECTION, 0); 154 return (0); 155 } 156 157 if (sh_align == 0) 158 sh_align = _libelf_falign(elftype, ec); 159 160 /* 161 * Compute the section's size and alignment using the data 162 * descriptors associated with the section. 163 */ 164 if (STAILQ_EMPTY(&s->s_data)) { 165 /* 166 * The section's content (if any) has not been read in 167 * yet. If section is not dirty marked dirty, we can 168 * reuse the values in the 'sh_size' and 'sh_offset' 169 * fields of the section header. 170 */ 171 if ((s->s_flags & ELF_F_DIRTY) == 0) { 172 /* 173 * If the library is doing the layout, then we 174 * compute the new start offset for the 175 * section based on the current offset and the 176 * section's alignment needs. 177 * 178 * If the application is doing the layout, we 179 * can use the value in the 'sh_offset' field 180 * in the section header directly. 181 */ 182 if (e->e_flags & ELF_F_LAYOUT) 183 goto updatedescriptor; 184 else 185 goto computeoffset; 186 } 187 188 /* 189 * Otherwise, we need to bring in the section's data 190 * from the underlying ELF object. 191 */ 192 if (e->e_cmd != ELF_C_WRITE && elf_getdata(s, NULL) == NULL) 193 return (0); 194 } 195 196 /* 197 * Loop through the section's data descriptors. 198 */ 199 scn_size = 0L; 200 scn_alignment = 0; 201 STAILQ_FOREACH(ld, &s->s_data, d_next) { 202 203 d = &ld->d_data; 204 205 /* 206 * The data buffer's type is known. 207 */ 208 if (d->d_type >= ELF_T_NUM) { 209 LIBELF_SET_ERROR(DATA, 0); 210 return (0); 211 } 212 213 /* 214 * The data buffer's version is supported. 215 */ 216 if (d->d_version != e->e_version) { 217 LIBELF_SET_ERROR(VERSION, 0); 218 return (0); 219 } 220 221 /* 222 * The buffer's alignment is non-zero and a power of 223 * two. 224 */ 225 if ((d_align = d->d_align) == 0 || 226 (d_align & (d_align - 1))) { 227 LIBELF_SET_ERROR(DATA, 0); 228 return (0); 229 } 230 231 /* 232 * The buffer's size should be a multiple of the 233 * memory size of the underlying type. 234 */ 235 msz = _libelf_msize(d->d_type, ec, e->e_version); 236 if (d->d_size % msz) { 237 LIBELF_SET_ERROR(DATA, 0); 238 return (0); 239 } 240 241 /* 242 * If the application is controlling layout, then the 243 * d_offset field should be compatible with the 244 * buffer's specified alignment. 245 */ 246 if ((e->e_flags & ELF_F_LAYOUT) && 247 (d->d_off & (d_align - 1))) { 248 LIBELF_SET_ERROR(LAYOUT, 0); 249 return (0); 250 } 251 252 /* 253 * Compute the section's size. 254 */ 255 if (e->e_flags & ELF_F_LAYOUT) { 256 if ((uint64_t) d->d_off + d->d_size > scn_size) 257 scn_size = d->d_off + d->d_size; 258 } else { 259 scn_size = roundup2(scn_size, d->d_align); 260 d->d_off = scn_size; 261 fsz = _libelf_fsize(d->d_type, ec, d->d_version, 262 (size_t) d->d_size / msz); 263 scn_size += fsz; 264 } 265 266 /* 267 * The section's alignment is the maximum alignment 268 * needed for its data buffers. 269 */ 270 if (d_align > scn_alignment) 271 scn_alignment = d_align; 272 } 273 274 275 /* 276 * If the application is requesting full control over the 277 * layout of the section, check the section's specified size, 278 * offsets and alignment for sanity. 279 */ 280 if (e->e_flags & ELF_F_LAYOUT) { 281 if (scn_alignment > sh_align || 282 sh_offset % sh_align || 283 sh_size < scn_size || 284 sh_offset % _libelf_falign(elftype, ec)) { 285 LIBELF_SET_ERROR(LAYOUT, 0); 286 return (0); 287 } 288 goto updatedescriptor; 289 } 290 291 /* 292 * Otherwise, compute the values in the section header. 293 * 294 * The section alignment is the maximum alignment for any of 295 * its contained data descriptors. 296 */ 297 if (scn_alignment > sh_align) 298 sh_align = scn_alignment; 299 300 /* 301 * If the section entry size is zero, try and fill in an 302 * appropriate entry size. Per the elf(5) manual page 303 * sections without fixed-size entries should have their 304 * 'sh_entsize' field set to zero. 305 */ 306 if (sh_entsize == 0 && 307 (sh_entsize = _libelf_fsize(elftype, ec, e->e_version, 308 (size_t) 1)) == 1) 309 sh_entsize = 0; 310 311 sh_size = scn_size; 312 313 computeoffset: 314 /* 315 * Compute the new offset for the section based on 316 * the section's alignment needs. 317 */ 318 sh_offset = roundup((uint64_t) rc, sh_align); 319 320 /* 321 * Update the section header. 322 */ 323 if (ec == ELFCLASS32) { 324 shdr32->sh_addralign = (uint32_t) sh_align; 325 shdr32->sh_entsize = (uint32_t) sh_entsize; 326 shdr32->sh_offset = (uint32_t) sh_offset; 327 shdr32->sh_size = (uint32_t) sh_size; 328 } else { 329 shdr64->sh_addralign = sh_align; 330 shdr64->sh_entsize = sh_entsize; 331 shdr64->sh_offset = sh_offset; 332 shdr64->sh_size = sh_size; 333 } 334 335 updatedescriptor: 336 /* 337 * Update the section descriptor. 338 */ 339 s->s_size = sh_size; 340 s->s_offset = sh_offset; 341 342 return (1); 343 } 344 345 /* 346 * Free a list of extent descriptors. 347 */ 348 349 static void 350 _libelf_release_extents(struct _Elf_Extent_List *extents) 351 { 352 struct _Elf_Extent *ex; 353 354 while ((ex = SLIST_FIRST(extents)) != NULL) { 355 SLIST_REMOVE_HEAD(extents, ex_next); 356 free(ex); 357 } 358 } 359 360 /* 361 * Check if an extent 's' defined by [start..start+size) is free. 362 * This routine assumes that the given extent list is sorted in order 363 * of ascending extent offsets. 364 */ 365 366 static int 367 _libelf_extent_is_unused(struct _Elf_Extent_List *extents, 368 const uint64_t start, const uint64_t size, struct _Elf_Extent **prevt) 369 { 370 uint64_t tmax, tmin; 371 struct _Elf_Extent *t, *pt; 372 const uint64_t smax = start + size; 373 374 /* First, look for overlaps with existing extents. */ 375 pt = NULL; 376 SLIST_FOREACH(t, extents, ex_next) { 377 tmin = t->ex_start; 378 tmax = tmin + t->ex_size; 379 380 if (tmax <= start) { 381 /* 382 * 't' lies entirely before 's': ...| t |...| s |... 383 */ 384 pt = t; 385 continue; 386 } else if (smax <= tmin) { 387 /* 388 * 's' lies entirely before 't', and after 'pt': 389 * ...| pt |...| s |...| t |... 390 */ 391 assert(pt == NULL || 392 pt->ex_start + pt->ex_size <= start); 393 break; 394 } else 395 /* 's' and 't' overlap. */ 396 return (0); 397 } 398 399 if (prevt) 400 *prevt = pt; 401 return (1); 402 } 403 404 /* 405 * Insert an extent into the list of extents. 406 */ 407 408 static int 409 _libelf_insert_extent(struct _Elf_Extent_List *extents, int type, 410 uint64_t start, uint64_t size, void *desc) 411 { 412 struct _Elf_Extent *ex, *prevt; 413 414 assert(type >= ELF_EXTENT_EHDR && type <= ELF_EXTENT_SHDR); 415 416 prevt = NULL; 417 418 /* 419 * If the requested range overlaps with an existing extent, 420 * signal an error. 421 */ 422 if (!_libelf_extent_is_unused(extents, start, size, &prevt)) { 423 LIBELF_SET_ERROR(LAYOUT, 0); 424 return (0); 425 } 426 427 /* Allocate and fill in a new extent descriptor. */ 428 if ((ex = malloc(sizeof(struct _Elf_Extent))) == NULL) { 429 LIBELF_SET_ERROR(RESOURCE, errno); 430 return (0); 431 } 432 ex->ex_start = start; 433 ex->ex_size = size; 434 ex->ex_desc = desc; 435 ex->ex_type = type; 436 437 /* Insert the region descriptor into the list. */ 438 if (prevt) 439 SLIST_INSERT_AFTER(prevt, ex, ex_next); 440 else 441 SLIST_INSERT_HEAD(extents, ex, ex_next); 442 return (1); 443 } 444 445 /* 446 * Recompute section layout. 447 */ 448 449 static off_t 450 _libelf_resync_sections(Elf *e, off_t rc, struct _Elf_Extent_List *extents) 451 { 452 int ec; 453 Elf_Scn *s; 454 size_t sh_type; 455 456 ec = e->e_class; 457 458 /* 459 * Make a pass through sections, computing the extent of each 460 * section. 461 */ 462 STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) { 463 if (ec == ELFCLASS32) 464 sh_type = s->s_shdr.s_shdr32.sh_type; 465 else 466 sh_type = s->s_shdr.s_shdr64.sh_type; 467 468 if (sh_type == SHT_NOBITS || sh_type == SHT_NULL) 469 continue; 470 471 if (_libelf_compute_section_extents(e, s, rc) == 0) 472 return ((off_t) -1); 473 474 if (s->s_size == 0) 475 continue; 476 477 if (!_libelf_insert_extent(extents, ELF_EXTENT_SECTION, 478 s->s_offset, s->s_size, s)) 479 return ((off_t) -1); 480 481 if ((size_t) rc < s->s_offset + s->s_size) 482 rc = (off_t) (s->s_offset + s->s_size); 483 } 484 485 return (rc); 486 } 487 488 /* 489 * Recompute the layout of the ELF object and update the internal data 490 * structures associated with the ELF descriptor. 491 * 492 * Returns the size in bytes the ELF object would occupy in its file 493 * representation. 494 * 495 * After a successful call to this function, the following structures 496 * are updated: 497 * 498 * - The ELF header is updated. 499 * - All extents in the ELF object are sorted in order of ascending 500 * addresses. Sections have their section header table entries 501 * updated. An error is signalled if an overlap was detected among 502 * extents. 503 * - Data descriptors associated with sections are checked for valid 504 * types, offsets and alignment. 505 * 506 * After a resync_elf() successfully returns, the ELF descriptor is 507 * ready for being handed over to _libelf_write_elf(). 508 */ 509 510 static off_t 511 _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) 512 { 513 int ec, eh_class; 514 unsigned int eh_byteorder, eh_version; 515 size_t align, fsz; 516 size_t phnum, shnum; 517 off_t rc, phoff, shoff; 518 void *ehdr, *phdr; 519 Elf32_Ehdr *eh32; 520 Elf64_Ehdr *eh64; 521 522 rc = 0; 523 524 ec = e->e_class; 525 526 assert(ec == ELFCLASS32 || ec == ELFCLASS64); 527 528 /* 529 * Prepare the EHDR. 530 */ 531 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 532 return ((off_t) -1); 533 534 eh32 = ehdr; 535 eh64 = ehdr; 536 537 if (ec == ELFCLASS32) { 538 eh_byteorder = eh32->e_ident[EI_DATA]; 539 eh_class = eh32->e_ident[EI_CLASS]; 540 phoff = (off_t) eh32->e_phoff; 541 shoff = (off_t) eh32->e_shoff; 542 eh_version = eh32->e_version; 543 } else { 544 eh_byteorder = eh64->e_ident[EI_DATA]; 545 eh_class = eh64->e_ident[EI_CLASS]; 546 phoff = (off_t) eh64->e_phoff; 547 shoff = (off_t) eh64->e_shoff; 548 eh_version = eh64->e_version; 549 } 550 551 if (phoff < 0 || shoff < 0) { 552 LIBELF_SET_ERROR(HEADER, 0); 553 return ((off_t) -1); 554 } 555 556 if (eh_version == EV_NONE) 557 eh_version = EV_CURRENT; 558 559 if (eh_version != e->e_version) { /* always EV_CURRENT */ 560 LIBELF_SET_ERROR(VERSION, 0); 561 return ((off_t) -1); 562 } 563 564 if (eh_class != e->e_class) { 565 LIBELF_SET_ERROR(CLASS, 0); 566 return ((off_t) -1); 567 } 568 569 if (e->e_cmd != ELF_C_WRITE && eh_byteorder != e->e_byteorder) { 570 LIBELF_SET_ERROR(HEADER, 0); 571 return ((off_t) -1); 572 } 573 574 shnum = e->e_u.e_elf.e_nscn; 575 phnum = e->e_u.e_elf.e_nphdr; 576 577 e->e_byteorder = eh_byteorder; 578 579 #define INITIALIZE_EHDR(E,EC,V) do { \ 580 unsigned int _version = (unsigned int) (V); \ 581 (E)->e_ident[EI_MAG0] = ELFMAG0; \ 582 (E)->e_ident[EI_MAG1] = ELFMAG1; \ 583 (E)->e_ident[EI_MAG2] = ELFMAG2; \ 584 (E)->e_ident[EI_MAG3] = ELFMAG3; \ 585 (E)->e_ident[EI_CLASS] = (unsigned char) (EC); \ 586 (E)->e_ident[EI_VERSION] = (_version & 0xFFU); \ 587 (E)->e_ehsize = (uint16_t) _libelf_fsize(ELF_T_EHDR, \ 588 (EC), _version, (size_t) 1); \ 589 (E)->e_phentsize = (uint16_t) ((phnum == 0) ? 0 : \ 590 _libelf_fsize(ELF_T_PHDR, (EC), _version, \ 591 (size_t) 1)); \ 592 (E)->e_shentsize = (uint16_t) _libelf_fsize(ELF_T_SHDR, \ 593 (EC), _version, (size_t) 1); \ 594 } while (/*CONSTCOND*/0) 595 596 if (ec == ELFCLASS32) 597 INITIALIZE_EHDR(eh32, ec, eh_version); 598 else 599 INITIALIZE_EHDR(eh64, ec, eh_version); 600 601 (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); 602 603 rc += (off_t) _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1); 604 605 if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, (uint64_t) rc, 606 ehdr)) 607 return ((off_t) -1); 608 609 /* 610 * Compute the layout the program header table, if one is 611 * present. The program header table needs to be aligned to a 612 * `natural' boundary. 613 */ 614 if (phnum) { 615 fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum); 616 align = _libelf_falign(ELF_T_PHDR, ec); 617 618 if (e->e_flags & ELF_F_LAYOUT) { 619 /* 620 * Check offsets for sanity. 621 */ 622 if (rc > phoff) { 623 LIBELF_SET_ERROR(LAYOUT, 0); 624 return ((off_t) -1); 625 } 626 627 if (phoff % (off_t) align) { 628 LIBELF_SET_ERROR(LAYOUT, 0); 629 return ((off_t) -1); 630 } 631 632 } else 633 phoff = roundup(rc, (off_t) align); 634 635 rc = phoff + (off_t) fsz; 636 637 phdr = _libelf_getphdr(e, ec); 638 639 if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR, 640 (uint64_t) phoff, fsz, phdr)) 641 return ((off_t) -1); 642 } else 643 phoff = 0; 644 645 /* 646 * Compute the layout of the sections associated with the 647 * file. 648 */ 649 650 if (e->e_cmd != ELF_C_WRITE && 651 (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 && 652 _libelf_load_section_headers(e, ehdr) == 0) 653 return ((off_t) -1); 654 655 if ((rc = _libelf_resync_sections(e, rc, extents)) < 0) 656 return ((off_t) -1); 657 658 /* 659 * Compute the space taken up by the section header table, if 660 * one is needed. 661 * 662 * If ELF_F_LAYOUT has been asserted, the application may have 663 * placed the section header table in between existing 664 * sections, so the net size of the file need not increase due 665 * to the presence of the section header table. 666 * 667 * If the library is responsible for laying out the object, 668 * the section header table is placed after section data. 669 */ 670 if (shnum) { 671 fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, shnum); 672 align = _libelf_falign(ELF_T_SHDR, ec); 673 674 if (e->e_flags & ELF_F_LAYOUT) { 675 if (shoff % (off_t) align) { 676 LIBELF_SET_ERROR(LAYOUT, 0); 677 return ((off_t) -1); 678 } 679 } else 680 shoff = roundup(rc, (off_t) align); 681 682 if (shoff + (off_t) fsz > rc) 683 rc = shoff + (off_t) fsz; 684 685 if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR, 686 (uint64_t) shoff, fsz, NULL)) 687 return ((off_t) -1); 688 } else 689 shoff = 0; 690 691 /* 692 * Set the fields of the Executable Header that could potentially use 693 * extended numbering. 694 */ 695 _libelf_setphnum(e, ehdr, ec, phnum); 696 _libelf_setshnum(e, ehdr, ec, shnum); 697 698 /* 699 * Update the `e_phoff' and `e_shoff' fields if the library is 700 * doing the layout. 701 */ 702 if ((e->e_flags & ELF_F_LAYOUT) == 0) { 703 if (ec == ELFCLASS32) { 704 eh32->e_phoff = (uint32_t) phoff; 705 eh32->e_shoff = (uint32_t) shoff; 706 } else { 707 eh64->e_phoff = (uint64_t) phoff; 708 eh64->e_shoff = (uint64_t) shoff; 709 } 710 } 711 712 return (rc); 713 } 714 715 /* 716 * Write out the contents of an ELF section. 717 */ 718 719 static off_t 720 _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 721 { 722 int ec; 723 off_t rc; 724 Elf_Scn *s; 725 int elftype; 726 Elf_Data *d, dst; 727 uint32_t sh_type; 728 struct _Libelf_Data *ld; 729 uint64_t sh_off, sh_size; 730 size_t fsz, msz, nobjects; 731 732 assert(ex->ex_type == ELF_EXTENT_SECTION); 733 734 s = ex->ex_desc; 735 rc = (off_t) ex->ex_start; 736 737 if ((ec = e->e_class) == ELFCLASS32) { 738 sh_type = s->s_shdr.s_shdr32.sh_type; 739 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; 740 } else { 741 sh_type = s->s_shdr.s_shdr64.sh_type; 742 sh_size = s->s_shdr.s_shdr64.sh_size; 743 } 744 745 /* 746 * Ignore sections that do not allocate space in the file. 747 */ 748 if (sh_type == SHT_NOBITS || sh_type == SHT_NULL || sh_size == 0) 749 return (rc); 750 751 elftype = _libelf_xlate_shtype(sh_type); 752 assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST); 753 754 sh_off = s->s_offset; 755 assert(sh_off % _libelf_falign(elftype, ec) == 0); 756 757 /* 758 * If the section has a `rawdata' descriptor, and the section 759 * contents have not been modified, use its contents directly. 760 * The `s_rawoff' member contains the offset into the original 761 * file, while `s_offset' contains its new location in the 762 * destination. 763 */ 764 765 if (STAILQ_EMPTY(&s->s_data)) { 766 767 if ((d = elf_rawdata(s, NULL)) == NULL) 768 return ((off_t) -1); 769 770 STAILQ_FOREACH(ld, &s->s_rawdata, d_next) { 771 772 d = &ld->d_data; 773 774 if ((uint64_t) rc < sh_off + d->d_off) 775 (void) memset(nf + rc, 776 LIBELF_PRIVATE(fillchar), 777 (size_t) (sh_off + d->d_off - 778 (uint64_t) rc)); 779 rc = (off_t) (sh_off + d->d_off); 780 781 assert(d->d_buf != NULL); 782 assert(d->d_type == ELF_T_BYTE); 783 assert(d->d_version == e->e_version); 784 785 (void) memcpy(nf + rc, 786 e->e_rawfile + s->s_rawoff + d->d_off, 787 (size_t) d->d_size); 788 789 rc += (off_t) d->d_size; 790 } 791 792 return (rc); 793 } 794 795 /* 796 * Iterate over the set of data descriptors for this section. 797 * The prior call to _libelf_resync_elf() would have setup the 798 * descriptors for this step. 799 */ 800 801 dst.d_version = e->e_version; 802 803 STAILQ_FOREACH(ld, &s->s_data, d_next) { 804 805 d = &ld->d_data; 806 807 msz = _libelf_msize(d->d_type, ec, e->e_version); 808 809 if ((uint64_t) rc < sh_off + d->d_off) 810 (void) memset(nf + rc, 811 LIBELF_PRIVATE(fillchar), 812 (size_t) (sh_off + d->d_off - (uint64_t) rc)); 813 814 rc = (off_t) (sh_off + d->d_off); 815 816 assert(d->d_buf != NULL); 817 assert(d->d_version == e->e_version); 818 assert(d->d_size % msz == 0); 819 820 nobjects = (size_t) (d->d_size / msz); 821 822 fsz = _libelf_fsize(d->d_type, ec, e->e_version, nobjects); 823 824 dst.d_buf = nf + rc; 825 dst.d_size = fsz; 826 827 if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) == 828 NULL) 829 return ((off_t) -1); 830 831 rc += (off_t) fsz; 832 } 833 834 return (rc); 835 } 836 837 /* 838 * Write out an ELF Executable Header. 839 */ 840 841 static off_t 842 _libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 843 { 844 int ec; 845 void *ehdr; 846 size_t fsz, msz; 847 Elf_Data dst, src; 848 849 assert(ex->ex_type == ELF_EXTENT_EHDR); 850 assert(ex->ex_start == 0); /* Ehdr always comes first. */ 851 852 ec = e->e_class; 853 854 ehdr = _libelf_ehdr(e, ec, 0); 855 assert(ehdr != NULL); 856 857 fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); 858 msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version); 859 860 (void) memset(&dst, 0, sizeof(dst)); 861 (void) memset(&src, 0, sizeof(src)); 862 863 src.d_buf = ehdr; 864 src.d_size = msz; 865 src.d_type = ELF_T_EHDR; 866 src.d_version = dst.d_version = e->e_version; 867 868 dst.d_buf = nf; 869 dst.d_size = fsz; 870 871 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == 872 NULL) 873 return ((off_t) -1); 874 875 return ((off_t) fsz); 876 } 877 878 /* 879 * Write out an ELF program header table. 880 */ 881 882 static off_t 883 _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 884 { 885 int ec; 886 void *ehdr; 887 Elf32_Ehdr *eh32; 888 Elf64_Ehdr *eh64; 889 Elf_Data dst, src; 890 size_t fsz, phnum; 891 uint64_t phoff; 892 893 assert(ex->ex_type == ELF_EXTENT_PHDR); 894 895 ec = e->e_class; 896 ehdr = _libelf_ehdr(e, ec, 0); 897 phnum = e->e_u.e_elf.e_nphdr; 898 899 assert(phnum > 0); 900 901 if (ec == ELFCLASS32) { 902 eh32 = (Elf32_Ehdr *) ehdr; 903 phoff = (uint64_t) eh32->e_phoff; 904 } else { 905 eh64 = (Elf64_Ehdr *) ehdr; 906 phoff = eh64->e_phoff; 907 } 908 909 assert(phoff > 0); 910 assert(ex->ex_start == phoff); 911 assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0); 912 913 (void) memset(&dst, 0, sizeof(dst)); 914 (void) memset(&src, 0, sizeof(src)); 915 916 fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum); 917 assert(fsz > 0); 918 919 src.d_buf = _libelf_getphdr(e, ec); 920 src.d_version = dst.d_version = e->e_version; 921 src.d_type = ELF_T_PHDR; 922 src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec, 923 e->e_version); 924 925 dst.d_size = fsz; 926 dst.d_buf = nf + ex->ex_start; 927 928 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == 929 NULL) 930 return ((off_t) -1); 931 932 return ((off_t) (phoff + fsz)); 933 } 934 935 /* 936 * Write out an ELF section header table. 937 */ 938 939 static off_t 940 _libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 941 { 942 int ec; 943 void *ehdr; 944 Elf_Scn *scn; 945 uint64_t shoff; 946 Elf32_Ehdr *eh32; 947 Elf64_Ehdr *eh64; 948 size_t fsz, nscn; 949 Elf_Data dst, src; 950 951 assert(ex->ex_type == ELF_EXTENT_SHDR); 952 953 ec = e->e_class; 954 ehdr = _libelf_ehdr(e, ec, 0); 955 nscn = e->e_u.e_elf.e_nscn; 956 957 if (ec == ELFCLASS32) { 958 eh32 = (Elf32_Ehdr *) ehdr; 959 shoff = (uint64_t) eh32->e_shoff; 960 } else { 961 eh64 = (Elf64_Ehdr *) ehdr; 962 shoff = eh64->e_shoff; 963 } 964 965 assert(nscn > 0); 966 assert(shoff % _libelf_falign(ELF_T_SHDR, ec) == 0); 967 assert(ex->ex_start == shoff); 968 969 (void) memset(&dst, 0, sizeof(dst)); 970 (void) memset(&src, 0, sizeof(src)); 971 972 src.d_type = ELF_T_SHDR; 973 src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version); 974 src.d_version = dst.d_version = e->e_version; 975 976 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1); 977 978 STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) { 979 if (ec == ELFCLASS32) 980 src.d_buf = &scn->s_shdr.s_shdr32; 981 else 982 src.d_buf = &scn->s_shdr.s_shdr64; 983 984 dst.d_size = fsz; 985 dst.d_buf = nf + ex->ex_start + scn->s_ndx * fsz; 986 987 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, 988 ELF_TOFILE) == NULL) 989 return ((off_t) -1); 990 } 991 992 return ((off_t) (ex->ex_start + nscn * fsz)); 993 } 994 995 /* 996 * Write out the file image. 997 * 998 * The original file could have been mapped in with an ELF_C_RDWR 999 * command and the application could have added new content or 1000 * re-arranged its sections before calling elf_update(). Consequently 1001 * its not safe to work `in place' on the original file. So we 1002 * malloc() the required space for the updated ELF object and build 1003 * the object there and write it out to the underlying file at the 1004 * end. Note that the application may have opened the underlying file 1005 * in ELF_C_RDWR and only retrieved/modified a few sections. We take 1006 * care to avoid translating file sections unnecessarily. 1007 * 1008 * Gaps in the coverage of the file by the file's sections will be 1009 * filled with the fill character set by elf_fill(3). 1010 */ 1011 1012 static off_t 1013 _libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) 1014 { 1015 off_t nrc, rc; 1016 Elf_Scn *scn, *tscn; 1017 struct _Elf_Extent *ex; 1018 unsigned char *newfile; 1019 1020 assert(e->e_kind == ELF_K_ELF); 1021 assert(e->e_cmd == ELF_C_RDWR || e->e_cmd == ELF_C_WRITE); 1022 assert(e->e_fd >= 0); 1023 1024 if ((newfile = malloc((size_t) newsize)) == NULL) { 1025 LIBELF_SET_ERROR(RESOURCE, errno); 1026 return ((off_t) -1); 1027 } 1028 1029 nrc = rc = 0; 1030 SLIST_FOREACH(ex, extents, ex_next) { 1031 1032 /* Fill inter-extent gaps. */ 1033 if (ex->ex_start > (size_t) rc) 1034 (void) memset(newfile + rc, LIBELF_PRIVATE(fillchar), 1035 (size_t) (ex->ex_start - (uint64_t) rc)); 1036 1037 switch (ex->ex_type) { 1038 case ELF_EXTENT_EHDR: 1039 if ((nrc = _libelf_write_ehdr(e, newfile, ex)) < 0) 1040 goto error; 1041 break; 1042 1043 case ELF_EXTENT_PHDR: 1044 if ((nrc = _libelf_write_phdr(e, newfile, ex)) < 0) 1045 goto error; 1046 break; 1047 1048 case ELF_EXTENT_SECTION: 1049 if ((nrc = _libelf_write_scn(e, newfile, ex)) < 0) 1050 goto error; 1051 break; 1052 1053 case ELF_EXTENT_SHDR: 1054 if ((nrc = _libelf_write_shdr(e, newfile, ex)) < 0) 1055 goto error; 1056 break; 1057 1058 default: 1059 assert(0); 1060 break; 1061 } 1062 1063 assert(ex->ex_start + ex->ex_size == (size_t) nrc); 1064 assert(rc < nrc); 1065 1066 rc = nrc; 1067 } 1068 1069 assert(rc == newsize); 1070 1071 /* 1072 * For regular files, throw away existing file content and 1073 * unmap any existing mappings. 1074 */ 1075 if ((e->e_flags & LIBELF_F_SPECIAL_FILE) == 0) { 1076 if (ftruncate(e->e_fd, (off_t) 0) < 0 || 1077 lseek(e->e_fd, (off_t) 0, SEEK_SET)) { 1078 LIBELF_SET_ERROR(IO, errno); 1079 goto error; 1080 } 1081 #if ELFTC_HAVE_MMAP 1082 if (e->e_flags & LIBELF_F_RAWFILE_MMAP) { 1083 assert(e->e_rawfile != NULL); 1084 assert(e->e_cmd == ELF_C_RDWR); 1085 if (munmap(e->e_rawfile, e->e_rawsize) < 0) { 1086 LIBELF_SET_ERROR(IO, errno); 1087 goto error; 1088 } 1089 } 1090 #endif 1091 } 1092 1093 /* 1094 * Write out the new contents. 1095 */ 1096 if (write(e->e_fd, newfile, (size_t) newsize) != newsize) { 1097 LIBELF_SET_ERROR(IO, errno); 1098 goto error; 1099 } 1100 1101 /* 1102 * For files opened in ELF_C_RDWR mode, set up the new 'raw' 1103 * contents. 1104 */ 1105 if (e->e_cmd == ELF_C_RDWR) { 1106 assert(e->e_rawfile != NULL); 1107 assert((e->e_flags & LIBELF_F_RAWFILE_MALLOC) || 1108 (e->e_flags & LIBELF_F_RAWFILE_MMAP)); 1109 if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) { 1110 free(e->e_rawfile); 1111 e->e_rawfile = newfile; 1112 newfile = NULL; 1113 } 1114 #if ELFTC_HAVE_MMAP 1115 else if (e->e_flags & LIBELF_F_RAWFILE_MMAP) { 1116 if ((e->e_rawfile = mmap(NULL, (size_t) newsize, 1117 PROT_READ, MAP_PRIVATE, e->e_fd, (off_t) 0)) == 1118 MAP_FAILED) { 1119 LIBELF_SET_ERROR(IO, errno); 1120 goto error; 1121 } 1122 } 1123 #endif /* ELFTC_HAVE_MMAP */ 1124 1125 /* Record the new size of the file. */ 1126 e->e_rawsize = (size_t) newsize; 1127 } else { 1128 /* File opened in ELF_C_WRITE mode. */ 1129 assert(e->e_rawfile == NULL); 1130 } 1131 1132 /* 1133 * Reset flags, remove existing section descriptors and 1134 * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr() 1135 * and elf_getscn() will function correctly. 1136 */ 1137 1138 e->e_flags &= ~ELF_F_DIRTY; 1139 1140 STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn) 1141 _libelf_release_scn(scn); 1142 1143 if (e->e_class == ELFCLASS32) { 1144 free(e->e_u.e_elf.e_ehdr.e_ehdr32); 1145 if (e->e_u.e_elf.e_phdr.e_phdr32) 1146 free(e->e_u.e_elf.e_phdr.e_phdr32); 1147 1148 e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL; 1149 e->e_u.e_elf.e_phdr.e_phdr32 = NULL; 1150 } else { 1151 free(e->e_u.e_elf.e_ehdr.e_ehdr64); 1152 if (e->e_u.e_elf.e_phdr.e_phdr64) 1153 free(e->e_u.e_elf.e_phdr.e_phdr64); 1154 1155 e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL; 1156 e->e_u.e_elf.e_phdr.e_phdr64 = NULL; 1157 } 1158 1159 /* Free the temporary buffer. */ 1160 if (newfile) 1161 free(newfile); 1162 1163 return (rc); 1164 1165 error: 1166 free(newfile); 1167 1168 return ((off_t) -1); 1169 } 1170 1171 /* 1172 * Update an ELF object. 1173 */ 1174 1175 off_t 1176 elf_update(Elf *e, Elf_Cmd c) 1177 { 1178 int ec; 1179 off_t rc; 1180 struct _Elf_Extent_List extents; 1181 1182 rc = (off_t) -1; 1183 1184 if (e == NULL || e->e_kind != ELF_K_ELF || 1185 (c != ELF_C_NULL && c != ELF_C_WRITE)) { 1186 LIBELF_SET_ERROR(ARGUMENT, 0); 1187 return (rc); 1188 } 1189 1190 if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) { 1191 LIBELF_SET_ERROR(CLASS, 0); 1192 return (rc); 1193 } 1194 1195 if (e->e_version == EV_NONE) 1196 e->e_version = EV_CURRENT; 1197 1198 if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) { 1199 LIBELF_SET_ERROR(MODE, 0); 1200 return (rc); 1201 } 1202 1203 SLIST_INIT(&extents); 1204 1205 if ((rc = _libelf_resync_elf(e, &extents)) < 0) 1206 goto done; 1207 1208 if (c == ELF_C_NULL) 1209 goto done; 1210 1211 if (e->e_fd < 0) { 1212 rc = (off_t) -1; 1213 LIBELF_SET_ERROR(SEQUENCE, 0); 1214 goto done; 1215 } 1216 1217 rc = _libelf_write_elf(e, rc, &extents); 1218 1219 done: 1220 _libelf_release_extents(&extents); 1221 return (rc); 1222 } 1223