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 2007 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * Module sections. Initialize special sections 33 */ 34 #include <string.h> 35 #include <strings.h> 36 #include <stdio.h> 37 #include <link.h> 38 #include <debug.h> 39 #include "msg.h" 40 #include "_libld.h" 41 42 43 /* 44 * If -zignore is in effect, scan all input sections to see if there are any 45 * which haven't been referenced (and hence can be discarded). If sections are 46 * to be discarded, rescan the output relocations and the symbol table and 47 * remove the relocations and symbol entries that are no longer required. 48 * 49 * Note: It's possible that a section which is being discarded has contributed 50 * to the GOT table or the PLT table. However, we can't at this point 51 * eliminate the corresponding entries. This is because there could well 52 * be other sections referencing those same entries, but we don't have 53 * the infrastructure to determine this. So, keep the PLT and GOT 54 * entries in the table in case someone wants them. 55 * Note: The section to be affected needs to be allocatable. 56 * So even if -zignore is in effect, if the section is not allocatable, 57 * we do not eliminate it. 58 */ 59 static uintptr_t 60 ignore_section_processing(Ofl_desc *ofl) 61 { 62 Listnode *lnp; 63 Ifl_desc *ifl; 64 Rel_cache *rcp; 65 int allow_ldynsym; 66 67 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 68 69 for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, ifl)) { 70 uint_t num, discard; 71 72 /* 73 * Diagnose (-D unused) a completely unreferenced file. 74 */ 75 if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0) 76 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, 77 ifl->ifl_name, 0, 0)); 78 if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) || 79 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0)) 80 continue; 81 82 /* 83 * Before scanning the whole symbol table to determine if 84 * symbols should be discard - quickly (relatively) scan the 85 * sections to determine if any are to be discarded. 86 */ 87 discard = 0; 88 if (ifl->ifl_flags & FLG_IF_FILEREF) { 89 for (num = 1; num < ifl->ifl_shnum; num++) { 90 Is_desc *isp = ifl->ifl_isdesc[num]; 91 Os_desc *osp; 92 Sg_desc *sgp; 93 94 if (((isp = ifl->ifl_isdesc[num]) != 0) && 95 ((isp->is_flags & FLG_IS_SECTREF) == 0) && 96 ((osp = isp->is_osdesc) != 0) && 97 ((sgp = osp->os_sgdesc) != 0) && 98 (sgp->sg_phdr.p_type == PT_LOAD)) { 99 discard++; 100 break; 101 } 102 } 103 } 104 105 /* 106 * No sections are to be 'ignored' 107 */ 108 if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF)) 109 continue; 110 111 /* 112 * We know that we have discarded sections. Scan the symbol 113 * table for this file to determine if symbols need to be 114 * discarded that are associated with the 'ignored' sections. 115 */ 116 for (num = 1; num < ifl->ifl_symscnt; num++) { 117 Sym_desc *sdp; 118 Sym *symp; 119 Os_desc *osp; 120 /* LINTED - only used for assert() */ 121 int err; 122 uchar_t type; 123 124 sdp = ifl->ifl_oldndx[num]; 125 symp = sdp->sd_sym; 126 type = ELF_ST_TYPE(symp->st_info); 127 128 /* 129 * If the whole file is being eliminated, remove the 130 * local file symbol, and any COMMON symbols (which 131 * aren't associated with a section) provided they 132 * haven't been referenced by a relocation. 133 */ 134 if ((ofl->ofl_flags1 & FLG_OF1_IGNORE) && 135 ((ifl->ifl_flags & FLG_IF_FILEREF) == 0) && 136 ((type == STT_FILE) || 137 ((symp->st_shndx == SHN_COMMON) && 138 ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) { 139 if ((ofl->ofl_flags1 & FLG_OF1_REDLSYM) == 0) { 140 ofl->ofl_locscnt--; 141 err = st_delstring(ofl->ofl_strtab, 142 sdp->sd_name); 143 assert(err != -1); 144 if (allow_ldynsym && 145 ldynsym_symtype[type]) { 146 ofl->ofl_dynlocscnt--; 147 err = st_delstring( 148 ofl->ofl_dynstrtab, 149 sdp->sd_name); 150 assert(err != -1); 151 /* Remove from sort section? */ 152 DYNSORT_COUNT(sdp, symp, 153 type, --); 154 } 155 } 156 sdp->sd_flags |= FLG_SY_ISDISC; 157 continue; 158 } 159 160 /* 161 * Skip any undefined, reserved section symbols, already 162 * discarded or eliminated symbols. Also skip any 163 * symbols that don't originate from a section, or 164 * aren't defined from the file being examined. 165 */ 166 if ((symp->st_shndx == SHN_UNDEF) || 167 (symp->st_shndx >= SHN_LORESERVE) || 168 (type == STT_SECTION) || 169 (sdp->sd_flags & FLG_SY_ISDISC) || 170 (sdp->sd_flags1 & FLG_SY1_ELIM) || 171 (sdp->sd_isc == 0) || (sdp->sd_file != ifl)) 172 continue; 173 174 /* 175 * If any references were made against the section 176 * the symbol is being defined in - skip it. 177 */ 178 if ((sdp->sd_isc->is_flags & FLG_IS_SECTREF) || 179 ((ifl->ifl_flags & FLG_IF_FILEREF) && 180 ((osp = sdp->sd_isc->is_osdesc) != 0) && 181 (osp->os_sgdesc->sg_phdr.p_type != PT_LOAD))) 182 continue; 183 184 /* 185 * Finish processing any local symbols. 186 */ 187 if (ELF_ST_BIND(symp->st_info) == STB_LOCAL) { 188 if (ofl->ofl_flags1 & FLG_OF1_IGNORE) { 189 if ((ofl->ofl_flags1 & 190 FLG_OF1_REDLSYM) == 0) { 191 ofl->ofl_locscnt--; 192 err = st_delstring(ofl->ofl_strtab, 193 sdp->sd_name); 194 assert(err != -1); 195 if (allow_ldynsym && 196 ldynsym_symtype[type]) { 197 ofl->ofl_dynlocscnt--; 198 err = st_delstring( 199 ofl->ofl_dynstrtab, 200 sdp->sd_name); 201 assert(err != -1); 202 /* Remove from sort section? */ 203 DYNSORT_COUNT(sdp, symp, 204 type, --); 205 } 206 } 207 sdp->sd_flags |= FLG_SY_ISDISC; 208 } 209 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, 210 sdp, sdp->sd_isc)); 211 continue; 212 } 213 214 /* 215 * Global symbols can only be eliminated when an objects 216 * interfaces (versioning/scoping) is defined. 217 */ 218 if (sdp->sd_flags1 & FLG_SY1_LOCL) { 219 if (ofl->ofl_flags1 & FLG_OF1_IGNORE) { 220 ofl->ofl_scopecnt--; 221 ofl->ofl_elimcnt++; 222 223 err = st_delstring(ofl->ofl_strtab, 224 sdp->sd_name); 225 assert(err != -1); 226 227 sdp->sd_flags1 |= FLG_SY1_ELIM; 228 if (allow_ldynsym && 229 ldynsym_symtype[type]) { 230 ofl->ofl_dynscopecnt--; 231 err = st_delstring( 232 ofl->ofl_dynstrtab, 233 sdp->sd_name); 234 assert(err != -1); 235 /* Remove from sort section? */ 236 DYNSORT_COUNT(sdp, symp, type, --); 237 } 238 } 239 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, 240 sdp, sdp->sd_isc)); 241 continue; 242 } 243 } 244 } 245 246 if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) 247 return (1); 248 249 /* 250 * Scan all output relocations searching for those against discarded or 251 * ignored sections. If one is found, decrement the total outrel count. 252 */ 253 for (LIST_TRAVERSE(&ofl->ofl_outrels, lnp, rcp)) { 254 Rel_desc *orsp; 255 Os_desc *relosp; 256 257 /* LINTED */ 258 for (orsp = (Rel_desc *)(rcp + 1); 259 orsp < rcp->rc_free; orsp++) { 260 Is_desc *_isdesc = orsp->rel_isdesc; 261 uint_t flags, entsize; 262 Shdr *shdr; 263 Ifl_desc *ifl; 264 265 if ((_isdesc == 0) || 266 ((_isdesc->is_flags & (FLG_IS_SECTREF))) || 267 ((ifl = _isdesc->is_file) == 0) || 268 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) || 269 ((shdr = _isdesc->is_shdr) == 0) || 270 ((shdr->sh_flags & SHF_ALLOC) == 0)) 271 continue; 272 273 flags = orsp->rel_flags; 274 275 if (flags & (FLG_REL_GOT | FLG_REL_BSS | 276 FLG_REL_NOINFO | FLG_REL_PLT)) 277 continue; 278 279 relosp = orsp->rel_osdesc; 280 281 if (orsp->rel_flags & FLG_REL_RELA) 282 entsize = sizeof (Rela); 283 else 284 entsize = sizeof (Rel); 285 286 assert(relosp->os_szoutrels > 0); 287 relosp->os_szoutrels -= entsize; 288 289 if (!(flags & FLG_REL_PLT)) 290 ofl->ofl_reloccntsub++; 291 292 if (orsp->rel_rtype == M_R_RELATIVE) 293 ofl->ofl_relocrelcnt--; 294 } 295 } 296 return (1); 297 } 298 299 /* 300 * Allocate Elf_Data, Shdr, and Is_desc structures for a new 301 * section. 302 * 303 * entry: 304 * ofl - Output file descriptor 305 * shtype - SHT_ type code for section. 306 * shname - String giving the name for the new section. 307 * entcnt - # of items contained in the data part of the new section. 308 * This value is multiplied against the known element size 309 * for the section type to determine the size of the data 310 * area for the section. It is only meaningful in cases where 311 * the section type has a non-zero element size. In other cases, 312 * the caller must set the size fields in the *ret_data and 313 * *ret_shdr structs manually. 314 * ret_isec, ret_shdr, ret_data - Address of pointers to 315 * receive address of newly allocated structs. 316 * 317 * exit: 318 * On error, returns S_ERROR. On success, returns (1), and the 319 * ret_ pointers have been updated to point at the new structures, 320 * which has been filled in. To finish the task, the caller must 321 * update any fields within the supplied descriptors that differ 322 * from its needs, and then call ld_place_section(). 323 */ 324 static uintptr_t 325 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt, 326 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data) 327 { 328 typedef struct sec_info { 329 Word d_type; 330 Word align; /* Used in both data and section header */ 331 Word sh_flags; 332 Word sh_entsize; 333 } SEC_INFO_T; 334 335 const SEC_INFO_T *sec_info; 336 337 Shdr *shdr; 338 Elf_Data *data; 339 Is_desc *isec; 340 size_t size; 341 342 /* 343 * For each type of section, we have a distinct set of 344 * SEC_INFO_T values. This macro defines a static structure 345 * containing those values and generates code to set the sec_info 346 * pointer to refer to it. The pointer in sec_info remains valid 347 * outside of the declaration scope because the info_s struct is static. 348 */ 349 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \ 350 { \ 351 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \ 352 sh_entsize}; \ 353 sec_info = &info_s; \ 354 } 355 356 switch (shtype) { 357 case SHT_PROGBITS: 358 /* 359 * SHT_PROGBITS sections contain are used for many 360 * different sections. Alignments and flags differ. 361 * Some have a standard entsize, and others don't. 362 * We set some defaults here, but there is no expectation 363 * that they are correct or complete for any specific 364 * purpose. The caller must provide the correct values. 365 */ 366 SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN, SHF_ALLOC, 0) 367 break; 368 369 case SHT_SYMTAB: 370 SET_SEC_INFO(ELF_T_SYM, M_WORD_ALIGN, 0, sizeof (Sym)) 371 break; 372 373 case SHT_DYNSYM: 374 case SHT_SUNW_LDYNSYM: 375 SET_SEC_INFO(ELF_T_SYM, M_WORD_ALIGN, SHF_ALLOC, sizeof (Sym)) 376 break; 377 378 case SHT_STRTAB: 379 /* 380 * A string table may or may not be allocable, depending 381 * on context, so we leave that flag unset and leave it to 382 * the caller to add it if necessary. 383 * 384 * String tables do not have a standard entsize, so 385 * we set it to 0. 386 */ 387 SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0) 388 break; 389 390 case SHT_RELA: 391 /* 392 * Relocations with an addend (Everything except 32-bit X86). 393 * The caller is expected to set all section header flags. 394 */ 395 SET_SEC_INFO(ELF_T_RELA, M_WORD_ALIGN, 0, sizeof (Rela)) 396 break; 397 398 case SHT_REL: 399 /* 400 * Relocations without an addend (32-bit X86 only). 401 * The caller is expected to set all section header flags. 402 */ 403 SET_SEC_INFO(ELF_T_REL, M_WORD_ALIGN, 0, sizeof (Rel)) 404 break; 405 406 case SHT_HASH: 407 case SHT_SUNW_symsort: 408 case SHT_SUNW_tlssort: 409 SET_SEC_INFO(ELF_T_WORD, M_WORD_ALIGN, SHF_ALLOC, sizeof (Word)) 410 break; 411 412 case SHT_DYNAMIC: 413 /* 414 * A dynamic section may or may not be allocable, depending 415 * on context, so we leave that flag unset and leave it to 416 * the caller to add it if necessary. 417 */ 418 SET_SEC_INFO(ELF_T_DYN, M_WORD_ALIGN, SHF_WRITE, sizeof (Dyn)) 419 break; 420 421 case SHT_NOBITS: 422 /* 423 * SHT_NOBITS is used for BSS-type sections. The size and 424 * alignment depend on the specific use and must be adjusted 425 * by the caller. 426 */ 427 SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0) 428 break; 429 430 case SHT_INIT_ARRAY: 431 case SHT_FINI_ARRAY: 432 case SHT_PREINIT_ARRAY: 433 SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE, 434 sizeof (Addr)) 435 break; 436 437 case SHT_SYMTAB_SHNDX: 438 /* 439 * Note that these sections are created to be associated 440 * with both symtab and dynsym symbol tables. However, they 441 * are non-allocable in all cases, because the runtime 442 * linker has no need for this information. It is purely 443 * informational, used by elfdump(1), debuggers, etc. 444 */ 445 SET_SEC_INFO(ELF_T_WORD, M_WORD_ALIGN, 0, sizeof (Word)); 446 break; 447 448 case SHT_SUNW_cap: 449 SET_SEC_INFO(ELF_T_CAP, M_WORD_ALIGN, SHF_ALLOC, sizeof (Cap)); 450 break; 451 452 case SHT_SUNW_move: 453 /* 454 * The sh_info field of the SHT_*_syminfo section points 455 * to the header index of the associated .dynamic section, 456 * so we also set SHF_INFO_LINK. 457 */ 458 SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword), 459 SHF_ALLOC | SHF_WRITE, sizeof (Move)); 460 break; 461 462 case SHT_SUNW_syminfo: 463 /* 464 * The sh_info field of the SHT_*_syminfo section points 465 * to the header index of the associated .dynamic section, 466 * so we also set SHF_INFO_LINK. 467 */ 468 SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN, 469 SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo)); 470 break; 471 472 case SHT_SUNW_verneed: 473 case SHT_SUNW_verdef: 474 /* 475 * The info for verneed and versym happen to be the same. 476 * The entries in these sections are not of uniform size, 477 * so we set the entsize to 0. 478 */ 479 SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN, SHF_ALLOC, 0); 480 break; 481 482 case SHT_SUNW_versym: 483 SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN, SHF_ALLOC, 484 sizeof (Versym)); 485 break; 486 487 default: 488 /* Should not happen: fcn called with unknown section type */ 489 assert(0); 490 return (S_ERROR); 491 } 492 #undef SET_SEC_INFO 493 494 size = entcnt * sec_info->sh_entsize; 495 496 /* 497 * Allocate and initialize the Elf_Data structure. 498 */ 499 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0) 500 return (S_ERROR); 501 data->d_type = sec_info->d_type; 502 data->d_size = size; 503 data->d_align = sec_info->align; 504 data->d_version = ofl->ofl_dehdr->e_version; 505 506 /* 507 * Allocate and initialize the Shdr structure. 508 */ 509 if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0) 510 return (S_ERROR); 511 shdr->sh_type = shtype; 512 shdr->sh_size = size; 513 shdr->sh_flags = sec_info->sh_flags; 514 shdr->sh_addralign = sec_info->align; 515 shdr->sh_entsize = sec_info->sh_entsize; 516 517 /* 518 * Allocate and initialize the Is_desc structure. 519 */ 520 if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0) 521 return (S_ERROR); 522 isec->is_name = shname; 523 isec->is_shdr = shdr; 524 isec->is_indata = data; 525 526 527 *ret_isec = isec; 528 *ret_shdr = shdr; 529 *ret_data = data; 530 return (1); 531 } 532 533 /* 534 * Build a .bss section for allocation of tentative definitions. Any `static' 535 * .bss definitions would have been associated to their own .bss sections and 536 * thus collected from the input files. `global' .bss definitions are tagged 537 * as COMMON and do not cause any associated .bss section elements to be 538 * generated. Here we add up all these COMMON symbols and generate the .bss 539 * section required to represent them. 540 */ 541 uintptr_t 542 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, Bss_Type which) 543 { 544 Shdr *shdr; 545 Elf_Data *data; 546 Is_desc *isec; 547 Os_desc *osp; 548 uint_t ident; 549 Xword rsize = (Xword)ofl->ofl_relocbsssz; 550 551 /* 552 * Allocate header structs. We will set the name ourselves below, 553 * and there is no entcnt for a BSS. So, the shname and entcnt 554 * arguments are 0. 555 */ 556 if (new_section(ofl, SHT_NOBITS, NULL, 0, 557 &isec, &shdr, &data) == S_ERROR) 558 return (S_ERROR); 559 560 data->d_size = (size_t)size; 561 data->d_align = (size_t)align; 562 563 shdr->sh_size = size; 564 shdr->sh_addralign = align; 565 566 if (which == MAKE_TLS) { 567 isec->is_name = MSG_ORIG(MSG_SCN_TBSS); 568 ident = M_ID_TLSBSS; 569 ofl->ofl_istlsbss = isec; 570 shdr->sh_flags |= SHF_TLS; 571 572 } else if (which == MAKE_BSS) { 573 isec->is_name = MSG_ORIG(MSG_SCN_BSS); 574 ofl->ofl_isbss = isec; 575 ident = M_ID_BSS; 576 577 #if defined(__x86) && defined(_ELF64) 578 } else if (which == MAKE_LBSS) { 579 isec->is_name = MSG_ORIG(MSG_SCN_LBSS); 580 ofl->ofl_islbss = isec; 581 ident = M_ID_LBSS; 582 shdr->sh_flags |= SHF_AMD64_LARGE; 583 #endif 584 } 585 586 /* 587 * Retain this .bss input section as this will be where global 588 * symbol references are added. 589 */ 590 if ((osp = ld_place_section(ofl, isec, ident, 0)) == (Os_desc *)S_ERROR) 591 return (S_ERROR); 592 593 /* 594 * If relocations exist against .*bss section, a 595 * section symbol must be created for the section in 596 * the .dynsym symbol table. 597 */ 598 if (!(osp->os_flags & FLG_OS_OUTREL)) { 599 Word flagtotest; 600 if (which == MAKE_TLS) 601 flagtotest = FLG_OF1_TLSOREL; 602 else 603 flagtotest = FLG_OF1_BSSOREL; 604 605 if (ofl->ofl_flags1 & flagtotest) { 606 ofl->ofl_dynshdrcnt++; 607 osp->os_flags |= FLG_OS_OUTREL; 608 } 609 } 610 611 osp->os_szoutrels = rsize; 612 613 return (1); 614 } 615 616 617 /* 618 * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via 619 * ld -z *array=name 620 */ 621 static uintptr_t 622 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, List *list) 623 { 624 uint_t entcount; 625 Listnode *lnp; 626 Elf_Data *data; 627 Is_desc *isec; 628 Shdr *shdr; 629 Sym_desc *sdp; 630 Rel_desc reld; 631 Rela reloc; 632 Os_desc *osp; 633 uintptr_t ret = 1; 634 635 if (list->head == NULL) 636 return (1); 637 638 entcount = 0; 639 for (LIST_TRAVERSE(list, lnp, sdp)) 640 entcount++; 641 642 if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) == 643 S_ERROR) 644 return (S_ERROR); 645 646 if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == 0) 647 return (S_ERROR); 648 649 if (ld_place_section(ofl, isec, M_ID_ARRAY, 0) == (Os_desc *)S_ERROR) 650 return (S_ERROR); 651 652 osp = isec->is_osdesc; 653 654 if ((ofl->ofl_osinitarray == 0) && (shtype == SHT_INIT_ARRAY)) 655 ofl->ofl_osinitarray = osp; 656 if ((ofl->ofl_ospreinitarray == 0) && (shtype == SHT_PREINIT_ARRAY)) 657 ofl->ofl_ospreinitarray = osp; 658 else if ((ofl->ofl_osfiniarray == 0) && (shtype == SHT_FINI_ARRAY)) 659 ofl->ofl_osfiniarray = osp; 660 661 /* 662 * Create relocations against this section to initialize it to the 663 * function addresses. 664 */ 665 reld.rel_osdesc = osp; 666 reld.rel_isdesc = isec; 667 reld.rel_move = 0; 668 reld.rel_flags = FLG_REL_LOAD; 669 670 /* 671 * Fabricate the relocation information (as if a relocation record had 672 * been input - see init_rel()). 673 */ 674 reld.rel_rtype = M_R_ARRAYADDR; 675 reld.rel_roffset = 0; 676 reld.rel_raddend = 0; 677 reld.rel_typedata = 0; 678 679 /* 680 * Create a minimal relocation record to satisfy process_sym_reloc() 681 * debugging requirements. 682 */ 683 reloc.r_offset = 0; 684 reloc.r_info = ELF_R_INFO(0, M_R_ARRAYADDR); 685 reloc.r_addend = 0; 686 687 DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp, M_REL_SHT_TYPE)); 688 for (LIST_TRAVERSE(list, lnp, sdp)) { 689 reld.rel_sname = sdp->sd_name; 690 reld.rel_sym = sdp; 691 692 if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec, 693 MSG_INTL(MSG_STR_COMMAND)) == S_ERROR) { 694 ret = S_ERROR; 695 continue; 696 } 697 698 reld.rel_roffset += (Xword)sizeof (Addr); 699 reloc.r_offset = reld.rel_roffset; 700 } 701 702 return (ret); 703 } 704 705 /* 706 * Build a comment section (-Qy option). 707 */ 708 static uintptr_t 709 make_comment(Ofl_desc *ofl) 710 { 711 Shdr *shdr; 712 Elf_Data *data; 713 Is_desc *isec; 714 715 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0, 716 &isec, &shdr, &data) == S_ERROR) 717 return (S_ERROR); 718 719 data->d_buf = (void *)ofl->ofl_sgsid; 720 data->d_size = strlen(ofl->ofl_sgsid) + 1; 721 data->d_align = 1; 722 723 shdr->sh_size = (Xword)data->d_size; 724 shdr->sh_flags = 0; 725 shdr->sh_addralign = 1; 726 727 return ((uintptr_t)ld_place_section(ofl, isec, M_ID_NOTE, 0)); 728 } 729 730 /* 731 * Make the dynamic section. Calculate the size of any strings referenced 732 * within this structure, they will be added to the global string table 733 * (.dynstr). This routine should be called before make_dynstr(). 734 */ 735 static uintptr_t 736 make_dynamic(Ofl_desc *ofl) 737 { 738 Shdr *shdr; 739 Os_desc *osp; 740 Elf_Data *data; 741 Is_desc *isec; 742 size_t cnt = 0; 743 Listnode *lnp; 744 Ifl_desc *ifl; 745 Sym_desc *sdp; 746 size_t size; 747 Word flags = ofl->ofl_flags; 748 int unused = 0; 749 750 if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0, 751 &isec, &shdr, &data) == S_ERROR) 752 return (S_ERROR); 753 754 /* new_section() does not set SHF_ALLOC. Add it if needed */ 755 if (!(flags & FLG_OF_RELOBJ)) 756 shdr->sh_flags |= SHF_ALLOC; 757 758 osp = ofl->ofl_osdynamic = ld_place_section(ofl, isec, M_ID_DYNAMIC, 0); 759 760 /* 761 * Reserve entries for any needed dependencies. 762 */ 763 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) { 764 Sdf_desc * sdf; 765 766 if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))) 767 continue; 768 769 /* 770 * If this dependency didn't satisfy any symbol references, 771 * generate a debugging diagnostic (ld(1) -Dunused can be used 772 * to display these). If this is a standard needed dependency, 773 * and -z ignore is in effect, drop the dependency. Explicitly 774 * defined dependencies (i.e., -N dep) don't get dropped, and 775 * are flagged as being required to simplify update_odynamic() 776 * processing. 777 */ 778 if ((ifl->ifl_flags & FLG_IF_NEEDSTR) || 779 ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) { 780 if (unused++ == 0) 781 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 782 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname, 783 (ifl->ifl_flags & FLG_IF_NEEDSTR), 0)); 784 785 if (ifl->ifl_flags & FLG_IF_NEEDSTR) 786 ifl->ifl_flags |= FLG_IF_DEPREQD; 787 else if (ifl->ifl_flags & FLG_IF_IGNORE) 788 continue; 789 } 790 791 /* 792 * If this object has an accompanying shared object definition 793 * determine if an alternative shared object name has been 794 * specified. 795 */ 796 if (((sdf = ifl->ifl_sdfdesc) != 0) && 797 (sdf->sdf_flags & FLG_SDF_SONAME)) 798 ifl->ifl_soname = sdf->sdf_soname; 799 800 /* 801 * If this object is a lazyload reserve a DT_POSFLAG1 entry. 802 */ 803 if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM)) 804 cnt++; 805 806 if (st_insert(ofl->ofl_dynstrtab, ifl->ifl_soname) == -1) 807 return (S_ERROR); 808 cnt++; 809 810 /* 811 * If the needed entry contains the $ORIGIN token make sure 812 * the associated DT_1_FLAGS entry is created. 813 */ 814 if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) { 815 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 816 ofl->ofl_dtflags |= DF_ORIGIN; 817 } 818 } 819 820 if (unused) 821 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 822 823 /* 824 * Reserve entries for any per-symbol auxiliary/filter strings. 825 */ 826 if (ofl->ofl_dtsfltrs) { 827 /* LINTED */ 828 Dfltr_desc * dftp; 829 Aliste off; 830 831 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp)) 832 cnt++; 833 } 834 835 /* 836 * Reserve entries for any _init() and _fini() section addresses. 837 */ 838 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), 839 SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && 840 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 841 sdp->sd_flags |= FLG_SY_UPREQD; 842 cnt++; 843 } 844 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U), 845 SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && 846 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 847 sdp->sd_flags |= FLG_SY_UPREQD; 848 cnt++; 849 } 850 851 /* 852 * Reserve entries for any soname, filter name (shared libs only), 853 * run-path pointers, cache names and audit requirements.. 854 */ 855 if (ofl->ofl_soname) { 856 cnt++; 857 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_soname) == -1) 858 return (S_ERROR); 859 } 860 if (ofl->ofl_filtees) { 861 cnt++; 862 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_filtees) == -1) 863 return (S_ERROR); 864 865 /* 866 * If the filtees entry contains the $ORIGIN token make sure 867 * the associated DT_1_FLAGS entry is created. 868 */ 869 if (strstr(ofl->ofl_filtees, MSG_ORIG(MSG_STR_ORIGIN))) { 870 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 871 ofl->ofl_dtflags |= DF_ORIGIN; 872 } 873 } 874 if (ofl->ofl_rpath) { 875 cnt += 2; /* DT_RPATH & DT_RUNPATH */ 876 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_rpath) == -1) 877 return (S_ERROR); 878 879 /* 880 * If the rpath entry contains the $ORIGIN token make sure 881 * the associated DT_1_FLAGS entry is created. 882 */ 883 if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) { 884 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 885 ofl->ofl_dtflags |= DF_ORIGIN; 886 } 887 } 888 if (ofl->ofl_config) { 889 cnt++; 890 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_config) == -1) 891 return (S_ERROR); 892 893 /* 894 * If the config entry contains the $ORIGIN token make sure 895 * the associated DT_1_FLAGS entry is created. 896 */ 897 if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) { 898 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 899 ofl->ofl_dtflags |= DF_ORIGIN; 900 } 901 } 902 if (ofl->ofl_depaudit) { 903 cnt++; 904 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_depaudit) == -1) 905 return (S_ERROR); 906 } 907 if (ofl->ofl_audit) { 908 cnt++; 909 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_audit) == -1) 910 return (S_ERROR); 911 } 912 913 914 /* 915 * The following DT_* entries do not apply to relocatable objects 916 */ 917 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 918 /* 919 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT, 920 * and CHECKSUM. 921 */ 922 cnt += 6; 923 924 /* 925 * If we are including local functions at the head of 926 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB 927 * and DT_SUNW_SYMSZ. 928 */ 929 if (OFL_ALLOW_LDYNSYM(ofl)) 930 cnt += 2; 931 932 if ((ofl->ofl_dynsymsortcnt > 0) || 933 (ofl->ofl_dyntlssortcnt > 0)) 934 cnt++; /* DT_SUNW_SORTENT */ 935 936 if (ofl->ofl_dynsymsortcnt > 0) 937 cnt += 2; /* DT_SUNW_[SYMSORT|SYMSORTSZ] */ 938 939 if (ofl->ofl_dyntlssortcnt > 0) 940 cnt += 2; /* DT_SUNW_[TLSSORT|TLSSORTSZ] */ 941 942 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == 943 FLG_OF_VERDEF) 944 cnt += 2; /* DT_VERDEF & DT_VERDEFNUM */ 945 946 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == 947 FLG_OF_VERNEED) 948 cnt += 2; /* DT_VERNEED & DT_VERNEEDNUM */ 949 950 if ((ofl->ofl_flags1 & FLG_OF1_RELCNT) && 951 ofl->ofl_relocrelcnt) /* RELACOUNT */ 952 cnt++; 953 954 if (flags & FLG_OF_TEXTREL) /* TEXTREL */ 955 cnt++; 956 957 if (ofl->ofl_osfiniarray) /* FINI_ARRAY & FINI_ARRAYSZ */ 958 cnt += 2; 959 960 if (ofl->ofl_osinitarray) /* INIT_ARRAY & INIT_ARRAYSZ */ 961 cnt += 2; 962 963 if (ofl->ofl_ospreinitarray) /* PREINIT_ARRAY & */ 964 cnt += 2; /* PREINIT_ARRAYSZ */ 965 966 /* 967 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL. 968 */ 969 if (ofl->ofl_pltcnt) 970 cnt += 3; 971 972 /* 973 * If pltpadding is needed (Sparcv9) 974 */ 975 if (ofl->ofl_pltpad) 976 cnt += 2; /* DT_PLTPAD & DT_PLTPADSZ */ 977 978 /* 979 * If we have any relocations reserve a REL, RELSZ and 980 * RELENT entry. 981 */ 982 if (ofl->ofl_relocsz) 983 cnt += 3; 984 985 /* 986 * If a syminfo section is required create SYMINFO, SYMINSZ, 987 * and SYMINENT entries. 988 */ 989 if (ofl->ofl_flags & FLG_OF_SYMINFO) 990 cnt += 3; 991 992 /* 993 * If there are any partially initialized sections allocate 994 * MOVEENT, MOVESZ and MOVETAB. 995 */ 996 if (ofl->ofl_osmove) 997 cnt += 3; 998 999 /* 1000 * Allocate one DT_REGISTER entry for every register symbol. 1001 */ 1002 cnt += ofl->ofl_regsymcnt; 1003 1004 /* 1005 * Reserve a entry for each '-zrtldinfo=...' specified 1006 * on the command line. 1007 */ 1008 for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp)) 1009 cnt++; 1010 1011 /* 1012 * These two entries should only be placed in a segment 1013 * which is writable. If it's a read-only segment 1014 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate 1015 * these entries. 1016 */ 1017 if ((osp->os_sgdesc) && 1018 (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) { 1019 cnt++; /* FEATURE_1 */ 1020 1021 if (ofl->ofl_osinterp) 1022 cnt++; /* DEBUG */ 1023 } 1024 1025 /* 1026 * Any hardware/software capabilities? 1027 */ 1028 if (ofl->ofl_oscap) 1029 cnt++; /* SUNW_CAP */ 1030 } 1031 1032 if (flags & FLG_OF_SYMBOLIC) 1033 cnt++; /* SYMBOLIC */ 1034 1035 /* 1036 * Account for Architecture dependent .dynamic entries, and defaults. 1037 */ 1038 ld_mach_make_dynamic(ofl, &cnt); 1039 1040 cnt += 3; /* DT_FLAGS, DT_FLAGS_1, */ 1041 /* and DT_NULL */ 1042 1043 /* 1044 * Determine the size of the section from the number of entries. 1045 */ 1046 size = cnt * (size_t)shdr->sh_entsize; 1047 1048 shdr->sh_size = (Xword)size; 1049 data->d_size = size; 1050 1051 return ((uintptr_t)ofl->ofl_osdynamic); 1052 } 1053 1054 /* 1055 * Build the GOT section and its associated relocation entries. 1056 */ 1057 uintptr_t 1058 ld_make_got(Ofl_desc *ofl) 1059 { 1060 Shdr *shdr; 1061 Elf_Data *data; 1062 Is_desc *isec; 1063 size_t size = (size_t)ofl->ofl_gotcnt * M_GOT_ENTSIZE; 1064 size_t rsize = (size_t)ofl->ofl_relocgotsz; 1065 1066 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0, 1067 &isec, &shdr, &data) == S_ERROR) 1068 return (S_ERROR); 1069 1070 data->d_size = size; 1071 1072 shdr->sh_flags |= SHF_WRITE; 1073 shdr->sh_size = (Xword)size; 1074 shdr->sh_entsize = M_GOT_ENTSIZE; 1075 1076 if ((ofl->ofl_osgot = ld_place_section(ofl, isec, M_ID_GOT, 0)) == 1077 (Os_desc *)S_ERROR) 1078 return (S_ERROR); 1079 1080 ofl->ofl_osgot->os_szoutrels = (Xword)rsize; 1081 1082 return (1); 1083 } 1084 1085 /* 1086 * Build an interpreter section. 1087 */ 1088 static uintptr_t 1089 make_interp(Ofl_desc *ofl) 1090 { 1091 Shdr *shdr; 1092 Elf_Data *data; 1093 Is_desc *isec; 1094 const char *iname = ofl->ofl_interp; 1095 size_t size; 1096 1097 /* 1098 * If -z nointerp is in effect, don't create an interpreter section. 1099 */ 1100 if (ofl->ofl_flags1 & FLG_OF1_NOINTRP) 1101 return (1); 1102 1103 /* 1104 * We always build an .interp section for dynamic executables. However 1105 * if the user has specifically specified an interpreter we'll build 1106 * this section for any output (presumably the user knows what they are 1107 * doing. refer ABI section 5-4, and ld.1 man page use of -I). 1108 */ 1109 if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC | 1110 FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname) 1111 return (1); 1112 1113 /* 1114 * In the case of a dynamic executable supply a default interpreter 1115 * if a specific interpreter has not been specified. 1116 */ 1117 if (iname == 0) { 1118 if (ofl->ofl_dehdr->e_machine == EM_SPARCV9) 1119 iname = ofl->ofl_interp = 1120 MSG_ORIG(MSG_PTH_RTLD_SPARCV9); 1121 else if (ofl->ofl_dehdr->e_machine == EM_AMD64) 1122 iname = ofl->ofl_interp = 1123 MSG_ORIG(MSG_PTH_RTLD_AMD64); 1124 else 1125 iname = ofl->ofl_interp = MSG_ORIG(MSG_PTH_RTLD); 1126 } 1127 1128 size = strlen(iname) + 1; 1129 1130 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0, 1131 &isec, &shdr, &data) == S_ERROR) 1132 return (S_ERROR); 1133 1134 data->d_size = size; 1135 shdr->sh_size = (Xword)size; 1136 data->d_align = shdr->sh_addralign = 1; 1137 1138 ofl->ofl_osinterp = ld_place_section(ofl, isec, M_ID_INTERP, 0); 1139 return ((uintptr_t)ofl->ofl_osinterp); 1140 } 1141 1142 /* 1143 * Build a hardware/software capabilities section. 1144 */ 1145 static uintptr_t 1146 make_cap(Ofl_desc *ofl) 1147 { 1148 Shdr *shdr; 1149 Elf_Data *data; 1150 Is_desc *isec; 1151 Os_desc *osec; 1152 Cap *cap; 1153 size_t size = 0; 1154 1155 /* 1156 * Determine how many entries are required. 1157 */ 1158 if (ofl->ofl_hwcap_1) 1159 size++; 1160 if (ofl->ofl_sfcap_1) 1161 size++; 1162 if (size == 0) 1163 return (1); 1164 size++; /* Add CA_SUNW_NULL */ 1165 1166 if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size, 1167 &isec, &shdr, &data) == S_ERROR) 1168 return (S_ERROR); 1169 1170 if ((data->d_buf = libld_malloc(shdr->sh_size)) == 0) 1171 return (S_ERROR); 1172 1173 cap = (Cap *)data->d_buf; 1174 if (ofl->ofl_hwcap_1) { 1175 cap->c_tag = CA_SUNW_HW_1; 1176 cap->c_un.c_val = ofl->ofl_hwcap_1; 1177 cap++; 1178 } 1179 if (ofl->ofl_sfcap_1) { 1180 cap->c_tag = CA_SUNW_SF_1; 1181 cap->c_un.c_val = ofl->ofl_sfcap_1; 1182 cap++; 1183 } 1184 cap->c_tag = CA_SUNW_NULL; 1185 cap->c_un.c_val = 0; 1186 1187 /* 1188 * If we're not creating a relocatable object, save the output section 1189 * to trigger the creation of an associated program header. 1190 */ 1191 osec = ld_place_section(ofl, isec, M_ID_CAP, 0); 1192 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) 1193 ofl->ofl_oscap = osec; 1194 1195 return ((uintptr_t)osec); 1196 } 1197 1198 /* 1199 * Build the PLT section and its associated relocation entries. 1200 */ 1201 static uintptr_t 1202 make_plt(Ofl_desc *ofl) 1203 { 1204 Shdr *shdr; 1205 Elf_Data *data; 1206 Is_desc *isec; 1207 size_t size = (size_t)M_PLT_RESERVSZ + 1208 (((size_t)ofl->ofl_pltcnt + 1209 (size_t)ofl->ofl_pltpad) * M_PLT_ENTSIZE); 1210 size_t rsize = (size_t)ofl->ofl_relocpltsz; 1211 1212 #if defined(__sparc) 1213 /* 1214 * Account for the NOP at the end of the plt. 1215 */ 1216 size += sizeof (Word); 1217 #endif 1218 1219 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0, 1220 &isec, &shdr, &data) == S_ERROR) 1221 return (S_ERROR); 1222 1223 data->d_size = size; 1224 data->d_align = M_PLT_ALIGN; 1225 1226 shdr->sh_flags = M_PLT_SHF_FLAGS; 1227 shdr->sh_size = (Xword)size; 1228 shdr->sh_addralign = M_PLT_ALIGN; 1229 shdr->sh_entsize = M_PLT_ENTSIZE; 1230 1231 if ((ofl->ofl_osplt = ld_place_section(ofl, isec, M_ID_PLT, 0)) == 1232 (Os_desc *)S_ERROR) 1233 return (S_ERROR); 1234 1235 ofl->ofl_osplt->os_szoutrels = (Xword)rsize; 1236 1237 return (1); 1238 } 1239 1240 /* 1241 * Make the hash table. Only built for dynamic executables and shared 1242 * libraries, and provides hashed lookup into the global symbol table 1243 * (.dynsym) for the run-time linker to resolve symbol lookups. 1244 */ 1245 static uintptr_t 1246 make_hash(Ofl_desc *ofl) 1247 { 1248 Shdr *shdr; 1249 Elf_Data *data; 1250 Is_desc *isec; 1251 size_t size; 1252 Word nsyms = ofl->ofl_globcnt; 1253 size_t cnt; 1254 1255 /* 1256 * Allocate section header structures. We set entcnt to 0 1257 * because it's going to change after we place this section. 1258 */ 1259 if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0, 1260 &isec, &shdr, &data) == S_ERROR) 1261 return (S_ERROR); 1262 1263 /* 1264 * Place the section first since it will affect the local symbol 1265 * count. 1266 */ 1267 if ((ofl->ofl_oshash = ld_place_section(ofl, isec, M_ID_HASH, 0)) == 1268 (Os_desc *)S_ERROR) 1269 return (S_ERROR); 1270 1271 /* 1272 * Calculate the number of output hash buckets. 1273 */ 1274 ofl->ofl_hashbkts = findprime(nsyms); 1275 1276 /* 1277 * The size of the hash table is determined by 1278 * 1279 * i. the initial nbucket and nchain entries (2) 1280 * ii. the number of buckets (calculated above) 1281 * iii. the number of chains (this is based on the number of 1282 * symbols in the .dynsym array + NULL symbol). 1283 */ 1284 cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt + 1285 ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1); 1286 size = cnt * shdr->sh_entsize; 1287 1288 /* 1289 * Finalize the section header and data buffer initialization. 1290 */ 1291 if ((data->d_buf = libld_calloc(size, 1)) == 0) 1292 return (S_ERROR); 1293 data->d_size = size; 1294 shdr->sh_size = (Xword)size; 1295 1296 return (1); 1297 } 1298 1299 /* 1300 * Generate the standard symbol table. Contains all locals and globals, 1301 * and resides in a non-allocatable section (ie. it can be stripped). 1302 */ 1303 static uintptr_t 1304 make_symtab(Ofl_desc *ofl) 1305 { 1306 Shdr *shdr; 1307 Elf_Data *data; 1308 Is_desc *isec; 1309 Is_desc *xisec = 0; 1310 size_t size; 1311 Word symcnt; 1312 1313 /* 1314 * Create the section headers. Note that we supply an ent_cnt 1315 * of 0. We won't know the count until the section has been placed. 1316 */ 1317 if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0, 1318 &isec, &shdr, &data) == S_ERROR) 1319 return (S_ERROR); 1320 1321 /* 1322 * Place the section first since it will affect the local symbol 1323 * count. 1324 */ 1325 if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, M_ID_SYMTAB, 0)) == 1326 (Os_desc *)S_ERROR) 1327 return (S_ERROR); 1328 1329 /* 1330 * At this point we've created all but the 'shstrtab' section. 1331 * Determine if we have to use 'Extended Sections'. If so - then 1332 * also create a SHT_SYMTAB_SHNDX section. 1333 */ 1334 if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) { 1335 Shdr *xshdr; 1336 Elf_Data *xdata; 1337 1338 if (new_section(ofl, SHT_SYMTAB_SHNDX, 1339 MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec, 1340 &xshdr, &xdata) == S_ERROR) 1341 return (S_ERROR); 1342 1343 if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, 1344 M_ID_SYMTAB_NDX, 0)) == (Os_desc *)S_ERROR) 1345 return (S_ERROR); 1346 } 1347 /* 1348 * Calculated number of symbols, which need to be augmented by 1349 * the null first entry, the FILE symbol, and the .shstrtab entry. 1350 */ 1351 symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt + 1352 ofl->ofl_locscnt + ofl->ofl_globcnt); 1353 size = symcnt * shdr->sh_entsize; 1354 1355 /* 1356 * Finalize the section header and data buffer initialization. 1357 */ 1358 data->d_size = size; 1359 shdr->sh_size = (Xword)size; 1360 1361 /* 1362 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too. 1363 */ 1364 if (xisec) { 1365 size_t xsize = symcnt * sizeof (Word); 1366 1367 xisec->is_indata->d_size = xsize; 1368 xisec->is_shdr->sh_size = (Xword)xsize; 1369 } 1370 1371 return (1); 1372 } 1373 1374 1375 /* 1376 * Build a dynamic symbol table. These tables reside in the text 1377 * segment of a dynamic executable or shared library. 1378 * 1379 * .SUNW_ldynsym contains local function symbols 1380 * .dynsym contains only globals symbols 1381 * 1382 * The two tables are created adjacent to each other, with .SUNW_ldynsym 1383 * coming first. 1384 */ 1385 static uintptr_t 1386 make_dynsym(Ofl_desc *ofl) 1387 { 1388 Shdr *shdr, *lshdr; 1389 Elf_Data *data, *ldata; 1390 Is_desc *isec, *lisec; 1391 size_t size; 1392 Xword cnt; 1393 int allow_ldynsym; 1394 1395 /* 1396 * Unless explicitly disabled, always produce a .SUNW_ldynsym section 1397 * when it is allowed by the file type, even if the resulting 1398 * table only ends up with a single STT_FILE in it. There are 1399 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB 1400 * entry in the .dynamic section, which is something we would 1401 * like to encourage, and (2) Without it, we cannot generate 1402 * the associated .SUNW_dyn[sym|tls]sort sections, which are of 1403 * value to DTrace. 1404 * 1405 * In practice, it is extremely rare for an object not to have 1406 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be 1407 * doing it anyway. 1408 */ 1409 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 1410 1411 /* 1412 * Create the section headers. Note that we supply an ent_cnt 1413 * of 0. We won't know the count until the section has been placed. 1414 */ 1415 if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM, 1416 MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR) 1417 return (S_ERROR); 1418 1419 if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0, 1420 &isec, &shdr, &data) == S_ERROR) 1421 return (S_ERROR); 1422 1423 /* 1424 * Place the section(s) first since it will affect the local symbol 1425 * count. 1426 */ 1427 if (allow_ldynsym && 1428 ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, 1429 M_ID_LDYNSYM, 0)) == (Os_desc *)S_ERROR)) 1430 return (S_ERROR); 1431 if ((ofl->ofl_osdynsym = ld_place_section(ofl, isec, M_ID_DYNSYM, 0)) 1432 == (Os_desc *)S_ERROR) 1433 return (S_ERROR); 1434 1435 /* 1436 * One extra section header entry for the 'null' entry. 1437 */ 1438 cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt; 1439 size = (size_t)cnt * shdr->sh_entsize; 1440 1441 /* 1442 * Finalize the section header and data buffer initialization. 1443 */ 1444 data->d_size = size; 1445 shdr->sh_size = (Xword)size; 1446 1447 /* 1448 * An ldynsym contains local function symbols. It is not 1449 * used for linking, but if present, serves to allow better 1450 * stack traces to be generated in contexts where the symtab 1451 * is not available. (dladdr(), or stripped executable/library files). 1452 */ 1453 if (allow_ldynsym) { 1454 cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt; 1455 size = (size_t)cnt * shdr->sh_entsize; 1456 1457 ldata->d_size = size; 1458 lshdr->sh_size = (Xword)size; 1459 } 1460 1461 return (1); 1462 } 1463 1464 /* 1465 * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are 1466 * index sections for the .SUNW_ldynsym/.dynsym pair that present data 1467 * and function symbols sorted by address. 1468 */ 1469 static uintptr_t 1470 make_dynsort(Ofl_desc *ofl) 1471 { 1472 Shdr *shdr; 1473 Elf_Data *data; 1474 Is_desc *isec; 1475 1476 1477 /* Only do it if the .SUNW_ldynsym section is present */ 1478 if (!OFL_ALLOW_LDYNSYM(ofl)) 1479 return (1); 1480 1481 /* .SUNW_dynsymsort */ 1482 if (ofl->ofl_dynsymsortcnt > 0) { 1483 if (new_section(ofl, SHT_SUNW_symsort, 1484 MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt, 1485 &isec, &shdr, &data) == S_ERROR) 1486 return (S_ERROR); 1487 1488 if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, 1489 M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR) 1490 return (S_ERROR); 1491 } 1492 1493 /* .SUNW_dyntlssort */ 1494 if (ofl->ofl_dyntlssortcnt > 0) { 1495 if (new_section(ofl, SHT_SUNW_tlssort, 1496 MSG_ORIG(MSG_SCN_DYNTLSSORT), 1497 ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR) 1498 return (S_ERROR); 1499 1500 if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec, 1501 M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR) 1502 return (S_ERROR); 1503 } 1504 1505 return (1); 1506 } 1507 1508 /* 1509 * Helper routine for make_dynsym_shndx. Builds a 1510 * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing 1511 * which one it is. 1512 */ 1513 static uintptr_t 1514 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab, 1515 Os_desc **ret_os) 1516 { 1517 Is_desc *isec; 1518 Is_desc *dynsymisp; 1519 Shdr *shdr, *dynshdr; 1520 Elf_Data *data; 1521 1522 dynsymisp = (Is_desc *)symtab->os_isdescs.head->data; 1523 dynshdr = dynsymisp->is_shdr; 1524 1525 if (new_section(ofl, SHT_SYMTAB_SHNDX, shname, 1526 (dynshdr->sh_size / dynshdr->sh_entsize), 1527 &isec, &shdr, &data) == S_ERROR) 1528 return (S_ERROR); 1529 1530 if ((*ret_os = ld_place_section(ofl, isec, 1531 M_ID_DYNSYM_NDX, 0)) == (Os_desc *)S_ERROR) 1532 return (S_ERROR); 1533 1534 assert(*ret_os); 1535 1536 return (1); 1537 } 1538 1539 /* 1540 * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym 1541 */ 1542 static uintptr_t 1543 make_dynsym_shndx(Ofl_desc *ofl) 1544 { 1545 /* 1546 * If there is a .SUNW_ldynsym, generate a section for its extended 1547 * index section as well. 1548 */ 1549 if (OFL_ALLOW_LDYNSYM(ofl)) { 1550 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX), 1551 ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR) 1552 return (S_ERROR); 1553 } 1554 1555 /* The Generate a section for the dynsym */ 1556 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX), 1557 ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR) 1558 return (S_ERROR); 1559 1560 return (1); 1561 } 1562 1563 1564 /* 1565 * Build a string table for the section headers. 1566 */ 1567 static uintptr_t 1568 make_shstrtab(Ofl_desc *ofl) 1569 { 1570 Shdr *shdr; 1571 Elf_Data *data; 1572 Is_desc *isec; 1573 size_t size; 1574 1575 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB), 1576 0, &isec, &shdr, &data) == S_ERROR) 1577 return (S_ERROR); 1578 1579 /* 1580 * Place the section first, as it may effect the number of section 1581 * headers to account for. 1582 */ 1583 if ((ofl->ofl_osshstrtab = ld_place_section(ofl, isec, M_ID_NOTE, 0)) == 1584 (Os_desc *)S_ERROR) 1585 return (S_ERROR); 1586 1587 size = st_getstrtab_sz(ofl->ofl_shdrsttab); 1588 assert(size > 0); 1589 1590 data->d_size = size; 1591 shdr->sh_size = (Xword)size; 1592 1593 return (1); 1594 } 1595 1596 /* 1597 * Build a string section for the standard symbol table. 1598 */ 1599 static uintptr_t 1600 make_strtab(Ofl_desc *ofl) 1601 { 1602 Shdr *shdr; 1603 Elf_Data *data; 1604 Is_desc *isec; 1605 size_t size; 1606 1607 /* 1608 * This string table consists of all the global and local symbols. 1609 * Account for null bytes at end of the file name and the beginning 1610 * of section. 1611 */ 1612 if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1) 1613 return (S_ERROR); 1614 1615 size = st_getstrtab_sz(ofl->ofl_strtab); 1616 assert(size > 0); 1617 1618 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB), 1619 0, &isec, &shdr, &data) == S_ERROR) 1620 return (S_ERROR); 1621 1622 /* Set the size of the data area */ 1623 data->d_size = size; 1624 shdr->sh_size = (Xword)size; 1625 1626 ofl->ofl_osstrtab = ld_place_section(ofl, isec, M_ID_STRTAB, 0); 1627 return ((uintptr_t)ofl->ofl_osstrtab); 1628 } 1629 1630 /* 1631 * Build a string table for the dynamic symbol table. 1632 */ 1633 static uintptr_t 1634 make_dynstr(Ofl_desc *ofl) 1635 { 1636 Shdr *shdr; 1637 Elf_Data *data; 1638 Is_desc *isec; 1639 size_t size; 1640 1641 /* 1642 * If producing a .SUNW_ldynsym, account for the initial STT_FILE 1643 * symbol that precedes the scope reduced global symbols. 1644 */ 1645 if (OFL_ALLOW_LDYNSYM(ofl)) { 1646 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1) 1647 return (S_ERROR); 1648 ofl->ofl_dynscopecnt++; 1649 } 1650 1651 1652 /* 1653 * Account for any local, named register symbols. These locals are 1654 * required for reference from DT_REGISTER .dynamic entries. 1655 */ 1656 if (ofl->ofl_regsyms) { 1657 int ndx; 1658 1659 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) { 1660 Sym_desc * sdp; 1661 1662 if ((sdp = ofl->ofl_regsyms[ndx]) == 0) 1663 continue; 1664 1665 if (((sdp->sd_flags1 & FLG_SY1_LOCL) == 0) && 1666 (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL)) 1667 continue; 1668 1669 if (sdp->sd_sym->st_name == 0) 1670 continue; 1671 1672 if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1) 1673 return (S_ERROR); 1674 } 1675 } 1676 1677 /* 1678 * Reserve entries for any per-symbol auxiliary/filter strings. 1679 */ 1680 if (ofl->ofl_dtsfltrs) { 1681 Dfltr_desc * dftp; 1682 Aliste off; 1683 1684 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp)) 1685 if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1) 1686 return (S_ERROR); 1687 } 1688 1689 size = st_getstrtab_sz(ofl->ofl_dynstrtab); 1690 assert(size > 0); 1691 1692 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR), 1693 0, &isec, &shdr, &data) == S_ERROR) 1694 return (S_ERROR); 1695 1696 /* Make it allocable if necessary */ 1697 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 1698 shdr->sh_flags |= SHF_ALLOC; 1699 1700 /* Set the size of the data area */ 1701 data->d_size = size; 1702 shdr->sh_size = (Xword)size; 1703 1704 ofl->ofl_osdynstr = ld_place_section(ofl, isec, M_ID_DYNSTR, 0); 1705 return ((uintptr_t)ofl->ofl_osdynstr); 1706 } 1707 1708 /* 1709 * Generate an output relocation section which will contain the relocation 1710 * information to be applied to the `osp' section. 1711 * 1712 * If (osp == NULL) then we are creating the coalesced relocation section 1713 * for an executable and/or a shared object. 1714 */ 1715 static uintptr_t 1716 make_reloc(Ofl_desc *ofl, Os_desc *osp) 1717 { 1718 Shdr *shdr; 1719 Elf_Data *data; 1720 Is_desc *isec; 1721 size_t size; 1722 Xword sh_flags; 1723 char *sectname; 1724 Os_desc *rosp; 1725 Word relsize; 1726 const char *rel_prefix; 1727 1728 /* LINTED */ 1729 if (M_REL_SHT_TYPE == SHT_REL) { 1730 /* REL */ 1731 relsize = sizeof (Rel); 1732 rel_prefix = MSG_ORIG(MSG_SCN_REL); 1733 } else { 1734 /* RELA */ 1735 relsize = sizeof (Rela); 1736 rel_prefix = MSG_ORIG(MSG_SCN_RELA); 1737 } 1738 1739 if (osp) { 1740 size = osp->os_szoutrels; 1741 sh_flags = osp->os_shdr->sh_flags; 1742 if ((sectname = libld_malloc(strlen(rel_prefix) + 1743 strlen(osp->os_name) + 1)) == 0) 1744 return (S_ERROR); 1745 (void) strcpy(sectname, rel_prefix); 1746 (void) strcat(sectname, osp->os_name); 1747 } else if (ofl->ofl_flags1 & FLG_OF1_RELCNT) { 1748 size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize; 1749 sh_flags = SHF_ALLOC; 1750 sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC); 1751 } else { 1752 size = ofl->ofl_relocrelsz; 1753 sh_flags = SHF_ALLOC; 1754 sectname = (char *)rel_prefix; 1755 } 1756 1757 /* 1758 * Keep track of total size of 'output relocations' (to be stored 1759 * in .dynamic) 1760 */ 1761 /* LINTED */ 1762 ofl->ofl_relocsz += (Xword)size; 1763 1764 if (new_section(ofl, M_REL_SHT_TYPE, sectname, 0, &isec, &shdr, &data) 1765 == S_ERROR) 1766 return (S_ERROR); 1767 1768 data->d_size = size; 1769 1770 shdr->sh_size = (Xword)size; 1771 if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC)) 1772 shdr->sh_flags = SHF_ALLOC; 1773 1774 if (osp) { 1775 /* 1776 * The sh_info field of the SHT_REL* sections points to the 1777 * section the relocations are to be applied to. 1778 */ 1779 shdr->sh_flags |= SHF_INFO_LINK; 1780 } 1781 1782 /* 1783 * Associate this relocation section to the section its going to 1784 * relocate. 1785 */ 1786 if ((rosp = ld_place_section(ofl, isec, M_ID_REL, 0)) == 1787 (Os_desc *)S_ERROR) 1788 return (S_ERROR); 1789 1790 if (osp) { 1791 Listnode *lnp; 1792 Is_desc *risp; 1793 1794 /* 1795 * We associate the input relocation sections - with 1796 * the newly created output relocation section. 1797 * 1798 * This is used primarily so that we can update 1799 * SHT_GROUP[sect_no] entries to point to the 1800 * created output relocation sections. 1801 */ 1802 for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) { 1803 risp->is_osdesc = rosp; 1804 1805 /* 1806 * If the input relocation section had the SHF_GROUP 1807 * flag set - propagate it to the output relocation 1808 * section. 1809 */ 1810 if (risp->is_shdr->sh_flags & SHF_GROUP) { 1811 rosp->os_shdr->sh_flags |= SHF_GROUP; 1812 break; 1813 } 1814 } 1815 osp->os_relosdesc = rosp; 1816 } else 1817 ofl->ofl_osrel = rosp; 1818 1819 /* 1820 * If this is the first relocation section we've encountered save it 1821 * so that the .dynamic entry can be initialized accordingly. 1822 */ 1823 if (ofl->ofl_osrelhead == (Os_desc *)0) 1824 ofl->ofl_osrelhead = rosp; 1825 1826 return (1); 1827 } 1828 1829 /* 1830 * Generate version needed section. 1831 */ 1832 static uintptr_t 1833 make_verneed(Ofl_desc *ofl) 1834 { 1835 Shdr *shdr; 1836 Elf_Data *data; 1837 Is_desc *isec; 1838 1839 /* 1840 * verneed sections do not have a constant element size, so the 1841 * value of ent_cnt specified here (0) is meaningless. 1842 */ 1843 if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION), 1844 0, &isec, &shdr, &data) == S_ERROR) 1845 return (S_ERROR); 1846 1847 /* During version processing we calculated the total size. */ 1848 data->d_size = ofl->ofl_verneedsz; 1849 shdr->sh_size = (Xword)ofl->ofl_verneedsz; 1850 1851 ofl->ofl_osverneed = ld_place_section(ofl, isec, M_ID_VERSION, 0); 1852 return ((uintptr_t)ofl->ofl_osverneed); 1853 } 1854 1855 /* 1856 * Generate a version definition section. 1857 * 1858 * o the SHT_SUNW_verdef section defines the versions that exist within this 1859 * image. 1860 */ 1861 static uintptr_t 1862 make_verdef(Ofl_desc *ofl) 1863 { 1864 Shdr *shdr; 1865 Elf_Data *data; 1866 Is_desc *isec; 1867 Ver_desc *vdp; 1868 1869 /* 1870 * Reserve a string table entry for the base version dependency (other 1871 * dependencies have symbol representations, which will already be 1872 * accounted for during symbol processing). 1873 */ 1874 vdp = (Ver_desc *)ofl->ofl_verdesc.head->data; 1875 1876 if (ofl->ofl_flags & FLG_OF_DYNAMIC) { 1877 if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1) 1878 return (S_ERROR); 1879 } else { 1880 if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1) 1881 return (S_ERROR); 1882 } 1883 1884 /* 1885 * verdef sections do not have a constant element size, so the 1886 * value of ent_cnt specified here (0) is meaningless. 1887 */ 1888 if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION), 1889 0, &isec, &shdr, &data) == S_ERROR) 1890 return (S_ERROR); 1891 1892 /* During version processing we calculated the total size. */ 1893 data->d_size = ofl->ofl_verdefsz; 1894 shdr->sh_size = (Xword)ofl->ofl_verdefsz; 1895 1896 ofl->ofl_osverdef = ld_place_section(ofl, isec, M_ID_VERSION, 0); 1897 return ((uintptr_t)ofl->ofl_osverdef); 1898 } 1899 1900 /* 1901 * Common function used to build both the SHT_SUNW_versym 1902 * section and the SHT_SUNW_syminfo section. Each of these sections 1903 * provides additional symbol information. 1904 */ 1905 static Os_desc * 1906 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident) 1907 { 1908 Shdr *shdr; 1909 Elf_Data *data; 1910 Is_desc *isec; 1911 1912 /* 1913 * We don't know the size of this section yet, so set it to 0. 1914 * It gets filled in after the dynsym is sized. 1915 */ 1916 if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) == 1917 S_ERROR) 1918 return ((Os_desc *)S_ERROR); 1919 1920 return (ld_place_section(ofl, isec, ident, 0)); 1921 } 1922 1923 /* 1924 * Build a .sunwbss section for allocation of tentative definitions. 1925 */ 1926 uintptr_t 1927 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align) 1928 { 1929 Shdr *shdr; 1930 Elf_Data *data; 1931 Is_desc *isec; 1932 1933 /* 1934 * Allocate header structs. We will set the name ourselves below, 1935 * and there is no entcnt for a BSS. So, the shname and entcnt 1936 * arguments are 0. 1937 */ 1938 if (new_section(ofl, SHT_NOBITS, MSG_ORIG(MSG_SCN_SUNWBSS), 0, 1939 &isec, &shdr, &data) == S_ERROR) 1940 return (S_ERROR); 1941 1942 data->d_size = size; 1943 data->d_align = align; 1944 1945 shdr->sh_size = (Xword)size; 1946 shdr->sh_addralign = align; 1947 1948 /* 1949 * Retain this .sunwbss input section as this will be where global 1950 * symbol references are added. 1951 */ 1952 ofl->ofl_issunwbss = isec; 1953 if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR) 1954 return (S_ERROR); 1955 1956 return (1); 1957 } 1958 1959 /* 1960 * This routine is called when -z nopartial is in effect. 1961 */ 1962 uintptr_t 1963 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align) 1964 { 1965 Shdr *shdr; 1966 Elf_Data *data; 1967 Is_desc *isec; 1968 Os_desc *osp; 1969 1970 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_SUNWDATA1), 0, 1971 &isec, &shdr, &data) == S_ERROR) 1972 return (S_ERROR); 1973 1974 shdr->sh_flags |= SHF_WRITE; 1975 data->d_size = size; 1976 shdr->sh_size = (Xword)size; 1977 if (align != 0) { 1978 data->d_align = align; 1979 shdr->sh_addralign = align; 1980 } 1981 1982 if ((data->d_buf = libld_calloc(size, 1)) == 0) 1983 return (S_ERROR); 1984 1985 /* 1986 * Retain this .sunwdata1 input section as this will 1987 * be where global 1988 * symbol references are added. 1989 */ 1990 ofl->ofl_issunwdata1 = isec; 1991 if ((osp = ld_place_section(ofl, isec, M_ID_DATA, 0)) == 1992 (Os_desc *)S_ERROR) 1993 return (S_ERROR); 1994 1995 if (!(osp->os_flags & FLG_OS_OUTREL)) { 1996 ofl->ofl_dynshdrcnt++; 1997 osp->os_flags |= FLG_OS_OUTREL; 1998 } 1999 return (1); 2000 } 2001 2002 /* 2003 * Make .sunwmove section 2004 */ 2005 uintptr_t 2006 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums) 2007 { 2008 Shdr *shdr; 2009 Elf_Data *data; 2010 Is_desc *isec; 2011 Listnode *lnp1; 2012 Psym_info *psym; 2013 int cnt = 1; 2014 2015 2016 if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE), 2017 mv_nums, &isec, &shdr, &data) == S_ERROR) 2018 return (S_ERROR); 2019 2020 if ((data->d_buf = libld_calloc(data->d_size, 1)) == 0) 2021 return (S_ERROR); 2022 2023 /* 2024 * Copy move entries 2025 */ 2026 for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) { 2027 Listnode * lnp2; 2028 Mv_itm * mvitm; 2029 2030 if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN) 2031 continue; 2032 for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) { 2033 if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0) 2034 continue; 2035 mvitm->mv_oidx = cnt; 2036 cnt++; 2037 } 2038 } 2039 if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) == 2040 (Os_desc *)S_ERROR) 2041 return (S_ERROR); 2042 2043 return (1); 2044 } 2045 2046 2047 /* 2048 * The following sections are built after all input file processing and symbol 2049 * validation has been carried out. The order is important (because the 2050 * addition of a section adds a new symbol there is a chicken and egg problem 2051 * of maintaining the appropriate counts). By maintaining a known order the 2052 * individual routines can compensate for later, known, additions. 2053 */ 2054 uintptr_t 2055 ld_make_sections(Ofl_desc *ofl) 2056 { 2057 Word flags = ofl->ofl_flags; 2058 Listnode *lnp1; 2059 Sg_desc *sgp; 2060 2061 /* 2062 * Generate any special sections. 2063 */ 2064 if (flags & FLG_OF_ADDVERS) 2065 if (make_comment(ofl) == S_ERROR) 2066 return (S_ERROR); 2067 2068 if (make_interp(ofl) == S_ERROR) 2069 return (S_ERROR); 2070 2071 if (make_cap(ofl) == S_ERROR) 2072 return (S_ERROR); 2073 2074 if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY), 2075 &ofl->ofl_initarray) == S_ERROR) 2076 return (S_ERROR); 2077 2078 if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY), 2079 &ofl->ofl_finiarray) == S_ERROR) 2080 return (S_ERROR); 2081 2082 if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY), 2083 &ofl->ofl_preiarray) == S_ERROR) 2084 return (S_ERROR); 2085 2086 /* 2087 * Make the .plt section. This occurs after any other relocation 2088 * sections are generated (see reloc_init()) to ensure that the 2089 * associated relocation section is after all the other relocation 2090 * sections. 2091 */ 2092 if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad)) 2093 if (make_plt(ofl) == S_ERROR) 2094 return (S_ERROR); 2095 2096 /* 2097 * Determine whether any sections or files are not referenced. Under 2098 * -Dunused a diagnostic for any unused components is generated, under 2099 * -zignore the component is removed from the final output. 2100 */ 2101 if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) { 2102 if (ignore_section_processing(ofl) == S_ERROR) 2103 return (S_ERROR); 2104 } 2105 2106 /* 2107 * Add any necessary versioning information. 2108 */ 2109 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) { 2110 if (make_verneed(ofl) == S_ERROR) 2111 return (S_ERROR); 2112 } 2113 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) { 2114 if (make_verdef(ofl) == S_ERROR) 2115 return (S_ERROR); 2116 if ((ofl->ofl_osversym = make_sym_sec(ofl, 2117 MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym, 2118 M_ID_VERSION)) == (Os_desc*)S_ERROR) 2119 return (S_ERROR); 2120 } 2121 2122 /* 2123 * Create a syminfo section if necessary. 2124 */ 2125 if (ofl->ofl_flags & FLG_OF_SYMINFO) { 2126 if ((ofl->ofl_ossyminfo = make_sym_sec(ofl, 2127 MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo, 2128 M_ID_SYMINFO)) == (Os_desc *)S_ERROR) 2129 return (S_ERROR); 2130 } 2131 2132 if (ofl->ofl_flags1 & FLG_OF1_RELCNT) { 2133 /* 2134 * If -zcombreloc is enabled then all relocations (except for 2135 * the PLT's) are coalesced into a single relocation section. 2136 */ 2137 if (ofl->ofl_reloccnt) { 2138 if (make_reloc(ofl, NULL) == S_ERROR) 2139 return (S_ERROR); 2140 } 2141 } else { 2142 /* 2143 * Create the required output relocation sections. Note, new 2144 * sections may be added to the section list that is being 2145 * traversed. These insertions can move the elements of the 2146 * Alist such that a section descriptor is re-read. Recursion 2147 * is prevented by maintaining a previous section pointer and 2148 * insuring that this pointer isn't re-examined. 2149 */ 2150 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) { 2151 Os_desc **ospp, *posp = 0; 2152 Aliste off; 2153 2154 for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) { 2155 Os_desc *osp = *ospp; 2156 2157 if ((osp != posp) && osp->os_szoutrels && 2158 (osp != ofl->ofl_osplt)) { 2159 if (make_reloc(ofl, osp) == S_ERROR) 2160 return (S_ERROR); 2161 } 2162 posp = osp; 2163 } 2164 } 2165 2166 /* 2167 * If we're not building a combined relocation section, then 2168 * build a .rel[a] section as required. 2169 */ 2170 if (ofl->ofl_relocrelsz) { 2171 if (make_reloc(ofl, NULL) == S_ERROR) 2172 return (S_ERROR); 2173 } 2174 } 2175 2176 /* 2177 * The PLT relocations are always in their own section, and we try to 2178 * keep them at the end of the PLT table. We do this to keep the hot 2179 * "data" PLT's at the head of the table nearer the .dynsym & .hash. 2180 */ 2181 if (ofl->ofl_osplt && ofl->ofl_relocpltsz) { 2182 if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR) 2183 return (S_ERROR); 2184 } 2185 2186 /* 2187 * Finally build the symbol and section header sections. 2188 */ 2189 if (flags & FLG_OF_DYNAMIC) { 2190 if (make_dynamic(ofl) == S_ERROR) 2191 return (S_ERROR); 2192 if (make_dynstr(ofl) == S_ERROR) 2193 return (S_ERROR); 2194 /* 2195 * There is no use for .hash and .dynsym sections in a 2196 * relocatable object. 2197 */ 2198 if (!(flags & FLG_OF_RELOBJ)) { 2199 if (make_hash(ofl) == S_ERROR) 2200 return (S_ERROR); 2201 if (make_dynsym(ofl) == S_ERROR) 2202 return (S_ERROR); 2203 #if (defined(__i386) || defined(__amd64)) && defined(_ELF64) 2204 if (make_amd64_unwindhdr(ofl) == S_ERROR) 2205 return (S_ERROR); 2206 #endif 2207 if (make_dynsort(ofl) == S_ERROR) 2208 return (S_ERROR); 2209 } 2210 } 2211 2212 if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) || 2213 ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) { 2214 /* 2215 * Do we need to make a SHT_SYMTAB_SHNDX section 2216 * for the dynsym. If so - do it now. 2217 */ 2218 if (ofl->ofl_osdynsym && 2219 ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) { 2220 if (make_dynsym_shndx(ofl) == S_ERROR) 2221 return (S_ERROR); 2222 } 2223 2224 if (make_strtab(ofl) == S_ERROR) 2225 return (S_ERROR); 2226 if (make_symtab(ofl) == S_ERROR) 2227 return (S_ERROR); 2228 } else { 2229 /* 2230 * Do we need to make a SHT_SYMTAB_SHNDX section 2231 * for the dynsym. If so - do it now. 2232 */ 2233 if (ofl->ofl_osdynsym && 2234 ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) { 2235 if (make_dynsym_shndx(ofl) == S_ERROR) 2236 return (S_ERROR); 2237 } 2238 } 2239 2240 if (make_shstrtab(ofl) == S_ERROR) 2241 return (S_ERROR); 2242 2243 /* 2244 * Now that we've created all of our sections adjust the size 2245 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on 2246 * the symbol table sizes. 2247 */ 2248 if (ofl->ofl_osversym || ofl->ofl_ossyminfo) { 2249 Shdr * shdr; 2250 Is_desc * isec; 2251 Elf_Data * data; 2252 size_t size; 2253 ulong_t cnt; 2254 2255 if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) { 2256 isec = (Is_desc *)ofl->ofl_ossymtab-> 2257 os_isdescs.head->data; 2258 } else { 2259 isec = (Is_desc *)ofl->ofl_osdynsym-> 2260 os_isdescs.head->data; 2261 } 2262 cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize); 2263 2264 if (ofl->ofl_osversym) { 2265 isec = (Is_desc *)ofl->ofl_osversym->os_isdescs. 2266 head->data; 2267 data = isec->is_indata; 2268 shdr = ofl->ofl_osversym->os_shdr; 2269 size = cnt * shdr->sh_entsize; 2270 shdr->sh_size = (Xword)size; 2271 data->d_size = size; 2272 } 2273 if (ofl->ofl_ossyminfo) { 2274 isec = (Is_desc *)ofl->ofl_ossyminfo->os_isdescs. 2275 head->data; 2276 data = isec->is_indata; 2277 shdr = ofl->ofl_ossyminfo->os_shdr; 2278 size = cnt * shdr->sh_entsize; 2279 shdr->sh_size = (Xword)size; 2280 data->d_size = size; 2281 } 2282 } 2283 2284 return (1); 2285 } 2286 2287 /* 2288 * Build an additional data section - used to back OBJT symbol definitions 2289 * added with a mapfile. 2290 */ 2291 Is_desc * 2292 ld_make_data(Ofl_desc *ofl, size_t size) 2293 { 2294 Shdr *shdr; 2295 Elf_Data *data; 2296 Is_desc *isec; 2297 2298 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0, 2299 &isec, &shdr, &data) == S_ERROR) 2300 return ((Is_desc *)S_ERROR); 2301 2302 data->d_size = size; 2303 shdr->sh_size = (Xword)size; 2304 shdr->sh_flags |= SHF_WRITE; 2305 2306 if (ld_place_section(ofl, isec, M_ID_DATA, 0) == (Os_desc *)S_ERROR) 2307 return ((Is_desc *)S_ERROR); 2308 2309 return (isec); 2310 } 2311 2312 /* 2313 * Define a set of templates for generating "void (*)(void)" function 2314 * definitions. 2315 */ 2316 #if defined(__i386) || defined(__amd64) 2317 #if defined(__lint) 2318 static const uchar_t ret_template[] = { 0 }; 2319 #else /* __lint */ 2320 #if defined(_ELF64) 2321 #define ret_template ret64_template 2322 #else 2323 #define ret_template ret32_template 2324 #endif 2325 2326 static const uchar_t ret32_template[] = { 2327 /* 0x00 */ 0xc3 /* ret */ 2328 }; 2329 2330 static const uchar_t ret64_template[] = { 2331 /* 0x00 */ 0x55, /* pushq %rbp */ 2332 /* 0x01 */ 0x48, 0x8b, 0xec, /* movq %rsp,%rbp */ 2333 /* 0x04 */ 0x48, 0x8b, 0xe5, /* movq %rbp,%rsp */ 2334 /* 0x07 */ 0x5d, /* popq %rbp */ 2335 /* 0x08 */ 0xc3 /* ret */ 2336 }; 2337 #endif /* __lint */ 2338 2339 #elif defined(__sparc) 2340 static const uchar_t ret_template[] = { 2341 /* 0x00 */ 0x81, 0xc3, 0xe0, 0x08, /* retl */ 2342 /* 0x04 */ 0x01, 0x00, 0x00, 0x00 /* nop */ 2343 }; 2344 #else 2345 #error unsupported architecture! 2346 #endif 2347 2348 /* 2349 * Build an additional text section - used to back FUNC symbol definitions 2350 * added with a mapfile. 2351 */ 2352 Is_desc * 2353 ld_make_text(Ofl_desc *ofl, size_t size) 2354 { 2355 Shdr *shdr; 2356 Elf_Data *data; 2357 Is_desc *isec; 2358 2359 /* 2360 * Insure the size is sufficient to contain the minimum return 2361 * instruction. 2362 */ 2363 if (size < sizeof (ret_template)) 2364 size = sizeof (ret_template); 2365 2366 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0, 2367 &isec, &shdr, &data) == S_ERROR) 2368 return ((Is_desc *)S_ERROR); 2369 2370 data->d_size = size; 2371 shdr->sh_size = (Xword)size; 2372 shdr->sh_flags |= SHF_EXECINSTR; 2373 2374 /* Fill the buffer with the appropriate return instruction. */ 2375 if ((data->d_buf = libld_calloc(size, 1)) == 0) 2376 return ((Is_desc *)S_ERROR); 2377 (void) memcpy(data->d_buf, ret_template, sizeof (ret_template)); 2378 2379 if (ld_place_section(ofl, isec, M_ID_TEXT, 0) == (Os_desc *)S_ERROR) 2380 return ((Is_desc *)S_ERROR); 2381 2382 return (isec); 2383 } 2384