1260684Skaiw/*- 2260684Skaiw * Copyright (c) 2006-2011 Joseph Koshy 3260684Skaiw * All rights reserved. 4260684Skaiw * 5260684Skaiw * Redistribution and use in source and binary forms, with or without 6260684Skaiw * modification, are permitted provided that the following conditions 7260684Skaiw * are met: 8260684Skaiw * 1. Redistributions of source code must retain the above copyright 9260684Skaiw * notice, this list of conditions and the following disclaimer. 10260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright 11260684Skaiw * notice, this list of conditions and the following disclaimer in the 12260684Skaiw * documentation and/or other materials provided with the distribution. 13260684Skaiw * 14260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17260684Skaiw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21260684Skaiw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24260684Skaiw * SUCH DAMAGE. 25260684Skaiw */ 26260684Skaiw 27260684Skaiw#include <sys/param.h> 28260684Skaiw#include <sys/stat.h> 29260684Skaiw 30260684Skaiw#include <assert.h> 31260684Skaiw#include <errno.h> 32260684Skaiw#include <gelf.h> 33260684Skaiw#include <libelf.h> 34260684Skaiw#include <stdlib.h> 35260684Skaiw#include <string.h> 36260684Skaiw#include <unistd.h> 37260684Skaiw 38260684Skaiw#include "_libelf.h" 39260684Skaiw 40260684Skaiw#if ELFTC_HAVE_MMAP 41260684Skaiw#include <sys/mman.h> 42260684Skaiw#endif 43260684Skaiw 44282918SemasteELFTC_VCSID("$Id: elf_update.c 3190 2015-05-04 15:23:08Z jkoshy $"); 45260684Skaiw 46260684Skaiw/* 47260684Skaiw * Layout strategy: 48260684Skaiw * 49260684Skaiw * - Case 1: ELF_F_LAYOUT is asserted 50260684Skaiw * In this case the application has full control over where the 51260684Skaiw * section header table, program header table, and section data 52260684Skaiw * will reside. The library only perform error checks. 53260684Skaiw * 54260684Skaiw * - Case 2: ELF_F_LAYOUT is not asserted 55260684Skaiw * 56260684Skaiw * The library will do the object layout using the following 57260684Skaiw * ordering: 58260684Skaiw * - The executable header is placed first, are required by the 59260684Skaiw * ELF specification. 60260684Skaiw * - The program header table is placed immediately following the 61260684Skaiw * executable header. 62260684Skaiw * - Section data, if any, is placed after the program header 63260684Skaiw * table, aligned appropriately. 64260684Skaiw * - The section header table, if needed, is placed last. 65260684Skaiw * 66260684Skaiw * There are two sub-cases to be taken care of: 67260684Skaiw * 68260684Skaiw * - Case 2a: e->e_cmd == ELF_C_READ or ELF_C_RDWR 69260684Skaiw * 70260684Skaiw * In this sub-case, the underlying ELF object may already have 71260684Skaiw * content in it, which the application may have modified. The 72260684Skaiw * library will retrieve content from the existing object as 73260684Skaiw * needed. 74260684Skaiw * 75260684Skaiw * - Case 2b: e->e_cmd == ELF_C_WRITE 76260684Skaiw * 77260684Skaiw * The ELF object is being created afresh in this sub-case; 78260684Skaiw * there is no pre-existing content in the underlying ELF 79260684Skaiw * object. 80260684Skaiw */ 81260684Skaiw 82260684Skaiw/* 83260684Skaiw * The types of extents in an ELF object. 84260684Skaiw */ 85260684Skaiwenum elf_extent { 86260684Skaiw ELF_EXTENT_EHDR, 87260684Skaiw ELF_EXTENT_PHDR, 88260684Skaiw ELF_EXTENT_SECTION, 89260684Skaiw ELF_EXTENT_SHDR 90260684Skaiw}; 91260684Skaiw 92260684Skaiw/* 93260684Skaiw * A extent descriptor, used when laying out an ELF object. 94260684Skaiw */ 95260684Skaiwstruct _Elf_Extent { 96260684Skaiw SLIST_ENTRY(_Elf_Extent) ex_next; 97260684Skaiw uint64_t ex_start; /* Start of the region. */ 98260684Skaiw uint64_t ex_size; /* The size of the region. */ 99260684Skaiw enum elf_extent ex_type; /* Type of region. */ 100260684Skaiw void *ex_desc; /* Associated descriptor. */ 101260684Skaiw}; 102260684Skaiw 103260684SkaiwSLIST_HEAD(_Elf_Extent_List, _Elf_Extent); 104260684Skaiw 105260684Skaiw/* 106260684Skaiw * Compute the extents of a section, by looking at the data 107260684Skaiw * descriptors associated with it. The function returns 1 108260684Skaiw * if successful, or zero if an error was detected. 109260684Skaiw */ 110260684Skaiwstatic int 111260684Skaiw_libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) 112260684Skaiw{ 113260684Skaiw Elf_Data *d; 114260684Skaiw size_t fsz, msz; 115276371Semaste int ec, elftype; 116260684Skaiw uint32_t sh_type; 117260684Skaiw uint64_t d_align; 118260684Skaiw Elf32_Shdr *shdr32; 119260684Skaiw Elf64_Shdr *shdr64; 120260684Skaiw struct _Libelf_Data *ld; 121260684Skaiw uint64_t scn_size, scn_alignment; 122260684Skaiw uint64_t sh_align, sh_entsize, sh_offset, sh_size; 123260684Skaiw 124260684Skaiw ec = e->e_class; 125260684Skaiw 126260684Skaiw shdr32 = &s->s_shdr.s_shdr32; 127260684Skaiw shdr64 = &s->s_shdr.s_shdr64; 128260684Skaiw if (ec == ELFCLASS32) { 129260684Skaiw sh_type = shdr32->sh_type; 130260684Skaiw sh_align = (uint64_t) shdr32->sh_addralign; 131260684Skaiw sh_entsize = (uint64_t) shdr32->sh_entsize; 132260684Skaiw sh_offset = (uint64_t) shdr32->sh_offset; 133260684Skaiw sh_size = (uint64_t) shdr32->sh_size; 134260684Skaiw } else { 135260684Skaiw sh_type = shdr64->sh_type; 136260684Skaiw sh_align = shdr64->sh_addralign; 137260684Skaiw sh_entsize = shdr64->sh_entsize; 138260684Skaiw sh_offset = shdr64->sh_offset; 139260684Skaiw sh_size = shdr64->sh_size; 140260684Skaiw } 141260684Skaiw 142260684Skaiw assert(sh_type != SHT_NULL && sh_type != SHT_NOBITS); 143260684Skaiw 144260684Skaiw elftype = _libelf_xlate_shtype(sh_type); 145260684Skaiw if (elftype > ELF_T_LAST) { 146260684Skaiw LIBELF_SET_ERROR(SECTION, 0); 147260684Skaiw return (0); 148260684Skaiw } 149260684Skaiw 150260684Skaiw if (sh_align == 0) 151260684Skaiw sh_align = _libelf_falign(elftype, ec); 152260684Skaiw 153260684Skaiw /* 154260684Skaiw * Compute the section's size and alignment using the data 155260684Skaiw * descriptors associated with the section. 156260684Skaiw */ 157260684Skaiw if (STAILQ_EMPTY(&s->s_data)) { 158260684Skaiw /* 159260684Skaiw * The section's content (if any) has not been read in 160260684Skaiw * yet. If section is not dirty marked dirty, we can 161260684Skaiw * reuse the values in the 'sh_size' and 'sh_offset' 162260684Skaiw * fields of the section header. 163260684Skaiw */ 164260684Skaiw if ((s->s_flags & ELF_F_DIRTY) == 0) { 165260684Skaiw /* 166260684Skaiw * If the library is doing the layout, then we 167260684Skaiw * compute the new start offset for the 168260684Skaiw * section based on the current offset and the 169260684Skaiw * section's alignment needs. 170260684Skaiw * 171260684Skaiw * If the application is doing the layout, we 172260684Skaiw * can use the value in the 'sh_offset' field 173260684Skaiw * in the section header directly. 174260684Skaiw */ 175260684Skaiw if (e->e_flags & ELF_F_LAYOUT) 176260684Skaiw goto updatedescriptor; 177260684Skaiw else 178260684Skaiw goto computeoffset; 179260684Skaiw } 180260684Skaiw 181260684Skaiw /* 182260684Skaiw * Otherwise, we need to bring in the section's data 183260684Skaiw * from the underlying ELF object. 184260684Skaiw */ 185260684Skaiw if (e->e_cmd != ELF_C_WRITE && elf_getdata(s, NULL) == NULL) 186260684Skaiw return (0); 187260684Skaiw } 188260684Skaiw 189260684Skaiw /* 190260684Skaiw * Loop through the section's data descriptors. 191260684Skaiw */ 192260684Skaiw scn_size = 0L; 193260684Skaiw scn_alignment = 0; 194260684Skaiw STAILQ_FOREACH(ld, &s->s_data, d_next) { 195260684Skaiw 196260684Skaiw d = &ld->d_data; 197260684Skaiw 198260684Skaiw /* 199260684Skaiw * The data buffer's type is known. 200260684Skaiw */ 201260684Skaiw if (d->d_type >= ELF_T_NUM) { 202260684Skaiw LIBELF_SET_ERROR(DATA, 0); 203260684Skaiw return (0); 204260684Skaiw } 205260684Skaiw 206260684Skaiw /* 207260684Skaiw * The data buffer's version is supported. 208260684Skaiw */ 209260684Skaiw if (d->d_version != e->e_version) { 210260684Skaiw LIBELF_SET_ERROR(VERSION, 0); 211260684Skaiw return (0); 212260684Skaiw } 213260684Skaiw 214260684Skaiw /* 215260684Skaiw * The buffer's alignment is non-zero and a power of 216260684Skaiw * two. 217260684Skaiw */ 218260684Skaiw if ((d_align = d->d_align) == 0 || 219260684Skaiw (d_align & (d_align - 1))) { 220260684Skaiw LIBELF_SET_ERROR(DATA, 0); 221260684Skaiw return (0); 222260684Skaiw } 223260684Skaiw 224260684Skaiw /* 225260684Skaiw * The buffer's size should be a multiple of the 226260684Skaiw * memory size of the underlying type. 227260684Skaiw */ 228260684Skaiw msz = _libelf_msize(d->d_type, ec, e->e_version); 229260684Skaiw if (d->d_size % msz) { 230260684Skaiw LIBELF_SET_ERROR(DATA, 0); 231260684Skaiw return (0); 232260684Skaiw } 233260684Skaiw 234260684Skaiw /* 235260684Skaiw * If the application is controlling layout, then the 236260684Skaiw * d_offset field should be compatible with the 237260684Skaiw * buffer's specified alignment. 238260684Skaiw */ 239260684Skaiw if ((e->e_flags & ELF_F_LAYOUT) && 240260684Skaiw (d->d_off & (d_align - 1))) { 241260684Skaiw LIBELF_SET_ERROR(LAYOUT, 0); 242260684Skaiw return (0); 243260684Skaiw } 244260684Skaiw 245260684Skaiw /* 246260684Skaiw * Compute the section's size. 247260684Skaiw */ 248260684Skaiw if (e->e_flags & ELF_F_LAYOUT) { 249260684Skaiw if ((uint64_t) d->d_off + d->d_size > scn_size) 250260684Skaiw scn_size = d->d_off + d->d_size; 251260684Skaiw } else { 252260684Skaiw scn_size = roundup2(scn_size, d->d_align); 253260684Skaiw d->d_off = scn_size; 254260684Skaiw fsz = _libelf_fsize(d->d_type, ec, d->d_version, 255276371Semaste (size_t) d->d_size / msz); 256260684Skaiw scn_size += fsz; 257260684Skaiw } 258260684Skaiw 259260684Skaiw /* 260260684Skaiw * The section's alignment is the maximum alignment 261260684Skaiw * needed for its data buffers. 262260684Skaiw */ 263260684Skaiw if (d_align > scn_alignment) 264260684Skaiw scn_alignment = d_align; 265260684Skaiw } 266260684Skaiw 267260684Skaiw 268260684Skaiw /* 269260684Skaiw * If the application is requesting full control over the 270260684Skaiw * layout of the section, check the section's specified size, 271260684Skaiw * offsets and alignment for sanity. 272260684Skaiw */ 273260684Skaiw if (e->e_flags & ELF_F_LAYOUT) { 274282918Semaste if (scn_alignment > sh_align || 275282918Semaste sh_offset % sh_align || 276282918Semaste sh_size < scn_size || 277282918Semaste sh_offset % _libelf_falign(elftype, ec)) { 278260684Skaiw LIBELF_SET_ERROR(LAYOUT, 0); 279260684Skaiw return (0); 280260684Skaiw } 281260684Skaiw goto updatedescriptor; 282260684Skaiw } 283260684Skaiw 284260684Skaiw /* 285260684Skaiw * Otherwise, compute the values in the section header. 286260684Skaiw * 287260684Skaiw * The section alignment is the maximum alignment for any of 288260684Skaiw * its contained data descriptors. 289260684Skaiw */ 290260684Skaiw if (scn_alignment > sh_align) 291260684Skaiw sh_align = scn_alignment; 292260684Skaiw 293260684Skaiw /* 294260684Skaiw * If the section entry size is zero, try and fill in an 295260684Skaiw * appropriate entry size. Per the elf(5) manual page 296260684Skaiw * sections without fixed-size entries should have their 297260684Skaiw * 'sh_entsize' field set to zero. 298260684Skaiw */ 299260684Skaiw if (sh_entsize == 0 && 300260684Skaiw (sh_entsize = _libelf_fsize(elftype, ec, e->e_version, 301260684Skaiw (size_t) 1)) == 1) 302260684Skaiw sh_entsize = 0; 303260684Skaiw 304260684Skaiw sh_size = scn_size; 305260684Skaiw 306260684Skaiwcomputeoffset: 307260684Skaiw /* 308260684Skaiw * Compute the new offset for the section based on 309260684Skaiw * the section's alignment needs. 310260684Skaiw */ 311276371Semaste sh_offset = roundup((uint64_t) rc, sh_align); 312260684Skaiw 313260684Skaiw /* 314260684Skaiw * Update the section header. 315260684Skaiw */ 316260684Skaiw if (ec == ELFCLASS32) { 317260684Skaiw shdr32->sh_addralign = (uint32_t) sh_align; 318260684Skaiw shdr32->sh_entsize = (uint32_t) sh_entsize; 319260684Skaiw shdr32->sh_offset = (uint32_t) sh_offset; 320260684Skaiw shdr32->sh_size = (uint32_t) sh_size; 321260684Skaiw } else { 322260684Skaiw shdr64->sh_addralign = sh_align; 323260684Skaiw shdr64->sh_entsize = sh_entsize; 324260684Skaiw shdr64->sh_offset = sh_offset; 325260684Skaiw shdr64->sh_size = sh_size; 326260684Skaiw } 327260684Skaiw 328260684Skaiwupdatedescriptor: 329260684Skaiw /* 330260684Skaiw * Update the section descriptor. 331260684Skaiw */ 332260684Skaiw s->s_size = sh_size; 333260684Skaiw s->s_offset = sh_offset; 334260684Skaiw 335260684Skaiw return (1); 336260684Skaiw} 337260684Skaiw 338260684Skaiw/* 339260684Skaiw * Free a list of extent descriptors. 340260684Skaiw */ 341260684Skaiw 342260684Skaiwstatic void 343260684Skaiw_libelf_release_extents(struct _Elf_Extent_List *extents) 344260684Skaiw{ 345260684Skaiw struct _Elf_Extent *ex; 346260684Skaiw 347260684Skaiw while ((ex = SLIST_FIRST(extents)) != NULL) { 348260684Skaiw SLIST_REMOVE_HEAD(extents, ex_next); 349260684Skaiw free(ex); 350260684Skaiw } 351260684Skaiw} 352260684Skaiw 353260684Skaiw/* 354260684Skaiw * Check if an extent 's' defined by [start..start+size) is free. 355260684Skaiw * This routine assumes that the given extent list is sorted in order 356260684Skaiw * of ascending extent offsets. 357260684Skaiw */ 358260684Skaiw 359260684Skaiwstatic int 360260684Skaiw_libelf_extent_is_unused(struct _Elf_Extent_List *extents, 361260684Skaiw const uint64_t start, const uint64_t size, struct _Elf_Extent **prevt) 362260684Skaiw{ 363260684Skaiw uint64_t tmax, tmin; 364260684Skaiw struct _Elf_Extent *t, *pt; 365260684Skaiw const uint64_t smax = start + size; 366260684Skaiw 367260684Skaiw /* First, look for overlaps with existing extents. */ 368260684Skaiw pt = NULL; 369260684Skaiw SLIST_FOREACH(t, extents, ex_next) { 370260684Skaiw tmin = t->ex_start; 371260684Skaiw tmax = tmin + t->ex_size; 372260684Skaiw 373260684Skaiw if (tmax <= start) { 374260684Skaiw /* 375260684Skaiw * 't' lies entirely before 's': ...| t |...| s |... 376260684Skaiw */ 377260684Skaiw pt = t; 378260684Skaiw continue; 379260684Skaiw } else if (smax <= tmin) { 380260684Skaiw /* 381260684Skaiw * 's' lies entirely before 't', and after 'pt': 382260684Skaiw * ...| pt |...| s |...| t |... 383260684Skaiw */ 384260684Skaiw assert(pt == NULL || 385260684Skaiw pt->ex_start + pt->ex_size <= start); 386260684Skaiw break; 387260684Skaiw } else 388260684Skaiw /* 's' and 't' overlap. */ 389260684Skaiw return (0); 390260684Skaiw } 391260684Skaiw 392260684Skaiw if (prevt) 393260684Skaiw *prevt = pt; 394260684Skaiw return (1); 395260684Skaiw} 396260684Skaiw 397260684Skaiw/* 398260684Skaiw * Insert an extent into the list of extents. 399260684Skaiw */ 400260684Skaiw 401260684Skaiwstatic int 402260684Skaiw_libelf_insert_extent(struct _Elf_Extent_List *extents, int type, 403260684Skaiw uint64_t start, uint64_t size, void *desc) 404260684Skaiw{ 405260684Skaiw struct _Elf_Extent *ex, *prevt; 406260684Skaiw 407260684Skaiw assert(type >= ELF_EXTENT_EHDR && type <= ELF_EXTENT_SHDR); 408260684Skaiw 409260684Skaiw prevt = NULL; 410260684Skaiw 411260684Skaiw /* 412260684Skaiw * If the requested range overlaps with an existing extent, 413260684Skaiw * signal an error. 414260684Skaiw */ 415260684Skaiw if (!_libelf_extent_is_unused(extents, start, size, &prevt)) { 416260684Skaiw LIBELF_SET_ERROR(LAYOUT, 0); 417260684Skaiw return (0); 418260684Skaiw } 419260684Skaiw 420260684Skaiw /* Allocate and fill in a new extent descriptor. */ 421260684Skaiw if ((ex = malloc(sizeof(struct _Elf_Extent))) == NULL) { 422260684Skaiw LIBELF_SET_ERROR(RESOURCE, errno); 423260684Skaiw return (0); 424260684Skaiw } 425260684Skaiw ex->ex_start = start; 426260684Skaiw ex->ex_size = size; 427260684Skaiw ex->ex_desc = desc; 428260684Skaiw ex->ex_type = type; 429260684Skaiw 430260684Skaiw /* Insert the region descriptor into the list. */ 431260684Skaiw if (prevt) 432260684Skaiw SLIST_INSERT_AFTER(prevt, ex, ex_next); 433260684Skaiw else 434260684Skaiw SLIST_INSERT_HEAD(extents, ex, ex_next); 435260684Skaiw return (1); 436260684Skaiw} 437260684Skaiw 438260684Skaiw/* 439260684Skaiw * Recompute section layout. 440260684Skaiw */ 441260684Skaiw 442260684Skaiwstatic off_t 443260684Skaiw_libelf_resync_sections(Elf *e, off_t rc, struct _Elf_Extent_List *extents) 444260684Skaiw{ 445260684Skaiw int ec; 446260684Skaiw Elf_Scn *s; 447260684Skaiw size_t sh_type; 448260684Skaiw 449260684Skaiw ec = e->e_class; 450260684Skaiw 451260684Skaiw /* 452260684Skaiw * Make a pass through sections, computing the extent of each 453260684Skaiw * section. 454260684Skaiw */ 455260684Skaiw STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) { 456260684Skaiw if (ec == ELFCLASS32) 457260684Skaiw sh_type = s->s_shdr.s_shdr32.sh_type; 458260684Skaiw else 459260684Skaiw sh_type = s->s_shdr.s_shdr64.sh_type; 460260684Skaiw 461260684Skaiw if (sh_type == SHT_NOBITS || sh_type == SHT_NULL) 462260684Skaiw continue; 463260684Skaiw 464260684Skaiw if (_libelf_compute_section_extents(e, s, rc) == 0) 465260684Skaiw return ((off_t) -1); 466260684Skaiw 467260684Skaiw if (s->s_size == 0) 468260684Skaiw continue; 469260684Skaiw 470260684Skaiw if (!_libelf_insert_extent(extents, ELF_EXTENT_SECTION, 471260684Skaiw s->s_offset, s->s_size, s)) 472260684Skaiw return ((off_t) -1); 473260684Skaiw 474260684Skaiw if ((size_t) rc < s->s_offset + s->s_size) 475276371Semaste rc = (off_t) (s->s_offset + s->s_size); 476260684Skaiw } 477260684Skaiw 478260684Skaiw return (rc); 479260684Skaiw} 480260684Skaiw 481260684Skaiw/* 482260684Skaiw * Recompute the layout of the ELF object and update the internal data 483260684Skaiw * structures associated with the ELF descriptor. 484260684Skaiw * 485260684Skaiw * Returns the size in bytes the ELF object would occupy in its file 486260684Skaiw * representation. 487260684Skaiw * 488260684Skaiw * After a successful call to this function, the following structures 489260684Skaiw * are updated: 490260684Skaiw * 491260684Skaiw * - The ELF header is updated. 492260684Skaiw * - All extents in the ELF object are sorted in order of ascending 493260684Skaiw * addresses. Sections have their section header table entries 494260684Skaiw * updated. An error is signalled if an overlap was detected among 495260684Skaiw * extents. 496260684Skaiw * - Data descriptors associated with sections are checked for valid 497260684Skaiw * types, offsets and alignment. 498260684Skaiw * 499260684Skaiw * After a resync_elf() successfully returns, the ELF descriptor is 500260684Skaiw * ready for being handed over to _libelf_write_elf(). 501260684Skaiw */ 502260684Skaiw 503260684Skaiwstatic off_t 504260684Skaiw_libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) 505260684Skaiw{ 506260684Skaiw int ec, eh_class; 507260684Skaiw unsigned int eh_byteorder, eh_version; 508260684Skaiw size_t align, fsz; 509260684Skaiw size_t phnum, shnum; 510260684Skaiw off_t rc, phoff, shoff; 511260684Skaiw void *ehdr, *phdr; 512260684Skaiw Elf32_Ehdr *eh32; 513260684Skaiw Elf64_Ehdr *eh64; 514260684Skaiw 515260684Skaiw rc = 0; 516260684Skaiw 517260684Skaiw ec = e->e_class; 518260684Skaiw 519260684Skaiw assert(ec == ELFCLASS32 || ec == ELFCLASS64); 520260684Skaiw 521260684Skaiw /* 522260684Skaiw * Prepare the EHDR. 523260684Skaiw */ 524260684Skaiw if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 525260684Skaiw return ((off_t) -1); 526260684Skaiw 527260684Skaiw eh32 = ehdr; 528260684Skaiw eh64 = ehdr; 529260684Skaiw 530260684Skaiw if (ec == ELFCLASS32) { 531260684Skaiw eh_byteorder = eh32->e_ident[EI_DATA]; 532260684Skaiw eh_class = eh32->e_ident[EI_CLASS]; 533276371Semaste phoff = (off_t) eh32->e_phoff; 534276371Semaste shoff = (off_t) eh32->e_shoff; 535260684Skaiw eh_version = eh32->e_version; 536260684Skaiw } else { 537260684Skaiw eh_byteorder = eh64->e_ident[EI_DATA]; 538260684Skaiw eh_class = eh64->e_ident[EI_CLASS]; 539276371Semaste phoff = (off_t) eh64->e_phoff; 540276371Semaste shoff = (off_t) eh64->e_shoff; 541260684Skaiw eh_version = eh64->e_version; 542260684Skaiw } 543260684Skaiw 544276371Semaste if (phoff < 0 || shoff < 0) { 545276371Semaste LIBELF_SET_ERROR(HEADER, 0); 546276371Semaste return ((off_t) -1); 547276371Semaste } 548276371Semaste 549260684Skaiw if (eh_version == EV_NONE) 550260684Skaiw eh_version = EV_CURRENT; 551260684Skaiw 552260684Skaiw if (eh_version != e->e_version) { /* always EV_CURRENT */ 553260684Skaiw LIBELF_SET_ERROR(VERSION, 0); 554260684Skaiw return ((off_t) -1); 555260684Skaiw } 556260684Skaiw 557260684Skaiw if (eh_class != e->e_class) { 558260684Skaiw LIBELF_SET_ERROR(CLASS, 0); 559260684Skaiw return ((off_t) -1); 560260684Skaiw } 561260684Skaiw 562260684Skaiw if (e->e_cmd != ELF_C_WRITE && eh_byteorder != e->e_byteorder) { 563260684Skaiw LIBELF_SET_ERROR(HEADER, 0); 564260684Skaiw return ((off_t) -1); 565260684Skaiw } 566260684Skaiw 567260684Skaiw shnum = e->e_u.e_elf.e_nscn; 568260684Skaiw phnum = e->e_u.e_elf.e_nphdr; 569260684Skaiw 570260684Skaiw e->e_byteorder = eh_byteorder; 571260684Skaiw 572260684Skaiw#define INITIALIZE_EHDR(E,EC,V) do { \ 573276371Semaste unsigned int _version = (unsigned int) (V); \ 574260684Skaiw (E)->e_ident[EI_MAG0] = ELFMAG0; \ 575260684Skaiw (E)->e_ident[EI_MAG1] = ELFMAG1; \ 576260684Skaiw (E)->e_ident[EI_MAG2] = ELFMAG2; \ 577260684Skaiw (E)->e_ident[EI_MAG3] = ELFMAG3; \ 578276371Semaste (E)->e_ident[EI_CLASS] = (unsigned char) (EC); \ 579276371Semaste (E)->e_ident[EI_VERSION] = (_version & 0xFFU); \ 580276371Semaste (E)->e_ehsize = (uint16_t) _libelf_fsize(ELF_T_EHDR, \ 581276371Semaste (EC), _version, (size_t) 1); \ 582276371Semaste (E)->e_phentsize = (uint16_t) ((phnum == 0) ? 0 : \ 583276371Semaste _libelf_fsize(ELF_T_PHDR, (EC), _version, \ 584276371Semaste (size_t) 1)); \ 585276371Semaste (E)->e_shentsize = (uint16_t) _libelf_fsize(ELF_T_SHDR, \ 586276371Semaste (EC), _version, (size_t) 1); \ 587260684Skaiw } while (0) 588260684Skaiw 589260684Skaiw if (ec == ELFCLASS32) 590260684Skaiw INITIALIZE_EHDR(eh32, ec, eh_version); 591260684Skaiw else 592260684Skaiw INITIALIZE_EHDR(eh64, ec, eh_version); 593260684Skaiw 594260684Skaiw (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); 595260684Skaiw 596276371Semaste rc += (off_t) _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1); 597260684Skaiw 598276371Semaste if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, (uint64_t) rc, 599276371Semaste ehdr)) 600260684Skaiw return ((off_t) -1); 601260684Skaiw 602260684Skaiw /* 603260684Skaiw * Compute the layout the program header table, if one is 604260684Skaiw * present. The program header table needs to be aligned to a 605260684Skaiw * `natural' boundary. 606260684Skaiw */ 607260684Skaiw if (phnum) { 608260684Skaiw fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum); 609260684Skaiw align = _libelf_falign(ELF_T_PHDR, ec); 610260684Skaiw 611260684Skaiw if (e->e_flags & ELF_F_LAYOUT) { 612260684Skaiw /* 613260684Skaiw * Check offsets for sanity. 614260684Skaiw */ 615260684Skaiw if (rc > phoff) { 616260684Skaiw LIBELF_SET_ERROR(LAYOUT, 0); 617260684Skaiw return ((off_t) -1); 618260684Skaiw } 619260684Skaiw 620276371Semaste if (phoff % (off_t) align) { 621260684Skaiw LIBELF_SET_ERROR(LAYOUT, 0); 622260684Skaiw return ((off_t) -1); 623260684Skaiw } 624260684Skaiw 625260684Skaiw } else 626276371Semaste phoff = roundup(rc, (off_t) align); 627260684Skaiw 628276371Semaste rc = phoff + (off_t) fsz; 629260684Skaiw 630260684Skaiw phdr = _libelf_getphdr(e, ec); 631260684Skaiw 632276371Semaste if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR, 633276371Semaste (uint64_t) phoff, fsz, phdr)) 634260684Skaiw return ((off_t) -1); 635260684Skaiw } else 636260684Skaiw phoff = 0; 637260684Skaiw 638260684Skaiw /* 639260684Skaiw * Compute the layout of the sections associated with the 640260684Skaiw * file. 641260684Skaiw */ 642260684Skaiw 643260684Skaiw if (e->e_cmd != ELF_C_WRITE && 644260684Skaiw (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 && 645260684Skaiw _libelf_load_section_headers(e, ehdr) == 0) 646260684Skaiw return ((off_t) -1); 647260684Skaiw 648260684Skaiw if ((rc = _libelf_resync_sections(e, rc, extents)) < 0) 649260684Skaiw return ((off_t) -1); 650260684Skaiw 651260684Skaiw /* 652260684Skaiw * Compute the space taken up by the section header table, if 653260684Skaiw * one is needed. 654260684Skaiw * 655260684Skaiw * If ELF_F_LAYOUT has been asserted, the application may have 656260684Skaiw * placed the section header table in between existing 657260684Skaiw * sections, so the net size of the file need not increase due 658260684Skaiw * to the presence of the section header table. 659260684Skaiw * 660260684Skaiw * If the library is responsible for laying out the object, 661260684Skaiw * the section header table is placed after section data. 662260684Skaiw */ 663260684Skaiw if (shnum) { 664260684Skaiw fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, shnum); 665260684Skaiw align = _libelf_falign(ELF_T_SHDR, ec); 666260684Skaiw 667260684Skaiw if (e->e_flags & ELF_F_LAYOUT) { 668276371Semaste if (shoff % (off_t) align) { 669260684Skaiw LIBELF_SET_ERROR(LAYOUT, 0); 670260684Skaiw return ((off_t) -1); 671260684Skaiw } 672260684Skaiw } else 673276371Semaste shoff = roundup(rc, (off_t) align); 674260684Skaiw 675276371Semaste if (shoff + (off_t) fsz > rc) 676276371Semaste rc = shoff + (off_t) fsz; 677260684Skaiw 678276371Semaste if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR, 679276371Semaste (uint64_t) shoff, fsz, NULL)) 680260684Skaiw return ((off_t) -1); 681260684Skaiw } else 682260684Skaiw shoff = 0; 683260684Skaiw 684260684Skaiw /* 685260684Skaiw * Set the fields of the Executable Header that could potentially use 686260684Skaiw * extended numbering. 687260684Skaiw */ 688260684Skaiw _libelf_setphnum(e, ehdr, ec, phnum); 689260684Skaiw _libelf_setshnum(e, ehdr, ec, shnum); 690260684Skaiw 691260684Skaiw /* 692260684Skaiw * Update the `e_phoff' and `e_shoff' fields if the library is 693260684Skaiw * doing the layout. 694260684Skaiw */ 695260684Skaiw if ((e->e_flags & ELF_F_LAYOUT) == 0) { 696260684Skaiw if (ec == ELFCLASS32) { 697260684Skaiw eh32->e_phoff = (uint32_t) phoff; 698260684Skaiw eh32->e_shoff = (uint32_t) shoff; 699260684Skaiw } else { 700260684Skaiw eh64->e_phoff = (uint64_t) phoff; 701260684Skaiw eh64->e_shoff = (uint64_t) shoff; 702260684Skaiw } 703260684Skaiw } 704260684Skaiw 705260684Skaiw return (rc); 706260684Skaiw} 707260684Skaiw 708260684Skaiw/* 709260684Skaiw * Write out the contents of an ELF section. 710260684Skaiw */ 711260684Skaiw 712276371Semastestatic off_t 713276371Semaste_libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 714260684Skaiw{ 715260684Skaiw int ec; 716276371Semaste off_t rc; 717260684Skaiw Elf_Scn *s; 718260684Skaiw int elftype; 719260684Skaiw Elf_Data *d, dst; 720260684Skaiw uint32_t sh_type; 721260684Skaiw struct _Libelf_Data *ld; 722260684Skaiw uint64_t sh_off, sh_size; 723276371Semaste size_t fsz, msz, nobjects; 724260684Skaiw 725260684Skaiw assert(ex->ex_type == ELF_EXTENT_SECTION); 726260684Skaiw 727260684Skaiw s = ex->ex_desc; 728276371Semaste rc = (off_t) ex->ex_start; 729260684Skaiw 730260684Skaiw if ((ec = e->e_class) == ELFCLASS32) { 731260684Skaiw sh_type = s->s_shdr.s_shdr32.sh_type; 732260684Skaiw sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; 733260684Skaiw } else { 734260684Skaiw sh_type = s->s_shdr.s_shdr64.sh_type; 735260684Skaiw sh_size = s->s_shdr.s_shdr64.sh_size; 736260684Skaiw } 737260684Skaiw 738260684Skaiw /* 739260684Skaiw * Ignore sections that do not allocate space in the file. 740260684Skaiw */ 741260684Skaiw if (sh_type == SHT_NOBITS || sh_type == SHT_NULL || sh_size == 0) 742260684Skaiw return (rc); 743260684Skaiw 744260684Skaiw elftype = _libelf_xlate_shtype(sh_type); 745260684Skaiw assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST); 746260684Skaiw 747260684Skaiw sh_off = s->s_offset; 748260684Skaiw assert(sh_off % _libelf_falign(elftype, ec) == 0); 749260684Skaiw 750260684Skaiw /* 751260684Skaiw * If the section has a `rawdata' descriptor, and the section 752260684Skaiw * contents have not been modified, use its contents directly. 753260684Skaiw * The `s_rawoff' member contains the offset into the original 754260684Skaiw * file, while `s_offset' contains its new location in the 755260684Skaiw * destination. 756260684Skaiw */ 757260684Skaiw 758260684Skaiw if (STAILQ_EMPTY(&s->s_data)) { 759260684Skaiw 760260684Skaiw if ((d = elf_rawdata(s, NULL)) == NULL) 761260684Skaiw return ((off_t) -1); 762260684Skaiw 763260684Skaiw STAILQ_FOREACH(ld, &s->s_rawdata, d_next) { 764260684Skaiw 765260684Skaiw d = &ld->d_data; 766260684Skaiw 767260684Skaiw if ((uint64_t) rc < sh_off + d->d_off) 768260684Skaiw (void) memset(nf + rc, 769276371Semaste LIBELF_PRIVATE(fillchar), 770276371Semaste (size_t) (sh_off + d->d_off - 771276371Semaste (uint64_t) rc)); 772276371Semaste rc = (off_t) (sh_off + d->d_off); 773260684Skaiw 774260684Skaiw assert(d->d_buf != NULL); 775260684Skaiw assert(d->d_type == ELF_T_BYTE); 776260684Skaiw assert(d->d_version == e->e_version); 777260684Skaiw 778260684Skaiw (void) memcpy(nf + rc, 779276371Semaste e->e_rawfile + s->s_rawoff + d->d_off, 780276371Semaste (size_t) d->d_size); 781260684Skaiw 782276371Semaste rc += (off_t) d->d_size; 783260684Skaiw } 784260684Skaiw 785260684Skaiw return (rc); 786260684Skaiw } 787260684Skaiw 788260684Skaiw /* 789260684Skaiw * Iterate over the set of data descriptors for this section. 790260684Skaiw * The prior call to _libelf_resync_elf() would have setup the 791260684Skaiw * descriptors for this step. 792260684Skaiw */ 793260684Skaiw 794260684Skaiw dst.d_version = e->e_version; 795260684Skaiw 796260684Skaiw STAILQ_FOREACH(ld, &s->s_data, d_next) { 797260684Skaiw 798260684Skaiw d = &ld->d_data; 799260684Skaiw 800260684Skaiw msz = _libelf_msize(d->d_type, ec, e->e_version); 801260684Skaiw 802260684Skaiw if ((uint64_t) rc < sh_off + d->d_off) 803260684Skaiw (void) memset(nf + rc, 804276371Semaste LIBELF_PRIVATE(fillchar), 805276371Semaste (size_t) (sh_off + d->d_off - (uint64_t) rc)); 806260684Skaiw 807276371Semaste rc = (off_t) (sh_off + d->d_off); 808260684Skaiw 809260684Skaiw assert(d->d_buf != NULL); 810260684Skaiw assert(d->d_version == e->e_version); 811260684Skaiw assert(d->d_size % msz == 0); 812260684Skaiw 813276371Semaste nobjects = (size_t) (d->d_size / msz); 814260684Skaiw 815260684Skaiw fsz = _libelf_fsize(d->d_type, ec, e->e_version, nobjects); 816260684Skaiw 817260684Skaiw dst.d_buf = nf + rc; 818260684Skaiw dst.d_size = fsz; 819260684Skaiw 820260684Skaiw if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) == 821260684Skaiw NULL) 822260684Skaiw return ((off_t) -1); 823260684Skaiw 824276371Semaste rc += (off_t) fsz; 825260684Skaiw } 826260684Skaiw 827276371Semaste return (rc); 828260684Skaiw} 829260684Skaiw 830260684Skaiw/* 831260684Skaiw * Write out an ELF Executable Header. 832260684Skaiw */ 833260684Skaiw 834260684Skaiwstatic off_t 835276371Semaste_libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 836260684Skaiw{ 837260684Skaiw int ec; 838260684Skaiw void *ehdr; 839260684Skaiw size_t fsz, msz; 840260684Skaiw Elf_Data dst, src; 841260684Skaiw 842260684Skaiw assert(ex->ex_type == ELF_EXTENT_EHDR); 843260684Skaiw assert(ex->ex_start == 0); /* Ehdr always comes first. */ 844260684Skaiw 845260684Skaiw ec = e->e_class; 846260684Skaiw 847260684Skaiw ehdr = _libelf_ehdr(e, ec, 0); 848260684Skaiw assert(ehdr != NULL); 849260684Skaiw 850260684Skaiw fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); 851260684Skaiw msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version); 852260684Skaiw 853260684Skaiw (void) memset(&dst, 0, sizeof(dst)); 854260684Skaiw (void) memset(&src, 0, sizeof(src)); 855260684Skaiw 856260684Skaiw src.d_buf = ehdr; 857260684Skaiw src.d_size = msz; 858260684Skaiw src.d_type = ELF_T_EHDR; 859260684Skaiw src.d_version = dst.d_version = e->e_version; 860260684Skaiw 861260684Skaiw dst.d_buf = nf; 862260684Skaiw dst.d_size = fsz; 863260684Skaiw 864260684Skaiw if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == 865260684Skaiw NULL) 866260684Skaiw return ((off_t) -1); 867260684Skaiw 868260684Skaiw return ((off_t) fsz); 869260684Skaiw} 870260684Skaiw 871260684Skaiw/* 872260684Skaiw * Write out an ELF program header table. 873260684Skaiw */ 874260684Skaiw 875260684Skaiwstatic off_t 876276371Semaste_libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 877260684Skaiw{ 878260684Skaiw int ec; 879260684Skaiw void *ehdr; 880260684Skaiw Elf32_Ehdr *eh32; 881260684Skaiw Elf64_Ehdr *eh64; 882260684Skaiw Elf_Data dst, src; 883260684Skaiw size_t fsz, phnum; 884260684Skaiw uint64_t phoff; 885260684Skaiw 886260684Skaiw assert(ex->ex_type == ELF_EXTENT_PHDR); 887260684Skaiw 888260684Skaiw ec = e->e_class; 889260684Skaiw ehdr = _libelf_ehdr(e, ec, 0); 890260684Skaiw phnum = e->e_u.e_elf.e_nphdr; 891260684Skaiw 892260684Skaiw assert(phnum > 0); 893260684Skaiw 894260684Skaiw if (ec == ELFCLASS32) { 895260684Skaiw eh32 = (Elf32_Ehdr *) ehdr; 896260684Skaiw phoff = (uint64_t) eh32->e_phoff; 897260684Skaiw } else { 898260684Skaiw eh64 = (Elf64_Ehdr *) ehdr; 899260684Skaiw phoff = eh64->e_phoff; 900260684Skaiw } 901260684Skaiw 902260684Skaiw assert(phoff > 0); 903260684Skaiw assert(ex->ex_start == phoff); 904260684Skaiw assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0); 905260684Skaiw 906260684Skaiw (void) memset(&dst, 0, sizeof(dst)); 907260684Skaiw (void) memset(&src, 0, sizeof(src)); 908260684Skaiw 909260684Skaiw fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum); 910260684Skaiw assert(fsz > 0); 911260684Skaiw 912260684Skaiw src.d_buf = _libelf_getphdr(e, ec); 913260684Skaiw src.d_version = dst.d_version = e->e_version; 914260684Skaiw src.d_type = ELF_T_PHDR; 915260684Skaiw src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec, 916260684Skaiw e->e_version); 917260684Skaiw 918260684Skaiw dst.d_size = fsz; 919260684Skaiw dst.d_buf = nf + ex->ex_start; 920260684Skaiw 921260684Skaiw if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == 922260684Skaiw NULL) 923260684Skaiw return ((off_t) -1); 924260684Skaiw 925276371Semaste return ((off_t) (phoff + fsz)); 926260684Skaiw} 927260684Skaiw 928260684Skaiw/* 929260684Skaiw * Write out an ELF section header table. 930260684Skaiw */ 931260684Skaiw 932260684Skaiwstatic off_t 933276371Semaste_libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) 934260684Skaiw{ 935260684Skaiw int ec; 936260684Skaiw void *ehdr; 937260684Skaiw Elf_Scn *scn; 938260684Skaiw uint64_t shoff; 939260684Skaiw Elf32_Ehdr *eh32; 940260684Skaiw Elf64_Ehdr *eh64; 941260684Skaiw size_t fsz, nscn; 942260684Skaiw Elf_Data dst, src; 943260684Skaiw 944260684Skaiw assert(ex->ex_type == ELF_EXTENT_SHDR); 945260684Skaiw 946260684Skaiw ec = e->e_class; 947260684Skaiw ehdr = _libelf_ehdr(e, ec, 0); 948260684Skaiw nscn = e->e_u.e_elf.e_nscn; 949260684Skaiw 950260684Skaiw if (ec == ELFCLASS32) { 951260684Skaiw eh32 = (Elf32_Ehdr *) ehdr; 952260684Skaiw shoff = (uint64_t) eh32->e_shoff; 953260684Skaiw } else { 954260684Skaiw eh64 = (Elf64_Ehdr *) ehdr; 955260684Skaiw shoff = eh64->e_shoff; 956260684Skaiw } 957260684Skaiw 958260684Skaiw assert(nscn > 0); 959260684Skaiw assert(shoff % _libelf_falign(ELF_T_SHDR, ec) == 0); 960260684Skaiw assert(ex->ex_start == shoff); 961260684Skaiw 962260684Skaiw (void) memset(&dst, 0, sizeof(dst)); 963260684Skaiw (void) memset(&src, 0, sizeof(src)); 964260684Skaiw 965260684Skaiw src.d_type = ELF_T_SHDR; 966260684Skaiw src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version); 967260684Skaiw src.d_version = dst.d_version = e->e_version; 968260684Skaiw 969260684Skaiw fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1); 970260684Skaiw 971260684Skaiw STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) { 972260684Skaiw if (ec == ELFCLASS32) 973260684Skaiw src.d_buf = &scn->s_shdr.s_shdr32; 974260684Skaiw else 975260684Skaiw src.d_buf = &scn->s_shdr.s_shdr64; 976260684Skaiw 977260684Skaiw dst.d_size = fsz; 978260684Skaiw dst.d_buf = nf + ex->ex_start + scn->s_ndx * fsz; 979260684Skaiw 980260684Skaiw if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, 981260684Skaiw ELF_TOFILE) == NULL) 982260684Skaiw return ((off_t) -1); 983260684Skaiw } 984260684Skaiw 985276371Semaste return ((off_t) (ex->ex_start + nscn * fsz)); 986260684Skaiw} 987260684Skaiw 988260684Skaiw/* 989260684Skaiw * Write out the file image. 990260684Skaiw * 991260684Skaiw * The original file could have been mapped in with an ELF_C_RDWR 992260684Skaiw * command and the application could have added new content or 993260684Skaiw * re-arranged its sections before calling elf_update(). Consequently 994260684Skaiw * its not safe to work `in place' on the original file. So we 995260684Skaiw * malloc() the required space for the updated ELF object and build 996260684Skaiw * the object there and write it out to the underlying file at the 997260684Skaiw * end. Note that the application may have opened the underlying file 998260684Skaiw * in ELF_C_RDWR and only retrieved/modified a few sections. We take 999260684Skaiw * care to avoid translating file sections unnecessarily. 1000260684Skaiw * 1001260684Skaiw * Gaps in the coverage of the file by the file's sections will be 1002260684Skaiw * filled with the fill character set by elf_fill(3). 1003260684Skaiw */ 1004260684Skaiw 1005260684Skaiwstatic off_t 1006260684Skaiw_libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) 1007260684Skaiw{ 1008260684Skaiw off_t nrc, rc; 1009260684Skaiw Elf_Scn *scn, *tscn; 1010260684Skaiw struct _Elf_Extent *ex; 1011276371Semaste unsigned char *newfile; 1012260684Skaiw 1013260684Skaiw assert(e->e_kind == ELF_K_ELF); 1014260684Skaiw assert(e->e_cmd == ELF_C_RDWR || e->e_cmd == ELF_C_WRITE); 1015260684Skaiw assert(e->e_fd >= 0); 1016260684Skaiw 1017260684Skaiw if ((newfile = malloc((size_t) newsize)) == NULL) { 1018260684Skaiw LIBELF_SET_ERROR(RESOURCE, errno); 1019260684Skaiw return ((off_t) -1); 1020260684Skaiw } 1021260684Skaiw 1022260684Skaiw nrc = rc = 0; 1023260684Skaiw SLIST_FOREACH(ex, extents, ex_next) { 1024260684Skaiw 1025260684Skaiw /* Fill inter-extent gaps. */ 1026260684Skaiw if (ex->ex_start > (size_t) rc) 1027260684Skaiw (void) memset(newfile + rc, LIBELF_PRIVATE(fillchar), 1028276371Semaste (size_t) (ex->ex_start - (uint64_t) rc)); 1029260684Skaiw 1030260684Skaiw switch (ex->ex_type) { 1031260684Skaiw case ELF_EXTENT_EHDR: 1032260684Skaiw if ((nrc = _libelf_write_ehdr(e, newfile, ex)) < 0) 1033260684Skaiw goto error; 1034260684Skaiw break; 1035260684Skaiw 1036260684Skaiw case ELF_EXTENT_PHDR: 1037260684Skaiw if ((nrc = _libelf_write_phdr(e, newfile, ex)) < 0) 1038260684Skaiw goto error; 1039260684Skaiw break; 1040260684Skaiw 1041260684Skaiw case ELF_EXTENT_SECTION: 1042260684Skaiw if ((nrc = _libelf_write_scn(e, newfile, ex)) < 0) 1043260684Skaiw goto error; 1044260684Skaiw break; 1045260684Skaiw 1046260684Skaiw case ELF_EXTENT_SHDR: 1047260684Skaiw if ((nrc = _libelf_write_shdr(e, newfile, ex)) < 0) 1048260684Skaiw goto error; 1049260684Skaiw break; 1050260684Skaiw 1051260684Skaiw default: 1052260684Skaiw assert(0); 1053260684Skaiw break; 1054260684Skaiw } 1055260684Skaiw 1056260684Skaiw assert(ex->ex_start + ex->ex_size == (size_t) nrc); 1057260684Skaiw assert(rc < nrc); 1058260684Skaiw 1059260684Skaiw rc = nrc; 1060260684Skaiw } 1061260684Skaiw 1062260684Skaiw assert(rc == newsize); 1063260684Skaiw 1064260684Skaiw /* 1065260684Skaiw * For regular files, throw away existing file content and 1066260684Skaiw * unmap any existing mappings. 1067260684Skaiw */ 1068260684Skaiw if ((e->e_flags & LIBELF_F_SPECIAL_FILE) == 0) { 1069260684Skaiw if (ftruncate(e->e_fd, (off_t) 0) < 0 || 1070260684Skaiw lseek(e->e_fd, (off_t) 0, SEEK_SET)) { 1071260684Skaiw LIBELF_SET_ERROR(IO, errno); 1072260684Skaiw goto error; 1073260684Skaiw } 1074260684Skaiw#if ELFTC_HAVE_MMAP 1075260684Skaiw if (e->e_flags & LIBELF_F_RAWFILE_MMAP) { 1076260684Skaiw assert(e->e_rawfile != NULL); 1077260684Skaiw assert(e->e_cmd == ELF_C_RDWR); 1078260684Skaiw if (munmap(e->e_rawfile, e->e_rawsize) < 0) { 1079260684Skaiw LIBELF_SET_ERROR(IO, errno); 1080260684Skaiw goto error; 1081260684Skaiw } 1082260684Skaiw } 1083260684Skaiw#endif 1084260684Skaiw } 1085260684Skaiw 1086260684Skaiw /* 1087260684Skaiw * Write out the new contents. 1088260684Skaiw */ 1089260684Skaiw if (write(e->e_fd, newfile, (size_t) newsize) != newsize) { 1090260684Skaiw LIBELF_SET_ERROR(IO, errno); 1091260684Skaiw goto error; 1092260684Skaiw } 1093260684Skaiw 1094260684Skaiw /* 1095260684Skaiw * For files opened in ELF_C_RDWR mode, set up the new 'raw' 1096260684Skaiw * contents. 1097260684Skaiw */ 1098260684Skaiw if (e->e_cmd == ELF_C_RDWR) { 1099260684Skaiw assert(e->e_rawfile != NULL); 1100260684Skaiw assert((e->e_flags & LIBELF_F_RAWFILE_MALLOC) || 1101260684Skaiw (e->e_flags & LIBELF_F_RAWFILE_MMAP)); 1102260684Skaiw if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) { 1103260684Skaiw free(e->e_rawfile); 1104260684Skaiw e->e_rawfile = newfile; 1105260684Skaiw newfile = NULL; 1106260684Skaiw } 1107260684Skaiw#if ELFTC_HAVE_MMAP 1108260684Skaiw else if (e->e_flags & LIBELF_F_RAWFILE_MMAP) { 1109260684Skaiw if ((e->e_rawfile = mmap(NULL, (size_t) newsize, 1110260684Skaiw PROT_READ, MAP_PRIVATE, e->e_fd, (off_t) 0)) == 1111260684Skaiw MAP_FAILED) { 1112260684Skaiw LIBELF_SET_ERROR(IO, errno); 1113260684Skaiw goto error; 1114260684Skaiw } 1115260684Skaiw } 1116260684Skaiw#endif /* ELFTC_HAVE_MMAP */ 1117260684Skaiw 1118260684Skaiw /* Record the new size of the file. */ 1119276371Semaste e->e_rawsize = (size_t) newsize; 1120260684Skaiw } else { 1121260684Skaiw /* File opened in ELF_C_WRITE mode. */ 1122260684Skaiw assert(e->e_rawfile == NULL); 1123260684Skaiw } 1124260684Skaiw 1125260684Skaiw /* 1126260684Skaiw * Reset flags, remove existing section descriptors and 1127260684Skaiw * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr() 1128260684Skaiw * and elf_getscn() will function correctly. 1129260684Skaiw */ 1130260684Skaiw 1131260684Skaiw e->e_flags &= ~ELF_F_DIRTY; 1132260684Skaiw 1133260684Skaiw STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn) 1134260684Skaiw _libelf_release_scn(scn); 1135260684Skaiw 1136260684Skaiw if (e->e_class == ELFCLASS32) { 1137260684Skaiw free(e->e_u.e_elf.e_ehdr.e_ehdr32); 1138260684Skaiw if (e->e_u.e_elf.e_phdr.e_phdr32) 1139260684Skaiw free(e->e_u.e_elf.e_phdr.e_phdr32); 1140260684Skaiw 1141260684Skaiw e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL; 1142260684Skaiw e->e_u.e_elf.e_phdr.e_phdr32 = NULL; 1143260684Skaiw } else { 1144260684Skaiw free(e->e_u.e_elf.e_ehdr.e_ehdr64); 1145260684Skaiw if (e->e_u.e_elf.e_phdr.e_phdr64) 1146260684Skaiw free(e->e_u.e_elf.e_phdr.e_phdr64); 1147260684Skaiw 1148260684Skaiw e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL; 1149260684Skaiw e->e_u.e_elf.e_phdr.e_phdr64 = NULL; 1150260684Skaiw } 1151260684Skaiw 1152260684Skaiw /* Free the temporary buffer. */ 1153260684Skaiw if (newfile) 1154260684Skaiw free(newfile); 1155260684Skaiw 1156260684Skaiw return (rc); 1157260684Skaiw 1158260684Skaiw error: 1159260684Skaiw free(newfile); 1160260684Skaiw 1161260684Skaiw return ((off_t) -1); 1162260684Skaiw} 1163260684Skaiw 1164260684Skaiw/* 1165260684Skaiw * Update an ELF object. 1166260684Skaiw */ 1167260684Skaiw 1168260684Skaiwoff_t 1169260684Skaiwelf_update(Elf *e, Elf_Cmd c) 1170260684Skaiw{ 1171260684Skaiw int ec; 1172260684Skaiw off_t rc; 1173260684Skaiw struct _Elf_Extent_List extents; 1174260684Skaiw 1175260684Skaiw rc = (off_t) -1; 1176260684Skaiw 1177260684Skaiw if (e == NULL || e->e_kind != ELF_K_ELF || 1178260684Skaiw (c != ELF_C_NULL && c != ELF_C_WRITE)) { 1179260684Skaiw LIBELF_SET_ERROR(ARGUMENT, 0); 1180260684Skaiw return (rc); 1181260684Skaiw } 1182260684Skaiw 1183260684Skaiw if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) { 1184260684Skaiw LIBELF_SET_ERROR(CLASS, 0); 1185260684Skaiw return (rc); 1186260684Skaiw } 1187260684Skaiw 1188260684Skaiw if (e->e_version == EV_NONE) 1189260684Skaiw e->e_version = EV_CURRENT; 1190260684Skaiw 1191260684Skaiw if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) { 1192260684Skaiw LIBELF_SET_ERROR(MODE, 0); 1193260684Skaiw return (rc); 1194260684Skaiw } 1195260684Skaiw 1196260684Skaiw SLIST_INIT(&extents); 1197260684Skaiw 1198260684Skaiw if ((rc = _libelf_resync_elf(e, &extents)) < 0) 1199260684Skaiw goto done; 1200260684Skaiw 1201260684Skaiw if (c == ELF_C_NULL) 1202260684Skaiw goto done; 1203260684Skaiw 1204260684Skaiw if (e->e_fd < 0) { 1205260684Skaiw rc = (off_t) -1; 1206260684Skaiw LIBELF_SET_ERROR(SEQUENCE, 0); 1207260684Skaiw goto done; 1208260684Skaiw } 1209260684Skaiw 1210260684Skaiw rc = _libelf_write_elf(e, rc, &extents); 1211260684Skaiw 1212260684Skaiwdone: 1213260684Skaiw _libelf_release_extents(&extents); 1214260684Skaiw return (rc); 1215260684Skaiw} 1216