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