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