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