elf_update.c revision 247221
1101099Srwatson/*- 2225344Srwatson * Copyright (c) 2006-2008 Joseph Koshy 3141802Srwatson * All rights reserved. 4172930Srwatson * 5182063Srwatson * Redistribution and use in source and binary forms, with or without 6101099Srwatson * modification, are permitted provided that the following conditions 7101099Srwatson * are met: 8101099Srwatson * 1. Redistributions of source code must retain the above copyright 9101099Srwatson * notice, this list of conditions and the following disclaimer. 10141802Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11141802Srwatson * notice, this list of conditions and the following disclaimer in the 12141802Srwatson * documentation and/or other materials provided with the distribution. 13141802Srwatson * 14101099Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15172930Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16172930Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17172930Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18189529Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19189529Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20189529Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21101099Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22101099Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23101099Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24101099Srwatson * SUCH DAMAGE. 25101099Srwatson */ 26101099Srwatson 27101099Srwatson#include <sys/cdefs.h> 28101099Srwatson__FBSDID("$FreeBSD: head/lib/libelf/elf_update.c 247221 2013-02-24 15:15:50Z markj $"); 29101099Srwatson 30101099Srwatson#include <sys/mman.h> 31101099Srwatson#include <sys/param.h> 32101099Srwatson 33101099Srwatson#include <assert.h> 34101099Srwatson#include <errno.h> 35101099Srwatson#include <gelf.h> 36101099Srwatson#include <libelf.h> 37101099Srwatson#include <stdlib.h> 38101099Srwatson#include <string.h> 39101099Srwatson#include <unistd.h> 40101099Srwatson 41101099Srwatson#include "_libelf.h" 42101099Srwatson 43101099Srwatson/* 44101099Srwatson * Layout strategy: 45101099Srwatson * 46101099Srwatson * - Case 1: ELF_F_LAYOUT is asserted 47168944Srwatson * In this case the application has full control over where the 48168944Srwatson * section header table, program header table, and section data 49168944Srwatson * will reside. The library only perform error checks. 50168944Srwatson * 51168944Srwatson * - Case 2: ELF_F_LAYOUT is not asserted 52101099Srwatson * 53101099Srwatson * The library will do the object layout using the following 54101099Srwatson * ordering: 55101099Srwatson * - The executable header is placed first, are required by the 56131934Smarcel * ELF specification. 57101099Srwatson * - The program header table is placed immediately following the 58164184Strhodes * executable header. 59113534Srwatson * - Section data, if any, is placed after the program header 60166905Srwatson * table, aligned appropriately. 61101099Srwatson * - The section header table, if needed, is placed last. 62166905Srwatson * 63101099Srwatson * There are two sub-cases to be taken care of: 64101099Srwatson * 65166905Srwatson * - Case 2a: e->e_cmd == ELF_C_READ or ELF_C_RDWR 66166905Srwatson * 67101099Srwatson * In this sub-case, the underlying ELF object may already have 68101099Srwatson * content in it, which the application may have modified. The 69150340Sphk * library will retrieve content from the existing object as 70101099Srwatson * needed. 71101099Srwatson * 72101099Srwatson * - Case 2b: e->e_cmd == ELF_C_WRITE 73101099Srwatson * 74101099Srwatson * The ELF object is being created afresh in this sub-case; 75101099Srwatson * there is no pre-existing content in the underlying ELF 76101099Srwatson * object. 77101099Srwatson */ 78101099Srwatson 79165469Srwatson/* 80101099Srwatson * Compute the extents of a section, by looking at the data 81101099Srwatson * descriptors associated with it. The function returns 1 if 82101099Srwatson * successful, or zero if an error was detected. 83227309Sed */ 84101099Srwatsonstatic int 85101099Srwatson_libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) 86168944Srwatson{ 87168944Srwatson int ec; 88168944Srwatson size_t fsz, msz; 89168944Srwatson Elf_Data *d; 90184308Srwatson Elf32_Shdr *shdr32; 91168944Srwatson Elf64_Shdr *shdr64; 92168944Srwatson unsigned int elftype; 93168944Srwatson uint32_t sh_type; 94168944Srwatson uint64_t d_align; 95173112Srwatson uint64_t sh_align, sh_entsize, sh_offset, sh_size; 96168947Srwatson uint64_t scn_size, scn_alignment; 97168947Srwatson 98168947Srwatson ec = e->e_class; 99168947Srwatson 100168944Srwatson shdr32 = &s->s_shdr.s_shdr32; 101168947Srwatson shdr64 = &s->s_shdr.s_shdr64; 102175164Sjhb if (ec == ELFCLASS32) { 103168944Srwatson sh_type = shdr32->sh_type; 104168944Srwatson sh_align = (uint64_t) shdr32->sh_addralign; 105168944Srwatson sh_entsize = (uint64_t) shdr32->sh_entsize; 106168944Srwatson sh_offset = (uint64_t) shdr32->sh_offset; 107101099Srwatson sh_size = (uint64_t) shdr32->sh_size; 108166533Srwatson } else { 109166533Srwatson sh_type = shdr64->sh_type; 110119228Srwatson sh_align = shdr64->sh_addralign; 111101099Srwatson sh_entsize = shdr64->sh_entsize; 112101099Srwatson sh_offset = shdr64->sh_offset; 113101099Srwatson sh_size = shdr64->sh_size; 114101099Srwatson } 115227309Sed 116168944Srwatson assert(sh_type != SHT_NULL && sh_type != SHT_NOBITS); 117101099Srwatson 118168944Srwatson elftype = _libelf_xlate_shtype(sh_type); 119168944Srwatson if (elftype > ELF_T_LAST) { 120168944Srwatson LIBELF_SET_ERROR(SECTION, 0); 121168944Srwatson return (0); 122101099Srwatson } 123168944Srwatson 124101099Srwatson if (sh_align == 0) 125136775Srwatson sh_align = _libelf_falign(elftype, ec); 126174898Srwatson 127136775Srwatson /* 128168944Srwatson * Check the section's data buffers for sanity and compute the 129136775Srwatson * section's alignment. 130136775Srwatson * Compute the section's size and alignment using the data 131168944Srwatson * descriptors associated with the section. 132168944Srwatson */ 133168944Srwatson if (STAILQ_EMPTY(&s->s_data)) { 134168944Srwatson /* 135168944Srwatson * The section's content (if any) has not been read in 136168944Srwatson * yet. If section is not dirty marked dirty, we can 137168944Srwatson * reuse the values in the 'sh_size' and 'sh_offset' 138168947Srwatson * fields of the section header. 139168944Srwatson */ 140168944Srwatson if ((s->s_flags & ELF_F_DIRTY) == 0) { 141168944Srwatson /* 142168944Srwatson * If the library is doing the layout, then we 143168944Srwatson * compute the new start offset for the 144168944Srwatson * section based on the current offset and the 145168944Srwatson * section's alignment needs. 146168944Srwatson * 147168944Srwatson * If the application is doing the layout, we 148168947Srwatson * can use the value in the 'sh_offset' field 149168947Srwatson * in the section header directly. 150168947Srwatson */ 151168947Srwatson if (e->e_flags & ELF_F_LAYOUT) 152168944Srwatson goto updatedescriptor; 153168944Srwatson else 154168944Srwatson goto computeoffset; 155168944Srwatson } 156168944Srwatson 157101099Srwatson /* 158173138Srwatson * Otherwise, we need to bring in the section's data 159173138Srwatson * from the underlying ELF object. 160173138Srwatson */ 161173138Srwatson if (e->e_cmd != ELF_C_WRITE && elf_getdata(s, NULL) == NULL) 162112675Srwatson return (0); 163173141Srwatson } 164173138Srwatson 165122875Srwatson /* 166122875Srwatson * Loop through the section's data descriptors. 167173141Srwatson */ 168173138Srwatson scn_size = 0L; 169173138Srwatson scn_alignment = 0L; 170122875Srwatson STAILQ_FOREACH(d, &s->s_data, d_next) { 171122875Srwatson if (d->d_type > ELF_T_LAST) { 172122875Srwatson LIBELF_SET_ERROR(DATA, 0); 173122875Srwatson return (0); 174173138Srwatson } 175140635Srwatson if (d->d_version != e->e_version) { 176173141Srwatson LIBELF_SET_ERROR(VERSION, 0); 177173141Srwatson return (0); 178140635Srwatson } 179173138Srwatson if ((d_align = d->d_align) == 0 || (d_align & (d_align - 1))) { 180173138Srwatson LIBELF_SET_ERROR(DATA, 0); 181173141Srwatson return (0); 182173138Srwatson } 183140635Srwatson 184140635Srwatson /* 185173138Srwatson * The buffer's size should be a multiple of the 186140635Srwatson * memory size of the underlying type. 187173141Srwatson */ 188173141Srwatson msz = _libelf_msize(d->d_type, ec, e->e_version); 189140635Srwatson if (d->d_size % msz) { 190140635Srwatson LIBELF_SET_ERROR(DATA, 0); 191173141Srwatson return (0); 192173141Srwatson } 193173138Srwatson 194140635Srwatson /* 195140635Srwatson * Compute the section's size. 196173138Srwatson */ 197140635Srwatson if (e->e_flags & ELF_F_LAYOUT) { 198173138Srwatson if ((uint64_t) d->d_off + d->d_size > scn_size) 199140635Srwatson scn_size = d->d_off + d->d_size; 200140635Srwatson } else { 201173138Srwatson scn_size = roundup2(scn_size, d->d_align); 202173138Srwatson d->d_off = scn_size; 203101099Srwatson fsz = _libelf_fsize(d->d_type, ec, d->d_version, 204101099Srwatson d->d_size / msz); 205173138Srwatson scn_size += fsz; 206101099Srwatson } 207173138Srwatson 208101099Srwatson /* 209101099Srwatson * The section's alignment is the maximum alignment 210173138Srwatson * needed for its data buffers. 211173138Srwatson */ 212101099Srwatson if (d_align > scn_alignment) 213101099Srwatson scn_alignment = d_align; 214173138Srwatson } 215104541Srwatson 216173138Srwatson 217104514Srwatson /* 218104514Srwatson * If the application is requesting full control over the layout 219173138Srwatson * of the section, check its values for sanity. 220173138Srwatson */ 221173138Srwatson if (e->e_flags & ELF_F_LAYOUT) { 222113534Srwatson if (scn_alignment > sh_align || sh_offset % sh_align || 223104541Srwatson sh_size < scn_size) { 224101099Srwatson LIBELF_SET_ERROR(LAYOUT, 0); 225101099Srwatson return (0); 226189529Srwatson } 227189529Srwatson goto updatedescriptor; 228189529Srwatson } 229189529Srwatson 230189529Srwatson /* 231189529Srwatson * Otherwise compute the values in the section header. 232189529Srwatson * 233189529Srwatson * The section alignment is the maximum alignment for any of 234189529Srwatson * its contained data descriptors. 235189529Srwatson */ 236189529Srwatson if (scn_alignment > sh_align) 237189529Srwatson sh_align = scn_alignment; 238189529Srwatson 239189529Srwatson /* 240189529Srwatson * If the section entry size is zero, try and fill in an 241189529Srwatson * appropriate entry size. Per the elf(5) manual page 242189529Srwatson * sections without fixed-size entries should have their 243189529Srwatson * 'sh_entsize' field set to zero. 244189529Srwatson */ 245189529Srwatson if (sh_entsize == 0 && 246189529Srwatson (sh_entsize = _libelf_fsize(elftype, ec, e->e_version, 247189529Srwatson (size_t) 1)) == 1) 248189529Srwatson sh_entsize = 0; 249189529Srwatson 250189529Srwatson sh_size = scn_size; 251189529Srwatson 252189529Srwatsoncomputeoffset: 253189529Srwatson /* 254189529Srwatson * Compute the new offset for the section based on 255189529Srwatson * the section's alignment needs. 256189529Srwatson */ 257189529Srwatson sh_offset = roundup(rc, sh_align); 258189529Srwatson 259189529Srwatson /* 260189529Srwatson * Update the section header. 261189529Srwatson */ 262189529Srwatson if (ec == ELFCLASS32) { 263189529Srwatson shdr32->sh_addralign = (uint32_t) sh_align; 264189529Srwatson shdr32->sh_entsize = (uint32_t) sh_entsize; 265189529Srwatson shdr32->sh_offset = (uint32_t) sh_offset; 266189529Srwatson shdr32->sh_size = (uint32_t) sh_size; 267189529Srwatson } else { 268189529Srwatson shdr64->sh_addralign = sh_align; 269189529Srwatson shdr64->sh_entsize = sh_entsize; 270189529Srwatson shdr64->sh_offset = sh_offset; 271189529Srwatson shdr64->sh_size = sh_size; 272189529Srwatson } 273189529Srwatson 274189529Srwatsonupdatedescriptor: 275189529Srwatson /* 276189529Srwatson * Update the section descriptor. 277189529Srwatson */ 278189529Srwatson s->s_size = sh_size; 279189529Srwatson s->s_offset = sh_offset; 280189529Srwatson 281189529Srwatson return (1); 282189529Srwatson} 283189529Srwatson 284189529Srwatson 285189529Srwatson/* 286189529Srwatson * Insert a section in ascending order in the list 287189529Srwatson */ 288189529Srwatson 289189529Srwatsonstatic int 290189529Srwatson_libelf_insert_section(Elf *e, Elf_Scn *s) 291189529Srwatson{ 292189529Srwatson Elf_Scn *t, *prevt; 293189529Srwatson uint64_t smax, smin, tmax, tmin; 294189529Srwatson 295189529Srwatson smin = s->s_offset; 296189529Srwatson smax = smin + s->s_size; 297189529Srwatson 298189529Srwatson prevt = NULL; 299189529Srwatson STAILQ_FOREACH(t, &e->e_u.e_elf.e_scn, s_next) { 300189529Srwatson tmin = t->s_offset; 301189529Srwatson tmax = tmin + t->s_size; 302189529Srwatson 303189529Srwatson if (tmax <= smin) { 304189529Srwatson /* 305189529Srwatson * 't' lies entirely before 's': ...| t |...| s |... 306189529Srwatson */ 307189529Srwatson prevt = t; 308189529Srwatson continue; 309189529Srwatson } else if (smax <= tmin) 310189529Srwatson /* 311189529Srwatson * 's' lies entirely before 't', and after 'prevt': 312189529Srwatson * ...| prevt |...| s |...| t |... 313189529Srwatson */ 314189529Srwatson break; 315189529Srwatson else { /* 's' and 't' overlap. */ 316189529Srwatson LIBELF_SET_ERROR(LAYOUT, 0); 317189529Srwatson return (0); 318189529Srwatson } 319189529Srwatson } 320189529Srwatson 321189529Srwatson if (prevt) 322189529Srwatson STAILQ_INSERT_AFTER(&e->e_u.e_elf.e_scn, prevt, s, s_next); 323189529Srwatson else 324189529Srwatson STAILQ_INSERT_HEAD(&e->e_u.e_elf.e_scn, s, s_next); 325189529Srwatson return (1); 326189529Srwatson} 327189529Srwatson 328189529Srwatson/* 329189529Srwatson * Recompute section layout. 330189529Srwatson */ 331189529Srwatson 332189529Srwatsonstatic off_t 333189529Srwatson_libelf_resync_sections(Elf *e, off_t rc) 334189529Srwatson{ 335189529Srwatson int ec; 336189529Srwatson Elf_Scn *s; 337189529Srwatson size_t sh_type, shdr_start, shdr_end; 338189529Srwatson 339189529Srwatson ec = e->e_class; 340189529Srwatson 341189529Srwatson /* 342189529Srwatson * Make a pass through sections, computing the extent of each 343189529Srwatson * section. Order in increasing order of addresses. 344189529Srwatson */ 345189529Srwatson STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) { 346189529Srwatson if (ec == ELFCLASS32) 347189529Srwatson sh_type = s->s_shdr.s_shdr32.sh_type; 348189529Srwatson else 349189529Srwatson sh_type = s->s_shdr.s_shdr64.sh_type; 350189529Srwatson 351189529Srwatson if (sh_type == SHT_NOBITS || sh_type == SHT_NULL) 352189529Srwatson continue; 353189529Srwatson 354189529Srwatson if (_libelf_compute_section_extents(e, s, rc) == 0) 355189529Srwatson return ((off_t) -1); 356189529Srwatson 357189529Srwatson if (s->s_size == 0) 358189529Srwatson continue; 359189529Srwatson 360189529Srwatson if (s->s_offset + s->s_size < (size_t) rc) { 361189529Srwatson /* 362173138Srwatson * Try insert this section in the 363104541Srwatson * correct place in the list, 364173138Srwatson * detecting overlaps if any. 365101099Srwatson */ 366101099Srwatson STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, 367173138Srwatson s_next); 368173138Srwatson if (_libelf_insert_section(e, s) == 0) 369173138Srwatson return ((off_t) -1); 370113534Srwatson } else 371104541Srwatson rc = s->s_offset + s->s_size; 372104514Srwatson } 373104514Srwatson 374173138Srwatson /* 375104514Srwatson * If the application is controlling file layout, check for an 376173138Srwatson * overlap between this section's extents and the SHDR table. 377104514Srwatson */ 378104514Srwatson if (e->e_flags & ELF_F_LAYOUT) { 379173138Srwatson 380173138Srwatson if (e->e_class == ELFCLASS32) 381173138Srwatson shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr32->e_shoff; 382101099Srwatson else 383101099Srwatson shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr64->e_shoff; 384184407Srwatson 385184407Srwatson shdr_end = shdr_start + _libelf_fsize(ELF_T_SHDR, e->e_class, 386184407Srwatson e->e_version, e->e_u.e_elf.e_nscn); 387184407Srwatson 388184407Srwatson STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) { 389184407Srwatson if (s->s_offset >= shdr_end || 390184407Srwatson s->s_offset + s->s_size <= shdr_start) 391184407Srwatson continue; 392184407Srwatson LIBELF_SET_ERROR(LAYOUT, 0); 393184407Srwatson return ((off_t) -1); 394184407Srwatson } 395184407Srwatson } 396184407Srwatson 397184407Srwatson return (rc); 398184407Srwatson} 399184407Srwatson 400184407Srwatsonstatic off_t 401184407Srwatson_libelf_resync_elf(Elf *e) 402172930Srwatson{ 403101099Srwatson int ec, eh_class, eh_type; 404172955Srwatson unsigned int eh_byteorder, eh_version; 405101099Srwatson size_t align, fsz; 406101099Srwatson size_t phnum, shnum; 407168947Srwatson off_t rc, phoff, shoff; 408172930Srwatson void *ehdr; 409101099Srwatson Elf32_Ehdr *eh32; 410101099Srwatson Elf64_Ehdr *eh64; 411173138Srwatson 412173138Srwatson rc = 0; 413173138Srwatson 414173138Srwatson ec = e->e_class; 415101099Srwatson 416101099Srwatson assert(ec == ELFCLASS32 || ec == ELFCLASS64); 417173138Srwatson 418173138Srwatson /* 419101099Srwatson * Prepare the EHDR. 420173138Srwatson */ 421101099Srwatson if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) 422101099Srwatson return ((off_t) -1); 423173138Srwatson 424101099Srwatson eh32 = ehdr; 425173138Srwatson eh64 = ehdr; 426122875Srwatson 427122875Srwatson if (ec == ELFCLASS32) { 428173138Srwatson eh_byteorder = eh32->e_ident[EI_DATA]; 429173138Srwatson eh_class = eh32->e_ident[EI_CLASS]; 430122875Srwatson phoff = (uint64_t) eh32->e_phoff; 431122875Srwatson shoff = (uint64_t) eh32->e_shoff; 432173163Srwatson eh_type = eh32->e_type; 433173163Srwatson eh_version = eh32->e_version; 434173163Srwatson } else { 435173163Srwatson eh_byteorder = eh64->e_ident[EI_DATA]; 436173163Srwatson eh_class = eh64->e_ident[EI_CLASS]; 437173163Srwatson phoff = eh64->e_phoff; 438173163Srwatson shoff = eh64->e_shoff; 439173163Srwatson eh_type = eh64->e_type; 440173163Srwatson eh_version = eh64->e_version; 441173163Srwatson } 442173163Srwatson 443173163Srwatson if (eh_version == EV_NONE) 444173138Srwatson eh_version = EV_CURRENT; 445173112Srwatson 446173138Srwatson if (eh_version != e->e_version) { /* always EV_CURRENT */ 447173112Srwatson LIBELF_SET_ERROR(VERSION, 0); 448173112Srwatson return ((off_t) -1); 449173138Srwatson } 450173138Srwatson 451173138Srwatson if (eh_class != e->e_class) { 452173112Srwatson LIBELF_SET_ERROR(CLASS, 0); 453173112Srwatson return ((off_t) -1); 454173138Srwatson } 455122875Srwatson 456173138Srwatson if (e->e_cmd != ELF_C_WRITE && eh_byteorder != e->e_byteorder) { 457173138Srwatson LIBELF_SET_ERROR(HEADER, 0); 458140635Srwatson return ((off_t) -1); 459140635Srwatson } 460173138Srwatson 461173138Srwatson shnum = e->e_u.e_elf.e_nscn; 462173138Srwatson phnum = e->e_u.e_elf.e_nphdr; 463173138Srwatson 464140635Srwatson e->e_byteorder = eh_byteorder; 465140635Srwatson 466173138Srwatson#define INITIALIZE_EHDR(E,EC,V) do { \ 467140635Srwatson (E)->e_ident[EI_MAG0] = ELFMAG0; \ 468173138Srwatson (E)->e_ident[EI_MAG1] = ELFMAG1; \ 469173138Srwatson (E)->e_ident[EI_MAG2] = ELFMAG2; \ 470140635Srwatson (E)->e_ident[EI_MAG3] = ELFMAG3; \ 471140635Srwatson (E)->e_ident[EI_CLASS] = (EC); \ 472173138Srwatson (E)->e_ident[EI_VERSION] = (V); \ 473173138Srwatson (E)->e_ehsize = _libelf_fsize(ELF_T_EHDR, (EC), (V), \ 474140635Srwatson (size_t) 1); \ 475140635Srwatson (E)->e_phentsize = (phnum == 0) ? 0 : _libelf_fsize( \ 476173138Srwatson ELF_T_PHDR, (EC), (V), (size_t) 1); \ 477140635Srwatson (E)->e_shentsize = _libelf_fsize(ELF_T_SHDR, (EC), (V), \ 478173138Srwatson (size_t) 1); \ 479173138Srwatson } while (0) 480173138Srwatson 481140635Srwatson if (ec == ELFCLASS32) 482140635Srwatson INITIALIZE_EHDR(eh32, ec, eh_version); 483173138Srwatson else 484173138Srwatson INITIALIZE_EHDR(eh64, ec, eh_version); 485173138Srwatson 486173138Srwatson (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); 487140635Srwatson 488140635Srwatson rc += _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1); 489173138Srwatson 490140635Srwatson /* 491173138Srwatson * Compute the layout the program header table, if one is 492140635Srwatson * present. The program header table needs to be aligned to a 493140635Srwatson * `natural' boundary. 494173138Srwatson */ 495173138Srwatson if (phnum) { 496140635Srwatson fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum); 497140635Srwatson align = _libelf_falign(ELF_T_PHDR, ec); 498173138Srwatson 499140635Srwatson if (e->e_flags & ELF_F_LAYOUT) { 500173138Srwatson /* 501101099Srwatson * Check offsets for sanity. 502101099Srwatson */ 503173138Srwatson if (rc > phoff) { 504173138Srwatson LIBELF_SET_ERROR(HEADER, 0); 505101099Srwatson return ((off_t) -1); 506101099Srwatson } 507173138Srwatson 508101099Srwatson if (phoff % align) { 509173138Srwatson LIBELF_SET_ERROR(LAYOUT, 0); 510173138Srwatson return ((off_t) -1); 511101099Srwatson } 512101099Srwatson 513173138Srwatson } else 514173138Srwatson phoff = roundup(rc, align); 515173138Srwatson 516101099Srwatson rc = phoff + fsz; 517101099Srwatson } else 518173138Srwatson phoff = 0; 519101099Srwatson 520173138Srwatson /* 521173138Srwatson * Compute the layout of the sections associated with the 522173138Srwatson * file. 523101099Srwatson */ 524101099Srwatson 525173138Srwatson if (e->e_cmd != ELF_C_WRITE && 526173138Srwatson (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 && 527173138Srwatson _libelf_load_scn(e, ehdr) == 0) 528173138Srwatson return ((off_t) -1); 529101099Srwatson 530101099Srwatson if ((rc = _libelf_resync_sections(e, rc)) < 0) 531173138Srwatson return ((off_t) -1); 532173138Srwatson 533173138Srwatson /* 534173138Srwatson * Compute the space taken up by the section header table, if 535104514Srwatson * one is needed. If ELF_F_LAYOUT is asserted, the 536104514Srwatson * application may have placed the section header table in 537173138Srwatson * between existing sections, so the net size of the file need 538173138Srwatson * not increase due to the presence of the section header 539173138Srwatson * table. 540173138Srwatson */ 541104514Srwatson if (shnum) { 542173138Srwatson fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, (size_t) 1); 543104514Srwatson align = _libelf_falign(ELF_T_SHDR, ec); 544104514Srwatson 545173138Srwatson if (e->e_flags & ELF_F_LAYOUT) { 546173138Srwatson if (shoff % align) { 547173138Srwatson LIBELF_SET_ERROR(LAYOUT, 0); 548173141Srwatson return ((off_t) -1); 549104514Srwatson } 550104514Srwatson } else 551173138Srwatson shoff = roundup(rc, align); 552173141Srwatson 553173138Srwatson if (shoff + fsz * shnum > (size_t) rc) 554101099Srwatson rc = shoff + fsz * shnum; 555173138Srwatson } else 556145855Srwatson shoff = 0; 557145855Srwatson 558172930Srwatson /* 559123173Srwatson * Set the fields of the Executable Header that could potentially use 560172955Srwatson * extended numbering. 561131025Srwatson */ 562131025Srwatson _libelf_setphnum(e, ehdr, ec, phnum); 563168944Srwatson _libelf_setshnum(e, ehdr, ec, shnum); 564168944Srwatson 565172930Srwatson /* 566131025Srwatson * Update the `e_phoff' and `e_shoff' fields if the library is 567131025Srwatson * doing the layout. 568173138Srwatson */ 569131025Srwatson if ((e->e_flags & ELF_F_LAYOUT) == 0) { 570173138Srwatson if (ec == ELFCLASS32) { 571122808Srwatson eh32->e_phoff = (uint32_t) phoff; 572122808Srwatson eh32->e_shoff = (uint32_t) shoff; 573173138Srwatson } else { 574173138Srwatson eh64->e_phoff = (uint64_t) phoff; 575122808Srwatson eh64->e_shoff = (uint64_t) shoff; 576122808Srwatson } 577173138Srwatson } 578122808Srwatson 579173138Srwatson return (rc); 580173141Srwatson} 581122808Srwatson 582122808Srwatson/* 583173138Srwatson * Write out the contents of a section. 584173141Srwatson */ 585173138Srwatson 586122808Srwatsonstatic off_t 587122808Srwatson_libelf_write_scn(Elf *e, char *nf, Elf_Scn *s, off_t rc) 588173138Srwatson{ 589122808Srwatson int ec; 590173138Srwatson size_t fsz, msz, nobjects; 591122820Srwatson uint32_t sh_type; 592122820Srwatson uint64_t sh_off, sh_size; 593173138Srwatson int elftype; 594173138Srwatson Elf_Data *d, dst; 595122820Srwatson 596122820Srwatson if ((ec = e->e_class) == ELFCLASS32) { 597173093Srwatson sh_type = s->s_shdr.s_shdr32.sh_type; 598173093Srwatson sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; 599173093Srwatson } else { 600173093Srwatson sh_type = s->s_shdr.s_shdr64.sh_type; 601173093Srwatson sh_size = s->s_shdr.s_shdr64.sh_size; 602173093Srwatson } 603173093Srwatson 604173093Srwatson /* 605173093Srwatson * Ignore sections that do not allocate space in the file. 606173093Srwatson */ 607173093Srwatson if (sh_type == SHT_NOBITS || sh_type == SHT_NULL || sh_size == 0) 608173093Srwatson return (rc); 609173138Srwatson 610173138Srwatson elftype = _libelf_xlate_shtype(sh_type); 611173138Srwatson assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST); 612173093Srwatson 613173093Srwatson sh_off = s->s_offset; 614173138Srwatson assert(sh_off % _libelf_falign(elftype, ec) == 0); 615173138Srwatson 616173093Srwatson /* 617173093Srwatson * If the section has a `rawdata' descriptor, and the section 618173163Srwatson * contents have not been modified, use its contents directly. 619173163Srwatson * The `s_rawoff' member contains the offset into the original 620173163Srwatson * file, while `s_offset' contains its new location in the 621173163Srwatson * destination. 622173163Srwatson */ 623173163Srwatson 624173163Srwatson if (STAILQ_EMPTY(&s->s_data)) { 625173163Srwatson 626173163Srwatson if ((d = elf_rawdata(s, NULL)) == NULL) 627173163Srwatson return ((off_t) -1); 628173163Srwatson 629173163Srwatson STAILQ_FOREACH(d, &s->s_rawdata, d_next) { 630173138Srwatson if ((uint64_t) rc < sh_off + d->d_off) 631173138Srwatson (void) memset(nf + rc, 632173138Srwatson LIBELF_PRIVATE(fillchar), sh_off + 633173138Srwatson d->d_off - rc); 634173093Srwatson rc = sh_off + d->d_off; 635173093Srwatson 636173138Srwatson assert(d->d_buf != NULL); 637173138Srwatson assert(d->d_type == ELF_T_BYTE); 638173138Srwatson assert(d->d_version == e->e_version); 639173138Srwatson 640173093Srwatson (void) memcpy(nf + rc, 641173093Srwatson e->e_rawfile + s->s_rawoff + d->d_off, d->d_size); 642173138Srwatson 643173093Srwatson rc += d->d_size; 644173138Srwatson } 645173138Srwatson 646173093Srwatson return (rc); 647173093Srwatson } 648173138Srwatson 649173138Srwatson /* 650173138Srwatson * Iterate over the set of data descriptors for this section. 651173093Srwatson * The prior call to _libelf_resync_elf() would have setup the 652173093Srwatson * descriptors for this step. 653173093Srwatson */ 654173093Srwatson 655183980Sbz dst.d_version = e->e_version; 656183980Sbz 657183980Sbz STAILQ_FOREACH(d, &s->s_data, d_next) { 658183980Sbz 659183980Sbz msz = _libelf_msize(d->d_type, ec, e->e_version); 660183980Sbz 661183980Sbz if ((uint64_t) rc < sh_off + d->d_off) 662183980Sbz (void) memset(nf + rc, 663183980Sbz LIBELF_PRIVATE(fillchar), sh_off + d->d_off - rc); 664183980Sbz 665183980Sbz rc = sh_off + d->d_off; 666183980Sbz 667183980Sbz assert(d->d_buf != NULL); 668173138Srwatson assert(d->d_version == e->e_version); 669173138Srwatson assert(d->d_size % msz == 0); 670173138Srwatson 671173138Srwatson nobjects = d->d_size / msz; 672173093Srwatson 673173093Srwatson fsz = _libelf_fsize(d->d_type, ec, e->e_version, nobjects); 674193391Srwatson 675173138Srwatson dst.d_buf = nf + rc; 676193391Srwatson dst.d_size = fsz; 677173138Srwatson 678173138Srwatson if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) == 679173093Srwatson NULL) 680173093Srwatson return ((off_t) -1); 681173138Srwatson 682173138Srwatson rc += fsz; 683173138Srwatson } 684173138Srwatson 685105696Srwatson return ((off_t) rc); 686105696Srwatson} 687173138Srwatson 688173138Srwatson/* 689173138Srwatson * Write out the file image. 690101099Srwatson * 691101099Srwatson * The original file could have been mapped in with an ELF_C_RDWR 692173138Srwatson * command and the application could have added new content or 693101099Srwatson * re-arranged its sections before calling elf_update(). Consequently 694173138Srwatson * its not safe to work `in place' on the original file. So we 695105988Srwatson * malloc() the required space for the updated ELF object and build 696105988Srwatson * the object there and write it out to the underlying file at the 697173138Srwatson * end. Note that the application may have opened the underlying file 698173138Srwatson * in ELF_C_RDWR and only retrieved/modified a few sections. We take 699105988Srwatson * care to avoid translating file sections unnecessarily. 700105988Srwatson * 701173138Srwatson * Gaps in the coverage of the file by the file's sections will be 702105988Srwatson * filled with the fill character set by elf_fill(3). 703173138Srwatson */ 704105988Srwatson 705105988Srwatsonstatic off_t 706173138Srwatson_libelf_write_elf(Elf *e, off_t newsize) 707173138Srwatson{ 708173138Srwatson int ec; 709173138Srwatson off_t maxrc, rc; 710168947Srwatson size_t fsz, msz, phnum, shnum; 711173138Srwatson uint64_t phoff, shoff; 712173138Srwatson void *ehdr; 713105988Srwatson char *newfile; 714105988Srwatson Elf_Data dst, src; 715105988Srwatson Elf_Scn *scn, *tscn; 716173138Srwatson Elf32_Ehdr *eh32; 717105988Srwatson Elf64_Ehdr *eh64; 718173138Srwatson 719173138Srwatson assert(e->e_kind == ELF_K_ELF); 720105988Srwatson assert(e->e_cmd != ELF_C_READ); 721105988Srwatson assert(e->e_fd >= 0); 722193391Srwatson 723193391Srwatson if ((newfile = malloc((size_t) newsize)) == NULL) { 724173138Srwatson LIBELF_SET_ERROR(RESOURCE, errno); 725173138Srwatson return ((off_t) -1); 726173138Srwatson } 727105988Srwatson 728105988Srwatson ec = e->e_class; 729184308Srwatson 730184308Srwatson ehdr = _libelf_ehdr(e, ec, 0); 731184308Srwatson assert(ehdr != NULL); 732184308Srwatson 733184308Srwatson phnum = e->e_u.e_elf.e_nphdr; 734184308Srwatson 735184308Srwatson if (ec == ELFCLASS32) { 736184308Srwatson eh32 = (Elf32_Ehdr *) ehdr; 737184308Srwatson 738184308Srwatson phoff = (uint64_t) eh32->e_phoff; 739184308Srwatson shnum = eh32->e_shnum; 740184308Srwatson shoff = (uint64_t) eh32->e_shoff; 741184308Srwatson } else { 742184308Srwatson eh64 = (Elf64_Ehdr *) ehdr; 743184308Srwatson 744184308Srwatson phoff = eh64->e_phoff; 745184308Srwatson shnum = eh64->e_shnum; 746184308Srwatson shoff = eh64->e_shoff; 747184308Srwatson } 748184308Srwatson 749184308Srwatson fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); 750184308Srwatson msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version); 751184308Srwatson 752184308Srwatson (void) memset(&dst, 0, sizeof(dst)); 753184308Srwatson (void) memset(&src, 0, sizeof(src)); 754184308Srwatson 755184308Srwatson src.d_buf = ehdr; 756184308Srwatson src.d_size = msz; 757184308Srwatson src.d_type = ELF_T_EHDR; 758184308Srwatson src.d_version = dst.d_version = e->e_version; 759184308Srwatson 760184308Srwatson rc = 0; 761184308Srwatson 762184308Srwatson dst.d_buf = newfile + rc; 763184308Srwatson dst.d_size = fsz; 764184308Srwatson 765184308Srwatson if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == 766184308Srwatson NULL) 767184308Srwatson goto error; 768184308Srwatson 769184308Srwatson rc += fsz; 770184308Srwatson 771184308Srwatson /* 772184308Srwatson * Write the program header table if present. 773184308Srwatson */ 774184308Srwatson 775184308Srwatson if (phnum != 0 && phoff != 0) { 776184308Srwatson assert((unsigned) rc <= phoff); 777184308Srwatson 778184308Srwatson fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum); 779184308Srwatson 780184308Srwatson assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0); 781184308Srwatson assert(fsz > 0); 782184308Srwatson 783184308Srwatson src.d_buf = _libelf_getphdr(e, ec); 784184308Srwatson src.d_version = dst.d_version = e->e_version; 785184308Srwatson src.d_type = ELF_T_PHDR; 786184308Srwatson src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec, 787184308Srwatson e->e_version); 788184308Srwatson 789184308Srwatson dst.d_size = fsz; 790184308Srwatson 791184308Srwatson if ((uint64_t) rc < phoff) 792184308Srwatson (void) memset(newfile + rc, 793184308Srwatson LIBELF_PRIVATE(fillchar), phoff - rc); 794184308Srwatson 795184308Srwatson dst.d_buf = newfile + rc; 796184308Srwatson 797184308Srwatson if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == 798184308Srwatson NULL) 799173138Srwatson goto error; 800105988Srwatson 801173138Srwatson rc = phoff + fsz; 802179781Srwatson } 803101099Srwatson 804101099Srwatson /* 805173138Srwatson * Write out individual sections. 806179781Srwatson */ 807173138Srwatson 808101099Srwatson STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) 809101099Srwatson if ((rc = _libelf_write_scn(e, newfile, scn, rc)) < 0) 810173138Srwatson goto error; 811101099Srwatson 812173138Srwatson /* 813101099Srwatson * Write out the section header table, if required. Note that 814101099Srwatson * if flag ELF_F_LAYOUT has been set the section header table 815173138Srwatson * could reside in between byte ranges mapped by section 816173138Srwatson * descriptors. 817101099Srwatson */ 818101099Srwatson if (shnum != 0 && shoff != 0) { 819173138Srwatson if ((uint64_t) rc < shoff) 820173138Srwatson (void) memset(newfile + rc, 821173138Srwatson LIBELF_PRIVATE(fillchar), shoff - rc); 822104535Srwatson 823104535Srwatson maxrc = rc; 824173138Srwatson rc = shoff; 825173138Srwatson 826173138Srwatson assert(rc % _libelf_falign(ELF_T_SHDR, ec) == 0); 827173138Srwatson 828173138Srwatson src.d_type = ELF_T_SHDR; 829173138Srwatson src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version); 830173138Srwatson src.d_version = dst.d_version = e->e_version; 831173138Srwatson 832104535Srwatson fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1); 833104535Srwatson 834173138Srwatson STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) { 835105988Srwatson if (ec == ELFCLASS32) 836173138Srwatson src.d_buf = &scn->s_shdr.s_shdr32; 837179781Srwatson else 838101099Srwatson src.d_buf = &scn->s_shdr.s_shdr64; 839101099Srwatson 840173138Srwatson dst.d_size = fsz; 841179781Srwatson dst.d_buf = newfile + rc + scn->s_ndx * fsz; 842173138Srwatson 843119228Srwatson if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, 844173138Srwatson ELF_TOFILE) != &dst) 845101099Srwatson goto error; 846101099Srwatson } 847173138Srwatson 848101099Srwatson rc += e->e_u.e_elf.e_nscn * fsz; 849179781Srwatson if (maxrc > rc) 850173141Srwatson rc = maxrc; 851101099Srwatson } 852101099Srwatson 853179781Srwatson assert(rc == newsize); 854173141Srwatson 855173138Srwatson /* 856101099Srwatson * Write out the constructed contents and remap the file in 857101099Srwatson * read-only. 858173138Srwatson */ 859101099Srwatson 860179781Srwatson if (e->e_rawfile && munmap(e->e_rawfile, e->e_rawsize) < 0) { 861179781Srwatson LIBELF_SET_ERROR(IO, errno); 862101099Srwatson goto error; 863101099Srwatson } 864173141Srwatson 865179781Srwatson if (write(e->e_fd, newfile, (size_t) newsize) != newsize || 866173138Srwatson lseek(e->e_fd, (off_t) 0, SEEK_SET) < 0) { 867101099Srwatson LIBELF_SET_ERROR(IO, errno); 868101099Srwatson goto error; 869173138Srwatson } 870101099Srwatson 871173138Srwatson if (e->e_cmd != ELF_C_WRITE) { 872101099Srwatson if ((e->e_rawfile = mmap(NULL, (size_t) newsize, PROT_READ, 873101099Srwatson MAP_PRIVATE, e->e_fd, (off_t) 0)) == MAP_FAILED) { 874168944Srwatson LIBELF_SET_ERROR(IO, errno); 875173138Srwatson goto error; 876168947Srwatson } 877101099Srwatson e->e_rawsize = newsize; 878101099Srwatson } 879101099Srwatson 880173138Srwatson /* 881173138Srwatson * Reset flags, remove existing section descriptors and 882173138Srwatson * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr() 883101099Srwatson * and elf_getscn() will function correctly. 884101099Srwatson */ 885173138Srwatson 886173138Srwatson e->e_flags &= ~ELF_F_DIRTY; 887101099Srwatson 888173138Srwatson STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn) 889101099Srwatson _libelf_release_scn(scn); 890101099Srwatson 891173138Srwatson if (ec == ELFCLASS32) { 892173138Srwatson free(e->e_u.e_elf.e_ehdr.e_ehdr32); 893173138Srwatson if (e->e_u.e_elf.e_phdr.e_phdr32) 894101099Srwatson free(e->e_u.e_elf.e_phdr.e_phdr32); 895101099Srwatson 896168944Srwatson e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL; 897173138Srwatson e->e_u.e_elf.e_phdr.e_phdr32 = NULL; 898101099Srwatson } else { 899173138Srwatson free(e->e_u.e_elf.e_ehdr.e_ehdr64); 900101099Srwatson if (e->e_u.e_elf.e_phdr.e_phdr64) 901101099Srwatson free(e->e_u.e_elf.e_phdr.e_phdr64); 902173138Srwatson 903173138Srwatson e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL; 904173138Srwatson e->e_u.e_elf.e_phdr.e_phdr64 = NULL; 905145855Srwatson } 906145855Srwatson 907168944Srwatson free(newfile); 908173138Srwatson 909145855Srwatson return (rc); 910173138Srwatson 911101099Srwatson error: 912101099Srwatson free(newfile); 913173138Srwatson 914173138Srwatson return ((off_t) -1); 915173138Srwatson} 916173138Srwatson 917101099Srwatsonoff_t 918101099Srwatsonelf_update(Elf *e, Elf_Cmd c) 919168944Srwatson{ 920173138Srwatson int ec; 921173138Srwatson off_t rc; 922101099Srwatson 923173138Srwatson rc = (off_t) -1; 924101099Srwatson 925101099Srwatson if (e == NULL || e->e_kind != ELF_K_ELF || 926173138Srwatson (c != ELF_C_NULL && c != ELF_C_WRITE)) { 927173138Srwatson LIBELF_SET_ERROR(ARGUMENT, 0); 928173138Srwatson return (rc); 929101099Srwatson } 930101099Srwatson 931168944Srwatson if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) { 932173138Srwatson LIBELF_SET_ERROR(CLASS, 0); 933101099Srwatson return (rc); 934173138Srwatson } 935101099Srwatson 936101099Srwatson if (e->e_version == EV_NONE) 937173138Srwatson e->e_version = EV_CURRENT; 938101099Srwatson 939173138Srwatson if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) { 940101099Srwatson LIBELF_SET_ERROR(MODE, 0); 941101099Srwatson return (rc); 942173138Srwatson } 943173138Srwatson 944173138Srwatson if ((rc = _libelf_resync_elf(e)) < 0) 945101099Srwatson return (rc); 946101099Srwatson 947173138Srwatson if (c == ELF_C_NULL) 948101099Srwatson return (rc); 949173138Srwatson 950101099Srwatson if (e->e_cmd == ELF_C_READ) { 951101099Srwatson /* 952173138Srwatson * This descriptor was opened in read-only mode or by 953173138Srwatson * elf_memory(). 954173138Srwatson */ 955173138Srwatson if (e->e_fd) 956173138Srwatson LIBELF_SET_ERROR(MODE, 0); 957173138Srwatson else 958173138Srwatson LIBELF_SET_ERROR(ARGUMENT, 0); 959101099Srwatson return ((off_t) -1); 960173138Srwatson } 961173138Srwatson 962122875Srwatson if (e->e_fd < 0) { 963122875Srwatson LIBELF_SET_ERROR(SEQUENCE, 0); 964173138Srwatson return ((off_t) -1); 965173138Srwatson } 966173138Srwatson 967173112Srwatson return (_libelf_write_elf(e, rc)); 968173112Srwatson} 969173138Srwatson