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 * Module sections. Initialize special sections 31 */ 32 33 #define ELF_TARGET_AMD64 34 35 #include <sys/debug.h> 36 37 #include <string.h> 38 #include <strings.h> 39 #include <stdio.h> 40 #include <link.h> 41 #include <debug.h> 42 #include "msg.h" 43 #include "_libld.h" 44 45 inline static void 46 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym) 47 { 48 Sym *sym = sdp->sd_sym; 49 uchar_t type = ELF_ST_TYPE(sym->st_info); 50 /* LINTED - only used for assert() */ 51 int err; 52 53 if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) { 54 ofl->ofl_locscnt--; 55 56 err = st_delstring(ofl->ofl_strtab, sdp->sd_name); 57 assert(err != -1); 58 59 if (allow_ldynsym && ldynsym_symtype[type]) { 60 ofl->ofl_dynlocscnt--; 61 62 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name); 63 assert(err != -1); 64 /* Remove from sort section? */ 65 DYNSORT_COUNT(sdp, sym, type, --); 66 } 67 } 68 sdp->sd_flags |= FLG_SY_ISDISC; 69 } 70 71 inline static void 72 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym) 73 { 74 Sym *sym = sdp->sd_sym; 75 uchar_t type = ELF_ST_TYPE(sym->st_info); 76 /* LINTED - only used for assert() */ 77 int err; 78 79 ofl->ofl_scopecnt--; 80 ofl->ofl_elimcnt++; 81 82 err = st_delstring(ofl->ofl_strtab, sdp->sd_name); 83 assert(err != -1); 84 85 if (allow_ldynsym && ldynsym_symtype[type]) { 86 ofl->ofl_dynscopecnt--; 87 88 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name); 89 assert(err != -1); 90 /* Remove from sort section? */ 91 DYNSORT_COUNT(sdp, sym, type, --); 92 } 93 sdp->sd_flags |= FLG_SY_ELIM; 94 } 95 96 inline static void 97 ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym) 98 { 99 Os_desc *osp; 100 Is_desc *isp = sdp->sd_isc; 101 uchar_t bind = ELF_ST_BIND(sdp->sd_sym->st_info); 102 103 if (bind == STB_LOCAL) { 104 uchar_t type = ELF_ST_TYPE(sdp->sd_sym->st_info); 105 106 /* 107 * Skip section symbols, these were never collected in the 108 * first place. 109 */ 110 if (type == STT_SECTION) 111 return; 112 113 /* 114 * Determine if the whole file is being removed. Remove any 115 * file symbol, and any symbol that is not associated with a 116 * section, provided the symbol has not been identified as 117 * (update) required. 118 */ 119 if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) && 120 ((type == STT_FILE) || ((isp == NULL) && 121 ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) { 122 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 123 if (ifl->ifl_flags & FLG_IF_IGNORE) 124 remove_local(ofl, sdp, allow_ldynsym); 125 return; 126 } 127 128 } else { 129 /* 130 * Global symbols can only be eliminated when the interfaces of 131 * an object have been defined via versioning/scoping. 132 */ 133 if (!SYM_IS_HIDDEN(sdp)) 134 return; 135 136 /* 137 * Remove any unreferenced symbols that are not associated with 138 * a section. 139 */ 140 if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) { 141 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 142 if (ifl->ifl_flags & FLG_IF_IGNORE) 143 remove_scoped(ofl, sdp, allow_ldynsym); 144 return; 145 } 146 } 147 148 /* 149 * Do not discard any symbols that are associated with non-allocable 150 * segments. 151 */ 152 if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) && 153 ((osp = isp->is_osdesc) != 0) && 154 (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) { 155 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 156 if (ifl->ifl_flags & FLG_IF_IGNORE) { 157 if (bind == STB_LOCAL) 158 remove_local(ofl, sdp, allow_ldynsym); 159 else 160 remove_scoped(ofl, sdp, allow_ldynsym); 161 } 162 } 163 } 164 165 static Boolean 166 isdesc_discarded(Is_desc *isp) 167 { 168 Ifl_desc *ifl = isp->is_file; 169 Os_desc *osp = isp->is_osdesc; 170 Word ptype = osp->os_sgdesc->sg_phdr.p_type; 171 172 if (isp->is_flags & FLG_IS_DISCARD) 173 return (TRUE); 174 175 /* 176 * If the file is discarded, it will take 177 * the section with it. 178 */ 179 if (ifl && 180 (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) || 181 ((ptype == PT_LOAD) && 182 ((isp->is_flags & FLG_IS_SECTREF) == 0) && 183 (isp->is_shdr->sh_size > 0))) && 184 (ifl->ifl_flags & FLG_IF_IGNORE)) 185 return (TRUE); 186 187 return (FALSE); 188 } 189 190 /* 191 * There are situations where we may count output sections (ofl_shdrcnt) 192 * that are subsequently eliminated from the output object. Whether or 193 * not this happens cannot be known until all input has been seen and 194 * section elimination code has run. However, the situations where this 195 * outcome is possible are known, and are flagged by setting FLG_OF_ADJOSCNT. 196 * 197 * If FLG_OF_ADJOSCNT is set, this routine makes a pass over the output 198 * sections. If an unused output section is encountered, we decrement 199 * ofl->ofl_shdrcnt and remove the section name from the .shstrtab string 200 * table (ofl->ofl_shdrsttab). 201 * 202 * This code must be kept in sync with the similar code 203 * found in outfile.c:ld_create_outfile(). 204 */ 205 static void 206 adjust_os_count(Ofl_desc *ofl) 207 { 208 Sg_desc *sgp; 209 Is_desc *isp; 210 Os_desc *osp; 211 Aliste idx1; 212 213 if ((ofl->ofl_flags & FLG_OF_ADJOSCNT) == 0) 214 return; 215 216 /* 217 * For each output section, look at the input sections to find at least 218 * one input section that has not been eliminated. If none are found, 219 * the -z ignore processing above has eliminated that output section. 220 */ 221 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) { 222 Aliste idx2; 223 224 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) { 225 Aliste idx3; 226 int keep = 0, os_isdescs_idx; 227 228 OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) { 229 /* 230 * We have found a kept input section, 231 * so the output section will be created. 232 */ 233 if (!isdesc_discarded(isp)) { 234 keep = 1; 235 break; 236 } 237 } 238 /* 239 * If no section of this name was kept, decrement 240 * the count and remove the name from .shstrtab. 241 */ 242 if (keep == 0) { 243 /* LINTED - only used for assert() */ 244 int err; 245 246 ofl->ofl_shdrcnt--; 247 err = st_delstring(ofl->ofl_shdrsttab, 248 osp->os_name); 249 assert(err != -1); 250 } 251 } 252 } 253 } 254 255 /* 256 * If -zignore has been in effect, scan all input files to determine if the 257 * file, or sections from the file, have been referenced. If not, the file or 258 * some of the files sections can be discarded. If sections are to be 259 * discarded, rescan the output relocations and the symbol table and remove 260 * the relocations and symbol entries that are no longer required. 261 * 262 * Note: It's possible that a section which is being discarded has contributed 263 * to the GOT table or the PLT table. However, we can't at this point 264 * eliminate the corresponding entries. This is because there could well 265 * be other sections referencing those same entries, but we don't have 266 * the infrastructure to determine this. So, keep the PLT and GOT 267 * entries in the table in case someone wants them. 268 * Note: The section to be affected needs to be allocatable. 269 * So even if -zignore is in effect, if the section is not allocatable, 270 * we do not eliminate it. 271 */ 272 static uintptr_t 273 ignore_section_processing(Ofl_desc *ofl) 274 { 275 Sg_desc *sgp; 276 Is_desc *isp; 277 Os_desc *osp; 278 Ifl_desc *ifl; 279 Rel_cachebuf *rcbp; 280 Rel_desc *rsp; 281 int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 282 Aliste idx1; 283 284 for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) { 285 uint_t num, discard; 286 287 /* 288 * Diagnose (-D unused) a completely unreferenced file. 289 */ 290 if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0) 291 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, 292 ifl->ifl_name, 0, 0)); 293 if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) || 294 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0)) 295 continue; 296 297 /* 298 * Before scanning the whole symbol table to determine if 299 * symbols should be discard - quickly (relatively) scan the 300 * sections to determine if any are to be discarded. 301 */ 302 discard = 0; 303 if (ifl->ifl_flags & FLG_IF_FILEREF) { 304 for (num = 1; num < ifl->ifl_shnum; num++) { 305 if (((isp = ifl->ifl_isdesc[num]) != NULL) && 306 ((isp->is_flags & FLG_IS_SECTREF) == 0) && 307 ((osp = isp->is_osdesc) != NULL) && 308 ((sgp = osp->os_sgdesc) != NULL) && 309 (sgp->sg_phdr.p_type == PT_LOAD)) { 310 discard++; 311 break; 312 } 313 } 314 } 315 316 /* 317 * No sections are to be 'ignored' 318 */ 319 if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF)) 320 continue; 321 322 /* 323 * We know that we have discarded sections. Scan the symbol 324 * table for this file to determine if symbols need to be 325 * discarded that are associated with the 'ignored' sections. 326 */ 327 for (num = 1; num < ifl->ifl_symscnt; num++) { 328 Sym_desc *sdp; 329 330 /* 331 * If the symbol definition has been resolved to another 332 * file, or the symbol has already been discarded or 333 * eliminated, skip it. 334 */ 335 sdp = ifl->ifl_oldndx[num]; 336 if ((sdp->sd_file != ifl) || 337 (sdp->sd_flags & 338 (FLG_SY_ISDISC | FLG_SY_INVALID | FLG_SY_ELIM))) 339 continue; 340 341 /* 342 * Complete the investigation of the symbol. 343 */ 344 ignore_sym(ofl, ifl, sdp, allow_ldynsym); 345 } 346 } 347 348 /* 349 * If we were only here to solicit debugging diagnostics, we're done. 350 */ 351 if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) 352 return (1); 353 354 /* 355 * Scan all output relocations searching for those against discarded or 356 * ignored sections. If one is found, decrement the total outrel count. 357 */ 358 REL_CACHE_TRAVERSE(&ofl->ofl_outrels, idx1, rcbp, rsp) { 359 Is_desc *isc = rsp->rel_isdesc; 360 uint_t flags, entsize; 361 Shdr *shdr; 362 363 if ((isc == NULL) || ((isc->is_flags & (FLG_IS_SECTREF))) || 364 ((ifl = isc->is_file) == NULL) || 365 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) || 366 ((shdr = isc->is_shdr) == NULL) || 367 ((shdr->sh_flags & SHF_ALLOC) == 0)) 368 continue; 369 370 flags = rsp->rel_flags; 371 372 if (flags & (FLG_REL_GOT | FLG_REL_BSS | 373 FLG_REL_NOINFO | FLG_REL_PLT)) 374 continue; 375 376 osp = RELAUX_GET_OSDESC(rsp); 377 378 if (rsp->rel_flags & FLG_REL_RELA) 379 entsize = sizeof (Rela); 380 else 381 entsize = sizeof (Rel); 382 383 assert(osp->os_szoutrels > 0); 384 osp->os_szoutrels -= entsize; 385 386 if (!(flags & FLG_REL_PLT)) 387 ofl->ofl_reloccntsub++; 388 389 if (rsp->rel_rtype == ld_targ.t_m.m_r_relative) 390 ofl->ofl_relocrelcnt--; 391 } 392 393 /* 394 * As a result of our work here, the number of output sections may 395 * have decreased. Trigger a call to adjust_os_count(). 396 */ 397 ofl->ofl_flags |= FLG_OF_ADJOSCNT; 398 399 return (1); 400 } 401 402 /* 403 * Allocate Elf_Data, Shdr, and Is_desc structures for a new 404 * section. 405 * 406 * entry: 407 * ofl - Output file descriptor 408 * shtype - SHT_ type code for section. 409 * shname - String giving the name for the new section. 410 * entcnt - # of items contained in the data part of the new section. 411 * This value is multiplied against the known element size 412 * for the section type to determine the size of the data 413 * area for the section. It is only meaningful in cases where 414 * the section type has a non-zero element size. In other cases, 415 * the caller must set the size fields in the *ret_data and 416 * *ret_shdr structs manually. 417 * ret_isec, ret_shdr, ret_data - Address of pointers to 418 * receive address of newly allocated structs. 419 * 420 * exit: 421 * On error, returns S_ERROR. On success, returns (1), and the 422 * ret_ pointers have been updated to point at the new structures, 423 * which have been filled in. To finish the task, the caller must 424 * update any fields within the supplied descriptors that differ 425 * from its needs, and then call ld_place_section(). 426 */ 427 static uintptr_t 428 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt, 429 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data) 430 { 431 typedef struct sec_info { 432 Word d_type; 433 Word align; /* Used in both data and section header */ 434 Word sh_flags; 435 Word sh_entsize; 436 } SEC_INFO_T; 437 438 const SEC_INFO_T *sec_info; 439 440 Shdr *shdr; 441 Elf_Data *data; 442 Is_desc *isec; 443 size_t size; 444 445 /* 446 * For each type of section, we have a distinct set of 447 * SEC_INFO_T values. This macro defines a static structure 448 * containing those values and generates code to set the sec_info 449 * pointer to refer to it. The pointer in sec_info remains valid 450 * outside of the declaration scope because the info_s struct is static. 451 * 452 * We can't determine the value of M_WORD_ALIGN at compile time, so 453 * a different variant is used for those cases. 454 */ 455 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \ 456 { \ 457 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \ 458 sh_entsize}; \ 459 sec_info = &info_s; \ 460 } 461 #define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \ 462 { \ 463 static SEC_INFO_T info_s = { d_type, 0, sh_flags, \ 464 sh_entsize}; \ 465 info_s.align = ld_targ.t_m.m_word_align; \ 466 sec_info = &info_s; \ 467 } 468 469 switch (shtype) { 470 case SHT_PROGBITS: 471 /* 472 * SHT_PROGBITS sections contain are used for many 473 * different sections. Alignments and flags differ. 474 * Some have a standard entsize, and others don't. 475 * We set some defaults here, but there is no expectation 476 * that they are correct or complete for any specific 477 * purpose. The caller must provide the correct values. 478 */ 479 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0) 480 break; 481 482 case SHT_SYMTAB: 483 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym)) 484 break; 485 486 case SHT_DYNSYM: 487 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym)) 488 break; 489 490 case SHT_SUNW_LDYNSYM: 491 ofl->ofl_flags |= FLG_OF_OSABI; 492 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym)) 493 break; 494 495 case SHT_STRTAB: 496 /* 497 * A string table may or may not be allocable, depending 498 * on context, so we leave that flag unset and leave it to 499 * the caller to add it if necessary. 500 * 501 * String tables do not have a standard entsize, so 502 * we set it to 0. 503 */ 504 SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0) 505 break; 506 507 case SHT_RELA: 508 /* 509 * Relocations with an addend (Everything except 32-bit X86). 510 * The caller is expected to set all section header flags. 511 */ 512 SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela)) 513 break; 514 515 case SHT_REL: 516 /* 517 * Relocations without an addend (32-bit X86 only). 518 * The caller is expected to set all section header flags. 519 */ 520 SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel)) 521 break; 522 523 case SHT_HASH: 524 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word)) 525 break; 526 527 case SHT_SUNW_symsort: 528 case SHT_SUNW_tlssort: 529 ofl->ofl_flags |= FLG_OF_OSABI; 530 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word)) 531 break; 532 533 case SHT_DYNAMIC: 534 /* 535 * A dynamic section may or may not be allocable, and may or 536 * may not be writable, depending on context, so we leave the 537 * flags unset and leave it to the caller to add them if 538 * necessary. 539 */ 540 SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, 0, sizeof (Dyn)) 541 break; 542 543 case SHT_NOBITS: 544 /* 545 * SHT_NOBITS is used for BSS-type sections. The size and 546 * alignment depend on the specific use and must be adjusted 547 * by the caller. 548 */ 549 SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0) 550 break; 551 552 case SHT_INIT_ARRAY: 553 case SHT_FINI_ARRAY: 554 case SHT_PREINIT_ARRAY: 555 SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE, 556 sizeof (Addr)) 557 break; 558 559 case SHT_SYMTAB_SHNDX: 560 /* 561 * Note that these sections are created to be associated 562 * with both symtab and dynsym symbol tables. However, they 563 * are non-allocable in all cases, because the runtime 564 * linker has no need for this information. It is purely 565 * informational, used by elfdump(1), debuggers, etc. 566 */ 567 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word)); 568 break; 569 570 case SHT_SUNW_cap: 571 ofl->ofl_flags |= FLG_OF_OSABI; 572 SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap)); 573 break; 574 575 case SHT_SUNW_capchain: 576 ofl->ofl_flags |= FLG_OF_OSABI; 577 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, 578 sizeof (Capchain)); 579 break; 580 581 case SHT_SUNW_capinfo: 582 ofl->ofl_flags |= FLG_OF_OSABI; 583 #if _ELF64 584 SET_SEC_INFO(ELF_T_XWORD, sizeof (Xword), SHF_ALLOC, 585 sizeof (Capinfo)); 586 #else 587 SET_SEC_INFO(ELF_T_WORD, sizeof (Word), SHF_ALLOC, 588 sizeof (Capinfo)); 589 #endif 590 break; 591 592 case SHT_SUNW_move: 593 ofl->ofl_flags |= FLG_OF_OSABI; 594 SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword), 595 SHF_ALLOC | SHF_WRITE, sizeof (Move)); 596 break; 597 598 case SHT_SUNW_syminfo: 599 ofl->ofl_flags |= FLG_OF_OSABI; 600 /* 601 * The sh_info field of the SHT_*_syminfo section points 602 * to the header index of the associated .dynamic section, 603 * so we also set SHF_INFO_LINK. 604 */ 605 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, 606 SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo)); 607 break; 608 609 case SHT_SUNW_verneed: 610 case SHT_SUNW_verdef: 611 ofl->ofl_flags |= FLG_OF_OSABI; 612 /* 613 * The info for verneed and versym happen to be the same. 614 * The entries in these sections are not of uniform size, 615 * so we set the entsize to 0. 616 */ 617 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0); 618 break; 619 620 case SHT_SUNW_versym: 621 ofl->ofl_flags |= FLG_OF_OSABI; 622 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 623 sizeof (Versym)); 624 break; 625 626 default: 627 /* Should not happen: fcn called with unknown section type */ 628 assert(0); 629 return (S_ERROR); 630 } 631 #undef SET_SEC_INFO 632 #undef SET_SEC_INFO_WORD_ALIGN 633 634 size = entcnt * sec_info->sh_entsize; 635 636 /* 637 * Allocate and initialize the Elf_Data structure. 638 */ 639 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL) 640 return (S_ERROR); 641 data->d_type = sec_info->d_type; 642 data->d_size = size; 643 data->d_align = sec_info->align; 644 data->d_version = ofl->ofl_dehdr->e_version; 645 646 /* 647 * Allocate and initialize the Shdr structure. 648 */ 649 if ((shdr = libld_calloc(sizeof (Shdr), 1)) == NULL) 650 return (S_ERROR); 651 shdr->sh_type = shtype; 652 shdr->sh_size = size; 653 shdr->sh_flags = sec_info->sh_flags; 654 shdr->sh_addralign = sec_info->align; 655 shdr->sh_entsize = sec_info->sh_entsize; 656 657 /* 658 * Allocate and initialize the Is_desc structure. 659 */ 660 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL) 661 return (S_ERROR); 662 isec->is_name = shname; 663 isec->is_shdr = shdr; 664 isec->is_indata = data; 665 666 667 *ret_isec = isec; 668 *ret_shdr = shdr; 669 *ret_data = data; 670 return (1); 671 } 672 673 /* 674 * Use an existing input section as a template to create a new 675 * input section with the same values as the original, other than 676 * the size of the data area which is supplied by the caller. 677 * 678 * entry: 679 * ofl - Output file descriptor 680 * ifl - Input file section to use as a template 681 * size - Size of data area for new section 682 * ret_isec, ret_shdr, ret_data - Address of pointers to 683 * receive address of newly allocated structs. 684 * 685 * exit: 686 * On error, returns S_ERROR. On success, returns (1), and the 687 * ret_ pointers have been updated to point at the new structures, 688 * which have been filled in. To finish the task, the caller must 689 * update any fields within the supplied descriptors that differ 690 * from its needs, and then call ld_place_section(). 691 */ 692 static uintptr_t 693 new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size, 694 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data) 695 { 696 Shdr *shdr; 697 Elf_Data *data; 698 Is_desc *isec; 699 700 /* 701 * Allocate and initialize the Elf_Data structure. 702 */ 703 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL) 704 return (S_ERROR); 705 data->d_type = tmpl_isp->is_indata->d_type; 706 data->d_size = size; 707 data->d_align = tmpl_isp->is_shdr->sh_addralign; 708 data->d_version = ofl->ofl_dehdr->e_version; 709 710 /* 711 * Allocate and initialize the Shdr structure. 712 */ 713 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL) 714 return (S_ERROR); 715 *shdr = *tmpl_isp->is_shdr; 716 shdr->sh_addr = 0; 717 shdr->sh_offset = 0; 718 shdr->sh_size = size; 719 720 /* 721 * Allocate and initialize the Is_desc structure. 722 */ 723 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL) 724 return (S_ERROR); 725 isec->is_name = tmpl_isp->is_name; 726 isec->is_shdr = shdr; 727 isec->is_indata = data; 728 729 730 *ret_isec = isec; 731 *ret_shdr = shdr; 732 *ret_data = data; 733 return (1); 734 } 735 736 /* 737 * Build a .bss section for allocation of tentative definitions. Any `static' 738 * .bss definitions would have been associated to their own .bss sections and 739 * thus collected from the input files. `global' .bss definitions are tagged 740 * as COMMON and do not cause any associated .bss section elements to be 741 * generated. Here we add up all these COMMON symbols and generate the .bss 742 * section required to represent them. 743 */ 744 uintptr_t 745 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, uint_t ident) 746 { 747 Shdr *shdr; 748 Elf_Data *data; 749 Is_desc *isec; 750 Os_desc *osp; 751 Xword rsize = (Xword)ofl->ofl_relocbsssz; 752 753 /* 754 * Allocate header structs. We will set the name ourselves below, 755 * and there is no entcnt for a BSS. So, the shname and entcnt 756 * arguments are 0. 757 */ 758 if (new_section(ofl, SHT_NOBITS, NULL, 0, 759 &isec, &shdr, &data) == S_ERROR) 760 return (S_ERROR); 761 762 data->d_size = (size_t)size; 763 data->d_align = (size_t)align; 764 765 shdr->sh_size = size; 766 shdr->sh_addralign = align; 767 768 if (ident == ld_targ.t_id.id_tlsbss) { 769 isec->is_name = MSG_ORIG(MSG_SCN_TBSS); 770 ofl->ofl_istlsbss = isec; 771 shdr->sh_flags |= SHF_TLS; 772 773 } else if (ident == ld_targ.t_id.id_bss) { 774 isec->is_name = MSG_ORIG(MSG_SCN_BSS); 775 ofl->ofl_isbss = isec; 776 777 #if defined(_ELF64) 778 } else if ((ld_targ.t_m.m_mach == EM_AMD64) && 779 (ident == ld_targ.t_id.id_lbss)) { 780 isec->is_name = MSG_ORIG(MSG_SCN_LBSS); 781 ofl->ofl_islbss = isec; 782 shdr->sh_flags |= SHF_AMD64_LARGE; 783 #endif 784 } 785 786 /* 787 * Retain this .*bss input section as this will be where global symbol 788 * references are added. 789 */ 790 if ((osp = ld_place_section(ofl, isec, NULL, ident, NULL)) == 791 (Os_desc *)S_ERROR) 792 return (S_ERROR); 793 794 /* 795 * If relocations exist against a .*bss section, a section symbol must 796 * be created for the section in the .dynsym symbol table. 797 */ 798 if (!(osp->os_flags & FLG_OS_OUTREL)) { 799 ofl_flag_t flagtotest; 800 801 if (ident == ld_targ.t_id.id_tlsbss) 802 flagtotest = FLG_OF1_TLSOREL; 803 else 804 flagtotest = FLG_OF1_BSSOREL; 805 806 if (ofl->ofl_flags1 & flagtotest) { 807 ofl->ofl_dynshdrcnt++; 808 osp->os_flags |= FLG_OS_OUTREL; 809 } 810 } 811 812 osp->os_szoutrels = rsize; 813 return (1); 814 } 815 816 /* 817 * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via 818 * ld -z *array=name). 819 */ 820 static uintptr_t 821 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, APlist *alp) 822 { 823 uint_t entcount; 824 Aliste idx; 825 Elf_Data *data; 826 Is_desc *isec; 827 Shdr *shdr; 828 Sym_desc *sdp; 829 Rel_desc reld; 830 Rela reloc; 831 Os_desc *osp; 832 uintptr_t ret = 1; 833 834 if (alp == NULL) 835 return (1); 836 837 entcount = 0; 838 for (APLIST_TRAVERSE(alp, idx, sdp)) 839 entcount++; 840 841 if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) == 842 S_ERROR) 843 return (S_ERROR); 844 845 if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == NULL) 846 return (S_ERROR); 847 848 if (ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_array, NULL) == 849 (Os_desc *)S_ERROR) 850 return (S_ERROR); 851 852 osp = isec->is_osdesc; 853 854 if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY)) 855 ofl->ofl_osinitarray = osp; 856 if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY)) 857 ofl->ofl_ospreinitarray = osp; 858 else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY)) 859 ofl->ofl_osfiniarray = osp; 860 861 /* 862 * Create relocations against this section to initialize it to the 863 * function addresses. 864 */ 865 reld.rel_isdesc = isec; 866 reld.rel_aux = NULL; 867 reld.rel_flags = FLG_REL_LOAD; 868 869 /* 870 * Fabricate the relocation information (as if a relocation record had 871 * been input - see init_rel()). 872 */ 873 reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr; 874 reld.rel_roffset = 0; 875 reld.rel_raddend = 0; 876 877 /* 878 * Create a minimal relocation record to satisfy process_sym_reloc() 879 * debugging requirements. 880 */ 881 reloc.r_offset = 0; 882 reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr); 883 reloc.r_addend = 0; 884 885 DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp, 886 ld_targ.t_m.m_rel_sht_type)); 887 for (APLIST_TRAVERSE(alp, idx, sdp)) { 888 reld.rel_sym = sdp; 889 890 if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec, 891 MSG_INTL(MSG_STR_COMMAND), 0) == S_ERROR) { 892 ret = S_ERROR; 893 continue; 894 } 895 896 reld.rel_roffset += (Xword)sizeof (Addr); 897 reloc.r_offset = reld.rel_roffset; 898 } 899 900 return (ret); 901 } 902 903 /* 904 * Build a comment section (-Qy option). 905 */ 906 static uintptr_t 907 make_comment(Ofl_desc *ofl) 908 { 909 Shdr *shdr; 910 Elf_Data *data; 911 Is_desc *isec; 912 913 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0, 914 &isec, &shdr, &data) == S_ERROR) 915 return (S_ERROR); 916 917 data->d_buf = (void *)ofl->ofl_sgsid; 918 data->d_size = strlen(ofl->ofl_sgsid) + 1; 919 data->d_align = 1; 920 921 shdr->sh_size = (Xword)data->d_size; 922 shdr->sh_flags = 0; 923 shdr->sh_addralign = 1; 924 925 return ((uintptr_t)ld_place_section(ofl, isec, NULL, 926 ld_targ.t_id.id_note, NULL)); 927 } 928 929 /* 930 * Make the dynamic section. Calculate the size of any strings referenced 931 * within this structure, they will be added to the global string table 932 * (.dynstr). This routine should be called before make_dynstr(). 933 * 934 * This routine must be maintained in parallel with update_odynamic() 935 * in update.c 936 */ 937 static uintptr_t 938 make_dynamic(Ofl_desc *ofl) 939 { 940 Shdr *shdr; 941 Os_desc *osp; 942 Elf_Data *data; 943 Is_desc *isec; 944 size_t cnt = 0; 945 Aliste idx; 946 Ifl_desc *ifl; 947 Sym_desc *sdp; 948 size_t size; 949 Str_tbl *strtbl; 950 ofl_flag_t flags = ofl->ofl_flags; 951 int not_relobj = !(flags & FLG_OF_RELOBJ); 952 int unused = 0; 953 954 /* 955 * Select the required string table. 956 */ 957 if (OFL_IS_STATIC_OBJ(ofl)) 958 strtbl = ofl->ofl_strtab; 959 else 960 strtbl = ofl->ofl_dynstrtab; 961 962 /* 963 * Only a limited subset of DT_ entries apply to relocatable 964 * objects. See the comment at the head of update_odynamic() in 965 * update.c for details. 966 */ 967 if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0, 968 &isec, &shdr, &data) == S_ERROR) 969 return (S_ERROR); 970 971 /* 972 * new_section() does not set SHF_ALLOC. If we're building anything 973 * besides a relocatable object, then the .dynamic section should 974 * reside in allocatable memory. 975 */ 976 if (not_relobj) 977 shdr->sh_flags |= SHF_ALLOC; 978 979 /* 980 * new_section() does not set SHF_WRITE. If we're building an object 981 * that specifies an interpretor, then a DT_DEBUG entry is created, 982 * which is initialized to the applications link-map list at runtime. 983 */ 984 if (ofl->ofl_osinterp) 985 shdr->sh_flags |= SHF_WRITE; 986 987 osp = ofl->ofl_osdynamic = 988 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynamic, NULL); 989 990 /* 991 * Reserve entries for any needed dependencies. 992 */ 993 for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) { 994 if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))) 995 continue; 996 997 /* 998 * If this dependency didn't satisfy any symbol references, 999 * generate a debugging diagnostic (ld(1) -Dunused can be used 1000 * to display these). If this is a standard needed dependency, 1001 * and -z ignore is in effect, drop the dependency. Explicitly 1002 * defined dependencies (i.e., -N dep) don't get dropped, and 1003 * are flagged as being required to simplify update_odynamic() 1004 * processing. 1005 */ 1006 if ((ifl->ifl_flags & FLG_IF_NEEDSTR) || 1007 ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) { 1008 if (unused++ == 0) 1009 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 1010 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname, 1011 (ifl->ifl_flags & FLG_IF_NEEDSTR), 0)); 1012 1013 /* 1014 * Guidance: Remove unused dependency. 1015 * 1016 * If -z ignore is in effect, this warning is not 1017 * needed because we will quietly remove the unused 1018 * dependency. 1019 */ 1020 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_UNUSED) && 1021 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0)) 1022 ld_eprintf(ofl, ERR_GUIDANCE, 1023 MSG_INTL(MSG_GUIDE_UNUSED), 1024 ifl->ifl_soname); 1025 1026 if (ifl->ifl_flags & FLG_IF_NEEDSTR) 1027 ifl->ifl_flags |= FLG_IF_DEPREQD; 1028 else if (ifl->ifl_flags & FLG_IF_IGNORE) 1029 continue; 1030 } 1031 1032 /* 1033 * If this object requires a DT_POSFLAG_1 entry, reserve it. 1034 */ 1035 if ((ifl->ifl_flags & MSK_IF_POSFLAG1) && not_relobj) 1036 cnt++; 1037 1038 if (st_insert(strtbl, ifl->ifl_soname) == -1) 1039 return (S_ERROR); 1040 cnt++; 1041 1042 /* 1043 * If the needed entry contains the $ORIGIN token make sure 1044 * the associated DT_1_FLAGS entry is created. 1045 */ 1046 if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) { 1047 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 1048 ofl->ofl_dtflags |= DF_ORIGIN; 1049 } 1050 } 1051 1052 if (unused) 1053 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 1054 1055 if (not_relobj) { 1056 /* 1057 * Reserve entries for any per-symbol auxiliary/filter strings. 1058 */ 1059 cnt += alist_nitems(ofl->ofl_dtsfltrs); 1060 1061 /* 1062 * Reserve entries for _init() and _fini() section addresses. 1063 */ 1064 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), 1065 SYM_NOHASH, NULL, ofl)) != NULL) && 1066 (sdp->sd_ref == REF_REL_NEED) && 1067 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 1068 sdp->sd_flags |= FLG_SY_UPREQD; 1069 cnt++; 1070 } 1071 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U), 1072 SYM_NOHASH, NULL, ofl)) != NULL) && 1073 (sdp->sd_ref == REF_REL_NEED) && 1074 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 1075 sdp->sd_flags |= FLG_SY_UPREQD; 1076 cnt++; 1077 } 1078 1079 /* 1080 * Reserve entries for any soname, filter name (shared libs 1081 * only), run-path pointers, cache names and audit requirements. 1082 */ 1083 if (ofl->ofl_soname) { 1084 cnt++; 1085 if (st_insert(strtbl, ofl->ofl_soname) == -1) 1086 return (S_ERROR); 1087 } 1088 if (ofl->ofl_filtees) { 1089 cnt++; 1090 if (st_insert(strtbl, ofl->ofl_filtees) == -1) 1091 return (S_ERROR); 1092 1093 /* 1094 * If the filtees entry contains the $ORIGIN token 1095 * make sure the associated DT_1_FLAGS entry is created. 1096 */ 1097 if (strstr(ofl->ofl_filtees, 1098 MSG_ORIG(MSG_STR_ORIGIN))) { 1099 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 1100 ofl->ofl_dtflags |= DF_ORIGIN; 1101 } 1102 } 1103 } 1104 1105 if (ofl->ofl_rpath) { 1106 cnt += 2; /* DT_RPATH & DT_RUNPATH */ 1107 if (st_insert(strtbl, ofl->ofl_rpath) == -1) 1108 return (S_ERROR); 1109 1110 /* 1111 * If the rpath entry contains the $ORIGIN token make sure 1112 * the associated DT_1_FLAGS entry is created. 1113 */ 1114 if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) { 1115 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 1116 ofl->ofl_dtflags |= DF_ORIGIN; 1117 } 1118 } 1119 1120 if (not_relobj) { 1121 Aliste idx; 1122 Sg_desc *sgp; 1123 1124 if (ofl->ofl_config) { 1125 cnt++; 1126 if (st_insert(strtbl, ofl->ofl_config) == -1) 1127 return (S_ERROR); 1128 1129 /* 1130 * If the config entry contains the $ORIGIN token 1131 * make sure the associated DT_1_FLAGS entry is created. 1132 */ 1133 if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) { 1134 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 1135 ofl->ofl_dtflags |= DF_ORIGIN; 1136 } 1137 } 1138 if (ofl->ofl_depaudit) { 1139 cnt++; 1140 if (st_insert(strtbl, ofl->ofl_depaudit) == -1) 1141 return (S_ERROR); 1142 } 1143 if (ofl->ofl_audit) { 1144 cnt++; 1145 if (st_insert(strtbl, ofl->ofl_audit) == -1) 1146 return (S_ERROR); 1147 } 1148 1149 /* 1150 * Reserve entries for the DT_HASH, DT_STRTAB, DT_STRSZ, 1151 * DT_SYMTAB, DT_SYMENT, and DT_CHECKSUM. 1152 */ 1153 cnt += 6; 1154 1155 /* 1156 * If we are including local functions at the head of 1157 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB 1158 * and DT_SUNW_SYMSZ. 1159 */ 1160 if (OFL_ALLOW_LDYNSYM(ofl)) 1161 cnt += 2; 1162 1163 if ((ofl->ofl_dynsymsortcnt > 0) || 1164 (ofl->ofl_dyntlssortcnt > 0)) 1165 cnt++; /* DT_SUNW_SORTENT */ 1166 1167 if (ofl->ofl_dynsymsortcnt > 0) 1168 cnt += 2; /* DT_SUNW_[SYMSORT|SYMSORTSZ] */ 1169 1170 if (ofl->ofl_dyntlssortcnt > 0) 1171 cnt += 2; /* DT_SUNW_[TLSSORT|TLSSORTSZ] */ 1172 1173 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == 1174 FLG_OF_VERDEF) 1175 cnt += 2; /* DT_VERDEF & DT_VERDEFNUM */ 1176 1177 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == 1178 FLG_OF_VERNEED) 1179 cnt += 2; /* DT_VERNEED & DT_VERNEEDNUM */ 1180 1181 if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt) 1182 cnt++; /* DT_RELACOUNT */ 1183 1184 if (flags & FLG_OF_TEXTREL) /* DT_TEXTREL */ 1185 cnt++; 1186 1187 if (ofl->ofl_osfiniarray) /* DT_FINI_ARRAY */ 1188 cnt += 2; /* DT_FINI_ARRAYSZ */ 1189 1190 if (ofl->ofl_osinitarray) /* DT_INIT_ARRAY */ 1191 cnt += 2; /* DT_INIT_ARRAYSZ */ 1192 1193 if (ofl->ofl_ospreinitarray) /* DT_PREINIT_ARRAY & */ 1194 cnt += 2; /* DT_PREINIT_ARRAYSZ */ 1195 1196 /* 1197 * If we have plt's reserve a DT_PLTRELSZ, DT_PLTREL and 1198 * DT_JMPREL. 1199 */ 1200 if (ofl->ofl_pltcnt) 1201 cnt += 3; 1202 1203 /* 1204 * If plt padding is needed (Sparcv9). 1205 */ 1206 if (ofl->ofl_pltpad) 1207 cnt += 2; /* DT_PLTPAD & DT_PLTPADSZ */ 1208 1209 /* 1210 * If we have any relocations reserve a DT_REL, DT_RELSZ and 1211 * DT_RELENT entry. 1212 */ 1213 if (ofl->ofl_relocsz) 1214 cnt += 3; 1215 1216 /* 1217 * If a syminfo section is required create DT_SYMINFO, 1218 * DT_SYMINSZ, and DT_SYMINENT entries. 1219 */ 1220 if (flags & FLG_OF_SYMINFO) 1221 cnt += 3; 1222 1223 /* 1224 * If there are any partially initialized sections allocate 1225 * DT_MOVETAB, DT_MOVESZ and DT_MOVEENT. 1226 */ 1227 if (ofl->ofl_osmove) 1228 cnt += 3; 1229 1230 /* 1231 * Allocate one DT_REGISTER entry for every register symbol. 1232 */ 1233 cnt += ofl->ofl_regsymcnt; 1234 1235 /* 1236 * Reserve a entry for each '-zrtldinfo=...' specified 1237 * on the command line. 1238 */ 1239 for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp)) 1240 cnt++; 1241 1242 /* 1243 * The following entry should only be placed in a segment that 1244 * is writable. 1245 */ 1246 if (((sgp = osp->os_sgdesc) != NULL) && 1247 (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp) 1248 cnt++; /* DT_DEBUG */ 1249 1250 /* 1251 * Capabilities require a .dynamic entry for the .SUNW_cap 1252 * section. 1253 */ 1254 if (ofl->ofl_oscap) 1255 cnt++; /* DT_SUNW_CAP */ 1256 1257 /* 1258 * Symbol capabilities require a .dynamic entry for the 1259 * .SUNW_capinfo section. 1260 */ 1261 if (ofl->ofl_oscapinfo) 1262 cnt++; /* DT_SUNW_CAPINFO */ 1263 1264 /* 1265 * Capabilities chain information requires a .SUNW_capchain 1266 * entry (DT_SUNW_CAPCHAIN), entry size (DT_SUNW_CAPCHAINENT), 1267 * and total size (DT_SUNW_CAPCHAINSZ). 1268 */ 1269 if (ofl->ofl_oscapchain) 1270 cnt += 3; 1271 1272 if (flags & FLG_OF_SYMBOLIC) 1273 cnt++; /* DT_SYMBOLIC */ 1274 1275 if (ofl->ofl_aslr != 0) /* DT_SUNW_ASLR */ 1276 cnt++; 1277 } 1278 1279 /* DT_SUNW_KMOD */ 1280 if (ofl->ofl_flags & FLG_OF_KMOD) 1281 cnt++; 1282 1283 /* 1284 * Account for Architecture dependent .dynamic entries, and defaults. 1285 */ 1286 (*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt); 1287 1288 /* 1289 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also, 1290 * allow room for the unused extra DT_NULLs. These are included 1291 * to allow an ELF editor room to add items later. 1292 */ 1293 cnt += 4 + DYNAMIC_EXTRA_ELTS; 1294 1295 /* 1296 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the 1297 * linker that produced the output object. This information 1298 * allows us to determine whether a given object was linked 1299 * natively, or by a linker running on a different type of 1300 * system. This information can be valuable if one suspects 1301 * that a problem might be due to alignment or byte order issues. 1302 */ 1303 cnt++; 1304 1305 /* 1306 * Determine the size of the section from the number of entries. 1307 */ 1308 size = cnt * (size_t)shdr->sh_entsize; 1309 1310 shdr->sh_size = (Xword)size; 1311 data->d_size = size; 1312 1313 /* 1314 * There are several tags that are specific to the Solaris osabi 1315 * range which we unconditionally put into any dynamic section 1316 * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such, 1317 * any Solaris object with a dynamic section should be tagged as 1318 * ELFOSABI_SOLARIS. 1319 */ 1320 ofl->ofl_flags |= FLG_OF_OSABI; 1321 1322 return ((uintptr_t)ofl->ofl_osdynamic); 1323 } 1324 1325 /* 1326 * Build the GOT section and its associated relocation entries. 1327 */ 1328 uintptr_t 1329 ld_make_got(Ofl_desc *ofl) 1330 { 1331 Elf_Data *data; 1332 Shdr *shdr; 1333 Is_desc *isec; 1334 size_t size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize; 1335 size_t rsize = (size_t)ofl->ofl_relocgotsz; 1336 1337 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0, 1338 &isec, &shdr, &data) == S_ERROR) 1339 return (S_ERROR); 1340 1341 data->d_size = size; 1342 1343 shdr->sh_flags |= SHF_WRITE; 1344 shdr->sh_size = (Xword)size; 1345 shdr->sh_entsize = ld_targ.t_m.m_got_entsize; 1346 1347 ofl->ofl_osgot = ld_place_section(ofl, isec, NULL, 1348 ld_targ.t_id.id_got, NULL); 1349 if (ofl->ofl_osgot == (Os_desc *)S_ERROR) 1350 return (S_ERROR); 1351 1352 ofl->ofl_osgot->os_szoutrels = (Xword)rsize; 1353 1354 return (1); 1355 } 1356 1357 /* 1358 * Build an interpreter section. 1359 */ 1360 static uintptr_t 1361 make_interp(Ofl_desc *ofl) 1362 { 1363 Shdr *shdr; 1364 Elf_Data *data; 1365 Is_desc *isec; 1366 const char *iname = ofl->ofl_interp; 1367 size_t size; 1368 1369 /* 1370 * If -z nointerp is in effect, don't create an interpreter section. 1371 */ 1372 if (ofl->ofl_flags1 & FLG_OF1_NOINTRP) 1373 return (1); 1374 1375 /* 1376 * An .interp section is always created for a dynamic executable. 1377 * A user can define the interpreter to use. This definition overrides 1378 * the default that would be recorded in an executable, and triggers 1379 * the creation of an .interp section in any other object. Presumably 1380 * the user knows what they are doing. Refer to the generic ELF ABI 1381 * section 5-4, and the ld(1) -I option. 1382 */ 1383 if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC | 1384 FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname) 1385 return (1); 1386 1387 /* 1388 * In the case of a dynamic executable, supply a default interpreter 1389 * if the user has not specified their own. 1390 */ 1391 if (iname == NULL) 1392 iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp; 1393 1394 size = strlen(iname) + 1; 1395 1396 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0, 1397 &isec, &shdr, &data) == S_ERROR) 1398 return (S_ERROR); 1399 1400 data->d_size = size; 1401 shdr->sh_size = (Xword)size; 1402 data->d_align = shdr->sh_addralign = 1; 1403 1404 ofl->ofl_osinterp = 1405 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_interp, NULL); 1406 return ((uintptr_t)ofl->ofl_osinterp); 1407 } 1408 1409 /* 1410 * Common function used to build the SHT_SUNW_versym section, SHT_SUNW_syminfo 1411 * section, and SHT_SUNW_capinfo section. Each of these sections provide 1412 * additional symbol information, and their size parallels the associated 1413 * symbol table. 1414 */ 1415 static Os_desc * 1416 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident) 1417 { 1418 Shdr *shdr; 1419 Elf_Data *data; 1420 Is_desc *isec; 1421 1422 /* 1423 * We don't know the size of this section yet, so set it to 0. The 1424 * size gets filled in after the associated symbol table is sized. 1425 */ 1426 if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) == 1427 S_ERROR) 1428 return ((Os_desc *)S_ERROR); 1429 1430 return (ld_place_section(ofl, isec, NULL, ident, NULL)); 1431 } 1432 1433 /* 1434 * Determine whether a symbol capability is redundant because the object 1435 * capabilities are more restrictive. 1436 */ 1437 inline static int 1438 is_cap_redundant(Objcapset *ocapset, Objcapset *scapset) 1439 { 1440 Alist *oalp, *salp; 1441 elfcap_mask_t omsk, smsk; 1442 1443 /* 1444 * Inspect any platform capabilities. If the object defines platform 1445 * capabilities, then the object will only be loaded for those 1446 * platforms. A symbol capability set that doesn't define the same 1447 * platforms is redundant, and a symbol capability that does not provide 1448 * at least one platform name that matches a platform name in the object 1449 * capabilities will never execute (as the object wouldn't have been 1450 * loaded). 1451 */ 1452 oalp = ocapset->oc_plat.cl_val; 1453 salp = scapset->oc_plat.cl_val; 1454 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp))) 1455 return (1); 1456 1457 /* 1458 * If the symbol capability set defines platforms, and the object 1459 * doesn't, then the symbol set is more restrictive. 1460 */ 1461 if (salp && (oalp == NULL)) 1462 return (0); 1463 1464 /* 1465 * Next, inspect any machine name capabilities. If the object defines 1466 * machine name capabilities, then the object will only be loaded for 1467 * those machines. A symbol capability set that doesn't define the same 1468 * machine names is redundant, and a symbol capability that does not 1469 * provide at least one machine name that matches a machine name in the 1470 * object capabilities will never execute (as the object wouldn't have 1471 * been loaded). 1472 */ 1473 oalp = ocapset->oc_plat.cl_val; 1474 salp = scapset->oc_plat.cl_val; 1475 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp))) 1476 return (1); 1477 1478 /* 1479 * If the symbol capability set defines machine names, and the object 1480 * doesn't, then the symbol set is more restrictive. 1481 */ 1482 if (salp && (oalp == NULL)) 1483 return (0); 1484 1485 /* 1486 * Next, inspect any hardware capabilities. If the objects hardware 1487 * capabilities are greater than or equal to that of the symbols 1488 * capabilities, then the symbol capability set is redundant. If the 1489 * symbols hardware capabilities are greater that the objects, then the 1490 * symbol set is more restrictive. 1491 * 1492 * Note that this is a somewhat arbitrary definition, as each capability 1493 * bit is independent of the others, and some of the higher order bits 1494 * could be considered to be less important than lower ones. However, 1495 * this is the only reasonable non-subjective definition. 1496 */ 1497 omsk = ocapset->oc_hw_2.cm_val; 1498 smsk = scapset->oc_hw_2.cm_val; 1499 if ((omsk > smsk) || (omsk && (omsk == smsk))) 1500 return (1); 1501 if (omsk < smsk) 1502 return (0); 1503 1504 /* 1505 * Finally, inspect the remaining hardware capabilities. 1506 */ 1507 omsk = ocapset->oc_hw_1.cm_val; 1508 smsk = scapset->oc_hw_1.cm_val; 1509 if ((omsk > smsk) || (omsk && (omsk == smsk))) 1510 return (1); 1511 1512 return (0); 1513 } 1514 1515 /* 1516 * Capabilities values might have been assigned excluded values. These 1517 * excluded values should be removed before calculating any capabilities 1518 * sections size. 1519 */ 1520 static void 1521 capmask_value(Lm_list *lml, Word type, Capmask *capmask, int *title) 1522 { 1523 /* 1524 * First determine whether any bits should be excluded. 1525 */ 1526 if ((capmask->cm_val & capmask->cm_exc) == 0) 1527 return; 1528 1529 DBG_CALL(Dbg_cap_post_title(lml, title)); 1530 1531 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_CURRENT, type, 1532 capmask->cm_val, ld_targ.t_m.m_mach)); 1533 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_EXCLUDE, type, 1534 capmask->cm_exc, ld_targ.t_m.m_mach)); 1535 1536 capmask->cm_val &= ~capmask->cm_exc; 1537 1538 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_RESOLVED, type, 1539 capmask->cm_val, ld_targ.t_m.m_mach)); 1540 } 1541 1542 static void 1543 capstr_value(Lm_list *lml, Word type, Caplist *caplist, int *title) 1544 { 1545 Aliste idx1, idx2; 1546 char *estr; 1547 Capstr *capstr; 1548 Boolean found = FALSE; 1549 1550 /* 1551 * First determine whether any strings should be excluded. 1552 */ 1553 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) { 1554 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) { 1555 if (strcmp(estr, capstr->cs_str) == 0) { 1556 found = TRUE; 1557 break; 1558 } 1559 } 1560 } 1561 1562 if (found == FALSE) 1563 return; 1564 1565 /* 1566 * Traverse the current strings, then delete the excluded strings, 1567 * and finally display the resolved strings. 1568 */ 1569 if (DBG_ENABLED) { 1570 Dbg_cap_post_title(lml, title); 1571 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) { 1572 Dbg_cap_ptr_entry(lml, DBG_STATE_CURRENT, type, 1573 capstr->cs_str); 1574 } 1575 } 1576 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) { 1577 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) { 1578 if (strcmp(estr, capstr->cs_str) == 0) { 1579 DBG_CALL(Dbg_cap_ptr_entry(lml, 1580 DBG_STATE_EXCLUDE, type, capstr->cs_str)); 1581 alist_delete(caplist->cl_val, &idx2); 1582 break; 1583 } 1584 } 1585 } 1586 if (DBG_ENABLED) { 1587 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) { 1588 Dbg_cap_ptr_entry(lml, DBG_STATE_RESOLVED, type, 1589 capstr->cs_str); 1590 } 1591 } 1592 } 1593 1594 /* 1595 * Build a capabilities section. 1596 */ 1597 #define CAP_UPDATE(cap, capndx, tag, val) \ 1598 cap->c_tag = tag; \ 1599 cap->c_un.c_val = val; \ 1600 cap++, capndx++; 1601 1602 static uintptr_t 1603 make_cap(Ofl_desc *ofl, Word shtype, const char *shname, int ident) 1604 { 1605 Shdr *shdr; 1606 Elf_Data *data; 1607 Is_desc *isec; 1608 Cap *cap; 1609 size_t size = 0; 1610 Word capndx = 0; 1611 Str_tbl *strtbl; 1612 Objcapset *ocapset = &ofl->ofl_ocapset; 1613 Aliste idx1; 1614 Capstr *capstr; 1615 int title = 0; 1616 1617 /* 1618 * Determine which string table to use for any CA_SUNW_MACH, 1619 * CA_SUNW_PLAT, or CA_SUNW_ID strings. 1620 */ 1621 if (OFL_IS_STATIC_OBJ(ofl)) 1622 strtbl = ofl->ofl_strtab; 1623 else 1624 strtbl = ofl->ofl_dynstrtab; 1625 1626 /* 1627 * If symbol capabilities have been requested, but none have been 1628 * created, warn the user. This scenario can occur if none of the 1629 * input relocatable objects defined any object capabilities. 1630 */ 1631 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && (ofl->ofl_capsymcnt == 0)) 1632 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND)); 1633 1634 /* 1635 * If symbol capabilities have been collected, but no symbols are left 1636 * referencing these capabilities, promote the capability groups back 1637 * to an object capability definition. 1638 */ 1639 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && ofl->ofl_capsymcnt && 1640 (ofl->ofl_capfamilies == NULL)) { 1641 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND)); 1642 ld_cap_move_symtoobj(ofl); 1643 ofl->ofl_capsymcnt = 0; 1644 ofl->ofl_capgroups = NULL; 1645 ofl->ofl_flags &= ~FLG_OF_OTOSCAP; 1646 } 1647 1648 /* 1649 * Remove any excluded capabilities. 1650 */ 1651 capstr_value(ofl->ofl_lml, CA_SUNW_PLAT, &ocapset->oc_plat, &title); 1652 capstr_value(ofl->ofl_lml, CA_SUNW_MACH, &ocapset->oc_mach, &title); 1653 capmask_value(ofl->ofl_lml, CA_SUNW_HW_2, &ocapset->oc_hw_2, &title); 1654 capmask_value(ofl->ofl_lml, CA_SUNW_HW_1, &ocapset->oc_hw_1, &title); 1655 capmask_value(ofl->ofl_lml, CA_SUNW_SF_1, &ocapset->oc_sf_1, &title); 1656 1657 /* 1658 * Determine how many entries are required for any object capabilities. 1659 */ 1660 size += alist_nitems(ocapset->oc_plat.cl_val); 1661 size += alist_nitems(ocapset->oc_mach.cl_val); 1662 if (ocapset->oc_hw_2.cm_val) 1663 size++; 1664 if (ocapset->oc_hw_1.cm_val) 1665 size++; 1666 if (ocapset->oc_sf_1.cm_val) 1667 size++; 1668 1669 /* 1670 * Only identify a capabilities group if the group has content. If a 1671 * capabilities identifier exists, and no other capabilities have been 1672 * supplied, remove the identifier. This scenario could exist if a 1673 * user mistakenly defined a lone identifier, or if an identified group 1674 * was overridden so as to clear the existing capabilities and the 1675 * identifier was not also cleared. 1676 */ 1677 if (ocapset->oc_id.cs_str) { 1678 if (size) 1679 size++; 1680 else 1681 ocapset->oc_id.cs_str = NULL; 1682 } 1683 if (size) 1684 size++; /* Add CA_SUNW_NULL */ 1685 1686 /* 1687 * Determine how many entries are required for any symbol capabilities. 1688 */ 1689 if (ofl->ofl_capsymcnt) { 1690 /* 1691 * If there are no object capabilities, a CA_SUNW_NULL entry 1692 * is required before any symbol capabilities. 1693 */ 1694 if (size == 0) 1695 size++; 1696 size += ofl->ofl_capsymcnt; 1697 } 1698 1699 if (size == 0) 1700 return (0); 1701 1702 if (new_section(ofl, shtype, shname, size, &isec, 1703 &shdr, &data) == S_ERROR) 1704 return (S_ERROR); 1705 1706 if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL) 1707 return (S_ERROR); 1708 1709 cap = (Cap *)data->d_buf; 1710 1711 /* 1712 * Fill in any object capabilities. If there is an identifier, then the 1713 * identifier comes first. The remaining items follow in precedence 1714 * order, although the order isn't important for runtime verification. 1715 */ 1716 if (ocapset->oc_id.cs_str) { 1717 ofl->ofl_flags |= FLG_OF_CAPSTRS; 1718 if (st_insert(strtbl, ocapset->oc_id.cs_str) == -1) 1719 return (S_ERROR); 1720 ocapset->oc_id.cs_ndx = capndx; 1721 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0); 1722 } 1723 if (ocapset->oc_plat.cl_val) { 1724 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS); 1725 1726 /* 1727 * Insert any platform name strings in the appropriate string 1728 * table. The capability value can't be filled in yet, as the 1729 * final offset of the strings isn't known until later. 1730 */ 1731 for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx1, capstr)) { 1732 if (st_insert(strtbl, capstr->cs_str) == -1) 1733 return (S_ERROR); 1734 capstr->cs_ndx = capndx; 1735 CAP_UPDATE(cap, capndx, CA_SUNW_PLAT, 0); 1736 } 1737 } 1738 if (ocapset->oc_mach.cl_val) { 1739 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS); 1740 1741 /* 1742 * Insert the machine name strings in the appropriate string 1743 * table. The capability value can't be filled in yet, as the 1744 * final offset of the strings isn't known until later. 1745 */ 1746 for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx1, capstr)) { 1747 if (st_insert(strtbl, capstr->cs_str) == -1) 1748 return (S_ERROR); 1749 capstr->cs_ndx = capndx; 1750 CAP_UPDATE(cap, capndx, CA_SUNW_MACH, 0); 1751 } 1752 } 1753 if (ocapset->oc_hw_2.cm_val) { 1754 ofl->ofl_flags |= FLG_OF_PTCAP; 1755 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2, ocapset->oc_hw_2.cm_val); 1756 } 1757 if (ocapset->oc_hw_1.cm_val) { 1758 ofl->ofl_flags |= FLG_OF_PTCAP; 1759 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1, ocapset->oc_hw_1.cm_val); 1760 } 1761 if (ocapset->oc_sf_1.cm_val) { 1762 ofl->ofl_flags |= FLG_OF_PTCAP; 1763 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1, ocapset->oc_sf_1.cm_val); 1764 } 1765 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0); 1766 1767 /* 1768 * Fill in any symbol capabilities. 1769 */ 1770 if (ofl->ofl_capgroups) { 1771 Cap_group *cgp; 1772 1773 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) { 1774 Objcapset *scapset = &cgp->cg_set; 1775 Aliste idx2; 1776 Is_desc *isp; 1777 1778 cgp->cg_ndx = capndx; 1779 1780 if (scapset->oc_id.cs_str) { 1781 ofl->ofl_flags |= FLG_OF_CAPSTRS; 1782 /* 1783 * Insert the identifier string in the 1784 * appropriate string table. The capability 1785 * value can't be filled in yet, as the final 1786 * offset of the string isn't known until later. 1787 */ 1788 if (st_insert(strtbl, 1789 scapset->oc_id.cs_str) == -1) 1790 return (S_ERROR); 1791 scapset->oc_id.cs_ndx = capndx; 1792 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0); 1793 } 1794 1795 if (scapset->oc_plat.cl_val) { 1796 ofl->ofl_flags |= FLG_OF_CAPSTRS; 1797 1798 /* 1799 * Insert the platform name string in the 1800 * appropriate string table. The capability 1801 * value can't be filled in yet, as the final 1802 * offset of the string isn't known until later. 1803 */ 1804 for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, 1805 idx2, capstr)) { 1806 if (st_insert(strtbl, 1807 capstr->cs_str) == -1) 1808 return (S_ERROR); 1809 capstr->cs_ndx = capndx; 1810 CAP_UPDATE(cap, capndx, 1811 CA_SUNW_PLAT, 0); 1812 } 1813 } 1814 if (scapset->oc_mach.cl_val) { 1815 ofl->ofl_flags |= FLG_OF_CAPSTRS; 1816 1817 /* 1818 * Insert the machine name string in the 1819 * appropriate string table. The capability 1820 * value can't be filled in yet, as the final 1821 * offset of the string isn't known until later. 1822 */ 1823 for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, 1824 idx2, capstr)) { 1825 if (st_insert(strtbl, 1826 capstr->cs_str) == -1) 1827 return (S_ERROR); 1828 capstr->cs_ndx = capndx; 1829 CAP_UPDATE(cap, capndx, 1830 CA_SUNW_MACH, 0); 1831 } 1832 } 1833 if (scapset->oc_hw_2.cm_val) { 1834 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2, 1835 scapset->oc_hw_2.cm_val); 1836 } 1837 if (scapset->oc_hw_1.cm_val) { 1838 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1, 1839 scapset->oc_hw_1.cm_val); 1840 } 1841 if (scapset->oc_sf_1.cm_val) { 1842 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1, 1843 scapset->oc_sf_1.cm_val); 1844 } 1845 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0); 1846 1847 /* 1848 * If any object capabilities are available, determine 1849 * whether these symbol capabilities are less 1850 * restrictive, and hence redundant. 1851 */ 1852 if (((ofl->ofl_flags & FLG_OF_PTCAP) == 0) || 1853 (is_cap_redundant(ocapset, scapset) == 0)) 1854 continue; 1855 1856 /* 1857 * Indicate any files that provide redundant symbol 1858 * capabilities. 1859 */ 1860 for (APLIST_TRAVERSE(cgp->cg_secs, idx2, isp)) { 1861 ld_eprintf(ofl, ERR_WARNING, 1862 MSG_INTL(MSG_CAP_REDUNDANT), 1863 isp->is_file->ifl_name, 1864 EC_WORD(isp->is_scnndx), isp->is_name); 1865 } 1866 } 1867 } 1868 1869 /* 1870 * If capabilities strings are required, the sh_info field of the 1871 * section header will be set to the associated string table. 1872 */ 1873 if (ofl->ofl_flags & FLG_OF_CAPSTRS) 1874 shdr->sh_flags |= SHF_INFO_LINK; 1875 1876 /* 1877 * Place these capabilities in the output file. 1878 */ 1879 if ((ofl->ofl_oscap = ld_place_section(ofl, isec, 1880 NULL, ident, NULL)) == (Os_desc *)S_ERROR) 1881 return (S_ERROR); 1882 1883 /* 1884 * If symbol capabilities are required, then a .SUNW_capinfo section is 1885 * also created. This table will eventually be sized to match the 1886 * associated symbol table. 1887 */ 1888 if (ofl->ofl_capfamilies) { 1889 if ((ofl->ofl_oscapinfo = make_sym_sec(ofl, 1890 MSG_ORIG(MSG_SCN_SUNWCAPINFO), SHT_SUNW_capinfo, 1891 ld_targ.t_id.id_capinfo)) == (Os_desc *)S_ERROR) 1892 return (S_ERROR); 1893 1894 /* 1895 * If we're generating a dynamic object, capabilities family 1896 * members are maintained in a .SUNW_capchain section. 1897 */ 1898 if (ofl->ofl_capchaincnt && 1899 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) { 1900 if (new_section(ofl, SHT_SUNW_capchain, 1901 MSG_ORIG(MSG_SCN_SUNWCAPCHAIN), 1902 ofl->ofl_capchaincnt, &isec, &shdr, 1903 &data) == S_ERROR) 1904 return (S_ERROR); 1905 1906 ofl->ofl_oscapchain = ld_place_section(ofl, isec, 1907 NULL, ld_targ.t_id.id_capchain, NULL); 1908 if (ofl->ofl_oscapchain == (Os_desc *)S_ERROR) 1909 return (S_ERROR); 1910 1911 } 1912 } 1913 return (1); 1914 } 1915 #undef CAP_UPDATE 1916 1917 /* 1918 * Build the PLT section and its associated relocation entries. 1919 */ 1920 static uintptr_t 1921 make_plt(Ofl_desc *ofl) 1922 { 1923 Shdr *shdr; 1924 Elf_Data *data; 1925 Is_desc *isec; 1926 size_t size = ld_targ.t_m.m_plt_reservsz + 1927 (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) * 1928 ld_targ.t_m.m_plt_entsize); 1929 size_t rsize = (size_t)ofl->ofl_relocpltsz; 1930 1931 /* 1932 * On sparc, account for the NOP at the end of the plt. 1933 */ 1934 if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9)) 1935 size += sizeof (Word); 1936 1937 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0, 1938 &isec, &shdr, &data) == S_ERROR) 1939 return (S_ERROR); 1940 1941 data->d_size = size; 1942 data->d_align = ld_targ.t_m.m_plt_align; 1943 1944 shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags; 1945 shdr->sh_size = (Xword)size; 1946 shdr->sh_addralign = ld_targ.t_m.m_plt_align; 1947 shdr->sh_entsize = ld_targ.t_m.m_plt_entsize; 1948 1949 ofl->ofl_osplt = ld_place_section(ofl, isec, NULL, 1950 ld_targ.t_id.id_plt, NULL); 1951 if (ofl->ofl_osplt == (Os_desc *)S_ERROR) 1952 return (S_ERROR); 1953 1954 ofl->ofl_osplt->os_szoutrels = (Xword)rsize; 1955 1956 return (1); 1957 } 1958 1959 /* 1960 * Make the hash table. Only built for dynamic executables and shared 1961 * libraries, and provides hashed lookup into the global symbol table 1962 * (.dynsym) for the run-time linker to resolve symbol lookups. 1963 */ 1964 static uintptr_t 1965 make_hash(Ofl_desc *ofl) 1966 { 1967 Shdr *shdr; 1968 Elf_Data *data; 1969 Is_desc *isec; 1970 size_t size; 1971 Word nsyms = ofl->ofl_globcnt; 1972 size_t cnt; 1973 1974 /* 1975 * Allocate section header structures. We set entcnt to 0 1976 * because it's going to change after we place this section. 1977 */ 1978 if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0, 1979 &isec, &shdr, &data) == S_ERROR) 1980 return (S_ERROR); 1981 1982 /* 1983 * Place the section first since it will affect the local symbol 1984 * count. 1985 */ 1986 ofl->ofl_oshash = 1987 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_hash, NULL); 1988 if (ofl->ofl_oshash == (Os_desc *)S_ERROR) 1989 return (S_ERROR); 1990 1991 /* 1992 * Calculate the number of output hash buckets. 1993 */ 1994 ofl->ofl_hashbkts = findprime(nsyms); 1995 1996 /* 1997 * The size of the hash table is determined by 1998 * 1999 * i. the initial nbucket and nchain entries (2) 2000 * ii. the number of buckets (calculated above) 2001 * iii. the number of chains (this is based on the number of 2002 * symbols in the .dynsym array). 2003 */ 2004 cnt = 2 + ofl->ofl_hashbkts + DYNSYM_ALL_CNT(ofl); 2005 size = cnt * shdr->sh_entsize; 2006 2007 /* 2008 * Finalize the section header and data buffer initialization. 2009 */ 2010 if ((data->d_buf = libld_calloc(size, 1)) == NULL) 2011 return (S_ERROR); 2012 data->d_size = size; 2013 shdr->sh_size = (Xword)size; 2014 2015 return (1); 2016 } 2017 2018 /* 2019 * Generate the standard symbol table. Contains all locals and globals, 2020 * and resides in a non-allocatable section (ie. it can be stripped). 2021 */ 2022 static uintptr_t 2023 make_symtab(Ofl_desc *ofl) 2024 { 2025 Shdr *shdr; 2026 Elf_Data *data; 2027 Is_desc *isec; 2028 Is_desc *xisec = 0; 2029 size_t size; 2030 Word symcnt; 2031 2032 /* 2033 * Create the section headers. Note that we supply an ent_cnt 2034 * of 0. We won't know the count until the section has been placed. 2035 */ 2036 if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0, 2037 &isec, &shdr, &data) == S_ERROR) 2038 return (S_ERROR); 2039 2040 /* 2041 * Place the section first since it will affect the local symbol 2042 * count. 2043 */ 2044 if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, NULL, 2045 ld_targ.t_id.id_symtab, NULL)) == (Os_desc *)S_ERROR) 2046 return (S_ERROR); 2047 2048 /* 2049 * At this point we've created all but the 'shstrtab' section. 2050 * Determine if we have to use 'Extended Sections'. If so - then 2051 * also create a SHT_SYMTAB_SHNDX section. 2052 */ 2053 if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) { 2054 Shdr *xshdr; 2055 Elf_Data *xdata; 2056 2057 if (new_section(ofl, SHT_SYMTAB_SHNDX, 2058 MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec, 2059 &xshdr, &xdata) == S_ERROR) 2060 return (S_ERROR); 2061 2062 if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, NULL, 2063 ld_targ.t_id.id_symtab_ndx, NULL)) == (Os_desc *)S_ERROR) 2064 return (S_ERROR); 2065 } 2066 2067 /* 2068 * Calculated number of symbols, which need to be augmented by 2069 * the (yet to be created) .shstrtab entry. 2070 */ 2071 symcnt = (size_t)(1 + SYMTAB_ALL_CNT(ofl)); 2072 size = symcnt * shdr->sh_entsize; 2073 2074 /* 2075 * Finalize the section header and data buffer initialization. 2076 */ 2077 data->d_size = size; 2078 shdr->sh_size = (Xword)size; 2079 2080 /* 2081 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too. 2082 */ 2083 if (xisec) { 2084 size_t xsize = symcnt * sizeof (Word); 2085 2086 xisec->is_indata->d_size = xsize; 2087 xisec->is_shdr->sh_size = (Xword)xsize; 2088 } 2089 2090 return (1); 2091 } 2092 2093 /* 2094 * Build a dynamic symbol table. These tables reside in the text 2095 * segment of a dynamic executable or shared library. 2096 * 2097 * .SUNW_ldynsym contains local function symbols 2098 * .dynsym contains only globals symbols 2099 * 2100 * The two tables are created adjacent to each other, with .SUNW_ldynsym 2101 * coming first. 2102 */ 2103 static uintptr_t 2104 make_dynsym(Ofl_desc *ofl) 2105 { 2106 Shdr *shdr, *lshdr; 2107 Elf_Data *data, *ldata; 2108 Is_desc *isec, *lisec; 2109 size_t size; 2110 Xword cnt; 2111 int allow_ldynsym; 2112 2113 /* 2114 * Unless explicitly disabled, always produce a .SUNW_ldynsym section 2115 * when it is allowed by the file type, even if the resulting 2116 * table only ends up with a single STT_FILE in it. There are 2117 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB 2118 * entry in the .dynamic section, which is something we would 2119 * like to encourage, and (2) Without it, we cannot generate 2120 * the associated .SUNW_dyn[sym|tls]sort sections, which are of 2121 * value to DTrace. 2122 * 2123 * In practice, it is extremely rare for an object not to have 2124 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be 2125 * doing it anyway. 2126 */ 2127 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 2128 2129 /* 2130 * Create the section headers. Note that we supply an ent_cnt 2131 * of 0. We won't know the count until the section has been placed. 2132 */ 2133 if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM, 2134 MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR) 2135 return (S_ERROR); 2136 2137 if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0, 2138 &isec, &shdr, &data) == S_ERROR) 2139 return (S_ERROR); 2140 2141 /* 2142 * Place the section(s) first since it will affect the local symbol 2143 * count. 2144 */ 2145 if (allow_ldynsym && 2146 ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, NULL, 2147 ld_targ.t_id.id_ldynsym, NULL)) == (Os_desc *)S_ERROR)) 2148 return (S_ERROR); 2149 ofl->ofl_osdynsym = 2150 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynsym, NULL); 2151 if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR) 2152 return (S_ERROR); 2153 2154 cnt = DYNSYM_ALL_CNT(ofl); 2155 size = (size_t)cnt * shdr->sh_entsize; 2156 2157 /* 2158 * Finalize the section header and data buffer initialization. 2159 */ 2160 data->d_size = size; 2161 shdr->sh_size = (Xword)size; 2162 2163 /* 2164 * An ldynsym contains local function symbols. It is not 2165 * used for linking, but if present, serves to allow better 2166 * stack traces to be generated in contexts where the symtab 2167 * is not available. (dladdr(), or stripped executable/library files). 2168 */ 2169 if (allow_ldynsym) { 2170 cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt; 2171 size = (size_t)cnt * shdr->sh_entsize; 2172 2173 ldata->d_size = size; 2174 lshdr->sh_size = (Xword)size; 2175 } 2176 2177 return (1); 2178 } 2179 2180 /* 2181 * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are 2182 * index sections for the .SUNW_ldynsym/.dynsym pair that present data 2183 * and function symbols sorted by address. 2184 */ 2185 static uintptr_t 2186 make_dynsort(Ofl_desc *ofl) 2187 { 2188 Shdr *shdr; 2189 Elf_Data *data; 2190 Is_desc *isec; 2191 2192 /* Only do it if the .SUNW_ldynsym section is present */ 2193 if (!OFL_ALLOW_LDYNSYM(ofl)) 2194 return (1); 2195 2196 /* .SUNW_dynsymsort */ 2197 if (ofl->ofl_dynsymsortcnt > 0) { 2198 if (new_section(ofl, SHT_SUNW_symsort, 2199 MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt, 2200 &isec, &shdr, &data) == S_ERROR) 2201 return (S_ERROR); 2202 2203 if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, NULL, 2204 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR) 2205 return (S_ERROR); 2206 } 2207 2208 /* .SUNW_dyntlssort */ 2209 if (ofl->ofl_dyntlssortcnt > 0) { 2210 if (new_section(ofl, SHT_SUNW_tlssort, 2211 MSG_ORIG(MSG_SCN_DYNTLSSORT), 2212 ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR) 2213 return (S_ERROR); 2214 2215 if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec, NULL, 2216 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR) 2217 return (S_ERROR); 2218 } 2219 2220 return (1); 2221 } 2222 2223 /* 2224 * Helper routine for make_dynsym_shndx. Builds a 2225 * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing 2226 * which one it is. 2227 */ 2228 static uintptr_t 2229 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab, 2230 Os_desc **ret_os) 2231 { 2232 Is_desc *isec; 2233 Is_desc *dynsymisp; 2234 Shdr *shdr, *dynshdr; 2235 Elf_Data *data; 2236 2237 dynsymisp = ld_os_first_isdesc(symtab); 2238 dynshdr = dynsymisp->is_shdr; 2239 2240 if (new_section(ofl, SHT_SYMTAB_SHNDX, shname, 2241 (dynshdr->sh_size / dynshdr->sh_entsize), 2242 &isec, &shdr, &data) == S_ERROR) 2243 return (S_ERROR); 2244 2245 if ((*ret_os = ld_place_section(ofl, isec, NULL, 2246 ld_targ.t_id.id_dynsym_ndx, NULL)) == (Os_desc *)S_ERROR) 2247 return (S_ERROR); 2248 2249 assert(*ret_os); 2250 2251 return (1); 2252 } 2253 2254 /* 2255 * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym 2256 */ 2257 static uintptr_t 2258 make_dynsym_shndx(Ofl_desc *ofl) 2259 { 2260 /* 2261 * If there is a .SUNW_ldynsym, generate a section for its extended 2262 * index section as well. 2263 */ 2264 if (OFL_ALLOW_LDYNSYM(ofl)) { 2265 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX), 2266 ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR) 2267 return (S_ERROR); 2268 } 2269 2270 /* The Generate a section for the dynsym */ 2271 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX), 2272 ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR) 2273 return (S_ERROR); 2274 2275 return (1); 2276 } 2277 2278 2279 /* 2280 * Build a string table for the section headers. 2281 */ 2282 static uintptr_t 2283 make_shstrtab(Ofl_desc *ofl) 2284 { 2285 Shdr *shdr; 2286 Elf_Data *data; 2287 Is_desc *isec; 2288 size_t size; 2289 2290 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB), 2291 0, &isec, &shdr, &data) == S_ERROR) 2292 return (S_ERROR); 2293 2294 /* 2295 * Place the section first, as it may effect the number of section 2296 * headers to account for. 2297 */ 2298 ofl->ofl_osshstrtab = 2299 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_note, NULL); 2300 if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR) 2301 return (S_ERROR); 2302 2303 size = st_getstrtab_sz(ofl->ofl_shdrsttab); 2304 assert(size > 0); 2305 2306 data->d_size = size; 2307 shdr->sh_size = (Xword)size; 2308 2309 return (1); 2310 } 2311 2312 /* 2313 * Build a string section for the standard symbol table. 2314 */ 2315 static uintptr_t 2316 make_strtab(Ofl_desc *ofl) 2317 { 2318 Shdr *shdr; 2319 Elf_Data *data; 2320 Is_desc *isec; 2321 size_t size; 2322 2323 /* 2324 * This string table consists of all the global and local symbols. 2325 * Account for null bytes at end of the file name and the beginning 2326 * of section. 2327 */ 2328 if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1) 2329 return (S_ERROR); 2330 2331 size = st_getstrtab_sz(ofl->ofl_strtab); 2332 assert(size > 0); 2333 2334 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB), 2335 0, &isec, &shdr, &data) == S_ERROR) 2336 return (S_ERROR); 2337 2338 /* Set the size of the data area */ 2339 data->d_size = size; 2340 shdr->sh_size = (Xword)size; 2341 2342 ofl->ofl_osstrtab = 2343 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_strtab, NULL); 2344 return ((uintptr_t)ofl->ofl_osstrtab); 2345 } 2346 2347 /* 2348 * Build a string table for the dynamic symbol table. 2349 */ 2350 static uintptr_t 2351 make_dynstr(Ofl_desc *ofl) 2352 { 2353 Shdr *shdr; 2354 Elf_Data *data; 2355 Is_desc *isec; 2356 size_t size; 2357 2358 /* 2359 * If producing a .SUNW_ldynsym, account for the initial STT_FILE 2360 * symbol that precedes the scope reduced global symbols. 2361 */ 2362 if (OFL_ALLOW_LDYNSYM(ofl)) { 2363 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1) 2364 return (S_ERROR); 2365 ofl->ofl_dynscopecnt++; 2366 } 2367 2368 /* 2369 * Account for any local, named register symbols. These locals are 2370 * required for reference from DT_REGISTER .dynamic entries. 2371 */ 2372 if (ofl->ofl_regsyms) { 2373 int ndx; 2374 2375 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) { 2376 Sym_desc *sdp; 2377 2378 if ((sdp = ofl->ofl_regsyms[ndx]) == NULL) 2379 continue; 2380 2381 if (!SYM_IS_HIDDEN(sdp) && 2382 (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL)) 2383 continue; 2384 2385 if (sdp->sd_sym->st_name == 0) 2386 continue; 2387 2388 if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1) 2389 return (S_ERROR); 2390 } 2391 } 2392 2393 /* 2394 * Reserve entries for any per-symbol auxiliary/filter strings. 2395 */ 2396 if (ofl->ofl_dtsfltrs != NULL) { 2397 Dfltr_desc *dftp; 2398 Aliste idx; 2399 2400 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp)) 2401 if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1) 2402 return (S_ERROR); 2403 } 2404 2405 size = st_getstrtab_sz(ofl->ofl_dynstrtab); 2406 assert(size > 0); 2407 2408 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR), 2409 0, &isec, &shdr, &data) == S_ERROR) 2410 return (S_ERROR); 2411 2412 /* Make it allocable if necessary */ 2413 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 2414 shdr->sh_flags |= SHF_ALLOC; 2415 2416 /* Set the size of the data area */ 2417 data->d_size = size + DYNSTR_EXTRA_PAD; 2418 2419 shdr->sh_size = (Xword)size; 2420 2421 ofl->ofl_osdynstr = 2422 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynstr, NULL); 2423 return ((uintptr_t)ofl->ofl_osdynstr); 2424 } 2425 2426 /* 2427 * Generate an output relocation section which will contain the relocation 2428 * information to be applied to the `osp' section. 2429 * 2430 * If (osp == NULL) then we are creating the coalesced relocation section 2431 * for an executable and/or a shared object. 2432 */ 2433 static uintptr_t 2434 make_reloc(Ofl_desc *ofl, Os_desc *osp) 2435 { 2436 Shdr *shdr; 2437 Elf_Data *data; 2438 Is_desc *isec; 2439 size_t size; 2440 Xword sh_flags; 2441 char *sectname; 2442 Os_desc *rosp; 2443 Word relsize; 2444 const char *rel_prefix; 2445 2446 /* LINTED */ 2447 if (ld_targ.t_m.m_rel_sht_type == SHT_REL) { 2448 /* REL */ 2449 relsize = sizeof (Rel); 2450 rel_prefix = MSG_ORIG(MSG_SCN_REL); 2451 } else { 2452 /* RELA */ 2453 relsize = sizeof (Rela); 2454 rel_prefix = MSG_ORIG(MSG_SCN_RELA); 2455 } 2456 2457 if (osp) { 2458 size = osp->os_szoutrels; 2459 sh_flags = osp->os_shdr->sh_flags; 2460 if ((sectname = libld_malloc(strlen(rel_prefix) + 2461 strlen(osp->os_name) + 1)) == 0) 2462 return (S_ERROR); 2463 (void) strcpy(sectname, rel_prefix); 2464 (void) strcat(sectname, osp->os_name); 2465 } else if (ofl->ofl_flags & FLG_OF_COMREL) { 2466 size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize; 2467 sh_flags = SHF_ALLOC; 2468 sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC); 2469 } else { 2470 size = ofl->ofl_relocrelsz; 2471 sh_flags = SHF_ALLOC; 2472 sectname = (char *)rel_prefix; 2473 } 2474 2475 /* 2476 * Keep track of total size of 'output relocations' (to be stored 2477 * in .dynamic) 2478 */ 2479 /* LINTED */ 2480 ofl->ofl_relocsz += (Xword)size; 2481 2482 if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec, 2483 &shdr, &data) == S_ERROR) 2484 return (S_ERROR); 2485 2486 data->d_size = size; 2487 2488 shdr->sh_size = (Xword)size; 2489 if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC)) 2490 shdr->sh_flags = SHF_ALLOC; 2491 2492 if (osp) { 2493 /* 2494 * The sh_info field of the SHT_REL* sections points to the 2495 * section the relocations are to be applied to. 2496 */ 2497 shdr->sh_flags |= SHF_INFO_LINK; 2498 } 2499 2500 rosp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_rel, NULL); 2501 if (rosp == (Os_desc *)S_ERROR) 2502 return (S_ERROR); 2503 2504 /* 2505 * Associate this relocation section to the section its going to 2506 * relocate. 2507 */ 2508 if (osp) { 2509 Aliste idx; 2510 Is_desc *risp; 2511 2512 /* 2513 * This is used primarily so that we can update 2514 * SHT_GROUP[sect_no] entries to point to the 2515 * created output relocation sections. 2516 */ 2517 for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) { 2518 risp->is_osdesc = rosp; 2519 2520 /* 2521 * If the input relocation section had the SHF_GROUP 2522 * flag set - propagate it to the output relocation 2523 * section. 2524 */ 2525 if (risp->is_shdr->sh_flags & SHF_GROUP) { 2526 rosp->os_shdr->sh_flags |= SHF_GROUP; 2527 break; 2528 } 2529 } 2530 osp->os_relosdesc = rosp; 2531 } else 2532 ofl->ofl_osrel = rosp; 2533 2534 /* 2535 * If this is the first relocation section we've encountered save it 2536 * so that the .dynamic entry can be initialized accordingly. 2537 */ 2538 if (ofl->ofl_osrelhead == (Os_desc *)0) 2539 ofl->ofl_osrelhead = rosp; 2540 2541 return (1); 2542 } 2543 2544 /* 2545 * Generate version needed section. 2546 */ 2547 static uintptr_t 2548 make_verneed(Ofl_desc *ofl) 2549 { 2550 Shdr *shdr; 2551 Elf_Data *data; 2552 Is_desc *isec; 2553 2554 /* 2555 * verneed sections do not have a constant element size, so the 2556 * value of ent_cnt specified here (0) is meaningless. 2557 */ 2558 if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION), 2559 0, &isec, &shdr, &data) == S_ERROR) 2560 return (S_ERROR); 2561 2562 /* During version processing we calculated the total size. */ 2563 data->d_size = ofl->ofl_verneedsz; 2564 shdr->sh_size = (Xword)ofl->ofl_verneedsz; 2565 2566 ofl->ofl_osverneed = 2567 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL); 2568 return ((uintptr_t)ofl->ofl_osverneed); 2569 } 2570 2571 /* 2572 * Generate a version definition section. 2573 * 2574 * o the SHT_SUNW_verdef section defines the versions that exist within this 2575 * image. 2576 */ 2577 static uintptr_t 2578 make_verdef(Ofl_desc *ofl) 2579 { 2580 Shdr *shdr; 2581 Elf_Data *data; 2582 Is_desc *isec; 2583 Ver_desc *vdp; 2584 Str_tbl *strtab; 2585 2586 /* 2587 * Reserve a string table entry for the base version dependency (other 2588 * dependencies have symbol representations, which will already be 2589 * accounted for during symbol processing). 2590 */ 2591 vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0]; 2592 2593 if (OFL_IS_STATIC_OBJ(ofl)) 2594 strtab = ofl->ofl_strtab; 2595 else 2596 strtab = ofl->ofl_dynstrtab; 2597 2598 if (st_insert(strtab, vdp->vd_name) == -1) 2599 return (S_ERROR); 2600 2601 /* 2602 * verdef sections do not have a constant element size, so the 2603 * value of ent_cnt specified here (0) is meaningless. 2604 */ 2605 if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION), 2606 0, &isec, &shdr, &data) == S_ERROR) 2607 return (S_ERROR); 2608 2609 /* During version processing we calculated the total size. */ 2610 data->d_size = ofl->ofl_verdefsz; 2611 shdr->sh_size = (Xword)ofl->ofl_verdefsz; 2612 2613 ofl->ofl_osverdef = 2614 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL); 2615 return ((uintptr_t)ofl->ofl_osverdef); 2616 } 2617 2618 /* 2619 * This routine is called when -z nopartial is in effect. 2620 */ 2621 uintptr_t 2622 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align) 2623 { 2624 Shdr *shdr; 2625 Elf_Data *data; 2626 Is_desc *isec; 2627 Os_desc *osp; 2628 2629 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0, 2630 &isec, &shdr, &data) == S_ERROR) 2631 return (S_ERROR); 2632 2633 shdr->sh_flags |= SHF_WRITE; 2634 data->d_size = size; 2635 shdr->sh_size = (Xword)size; 2636 if (align != 0) { 2637 data->d_align = align; 2638 shdr->sh_addralign = align; 2639 } 2640 2641 if ((data->d_buf = libld_calloc(size, 1)) == NULL) 2642 return (S_ERROR); 2643 2644 /* 2645 * Retain handle to this .data input section. Variables using move 2646 * sections (partial initialization) will be redirected here when 2647 * such global references are added and '-z nopartial' is in effect. 2648 */ 2649 ofl->ofl_isparexpn = isec; 2650 osp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_data, NULL); 2651 if (osp == (Os_desc *)S_ERROR) 2652 return (S_ERROR); 2653 2654 if (!(osp->os_flags & FLG_OS_OUTREL)) { 2655 ofl->ofl_dynshdrcnt++; 2656 osp->os_flags |= FLG_OS_OUTREL; 2657 } 2658 return (1); 2659 } 2660 2661 /* 2662 * Make .sunwmove section 2663 */ 2664 uintptr_t 2665 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums) 2666 { 2667 Shdr *shdr; 2668 Elf_Data *data; 2669 Is_desc *isec; 2670 Aliste idx; 2671 Sym_desc *sdp; 2672 int cnt = 1; 2673 2674 2675 if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE), 2676 mv_nums, &isec, &shdr, &data) == S_ERROR) 2677 return (S_ERROR); 2678 2679 if ((data->d_buf = libld_calloc(data->d_size, 1)) == NULL) 2680 return (S_ERROR); 2681 2682 /* 2683 * Copy move entries 2684 */ 2685 for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx, sdp)) { 2686 Aliste idx2; 2687 Mv_desc *mdp; 2688 2689 if (sdp->sd_flags & FLG_SY_PAREXPN) 2690 continue; 2691 2692 for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) 2693 mdp->md_oidx = cnt++; 2694 } 2695 2696 if ((ofl->ofl_osmove = ld_place_section(ofl, isec, NULL, 0, NULL)) == 2697 (Os_desc *)S_ERROR) 2698 return (S_ERROR); 2699 2700 return (1); 2701 } 2702 2703 /* 2704 * Given a relocation descriptor that references a string table 2705 * input section, locate the string referenced and return a pointer 2706 * to it. 2707 */ 2708 static const char * 2709 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp) 2710 { 2711 Sym_desc *sdp = rsp->rel_sym; 2712 Xword str_off; 2713 2714 /* 2715 * In the case of an STT_SECTION symbol, the addend of the 2716 * relocation gives the offset into the string section. For 2717 * other symbol types, the symbol value is the offset. 2718 */ 2719 2720 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) { 2721 str_off = sdp->sd_sym->st_value; 2722 } else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) { 2723 /* 2724 * For SHT_RELA, the addend value is found in the 2725 * rel_raddend field of the relocation. 2726 */ 2727 str_off = rsp->rel_raddend; 2728 } else { /* REL and STT_SECTION */ 2729 /* 2730 * For SHT_REL, the "addend" is not part of the relocation 2731 * record. Instead, it is found at the relocation target 2732 * address. 2733 */ 2734 uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset + 2735 (uintptr_t)rsp->rel_isdesc->is_indata->d_buf); 2736 2737 if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0) 2738 return (0); 2739 } 2740 2741 return (str_off + (char *)sdp->sd_isc->is_indata->d_buf); 2742 } 2743 2744 /* 2745 * First pass over the relocation records for string table merging. 2746 * Build lists of relocations and symbols that will need modification, 2747 * and insert the strings they reference into the output string table. 2748 * 2749 * entry: 2750 * ofl - Output file descriptor 2751 * 2752 * exit: On success, the string merging specific members of each output 2753 * section descriptor in ofl are updated based on information from the 2754 * relocation entries, and 0 is returned. 2755 * 2756 * On error, S_ERROR is returned. 2757 */ 2758 static uintptr_t 2759 ld_gather_strmerge(Ofl_desc *ofl, Rel_cache *cache) 2760 { 2761 Rel_cachebuf *rbcp; 2762 Rel_desc *rsp; 2763 Sym_desc *last_sdp = NULL; 2764 Aliste idx1; 2765 2766 /* 2767 * Pass 1: 2768 * 2769 * Build lists of relocations and symbols that will need 2770 * modification, and insert the strings they reference into 2771 * the output string table. 2772 */ 2773 REL_CACHE_TRAVERSE(cache, idx1, rbcp, rsp) { 2774 Sym_desc *sdp = rsp->rel_sym; 2775 Os_desc *osp; 2776 const char *name; 2777 2778 /* 2779 * If there's no input section, or the input section is 2780 * discarded or does not contain mergable strings, we have 2781 * nothing to do. 2782 */ 2783 if ((sdp->sd_isc == NULL) || ((sdp->sd_isc->is_flags & 2784 (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) != FLG_IS_INSTRMRG)) 2785 continue; 2786 2787 osp = sdp->sd_isc->is_osdesc; 2788 2789 /* 2790 * Remember symbol for use in the third pass. There is no 2791 * reason to save a given symbol more than once, so we take 2792 * advantage of the fact that relocations to a given symbol 2793 * tend to cluster in the list. If this is the same symbol 2794 * we saved last time, don't bother. 2795 */ 2796 if (last_sdp != sdp) { 2797 if (aplist_append(&osp->os_mstrsyms, sdp, 2798 AL_CNT_STRMRGSYM) == NULL) 2799 return (S_ERROR); 2800 last_sdp = sdp; 2801 } 2802 2803 if ((osp->os_mstrtab == NULL) && 2804 (osp->os_mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL) 2805 return (S_ERROR); 2806 2807 /* Enter the string into our new string table */ 2808 name = strmerge_get_reloc_str(ofl, rsp); 2809 if (st_insert(osp->os_mstrtab, name) == -1) 2810 return (S_ERROR); 2811 2812 /* 2813 * If this is an STT_SECTION symbol, then the second pass 2814 * will need to modify this relocation, so hang on to it. 2815 */ 2816 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) && 2817 (aplist_append(&osp->os_mstrrels, rsp, 2818 AL_CNT_STRMRGREL) == NULL)) { 2819 return (S_ERROR); 2820 } 2821 } 2822 2823 return (0); 2824 } 2825 2826 /* 2827 * If any an output section has more than one SHF_MERGE|SHF_STRINGS input 2828 * section, replace them with a single merged/compressed input section. 2829 * 2830 * This is done by making a Str_tbl (as we use for managing SHT_STRTAB 2831 * sections) per output section with compression enabled to manage all strings 2832 * in the mergeable input sections. We then discard all inputs which 2833 * contributed to this table and replace them with an input section we create 2834 * taking data from this Str_tbl. References to the now discarded sections 2835 * are then updated to refer to our new merged input section, and the string 2836 * table and other metadata are freed. 2837 * 2838 * This process is done in 3 passes. For efficiency reasons half of pass 1 is 2839 * done by ld_strmerge_gather() so relocations only need to be processed once. 2840 * Steps 1.5 onward are performed here. The steps are: 2841 * 2842 * 1) In ld_strmerge_gather() examine all relocations, insert strings 2843 * from relocations to the mergeable input sections into the string 2844 * table. 2845 * 1.5) Gather every string from the mergeable input sections, regardless 2846 * of whether it is referenced from a relocation. All strings 2847 * must be processed, and relocations may point into the middle 2848 * of an actual NUL-terminated string, so we must enter both the 2849 * precise strings referenced by relocations and full strings 2850 * within the section. 2851 * 2) Modify the relocation values to be correct for the 2852 * new merged section. 2853 * 3) Modify the symbols used by the relocations to reference 2854 * the new section. 2855 * 2856 * These passes cannot be combined: 2857 * - The string table code works in two passes, and all 2858 * strings have to be loaded in pass one before the 2859 * offset of any strings can be determined. 2860 * - Multiple relocations reference a single symbol, so the 2861 * symbol cannot be modified until all relocations are 2862 * fixed. 2863 * 2864 * entry: 2865 * ofl - Output file descriptor 2866 * osp - Outputs section descriptor 2867 * 2868 * exit: 2869 * If section merging is possible for this output section, it is done. 2870 * If no errors are encountered, 0 is returned. On error, S_ERROR is 2871 * returned. 2872 * 2873 * The contents of the string-merging specific members of this output 2874 * section descriptor are undefined after this function returns. 2875 */ 2876 static uintptr_t 2877 ld_strmerge_sec(Ofl_desc *ofl, Os_desc *osp) 2878 { 2879 Is_desc *isp = NULL; 2880 Sym_desc *sdp; 2881 Rel_desc *rsp; 2882 Is_desc *mstrsec = NULL; /* Generated string merge section */ 2883 Shdr *mstr_shdr = NULL; 2884 Elf_Data *mstr_data = NULL; 2885 size_t data_size; 2886 Aliste idx; 2887 uintptr_t ret = 0; 2888 Boolean placed = FALSE; 2889 2890 /* 2891 * Pass 1.5: Add all strings from all mergeable input sections. 2892 * 2893 * The last section we find also serves as the template for our 2894 * replacement merged section, providing the section attributes, etc. 2895 */ 2896 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) { 2897 const char *str, *end; 2898 2899 if (isdesc_discarded(isp)) 2900 continue; 2901 2902 /* 2903 * Input sections of 0 size are dubiously valid since they do 2904 * not even contain the NUL string. Ignore them. 2905 */ 2906 if (isp->is_shdr->sh_size == 0) 2907 continue; 2908 2909 if ((osp->os_mstrtab == NULL) && 2910 (osp->os_mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL) { 2911 ret = S_ERROR; 2912 goto out; 2913 } 2914 2915 end = isp->is_indata->d_buf + isp->is_indata->d_size; 2916 for (str = isp->is_indata->d_buf; str < end; 2917 str += strlen(str) + 1) { 2918 if (st_insert(osp->os_mstrtab, str) != 0) { 2919 ret = S_ERROR; 2920 goto out; 2921 } 2922 } 2923 } 2924 2925 IMPLY(osp->os_mstrtab != NULL, isp != NULL); 2926 if (osp->os_mstrtab == NULL) { 2927 ret = 0; 2928 goto out; 2929 } 2930 2931 /* 2932 * Get the size of the new input section. Requesting the string 2933 * table size "cooks" the table, and finalizes its contents. 2934 */ 2935 data_size = st_getstrtab_sz(osp->os_mstrtab); 2936 2937 /* Create a new input section to hold the merged strings */ 2938 if (new_section_from_template(ofl, isp, data_size, 2939 &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR) { 2940 ret = S_ERROR; 2941 goto out; 2942 } 2943 mstrsec->is_flags |= FLG_IS_GNSTRMRG; 2944 2945 /* 2946 * Allocate a data buffer for the new input section, associate the 2947 * buffer with the string table descriptor, and fill it from the 2948 * string table. 2949 */ 2950 if ((mstr_data->d_buf = libld_malloc(data_size)) == NULL) { 2951 ret = S_ERROR; 2952 goto out; 2953 } 2954 if ((st_setstrbuf(osp->os_mstrtab, mstr_data->d_buf, 2955 data_size) == -1)) { 2956 ret = S_ERROR; 2957 goto out; 2958 } 2959 2960 st_setallstrings(osp->os_mstrtab); 2961 2962 /* Add the new section to the output image */ 2963 if (ld_place_section(ofl, mstrsec, NULL, osp->os_identndx, NULL) == 2964 (Os_desc *)S_ERROR) { 2965 ret = S_ERROR; 2966 goto out; 2967 } 2968 placed = TRUE; 2969 2970 /* 2971 * Pass 2: 2972 * 2973 * Revisit the relocation descriptors with STT_SECTION symbols 2974 * that were saved by the first pass. Update each relocation 2975 * record so that the offset it contains is for the new section 2976 * instead of the original. 2977 */ 2978 for (APLIST_TRAVERSE(osp->os_mstrrels, idx, rsp)) { 2979 const char *name; 2980 size_t stoff; 2981 2982 /* 2983 * Find the string to the merged table's buffer and get its 2984 * offset. 2985 */ 2986 name = strmerge_get_reloc_str(ofl, rsp); 2987 stoff = st_findstring(osp->os_mstrtab, name); 2988 VERIFY3S(stoff, !=, -1); 2989 2990 /* 2991 * Alter the relocation to access the string at the 2992 * new offset in our new string table. 2993 * 2994 * For SHT_RELA platforms, it suffices to simply 2995 * update the rel_raddend field of the relocation. 2996 * 2997 * For SHT_REL platforms, the new "addend" value 2998 * needs to be written at the address being relocated. 2999 * However, we can't alter the input sections which 3000 * are mapped readonly, and the output image has not 3001 * been created yet. So, we defer this operation, 3002 * using the rel_raddend field of the relocation 3003 * which is normally 0 on a REL platform, to pass the 3004 * new "addend" value to ld_perform_outreloc() or 3005 * ld_do_activerelocs(). The FLG_REL_NADDEND flag 3006 * tells them that this is the case. 3007 */ 3008 if ((rsp->rel_flags & FLG_REL_RELA) == 0) { 3009 /* REL */ 3010 rsp->rel_flags |= FLG_REL_NADDEND; 3011 } 3012 rsp->rel_raddend = (Sxword)stoff; 3013 3014 /* 3015 * Generate a symbol name string for STT_SECTION symbols 3016 * that might reference our merged section. This shows up 3017 * in debug output and helps show how the relocation has 3018 * changed from its original input section to our merged 3019 * one. 3020 */ 3021 if (ld_stt_section_sym_name(mstrsec) == NULL) { 3022 ret = S_ERROR; 3023 goto out; 3024 } 3025 } 3026 3027 /* 3028 * Pass 3: 3029 * 3030 * Modify the symbols referenced by the relocation descriptors 3031 * so that they reference the new input section containing the 3032 * merged strings instead of the original input sections. 3033 */ 3034 for (APLIST_TRAVERSE(osp->os_mstrsyms, idx, sdp)) { 3035 /* 3036 * If we've already redirected this symbol to the merged data, 3037 * don't do it again. ld_gather_strmerge() uses a heuristic 3038 * (relocations to the same symbol clump together) to avoid 3039 * inserting a given symbol more than once, but repeat symbols 3040 * in the list can occur. 3041 */ 3042 if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0) 3043 continue; 3044 3045 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) { 3046 /* 3047 * This is not an STT_SECTION symbol, so its 3048 * value is the offset of the string within the 3049 * input section. Update the address to reflect 3050 * the address in our new merged section. 3051 */ 3052 const char *name; 3053 size_t stoff; 3054 3055 /* 3056 * Find the string in the merged table's buffer and get 3057 * its offset. 3058 */ 3059 name = sdp->sd_sym->st_value + 3060 (char *)sdp->sd_isc->is_indata->d_buf; 3061 stoff = st_findstring(osp->os_mstrtab, name); 3062 VERIFY3S(stoff, !=, -1); 3063 3064 if (ld_sym_copy(sdp) == S_ERROR) { 3065 ret = S_ERROR; 3066 goto out; 3067 } 3068 sdp->sd_sym->st_value = (Word)stoff; 3069 } 3070 3071 /* Redirect the symbol to our new merged section */ 3072 sdp->sd_isc = mstrsec; 3073 } 3074 3075 /* 3076 * There are no references left to the original input string sections. 3077 * Mark them as discarded so they don't go into the output image. 3078 * At the same time, add up the sizes of the replaced sections. 3079 */ 3080 data_size = 0; 3081 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) { 3082 if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG)) 3083 continue; 3084 3085 data_size += isp->is_indata->d_size; 3086 3087 isp->is_flags |= FLG_IS_DISCARD; 3088 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec)); 3089 } 3090 3091 /* Report how much space we saved in the output section */ 3092 DBG_CALL(Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size, 3093 mstr_data->d_size)); 3094 3095 out: 3096 if ((ret == S_ERROR) && !placed) { 3097 libld_free(mstrsec); 3098 if (mstr_data != NULL) 3099 libld_free(mstr_data->d_buf); 3100 libld_free(mstr_data); 3101 libld_free(mstr_shdr); 3102 } 3103 3104 libld_free(osp->os_mstrsyms); 3105 osp->os_mstrsyms = NULL; 3106 libld_free(osp->os_mstrrels); 3107 osp->os_mstrrels = NULL; 3108 3109 if (osp->os_mstrtab != NULL) { 3110 st_destroy(osp->os_mstrtab); 3111 osp->os_mstrtab = NULL; 3112 } 3113 3114 return (ret); 3115 } 3116 3117 /* 3118 * If any output section has SHF_MERGE|SHF_STRINGS input sections, 3119 * replace them with a single merged/compressed input section. 3120 * 3121 * entry: 3122 * ofl - Output file descriptor 3123 * 3124 * exit: 3125 * If section merging is possible, it is done. If no errors are 3126 * encountered, 0 is returned. On error, S_ERROR is returned. 3127 * 3128 * The contents of the string-merging specific members of any output 3129 * section descriptor are undefined after this function returns. 3130 */ 3131 static uintptr_t 3132 ld_make_strmerge(Ofl_desc *ofl) 3133 { 3134 Sg_desc *sgp; 3135 Aliste idx1; 3136 3137 if (ld_gather_strmerge(ofl, &ofl->ofl_actrels) == S_ERROR) 3138 return (S_ERROR); 3139 if (ld_gather_strmerge(ofl, &ofl->ofl_outrels) == S_ERROR) 3140 return (S_ERROR); 3141 3142 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) { 3143 Os_desc *osp; 3144 Aliste idx2; 3145 3146 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) { 3147 if (ld_strmerge_sec(ofl, osp) == S_ERROR) 3148 return (S_ERROR); 3149 } 3150 } 3151 3152 return (0); 3153 } 3154 3155 /* 3156 * Update a data buffers size. A number of sections have to be created, and 3157 * the sections header contributes to the size of the eventual section. Thus, 3158 * a section may be created, and once all associated sections have been created, 3159 * we return to establish the required section size. 3160 */ 3161 inline static void 3162 update_data_size(Os_desc *osp, ulong_t cnt) 3163 { 3164 Is_desc *isec = ld_os_first_isdesc(osp); 3165 Elf_Data *data = isec->is_indata; 3166 Shdr *shdr = osp->os_shdr; 3167 size_t size = cnt * shdr->sh_entsize; 3168 3169 shdr->sh_size = (Xword)size; 3170 data->d_size = size; 3171 } 3172 3173 /* 3174 * The following sections are built after all input file processing and symbol 3175 * validation has been carried out. The order is important (because the 3176 * addition of a section adds a new symbol there is a chicken and egg problem 3177 * of maintaining the appropriate counts). By maintaining a known order the 3178 * individual routines can compensate for later, known, additions. 3179 */ 3180 uintptr_t 3181 ld_make_sections(Ofl_desc *ofl) 3182 { 3183 ofl_flag_t flags = ofl->ofl_flags; 3184 Sg_desc *sgp; 3185 3186 /* 3187 * Generate any special sections. 3188 */ 3189 if (flags & FLG_OF_ADDVERS) 3190 if (make_comment(ofl) == S_ERROR) 3191 return (S_ERROR); 3192 3193 if (make_interp(ofl) == S_ERROR) 3194 return (S_ERROR); 3195 3196 /* 3197 * Create a capabilities section if required. 3198 */ 3199 if (make_cap(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), 3200 ld_targ.t_id.id_cap) == S_ERROR) 3201 return (S_ERROR); 3202 3203 /* 3204 * Create any init/fini array sections. 3205 */ 3206 if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY), 3207 ofl->ofl_initarray) == S_ERROR) 3208 return (S_ERROR); 3209 3210 if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY), 3211 ofl->ofl_finiarray) == S_ERROR) 3212 return (S_ERROR); 3213 3214 if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY), 3215 ofl->ofl_preiarray) == S_ERROR) 3216 return (S_ERROR); 3217 3218 /* 3219 * Make the .plt section. This occurs after any other relocation 3220 * sections are generated (see reloc_init()) to ensure that the 3221 * associated relocation section is after all the other relocation 3222 * sections. 3223 */ 3224 if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad)) 3225 if (make_plt(ofl) == S_ERROR) 3226 return (S_ERROR); 3227 3228 /* 3229 * Determine whether any sections or files are not referenced. Under 3230 * -Dunused a diagnostic for any unused components is generated, under 3231 * -zignore the component is removed from the final output. 3232 */ 3233 if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) { 3234 if (ignore_section_processing(ofl) == S_ERROR) 3235 return (S_ERROR); 3236 } 3237 3238 /* 3239 * If we have detected a situation in which previously placed 3240 * output sections may have been discarded, perform the necessary 3241 * readjustment. 3242 */ 3243 if (ofl->ofl_flags & FLG_OF_ADJOSCNT) 3244 adjust_os_count(ofl); 3245 3246 /* 3247 * Do any of the output sections contain input sections that 3248 * are candidates for string table merging? For each such case, 3249 * we create a replacement section, insert it, and discard the 3250 * originals. 3251 * 3252 * rel_alpp and sym_alpp are used by ld_make_strmerge() 3253 * for its internal processing. We are responsible for the 3254 * initialization and cleanup, and ld_make_strmerge() handles the rest. 3255 * This allows us to reuse a single pair of memory buffers, allocated 3256 * for this processing, for all the output sections. 3257 */ 3258 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) { 3259 if (ld_make_strmerge(ofl) == S_ERROR) 3260 return (S_ERROR); 3261 } 3262 3263 /* 3264 * Add any necessary versioning information. 3265 */ 3266 if (!(flags & FLG_OF_NOVERSEC)) { 3267 if ((flags & FLG_OF_VERNEED) && 3268 (make_verneed(ofl) == S_ERROR)) 3269 return (S_ERROR); 3270 if ((flags & FLG_OF_VERDEF) && 3271 (make_verdef(ofl) == S_ERROR)) 3272 return (S_ERROR); 3273 if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) && 3274 ((ofl->ofl_osversym = make_sym_sec(ofl, 3275 MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym, 3276 ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR)) 3277 return (S_ERROR); 3278 } 3279 3280 /* 3281 * Create a syminfo section if necessary. 3282 */ 3283 if (flags & FLG_OF_SYMINFO) { 3284 if ((ofl->ofl_ossyminfo = make_sym_sec(ofl, 3285 MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo, 3286 ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR) 3287 return (S_ERROR); 3288 } 3289 3290 if (flags & FLG_OF_COMREL) { 3291 /* 3292 * If -zcombreloc is enabled then all relocations (except for 3293 * the PLT's) are coalesced into a single relocation section. 3294 */ 3295 if (ofl->ofl_reloccnt) { 3296 if (make_reloc(ofl, NULL) == S_ERROR) 3297 return (S_ERROR); 3298 } 3299 } else { 3300 Aliste idx1; 3301 3302 /* 3303 * Create the required output relocation sections. Note, new 3304 * sections may be added to the section list that is being 3305 * traversed. These insertions can move the elements of the 3306 * Alist such that a section descriptor is re-read. Recursion 3307 * is prevented by maintaining a previous section pointer and 3308 * insuring that this pointer isn't re-examined. 3309 */ 3310 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) { 3311 Os_desc *osp, *posp = 0; 3312 Aliste idx2; 3313 3314 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) { 3315 if ((osp != posp) && osp->os_szoutrels && 3316 (osp != ofl->ofl_osplt)) { 3317 if (make_reloc(ofl, osp) == S_ERROR) 3318 return (S_ERROR); 3319 } 3320 posp = osp; 3321 } 3322 } 3323 3324 /* 3325 * If we're not building a combined relocation section, then 3326 * build a .rel[a] section as required. 3327 */ 3328 if (ofl->ofl_relocrelsz) { 3329 if (make_reloc(ofl, NULL) == S_ERROR) 3330 return (S_ERROR); 3331 } 3332 } 3333 3334 /* 3335 * The PLT relocations are always in their own section, and we try to 3336 * keep them at the end of the PLT table. We do this to keep the hot 3337 * "data" PLT's at the head of the table nearer the .dynsym & .hash. 3338 */ 3339 if (ofl->ofl_osplt && ofl->ofl_relocpltsz) { 3340 if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR) 3341 return (S_ERROR); 3342 } 3343 3344 /* 3345 * Finally build the symbol and section header sections. 3346 */ 3347 if (flags & FLG_OF_DYNAMIC) { 3348 if (make_dynamic(ofl) == S_ERROR) 3349 return (S_ERROR); 3350 3351 /* 3352 * A number of sections aren't necessary within a relocatable 3353 * object, even if -dy has been used. 3354 */ 3355 if (!(flags & FLG_OF_RELOBJ)) { 3356 if (make_hash(ofl) == S_ERROR) 3357 return (S_ERROR); 3358 if (make_dynstr(ofl) == S_ERROR) 3359 return (S_ERROR); 3360 if (make_dynsym(ofl) == S_ERROR) 3361 return (S_ERROR); 3362 if (ld_unwind_make_hdr(ofl) == S_ERROR) 3363 return (S_ERROR); 3364 if (make_dynsort(ofl) == S_ERROR) 3365 return (S_ERROR); 3366 } 3367 } 3368 3369 if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) || 3370 ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) { 3371 /* 3372 * Do we need to make a SHT_SYMTAB_SHNDX section 3373 * for the dynsym. If so - do it now. 3374 */ 3375 if (ofl->ofl_osdynsym && 3376 ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) { 3377 if (make_dynsym_shndx(ofl) == S_ERROR) 3378 return (S_ERROR); 3379 } 3380 3381 if (make_strtab(ofl) == S_ERROR) 3382 return (S_ERROR); 3383 if (make_symtab(ofl) == S_ERROR) 3384 return (S_ERROR); 3385 } else { 3386 /* 3387 * Do we need to make a SHT_SYMTAB_SHNDX section 3388 * for the dynsym. If so - do it now. 3389 */ 3390 if (ofl->ofl_osdynsym && 3391 ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) { 3392 if (make_dynsym_shndx(ofl) == S_ERROR) 3393 return (S_ERROR); 3394 } 3395 } 3396 3397 if (make_shstrtab(ofl) == S_ERROR) 3398 return (S_ERROR); 3399 3400 /* 3401 * Now that we've created all output sections, adjust the size of the 3402 * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on 3403 * the associated symbol table sizes. 3404 */ 3405 if (ofl->ofl_osversym || ofl->ofl_ossyminfo) { 3406 ulong_t cnt; 3407 Is_desc *isp; 3408 Os_desc *osp; 3409 3410 if (OFL_IS_STATIC_OBJ(ofl)) 3411 osp = ofl->ofl_ossymtab; 3412 else 3413 osp = ofl->ofl_osdynsym; 3414 3415 isp = ld_os_first_isdesc(osp); 3416 cnt = (isp->is_shdr->sh_size / isp->is_shdr->sh_entsize); 3417 3418 if (ofl->ofl_osversym) 3419 update_data_size(ofl->ofl_osversym, cnt); 3420 3421 if (ofl->ofl_ossyminfo) 3422 update_data_size(ofl->ofl_ossyminfo, cnt); 3423 } 3424 3425 /* 3426 * Now that we've created all output sections, adjust the size of the 3427 * SHT_SUNW_capinfo, which is dependent on the associated symbol table 3428 * size. 3429 */ 3430 if (ofl->ofl_oscapinfo) { 3431 ulong_t cnt; 3432 3433 /* 3434 * Symbol capabilities symbols are placed directly after the 3435 * STT_FILE symbol, section symbols, and any register symbols. 3436 * Effectively these are the first of any series of demoted 3437 * (scoped) symbols. 3438 */ 3439 if (OFL_IS_STATIC_OBJ(ofl)) 3440 cnt = SYMTAB_ALL_CNT(ofl); 3441 else 3442 cnt = DYNSYM_ALL_CNT(ofl); 3443 3444 update_data_size(ofl->ofl_oscapinfo, cnt); 3445 } 3446 return (1); 3447 } 3448 3449 /* 3450 * Build an additional data section - used to back OBJT symbol definitions 3451 * added with a mapfile. 3452 */ 3453 Is_desc * 3454 ld_make_data(Ofl_desc *ofl, size_t size) 3455 { 3456 Shdr *shdr; 3457 Elf_Data *data; 3458 Is_desc *isec; 3459 3460 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0, 3461 &isec, &shdr, &data) == S_ERROR) 3462 return ((Is_desc *)S_ERROR); 3463 3464 data->d_size = size; 3465 shdr->sh_size = (Xword)size; 3466 shdr->sh_flags |= SHF_WRITE; 3467 3468 if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL) 3469 return ((Is_desc *)S_ERROR); 3470 3471 return (isec); 3472 } 3473 3474 /* 3475 * Build an additional text section - used to back FUNC symbol definitions 3476 * added with a mapfile. 3477 */ 3478 Is_desc * 3479 ld_make_text(Ofl_desc *ofl, size_t size) 3480 { 3481 Shdr *shdr; 3482 Elf_Data *data; 3483 Is_desc *isec; 3484 3485 /* 3486 * Insure the size is sufficient to contain the minimum return 3487 * instruction. 3488 */ 3489 if (size < ld_targ.t_nf.nf_size) 3490 size = ld_targ.t_nf.nf_size; 3491 3492 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0, 3493 &isec, &shdr, &data) == S_ERROR) 3494 return ((Is_desc *)S_ERROR); 3495 3496 data->d_size = size; 3497 shdr->sh_size = (Xword)size; 3498 shdr->sh_flags |= SHF_EXECINSTR; 3499 3500 /* 3501 * Fill the buffer with the appropriate return instruction. 3502 * Note that there is no need to swap bytes on a non-native, 3503 * link, as the data being copied is given in bytes. 3504 */ 3505 if ((data->d_buf = libld_calloc(size, 1)) == NULL) 3506 return ((Is_desc *)S_ERROR); 3507 (void) memcpy(data->d_buf, ld_targ.t_nf.nf_template, 3508 ld_targ.t_nf.nf_size); 3509 3510 /* 3511 * If size was larger than required, and the target supplies 3512 * a fill function, use it to fill the balance. If there is no 3513 * fill function, we accept the 0-fill supplied by libld_calloc(). 3514 */ 3515 if ((ld_targ.t_ff.ff_execfill != NULL) && (size > ld_targ.t_nf.nf_size)) 3516 ld_targ.t_ff.ff_execfill(data->d_buf, ld_targ.t_nf.nf_size, 3517 size - ld_targ.t_nf.nf_size); 3518 3519 if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL) 3520 return ((Is_desc *)S_ERROR); 3521 3522 return (isec); 3523 } 3524 3525 void 3526 ld_comdat_validate(Ofl_desc *ofl, Ifl_desc *ifl) 3527 { 3528 int i; 3529 3530 for (i = 0; i < ifl->ifl_shnum; i++) { 3531 Is_desc *isp = ifl->ifl_isdesc[i]; 3532 int types = 0; 3533 char buf[1024] = ""; 3534 Group_desc *gr = NULL; 3535 3536 if ((isp == NULL) || (isp->is_flags & FLG_IS_COMDAT) == 0) 3537 continue; 3538 3539 if (isp->is_shdr->sh_type == SHT_SUNW_COMDAT) { 3540 types++; 3541 (void) strlcpy(buf, MSG_ORIG(MSG_STR_SUNW_COMDAT), 3542 sizeof (buf)); 3543 } 3544 3545 if (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name, 3546 MSG_SCN_GNU_LINKONCE_SIZE) == 0) { 3547 types++; 3548 if (types > 1) 3549 (void) strlcat(buf, ", ", sizeof (buf)); 3550 (void) strlcat(buf, MSG_ORIG(MSG_SCN_GNU_LINKONCE), 3551 sizeof (buf)); 3552 } 3553 3554 if ((isp->is_shdr->sh_flags & SHF_GROUP) && 3555 ((gr = ld_get_group(ofl, isp)) != NULL) && 3556 (gr->gd_data[0] & GRP_COMDAT)) { 3557 types++; 3558 if (types > 1) 3559 (void) strlcat(buf, ", ", sizeof (buf)); 3560 (void) strlcat(buf, MSG_ORIG(MSG_STR_GROUP), 3561 sizeof (buf)); 3562 } 3563 3564 if (types > 1) 3565 ld_eprintf(ofl, ERR_FATAL, 3566 MSG_INTL(MSG_SCN_MULTICOMDAT), ifl->ifl_name, 3567 EC_WORD(isp->is_scnndx), isp->is_name, buf); 3568 } 3569 } 3570