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