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, Half 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 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC), 492 ifl->ifl_name, name, conv_sec_type(ifl->ifl_ehdr->e_machine, 493 shdr->sh_type, 0)); 494 return (1); 495 } 496 497 /* 498 * Process a progbits section. 499 */ 500 static uintptr_t 501 process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 502 Word ndx, int ident, Ofl_desc *ofl) 503 { 504 int stab_index = 0; 505 506 /* 507 * Never include .stab.excl sections in any output file. 508 * If the -s flag has been specified strip any .stab sections. 509 */ 510 if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB), 511 MSG_SCN_STAB_SIZE) == 0)) { 512 if ((ofl->ofl_flags & FLG_OF_STRIP) || 513 (strcmp((name + MSG_SCN_STAB_SIZE), 514 MSG_ORIG(MSG_SCN_EXCL)) == 0)) 515 return (1); 516 517 if (strcmp((name + MSG_SCN_STAB_SIZE), 518 MSG_ORIG(MSG_SCN_INDEX)) == 0) 519 stab_index = 1; 520 } 521 522 if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) { 523 if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG), 524 MSG_SCN_DEBUG_SIZE) == 0) || 525 (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0)) 526 return (1); 527 } 528 529 /* 530 * Update the ident to reflect the type of section we've got. 531 * 532 * If there is any .plt or .got section to generate we'll be creating 533 * our own version, so don't allow any input sections of these types to 534 * be added to the output section list (why a relocatable object would 535 * have a .plt or .got is a mystery, but stranger things have occurred). 536 */ 537 if (ident) { 538 if (shdr->sh_flags & SHF_TLS) 539 ident = M_ID_TLS; 540 else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) == 541 (SHF_ALLOC | SHF_EXECINSTR)) 542 ident = M_ID_TEXT; 543 else if (shdr->sh_flags & SHF_ALLOC) { 544 if ((strcmp(name, MSG_ORIG(MSG_SCN_PLT)) == 0) || 545 (strcmp(name, MSG_ORIG(MSG_SCN_GOT)) == 0)) 546 ident = M_ID_NULL; 547 else if (stab_index) { 548 /* 549 * This is a work-around for x86 compilers that 550 * have set SHF_ALLOC for the .stab.index 551 * section. 552 * 553 * Because of this, make sure that the 554 * .stab.index does not end up as the last 555 * section in the text segment. Older linkers 556 * can produce segmentation violations when they 557 * strip (ld -s) against a shared object whose 558 * last section in the text segment is a .stab. 559 */ 560 ident = M_ID_INTERP; 561 } else 562 ident = M_ID_DATA; 563 } else 564 ident = M_ID_NOTE; 565 } 566 return (process_section(name, ifl, shdr, scn, ndx, ident, ofl)); 567 } 568 569 /* 570 * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections. 571 */ 572 static uintptr_t 573 process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 574 Word ndx, int ident, Ofl_desc *ofl) 575 { 576 /* 577 * Debug information is discarded when the 'ld -s' flag is invoked. 578 */ 579 if (ofl->ofl_flags & FLG_OF_STRIP) { 580 return (1); 581 } 582 return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl)); 583 } 584 585 /* 586 * Process a nobits section. 587 */ 588 static uintptr_t 589 process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 590 Word ndx, int ident, Ofl_desc *ofl) 591 { 592 if (ident) { 593 if (shdr->sh_flags & SHF_TLS) 594 ident = M_ID_TLSBSS; 595 #if defined(__x86) && defined(_ELF64) 596 else if (shdr->sh_flags & SHF_AMD64_LARGE) 597 ident = M_ID_LBSS; 598 #endif 599 else 600 ident = M_ID_BSS; 601 } 602 return (process_section(name, ifl, shdr, scn, ndx, ident, ofl)); 603 } 604 605 /* 606 * Process a SHT_*_ARRAY section. 607 */ 608 static uintptr_t 609 process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 610 Word ndx, int ident, Ofl_desc *ofl) 611 { 612 Os_desc *osp; 613 Is_desc *isp; 614 615 if (ident) 616 ident = M_ID_ARRAY; 617 618 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 619 return (S_ERROR); 620 621 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 622 ((osp = isp->is_osdesc) == 0)) 623 return (0); 624 625 if ((shdr->sh_type == SHT_FINI_ARRAY) && 626 (ofl->ofl_osfiniarray == 0)) 627 ofl->ofl_osfiniarray = osp; 628 else if ((shdr->sh_type == SHT_INIT_ARRAY) && 629 (ofl->ofl_osinitarray == 0)) 630 ofl->ofl_osinitarray = osp; 631 else if ((shdr->sh_type == SHT_PREINIT_ARRAY) && 632 (ofl->ofl_ospreinitarray == 0)) 633 ofl->ofl_ospreinitarray = osp; 634 635 return (1); 636 } 637 638 /* 639 * Process a SHT_SYMTAB_SHNDX section. 640 */ 641 static uintptr_t 642 process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 643 Word ndx, int ident, Ofl_desc *ofl) 644 { 645 if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 646 return (S_ERROR); 647 648 /* 649 * Have we already seen the related SYMTAB - if so verify it now. 650 */ 651 if (shdr->sh_link < ndx) { 652 Is_desc *isp = ifl->ifl_isdesc[shdr->sh_link]; 653 654 if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) && 655 (isp->is_shdr->sh_type != SHT_DYNSYM))) { 656 eprintf(ofl->ofl_lml, ERR_FATAL, 657 MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name, name, 658 EC_XWORD(shdr->sh_link)); 659 return (S_ERROR); 660 } 661 isp->is_symshndx = ifl->ifl_isdesc[ndx]; 662 } 663 return (1); 664 } 665 666 /* 667 * Final processing for SHT_SYMTAB_SHNDX section. 668 */ 669 static uintptr_t 670 /* ARGSUSED2 */ 671 sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 672 { 673 if (isc->is_shdr->sh_link > isc->is_scnndx) { 674 Is_desc *isp = ifl->ifl_isdesc[isc->is_shdr->sh_link]; 675 676 if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) && 677 (isp->is_shdr->sh_type != SHT_DYNSYM))) { 678 eprintf(ofl->ofl_lml, ERR_FATAL, 679 MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name, 680 isc->is_name, EC_XWORD(isc->is_shdr->sh_link)); 681 return (S_ERROR); 682 } 683 isp->is_symshndx = isc; 684 } 685 return (1); 686 } 687 688 /* 689 * Process .dynamic section from a relocatable object. 690 * 691 * Note: That the .dynamic section is only considered interesting when 692 * dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get 693 * set when libld is called from ld.so.1). 694 */ 695 /*ARGSUSED*/ 696 static uintptr_t 697 process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 698 Word ndx, int ident, Ofl_desc *ofl) 699 { 700 Dyn *dyn; 701 Elf_Scn *strscn; 702 Elf_Data *dp; 703 char *str; 704 705 /* 706 * Process .dynamic sections from relocatable objects ? 707 */ 708 if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0) 709 return (1); 710 711 /* 712 * Find the string section associated with the .dynamic section. 713 */ 714 if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) { 715 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN), 716 ifl->ifl_name); 717 ofl->ofl_flags |= FLG_OF_FATAL; 718 return (0); 719 } 720 dp = elf_getdata(strscn, NULL); 721 str = (char *)dp->d_buf; 722 723 /* 724 * And get the .dynamic data 725 */ 726 dp = elf_getdata(scn, NULL); 727 728 for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) { 729 Ifl_desc *difl; 730 731 switch (dyn->d_tag) { 732 case DT_NEEDED: 733 case DT_USED: 734 if (((difl = 735 libld_calloc(1, sizeof (Ifl_desc))) == 0) || 736 (list_appendc(&ofl->ofl_sos, difl) == 0)) 737 return (S_ERROR); 738 739 difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC); 740 difl->ifl_soname = str + (size_t)dyn->d_un.d_val; 741 difl->ifl_flags = FLG_IF_NEEDSTR; 742 break; 743 case DT_RPATH: 744 case DT_RUNPATH: 745 if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath, 746 (str + (size_t)dyn->d_un.d_val))) == 747 (const char *)S_ERROR) 748 return (S_ERROR); 749 break; 750 } 751 } 752 return (1); 753 } 754 755 /* 756 * Expand implicit references. Dependencies can be specified in terms of the 757 * $ORIGIN, $PLATFORM, $OSREL and $OSNAME tokens, either from their needed name, 758 * or via a runpath. In addition runpaths may also specify the $ISALIST token. 759 * 760 * Probably the most common reference to explicit dependencies (via -L) will be 761 * sufficient to find any associated implicit dependencies, but just in case we 762 * expand any occurrence of these known tokens here. 763 * 764 * Note, if any errors occur we simply return the original name. 765 * 766 * This code is remarkably similar to expand() in rtld/common/paths.c. 767 */ 768 static char *platform = 0; 769 static size_t platform_sz = 0; 770 static Isa_desc *isa = 0; 771 static Uts_desc *uts = 0; 772 773 static char * 774 expand(const char *parent, const char *name, char **next) 775 { 776 char _name[PATH_MAX], *nptr, *_next; 777 const char *optr; 778 size_t nrem = PATH_MAX - 1; 779 int expanded = 0, _expanded, isaflag = 0; 780 781 optr = name; 782 nptr = _name; 783 784 while (*optr) { 785 if (nrem == 0) 786 return ((char *)name); 787 788 if (*optr != '$') { 789 *nptr++ = *optr++, nrem--; 790 continue; 791 } 792 793 _expanded = 0; 794 795 if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN), 796 MSG_STR_ORIGIN_SIZE) == 0) { 797 char *eptr; 798 799 /* 800 * For $ORIGIN, expansion is really just a concatenation 801 * of the parents directory name. For example, an 802 * explicit dependency foo/bar/lib1.so with a dependency 803 * on $ORIGIN/lib2.so would be expanded to 804 * foo/bar/lib2.so. 805 */ 806 if ((eptr = strrchr(parent, '/')) == 0) { 807 *nptr++ = '.'; 808 nrem--; 809 } else { 810 size_t len = eptr - parent; 811 812 if (len >= nrem) 813 return ((char *)name); 814 815 (void) strncpy(nptr, parent, len); 816 nptr = nptr + len; 817 nrem -= len; 818 } 819 optr += MSG_STR_ORIGIN_SIZE; 820 expanded = _expanded = 1; 821 822 } else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM), 823 MSG_STR_PLATFORM_SIZE) == 0) { 824 /* 825 * Establish the platform from sysconf - like uname -i. 826 */ 827 if ((platform == 0) && (platform_sz == 0)) { 828 char info[SYS_NMLN]; 829 long size; 830 831 size = sysinfo(SI_PLATFORM, info, SYS_NMLN); 832 if ((size != -1) && 833 (platform = libld_malloc((size_t)size))) { 834 (void) strcpy(platform, info); 835 platform_sz = (size_t)size - 1; 836 } else 837 platform_sz = 1; 838 } 839 if (platform != 0) { 840 if (platform_sz >= nrem) 841 return ((char *)name); 842 843 (void) strncpy(nptr, platform, platform_sz); 844 nptr = nptr + platform_sz; 845 nrem -= platform_sz; 846 847 optr += MSG_STR_PLATFORM_SIZE; 848 expanded = _expanded = 1; 849 } 850 851 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME), 852 MSG_STR_OSNAME_SIZE) == 0) { 853 /* 854 * Establish the os name - like uname -s. 855 */ 856 if (uts == 0) 857 uts = conv_uts(); 858 859 if (uts && uts->uts_osnamesz) { 860 if (uts->uts_osnamesz >= nrem) 861 return ((char *)name); 862 863 (void) strncpy(nptr, uts->uts_osname, 864 uts->uts_osnamesz); 865 nptr = nptr + uts->uts_osnamesz; 866 nrem -= uts->uts_osnamesz; 867 868 optr += MSG_STR_OSNAME_SIZE; 869 expanded = _expanded = 1; 870 } 871 872 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL), 873 MSG_STR_OSREL_SIZE) == 0) { 874 /* 875 * Establish the os release - like uname -r. 876 */ 877 if (uts == 0) 878 uts = conv_uts(); 879 880 if (uts && uts->uts_osrelsz) { 881 if (uts->uts_osrelsz >= nrem) 882 return ((char *)name); 883 884 (void) strncpy(nptr, uts->uts_osrel, 885 uts->uts_osrelsz); 886 nptr = nptr + uts->uts_osrelsz; 887 nrem -= uts->uts_osrelsz; 888 889 optr += MSG_STR_OSREL_SIZE; 890 expanded = _expanded = 1; 891 } 892 893 } else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST), 894 MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) { 895 /* 896 * Establish instruction sets from sysconf. Note that 897 * this is only meaningful from runpaths. 898 */ 899 if (isa == 0) 900 isa = conv_isalist(); 901 902 if (isa && isa->isa_listsz && 903 (nrem > isa->isa_opt->isa_namesz)) { 904 size_t mlen, tlen, hlen = optr - name; 905 size_t no; 906 char *lptr; 907 Isa_opt *opt = isa->isa_opt; 908 909 (void) strncpy(nptr, opt->isa_name, 910 opt->isa_namesz); 911 nptr = nptr + opt->isa_namesz; 912 nrem -= opt->isa_namesz; 913 914 optr += MSG_STR_ISALIST_SIZE; 915 expanded = _expanded = 1; 916 917 tlen = strlen(optr); 918 919 /* 920 * As ISALIST expands to a number of elements, 921 * establish a new list to return to the caller. 922 * This will contain the present path being 923 * processed redefined for each isalist option, 924 * plus the original remaining list entries. 925 */ 926 mlen = ((hlen + tlen) * (isa->isa_optno - 1)) + 927 isa->isa_listsz - opt->isa_namesz; 928 if (*next) 929 mlen += strlen(*next); 930 if ((_next = lptr = libld_malloc(mlen)) == 0) 931 return (0); 932 933 for (no = 1, opt++; no < isa->isa_optno; 934 no++, opt++) { 935 (void) strncpy(lptr, name, hlen); 936 lptr = lptr + hlen; 937 (void) strncpy(lptr, opt->isa_name, 938 opt->isa_namesz); 939 lptr = lptr + opt->isa_namesz; 940 (void) strncpy(lptr, optr, tlen); 941 lptr = lptr + tlen; 942 *lptr++ = ':'; 943 } 944 if (*next) 945 (void) strcpy(lptr, *next); 946 else 947 *--lptr = '\0'; 948 } 949 } 950 951 /* 952 * If no expansion occurred skip the $ and continue. 953 */ 954 if (_expanded == 0) 955 *nptr++ = *optr++, nrem--; 956 } 957 958 /* 959 * If any ISALIST processing has occurred not only do we return the 960 * expanded node we're presently working on, but we must also update the 961 * remaining list so that it is effectively prepended with this node 962 * expanded to all remaining isalist options. Note that we can only 963 * handle one ISALIST per node. For more than one ISALIST to be 964 * processed we'd need a better algorithm than above to replace the 965 * newly generated list. Whether we want to encourage the number of 966 * pathname permutations this would provide is another question. So, for 967 * now if more than one ISALIST is encountered we return the original 968 * node untouched. 969 */ 970 if (isaflag) { 971 if (isaflag == 1) 972 *next = _next; 973 else 974 return ((char *)name); 975 } 976 977 *nptr = '\0'; 978 979 if (expanded) { 980 if ((nptr = libld_malloc(strlen(_name) + 1)) == 0) 981 return ((char *)name); 982 (void) strcpy(nptr, _name); 983 return (nptr); 984 } 985 return ((char *)name); 986 } 987 988 /* 989 * Process a dynamic section. If we are processing an explicit shared object 990 * then we need to determine if it has a recorded SONAME, if so, this name will 991 * be recorded in the output file being generated as the NEEDED entry rather 992 * than the shared objects filename itself. 993 * If the mode of the link-edit indicates that no undefined symbols should 994 * remain, then we also need to build up a list of any additional shared object 995 * dependencies this object may have. In this case save any NEEDED entries 996 * together with any associated run-path specifications. This information is 997 * recorded on the `ofl_soneed' list and will be analyzed after all explicit 998 * file processing has been completed (refer finish_libs()). 999 */ 1000 static uintptr_t 1001 process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 1002 { 1003 Dyn *data, *dyn; 1004 char *str, *rpath = NULL; 1005 const char *soname, *needed; 1006 Sdf_desc *sdf; 1007 Listnode *lnp; 1008 1009 data = (Dyn *)isc->is_indata->d_buf; 1010 str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf; 1011 1012 /* 1013 * First loop through the dynamic section looking for a run path. 1014 */ 1015 if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) { 1016 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) { 1017 if ((dyn->d_tag != DT_RPATH) && 1018 (dyn->d_tag != DT_RUNPATH)) 1019 continue; 1020 if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL) 1021 continue; 1022 break; 1023 } 1024 } 1025 1026 /* 1027 * Now look for any needed dependencies (which may use the rpath) 1028 * or a new SONAME. 1029 */ 1030 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) { 1031 if (dyn->d_tag == DT_SONAME) { 1032 if ((soname = str + (size_t)dyn->d_un.d_val) == NULL) 1033 continue; 1034 1035 /* 1036 * Update the input file structure with this new name. 1037 */ 1038 ifl->ifl_soname = soname; 1039 1040 } else if ((dyn->d_tag == DT_NEEDED) || 1041 (dyn->d_tag == DT_USED)) { 1042 if (!(ofl->ofl_flags & 1043 (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC))) 1044 continue; 1045 if ((needed = str + (size_t)dyn->d_un.d_val) == NULL) 1046 continue; 1047 1048 /* 1049 * Determine if this needed entry is already recorded on 1050 * the shared object needed list, if not create a new 1051 * definition for later processing (see finish_libs()). 1052 */ 1053 needed = expand(ifl->ifl_name, needed, (char **)0); 1054 1055 if ((sdf = sdf_find(needed, &ofl->ofl_soneed)) == 0) { 1056 if ((sdf = sdf_add(needed, 1057 &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR) 1058 return (S_ERROR); 1059 sdf->sdf_rfile = ifl->ifl_name; 1060 } 1061 1062 /* 1063 * Record the runpath (Note that we take the first 1064 * runpath which is exactly what ld.so.1 would do during 1065 * its dependency processing). 1066 */ 1067 if (rpath && (sdf->sdf_rpath == 0)) 1068 sdf->sdf_rpath = rpath; 1069 1070 } else if (dyn->d_tag == DT_FLAGS_1) { 1071 if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE)) 1072 ifl->ifl_flags &= ~FLG_IF_LAZYLD; 1073 if (dyn->d_un.d_val & DF_1_DISPRELPND) 1074 ifl->ifl_flags |= FLG_IF_DISPPEND; 1075 if (dyn->d_un.d_val & DF_1_DISPRELDNE) 1076 ifl->ifl_flags |= FLG_IF_DISPDONE; 1077 if (dyn->d_un.d_val & DF_1_NODIRECT) 1078 ifl->ifl_flags |= FLG_IF_NODIRECT; 1079 1080 } else if ((dyn->d_tag == DT_AUDIT) && 1081 (ifl->ifl_flags & FLG_IF_NEEDED)) { 1082 /* 1083 * Record audit string as DT_DEPAUDIT. 1084 */ 1085 if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit, 1086 (str + (size_t)dyn->d_un.d_val))) == 1087 (const char *)S_ERROR) 1088 return (S_ERROR); 1089 1090 } else if (dyn->d_tag == DT_SUNW_RTLDINF) { 1091 /* 1092 * If a library has the SUNW_RTLDINF .dynamic entry 1093 * then we must not permit lazyloading of this library. 1094 * This is because critical startup information (TLS 1095 * routines) are provided as part of these interfaces 1096 * and we must have them as part of process startup. 1097 */ 1098 ifl->ifl_flags &= ~FLG_IF_LAZYLD; 1099 } 1100 } 1101 1102 /* 1103 * Perform some SONAME sanity checks. 1104 */ 1105 if (ifl->ifl_flags & FLG_IF_NEEDED) { 1106 Ifl_desc * sifl; 1107 1108 /* 1109 * Determine if anyone else will cause the same SONAME to be 1110 * used (this is either caused by two different files having the 1111 * same SONAME, or by one file SONAME actually matching another 1112 * file basename (if no SONAME is specified within a shared 1113 * library its basename will be used)). Probably rare, but some 1114 * idiot will do it. 1115 */ 1116 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, sifl)) { 1117 if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) && 1118 (ifl != sifl)) { 1119 const char *hint, *iflb, *siflb; 1120 1121 /* 1122 * Determine the basename of each file. Perhaps 1123 * there are multiple copies of the same file 1124 * being brought in using different -L search 1125 * paths, and if so give an extra hint in the 1126 * error message. 1127 */ 1128 iflb = strrchr(ifl->ifl_name, '/'); 1129 if (iflb == NULL) 1130 iflb = ifl->ifl_name; 1131 else 1132 iflb++; 1133 1134 siflb = strrchr(sifl->ifl_name, '/'); 1135 if (siflb == NULL) 1136 siflb = sifl->ifl_name; 1137 else 1138 siflb++; 1139 1140 if (strcmp(iflb, siflb) == 0) 1141 hint = MSG_INTL(MSG_REC_CNFLTHINT); 1142 else 1143 hint = MSG_ORIG(MSG_STR_EMPTY); 1144 1145 eprintf(ofl->ofl_lml, ERR_FATAL, 1146 MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name, 1147 ifl->ifl_name, sifl->ifl_soname, hint); 1148 ofl->ofl_flags |= FLG_OF_FATAL; 1149 return (0); 1150 } 1151 } 1152 1153 /* 1154 * If the SONAME is the same as the name the user wishes to 1155 * record when building a dynamic library (refer -h option), 1156 * we also have a name clash. 1157 */ 1158 if (ofl->ofl_soname && 1159 (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) { 1160 eprintf(ofl->ofl_lml, ERR_FATAL, 1161 MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name, 1162 ifl->ifl_soname); 1163 ofl->ofl_flags |= FLG_OF_FATAL; 1164 return (0); 1165 } 1166 } 1167 return (1); 1168 } 1169 1170 /* 1171 * Process a relocation entry. At this point all input sections from this 1172 * input file have been assigned an input section descriptor which is saved 1173 * in the `ifl_isdesc' array. 1174 */ 1175 static uintptr_t 1176 rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 1177 { 1178 Word rndx; 1179 Is_desc *risc; 1180 Os_desc *osp; 1181 Shdr *shdr = isc->is_shdr; 1182 1183 /* 1184 * Make sure this is a valid relocation we can handle. 1185 */ 1186 if (shdr->sh_type != M_REL_SHT_TYPE) { 1187 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC), 1188 ifl->ifl_name, isc->is_name, 1189 conv_sec_type(ifl->ifl_ehdr->e_machine, shdr->sh_type, 0)); 1190 ofl->ofl_flags |= FLG_OF_FATAL; 1191 return (0); 1192 } 1193 1194 /* 1195 * From the relocation section header information determine which 1196 * section needs the actual relocation. Determine which output section 1197 * this input section has been assigned to and add to its relocation 1198 * list. Note that the relocation section may be null if it is not 1199 * required (ie. .debug, .stabs, etc). 1200 */ 1201 rndx = shdr->sh_info; 1202 if (rndx >= ifl->ifl_shnum) { 1203 /* 1204 * Broken input file. 1205 */ 1206 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO), 1207 ifl->ifl_name, isc->is_name, EC_XWORD(rndx)); 1208 ofl->ofl_flags |= FLG_OF_FATAL; 1209 return (0); 1210 } 1211 if (rndx == 0) { 1212 if (list_appendc(&ofl->ofl_extrarels, isc) == 0) 1213 return (S_ERROR); 1214 } else if ((risc = ifl->ifl_isdesc[rndx]) != 0) { 1215 /* 1216 * Discard relocations if they are against a section 1217 * which has been discarded. 1218 */ 1219 if (risc->is_flags & FLG_IS_DISCARD) 1220 return (1); 1221 if ((osp = risc->is_osdesc) == 0) { 1222 if (risc->is_shdr->sh_type == SHT_SUNW_move) { 1223 /* 1224 * This section is processed later 1225 * in sunwmove_preprocess() and 1226 * reloc_init(). 1227 */ 1228 if (list_appendc(&ofl->ofl_mvrelisdescs, 1229 isc) == 0) 1230 return (S_ERROR); 1231 return (1); 1232 } 1233 eprintf(ofl->ofl_lml, ERR_FATAL, 1234 MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name, 1235 isc->is_name, risc->is_name); 1236 return (0); 1237 } 1238 if (list_appendc(&osp->os_relisdescs, isc) == 0) 1239 return (S_ERROR); 1240 } 1241 return (1); 1242 } 1243 1244 /* 1245 * SHF_EXCLUDE flags is set for this section. 1246 */ 1247 static uintptr_t 1248 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 1249 Word ndx, Ofl_desc *ofl) 1250 { 1251 /* 1252 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might 1253 * be needed for ld processing. These sections need to be in the 1254 * internal table. Later it will be determined whether they can be 1255 * eliminated or not. 1256 */ 1257 if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM) 1258 return (0); 1259 1260 /* 1261 * Other checks 1262 */ 1263 if (shdr->sh_flags & SHF_ALLOC) { 1264 /* 1265 * A conflict, issue an warning message, and ignore the section. 1266 */ 1267 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE), 1268 ifl->ifl_name, name); 1269 return (0); 1270 } 1271 1272 /* 1273 * This sections is not going to the output file. 1274 */ 1275 return (process_section(name, ifl, shdr, scn, ndx, 0, ofl)); 1276 } 1277 1278 #if defined(__x86) && defined(_ELF64) 1279 1280 static uintptr_t 1281 process_amd64_unwind(const char *name, Ifl_desc *ifl, Shdr *shdr, 1282 Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl) 1283 { 1284 Os_desc *osp, *eosp; 1285 Is_desc *isp; 1286 Listnode *lnp; 1287 1288 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 1289 return (S_ERROR); 1290 1291 /* 1292 * When producing a relocatable object - just collect the sections. 1293 */ 1294 if (ofl->ofl_flags & FLG_OF_RELOBJ) 1295 return (1); 1296 1297 /* 1298 * If producing a executable or shared library, keep track of all the 1299 * output UNWIND sections to allow the creation of the appropriate 1300 * frame_hdr information. 1301 * 1302 * If the section hasn't been placed in the output file, then there's 1303 * nothing for us to do. 1304 */ 1305 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 1306 ((osp = isp->is_osdesc) == 0)) 1307 return (1); 1308 1309 /* 1310 * Check to see if this output section is already on the list, and if 1311 * not, add it. 1312 */ 1313 for (LIST_TRAVERSE(&ofl->ofl_unwind, lnp, eosp)) 1314 if (osp == eosp) 1315 return (1); 1316 1317 if (list_appendc(&ofl->ofl_unwind, osp) == 0) 1318 return (S_ERROR); 1319 1320 return (1); 1321 } 1322 1323 #endif 1324 1325 /* 1326 * Section processing state table. `Initial' describes the required initial 1327 * procedure to be called (if any), `Final' describes the final processing 1328 * procedure (ie. things that can only be done when all required sections 1329 * have been collected). 1330 */ 1331 static uintptr_t (*Initial[SHT_NUM][2])() = { 1332 1333 /* ET_REL ET_DYN */ 1334 1335 /* SHT_NULL */ invalid_section, invalid_section, 1336 /* SHT_PROGBITS */ process_progbits, process_progbits, 1337 /* SHT_SYMTAB */ process_input, process_input, 1338 /* SHT_STRTAB */ process_strtab, process_strtab, 1339 /* SHT_RELA */ process_reloc, process_reloc, 1340 /* SHT_HASH */ invalid_section, NULL, 1341 /* SHT_DYNAMIC */ process_rel_dynamic, process_section, 1342 /* SHT_NOTE */ process_section, NULL, 1343 /* SHT_NOBITS */ process_nobits, process_nobits, 1344 /* SHT_REL */ process_reloc, process_reloc, 1345 /* SHT_SHLIB */ process_section, invalid_section, 1346 /* SHT_DYNSYM */ invalid_section, process_input, 1347 /* SHT_UNKNOWN12 */ process_progbits, process_progbits, 1348 /* SHT_UNKNOWN13 */ process_progbits, process_progbits, 1349 /* SHT_INIT_ARRAY */ process_array, NULL, 1350 /* SHT_FINI_ARRAY */ process_array, NULL, 1351 /* SHT_PREINIT_ARRAY */ process_array, NULL, 1352 /* SHT_GROUP */ process_section, invalid_section, 1353 /* SHT_SYMTAB_SHNDX */ process_sym_shndx, NULL 1354 }; 1355 1356 static uintptr_t (*Final[SHT_NUM][2])() = { 1357 1358 /* SHT_NULL */ NULL, NULL, 1359 /* SHT_PROGBITS */ NULL, NULL, 1360 /* SHT_SYMTAB */ ld_sym_process, ld_sym_process, 1361 /* SHT_STRTAB */ NULL, NULL, 1362 /* SHT_RELA */ rel_process, NULL, 1363 /* SHT_HASH */ NULL, NULL, 1364 /* SHT_DYNAMIC */ NULL, process_dynamic, 1365 /* SHT_NOTE */ NULL, NULL, 1366 /* SHT_NOBITS */ NULL, NULL, 1367 /* SHT_REL */ rel_process, NULL, 1368 /* SHT_SHLIB */ NULL, NULL, 1369 /* SHT_DYNSYM */ NULL, ld_sym_process, 1370 /* SHT_UNKNOWN12 */ NULL, NULL, 1371 /* SHT_UNKNOWN13 */ NULL, NULL, 1372 /* SHT_INIT_ARRAY */ NULL, NULL, 1373 /* SHT_FINI_ARRAY */ NULL, NULL, 1374 /* SHT_PREINIT_ARRAY */ NULL, NULL, 1375 /* SHT_GROUP */ NULL, NULL, 1376 /* SHT_SYMTAB_SHNDX */ sym_shndx_process, NULL 1377 }; 1378 1379 #define MAXNDXSIZE 10 1380 1381 /* 1382 * Process an elf file. Each section is compared against the section state 1383 * table to determine whether it should be processed (saved), ignored, or 1384 * is invalid for the type of input file being processed. 1385 */ 1386 static uintptr_t 1387 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl) 1388 { 1389 Elf_Scn *scn; 1390 Shdr *shdr; 1391 Word ndx, sndx; 1392 char *str, *name, _name[MAXNDXSIZE]; 1393 Word row, column; 1394 int ident; 1395 uintptr_t error; 1396 Is_desc *vdfisp, *vndisp, *vsyisp, *sifisp, *capisp; 1397 Sdf_desc *sdf; 1398 Word ordered_shndx = 0; /* index to first ordered section */ 1399 Word ordered_cnt = 0; 1400 1401 /* 1402 * First process the .shstrtab section so that later sections can 1403 * reference their name. 1404 */ 1405 ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf); 1406 1407 sndx = ifl->ifl_shstrndx; 1408 if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) { 1409 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN), 1410 ifl->ifl_name); 1411 ofl->ofl_flags |= FLG_OF_FATAL; 1412 return (0); 1413 } 1414 if ((shdr = elf_getshdr(scn)) == NULL) { 1415 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR), 1416 ifl->ifl_name); 1417 ofl->ofl_flags |= FLG_OF_FATAL; 1418 return (0); 1419 } 1420 if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) == 1421 NULL) { 1422 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), 1423 ifl->ifl_name); 1424 ofl->ofl_flags |= FLG_OF_FATAL; 1425 return (0); 1426 } 1427 1428 if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn, 1429 elf) == S_ERROR) 1430 return (S_ERROR); 1431 1432 /* 1433 * Reset the name since the shdr->sh_name could have been changed as 1434 * part of ld_sup_input_section(). If there is no name, fabricate one 1435 * using the section index. 1436 */ 1437 if (shdr->sh_name == 0) { 1438 (void) snprintf(_name, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX), 1439 EC_XWORD(sndx)); 1440 if ((name = libld_malloc(strlen(_name) + 1)) == 0) 1441 return (S_ERROR); 1442 (void) strcpy(name, _name); 1443 1444 } else if ((name = elf_strptr(elf, (size_t)sndx, 1445 (size_t)shdr->sh_name)) == NULL) { 1446 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), 1447 ifl->ifl_name); 1448 ofl->ofl_flags |= FLG_OF_FATAL; 1449 return (0); 1450 } 1451 1452 error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl); 1453 if ((error == 0) || (error == S_ERROR)) 1454 return (error); 1455 str = ifl->ifl_isdesc[sndx]->is_indata->d_buf; 1456 1457 /* 1458 * Determine the state table column from the input file type. Note, 1459 * shared library sections are not added to the output section list. 1460 */ 1461 if (ifl->ifl_ehdr->e_type == ET_DYN) { 1462 column = 1; 1463 ofl->ofl_soscnt++; 1464 ident = M_ID_NULL; 1465 } else { 1466 column = 0; 1467 ofl->ofl_objscnt++; 1468 ident = M_ID_UNKNOWN; 1469 } 1470 1471 DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl)); 1472 ndx = 0; 1473 vdfisp = vndisp = vsyisp = sifisp = capisp = 0; 1474 scn = NULL; 1475 while (scn = elf_nextscn(elf, scn)) { 1476 ndx++; 1477 /* 1478 * As we've already processed the .shstrtab don't do it again. 1479 */ 1480 if (ndx == sndx) 1481 continue; 1482 1483 if ((shdr = elf_getshdr(scn)) == NULL) { 1484 eprintf(ofl->ofl_lml, ERR_ELF, 1485 MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name); 1486 ofl->ofl_flags |= FLG_OF_FATAL; 1487 return (0); 1488 } 1489 name = str + (size_t)(shdr->sh_name); 1490 1491 if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn, 1492 elf) == S_ERROR) 1493 return (S_ERROR); 1494 1495 /* 1496 * Reset the name since the shdr->sh_name could have been 1497 * changed as part of ld_sup_input_section(). If there is no 1498 * name, fabricate one using the section index. 1499 */ 1500 if (shdr->sh_name == 0) { 1501 (void) snprintf(_name, MAXNDXSIZE, 1502 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(ndx)); 1503 if ((name = libld_malloc(strlen(_name) + 1)) == 0) 1504 return (S_ERROR); 1505 (void) strcpy(name, _name); 1506 } else 1507 name = str + (size_t)(shdr->sh_name); 1508 1509 row = shdr->sh_type; 1510 1511 /* 1512 * If the section has the SHF_EXCLUDE flag on, and we're not 1513 * generating a relocatable object, exclude the section. 1514 */ 1515 if (((shdr->sh_flags & SHF_EXCLUDE) != 0) && 1516 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) { 1517 if ((error = process_exclude(name, ifl, shdr, scn, 1518 ndx, ofl)) == S_ERROR) 1519 return (S_ERROR); 1520 if (error == 1) 1521 continue; 1522 } 1523 1524 /* 1525 * If this is a standard section type process it via the 1526 * appropriate action routine. 1527 */ 1528 if (row < SHT_NUM) { 1529 if (Initial[row][column] != NULL) { 1530 if (Initial[row][column](name, ifl, shdr, scn, 1531 ndx, ident, ofl) == S_ERROR) 1532 return (S_ERROR); 1533 } 1534 } else { 1535 /* 1536 * If this section is below SHT_LOSUNW then we don't 1537 * really know what to do with it, issue a warning 1538 * message but do the basic section processing anyway. 1539 */ 1540 if (row < (Word)SHT_LOSUNW) 1541 eprintf(ofl->ofl_lml, ERR_WARNING, 1542 MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name, 1543 name, 1544 conv_sec_type(ifl->ifl_ehdr->e_machine, 1545 shdr->sh_type, 0)); 1546 1547 /* 1548 * Handle sections greater than SHT_LOSUNW. 1549 */ 1550 switch (row) { 1551 case SHT_SUNW_dof: 1552 if (process_section(name, ifl, shdr, scn, 1553 ndx, ident, ofl) == S_ERROR) 1554 return (S_ERROR); 1555 break; 1556 case SHT_SUNW_cap: 1557 if (process_section(name, ifl, shdr, scn, 1558 ndx, M_ID_NULL, ofl) == S_ERROR) 1559 return (S_ERROR); 1560 capisp = ifl->ifl_isdesc[ndx]; 1561 break; 1562 case SHT_SUNW_DEBUGSTR: 1563 case SHT_SUNW_DEBUG: 1564 if (process_debug(name, ifl, shdr, scn, 1565 ndx, ident, ofl) == S_ERROR) 1566 return (S_ERROR); 1567 break; 1568 case SHT_SUNW_move: 1569 if (process_section(name, ifl, shdr, scn, 1570 ndx, M_ID_NULL, ofl) == S_ERROR) 1571 return (S_ERROR); 1572 break; 1573 case SHT_SUNW_syminfo: 1574 if (process_section(name, ifl, shdr, scn, 1575 ndx, M_ID_NULL, ofl) == S_ERROR) 1576 return (S_ERROR); 1577 sifisp = ifl->ifl_isdesc[ndx]; 1578 break; 1579 case SHT_SUNW_ANNOTATE: 1580 case SHT_SUNW_COMDAT: 1581 if (process_progbits(name, ifl, shdr, scn, 1582 ndx, ident, ofl) == S_ERROR) 1583 return (S_ERROR); 1584 break; 1585 case SHT_SUNW_verdef: 1586 if (process_section(name, ifl, shdr, scn, 1587 ndx, M_ID_NULL, ofl) == S_ERROR) 1588 return (S_ERROR); 1589 vdfisp = ifl->ifl_isdesc[ndx]; 1590 break; 1591 case SHT_SUNW_verneed: 1592 if (process_section(name, ifl, shdr, scn, 1593 ndx, M_ID_NULL, ofl) == S_ERROR) 1594 return (S_ERROR); 1595 vndisp = ifl->ifl_isdesc[ndx]; 1596 break; 1597 case SHT_SUNW_versym: 1598 if (process_section(name, ifl, shdr, scn, 1599 ndx, M_ID_NULL, ofl) == S_ERROR) 1600 return (S_ERROR); 1601 vsyisp = ifl->ifl_isdesc[ndx]; 1602 break; 1603 #if defined(__sparc) 1604 case SHT_SPARC_GOTDATA: 1605 if (process_section(name, ifl, shdr, scn, 1606 ndx, M_ID_GOTDATA, ofl) == S_ERROR) 1607 return (S_ERROR); 1608 break; 1609 #endif 1610 #if defined(__x86) && defined(_ELF64) 1611 case SHT_AMD64_UNWIND: 1612 if (column == 0) { 1613 /* 1614 * column == ET_REL 1615 */ 1616 if (process_amd64_unwind(name, ifl, 1617 shdr, scn, ndx, M_ID_UNWIND, 1618 ofl) == S_ERROR) 1619 return (S_ERROR); 1620 } 1621 break; 1622 #endif 1623 default: 1624 if (ident != M_ID_NULL) 1625 ident = M_ID_USER; 1626 if (process_section(name, ifl, shdr, scn, 1627 ndx, ident, ofl) == S_ERROR) 1628 return (S_ERROR); 1629 break; 1630 } 1631 } 1632 1633 /* 1634 * If we have any sections that require ORDERED processing, 1635 * remember the index of the first ordered section. This let's 1636 * us know if we need an ORDERED place_section pass, and if so, 1637 * where to start. 1638 */ 1639 if (ifl->ifl_isdesc[ndx] && 1640 (ifl->ifl_isdesc[ndx]->is_shdr->sh_flags & ALL_SHF_ORDER)) { 1641 ordered_cnt++; 1642 if (ordered_shndx == 0) 1643 ordered_shndx = ndx; 1644 } 1645 } 1646 1647 /* 1648 * Now that all of sections have been placed, scan through any sections 1649 * which have special ordering requirements and place them now. 1650 */ 1651 if (ordered_shndx) { 1652 Word cnt; 1653 1654 for (ndx = ordered_shndx, cnt = 0; 1655 (ndx < ifl->ifl_shnum) && (cnt < ordered_cnt); ndx++) { 1656 Is_desc *isp; 1657 /* LINTED */ 1658 Os_desc *osp; 1659 1660 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 1661 ((isp->is_shdr->sh_flags & ALL_SHF_ORDER) == 0)) 1662 continue; 1663 1664 /* 1665 * If this is an ordered section, process it. 1666 */ 1667 cnt++; 1668 if ((osp = (Os_desc *)ld_process_ordered(ifl, ofl, ndx, 1669 ifl->ifl_shnum)) == (Os_desc *)S_ERROR) 1670 return (S_ERROR); 1671 1672 #if defined(__x86) && defined(_ELF64) 1673 /* 1674 * If this section is 'ordered' then it was not 1675 * caught in the previous 'place_section' operation. 1676 * 1677 * So - now that we have a OSP section for 1678 * the unwind info - record it. 1679 */ 1680 if (osp && 1681 (osp->os_shdr->sh_type == SHT_AMD64_UNWIND) && 1682 (append_amd64_unwind(osp, ofl) == S_ERROR)) 1683 return (S_ERROR); 1684 #endif 1685 } 1686 } 1687 1688 /* 1689 * If this is an explicit shared object determine if the user has 1690 * specified a control definition. This descriptor may specify which 1691 * version definitions can be used from this object (it may also update 1692 * the dependency to USED and supply an alternative SONAME). 1693 */ 1694 sdf = 0; 1695 if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) { 1696 const char *base; 1697 1698 /* 1699 * Use the basename of the input file (typically this is the 1700 * compilation environment name, ie. libfoo.so). 1701 */ 1702 if ((base = strrchr(ifl->ifl_name, '/')) == NULL) 1703 base = ifl->ifl_name; 1704 else 1705 base++; 1706 1707 if ((sdf = sdf_find(base, &ofl->ofl_socntl)) != 0) { 1708 sdf->sdf_file = ifl; 1709 ifl->ifl_sdfdesc = sdf; 1710 } 1711 } 1712 1713 /* 1714 * Process any hardware/software capabilities sections. Only propagate 1715 * capabilities for input relocatable objects. If the object doesn't 1716 * contain any capabilities, any capability state that has already been 1717 * gathered will prevail. 1718 */ 1719 if (capisp && (ifl->ifl_ehdr->e_type == ET_REL)) 1720 process_cap(ifl, capisp, ofl); 1721 1722 /* 1723 * Process any version dependencies. These will establish shared object 1724 * `needed' entries in the same manner as will be generated from the 1725 * .dynamic's NEEDED entries. 1726 */ 1727 if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC))) 1728 if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR) 1729 return (S_ERROR); 1730 1731 /* 1732 * Before processing any symbol resolution or relocations process any 1733 * version sections. 1734 */ 1735 if (vsyisp) 1736 (void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl); 1737 1738 if (ifl->ifl_versym && 1739 (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT)))) 1740 if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR) 1741 return (S_ERROR); 1742 1743 /* 1744 * Having collected the appropriate sections carry out any additional 1745 * processing if necessary. 1746 */ 1747 for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) { 1748 Is_desc * isp; 1749 1750 if ((isp = ifl->ifl_isdesc[ndx]) == 0) 1751 continue; 1752 row = isp->is_shdr->sh_type; 1753 1754 if ((isp->is_flags & FLG_IS_DISCARD) == 0) 1755 ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx, 1756 isp->is_indata, elf); 1757 1758 /* 1759 * If this is a ST_SUNW_move section from a 1760 * a relocatable file, keep the input section. 1761 */ 1762 if ((row == SHT_SUNW_move) && (column == 0)) { 1763 if (list_appendc(&(ofl->ofl_ismove), isp) == 0) 1764 return (S_ERROR); 1765 } 1766 1767 /* 1768 * If this is a standard section type process it via the 1769 * appropriate action routine. 1770 */ 1771 if (row < SHT_NUM) { 1772 if (Final[row][column] != NULL) 1773 if (Final[row][column](isp, ifl, ofl) == 1774 S_ERROR) 1775 return (S_ERROR); 1776 } 1777 } 1778 1779 /* 1780 * After processing any symbol resolution, and if this dependency 1781 * indicates it contains symbols that can't be directly bound to, 1782 * set the symbols appropriately. 1783 */ 1784 if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) == 1785 (FLG_IF_NEEDED | FLG_IF_NODIRECT))) 1786 (void) ld_sym_nodirect(sifisp, ifl, ofl); 1787 1788 return (1); 1789 } 1790 1791 /* 1792 * Process the current input file. There are basically three types of files 1793 * that come through here: 1794 * 1795 * o files explicitly defined on the command line (ie. foo.o or bar.so), 1796 * in this case only the `name' field is valid. 1797 * 1798 * o libraries determined from the -l command line option (ie. -lbar), 1799 * in this case the `soname' field contains the basename of the located 1800 * file. 1801 * 1802 * Any shared object specified via the above two conventions must be recorded 1803 * as a needed dependency. 1804 * 1805 * o libraries specified as dependencies of those libraries already obtained 1806 * via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1), 1807 * in this case the `soname' field contains either a full pathname (if the 1808 * needed entry contained a `/'), or the basename of the located file. 1809 * These libraries are processed to verify symbol binding but are not 1810 * recorded as dependencies of the output file being generated. 1811 */ 1812 Ifl_desc * 1813 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf, 1814 Half flags, Ofl_desc * ofl, Rej_desc * rej) 1815 { 1816 Ifl_desc *ifl; 1817 Ehdr *ehdr; 1818 Listnode *lnp; 1819 uintptr_t error = 0; 1820 struct stat status; 1821 Ar_desc *adp; 1822 Rej_desc _rej; 1823 1824 /* 1825 * If this file was not extracted from an archive obtain its device 1826 * information. This will be used to determine if the file has already 1827 * been processed (rather than simply comparing filenames, the device 1828 * information provides a quicker comparison and detects linked files). 1829 */ 1830 if (!(flags & FLG_IF_EXTRACT)) 1831 (void) fstat(fd, &status); 1832 else { 1833 status.st_dev = 0; 1834 status.st_ino = 0; 1835 } 1836 1837 switch (elf_kind(elf)) { 1838 case ELF_K_AR: 1839 /* 1840 * Determine if we've already come across this archive file. 1841 */ 1842 if (!(flags & FLG_IF_EXTRACT)) { 1843 for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) { 1844 if ((adp->ad_stdev != status.st_dev) || 1845 (adp->ad_stino != status.st_ino)) 1846 continue; 1847 1848 /* 1849 * We've seen this file before so reuse the 1850 * original archive descriptor and discard the 1851 * new elf descriptor. 1852 */ 1853 DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name, 1854 adp->ad_name)); 1855 (void) elf_end(elf); 1856 return ((Ifl_desc *)ld_process_archive(name, fd, 1857 adp, ofl)); 1858 } 1859 } 1860 1861 /* 1862 * As we haven't processed this file before establish a new 1863 * archive descriptor. 1864 */ 1865 adp = ld_ar_setup(name, elf, ofl); 1866 if ((adp == 0) || (adp == (Ar_desc *)S_ERROR)) 1867 return ((Ifl_desc *)adp); 1868 adp->ad_stdev = status.st_dev; 1869 adp->ad_stino = status.st_ino; 1870 1871 ld_sup_file(ofl, name, ELF_K_AR, flags, elf); 1872 1873 return ((Ifl_desc *)ld_process_archive(name, fd, adp, ofl)); 1874 1875 case ELF_K_ELF: 1876 /* 1877 * Obtain the elf header so that we can determine what type of 1878 * elf ELF_K_ELF file this is. 1879 */ 1880 if ((ehdr = elf_getehdr(elf)) == NULL) { 1881 int _class = gelf_getclass(elf); 1882 1883 /* 1884 * Failure could occur for a number of reasons at this 1885 * point. Typically the files class is incorrect (ie. 1886 * user is building 64-bit but managed to pint at 32-bit 1887 * libraries). However any number of elf errors can 1888 * also occur, such as from a truncated or corrupt file. 1889 * Here we try and get the best error message possible. 1890 */ 1891 if (M_CLASS != _class) { 1892 _rej.rej_type = SGS_REJ_CLASS; 1893 _rej.rej_info = (uint_t)_class; 1894 } else { 1895 _rej.rej_type = SGS_REJ_STR; 1896 _rej.rej_str = elf_errmsg(-1); 1897 } 1898 _rej.rej_name = name; 1899 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 1900 if (rej->rej_type == 0) { 1901 *rej = _rej; 1902 rej->rej_name = strdup(_rej.rej_name); 1903 } 1904 return (0); 1905 } 1906 1907 /* 1908 * Determine if we've already come across this file. 1909 */ 1910 if (!(flags & FLG_IF_EXTRACT)) { 1911 List * lst; 1912 1913 if (ehdr->e_type == ET_REL) 1914 lst = &ofl->ofl_objs; 1915 else 1916 lst = &ofl->ofl_sos; 1917 1918 /* 1919 * Traverse the appropriate file list and determine if 1920 * a dev/inode match is found. 1921 */ 1922 for (LIST_TRAVERSE(lst, lnp, ifl)) { 1923 /* 1924 * Ifl_desc generated via -Nneed, therefore no 1925 * actual file behind it. 1926 */ 1927 if (ifl->ifl_flags & FLG_IF_NEEDSTR) 1928 continue; 1929 1930 if ((ifl->ifl_stino != status.st_ino) || 1931 (ifl->ifl_stdev != status.st_dev)) 1932 continue; 1933 1934 /* 1935 * Disregard (skip) this image. 1936 */ 1937 DBG_CALL(Dbg_file_skip(ofl->ofl_lml, 1938 ifl->ifl_name, name)); 1939 (void) elf_end(elf); 1940 1941 /* 1942 * If the file was explicitly defined on the 1943 * command line (this is always the case for 1944 * relocatable objects, and is true for shared 1945 * objects when they weren't specified via -l or 1946 * were dragged in as an implicit dependency), 1947 * then warn the user. 1948 */ 1949 if ((flags & FLG_IF_CMDLINE) || 1950 (ifl->ifl_flags & FLG_IF_CMDLINE)) { 1951 const char *errmsg; 1952 1953 /* 1954 * Determine whether this is the same 1955 * file name as originally encountered 1956 * so as to provide the most 1957 * descriptive diagnostic. 1958 */ 1959 if (strcmp(name, ifl->ifl_name) == 0) 1960 errmsg = MSG_INTL(MSG_FIL_MULINC_1); 1961 else 1962 errmsg = MSG_INTL(MSG_FIL_MULINC_2); 1963 1964 eprintf(ofl->ofl_lml, ERR_WARNING, 1965 errmsg, name, ifl->ifl_name); 1966 } 1967 return (ifl); 1968 } 1969 } 1970 1971 /* 1972 * At this point, we know we need the file. Establish an input 1973 * file descriptor and continue processing. 1974 */ 1975 ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej); 1976 if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR)) 1977 return (ifl); 1978 ifl->ifl_stdev = status.st_dev; 1979 ifl->ifl_stino = status.st_ino; 1980 1981 /* 1982 * If -zignore is in effect, mark this file as a potential 1983 * candidate (the files use isn't actually determined until 1984 * symbol resolution and relocation processing are completed). 1985 */ 1986 if (ofl->ofl_flags1 & FLG_OF1_IGNORE) 1987 ifl->ifl_flags |= FLG_IF_IGNORE; 1988 1989 switch (ehdr->e_type) { 1990 case ET_REL: 1991 ld_mach_eflags(ehdr, ofl); 1992 error = process_elf(ifl, elf, ofl); 1993 break; 1994 case ET_DYN: 1995 if ((ofl->ofl_flags & FLG_OF_STATIC) || 1996 !(ofl->ofl_flags & FLG_OF_DYNLIBS)) { 1997 eprintf(ofl->ofl_lml, ERR_FATAL, 1998 MSG_INTL(MSG_FIL_SOINSTAT), name); 1999 ofl->ofl_flags |= FLG_OF_FATAL; 2000 return (0); 2001 } 2002 2003 /* 2004 * Record any additional shared object information. 2005 * If no soname is specified (eg. this file was 2006 * derived from a explicit filename declaration on the 2007 * command line, ie. bar.so) use the pathname. 2008 * This entry may be overridden if the files dynamic 2009 * section specifies an DT_SONAME value. 2010 */ 2011 if (soname == NULL) 2012 ifl->ifl_soname = ifl->ifl_name; 2013 else 2014 ifl->ifl_soname = soname; 2015 2016 /* 2017 * If direct bindings, lazy loading, or group 2018 * permissions need to be established, mark this object. 2019 */ 2020 if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT) 2021 ifl->ifl_flags |= FLG_IF_DIRECT; 2022 if (ofl->ofl_flags1 & FLG_OF1_LAZYLD) 2023 ifl->ifl_flags |= FLG_IF_LAZYLD; 2024 if (ofl->ofl_flags1 & FLG_OF1_GRPPRM) 2025 ifl->ifl_flags |= FLG_IF_GRPPRM; 2026 error = process_elf(ifl, elf, ofl); 2027 2028 /* 2029 * At this point we know if this file will be 2030 * lazyloaded, or whether bindings to it must be direct. 2031 * In either case, a syminfo section is required. 2032 */ 2033 if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT)) 2034 ofl->ofl_flags |= FLG_OF_SYMINFO; 2035 2036 break; 2037 default: 2038 (void) elf_errno(); 2039 _rej.rej_type = SGS_REJ_UNKFILE; 2040 _rej.rej_name = name; 2041 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 2042 if (rej->rej_type == 0) { 2043 *rej = _rej; 2044 rej->rej_name = strdup(_rej.rej_name); 2045 } 2046 return (0); 2047 } 2048 break; 2049 default: 2050 (void) elf_errno(); 2051 _rej.rej_type = SGS_REJ_UNKFILE; 2052 _rej.rej_name = name; 2053 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 2054 if (rej->rej_type == 0) { 2055 *rej = _rej; 2056 rej->rej_name = strdup(_rej.rej_name); 2057 } 2058 return (0); 2059 } 2060 if ((error == 0) || (error == S_ERROR)) 2061 return ((Ifl_desc *)error); 2062 else 2063 return (ifl); 2064 } 2065 2066 /* 2067 * Having successfully opened a file, set up the necessary elf structures to 2068 * process it further. This small section of processing is slightly different 2069 * from the elf initialization required to process a relocatable object from an 2070 * archive (see libs.c: ld_process_archive()). 2071 */ 2072 Ifl_desc * 2073 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl, 2074 Half flags, Rej_desc *rej) 2075 { 2076 Elf *elf; 2077 const char *npath = opath; 2078 const char *nfile = ofile; 2079 2080 if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) { 2081 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath); 2082 ofl->ofl_flags |= FLG_OF_FATAL; 2083 return (0); 2084 } 2085 2086 /* 2087 * Determine whether the support library wishes to process this open. 2088 * The support library may return: 2089 * . a different ELF descriptor (in which case they should have 2090 * closed the original) 2091 * . a different file descriptor (in which case they should have 2092 * closed the original) 2093 * . a different path and file name (presumably associated with 2094 * a different file descriptor) 2095 * 2096 * A file descriptor of -1, or and ELF descriptor of zero indicates 2097 * the file should be ignored. 2098 */ 2099 ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0, 2100 elf_kind(elf)); 2101 2102 if ((*fd == -1) || (elf == NULL)) 2103 return (0); 2104 2105 return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej)); 2106 } 2107 2108 /* 2109 * Process a required library (i.e. the dependency of a shared object). 2110 * Combine the directory and filename, check the resultant path size, and try 2111 * opening the pathname. 2112 */ 2113 static Ifl_desc * 2114 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file, 2115 Ofl_desc * ofl, Rej_desc * rej) 2116 { 2117 size_t dlen, plen; 2118 int fd; 2119 char path[PATH_MAX]; 2120 const char *_dir = dir; 2121 2122 /* 2123 * Determine the sizes of the directory and filename to insure we don't 2124 * exceed our buffer. 2125 */ 2126 if ((dlen = strlen(dir)) == 0) { 2127 _dir = MSG_ORIG(MSG_STR_DOT); 2128 dlen = 1; 2129 } 2130 dlen++; 2131 plen = dlen + strlen(file) + 1; 2132 if (plen > PATH_MAX) { 2133 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG), 2134 _dir, file); 2135 ofl->ofl_flags |= FLG_OF_FATAL; 2136 return (0); 2137 } 2138 2139 /* 2140 * Build the entire pathname and try and open the file. 2141 */ 2142 (void) strcpy(path, _dir); 2143 (void) strcat(path, MSG_ORIG(MSG_STR_SLASH)); 2144 (void) strcat(path, file); 2145 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name, 2146 sdf->sdf_rfile, path)); 2147 2148 if ((fd = open(path, O_RDONLY)) == -1) 2149 return (0); 2150 else { 2151 Ifl_desc *ifl; 2152 char *_path; 2153 2154 if ((_path = libld_malloc(strlen(path) + 1)) == 0) 2155 return ((Ifl_desc *)S_ERROR); 2156 (void) strcpy(_path, path); 2157 ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, NULL, rej); 2158 if (fd != -1) 2159 (void) close(fd); 2160 return (ifl); 2161 } 2162 } 2163 2164 /* 2165 * Finish any library processing. Walk the list of so's that have been listed 2166 * as "included" by shared objects we have previously processed. Examine them, 2167 * without adding them as explicit dependents of this program, in order to 2168 * complete our symbol definition process. The search path rules are: 2169 * 2170 * o use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then 2171 * 2172 * o use any RPATH defined within the parent shared object, then 2173 * 2174 * o use the default directories, i.e. LIBPATH or -YP. 2175 */ 2176 uintptr_t 2177 ld_finish_libs(Ofl_desc *ofl) 2178 { 2179 Listnode *lnp1; 2180 Sdf_desc *sdf; 2181 Rej_desc rej = { 0 }; 2182 2183 /* 2184 * Make sure we are back in dynamic mode. 2185 */ 2186 ofl->ofl_flags |= FLG_OF_DYNLIBS; 2187 2188 for (LIST_TRAVERSE(&ofl->ofl_soneed, lnp1, sdf)) { 2189 Listnode *lnp2; 2190 char *path, *slash = NULL; 2191 int fd; 2192 Ifl_desc *ifl; 2193 char *file = (char *)sdf->sdf_name; 2194 2195 /* 2196 * See if this file has already been processed. At the time 2197 * this implicit dependency was determined there may still have 2198 * been more explicit dependencies to process. Note, if we ever 2199 * do parse the command line three times we would be able to 2200 * do all this checking when processing the dynamic section. 2201 */ 2202 if (sdf->sdf_file) 2203 continue; 2204 2205 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp2, ifl)) { 2206 if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) && 2207 (strcmp(file, ifl->ifl_soname) == 0)) { 2208 sdf->sdf_file = ifl; 2209 break; 2210 } 2211 } 2212 if (sdf->sdf_file) 2213 continue; 2214 2215 /* 2216 * If the current path name element embeds a "/", then it's to 2217 * be taken "as is", with no searching involved. Process all 2218 * "/" occurrences, so that we can deduce the base file name. 2219 */ 2220 for (path = file; *path; path++) { 2221 if (*path == '/') 2222 slash = path; 2223 } 2224 if (slash) { 2225 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name, 2226 sdf->sdf_rfile, file)); 2227 if ((fd = open(file, O_RDONLY)) == -1) { 2228 eprintf(ofl->ofl_lml, ERR_WARNING, 2229 MSG_INTL(MSG_FIL_NOTFOUND), file, 2230 sdf->sdf_rfile); 2231 } else { 2232 Rej_desc _rej = { 0 }; 2233 2234 ifl = ld_process_open(file, ++slash, &fd, ofl, 2235 NULL, &_rej); 2236 if (fd != -1) 2237 (void) close(fd); 2238 2239 if (ifl == (Ifl_desc *)S_ERROR) { 2240 return (S_ERROR); 2241 } 2242 if (_rej.rej_type) { 2243 eprintf(ofl->ofl_lml, ERR_WARNING, 2244 MSG_INTL(reject[_rej.rej_type]), 2245 _rej.rej_name ? rej.rej_name : 2246 MSG_INTL(MSG_STR_UNKNOWN), 2247 conv_reject_desc(&_rej)); 2248 } else 2249 sdf->sdf_file = ifl; 2250 } 2251 continue; 2252 } 2253 2254 /* 2255 * Now search for this file in any user defined directories. 2256 */ 2257 for (LIST_TRAVERSE(&ofl->ofl_ulibdirs, lnp2, path)) { 2258 Rej_desc _rej = { 0 }; 2259 2260 ifl = process_req_lib(sdf, path, file, ofl, &_rej); 2261 if (ifl == (Ifl_desc *)S_ERROR) { 2262 return (S_ERROR); 2263 } 2264 if (_rej.rej_type) { 2265 if (rej.rej_type == 0) { 2266 rej = _rej; 2267 rej.rej_name = strdup(_rej.rej_name); 2268 } 2269 } 2270 if (ifl) { 2271 sdf->sdf_file = ifl; 2272 break; 2273 } 2274 } 2275 if (sdf->sdf_file) 2276 continue; 2277 2278 /* 2279 * Next use the local rules defined within the parent shared 2280 * object. 2281 */ 2282 if (sdf->sdf_rpath != NULL) { 2283 char *rpath, *next; 2284 2285 rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1); 2286 if (rpath == 0) 2287 return (S_ERROR); 2288 (void) strcpy(rpath, sdf->sdf_rpath); 2289 DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath, 2290 LA_SER_RUNPATH, sdf->sdf_rfile)); 2291 if ((path = strtok_r(rpath, 2292 MSG_ORIG(MSG_STR_COLON), &next)) != NULL) { 2293 do { 2294 Rej_desc _rej = { 0 }; 2295 2296 path = expand(sdf->sdf_rfile, path, 2297 &next); 2298 2299 ifl = process_req_lib(sdf, path, 2300 file, ofl, &_rej); 2301 if (ifl == (Ifl_desc *)S_ERROR) { 2302 return (S_ERROR); 2303 } 2304 if (_rej.rej_type) { 2305 if (rej.rej_type == 0) { 2306 rej = _rej; 2307 rej.rej_name = 2308 strdup(_rej.rej_name); 2309 } 2310 } 2311 if (ifl) { 2312 sdf->sdf_file = ifl; 2313 break; 2314 } 2315 } while ((path = strtok_r(NULL, 2316 MSG_ORIG(MSG_STR_COLON), &next)) != NULL); 2317 } 2318 } 2319 if (sdf->sdf_file) 2320 continue; 2321 2322 /* 2323 * Finally try the default library search directories. 2324 */ 2325 for (LIST_TRAVERSE(&ofl->ofl_dlibdirs, lnp2, path)) { 2326 Rej_desc _rej = { 0 }; 2327 2328 ifl = process_req_lib(sdf, path, file, ofl, &rej); 2329 if (ifl == (Ifl_desc *)S_ERROR) { 2330 return (S_ERROR); 2331 } 2332 if (_rej.rej_type) { 2333 if (rej.rej_type == 0) { 2334 rej = _rej; 2335 rej.rej_name = strdup(_rej.rej_name); 2336 } 2337 } 2338 if (ifl) { 2339 sdf->sdf_file = ifl; 2340 break; 2341 } 2342 } 2343 if (sdf->sdf_file) 2344 continue; 2345 2346 /* 2347 * If we've got this far we haven't found the shared object. 2348 * If an object was found, but was rejected for some reason, 2349 * print a diagnostic to that effect, otherwise generate a 2350 * generic "not found" diagnostic. 2351 */ 2352 if (rej.rej_type) { 2353 eprintf(ofl->ofl_lml, ERR_WARNING, 2354 MSG_INTL(reject[rej.rej_type]), 2355 rej.rej_name ? rej.rej_name : 2356 MSG_INTL(MSG_STR_UNKNOWN), conv_reject_desc(&rej)); 2357 } else { 2358 eprintf(ofl->ofl_lml, ERR_WARNING, 2359 MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile); 2360 } 2361 } 2362 2363 /* 2364 * Finally, now that all objects have been input, make sure any version 2365 * requirements have been met. 2366 */ 2367 return (ld_vers_verify(ofl)); 2368 } 2369