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