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