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