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