1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved. 27 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 28 * Copyright 2024 Oxide Computer Company 29 */ 30 31 /* 32 * Map file parsing and input section to output segment mapping. 33 */ 34 #include <stdio.h> 35 #include <string.h> 36 #include <debug.h> 37 #include "msg.h" 38 #include "_libld.h" 39 40 /* 41 * Each time a section is placed, the function set_addralign() 42 * is called. This function performs: 43 * 44 * - if the section is from an external file, check if this is empty or not. 45 * If not, we know the segment this section will belong needs a program 46 * header. (Of course, the program is needed only if this section falls 47 * into a loadable segment.) 48 * - compute the Least Common Multiplier for setting the segment alignment. 49 */ 50 static void 51 set_addralign(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp) 52 { 53 Shdr *shdr = isp->is_shdr; 54 55 /* A discarded section has no influence on the output */ 56 if (isp->is_flags & FLG_IS_DISCARD) 57 return; 58 59 /* 60 * If this section has data or will be assigned data 61 * later, mark this segment not-empty. 62 */ 63 if ((shdr->sh_size != 0) || 64 ((isp->is_flags & FLG_IS_EXTERNAL) == 0)) 65 osp->os_sgdesc->sg_flags |= FLG_SG_PHREQ; 66 67 if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) && 68 (osp->os_sgdesc->sg_phdr).p_type != PT_LOAD) 69 return; 70 71 osp->os_sgdesc->sg_align = 72 ld_lcm(osp->os_sgdesc->sg_align, shdr->sh_addralign); 73 } 74 75 /* 76 * Return the first input descriptor for a given output descriptor, 77 * or NULL if there are none. 78 */ 79 80 Is_desc * 81 ld_os_first_isdesc(Os_desc *osp) 82 { 83 int i; 84 85 for (i = 0; i < OS_ISD_NUM; i++) { 86 APlist *ap_isdesc = osp->os_isdescs[i]; 87 88 if (aplist_nitems(ap_isdesc) > 0) 89 return ((Is_desc *)ap_isdesc->apl_data[0]); 90 } 91 92 return (NULL); 93 } 94 95 /* 96 * Attach an input section to an output section 97 * 98 * entry: 99 * ofl - File descriptor 100 * osp - Output section descriptor 101 * isp - Input section descriptor 102 * mapfile_sort - True (1) if segment supports mapfile specified ordering 103 * of otherwise unordered input sections, and False (0) otherwise. 104 * 105 * exit: 106 * - The input section has been attached to the output section 107 * - If the input section is a candidate for string table merging, 108 * then it is appended to the output section's list of merge 109 * candidates (os_mstridescs). 110 * 111 * On success, returns True (1). On failure, False (0). 112 */ 113 static int 114 os_attach_isp(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp, int mapfile_sort) 115 { 116 Aliste init_arritems; 117 int os_isdescs_idx, do_append = 1; 118 119 if ((isp->is_flags & FLG_IS_ORDERED) == 0) { 120 init_arritems = AL_CNT_OS_ISDESCS; 121 os_isdescs_idx = OS_ISD_DEFAULT; 122 123 /* 124 * If section ordering was specified for an unordered section 125 * via the mapfile, then search in the OS_ISD_DEFAULT list 126 * and insert it in the specified position. Ordered sections 127 * are placed in ascending order before unordered sections 128 * (sections with an is_ordndx value of zero). 129 * 130 * If no mapfile ordering was specified, we append it in 131 * the usual way below. 132 */ 133 if (mapfile_sort && (isp->is_ordndx > 0)) { 134 APlist *ap_isdesc = osp->os_isdescs[OS_ISD_DEFAULT]; 135 Aliste idx2; 136 Is_desc *isp2; 137 138 for (APLIST_TRAVERSE(ap_isdesc, idx2, isp2)) { 139 if (isp2->is_ordndx && 140 (isp2->is_ordndx <= isp->is_ordndx)) 141 continue; 142 143 if (aplist_insert( 144 &osp->os_isdescs[OS_ISD_DEFAULT], 145 isp, init_arritems, idx2) == NULL) 146 return (0); 147 do_append = 0; 148 break; 149 } 150 } 151 } else { /* Ordered section (via shdr flags) */ 152 Word shndx; 153 154 /* SHF_ORDERED uses sh_info, SHF_LINK_ORDERED uses sh_link */ 155 shndx = (isp->is_shdr->sh_flags & SHF_ORDERED) ? 156 isp->is_shdr->sh_info : isp->is_shdr->sh_link; 157 158 if (shndx == SHN_BEFORE) { 159 init_arritems = AL_CNT_OS_ISDESCS_BA; 160 os_isdescs_idx = OS_ISD_BEFORE; 161 } else if (shndx == SHN_AFTER) { 162 init_arritems = AL_CNT_OS_ISDESCS_BA; 163 os_isdescs_idx = OS_ISD_AFTER; 164 } else { 165 init_arritems = AL_CNT_OS_ISDESCS; 166 os_isdescs_idx = OS_ISD_ORDERED; 167 } 168 } 169 170 /* 171 * If we didn't insert a section into the default list using 172 * mapfile specified ordering above, then append the input 173 * section to the appropriate list. 174 */ 175 if (do_append && aplist_append(&(osp->os_isdescs[os_isdescs_idx]), 176 isp, init_arritems) == NULL) 177 return (0); 178 isp->is_osdesc = osp; 179 180 /* 181 * A section can be merged if the following are true: 182 * - The SHF_MERGE|SHF_STRINGS flags must be set 183 * - String table compression must not be disabled (-znocompstrtab) 184 * - Mapfile ordering must not have been used. 185 * - The section must not be ordered via section header flags. 186 * - It must not be the generated section being built to 187 * replace the sections on this list. 188 */ 189 if (((isp->is_shdr->sh_flags & (SHF_MERGE | SHF_STRINGS)) != 190 (SHF_MERGE | SHF_STRINGS)) || 191 ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0) || 192 !do_append || 193 ((isp->is_flags & (FLG_IS_ORDERED | FLG_IS_GNSTRMRG)) != 0)) 194 return (1); 195 196 /* 197 * Skip sections with (sh_entsize > 1) or (sh_addralign > 1). 198 * 199 * sh_entsize: 200 * We are currently only able to merge string tables containing 201 * strings with 1-byte (char) characters. Support for wide 202 * characters will require our string table compression code 203 * to be extended to handle larger character sizes. 204 * 205 * sh_addralign: 206 * Alignments greater than 1 would require our string table 207 * compression code to insert null bytes to move each 208 * string to the required alignment. 209 */ 210 if ((isp->is_shdr->sh_entsize > 1) || 211 (isp->is_shdr->sh_addralign > 1)) { 212 DBG_CALL(Dbg_sec_unsup_strmerge(ofl->ofl_lml, isp)); 213 return (1); 214 } 215 216 if (aplist_append(&osp->os_mstrisdescs, isp, 217 AL_CNT_OS_MSTRISDESCS) == NULL) 218 return (0); 219 220 /* 221 * The SHF_MERGE|SHF_STRINGS flags tell us that the program that 222 * created the section intended it to be mergeable. The 223 * FLG_IS_INSTRMRG flag says that we have done validity testing 224 * and decided that it is safe to act on that hint. 225 */ 226 isp->is_flags |= FLG_IS_INSTRMRG; 227 228 return (1); 229 } 230 231 /* 232 * Determine whether this input COMDAT section already exists for the associated 233 * output section. If so, then discard this input section. Otherwise, this 234 * must be the first COMDAT section, thus it is kept for future comparisons. 235 */ 236 static uintptr_t 237 add_comdat(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp) 238 { 239 Isd_node isd, *isdp; 240 avl_tree_t *avlt; 241 avl_index_t where; 242 Group_desc *gr; 243 244 /* 245 * Sections to which COMDAT groups apply are FLG_IS_COMDAT but are 246 * discarded separately by the group logic so should never be 247 * discarded here. 248 */ 249 if ((isp->is_shdr->sh_flags & SHF_GROUP) && 250 ((gr = ld_get_group(ofl, isp)) != NULL) && 251 (gr->gd_data[0] & GRP_COMDAT)) 252 return (1); 253 254 /* 255 * Create a COMDAT avl tree for this output section if required. 256 */ 257 if ((avlt = osp->os_comdats) == NULL) { 258 if ((avlt = libld_calloc(1, sizeof (avl_tree_t))) == NULL) 259 return (S_ERROR); 260 avl_create(avlt, isdavl_compare, sizeof (Isd_node), 261 SGSOFFSETOF(Isd_node, isd_avl)); 262 osp->os_comdats = avlt; 263 } 264 265 /* 266 * A standard COMDAT section uses the section name as search key. 267 */ 268 isd.isd_name = isp->is_name; 269 isd.isd_hash = sgs_str_hash(isd.isd_name); 270 271 if ((isdp = avl_find(avlt, &isd, &where)) != NULL) { 272 isp->is_osdesc = osp; 273 274 /* 275 * If this section hasn't already been identified as discarded, 276 * generate a suitable diagnostic. 277 */ 278 if ((isp->is_flags & FLG_IS_DISCARD) == 0) { 279 isp->is_flags |= FLG_IS_DISCARD; 280 isp->is_comdatkeep = isdp->isd_isp; 281 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, 282 isdp->isd_isp)); 283 } 284 285 /* 286 * A discarded section does not require assignment to an output 287 * section. However, if relaxed relocations have been enabled 288 * (either from -z relaxreloc, or asserted with .gnu.linkonce 289 * processing), then this section must still be assigned to an 290 * output section so that the sloppy relocation logic will have 291 * the information necessary to do its work. 292 */ 293 return (0); 294 } 295 296 /* 297 * This is a new COMDAT section - so keep it. 298 */ 299 if ((isdp = libld_calloc(1, sizeof (Isd_node))) == NULL) 300 return (S_ERROR); 301 302 isdp->isd_name = isd.isd_name; 303 isdp->isd_hash = isd.isd_hash; 304 isdp->isd_isp = isp; 305 306 avl_insert(avlt, isdp, where); 307 return (1); 308 } 309 310 /* 311 * Determine whether a GNU group COMDAT section name follows the convention 312 * 313 * section-name.symbol-name 314 * 315 * Each section within the input file is compared to see if the full section 316 * name matches the beginning of the COMDAT section, with a following '.'. 317 * A pointer to the symbol name, starting with the '.' is returned so that the 318 * caller can strip off the required section name. 319 */ 320 static char * 321 gnu_comdat_sym(Ifl_desc *ifl, Is_desc *gisp) 322 { 323 size_t ndx; 324 325 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) { 326 Is_desc *isp; 327 size_t ssize; 328 329 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) || 330 (isp == gisp) || (isp->is_name == NULL)) 331 continue; 332 333 /* 334 * It's questionable whether this size should be cached in the 335 * Is_desc. However, this seems an infrequent operation and 336 * adding Is_desc members can escalate memory usage for large 337 * link-edits. For now, size the section name dynamically. 338 */ 339 ssize = strlen(isp->is_name); 340 if ((strncmp(isp->is_name, gisp->is_name, ssize) == 0) && 341 (gisp->is_name[ssize] == '.')) 342 return ((char *)&gisp->is_name[ssize]); 343 } 344 return (NULL); 345 } 346 347 /* 348 * GNU .gnu.linkonce sections follow a naming convention that indicates the 349 * required association with an output section. Determine whether this input 350 * section follows the convention, and if so return the appropriate output 351 * section name. 352 * 353 * .gnu.linkonce.b.* -> .bss 354 * .gnu.linkonce.d.* -> .data 355 * .gnu.linkonce.l.* -> .ldata 356 * .gnu.linkonce.lb.* -> .lbss 357 * .gnu.linkonce.lr.* -> .lrodata 358 * .gnu.linkonce.r.* -> .rodata 359 * .gnu.linkonce.s.* -> .sdata 360 * .gnu.linkonce.s2.* -> .sdata2 361 * .gnu.linkonce.sb.* -> .sbss 362 * .gnu.linkonce.sb2.* -> .sbss2 363 * .gnu.linkonce.t.* -> .text 364 * .gnu.linkonce.tb.* -> .tbss 365 * .gnu.linkonce.td.* -> .tdata 366 * .gnu.linkonce.wi.* -> .debug_info 367 */ 368 #define NSTR_CH1(ch) (*(nstr + 1) == (ch)) 369 #define NSTR_CH2(ch) (*(nstr + 2) == (ch)) 370 #define NSTR_CH3(ch) (*(nstr + 3) == (ch)) 371 372 static const char * 373 gnu_linkonce_sec(const char *ostr) 374 { 375 const char *nstr = &ostr[MSG_SCN_GNU_LINKONCE_SIZE]; 376 377 switch (*nstr) { 378 case 'b': 379 if (NSTR_CH1('.')) 380 return (MSG_ORIG(MSG_SCN_BSS)); 381 break; 382 case 'd': 383 if (NSTR_CH1('.')) 384 return (MSG_ORIG(MSG_SCN_DATA)); 385 break; 386 case 'l': 387 if (NSTR_CH1('.')) 388 return (MSG_ORIG(MSG_SCN_LDATA)); 389 else if (NSTR_CH1('b') && NSTR_CH2('.')) 390 return (MSG_ORIG(MSG_SCN_LBSS)); 391 else if (NSTR_CH1('r') && NSTR_CH2('.')) 392 return (MSG_ORIG(MSG_SCN_LRODATA)); 393 break; 394 case 'r': 395 if (NSTR_CH1('.')) 396 return (MSG_ORIG(MSG_SCN_RODATA)); 397 break; 398 case 's': 399 if (NSTR_CH1('.')) 400 return (MSG_ORIG(MSG_SCN_SDATA)); 401 else if (NSTR_CH1('2') && NSTR_CH2('.')) 402 return (MSG_ORIG(MSG_SCN_SDATA2)); 403 else if (NSTR_CH1('b') && NSTR_CH2('.')) 404 return (MSG_ORIG(MSG_SCN_SBSS)); 405 else if (NSTR_CH1('b') && NSTR_CH2('2') && NSTR_CH3('.')) 406 return (MSG_ORIG(MSG_SCN_SBSS2)); 407 break; 408 case 't': 409 if (NSTR_CH1('.')) 410 return (MSG_ORIG(MSG_SCN_TEXT)); 411 else if (NSTR_CH1('b') && NSTR_CH2('.')) 412 return (MSG_ORIG(MSG_SCN_TBSS)); 413 else if (NSTR_CH1('d') && NSTR_CH2('.')) 414 return (MSG_ORIG(MSG_SCN_TDATA)); 415 break; 416 case 'w': 417 if (NSTR_CH1('i') && NSTR_CH2('.')) 418 return (MSG_ORIG(MSG_SCN_DEBUG_INFO)); 419 break; 420 default: 421 break; 422 } 423 424 /* 425 * No special name match found. 426 */ 427 return (ostr); 428 } 429 #undef NSTR_CH1 430 #undef NSTR_CH2 431 #undef NSTR_CH3 432 433 /* 434 * The GNU link-editor maps sections generated by the GNU compiler separately 435 * due to -ffunction-sections, -fdata-sections or for other reasons into the 436 * "normal" section represented. 437 * 438 * Sections are named .<main>.<symbol> where <main> is the usual section to 439 * which it should be mapped, and <symbol> is providing the unique name for 440 * the original section. Both parts of the name may contain periods, in cases 441 * where the unique part of the name contains a '.' and/or the section it 442 * contributes to does (such as .data.rel.ro) 443 * 444 * .rodata.str* and .rodata.cst* are mapped to .rodata. 445 * 446 * As a further complication, the GNU link-editor may or may not merge 447 * .ctors.* and .dtors.* into init_array and fini_array, rather than ctors and 448 * dtors. We do not implement this at this time. 449 * 450 * The GNU link editor may also arrange for sections with .local in their name 451 * to be mapped as above, but grouped together. We do not implement this (and 452 * do not merge them at all, to make this clear) 453 * 454 * This table is processed in order. Longer mappings must come first. 455 */ 456 static struct split_sec_mapping { 457 char *leader; 458 char *section; 459 boolean_t precise; 460 } split_sec_mapping[] = { 461 { ".bss.", ".bss", B_FALSE }, 462 { ".ctors.", ".ctors", B_FALSE }, 463 { ".data.rel.local.", ".data.rel.local", B_FALSE }, 464 { ".data.rel.local", ".data.rel.local", B_TRUE }, 465 { ".data.rel.ro.local.", ".data.rel.ro", B_FALSE }, 466 { ".data.rel.ro.", ".data.rel.ro", B_FALSE }, 467 { ".data.rel.ro", ".data.rel.ro", B_TRUE }, 468 { ".data.rel.", ".data.rel", B_FALSE }, 469 { ".data.rel", ".data.rel", B_TRUE }, 470 { ".data.", ".data", B_FALSE }, 471 { ".dtors.", ".dtors", B_FALSE }, 472 { ".fini_array.", ".fini_array", B_FALSE }, 473 { ".init_array.", ".init_array", B_FALSE }, 474 { ".lbss.", ".lbss", B_FALSE }, 475 { ".ldata.", ".ldata", B_FALSE }, 476 { ".lrodata.", ".lrodata", B_FALSE }, 477 /* This intentionally applies to .rodata.cstN and .rodata.strN, too */ 478 { ".rodata.", ".rodata", B_FALSE }, 479 { ".sbss2.", ".sbss2", B_FALSE }, 480 { ".sbss.", ".sbss", B_FALSE }, 481 { ".sdata2.", ".sdata2", B_FALSE }, 482 { ".sdata.", ".sdata", B_FALSE }, 483 { ".tbss.", ".tbss", B_FALSE }, 484 { ".tdata.", ".tdata", B_FALSE }, 485 { ".text.", ".text", B_FALSE }, 486 { NULL, NULL, B_FALSE } 487 }; 488 489 static const char * 490 gnu_split_sec(const char *ostr) 491 { 492 struct split_sec_mapping *mp; 493 494 for (mp = split_sec_mapping; mp->leader != NULL; mp++) { 495 if (mp->precise) { 496 if (strcmp(ostr, mp->leader) == 0) 497 return (mp->section); 498 } else if (strncmp(ostr, mp->leader, strlen(mp->leader)) == 0) { 499 return (mp->section); 500 } 501 } 502 503 return (ostr); 504 } 505 506 /* 507 * Initialize a path info buffer for use with ld_place_section(). 508 * 509 * entry: 510 * ofl - Output descriptor 511 * ifl - Descriptor for input file, or NULL if there is none. 512 * info - Address of buffer to be initialized. 513 * 514 * exit: 515 * If this is an input file, and if the entrance criteria list 516 * contains at least one criteria that has a non-empty file string 517 * match list (ec_files), then the block pointed at by info is 518 * initialized, and info is returned. 519 * 520 * If there is no input file, and/or no entrance criteria containing 521 * a non-empty ec_files list, then NULL is returned. This is not 522 * an error --- the NULL is simply an optimization, understood by 523 * ld_place_path(), that allows it to skip unnecessary work. 524 */ 525 Place_path_info * 526 ld_place_path_info_init(Ofl_desc *ofl, Ifl_desc *ifl, Place_path_info *info) 527 { 528 /* 529 * Return NULL if there is no input file (internally generated section) 530 * or if the entrance criteria list does not contain any items that will 531 * need to be compared to the path (all the ec_files lists are empty). 532 */ 533 if ((ifl == NULL) || !(ofl->ofl_flags & FLG_OF_EC_FILES)) 534 return (NULL); 535 536 info->ppi_path = ifl->ifl_name; 537 info->ppi_path_len = strlen(info->ppi_path); 538 info->ppi_isar = (ifl->ifl_flags & FLG_IF_EXTRACT) != 0; 539 540 /* 541 * The basename is the final segment of the path, equivalent to 542 * the path itself if there are no '/' delimiters. 543 */ 544 info->ppi_bname = strrchr(info->ppi_path, '/'); 545 if (info->ppi_bname == NULL) 546 info->ppi_bname = info->ppi_path; 547 else 548 info->ppi_bname++; /* Skip leading '/' */ 549 info->ppi_bname_len = 550 info->ppi_path_len - (info->ppi_bname - info->ppi_path); 551 552 /* 553 * For an archive, the object name is the member name, which is 554 * enclosed in () at the end of the name string. Otherwise, it is 555 * the same as the basename. 556 */ 557 if (info->ppi_isar) { 558 info->ppi_oname = strrchr(info->ppi_bname, '('); 559 /* There must be an archive member suffix delimited by parens */ 560 assert((info->ppi_bname[info->ppi_bname_len - 1] == ')') && 561 (info->ppi_oname != NULL)); 562 info->ppi_oname++; /* skip leading '(' */ 563 info->ppi_oname_len = info->ppi_bname_len - 564 (info->ppi_oname - info->ppi_bname + 1); 565 } else { 566 info->ppi_oname = info->ppi_bname; 567 info->ppi_oname_len = info->ppi_bname_len; 568 } 569 570 return (info); 571 } 572 573 /* 574 * Compare an input section path to the file comparison list the given 575 * entrance criteria. 576 * 577 * entry: 578 * path_info - A non-NULL Place_path_info block for the file 579 * containing the input section, initialized by 580 * ld_place_path_info_init() 581 * enp - Entrance criteria with a non-empty ec_files list of file 582 * comparisons to be carried out. 583 * 584 * exit: 585 * Return TRUE if a match is seen, and FALSE otherwise. 586 */ 587 static Boolean 588 eval_ec_files(Place_path_info *path_info, Ent_desc *enp) 589 { 590 Aliste idx; 591 Ent_desc_file *edfp; 592 size_t cmp_len; 593 const char *cmp_str; 594 595 for (ALIST_TRAVERSE(enp->ec_files, idx, edfp)) { 596 Word type = edfp->edf_flags & TYP_ECF_MASK; 597 598 /* 599 * Determine the starting character, and # of characters, 600 * from the file path to compare against this entrance criteria 601 * file string. 602 */ 603 if (type == TYP_ECF_OBJNAME) { 604 cmp_str = path_info->ppi_oname; 605 cmp_len = path_info->ppi_oname_len; 606 } else { 607 int ar_stat_diff = path_info->ppi_isar != 608 ((edfp->edf_flags & FLG_ECF_ARMEMBER) != 0); 609 610 /* 611 * If the entrance criteria specifies an archive member 612 * and the file does not, then there can be no match. 613 */ 614 615 if (ar_stat_diff && !path_info->ppi_isar) 616 continue; 617 618 if (type == TYP_ECF_PATH) { 619 cmp_str = path_info->ppi_path; 620 cmp_len = path_info->ppi_path_len; 621 } else { /* TYP_ECF_BASENAME */ 622 cmp_str = path_info->ppi_bname; 623 cmp_len = path_info->ppi_bname_len; 624 } 625 626 /* 627 * If the entrance criteria does not specify an archive 628 * member and the file does, then a match just requires 629 * the paths (without the archive member) to match. 630 * Reduce the length to not include the ar member or 631 * the '(' that precedes it. 632 */ 633 if (ar_stat_diff && path_info->ppi_isar) 634 cmp_len = path_info->ppi_oname - cmp_str - 1; 635 } 636 637 /* 638 * Compare the resulting string to the one from the 639 * entrance criteria. 640 */ 641 if ((cmp_len == edfp->edf_name_len) && 642 (strncmp(edfp->edf_name, cmp_str, cmp_len) == 0)) 643 return (TRUE); 644 } 645 646 return (FALSE); 647 } 648 649 /* 650 * Replace the section header for the given input section with a new section 651 * header of the specified type. All values in the replacement header other 652 * than the type retain their previous values. 653 * 654 * entry: 655 * isp - Input section to replace 656 * sh_type - New section type to apply 657 * 658 * exit: 659 * Returns the pointer to the new section header on success, and 660 * NULL for failure. 661 */ 662 static Shdr * 663 isp_convert_type(Is_desc *isp, Word sh_type) 664 { 665 Shdr *shdr; 666 667 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL) 668 return (NULL); 669 *shdr = *isp->is_shdr; 670 isp->is_shdr = shdr; 671 shdr->sh_type = sh_type; 672 return (shdr); 673 } 674 675 /* 676 * Issue a fatal warning for the given .eh_frame section, which 677 * cannot be merged with the existing .eh_frame output section. 678 */ 679 static void 680 eh_frame_muldef(Ofl_desc *ofl, Is_desc *isp) 681 { 682 Sg_desc *sgp; 683 Is_desc *isp1; 684 Os_desc *osp; 685 Aliste idx1, idx2, idx3; 686 687 /* 688 * Locate the .eh_frame output section, and use the first section 689 * assigned to it in the error message. The user can then compare 690 * the two sections to determine what attribute prevented the merge. 691 */ 692 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) { 693 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) { 694 if ((osp->os_flags & FLG_OS_EHFRAME) == 0) 695 continue; 696 697 for (idx3 = 0; idx3 < OS_ISD_NUM; idx3++) { 698 APlist *lst = osp->os_isdescs[idx3]; 699 700 if (aplist_nitems(lst) == 0) 701 continue; 702 703 isp1 = lst->apl_data[0]; 704 ld_eprintf(ofl, ERR_FATAL, 705 MSG_INTL(MSG_UPD_MULEHFRAME), 706 isp1->is_file->ifl_name, 707 EC_WORD(isp1->is_scnndx), isp1->is_name, 708 isp->is_file->ifl_name, 709 EC_WORD(isp->is_scnndx), isp->is_name); 710 return; 711 } 712 } 713 } 714 } 715 716 /* 717 * Hash the specified Os_desc on the specified segment descriptor, allocating 718 * and resizing the hash table as necessary. (We only hash when the number of 719 * output secctions exceeds a minimum, below which we deem it not worth it to 720 * have the auxiliary structure.) 721 */ 722 static void 723 os_desc_hash(Sg_desc *sgp, Os_desc *osp) 724 { 725 const size_t min_size = 31; 726 Aliste nitems, idx, idx1; 727 os_desc_hash_t *hash; 728 size_t new_size; 729 730 if ((nitems = aplist_nitems(sgp->sg_osdescs)) < min_size) { 731 return; 732 } 733 734 if ((hash = sgp->sg_hashtab) != NULL && hash->osh_hashtab != NULL) { 735 if (nitems < hash->osh_size) { 736 /* 737 * We have a hash table, and it's not undersized -- just 738 * add our newest element. 739 */ 740 idx = osp->os_namehash % hash->osh_size; 741 osp->os_hashnext = hash->osh_hashtab[idx]; 742 hash->osh_hashtab[idx] = osp; 743 return; 744 } 745 746 /* 747 * We have a hash table, but it's full: we are going to want 748 * to double our size and rehash all of the output section 749 * descriptions. First, free our old hash table... 750 */ 751 libld_free(hash->osh_hashtab); 752 new_size = (hash->osh_size << 1) - 1; 753 } else { 754 /* 755 * We either don't have a hash structure or we don't have a 756 * hash table (the partial construction of which may be due to 757 * a previous allocation failure). Determine what we want 758 * our new size to be, and allocate our hash structure as 759 * needed. 760 */ 761 new_size = min_size; 762 763 while (new_size <= nitems) 764 new_size = (new_size << 1) - 1; 765 766 if (hash == NULL) { 767 hash = libld_calloc(1, sizeof (os_desc_hash_t)); 768 769 if ((sgp->sg_hashtab = hash) == NULL) 770 return; 771 } 772 } 773 774 if ((hash->osh_hashtab = 775 libld_calloc(new_size, sizeof (void *))) == NULL) { 776 return; 777 } 778 779 /* 780 * Set our new size, and scan everything and hash it in. 781 */ 782 hash->osh_size = new_size; 783 784 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) { 785 idx = osp->os_namehash % hash->osh_size; 786 osp->os_hashnext = hash->osh_hashtab[idx]; 787 hash->osh_hashtab[idx] = osp; 788 } 789 } 790 791 /* 792 * Place a section into the appropriate segment and output section. 793 * 794 * entry: 795 * ofl - File descriptor 796 * isp - Input section descriptor of section to be placed. 797 * path_info - NULL, or pointer to Place_path_info buffer initialized 798 * by ld_place_path_info_init() for the file associated to isp, 799 * for use in processing entrance criteria with non-empty 800 * file matching string list (ec_files) 801 * ident - Section identifier, used to order sections relative to 802 * others within the output segment. 803 * alt_os_name - If non-NULL, the name of the output section to place 804 * isp into. If NULL, input sections go to an output section 805 * with the same name as the input section. 806 */ 807 Os_desc * 808 ld_place_section(Ofl_desc *ofl, Is_desc *isp, Place_path_info *path_info, 809 int ident, const char *alt_os_name) 810 { 811 Ent_desc *enp; 812 Sg_desc *sgp; 813 Os_desc *osp; 814 Aliste idx1, iidx; 815 int os_ndx; 816 Shdr *shdr = isp->is_shdr; 817 Xword shflagmask, shflags = shdr->sh_flags; 818 Ifl_desc *ifl = isp->is_file; 819 char *oname, *sname; 820 uint_t onamehash; 821 Boolean is_ehframe = (isp->is_flags & FLG_IS_EHFRAME) != 0; 822 Boolean linear_scan = TRUE; 823 os_desc_hash_t *hash; 824 825 /* 826 * Define any sections that must be thought of as referenced. These 827 * sections may not be referenced externally in a manner ld(1) can 828 * discover, but they must be retained (ie. not removed by -zignore). 829 */ 830 static const Msg RefSecs[] = { 831 MSG_SCN_INIT, /* MSG_ORIG(MSG_SCN_INIT) */ 832 MSG_SCN_FINI, /* MSG_ORIG(MSG_SCN_FINI) */ 833 MSG_SCN_EX_RANGES, /* MSG_ORIG(MSG_SCN_EX_RANGES) */ 834 MSG_SCN_EX_SHARED, /* MSG_ORIG(MSG_SCN_EX_SHARED) */ 835 MSG_SCN_CTORS, /* MSG_ORIG(MSG_SCN_CTORS) */ 836 MSG_SCN_DTORS, /* MSG_ORIG(MSG_SCN_DTORS) */ 837 MSG_SCN_EHFRAME, /* MSG_ORIG(MSG_SCN_EHFRAME) */ 838 MSG_SCN_EHFRAME_HDR, /* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */ 839 MSG_SCN_JCR, /* MSG_ORIG(MSG_SCN_JCR) */ 840 MSG_SCN_INITARRAY, /* MSG_ORIG(MSG_SCN_INITARRAY) */ 841 MSG_SCN_FINIARRAY, /* MSG_ORIG(MSG_SCN_FINIARRAY) */ 842 MSG_SCN_PREINITARRAY, /* MSG_ORIG(MSG_SCN_PREINITARRAY) */ 843 0 844 }; 845 846 DBG_CALL(Dbg_sec_in(ofl->ofl_lml, isp)); 847 848 /* 849 * If this section identifies group members, or this section indicates 850 * that it is a member of a group, determine whether the section is 851 * still required. 852 */ 853 if ((shflags & SHF_GROUP) || (shdr->sh_type == SHT_GROUP)) { 854 Group_desc *gdesc; 855 856 if ((gdesc = ld_get_group(ofl, isp)) != NULL) { 857 DBG_CALL(Dbg_sec_group(ofl->ofl_lml, isp, gdesc)); 858 859 /* 860 * If this group has been replaced by another group, 861 * then this section needs to be discarded. 862 */ 863 if (gdesc->gd_oisc) { 864 isp->is_flags |= FLG_IS_DISCARD; 865 866 /* 867 * Since we're discarding the section, we 868 * can skip assigning it to an output section. 869 * The exception is that if the user 870 * specifies -z relaxreloc, then 871 * we need to assign the output section so 872 * that the sloppy relocation logic will have 873 * the information necessary to do its work. 874 */ 875 if (!(ofl->ofl_flags1 & FLG_OF1_RLXREL)) 876 return (NULL); 877 } 878 } 879 880 /* 881 * SHT_GROUP sections can only be included into relocatable 882 * objects. 883 */ 884 if (shdr->sh_type == SHT_GROUP) { 885 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) { 886 isp->is_flags |= FLG_IS_DISCARD; 887 return (NULL); 888 } 889 } 890 } 891 892 /* 893 * Always assign SHF_TLS sections to the DATA segment (and then the 894 * PT_TLS embedded inside of there). 895 */ 896 if (shflags & SHF_TLS) 897 shflags |= SHF_WRITE; 898 899 /* 900 * Traverse the entrance criteria list searching for a segment that 901 * matches the input section we have. If an entrance criterion is set 902 * then there must be an exact match. If we complete the loop without 903 * finding a segment, then sgp will be NULL. 904 */ 905 sgp = NULL; 906 for (APLIST_TRAVERSE(ofl->ofl_ents, idx1, enp)) { 907 908 /* Disabled segments are not available for assignment */ 909 if (enp->ec_segment->sg_flags & FLG_SG_DISABLED) 910 continue; 911 912 /* 913 * If an entrance criteria doesn't have any of its fields 914 * set, it will match any section it is tested against. 915 * We set the FLG_EC_CATCHALL flag on these, primarily because 916 * it helps readers of our debug output to understand what 917 * the criteria means --- otherwise the user would just see 918 * that every field is 0, but might not understand the 919 * significance of that. 920 * 921 * Given that we set this flag, we can use it here as an 922 * optimization to short circuit all of the tests in this 923 * loop. Note however, that if we did not do this, the end 924 * result would be the same --- the empty criteria will sail 925 * past the following tests and reach the end of the loop. 926 */ 927 if (enp->ec_flags & FLG_EC_CATCHALL) { 928 sgp = enp->ec_segment; 929 break; 930 } 931 932 if (enp->ec_type && (enp->ec_type != shdr->sh_type)) 933 continue; 934 if (enp->ec_attrmask && 935 /* LINTED */ 936 (enp->ec_attrmask & enp->ec_attrbits) != 937 (enp->ec_attrmask & shflags)) 938 continue; 939 if (enp->ec_is_name && 940 (strcmp(enp->ec_is_name, isp->is_name) != 0)) 941 continue; 942 943 if ((alist_nitems(enp->ec_files) > 0) && 944 ((path_info == NULL) || !eval_ec_files(path_info, enp))) 945 continue; 946 947 /* All entrance criteria tests passed */ 948 sgp = enp->ec_segment; 949 break; 950 } 951 952 /* 953 * The final entrance criteria record is a FLG_EC_CATCHALL that points 954 * at the final predefined segment "extra", and this final segment is 955 * tagged FLG_SG_NODISABLE. Therefore, the above loop must always find 956 * a segment. 957 */ 958 assert(sgp != NULL); 959 960 /* 961 * Transfer the input section sorting key from the entrance criteria 962 * to the input section. A non-zero value means that the section 963 * will be sorted on this key amoung the other sections that have a 964 * non-zero key. These sorted sections are collectively placed at the 965 * head of the output section. 966 * 967 * If the sort key is 0, the section is placed after the sorted 968 * sections in the order they are encountered. 969 */ 970 isp->is_ordndx = enp->ec_ordndx; 971 972 /* Remember that this entrance criteria has placed a section */ 973 enp->ec_flags |= FLG_EC_USED; 974 975 /* 976 * If our caller has supplied an alternative name for the output 977 * section, then we defer to their request. Otherwise, the default 978 * is to use the same name as that of the input section being placed. 979 * 980 * The COMDAT, SHT_GROUP and GNU name translations that follow have 981 * the potential to alter this initial name. 982 */ 983 oname = (char *)((alt_os_name == NULL) ? isp->is_name : alt_os_name); 984 985 /* 986 * Solaris section names may follow the convention: 987 * 988 * section-name%symbol-name 989 * 990 * This convention has been used to order the layout of sections within 991 * segments for objects built with the compilers -xF option. However, 992 * the final object should not contain individual section headers for 993 * all such input sections, instead the symbol name is stripped from the 994 * name to establish the final output section name. 995 * 996 * This convention has also been followed for COMDAT and sections 997 * identified though SHT_GROUP data. 998 * 999 * Strip out the % from the section name for: 1000 * - Non-relocatable objects 1001 * - Relocatable objects if input section sorting is 1002 * in force for the segment in question. 1003 */ 1004 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) || 1005 (sgp->sg_flags & FLG_SG_IS_ORDER)) { 1006 if ((sname = strchr(isp->is_name, '%')) != NULL) { 1007 size_t size = sname - isp->is_name; 1008 1009 if ((oname = libld_malloc(size + 1)) == NULL) 1010 return ((Os_desc *)S_ERROR); 1011 (void) strncpy(oname, isp->is_name, size); 1012 oname[size] = '\0'; 1013 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname)); 1014 } 1015 } 1016 1017 /* 1018 * When building relocatable objects, we must not redirect COMDAT 1019 * section names into their outputs, such that our output object may 1020 * be successfully used as an input object also requiring COMDAT 1021 * processing 1022 */ 1023 1024 /* 1025 * GNU section names may follow the convention: 1026 * 1027 * .gnu.linkonce.* 1028 * 1029 * The .gnu.linkonce is a section naming convention that indicates a 1030 * COMDAT requirement. Determine whether this section follows the GNU 1031 * pattern, and if so, determine whether this section should be 1032 * discarded or retained. The comparison of is_name[1] with 'g' 1033 * is an optimization to skip using strncmp() too much. This is safe, 1034 * because we know the name is not NULL, and therefore must have 1035 * at least one character plus a NULL termination. 1036 */ 1037 if ((isp->is_name == oname) && (isp->is_name[1] == 'g') && 1038 (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name, 1039 MSG_SCN_GNU_LINKONCE_SIZE) == 0)) { 1040 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) { 1041 if ((oname = (char *)gnu_linkonce_sec(isp->is_name)) != 1042 isp->is_name) { 1043 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, 1044 oname)); 1045 } 1046 } 1047 1048 /* 1049 * Explicitly identify this section type as COMDAT. Also, 1050 * enable relaxed relocation processing, as this is typically 1051 * a requirement with .gnu.linkonce sections. 1052 */ 1053 isp->is_flags |= FLG_IS_COMDAT; 1054 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) 1055 ofl->ofl_flags1 |= FLG_OF1_RLXREL; 1056 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, TRUE, 1057 (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0)); 1058 } 1059 1060 /* 1061 * GNU section names may also follow the convention: 1062 * 1063 * section-name.symbol-name 1064 * 1065 * This convention is used when defining SHT_GROUP sections of type 1066 * COMDAT. Thus, any group processing will have discovered any group 1067 * sections, and this identification can be triggered by a pattern 1068 * match section names. 1069 */ 1070 if ((isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) && 1071 ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) { 1072 size_t size = sname - isp->is_name; 1073 1074 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) { 1075 if ((oname = libld_malloc(size + 1)) == NULL) 1076 return ((Os_desc *)S_ERROR); 1077 (void) strncpy(oname, isp->is_name, size); 1078 oname[size] = '\0'; 1079 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname)); 1080 } 1081 1082 /* 1083 * Enable relaxed relocation processing, as this is 1084 * typically a requirement with GNU COMDAT sections. 1085 */ 1086 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) { 1087 ofl->ofl_flags1 |= FLG_OF1_RLXREL; 1088 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, 1089 FALSE, TRUE)); 1090 } 1091 } 1092 1093 /* 1094 * GNU section names named section-name.symbol-name which are not 1095 * members of COMDAT groups are merged according to the behaviour of 1096 * the GNU link-editor. 1097 * 1098 * See the description of gnu_split_sec(). 1099 */ 1100 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && 1101 (isp->is_name == oname) && 1102 ((oname = (char *)gnu_split_sec(oname)) != isp->is_name)) { 1103 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname)); 1104 } 1105 1106 /* 1107 * Assign a hash value now that the output section name has been 1108 * finalized. 1109 */ 1110 onamehash = sgs_str_hash(oname); 1111 1112 /* 1113 * Determine if output section ordering is turned on. If so, return 1114 * the appropriate ordering index for the section. This information 1115 * is derived from the Sg_desc->sg_os_order list that was built 1116 * up from the Mapfile. 1117 * 1118 * A value of 0 for os_ndx means that the section is not sorted 1119 * (i.e. is not found in the sg_os_order). The items in sg_os_order 1120 * are in the desired sort order, so adding 1 to their alist index 1121 * gives a suitable index for sorting. 1122 */ 1123 os_ndx = 0; 1124 if (alist_nitems(sgp->sg_os_order) > 0) { 1125 Sec_order *scop; 1126 1127 for (ALIST_TRAVERSE(sgp->sg_os_order, idx1, scop)) { 1128 if (strcmp(scop->sco_secname, oname) == 0) { 1129 scop->sco_flags |= FLG_SGO_USED; 1130 os_ndx = idx1 + 1; 1131 break; 1132 } 1133 } 1134 } 1135 1136 /* 1137 * Mask of section header flags to ignore when matching sections. We 1138 * are more strict with relocatable objects, ignoring only the order 1139 * flags, and keeping sections apart if they differ otherwise. This 1140 * follows the policy that sections in a relative object should only 1141 * be merged if their flags are the same, and avoids destroying 1142 * information prematurely. For final products however, we ignore all 1143 * flags that do not prevent a merge. 1144 */ 1145 shflagmask = 1146 (ofl->ofl_flags & FLG_OF_RELOBJ) ? ALL_SHF_ORDER : ALL_SHF_IGNORE; 1147 1148 /* 1149 * Traverse the input section list for the output section we have been 1150 * assigned. If we find a matching section simply add this new section. 1151 */ 1152 iidx = 0; 1153 1154 /* 1155 * To not become quadratic with respect to the number of output 1156 * sections, we want to avoid a scan of the existing output sections 1157 * on every section placement. To effect this, we use a hash table 1158 * for sections when the number of sections gets sufficiently large; 1159 * if this hash table is present, we check the absence of the other 1160 * (uncommon) conditions that necessitate a linear scan, using the 1161 * hash chain if we can. 1162 */ 1163 if (os_ndx == 0 && (hash = sgp->sg_hashtab) != NULL) { 1164 APlist *list = sgp->sg_osdescs; 1165 1166 osp = list->apl_data[list->apl_nitems - 1]; 1167 1168 if (ident >= osp->os_identndx) { 1169 linear_scan = FALSE; 1170 osp = hash->osh_hashtab[onamehash % hash->osh_size]; 1171 } 1172 } 1173 1174 /* 1175 * We now want to iterate over output sections, looking for a match 1176 * or an insertion point. Note that this loop condition is a bit 1177 * convoluted because it's encoding two different ways of iterating 1178 * over output section descriptors: if it's a linear scan, we will 1179 * iterate over the list elements, and if it's not a linear scan we 1180 * will iterate over the hash chain that we discovered above. (The 1181 * only condition that will actually change over the loop is osp; the 1182 * mechanics of iterating to the next section depend on linear_scan 1183 * and are contained in the body of the loop.) 1184 */ 1185 while ((linear_scan && sgp->sg_osdescs != NULL) || 1186 (!linear_scan && osp != NULL)) { 1187 if (linear_scan) { 1188 if ((idx1 = iidx) >= sgp->sg_osdescs->apl_nitems) 1189 break; 1190 1191 osp = sgp->sg_osdescs->apl_data[idx1]; 1192 } 1193 1194 Shdr *os_shdr = osp->os_shdr; 1195 1196 /* 1197 * An input section matches an output section if: 1198 * - The ident values match 1199 * - The names match 1200 * - Not a GROUP section 1201 * - Not a GROUP member, if producing a relocatable object 1202 * - Not a DTrace dof section 1203 * - Section types match 1204 * - Matching section flags, after screening out the 1205 * shflagmask flags. 1206 * 1207 * Section types are considered to match if any one of 1208 * the following are true: 1209 * - The type codes are the same 1210 * - Both are .eh_frame sections (regardless of type code) 1211 * - The input section is COMDAT, and the output section 1212 * is SHT_PROGBITS. 1213 */ 1214 if ((ident == osp->os_identndx) && 1215 (ident != ld_targ.t_id.id_rel) && 1216 (onamehash == osp->os_namehash) && 1217 (shdr->sh_type != SHT_GROUP) && 1218 (((shdr->sh_flags & SHF_GROUP) == 0) || 1219 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) && 1220 (shdr->sh_type != SHT_SUNW_dof) && 1221 ((shdr->sh_type == os_shdr->sh_type) || 1222 (is_ehframe && (osp->os_flags & FLG_OS_EHFRAME)) || 1223 ((shdr->sh_type == SHT_SUNW_COMDAT) && 1224 (os_shdr->sh_type == SHT_PROGBITS))) && 1225 ((shflags & ~shflagmask) == 1226 (os_shdr->sh_flags & ~shflagmask)) && 1227 (strcmp(oname, osp->os_name) == 0)) { 1228 uintptr_t err; 1229 1230 /* 1231 * Process any COMDAT section, keeping the first and 1232 * discarding all others. 1233 */ 1234 if ((isp->is_flags & FLG_IS_COMDAT) && 1235 ((err = add_comdat(ofl, osp, isp)) != 1)) 1236 return ((Os_desc *)err); 1237 1238 /* 1239 * Set alignment 1240 */ 1241 set_addralign(ofl, osp, isp); 1242 1243 /* 1244 * If this section is a non-empty TLS section indicate 1245 * that a PT_TLS program header is required. 1246 */ 1247 if ((shflags & SHF_TLS) && shdr->sh_size && 1248 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) 1249 ofl->ofl_flags |= FLG_OF_TLSPHDR; 1250 1251 /* 1252 * Insert the input section descriptor on the proper 1253 * output section descriptor list. 1254 * 1255 * If this segment requires input section ordering, 1256 * honor any mapfile specified ordering for otherwise 1257 * unordered sections by setting the mapfile_sort 1258 * argument of os_attach_isp() to True. 1259 */ 1260 1261 if (os_attach_isp(ofl, osp, isp, 1262 (sgp->sg_flags & FLG_SG_IS_ORDER) != 0) == 0) 1263 return ((Os_desc *)S_ERROR); 1264 1265 /* 1266 * If this input section and file is associated to an 1267 * artificially referenced output section, make sure 1268 * they are marked as referenced also. This ensures 1269 * that this input section and file isn't eliminated 1270 * when -zignore is in effect. 1271 * 1272 * See -zignore comments when creating a new output 1273 * section below. 1274 */ 1275 if (((ifl && 1276 (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) && 1277 (osp->os_flags & FLG_OS_SECTREF)) { 1278 isp->is_flags |= FLG_IS_SECTREF; 1279 if (ifl) 1280 ifl->ifl_flags |= FLG_IF_FILEREF; 1281 } 1282 1283 DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp)); 1284 return (osp); 1285 } 1286 1287 if (!linear_scan) { 1288 osp = osp->os_hashnext; 1289 continue; 1290 } 1291 1292 /* 1293 * Do we need to worry about section ordering? 1294 */ 1295 if (os_ndx) { 1296 if (osp->os_ordndx) { 1297 if (os_ndx < osp->os_ordndx) 1298 /* insert section here. */ 1299 break; 1300 else { 1301 iidx = idx1 + 1; 1302 continue; 1303 } 1304 } else { 1305 /* insert section here. */ 1306 break; 1307 } 1308 } else if (osp->os_ordndx) { 1309 iidx = idx1 + 1; 1310 continue; 1311 } 1312 1313 /* 1314 * If the new sections identifier is less than that of the 1315 * present input section we need to insert the new section 1316 * at this point. 1317 */ 1318 if (ident < osp->os_identndx) 1319 break; 1320 1321 iidx = idx1 + 1; 1322 } 1323 1324 if (!linear_scan) { 1325 iidx = sgp->sg_osdescs->apl_nitems; 1326 } 1327 1328 /* 1329 * We are adding a new output section. Update the section header 1330 * count and associated string size. 1331 * 1332 * If the input section triggering this output section has been marked 1333 * for discard, and if no other non-discarded input section comes along 1334 * to join it, then we will over count. We cannot know if this will 1335 * happen or not until all input is seen. Set FLG_OF_AJDOSCNT to 1336 * trigger a final count readjustment. 1337 */ 1338 if (isp->is_flags & FLG_IS_DISCARD) 1339 ofl->ofl_flags |= FLG_OF_ADJOSCNT; 1340 ofl->ofl_shdrcnt++; 1341 if (st_insert(ofl->ofl_shdrsttab, oname) == -1) 1342 return ((Os_desc *)S_ERROR); 1343 1344 /* 1345 * Create a new output section descriptor. 1346 */ 1347 if ((osp = libld_calloc(1, sizeof (Os_desc))) == NULL) 1348 return ((Os_desc *)S_ERROR); 1349 if ((osp->os_shdr = libld_calloc(1, sizeof (Shdr))) == NULL) 1350 return ((Os_desc *)S_ERROR); 1351 1352 /* 1353 * Convert COMDAT section to PROGBITS as this the first section of the 1354 * output section. Save any COMDAT section for later processing, as 1355 * additional COMDAT sections that match this section need discarding. 1356 */ 1357 if ((shdr->sh_type == SHT_SUNW_COMDAT) && 1358 ((shdr = isp_convert_type(isp, SHT_PROGBITS)) == NULL)) 1359 return ((Os_desc *)S_ERROR); 1360 if ((isp->is_flags & FLG_IS_COMDAT) && 1361 (add_comdat(ofl, osp, isp) == S_ERROR)) 1362 return ((Os_desc *)S_ERROR); 1363 1364 if (is_ehframe) { 1365 /* 1366 * Executable or sharable objects can have at most a single 1367 * .eh_frame section. Detect attempts to create more than 1368 * one. This occurs if the input sections have incompatible 1369 * attributes. 1370 */ 1371 if ((ofl->ofl_flags & FLG_OF_EHFRAME) && 1372 !(ofl->ofl_flags & FLG_OF_RELOBJ)) { 1373 eh_frame_muldef(ofl, isp); 1374 return ((Os_desc *)S_ERROR); 1375 } 1376 ofl->ofl_flags |= FLG_OF_EHFRAME; 1377 1378 /* 1379 * For .eh_frame sections, we always set the type to be the 1380 * type specified by the ABI. This allows .eh_frame sections 1381 * of type SHT_PROGBITS to be correctly merged with .eh_frame 1382 * sections of the ABI-defined type (e.g. SHT_AMD64_UNWIND), 1383 * with the output being of the ABI-defined type. 1384 */ 1385 osp->os_shdr->sh_type = ld_targ.t_m.m_sht_unwind; 1386 } else { 1387 osp->os_shdr->sh_type = shdr->sh_type; 1388 } 1389 1390 osp->os_shdr->sh_flags = shdr->sh_flags; 1391 osp->os_shdr->sh_entsize = shdr->sh_entsize; 1392 osp->os_name = oname; 1393 osp->os_namehash = onamehash; 1394 osp->os_ordndx = os_ndx; 1395 osp->os_sgdesc = sgp; 1396 if (is_ehframe) 1397 osp->os_flags |= FLG_OS_EHFRAME; 1398 1399 if (ifl && (shdr->sh_type == SHT_PROGBITS)) { 1400 /* 1401 * Try to preserve the intended meaning of sh_link/sh_info. 1402 * See the translate_link() in update.c. 1403 */ 1404 osp->os_shdr->sh_link = shdr->sh_link; 1405 if (shdr->sh_flags & SHF_INFO_LINK) 1406 osp->os_shdr->sh_info = shdr->sh_info; 1407 } 1408 1409 /* 1410 * When -zignore is in effect, user supplied sections and files that are 1411 * not referenced from other sections, are eliminated from the object 1412 * being produced. Some sections, although unreferenced, are special, 1413 * and must not be eliminated. Determine if this new output section is 1414 * one of those special sections, and if so mark it artificially as 1415 * referenced. Any input section and file associated to this output 1416 * section is also be marked as referenced, and thus won't be eliminated 1417 * from the final output. 1418 */ 1419 if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) { 1420 const Msg *refsec; 1421 1422 for (refsec = RefSecs; *refsec; refsec++) { 1423 if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) { 1424 osp->os_flags |= FLG_OS_SECTREF; 1425 1426 if ((ifl->ifl_flags & FLG_IF_IGNORE) || 1427 DBG_ENABLED) { 1428 isp->is_flags |= FLG_IS_SECTREF; 1429 ifl->ifl_flags |= FLG_IF_FILEREF; 1430 } 1431 break; 1432 } 1433 } 1434 } 1435 1436 /* 1437 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list, 1438 * so that they can be updated as a group later. 1439 */ 1440 if ((shdr->sh_type == SHT_GROUP) && 1441 ((isp->is_flags & FLG_IS_DISCARD) == 0) && 1442 (aplist_append(&ofl->ofl_osgroups, osp, 1443 AL_CNT_OFL_OSGROUPS) == NULL)) 1444 return ((Os_desc *)S_ERROR); 1445 1446 /* 1447 * If this section is a non-empty TLS section indicate that a PT_TLS 1448 * program header is required. 1449 */ 1450 if ((shflags & SHF_TLS) && shdr->sh_size && 1451 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) 1452 ofl->ofl_flags |= FLG_OF_TLSPHDR; 1453 1454 /* 1455 * If a non-allocatable section is going to be put into a loadable 1456 * segment then turn on the allocate bit for this section and warn the 1457 * user that we have done so. This could only happen through the use 1458 * of a mapfile. 1459 */ 1460 if ((sgp->sg_phdr.p_type == PT_LOAD) && 1461 ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) { 1462 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC), 1463 ofl->ofl_name, osp->os_name, sgp->sg_name); 1464 osp->os_shdr->sh_flags |= SHF_ALLOC; 1465 } 1466 1467 /* 1468 * Retain this sections identifier for future comparisons when placing 1469 * a section (after all sections have been processed this variable will 1470 * be used to hold the sections symbol index as we don't need to retain 1471 * the identifier any more). 1472 */ 1473 osp->os_identndx = ident; 1474 1475 /* 1476 * Set alignment. 1477 */ 1478 set_addralign(ofl, osp, isp); 1479 1480 if (os_attach_isp(ofl, osp, isp, 0) == 0) 1481 return ((Os_desc *)S_ERROR); 1482 1483 DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp)); 1484 1485 /* 1486 * Insert the new section at the offset given by iidx. If no position 1487 * for it was identified above, this will be index 0, causing the new 1488 * section to be prepended to the beginning of the section list. 1489 * Otherwise, it is the index following the section that was identified. 1490 */ 1491 if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC, 1492 iidx) == NULL) 1493 return ((Os_desc *)S_ERROR); 1494 1495 os_desc_hash(sgp, osp); 1496 1497 return (osp); 1498 } 1499