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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 1988 AT&T 29 * All Rights Reserved 30 */ 31 32 /* 33 * Utility functions 34 */ 35 #include <unistd.h> 36 #include <stdio.h> 37 #include <stdarg.h> 38 #include <string.h> 39 #include <fcntl.h> 40 #include <sys/types.h> 41 #include <sys/mman.h> 42 #include <errno.h> 43 #include <sgs.h> 44 #include <libintl.h> 45 #include <debug.h> 46 #include "msg.h" 47 #include "_libld.h" 48 49 /* 50 * libld_malloc() and dz_map() are used for both performance and for ease of 51 * programming: 52 * 53 * Performance: 54 * The link-edit is a short lived process which doesn't really free much 55 * of the dynamic memory that it requests. Because of this, it is more 56 * important to optimize for quick memory allocations than the 57 * re-usability of the memory. 58 * 59 * By also mmaping blocks of pages in from /dev/zero we don't need to 60 * waste the overhead of zeroing out these pages for calloc() requests. 61 * 62 * Memory Management: 63 * By doing all libld memory management through the ld_malloc routine 64 * it's much easier to free up all memory at the end by simply unmaping 65 * all of the blocks that were mapped in through dz_map(). This is much 66 * simpler then trying to track all of the libld structures that were 67 * dynamically allocate and are actually pointers into the ELF files. 68 * 69 * It's important that we can free up all of our dynamic memory because 70 * libld is used by ld.so.1 when it performs dlopen()'s of relocatable 71 * objects. 72 * 73 * Format: 74 * The memory blocks for each allocation store the size of the allocation 75 * in the first 8 bytes of the block. The pointer that is returned by 76 * libld_malloc() is actually the address of (block + 8): 77 * 78 * (addr - 8) block_size 79 * (addr) <allocated block> 80 * 81 * The size is retained in order to implement realloc(), and to perform 82 * the required memcpy(). 8 bytes are uses, as the memory area returned 83 * by libld_malloc() must be 8 byte-aligned. Even in a 32-bit environment, 84 * u_longlog_t pointers are employed. 85 * 86 * Map anonymous memory via MAP_ANON (added in Solaris 8). 87 */ 88 static void * 89 dz_map(size_t size) 90 { 91 void *addr; 92 93 if ((addr = mmap(0, size, (PROT_READ | PROT_WRITE | PROT_EXEC), 94 (MAP_PRIVATE | MAP_ANON), -1, 0)) == MAP_FAILED) { 95 int err = errno; 96 eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON), 97 strerror(err)); 98 return (MAP_FAILED); 99 } 100 return (addr); 101 } 102 103 void * 104 libld_malloc(size_t size) 105 { 106 Ld_heap *chp = ld_heap; 107 void *vptr; 108 size_t asize = size + HEAPALIGN; 109 110 /* 111 * If this is the first allocation, or the allocation request is greater 112 * than the current free space available, allocate a new heap. 113 */ 114 if ((chp == 0) || 115 (((size_t)chp->lh_end - (size_t)chp->lh_free) <= asize)) { 116 Ld_heap *nhp; 117 size_t hsize = (size_t)S_ROUND(sizeof (Ld_heap), HEAPALIGN); 118 size_t tsize = (size_t)S_ROUND((asize + hsize), HEAPALIGN); 119 120 /* 121 * Allocate a block that is at minimum 'HEAPBLOCK' size 122 */ 123 if (tsize < HEAPBLOCK) 124 tsize = HEAPBLOCK; 125 126 if ((nhp = dz_map(tsize)) == MAP_FAILED) 127 return (NULL); 128 129 nhp->lh_next = chp; 130 nhp->lh_free = (void *)((size_t)nhp + hsize); 131 nhp->lh_end = (void *)((size_t)nhp + tsize); 132 133 ld_heap = chp = nhp; 134 } 135 vptr = chp->lh_free; 136 137 /* 138 * Assign size to head of allocated block (used by realloc), and 139 * memory arena as then next 8-byte aligned offset. 140 */ 141 *((size_t *)vptr) = size; 142 vptr = (void *)((size_t)vptr + HEAPALIGN); 143 144 /* 145 * Increment free to point to next available block 146 */ 147 chp->lh_free = (void *)S_ROUND((size_t)chp->lh_free + asize, 148 HEAPALIGN); 149 150 return (vptr); 151 } 152 153 void * 154 libld_realloc(void *ptr, size_t size) 155 { 156 size_t psize; 157 void *vptr; 158 159 if (ptr == NULL) 160 return (libld_malloc(size)); 161 162 /* 163 * Size of the allocated blocks is stored *just* before the blocks 164 * address. 165 */ 166 psize = *((size_t *)((size_t)ptr - HEAPALIGN)); 167 168 /* 169 * If the block actually fits then just return. 170 */ 171 if (size <= psize) 172 return (ptr); 173 174 if ((vptr = libld_malloc(size)) != 0) 175 (void) memcpy(vptr, ptr, psize); 176 177 return (vptr); 178 } 179 180 void 181 /* ARGSUSED 0 */ 182 libld_free(void *ptr) 183 { 184 } 185 186 /* 187 * Append an item to the specified list, and return a pointer to the list 188 * node created. 189 */ 190 Listnode * 191 list_appendc(List *lst, const void *item) 192 { 193 Listnode *_lnp; 194 195 if ((_lnp = libld_malloc(sizeof (Listnode))) == NULL) 196 return (NULL); 197 198 _lnp->data = (void *)item; 199 _lnp->next = NULL; 200 201 if (lst->head == NULL) 202 lst->tail = lst->head = _lnp; 203 else { 204 lst->tail->next = _lnp; 205 lst->tail = lst->tail->next; 206 } 207 return (_lnp); 208 } 209 210 /* 211 * Add an item after the specified listnode, and return a pointer to the list 212 * node created. 213 */ 214 Listnode * 215 list_insertc(List *lst, const void *item, Listnode *lnp) 216 { 217 Listnode *_lnp; 218 219 if ((_lnp = libld_malloc(sizeof (Listnode))) == NULL) 220 return (NULL); 221 222 _lnp->data = (void *)item; 223 _lnp->next = lnp->next; 224 if (_lnp->next == NULL) 225 lst->tail = _lnp; 226 lnp->next = _lnp; 227 return (_lnp); 228 } 229 230 /* 231 * Prepend an item to the specified list, and return a pointer to the 232 * list node created. 233 */ 234 Listnode * 235 list_prependc(List *lst, const void *item) 236 { 237 Listnode *_lnp; 238 239 if ((_lnp = libld_malloc(sizeof (Listnode))) == NULL) 240 return (NULL); 241 242 _lnp->data = (void *)item; 243 244 if (lst->head == NULL) { 245 _lnp->next = NULL; 246 lst->tail = lst->head = _lnp; 247 } else { 248 _lnp->next = lst->head; 249 lst->head = _lnp; 250 } 251 return (_lnp); 252 } 253 254 /* 255 * Find out where to insert the node for reordering. List of insect structures 256 * is traversed and the is_txtndx field of the insect structure is examined 257 * and that determines where the new input section should be inserted. 258 * All input sections which have a non zero is_txtndx value will be placed 259 * in ascending order before sections with zero is_txtndx value. This 260 * implies that any section that does not appear in the map file will be 261 * placed at the end of this list as it will have a is_txtndx value of 0. 262 * Returns: NULL if the input section should be inserted at beginning 263 * of list else A pointer to the entry AFTER which this new section should 264 * be inserted. 265 */ 266 Listnode * 267 list_where(List *lst, Word num) 268 { 269 Listnode *ln, *pln; /* Temp list node ptr */ 270 Is_desc *isp; /* Temp Insect structure */ 271 Word n; 272 273 /* 274 * No input sections exist, so add at beginning of list 275 */ 276 if (lst->head == NULL) 277 return (NULL); 278 279 for (ln = lst->head, pln = ln; ln != NULL; pln = ln, ln = ln->next) { 280 isp = (Is_desc *)ln->data; 281 /* 282 * This should never happen, but if it should we 283 * try to do the right thing. Insert at the 284 * beginning of list if no other items exist, else 285 * end of already existing list, prior to this null 286 * item. 287 */ 288 if (isp == NULL) { 289 if (ln == pln) { 290 return (NULL); 291 } else { 292 return (pln); 293 } 294 } 295 /* 296 * We have reached end of reorderable items. All 297 * following items have is_txtndx values of zero 298 * So insert at end of reorderable items. 299 */ 300 if ((n = isp->is_txtndx) > num || n == 0) { 301 if (ln == pln) { 302 return (NULL); 303 } else { 304 return (pln); 305 } 306 } 307 /* 308 * We have reached end of list, so insert 309 * at the end of this list. 310 */ 311 if ((n != 0) && (ln->next == NULL)) 312 return (ln); 313 } 314 return (NULL); 315 } 316 317 /* 318 * Determine if a shared object definition structure already exists and if 319 * not create one. These definitions provide for recording information 320 * regarding shared objects that are still to be processed. Once processed 321 * shared objects are maintained on the ofl_sos list. The information 322 * recorded in this structure includes: 323 * 324 * o DT_USED requirements. In these cases definitions are added during 325 * mapfile processing of `-' entries (see map_dash()). 326 * 327 * o implicit NEEDED entries. As shared objects are processed from the 328 * command line so any of their dependencies are recorded in these 329 * structures for later processing (see process_dynamic()). 330 * 331 * o version requirements. Any explicit shared objects that have version 332 * dependencies on other objects have their version requirements recorded. 333 * In these cases definitions are added during mapfile processing of `-' 334 * entries (see map_dash()). Also, shared objects may have versioning 335 * requirements on their NEEDED entries. These cases are added during 336 * their version processing (see vers_need_process()). 337 * 338 * Note: Both process_dynamic() and vers_need_process() may generate the 339 * initial version definition structure because you can't rely on what 340 * section (.dynamic or .SUNW_version) may be processed first from any 341 * input file. 342 */ 343 Sdf_desc * 344 sdf_find(const char *name, List *lst) 345 { 346 Listnode *lnp; 347 Sdf_desc *sdf; 348 349 for (LIST_TRAVERSE(lst, lnp, sdf)) 350 if (strcmp(name, sdf->sdf_name) == 0) 351 return (sdf); 352 353 return (NULL); 354 } 355 356 Sdf_desc * 357 sdf_add(const char *name, List *lst) 358 { 359 Sdf_desc *sdf; 360 361 if (!(sdf = libld_calloc(sizeof (Sdf_desc), 1))) 362 return ((Sdf_desc *)S_ERROR); 363 364 sdf->sdf_name = name; 365 366 if (list_appendc(lst, sdf) == 0) 367 return ((Sdf_desc *)S_ERROR); 368 else 369 return (sdf); 370 } 371 372 /* 373 * Add a string, separated by a colon, to an existing string. Typically used 374 * to maintain filter, rpath and audit names, of which there is normally only 375 * one string supplied anyway. 376 */ 377 char * 378 add_string(char *old, char *str) 379 { 380 char *new; 381 382 if (old) { 383 char *_str; 384 size_t len; 385 386 /* 387 * If an original string exists, make sure this new string 388 * doesn't get duplicated. 389 */ 390 if ((_str = strstr(old, str)) != NULL) { 391 if (((_str == old) || 392 (*(_str - 1) == *(MSG_ORIG(MSG_STR_COLON)))) && 393 (_str += strlen(str)) && 394 ((*_str == '\0') || 395 (*_str == *(MSG_ORIG(MSG_STR_COLON))))) 396 return (old); 397 } 398 399 len = strlen(old) + strlen(str) + 2; 400 if ((new = libld_calloc(1, len)) == NULL) 401 return ((char *)S_ERROR); 402 (void) snprintf(new, len, MSG_ORIG(MSG_FMT_COLPATH), old, str); 403 } else { 404 if ((new = libld_malloc(strlen(str) + 1)) == NULL) 405 return ((char *)S_ERROR); 406 (void) strcpy(new, str); 407 } 408 409 return (new); 410 } 411 412 /* 413 * Determine whether this string, possibly with an associated option, should be 414 * translated to an option character. If so, update the optind and optarg 415 * as described for short options in getopt(3c). 416 */ 417 static int 418 str2chr(Lm_list *lml, int ndx, int argc, char **argv, char *arg, int c, 419 const char *opt, size_t optsz) 420 { 421 if (optsz == 0) { 422 /* 423 * Compare a single option (ie. there's no associated option 424 * argument). 425 */ 426 if (strcmp(arg, opt) == 0) { 427 DBG_CALL(Dbg_args_str2chr(lml, ndx, opt, c)); 428 optind += 1; 429 return (c); 430 } 431 432 } else if (strncmp(arg, opt, optsz) == 0) { 433 /* 434 * Otherwise, compare the option name, which may be 435 * concatenated with the option argument. 436 */ 437 DBG_CALL(Dbg_args_str2chr(lml, ndx, opt, c)); 438 439 if (arg[optsz] == '\0') { 440 /* 441 * Optarg is the next argument (white space separated). 442 * Make sure an optarg is available, and if not return 443 * a failure to prevent any fall-through to the generic 444 * getopt() processing. 445 */ 446 if ((++optind + 1) > argc) { 447 return ('?'); 448 } 449 optarg = argv[optind]; 450 optind++; 451 } else { 452 /* 453 * Optarg concatenated to option (no white space). 454 * GNU option/option argument pairs can be represented 455 * with a "=" separator. If this is the case, remove 456 * the separator. 457 */ 458 optarg = &arg[optsz]; 459 optind++; 460 if (*optarg == '=') { 461 if (*(++optarg) == '\0') 462 return ('?'); 463 } 464 } 465 return (c); 466 } 467 return (0); 468 } 469 470 /* 471 * Parse an individual option. The intent of this function is to determine if 472 * any known, non-Solaris options have been passed to ld(1). This condition 473 * can occur as a result of build configuration tools, because of users 474 * familiarity with other systems, or simply the users preferences. If a known 475 * non-Solaris option can be determined, translate that option into the Solaris 476 * counterpart. 477 * 478 * This function will probably never be a complete solution, as new, non-Solaris 479 * options are discovered, their translation will have to be added. Other 480 * non-Solaris options are incompatible with the Solaris link-editor, and will 481 * never be recognized. We support what we can. 482 */ 483 int 484 ld_getopt(Lm_list *lml, int ndx, int argc, char **argv) 485 { 486 int c; 487 488 if ((optind < argc) && argv[optind] && (argv[optind][0] == '-')) { 489 char *arg = &argv[optind][1]; 490 491 switch (*arg) { 492 case 'r': 493 /* Translate -rpath <optarg> to -R <optarg> */ 494 if ((c = str2chr(lml, ndx, argc, argv, arg, 'R', 495 MSG_ORIG(MSG_ARG_T_RPATH), 496 MSG_ARG_T_RPATH_SIZE)) != 0) { 497 return (c); 498 } 499 break; 500 case 's': 501 /* Translate -shared to -G */ 502 if ((c = str2chr(lml, ndx, argc, argv, arg, 'G', 503 MSG_ORIG(MSG_ARG_T_SHARED), 0)) != 0) { 504 return (c); 505 506 /* Translate -soname <optarg> to -h <optarg> */ 507 } else if ((c = str2chr(lml, ndx, argc, argv, arg, 'h', 508 MSG_ORIG(MSG_ARG_T_SONAME), 509 MSG_ARG_T_SONAME_SIZE)) != 0) { 510 return (c); 511 } 512 break; 513 case '-': 514 switch (*(arg + 1)) { 515 case 'a': 516 /* 517 * Translate --allow-multiple-definition to 518 * -zmuldefs 519 */ 520 if ((c = str2chr(lml, ndx, argc, argv, arg, 'z', 521 MSG_ORIG(MSG_ARG_T_MULDEFS), 0)) != 0) { 522 optarg = 523 (char *)MSG_ORIG(MSG_ARG_MULDEFS); 524 return (c); 525 526 /* 527 * Translate --auxiliary <optarg> to 528 * -f <optarg> 529 */ 530 } else if ((c = str2chr(lml, argc, ndx, argv, 531 arg, 'f', MSG_ORIG(MSG_ARG_T_AUXFLTR), 532 MSG_ARG_T_AUXFLTR_SIZE)) != 0) { 533 return (c); 534 } 535 break; 536 case 'd': 537 /* 538 * Translate --dynamic-linker <optarg> to 539 * -I <optarg> 540 */ 541 if ((c = str2chr(lml, ndx, argc, argv, arg, 'I', 542 MSG_ORIG(MSG_ARG_T_INTERP), 543 MSG_ARG_T_INTERP_SIZE)) != 0) { 544 return (c); 545 } 546 break; 547 case 'e': 548 /* Translate --entry <optarg> to -e <optarg> */ 549 if ((c = str2chr(lml, ndx, argc, argv, arg, 'e', 550 MSG_ORIG(MSG_ARG_T_ENTRY), 551 MSG_ARG_T_ENTRY_SIZE)) != 0) { 552 return (c); 553 } 554 break; 555 case 'f': 556 /* Translate --filter <optarg> to -F <optarg> */ 557 if ((c = str2chr(lml, ndx, argc, argv, arg, 'F', 558 MSG_ORIG(MSG_ARG_T_STDFLTR), 559 MSG_ARG_T_STDFLTR_SIZE)) != 0) { 560 return (c); 561 } 562 break; 563 case 'h': 564 /* Translate --help to -zhelp */ 565 if ((c = str2chr(lml, ndx, argc, argv, arg, 'z', 566 MSG_ORIG(MSG_ARG_T_HELP), 0)) != 0) { 567 optarg = (char *)MSG_ORIG(MSG_ARG_HELP); 568 return (c); 569 } 570 break; 571 case 'l': 572 /* 573 * Translate --library <optarg> to -l <optarg> 574 */ 575 if ((c = str2chr(lml, ndx, argc, argv, arg, 'l', 576 MSG_ORIG(MSG_ARG_T_LIBRARY), 577 MSG_ARG_T_LIBRARY_SIZE)) != 0) { 578 return (c); 579 580 /* 581 * Translate --library-path <optarg> to 582 * -L <optarg> 583 */ 584 } else if ((c = str2chr(lml, ndx, argc, argv, 585 arg, 'L', MSG_ORIG(MSG_ARG_T_LIBPATH), 586 MSG_ARG_T_LIBPATH_SIZE)) != 0) { 587 return (c); 588 } 589 break; 590 case 'n': 591 /* Translate --no-undefined to -zdefs */ 592 if ((c = str2chr(lml, ndx, argc, argv, arg, 'z', 593 MSG_ORIG(MSG_ARG_T_NOUNDEF), 0)) != 0) { 594 optarg = (char *)MSG_ORIG(MSG_ARG_DEFS); 595 return (c); 596 597 /* 598 * Translate --no-whole-archive to 599 * -z defaultextract 600 */ 601 } else if ((c = str2chr(lml, ndx, argc, argv, 602 arg, 'z', 603 MSG_ORIG(MSG_ARG_T_NOWHOLEARC), 0)) != 0) { 604 optarg = 605 (char *)MSG_ORIG(MSG_ARG_DFLEXTRT); 606 return (c); 607 } 608 break; 609 case 'o': 610 /* Translate --output <optarg> to -o <optarg> */ 611 if ((c = str2chr(lml, ndx, argc, argv, arg, 'o', 612 MSG_ORIG(MSG_ARG_T_OUTPUT), 613 MSG_ARG_T_OUTPUT_SIZE)) != 0) { 614 return (c); 615 } 616 break; 617 case 'r': 618 /* Translate --relocatable to -r */ 619 if ((c = str2chr(lml, ndx, argc, argv, arg, 'r', 620 MSG_ORIG(MSG_ARG_T_RELOCATABLE), 0)) != 0) { 621 return (c); 622 } 623 break; 624 case 's': 625 /* Translate --strip-all to -s */ 626 if ((c = str2chr(lml, ndx, argc, argv, arg, 's', 627 MSG_ORIG(MSG_ARG_T_STRIP), 0)) != 0) { 628 return (c); 629 } 630 break; 631 case 'u': 632 /* 633 * Translate --undefined <optarg> to 634 * -u <optarg> 635 */ 636 if ((c = str2chr(lml, ndx, argc, argv, arg, 'u', 637 MSG_ORIG(MSG_ARG_T_UNDEF), 638 MSG_ARG_T_UNDEF_SIZE)) != 0) { 639 return (c); 640 } 641 break; 642 case 'v': 643 /* Translate --version to -V */ 644 if ((c = str2chr(lml, ndx, argc, argv, arg, 'V', 645 MSG_ORIG(MSG_ARG_T_VERSION), 0)) != 0) { 646 return (c); 647 648 /* 649 * Translate --version-script <optarg> to 650 * -M <optarg> 651 */ 652 } else if ((c = str2chr(lml, ndx, argc, argv, 653 arg, 'M', MSG_ORIG(MSG_ARG_T_VERSCRIPT), 654 MSG_ARG_T_VERSCRIPT_SIZE)) != 0) { 655 return (c); 656 } 657 break; 658 case 'w': 659 /* 660 * Translate --whole-archive to -z alltextract 661 */ 662 if ((c = str2chr(lml, ndx, argc, argv, 663 arg, 'z', 664 MSG_ORIG(MSG_ARG_T_WHOLEARC), 0)) != 0) { 665 optarg = 666 (char *)MSG_ORIG(MSG_ARG_ALLEXTRT); 667 return (c); 668 } 669 break; 670 } 671 break; 672 } 673 } 674 if ((c = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != -1) { 675 /* 676 * It is possible that a "-Wl," argument has been used to 677 * specify an option. This isn't advertized ld(1) syntax, but 678 * compiler drivers and configuration tools, have been known to 679 * pass this compiler option to ld(1). Strip off the "-Wl," 680 * prefix and pass the option through. 681 */ 682 if ((c == 'W') && (strncmp(optarg, 683 MSG_ORIG(MSG_ARG_T_WL), MSG_ARG_T_WL_SIZE) == 0)) { 684 DBG_CALL(Dbg_args_Wldel(lml, ndx, optarg)); 685 c = optarg[MSG_ARG_T_WL_SIZE]; 686 optarg += MSG_ARG_T_WL_SIZE + 1; 687 } 688 } 689 690 return (c); 691 } 692 693 /* 694 * Messaging support - funnel everything through dgettext(). 695 */ 696 697 const char * 698 _libld_msg(Msg mid) 699 { 700 return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid))); 701 } 702 703 /* 704 * Determine whether a symbol name should be demangled. 705 */ 706 const char * 707 demangle(const char *name) 708 { 709 if (demangle_flag) 710 return (Elf_demangle_name(name)); 711 else 712 return (name); 713 } 714