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