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