xref: /titanic_41/usr/src/cmd/sgs/libld/common/outfile.c (revision d92fc07239af943d62e4e67791879707ec89e7d3)
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 2009 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*
31  * This file contains the functions responsible for opening the output file
32  * image, associating the appropriate input elf structures with the new image,
33  * and obtaining new elf structures to define the new image.
34  */
35 #include	<stdio.h>
36 #include	<sys/stat.h>
37 #include	<fcntl.h>
38 #include	<link.h>
39 #include	<errno.h>
40 #include	<string.h>
41 #include	<limits.h>
42 #include	<debug.h>
43 #include	<unistd.h>
44 #include	"msg.h"
45 #include	"_libld.h"
46 
47 /*
48  * Determine a least common multiplier.  Input sections contain an alignment
49  * requirement, which elf_update() uses to insure that the section is aligned
50  * correctly off of the base of the elf image.  We must also insure that the
51  * sections mapping is congruent with this alignment requirement.  For each
52  * input section associated with a loadable segment determine whether the
53  * segments alignment must be adjusted to compensate for a sections alignment
54  * requirements.
55  */
56 Xword
57 ld_lcm(Xword a, Xword b)
58 {
59 	Xword	_r, _a, _b;
60 
61 	if ((_a = a) == 0)
62 		return (b);
63 	if ((_b = b) == 0)
64 		return (a);
65 
66 	if (_a > _b)
67 		_a = b, _b = a;
68 	while ((_r = _b % _a) != 0)
69 		_b = _a, _a = _r;
70 	return ((a / _a) * b);
71 }
72 
73 /*
74  * Open the output file and insure the correct access modes.
75  */
76 uintptr_t
77 ld_open_outfile(Ofl_desc * ofl)
78 {
79 	mode_t		mode;
80 	struct stat	status;
81 
82 	/*
83 	 * Determine the required file mode from the type of output file we
84 	 * are creating.
85 	 */
86 	mode = (ofl->ofl_flags & (FLG_OF_EXEC | FLG_OF_SHAROBJ))
87 	    ? 0777 : 0666;
88 
89 	/* Determine if the output file already exists */
90 	if (stat(ofl->ofl_name, &status) == 0) {
91 		if ((status.st_mode & S_IFMT) != S_IFREG) {
92 			/*
93 			 * It is not a regular file, so don't delete it
94 			 * or allow it to be deleted.  This allows root
95 			 * users to specify /dev/null output file for
96 			 * verification links.
97 			 */
98 			ofl->ofl_flags1 |= FLG_OF1_NONREG;
99 		} else {
100 			/*
101 			 * It's a regular file, so unlink it. In standard
102 			 * Unix fashion, the old file will continue to
103 			 * exist until its link count drops to 0 and no
104 			 * process has the file open. In the meantime, we
105 			 * create a new file (inode) under the same name,
106 			 * available for new use.
107 			 *
108 			 * The advantage of this policy is that creating
109 			 * a new executable or sharable library does not
110 			 * corrupt existing processes using the old file.
111 			 * A possible disadvantage is that if the existing
112 			 * file has a (link_count > 1), the other names will
113 			 * continue to reference the old inode, thus
114 			 * breaking the link.
115 			 *
116 			 * A subtlety here is that POSIX says we are not
117 			 * supposed to replace a non-writable file, which
118 			 * is something that unlink() is happy to do. The
119 			 * only 100% reliable test against this is to open
120 			 * the file for non-destructive write access. If the
121 			 * open succeeds, we are clear to unlink it, and if
122 			 * not, then the error generated is the error we
123 			 * need to report.
124 			 */
125 			if ((ofl->ofl_fd = open(ofl->ofl_name, O_RDWR,
126 			    mode)) < 0) {
127 				int	err = errno;
128 
129 				if (err != ENOENT) {
130 					eprintf(ofl->ofl_lml, ERR_FATAL,
131 					    MSG_INTL(MSG_SYS_OPEN),
132 					    ofl->ofl_name, strerror(err));
133 					return (S_ERROR);
134 				}
135 			} else {
136 				(void) close(ofl->ofl_fd);
137 			}
138 
139 			if ((unlink(ofl->ofl_name) == -1) &&
140 			    (errno != ENOENT)) {
141 				int err = errno;
142 
143 				eprintf(ofl->ofl_lml, ERR_FATAL,
144 				    MSG_INTL(MSG_SYS_UNLINK),
145 				    ofl->ofl_name, strerror(err));
146 				return (S_ERROR);
147 			}
148 		}
149 	}
150 
151 	/*
152 	 * Open (or create) the output file name (ofl_fd acts as a global
153 	 * flag to ldexit() signifying whether the output file should be
154 	 * removed or not on error).
155 	 */
156 	if ((ofl->ofl_fd = open(ofl->ofl_name, O_RDWR | O_CREAT | O_TRUNC,
157 	    mode)) < 0) {
158 		int	err = errno;
159 
160 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
161 		    ofl->ofl_name, strerror(err));
162 		return (S_ERROR);
163 	}
164 
165 	return (1);
166 }
167 
168 
169 /*
170  * If we are creating a memory model we need to update the present memory image.
171  * Use elf_update(ELF_C_NULL) to calculate the offset of each section and their
172  * associated data buffers.  From this information determine what padding is
173  * required.
174  *
175  * Two actions are necessary to convert the present disc image into a memory
176  * image:
177  *
178  *  -	Loadable segments must be padded so that the next segment virtual
179  *	address and file offset are the same.
180  *
181  *  -	NOBITS sections must be converted into allocated, null filled sections.
182  */
183 static uintptr_t
184 pad_outfile(Ofl_desc *ofl)
185 {
186 	Aliste		idx1;
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 (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
217 		Phdr	*phdr = &(sgp->sg_phdr);
218 		Os_desc	*osp;
219 		Aliste	idx2;
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, idx2, 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 	 * If not building a relocatable object, remove any of the
323 	 * following flags, as they have been acted upon and are not
324 	 * meaningful in the output:
325 	 *	SHF_ORDERED, SHF_LINK_ORDER, SHF_GROUP
326 	 * For relocatable objects, we allow them to propagate to
327 	 * the output object to be handled by the next linker that
328 	 * sees them.
329 	 */
330 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
331 		osp->os_shdr->sh_flags &= ~(ALL_SHF_ORDER|SHF_GROUP);
332 
333 	/*
334 	 * If this is a TLS section, save it so that the PT_TLS program header
335 	 * information can be established after the output image has been
336 	 * initially created.  At this point, all TLS input sections are ordered
337 	 * as they will appear in the output image.
338 	 */
339 	if ((ofl->ofl_flags & FLG_OF_TLSPHDR) &&
340 	    (osp->os_shdr->sh_flags & SHF_TLS) &&
341 	    (aplist_append(&ofl->ofl_ostlsseg, osp,
342 	    AL_CNT_OFL_OSTLSSEG) == NULL))
343 		return (S_ERROR);
344 
345 	return (0);
346 }
347 
348 /*
349  * Create the elf structures that allow the input data to be associated with the
350  * new image:
351  *
352  *  -	define the new elf image using elf_begin(),
353  *
354  *  -	obtain an elf header for the image,
355  *
356  *  -	traverse the input segments and create a program header array to define
357  *	the required segments,
358  *
359  *  -	traverse the output sections for each segment assigning a new section
360  *	descriptor and section header for each,
361  *
362  *  -	traverse the input sections associated with each output section and
363  *	assign a new data descriptor to each (each output section becomes a
364  *	linked list of input data buffers).
365  */
366 uintptr_t
367 ld_create_outfile(Ofl_desc *ofl)
368 {
369 	Sg_desc		*sgp;
370 	Os_desc		*osp;
371 	Is_desc		*isp;
372 	Elf_Data	*tlsdata = 0;
373 	Aliste		idx1;
374 	ofl_flag_t	flags = ofl->ofl_flags;
375 	ofl_flag_t	flags1 = ofl->ofl_flags1;
376 	size_t		ndx;
377 	Elf_Cmd		cmd;
378 	Boolean		fixalign = FALSE;
379 	int		fd, nseg = 0, shidx, dataidx, ptloadidx = 0;
380 
381 	DBG_CALL(Dbg_basic_create(ofl->ofl_lml));
382 
383 	/*
384 	 * If DF_1_NOHDR was set in map_parse() or FLG_OF1_VADDR was set,
385 	 * we need to do alignment adjustment.
386 	 */
387 	if ((flags1 & FLG_OF1_VADDR) ||
388 	    (ofl->ofl_dtflags_1 & DF_1_NOHDR)) {
389 		fixalign = TRUE;
390 	}
391 
392 	if (flags1 & FLG_OF1_MEMORY) {
393 		cmd = ELF_C_IMAGE;
394 		fd = 0;
395 	} else {
396 		fd = ofl->ofl_fd;
397 		cmd = ELF_C_WRITE;
398 	}
399 
400 	/*
401 	 * If there are any ordered sections, handle them here.
402 	 */
403 	if ((ofl->ofl_ordered != NULL) &&
404 	    (ld_sort_ordered(ofl) == S_ERROR))
405 		return (S_ERROR);
406 
407 	/*
408 	 * Tell the access library about our new temporary file.
409 	 */
410 	if ((ofl->ofl_welf = elf_begin(fd, cmd, 0)) == NULL) {
411 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN),
412 		    ofl->ofl_name);
413 		return (S_ERROR);
414 	}
415 
416 	/*
417 	 * Obtain a new Elf header.
418 	 */
419 	if ((ofl->ofl_nehdr = elf_newehdr(ofl->ofl_welf)) == NULL) {
420 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_NEWEHDR),
421 		    ofl->ofl_name);
422 		return (S_ERROR);
423 	}
424 	ofl->ofl_nehdr->e_machine = ofl->ofl_dehdr->e_machine;
425 
426 	DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
427 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
428 		int	frst = 0;
429 		Phdr	*phdr = &(sgp->sg_phdr);
430 		Word	ptype = phdr->p_type;
431 		Aliste	idx2;
432 
433 		/*
434 		 * Count the number of segments that will go in the program
435 		 * header table. If a segment is empty, ignore it.
436 		 */
437 		if (!(flags & FLG_OF_RELOBJ)) {
438 			/*
439 			 * If the program header type belongs to the os range,
440 			 * the resulting object is ELFOSABI_SOLARIS.
441 			 */
442 			if ((ptype >= PT_LOOS) && (ptype <= PT_HIOS))
443 				ofl->ofl_flags |= FLG_OF_OSABI;
444 
445 			if (ptype == PT_PHDR) {
446 				/*
447 				 * If we are generating an interp section (and
448 				 * thus an associated PT_INTERP program header
449 				 * entry) also generate a PT_PHDR program header
450 				 * entry.  This allows the kernel to generate
451 				 * the appropriate aux vector entries to pass to
452 				 * the interpreter (refer to exec/elf/elf.c).
453 				 * Note that if an image was generated with an
454 				 * interp section, but no associated PT_PHDR
455 				 * program header entry, the kernel will simply
456 				 * pass the interpreter an open file descriptor
457 				 * when the image is executed).
458 				 */
459 				if (ofl->ofl_osinterp)
460 					nseg++;
461 			} else if (ptype == PT_INTERP) {
462 				if (ofl->ofl_osinterp)
463 					nseg++;
464 			} else if (ptype == PT_DYNAMIC) {
465 				if (flags & FLG_OF_DYNAMIC)
466 					nseg++;
467 			} else if (ptype == PT_TLS) {
468 				if (flags & FLG_OF_TLSPHDR)
469 					nseg++;
470 			} else if (ptype == PT_SUNW_UNWIND) {
471 				if (ofl->ofl_unwindhdr)
472 					nseg++;
473 			} else if (ptype == PT_SUNWDTRACE) {
474 				if (ofl->ofl_dtracesym)
475 					nseg++;
476 			} else if (ptype == PT_SUNWCAP) {
477 				if (ofl->ofl_oscap)
478 					nseg++;
479 			} else if (sgp->sg_flags & FLG_SG_EMPTY) {
480 					nseg++;
481 			} else if (sgp->sg_osdescs != NULL) {
482 				if ((sgp->sg_flags & FLG_SG_PHREQ) == 0) {
483 					/*
484 					 * If this is a segment for which
485 					 * we are not making a program header,
486 					 * don't increment nseg
487 					 */
488 					ptype = (sgp->sg_phdr).p_type = PT_NULL;
489 				} else if (ptype != PT_NULL)
490 					nseg++;
491 			}
492 		}
493 
494 		/*
495 		 * Establish any processing unique to the first loadable
496 		 * segment.
497 		 */
498 		if ((ptype == PT_LOAD) && (ptloadidx == 0)) {
499 			ptloadidx++;
500 
501 			/*
502 			 * If the first loadable segment has the ?N flag then
503 			 * alignments of following segments need to be fixed,
504 			 * plus a .dynamic FLAGS1 setting is required.
505 			 */
506 			if (sgp->sg_flags & FLG_SG_NOHDR) {
507 				fixalign = TRUE;
508 				ofl->ofl_dtflags_1 |= DF_1_NOHDR;
509 			}
510 		}
511 
512 		shidx = 0;
513 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
514 			Aliste	idx3;
515 			int	os_isdescs_idx;
516 
517 			dataidx = 0;
518 
519 			OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) {
520 				Elf_Data	*data;
521 				Ifl_desc	*ifl = isp->is_file;
522 
523 				/*
524 				 * An input section in the list that has
525 				 * been previously marked to be discarded
526 				 * should be completely ignored.
527 				 */
528 				if (isp->is_flags & FLG_IS_DISCARD)
529 					continue;
530 
531 				/*
532 				 * At this point we know whether a section has
533 				 * been referenced.  If it hasn't, and the whole
534 				 * file hasn't been referenced (which would have
535 				 * been caught in ignore_section_processing()),
536 				 * give a diagnostic (-D unused,detail) or
537 				 * discard the section if -zignore is in effect.
538 				 */
539 				if (ifl &&
540 				    (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
541 				    ((ptype == PT_LOAD) &&
542 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
543 				    (isp->is_shdr->sh_size > 0)))) {
544 					Lm_list	*lml = ofl->ofl_lml;
545 
546 					if (ifl->ifl_flags & FLG_IF_IGNORE) {
547 						isp->is_flags |= FLG_IS_DISCARD;
548 						DBG_CALL(Dbg_unused_sec(lml,
549 						    isp));
550 						continue;
551 					} else {
552 						DBG_CALL(Dbg_unused_sec(lml,
553 						    isp));
554 					}
555 				}
556 
557 				/*
558 				 * If this section provides no data, and isn't
559 				 * referenced, then it can be discarded as well.
560 				 * Note, if this is the first input section
561 				 * associated to an output section, let it
562 				 * through, there may be a legitimate reason why
563 				 * the user wants a null section.  Discarding
564 				 * additional sections is intended to remove the
565 				 * empty clutter the compilers have a habit of
566 				 * creating.  Don't provide an unused diagnostic
567 				 * as these sections aren't typically the users
568 				 * creation.
569 				 */
570 				if (ifl && dataidx &&
571 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
572 				    (isp->is_shdr->sh_size == 0)) {
573 					isp->is_flags |= FLG_IS_DISCARD;
574 					continue;
575 				}
576 
577 				/*
578 				 * The first input section triggers the creation
579 				 * of the associated output section.
580 				 */
581 				if (osp->os_scn == NULL) {
582 					shidx++;
583 
584 					if (create_outsec(ofl, sgp, osp, ptype,
585 					    shidx, fixalign) == S_ERROR)
586 						return (S_ERROR);
587 				}
588 
589 				dataidx++;
590 
591 				/*
592 				 * Create a new output data buffer for each
593 				 * input data buffer, thus linking the new
594 				 * buffers to the new elf output structures.
595 				 * Simply make the new data buffers point to
596 				 * the old data.
597 				 */
598 				if ((data = elf_newdata(osp->os_scn)) == NULL) {
599 					eprintf(ofl->ofl_lml, ERR_ELF,
600 					    MSG_INTL(MSG_ELF_NEWDATA),
601 					    ofl->ofl_name);
602 					return (S_ERROR);
603 				}
604 				*data = *(isp->is_indata);
605 				isp->is_indata = data;
606 
607 				if ((fixalign == TRUE) && (ptype == PT_LOAD) &&
608 				    (shidx == 1) && (dataidx == 1))
609 					data->d_align = sgp->sg_addralign;
610 
611 				/*
612 				 * Save the first TLS data buffer, as this is
613 				 * the start of the TLS segment. Realign this
614 				 * buffer based on the alignment requirements
615 				 * of all the TLS input sections.
616 				 */
617 				if ((flags & FLG_OF_TLSPHDR) &&
618 				    (isp->is_shdr->sh_flags & SHF_TLS)) {
619 					if (tlsdata == 0)
620 						tlsdata = data;
621 					tlsdata->d_align =
622 					    ld_lcm(tlsdata->d_align,
623 					    isp->is_shdr->sh_addralign);
624 				}
625 
626 #if	defined(_ELF64) && defined(_ILP32)
627 				/*
628 				 * 4106312, the 32-bit ELF64 version of ld
629 				 * needs to be able to create large .bss
630 				 * sections.  The d_size member of Elf_Data
631 				 * only allows 32-bits in _ILP32, so we build
632 				 * multiple data-items that each fit into 32-
633 				 * bits.  libelf (4106398) can summ these up
634 				 * into a 64-bit quantity.  This only works
635 				 * for NOBITS sections which don't have any
636 				 * real data to maintain and don't require
637 				 * large file support.
638 				 */
639 				if (isp->is_shdr->sh_type == SHT_NOBITS) {
640 					Xword sz = isp->is_shdr->sh_size;
641 
642 					while (sz >> 32) {
643 						data->d_size = SIZE_MAX;
644 						sz -= (Xword)SIZE_MAX;
645 
646 						data = elf_newdata(osp->os_scn);
647 						if (data == NULL)
648 							return (S_ERROR);
649 					}
650 					data->d_size = (size_t)sz;
651 				}
652 #endif
653 
654 				/*
655 				 * If this segment requires rounding realign the
656 				 * first data buffer associated with the first
657 				 * section.
658 				 */
659 				if ((frst++ == 0) &&
660 				    (sgp->sg_flags & FLG_SG_ROUND)) {
661 					Xword    align;
662 
663 					if (data->d_align)
664 						align = (Xword)
665 						    S_ROUND(data->d_align,
666 						    sgp->sg_round);
667 					else
668 						align = sgp->sg_round;
669 
670 					data->d_align = (size_t)align;
671 				}
672 			}
673 
674 			/*
675 			 * Clear the szoutrels counter so that it can be used
676 			 * again in the building of relocs.  See machrel.c.
677 			 */
678 			osp->os_szoutrels = 0;
679 		}
680 	}
681 
682 	/*
683 	 * Did we use ELF features from the osabi range? If so,
684 	 * update the ELF header osabi fields. If this doesn't happen,
685 	 * those fields remain 0, reflecting a generic System V ELF ABI.
686 	 */
687 	if (ofl->ofl_flags & FLG_OF_OSABI) {
688 		ofl->ofl_nehdr->e_ident[EI_OSABI] = ELFOSABI_SOLARIS;
689 		ofl->ofl_nehdr->e_ident[EI_ABIVERSION] = EAV_SUNW_CURRENT;
690 	}
691 
692 	/*
693 	 * Build an empty PHDR.
694 	 */
695 	if (nseg) {
696 		if ((ofl->ofl_phdr = elf_newphdr(ofl->ofl_welf,
697 		    nseg)) == NULL) {
698 			eprintf(ofl->ofl_lml, ERR_ELF,
699 			    MSG_INTL(MSG_ELF_NEWPHDR), ofl->ofl_name);
700 			return (S_ERROR);
701 		}
702 	}
703 
704 	/*
705 	 * If we need to generate a memory model, pad the image.
706 	 */
707 	if (flags1 & FLG_OF1_MEMORY) {
708 		if (pad_outfile(ofl) == S_ERROR)
709 			return (S_ERROR);
710 	}
711 
712 	/*
713 	 * After all the basic input file processing, all data pointers are
714 	 * referencing two types of memory:
715 	 *
716 	 *  -	allocated memory, ie. elf structures, internal link editor
717 	 *	structures, and any new sections that have been created.
718 	 *
719 	 *  -	original input file mmap'ed memory, ie. the actual data
720 	 *	sections of the input file images.
721 	 *
722 	 * Up until now, the only memory modifications have been carried out on
723 	 * the allocated memory.  Before carrying out any relocations, write the
724 	 * new output file image and reassign any necessary data pointers to the
725 	 * output files memory image.  This insures that any relocation
726 	 * modifications are made to the output file image and not to the input
727 	 * file image, thus preventing the creation of dirty pages and reducing
728 	 * the overall swap space requirement.
729 	 *
730 	 * Write out the elf structure so as to create the new file image.
731 	 */
732 	if ((ofl->ofl_size = (size_t)elf_update(ofl->ofl_welf,
733 	    ELF_C_WRIMAGE)) == (size_t)-1) {
734 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
735 		    ofl->ofl_name);
736 		return (S_ERROR);
737 	}
738 
739 	/*
740 	 * Initialize the true `ofl' information with the memory images address
741 	 * and size.  This will be used to write() out the image once any
742 	 * relocation processing has been completed.  We also use this image
743 	 * information to setup a new Elf descriptor, which is used to obtain
744 	 * all the necessary elf pointers within the new output image.
745 	 */
746 	if ((ofl->ofl_elf = elf_begin(0, ELF_C_IMAGE,
747 	    ofl->ofl_welf)) == NULL) {
748 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN),
749 		    ofl->ofl_name);
750 		return (S_ERROR);
751 	}
752 	if ((ofl->ofl_nehdr = elf_getehdr(ofl->ofl_elf)) == NULL) {
753 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETEHDR),
754 		    ofl->ofl_name);
755 		return (S_ERROR);
756 	}
757 	if (!(flags & FLG_OF_RELOBJ))
758 		if ((ofl->ofl_phdr = elf_getphdr(ofl->ofl_elf)) == NULL) {
759 			eprintf(ofl->ofl_lml, ERR_ELF,
760 			    MSG_INTL(MSG_ELF_GETPHDR), ofl->ofl_name);
761 			return (S_ERROR);
762 		}
763 
764 	/*
765 	 * Reinitialize the section descriptors, section headers and obtain new
766 	 * output data buffer pointers (these will be used to perform any
767 	 * relocations).
768 	 */
769 	ndx = 0;
770 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
771 		Phdr	*_phdr = &(sgp->sg_phdr);
772 		Os_desc	*osp;
773 		Aliste	idx2;
774 		Boolean	recorded = FALSE;
775 
776 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
777 			/*
778 			 * Make sure that an output section was originally
779 			 * created.  Input sections that had been marked as
780 			 * discarded may have made an output section
781 			 * unnecessary.  Remove this alist entry so that
782 			 * future output section descriptor processing doesn't
783 			 * have to compensate for this empty section.
784 			 */
785 			if (osp->os_scn == NULL) {
786 				aplist_delete(sgp->sg_osdescs, &idx2);
787 				continue;
788 			}
789 			if ((osp->os_scn =
790 			    elf_getscn(ofl->ofl_elf, ++ndx)) == NULL) {
791 				eprintf(ofl->ofl_lml, ERR_ELF,
792 				    MSG_INTL(MSG_ELF_GETSCN), ofl->ofl_name,
793 				    ndx);
794 				return (S_ERROR);
795 			}
796 			if ((osp->os_shdr =
797 			    elf_getshdr(osp->os_scn)) == NULL) {
798 				eprintf(ofl->ofl_lml, ERR_ELF,
799 				    MSG_INTL(MSG_ELF_GETSHDR), ofl->ofl_name);
800 				return (S_ERROR);
801 			}
802 			if ((fixalign == TRUE) && sgp->sg_fscn &&
803 			    (recorded == FALSE)) {
804 				size_t	fndx;
805 				Elf_Scn *scn;
806 
807 				scn = sgp->sg_fscn;
808 				if ((fndx = elf_ndxscn(scn)) == SHN_UNDEF) {
809 					eprintf(ofl->ofl_lml, ERR_ELF,
810 					    MSG_INTL(MSG_ELF_NDXSCN),
811 					    ofl->ofl_name);
812 					return (S_ERROR);
813 				}
814 				if (ndx == fndx) {
815 					sgp->sg_fscn = osp->os_scn;
816 					recorded = TRUE;
817 				}
818 			}
819 
820 			if ((osp->os_outdata =
821 			    elf_getdata(osp->os_scn, NULL)) == NULL) {
822 				eprintf(ofl->ofl_lml, ERR_ELF,
823 				    MSG_INTL(MSG_ELF_GETDATA), ofl->ofl_name);
824 				return (S_ERROR);
825 			}
826 
827 			/*
828 			 * If this section is part of a loadable segment insure
829 			 * that the segments alignment is appropriate.
830 			 */
831 			if (_phdr->p_type == PT_LOAD) {
832 				_phdr->p_align = ld_lcm(_phdr->p_align,
833 				    osp->os_shdr->sh_addralign);
834 			}
835 		}
836 	}
837 	return (1);
838 }
839