xref: /titanic_44/usr/src/cmd/sgs/libelf/common/update.c (revision 1ae9ce9bbe05d3e1e28fecb2d33f11d563cc77d4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *	Copyright (c) 1988 AT&T
29  *	  All Rights Reserved
30  */
31 
32 #include <memory.h>
33 #include <malloc.h>
34 #include <limits.h>
35 
36 #include <sgs.h>
37 #include "decl.h"
38 #include "msg.h"
39 
40 /*
41  * This module is compiled twice, the second time having
42  * -D_ELF64 defined.  The following set of macros, along
43  * with machelf.h, represent the differences between the
44  * two compilations.  Be careful *not* to add any class-
45  * dependent code (anything that has elf32 or elf64 in the
46  * name) to this code without hiding it behind a switch-
47  * able macro like these.
48  */
49 #if	defined(_ELF64)
50 
51 #define	FSZ_LONG	ELF64_FSZ_XWORD
52 #define	ELFCLASS	ELFCLASS64
53 #define	_elf_snode_init	_elf64_snode_init
54 #define	_elfxx_cookscn	_elf64_cookscn
55 #define	_elf_upd_lib	_elf64_upd_lib
56 #define	elf_fsize	elf64_fsize
57 #define	_elf_entsz	_elf64_entsz
58 #define	_elf_msize	_elf64_msize
59 #define	_elf_upd_usr	_elf64_upd_usr
60 #define	wrt		wrt64
61 #define	elf_xlatetof	elf64_xlatetof
62 #define	_elfxx_update	_elf64_update
63 #define	_elfxx_swap_wrimage	_elf64_swap_wrimage
64 
65 #else	/* ELF32 */
66 
67 #define	FSZ_LONG	ELF32_FSZ_WORD
68 #define	ELFCLASS	ELFCLASS32
69 #define	_elf_snode_init	_elf32_snode_init
70 #define	_elfxx_cookscn	_elf32_cookscn
71 #define	_elf_upd_lib	_elf32_upd_lib
72 #define	elf_fsize	elf32_fsize
73 #define	_elf_entsz	_elf32_entsz
74 #define	_elf_msize	_elf32_msize
75 #define	_elf_upd_usr	_elf32_upd_usr
76 #define	wrt		wrt32
77 #define	elf_xlatetof	elf32_xlatetof
78 #define	_elfxx_update	_elf32_update
79 #define	_elfxx_swap_wrimage	_elf32_swap_wrimage
80 
81 #endif /* ELF64 */
82 
83 
84 #if	!(defined(_LP64) && defined(_ELF64))
85 #define	TEST_SIZE
86 
87 /*
88  * Handle the decision of whether the current linker can handle the
89  * desired object size, and if not, which error to issue.
90  *
91  * Input is the desired size. On failure, an error has been issued
92  * and 0 is returned. On success, 1 is returned.
93  */
94 static int
95 test_size(Lword hi)
96 {
97 #ifndef _LP64			/* 32-bit linker */
98 	/*
99 	 * A 32-bit libelf is limited to a 2GB output file. This limit
100 	 * is due to the fact that off_t is a signed value, and that
101 	 * libelf cannot support large file support:
102 	 *	- ABI reasons
103 	 *	- Memory use generally is 2x output file size anyway,
104 	 *		so lifting the file size limit will just send
105 	 *		you crashing into the 32-bit VM limit.
106 	 * If the output is an ELFCLASS64 object, or an ELFCLASS32 object
107 	 * under 4GB, switching to the 64-bit version of libelf will help.
108 	 * However, an ELFCLASS32 object must not exceed 4GB.
109 	 */
110 	if (hi > INT_MAX) {	/* Bigger than 2GB */
111 #ifndef _ELF64
112 		/* ELFCLASS32 object is fundamentally too big? */
113 		if (hi > UINT_MAX) {
114 			_elf_seterr(EFMT_FBIG_CLASS32, 0);
115 			return (0);
116 		}
117 #endif				/* _ELF64 */
118 
119 		/* Should switch to the 64-bit libelf? */
120 		_elf_seterr(EFMT_FBIG_LARGEFILE, 0);
121 		return (0);
122 	}
123 #endif				/* !_LP64 */
124 
125 
126 #if	defined(_LP64) && !defined(_ELF64)   /* 64-bit linker, ELFCLASS32 */
127 	/*
128 	 * A 64-bit linker can produce any size output
129 	 * file, but if the resulting file is ELFCLASS32,
130 	 * it must not exceed 4GB.
131 	 */
132 	if (hi > UINT_MAX) {
133 		_elf_seterr(EFMT_FBIG_CLASS32, 0);
134 		return (0);
135 	}
136 #endif
137 
138 	return (1);
139 }
140 #endif				/* TEST_SIZE */
141 
142 /*
143  * Output file update
144  *	These functions walk an Elf structure, update its information,
145  *	and optionally write the output file.  Because the application
146  *	may control of the output file layout, two upd_... routines
147  *	exist.  They're similar but too different to merge cleanly.
148  *
149  *	The library defines a "dirty" bit to force parts of the file
150  *	to be written on update.  These routines ignore the dirty bit
151  *	and do everything.  A minimal update routine might be useful
152  *	someday.
153  */
154 
155 static size_t
156 _elf_upd_lib(Elf * elf)
157 {
158 	NOTE(ASSUMING_PROTECTED(*elf))
159 	Lword		hi;
160 	Lword		hibit;
161 	Elf_Scn *	s;
162 	register Lword	sz;
163 	Ehdr *		eh = elf->ed_ehdr;
164 	unsigned	ver = eh->e_version;
165 	register char	*p = (char *)eh->e_ident;
166 	size_t		scncnt;
167 
168 	/*
169 	 * Ehdr and Phdr table go first
170 	 */
171 	p[EI_MAG0] = ELFMAG0;
172 	p[EI_MAG1] = ELFMAG1;
173 	p[EI_MAG2] = ELFMAG2;
174 	p[EI_MAG3] = ELFMAG3;
175 	p[EI_CLASS] = ELFCLASS;
176 	/* LINTED */
177 	p[EI_VERSION] = (Byte)ver;
178 	hi = elf_fsize(ELF_T_EHDR, 1, ver);
179 	/* LINTED */
180 	eh->e_ehsize = (Half)hi;
181 	if (eh->e_phnum != 0) {
182 		/* LINTED */
183 		eh->e_phentsize = (Half)elf_fsize(ELF_T_PHDR, 1, ver);
184 		/* LINTED */
185 		eh->e_phoff = (Off)hi;
186 		hi += eh->e_phentsize * eh->e_phnum;
187 	} else {
188 		eh->e_phoff = 0;
189 		eh->e_phentsize = 0;
190 	}
191 
192 	/*
193 	 * Obtain the first section header.  Typically, this section has NULL
194 	 * contents, however in the case of Extended ELF Sections this section
195 	 * is used to hold an alternative e_shnum, e_shstrndx and e_phnum.
196 	 * On initial allocation (see _elf_snode) the elements of this section
197 	 * would have been zeroed.  The e_shnum is initialized later, after the
198 	 * section header count has been determined.  The e_shstrndx and
199 	 * e_phnum may have already been initialized by the caller (for example,
200 	 * gelf_update_shdr() in mcs(1)).
201 	 */
202 	if ((s = elf->ed_hdscn) == 0) {
203 		eh->e_shnum = 0;
204 		scncnt = 0;
205 	} else {
206 		s = s->s_next;
207 		scncnt = 1;
208 	}
209 
210 	/*
211 	 * Loop through sections.  Compute section size before changing hi.
212 	 * Allow null buffers for NOBITS.
213 	 */
214 	hibit = 0;
215 	for (; s != 0; s = s->s_next) {
216 		register Dnode	*d;
217 		register Lword	fsz, j;
218 		Shdr *sh = s->s_shdr;
219 
220 		scncnt++;
221 		if (sh->sh_type == SHT_NULL) {
222 			*sh = _elf_snode_init.sb_shdr;
223 			continue;
224 		}
225 
226 		if ((s->s_myflags & SF_READY) == 0)
227 			(void) _elfxx_cookscn(s);
228 
229 		sh->sh_addralign = 1;
230 		if ((sz = (Lword)_elf_entsz(elf, sh->sh_type, ver)) != 0)
231 			/* LINTED */
232 			sh->sh_entsize = (Half)sz;
233 		sz = 0;
234 		for (d = s->s_hdnode; d != 0; d = d->db_next) {
235 			if ((fsz = elf_fsize(d->db_data.d_type,
236 			    1, ver)) == 0)
237 				return (0);
238 
239 			j = _elf_msize(d->db_data.d_type, ver);
240 			fsz *= (d->db_data.d_size / j);
241 			d->db_osz = (size_t)fsz;
242 			if ((j = d->db_data.d_align) > 1) {
243 				if (j > sh->sh_addralign)
244 					sh->sh_addralign = (Xword)j;
245 
246 				if (sz % j != 0)
247 					sz += j - sz % j;
248 			}
249 			d->db_data.d_off = (off_t)sz;
250 			d->db_xoff = sz;
251 			sz += fsz;
252 		}
253 
254 		sh->sh_size = (Xword) sz;
255 		/*
256 		 * We want to take into account the offsets for NOBITS
257 		 * sections and let the "sh_offsets" point to where
258 		 * the section would 'conceptually' fit within
259 		 * the file (as required by the ABI).
260 		 *
261 		 * But - we must also make sure that the NOBITS does
262 		 * not take up any actual space in the file.  We preserve
263 		 * the actual offset into the file in the 'hibit' variable.
264 		 * When we come to the first non-NOBITS section after a
265 		 * encountering a NOBITS section the hi counter is restored
266 		 * to its proper place in the file.
267 		 */
268 		if (sh->sh_type == SHT_NOBITS) {
269 			if (hibit == 0)
270 				hibit = hi;
271 		} else {
272 			if (hibit) {
273 				hi = hibit;
274 				hibit = 0;
275 			}
276 		}
277 		j = sh->sh_addralign;
278 		if ((fsz = hi % j) != 0)
279 			hi += j - fsz;
280 
281 		/* LINTED */
282 		sh->sh_offset = (Off)hi;
283 		hi += sz;
284 	}
285 
286 	/*
287 	 * if last section was a 'NOBITS' section then we need to
288 	 * restore the 'hi' counter to point to the end of the last
289 	 * non 'NOBITS' section.
290 	 */
291 	if (hibit) {
292 		hi = hibit;
293 		hibit = 0;
294 	}
295 
296 	/*
297 	 * Shdr table last
298 	 */
299 	if (scncnt != 0) {
300 		if (hi % FSZ_LONG != 0)
301 			hi += FSZ_LONG - hi % FSZ_LONG;
302 		/* LINTED */
303 		eh->e_shoff = (Off)hi;
304 		/*
305 		 * If we are using 'extended sections' then the
306 		 * e_shnum is stored in the sh_size field of the
307 		 * first section header.
308 		 *
309 		 * NOTE: we set e_shnum to '0' because it's specified
310 		 * this way in the gABI, and in the hopes that
311 		 * this will cause less problems to unaware
312 		 * tools then if we'd set it to SHN_XINDEX (0xffff).
313 		 */
314 		if (scncnt < SHN_LORESERVE)
315 			eh->e_shnum = scncnt;
316 		else {
317 			Shdr	*sh;
318 			sh = (Shdr *)elf->ed_hdscn->s_shdr;
319 			sh->sh_size = scncnt;
320 			eh->e_shnum = 0;
321 		}
322 		/* LINTED */
323 		eh->e_shentsize = (Half)elf_fsize(ELF_T_SHDR, 1, ver);
324 		hi += eh->e_shentsize * scncnt;
325 	} else {
326 		eh->e_shoff = 0;
327 		eh->e_shentsize = 0;
328 	}
329 
330 #ifdef TEST_SIZE
331 	if (test_size(hi) == 0)
332 		return (0);
333 #endif
334 
335 	return ((size_t)hi);
336 }
337 
338 
339 
340 static size_t
341 _elf_upd_usr(Elf * elf)
342 {
343 	NOTE(ASSUMING_PROTECTED(*elf))
344 	Lword		hi;
345 	Elf_Scn *	s;
346 	register Lword	sz;
347 	Ehdr *		eh = elf->ed_ehdr;
348 	unsigned	ver = eh->e_version;
349 	register char	*p = (char *)eh->e_ident;
350 
351 
352 	/*
353 	 * Ehdr and Phdr table go first
354 	 */
355 	p[EI_MAG0] = ELFMAG0;
356 	p[EI_MAG1] = ELFMAG1;
357 	p[EI_MAG2] = ELFMAG2;
358 	p[EI_MAG3] = ELFMAG3;
359 	p[EI_CLASS] = ELFCLASS;
360 	/* LINTED */
361 	p[EI_VERSION] = (Byte)ver;
362 	hi = elf_fsize(ELF_T_EHDR, 1, ver);
363 	/* LINTED */
364 	eh->e_ehsize = (Half)hi;
365 
366 	/*
367 	 * If phnum is zero, phoff "should" be zero too,
368 	 * but the application is responsible for it.
369 	 * Allow a non-zero value here and update the
370 	 * hi water mark accordingly.
371 	 */
372 
373 	if (eh->e_phnum != 0)
374 		/* LINTED */
375 		eh->e_phentsize = (Half)elf_fsize(ELF_T_PHDR, 1, ver);
376 	else
377 		eh->e_phentsize = 0;
378 	if ((sz = eh->e_phoff + eh->e_phentsize * eh->e_phnum) > hi)
379 		hi = sz;
380 
381 	/*
382 	 * Loop through sections, skipping index zero.
383 	 * Compute section size before changing hi.
384 	 * Allow null buffers for NOBITS.
385 	 */
386 
387 	if ((s = elf->ed_hdscn) == 0)
388 		eh->e_shnum = 0;
389 	else {
390 		eh->e_shnum = 1;
391 		*(Shdr*)s->s_shdr = _elf_snode_init.sb_shdr;
392 		s = s->s_next;
393 	}
394 	for (; s != 0; s = s->s_next) {
395 		register Dnode	*d;
396 		register Lword	fsz, j;
397 		Shdr *sh = s->s_shdr;
398 
399 		if ((s->s_myflags & SF_READY) == 0)
400 			(void) _elfxx_cookscn(s);
401 
402 		++eh->e_shnum;
403 		sz = 0;
404 		for (d = s->s_hdnode; d != 0; d = d->db_next) {
405 			if ((fsz = elf_fsize(d->db_data.d_type, 1,
406 			    ver)) == 0)
407 				return (0);
408 			j = _elf_msize(d->db_data.d_type, ver);
409 			fsz *= (d->db_data.d_size / j);
410 			d->db_osz = (size_t)fsz;
411 
412 			if ((sh->sh_type != SHT_NOBITS) &&
413 			    ((j = (d->db_data.d_off + d->db_osz)) > sz))
414 				sz = j;
415 		}
416 		if (sh->sh_size < sz) {
417 			_elf_seterr(EFMT_SCNSZ, 0);
418 			return (0);
419 		}
420 		if ((sh->sh_type != SHT_NOBITS) &&
421 		    (hi < sh->sh_offset + sh->sh_size))
422 			hi = sh->sh_offset + sh->sh_size;
423 	}
424 
425 	/*
426 	 * Shdr table last.  Comment above for phnum/phoff applies here.
427 	 */
428 	if (eh->e_shnum != 0)
429 		/* LINTED */
430 		eh->e_shentsize = (Half)elf_fsize(ELF_T_SHDR, 1, ver);
431 	else
432 		eh->e_shentsize = 0;
433 
434 	if ((sz = eh->e_shoff + eh->e_shentsize * eh->e_shnum) > hi)
435 		hi = sz;
436 
437 #ifdef TEST_SIZE
438 	if (test_size(hi) == 0)
439 		return (0);
440 #endif
441 
442 	return ((size_t)hi);
443 }
444 
445 
446 static size_t
447 wrt(Elf * elf, Xword outsz, unsigned fill, int update_cmd)
448 {
449 	NOTE(ASSUMING_PROTECTED(*elf))
450 	Elf_Data		dst, src;
451 	unsigned		flag;
452 	Xword			hi, sz;
453 	char			*image;
454 	Elf_Scn			*s;
455 	Ehdr			*eh = elf->ed_ehdr;
456 	unsigned		ver = eh->e_version;
457 	unsigned		encode;
458 	int			byte;
459 	_elf_execfill_func_t	*execfill_func;
460 
461 	/*
462 	 * If this is an ELF_C_WRIMAGE write, then we encode into the
463 	 * byte order of the system we are running on rather than that of
464 	 * of the object. For ld.so.1, this is the same order, but
465 	 * for 'ld', it might not be in the case where we are cross
466 	 * linking an object for a different target. In this later case,
467 	 * the linker-host byte order is necessary so that the linker can
468 	 * manipulate the resulting  image. It is expected that the linker
469 	 * will call elf_swap_wrimage() if necessary to convert the image
470 	 * to the target byte order.
471 	 */
472 	encode = (update_cmd == ELF_C_WRIMAGE) ? _elf_sys_encoding() :
473 	    eh->e_ident[EI_DATA];
474 
475 	/*
476 	 * Two issues can cause trouble for the output file.
477 	 * First, begin() with ELF_C_RDWR opens a file for both
478 	 * read and write.  On the write update(), the library
479 	 * has to read everything it needs before truncating
480 	 * the file.  Second, using mmap for both read and write
481 	 * is too tricky.  Consequently, the library disables mmap
482 	 * on the read side.  Using mmap for the output saves swap
483 	 * space, because that mapping is SHARED, not PRIVATE.
484 	 *
485 	 * If the file is write-only, there can be nothing of
486 	 * interest to bother with.
487 	 *
488 	 * The following reads the entire file, which might be
489 	 * more than necessary.  Better safe than sorry.
490 	 */
491 
492 	if ((elf->ed_myflags & EDF_READ) &&
493 	    (_elf_vm(elf, (size_t)0, elf->ed_fsz) != OK_YES))
494 		return (0);
495 
496 	flag = elf->ed_myflags & EDF_WRALLOC;
497 	if ((image = _elf_outmap(elf->ed_fd, outsz, &flag)) == 0)
498 		return (0);
499 
500 	if (flag == 0)
501 		elf->ed_myflags |= EDF_IMALLOC;
502 
503 	/*
504 	 * If an error occurs below, a "dirty" bit may be cleared
505 	 * improperly.  To save a second pass through the file,
506 	 * this code sets the dirty bit on the elf descriptor
507 	 * when an error happens, assuming that will "cover" any
508 	 * accidents.
509 	 */
510 
511 	/*
512 	 * Hi is needed only when 'fill' is non-zero.
513 	 * Fill is non-zero only when the library
514 	 * calculates file/section/data buffer offsets.
515 	 * The lib guarantees they increase monotonically.
516 	 * That guarantees proper filling below.
517 	 */
518 
519 
520 	/*
521 	 * Ehdr first
522 	 */
523 
524 	src.d_buf = (Elf_Void *)eh;
525 	src.d_type = ELF_T_EHDR;
526 	src.d_size = sizeof (Ehdr);
527 	src.d_version = EV_CURRENT;
528 	dst.d_buf = (Elf_Void *)image;
529 	dst.d_size = eh->e_ehsize;
530 	dst.d_version = ver;
531 	if (elf_xlatetof(&dst, &src, encode) == 0)
532 		return (0);
533 	elf->ed_ehflags &= ~ELF_F_DIRTY;
534 	hi = eh->e_ehsize;
535 
536 	/*
537 	 * Phdr table if one exists
538 	 */
539 
540 	if (eh->e_phnum != 0) {
541 		unsigned	work;
542 		/*
543 		 * Unlike other library data, phdr table is
544 		 * in the user version.  Change src buffer
545 		 * version here, fix it after translation.
546 		 */
547 
548 		src.d_buf = (Elf_Void *)elf->ed_phdr;
549 		src.d_type = ELF_T_PHDR;
550 		src.d_size = elf->ed_phdrsz;
551 		ELFACCESSDATA(work, _elf_work)
552 		src.d_version = work;
553 		dst.d_buf = (Elf_Void *)(image + eh->e_phoff);
554 		dst.d_size = eh->e_phnum * eh->e_phentsize;
555 		hi = (Xword)(eh->e_phoff + dst.d_size);
556 		if (elf_xlatetof(&dst, &src, encode) == 0) {
557 			elf->ed_uflags |= ELF_F_DIRTY;
558 			return (0);
559 		}
560 		elf->ed_phflags &= ~ELF_F_DIRTY;
561 		src.d_version = EV_CURRENT;
562 	}
563 
564 	/*
565 	 * Loop through sections
566 	 */
567 
568 	ELFACCESSDATA(byte, _elf_byte);
569 	ELFACCESSDATA(execfill_func, _elf_execfill_func);
570 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
571 		register Dnode	*d, *prevd;
572 		Xword		off = 0;
573 		Shdr		*sh = s->s_shdr;
574 		char		*start = image + sh->sh_offset;
575 		char		*here;
576 		_elf_execfill_func_t	*execfill;
577 
578 		/* Only use the execfill function on SHF_EXECINSTR sections */
579 		execfill = (sh->sh_flags & SHF_EXECINSTR) ?
580 		    execfill_func : NULL;
581 
582 		/*
583 		 * Just "clean" DIRTY flag for "empty" sections.  Even if
584 		 * NOBITS needs padding, the next thing in the
585 		 * file will provide it.  (And if this NOBITS is
586 		 * the last thing in the file, no padding needed.)
587 		 */
588 		if ((sh->sh_type == SHT_NOBITS) ||
589 		    (sh->sh_type == SHT_NULL)) {
590 			d = s->s_hdnode, prevd = 0;
591 			for (; d != 0; prevd = d, d = d->db_next)
592 				d->db_uflags &= ~ELF_F_DIRTY;
593 			continue;
594 		}
595 		/*
596 		 * Clear out the memory between the end of the last
597 		 * section and the begining of this section.
598 		 */
599 		if (fill && (sh->sh_offset > hi)) {
600 			sz = sh->sh_offset - hi;
601 			(void) memset(start - sz, byte, sz);
602 		}
603 
604 
605 		for (d = s->s_hdnode, prevd = 0;
606 		    d != 0; prevd = d, d = d->db_next) {
607 			d->db_uflags &= ~ELF_F_DIRTY;
608 			here = start + d->db_data.d_off;
609 
610 			/*
611 			 * Clear out the memory between the end of the
612 			 * last update and the start of this data buffer.
613 			 *
614 			 * These buffers represent input sections that have
615 			 * been concatenated into an output section, so if
616 			 * the output section is executable (SHF_EXECINSTR)
617 			 * and a fill function has been registered, use the
618 			 * function. Otherwise, use the fill byte.
619 			 */
620 			if (fill && (d->db_data.d_off > off)) {
621 				sz = (Xword)(d->db_data.d_off - off);
622 				if (execfill != NULL)
623 					(* execfill)(start,
624 					    here - start - sz, sz);
625 				else
626 					(void) memset(here - sz, byte, sz);
627 			}
628 
629 			if ((d->db_myflags & DBF_READY) == 0) {
630 				SCNLOCK(s);
631 				if (_elf_locked_getdata(s, &prevd->db_data) !=
632 				    &d->db_data) {
633 					elf->ed_uflags |= ELF_F_DIRTY;
634 					SCNUNLOCK(s);
635 					return (0);
636 				}
637 				SCNUNLOCK(s);
638 			}
639 			dst.d_buf = (Elf_Void *)here;
640 			dst.d_size = d->db_osz;
641 
642 			/*
643 			 * Copy the translated bits out to the destination
644 			 * image.
645 			 */
646 			if (elf_xlatetof(&dst, &d->db_data, encode) == 0) {
647 				elf->ed_uflags |= ELF_F_DIRTY;
648 				return (0);
649 			}
650 
651 			off = (Xword)(d->db_data.d_off + dst.d_size);
652 		}
653 		hi = sh->sh_offset + sh->sh_size;
654 	}
655 
656 	/*
657 	 * Shdr table last
658 	 */
659 
660 	if (fill && (eh->e_shoff > hi)) {
661 		sz = eh->e_shoff - hi;
662 		(void) memset(image + hi, byte, sz);
663 	}
664 
665 	src.d_type = ELF_T_SHDR;
666 	src.d_size = sizeof (Shdr);
667 	dst.d_buf = (Elf_Void *)(image + eh->e_shoff);
668 	dst.d_size = eh->e_shentsize;
669 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
670 		assert((uintptr_t)dst.d_buf < ((uintptr_t)image + outsz));
671 		s->s_shflags &= ~ELF_F_DIRTY;
672 		s->s_uflags &= ~ELF_F_DIRTY;
673 		src.d_buf = s->s_shdr;
674 
675 		if (elf_xlatetof(&dst, &src, encode) == 0) {
676 			elf->ed_uflags |= ELF_F_DIRTY;
677 			return (0);
678 		}
679 
680 		dst.d_buf = (char *)dst.d_buf + eh->e_shentsize;
681 	}
682 	/*
683 	 * ELF_C_WRIMAGE signifyes that we build the memory image, but
684 	 * that we do not actually write it to disk.  This is used
685 	 * by ld(1) to build up a full image of an elf file and then
686 	 * to process the file before it's actually written out to
687 	 * disk.  This saves ld(1) the overhead of having to write
688 	 * the image out to disk twice.
689 	 */
690 	if (update_cmd == ELF_C_WRIMAGE) {
691 		elf->ed_uflags &= ~ELF_F_DIRTY;
692 		elf->ed_wrimage = image;
693 		elf->ed_wrimagesz = outsz;
694 		return (outsz);
695 	}
696 
697 	if (_elf_outsync(elf->ed_fd, image, outsz,
698 	    ((elf->ed_myflags & EDF_IMALLOC) ? 0 : 1)) != 0) {
699 		elf->ed_uflags &= ~ELF_F_DIRTY;
700 		elf->ed_myflags &= ~EDF_IMALLOC;
701 		return (outsz);
702 	}
703 
704 	elf->ed_uflags |= ELF_F_DIRTY;
705 	return (0);
706 }
707 
708 
709 
710 
711 /*
712  * The following is a private interface between the linkers (ld & ld.so.1)
713  * and libelf:
714  *
715  * elf_update(elf, ELF_C_WRIMAGE)
716  *	This will cause full image representing the elf file
717  *	described by the elf pointer to be built in memory.  If the
718  *	elf pointer has a valid file descriptor associated with it
719  *	we will attempt to build the memory image from mmap()'ed
720  *	storage.  If the elf descriptor does not have a valid
721  *	file descriptor (opened with elf_begin(0, ELF_C_IMAGE, 0))
722  *	then the image will be allocated from dynamic memory (malloc()).
723  *
724  *	elf_update() will return the size of the memory image built
725  *	when sucessful.
726  *
727  *	When a subsequent call to elf_update() with ELF_C_WRITE as
728  *	the command is performed it will sync the image created
729  *	by ELF_C_WRIMAGE to disk (if fd available) and
730  *	free the memory allocated.
731  */
732 
733 off_t
734 _elfxx_update(Elf * elf, Elf_Cmd cmd)
735 {
736 	size_t		sz;
737 	unsigned	u;
738 	Ehdr		*eh = elf->ed_ehdr;
739 
740 	if (elf == 0)
741 		return (-1);
742 
743 	ELFWLOCK(elf)
744 	switch (cmd) {
745 	default:
746 		_elf_seterr(EREQ_UPDATE, 0);
747 		ELFUNLOCK(elf)
748 		return (-1);
749 
750 	case ELF_C_WRIMAGE:
751 		if ((elf->ed_myflags & EDF_WRITE) == 0) {
752 			_elf_seterr(EREQ_UPDWRT, 0);
753 			ELFUNLOCK(elf)
754 			return (-1);
755 		}
756 		break;
757 	case ELF_C_WRITE:
758 		if ((elf->ed_myflags & EDF_WRITE) == 0) {
759 			_elf_seterr(EREQ_UPDWRT, 0);
760 			ELFUNLOCK(elf)
761 			return (-1);
762 		}
763 		if (elf->ed_wrimage) {
764 			if (elf->ed_myflags & EDF_WRALLOC) {
765 				free(elf->ed_wrimage);
766 				/*
767 				 * The size is still returned even
768 				 * though nothing is actually written
769 				 * out.  This is just to be consistant
770 				 * with the rest of the interface.
771 				 */
772 				sz = elf->ed_wrimagesz;
773 				elf->ed_wrimage = 0;
774 				elf->ed_wrimagesz = 0;
775 				ELFUNLOCK(elf);
776 				return ((off_t)sz);
777 			}
778 			sz = _elf_outsync(elf->ed_fd, elf->ed_wrimage,
779 			    elf->ed_wrimagesz,
780 			    (elf->ed_myflags & EDF_IMALLOC ? 0 : 1));
781 			elf->ed_myflags &= ~EDF_IMALLOC;
782 			elf->ed_wrimage = 0;
783 			elf->ed_wrimagesz = 0;
784 			ELFUNLOCK(elf);
785 			return ((off_t)sz);
786 		}
787 		/* FALLTHROUGH */
788 	case ELF_C_NULL:
789 		break;
790 	}
791 
792 	if (eh == 0) {
793 		_elf_seterr(ESEQ_EHDR, 0);
794 		ELFUNLOCK(elf)
795 		return (-1);
796 	}
797 
798 	if ((u = eh->e_version) > EV_CURRENT) {
799 		_elf_seterr(EREQ_VER, 0);
800 		ELFUNLOCK(elf)
801 		return (-1);
802 	}
803 
804 	if (u == EV_NONE)
805 		eh->e_version = EV_CURRENT;
806 
807 	if ((u = eh->e_ident[EI_DATA]) == ELFDATANONE) {
808 		unsigned	encode;
809 
810 		ELFACCESSDATA(encode, _elf_encode)
811 		if (encode == ELFDATANONE) {
812 			_elf_seterr(EREQ_ENCODE, 0);
813 			ELFUNLOCK(elf)
814 			return (-1);
815 		}
816 		/* LINTED */
817 		eh->e_ident[EI_DATA] = (Byte)encode;
818 	}
819 
820 	u = 1;
821 	if (elf->ed_uflags & ELF_F_LAYOUT) {
822 		sz = _elf_upd_usr(elf);
823 		u = 0;
824 	} else
825 		sz = _elf_upd_lib(elf);
826 
827 	if ((sz != 0) && ((cmd == ELF_C_WRITE) || (cmd == ELF_C_WRIMAGE)))
828 		sz = wrt(elf, (Xword)sz, u, cmd);
829 
830 	if (sz == 0) {
831 		ELFUNLOCK(elf)
832 		return (-1);
833 	}
834 
835 	ELFUNLOCK(elf)
836 	return ((off_t)sz);
837 }
838 
839 
840 /*
841  * When wrt() processes an ELF_C_WRIMAGE request, the resulting image
842  * gets the byte order (encoding) of the platform running the linker
843  * rather than that of the target host. This allows the linker to modify
844  * the image, prior to flushing it to the output file. This routine
845  * is used to re-translate such an image into the byte order of the
846  * target host.
847  */
848 int
849 _elfxx_swap_wrimage(Elf *elf)
850 {
851 	Elf_Data	dst, src;
852 	Elf_Scn		*s;
853 	Ehdr		*eh;
854 	Half		e_phnum;
855 	unsigned	ver;
856 	unsigned	encode;
857 
858 	/*
859 	 * Ehdr first
860 	 */
861 
862 	ELFWLOCK(elf);
863 	eh = elf->ed_ehdr;
864 	e_phnum = eh->e_phnum;
865 	ver = eh->e_version;
866 	encode = eh->e_ident[EI_DATA];
867 
868 	src.d_buf = dst.d_buf = (Elf_Void *)eh;
869 	src.d_type = dst.d_type = ELF_T_EHDR;
870 	src.d_size = dst.d_size = sizeof (Ehdr);
871 	src.d_version = dst.d_version = ver;
872 	if (elf_xlatetof(&dst, &src, encode) == 0) {
873 		ELFUNLOCK(elf);
874 		return (1);
875 	}
876 
877 	/*
878 	 * Phdr table if one exists
879 	 */
880 
881 	if (e_phnum != 0) {
882 		unsigned	work;
883 		/*
884 		 * Unlike other library data, phdr table is
885 		 * in the user version.
886 		 */
887 
888 		src.d_buf = dst.d_buf = (Elf_Void *)elf->ed_phdr;
889 		src.d_type = dst.d_type = ELF_T_PHDR;
890 		src.d_size = dst.d_size = elf->ed_phdrsz;
891 		ELFACCESSDATA(work, _elf_work)
892 		src.d_version = dst.d_version = work;
893 		if (elf_xlatetof(&dst, &src, encode) == 0) {
894 			ELFUNLOCK(elf);
895 			return (1);
896 		}
897 	}
898 
899 	/*
900 	 * Loop through sections
901 	 */
902 
903 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
904 		register Dnode	*d, *prevd;
905 		Shdr		*sh = s->s_shdr;
906 
907 		if ((sh->sh_type == SHT_NOBITS) || (sh->sh_type == SHT_NULL))
908 			continue;
909 
910 		for (d = s->s_hdnode, prevd = 0;
911 		    d != 0; prevd = d, d = d->db_next) {
912 
913 			if ((d->db_myflags & DBF_READY) == 0) {
914 				SCNLOCK(s);
915 				if (_elf_locked_getdata(s, &prevd->db_data) !=
916 				    &d->db_data) {
917 					SCNUNLOCK(s);
918 					ELFUNLOCK(elf);
919 					return (1);
920 				}
921 				SCNUNLOCK(s);
922 			}
923 
924 			dst = d->db_data;
925 			if (elf_xlatetof(&dst, &d->db_data, encode) == 0) {
926 				ELFUNLOCK(elf);
927 				return (1);
928 			}
929 		}
930 	}
931 
932 	/*
933 	 * Shdr table
934 	 */
935 
936 	src.d_type = dst.d_type = ELF_T_SHDR;
937 	src.d_version = dst.d_version = ver;
938 	for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
939 		src.d_buf = dst.d_buf = s->s_shdr;
940 		src.d_size = dst.d_size = sizeof (Shdr);
941 		if (elf_xlatetof(&dst, &src, encode) == 0) {
942 			ELFUNLOCK(elf);
943 			return (1);
944 		}
945 	}
946 
947 	ELFUNLOCK(elf);
948 	return (0);
949 }
950 
951 
952 
953 #ifndef _ELF64
954 /* class-independent, only needs to be compiled once */
955 
956 off_t
957 elf_update(Elf *elf, Elf_Cmd cmd)
958 {
959 	if (elf == 0)
960 		return (-1);
961 
962 	if (elf->ed_class == ELFCLASS32)
963 		return (_elf32_update(elf, cmd));
964 	else if (elf->ed_class == ELFCLASS64) {
965 		return (_elf64_update(elf, cmd));
966 	}
967 
968 	_elf_seterr(EREQ_CLASS, 0);
969 	return (-1);
970 }
971 
972 int
973 _elf_swap_wrimage(Elf *elf)
974 {
975 	if (elf == 0)
976 		return (0);
977 
978 	if (elf->ed_class == ELFCLASS32)
979 		return (_elf32_swap_wrimage(elf));
980 
981 	if (elf->ed_class == ELFCLASS64)
982 		return (_elf64_swap_wrimage(elf));
983 
984 	_elf_seterr(EREQ_CLASS, 0);
985 	return (0);
986 }
987 
988 /*
989  * 4106312, 4106398, This is an ad-hoc means for the 32-bit
990  * Elf64 version of libld.so.3 to get around the limitation
991  * of a 32-bit d_off field.  This is only intended to be
992  * used by libld to relocate symbols in large NOBITS sections.
993  */
994 Elf64_Off
995 _elf_getxoff(Elf_Data * d)
996 {
997 	return (((Dnode *)d)->db_xoff);
998 }
999 #endif /* !_ELF64 */
1000