1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * Processing of relocatable objects and shared objects. 33 */ 34 #include <stdio.h> 35 #include <string.h> 36 #include <fcntl.h> 37 #include <unistd.h> 38 #include <link.h> 39 #include <limits.h> 40 #include <sys/stat.h> 41 #include <sys/systeminfo.h> 42 #include <debug.h> 43 #include <msg.h> 44 #include <_libld.h> 45 46 /* 47 * Decide if we can link against this input file. 48 */ 49 static int 50 ifl_verify(Ehdr * ehdr, Ofl_desc * ofl, Rej_desc * rej) 51 { 52 /* 53 * Check the validity of the elf header information for compatibility 54 * with this machine and our own internal elf library. 55 */ 56 if ((ehdr->e_machine != M_MACH) && 57 ((ehdr->e_machine != M_MACHPLUS) && 58 ((ehdr->e_flags & M_FLAGSPLUS) == 0))) { 59 rej->rej_type = SGS_REJ_MACH; 60 rej->rej_info = (uint_t)ehdr->e_machine; 61 return (0); 62 } 63 if (ehdr->e_ident[EI_DATA] != M_DATA) { 64 rej->rej_type = SGS_REJ_DATA; 65 rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA]; 66 return (0); 67 } 68 if (ehdr->e_version > ofl->ofl_dehdr->e_version) { 69 rej->rej_type = SGS_REJ_VERSION; 70 rej->rej_info = (uint_t)ehdr->e_version; 71 return (0); 72 } 73 return (1); 74 } 75 76 /* 77 * Check sanity of file header and allocate an infile descriptor 78 * for the file being processed. 79 */ 80 static Ifl_desc * 81 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl, 82 Rej_desc *rej) 83 { 84 Ifl_desc *ifl; 85 List *list; 86 Rej_desc _rej = { 0 }; 87 88 if (ifl_verify(ehdr, ofl, &_rej) == 0) { 89 _rej.rej_name = name; 90 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 91 if (rej->rej_type == 0) { 92 *rej = _rej; 93 rej->rej_name = strdup(_rej.rej_name); 94 } 95 return (0); 96 } 97 98 if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == 0) 99 return ((Ifl_desc *)S_ERROR); 100 ifl->ifl_name = name; 101 ifl->ifl_ehdr = ehdr; 102 ifl->ifl_elf = elf; 103 ifl->ifl_flags = flags; 104 105 /* 106 * Is this file using 'extended Section Indexes'. If so, use the 107 * e_shnum & e_shstrndx which can be found at: 108 * 109 * e_shnum == Shdr[0].sh_size 110 * e_shstrndx == Shdr[0].sh_link 111 */ 112 if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) { 113 Elf_Scn *scn; 114 Shdr *shdr0; 115 116 if ((scn = elf_getscn(elf, 0)) == NULL) { 117 eprintf(ofl->ofl_lml, ERR_ELF, 118 MSG_INTL(MSG_ELF_GETSCN), name); 119 ofl->ofl_flags |= FLG_OF_FATAL; 120 return ((Ifl_desc *)S_ERROR); 121 } 122 if ((shdr0 = elf_getshdr(scn)) == NULL) { 123 eprintf(ofl->ofl_lml, ERR_ELF, 124 MSG_INTL(MSG_ELF_GETSHDR), name); 125 ofl->ofl_flags |= FLG_OF_FATAL; 126 return ((Ifl_desc *)S_ERROR); 127 } 128 ifl->ifl_shnum = (Word)shdr0->sh_size; 129 if (ehdr->e_shstrndx == SHN_XINDEX) 130 ifl->ifl_shstrndx = shdr0->sh_link; 131 else 132 ifl->ifl_shstrndx = ehdr->e_shstrndx; 133 } else { 134 ifl->ifl_shnum = ehdr->e_shnum; 135 ifl->ifl_shstrndx = ehdr->e_shstrndx; 136 } 137 138 if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum, 139 sizeof (Is_desc *))) == 0) 140 return ((Ifl_desc *)S_ERROR); 141 142 /* 143 * Record this new input file on the shared object or relocatable 144 * object input file list. 145 */ 146 if (ifl->ifl_ehdr->e_type == ET_DYN) { 147 list = &ofl->ofl_sos; 148 } else { 149 list = &ofl->ofl_objs; 150 } 151 152 if (list_appendc(list, ifl) == 0) 153 return ((Ifl_desc *)S_ERROR); 154 return (ifl); 155 } 156 157 /* 158 * Process a generic section. The appropriate section information is added 159 * to the files input descriptor list. 160 */ 161 static uintptr_t 162 process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 163 Word ndx, int ident, Ofl_desc *ofl) 164 { 165 Is_desc *isp; 166 167 /* 168 * Create a new input section descriptor. If this is a NOBITS 169 * section elf_getdata() will still create a data buffer (the buffer 170 * will be null and the size will reflect the actual memory size). 171 */ 172 if ((isp = libld_calloc(sizeof (Is_desc), 1)) == 0) 173 return (S_ERROR); 174 isp->is_shdr = shdr; 175 isp->is_file = ifl; 176 isp->is_name = name; 177 isp->is_scnndx = ndx; 178 isp->is_flags = FLG_IS_EXTERNAL; 179 /* LINTED */ 180 isp->is_key = (Half)ident; 181 if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) { 182 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA), 183 ifl->ifl_name); 184 ofl->ofl_flags |= FLG_OF_FATAL; 185 return (0); 186 } 187 188 if ((shdr->sh_flags & SHF_EXCLUDE) && 189 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) { 190 isp->is_flags |= FLG_IS_DISCARD; 191 } 192 193 /* 194 * Add the new input section to the files input section list and 195 * to the output section list (some sections like .strtab and 196 * .shstrtab are not added to the output section list). 197 * 198 * If the section has the SHF_ORDERED flag on, do the ld_place_section() 199 * after all input sections from this file are read in. 200 */ 201 ifl->ifl_isdesc[ndx] = isp; 202 if (ident && (shdr->sh_flags & ALL_SHF_ORDER) == 0) 203 return ((uintptr_t)ld_place_section(ofl, isp, ident, 0)); 204 205 if (ident && (shdr->sh_flags & ALL_SHF_ORDER)) { 206 isp->is_flags |= FLG_IS_ORDERED; 207 isp->is_ident = ident; 208 209 if ((ndx != 0) && (ndx == shdr->sh_link) && 210 (shdr->sh_flags & SHF_ORDERED)) { 211 return ((uintptr_t)ld_place_section(ofl, isp, 212 ident, 0)); 213 } 214 } 215 return (1); 216 } 217 218 /* 219 * Determine the software capabilities of the object being built from the 220 * capabilities of the input relocatable objects. One software capability 221 * is presently recognized, and represented with the following (sys/elf.h): 222 * 223 * SF1_SUNW_FPKNWN use/non-use of frame pointer is known, and 224 * SF1_SUNW_FPUSED the frame pointer is in use. 225 * 226 * The resolution of the present fame pointer state, and the capabilities 227 * provided by a new input relocatable object are: 228 * 229 * new input relocatable object 230 * 231 * present | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | <unknown> 232 * state | SF1_SUNW_FPUSED | | 233 * --------------------------------------------------------------------------- 234 * SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN 235 * SF1_SUNW_FPUSED | SF1_SUNW_FPUSED | | SF1_SUNW_FPUSED 236 * --------------------------------------------------------------------------- 237 * SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN 238 * | | | 239 * --------------------------------------------------------------------------- 240 * <unknown> | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | <unknown> 241 * | SF1_SUNW_FPUSED | | 242 */ 243 static void 244 sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, const char *name) 245 { 246 Xword badval; 247 248 /* 249 * If a mapfile has established definitions to override any input 250 * capabilities, ignore any new input capabilities. 251 */ 252 if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP) { 253 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_SF_1, 254 val, M_MACH); 255 return; 256 } 257 258 /* 259 * If this object doesn't specify any capabilities, ignore it, and 260 * leave the state as is. 261 */ 262 if (val == 0) 263 return; 264 265 /* 266 * Make sure we only accept known software capabilities. Note, that 267 * an F1_SUNW_FPUSED by itself is viewed as bad practice. 268 */ 269 if ((badval = (val & ~SF1_SUNW_MASK)) != 0) { 270 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1), 271 ifl->ifl_name, name, EC_XWORD(badval)); 272 val &= SF1_SUNW_MASK; 273 } 274 if (val == SF1_SUNW_FPUSED) { 275 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1), 276 ifl->ifl_name, name, EC_XWORD(val)); 277 return; 278 } 279 280 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_SF_1, 281 ofl->ofl_sfcap_1, M_MACH); 282 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_SF_1, 283 val, M_MACH); 284 285 /* 286 * Determine the resolution of the present frame pointer and the 287 * new input relocatable objects frame pointer. 288 */ 289 if ((ofl->ofl_sfcap_1 & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) == 290 (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) { 291 /* 292 * If the new relocatable object isn't using a frame pointer, 293 * reduce the present state to unused. 294 */ 295 if ((val & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) != 296 (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) 297 ofl->ofl_sfcap_1 &= ~SF1_SUNW_FPUSED; 298 299 } else if ((ofl->ofl_sfcap_1 & SF1_SUNW_FPKNWN) == 0) { 300 /* 301 * If the present state is unknown, take the new relocatable 302 * object frame pointer usage. 303 */ 304 ofl->ofl_sfcap_1 = val; 305 } 306 307 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_SF_1, 308 ofl->ofl_sfcap_1, M_MACH); 309 } 310 311 /* 312 * Determine the hardware capabilities of the object being built from the 313 * capabilities of the input relocatable objects. There's really little to 314 * do here, other than to offer diagnostics, hardware capabilities are simply 315 * additive. 316 */ 317 static void 318 hw1_cap(Ofl_desc *ofl, Xword val) 319 { 320 /* 321 * If a mapfile has established definitions to override any input 322 * capabilities, ignore any new input capabilities. 323 */ 324 if (ofl->ofl_flags1 & FLG_OF1_OVHWCAP) { 325 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_HW_1, 326 val, M_MACH); 327 return; 328 } 329 330 /* 331 * If this object doesn't specify any capabilities, ignore it, and 332 * leave the state as is. 333 */ 334 if (val == 0) 335 return; 336 337 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_HW_1, 338 ofl->ofl_hwcap_1, M_MACH); 339 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_HW_1, val, M_MACH); 340 341 ofl->ofl_hwcap_1 |= val; 342 343 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_HW_1, 344 ofl->ofl_hwcap_1, M_MACH); 345 } 346 347 /* 348 * Process a hardware/software capabilities section. Traverse the section 349 * updating the global capabilities variables as necessary. 350 */ 351 static void 352 process_cap(Ifl_desc *ifl, Is_desc *cisp, Ofl_desc *ofl) 353 { 354 Cap *cdata; 355 Word ndx, cnum; 356 357 DBG_CALL(Dbg_cap_sec_title(ofl)); 358 359 /* 360 * The capabilities are supposed to be terminated with a CA_SUNW_NULL 361 * entry. However, the compilers have been known to not follow this 362 * convention. Use the section information to determine the number 363 * of capabilities, and skip any CA_SUNW_NULL entries. 364 */ 365 cdata = (Cap *)cisp->is_indata->d_buf; 366 cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize); 367 368 for (ndx = 0; ndx < cnum; cdata++, ndx++) { 369 switch (cdata->c_tag) { 370 case CA_SUNW_HW_1: 371 hw1_cap(ofl, cdata->c_un.c_val); 372 break; 373 case CA_SUNW_SF_1: 374 sf1_cap(ofl, cdata->c_un.c_val, ifl, 375 cisp->is_name); 376 break; 377 case CA_SUNW_NULL: 378 break; 379 default: 380 eprintf(ofl->ofl_lml, ERR_WARNING, 381 MSG_INTL(MSG_FIL_UNKCAP), 382 ifl->ifl_name, cisp->is_name, cdata->c_tag); 383 } 384 } 385 } 386 387 /* 388 * Simply process the section so that we have pointers to the data for use 389 * in later routines, however don't add the section to the output section 390 * list as we will be creating our own replacement sections later (ie. 391 * symtab and relocation). 392 */ 393 static uintptr_t 394 /* ARGSUSED5 */ 395 process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 396 Word ndx, int ident, Ofl_desc *ofl) 397 { 398 return (process_section(name, ifl, shdr, scn, ndx, M_ID_NULL, ofl)); 399 } 400 401 /* 402 * Keep a running count of relocation entries from input relocatable objects for 403 * sizing relocation buckets later. If we're building an executable, save any 404 * relocations from shared objects to determine if any copy relocation symbol 405 * has a displacement relocation against it. 406 */ 407 static uintptr_t 408 /* ARGSUSED5 */ 409 process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 410 Word ndx, int ident, Ofl_desc *ofl) 411 { 412 if (process_section(name, ifl, 413 shdr, scn, ndx, M_ID_NULL, ofl) == S_ERROR) 414 return (S_ERROR); 415 416 if (ifl->ifl_ehdr->e_type == ET_REL) { 417 if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size)) 418 /* LINTED */ 419 ofl->ofl_relocincnt += 420 (Word)(shdr->sh_size / shdr->sh_entsize); 421 } else if (ofl->ofl_flags & FLG_OF_EXEC) { 422 if (list_appendc(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx]) == 0) 423 return (S_ERROR); 424 } 425 return (1); 426 } 427 428 429 /* 430 * Process a string table section. A valid section contains an initial and 431 * final null byte. 432 */ 433 static uintptr_t 434 process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 435 Word ndx, int ident, Ofl_desc *ofl) 436 { 437 char *data; 438 size_t size; 439 Is_desc *isp; 440 uintptr_t error; 441 442 /* 443 * Never include .stab.excl sections in any output file. 444 * If the -s flag has been specified strip any .stab sections. 445 */ 446 if (((ofl->ofl_flags & FLG_OF_STRIP) && ident && 447 (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) || 448 (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident) 449 return (1); 450 451 /* 452 * If we got here to process a .shstrtab or .dynstr table, `ident' will 453 * be null. Otherwise make sure we don't have a .strtab section as this 454 * should not be added to the output section list either. 455 */ 456 if ((ident != M_ID_NULL) && 457 (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0)) 458 ident = M_ID_NULL; 459 460 error = process_section(name, ifl, shdr, scn, ndx, ident, ofl); 461 if ((error == 0) || (error == S_ERROR)) 462 return (error); 463 464 /* 465 * String tables should start and end with a NULL byte. Note, it has 466 * been known for the assembler to create empty string tables, so check 467 * the size before attempting to verify the data itself. 468 */ 469 isp = ifl->ifl_isdesc[ndx]; 470 size = isp->is_indata->d_size; 471 if (size) { 472 data = isp->is_indata->d_buf; 473 if (data[0] != '\0' || data[size - 1] != '\0') 474 eprintf(ofl->ofl_lml, ERR_WARNING, 475 MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name, name); 476 } else 477 isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY); 478 479 ifl->ifl_flags |= FLG_IF_HSTRTAB; 480 return (1); 481 } 482 483 /* 484 * Invalid sections produce a warning and are skipped. 485 */ 486 static uintptr_t 487 /* ARGSUSED3 */ 488 invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 489 Word ndx, int ident, Ofl_desc *ofl) 490 { 491 Conv_inv_buf_t inv_buf; 492 493 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC), 494 ifl->ifl_name, name, conv_sec_type(ifl->ifl_ehdr->e_machine, 495 shdr->sh_type, 0, &inv_buf)); 496 return (1); 497 } 498 499 /* 500 * Process a progbits section. 501 */ 502 static uintptr_t 503 process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 504 Word ndx, int ident, Ofl_desc *ofl) 505 { 506 int stab_index = 0; 507 508 /* 509 * Never include .stab.excl sections in any output file. 510 * If the -s flag has been specified strip any .stab sections. 511 */ 512 if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB), 513 MSG_SCN_STAB_SIZE) == 0)) { 514 if ((ofl->ofl_flags & FLG_OF_STRIP) || 515 (strcmp((name + MSG_SCN_STAB_SIZE), 516 MSG_ORIG(MSG_SCN_EXCL)) == 0)) 517 return (1); 518 519 if (strcmp((name + MSG_SCN_STAB_SIZE), 520 MSG_ORIG(MSG_SCN_INDEX)) == 0) 521 stab_index = 1; 522 } 523 524 if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) { 525 if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG), 526 MSG_SCN_DEBUG_SIZE) == 0) || 527 (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0)) 528 return (1); 529 } 530 531 /* 532 * Update the ident to reflect the type of section we've got. 533 * 534 * If there is any .plt or .got section to generate we'll be creating 535 * our own version, so don't allow any input sections of these types to 536 * be added to the output section list (why a relocatable object would 537 * have a .plt or .got is a mystery, but stranger things have occurred). 538 */ 539 if (ident) { 540 if (shdr->sh_flags & SHF_TLS) 541 ident = M_ID_TLS; 542 else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) == 543 (SHF_ALLOC | SHF_EXECINSTR)) 544 ident = M_ID_TEXT; 545 else if (shdr->sh_flags & SHF_ALLOC) { 546 if ((strcmp(name, MSG_ORIG(MSG_SCN_PLT)) == 0) || 547 (strcmp(name, MSG_ORIG(MSG_SCN_GOT)) == 0)) 548 ident = M_ID_NULL; 549 else if (stab_index) { 550 /* 551 * This is a work-around for x86 compilers that 552 * have set SHF_ALLOC for the .stab.index 553 * section. 554 * 555 * Because of this, make sure that the 556 * .stab.index does not end up as the last 557 * section in the text segment. Older linkers 558 * can produce segmentation violations when they 559 * strip (ld -s) against a shared object whose 560 * last section in the text segment is a .stab. 561 */ 562 ident = M_ID_INTERP; 563 } else 564 ident = M_ID_DATA; 565 } else 566 ident = M_ID_NOTE; 567 } 568 return (process_section(name, ifl, shdr, scn, ndx, ident, ofl)); 569 } 570 571 /* 572 * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections. 573 */ 574 static uintptr_t 575 process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 576 Word ndx, int ident, Ofl_desc *ofl) 577 { 578 /* 579 * Debug information is discarded when the 'ld -s' flag is invoked. 580 */ 581 if (ofl->ofl_flags & FLG_OF_STRIP) { 582 return (1); 583 } 584 return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl)); 585 } 586 587 /* 588 * Process a nobits section. 589 */ 590 static uintptr_t 591 process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 592 Word ndx, int ident, Ofl_desc *ofl) 593 { 594 if (ident) { 595 if (shdr->sh_flags & SHF_TLS) 596 ident = M_ID_TLSBSS; 597 #if defined(__x86) && defined(_ELF64) 598 else if (shdr->sh_flags & SHF_AMD64_LARGE) 599 ident = M_ID_LBSS; 600 #endif 601 else 602 ident = M_ID_BSS; 603 } 604 return (process_section(name, ifl, shdr, scn, ndx, ident, ofl)); 605 } 606 607 /* 608 * Process a SHT_*_ARRAY section. 609 */ 610 static uintptr_t 611 process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 612 Word ndx, int ident, Ofl_desc *ofl) 613 { 614 Os_desc *osp; 615 Is_desc *isp; 616 617 if (ident) 618 ident = M_ID_ARRAY; 619 620 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 621 return (S_ERROR); 622 623 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 624 ((osp = isp->is_osdesc) == 0)) 625 return (0); 626 627 if ((shdr->sh_type == SHT_FINI_ARRAY) && 628 (ofl->ofl_osfiniarray == 0)) 629 ofl->ofl_osfiniarray = osp; 630 else if ((shdr->sh_type == SHT_INIT_ARRAY) && 631 (ofl->ofl_osinitarray == 0)) 632 ofl->ofl_osinitarray = osp; 633 else if ((shdr->sh_type == SHT_PREINIT_ARRAY) && 634 (ofl->ofl_ospreinitarray == 0)) 635 ofl->ofl_ospreinitarray = osp; 636 637 return (1); 638 } 639 640 /* 641 * Process a SHT_SYMTAB_SHNDX section. 642 */ 643 static uintptr_t 644 process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 645 Word ndx, int ident, Ofl_desc *ofl) 646 { 647 if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 648 return (S_ERROR); 649 650 /* 651 * Have we already seen the related SYMTAB - if so verify it now. 652 */ 653 if (shdr->sh_link < ndx) { 654 Is_desc *isp = ifl->ifl_isdesc[shdr->sh_link]; 655 656 if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) && 657 (isp->is_shdr->sh_type != SHT_DYNSYM))) { 658 eprintf(ofl->ofl_lml, ERR_FATAL, 659 MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name, name, 660 EC_XWORD(shdr->sh_link)); 661 return (S_ERROR); 662 } 663 isp->is_symshndx = ifl->ifl_isdesc[ndx]; 664 } 665 return (1); 666 } 667 668 /* 669 * Final processing for SHT_SYMTAB_SHNDX section. 670 */ 671 static uintptr_t 672 /* ARGSUSED2 */ 673 sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 674 { 675 if (isc->is_shdr->sh_link > isc->is_scnndx) { 676 Is_desc *isp = ifl->ifl_isdesc[isc->is_shdr->sh_link]; 677 678 if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) && 679 (isp->is_shdr->sh_type != SHT_DYNSYM))) { 680 eprintf(ofl->ofl_lml, ERR_FATAL, 681 MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name, 682 isc->is_name, EC_XWORD(isc->is_shdr->sh_link)); 683 return (S_ERROR); 684 } 685 isp->is_symshndx = isc; 686 } 687 return (1); 688 } 689 690 /* 691 * Process .dynamic section from a relocatable object. 692 * 693 * Note: That the .dynamic section is only considered interesting when 694 * dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get 695 * set when libld is called from ld.so.1). 696 */ 697 /*ARGSUSED*/ 698 static uintptr_t 699 process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 700 Word ndx, int ident, Ofl_desc *ofl) 701 { 702 Dyn *dyn; 703 Elf_Scn *strscn; 704 Elf_Data *dp; 705 char *str; 706 707 /* 708 * Process .dynamic sections from relocatable objects ? 709 */ 710 if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0) 711 return (1); 712 713 /* 714 * Find the string section associated with the .dynamic section. 715 */ 716 if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) { 717 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN), 718 ifl->ifl_name); 719 ofl->ofl_flags |= FLG_OF_FATAL; 720 return (0); 721 } 722 dp = elf_getdata(strscn, NULL); 723 str = (char *)dp->d_buf; 724 725 /* 726 * And get the .dynamic data 727 */ 728 dp = elf_getdata(scn, NULL); 729 730 for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) { 731 Ifl_desc *difl; 732 733 switch (dyn->d_tag) { 734 case DT_NEEDED: 735 case DT_USED: 736 if (((difl = 737 libld_calloc(1, sizeof (Ifl_desc))) == 0) || 738 (list_appendc(&ofl->ofl_sos, difl) == 0)) 739 return (S_ERROR); 740 741 difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC); 742 difl->ifl_soname = str + (size_t)dyn->d_un.d_val; 743 difl->ifl_flags = FLG_IF_NEEDSTR; 744 break; 745 case DT_RPATH: 746 case DT_RUNPATH: 747 if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath, 748 (str + (size_t)dyn->d_un.d_val))) == 749 (const char *)S_ERROR) 750 return (S_ERROR); 751 break; 752 case DT_VERSYM: 753 /* 754 * The Solaris ld does not put DT_VERSYM in the 755 * dynamic section. If the object has DT_VERSYM, 756 * then it must have been produced by the GNU ld, 757 * and is using the GNU style of versioning. 758 */ 759 ifl->ifl_flags |= FLG_IF_GNUVER; 760 break; 761 } 762 } 763 return (1); 764 } 765 766 /* 767 * Expand implicit references. Dependencies can be specified in terms of the 768 * $ORIGIN, $PLATFORM, $OSREL and $OSNAME tokens, either from their needed name, 769 * or via a runpath. In addition runpaths may also specify the $ISALIST token. 770 * 771 * Probably the most common reference to explicit dependencies (via -L) will be 772 * sufficient to find any associated implicit dependencies, but just in case we 773 * expand any occurrence of these known tokens here. 774 * 775 * Note, if any errors occur we simply return the original name. 776 * 777 * This code is remarkably similar to expand() in rtld/common/paths.c. 778 */ 779 static char *platform = 0; 780 static size_t platform_sz = 0; 781 static Isa_desc *isa = 0; 782 static Uts_desc *uts = 0; 783 784 static char * 785 expand(const char *parent, const char *name, char **next) 786 { 787 char _name[PATH_MAX], *nptr, *_next; 788 const char *optr; 789 size_t nrem = PATH_MAX - 1; 790 int expanded = 0, _expanded, isaflag = 0; 791 792 optr = name; 793 nptr = _name; 794 795 while (*optr) { 796 if (nrem == 0) 797 return ((char *)name); 798 799 if (*optr != '$') { 800 *nptr++ = *optr++, nrem--; 801 continue; 802 } 803 804 _expanded = 0; 805 806 if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN), 807 MSG_STR_ORIGIN_SIZE) == 0) { 808 char *eptr; 809 810 /* 811 * For $ORIGIN, expansion is really just a concatenation 812 * of the parents directory name. For example, an 813 * explicit dependency foo/bar/lib1.so with a dependency 814 * on $ORIGIN/lib2.so would be expanded to 815 * foo/bar/lib2.so. 816 */ 817 if ((eptr = strrchr(parent, '/')) == 0) { 818 *nptr++ = '.'; 819 nrem--; 820 } else { 821 size_t len = eptr - parent; 822 823 if (len >= nrem) 824 return ((char *)name); 825 826 (void) strncpy(nptr, parent, len); 827 nptr = nptr + len; 828 nrem -= len; 829 } 830 optr += MSG_STR_ORIGIN_SIZE; 831 expanded = _expanded = 1; 832 833 } else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM), 834 MSG_STR_PLATFORM_SIZE) == 0) { 835 /* 836 * Establish the platform from sysconf - like uname -i. 837 */ 838 if ((platform == 0) && (platform_sz == 0)) { 839 char info[SYS_NMLN]; 840 long size; 841 842 size = sysinfo(SI_PLATFORM, info, SYS_NMLN); 843 if ((size != -1) && 844 (platform = libld_malloc((size_t)size))) { 845 (void) strcpy(platform, info); 846 platform_sz = (size_t)size - 1; 847 } else 848 platform_sz = 1; 849 } 850 if (platform != 0) { 851 if (platform_sz >= nrem) 852 return ((char *)name); 853 854 (void) strncpy(nptr, platform, platform_sz); 855 nptr = nptr + platform_sz; 856 nrem -= platform_sz; 857 858 optr += MSG_STR_PLATFORM_SIZE; 859 expanded = _expanded = 1; 860 } 861 862 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME), 863 MSG_STR_OSNAME_SIZE) == 0) { 864 /* 865 * Establish the os name - like uname -s. 866 */ 867 if (uts == 0) 868 uts = conv_uts(); 869 870 if (uts && uts->uts_osnamesz) { 871 if (uts->uts_osnamesz >= nrem) 872 return ((char *)name); 873 874 (void) strncpy(nptr, uts->uts_osname, 875 uts->uts_osnamesz); 876 nptr = nptr + uts->uts_osnamesz; 877 nrem -= uts->uts_osnamesz; 878 879 optr += MSG_STR_OSNAME_SIZE; 880 expanded = _expanded = 1; 881 } 882 883 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL), 884 MSG_STR_OSREL_SIZE) == 0) { 885 /* 886 * Establish the os release - like uname -r. 887 */ 888 if (uts == 0) 889 uts = conv_uts(); 890 891 if (uts && uts->uts_osrelsz) { 892 if (uts->uts_osrelsz >= nrem) 893 return ((char *)name); 894 895 (void) strncpy(nptr, uts->uts_osrel, 896 uts->uts_osrelsz); 897 nptr = nptr + uts->uts_osrelsz; 898 nrem -= uts->uts_osrelsz; 899 900 optr += MSG_STR_OSREL_SIZE; 901 expanded = _expanded = 1; 902 } 903 904 } else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST), 905 MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) { 906 /* 907 * Establish instruction sets from sysconf. Note that 908 * this is only meaningful from runpaths. 909 */ 910 if (isa == 0) 911 isa = conv_isalist(); 912 913 if (isa && isa->isa_listsz && 914 (nrem > isa->isa_opt->isa_namesz)) { 915 size_t mlen, tlen, hlen = optr - name; 916 size_t no; 917 char *lptr; 918 Isa_opt *opt = isa->isa_opt; 919 920 (void) strncpy(nptr, opt->isa_name, 921 opt->isa_namesz); 922 nptr = nptr + opt->isa_namesz; 923 nrem -= opt->isa_namesz; 924 925 optr += MSG_STR_ISALIST_SIZE; 926 expanded = _expanded = 1; 927 928 tlen = strlen(optr); 929 930 /* 931 * As ISALIST expands to a number of elements, 932 * establish a new list to return to the caller. 933 * This will contain the present path being 934 * processed redefined for each isalist option, 935 * plus the original remaining list entries. 936 */ 937 mlen = ((hlen + tlen) * (isa->isa_optno - 1)) + 938 isa->isa_listsz - opt->isa_namesz; 939 if (*next) 940 mlen += strlen(*next); 941 if ((_next = lptr = libld_malloc(mlen)) == 0) 942 return (0); 943 944 for (no = 1, opt++; no < isa->isa_optno; 945 no++, opt++) { 946 (void) strncpy(lptr, name, hlen); 947 lptr = lptr + hlen; 948 (void) strncpy(lptr, opt->isa_name, 949 opt->isa_namesz); 950 lptr = lptr + opt->isa_namesz; 951 (void) strncpy(lptr, optr, tlen); 952 lptr = lptr + tlen; 953 *lptr++ = ':'; 954 } 955 if (*next) 956 (void) strcpy(lptr, *next); 957 else 958 *--lptr = '\0'; 959 } 960 } 961 962 /* 963 * If no expansion occurred skip the $ and continue. 964 */ 965 if (_expanded == 0) 966 *nptr++ = *optr++, nrem--; 967 } 968 969 /* 970 * If any ISALIST processing has occurred not only do we return the 971 * expanded node we're presently working on, but we must also update the 972 * remaining list so that it is effectively prepended with this node 973 * expanded to all remaining isalist options. Note that we can only 974 * handle one ISALIST per node. For more than one ISALIST to be 975 * processed we'd need a better algorithm than above to replace the 976 * newly generated list. Whether we want to encourage the number of 977 * pathname permutations this would provide is another question. So, for 978 * now if more than one ISALIST is encountered we return the original 979 * node untouched. 980 */ 981 if (isaflag) { 982 if (isaflag == 1) 983 *next = _next; 984 else 985 return ((char *)name); 986 } 987 988 *nptr = '\0'; 989 990 if (expanded) { 991 if ((nptr = libld_malloc(strlen(_name) + 1)) == 0) 992 return ((char *)name); 993 (void) strcpy(nptr, _name); 994 return (nptr); 995 } 996 return ((char *)name); 997 } 998 999 /* 1000 * The Solaris ld does not put DT_VERSYM in the dynamic section, but the 1001 * GNU ld does, and it is used by the runtime linker to implement their 1002 * versioning scheme. Use this fact to determine if the sharable object 1003 * was produced by the GNU ld rather than the Solaris one, and to set 1004 * FLG_IF_GNUVER if so. This needs to be done before the symbols are 1005 * processed, since the answer determines whether we interpret the 1006 * symbols versions according to Solaris or GNU rules. 1007 */ 1008 /*ARGSUSED*/ 1009 static uintptr_t 1010 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr, 1011 Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl) 1012 { 1013 Dyn *dyn; 1014 Elf_Data *dp; 1015 1016 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 1017 return (S_ERROR); 1018 1019 /* Get the .dynamic data */ 1020 dp = elf_getdata(scn, NULL); 1021 1022 for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) { 1023 if (dyn->d_tag == DT_VERSYM) { 1024 ifl->ifl_flags |= FLG_IF_GNUVER; 1025 break; 1026 } 1027 } 1028 return (1); 1029 } 1030 1031 /* 1032 * Process a dynamic section. If we are processing an explicit shared object 1033 * then we need to determine if it has a recorded SONAME, if so, this name will 1034 * be recorded in the output file being generated as the NEEDED entry rather 1035 * than the shared objects filename itself. 1036 * If the mode of the link-edit indicates that no undefined symbols should 1037 * remain, then we also need to build up a list of any additional shared object 1038 * dependencies this object may have. In this case save any NEEDED entries 1039 * together with any associated run-path specifications. This information is 1040 * recorded on the `ofl_soneed' list and will be analyzed after all explicit 1041 * file processing has been completed (refer finish_libs()). 1042 */ 1043 static uintptr_t 1044 process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 1045 { 1046 Dyn *data, *dyn; 1047 char *str, *rpath = NULL; 1048 const char *soname, *needed; 1049 Sdf_desc *sdf; 1050 Listnode *lnp; 1051 1052 data = (Dyn *)isc->is_indata->d_buf; 1053 str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf; 1054 1055 /* 1056 * First loop through the dynamic section looking for a run path. 1057 */ 1058 if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) { 1059 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) { 1060 if ((dyn->d_tag != DT_RPATH) && 1061 (dyn->d_tag != DT_RUNPATH)) 1062 continue; 1063 if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL) 1064 continue; 1065 break; 1066 } 1067 } 1068 1069 /* 1070 * Now look for any needed dependencies (which may use the rpath) 1071 * or a new SONAME. 1072 */ 1073 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) { 1074 if (dyn->d_tag == DT_SONAME) { 1075 if ((soname = str + (size_t)dyn->d_un.d_val) == NULL) 1076 continue; 1077 1078 /* 1079 * Update the input file structure with this new name. 1080 */ 1081 ifl->ifl_soname = soname; 1082 1083 } else if ((dyn->d_tag == DT_NEEDED) || 1084 (dyn->d_tag == DT_USED)) { 1085 if (!(ofl->ofl_flags & 1086 (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC))) 1087 continue; 1088 if ((needed = str + (size_t)dyn->d_un.d_val) == NULL) 1089 continue; 1090 1091 /* 1092 * Determine if this needed entry is already recorded on 1093 * the shared object needed list, if not create a new 1094 * definition for later processing (see finish_libs()). 1095 */ 1096 needed = expand(ifl->ifl_name, needed, (char **)0); 1097 1098 if ((sdf = sdf_find(needed, &ofl->ofl_soneed)) == 0) { 1099 if ((sdf = sdf_add(needed, 1100 &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR) 1101 return (S_ERROR); 1102 sdf->sdf_rfile = ifl->ifl_name; 1103 } 1104 1105 /* 1106 * Record the runpath (Note that we take the first 1107 * runpath which is exactly what ld.so.1 would do during 1108 * its dependency processing). 1109 */ 1110 if (rpath && (sdf->sdf_rpath == 0)) 1111 sdf->sdf_rpath = rpath; 1112 1113 } else if (dyn->d_tag == DT_FLAGS_1) { 1114 if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE)) 1115 ifl->ifl_flags &= ~FLG_IF_LAZYLD; 1116 if (dyn->d_un.d_val & DF_1_DISPRELPND) 1117 ifl->ifl_flags |= FLG_IF_DISPPEND; 1118 if (dyn->d_un.d_val & DF_1_DISPRELDNE) 1119 ifl->ifl_flags |= FLG_IF_DISPDONE; 1120 if (dyn->d_un.d_val & DF_1_NODIRECT) 1121 ifl->ifl_flags |= FLG_IF_NODIRECT; 1122 1123 } else if ((dyn->d_tag == DT_AUDIT) && 1124 (ifl->ifl_flags & FLG_IF_NEEDED)) { 1125 /* 1126 * Record audit string as DT_DEPAUDIT. 1127 */ 1128 if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit, 1129 (str + (size_t)dyn->d_un.d_val))) == 1130 (const char *)S_ERROR) 1131 return (S_ERROR); 1132 1133 } else if (dyn->d_tag == DT_SUNW_RTLDINF) { 1134 /* 1135 * If a library has the SUNW_RTLDINF .dynamic entry 1136 * then we must not permit lazyloading of this library. 1137 * This is because critical startup information (TLS 1138 * routines) are provided as part of these interfaces 1139 * and we must have them as part of process startup. 1140 */ 1141 ifl->ifl_flags &= ~FLG_IF_LAZYLD; 1142 } 1143 } 1144 1145 /* 1146 * Perform some SONAME sanity checks. 1147 */ 1148 if (ifl->ifl_flags & FLG_IF_NEEDED) { 1149 Ifl_desc * sifl; 1150 1151 /* 1152 * Determine if anyone else will cause the same SONAME to be 1153 * used (this is either caused by two different files having the 1154 * same SONAME, or by one file SONAME actually matching another 1155 * file basename (if no SONAME is specified within a shared 1156 * library its basename will be used)). Probably rare, but some 1157 * idiot will do it. 1158 */ 1159 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, sifl)) { 1160 if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) && 1161 (ifl != sifl)) { 1162 const char *hint, *iflb, *siflb; 1163 1164 /* 1165 * Determine the basename of each file. Perhaps 1166 * there are multiple copies of the same file 1167 * being brought in using different -L search 1168 * paths, and if so give an extra hint in the 1169 * error message. 1170 */ 1171 iflb = strrchr(ifl->ifl_name, '/'); 1172 if (iflb == NULL) 1173 iflb = ifl->ifl_name; 1174 else 1175 iflb++; 1176 1177 siflb = strrchr(sifl->ifl_name, '/'); 1178 if (siflb == NULL) 1179 siflb = sifl->ifl_name; 1180 else 1181 siflb++; 1182 1183 if (strcmp(iflb, siflb) == 0) 1184 hint = MSG_INTL(MSG_REC_CNFLTHINT); 1185 else 1186 hint = MSG_ORIG(MSG_STR_EMPTY); 1187 1188 eprintf(ofl->ofl_lml, ERR_FATAL, 1189 MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name, 1190 ifl->ifl_name, sifl->ifl_soname, hint); 1191 ofl->ofl_flags |= FLG_OF_FATAL; 1192 return (0); 1193 } 1194 } 1195 1196 /* 1197 * If the SONAME is the same as the name the user wishes to 1198 * record when building a dynamic library (refer -h option), 1199 * we also have a name clash. 1200 */ 1201 if (ofl->ofl_soname && 1202 (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) { 1203 eprintf(ofl->ofl_lml, ERR_FATAL, 1204 MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name, 1205 ifl->ifl_soname); 1206 ofl->ofl_flags |= FLG_OF_FATAL; 1207 return (0); 1208 } 1209 } 1210 return (1); 1211 } 1212 1213 /* 1214 * Process a relocation entry. At this point all input sections from this 1215 * input file have been assigned an input section descriptor which is saved 1216 * in the `ifl_isdesc' array. 1217 */ 1218 static uintptr_t 1219 rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 1220 { 1221 Word rndx; 1222 Is_desc *risc; 1223 Os_desc *osp; 1224 Shdr *shdr = isc->is_shdr; 1225 Conv_inv_buf_t inv_buf; 1226 1227 /* 1228 * Make sure this is a valid relocation we can handle. 1229 */ 1230 if (shdr->sh_type != M_REL_SHT_TYPE) { 1231 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC), 1232 ifl->ifl_name, isc->is_name, 1233 conv_sec_type(ifl->ifl_ehdr->e_machine, 1234 shdr->sh_type, 0, &inv_buf)); 1235 ofl->ofl_flags |= FLG_OF_FATAL; 1236 return (0); 1237 } 1238 1239 /* 1240 * From the relocation section header information determine which 1241 * section needs the actual relocation. Determine which output section 1242 * this input section has been assigned to and add to its relocation 1243 * list. Note that the relocation section may be null if it is not 1244 * required (ie. .debug, .stabs, etc). 1245 */ 1246 rndx = shdr->sh_info; 1247 if (rndx >= ifl->ifl_shnum) { 1248 /* 1249 * Broken input file. 1250 */ 1251 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO), 1252 ifl->ifl_name, isc->is_name, EC_XWORD(rndx)); 1253 ofl->ofl_flags |= FLG_OF_FATAL; 1254 return (0); 1255 } 1256 if (rndx == 0) { 1257 if (list_appendc(&ofl->ofl_extrarels, isc) == 0) 1258 return (S_ERROR); 1259 } else if ((risc = ifl->ifl_isdesc[rndx]) != 0) { 1260 /* 1261 * Discard relocations if they are against a section 1262 * which has been discarded. 1263 */ 1264 if (risc->is_flags & FLG_IS_DISCARD) 1265 return (1); 1266 if ((osp = risc->is_osdesc) == 0) { 1267 if (risc->is_shdr->sh_type == SHT_SUNW_move) { 1268 /* 1269 * This section is processed later 1270 * in sunwmove_preprocess() and 1271 * reloc_init(). 1272 */ 1273 if (list_appendc(&ofl->ofl_mvrelisdescs, 1274 isc) == 0) 1275 return (S_ERROR); 1276 return (1); 1277 } 1278 eprintf(ofl->ofl_lml, ERR_FATAL, 1279 MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name, 1280 isc->is_name, risc->is_name); 1281 return (0); 1282 } 1283 if (list_appendc(&osp->os_relisdescs, isc) == 0) 1284 return (S_ERROR); 1285 } 1286 return (1); 1287 } 1288 1289 /* 1290 * SHF_EXCLUDE flags is set for this section. 1291 */ 1292 static uintptr_t 1293 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 1294 Word ndx, Ofl_desc *ofl) 1295 { 1296 /* 1297 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might 1298 * be needed for ld processing. These sections need to be in the 1299 * internal table. Later it will be determined whether they can be 1300 * eliminated or not. 1301 */ 1302 if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM) 1303 return (0); 1304 1305 /* 1306 * Other checks 1307 */ 1308 if (shdr->sh_flags & SHF_ALLOC) { 1309 /* 1310 * A conflict, issue an warning message, and ignore the section. 1311 */ 1312 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE), 1313 ifl->ifl_name, name); 1314 return (0); 1315 } 1316 1317 /* 1318 * This sections is not going to the output file. 1319 */ 1320 return (process_section(name, ifl, shdr, scn, ndx, 0, ofl)); 1321 } 1322 1323 #if defined(__x86) && defined(_ELF64) 1324 1325 static uintptr_t 1326 process_amd64_unwind(const char *name, Ifl_desc *ifl, Shdr *shdr, 1327 Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl) 1328 { 1329 Os_desc *osp, *eosp; 1330 Is_desc *isp; 1331 Listnode *lnp; 1332 1333 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 1334 return (S_ERROR); 1335 1336 /* 1337 * When producing a relocatable object - just collect the sections. 1338 */ 1339 if (ofl->ofl_flags & FLG_OF_RELOBJ) 1340 return (1); 1341 1342 /* 1343 * If producing a executable or shared library, keep track of all the 1344 * output UNWIND sections to allow the creation of the appropriate 1345 * frame_hdr information. 1346 * 1347 * If the section hasn't been placed in the output file, then there's 1348 * nothing for us to do. 1349 */ 1350 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 1351 ((osp = isp->is_osdesc) == 0)) 1352 return (1); 1353 1354 /* 1355 * Check to see if this output section is already on the list, and if 1356 * not, add it. 1357 */ 1358 for (LIST_TRAVERSE(&ofl->ofl_unwind, lnp, eosp)) 1359 if (osp == eosp) 1360 return (1); 1361 1362 if (list_appendc(&ofl->ofl_unwind, osp) == 0) 1363 return (S_ERROR); 1364 1365 return (1); 1366 } 1367 1368 #endif 1369 1370 /* 1371 * Section processing state table. `Initial' describes the required initial 1372 * procedure to be called (if any), `Final' describes the final processing 1373 * procedure (ie. things that can only be done when all required sections 1374 * have been collected). 1375 */ 1376 static uintptr_t (*Initial[SHT_NUM][2])() = { 1377 1378 /* ET_REL ET_DYN */ 1379 1380 /* SHT_NULL */ invalid_section, invalid_section, 1381 /* SHT_PROGBITS */ process_progbits, process_progbits, 1382 /* SHT_SYMTAB */ process_input, process_input, 1383 /* SHT_STRTAB */ process_strtab, process_strtab, 1384 /* SHT_RELA */ process_reloc, process_reloc, 1385 /* SHT_HASH */ invalid_section, NULL, 1386 /* SHT_DYNAMIC */ process_rel_dynamic, process_dynamic_isgnu, 1387 /* SHT_NOTE */ process_section, NULL, 1388 /* SHT_NOBITS */ process_nobits, process_nobits, 1389 /* SHT_REL */ process_reloc, process_reloc, 1390 /* SHT_SHLIB */ process_section, invalid_section, 1391 /* SHT_DYNSYM */ invalid_section, process_input, 1392 /* SHT_UNKNOWN12 */ process_progbits, process_progbits, 1393 /* SHT_UNKNOWN13 */ process_progbits, process_progbits, 1394 /* SHT_INIT_ARRAY */ process_array, NULL, 1395 /* SHT_FINI_ARRAY */ process_array, NULL, 1396 /* SHT_PREINIT_ARRAY */ process_array, NULL, 1397 /* SHT_GROUP */ process_section, invalid_section, 1398 /* SHT_SYMTAB_SHNDX */ process_sym_shndx, NULL 1399 }; 1400 1401 static uintptr_t (*Final[SHT_NUM][2])() = { 1402 1403 /* SHT_NULL */ NULL, NULL, 1404 /* SHT_PROGBITS */ NULL, NULL, 1405 /* SHT_SYMTAB */ ld_sym_process, ld_sym_process, 1406 /* SHT_STRTAB */ NULL, NULL, 1407 /* SHT_RELA */ rel_process, NULL, 1408 /* SHT_HASH */ NULL, NULL, 1409 /* SHT_DYNAMIC */ NULL, process_dynamic, 1410 /* SHT_NOTE */ NULL, NULL, 1411 /* SHT_NOBITS */ NULL, NULL, 1412 /* SHT_REL */ rel_process, NULL, 1413 /* SHT_SHLIB */ NULL, NULL, 1414 /* SHT_DYNSYM */ NULL, ld_sym_process, 1415 /* SHT_UNKNOWN12 */ NULL, NULL, 1416 /* SHT_UNKNOWN13 */ NULL, NULL, 1417 /* SHT_INIT_ARRAY */ NULL, NULL, 1418 /* SHT_FINI_ARRAY */ NULL, NULL, 1419 /* SHT_PREINIT_ARRAY */ NULL, NULL, 1420 /* SHT_GROUP */ NULL, NULL, 1421 /* SHT_SYMTAB_SHNDX */ sym_shndx_process, NULL 1422 }; 1423 1424 #define MAXNDXSIZE 10 1425 1426 /* 1427 * Process an elf file. Each section is compared against the section state 1428 * table to determine whether it should be processed (saved), ignored, or 1429 * is invalid for the type of input file being processed. 1430 */ 1431 static uintptr_t 1432 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl) 1433 { 1434 Elf_Scn *scn; 1435 Shdr *shdr; 1436 Word ndx, sndx; 1437 char *str, *name, _name[MAXNDXSIZE]; 1438 Word row, column; 1439 int ident; 1440 uintptr_t error; 1441 Is_desc *vdfisp, *vndisp, *vsyisp, *sifisp, *capisp; 1442 Sdf_desc *sdf; 1443 Word ordered_shndx = 0; /* index to first ordered section */ 1444 Word ordered_cnt = 0; 1445 1446 /* 1447 * First process the .shstrtab section so that later sections can 1448 * reference their name. 1449 */ 1450 ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf); 1451 1452 sndx = ifl->ifl_shstrndx; 1453 if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) { 1454 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN), 1455 ifl->ifl_name); 1456 ofl->ofl_flags |= FLG_OF_FATAL; 1457 return (0); 1458 } 1459 if ((shdr = elf_getshdr(scn)) == NULL) { 1460 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR), 1461 ifl->ifl_name); 1462 ofl->ofl_flags |= FLG_OF_FATAL; 1463 return (0); 1464 } 1465 if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) == 1466 NULL) { 1467 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), 1468 ifl->ifl_name); 1469 ofl->ofl_flags |= FLG_OF_FATAL; 1470 return (0); 1471 } 1472 1473 if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn, 1474 elf) == S_ERROR) 1475 return (S_ERROR); 1476 1477 /* 1478 * Reset the name since the shdr->sh_name could have been changed as 1479 * part of ld_sup_input_section(). If there is no name, fabricate one 1480 * using the section index. 1481 */ 1482 if (shdr->sh_name == 0) { 1483 (void) snprintf(_name, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX), 1484 EC_XWORD(sndx)); 1485 if ((name = libld_malloc(strlen(_name) + 1)) == 0) 1486 return (S_ERROR); 1487 (void) strcpy(name, _name); 1488 1489 } else if ((name = elf_strptr(elf, (size_t)sndx, 1490 (size_t)shdr->sh_name)) == NULL) { 1491 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), 1492 ifl->ifl_name); 1493 ofl->ofl_flags |= FLG_OF_FATAL; 1494 return (0); 1495 } 1496 1497 error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl); 1498 if ((error == 0) || (error == S_ERROR)) 1499 return (error); 1500 str = ifl->ifl_isdesc[sndx]->is_indata->d_buf; 1501 1502 /* 1503 * Determine the state table column from the input file type. Note, 1504 * shared library sections are not added to the output section list. 1505 */ 1506 if (ifl->ifl_ehdr->e_type == ET_DYN) { 1507 column = 1; 1508 ofl->ofl_soscnt++; 1509 ident = M_ID_NULL; 1510 } else { 1511 column = 0; 1512 ofl->ofl_objscnt++; 1513 ident = M_ID_UNKNOWN; 1514 } 1515 1516 DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl)); 1517 ndx = 0; 1518 vdfisp = vndisp = vsyisp = sifisp = capisp = 0; 1519 scn = NULL; 1520 while (scn = elf_nextscn(elf, scn)) { 1521 ndx++; 1522 /* 1523 * As we've already processed the .shstrtab don't do it again. 1524 */ 1525 if (ndx == sndx) 1526 continue; 1527 1528 if ((shdr = elf_getshdr(scn)) == NULL) { 1529 eprintf(ofl->ofl_lml, ERR_ELF, 1530 MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name); 1531 ofl->ofl_flags |= FLG_OF_FATAL; 1532 return (0); 1533 } 1534 name = str + (size_t)(shdr->sh_name); 1535 1536 if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn, 1537 elf) == S_ERROR) 1538 return (S_ERROR); 1539 1540 /* 1541 * Reset the name since the shdr->sh_name could have been 1542 * changed as part of ld_sup_input_section(). If there is no 1543 * name, fabricate one using the section index. 1544 */ 1545 if (shdr->sh_name == 0) { 1546 (void) snprintf(_name, MAXNDXSIZE, 1547 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(ndx)); 1548 if ((name = libld_malloc(strlen(_name) + 1)) == 0) 1549 return (S_ERROR); 1550 (void) strcpy(name, _name); 1551 } else 1552 name = str + (size_t)(shdr->sh_name); 1553 1554 row = shdr->sh_type; 1555 1556 /* 1557 * If the section has the SHF_EXCLUDE flag on, and we're not 1558 * generating a relocatable object, exclude the section. 1559 */ 1560 if (((shdr->sh_flags & SHF_EXCLUDE) != 0) && 1561 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) { 1562 if ((error = process_exclude(name, ifl, shdr, scn, 1563 ndx, ofl)) == S_ERROR) 1564 return (S_ERROR); 1565 if (error == 1) 1566 continue; 1567 } 1568 1569 /* 1570 * If this is a standard section type process it via the 1571 * appropriate action routine. 1572 */ 1573 if (row < SHT_NUM) { 1574 if (Initial[row][column] != NULL) { 1575 if (Initial[row][column](name, ifl, shdr, scn, 1576 ndx, ident, ofl) == S_ERROR) 1577 return (S_ERROR); 1578 } 1579 } else { 1580 /* 1581 * If this section is below SHT_LOSUNW then we don't 1582 * really know what to do with it, issue a warning 1583 * message but do the basic section processing anyway. 1584 */ 1585 if (row < (Word)SHT_LOSUNW) { 1586 Conv_inv_buf_t inv_buf; 1587 1588 eprintf(ofl->ofl_lml, ERR_WARNING, 1589 MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name, 1590 name, 1591 conv_sec_type(ifl->ifl_ehdr->e_machine, 1592 shdr->sh_type, 0, &inv_buf)); 1593 } 1594 1595 /* 1596 * Handle sections greater than SHT_LOSUNW. 1597 */ 1598 switch (row) { 1599 case SHT_SUNW_dof: 1600 if (process_section(name, ifl, shdr, scn, 1601 ndx, ident, ofl) == S_ERROR) 1602 return (S_ERROR); 1603 break; 1604 case SHT_SUNW_cap: 1605 if (process_section(name, ifl, shdr, scn, 1606 ndx, M_ID_NULL, ofl) == S_ERROR) 1607 return (S_ERROR); 1608 capisp = ifl->ifl_isdesc[ndx]; 1609 break; 1610 case SHT_SUNW_DEBUGSTR: 1611 case SHT_SUNW_DEBUG: 1612 if (process_debug(name, ifl, shdr, scn, 1613 ndx, ident, ofl) == S_ERROR) 1614 return (S_ERROR); 1615 break; 1616 case SHT_SUNW_move: 1617 if (process_section(name, ifl, shdr, scn, 1618 ndx, M_ID_NULL, ofl) == S_ERROR) 1619 return (S_ERROR); 1620 break; 1621 case SHT_SUNW_syminfo: 1622 if (process_section(name, ifl, shdr, scn, 1623 ndx, M_ID_NULL, ofl) == S_ERROR) 1624 return (S_ERROR); 1625 sifisp = ifl->ifl_isdesc[ndx]; 1626 break; 1627 case SHT_SUNW_ANNOTATE: 1628 case SHT_SUNW_COMDAT: 1629 if (process_progbits(name, ifl, shdr, scn, 1630 ndx, ident, ofl) == S_ERROR) 1631 return (S_ERROR); 1632 break; 1633 case SHT_SUNW_verdef: 1634 if (process_section(name, ifl, shdr, scn, 1635 ndx, M_ID_NULL, ofl) == S_ERROR) 1636 return (S_ERROR); 1637 vdfisp = ifl->ifl_isdesc[ndx]; 1638 break; 1639 case SHT_SUNW_verneed: 1640 if (process_section(name, ifl, shdr, scn, 1641 ndx, M_ID_NULL, ofl) == S_ERROR) 1642 return (S_ERROR); 1643 vndisp = ifl->ifl_isdesc[ndx]; 1644 break; 1645 case SHT_SUNW_versym: 1646 if (process_section(name, ifl, shdr, scn, 1647 ndx, M_ID_NULL, ofl) == S_ERROR) 1648 return (S_ERROR); 1649 vsyisp = ifl->ifl_isdesc[ndx]; 1650 break; 1651 #if defined(__sparc) 1652 case SHT_SPARC_GOTDATA: 1653 if (process_section(name, ifl, shdr, scn, 1654 ndx, M_ID_GOTDATA, ofl) == S_ERROR) 1655 return (S_ERROR); 1656 break; 1657 #endif 1658 #if defined(__x86) && defined(_ELF64) 1659 case SHT_AMD64_UNWIND: 1660 if (column == 0) { 1661 /* 1662 * column == ET_REL 1663 */ 1664 if (process_amd64_unwind(name, ifl, 1665 shdr, scn, ndx, M_ID_UNWIND, 1666 ofl) == S_ERROR) 1667 return (S_ERROR); 1668 } 1669 break; 1670 #endif 1671 default: 1672 if (ident != M_ID_NULL) 1673 ident = M_ID_USER; 1674 if (process_section(name, ifl, shdr, scn, 1675 ndx, ident, ofl) == S_ERROR) 1676 return (S_ERROR); 1677 break; 1678 } 1679 } 1680 1681 /* 1682 * If we have any sections that require ORDERED processing, 1683 * remember the index of the first ordered section. This let's 1684 * us know if we need an ORDERED place_section pass, and if so, 1685 * where to start. 1686 */ 1687 if (ifl->ifl_isdesc[ndx] && 1688 (ifl->ifl_isdesc[ndx]->is_shdr->sh_flags & ALL_SHF_ORDER)) { 1689 ordered_cnt++; 1690 if (ordered_shndx == 0) 1691 ordered_shndx = ndx; 1692 } 1693 } 1694 1695 /* 1696 * Now that all of sections have been placed, scan through any sections 1697 * which have special ordering requirements and place them now. 1698 */ 1699 if (ordered_shndx) { 1700 Word cnt; 1701 1702 for (ndx = ordered_shndx, cnt = 0; 1703 (ndx < ifl->ifl_shnum) && (cnt < ordered_cnt); ndx++) { 1704 Is_desc *isp; 1705 /* LINTED */ 1706 Os_desc *osp; 1707 1708 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 1709 ((isp->is_shdr->sh_flags & ALL_SHF_ORDER) == 0)) 1710 continue; 1711 1712 /* 1713 * If this is an ordered section, process it. 1714 */ 1715 cnt++; 1716 if ((osp = (Os_desc *)ld_process_ordered(ifl, ofl, ndx, 1717 ifl->ifl_shnum)) == (Os_desc *)S_ERROR) 1718 return (S_ERROR); 1719 1720 #if defined(__x86) && defined(_ELF64) 1721 /* 1722 * If this section is 'ordered' then it was not 1723 * caught in the previous 'place_section' operation. 1724 * 1725 * So - now that we have a OSP section for 1726 * the unwind info - record it. 1727 */ 1728 if (osp && 1729 (osp->os_shdr->sh_type == SHT_AMD64_UNWIND) && 1730 (append_amd64_unwind(osp, ofl) == S_ERROR)) 1731 return (S_ERROR); 1732 #endif 1733 } 1734 } 1735 1736 /* 1737 * If this is an explicit shared object determine if the user has 1738 * specified a control definition. This descriptor may specify which 1739 * version definitions can be used from this object (it may also update 1740 * the dependency to USED and supply an alternative SONAME). 1741 */ 1742 sdf = 0; 1743 if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) { 1744 const char *base; 1745 1746 /* 1747 * Use the basename of the input file (typically this is the 1748 * compilation environment name, ie. libfoo.so). 1749 */ 1750 if ((base = strrchr(ifl->ifl_name, '/')) == NULL) 1751 base = ifl->ifl_name; 1752 else 1753 base++; 1754 1755 if ((sdf = sdf_find(base, &ofl->ofl_socntl)) != 0) { 1756 sdf->sdf_file = ifl; 1757 ifl->ifl_sdfdesc = sdf; 1758 } 1759 } 1760 1761 /* 1762 * Process any hardware/software capabilities sections. Only propagate 1763 * capabilities for input relocatable objects. If the object doesn't 1764 * contain any capabilities, any capability state that has already been 1765 * gathered will prevail. 1766 */ 1767 if (capisp && (ifl->ifl_ehdr->e_type == ET_REL)) 1768 process_cap(ifl, capisp, ofl); 1769 1770 /* 1771 * Process any version dependencies. These will establish shared object 1772 * `needed' entries in the same manner as will be generated from the 1773 * .dynamic's NEEDED entries. 1774 */ 1775 if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC))) 1776 if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR) 1777 return (S_ERROR); 1778 1779 /* 1780 * Before processing any symbol resolution or relocations process any 1781 * version sections. 1782 */ 1783 if (vsyisp) 1784 (void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl); 1785 1786 if (ifl->ifl_versym && 1787 (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT)))) 1788 if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR) 1789 return (S_ERROR); 1790 1791 /* 1792 * Having collected the appropriate sections carry out any additional 1793 * processing if necessary. 1794 */ 1795 for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) { 1796 Is_desc * isp; 1797 1798 if ((isp = ifl->ifl_isdesc[ndx]) == 0) 1799 continue; 1800 row = isp->is_shdr->sh_type; 1801 1802 if ((isp->is_flags & FLG_IS_DISCARD) == 0) 1803 ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx, 1804 isp->is_indata, elf); 1805 1806 /* 1807 * If this is a ST_SUNW_move section from a 1808 * a relocatable file, keep the input section. 1809 */ 1810 if ((row == SHT_SUNW_move) && (column == 0)) { 1811 if (list_appendc(&(ofl->ofl_ismove), isp) == 0) 1812 return (S_ERROR); 1813 } 1814 1815 /* 1816 * If this is a standard section type process it via the 1817 * appropriate action routine. 1818 */ 1819 if (row < SHT_NUM) { 1820 if (Final[row][column] != NULL) 1821 if (Final[row][column](isp, ifl, ofl) == 1822 S_ERROR) 1823 return (S_ERROR); 1824 } 1825 } 1826 1827 /* 1828 * After processing any symbol resolution, and if this dependency 1829 * indicates it contains symbols that can't be directly bound to, 1830 * set the symbols appropriately. 1831 */ 1832 if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) == 1833 (FLG_IF_NEEDED | FLG_IF_NODIRECT))) 1834 (void) ld_sym_nodirect(sifisp, ifl, ofl); 1835 1836 return (1); 1837 } 1838 1839 /* 1840 * Process the current input file. There are basically three types of files 1841 * that come through here: 1842 * 1843 * o files explicitly defined on the command line (ie. foo.o or bar.so), 1844 * in this case only the `name' field is valid. 1845 * 1846 * o libraries determined from the -l command line option (ie. -lbar), 1847 * in this case the `soname' field contains the basename of the located 1848 * file. 1849 * 1850 * Any shared object specified via the above two conventions must be recorded 1851 * as a needed dependency. 1852 * 1853 * o libraries specified as dependencies of those libraries already obtained 1854 * via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1), 1855 * in this case the `soname' field contains either a full pathname (if the 1856 * needed entry contained a `/'), or the basename of the located file. 1857 * These libraries are processed to verify symbol binding but are not 1858 * recorded as dependencies of the output file being generated. 1859 */ 1860 Ifl_desc * 1861 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf, 1862 Word flags, Ofl_desc * ofl, Rej_desc * rej) 1863 { 1864 Ifl_desc *ifl; 1865 Ehdr *ehdr; 1866 Listnode *lnp; 1867 uintptr_t error = 0; 1868 struct stat status; 1869 Ar_desc *adp; 1870 Rej_desc _rej; 1871 1872 /* 1873 * If this file was not extracted from an archive obtain its device 1874 * information. This will be used to determine if the file has already 1875 * been processed (rather than simply comparing filenames, the device 1876 * information provides a quicker comparison and detects linked files). 1877 */ 1878 if (!(flags & FLG_IF_EXTRACT)) 1879 (void) fstat(fd, &status); 1880 else { 1881 status.st_dev = 0; 1882 status.st_ino = 0; 1883 } 1884 1885 switch (elf_kind(elf)) { 1886 case ELF_K_AR: 1887 /* 1888 * Determine if we've already come across this archive file. 1889 */ 1890 if (!(flags & FLG_IF_EXTRACT)) { 1891 for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) { 1892 if ((adp->ad_stdev != status.st_dev) || 1893 (adp->ad_stino != status.st_ino)) 1894 continue; 1895 1896 /* 1897 * We've seen this file before so reuse the 1898 * original archive descriptor and discard the 1899 * new elf descriptor. 1900 */ 1901 DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name, 1902 adp->ad_name)); 1903 (void) elf_end(elf); 1904 return ((Ifl_desc *)ld_process_archive(name, fd, 1905 adp, ofl)); 1906 } 1907 } 1908 1909 /* 1910 * As we haven't processed this file before establish a new 1911 * archive descriptor. 1912 */ 1913 adp = ld_ar_setup(name, elf, ofl); 1914 if ((adp == 0) || (adp == (Ar_desc *)S_ERROR)) 1915 return ((Ifl_desc *)adp); 1916 adp->ad_stdev = status.st_dev; 1917 adp->ad_stino = status.st_ino; 1918 1919 ld_sup_file(ofl, name, ELF_K_AR, flags, elf); 1920 1921 return ((Ifl_desc *)ld_process_archive(name, fd, adp, ofl)); 1922 1923 case ELF_K_ELF: 1924 /* 1925 * Obtain the elf header so that we can determine what type of 1926 * elf ELF_K_ELF file this is. 1927 */ 1928 if ((ehdr = elf_getehdr(elf)) == NULL) { 1929 int _class = gelf_getclass(elf); 1930 1931 /* 1932 * Failure could occur for a number of reasons at this 1933 * point. Typically the files class is incorrect (ie. 1934 * user is building 64-bit but managed to pint at 32-bit 1935 * libraries). However any number of elf errors can 1936 * also occur, such as from a truncated or corrupt file. 1937 * Here we try and get the best error message possible. 1938 */ 1939 if (M_CLASS != _class) { 1940 _rej.rej_type = SGS_REJ_CLASS; 1941 _rej.rej_info = (uint_t)_class; 1942 } else { 1943 _rej.rej_type = SGS_REJ_STR; 1944 _rej.rej_str = elf_errmsg(-1); 1945 } 1946 _rej.rej_name = name; 1947 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 1948 if (rej->rej_type == 0) { 1949 *rej = _rej; 1950 rej->rej_name = strdup(_rej.rej_name); 1951 } 1952 return (0); 1953 } 1954 1955 /* 1956 * Determine if we've already come across this file. 1957 */ 1958 if (!(flags & FLG_IF_EXTRACT)) { 1959 List * lst; 1960 1961 if (ehdr->e_type == ET_REL) 1962 lst = &ofl->ofl_objs; 1963 else 1964 lst = &ofl->ofl_sos; 1965 1966 /* 1967 * Traverse the appropriate file list and determine if 1968 * a dev/inode match is found. 1969 */ 1970 for (LIST_TRAVERSE(lst, lnp, ifl)) { 1971 /* 1972 * Ifl_desc generated via -Nneed, therefore no 1973 * actual file behind it. 1974 */ 1975 if (ifl->ifl_flags & FLG_IF_NEEDSTR) 1976 continue; 1977 1978 if ((ifl->ifl_stino != status.st_ino) || 1979 (ifl->ifl_stdev != status.st_dev)) 1980 continue; 1981 1982 /* 1983 * Disregard (skip) this image. 1984 */ 1985 DBG_CALL(Dbg_file_skip(ofl->ofl_lml, 1986 ifl->ifl_name, name)); 1987 (void) elf_end(elf); 1988 1989 /* 1990 * If the file was explicitly defined on the 1991 * command line (this is always the case for 1992 * relocatable objects, and is true for shared 1993 * objects when they weren't specified via -l or 1994 * were dragged in as an implicit dependency), 1995 * then warn the user. 1996 */ 1997 if ((flags & FLG_IF_CMDLINE) || 1998 (ifl->ifl_flags & FLG_IF_CMDLINE)) { 1999 const char *errmsg; 2000 2001 /* 2002 * Determine whether this is the same 2003 * file name as originally encountered 2004 * so as to provide the most 2005 * descriptive diagnostic. 2006 */ 2007 errmsg = 2008 (strcmp(name, ifl->ifl_name) == 0) ? 2009 MSG_INTL(MSG_FIL_MULINC_1) : 2010 MSG_INTL(MSG_FIL_MULINC_2); 2011 eprintf(ofl->ofl_lml, ERR_WARNING, 2012 errmsg, name, ifl->ifl_name); 2013 } 2014 return (ifl); 2015 } 2016 } 2017 2018 /* 2019 * At this point, we know we need the file. Establish an input 2020 * file descriptor and continue processing. 2021 */ 2022 ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej); 2023 if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR)) 2024 return (ifl); 2025 ifl->ifl_stdev = status.st_dev; 2026 ifl->ifl_stino = status.st_ino; 2027 2028 /* 2029 * If -zignore is in effect, mark this file as a potential 2030 * candidate (the files use isn't actually determined until 2031 * symbol resolution and relocation processing are completed). 2032 */ 2033 if (ofl->ofl_flags1 & FLG_OF1_IGNORE) 2034 ifl->ifl_flags |= FLG_IF_IGNORE; 2035 2036 switch (ehdr->e_type) { 2037 case ET_REL: 2038 ld_mach_eflags(ehdr, ofl); 2039 error = process_elf(ifl, elf, ofl); 2040 break; 2041 case ET_DYN: 2042 if ((ofl->ofl_flags & FLG_OF_STATIC) || 2043 !(ofl->ofl_flags & FLG_OF_DYNLIBS)) { 2044 eprintf(ofl->ofl_lml, ERR_FATAL, 2045 MSG_INTL(MSG_FIL_SOINSTAT), name); 2046 ofl->ofl_flags |= FLG_OF_FATAL; 2047 return (0); 2048 } 2049 2050 /* 2051 * Record any additional shared object information. 2052 * If no soname is specified (eg. this file was 2053 * derived from a explicit filename declaration on the 2054 * command line, ie. bar.so) use the pathname. 2055 * This entry may be overridden if the files dynamic 2056 * section specifies an DT_SONAME value. 2057 */ 2058 if (soname == NULL) 2059 ifl->ifl_soname = ifl->ifl_name; 2060 else 2061 ifl->ifl_soname = soname; 2062 2063 /* 2064 * If direct bindings, lazy loading, or group 2065 * permissions need to be established, mark this object. 2066 */ 2067 if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT) 2068 ifl->ifl_flags |= FLG_IF_DIRECT; 2069 if (ofl->ofl_flags1 & FLG_OF1_LAZYLD) 2070 ifl->ifl_flags |= FLG_IF_LAZYLD; 2071 if (ofl->ofl_flags1 & FLG_OF1_GRPPRM) 2072 ifl->ifl_flags |= FLG_IF_GRPPRM; 2073 error = process_elf(ifl, elf, ofl); 2074 2075 /* 2076 * At this point we know if this file will be 2077 * lazyloaded, or whether bindings to it must be direct. 2078 * In either case, a syminfo section is required. 2079 */ 2080 if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT)) 2081 ofl->ofl_flags |= FLG_OF_SYMINFO; 2082 2083 break; 2084 default: 2085 (void) elf_errno(); 2086 _rej.rej_type = SGS_REJ_UNKFILE; 2087 _rej.rej_name = name; 2088 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 2089 if (rej->rej_type == 0) { 2090 *rej = _rej; 2091 rej->rej_name = strdup(_rej.rej_name); 2092 } 2093 return (0); 2094 } 2095 break; 2096 default: 2097 (void) elf_errno(); 2098 _rej.rej_type = SGS_REJ_UNKFILE; 2099 _rej.rej_name = name; 2100 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 2101 if (rej->rej_type == 0) { 2102 *rej = _rej; 2103 rej->rej_name = strdup(_rej.rej_name); 2104 } 2105 return (0); 2106 } 2107 if ((error == 0) || (error == S_ERROR)) 2108 return ((Ifl_desc *)error); 2109 else 2110 return (ifl); 2111 } 2112 2113 /* 2114 * Having successfully opened a file, set up the necessary elf structures to 2115 * process it further. This small section of processing is slightly different 2116 * from the elf initialization required to process a relocatable object from an 2117 * archive (see libs.c: ld_process_archive()). 2118 */ 2119 Ifl_desc * 2120 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl, 2121 Word flags, Rej_desc *rej) 2122 { 2123 Elf *elf; 2124 const char *npath = opath; 2125 const char *nfile = ofile; 2126 2127 if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) { 2128 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath); 2129 ofl->ofl_flags |= FLG_OF_FATAL; 2130 return (0); 2131 } 2132 2133 /* 2134 * Determine whether the support library wishes to process this open. 2135 * The support library may return: 2136 * . a different ELF descriptor (in which case they should have 2137 * closed the original) 2138 * . a different file descriptor (in which case they should have 2139 * closed the original) 2140 * . a different path and file name (presumably associated with 2141 * a different file descriptor) 2142 * 2143 * A file descriptor of -1, or and ELF descriptor of zero indicates 2144 * the file should be ignored. 2145 */ 2146 ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0, 2147 elf_kind(elf)); 2148 2149 if ((*fd == -1) || (elf == NULL)) 2150 return (0); 2151 2152 return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej)); 2153 } 2154 2155 /* 2156 * Process a required library (i.e. the dependency of a shared object). 2157 * Combine the directory and filename, check the resultant path size, and try 2158 * opening the pathname. 2159 */ 2160 static Ifl_desc * 2161 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file, 2162 Ofl_desc * ofl, Rej_desc * rej) 2163 { 2164 size_t dlen, plen; 2165 int fd; 2166 char path[PATH_MAX]; 2167 const char *_dir = dir; 2168 2169 /* 2170 * Determine the sizes of the directory and filename to insure we don't 2171 * exceed our buffer. 2172 */ 2173 if ((dlen = strlen(dir)) == 0) { 2174 _dir = MSG_ORIG(MSG_STR_DOT); 2175 dlen = 1; 2176 } 2177 dlen++; 2178 plen = dlen + strlen(file) + 1; 2179 if (plen > PATH_MAX) { 2180 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG), 2181 _dir, file); 2182 ofl->ofl_flags |= FLG_OF_FATAL; 2183 return (0); 2184 } 2185 2186 /* 2187 * Build the entire pathname and try and open the file. 2188 */ 2189 (void) strcpy(path, _dir); 2190 (void) strcat(path, MSG_ORIG(MSG_STR_SLASH)); 2191 (void) strcat(path, file); 2192 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name, 2193 sdf->sdf_rfile, path)); 2194 2195 if ((fd = open(path, O_RDONLY)) == -1) 2196 return (0); 2197 else { 2198 Ifl_desc *ifl; 2199 char *_path; 2200 2201 if ((_path = libld_malloc(strlen(path) + 1)) == 0) 2202 return ((Ifl_desc *)S_ERROR); 2203 (void) strcpy(_path, path); 2204 ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej); 2205 if (fd != -1) 2206 (void) close(fd); 2207 return (ifl); 2208 } 2209 } 2210 2211 /* 2212 * Finish any library processing. Walk the list of so's that have been listed 2213 * as "included" by shared objects we have previously processed. Examine them, 2214 * without adding them as explicit dependents of this program, in order to 2215 * complete our symbol definition process. The search path rules are: 2216 * 2217 * o use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then 2218 * 2219 * o use any RPATH defined within the parent shared object, then 2220 * 2221 * o use the default directories, i.e. LIBPATH or -YP. 2222 */ 2223 uintptr_t 2224 ld_finish_libs(Ofl_desc *ofl) 2225 { 2226 Listnode *lnp1; 2227 Sdf_desc *sdf; 2228 Rej_desc rej = { 0 }; 2229 2230 /* 2231 * Make sure we are back in dynamic mode. 2232 */ 2233 ofl->ofl_flags |= FLG_OF_DYNLIBS; 2234 2235 for (LIST_TRAVERSE(&ofl->ofl_soneed, lnp1, sdf)) { 2236 Listnode *lnp2; 2237 char *path, *slash = NULL; 2238 int fd; 2239 Ifl_desc *ifl; 2240 char *file = (char *)sdf->sdf_name; 2241 2242 /* 2243 * See if this file has already been processed. At the time 2244 * this implicit dependency was determined there may still have 2245 * been more explicit dependencies to process. Note, if we ever 2246 * do parse the command line three times we would be able to 2247 * do all this checking when processing the dynamic section. 2248 */ 2249 if (sdf->sdf_file) 2250 continue; 2251 2252 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp2, ifl)) { 2253 if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) && 2254 (strcmp(file, ifl->ifl_soname) == 0)) { 2255 sdf->sdf_file = ifl; 2256 break; 2257 } 2258 } 2259 if (sdf->sdf_file) 2260 continue; 2261 2262 /* 2263 * If the current path name element embeds a "/", then it's to 2264 * be taken "as is", with no searching involved. Process all 2265 * "/" occurrences, so that we can deduce the base file name. 2266 */ 2267 for (path = file; *path; path++) { 2268 if (*path == '/') 2269 slash = path; 2270 } 2271 if (slash) { 2272 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name, 2273 sdf->sdf_rfile, file)); 2274 if ((fd = open(file, O_RDONLY)) == -1) { 2275 eprintf(ofl->ofl_lml, ERR_WARNING, 2276 MSG_INTL(MSG_FIL_NOTFOUND), file, 2277 sdf->sdf_rfile); 2278 } else { 2279 Rej_desc _rej = { 0 }; 2280 2281 ifl = ld_process_open(file, ++slash, &fd, ofl, 2282 0, &_rej); 2283 if (fd != -1) 2284 (void) close(fd); 2285 2286 if (ifl == (Ifl_desc *)S_ERROR) { 2287 return (S_ERROR); 2288 } 2289 if (_rej.rej_type) { 2290 Conv_reject_desc_buf_t rej_buf; 2291 2292 eprintf(ofl->ofl_lml, ERR_WARNING, 2293 MSG_INTL(reject[_rej.rej_type]), 2294 _rej.rej_name ? rej.rej_name : 2295 MSG_INTL(MSG_STR_UNKNOWN), 2296 conv_reject_desc(&_rej, &rej_buf)); 2297 } else 2298 sdf->sdf_file = ifl; 2299 } 2300 continue; 2301 } 2302 2303 /* 2304 * Now search for this file in any user defined directories. 2305 */ 2306 for (LIST_TRAVERSE(&ofl->ofl_ulibdirs, lnp2, path)) { 2307 Rej_desc _rej = { 0 }; 2308 2309 ifl = process_req_lib(sdf, path, file, ofl, &_rej); 2310 if (ifl == (Ifl_desc *)S_ERROR) { 2311 return (S_ERROR); 2312 } 2313 if (_rej.rej_type) { 2314 if (rej.rej_type == 0) { 2315 rej = _rej; 2316 rej.rej_name = strdup(_rej.rej_name); 2317 } 2318 } 2319 if (ifl) { 2320 sdf->sdf_file = ifl; 2321 break; 2322 } 2323 } 2324 if (sdf->sdf_file) 2325 continue; 2326 2327 /* 2328 * Next use the local rules defined within the parent shared 2329 * object. 2330 */ 2331 if (sdf->sdf_rpath != NULL) { 2332 char *rpath, *next; 2333 2334 rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1); 2335 if (rpath == 0) 2336 return (S_ERROR); 2337 (void) strcpy(rpath, sdf->sdf_rpath); 2338 DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath, 2339 LA_SER_RUNPATH, sdf->sdf_rfile)); 2340 if ((path = strtok_r(rpath, 2341 MSG_ORIG(MSG_STR_COLON), &next)) != NULL) { 2342 do { 2343 Rej_desc _rej = { 0 }; 2344 2345 path = expand(sdf->sdf_rfile, path, 2346 &next); 2347 2348 ifl = process_req_lib(sdf, path, 2349 file, ofl, &_rej); 2350 if (ifl == (Ifl_desc *)S_ERROR) { 2351 return (S_ERROR); 2352 } 2353 if ((_rej.rej_type) && 2354 (rej.rej_type == 0)) { 2355 rej = _rej; 2356 rej.rej_name = 2357 strdup(_rej.rej_name); 2358 } 2359 if (ifl) { 2360 sdf->sdf_file = ifl; 2361 break; 2362 } 2363 } while ((path = strtok_r(NULL, 2364 MSG_ORIG(MSG_STR_COLON), &next)) != NULL); 2365 } 2366 } 2367 if (sdf->sdf_file) 2368 continue; 2369 2370 /* 2371 * Finally try the default library search directories. 2372 */ 2373 for (LIST_TRAVERSE(&ofl->ofl_dlibdirs, lnp2, path)) { 2374 Rej_desc _rej = { 0 }; 2375 2376 ifl = process_req_lib(sdf, path, file, ofl, &rej); 2377 if (ifl == (Ifl_desc *)S_ERROR) { 2378 return (S_ERROR); 2379 } 2380 if (_rej.rej_type) { 2381 if (rej.rej_type == 0) { 2382 rej = _rej; 2383 rej.rej_name = strdup(_rej.rej_name); 2384 } 2385 } 2386 if (ifl) { 2387 sdf->sdf_file = ifl; 2388 break; 2389 } 2390 } 2391 if (sdf->sdf_file) 2392 continue; 2393 2394 /* 2395 * If we've got this far we haven't found the shared object. 2396 * If an object was found, but was rejected for some reason, 2397 * print a diagnostic to that effect, otherwise generate a 2398 * generic "not found" diagnostic. 2399 */ 2400 if (rej.rej_type) { 2401 Conv_reject_desc_buf_t rej_buf; 2402 2403 eprintf(ofl->ofl_lml, ERR_WARNING, 2404 MSG_INTL(reject[rej.rej_type]), 2405 rej.rej_name ? rej.rej_name : 2406 MSG_INTL(MSG_STR_UNKNOWN), 2407 conv_reject_desc(&rej, &rej_buf)); 2408 } else { 2409 eprintf(ofl->ofl_lml, ERR_WARNING, 2410 MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile); 2411 } 2412 } 2413 2414 /* 2415 * Finally, now that all objects have been input, make sure any version 2416 * requirements have been met. 2417 */ 2418 return (ld_vers_verify(ofl)); 2419 } 2420