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 * Map file parsing and input section to output segment mapping. 32 */ 33 #include <stdio.h> 34 #include <string.h> 35 #include <debug.h> 36 #include "msg.h" 37 #include "_libld.h" 38 39 /* 40 * Each time a section is placed, the function set_addralign() 41 * is called. This function performs: 42 * 43 * - if the section is from an external file, check if this is empty or not. 44 * If not, we know the segment this section will belong needs a program 45 * header. (Of course, the program is needed only if this section falls 46 * into a loadable segment.) 47 * - compute the Least Common Multiplier for setting the segment alignment. 48 */ 49 static void 50 set_addralign(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp) 51 { 52 Shdr *shdr = isp->is_shdr; 53 54 /* A discarded section has no influence on the output */ 55 if (isp->is_flags & FLG_IS_DISCARD) 56 return; 57 58 /* 59 * If this section has data or will be assigned data 60 * later, mark this segment not-empty. 61 */ 62 if ((shdr->sh_size != 0) || 63 ((isp->is_flags & FLG_IS_EXTERNAL) == 0)) 64 osp->os_sgdesc->sg_flags |= FLG_SG_PHREQ; 65 66 if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) && 67 (osp->os_sgdesc->sg_phdr).p_type != PT_LOAD) 68 return; 69 70 osp->os_sgdesc->sg_align = 71 ld_lcm(osp->os_sgdesc->sg_align, shdr->sh_addralign); 72 } 73 74 /* 75 * Return the first input descriptor for a given output descriptor, 76 * or NULL if there are none. 77 */ 78 79 Is_desc * 80 ld_os_first_isdesc(Os_desc *osp) 81 { 82 int i; 83 84 for (i = 0; i < OS_ISD_NUM; i++) { 85 APlist *ap_isdesc = osp->os_isdescs[i]; 86 87 if (aplist_nitems(ap_isdesc) > 0) 88 return ((Is_desc *)ap_isdesc->apl_data[0]); 89 } 90 91 return (NULL); 92 } 93 94 /* 95 * Attach an input section to an output section 96 * 97 * entry: 98 * ofl - File descriptor 99 * osp - Output section descriptor 100 * isp - Input section descriptor 101 * mapfile_sort - True (1) if segment supports mapfile specified ordering 102 * of otherwise unordered input sections, and False (0) otherwise. 103 * 104 * exit: 105 * - The input section has been attached to the output section 106 * - If the input section is a candidate for string table merging, 107 * then it is appended to the output section's list of merge 108 * candidates (os_mstridescs). 109 * 110 * On success, returns True (1). On failure, False (0). 111 */ 112 static int 113 os_attach_isp(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp, int mapfile_sort) 114 { 115 Aliste init_arritems; 116 int os_isdescs_idx, do_append = 1; 117 118 if ((isp->is_flags & FLG_IS_ORDERED) == 0) { 119 init_arritems = AL_CNT_OS_ISDESCS; 120 os_isdescs_idx = OS_ISD_DEFAULT; 121 122 /* 123 * If section ordering was specified for an unordered section 124 * via the mapfile, then search in the OS_ISD_DEFAULT list 125 * and insert it in the specified position. Ordered sections 126 * are placed in ascending order before unordered sections 127 * (sections with an is_ordndx value of zero). 128 * 129 * If no mapfile ordering was specified, we append it in 130 * the usual way below. 131 */ 132 if (mapfile_sort && (isp->is_ordndx > 0)) { 133 APlist *ap_isdesc = osp->os_isdescs[OS_ISD_DEFAULT]; 134 Aliste idx2; 135 Is_desc *isp2; 136 137 for (APLIST_TRAVERSE(ap_isdesc, idx2, isp2)) { 138 if (isp2->is_ordndx && 139 (isp2->is_ordndx <= isp->is_ordndx)) 140 continue; 141 142 if (aplist_insert( 143 &osp->os_isdescs[OS_ISD_DEFAULT], 144 isp, init_arritems, idx2) == NULL) 145 return (0); 146 do_append = 0; 147 break; 148 } 149 } 150 } else { /* Ordered section (via shdr flags) */ 151 Word shndx; 152 153 /* SHF_ORDERED uses sh_info, SHF_LINK_ORDERED uses sh_link */ 154 shndx = (isp->is_shdr->sh_flags & SHF_ORDERED) ? 155 isp->is_shdr->sh_info : isp->is_shdr->sh_link; 156 157 if (shndx == SHN_BEFORE) { 158 init_arritems = AL_CNT_OS_ISDESCS_BA; 159 os_isdescs_idx = OS_ISD_BEFORE; 160 } else if (shndx == SHN_AFTER) { 161 init_arritems = AL_CNT_OS_ISDESCS_BA; 162 os_isdescs_idx = OS_ISD_AFTER; 163 } else { 164 init_arritems = AL_CNT_OS_ISDESCS; 165 os_isdescs_idx = OS_ISD_ORDERED; 166 } 167 } 168 169 /* 170 * If we didn't insert a section into the default list using 171 * mapfile specified ordering above, then append the input 172 * section to the appropriate list. 173 */ 174 if (do_append && aplist_append(&(osp->os_isdescs[os_isdescs_idx]), 175 isp, init_arritems) == NULL) 176 return (0); 177 isp->is_osdesc = osp; 178 179 /* 180 * A section can be merged if the following are true: 181 * - The SHF_MERGE|SHF_STRINGS flags must be set 182 * - String table compression must not be disabled (-znocompstrtab) 183 * - Mapfile ordering must not have been used. 184 * - The section must not be ordered via section header flags. 185 * - It must not be the generated section being built to 186 * replace the sections on this list. 187 */ 188 if (((isp->is_shdr->sh_flags & (SHF_MERGE | SHF_STRINGS)) != 189 (SHF_MERGE | SHF_STRINGS)) || 190 ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0) || 191 !do_append || 192 ((isp->is_flags & (FLG_IS_ORDERED | FLG_IS_GNSTRMRG)) != 0)) 193 return (1); 194 195 /* 196 * Skip sections with (sh_entsize > 1) or (sh_addralign > 1). 197 * 198 * sh_entsize: 199 * We are currently only able to merge string tables containing 200 * strings with 1-byte (char) characters. Support for wide 201 * characters will require our string table compression code 202 * to be extended to handle larger character sizes. 203 * 204 * sh_addralign: 205 * Alignments greater than 1 would require our string table 206 * compression code to insert null bytes to move each 207 * string to the required alignment. 208 */ 209 if ((isp->is_shdr->sh_entsize > 1) || 210 (isp->is_shdr->sh_addralign > 1)) { 211 DBG_CALL(Dbg_sec_unsup_strmerge(ofl->ofl_lml, isp)); 212 return (1); 213 } 214 215 if (aplist_append(&osp->os_mstrisdescs, isp, 216 AL_CNT_OS_MSTRISDESCS) == NULL) 217 return (0); 218 219 /* 220 * The SHF_MERGE|SHF_STRINGS flags tell us that the program that 221 * created the section intended it to be mergeable. The 222 * FLG_IS_INSTRMRG flag says that we have done validity testing 223 * and decided that it is safe to act on that hint. 224 */ 225 isp->is_flags |= FLG_IS_INSTRMRG; 226 227 return (1); 228 } 229 230 /* 231 * Determine whether this input COMDAT section already exists for the associated 232 * output section. If so, then discard this input section. Otherwise, this 233 * must be the first COMDAT section, thus it is kept for future comparisons. 234 */ 235 static uintptr_t 236 add_comdat(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp) 237 { 238 Isd_node isd, *isdp; 239 avl_tree_t *avlt; 240 avl_index_t where; 241 242 /* 243 * Create a COMDAT avl tree for this output section if required. 244 */ 245 if ((avlt = osp->os_comdats) == NULL) { 246 if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL) 247 return (S_ERROR); 248 avl_create(avlt, isdavl_compare, sizeof (Isd_node), 249 SGSOFFSETOF(Isd_node, isd_avl)); 250 osp->os_comdats = avlt; 251 } 252 253 /* 254 * A standard COMDAT section uses the section name as search key. 255 */ 256 isd.isd_name = isp->is_name; 257 isd.isd_hash = sgs_str_hash(isd.isd_name); 258 259 if ((isdp = avl_find(avlt, &isd, &where)) != NULL) { 260 isp->is_osdesc = osp; 261 262 /* 263 * If this section hasn't already been identified as discarded, 264 * generate a suitable diagnostic. 265 */ 266 if ((isp->is_flags & FLG_IS_DISCARD) == 0) { 267 isp->is_flags |= FLG_IS_DISCARD; 268 isp->is_comdatkeep = isdp->isd_isp; 269 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, 270 isdp->isd_isp)); 271 } 272 273 /* 274 * A discarded section does not require assignment to an output 275 * section. However, if relaxed relocations have been enabled 276 * (either from -z relaxreloc, or asserted with .gnu.linkonce 277 * processing), then this section must still be assigned to an 278 * output section so that the sloppy relocation logic will have 279 * the information necessary to do its work. 280 */ 281 return (0); 282 } 283 284 /* 285 * This is a new COMDAT section - so keep it. 286 */ 287 if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL) 288 return (S_ERROR); 289 290 isdp->isd_name = isd.isd_name; 291 isdp->isd_hash = isd.isd_hash; 292 isdp->isd_isp = isp; 293 294 avl_insert(avlt, isdp, where); 295 return (1); 296 } 297 298 /* 299 * Determine whether a GNU group COMDAT section name follows the convention 300 * 301 * section-name.symbol-name 302 * 303 * Each section within the input file is compared to see if the full section 304 * name matches the beginning of the COMDAT section, with a following '.'. 305 * A pointer to the symbol name, starting with the '.' is returned so that the 306 * caller can strip off the required section name. 307 */ 308 static char * 309 gnu_comdat_sym(Ifl_desc *ifl, Is_desc *gisp) 310 { 311 size_t ndx; 312 313 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) { 314 Is_desc *isp; 315 size_t ssize; 316 317 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) || 318 (isp == gisp) || (isp->is_name == NULL)) 319 continue; 320 321 /* 322 * It's questionable whether this size should be cached in the 323 * Is_desc. However, this seems an infrequent operation and 324 * adding Is_desc members can escalate memory usage for large 325 * link-edits. For now, size the section name dynamically. 326 */ 327 ssize = strlen(isp->is_name); 328 if ((strncmp(isp->is_name, gisp->is_name, ssize) != 0) && 329 (gisp->is_name[ssize] == '.')) 330 return ((char *)&gisp->is_name[ssize]); 331 } 332 return (NULL); 333 } 334 335 /* 336 * GNU .gnu.linkonce sections follow a naming convention that indicates the 337 * required association with an output section. Determine whether this input 338 * section follows the convention, and if so return the appropriate output 339 * section name. 340 * 341 * .gnu.linkonce.b.* -> .bss 342 * .gnu.linkonce.d.* -> .data 343 * .gnu.linkonce.l.* -> .ldata 344 * .gnu.linkonce.lb.* -> .lbss 345 * .gnu.linkonce.lr.* -> .lrodata 346 * .gnu.linkonce.r.* -> .rodata 347 * .gnu.linkonce.s.* -> .sdata 348 * .gnu.linkonce.s2.* -> .sdata2 349 * .gnu.linkonce.sb.* -> .sbss 350 * .gnu.linkonce.sb2.* -> .sbss2 351 * .gnu.linkonce.t.* -> .text 352 * .gnu.linkonce.tb.* -> .tbss 353 * .gnu.linkonce.td.* -> .tdata 354 * .gnu.linkonce.wi.* -> .debug_info 355 */ 356 #define NSTR_CH1(ch) (*(nstr + 1) == (ch)) 357 #define NSTR_CH2(ch) (*(nstr + 2) == (ch)) 358 #define NSTR_CH3(ch) (*(nstr + 3) == (ch)) 359 360 static const char * 361 gnu_linkonce_sec(const char *ostr) 362 { 363 const char *nstr = &ostr[MSG_SCN_GNU_LINKONCE_SIZE]; 364 365 switch (*nstr) { 366 case 'b': 367 if (NSTR_CH1('.')) 368 return (MSG_ORIG(MSG_SCN_BSS)); 369 break; 370 case 'd': 371 if (NSTR_CH1('.')) 372 return (MSG_ORIG(MSG_SCN_DATA)); 373 break; 374 case 'l': 375 if (NSTR_CH1('.')) 376 return (MSG_ORIG(MSG_SCN_LDATA)); 377 else if (NSTR_CH1('b') && NSTR_CH2('.')) 378 return (MSG_ORIG(MSG_SCN_LBSS)); 379 else if (NSTR_CH1('r') && NSTR_CH2('.')) 380 return (MSG_ORIG(MSG_SCN_LRODATA)); 381 break; 382 case 'r': 383 if (NSTR_CH1('.')) 384 return (MSG_ORIG(MSG_SCN_RODATA)); 385 break; 386 case 's': 387 if (NSTR_CH1('.')) 388 return (MSG_ORIG(MSG_SCN_SDATA)); 389 else if (NSTR_CH1('2') && NSTR_CH2('.')) 390 return (MSG_ORIG(MSG_SCN_SDATA2)); 391 else if (NSTR_CH1('b') && NSTR_CH2('.')) 392 return (MSG_ORIG(MSG_SCN_SBSS)); 393 else if (NSTR_CH1('b') && NSTR_CH2('2') && NSTR_CH3('.')) 394 return (MSG_ORIG(MSG_SCN_SBSS2)); 395 break; 396 case 't': 397 if (NSTR_CH1('.')) 398 return (MSG_ORIG(MSG_SCN_TEXT)); 399 else if (NSTR_CH1('b') && NSTR_CH2('.')) 400 return (MSG_ORIG(MSG_SCN_TBSS)); 401 else if (NSTR_CH1('d') && NSTR_CH2('.')) 402 return (MSG_ORIG(MSG_SCN_TDATA)); 403 break; 404 case 'w': 405 if (NSTR_CH1('i') && NSTR_CH2('.')) 406 return (MSG_ORIG(MSG_SCN_DEBUG_INFO)); 407 break; 408 default: 409 break; 410 } 411 412 /* 413 * No special name match found. 414 */ 415 return (ostr); 416 } 417 #undef NSTR_CH1 418 #undef NSTR_CH2 419 #undef NSTR_CH3 420 421 422 /* 423 * Initialize a path info buffer for use with ld_place_section(). 424 * 425 * entry: 426 * ofl - Output descriptor 427 * ifl - Descriptor for input file, or NULL if there is none. 428 * info - Address of buffer to be initialized. 429 * 430 * exit: 431 * If this is an input file, and if the entrance criteria list 432 * contains at least one criteria that has a non-empty file string 433 * match list (ec_files), then the block pointed at by info is 434 * initialized, and info is returned. 435 * 436 * If there is no input file, and/or no entrance criteria containing 437 * a non-empty ec_files list, then NULL is returned. This is not 438 * an error --- the NULL is simply an optimization, understood by 439 * ld_place_path(), that allows it to skip unnecessary work. 440 */ 441 Place_path_info * 442 ld_place_path_info_init(Ofl_desc *ofl, Ifl_desc *ifl, Place_path_info *info) 443 { 444 /* 445 * Return NULL if there is no input file (internally generated section) 446 * or if the entrance criteria list does not contain any items that will 447 * need to be compared to the path (all the ec_files lists are empty). 448 */ 449 if ((ifl == NULL) || !(ofl->ofl_flags & FLG_OF_EC_FILES)) 450 return (NULL); 451 452 info->ppi_path = ifl->ifl_name; 453 info->ppi_path_len = strlen(info->ppi_path); 454 info->ppi_isar = (ifl->ifl_flags & FLG_IF_EXTRACT) != 0; 455 456 /* 457 * The basename is the final segment of the path, equivalent to 458 * the path itself if there are no '/' delimiters. 459 */ 460 info->ppi_bname = strrchr(info->ppi_path, '/'); 461 if (info->ppi_bname == NULL) 462 info->ppi_bname = info->ppi_path; 463 else 464 info->ppi_bname++; /* Skip leading '/' */ 465 info->ppi_bname_len = 466 info->ppi_path_len - (info->ppi_bname - info->ppi_path); 467 468 /* 469 * For an archive, the object name is the member name, which is 470 * enclosed in () at the end of the name string. Otherwise, it is 471 * the same as the basename. 472 */ 473 if (info->ppi_isar) { 474 info->ppi_oname = strrchr(info->ppi_bname, '('); 475 /* There must be an archive member suffix delimited by parens */ 476 assert((info->ppi_bname[info->ppi_bname_len - 1] == ')') && 477 (info->ppi_oname != NULL)); 478 info->ppi_oname++; /* skip leading '(' */ 479 info->ppi_oname_len = info->ppi_bname_len - 480 (info->ppi_oname - info->ppi_bname + 1); 481 } else { 482 info->ppi_oname = info->ppi_bname; 483 info->ppi_oname_len = info->ppi_bname_len; 484 } 485 486 return (info); 487 } 488 489 /* 490 * Compare an input section path to the file comparison list the given 491 * entrance criteria. 492 * 493 * entry: 494 * path_info - A non-NULL Place_path_info block for the file 495 * containing the input section, initialized by 496 * ld_place_path_info_init() 497 * enp - Entrance criteria with a non-empty ec_files list of file 498 * comparisons to be carried out. 499 * 500 * exit: 501 * Return TRUE if a match is seen, and FALSE otherwise. 502 */ 503 static Boolean 504 eval_ec_files(Place_path_info *path_info, Ent_desc *enp) 505 { 506 Aliste idx; 507 Ent_desc_file *edfp; 508 size_t cmp_len; 509 const char *cmp_str; 510 511 for (ALIST_TRAVERSE(enp->ec_files, idx, edfp)) { 512 Word type = edfp->edf_flags & TYP_ECF_MASK; 513 514 /* 515 * Determine the starting character, and # of characters, 516 * from the file path to compare against this entrance criteria 517 * file string. 518 */ 519 if (type == TYP_ECF_OBJNAME) { 520 cmp_str = path_info->ppi_oname; 521 cmp_len = path_info->ppi_oname_len; 522 } else { 523 int ar_stat_diff = path_info->ppi_isar != 524 ((edfp->edf_flags & FLG_ECF_ARMEMBER) != 0); 525 526 /* 527 * If the entrance criteria specifies an archive member 528 * and the file does not, then there can be no match. 529 */ 530 531 if (ar_stat_diff && !path_info->ppi_isar) 532 continue; 533 534 if (type == TYP_ECF_PATH) { 535 cmp_str = path_info->ppi_path; 536 cmp_len = path_info->ppi_path_len; 537 } else { /* TYP_ECF_BASENAME */ 538 cmp_str = path_info->ppi_bname; 539 cmp_len = path_info->ppi_bname_len; 540 } 541 542 /* 543 * If the entrance criteria does not specify an archive 544 * member and the file does, then a match just requires 545 * the paths (without the archive member) to match. 546 * Reduce the length to not include the ar member or 547 * the '(' that precedes it. 548 */ 549 if (ar_stat_diff && path_info->ppi_isar) 550 cmp_len = path_info->ppi_oname - cmp_str - 1; 551 } 552 553 /* 554 * Compare the resulting string to the one from the 555 * entrance criteria. 556 */ 557 if ((cmp_len == edfp->edf_name_len) && 558 (strncmp(edfp->edf_name, cmp_str, cmp_len) == 0)) 559 return (TRUE); 560 } 561 562 return (FALSE); 563 } 564 565 /* 566 * Place a section into the appropriate segment and output section. 567 * 568 * entry: 569 * ofl - File descriptor 570 * isp - Input section descriptor of section to be placed. 571 * path_info - NULL, or pointer to Place_path_info buffer initialized 572 * by ld_place_path_info_init() for the file associated to isp, 573 * for use in processing entrance criteria with non-empty 574 * file matching string list (ec_files) 575 * ident - Section identifier, used to order sections relative to 576 * others within the output segment. 577 * alt_os_name - If non-NULL, the name of the output section to place 578 * isp into. If NULL, input sections go to an output section 579 * with the same name as the input section. 580 */ 581 Os_desc * 582 ld_place_section(Ofl_desc *ofl, Is_desc *isp, Place_path_info *path_info, 583 int ident, const char *alt_os_name) 584 { 585 Ent_desc *enp; 586 Sg_desc *sgp; 587 Os_desc *osp; 588 Aliste idx1, iidx; 589 int os_ndx; 590 Shdr *shdr = isp->is_shdr; 591 Xword shflagmask, shflags = shdr->sh_flags; 592 Ifl_desc *ifl = isp->is_file; 593 char *oname, *sname; 594 uint_t onamehash; 595 596 /* 597 * Define any sections that must be thought of as referenced. These 598 * sections may not be referenced externally in a manner ld(1) can 599 * discover, but they must be retained (ie. not removed by -zignore). 600 */ 601 static const Msg RefSecs[] = { 602 MSG_SCN_INIT, /* MSG_ORIG(MSG_SCN_INIT) */ 603 MSG_SCN_FINI, /* MSG_ORIG(MSG_SCN_FINI) */ 604 MSG_SCN_EX_RANGES, /* MSG_ORIG(MSG_SCN_EX_RANGES) */ 605 MSG_SCN_EX_SHARED, /* MSG_ORIG(MSG_SCN_EX_SHARED) */ 606 MSG_SCN_CTORS, /* MSG_ORIG(MSG_SCN_CTORS) */ 607 MSG_SCN_DTORS, /* MSG_ORIG(MSG_SCN_DTORS) */ 608 MSG_SCN_EHFRAME, /* MSG_ORIG(MSG_SCN_EHFRAME) */ 609 MSG_SCN_EHFRAME_HDR, /* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */ 610 MSG_SCN_JCR, /* MSG_ORIG(MSG_SCN_JCR) */ 611 0 612 }; 613 614 DBG_CALL(Dbg_sec_in(ofl->ofl_lml, isp)); 615 616 /* 617 * If this section identifies group members, or this section indicates 618 * that it is a member of a group, determine whether the section is 619 * still required. 620 */ 621 if ((shflags & SHF_GROUP) || (shdr->sh_type == SHT_GROUP)) { 622 Group_desc *gdesc; 623 624 if ((gdesc = ld_get_group(ofl, isp)) != NULL) { 625 DBG_CALL(Dbg_sec_group(ofl->ofl_lml, isp, gdesc)); 626 627 /* 628 * If this group has been replaced by another group, 629 * then this section needs to be discarded. 630 */ 631 if (gdesc->gd_oisc) { 632 isp->is_flags |= FLG_IS_DISCARD; 633 634 /* 635 * Since we're discarding the section, we 636 * can skip assigning it to an output section. 637 * The exception is that if the user 638 * specifies -z relaxreloc, then 639 * we need to assign the output section so 640 * that the sloppy relocation logic will have 641 * the information necessary to do its work. 642 */ 643 if (!(ofl->ofl_flags1 & FLG_OF1_RLXREL)) 644 return (NULL); 645 } 646 } 647 648 /* 649 * SHT_GROUP sections can only be included into relocatable 650 * objects. 651 */ 652 if (shdr->sh_type == SHT_GROUP) { 653 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) { 654 isp->is_flags |= FLG_IS_DISCARD; 655 return (NULL); 656 } 657 } 658 } 659 660 /* 661 * Always assign SHF_TLS sections to the DATA segment (and then the 662 * PT_TLS embedded inside of there). 663 */ 664 if (shflags & SHF_TLS) 665 shflags |= SHF_WRITE; 666 667 /* 668 * Traverse the entrance criteria list searching for a segment that 669 * matches the input section we have. If an entrance criterion is set 670 * then there must be an exact match. If we complete the loop without 671 * finding a segment, then sgp will be NULL. 672 */ 673 sgp = NULL; 674 for (APLIST_TRAVERSE(ofl->ofl_ents, idx1, enp)) { 675 676 /* Disabled segments are not available for assignment */ 677 if (enp->ec_segment->sg_flags & FLG_SG_DISABLED) 678 continue; 679 680 /* 681 * If an entrance criteria doesn't have any of its fields 682 * set, it will match any section it is tested against. 683 * We set the FLG_EC_CATCHALL flag on these, primarily because 684 * it helps readers of our debug output to understand what 685 * the criteria means --- otherwise the user would just see 686 * that every field is 0, but might not understand the 687 * significance of that. 688 * 689 * Given that we set this flag, we can use it here as an 690 * optimization to short circuit all of the tests in this 691 * loop. Note however, that if we did not do this, the end 692 * result would be the same --- the empty criteria will sail 693 * past the following tests and reach the end of the loop. 694 */ 695 if (enp->ec_flags & FLG_EC_CATCHALL) { 696 sgp = enp->ec_segment; 697 break; 698 } 699 700 if (enp->ec_type && (enp->ec_type != shdr->sh_type)) 701 continue; 702 if (enp->ec_attrmask && 703 /* LINTED */ 704 (enp->ec_attrmask & enp->ec_attrbits) != 705 (enp->ec_attrmask & shflags)) 706 continue; 707 if (enp->ec_is_name && 708 (strcmp(enp->ec_is_name, isp->is_name) != 0)) 709 continue; 710 711 if ((alist_nitems(enp->ec_files) > 0) && 712 ((path_info == NULL) || !eval_ec_files(path_info, enp))) 713 continue; 714 715 /* All entrance criteria tests passed */ 716 sgp = enp->ec_segment; 717 break; 718 } 719 720 /* 721 * The final entrance criteria record is a FLG_EC_CATCHALL that points 722 * at the final predefined segment "extra", and this final segment is 723 * tagged FLG_SG_NODISABLE. Therefore, the above loop must always find 724 * a segment. 725 */ 726 assert(sgp != NULL); 727 728 /* 729 * Transfer the input section sorting key from the entrance criteria 730 * to the input section. A non-zero value means that the section 731 * will be sorted on this key amoung the other sections that have a 732 * non-zero key. These sorted sections are collectively placed at the 733 * head of the output section. 734 * 735 * If the sort key is 0, the section is placed after the sorted 736 * sections in the order they are encountered. 737 */ 738 isp->is_ordndx = enp->ec_ordndx; 739 740 /* Remember that this entrance criteria has placed a section */ 741 enp->ec_flags |= FLG_EC_USED; 742 743 /* 744 * If our caller has supplied an alternative name for the output 745 * section, then we defer to their request. Otherwise, the default 746 * is to use the same name as that of the input section being placed. 747 * 748 * The COMDAT, SHT_GROUP and GNU name translations that follow have 749 * the potential to alter this initial name. 750 */ 751 oname = (char *)((alt_os_name == NULL) ? isp->is_name : alt_os_name); 752 753 /* 754 * Solaris section names may follow the convention: 755 * 756 * section-name%symbol-name 757 * 758 * This convention has been used to order the layout of sections within 759 * segments for objects built with the compilers -xF option. However, 760 * the final object should not contain individual section headers for 761 * all such input sections, instead the symbol name is stripped from the 762 * name to establish the final output section name. 763 * 764 * This convention has also been followed for COMDAT and sections 765 * identified though SHT_GROUP data. 766 * 767 * Strip out the % from the section name for: 768 * - Non-relocatable objects 769 * - Relocatable objects if input section sorting is 770 * in force for the segment in question. 771 */ 772 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) || 773 (sgp->sg_flags & FLG_SG_IS_ORDER)) { 774 if ((sname = strchr(isp->is_name, '%')) != NULL) { 775 size_t size = sname - isp->is_name; 776 777 if ((oname = libld_malloc(size + 1)) == NULL) 778 return ((Os_desc *)S_ERROR); 779 (void) strncpy(oname, isp->is_name, size); 780 oname[size] = '\0'; 781 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname)); 782 } 783 } 784 785 /* 786 * GNU section names may follow the convention: 787 * 788 * .gnu.linkonce.* 789 * 790 * The .gnu.linkonce is a section naming convention that indicates a 791 * COMDAT requirement. Determine whether this section follows the GNU 792 * pattern, and if so, determine whether this section should be 793 * discarded or retained. The comparison of is_name[1] with 'g' 794 * is an optimization to skip using strncmp() too much. This is safe, 795 * because we know the name is not NULL, and therefore must have 796 * at least one character plus a NULL termination. 797 */ 798 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && 799 (isp->is_name == oname) && (isp->is_name[1] == 'g') && 800 (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name, 801 MSG_SCN_GNU_LINKONCE_SIZE) == 0)) { 802 if ((oname = 803 (char *)gnu_linkonce_sec(isp->is_name)) != isp->is_name) { 804 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname)); 805 } 806 807 /* 808 * Explicitly identify this section type as COMDAT. Also, 809 * enable relaxed relocation processing, as this is typically 810 * a requirement with .gnu.linkonce sections. 811 */ 812 isp->is_flags |= FLG_IS_COMDAT; 813 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) 814 ofl->ofl_flags1 |= FLG_OF1_RLXREL; 815 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, TRUE, 816 (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0)); 817 } 818 819 /* 820 * GNU section names may also follow the convention: 821 * 822 * section-name.symbol-name 823 * 824 * This convention is used when defining SHT_GROUP sections of type 825 * COMDAT. Thus, any group processing will have discovered any group 826 * sections, and this identification can be triggered by a pattern 827 * match section names. 828 */ 829 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && 830 (isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) && 831 ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) { 832 size_t size = sname - isp->is_name; 833 834 if ((oname = libld_malloc(size + 1)) == NULL) 835 return ((Os_desc *)S_ERROR); 836 (void) strncpy(oname, isp->is_name, size); 837 oname[size] = '\0'; 838 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname)); 839 840 /* 841 * Enable relaxed relocation processing, as this is 842 * typically a requirement with GNU COMDAT sections. 843 */ 844 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) { 845 ofl->ofl_flags1 |= FLG_OF1_RLXREL; 846 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, 847 FALSE, TRUE)); 848 } 849 } 850 851 /* 852 * Assign a hash value now that the output section name has been 853 * finalized. 854 */ 855 onamehash = sgs_str_hash(oname); 856 857 /* 858 * Determine if output section ordering is turned on. If so, return 859 * the appropriate ordering index for the section. This information 860 * is derived from the Sg_desc->sg_os_order list that was built 861 * up from the Mapfile. 862 * 863 * A value of 0 for os_ndx means that the section is not sorted 864 * (i.e. is not found in the sg_os_order). The items in sg_os_order 865 * are in the desired sort order, so adding 1 to their alist index 866 * gives a suitable index for sorting. 867 */ 868 os_ndx = 0; 869 if (alist_nitems(sgp->sg_os_order) > 0) { 870 Sec_order *scop; 871 872 for (ALIST_TRAVERSE(sgp->sg_os_order, idx1, scop)) { 873 if (strcmp(scop->sco_secname, oname) == 0) { 874 scop->sco_flags |= FLG_SGO_USED; 875 os_ndx = idx1 + 1; 876 break; 877 } 878 } 879 } 880 881 /* 882 * Mask of section header flags to ignore when matching sections. We 883 * are more strict with relocatable objects, ignoring only the order 884 * flags, and keeping sections apart if they differ otherwise. This 885 * follows the policy that sections in a relative object should only 886 * be merged if their flags are the same, and avoids destroying 887 * information prematurely. For final products however, we ignore all 888 * flags that do not prevent a merge. 889 */ 890 shflagmask = 891 (ofl->ofl_flags & FLG_OF_RELOBJ) ? ALL_SHF_ORDER : ALL_SHF_IGNORE; 892 893 /* 894 * Traverse the input section list for the output section we have been 895 * assigned. If we find a matching section simply add this new section. 896 */ 897 iidx = 0; 898 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) { 899 Shdr *_shdr = osp->os_shdr; 900 901 if ((ident == osp->os_identndx) && 902 (ident != ld_targ.t_id.id_rel) && 903 (onamehash == osp->os_namehash) && 904 (shdr->sh_type != SHT_GROUP) && 905 (shdr->sh_type != SHT_SUNW_dof) && 906 ((shdr->sh_type == _shdr->sh_type) || 907 ((shdr->sh_type == SHT_SUNW_COMDAT) && 908 (_shdr->sh_type == SHT_PROGBITS))) && 909 ((shflags & ~shflagmask) == 910 (_shdr->sh_flags & ~shflagmask)) && 911 (strcmp(oname, osp->os_name) == 0)) { 912 uintptr_t err; 913 914 /* 915 * Process any COMDAT section, keeping the first and 916 * discarding all others. 917 */ 918 if ((isp->is_flags & FLG_IS_COMDAT) && 919 ((err = add_comdat(ofl, osp, isp)) != 1)) 920 return ((Os_desc *)err); 921 922 /* 923 * Set alignment 924 */ 925 set_addralign(ofl, osp, isp); 926 927 /* 928 * If this section is a non-empty TLS section indicate 929 * that a PT_TLS program header is required. 930 */ 931 if ((shflags & SHF_TLS) && shdr->sh_size && 932 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) 933 ofl->ofl_flags |= FLG_OF_TLSPHDR; 934 935 /* 936 * Insert the input section descriptor on the proper 937 * output section descriptor list. 938 * 939 * If this segment requires input section ordering, 940 * honor any mapfile specified ordering for otherwise 941 * unordered sections by setting the mapfile_sort 942 * argument of os_attach_isp() to True. 943 */ 944 945 if (os_attach_isp(ofl, osp, isp, 946 (sgp->sg_flags & FLG_SG_IS_ORDER) != 0) == 0) 947 return ((Os_desc *)S_ERROR); 948 949 /* 950 * If this input section and file is associated to an 951 * artificially referenced output section, make sure 952 * they are marked as referenced also. This ensures 953 * that this input section and file isn't eliminated 954 * when -zignore is in effect. 955 * 956 * See -zignore comments when creating a new output 957 * section below. 958 */ 959 if (((ifl && 960 (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) && 961 (osp->os_flags & FLG_OS_SECTREF)) { 962 isp->is_flags |= FLG_IS_SECTREF; 963 if (ifl) 964 ifl->ifl_flags |= FLG_IF_FILEREF; 965 } 966 967 DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp)); 968 return (osp); 969 } 970 971 /* 972 * Do we need to worry about section ordering? 973 */ 974 if (os_ndx) { 975 if (osp->os_ordndx) { 976 if (os_ndx < osp->os_ordndx) 977 /* insert section here. */ 978 break; 979 else { 980 iidx = idx1 + 1; 981 continue; 982 } 983 } else { 984 /* insert section here. */ 985 break; 986 } 987 } else if (osp->os_ordndx) { 988 iidx = idx1 + 1; 989 continue; 990 } 991 992 /* 993 * If the new sections identifier is less than that of the 994 * present input section we need to insert the new section 995 * at this point. 996 */ 997 if (ident < osp->os_identndx) 998 break; 999 1000 iidx = idx1 + 1; 1001 } 1002 1003 /* 1004 * We are adding a new output section. Update the section header 1005 * count and associated string size. 1006 * 1007 * If the input section triggering this output section has been marked 1008 * for discard, and if no other non-discarded input section comes along 1009 * to join it, then we will over count. We cannot know if this will 1010 * happen or not until all input is seen. Set FLG_OF_AJDOSCNT to 1011 * trigger a final count readjustment. 1012 */ 1013 if (isp->is_flags & FLG_IS_DISCARD) 1014 ofl->ofl_flags |= FLG_OF_ADJOSCNT; 1015 ofl->ofl_shdrcnt++; 1016 if (st_insert(ofl->ofl_shdrsttab, oname) == -1) 1017 return ((Os_desc *)S_ERROR); 1018 1019 /* 1020 * Create a new output section descriptor. 1021 */ 1022 if ((osp = libld_calloc(sizeof (Os_desc), 1)) == NULL) 1023 return ((Os_desc *)S_ERROR); 1024 if ((osp->os_shdr = libld_calloc(sizeof (Shdr), 1)) == NULL) 1025 return ((Os_desc *)S_ERROR); 1026 1027 /* 1028 * Convert COMDAT section to PROGBITS as this the first section of the 1029 * output section. Save any COMDAT section for later processing, as 1030 * additional COMDAT sections that match this section need discarding. 1031 */ 1032 if (shdr->sh_type == SHT_SUNW_COMDAT) { 1033 Shdr *tshdr; 1034 1035 if ((tshdr = libld_malloc(sizeof (Shdr))) == NULL) 1036 return ((Os_desc *)S_ERROR); 1037 *tshdr = *shdr; 1038 isp->is_shdr = shdr = tshdr; 1039 shdr->sh_type = SHT_PROGBITS; 1040 } 1041 if ((isp->is_flags & FLG_IS_COMDAT) && 1042 (add_comdat(ofl, osp, isp) == S_ERROR)) 1043 return ((Os_desc *)S_ERROR); 1044 1045 osp->os_shdr->sh_type = shdr->sh_type; 1046 osp->os_shdr->sh_flags = shdr->sh_flags; 1047 osp->os_shdr->sh_entsize = shdr->sh_entsize; 1048 osp->os_name = oname; 1049 osp->os_namehash = onamehash; 1050 osp->os_ordndx = os_ndx; 1051 osp->os_sgdesc = sgp; 1052 1053 if (ifl && (shdr->sh_type == SHT_PROGBITS)) { 1054 /* 1055 * Try to preserve the intended meaning of sh_link/sh_info. 1056 * See the translate_link() in update.c. 1057 */ 1058 osp->os_shdr->sh_link = shdr->sh_link; 1059 if (shdr->sh_flags & SHF_INFO_LINK) 1060 osp->os_shdr->sh_info = shdr->sh_info; 1061 } 1062 1063 /* 1064 * When -zignore is in effect, user supplied sections and files that are 1065 * not referenced from other sections, are eliminated from the object 1066 * being produced. Some sections, although unreferenced, are special, 1067 * and must not be eliminated. Determine if this new output section is 1068 * one of those special sections, and if so mark it artificially as 1069 * referenced. Any input section and file associated to this output 1070 * section is also be marked as referenced, and thus won't be eliminated 1071 * from the final output. 1072 */ 1073 if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) { 1074 const Msg *refsec; 1075 1076 for (refsec = RefSecs; *refsec; refsec++) { 1077 if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) { 1078 osp->os_flags |= FLG_OS_SECTREF; 1079 1080 if ((ifl->ifl_flags & FLG_IF_IGNORE) || 1081 DBG_ENABLED) { 1082 isp->is_flags |= FLG_IS_SECTREF; 1083 ifl->ifl_flags |= FLG_IF_FILEREF; 1084 } 1085 break; 1086 } 1087 } 1088 } 1089 1090 /* 1091 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list, 1092 * so that they can be updated as a group later. 1093 */ 1094 if ((shdr->sh_type == SHT_GROUP) && 1095 (aplist_append(&ofl->ofl_osgroups, osp, 1096 AL_CNT_OFL_OSGROUPS) == NULL)) 1097 return ((Os_desc *)S_ERROR); 1098 1099 /* 1100 * If this section is a non-empty TLS section indicate that a PT_TLS 1101 * program header is required. 1102 */ 1103 if ((shflags & SHF_TLS) && shdr->sh_size && 1104 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) 1105 ofl->ofl_flags |= FLG_OF_TLSPHDR; 1106 1107 /* 1108 * If a non-allocatable section is going to be put into a loadable 1109 * segment then turn on the allocate bit for this section and warn the 1110 * user that we have done so. This could only happen through the use 1111 * of a mapfile. 1112 */ 1113 if ((sgp->sg_phdr.p_type == PT_LOAD) && 1114 ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) { 1115 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC), 1116 ofl->ofl_name, osp->os_name, sgp->sg_name); 1117 osp->os_shdr->sh_flags |= SHF_ALLOC; 1118 } 1119 1120 /* 1121 * Retain this sections identifier for future comparisons when placing 1122 * a section (after all sections have been processed this variable will 1123 * be used to hold the sections symbol index as we don't need to retain 1124 * the identifier any more). 1125 */ 1126 osp->os_identndx = ident; 1127 1128 /* 1129 * Set alignment. 1130 */ 1131 set_addralign(ofl, osp, isp); 1132 1133 if (os_attach_isp(ofl, osp, isp, 0) == 0) 1134 return ((Os_desc *)S_ERROR); 1135 1136 DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp)); 1137 1138 /* 1139 * Insert the new section at the offset given by iidx. If no position 1140 * for it was identified above, this will be index 0, causing the new 1141 * section to be prepended to the beginning of the section list. 1142 * Otherwise, it is the index following the section that was identified. 1143 */ 1144 if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC, 1145 iidx) == NULL) 1146 return ((Os_desc *)S_ERROR); 1147 return (osp); 1148 } 1149