xref: /titanic_50/usr/src/cmd/sgs/libld/common/outfile.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
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 (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * This file contains the functions responsible for opening the output file
33  * image, associating the appropriate input elf structures with the new image,
34  * and obtaining new elf structures to define the new image.
35  */
36 #include	<stdio.h>
37 #include	<sys/stat.h>
38 #include	<fcntl.h>
39 #include	<link.h>
40 #include	<errno.h>
41 #include	<string.h>
42 #include	<limits.h>
43 #include	<debug.h>
44 #include	<unistd.h>
45 #include	"msg.h"
46 #include	"_libld.h"
47 
48 /*
49  * Determine a least common multiplier.  Input sections contain an alignment
50  * requirement, which elf_update() uses to insure that the section is aligned
51  * correctly off of the base of the elf image.  We must also insure that the
52  * sections mapping is congruent with this alignment requirement.  For each
53  * input section associated with a loadable segment determine whether the
54  * segments alignment must be adjusted to compensate for a sections alignment
55  * requirements.
56  */
57 Xword
58 ld_lcm(Xword a, Xword b)
59 {
60 	Xword	_r, _a, _b;
61 
62 	if ((_a = a) == 0)
63 		return (b);
64 	if ((_b = b) == 0)
65 		return (a);
66 
67 	if (_a > _b)
68 		_a = b, _b = a;
69 	while ((_r = _b % _a) != 0)
70 		_b = _a, _a = _r;
71 	return ((a / _a) * b);
72 }
73 
74 /*
75  * Open the output file and insure the correct access modes.
76  */
77 uintptr_t
78 ld_open_outfile(Ofl_desc * ofl)
79 {
80 	mode_t		mode;
81 	struct stat	status;
82 
83 	/* Determine if the output file already exists */
84 	if (stat(ofl->ofl_name, &status) == 0) {
85 		if ((status.st_mode & S_IFMT) != S_IFREG) {
86 			/*
87 			 * It is not a regular file, so don't delete it
88 			 * or allow it to be deleted.  This allows root
89 			 * users to specify /dev/null output file for
90 			 * verification links.
91 			 */
92 			ofl->ofl_flags1 |= FLG_OF1_NONREG;
93 		} else {
94 			/*
95 			 * It's a regular file, so unlink it. In standard
96 			 * Unix fashion, the old file will continue to
97 			 * exist until its link count drops to 0 and no
98 			 * process has the file open. In the meantime, we
99 			 * create a new file (inode) under the same name,
100 			 * available for new use.
101 			 *
102 			 * The advantage of this policy is that creating
103 			 * a new executable or sharable library does not
104 			 * corrupt existing processes using the old file.
105 			 * A possible disadvantage is that if the existing
106 			 * file has a (link_count > 1), the other names will
107 			 * continue to reference the old inode, thus
108 			 * breaking the link.
109 			 */
110 			if ((unlink(ofl->ofl_name) == -1) &&
111 			    (errno != ENOENT)) {
112 				int err = errno;
113 
114 				eprintf(ofl->ofl_lml, ERR_FATAL,
115 					MSG_INTL(MSG_SYS_UNLINK),
116 					ofl->ofl_name, strerror(err));
117 				return (S_ERROR);
118 			}
119 		}
120 	}
121 
122 	/*
123 	 * Determine the required file mode from the type of output file we
124 	 * are creating.
125 	 */
126 	mode = (ofl->ofl_flags & (FLG_OF_EXEC | FLG_OF_SHAROBJ))
127 		? 0777 : 0666;
128 
129 	/*
130 	 * Open (or create) the output file name (ofl_fd acts as a global
131 	 * flag to ldexit() signifying whether the output file should be
132 	 * removed or not on error).
133 	 */
134 	if ((ofl->ofl_fd = open(ofl->ofl_name, O_RDWR | O_CREAT | O_TRUNC,
135 	    mode)) < 0) {
136 		int	err = errno;
137 
138 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
139 		    ofl->ofl_name, strerror(err));
140 		return (S_ERROR);
141 	}
142 
143 	return (1);
144 }
145 
146 
147 /*
148  * If we are creating a memory model we need to update the present memory image.
149  * First we need to call elf_update(ELF_C_NULL) which will calculate the offsets
150  * of each section and its associated data buffers.  From this information we
151  * can then determine what padding is required.
152  * Two actions are necessary to convert the present disc image into a memory
153  * image:
154  *
155  *  o	Loadable segments must be padded so that the next segments virtual
156  *	address and file offset are the same.
157  *
158  *  o	NOBITS sections must be converted into allocated, null filled sections.
159  */
160 static uintptr_t
161 pad_outfile(Ofl_desc *ofl)
162 {
163 	Listnode	*lnp;
164 	off_t		offset;
165 	Elf_Scn		*oscn = 0;
166 	Sg_desc		*sgp;
167 	Ehdr		*ehdr;
168 
169 	/*
170 	 * Update all the elf structures.  This will assign offsets to the
171 	 * section headers and data buffers as they relate to the new image.
172 	 */
173 	if (elf_update(ofl->ofl_welf, ELF_C_NULL) == -1) {
174 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
175 		    ofl->ofl_name);
176 		return (S_ERROR);
177 	}
178 	if ((ehdr = elf_getehdr(ofl->ofl_welf)) == NULL) {
179 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETEHDR),
180 		    ofl->ofl_name);
181 		return (S_ERROR);
182 	}
183 
184 	/*
185 	 * Initialize the offset by skipping the Elf header and program
186 	 * headers.
187 	 */
188 	offset = ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize);
189 
190 	/*
191 	 * Traverse the segment list looking for loadable segments.
192 	 */
193 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp, sgp)) {
194 		Phdr	*phdr = &(sgp->sg_phdr);
195 		Os_desc	**ospp, *osp;
196 		Aliste	off;
197 
198 		/*
199 		 * If we've already processed a loadable segment, the `scn'
200 		 * variable will be initialized to the last section that was
201 		 * part of that segment.  Add sufficient padding to this section
202 		 * to cause the next segments virtual address and file offset to
203 		 * be the same.
204 		 */
205 		if (oscn && (phdr->p_type == PT_LOAD)) {
206 			Elf_Data *	data;
207 			size_t 		size;
208 
209 			size = (size_t)(S_ROUND(offset, phdr->p_align) -
210 			    offset);
211 
212 			if ((data = elf_newdata(oscn)) == NULL) {
213 				eprintf(ofl->ofl_lml, ERR_ELF,
214 				    MSG_INTL(MSG_ELF_NEWDATA), ofl->ofl_name);
215 				return (S_ERROR);
216 			}
217 			if ((data->d_buf = libld_calloc(size, 1)) == 0)
218 				return (S_ERROR);
219 
220 			data->d_type = ELF_T_BYTE;
221 			data->d_size = size;
222 			data->d_align = 1;
223 			data->d_version = ofl->ofl_dehdr->e_version;
224 		}
225 
226 		/*
227 		 * Traverse the output sections for this segment calculating the
228 		 * offset of each section. Retain the final section descriptor
229 		 * as this will be where any padding buffer will be added.
230 		 */
231 		for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
232 			Shdr	*shdr;
233 
234 			osp = *ospp;
235 			shdr = osp->os_shdr;
236 
237 			offset = (off_t)S_ROUND(offset, shdr->sh_addralign);
238 			offset += shdr->sh_size;
239 
240 			/*
241 			 * If this is a NOBITS output section convert all of
242 			 * its associated input sections into real, null filled,
243 			 * data buffers, and change the section to PROGBITS.
244 			 */
245 			if (shdr->sh_type == SHT_NOBITS)
246 				shdr->sh_type = SHT_PROGBITS;
247 		}
248 
249 		/*
250 		 * If this is a loadable segment retain the last output section
251 		 * descriptor.  This acts both as a flag that a loadable
252 		 * segment has been seen, and as the segment to which a padding
253 		 * buffer will be added.
254 		 */
255 		if (phdr->p_type == PT_LOAD)
256 			oscn =	osp->os_scn;
257 	}
258 	return (1);
259 }
260 
261 /*
262  * Create the elf structures that allow the input data to be associated with the
263  * new image:
264  *
265  *	o	define the new elf image using elf_begin(),
266  *
267  *	o	obtain an elf header for the image,
268  *
269  *	o	traverse the input segments and create a program header array
270  *		to define the required segments,
271  *
272  *	o 	traverse the output sections for each segment assigning a new
273  *		section descriptor and section header for each,
274  *
275  *	o	traverse the input sections associated with each output section
276  *		and assign a new data descriptor to each (each output section
277  *		becomes a linked list of input data buffers).
278  */
279 uintptr_t
280 ld_create_outfile(Ofl_desc *ofl)
281 {
282 	Listnode	*lnp1;
283 	Sg_desc		*sgp;
284 	Os_desc		**ospp;
285 	Is_desc		*isp;
286 	Elf_Scn		*scn;
287 	Elf_Data	*tlsdata = 0;
288 	Shdr		*shdr;
289 	Aliste		off;
290 	Word		flags = ofl->ofl_flags;
291 	size_t		ndx = 0, fndx = 0;
292 	Elf_Cmd		cmd;
293 	Boolean		fixalign = FALSE;
294 	int		fd, nseg = 0, shidx = 0, dataidx = 0, ptloadidx = 0;
295 
296 	/*
297 	 * If DF_1_NOHDR was set in map_parse() or FLG_OF1_VADDR was set,
298 	 * we need to do alignment adjustment.
299 	 */
300 	if ((ofl->ofl_flags1 & FLG_OF1_VADDR) ||
301 	    (ofl->ofl_dtflags_1 & DF_1_NOHDR)) {
302 		fixalign = TRUE;
303 	}
304 
305 	if (flags & FLG_OF_MEMORY) {
306 		cmd = ELF_C_IMAGE;
307 		fd = 0;
308 	} else {
309 		fd = ofl->ofl_fd;
310 		cmd = ELF_C_WRITE;
311 	}
312 
313 	/*
314 	 * If there are any ordered section, handle them here.
315 	 */
316 	if ((ofl->ofl_ordered.head != NULL) &&
317 	    (ld_sort_ordered(ofl) == S_ERROR))
318 		return (S_ERROR);
319 
320 	/*
321 	 * Tell the access library about our new temporary file.
322 	 */
323 	if ((ofl->ofl_welf = elf_begin(fd, cmd, 0)) == NULL) {
324 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN),
325 		    ofl->ofl_name);
326 		return (S_ERROR);
327 	}
328 
329 	/*
330 	 * Obtain a new Elf header.
331 	 */
332 	if ((ofl->ofl_nehdr = elf_newehdr(ofl->ofl_welf)) == NULL) {
333 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_NEWEHDR),
334 		    ofl->ofl_name);
335 		return (S_ERROR);
336 	}
337 	ofl->ofl_nehdr->e_machine = ofl->ofl_dehdr->e_machine;
338 
339 	DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
340 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
341 		int	frst = 0;
342 		Phdr	*phdr = &(sgp->sg_phdr);
343 		Word	ptype = phdr->p_type;
344 
345 		/*
346 		 * Count the number of segments that will go in the program
347 		 * header table. If a segment is empty, ignore it.
348 		 */
349 		if (!(flags & FLG_OF_RELOBJ)) {
350 			if (ptype == PT_PHDR) {
351 				/*
352 				 * If we are generating an interp section (and
353 				 * thus an associated PT_INTERP program header
354 				 * entry) also generate a PT_PHDR program header
355 				 * entry.  This allows the kernel to generate
356 				 * the appropriate aux vector entries to pass to
357 				 * the interpreter (refer to exec/elf/elf.c).
358 				 * Note that if an image was generated with an
359 				 * interp section, but no associated PT_PHDR
360 				 * program header entry, the kernel will simply
361 				 * pass the interpreter an open file descriptor
362 				 * when the image is executed).
363 				 */
364 				if (ofl->ofl_osinterp)
365 					nseg++;
366 			} else if (ptype == PT_INTERP) {
367 				if (ofl->ofl_osinterp)
368 					nseg++;
369 			} else if (ptype == PT_DYNAMIC) {
370 				if (flags & FLG_OF_DYNAMIC)
371 					nseg++;
372 			} else if (ptype == PT_TLS) {
373 				if (flags & FLG_OF_TLSPHDR)
374 					nseg++;
375 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
376 			} else if (ptype == PT_SUNW_UNWIND) {
377 				if (ofl->ofl_unwindhdr)
378 					nseg++;
379 #endif
380 			} else if (ptype == PT_SUNWBSS) {
381 				if (ofl->ofl_issunwbss)
382 					nseg++;
383 			} else if (ptype == PT_SUNWSTACK) {
384 					nseg++;
385 			} else if (ptype == PT_SUNWDTRACE) {
386 				if (ofl->ofl_dtracesym)
387 					nseg++;
388 			} else if (ptype == PT_SUNWCAP) {
389 				if (ofl->ofl_oscap)
390 					nseg++;
391 			} else if ((sgp->sg_osdescs != NULL) ||
392 			    (sgp->sg_flags & FLG_SG_EMPTY)) {
393 				if (((sgp->sg_flags & FLG_SG_EMPTY) == 0) &&
394 				    ((sgp->sg_flags & FLG_SG_PHREQ) == 0)) {
395 					/*
396 					 * If this is a segment for which
397 					 * we are not making a program header,
398 					 * don't increment nseg
399 					 */
400 					ptype = (sgp->sg_phdr).p_type = PT_NULL;
401 				} else if (ptype != PT_NULL)
402 					nseg++;
403 			}
404 		}
405 
406 		/*
407 		 * If the first loadable segment has the ?N flag,
408 		 * then ?N will be on.
409 		 */
410 		if ((ptype == PT_LOAD) && (ptloadidx == 0)) {
411 			ptloadidx++;
412 			if (sgp->sg_flags & FLG_SG_NOHDR) {
413 				fixalign = TRUE;
414 				ofl->ofl_dtflags_1 |= DF_1_NOHDR;
415 			}
416 		}
417 
418 		shidx = 0;
419 		for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
420 			Listnode	*lnp2;
421 			Os_desc		*osp = *ospp;
422 
423 			shidx++;
424 
425 			/*
426 			 * Get a section descriptor for the section.
427 			 */
428 			if ((scn = elf_newscn(ofl->ofl_welf)) == NULL) {
429 				eprintf(ofl->ofl_lml, ERR_ELF,
430 				    MSG_INTL(MSG_ELF_NEWSCN), ofl->ofl_name);
431 				return (S_ERROR);
432 			}
433 			osp->os_scn = scn;
434 
435 			/*
436 			 * Get a new section header table entry and copy the
437 			 * pertinent information from the in-core descriptor.
438 			 * As we had originally allocated the section header
439 			 * (refer place_section()) we might as well free it up.
440 			 */
441 			if ((shdr = elf_getshdr(scn)) == NULL) {
442 				eprintf(ofl->ofl_lml, ERR_ELF,
443 				    MSG_INTL(MSG_ELF_GETSHDR), ofl->ofl_name);
444 				return (S_ERROR);
445 			}
446 			*shdr = *(osp->os_shdr);
447 
448 			if ((fixalign == TRUE) && (ptype == PT_LOAD) &&
449 			    (shidx == 1))
450 				sgp->sg_fscn = scn;
451 
452 			osp->os_shdr = shdr;
453 
454 			/*
455 			 * Knock off the SHF_ORDERED & SHF_LINK_ORDER flags.
456 			 */
457 			osp->os_shdr->sh_flags &= ~ALL_SHF_ORDER;
458 
459 			/*
460 			 * If we are not building a RELOBJ - we strip
461 			 * off the SHF_GROUP flag (if present).
462 			 */
463 			if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
464 				osp->os_shdr->sh_flags &= ~SHF_GROUP;
465 
466 			/*
467 			 * If this is a TLS section, save it so that the PT_TLS
468 			 * program header information can be established after
469 			 * the output image has been initialy created.  At this
470 			 * point, all TLS input sections are ordered as they
471 			 * will appear in the output image.
472 			 */
473 			if ((ofl->ofl_flags & FLG_OF_TLSPHDR) &&
474 			    (osp->os_shdr->sh_flags & SHF_TLS)) {
475 				if (list_appendc(&ofl->ofl_ostlsseg, osp) == 0)
476 					return (S_ERROR);
477 			}
478 
479 			dataidx = 0;
480 			for (LIST_TRAVERSE(&(osp->os_isdescs), lnp2, isp)) {
481 				Elf_Data *	data;
482 				Ifl_desc *	ifl = isp->is_file;
483 
484 				/*
485 				 * At this point we know whether a section has
486 				 * been referenced.  If it hasn't, and the whole
487 				 * file hasn't been referenced (which would have
488 				 * been caught in ignore_section_processing()),
489 				 * give a diagnostic (-D unused,detail) or
490 				 * discard the section if -zignore is in effect.
491 				 */
492 				if (ifl &&
493 				    (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
494 				    ((ptype == PT_LOAD) &&
495 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
496 				    (isp->is_shdr->sh_size > 0)))) {
497 					Lm_list	*lml = ofl->ofl_lml;
498 
499 					if (ifl->ifl_flags & FLG_IF_IGNORE) {
500 					    isp->is_flags |= FLG_IS_DISCARD;
501 					    DBG_CALL(Dbg_unused_sec(lml, isp));
502 					    continue;
503 					} else
504 					    DBG_CALL(Dbg_unused_sec(lml, isp));
505 				}
506 
507 				dataidx++;
508 
509 				/*
510 				 * If this section provides no data, and isn't
511 				 * referenced, then it can be discarded as well.
512 				 * Note, if this is the first input section
513 				 * associated to an output section, let it
514 				 * through, there may be a legitimate reason why
515 				 * the user wants a null section.  Discarding
516 				 * additional sections is intended to remove the
517 				 * empty clutter the compilers have a habit of
518 				 * creating.  Don't provide an unused diagnostic
519 				 * as these sections aren't typically the users
520 				 * creation.
521 				 */
522 				if (ifl && dataidx &&
523 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
524 				    (isp->is_shdr->sh_size == 0)) {
525 					isp->is_flags |= FLG_IS_DISCARD;
526 					continue;
527 				}
528 
529 				/*
530 				 * Create new output data buffers for each of
531 				 * the input data buffers, thus linking the new
532 				 * buffers to the new elf output structures.
533 				 * Simply make the new data buffers point to
534 				 * the old data.
535 				 */
536 				if ((data = elf_newdata(scn)) == NULL) {
537 					eprintf(ofl->ofl_lml, ERR_ELF,
538 					    MSG_INTL(MSG_ELF_NEWDATA),
539 					    ofl->ofl_name);
540 					return (S_ERROR);
541 				}
542 				*data = *(isp->is_indata);
543 
544 				if ((fixalign == TRUE) && (ptype == PT_LOAD) &&
545 				    (shidx == 1) && (dataidx == 1)) {
546 					data->d_align = sgp->sg_addralign;
547 				}
548 				isp->is_indata = data;
549 
550 				/*
551 				 * Save the first TLS data buffer, as this is
552 				 * the start of the TLS segment. Realign this
553 				 * buffer based on the alignment requirements
554 				 * of all the TLS input sections.
555 				 */
556 				if ((ofl->ofl_flags & FLG_OF_TLSPHDR) &&
557 				    (isp->is_shdr->sh_flags & SHF_TLS)) {
558 					if (tlsdata == 0)
559 						tlsdata = data;
560 					tlsdata->d_align =
561 					    ld_lcm(tlsdata->d_align,
562 					    isp->is_shdr->sh_addralign);
563 				}
564 
565 #if	defined(_ELF64) && defined(_ILP32)
566 				/*
567 				 * 4106312, the 32-bit ELF64 version of ld
568 				 * needs to be able to create large .bss
569 				 * sections.  The d_size member of Elf_Data
570 				 * only allows 32-bits in _ILP32, so we build
571 				 * multiple data-items that each fit into 32-
572 				 * bits.  libelf (4106398) can summ these up
573 				 * into a 64-bit quantity.  This only works
574 				 * for NOBITS sections which don't have any
575 				 * real data to maintain and don't require
576 				 * large file support.
577 				 */
578 				if (isp->is_shdr->sh_type == SHT_NOBITS) {
579 					Xword sz = isp->is_shdr->sh_size;
580 
581 					while (sz >> 32) {
582 						data->d_size = SIZE_MAX;
583 						sz -= (Xword)SIZE_MAX;
584 						if ((data =
585 						    elf_newdata(scn)) == NULL)
586 							return (S_ERROR);
587 					}
588 					data->d_size = (size_t)sz;
589 				}
590 #endif
591 
592 				/*
593 				 * If this segment requires rounding realign the
594 				 * first data buffer associated with the first
595 				 * section.
596 				 */
597 				if ((frst++ == 0) &&
598 				    (sgp->sg_flags & FLG_SG_ROUND)) {
599 					Xword    align;
600 
601 					if (data->d_align)
602 						align = (Xword)
603 						    S_ROUND(data->d_align,
604 						    sgp->sg_round);
605 					else
606 						align = sgp->sg_round;
607 
608 					data->d_align = (size_t)align;
609 				}
610 			}
611 
612 			/*
613 			 * Clear the szoutrels counter so that it can be used
614 			 * again in the building of relocs.  See machrel.c.
615 			 */
616 			osp->os_szoutrels = 0;
617 		}
618 	}
619 
620 	/*
621 	 * Build an empty PHDR.
622 	 */
623 	if (nseg) {
624 		if ((ofl->ofl_phdr = elf_newphdr(ofl->ofl_welf,
625 		    nseg)) == NULL) {
626 			eprintf(ofl->ofl_lml, ERR_ELF,
627 			    MSG_INTL(MSG_ELF_NEWPHDR), ofl->ofl_name);
628 			return (S_ERROR);
629 		}
630 	}
631 
632 	/*
633 	 * If we need to generate a memory model, pad the image.
634 	 */
635 	if (flags & FLG_OF_MEMORY) {
636 		if (pad_outfile(ofl) == S_ERROR)
637 			return (S_ERROR);
638 	}
639 
640 	/*
641 	 * After all the basic input file processing, all data pointers are
642 	 * referencing two types of memory:
643 	 *
644 	 *	o	allocated memory, ie. elf structures, internal link
645 	 *		editor structures, and any new sections that have been
646 	 *		created.
647 	 *
648 	 *	o	original input file mmap'ed memory, ie. the actual data
649 	 *		sections of the input file images.
650 	 *
651 	 * Up until now, the only memory modifications have been carried out on
652 	 * the allocated memory.  Before carrying out any relocations, write the
653 	 * new output file image and reassign any necessary data pointers to the
654 	 * output files memory image.  This insures that any relocation
655 	 * modifications are made to the output file image and not to the input
656 	 * file image, thus preventing the creation of dirty pages and reducing
657 	 * the overall swap space requirement.
658 	 *
659 	 * Write out the elf structure so as to create the new file image.
660 	 */
661 	if ((ofl->ofl_size = (size_t)elf_update(ofl->ofl_welf,
662 	    ELF_C_WRIMAGE)) == (size_t)-1) {
663 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
664 		    ofl->ofl_name);
665 		return (S_ERROR);
666 	}
667 
668 	/*
669 	 * Initialize the true `ofl' information with the memory images address
670 	 * and size.  This will be used to write() out the image once any
671 	 * relocation processing has been completed.  We also use this image
672 	 * information to setup a new Elf descriptor, which is used to obtain
673 	 * all the necessary elf pointers within the new output image.
674 	 */
675 	if ((ofl->ofl_elf = elf_begin(0, ELF_C_IMAGE,
676 	    ofl->ofl_welf)) == NULL) {
677 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN),
678 		    ofl->ofl_name);
679 		return (S_ERROR);
680 	}
681 	if ((ofl->ofl_nehdr = elf_getehdr(ofl->ofl_elf)) == NULL) {
682 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETEHDR),
683 		    ofl->ofl_name);
684 		return (S_ERROR);
685 	}
686 	if (!(flags & FLG_OF_RELOBJ))
687 		if ((ofl->ofl_phdr = elf_getphdr(ofl->ofl_elf)) == NULL) {
688 			eprintf(ofl->ofl_lml, ERR_ELF,
689 			    MSG_INTL(MSG_ELF_GETPHDR), ofl->ofl_name);
690 			return (S_ERROR);
691 		}
692 
693 	/*
694 	 * Reinitialize the section descriptors, section headers and obtain new
695 	 * output data buffer pointers (these will be used to perform any
696 	 * relocations).
697 	 */
698 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
699 		Phdr	*_phdr = &(sgp->sg_phdr);
700 		Os_desc	**ospp;
701 		Aliste	off;
702 		Boolean	recorded = FALSE;
703 
704 		for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
705 			Os_desc	*osp = *ospp;
706 
707 			if ((osp->os_scn = elf_getscn(ofl->ofl_elf, ++ndx)) ==
708 			    NULL) {
709 				eprintf(ofl->ofl_lml, ERR_ELF,
710 				    MSG_INTL(MSG_ELF_GETSCN), ofl->ofl_name,
711 				    ndx);
712 				return (S_ERROR);
713 			}
714 			if ((osp->os_shdr = elf_getshdr(osp->os_scn)) ==
715 			    NULL) {
716 				eprintf(ofl->ofl_lml, ERR_ELF,
717 				    MSG_INTL(MSG_ELF_GETSHDR), ofl->ofl_name);
718 				return (S_ERROR);
719 			}
720 			if ((fixalign == TRUE) && (sgp->sg_fscn != 0) &&
721 			    (recorded == FALSE)) {
722 				Elf_Scn *scn;
723 
724 				scn = sgp->sg_fscn;
725 				if ((fndx = elf_ndxscn(scn)) == SHN_UNDEF) {
726 					eprintf(ofl->ofl_lml, ERR_ELF,
727 					    MSG_INTL(MSG_ELF_NDXSCN),
728 					    ofl->ofl_name);
729 					return (S_ERROR);
730 				}
731 				if (ndx == fndx) {
732 					sgp->sg_fscn = osp->os_scn;
733 					recorded = TRUE;
734 				}
735 			}
736 
737 			if ((osp->os_outdata =
738 			    elf_getdata(osp->os_scn, NULL)) == NULL) {
739 				eprintf(ofl->ofl_lml, ERR_ELF,
740 				    MSG_INTL(MSG_ELF_GETDATA), ofl->ofl_name);
741 				return (S_ERROR);
742 			}
743 
744 			/*
745 			 * If this section is part of a loadable segment insure
746 			 * that the segments alignment is appropriate.
747 			 */
748 			if (_phdr->p_type == PT_LOAD) {
749 				_phdr->p_align = ld_lcm(_phdr->p_align,
750 				    osp->os_shdr->sh_addralign);
751 			}
752 		}
753 	}
754 	return (1);
755 }
756