1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 27 */ 28 29 /* 30 * Processing of relocatable objects and shared objects. 31 */ 32 33 /* 34 * ld -- link/editor main program 35 */ 36 #include <sys/types.h> 37 #include <sys/time.h> 38 #include <sys/mman.h> 39 #include <string.h> 40 #include <stdio.h> 41 #include <locale.h> 42 #include <stdarg.h> 43 #include <debug.h> 44 #include "msg.h" 45 #include "_libld.h" 46 47 /* 48 * All target specific code is referenced via this global variable, which 49 * is initialized in ld_main(). This allows the linker to function as 50 * a cross linker, by vectoring to the target-specific code for the 51 * current target machine. 52 */ 53 Target ld_targ; 54 55 /* 56 * A default library search path is used if one was not supplied on the command 57 * line. Note: these strings can not use MSG_ORIG() since they are modified as 58 * part of the path processing. 59 */ 60 #if defined(_ELF64) 61 static char def_Plibpath[] = "/lib/64:/usr/lib/64"; 62 #else 63 static char def_Plibpath[] = "/usr/ccs/lib:/lib:/usr/lib"; 64 #endif 65 66 /* 67 * A default elf header provides for simplifying diagnostic processing. 68 */ 69 static Ehdr def_ehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, 70 ELFCLASSNONE, ELFDATANONE }, 0, EM_NONE, 71 EV_CURRENT }; 72 73 /* 74 * ld-centric wrapper on top of veprintf(): 75 * - Accepts output descriptor rather than linkmap list 76 * - Sets the FLG_OF_FATAL/FLG_OF_WARN flags as necessary 77 */ 78 void 79 ld_eprintf(Ofl_desc *ofl, Error error, const char *format, ...) 80 { 81 va_list args; 82 83 /* Set flag indicating type of error being issued */ 84 switch (error) { 85 case ERR_NONE: 86 case ERR_WARNING_NF: 87 break; 88 case ERR_WARNING: 89 ofl->ofl_flags |= FLG_OF_WARN; 90 break; 91 case ERR_GUIDANCE: 92 if ((ofl->ofl_guideflags & FLG_OFG_ENABLE) == 0) 93 return; 94 ofl->ofl_guideflags |= FLG_OFG_ISSUED; 95 ofl->ofl_flags |= FLG_OF_WARN; 96 break; 97 default: 98 ofl->ofl_flags |= FLG_OF_FATAL; 99 } 100 101 /* Issue the error */ 102 va_start(args, format); 103 veprintf(ofl->ofl_lml, error, format, args); 104 va_end(args); 105 } 106 107 /* 108 * Establish the global state necessary to link the desired machine 109 * target, as reflected by the ld_targ global variable. 110 */ 111 int 112 ld_init_target(Lm_list *lml, Half mach) 113 { 114 switch (mach) { 115 case EM_386: 116 case EM_AMD64: 117 ld_targ = *ld_targ_init_x86(); 118 break; 119 120 case EM_SPARC: 121 case EM_SPARC32PLUS: 122 case EM_SPARCV9: 123 ld_targ = *ld_targ_init_sparc(); 124 break; 125 126 default: 127 { 128 Conv_inv_buf_t inv_buf; 129 130 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TARG_UNSUPPORTED), 131 conv_ehdr_mach(mach, 0, &inv_buf)); 132 return (1); 133 } 134 } 135 136 return (0); 137 } 138 139 140 /* 141 * The main program 142 */ 143 int 144 ld_main(int argc, char **argv, Half mach) 145 { 146 char *sgs_support; /* SGS_SUPPORT environment string */ 147 Half etype; 148 Ofl_desc *ofl; 149 ofl_flag_t save_flg_of_warn; 150 151 /* 152 * Establish a base time. Total time diagnostics are relative to 153 * entering the link-editor here. 154 */ 155 (void) gettimeofday(&DBG_TOTALTIME, NULL); 156 DBG_DELTATIME = DBG_TOTALTIME; 157 158 /* Output file descriptor */ 159 if ((ofl = libld_calloc(1, sizeof (Ofl_desc))) == 0) 160 return (1); 161 162 /* Initialize target state */ 163 if (ld_init_target(NULL, mach) != 0) 164 return (1); 165 166 /* 167 * Set up the default output ELF header to satisfy diagnostic 168 * requirements, and initialize the machine and class details. 169 */ 170 ofl->ofl_dehdr = &def_ehdr; 171 def_ehdr.e_ident[EI_CLASS] = ld_targ.t_m.m_class; 172 def_ehdr.e_ident[EI_DATA] = ld_targ.t_m.m_data; 173 def_ehdr.e_machine = ld_targ.t_m.m_mach; 174 175 /* 176 * Build up linker version string 177 */ 178 if ((ofl->ofl_sgsid = (char *)libld_calloc(MSG_SGS_ID_SIZE + 179 strlen(link_ver_string) + 1, 1)) == NULL) 180 return (1); 181 (void) strcpy(ofl->ofl_sgsid, MSG_ORIG(MSG_SGS_ID)); 182 (void) strcat(ofl->ofl_sgsid, link_ver_string); 183 184 /* 185 * Argument pass one. Get all the input flags (skip any files) and 186 * check for consistency. Return from ld_process_flags() marks the 187 * end of mapfile processing. The entrance criteria and segment 188 * descriptors are complete and in their final form. 189 */ 190 if (ld_process_flags(ofl, argc, argv) == S_ERROR) { 191 /* If any ERR_GUIDANCE messages were issued, add a summary */ 192 if (ofl->ofl_guideflags & FLG_OFG_ISSUED) 193 ld_eprintf(ofl, ERR_GUIDANCE, 194 MSG_INTL(MSG_GUIDE_SUMMARY)); 195 return (1); 196 } 197 if (ofl->ofl_flags & FLG_OF_FATAL) { 198 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_FLAGS)); 199 /* If any ERR_GUIDANCE messages were issued, add a summary */ 200 if (ofl->ofl_guideflags & FLG_OFG_ISSUED) 201 ld_eprintf(ofl, ERR_GUIDANCE, 202 MSG_INTL(MSG_GUIDE_SUMMARY)); 203 return (1); 204 } 205 206 /* 207 * At this point a call such as ld -V is considered complete. 208 */ 209 if (ofl->ofl_flags1 & FLG_OF1_DONE) 210 return (0); 211 212 /* Initialize signal handler */ 213 ld_init_sighandler(ofl); 214 215 /* 216 * Determine whether any support libraries should be loaded, 217 * (either through the SGS_SUPPORT environment variable and/or 218 * through the -S option). 219 */ 220 #if defined(_LP64) 221 if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_64))) == NULL) 222 #else 223 if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_32))) == NULL) 224 #endif 225 sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT)); 226 227 if (sgs_support && sgs_support[0]) { 228 const char *sep = MSG_ORIG(MSG_STR_COLON); 229 char *lib; 230 char *lasts; 231 232 DBG_CALL(Dbg_support_req(ofl->ofl_lml, sgs_support, 233 DBG_SUP_ENVIRON)); 234 if ((lib = strtok_r(sgs_support, sep, &lasts)) != NULL) { 235 do { 236 if (ld_sup_loadso(ofl, lib) == S_ERROR) 237 return (ld_exit(ofl)); 238 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 239 240 } while ((lib = strtok_r(NULL, sep, &lasts)) != NULL); 241 } 242 } 243 if (lib_support) { 244 Aliste idx; 245 char *lib; 246 247 for (APLIST_TRAVERSE(lib_support, idx, lib)) { 248 DBG_CALL(Dbg_support_req(ofl->ofl_lml, lib, 249 DBG_SUP_CMDLINE)); 250 if (ld_sup_loadso(ofl, lib) == S_ERROR) 251 return (ld_exit(ofl)); 252 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 253 } 254 } 255 256 DBG_CALL(Dbg_ent_print(ofl->ofl_lml, 257 ofl->ofl_dehdr->e_ident[EI_OSABI], ofl->ofl_dehdr->e_machine, 258 ofl->ofl_ents)); 259 DBG_CALL(Dbg_seg_list(ofl->ofl_lml, 260 ofl->ofl_dehdr->e_ident[EI_OSABI], ofl->ofl_dehdr->e_machine, 261 ofl->ofl_segs)); 262 263 /* 264 * The objscnt and soscnt variables were used to estimate the expected 265 * input files, and size the symbol hash buckets accordingly. Reset 266 * these values now, so as to gain an accurate count from pass two, for 267 * later statistics diagnostics. 268 */ 269 ofl->ofl_objscnt = ofl->ofl_soscnt = 0; 270 271 /* 272 * Determine whether we can create the file before going any further. 273 */ 274 if (ld_open_outfile(ofl) == S_ERROR) 275 return (ld_exit(ofl)); 276 277 /* 278 * If the user didn't supply a library path supply a default. And, if 279 * no run-path has been specified (-R), see if the environment variable 280 * is in use (historic). 281 */ 282 if (Plibpath == NULL) 283 Plibpath = def_Plibpath; 284 285 if (ofl->ofl_rpath == NULL) { 286 char *rpath; 287 288 if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) && 289 rpath[0]) 290 ofl->ofl_rpath = rpath; 291 } 292 293 /* 294 * Argument pass two. Input all libraries and objects. 295 */ 296 if (ld_lib_setup(ofl) == S_ERROR) 297 return (ld_exit(ofl)); 298 299 /* 300 * Call ld_start() with the etype of our output file and the 301 * output file name. 302 */ 303 if (ofl->ofl_flags & FLG_OF_SHAROBJ) 304 etype = ET_DYN; 305 else if (ofl->ofl_flags & FLG_OF_RELOBJ) 306 etype = ET_REL; 307 else 308 etype = ET_EXEC; 309 310 ld_sup_start(ofl, etype, argv[0]); 311 312 /* 313 * Process all input files. 314 */ 315 if (ld_process_files(ofl, argc, argv) == S_ERROR) 316 return (ld_exit(ofl)); 317 if (ofl->ofl_flags & FLG_OF_FATAL) { 318 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_FILES), 319 ofl->ofl_name); 320 return (ld_exit(ofl)); 321 } 322 323 ld_sup_input_done(ofl); 324 325 /* 326 * Now that all input section processing is complete, validate and 327 * process any SHT_SUNW_move sections. 328 */ 329 if (ofl->ofl_ismove && (ld_process_move(ofl) == S_ERROR)) 330 return (ld_exit(ofl)); 331 332 /* 333 * Before validating all symbols count the number of relocation entries. 334 * If copy relocations exist, COMMON symbols must be generated which are 335 * assigned to the executables .bss. During sym_validate() the actual 336 * size and alignment of the .bss is calculated. Doing things in this 337 * order reduces the number of symbol table traversals required (however 338 * it does take a little longer for the user to be told of any undefined 339 * symbol errors). 340 */ 341 if (ld_reloc_init(ofl) == S_ERROR) 342 return (ld_exit(ofl)); 343 344 /* 345 * We need to know if FLG_OF_WARN is currently set, in case 346 * we need to honor a -z fatal-warnings request. However, we also 347 * need to know if a warning due to symbol validation results from 348 * the upcoming call to ld_sym_validate() in order to issue the 349 * appropriate message for it. So we save the current value, 350 * and clear the main flag. 351 */ 352 save_flg_of_warn = ofl->ofl_flags & FLG_OF_WARN; 353 ofl->ofl_flags &= ~FLG_OF_WARN; 354 355 if (ld_sym_validate(ofl) == S_ERROR) 356 return (ld_exit(ofl)); 357 358 /* 359 * Now that all symbol processing is complete see if any undefined 360 * references still remain. If we observed undefined symbols the 361 * FLG_OF_FATAL bit will be set: If creating a static executable, or a 362 * dynamic executable or shared object with the -zdefs flag set, this 363 * condition is fatal. If creating a shared object with the -Bsymbolic 364 * flag set, this condition is simply a warning. 365 */ 366 if (ofl->ofl_flags & FLG_OF_FATAL) 367 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL), 368 ofl->ofl_name); 369 else if (ofl->ofl_flags & FLG_OF_WARN) 370 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN)); 371 372 /* 373 * Guidance: Use -z defs|nodefs when building shared objects. 374 * 375 * ld_sym_validate() will mask this guidance message out unless we are 376 * intended to send it here, so all we need to do is use OFL_GUIDANCE() 377 * to decide whether to issue it or not. 378 */ 379 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)) 380 ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_DEFS)); 381 382 /* 383 * Symbol processing was the final step before we start producing the 384 * output object. At this time, if we've seen warnings and the 385 * -z fatal-warnings option is specified, promote them to fatal, which 386 * will cause us to exit without creating an object. 387 * 388 * We didn't do this as the warnings were reported in order to 389 * maximize the number of problems a given link-editor invocation 390 * can diagnose. This is safe, since warnings are by definition events 391 * one can choose to ignore. 392 */ 393 if (((ofl->ofl_flags | save_flg_of_warn) & 394 (FLG_OF_WARN | FLG_OF_FATWARN)) == 395 (FLG_OF_WARN | FLG_OF_FATWARN)) 396 ofl->ofl_flags |= FLG_OF_FATAL; 397 398 /* 399 * If fatal errors occurred in symbol processing, or due to warnings 400 * promoted by -z fatal-warnings, this is the end of the line. 401 */ 402 if (ofl->ofl_flags & FLG_OF_FATAL) 403 return (ld_exit(ofl)); 404 405 /* 406 * Generate any necessary sections. 407 */ 408 if (ld_make_sections(ofl) == S_ERROR) 409 return (ld_exit(ofl)); 410 411 /* 412 * Now that all sections have been added to the output file, determine 413 * whether any mapfile section ordering was specified, and verify that 414 * all mapfile ordering directives have been matched. Issue a warning 415 * for any directives that have not been matched. 416 * Also, if SHF_ORDERED sections exist, set up sort key values. 417 */ 418 if (ofl->ofl_flags & (FLG_OF_OS_ORDER | FLG_OF_KEY)) 419 ld_sec_validate(ofl); 420 421 /* 422 * Having collected all the input data create the initial output file 423 * image, assign virtual addresses to the image, and generate a load 424 * map if the user requested one. 425 */ 426 if (ld_create_outfile(ofl) == S_ERROR) 427 return (ld_exit(ofl)); 428 429 if (ld_update_outfile(ofl) == S_ERROR) 430 return (ld_exit(ofl)); 431 if (ofl->ofl_flags & FLG_OF_GENMAP) 432 ld_map_out(ofl); 433 434 /* 435 * Build relocation sections and perform any relocation updates. 436 */ 437 if (ld_reloc_process(ofl) == S_ERROR) 438 return (ld_exit(ofl)); 439 440 /* 441 * Fill in contents for unwind header (.eh_frame_hdr) 442 */ 443 if (ld_unwind_populate_hdr(ofl) == S_ERROR) 444 return (ld_exit(ofl)); 445 446 /* 447 * Finally create the files elf checksum. 448 */ 449 if (ofl->ofl_checksum) 450 *ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf); 451 452 /* 453 * If this is a cross link to a target with a different byte 454 * order than the linker, swap the data to the target byte order. 455 */ 456 if (((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) && 457 (_elf_swap_wrimage(ofl->ofl_elf) != 0)) { 458 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_SWAP_WRIMAGE), 459 ofl->ofl_name); 460 return (ld_exit(ofl)); 461 } 462 463 /* 464 * We're done, so make sure the updates are flushed to the output file. 465 */ 466 if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) { 467 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE), 468 ofl->ofl_name); 469 return (ld_exit(ofl)); 470 } 471 472 ld_sup_atexit(ofl, 0); 473 474 DBG_CALL(Dbg_statistics_ld(ofl)); 475 DBG_CALL(Dbg_basic_finish(ofl->ofl_lml)); 476 477 /* 478 * Wrap up debug output file if one is open 479 */ 480 dbg_cleanup(); 481 482 /* If any ERR_GUIDANCE messages were issued, add a summary */ 483 if (ofl->ofl_guideflags & FLG_OFG_ISSUED) 484 ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_SUMMARY)); 485 486 /* 487 * For performance reasons we don't actually free up the memory we've 488 * allocated, it will be freed when we exit. 489 * 490 * But the below line can be uncommented if/when we want to measure how 491 * our memory consumption and freeing are doing. We should be able to 492 * free all the memory that has been allocated as part of the link-edit 493 * process. 494 */ 495 /* ld_ofl_cleanup(ofl); */ 496 return (0); 497 } 498 499 /* 500 * Cleanup an Ifl_desc. 501 */ 502 static void 503 ifl_list_cleanup(APlist *apl) 504 { 505 Aliste idx; 506 Ifl_desc *ifl; 507 508 for (APLIST_TRAVERSE(apl, idx, ifl)) { 509 if (ifl->ifl_elf) 510 (void) elf_end(ifl->ifl_elf); 511 } 512 } 513 514 /* 515 * Cleanup all memory that has been dynamically allocated during libld 516 * processing and elf_end() all Elf descriptors that are still open. 517 */ 518 void 519 ld_ofl_cleanup(Ofl_desc *ofl) 520 { 521 Ld_heap *chp, *php; 522 Ar_desc *adp; 523 Aliste idx; 524 525 ifl_list_cleanup(ofl->ofl_objs); 526 ofl->ofl_objs = NULL; 527 ifl_list_cleanup(ofl->ofl_sos); 528 ofl->ofl_sos = NULL; 529 530 for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) { 531 Ar_aux *aup; 532 Elf_Arsym *arsym; 533 534 for (arsym = adp->ad_start, aup = adp->ad_aux; 535 arsym->as_name; ++arsym, ++aup) { 536 if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) { 537 (void) elf_end(aup->au_mem->am_elf); 538 539 /* 540 * Null out all entries to this member so 541 * that we don't attempt to elf_end() it again. 542 */ 543 ld_ar_member(adp, arsym, aup, 0); 544 } 545 } 546 (void) elf_end(adp->ad_elf); 547 } 548 ofl->ofl_ars = NULL; 549 550 (void) elf_end(ofl->ofl_elf); 551 (void) elf_end(ofl->ofl_welf); 552 553 for (chp = ld_heap, php = NULL; chp; php = chp, chp = chp->lh_next) { 554 if (php) 555 (void) munmap((void *)php, 556 (size_t)php->lh_end - (size_t)php); 557 } 558 if (php) 559 (void) munmap((void *)php, (size_t)php->lh_end - (size_t)php); 560 561 ld_heap = NULL; 562 }