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