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 (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 */
28
29 /*
30 * This file contains the functions responsible for opening the output file
31 * image, associating the appropriate input elf structures with the new image,
32 * and obtaining new elf structures to define the new image.
33 */
34 #include <stdio.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <link.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <limits.h>
41 #include <debug.h>
42 #include <unistd.h>
43 #include "msg.h"
44 #include "_libld.h"
45
46 /*
47 * Determine a least common multiplier. Input sections contain an alignment
48 * requirement, which elf_update() uses to insure that the section is aligned
49 * correctly off of the base of the elf image. We must also insure that the
50 * sections mapping is congruent with this alignment requirement. For each
51 * input section associated with a loadable segment determine whether the
52 * segments alignment must be adjusted to compensate for a sections alignment
53 * requirements.
54 */
55 Xword
ld_lcm(Xword a,Xword b)56 ld_lcm(Xword a, Xword b)
57 {
58 Xword _r, _a, _b;
59
60 if ((_a = a) == 0)
61 return (b);
62 if ((_b = b) == 0)
63 return (a);
64
65 if (_a > _b)
66 _a = b, _b = a;
67 while ((_r = _b % _a) != 0)
68 _b = _a, _a = _r;
69 return ((a / _a) * b);
70 }
71
72 /*
73 * Open the output file and insure the correct access modes.
74 */
75 uintptr_t
ld_open_outfile(Ofl_desc * ofl)76 ld_open_outfile(Ofl_desc * ofl)
77 {
78 mode_t mode;
79 struct stat status;
80
81 /*
82 * Determine the required file mode from the type of output file we
83 * are creating.
84 */
85 mode = (ofl->ofl_flags & (FLG_OF_EXEC | FLG_OF_SHAROBJ))
86 ? 0777 : 0666;
87
88 /* Determine if the output file already exists */
89 if (stat(ofl->ofl_name, &status) == 0) {
90 if ((status.st_mode & S_IFMT) != S_IFREG) {
91 /*
92 * It is not a regular file, so don't delete it
93 * or allow it to be deleted. This allows root
94 * users to specify /dev/null output file for
95 * verification links.
96 */
97 ofl->ofl_flags1 |= FLG_OF1_NONREG;
98 } else {
99 /*
100 * It's a regular file, so unlink it. In standard
101 * Unix fashion, the old file will continue to
102 * exist until its link count drops to 0 and no
103 * process has the file open. In the meantime, we
104 * create a new file (inode) under the same name,
105 * available for new use.
106 *
107 * The advantage of this policy is that creating
108 * a new executable or sharable library does not
109 * corrupt existing processes using the old file.
110 * A possible disadvantage is that if the existing
111 * file has a (link_count > 1), the other names will
112 * continue to reference the old inode, thus
113 * breaking the link.
114 *
115 * A subtlety here is that POSIX says we are not
116 * supposed to replace a non-writable file, which
117 * is something that unlink() is happy to do. The
118 * only 100% reliable test against this is to open
119 * the file for non-destructive write access. If the
120 * open succeeds, we are clear to unlink it, and if
121 * not, then the error generated is the error we
122 * need to report.
123 */
124 if ((ofl->ofl_fd = open(ofl->ofl_name, O_RDWR,
125 mode)) < 0) {
126 int err = errno;
127
128 if (err != ENOENT) {
129 ld_eprintf(ofl, ERR_FATAL,
130 MSG_INTL(MSG_SYS_OPEN),
131 ofl->ofl_name, strerror(err));
132 return (S_ERROR);
133 }
134 } else {
135 (void) close(ofl->ofl_fd);
136 }
137
138 if ((unlink(ofl->ofl_name) == -1) &&
139 (errno != ENOENT)) {
140 int err = errno;
141
142 ld_eprintf(ofl, ERR_FATAL,
143 MSG_INTL(MSG_SYS_UNLINK),
144 ofl->ofl_name, strerror(err));
145 return (S_ERROR);
146 }
147 }
148 }
149
150 /*
151 * Open (or create) the output file name (ofl_fd acts as a global
152 * flag to ldexit() signifying whether the output file should be
153 * removed or not on error).
154 */
155 if ((ofl->ofl_fd = open(ofl->ofl_name, O_RDWR | O_CREAT | O_TRUNC,
156 mode)) < 0) {
157 int err = errno;
158
159 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
160 ofl->ofl_name, strerror(err));
161 return (S_ERROR);
162 }
163
164 return (1);
165 }
166
167
168 /*
169 * If we are creating a memory model we need to update the present memory image.
170 * Use elf_update(ELF_C_NULL) to calculate the offset of each section and their
171 * associated data buffers. From this information determine what padding is
172 * required.
173 *
174 * Two actions are necessary to convert the present disc image into a memory
175 * image:
176 *
177 * - Loadable segments must be padded so that the next segment virtual
178 * address and file offset are the same.
179 *
180 * - NOBITS sections must be converted into allocated, null filled sections.
181 */
182 static uintptr_t
pad_outfile(Ofl_desc * ofl)183 pad_outfile(Ofl_desc *ofl)
184 {
185 Aliste idx1;
186 off_t offset;
187 Elf_Scn *oscn = 0;
188 Sg_desc *sgp;
189 Ehdr *ehdr;
190
191 /*
192 * Update all the elf structures. This will assign offsets to the
193 * section headers and data buffers as they relate to the new image.
194 */
195 if (elf_update(ofl->ofl_welf, ELF_C_NULL) == -1) {
196 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
197 ofl->ofl_name);
198 return (S_ERROR);
199 }
200 if ((ehdr = elf_getehdr(ofl->ofl_welf)) == NULL) {
201 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETEHDR),
202 ofl->ofl_name);
203 return (S_ERROR);
204 }
205
206 /*
207 * Initialize the offset by skipping the Elf header and program
208 * headers.
209 */
210 offset = ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize);
211
212 /*
213 * Traverse the segment list looking for loadable segments.
214 */
215 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
216 Phdr *phdr = &(sgp->sg_phdr);
217 Os_desc *osp;
218 Aliste idx2;
219
220 /*
221 * If we've already processed a loadable segment, the `scn'
222 * variable will be initialized to the last section that was
223 * part of that segment. Add sufficient padding to this section
224 * to cause the next segments virtual address and file offset to
225 * be the same.
226 */
227 if (oscn && (phdr->p_type == PT_LOAD)) {
228 Elf_Data * data;
229 size_t size;
230
231 size = (size_t)(S_ROUND(offset, phdr->p_align) -
232 offset);
233
234 if ((data = elf_newdata(oscn)) == NULL) {
235 ld_eprintf(ofl, ERR_ELF,
236 MSG_INTL(MSG_ELF_NEWDATA), ofl->ofl_name);
237 return (S_ERROR);
238 }
239 if ((data->d_buf = libld_calloc(size, 1)) == 0)
240 return (S_ERROR);
241
242 data->d_type = ELF_T_BYTE;
243 data->d_size = size;
244 data->d_align = 1;
245 data->d_version = ofl->ofl_dehdr->e_version;
246 }
247
248 /*
249 * Traverse the output sections for this segment calculating the
250 * offset of each section. Retain the final section descriptor
251 * as this will be where any padding buffer will be added.
252 */
253 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
254 Shdr *shdr = osp->os_shdr;
255
256 offset = (off_t)S_ROUND(offset, shdr->sh_addralign);
257 offset += shdr->sh_size;
258
259 /*
260 * If this is a NOBITS output section convert all of
261 * its associated input sections into real, null filled,
262 * data buffers, and change the section to PROGBITS.
263 */
264 if (shdr->sh_type == SHT_NOBITS)
265 shdr->sh_type = SHT_PROGBITS;
266 }
267
268 /*
269 * If this is a loadable segment retain the last output section
270 * descriptor. This acts both as a flag that a loadable
271 * segment has been seen, and as the segment to which a padding
272 * buffer will be added.
273 */
274 if (phdr->p_type == PT_LOAD)
275 oscn = osp->os_scn;
276 }
277 return (1);
278 }
279
280 /*
281 * Create an output section. The first instance of an input section triggers
282 * the creation of a new output section.
283 */
284 static uintptr_t
create_outsec(Ofl_desc * ofl,Sg_desc * sgp,Os_desc * osp,Word ptype,int shidx,Boolean fixalign)285 create_outsec(Ofl_desc *ofl, Sg_desc *sgp, Os_desc *osp, Word ptype, int shidx,
286 Boolean fixalign)
287 {
288 Elf_Scn *scn;
289 Shdr *shdr;
290
291 /*
292 * Get a section descriptor for the section.
293 */
294 if ((scn = elf_newscn(ofl->ofl_welf)) == NULL) {
295 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_NEWSCN),
296 ofl->ofl_name);
297 return (S_ERROR);
298 }
299 osp->os_scn = scn;
300
301 /*
302 * Get a new section header table entry and copy the pertinent
303 * information from the in-core descriptor.
304 */
305 if ((shdr = elf_getshdr(scn)) == NULL) {
306 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
307 ofl->ofl_name);
308 return (S_ERROR);
309 }
310 *shdr = *(osp->os_shdr);
311 osp->os_shdr = shdr;
312
313 /*
314 * If this is the first section within a loadable segment, and the
315 * alignment needs to be updated, record this section.
316 */
317 if ((fixalign == TRUE) && (ptype == PT_LOAD) && (shidx == 1))
318 sgp->sg_fscn = scn;
319
320 /*
321 * If not building a relocatable object, remove any of the
322 * following flags, as they have been acted upon and are not
323 * meaningful in the output:
324 * SHF_ORDERED, SHF_LINK_ORDER, SHF_GROUP
325 * For relocatable objects, we allow them to propagate to
326 * the output object to be handled by the next linker that
327 * sees them.
328 */
329 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
330 osp->os_shdr->sh_flags &= ~(ALL_SHF_ORDER|SHF_GROUP);
331
332 /*
333 * If this is a TLS section, save it so that the PT_TLS program header
334 * information can be established after the output image has been
335 * initially created. At this point, all TLS input sections are ordered
336 * as they will appear in the output image.
337 */
338 if ((ofl->ofl_flags & FLG_OF_TLSPHDR) &&
339 (osp->os_shdr->sh_flags & SHF_TLS) &&
340 (aplist_append(&ofl->ofl_ostlsseg, osp,
341 AL_CNT_OFL_OSTLSSEG) == NULL))
342 return (S_ERROR);
343
344 return (0);
345 }
346
347 /*
348 * Create the elf structures that allow the input data to be associated with the
349 * new image:
350 *
351 * - define the new elf image using elf_begin(),
352 *
353 * - obtain an elf header for the image,
354 *
355 * - traverse the input segments and create a program header array to define
356 * the required segments,
357 *
358 * - traverse the output sections for each segment assigning a new section
359 * descriptor and section header for each,
360 *
361 * - traverse the input sections associated with each output section and
362 * assign a new data descriptor to each (each output section becomes a
363 * linked list of input data buffers).
364 */
365 uintptr_t
ld_create_outfile(Ofl_desc * ofl)366 ld_create_outfile(Ofl_desc *ofl)
367 {
368 Sg_desc *sgp;
369 Os_desc *osp;
370 Is_desc *isp;
371 Elf_Data *tlsdata = 0;
372 Aliste idx1;
373 ofl_flag_t flags = ofl->ofl_flags;
374 ofl_flag_t flags1 = ofl->ofl_flags1;
375 size_t ndx;
376 Elf_Cmd cmd;
377 Boolean fixalign = FALSE;
378 int fd, nseg = 0, shidx, dataidx, ptloadidx = 0;
379
380 DBG_CALL(Dbg_basic_create(ofl->ofl_lml));
381
382 /*
383 * If DF_1_NOHDR or FLG_OF1_VADDR were set,
384 * we need to do alignment adjustment.
385 */
386 if ((flags1 & FLG_OF1_VADDR) ||
387 (ofl->ofl_dtflags_1 & DF_1_NOHDR)) {
388 fixalign = TRUE;
389 }
390
391 if (flags1 & FLG_OF1_MEMORY) {
392 cmd = ELF_C_IMAGE;
393 fd = 0;
394 } else {
395 fd = ofl->ofl_fd;
396 cmd = ELF_C_WRITE;
397 }
398
399 /*
400 * If there are any ordered sections, handle them here.
401 */
402 if ((ofl->ofl_ordered != NULL) &&
403 (ld_sort_ordered(ofl) == S_ERROR))
404 return (S_ERROR);
405
406 /*
407 * Tell the access library about our new temporary file.
408 */
409 if ((ofl->ofl_welf = elf_begin(fd, cmd, 0)) == NULL) {
410 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN),
411 ofl->ofl_name);
412 return (S_ERROR);
413 }
414
415 /*
416 * Obtain a new Elf header.
417 */
418 if ((ofl->ofl_nehdr = elf_newehdr(ofl->ofl_welf)) == NULL) {
419 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_NEWEHDR),
420 ofl->ofl_name);
421 return (S_ERROR);
422 }
423 ofl->ofl_nehdr->e_machine = ofl->ofl_dehdr->e_machine;
424
425 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
426 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
427 int frst = 0;
428 Phdr *phdr = &(sgp->sg_phdr);
429 Word ptype = phdr->p_type;
430 Aliste idx2;
431
432 /*
433 * Count the number of segments that will go in the program
434 * header table. If a segment is empty, ignore it.
435 */
436 if (!(flags & FLG_OF_RELOBJ)) {
437 /*
438 * If the program header type belongs to the os range,
439 * the resulting object is ELFOSABI_SOLARIS.
440 */
441 if ((ptype >= PT_LOOS) && (ptype <= PT_HIOS))
442 ofl->ofl_flags |= FLG_OF_OSABI;
443
444 if (ptype == PT_PHDR) {
445 /*
446 * If we are generating an interp section (and
447 * thus an associated PT_INTERP program header
448 * entry) also generate a PT_PHDR program header
449 * entry. This allows the kernel to generate
450 * the appropriate aux vector entries to pass to
451 * the interpreter (refer to exec/elf/elf.c).
452 * Note that if an image was generated with an
453 * interp section, but no associated PT_PHDR
454 * program header entry, the kernel will simply
455 * pass the interpreter an open file descriptor
456 * when the image is executed).
457 */
458 if (ofl->ofl_osinterp)
459 nseg++;
460 } else if (ptype == PT_INTERP) {
461 if (ofl->ofl_osinterp)
462 nseg++;
463 } else if (ptype == PT_DYNAMIC) {
464 if (flags & FLG_OF_DYNAMIC)
465 nseg++;
466 } else if (ptype == PT_TLS) {
467 if (flags & FLG_OF_TLSPHDR)
468 nseg++;
469 } else if (ptype == PT_SUNW_UNWIND) {
470 if (ofl->ofl_unwindhdr)
471 nseg++;
472 } else if (ptype == PT_SUNWDTRACE) {
473 if (ofl->ofl_dtracesym)
474 nseg++;
475 } else if (ptype == PT_SUNWCAP) {
476 if (ofl->ofl_oscap)
477 nseg++;
478 } else if (ptype == PT_SUNWSTACK) {
479 if ((sgp->sg_flags & FLG_SG_DISABLED) == 0)
480 nseg++;
481 } else if (sgp->sg_flags & FLG_SG_EMPTY) {
482 nseg++;
483 } else if (sgp->sg_osdescs != NULL) {
484 if ((sgp->sg_flags & FLG_SG_PHREQ) == 0) {
485 /*
486 * If this is a segment for which
487 * we are not making a program header,
488 * don't increment nseg
489 */
490 ptype = (sgp->sg_phdr).p_type = PT_NULL;
491 } else if (ptype != PT_NULL)
492 nseg++;
493 }
494 }
495
496 /*
497 * Establish any processing unique to the first loadable
498 * segment.
499 */
500 if ((ptype == PT_LOAD) && (ptloadidx == 0)) {
501 ptloadidx++;
502
503 /*
504 * If the first loadable segment is not supposed to
505 * include the ELF or program headers, alignments
506 * of the following segments need to be fixed,
507 * plus a .dynamic FLAGS1 setting is required.
508 */
509 if (ofl->ofl_dtflags_1 & DF_1_NOHDR)
510 fixalign = TRUE;
511 }
512
513 shidx = 0;
514 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
515 Aliste idx3;
516 int os_isdescs_idx;
517
518 dataidx = 0;
519
520 OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) {
521 Elf_Data *data;
522 Ifl_desc *ifl = isp->is_file;
523
524 /*
525 * An input section in the list that has
526 * been previously marked to be discarded
527 * should be completely ignored.
528 */
529 if (isp->is_flags & FLG_IS_DISCARD)
530 continue;
531
532 /*
533 * At this point we know whether a section has
534 * been referenced. If it hasn't, and the whole
535 * file hasn't been referenced (which would have
536 * been caught in ignore_section_processing()),
537 * give a diagnostic (-D unused,detail) or
538 * discard the section if -zignore is in effect.
539 */
540 if (ifl &&
541 (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
542 ((ptype == PT_LOAD) &&
543 ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
544 (isp->is_shdr->sh_size > 0)))) {
545 Lm_list *lml = ofl->ofl_lml;
546
547 if (ifl->ifl_flags & FLG_IF_IGNORE) {
548 isp->is_flags |= FLG_IS_DISCARD;
549 DBG_CALL(Dbg_unused_sec(lml,
550 isp));
551 continue;
552 } else {
553 DBG_CALL(Dbg_unused_sec(lml,
554 isp));
555 }
556 }
557
558 /*
559 * If this section provides no data, and isn't
560 * referenced, then it can be discarded as well.
561 * Note, if this is the first input section
562 * associated to an output section, let it
563 * through, there may be a legitimate reason why
564 * the user wants a null section. Discarding
565 * additional sections is intended to remove the
566 * empty clutter the compilers have a habit of
567 * creating. Don't provide an unused diagnostic
568 * as these sections aren't typically the users
569 * creation.
570 */
571 if (ifl && dataidx &&
572 ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
573 (isp->is_shdr->sh_size == 0)) {
574 isp->is_flags |= FLG_IS_DISCARD;
575 continue;
576 }
577
578 /*
579 * The first input section triggers the creation
580 * of the associated output section.
581 */
582 if (osp->os_scn == NULL) {
583 shidx++;
584
585 if (create_outsec(ofl, sgp, osp, ptype,
586 shidx, fixalign) == S_ERROR)
587 return (S_ERROR);
588 }
589
590 dataidx++;
591
592 /*
593 * Create a new output data buffer for each
594 * input data buffer, thus linking the new
595 * buffers to the new elf output structures.
596 * Simply make the new data buffers point to
597 * the old data.
598 */
599 if ((data = elf_newdata(osp->os_scn)) == NULL) {
600 ld_eprintf(ofl, ERR_ELF,
601 MSG_INTL(MSG_ELF_NEWDATA),
602 ofl->ofl_name);
603 return (S_ERROR);
604 }
605 *data = *(isp->is_indata);
606 isp->is_indata = data;
607
608 if ((fixalign == TRUE) && (ptype == PT_LOAD) &&
609 (shidx == 1) && (dataidx == 1))
610 data->d_align = sgp->sg_align;
611
612 /*
613 * Save the first TLS data buffer, as this is
614 * the start of the TLS segment. Realign this
615 * buffer based on the alignment requirements
616 * of all the TLS input sections.
617 */
618 if ((flags & FLG_OF_TLSPHDR) &&
619 (isp->is_shdr->sh_flags & SHF_TLS)) {
620 if (tlsdata == 0)
621 tlsdata = data;
622 tlsdata->d_align =
623 ld_lcm(tlsdata->d_align,
624 isp->is_shdr->sh_addralign);
625 }
626
627 #if defined(_ELF64) && defined(_ILP32)
628 /*
629 * 4106312, the 32-bit ELF64 version of ld
630 * needs to be able to create large .bss
631 * sections. The d_size member of Elf_Data
632 * only allows 32-bits in _ILP32, so we build
633 * multiple data-items that each fit into 32-
634 * bits. libelf (4106398) can summ these up
635 * into a 64-bit quantity. This only works
636 * for NOBITS sections which don't have any
637 * real data to maintain and don't require
638 * large file support.
639 */
640 if (isp->is_shdr->sh_type == SHT_NOBITS) {
641 Xword sz = isp->is_shdr->sh_size;
642
643 while (sz >> 32) {
644 data->d_size = SIZE_MAX;
645 sz -= (Xword)SIZE_MAX;
646
647 data = elf_newdata(osp->os_scn);
648 if (data == NULL)
649 return (S_ERROR);
650 }
651 data->d_size = (size_t)sz;
652 }
653 #endif
654
655 /*
656 * If this segment requires rounding realign the
657 * first data buffer associated with the first
658 * section.
659 */
660 if ((frst++ == 0) &&
661 (sgp->sg_flags & FLG_SG_ROUND)) {
662 Xword align;
663
664 if (data->d_align)
665 align = (Xword)
666 S_ROUND(data->d_align,
667 sgp->sg_round);
668 else
669 align = sgp->sg_round;
670
671 data->d_align = (size_t)align;
672 }
673 }
674
675 /*
676 * Clear the szoutrels counter so that it can be used
677 * again in the building of relocs. See machrel.c.
678 */
679 osp->os_szoutrels = 0;
680 }
681 }
682
683 /*
684 * Did we use ELF features from the osabi range? If so,
685 * update the ELF header osabi fields. If this doesn't happen,
686 * those fields remain 0, reflecting a generic System V ELF ABI.
687 */
688 if (ofl->ofl_flags & FLG_OF_OSABI) {
689 ofl->ofl_nehdr->e_ident[EI_OSABI] = ELFOSABI_SOLARIS;
690 ofl->ofl_nehdr->e_ident[EI_ABIVERSION] = EAV_SUNW_CURRENT;
691 }
692
693 /*
694 * Build an empty PHDR.
695 */
696 if (nseg) {
697 if ((ofl->ofl_phdr = elf_newphdr(ofl->ofl_welf,
698 nseg)) == NULL) {
699 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_NEWPHDR),
700 ofl->ofl_name);
701 return (S_ERROR);
702 }
703 }
704
705 /*
706 * If we need to generate a memory model, pad the image.
707 */
708 if (flags1 & FLG_OF1_MEMORY) {
709 if (pad_outfile(ofl) == S_ERROR)
710 return (S_ERROR);
711 }
712
713 /*
714 * After all the basic input file processing, all data pointers are
715 * referencing two types of memory:
716 *
717 * - allocated memory, ie. elf structures, internal link editor
718 * structures, and any new sections that have been created.
719 *
720 * - original input file mmap'ed memory, ie. the actual data
721 * sections of the input file images.
722 *
723 * Up until now, the only memory modifications have been carried out on
724 * the allocated memory. Before carrying out any relocations, write the
725 * new output file image and reassign any necessary data pointers to the
726 * output files memory image. This insures that any relocation
727 * modifications are made to the output file image and not to the input
728 * file image, thus preventing the creation of dirty pages and reducing
729 * the overall swap space requirement.
730 *
731 * Write out the elf structure so as to create the new file image.
732 */
733 if ((ofl->ofl_size = (size_t)elf_update(ofl->ofl_welf,
734 ELF_C_WRIMAGE)) == (size_t)-1) {
735 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
736 ofl->ofl_name);
737 return (S_ERROR);
738 }
739
740 /*
741 * Initialize the true `ofl' information with the memory images address
742 * and size. This will be used to write() out the image once any
743 * relocation processing has been completed. We also use this image
744 * information to setup a new Elf descriptor, which is used to obtain
745 * all the necessary elf pointers within the new output image.
746 */
747 if ((ofl->ofl_elf = elf_begin(0, ELF_C_IMAGE,
748 ofl->ofl_welf)) == NULL) {
749 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN),
750 ofl->ofl_name);
751 return (S_ERROR);
752 }
753 if ((ofl->ofl_nehdr = elf_getehdr(ofl->ofl_elf)) == NULL) {
754 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETEHDR),
755 ofl->ofl_name);
756 return (S_ERROR);
757 }
758 if (!(flags & FLG_OF_RELOBJ))
759 if ((ofl->ofl_phdr = elf_getphdr(ofl->ofl_elf)) == NULL) {
760 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETPHDR),
761 ofl->ofl_name);
762 return (S_ERROR);
763 }
764
765 /*
766 * Reinitialize the section descriptors, section headers and obtain new
767 * output data buffer pointers (these will be used to perform any
768 * relocations).
769 */
770 ndx = 0;
771 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
772 Phdr *_phdr = &(sgp->sg_phdr);
773 Os_desc *osp;
774 Aliste idx2;
775 Boolean recorded = FALSE;
776
777 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
778 /*
779 * Make sure that an output section was originally
780 * created. Input sections that had been marked as
781 * discarded may have made an output section
782 * unnecessary. Remove this alist entry so that
783 * future output section descriptor processing doesn't
784 * have to compensate for this empty section.
785 */
786 if (osp->os_scn == NULL) {
787 aplist_delete(sgp->sg_osdescs, &idx2);
788 continue;
789 }
790 if ((osp->os_scn =
791 elf_getscn(ofl->ofl_elf, ++ndx)) == NULL) {
792 ld_eprintf(ofl, ERR_ELF,
793 MSG_INTL(MSG_ELF_GETSCN), ofl->ofl_name,
794 ndx);
795 return (S_ERROR);
796 }
797 if ((osp->os_shdr =
798 elf_getshdr(osp->os_scn)) == NULL) {
799 ld_eprintf(ofl, ERR_ELF,
800 MSG_INTL(MSG_ELF_GETSHDR), ofl->ofl_name);
801 return (S_ERROR);
802 }
803 if ((fixalign == TRUE) && sgp->sg_fscn &&
804 (recorded == FALSE)) {
805 size_t fndx;
806 Elf_Scn *scn;
807
808 scn = sgp->sg_fscn;
809 if ((fndx = elf_ndxscn(scn)) == SHN_UNDEF) {
810 ld_eprintf(ofl, ERR_ELF,
811 MSG_INTL(MSG_ELF_NDXSCN),
812 ofl->ofl_name);
813 return (S_ERROR);
814 }
815 if (ndx == fndx) {
816 sgp->sg_fscn = osp->os_scn;
817 recorded = TRUE;
818 }
819 }
820
821 if ((osp->os_outdata =
822 elf_getdata(osp->os_scn, NULL)) == NULL) {
823 ld_eprintf(ofl, ERR_ELF,
824 MSG_INTL(MSG_ELF_GETDATA), ofl->ofl_name);
825 return (S_ERROR);
826 }
827
828 /*
829 * If this section is part of a loadable segment insure
830 * that the segments alignment is appropriate.
831 */
832 if (_phdr->p_type == PT_LOAD) {
833 _phdr->p_align = ld_lcm(_phdr->p_align,
834 osp->os_shdr->sh_addralign);
835 }
836 }
837 }
838 return (1);
839 }
840