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 2008 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 /* 31 * Module sections. Initialize special sections 32 */ 33 34 #define ELF_TARGET_AMD64 35 36 #include <string.h> 37 #include <strings.h> 38 #include <stdio.h> 39 #include <link.h> 40 #include <debug.h> 41 #include "msg.h" 42 #include "_libld.h" 43 44 inline static void 45 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym) 46 { 47 Sym *sym = sdp->sd_sym; 48 uchar_t type = ELF_ST_TYPE(sym->st_info); 49 /* LINTED - only used for assert() */ 50 int err; 51 52 if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) { 53 ofl->ofl_locscnt--; 54 55 err = st_delstring(ofl->ofl_strtab, sdp->sd_name); 56 assert(err != -1); 57 58 if (allow_ldynsym && ldynsym_symtype[type]) { 59 ofl->ofl_dynlocscnt--; 60 61 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name); 62 assert(err != -1); 63 /* Remove from sort section? */ 64 DYNSORT_COUNT(sdp, sym, type, --); 65 } 66 } 67 sdp->sd_flags |= FLG_SY_ISDISC; 68 } 69 70 inline static void 71 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym) 72 { 73 Sym *sym = sdp->sd_sym; 74 uchar_t type = ELF_ST_TYPE(sym->st_info); 75 /* LINTED - only used for assert() */ 76 int err; 77 78 ofl->ofl_scopecnt--; 79 ofl->ofl_elimcnt++; 80 81 err = st_delstring(ofl->ofl_strtab, sdp->sd_name); 82 assert(err != -1); 83 84 if (allow_ldynsym && ldynsym_symtype[type]) { 85 ofl->ofl_dynscopecnt--; 86 87 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name); 88 assert(err != -1); 89 /* Remove from sort section? */ 90 DYNSORT_COUNT(sdp, sym, type, --); 91 } 92 sdp->sd_flags1 |= FLG_SY1_ELIM; 93 } 94 95 inline static void 96 ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym) 97 { 98 Os_desc *osp; 99 Is_desc *isp = sdp->sd_isc; 100 uchar_t bind = ELF_ST_BIND(sdp->sd_sym->st_info); 101 102 if (bind == STB_LOCAL) { 103 uchar_t type = ELF_ST_TYPE(sdp->sd_sym->st_info); 104 105 /* 106 * Skip section symbols, these were never collected in the 107 * first place. 108 */ 109 if (type == STT_SECTION) 110 return; 111 112 /* 113 * Determine if the whole file is being removed. Remove any 114 * file symbol, and any symbol that is not associated with a 115 * section, provided the symbol has not been identified as 116 * (update) required. 117 */ 118 if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) && 119 ((type == STT_FILE) || ((isp == NULL) && 120 ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) { 121 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 122 if (ifl->ifl_flags & FLG_IF_IGNORE) 123 remove_local(ofl, sdp, allow_ldynsym); 124 return; 125 } 126 127 } else { 128 /* 129 * Global symbols can only be eliminated when the interfaces of 130 * an object have been defined via versioning/scoping. 131 */ 132 if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) 133 return; 134 135 /* 136 * Remove any unreferenced symbols that are not associated with 137 * a section. 138 */ 139 if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) { 140 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 141 if (ifl->ifl_flags & FLG_IF_IGNORE) 142 remove_scoped(ofl, sdp, allow_ldynsym); 143 return; 144 } 145 } 146 147 /* 148 * Do not discard any symbols that are associated with non-allocable 149 * segments. 150 */ 151 if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) && 152 ((osp = isp->is_osdesc) != 0) && 153 (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) { 154 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 155 if (ifl->ifl_flags & FLG_IF_IGNORE) { 156 if (bind == STB_LOCAL) 157 remove_local(ofl, sdp, allow_ldynsym); 158 else 159 remove_scoped(ofl, sdp, allow_ldynsym); 160 } 161 } 162 } 163 164 /* 165 * If -zignore has been in effect, scan all input files to determine if the 166 * file, or sections from the file, have been referenced. If not, the file or 167 * some of the files sections can be discarded. 168 * 169 * which haven't been referenced (and hence can be discarded). If sections are 170 * to be discarded, rescan the output relocations and the symbol table and 171 * remove the relocations and symbol entries that are no longer required. 172 * 173 * Note: It's possible that a section which is being discarded has contributed 174 * to the GOT table or the PLT table. However, we can't at this point 175 * eliminate the corresponding entries. This is because there could well 176 * be other sections referencing those same entries, but we don't have 177 * the infrastructure to determine this. So, keep the PLT and GOT 178 * entries in the table in case someone wants them. 179 * Note: The section to be affected needs to be allocatable. 180 * So even if -zignore is in effect, if the section is not allocatable, 181 * we do not eliminate it. 182 */ 183 static uintptr_t 184 ignore_section_processing(Ofl_desc *ofl) 185 { 186 Listnode *lnp; 187 Ifl_desc *ifl; 188 Rel_cache *rcp; 189 int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 190 191 for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, ifl)) { 192 uint_t num, discard; 193 194 /* 195 * Diagnose (-D unused) a completely unreferenced file. 196 */ 197 if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0) 198 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, 199 ifl->ifl_name, 0, 0)); 200 if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) || 201 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0)) 202 continue; 203 204 /* 205 * Before scanning the whole symbol table to determine if 206 * symbols should be discard - quickly (relatively) scan the 207 * sections to determine if any are to be discarded. 208 */ 209 discard = 0; 210 if (ifl->ifl_flags & FLG_IF_FILEREF) { 211 for (num = 1; num < ifl->ifl_shnum; num++) { 212 Is_desc *isp = ifl->ifl_isdesc[num]; 213 Os_desc *osp; 214 Sg_desc *sgp; 215 216 if (((isp = ifl->ifl_isdesc[num]) != 0) && 217 ((isp->is_flags & FLG_IS_SECTREF) == 0) && 218 ((osp = isp->is_osdesc) != 0) && 219 ((sgp = osp->os_sgdesc) != 0) && 220 (sgp->sg_phdr.p_type == PT_LOAD)) { 221 discard++; 222 break; 223 } 224 } 225 } 226 227 /* 228 * No sections are to be 'ignored' 229 */ 230 if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF)) 231 continue; 232 233 /* 234 * We know that we have discarded sections. Scan the symbol 235 * table for this file to determine if symbols need to be 236 * discarded that are associated with the 'ignored' sections. 237 */ 238 for (num = 1; num < ifl->ifl_symscnt; num++) { 239 Sym_desc *sdp; 240 241 /* 242 * If the symbol definition has been resolved to another 243 * file, or the symbol has already been discarded or 244 * eliminated, skip it. 245 */ 246 sdp = ifl->ifl_oldndx[num]; 247 if ((sdp->sd_file != ifl) || 248 (sdp->sd_flags & (FLG_SY_ISDISC|FLG_SY_INVALID)) || 249 (sdp->sd_flags1 & FLG_SY1_ELIM)) 250 continue; 251 252 /* 253 * Complete the investigation of the symbol. 254 */ 255 ignore_sym(ofl, ifl, sdp, allow_ldynsym); 256 } 257 } 258 259 /* 260 * If we were only here to solicit debugging diagnostics, we're done. 261 */ 262 if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) 263 return (1); 264 265 /* 266 * Scan all output relocations searching for those against discarded or 267 * ignored sections. If one is found, decrement the total outrel count. 268 */ 269 for (LIST_TRAVERSE(&ofl->ofl_outrels, lnp, rcp)) { 270 Rel_desc *rsp; 271 Os_desc *osp; 272 273 /* LINTED */ 274 for (rsp = (Rel_desc *)(rcp + 1); rsp < rcp->rc_free; rsp++) { 275 Is_desc *isc = rsp->rel_isdesc; 276 uint_t flags, entsize; 277 Shdr *shdr; 278 Ifl_desc *ifl; 279 280 if ((isc == 0) || 281 ((isc->is_flags & (FLG_IS_SECTREF))) || 282 ((ifl = isc->is_file) == 0) || 283 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) || 284 ((shdr = isc->is_shdr) == 0) || 285 ((shdr->sh_flags & SHF_ALLOC) == 0)) 286 continue; 287 288 flags = rsp->rel_flags; 289 290 if (flags & (FLG_REL_GOT | FLG_REL_BSS | 291 FLG_REL_NOINFO | FLG_REL_PLT)) 292 continue; 293 294 osp = rsp->rel_osdesc; 295 296 if (rsp->rel_flags & FLG_REL_RELA) 297 entsize = sizeof (Rela); 298 else 299 entsize = sizeof (Rel); 300 301 assert(osp->os_szoutrels > 0); 302 osp->os_szoutrels -= entsize; 303 304 if (!(flags & FLG_REL_PLT)) 305 ofl->ofl_reloccntsub++; 306 307 if (rsp->rel_rtype == ld_targ.t_m.m_r_relative) 308 ofl->ofl_relocrelcnt--; 309 } 310 } 311 return (1); 312 } 313 314 /* 315 * Allocate Elf_Data, Shdr, and Is_desc structures for a new 316 * section. 317 * 318 * entry: 319 * ofl - Output file descriptor 320 * shtype - SHT_ type code for section. 321 * shname - String giving the name for the new section. 322 * entcnt - # of items contained in the data part of the new section. 323 * This value is multiplied against the known element size 324 * for the section type to determine the size of the data 325 * area for the section. It is only meaningful in cases where 326 * the section type has a non-zero element size. In other cases, 327 * the caller must set the size fields in the *ret_data and 328 * *ret_shdr structs manually. 329 * ret_isec, ret_shdr, ret_data - Address of pointers to 330 * receive address of newly allocated structs. 331 * 332 * exit: 333 * On error, returns S_ERROR. On success, returns (1), and the 334 * ret_ pointers have been updated to point at the new structures, 335 * which have been filled in. To finish the task, the caller must 336 * update any fields within the supplied descriptors that differ 337 * from its needs, and then call ld_place_section(). 338 */ 339 static uintptr_t 340 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt, 341 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data) 342 { 343 typedef struct sec_info { 344 Word d_type; 345 Word align; /* Used in both data and section header */ 346 Word sh_flags; 347 Word sh_entsize; 348 } SEC_INFO_T; 349 350 const SEC_INFO_T *sec_info; 351 352 Shdr *shdr; 353 Elf_Data *data; 354 Is_desc *isec; 355 size_t size; 356 357 /* 358 * For each type of section, we have a distinct set of 359 * SEC_INFO_T values. This macro defines a static structure 360 * containing those values and generates code to set the sec_info 361 * pointer to refer to it. The pointer in sec_info remains valid 362 * outside of the declaration scope because the info_s struct is static. 363 * 364 * We can't determine the value of M_WORD_ALIGN at compile time, so 365 * a different variant is used for those cases. 366 */ 367 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \ 368 { \ 369 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \ 370 sh_entsize}; \ 371 sec_info = &info_s; \ 372 } 373 #define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \ 374 { \ 375 static SEC_INFO_T info_s = { d_type, 0, sh_flags, \ 376 sh_entsize}; \ 377 info_s.align = ld_targ.t_m.m_word_align; \ 378 sec_info = &info_s; \ 379 } 380 381 switch (shtype) { 382 case SHT_PROGBITS: 383 /* 384 * SHT_PROGBITS sections contain are used for many 385 * different sections. Alignments and flags differ. 386 * Some have a standard entsize, and others don't. 387 * We set some defaults here, but there is no expectation 388 * that they are correct or complete for any specific 389 * purpose. The caller must provide the correct values. 390 */ 391 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0) 392 break; 393 394 case SHT_SYMTAB: 395 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym)) 396 break; 397 398 case SHT_DYNSYM: 399 case SHT_SUNW_LDYNSYM: 400 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym)) 401 break; 402 403 case SHT_STRTAB: 404 /* 405 * A string table may or may not be allocable, depending 406 * on context, so we leave that flag unset and leave it to 407 * the caller to add it if necessary. 408 * 409 * String tables do not have a standard entsize, so 410 * we set it to 0. 411 */ 412 SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0) 413 break; 414 415 case SHT_RELA: 416 /* 417 * Relocations with an addend (Everything except 32-bit X86). 418 * The caller is expected to set all section header flags. 419 */ 420 SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela)) 421 break; 422 423 case SHT_REL: 424 /* 425 * Relocations without an addend (32-bit X86 only). 426 * The caller is expected to set all section header flags. 427 */ 428 SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel)) 429 break; 430 431 case SHT_HASH: 432 case SHT_SUNW_symsort: 433 case SHT_SUNW_tlssort: 434 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word)) 435 break; 436 437 case SHT_DYNAMIC: 438 /* 439 * A dynamic section may or may not be allocable, depending 440 * on context, so we leave that flag unset and leave it to 441 * the caller to add it if necessary. 442 */ 443 SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, SHF_WRITE, sizeof (Dyn)) 444 break; 445 446 case SHT_NOBITS: 447 /* 448 * SHT_NOBITS is used for BSS-type sections. The size and 449 * alignment depend on the specific use and must be adjusted 450 * by the caller. 451 */ 452 SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0) 453 break; 454 455 case SHT_INIT_ARRAY: 456 case SHT_FINI_ARRAY: 457 case SHT_PREINIT_ARRAY: 458 SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE, 459 sizeof (Addr)) 460 break; 461 462 case SHT_SYMTAB_SHNDX: 463 /* 464 * Note that these sections are created to be associated 465 * with both symtab and dynsym symbol tables. However, they 466 * are non-allocable in all cases, because the runtime 467 * linker has no need for this information. It is purely 468 * informational, used by elfdump(1), debuggers, etc. 469 */ 470 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word)); 471 break; 472 473 case SHT_SUNW_cap: 474 SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap)); 475 break; 476 477 case SHT_SUNW_move: 478 /* 479 * The sh_info field of the SHT_*_syminfo section points 480 * to the header index of the associated .dynamic section, 481 * so we also set SHF_INFO_LINK. 482 */ 483 SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword), 484 SHF_ALLOC | SHF_WRITE, sizeof (Move)); 485 break; 486 487 case SHT_SUNW_syminfo: 488 /* 489 * The sh_info field of the SHT_*_syminfo section points 490 * to the header index of the associated .dynamic section, 491 * so we also set SHF_INFO_LINK. 492 */ 493 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, 494 SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo)); 495 break; 496 497 case SHT_SUNW_verneed: 498 case SHT_SUNW_verdef: 499 /* 500 * The info for verneed and versym happen to be the same. 501 * The entries in these sections are not of uniform size, 502 * so we set the entsize to 0. 503 */ 504 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0); 505 break; 506 507 case SHT_SUNW_versym: 508 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 509 sizeof (Versym)); 510 break; 511 512 default: 513 /* Should not happen: fcn called with unknown section type */ 514 assert(0); 515 return (S_ERROR); 516 } 517 #undef SET_SEC_INFO 518 #undef SET_SEC_INFO_WORD_ALIGN 519 520 size = entcnt * sec_info->sh_entsize; 521 522 /* 523 * Allocate and initialize the Elf_Data structure. 524 */ 525 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0) 526 return (S_ERROR); 527 data->d_type = sec_info->d_type; 528 data->d_size = size; 529 data->d_align = sec_info->align; 530 data->d_version = ofl->ofl_dehdr->e_version; 531 532 /* 533 * Allocate and initialize the Shdr structure. 534 */ 535 if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0) 536 return (S_ERROR); 537 shdr->sh_type = shtype; 538 shdr->sh_size = size; 539 shdr->sh_flags = sec_info->sh_flags; 540 shdr->sh_addralign = sec_info->align; 541 shdr->sh_entsize = sec_info->sh_entsize; 542 543 /* 544 * Allocate and initialize the Is_desc structure. 545 */ 546 if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0) 547 return (S_ERROR); 548 isec->is_name = shname; 549 isec->is_shdr = shdr; 550 isec->is_indata = data; 551 552 553 *ret_isec = isec; 554 *ret_shdr = shdr; 555 *ret_data = data; 556 return (1); 557 } 558 559 /* 560 * Use an existing input section as a template to create a new 561 * input section with the same values as the original, other than 562 * the size of the data area which is supplied by the caller. 563 * 564 * entry: 565 * ofl - Output file descriptor 566 * ifl - Input file section to use as a template 567 * size - Size of data area for new section 568 * ret_isec, ret_shdr, ret_data - Address of pointers to 569 * receive address of newly allocated structs. 570 * 571 * exit: 572 * On error, returns S_ERROR. On success, returns (1), and the 573 * ret_ pointers have been updated to point at the new structures, 574 * which have been filled in. To finish the task, the caller must 575 * update any fields within the supplied descriptors that differ 576 * from its needs, and then call ld_place_section(). 577 */ 578 static uintptr_t 579 new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size, 580 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data) 581 { 582 Shdr *shdr; 583 Elf_Data *data; 584 Is_desc *isec; 585 586 /* 587 * Allocate and initialize the Elf_Data structure. 588 */ 589 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0) 590 return (S_ERROR); 591 data->d_type = tmpl_isp->is_indata->d_type; 592 data->d_size = size; 593 data->d_align = tmpl_isp->is_shdr->sh_addralign; 594 data->d_version = ofl->ofl_dehdr->e_version; 595 596 /* 597 * Allocate and initialize the Shdr structure. 598 */ 599 if ((shdr = libld_malloc(sizeof (Shdr))) == 0) 600 return (S_ERROR); 601 *shdr = *tmpl_isp->is_shdr; 602 shdr->sh_addr = 0; 603 shdr->sh_offset = 0; 604 shdr->sh_size = size; 605 606 /* 607 * Allocate and initialize the Is_desc structure. 608 */ 609 if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0) 610 return (S_ERROR); 611 isec->is_name = tmpl_isp->is_name; 612 isec->is_shdr = shdr; 613 isec->is_indata = data; 614 615 616 *ret_isec = isec; 617 *ret_shdr = shdr; 618 *ret_data = data; 619 return (1); 620 } 621 622 /* 623 * Build a .bss section for allocation of tentative definitions. Any `static' 624 * .bss definitions would have been associated to their own .bss sections and 625 * thus collected from the input files. `global' .bss definitions are tagged 626 * as COMMON and do not cause any associated .bss section elements to be 627 * generated. Here we add up all these COMMON symbols and generate the .bss 628 * section required to represent them. 629 */ 630 uintptr_t 631 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, Bss_Type which) 632 { 633 Shdr *shdr; 634 Elf_Data *data; 635 Is_desc *isec; 636 Os_desc *osp; 637 uint_t ident; 638 Xword rsize = (Xword)ofl->ofl_relocbsssz; 639 640 /* 641 * Allocate header structs. We will set the name ourselves below, 642 * and there is no entcnt for a BSS. So, the shname and entcnt 643 * arguments are 0. 644 */ 645 if (new_section(ofl, SHT_NOBITS, NULL, 0, 646 &isec, &shdr, &data) == S_ERROR) 647 return (S_ERROR); 648 649 data->d_size = (size_t)size; 650 data->d_align = (size_t)align; 651 652 shdr->sh_size = size; 653 shdr->sh_addralign = align; 654 655 if (which == MAKE_TLS) { 656 isec->is_name = MSG_ORIG(MSG_SCN_TBSS); 657 ident = ld_targ.t_id.id_tlsbss; 658 ofl->ofl_istlsbss = isec; 659 shdr->sh_flags |= SHF_TLS; 660 661 } else if (which == MAKE_BSS) { 662 isec->is_name = MSG_ORIG(MSG_SCN_BSS); 663 ofl->ofl_isbss = isec; 664 ident = ld_targ.t_id.id_bss; 665 666 #if defined(_ELF64) 667 } else if ((ld_targ.t_m.m_mach == EM_AMD64) && (which == MAKE_LBSS)) { 668 isec->is_name = MSG_ORIG(MSG_SCN_LBSS); 669 ofl->ofl_islbss = isec; 670 ident = ld_targ.t_id.id_lbss; 671 shdr->sh_flags |= SHF_AMD64_LARGE; 672 #endif 673 } 674 675 /* 676 * Retain this .bss input section as this will be where global 677 * symbol references are added. 678 */ 679 if ((osp = ld_place_section(ofl, isec, ident, 0)) == (Os_desc *)S_ERROR) 680 return (S_ERROR); 681 682 /* 683 * If relocations exist against .*bss section, a 684 * section symbol must be created for the section in 685 * the .dynsym symbol table. 686 */ 687 if (!(osp->os_flags & FLG_OS_OUTREL)) { 688 ofl_flag_t flagtotest; 689 if (which == MAKE_TLS) 690 flagtotest = FLG_OF1_TLSOREL; 691 else 692 flagtotest = FLG_OF1_BSSOREL; 693 694 if (ofl->ofl_flags1 & flagtotest) { 695 ofl->ofl_dynshdrcnt++; 696 osp->os_flags |= FLG_OS_OUTREL; 697 } 698 } 699 700 osp->os_szoutrels = rsize; 701 702 return (1); 703 } 704 705 706 /* 707 * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via 708 * ld -z *array=name 709 */ 710 static uintptr_t 711 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, List *list) 712 { 713 uint_t entcount; 714 Listnode *lnp; 715 Elf_Data *data; 716 Is_desc *isec; 717 Shdr *shdr; 718 Sym_desc *sdp; 719 Rel_desc reld; 720 Rela reloc; 721 Os_desc *osp; 722 uintptr_t ret = 1; 723 724 if (list->head == NULL) 725 return (1); 726 727 entcount = 0; 728 for (LIST_TRAVERSE(list, lnp, sdp)) 729 entcount++; 730 731 if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) == 732 S_ERROR) 733 return (S_ERROR); 734 735 if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == 0) 736 return (S_ERROR); 737 738 if (ld_place_section(ofl, isec, ld_targ.t_id.id_array, 0) == 739 (Os_desc *)S_ERROR) 740 return (S_ERROR); 741 742 osp = isec->is_osdesc; 743 744 if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY)) 745 ofl->ofl_osinitarray = osp; 746 if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY)) 747 ofl->ofl_ospreinitarray = osp; 748 else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY)) 749 ofl->ofl_osfiniarray = osp; 750 751 /* 752 * Create relocations against this section to initialize it to the 753 * function addresses. 754 */ 755 reld.rel_osdesc = osp; 756 reld.rel_isdesc = isec; 757 reld.rel_move = 0; 758 reld.rel_flags = FLG_REL_LOAD; 759 760 /* 761 * Fabricate the relocation information (as if a relocation record had 762 * been input - see init_rel()). 763 */ 764 reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr; 765 reld.rel_roffset = 0; 766 reld.rel_raddend = 0; 767 reld.rel_typedata = 0; 768 769 /* 770 * Create a minimal relocation record to satisfy process_sym_reloc() 771 * debugging requirements. 772 */ 773 reloc.r_offset = 0; 774 reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr); 775 reloc.r_addend = 0; 776 777 DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp, 778 ld_targ.t_m.m_rel_sht_type)); 779 for (LIST_TRAVERSE(list, lnp, sdp)) { 780 reld.rel_sname = sdp->sd_name; 781 reld.rel_sym = sdp; 782 783 if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec, 784 MSG_INTL(MSG_STR_COMMAND)) == S_ERROR) { 785 ret = S_ERROR; 786 continue; 787 } 788 789 reld.rel_roffset += (Xword)sizeof (Addr); 790 reloc.r_offset = reld.rel_roffset; 791 } 792 793 return (ret); 794 } 795 796 /* 797 * Build a comment section (-Qy option). 798 */ 799 static uintptr_t 800 make_comment(Ofl_desc *ofl) 801 { 802 Shdr *shdr; 803 Elf_Data *data; 804 Is_desc *isec; 805 806 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0, 807 &isec, &shdr, &data) == S_ERROR) 808 return (S_ERROR); 809 810 data->d_buf = (void *)ofl->ofl_sgsid; 811 data->d_size = strlen(ofl->ofl_sgsid) + 1; 812 data->d_align = 1; 813 814 shdr->sh_size = (Xword)data->d_size; 815 shdr->sh_flags = 0; 816 shdr->sh_addralign = 1; 817 818 return ((uintptr_t)ld_place_section(ofl, isec, 819 ld_targ.t_id.id_note, 0)); 820 } 821 822 /* 823 * Make the dynamic section. Calculate the size of any strings referenced 824 * within this structure, they will be added to the global string table 825 * (.dynstr). This routine should be called before make_dynstr(). 826 * 827 * This routine must be maintained in parallel with update_odynamic() 828 * in update.c 829 */ 830 static uintptr_t 831 make_dynamic(Ofl_desc *ofl) 832 { 833 Shdr *shdr; 834 Os_desc *osp; 835 Elf_Data *data; 836 Is_desc *isec; 837 size_t cnt = 0; 838 Listnode *lnp; 839 Ifl_desc *ifl; 840 Sym_desc *sdp; 841 size_t size; 842 ofl_flag_t flags = ofl->ofl_flags; 843 int not_relobj = !(flags & FLG_OF_RELOBJ); 844 int unused = 0; 845 846 /* 847 * Only a limited subset of DT_ entries apply to relocatable 848 * objects. See the comment at the head of update_odynamic() in 849 * update.c for details. 850 */ 851 if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0, 852 &isec, &shdr, &data) == S_ERROR) 853 return (S_ERROR); 854 855 /* new_section() does not set SHF_ALLOC. Add it if needed */ 856 if (not_relobj) 857 shdr->sh_flags |= SHF_ALLOC; 858 859 osp = ofl->ofl_osdynamic = 860 ld_place_section(ofl, isec, ld_targ.t_id.id_dynamic, 0); 861 862 /* 863 * Reserve entries for any needed dependencies. 864 */ 865 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) { 866 Sdf_desc *sdf; 867 868 if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))) 869 continue; 870 871 /* 872 * If this dependency didn't satisfy any symbol references, 873 * generate a debugging diagnostic (ld(1) -Dunused can be used 874 * to display these). If this is a standard needed dependency, 875 * and -z ignore is in effect, drop the dependency. Explicitly 876 * defined dependencies (i.e., -N dep) don't get dropped, and 877 * are flagged as being required to simplify update_odynamic() 878 * processing. 879 */ 880 if ((ifl->ifl_flags & FLG_IF_NEEDSTR) || 881 ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) { 882 if (unused++ == 0) 883 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 884 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname, 885 (ifl->ifl_flags & FLG_IF_NEEDSTR), 0)); 886 887 if (ifl->ifl_flags & FLG_IF_NEEDSTR) 888 ifl->ifl_flags |= FLG_IF_DEPREQD; 889 else if (ifl->ifl_flags & FLG_IF_IGNORE) 890 continue; 891 } 892 893 /* 894 * If this object has an accompanying shared object definition 895 * determine if an alternative shared object name has been 896 * specified. 897 */ 898 if (((sdf = ifl->ifl_sdfdesc) != 0) && 899 (sdf->sdf_flags & FLG_SDF_SONAME)) 900 ifl->ifl_soname = sdf->sdf_soname; 901 902 /* 903 * If this object is a lazyload reserve a DT_POSFLAG_1 entry. 904 */ 905 if ((ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM)) && 906 not_relobj) 907 cnt++; 908 909 if (st_insert(ofl->ofl_dynstrtab, ifl->ifl_soname) == -1) 910 return (S_ERROR); 911 cnt++; 912 913 /* 914 * If the needed entry contains the $ORIGIN token make sure 915 * the associated DT_1_FLAGS entry is created. 916 */ 917 if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) { 918 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 919 ofl->ofl_dtflags |= DF_ORIGIN; 920 } 921 } 922 923 if (unused) 924 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 925 926 if (not_relobj) { 927 /* 928 * Reserve entries for any per-symbol auxiliary/filter strings. 929 */ 930 cnt += alist_nitems(ofl->ofl_dtsfltrs); 931 932 /* 933 * Reserve entries for _init() and _fini() section addresses. 934 */ 935 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), 936 SYM_NOHASH, 0, ofl)) != NULL) && 937 (sdp->sd_ref == REF_REL_NEED) && 938 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 939 sdp->sd_flags |= FLG_SY_UPREQD; 940 cnt++; 941 } 942 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U), 943 SYM_NOHASH, 0, ofl)) != NULL) && 944 (sdp->sd_ref == REF_REL_NEED) && 945 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 946 sdp->sd_flags |= FLG_SY_UPREQD; 947 cnt++; 948 } 949 950 /* 951 * Reserve entries for any soname, filter name (shared libs 952 * only), run-path pointers, cache names and audit requirements. 953 */ 954 if (ofl->ofl_soname) { 955 cnt++; 956 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_soname) == 957 -1) 958 return (S_ERROR); 959 } 960 if (ofl->ofl_filtees) { 961 cnt++; 962 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_filtees) == 963 -1) 964 return (S_ERROR); 965 966 /* 967 * If the filtees entry contains the $ORIGIN token 968 * make sure the associated DT_1_FLAGS entry is created. 969 */ 970 if (strstr(ofl->ofl_filtees, 971 MSG_ORIG(MSG_STR_ORIGIN))) { 972 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 973 ofl->ofl_dtflags |= DF_ORIGIN; 974 } 975 } 976 } 977 978 if (ofl->ofl_rpath) { 979 cnt += 2; /* DT_RPATH & DT_RUNPATH */ 980 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_rpath) == -1) 981 return (S_ERROR); 982 983 /* 984 * If the rpath entry contains the $ORIGIN token make sure 985 * the associated DT_1_FLAGS entry is created. 986 */ 987 if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) { 988 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 989 ofl->ofl_dtflags |= DF_ORIGIN; 990 } 991 } 992 993 if (not_relobj) { 994 if (ofl->ofl_config) { 995 cnt++; 996 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_config) == 997 -1) 998 return (S_ERROR); 999 1000 /* 1001 * If the config entry contains the $ORIGIN token 1002 * make sure the associated DT_1_FLAGS entry is created. 1003 */ 1004 if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) { 1005 ofl->ofl_dtflags_1 |= DF_1_ORIGIN; 1006 ofl->ofl_dtflags |= DF_ORIGIN; 1007 } 1008 } 1009 if (ofl->ofl_depaudit) { 1010 cnt++; 1011 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_depaudit) == 1012 -1) 1013 return (S_ERROR); 1014 } 1015 if (ofl->ofl_audit) { 1016 cnt++; 1017 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_audit) == -1) 1018 return (S_ERROR); 1019 } 1020 1021 /* 1022 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT, 1023 * and CHECKSUM. 1024 */ 1025 cnt += 6; 1026 1027 /* 1028 * If we are including local functions at the head of 1029 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB 1030 * and DT_SUNW_SYMSZ. 1031 */ 1032 if (OFL_ALLOW_LDYNSYM(ofl)) 1033 cnt += 2; 1034 1035 if ((ofl->ofl_dynsymsortcnt > 0) || 1036 (ofl->ofl_dyntlssortcnt > 0)) 1037 cnt++; /* DT_SUNW_SORTENT */ 1038 1039 if (ofl->ofl_dynsymsortcnt > 0) 1040 cnt += 2; /* DT_SUNW_[SYMSORT|SYMSORTSZ] */ 1041 1042 if (ofl->ofl_dyntlssortcnt > 0) 1043 cnt += 2; /* DT_SUNW_[TLSSORT|TLSSORTSZ] */ 1044 1045 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == 1046 FLG_OF_VERDEF) 1047 cnt += 2; /* DT_VERDEF & DT_VERDEFNUM */ 1048 1049 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == 1050 FLG_OF_VERNEED) 1051 cnt += 2; /* DT_VERNEED & DT_VERNEEDNUM */ 1052 1053 if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt) 1054 cnt++; /* RELACOUNT */ 1055 1056 if (flags & FLG_OF_TEXTREL) /* TEXTREL */ 1057 cnt++; 1058 1059 if (ofl->ofl_osfiniarray) /* FINI_ARRAY & FINI_ARRAYSZ */ 1060 cnt += 2; 1061 1062 if (ofl->ofl_osinitarray) /* INIT_ARRAY & INIT_ARRAYSZ */ 1063 cnt += 2; 1064 1065 if (ofl->ofl_ospreinitarray) /* PREINIT_ARRAY & */ 1066 cnt += 2; /* PREINIT_ARRAYSZ */ 1067 1068 /* 1069 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL. 1070 */ 1071 if (ofl->ofl_pltcnt) 1072 cnt += 3; 1073 1074 /* 1075 * If pltpadding is needed (Sparcv9) 1076 */ 1077 if (ofl->ofl_pltpad) 1078 cnt += 2; /* DT_PLTPAD & DT_PLTPADSZ */ 1079 1080 /* 1081 * If we have any relocations reserve a REL, RELSZ and 1082 * RELENT entry. 1083 */ 1084 if (ofl->ofl_relocsz) 1085 cnt += 3; 1086 1087 /* 1088 * If a syminfo section is required create SYMINFO, SYMINSZ, 1089 * and SYMINENT entries. 1090 */ 1091 if (flags & FLG_OF_SYMINFO) 1092 cnt += 3; 1093 1094 /* 1095 * If there are any partially initialized sections allocate 1096 * MOVEENT, MOVESZ and MOVETAB. 1097 */ 1098 if (ofl->ofl_osmove) 1099 cnt += 3; 1100 1101 /* 1102 * Allocate one DT_REGISTER entry for every register symbol. 1103 */ 1104 cnt += ofl->ofl_regsymcnt; 1105 1106 /* 1107 * Reserve a entry for each '-zrtldinfo=...' specified 1108 * on the command line. 1109 */ 1110 for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp)) 1111 cnt++; 1112 1113 /* 1114 * These two entries should only be placed in a segment 1115 * which is writable. If it's a read-only segment 1116 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate 1117 * these entries. 1118 */ 1119 if ((osp->os_sgdesc) && 1120 (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) { 1121 cnt++; /* FEATURE_1 */ 1122 1123 if (ofl->ofl_osinterp) 1124 cnt++; /* DEBUG */ 1125 } 1126 1127 /* 1128 * Any hardware/software capabilities? 1129 */ 1130 if (ofl->ofl_oscap) 1131 cnt++; /* SUNW_CAP */ 1132 1133 if (flags & FLG_OF_SYMBOLIC) 1134 cnt++; /* SYMBOLIC */ 1135 } 1136 1137 /* 1138 * Account for Architecture dependent .dynamic entries, and defaults. 1139 */ 1140 (*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt); 1141 1142 /* 1143 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also, 1144 * allow room for the unused extra DT_NULLs. These are included 1145 * to allow an ELF editor room to add items later. 1146 */ 1147 cnt += 4 + DYNAMIC_EXTRA_ELTS; 1148 1149 /* 1150 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the 1151 * linker that produced the output object. This information 1152 * allows us to determine whether a given object was linked 1153 * natively, or by a linker running on a different type of 1154 * system. This information can be valuable if one suspects 1155 * that a problem might be due to alignment or byte order issues. 1156 */ 1157 cnt++; 1158 1159 /* 1160 * Determine the size of the section from the number of entries. 1161 */ 1162 size = cnt * (size_t)shdr->sh_entsize; 1163 1164 shdr->sh_size = (Xword)size; 1165 data->d_size = size; 1166 1167 return ((uintptr_t)ofl->ofl_osdynamic); 1168 } 1169 1170 /* 1171 * Build the GOT section and its associated relocation entries. 1172 */ 1173 uintptr_t 1174 ld_make_got(Ofl_desc *ofl) 1175 { 1176 Elf_Data *data; 1177 Shdr *shdr; 1178 Is_desc *isec; 1179 size_t size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize; 1180 size_t rsize = (size_t)ofl->ofl_relocgotsz; 1181 1182 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0, 1183 &isec, &shdr, &data) == S_ERROR) 1184 return (S_ERROR); 1185 1186 data->d_size = size; 1187 1188 shdr->sh_flags |= SHF_WRITE; 1189 shdr->sh_size = (Xword)size; 1190 shdr->sh_entsize = ld_targ.t_m.m_got_entsize; 1191 1192 ofl->ofl_osgot = ld_place_section(ofl, isec, ld_targ.t_id.id_got, 0); 1193 if (ofl->ofl_osgot == (Os_desc *)S_ERROR) 1194 return (S_ERROR); 1195 1196 ofl->ofl_osgot->os_szoutrels = (Xword)rsize; 1197 1198 return (1); 1199 } 1200 1201 /* 1202 * Build an interpreter section. 1203 */ 1204 static uintptr_t 1205 make_interp(Ofl_desc *ofl) 1206 { 1207 Shdr *shdr; 1208 Elf_Data *data; 1209 Is_desc *isec; 1210 const char *iname = ofl->ofl_interp; 1211 size_t size; 1212 1213 /* 1214 * If -z nointerp is in effect, don't create an interpreter section. 1215 */ 1216 if (ofl->ofl_flags1 & FLG_OF1_NOINTRP) 1217 return (1); 1218 1219 /* 1220 * We always build an .interp section for dynamic executables. However 1221 * if the user has specifically specified an interpreter we'll build 1222 * this section for any output (presumably the user knows what they are 1223 * doing. refer ABI section 5-4, and ld.1 man page use of -I). 1224 */ 1225 if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC | 1226 FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname) 1227 return (1); 1228 1229 /* 1230 * In the case of a dynamic executable supply a default interpreter 1231 * if a specific interpreter has not been specified. 1232 */ 1233 if (iname == NULL) 1234 iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp; 1235 1236 size = strlen(iname) + 1; 1237 1238 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0, 1239 &isec, &shdr, &data) == S_ERROR) 1240 return (S_ERROR); 1241 1242 data->d_size = size; 1243 shdr->sh_size = (Xword)size; 1244 data->d_align = shdr->sh_addralign = 1; 1245 1246 ofl->ofl_osinterp = 1247 ld_place_section(ofl, isec, ld_targ.t_id.id_interp, 0); 1248 return ((uintptr_t)ofl->ofl_osinterp); 1249 } 1250 1251 /* 1252 * Build a hardware/software capabilities section. 1253 */ 1254 static uintptr_t 1255 make_cap(Ofl_desc *ofl) 1256 { 1257 Shdr *shdr; 1258 Elf_Data *data; 1259 Is_desc *isec; 1260 Os_desc *osec; 1261 Cap *cap; 1262 size_t size = 0; 1263 1264 /* 1265 * Determine how many entries are required. 1266 */ 1267 if (ofl->ofl_hwcap_1) 1268 size++; 1269 if (ofl->ofl_sfcap_1) 1270 size++; 1271 if (size == 0) 1272 return (1); 1273 size++; /* Add CA_SUNW_NULL */ 1274 1275 if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size, 1276 &isec, &shdr, &data) == S_ERROR) 1277 return (S_ERROR); 1278 1279 if ((data->d_buf = libld_malloc(shdr->sh_size)) == 0) 1280 return (S_ERROR); 1281 1282 cap = (Cap *)data->d_buf; 1283 if (ofl->ofl_hwcap_1) { 1284 cap->c_tag = CA_SUNW_HW_1; 1285 cap->c_un.c_val = ofl->ofl_hwcap_1; 1286 cap++; 1287 } 1288 if (ofl->ofl_sfcap_1) { 1289 cap->c_tag = CA_SUNW_SF_1; 1290 cap->c_un.c_val = ofl->ofl_sfcap_1; 1291 cap++; 1292 } 1293 cap->c_tag = CA_SUNW_NULL; 1294 cap->c_un.c_val = 0; 1295 1296 /* 1297 * If we're not creating a relocatable object, save the output section 1298 * to trigger the creation of an associated program header. 1299 */ 1300 osec = ld_place_section(ofl, isec, ld_targ.t_id.id_cap, 0); 1301 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) 1302 ofl->ofl_oscap = osec; 1303 1304 return ((uintptr_t)osec); 1305 } 1306 1307 /* 1308 * Build the PLT section and its associated relocation entries. 1309 */ 1310 static uintptr_t 1311 make_plt(Ofl_desc *ofl) 1312 { 1313 Shdr *shdr; 1314 Elf_Data *data; 1315 Is_desc *isec; 1316 size_t size = ld_targ.t_m.m_plt_reservsz + 1317 (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) * 1318 ld_targ.t_m.m_plt_entsize); 1319 size_t rsize = (size_t)ofl->ofl_relocpltsz; 1320 1321 /* 1322 * On sparc, account for the NOP at the end of the plt. 1323 */ 1324 if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9)) 1325 size += sizeof (Word); 1326 1327 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0, 1328 &isec, &shdr, &data) == S_ERROR) 1329 return (S_ERROR); 1330 1331 data->d_size = size; 1332 data->d_align = ld_targ.t_m.m_plt_align; 1333 1334 shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags; 1335 shdr->sh_size = (Xword)size; 1336 shdr->sh_addralign = ld_targ.t_m.m_plt_align; 1337 shdr->sh_entsize = ld_targ.t_m.m_plt_entsize; 1338 1339 ofl->ofl_osplt = ld_place_section(ofl, isec, ld_targ.t_id.id_plt, 0); 1340 if (ofl->ofl_osplt == (Os_desc *)S_ERROR) 1341 return (S_ERROR); 1342 1343 ofl->ofl_osplt->os_szoutrels = (Xword)rsize; 1344 1345 return (1); 1346 } 1347 1348 /* 1349 * Make the hash table. Only built for dynamic executables and shared 1350 * libraries, and provides hashed lookup into the global symbol table 1351 * (.dynsym) for the run-time linker to resolve symbol lookups. 1352 */ 1353 static uintptr_t 1354 make_hash(Ofl_desc *ofl) 1355 { 1356 Shdr *shdr; 1357 Elf_Data *data; 1358 Is_desc *isec; 1359 size_t size; 1360 Word nsyms = ofl->ofl_globcnt; 1361 size_t cnt; 1362 1363 /* 1364 * Allocate section header structures. We set entcnt to 0 1365 * because it's going to change after we place this section. 1366 */ 1367 if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0, 1368 &isec, &shdr, &data) == S_ERROR) 1369 return (S_ERROR); 1370 1371 /* 1372 * Place the section first since it will affect the local symbol 1373 * count. 1374 */ 1375 ofl->ofl_oshash = ld_place_section(ofl, isec, ld_targ.t_id.id_hash, 0); 1376 if (ofl->ofl_oshash == (Os_desc *)S_ERROR) 1377 return (S_ERROR); 1378 1379 /* 1380 * Calculate the number of output hash buckets. 1381 */ 1382 ofl->ofl_hashbkts = findprime(nsyms); 1383 1384 /* 1385 * The size of the hash table is determined by 1386 * 1387 * i. the initial nbucket and nchain entries (2) 1388 * ii. the number of buckets (calculated above) 1389 * iii. the number of chains (this is based on the number of 1390 * symbols in the .dynsym array + NULL symbol). 1391 */ 1392 cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt + 1393 ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1); 1394 size = cnt * shdr->sh_entsize; 1395 1396 /* 1397 * Finalize the section header and data buffer initialization. 1398 */ 1399 if ((data->d_buf = libld_calloc(size, 1)) == 0) 1400 return (S_ERROR); 1401 data->d_size = size; 1402 shdr->sh_size = (Xword)size; 1403 1404 return (1); 1405 } 1406 1407 /* 1408 * Generate the standard symbol table. Contains all locals and globals, 1409 * and resides in a non-allocatable section (ie. it can be stripped). 1410 */ 1411 static uintptr_t 1412 make_symtab(Ofl_desc *ofl) 1413 { 1414 Shdr *shdr; 1415 Elf_Data *data; 1416 Is_desc *isec; 1417 Is_desc *xisec = 0; 1418 size_t size; 1419 Word symcnt; 1420 1421 /* 1422 * Create the section headers. Note that we supply an ent_cnt 1423 * of 0. We won't know the count until the section has been placed. 1424 */ 1425 if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0, 1426 &isec, &shdr, &data) == S_ERROR) 1427 return (S_ERROR); 1428 1429 /* 1430 * Place the section first since it will affect the local symbol 1431 * count. 1432 */ 1433 ofl->ofl_ossymtab = 1434 ld_place_section(ofl, isec, ld_targ.t_id.id_symtab, 0); 1435 if (ofl->ofl_ossymtab == (Os_desc *)S_ERROR) 1436 return (S_ERROR); 1437 1438 /* 1439 * At this point we've created all but the 'shstrtab' section. 1440 * Determine if we have to use 'Extended Sections'. If so - then 1441 * also create a SHT_SYMTAB_SHNDX section. 1442 */ 1443 if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) { 1444 Shdr *xshdr; 1445 Elf_Data *xdata; 1446 1447 if (new_section(ofl, SHT_SYMTAB_SHNDX, 1448 MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec, 1449 &xshdr, &xdata) == S_ERROR) 1450 return (S_ERROR); 1451 1452 if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, 1453 ld_targ.t_id.id_symtab_ndx, 0)) == (Os_desc *)S_ERROR) 1454 return (S_ERROR); 1455 } 1456 1457 /* 1458 * Calculated number of symbols, which need to be augmented by 1459 * the null first entry, the FILE symbol, and the .shstrtab entry. 1460 */ 1461 symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt + 1462 ofl->ofl_locscnt + ofl->ofl_globcnt); 1463 size = symcnt * shdr->sh_entsize; 1464 1465 /* 1466 * Finalize the section header and data buffer initialization. 1467 */ 1468 data->d_size = size; 1469 shdr->sh_size = (Xword)size; 1470 1471 /* 1472 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too. 1473 */ 1474 if (xisec) { 1475 size_t xsize = symcnt * sizeof (Word); 1476 1477 xisec->is_indata->d_size = xsize; 1478 xisec->is_shdr->sh_size = (Xword)xsize; 1479 } 1480 1481 return (1); 1482 } 1483 1484 1485 /* 1486 * Build a dynamic symbol table. These tables reside in the text 1487 * segment of a dynamic executable or shared library. 1488 * 1489 * .SUNW_ldynsym contains local function symbols 1490 * .dynsym contains only globals symbols 1491 * 1492 * The two tables are created adjacent to each other, with .SUNW_ldynsym 1493 * coming first. 1494 */ 1495 static uintptr_t 1496 make_dynsym(Ofl_desc *ofl) 1497 { 1498 Shdr *shdr, *lshdr; 1499 Elf_Data *data, *ldata; 1500 Is_desc *isec, *lisec; 1501 size_t size; 1502 Xword cnt; 1503 int allow_ldynsym; 1504 1505 /* 1506 * Unless explicitly disabled, always produce a .SUNW_ldynsym section 1507 * when it is allowed by the file type, even if the resulting 1508 * table only ends up with a single STT_FILE in it. There are 1509 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB 1510 * entry in the .dynamic section, which is something we would 1511 * like to encourage, and (2) Without it, we cannot generate 1512 * the associated .SUNW_dyn[sym|tls]sort sections, which are of 1513 * value to DTrace. 1514 * 1515 * In practice, it is extremely rare for an object not to have 1516 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be 1517 * doing it anyway. 1518 */ 1519 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 1520 1521 /* 1522 * Create the section headers. Note that we supply an ent_cnt 1523 * of 0. We won't know the count until the section has been placed. 1524 */ 1525 if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM, 1526 MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR) 1527 return (S_ERROR); 1528 1529 if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0, 1530 &isec, &shdr, &data) == S_ERROR) 1531 return (S_ERROR); 1532 1533 /* 1534 * Place the section(s) first since it will affect the local symbol 1535 * count. 1536 */ 1537 if (allow_ldynsym && 1538 ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, 1539 ld_targ.t_id.id_ldynsym, 0)) == (Os_desc *)S_ERROR)) 1540 return (S_ERROR); 1541 ofl->ofl_osdynsym = 1542 ld_place_section(ofl, isec, ld_targ.t_id.id_dynsym, 0); 1543 if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR) 1544 return (S_ERROR); 1545 1546 /* 1547 * One extra section header entry for the 'null' entry. 1548 */ 1549 cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt; 1550 size = (size_t)cnt * shdr->sh_entsize; 1551 1552 /* 1553 * Finalize the section header and data buffer initialization. 1554 */ 1555 data->d_size = size; 1556 shdr->sh_size = (Xword)size; 1557 1558 /* 1559 * An ldynsym contains local function symbols. It is not 1560 * used for linking, but if present, serves to allow better 1561 * stack traces to be generated in contexts where the symtab 1562 * is not available. (dladdr(), or stripped executable/library files). 1563 */ 1564 if (allow_ldynsym) { 1565 cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt; 1566 size = (size_t)cnt * shdr->sh_entsize; 1567 1568 ldata->d_size = size; 1569 lshdr->sh_size = (Xword)size; 1570 } 1571 1572 return (1); 1573 } 1574 1575 /* 1576 * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are 1577 * index sections for the .SUNW_ldynsym/.dynsym pair that present data 1578 * and function symbols sorted by address. 1579 */ 1580 static uintptr_t 1581 make_dynsort(Ofl_desc *ofl) 1582 { 1583 Shdr *shdr; 1584 Elf_Data *data; 1585 Is_desc *isec; 1586 1587 /* Only do it if the .SUNW_ldynsym section is present */ 1588 if (!OFL_ALLOW_LDYNSYM(ofl)) 1589 return (1); 1590 1591 /* .SUNW_dynsymsort */ 1592 if (ofl->ofl_dynsymsortcnt > 0) { 1593 if (new_section(ofl, SHT_SUNW_symsort, 1594 MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt, 1595 &isec, &shdr, &data) == S_ERROR) 1596 return (S_ERROR); 1597 1598 if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, 1599 ld_targ.t_id.id_dynsort, 0)) == (Os_desc *)S_ERROR) 1600 return (S_ERROR); 1601 } 1602 1603 /* .SUNW_dyntlssort */ 1604 if (ofl->ofl_dyntlssortcnt > 0) { 1605 if (new_section(ofl, SHT_SUNW_tlssort, 1606 MSG_ORIG(MSG_SCN_DYNTLSSORT), 1607 ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR) 1608 return (S_ERROR); 1609 1610 if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec, 1611 ld_targ.t_id.id_dynsort, 0)) == (Os_desc *)S_ERROR) 1612 return (S_ERROR); 1613 } 1614 1615 return (1); 1616 } 1617 1618 /* 1619 * Helper routine for make_dynsym_shndx. Builds a 1620 * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing 1621 * which one it is. 1622 */ 1623 static uintptr_t 1624 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab, 1625 Os_desc **ret_os) 1626 { 1627 Is_desc *isec; 1628 Is_desc *dynsymisp; 1629 Shdr *shdr, *dynshdr; 1630 Elf_Data *data; 1631 1632 dynsymisp = (Is_desc *)symtab->os_isdescs.head->data; 1633 dynshdr = dynsymisp->is_shdr; 1634 1635 if (new_section(ofl, SHT_SYMTAB_SHNDX, shname, 1636 (dynshdr->sh_size / dynshdr->sh_entsize), 1637 &isec, &shdr, &data) == S_ERROR) 1638 return (S_ERROR); 1639 1640 if ((*ret_os = ld_place_section(ofl, isec, 1641 ld_targ.t_id.id_dynsym_ndx, 0)) == (Os_desc *)S_ERROR) 1642 return (S_ERROR); 1643 1644 assert(*ret_os); 1645 1646 return (1); 1647 } 1648 1649 /* 1650 * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym 1651 */ 1652 static uintptr_t 1653 make_dynsym_shndx(Ofl_desc *ofl) 1654 { 1655 /* 1656 * If there is a .SUNW_ldynsym, generate a section for its extended 1657 * index section as well. 1658 */ 1659 if (OFL_ALLOW_LDYNSYM(ofl)) { 1660 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX), 1661 ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR) 1662 return (S_ERROR); 1663 } 1664 1665 /* The Generate a section for the dynsym */ 1666 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX), 1667 ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR) 1668 return (S_ERROR); 1669 1670 return (1); 1671 } 1672 1673 1674 /* 1675 * Build a string table for the section headers. 1676 */ 1677 static uintptr_t 1678 make_shstrtab(Ofl_desc *ofl) 1679 { 1680 Shdr *shdr; 1681 Elf_Data *data; 1682 Is_desc *isec; 1683 size_t size; 1684 1685 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB), 1686 0, &isec, &shdr, &data) == S_ERROR) 1687 return (S_ERROR); 1688 1689 /* 1690 * Place the section first, as it may effect the number of section 1691 * headers to account for. 1692 */ 1693 ofl->ofl_osshstrtab = 1694 ld_place_section(ofl, isec, ld_targ.t_id.id_note, 0); 1695 if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR) 1696 return (S_ERROR); 1697 1698 size = st_getstrtab_sz(ofl->ofl_shdrsttab); 1699 assert(size > 0); 1700 1701 data->d_size = size; 1702 shdr->sh_size = (Xword)size; 1703 1704 return (1); 1705 } 1706 1707 /* 1708 * Build a string section for the standard symbol table. 1709 */ 1710 static uintptr_t 1711 make_strtab(Ofl_desc *ofl) 1712 { 1713 Shdr *shdr; 1714 Elf_Data *data; 1715 Is_desc *isec; 1716 size_t size; 1717 1718 /* 1719 * This string table consists of all the global and local symbols. 1720 * Account for null bytes at end of the file name and the beginning 1721 * of section. 1722 */ 1723 if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1) 1724 return (S_ERROR); 1725 1726 size = st_getstrtab_sz(ofl->ofl_strtab); 1727 assert(size > 0); 1728 1729 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB), 1730 0, &isec, &shdr, &data) == S_ERROR) 1731 return (S_ERROR); 1732 1733 /* Set the size of the data area */ 1734 data->d_size = size; 1735 shdr->sh_size = (Xword)size; 1736 1737 ofl->ofl_osstrtab = 1738 ld_place_section(ofl, isec, ld_targ.t_id.id_strtab, 0); 1739 return ((uintptr_t)ofl->ofl_osstrtab); 1740 } 1741 1742 /* 1743 * Build a string table for the dynamic symbol table. 1744 */ 1745 static uintptr_t 1746 make_dynstr(Ofl_desc *ofl) 1747 { 1748 Shdr *shdr; 1749 Elf_Data *data; 1750 Is_desc *isec; 1751 size_t size; 1752 1753 /* 1754 * If producing a .SUNW_ldynsym, account for the initial STT_FILE 1755 * symbol that precedes the scope reduced global symbols. 1756 */ 1757 if (OFL_ALLOW_LDYNSYM(ofl)) { 1758 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1) 1759 return (S_ERROR); 1760 ofl->ofl_dynscopecnt++; 1761 } 1762 1763 1764 /* 1765 * Account for any local, named register symbols. These locals are 1766 * required for reference from DT_REGISTER .dynamic entries. 1767 */ 1768 if (ofl->ofl_regsyms) { 1769 int ndx; 1770 1771 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) { 1772 Sym_desc *sdp; 1773 1774 if ((sdp = ofl->ofl_regsyms[ndx]) == 0) 1775 continue; 1776 1777 if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) && 1778 (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL)) 1779 continue; 1780 1781 if (sdp->sd_sym->st_name == 0) 1782 continue; 1783 1784 if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1) 1785 return (S_ERROR); 1786 } 1787 } 1788 1789 /* 1790 * Reserve entries for any per-symbol auxiliary/filter strings. 1791 */ 1792 if (ofl->ofl_dtsfltrs != NULL) { 1793 Dfltr_desc *dftp; 1794 Aliste idx; 1795 1796 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp)) 1797 if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1) 1798 return (S_ERROR); 1799 } 1800 1801 size = st_getstrtab_sz(ofl->ofl_dynstrtab); 1802 assert(size > 0); 1803 1804 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR), 1805 0, &isec, &shdr, &data) == S_ERROR) 1806 return (S_ERROR); 1807 1808 /* Make it allocable if necessary */ 1809 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 1810 shdr->sh_flags |= SHF_ALLOC; 1811 1812 /* Set the size of the data area */ 1813 data->d_size = size + DYNSTR_EXTRA_PAD; 1814 1815 shdr->sh_size = (Xword)size; 1816 1817 ofl->ofl_osdynstr = 1818 ld_place_section(ofl, isec, ld_targ.t_id.id_dynstr, 0); 1819 return ((uintptr_t)ofl->ofl_osdynstr); 1820 } 1821 1822 /* 1823 * Generate an output relocation section which will contain the relocation 1824 * information to be applied to the `osp' section. 1825 * 1826 * If (osp == NULL) then we are creating the coalesced relocation section 1827 * for an executable and/or a shared object. 1828 */ 1829 static uintptr_t 1830 make_reloc(Ofl_desc *ofl, Os_desc *osp) 1831 { 1832 Shdr *shdr; 1833 Elf_Data *data; 1834 Is_desc *isec; 1835 size_t size; 1836 Xword sh_flags; 1837 char *sectname; 1838 Os_desc *rosp; 1839 Word relsize; 1840 const char *rel_prefix; 1841 1842 /* LINTED */ 1843 if (ld_targ.t_m.m_rel_sht_type == SHT_REL) { 1844 /* REL */ 1845 relsize = sizeof (Rel); 1846 rel_prefix = MSG_ORIG(MSG_SCN_REL); 1847 } else { 1848 /* RELA */ 1849 relsize = sizeof (Rela); 1850 rel_prefix = MSG_ORIG(MSG_SCN_RELA); 1851 } 1852 1853 if (osp) { 1854 size = osp->os_szoutrels; 1855 sh_flags = osp->os_shdr->sh_flags; 1856 if ((sectname = libld_malloc(strlen(rel_prefix) + 1857 strlen(osp->os_name) + 1)) == 0) 1858 return (S_ERROR); 1859 (void) strcpy(sectname, rel_prefix); 1860 (void) strcat(sectname, osp->os_name); 1861 } else if (ofl->ofl_flags & FLG_OF_COMREL) { 1862 size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize; 1863 sh_flags = SHF_ALLOC; 1864 sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC); 1865 } else { 1866 size = ofl->ofl_relocrelsz; 1867 sh_flags = SHF_ALLOC; 1868 sectname = (char *)rel_prefix; 1869 } 1870 1871 /* 1872 * Keep track of total size of 'output relocations' (to be stored 1873 * in .dynamic) 1874 */ 1875 /* LINTED */ 1876 ofl->ofl_relocsz += (Xword)size; 1877 1878 if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec, 1879 &shdr, &data) == S_ERROR) 1880 return (S_ERROR); 1881 1882 data->d_size = size; 1883 1884 shdr->sh_size = (Xword)size; 1885 if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC)) 1886 shdr->sh_flags = SHF_ALLOC; 1887 1888 if (osp) { 1889 /* 1890 * The sh_info field of the SHT_REL* sections points to the 1891 * section the relocations are to be applied to. 1892 */ 1893 shdr->sh_flags |= SHF_INFO_LINK; 1894 } 1895 1896 /* 1897 * Associate this relocation section to the section its going to 1898 * relocate. 1899 */ 1900 rosp = ld_place_section(ofl, isec, ld_targ.t_id.id_rel, 0); 1901 if (rosp == (Os_desc *)S_ERROR) 1902 return (S_ERROR); 1903 1904 if (osp) { 1905 Listnode *lnp; 1906 Is_desc *risp; 1907 1908 /* 1909 * We associate the input relocation sections - with 1910 * the newly created output relocation section. 1911 * 1912 * This is used primarily so that we can update 1913 * SHT_GROUP[sect_no] entries to point to the 1914 * created output relocation sections. 1915 */ 1916 for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) { 1917 risp->is_osdesc = rosp; 1918 1919 /* 1920 * If the input relocation section had the SHF_GROUP 1921 * flag set - propagate it to the output relocation 1922 * section. 1923 */ 1924 if (risp->is_shdr->sh_flags & SHF_GROUP) { 1925 rosp->os_shdr->sh_flags |= SHF_GROUP; 1926 break; 1927 } 1928 } 1929 osp->os_relosdesc = rosp; 1930 } else 1931 ofl->ofl_osrel = rosp; 1932 1933 /* 1934 * If this is the first relocation section we've encountered save it 1935 * so that the .dynamic entry can be initialized accordingly. 1936 */ 1937 if (ofl->ofl_osrelhead == (Os_desc *)0) 1938 ofl->ofl_osrelhead = rosp; 1939 1940 return (1); 1941 } 1942 1943 /* 1944 * Generate version needed section. 1945 */ 1946 static uintptr_t 1947 make_verneed(Ofl_desc *ofl) 1948 { 1949 Shdr *shdr; 1950 Elf_Data *data; 1951 Is_desc *isec; 1952 1953 /* 1954 * verneed sections do not have a constant element size, so the 1955 * value of ent_cnt specified here (0) is meaningless. 1956 */ 1957 if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION), 1958 0, &isec, &shdr, &data) == S_ERROR) 1959 return (S_ERROR); 1960 1961 /* During version processing we calculated the total size. */ 1962 data->d_size = ofl->ofl_verneedsz; 1963 shdr->sh_size = (Xword)ofl->ofl_verneedsz; 1964 1965 ofl->ofl_osverneed = 1966 ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0); 1967 return ((uintptr_t)ofl->ofl_osverneed); 1968 } 1969 1970 /* 1971 * Generate a version definition section. 1972 * 1973 * o the SHT_SUNW_verdef section defines the versions that exist within this 1974 * image. 1975 */ 1976 static uintptr_t 1977 make_verdef(Ofl_desc *ofl) 1978 { 1979 Shdr *shdr; 1980 Elf_Data *data; 1981 Is_desc *isec; 1982 Ver_desc *vdp; 1983 1984 /* 1985 * Reserve a string table entry for the base version dependency (other 1986 * dependencies have symbol representations, which will already be 1987 * accounted for during symbol processing). 1988 */ 1989 vdp = (Ver_desc *)ofl->ofl_verdesc.head->data; 1990 1991 if (ofl->ofl_flags & FLG_OF_DYNAMIC) { 1992 if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1) 1993 return (S_ERROR); 1994 } else { 1995 if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1) 1996 return (S_ERROR); 1997 } 1998 1999 /* 2000 * verdef sections do not have a constant element size, so the 2001 * value of ent_cnt specified here (0) is meaningless. 2002 */ 2003 if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION), 2004 0, &isec, &shdr, &data) == S_ERROR) 2005 return (S_ERROR); 2006 2007 /* During version processing we calculated the total size. */ 2008 data->d_size = ofl->ofl_verdefsz; 2009 shdr->sh_size = (Xword)ofl->ofl_verdefsz; 2010 2011 ofl->ofl_osverdef = 2012 ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0); 2013 return ((uintptr_t)ofl->ofl_osverdef); 2014 } 2015 2016 /* 2017 * Common function used to build both the SHT_SUNW_versym 2018 * section and the SHT_SUNW_syminfo section. Each of these sections 2019 * provides additional symbol information. 2020 */ 2021 static Os_desc * 2022 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident) 2023 { 2024 Shdr *shdr; 2025 Elf_Data *data; 2026 Is_desc *isec; 2027 2028 /* 2029 * We don't know the size of this section yet, so set it to 0. 2030 * It gets filled in after the dynsym is sized. 2031 */ 2032 if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) == 2033 S_ERROR) 2034 return ((Os_desc *)S_ERROR); 2035 2036 return (ld_place_section(ofl, isec, ident, 0)); 2037 } 2038 2039 /* 2040 * Build a .sunwbss section for allocation of tentative definitions. 2041 */ 2042 uintptr_t 2043 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align) 2044 { 2045 Shdr *shdr; 2046 Elf_Data *data; 2047 Is_desc *isec; 2048 2049 /* 2050 * Allocate header structs. We will set the name ourselves below, 2051 * and there is no entcnt for a BSS. So, the shname and entcnt 2052 * arguments are 0. 2053 */ 2054 if (new_section(ofl, SHT_NOBITS, MSG_ORIG(MSG_SCN_SUNWBSS), 0, 2055 &isec, &shdr, &data) == S_ERROR) 2056 return (S_ERROR); 2057 2058 data->d_size = size; 2059 data->d_align = align; 2060 2061 shdr->sh_size = (Xword)size; 2062 shdr->sh_addralign = align; 2063 2064 /* 2065 * Retain this .sunwbss input section as this will be where global 2066 * symbol references are added. 2067 */ 2068 ofl->ofl_issunwbss = isec; 2069 if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR) 2070 return (S_ERROR); 2071 2072 return (1); 2073 } 2074 2075 /* 2076 * This routine is called when -z nopartial is in effect. 2077 */ 2078 uintptr_t 2079 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align) 2080 { 2081 Shdr *shdr; 2082 Elf_Data *data; 2083 Is_desc *isec; 2084 Os_desc *osp; 2085 2086 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_SUNWDATA1), 0, 2087 &isec, &shdr, &data) == S_ERROR) 2088 return (S_ERROR); 2089 2090 shdr->sh_flags |= SHF_WRITE; 2091 data->d_size = size; 2092 shdr->sh_size = (Xword)size; 2093 if (align != 0) { 2094 data->d_align = align; 2095 shdr->sh_addralign = align; 2096 } 2097 2098 if ((data->d_buf = libld_calloc(size, 1)) == 0) 2099 return (S_ERROR); 2100 2101 /* 2102 * Retain this .sunwdata1 input section as this will 2103 * be where global 2104 * symbol references are added. 2105 */ 2106 ofl->ofl_issunwdata1 = isec; 2107 osp = ld_place_section(ofl, isec, ld_targ.t_id.id_data, 0); 2108 if (osp == (Os_desc *)S_ERROR) 2109 return (S_ERROR); 2110 2111 if (!(osp->os_flags & FLG_OS_OUTREL)) { 2112 ofl->ofl_dynshdrcnt++; 2113 osp->os_flags |= FLG_OS_OUTREL; 2114 } 2115 return (1); 2116 } 2117 2118 /* 2119 * Make .sunwmove section 2120 */ 2121 uintptr_t 2122 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums) 2123 { 2124 Shdr *shdr; 2125 Elf_Data *data; 2126 Is_desc *isec; 2127 Listnode *lnp1; 2128 Psym_info *psym; 2129 int cnt = 1; 2130 2131 2132 if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE), 2133 mv_nums, &isec, &shdr, &data) == S_ERROR) 2134 return (S_ERROR); 2135 2136 if ((data->d_buf = libld_calloc(data->d_size, 1)) == 0) 2137 return (S_ERROR); 2138 2139 /* 2140 * Copy move entries 2141 */ 2142 for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) { 2143 Listnode *lnp2; 2144 Mv_itm *mvitm; 2145 2146 if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN) 2147 continue; 2148 for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) { 2149 if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0) 2150 continue; 2151 mvitm->mv_oidx = cnt; 2152 cnt++; 2153 } 2154 } 2155 if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) == 2156 (Os_desc *)S_ERROR) 2157 return (S_ERROR); 2158 2159 return (1); 2160 } 2161 2162 2163 /* 2164 * Given a relocation descriptor that references a string table 2165 * input section, locate the string referenced and return a pointer 2166 * to it. 2167 */ 2168 static const char * 2169 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp) 2170 { 2171 Sym_desc *sdp = rsp->rel_sym; 2172 Xword str_off; 2173 2174 /* 2175 * In the case of an STT_SECTION symbol, the addend of the 2176 * relocation gives the offset into the string section. For 2177 * other symbol types, the symbol value is the offset. 2178 */ 2179 2180 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) { 2181 str_off = sdp->sd_sym->st_value; 2182 } else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) { 2183 /* 2184 * For SHT_RELA, the addend value is found in the 2185 * rel_raddend field of the relocation. 2186 */ 2187 str_off = rsp->rel_raddend; 2188 } else { /* REL and STT_SECTION */ 2189 /* 2190 * For SHT_REL, the "addend" is not part of the relocation 2191 * record. Instead, it is found at the relocation target 2192 * address. 2193 */ 2194 uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset + 2195 (uintptr_t)rsp->rel_isdesc->is_indata->d_buf); 2196 2197 if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0) 2198 return (0); 2199 } 2200 2201 return (str_off + (char *)sdp->sd_isc->is_indata->d_buf); 2202 } 2203 2204 /* 2205 * First pass over the relocation records for string table merging. 2206 * Build lists of relocations and symbols that will need modification, 2207 * and insert the strings they reference into the mstrtab string table. 2208 * 2209 * entry: 2210 * ofl, osp - As passed to ld_make_strmerge(). 2211 * mstrtab - String table to receive input strings. This table 2212 * must be in its first (initialization) pass and not 2213 * yet cooked (st_getstrtab_sz() not yet called). 2214 * rel_aplist - APlist to receive pointer to any relocation 2215 * descriptors with STT_SECTION symbols that reference 2216 * one of the input sections being merged. 2217 * sym_aplist - APlist to receive pointer to any symbols that reference 2218 * one of the input sections being merged. 2219 * reloc_list - List of relocation descriptors to examine. 2220 * Either ofl->&ofl->ofl_actrels (active relocations) 2221 * or &ofl->ofl_outrels (output relocations). 2222 * 2223 * exit: 2224 * On success, rel_aplist and sym_aplist are updated, and 2225 * any strings in the mergable input sections referenced by 2226 * a relocation has been entered into mstrtab. True (1) is returned. 2227 * 2228 * On failure, False (0) is returned. 2229 */ 2230 static int 2231 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab, 2232 APlist **rel_aplist, APlist **sym_aplist, List *reloc_list) 2233 { 2234 Listnode *lnp; 2235 Rel_cache *rcp; 2236 Sym_desc *sdp; 2237 Sym_desc *last_sdp = NULL; 2238 Rel_desc *rsp; 2239 const char *name; 2240 2241 for (LIST_TRAVERSE(reloc_list, lnp, rcp)) { 2242 /* LINTED */ 2243 for (rsp = (Rel_desc *)(rcp + 1); rsp < rcp->rc_free; rsp++) { 2244 sdp = rsp->rel_sym; 2245 if ((sdp->sd_isc == NULL) || 2246 ((sdp->sd_isc->is_flags & 2247 (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) != 2248 FLG_IS_INSTRMRG) || 2249 (sdp->sd_isc->is_osdesc != osp)) 2250 continue; 2251 2252 /* 2253 * Remember symbol for use in the third pass. 2254 * There is no reason to save a given symbol more 2255 * than once, so we take advantage of the fact that 2256 * relocations to a given symbol tend to cluster 2257 * in the list. If this is the same symbol we saved 2258 * last time, don't bother. 2259 */ 2260 if (last_sdp != sdp) { 2261 if (aplist_append(sym_aplist, sdp, 2262 AL_CNT_STRMRGSYM) == 0) 2263 return (0); 2264 last_sdp = sdp; 2265 } 2266 2267 /* Enter the string into our new string table */ 2268 name = strmerge_get_reloc_str(ofl, rsp); 2269 if (st_insert(mstrtab, name) == -1) 2270 return (0); 2271 2272 /* 2273 * If this is an STT_SECTION symbol, then the 2274 * second pass will need to modify this relocation, 2275 * so hang on to it. 2276 */ 2277 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == 2278 STT_SECTION) && 2279 (aplist_append(rel_aplist, rsp, 2280 AL_CNT_STRMRGREL) == 0)) 2281 return (0); 2282 } 2283 } 2284 2285 return (1); 2286 } 2287 2288 /* 2289 * If the output section has any SHF_MERGE|SHF_STRINGS input sections, 2290 * replace them with a single merged/compressed input section. 2291 * 2292 * entry: 2293 * ofl - Output file descriptor 2294 * osp - Output section descriptor 2295 * rel_aplist, sym_aplist, - Address of 2 APlists, to be used 2296 * for internal processing. On the initial call to 2297 * ld_make_strmerge, these list pointers must be NULL. 2298 * The caller is encouraged to pass the same lists back for 2299 * successive calls to this function without freeing 2300 * them in between calls. This causes a single pair of 2301 * memory allocations to be reused multiple times. 2302 * 2303 * exit: 2304 * If section merging is possible, it is done. If no errors are 2305 * encountered, True (1) is returned. On error, S_ERROR. 2306 * 2307 * The contents of rel_aplist and sym_aplist on exit are 2308 * undefined. The caller can free them, or pass them back to a subsequent 2309 * call to this routine, but should not examine their contents. 2310 */ 2311 static uintptr_t 2312 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_aplist, 2313 APlist **sym_aplist) 2314 { 2315 Str_tbl *mstrtab; /* string table for string merge secs */ 2316 Is_desc *mstrsec; /* Generated string merge section */ 2317 Is_desc *isp; 2318 Shdr *mstr_shdr; 2319 Elf_Data *mstr_data; 2320 Sym_desc *sdp; 2321 Rel_desc *rsp; 2322 Aliste idx; 2323 size_t data_size; 2324 int st_setstring_status; 2325 size_t stoff; 2326 2327 /* If string table compression is disabled, there's nothing to do */ 2328 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0) 2329 return (1); 2330 2331 /* 2332 * Pass over the mergeable input sections, and if they haven't 2333 * all been discarded, create a string table. 2334 */ 2335 mstrtab = NULL; 2336 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) { 2337 if (isp->is_flags & FLG_IS_DISCARD) 2338 continue; 2339 2340 /* 2341 * We have at least one non-discarded section. 2342 * Create a string table descriptor. 2343 */ 2344 if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL) 2345 return (S_ERROR); 2346 break; 2347 } 2348 2349 /* If no string table was created, we have no mergeable sections */ 2350 if (mstrtab == NULL) 2351 return (1); 2352 2353 /* 2354 * This routine has to make 3 passes: 2355 * 2356 * 1) Examine all relocations, insert strings from relocations 2357 * to the mergable input sections into the string table. 2358 * 2) Modify the relocation values to be correct for the 2359 * new merged section. 2360 * 3) Modify the symbols used by the relocations to reference 2361 * the new section. 2362 * 2363 * These passes cannot be combined: 2364 * - The string table code works in two passes, and all 2365 * strings have to be loaded in pass one before the 2366 * offset of any strings can be determined. 2367 * - Multiple relocations reference a single symbol, so the 2368 * symbol cannot be modified until all relocations are 2369 * fixed. 2370 * 2371 * The number of relocations related to section merging is usually 2372 * a mere fraction of the overall active and output relocation lists, 2373 * and the number of symbols is usually a fraction of the number 2374 * of related relocations. We therefore build APlists for the 2375 * relocations and symbols in the first pass, and then use those 2376 * lists to accelerate the operation of pass 2 and 3. 2377 * 2378 * Reinitialize the lists to a completely empty state. 2379 */ 2380 aplist_reset(*rel_aplist); 2381 aplist_reset(*sym_aplist); 2382 2383 /* 2384 * Pass 1: 2385 * 2386 * Every relocation related to this output section (and the input 2387 * sections that make it up) is found in either the active, or the 2388 * output relocation list, depending on whether the relocation is to 2389 * be processed by this invocation of the linker, or inserted into the 2390 * output object. 2391 * 2392 * Build lists of relocations and symbols that will need modification, 2393 * and insert the strings they reference into the mstrtab string table. 2394 */ 2395 if (strmerge_pass1(ofl, osp, mstrtab, rel_aplist, sym_aplist, 2396 &ofl->ofl_actrels) == 0) 2397 goto return_s_error; 2398 if (strmerge_pass1(ofl, osp, mstrtab, rel_aplist, sym_aplist, 2399 &ofl->ofl_outrels) == 0) 2400 goto return_s_error; 2401 2402 /* 2403 * Get the size of the new input section. Requesting the 2404 * string table size "cooks" the table, and finalizes its contents. 2405 */ 2406 data_size = st_getstrtab_sz(mstrtab); 2407 2408 /* Create a new input section to hold the merged strings */ 2409 if (new_section_from_template(ofl, isp, data_size, 2410 &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR) 2411 goto return_s_error; 2412 mstrsec->is_flags |= FLG_IS_GNSTRMRG; 2413 2414 /* 2415 * Allocate a data buffer for the new input section. 2416 * Then, associate the buffer with the string table descriptor. 2417 */ 2418 if ((mstr_data->d_buf = libld_malloc(data_size)) == 0) 2419 goto return_s_error; 2420 if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1) 2421 goto return_s_error; 2422 2423 /* Add the new section to the output image */ 2424 if (ld_place_section(ofl, mstrsec, osp->os_scnsymndx, 0) == 2425 (Os_desc *)S_ERROR) 2426 goto return_s_error; 2427 2428 /* 2429 * Pass 2: 2430 * 2431 * Revisit the relocation descriptors with STT_SECTION symbols 2432 * that were saved by the first pass. Update each relocation 2433 * record so that the offset it contains is for the new section 2434 * instead of the original. 2435 */ 2436 for (APLIST_TRAVERSE(*rel_aplist, idx, rsp)) { 2437 const char *name; 2438 2439 /* Put the string into the merged string table */ 2440 name = strmerge_get_reloc_str(ofl, rsp); 2441 st_setstring_status = st_setstring(mstrtab, name, &stoff); 2442 if (st_setstring_status == -1) { 2443 /* 2444 * A failure to insert at this point means that 2445 * something is corrupt. This isn't a resource issue. 2446 */ 2447 assert(st_setstring_status != -1); 2448 goto return_s_error; 2449 } 2450 2451 /* 2452 * Alter the relocation to access the string at the 2453 * new offset in our new string table. 2454 * 2455 * For SHT_RELA platforms, it suffices to simply 2456 * update the rel_raddend field of the relocation. 2457 * 2458 * For SHT_REL platforms, the new "addend" value 2459 * needs to be written at the address being relocated. 2460 * However, we can't alter the input sections which 2461 * are mapped readonly, and the output image has not 2462 * been created yet. So, we defer this operation, 2463 * using the rel_raddend field of the relocation 2464 * which is normally 0 on a REL platform, to pass the 2465 * new "addend" value to ld_perform_outreloc() or 2466 * ld_do_activerelocs(). The FLG_REL_NADDEND flag 2467 * tells them that this is the case. 2468 */ 2469 if ((rsp->rel_flags & FLG_REL_RELA) == 0) /* REL */ 2470 rsp->rel_flags |= FLG_REL_NADDEND; 2471 rsp->rel_raddend = (Sxword)stoff; 2472 2473 /* 2474 * Change the descriptor name to reflect the fact that it 2475 * points at our merged section. This shows up in debug 2476 * output and helps show how the relocation has changed 2477 * from its original input section to our merged one. 2478 */ 2479 rsp->rel_sname = ld_section_reld_name(rsp->rel_sym, mstrsec); 2480 if (rsp->rel_sname == NULL) 2481 goto return_s_error; 2482 } 2483 2484 /* 2485 * Pass 3: 2486 * 2487 * Modify the symbols referenced by the relocation descriptors 2488 * so that they reference the new input section containing the 2489 * merged strings instead of the original input sections. 2490 */ 2491 for (APLIST_TRAVERSE(*sym_aplist, idx, sdp)) { 2492 /* 2493 * If we've already processed this symbol, don't do it 2494 * twice. strmerge_pass1() uses a heuristic (relocations to 2495 * the same symbol clump together) to avoid inserting a 2496 * given symbol more than once, but repeat symbols in 2497 * the list can occur. 2498 */ 2499 if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0) 2500 continue; 2501 2502 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) { 2503 /* 2504 * This is not an STT_SECTION symbol, so its 2505 * value is the offset of the string within the 2506 * input section. Update the address to reflect 2507 * the address in our new merged section. 2508 */ 2509 const char *name = sdp->sd_sym->st_value + 2510 (char *)sdp->sd_isc->is_indata->d_buf; 2511 2512 st_setstring_status = 2513 st_setstring(mstrtab, name, &stoff); 2514 if (st_setstring_status == -1) { 2515 /* 2516 * A failure to insert at this point means 2517 * something is corrupt. This isn't a 2518 * resource issue. 2519 */ 2520 assert(st_setstring_status != -1); 2521 goto return_s_error; 2522 } 2523 2524 if (ld_sym_copy(sdp) == S_ERROR) 2525 goto return_s_error; 2526 sdp->sd_sym->st_value = (Word)stoff; 2527 } 2528 2529 /* Redirect the symbol to our new merged section */ 2530 sdp->sd_isc = mstrsec; 2531 } 2532 2533 /* 2534 * There are no references left to the original input string sections. 2535 * Mark them as discarded so they don't go into the output image. 2536 * At the same time, add up the sizes of the replaced sections. 2537 */ 2538 data_size = 0; 2539 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) { 2540 if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG)) 2541 continue; 2542 2543 data_size += isp->is_indata->d_size; 2544 2545 isp->is_flags |= FLG_IS_DISCARD; 2546 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec)); 2547 } 2548 2549 /* Report how much space we saved in the output section */ 2550 Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size, 2551 mstr_data->d_size); 2552 2553 st_destroy(mstrtab); 2554 return (1); 2555 2556 return_s_error: 2557 st_destroy(mstrtab); 2558 return (S_ERROR); 2559 } 2560 2561 2562 /* 2563 * The following sections are built after all input file processing and symbol 2564 * validation has been carried out. The order is important (because the 2565 * addition of a section adds a new symbol there is a chicken and egg problem 2566 * of maintaining the appropriate counts). By maintaining a known order the 2567 * individual routines can compensate for later, known, additions. 2568 */ 2569 uintptr_t 2570 ld_make_sections(Ofl_desc *ofl) 2571 { 2572 ofl_flag_t flags = ofl->ofl_flags; 2573 Listnode *lnp1; 2574 Sg_desc *sgp; 2575 2576 /* 2577 * Generate any special sections. 2578 */ 2579 if (flags & FLG_OF_ADDVERS) 2580 if (make_comment(ofl) == S_ERROR) 2581 return (S_ERROR); 2582 2583 if (make_interp(ofl) == S_ERROR) 2584 return (S_ERROR); 2585 2586 if (make_cap(ofl) == S_ERROR) 2587 return (S_ERROR); 2588 2589 if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY), 2590 &ofl->ofl_initarray) == S_ERROR) 2591 return (S_ERROR); 2592 2593 if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY), 2594 &ofl->ofl_finiarray) == S_ERROR) 2595 return (S_ERROR); 2596 2597 if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY), 2598 &ofl->ofl_preiarray) == S_ERROR) 2599 return (S_ERROR); 2600 2601 /* 2602 * Make the .plt section. This occurs after any other relocation 2603 * sections are generated (see reloc_init()) to ensure that the 2604 * associated relocation section is after all the other relocation 2605 * sections. 2606 */ 2607 if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad)) 2608 if (make_plt(ofl) == S_ERROR) 2609 return (S_ERROR); 2610 2611 /* 2612 * Determine whether any sections or files are not referenced. Under 2613 * -Dunused a diagnostic for any unused components is generated, under 2614 * -zignore the component is removed from the final output. 2615 */ 2616 if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) { 2617 if (ignore_section_processing(ofl) == S_ERROR) 2618 return (S_ERROR); 2619 } 2620 2621 /* 2622 * Do any of the output sections contain input sections that 2623 * are candidates for string table merging? For each such case, 2624 * we create a replacement section, insert it, and discard the 2625 * originals. 2626 * 2627 * rel_aplist and sym_aplist are used by ld_make_strmerge() 2628 * for its internal processing. We are responsible for the 2629 * initialization and cleanup, and ld_make_strmerge() handles the rest. 2630 * This allows us to reuse a single pair of memory buffers allocatated 2631 * for this processing for all the output sections. 2632 */ 2633 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) { 2634 int error_seen = 0; 2635 APlist *rel_aplist = NULL; 2636 APlist *sym_aplist = NULL; 2637 2638 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) { 2639 Os_desc *osp; 2640 Aliste idx; 2641 2642 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) 2643 if ((osp->os_mstrisdescs != NULL) && 2644 (ld_make_strmerge(ofl, osp, 2645 &rel_aplist, &sym_aplist) == 2646 S_ERROR)) { 2647 error_seen = 1; 2648 break; 2649 } 2650 } 2651 if (rel_aplist != NULL) 2652 free(rel_aplist); 2653 if (sym_aplist != NULL) 2654 free(sym_aplist); 2655 if (error_seen != 0) 2656 return (S_ERROR); 2657 } 2658 2659 /* 2660 * Add any necessary versioning information. 2661 */ 2662 if (!(flags & FLG_OF_NOVERSEC)) { 2663 if ((flags & FLG_OF_VERNEED) && 2664 (make_verneed(ofl) == S_ERROR)) 2665 return (S_ERROR); 2666 if ((flags & FLG_OF_VERDEF) && 2667 (make_verdef(ofl) == S_ERROR)) 2668 return (S_ERROR); 2669 if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) && 2670 ((ofl->ofl_osversym = make_sym_sec(ofl, 2671 MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym, 2672 ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR)) 2673 return (S_ERROR); 2674 } 2675 2676 /* 2677 * Create a syminfo section if necessary. 2678 */ 2679 if (flags & FLG_OF_SYMINFO) { 2680 if ((ofl->ofl_ossyminfo = make_sym_sec(ofl, 2681 MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo, 2682 ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR) 2683 return (S_ERROR); 2684 } 2685 2686 if (flags & FLG_OF_COMREL) { 2687 /* 2688 * If -zcombreloc is enabled then all relocations (except for 2689 * the PLT's) are coalesced into a single relocation section. 2690 */ 2691 if (ofl->ofl_reloccnt) { 2692 if (make_reloc(ofl, NULL) == S_ERROR) 2693 return (S_ERROR); 2694 } 2695 } else { 2696 /* 2697 * Create the required output relocation sections. Note, new 2698 * sections may be added to the section list that is being 2699 * traversed. These insertions can move the elements of the 2700 * Alist such that a section descriptor is re-read. Recursion 2701 * is prevented by maintaining a previous section pointer and 2702 * insuring that this pointer isn't re-examined. 2703 */ 2704 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) { 2705 Os_desc *osp, *posp = 0; 2706 Aliste idx; 2707 2708 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) { 2709 if ((osp != posp) && osp->os_szoutrels && 2710 (osp != ofl->ofl_osplt)) { 2711 if (make_reloc(ofl, osp) == S_ERROR) 2712 return (S_ERROR); 2713 } 2714 posp = osp; 2715 } 2716 } 2717 2718 /* 2719 * If we're not building a combined relocation section, then 2720 * build a .rel[a] section as required. 2721 */ 2722 if (ofl->ofl_relocrelsz) { 2723 if (make_reloc(ofl, NULL) == S_ERROR) 2724 return (S_ERROR); 2725 } 2726 } 2727 2728 /* 2729 * The PLT relocations are always in their own section, and we try to 2730 * keep them at the end of the PLT table. We do this to keep the hot 2731 * "data" PLT's at the head of the table nearer the .dynsym & .hash. 2732 */ 2733 if (ofl->ofl_osplt && ofl->ofl_relocpltsz) { 2734 if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR) 2735 return (S_ERROR); 2736 } 2737 2738 /* 2739 * Finally build the symbol and section header sections. 2740 */ 2741 if (flags & FLG_OF_DYNAMIC) { 2742 if (make_dynamic(ofl) == S_ERROR) 2743 return (S_ERROR); 2744 if (make_dynstr(ofl) == S_ERROR) 2745 return (S_ERROR); 2746 /* 2747 * There is no use for .hash and .dynsym sections in a 2748 * relocatable object. 2749 */ 2750 if (!(flags & FLG_OF_RELOBJ)) { 2751 if (make_hash(ofl) == S_ERROR) 2752 return (S_ERROR); 2753 if (make_dynsym(ofl) == S_ERROR) 2754 return (S_ERROR); 2755 #if defined(_ELF64) 2756 if ((ld_targ.t_uw.uw_make_unwindhdr != NULL) && 2757 ((*ld_targ.t_uw.uw_make_unwindhdr)(ofl) == S_ERROR)) 2758 return (S_ERROR); 2759 #endif 2760 if (make_dynsort(ofl) == S_ERROR) 2761 return (S_ERROR); 2762 } 2763 } 2764 2765 if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) || 2766 ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) { 2767 /* 2768 * Do we need to make a SHT_SYMTAB_SHNDX section 2769 * for the dynsym. If so - do it now. 2770 */ 2771 if (ofl->ofl_osdynsym && 2772 ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) { 2773 if (make_dynsym_shndx(ofl) == S_ERROR) 2774 return (S_ERROR); 2775 } 2776 2777 if (make_strtab(ofl) == S_ERROR) 2778 return (S_ERROR); 2779 if (make_symtab(ofl) == S_ERROR) 2780 return (S_ERROR); 2781 } else { 2782 /* 2783 * Do we need to make a SHT_SYMTAB_SHNDX section 2784 * for the dynsym. If so - do it now. 2785 */ 2786 if (ofl->ofl_osdynsym && 2787 ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) { 2788 if (make_dynsym_shndx(ofl) == S_ERROR) 2789 return (S_ERROR); 2790 } 2791 } 2792 2793 if (make_shstrtab(ofl) == S_ERROR) 2794 return (S_ERROR); 2795 2796 /* 2797 * Now that we've created all of our sections adjust the size 2798 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on 2799 * the symbol table sizes. 2800 */ 2801 if (ofl->ofl_osversym || ofl->ofl_ossyminfo) { 2802 Shdr *shdr; 2803 Is_desc *isec; 2804 Elf_Data *data; 2805 size_t size; 2806 ulong_t cnt; 2807 Os_desc *osp; 2808 2809 if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) { 2810 osp = ofl->ofl_ossymtab; 2811 } else { 2812 osp = ofl->ofl_osdynsym; 2813 } 2814 isec = (Is_desc *)osp->os_isdescs.head->data; 2815 cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize); 2816 2817 if (ofl->ofl_osversym) { 2818 osp = ofl->ofl_osversym; 2819 isec = (Is_desc *)osp->os_isdescs.head->data; 2820 data = isec->is_indata; 2821 shdr = osp->os_shdr; 2822 size = cnt * shdr->sh_entsize; 2823 shdr->sh_size = (Xword)size; 2824 data->d_size = size; 2825 } 2826 if (ofl->ofl_ossyminfo) { 2827 osp = ofl->ofl_ossyminfo; 2828 isec = (Is_desc *)osp->os_isdescs.head->data; 2829 data = isec->is_indata; 2830 shdr = osp->os_shdr; 2831 size = cnt * shdr->sh_entsize; 2832 shdr->sh_size = (Xword)size; 2833 data->d_size = size; 2834 } 2835 } 2836 2837 return (1); 2838 } 2839 2840 /* 2841 * Build an additional data section - used to back OBJT symbol definitions 2842 * added with a mapfile. 2843 */ 2844 Is_desc * 2845 ld_make_data(Ofl_desc *ofl, size_t size) 2846 { 2847 Shdr *shdr; 2848 Elf_Data *data; 2849 Is_desc *isec; 2850 2851 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0, 2852 &isec, &shdr, &data) == S_ERROR) 2853 return ((Is_desc *)S_ERROR); 2854 2855 data->d_size = size; 2856 shdr->sh_size = (Xword)size; 2857 shdr->sh_flags |= SHF_WRITE; 2858 2859 if (ld_place_section(ofl, isec, ld_targ.t_id.id_data, 0) == 2860 (Os_desc *)S_ERROR) 2861 return ((Is_desc *)S_ERROR); 2862 2863 return (isec); 2864 } 2865 2866 2867 /* 2868 * Build an additional text section - used to back FUNC symbol definitions 2869 * added with a mapfile. 2870 */ 2871 Is_desc * 2872 ld_make_text(Ofl_desc *ofl, size_t size) 2873 { 2874 Shdr *shdr; 2875 Elf_Data *data; 2876 Is_desc *isec; 2877 2878 /* 2879 * Insure the size is sufficient to contain the minimum return 2880 * instruction. 2881 */ 2882 if (size < ld_targ.t_nf.nf_size) 2883 size = ld_targ.t_nf.nf_size; 2884 2885 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0, 2886 &isec, &shdr, &data) == S_ERROR) 2887 return ((Is_desc *)S_ERROR); 2888 2889 data->d_size = size; 2890 shdr->sh_size = (Xword)size; 2891 shdr->sh_flags |= SHF_EXECINSTR; 2892 2893 /* 2894 * Fill the buffer with the appropriate return instruction. 2895 * Note that there is no need to swap bytes on a non-native, 2896 * link, as the data being copied is given in bytes. 2897 */ 2898 if ((data->d_buf = libld_calloc(size, 1)) == 0) 2899 return ((Is_desc *)S_ERROR); 2900 (void) memcpy(data->d_buf, ld_targ.t_nf.nf_template, 2901 ld_targ.t_nf.nf_size); 2902 2903 if (ld_place_section(ofl, isec, ld_targ.t_id.id_text, 0) == 2904 (Os_desc *)S_ERROR) 2905 return ((Is_desc *)S_ERROR); 2906 2907 return (isec); 2908 } 2909