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