xref: /titanic_41/usr/src/cmd/sgs/libld/common/outfile.c (revision 8ba1bcfcc383cbdad998864b5dff1133decd99a6)
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 2005 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 	Elf_Data *	tlsdata = 0;
246 	Shdr *		shdr;
247 	Word		flags = ofl->ofl_flags;
248 	size_t		ndx = 0, fndx = 0;
249 	Elf_Cmd		cmd;
250 	Boolean		fixalign = FALSE;
251 	int		fd, nseg = 0, shidx = 0, dataidx = 0, ptloadidx = 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 		int	frst = 0;
295 		Phdr	*phdr = &(sgp->sg_phdr);
296 		Word	ptype = phdr->p_type;
297 
298 		/*
299 		 * Count the number of segments that will go in the program
300 		 * header table. If a segment is empty, ignore it.
301 		 */
302 		if (!(flags & FLG_OF_RELOBJ)) {
303 			if (ptype == PT_PHDR) {
304 				/*
305 				 * If we are generating an interp section (and
306 				 * thus an associated PT_INTERP program header
307 				 * entry) also generate a PT_PHDR program header
308 				 * entry.  This allows the kernel to generate
309 				 * the appropriate aux vector entries to pass to
310 				 * the interpreter (refer to exec/elf/elf.c).
311 				 * Note that if an image was generated with an
312 				 * interp section, but no associated PT_PHDR
313 				 * program header entry, the kernel will simply
314 				 * pass the interpreter an open file descriptor
315 				 * when the image is executed).
316 				 */
317 				if (ofl->ofl_osinterp)
318 					nseg++;
319 			} else if (ptype == PT_INTERP) {
320 				if (ofl->ofl_osinterp)
321 					nseg++;
322 			} else if (ptype == PT_DYNAMIC) {
323 				if (flags & FLG_OF_DYNAMIC)
324 					nseg++;
325 			} else if (ptype == PT_TLS) {
326 				if (flags & FLG_OF_TLSPHDR)
327 					nseg++;
328 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
329 			} else if (ptype == PT_SUNW_UNWIND) {
330 				if (ofl->ofl_unwindhdr)
331 					nseg++;
332 #endif
333 			} else if (ptype == PT_SUNWBSS) {
334 				if (ofl->ofl_issunwbss)
335 					nseg++;
336 			} else if (ptype == PT_SUNWSTACK) {
337 					nseg++;
338 			} else if (ptype == PT_SUNWDTRACE) {
339 				if (ofl->ofl_dtracesym)
340 					nseg++;
341 			} else if (ptype == PT_SUNWCAP) {
342 				if (ofl->ofl_oscap)
343 					nseg++;
344 			} else if ((sgp->sg_osdescs.head) ||
345 			    (sgp->sg_flags & FLG_SG_EMPTY)) {
346 				if (ptype != PT_NULL)
347 					nseg++;
348 			}
349 		}
350 
351 		/*
352 		 * If the first loadable segment has the ?N flag,
353 		 * then ?N will be on.
354 		 */
355 		if ((ptype == PT_LOAD) && (ptloadidx == 0)) {
356 			ptloadidx++;
357 			if (sgp->sg_flags & FLG_SG_NOHDR) {
358 				fixalign = TRUE;
359 				ofl->ofl_flags1 |= FLG_OF1_NOHDR;
360 			}
361 		}
362 
363 		shidx = 0;
364 		for (LIST_TRAVERSE(&(sgp->sg_osdescs), lnp2, osp)) {
365 			shidx++;
366 
367 			/*
368 			 * Get a section descriptor for the section.
369 			 */
370 			if ((scn = elf_newscn(ofl->ofl_welf)) == NULL) {
371 				eprintf(ERR_ELF, MSG_INTL(MSG_ELF_NEWSCN),
372 				    ofl->ofl_name);
373 				return (S_ERROR);
374 			}
375 			osp->os_scn = scn;
376 
377 			/*
378 			 * Get a new section header table entry and copy the
379 			 * pertinent information from the in-core descriptor.
380 			 * As we had originally allocated the section header
381 			 * (refer place_section()) we might as well free it up.
382 			 */
383 			if ((shdr = elf_getshdr(scn)) == NULL) {
384 				eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
385 				    ofl->ofl_name);
386 				return (S_ERROR);
387 			}
388 			*shdr = *(osp->os_shdr);
389 
390 			if ((fixalign == TRUE) && (ptype == PT_LOAD) &&
391 			    (shidx == 1))
392 				sgp->sg_fscn = scn;
393 
394 			osp->os_shdr = shdr;
395 
396 			/*
397 			 * Knock off the SHF_ORDERED & SHF_LINK_ORDER flags.
398 			 */
399 			osp->os_shdr->sh_flags &= ~ALL_SHF_ORDER;
400 
401 			/*
402 			 * If we are not building a RELOBJ - we strip
403 			 * off the SHF_GROUP flag (if present).
404 			 */
405 			if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
406 				osp->os_shdr->sh_flags &= ~SHF_GROUP;
407 
408 			/*
409 			 * If this is a TLS section, save it so that the PT_TLS
410 			 * program header information can be established after
411 			 * the output image has been initialy created.  At this
412 			 * point, all TLS input sections are ordered as they
413 			 * will appear in the output image.
414 			 */
415 			if ((ofl->ofl_flags & FLG_OF_TLSPHDR) &&
416 			    (osp->os_shdr->sh_flags & SHF_TLS)) {
417 				if (list_appendc(&ofl->ofl_ostlsseg, osp) == 0)
418 					return (S_ERROR);
419 			}
420 
421 			dataidx = 0;
422 			for (LIST_TRAVERSE(&(osp->os_isdescs), lnp3, isp)) {
423 				Elf_Data *	data;
424 				Ifl_desc *	ifl = isp->is_file;
425 
426 				/*
427 				 * At this point we know whether a section has
428 				 * been referenced.  If it hasn't, and the whole
429 				 * file hasn't been referenced (which would have
430 				 * been caught in ignore_section_processing()),
431 				 * give a diagnostic (-D unused,detail) or
432 				 * discard the section if -zignore is in effect.
433 				 */
434 				if (ifl &&
435 				    (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
436 				    ((ptype == PT_LOAD) &&
437 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
438 				    (isp->is_shdr->sh_size > 0)))) {
439 					if (ifl->ifl_flags & FLG_IF_IGNORE) {
440 						isp->is_flags |= FLG_IS_DISCARD;
441 						DBG_CALL(Dbg_unused_sec(isp));
442 						continue;
443 					} else
444 						DBG_CALL(Dbg_unused_sec(isp));
445 				}
446 
447 				dataidx++;
448 
449 				/*
450 				 * If this section provides no data, and isn't
451 				 * referenced, then it can be discarded as well.
452 				 * Note, if this is the first input section
453 				 * associated to an output section, let it
454 				 * through, there may be a legitimate reason why
455 				 * the user wants a null section.  Discarding
456 				 * additional sections is intended to remove the
457 				 * empty clutter the compilers have a habit of
458 				 * creating.  Don't provide an unused diagnostic
459 				 * as these sections aren't typically the users
460 				 * creation.
461 				 */
462 				if (ifl && dataidx &&
463 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
464 				    (isp->is_shdr->sh_size == 0)) {
465 					isp->is_flags |= FLG_IS_DISCARD;
466 					continue;
467 				}
468 
469 				/*
470 				 * Create new output data buffers for each of
471 				 * the input data buffers, thus linking the new
472 				 * buffers to the new elf output structures.
473 				 * Simply make the new data buffers point to
474 				 * the old data.
475 				 */
476 				if ((data = elf_newdata(scn)) == NULL) {
477 					eprintf(ERR_ELF,
478 					    MSG_INTL(MSG_ELF_NEWDATA),
479 					    ofl->ofl_name);
480 					return (S_ERROR);
481 				}
482 				*data = *(isp->is_indata);
483 
484 				if ((fixalign == TRUE) && (ptype == PT_LOAD) &&
485 				    (shidx == 1) && (dataidx == 1)) {
486 					data->d_align = sgp->sg_addralign;
487 				}
488 				isp->is_indata = data;
489 
490 				/*
491 				 * Save the first TLS data buffer, as this is
492 				 * the start of the TLS segment. Realign this
493 				 * buffer based on the alignment requirements
494 				 * of all the TLS input sections.
495 				 */
496 				if ((ofl->ofl_flags & FLG_OF_TLSPHDR) &&
497 				    (isp->is_shdr->sh_flags & SHF_TLS)) {
498 					if (tlsdata == 0)
499 						tlsdata = data;
500 					tlsdata->d_align = lcm(tlsdata->d_align,
501 					    isp->is_shdr->sh_addralign);
502 				}
503 
504 #if	defined(_ELF64) && defined(_ILP32)
505 				/*
506 				 * 4106312, the 32-bit ELF64 version of ld
507 				 * needs to be able to create large .bss
508 				 * sections.  The d_size member of Elf_Data
509 				 * only allows 32-bits in _ILP32, so we build
510 				 * multiple data-items that each fit into 32-
511 				 * bits.  libelf (4106398) can summ these up
512 				 * into a 64-bit quantity.  This only works
513 				 * for NOBITS sections which don't have any
514 				 * real data to maintain and don't require
515 				 * large file support.
516 				 */
517 				if (isp->is_shdr->sh_type == SHT_NOBITS) {
518 					Xword sz = isp->is_shdr->sh_size;
519 
520 					while (sz >> 32) {
521 						data->d_size = SIZE_MAX;
522 						sz -= (Xword)SIZE_MAX;
523 						if ((data =
524 						    elf_newdata(scn)) == NULL)
525 							return (S_ERROR);
526 					}
527 					data->d_size = (size_t)sz;
528 				}
529 #endif
530 
531 				/*
532 				 * If this segment requires rounding realign the
533 				 * first data buffer associated with the first
534 				 * section.
535 				 */
536 				if ((frst++ == 0) &&
537 				    (sgp->sg_flags & FLG_SG_ROUND)) {
538 					Xword    align;
539 
540 					if (data->d_align)
541 						align = (Xword)
542 						    S_ROUND(data->d_align,
543 						    sgp->sg_round);
544 					else
545 						align = sgp->sg_round;
546 
547 					data->d_align = (size_t)align;
548 				}
549 			}
550 
551 			/*
552 			 * Clear the szoutrels counter so that it can be used
553 			 * again in the building of relocs.  See machrel.c.
554 			 */
555 			osp->os_szoutrels = 0;
556 		}
557 	}
558 
559 	/*
560 	 * Build an empty PHDR.
561 	 */
562 	if (nseg) {
563 		if ((ofl->ofl_phdr = elf_newphdr(ofl->ofl_welf,
564 		    nseg)) == NULL) {
565 			eprintf(ERR_ELF, MSG_INTL(MSG_ELF_NEWPHDR),
566 			    ofl->ofl_name);
567 			return (S_ERROR);
568 		}
569 	}
570 
571 	/*
572 	 * If we need to generate a memory model, pad the image.
573 	 */
574 	if (flags & FLG_OF_MEMORY) {
575 		if (pad_outfile(ofl) == S_ERROR)
576 			return (S_ERROR);
577 	}
578 
579 	/*
580 	 * After all the basic input file processing, all data pointers are
581 	 * referencing two types of memory:
582 	 *
583 	 *	o	allocated memory, ie. elf structures, internal link
584 	 *		editor structures, and any new sections that have been
585 	 *		created.
586 	 *
587 	 *	o	original input file mmap'ed memory, ie. the actual data
588 	 *		sections of the input file images.
589 	 *
590 	 * Up until now, the only memory modifications have been carried out on
591 	 * the allocated memory.  Before carrying out any relocations, write the
592 	 * new output file image and reassign any necessary data pointers to the
593 	 * output files memory image.  This insures that any relocation
594 	 * modifications are made to the output file image and not to the input
595 	 * file image, thus preventing the creation of dirty pages and reducing
596 	 * the overall swap space requirement.
597 	 *
598 	 * Write out the elf structure so as to create the new file image.
599 	 */
600 	if ((ofl->ofl_size = (size_t)elf_update(ofl->ofl_welf,
601 	    ELF_C_WRIMAGE)) == (size_t)-1) {
602 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_UPDATE), ofl->ofl_name);
603 		return (S_ERROR);
604 	}
605 
606 	/*
607 	 * Initialize the true `ofl' information with the memory images address
608 	 * and size.  This will be used to write() out the image once any
609 	 * relocation processing has been completed.  We also use this image
610 	 * information to setup a new Elf descriptor, which is used to obtain
611 	 * all the necessary elf pointers within the new output image.
612 	 */
613 	if ((ofl->ofl_elf = elf_begin(0, ELF_C_IMAGE,
614 	    ofl->ofl_welf)) == NULL) {
615 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), ofl->ofl_name);
616 		return (S_ERROR);
617 	}
618 	if ((ofl->ofl_ehdr = elf_getehdr(ofl->ofl_elf)) == NULL) {
619 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETEHDR), ofl->ofl_name);
620 		return (S_ERROR);
621 	}
622 	if (!(flags & FLG_OF_RELOBJ))
623 		if ((ofl->ofl_phdr = elf_getphdr(ofl->ofl_elf)) == NULL) {
624 			eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETPHDR),
625 			    ofl->ofl_name);
626 			return (S_ERROR);
627 		}
628 
629 	/*
630 	 * Reinitialize the section descriptors, section headers and obtain new
631 	 * output data buffer pointers (these will be used to perform any
632 	 * relocations).
633 	 */
634 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
635 		Phdr *	_phdr = &(sgp->sg_phdr);
636 		Boolean	recorded = FALSE;
637 
638 		for (LIST_TRAVERSE(&(sgp->sg_osdescs), lnp2, osp)) {
639 			if ((osp->os_scn = elf_getscn(ofl->ofl_elf, ++ndx)) ==
640 			    NULL) {
641 				eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
642 				    ofl->ofl_name, ndx);
643 				return (S_ERROR);
644 			}
645 			if ((osp->os_shdr = elf_getshdr(osp->os_scn)) ==
646 			    NULL) {
647 				eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
648 				    ofl->ofl_name);
649 				return (S_ERROR);
650 			}
651 			if ((fixalign == TRUE) && (sgp->sg_fscn != 0) &&
652 			    (recorded == FALSE)) {
653 				Elf_Scn *scn;
654 
655 				scn = sgp->sg_fscn;
656 				if ((fndx = elf_ndxscn(scn)) == SHN_UNDEF) {
657 					eprintf(ERR_ELF,
658 					    MSG_INTL(MSG_ELF_NDXSCN),
659 					    ofl->ofl_name);
660 					return (S_ERROR);
661 				}
662 				if (ndx == fndx) {
663 					sgp->sg_fscn = osp->os_scn;
664 					recorded = TRUE;
665 				}
666 			}
667 
668 			if ((osp->os_outdata =
669 			    elf_getdata(osp->os_scn, NULL)) == NULL) {
670 				eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
671 				    ofl->ofl_name);
672 				return (S_ERROR);
673 			}
674 
675 			/*
676 			 * If this section is part of a loadable segment insure
677 			 * that the segments alignment is appropriate.
678 			 */
679 			if (_phdr->p_type == PT_LOAD) {
680 				_phdr->p_align = (Xword)lcm(_phdr->p_align,
681 				    osp->os_shdr->sh_addralign);
682 			}
683 		}
684 	}
685 	return (1);
686 }
687