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