elf32.em revision 60525
1# $FreeBSD: head/contrib/binutils/ld/emultempl/elf32.em 60525 2000-05-13 17:51:22Z obrien $ 2# This shell script emits a C file. -*- C -*- 3# It does some substitutions. 4# This file is now misnamed, because it supports both 32 bit and 64 bit 5# ELF emulations. 6test -z "${ELFSIZE}" && ELFSIZE=32 7cat >e${EMULATION_NAME}.c <<EOF 8/* This file is is generated by a shell script. DO NOT EDIT! */ 9 10/* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME} 11 Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 1999 12 Free Software Foundation, Inc. 13 Written by Steve Chamberlain <sac@cygnus.com> 14 ELF support by Ian Lance Taylor <ian@cygnus.com> 15 16This file is part of GLD, the Gnu Linker. 17 18This program is free software; you can redistribute it and/or modify 19it under the terms of the GNU General Public License as published by 20the Free Software Foundation; either version 2 of the License, or 21(at your option) any later version. 22 23This program is distributed in the hope that it will be useful, 24but WITHOUT ANY WARRANTY; without even the implied warranty of 25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26GNU General Public License for more details. 27 28You should have received a copy of the GNU General Public License 29along with this program; if not, write to the Free Software 30Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 31 32#define TARGET_IS_${EMULATION_NAME} 33 34#include "bfd.h" 35#include "sysdep.h" 36 37#include <ctype.h> 38 39#include "bfdlink.h" 40 41#include "ld.h" 42#include "ldmain.h" 43#include "ldemul.h" 44#include "ldfile.h" 45#include "ldmisc.h" 46#include "ldexp.h" 47#include "ldlang.h" 48#include "ldgram.h" 49 50static void gld${EMULATION_NAME}_before_parse PARAMS ((void)); 51static boolean gld${EMULATION_NAME}_open_dynamic_archive 52 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *)); 53static void gld${EMULATION_NAME}_after_open PARAMS ((void)); 54static void gld${EMULATION_NAME}_check_needed 55 PARAMS ((lang_input_statement_type *)); 56static void gld${EMULATION_NAME}_stat_needed 57 PARAMS ((lang_input_statement_type *)); 58static boolean gld${EMULATION_NAME}_search_needed 59 PARAMS ((const char *, const char *, int)); 60static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int)); 61static void gld${EMULATION_NAME}_vercheck 62 PARAMS ((lang_input_statement_type *)); 63static void gld${EMULATION_NAME}_before_allocation PARAMS ((void)); 64static void gld${EMULATION_NAME}_find_statement_assignment 65 PARAMS ((lang_statement_union_type *)); 66static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *)); 67static boolean gld${EMULATION_NAME}_place_orphan 68 PARAMS ((lang_input_statement_type *, asection *)); 69static void gld${EMULATION_NAME}_place_section 70 PARAMS ((lang_statement_union_type *)); 71static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile)); 72 73static void 74gld${EMULATION_NAME}_before_parse() 75{ 76 ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`; 77 config.dynamic_link = ${DYNAMIC_LINK-true}; 78 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`; 79} 80 81/* Try to open a dynamic archive. This is where we know that ELF 82 dynamic libraries have an extension of .so (or .sl on oddball systems 83 like hpux). */ 84 85static boolean 86gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry) 87 const char *arch; 88 search_dirs_type *search; 89 lang_input_statement_type *entry; 90{ 91 const char *filename; 92 char *string; 93 94 if (! entry->is_archive) 95 return false; 96 97 filename = entry->filename; 98 99 /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION 100 is defined, but it does not seem worth the headache to optimize 101 away those two bytes of space. */ 102 string = (char *) xmalloc (strlen (search->name) 103 + strlen (filename) 104 + strlen (arch) 105#ifdef EXTRA_SHLIB_EXTENSION 106 + strlen (EXTRA_SHLIB_EXTENSION) 107#endif 108 + sizeof "/lib.so"); 109 110 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch); 111 112#ifdef EXTRA_SHLIB_EXTENSION 113 /* Try the .so extension first. If that fails build a new filename 114 using EXTRA_SHLIB_EXTENSION. */ 115 if (! ldfile_try_open_bfd (string, entry)) 116 sprintf (string, "%s/lib%s%s%s", search->name, 117 filename, arch, EXTRA_SHLIB_EXTENSION); 118#endif 119 120 if (! ldfile_try_open_bfd (string, entry)) 121 { 122 free (string); 123 return false; 124 } 125 126 entry->filename = string; 127 128 /* We have found a dynamic object to include in the link. The ELF 129 backend linker will create a DT_NEEDED entry in the .dynamic 130 section naming this file. If this file includes a DT_SONAME 131 entry, it will be used. Otherwise, the ELF linker will just use 132 the name of the file. For an archive found by searching, like 133 this one, the DT_NEEDED entry should consist of just the name of 134 the file, without the path information used to find it. Note 135 that we only need to do this if we have a dynamic object; an 136 archive will never be referenced by a DT_NEEDED entry. 137 138 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not 139 very pretty. I haven't been able to think of anything that is 140 pretty, though. */ 141 if (bfd_check_format (entry->the_bfd, bfd_object) 142 && (entry->the_bfd->flags & DYNAMIC) != 0) 143 { 144 char *needed_name; 145 146 ASSERT (entry->is_archive && entry->search_dirs_flag); 147 148 /* Rather than duplicating the logic above. Just use the 149 filename we recorded earlier. 150 151 First strip off everything before the last '/'. */ 152 filename = strrchr (entry->filename, '/'); 153 filename++; 154 155 needed_name = (char *) xmalloc (strlen (filename) + 1); 156 strcpy (needed_name, filename); 157 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name); 158 } 159 160 return true; 161} 162 163EOF 164if [ "x${host}" = "x${target}" ] ; then 165 case " ${EMULATION_LIBPATH} " in 166 *" ${EMULATION_NAME} "*) 167 case ${target} in 168 *-*-freebsd*) 169 cat >>e${EMULATION_NAME}.c <<EOF 170/* 171 * Read the system search path the FreeBSD way rather than like Linux. 172 */ 173#include <elf.h> 174 175static boolean gld${EMULATION_NAME}_check_ld_elf_hints 176 PARAMS ((const char *, int)); 177 178static boolean 179gld${EMULATION_NAME}_check_ld_elf_hints (name, force) 180 const char *name; 181 int force; 182{ 183 static boolean initialized; 184 static char *ld_elf_hints; 185 186 if (! initialized) 187 { 188 FILE *f; 189 190 f = fopen (_PATH_ELF_HINTS, FOPEN_RB); 191 if (f != NULL) 192 { 193 struct elfhints_hdr hdr; 194 195 if (fread(&hdr, 1, sizeof(hdr), f) == sizeof(hdr) && 196 hdr.magic == ELFHINTS_MAGIC && 197 hdr.version == 1) 198 { 199 if (fseek(f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1) 200 { 201 char *b; 202 203 b = (char *) xmalloc (hdr.dirlistlen + 1); 204 if (fread(b, 1, hdr.dirlistlen + 1, f) != 205 hdr.dirlistlen + 1) 206 { 207 free(b); 208 } 209 else 210 { 211 ld_elf_hints = b; 212 } 213 } 214 } 215 fclose (f); 216 } 217 218 initialized = true; 219 } 220 221 if (ld_elf_hints == NULL) 222 return false; 223 224 return gld${EMULATION_NAME}_search_needed (ld_elf_hints, name, force); 225} 226EOF 227 ;; 228 229 *linux*|*Linux*|*LINUX*) 230 cat >>e${EMULATION_NAME}.c <<EOF 231 232/* For a native linker, check the file /etc/ld.so.conf for directories 233 in which we may find shared libraries. /etc/ld.so.conf is really 234 only meaningful on Linux. */ 235 236static boolean gld${EMULATION_NAME}_check_ld_so_conf 237 PARAMS ((const char *, int)); 238 239static boolean 240gld${EMULATION_NAME}_check_ld_so_conf (name, force) 241 const char *name; 242 int force; 243{ 244 static boolean initialized; 245 static char *ld_so_conf; 246 247 if (! initialized) 248 { 249 FILE *f; 250 251 f = fopen ("/etc/ld.so.conf", FOPEN_RT); 252 if (f != NULL) 253 { 254 char *b; 255 size_t len, alloc; 256 int c; 257 258 len = 0; 259 alloc = 100; 260 b = (char *) xmalloc (alloc); 261 262 while ((c = getc (f)) != EOF) 263 { 264 if (len + 1 >= alloc) 265 { 266 alloc *= 2; 267 b = (char *) xrealloc (b, alloc); 268 } 269 if (c != ':' 270 && c != ' ' 271 && c != '\t' 272 && c != '\n' 273 && c != ',') 274 { 275 b[len] = c; 276 ++len; 277 } 278 else 279 { 280 if (len > 0 && b[len - 1] != ':') 281 { 282 b[len] = ':'; 283 ++len; 284 } 285 } 286 } 287 288 if (len > 0 && b[len - 1] == ':') 289 --len; 290 291 if (len > 0) 292 b[len] = '\0'; 293 else 294 { 295 free (b); 296 b = NULL; 297 } 298 299 fclose (f); 300 301 ld_so_conf = b; 302 } 303 304 initialized = true; 305 } 306 307 if (ld_so_conf == NULL) 308 return false; 309 310 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force); 311} 312 313EOF 314 ;; 315 esac 316 esac 317fi 318cat >>e${EMULATION_NAME}.c <<EOF 319 320/* These variables are required to pass information back and forth 321 between after_open and check_needed and stat_needed and vercheck. */ 322 323static struct bfd_link_needed_list *global_needed; 324static struct stat global_stat; 325static boolean global_found; 326static struct bfd_link_needed_list *global_vercheck_needed; 327static boolean global_vercheck_failed; 328 329/* This is called after all the input files have been opened. */ 330 331static void 332gld${EMULATION_NAME}_after_open () 333{ 334 struct bfd_link_needed_list *needed, *l; 335 336 /* We only need to worry about this when doing a final link. */ 337 if (link_info.relocateable || link_info.shared) 338 return; 339 340 /* Get the list of files which appear in DT_NEEDED entries in 341 dynamic objects included in the link (often there will be none). 342 For each such file, we want to track down the corresponding 343 library, and include the symbol table in the link. This is what 344 the runtime dynamic linker will do. Tracking the files down here 345 permits one dynamic object to include another without requiring 346 special action by the person doing the link. Note that the 347 needed list can actually grow while we are stepping through this 348 loop. */ 349 needed = bfd_elf_get_needed_list (output_bfd, &link_info); 350 for (l = needed; l != NULL; l = l->next) 351 { 352 struct bfd_link_needed_list *ll; 353 int force; 354 355 /* If we've already seen this file, skip it. */ 356 for (ll = needed; ll != l; ll = ll->next) 357 if (strcmp (ll->name, l->name) == 0) 358 break; 359 if (ll != l) 360 continue; 361 362 /* See if this file was included in the link explicitly. */ 363 global_needed = l; 364 global_found = false; 365 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed); 366 if (global_found) 367 continue; 368 369 /* We need to find this file and include the symbol table. We 370 want to search for the file in the same way that the dynamic 371 linker will search. That means that we want to use 372 rpath_link, rpath, then the environment variable 373 LD_LIBRARY_PATH (native only), then the linker script 374 LIB_SEARCH_DIRS. We do not search using the -L arguments. 375 376 We search twice. The first time, we skip objects which may 377 introduce version mismatches. The second time, we force 378 their use. See gld${EMULATION_NAME}_vercheck comment. */ 379 for (force = 0; force < 2; force++) 380 { 381 const char *lib_path; 382 size_t len; 383 search_dirs_type *search; 384 385 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link, 386 l->name, force)) 387 break; 388 if (gld${EMULATION_NAME}_search_needed (command_line.rpath, 389 l->name, force)) 390 break; 391 if (command_line.rpath_link == NULL 392 && command_line.rpath == NULL) 393 { 394 lib_path = (const char *) getenv ("LD_RUN_PATH"); 395 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, 396 force)) 397 break; 398 } 399EOF 400if [ "x${host}" = "x${target}" ] ; then 401 case " ${EMULATION_LIBPATH} " in 402 *" ${EMULATION_NAME} "*) 403cat >>e${EMULATION_NAME}.c <<EOF 404 lib_path = (const char *) getenv ("LD_LIBRARY_PATH"); 405 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force)) 406 break; 407EOF 408 ;; 409 esac 410fi 411cat >>e${EMULATION_NAME}.c <<EOF 412 len = strlen (l->name); 413 for (search = search_head; search != NULL; search = search->next) 414 { 415 char *filename; 416 417 if (search->cmdline) 418 continue; 419 filename = (char *) xmalloc (strlen (search->name) + len + 2); 420 sprintf (filename, "%s/%s", search->name, l->name); 421 if (gld${EMULATION_NAME}_try_needed (filename, force)) 422 break; 423 free (filename); 424 } 425 if (search != NULL) 426 break; 427EOF 428if [ "x${host}" = "x${target}" ] ; then 429 case " ${EMULATION_LIBPATH} " in 430 *" ${EMULATION_NAME} "*) 431 case ${target} in 432 *-*-freebsd*) 433 cat >>e${EMULATION_NAME}.c <<EOF 434 if (gld${EMULATION_NAME}_check_ld_elf_hints (l->name, force)) 435 break; 436EOF 437 ;; 438 *linux*|*Linux*|*LINUX*) 439 cat >>e${EMULATION_NAME}.c <<EOF 440 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force)) 441 break; 442EOF 443 ;; 444 esac 445 ;; 446 esac 447fi 448cat >>e${EMULATION_NAME}.c <<EOF 449 } 450 451 if (force < 2) 452 continue; 453 454 einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n", 455 l->name, l->by); 456 } 457} 458 459/* Search for a needed file in a path. */ 460 461static boolean 462gld${EMULATION_NAME}_search_needed (path, name, force) 463 const char *path; 464 const char *name; 465 int force; 466{ 467 const char *s; 468 size_t len; 469 470 if (path == NULL || *path == '\0') 471 return false; 472 len = strlen (name); 473 while (1) 474 { 475 char *filename, *sset; 476 477 s = strchr (path, ':'); 478 if (s == NULL) 479 s = path + strlen (path); 480 481 filename = (char *) xmalloc (s - path + len + 2); 482 if (s == path) 483 sset = filename; 484 else 485 { 486 memcpy (filename, path, s - path); 487 filename[s - path] = '/'; 488 sset = filename + (s - path) + 1; 489 } 490 strcpy (sset, name); 491 492 if (gld${EMULATION_NAME}_try_needed (filename, force)) 493 return true; 494 495 free (filename); 496 497 if (*s == '\0') 498 break; 499 path = s + 1; 500 } 501 502 return false; 503} 504 505/* This function is called for each possible name for a dynamic object 506 named by a DT_NEEDED entry. The FORCE parameter indicates whether 507 to skip the check for a conflicting version. */ 508 509static boolean 510gld${EMULATION_NAME}_try_needed (name, force) 511 const char *name; 512 int force; 513{ 514 bfd *abfd; 515 516 abfd = bfd_openr (name, bfd_get_target (output_bfd)); 517 if (abfd == NULL) 518 return false; 519 if (! bfd_check_format (abfd, bfd_object)) 520 { 521 (void) bfd_close (abfd); 522 return false; 523 } 524 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0) 525 { 526 (void) bfd_close (abfd); 527 return false; 528 } 529 530 /* Check whether this object would include any conflicting library 531 versions. If FORCE is set, then we skip this check; we use this 532 the second time around, if we couldn't find any compatible 533 instance of the shared library. */ 534 535 if (! force) 536 { 537 struct bfd_link_needed_list *needed; 538 539 if (! bfd_elf_get_bfd_needed_list (abfd, &needed)) 540 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd); 541 542 if (needed != NULL) 543 { 544 global_vercheck_needed = needed; 545 global_vercheck_failed = false; 546 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck); 547 if (global_vercheck_failed) 548 { 549 (void) bfd_close (abfd); 550 /* Return false to force the caller to move on to try 551 another file on the search path. */ 552 return false; 553 } 554 555 /* But wait! It gets much worse. On Linux, if a shared 556 library does not use libc at all, we are supposed to skip 557 it the first time around in case we encounter a shared 558 library later on with the same name which does use the 559 version of libc that we want. This is much too horrible 560 to use on any system other than Linux. */ 561 562EOF 563case ${target} in 564 *-*-linux-gnu*) 565 cat >>e${EMULATION_NAME}.c <<EOF 566 { 567 struct bfd_link_needed_list *l; 568 569 for (l = needed; l != NULL; l = l->next) 570 if (strncmp (l->name, "libc.so", 7) == 0) 571 break; 572 if (l == NULL) 573 { 574 (void) bfd_close (abfd); 575 return false; 576 } 577 } 578 579EOF 580 ;; 581esac 582cat >>e${EMULATION_NAME}.c <<EOF 583 } 584 } 585 586 /* We've found a dynamic object matching the DT_NEEDED entry. */ 587 588 /* We have already checked that there is no other input file of the 589 same name. We must now check again that we are not including the 590 same file twice. We need to do this because on many systems 591 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will 592 reference libc.so.1. If we have already included libc.so, we 593 don't want to include libc.so.1 if they are the same file, and we 594 can only check that using stat. */ 595 596 if (bfd_stat (abfd, &global_stat) != 0) 597 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd); 598 global_found = false; 599 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed); 600 if (global_found) 601 { 602 /* Return true to indicate that we found the file, even though 603 we aren't going to do anything with it. */ 604 return true; 605 } 606 607 /* Tell the ELF backend that don't want the output file to have a 608 DT_NEEDED entry for this file. */ 609 bfd_elf_set_dt_needed_name (abfd, ""); 610 611 /* Add this file into the symbol table. */ 612 if (! bfd_link_add_symbols (abfd, &link_info)) 613 einfo ("%F%B: could not read symbols: %E\n", abfd); 614 615 return true; 616} 617 618/* See if an input file matches a DT_NEEDED entry by name. */ 619 620static void 621gld${EMULATION_NAME}_check_needed (s) 622 lang_input_statement_type *s; 623{ 624 if (global_found) 625 return; 626 627 if (s->filename != NULL 628 && strcmp (s->filename, global_needed->name) == 0) 629 { 630 global_found = true; 631 return; 632 } 633 634 if (s->the_bfd != NULL) 635 { 636 const char *soname; 637 638 soname = bfd_elf_get_dt_soname (s->the_bfd); 639 if (soname != NULL 640 && strcmp (soname, global_needed->name) == 0) 641 { 642 global_found = true; 643 return; 644 } 645 } 646 647 if (s->search_dirs_flag 648 && s->filename != NULL 649 && strchr (global_needed->name, '/') == NULL) 650 { 651 const char *f; 652 653 f = strrchr (s->filename, '/'); 654 if (f != NULL 655 && strcmp (f + 1, global_needed->name) == 0) 656 { 657 global_found = true; 658 return; 659 } 660 } 661} 662 663/* See if an input file matches a DT_NEEDED entry by running stat on 664 the file. */ 665 666static void 667gld${EMULATION_NAME}_stat_needed (s) 668 lang_input_statement_type *s; 669{ 670 struct stat st; 671 const char *suffix; 672 const char *soname; 673 const char *f; 674 675 if (global_found) 676 return; 677 if (s->the_bfd == NULL) 678 return; 679 680 if (bfd_stat (s->the_bfd, &st) != 0) 681 { 682 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd); 683 return; 684 } 685 686 if (st.st_dev == global_stat.st_dev 687 && st.st_ino == global_stat.st_ino) 688 { 689 global_found = true; 690 return; 691 } 692 693 /* We issue a warning if it looks like we are including two 694 different versions of the same shared library. For example, 695 there may be a problem if -lc picks up libc.so.6 but some other 696 shared library has a DT_NEEDED entry of libc.so.5. This is a 697 hueristic test, and it will only work if the name looks like 698 NAME.so.VERSION. FIXME: Depending on file names is error-prone. 699 If we really want to issue warnings about mixing version numbers 700 of shared libraries, we need to find a better way. */ 701 702 if (strchr (global_needed->name, '/') != NULL) 703 return; 704 suffix = strstr (global_needed->name, ".so."); 705 if (suffix == NULL) 706 return; 707 suffix += sizeof ".so." - 1; 708 709 soname = bfd_elf_get_dt_soname (s->the_bfd); 710 if (soname == NULL) 711 soname = s->filename; 712 713 f = strrchr (soname, '/'); 714 if (f != NULL) 715 ++f; 716 else 717 f = soname; 718 719 if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0) 720 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n", 721 global_needed->name, global_needed->by, f); 722} 723 724/* On Linux, it's possible to have different versions of the same 725 shared library linked against different versions of libc. The 726 dynamic linker somehow tags which libc version to use in 727 /etc/ld.so.cache, and, based on the libc that it sees in the 728 executable, chooses which version of the shared library to use. 729 730 We try to do a similar check here by checking whether this shared 731 library needs any other shared libraries which may conflict with 732 libraries we have already included in the link. If it does, we 733 skip it, and try to find another shared library farther on down the 734 link path. 735 736 This is called via lang_for_each_input_file. 737 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object 738 which we ar checking. This sets GLOBAL_VERCHECK_FAILED if we find 739 a conflicting version. */ 740 741static void 742gld${EMULATION_NAME}_vercheck (s) 743 lang_input_statement_type *s; 744{ 745 const char *soname, *f; 746 struct bfd_link_needed_list *l; 747 748 if (global_vercheck_failed) 749 return; 750 if (s->the_bfd == NULL 751 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0) 752 return; 753 754 soname = bfd_elf_get_dt_soname (s->the_bfd); 755 if (soname == NULL) 756 soname = bfd_get_filename (s->the_bfd); 757 758 f = strrchr (soname, '/'); 759 if (f != NULL) 760 ++f; 761 else 762 f = soname; 763 764 for (l = global_vercheck_needed; l != NULL; l = l->next) 765 { 766 const char *suffix; 767 768 if (strcmp (f, l->name) == 0) 769 { 770 /* Probably can't happen, but it's an easy check. */ 771 continue; 772 } 773 774 if (strchr (l->name, '/') != NULL) 775 continue; 776 777 suffix = strstr (l->name, ".so."); 778 if (suffix == NULL) 779 continue; 780 781 suffix += sizeof ".so." - 1; 782 783 if (strncmp (f, l->name, suffix - l->name) == 0) 784 { 785 /* Here we know that S is a dynamic object FOO.SO.VER1, and 786 the object we are considering needs a dynamic object 787 FOO.SO.VER2, and VER1 and VER2 are different. This 788 appears to be a version mismatch, so we tell the caller 789 to try a different version of this library. */ 790 global_vercheck_failed = true; 791 return; 792 } 793 } 794} 795 796/* This is called after the sections have been attached to output 797 sections, but before any sizes or addresses have been set. */ 798 799static void 800gld${EMULATION_NAME}_before_allocation () 801{ 802 const char *rpath; 803 asection *sinterp; 804 805 /* If we are going to make any variable assignments, we need to let 806 the ELF backend know about them in case the variables are 807 referred to by dynamic objects. */ 808 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment); 809 810 /* Let the ELF backend work out the sizes of any sections required 811 by dynamic linking. */ 812 rpath = command_line.rpath; 813 if (rpath == NULL) 814 rpath = (const char *) getenv ("LD_RUN_PATH"); 815 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections 816 (output_bfd, command_line.soname, rpath, 817 command_line.export_dynamic, command_line.filter_shlib, 818 (const char * const *) command_line.auxiliary_filters, 819 &link_info, &sinterp, lang_elf_version_info))) 820 einfo ("%P%F: failed to set dynamic section sizes: %E\n"); 821 822 /* Let the user override the dynamic linker we are using. */ 823 if (command_line.interpreter != NULL 824 && sinterp != NULL) 825 { 826 sinterp->contents = (bfd_byte *) command_line.interpreter; 827 sinterp->_raw_size = strlen (command_line.interpreter) + 1; 828 } 829 830 /* Look for any sections named .gnu.warning. As a GNU extensions, 831 we treat such sections as containing warning messages. We print 832 out the warning message, and then zero out the section size so 833 that it does not get copied into the output file. */ 834 835 { 836 LANG_FOR_EACH_INPUT_STATEMENT (is) 837 { 838 asection *s; 839 bfd_size_type sz; 840 char *msg; 841 boolean ret; 842 843 if (is->just_syms_flag) 844 continue; 845 846 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning"); 847 if (s == NULL) 848 continue; 849 850 sz = bfd_section_size (is->the_bfd, s); 851 msg = xmalloc ((size_t) sz + 1); 852 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz)) 853 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n", 854 is->the_bfd); 855 msg[sz] = '\0'; 856 ret = link_info.callbacks->warning (&link_info, msg, 857 (const char *) NULL, 858 is->the_bfd, (asection *) NULL, 859 (bfd_vma) 0); 860 ASSERT (ret); 861 free (msg); 862 863 /* Clobber the section size, so that we don't waste copying the 864 warning into the output file. */ 865 s->_raw_size = 0; 866 } 867 } 868} 869 870/* This is called by the before_allocation routine via 871 lang_for_each_statement. It locates any assignment statements, and 872 tells the ELF backend about them, in case they are assignments to 873 symbols which are referred to by dynamic objects. */ 874 875static void 876gld${EMULATION_NAME}_find_statement_assignment (s) 877 lang_statement_union_type *s; 878{ 879 if (s->header.type == lang_assignment_statement_enum) 880 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp); 881} 882 883/* Look through an expression for an assignment statement. */ 884 885static void 886gld${EMULATION_NAME}_find_exp_assignment (exp) 887 etree_type *exp; 888{ 889 struct bfd_link_hash_entry *h; 890 891 switch (exp->type.node_class) 892 { 893 case etree_provide: 894 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst, 895 false, false, false); 896 if (h == NULL) 897 break; 898 899 /* We call record_link_assignment even if the symbol is defined. 900 This is because if it is defined by a dynamic object, we 901 actually want to use the value defined by the linker script, 902 not the value from the dynamic object (because we are setting 903 symbols like etext). If the symbol is defined by a regular 904 object, then, as it happens, calling record_link_assignment 905 will do no harm. */ 906 907 /* Fall through. */ 908 case etree_assign: 909 if (strcmp (exp->assign.dst, ".") != 0) 910 { 911 if (! (bfd_elf${ELFSIZE}_record_link_assignment 912 (output_bfd, &link_info, exp->assign.dst, 913 exp->type.node_class == etree_provide ? true : false))) 914 einfo ("%P%F: failed to record assignment to %s: %E\n", 915 exp->assign.dst); 916 } 917 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src); 918 break; 919 920 case etree_binary: 921 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs); 922 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs); 923 break; 924 925 case etree_trinary: 926 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond); 927 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs); 928 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs); 929 break; 930 931 case etree_unary: 932 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child); 933 break; 934 935 default: 936 break; 937 } 938} 939 940/* Place an orphan section. We use this to put random SHF_ALLOC 941 sections in the right segment. */ 942 943static asection *hold_section; 944static lang_output_section_statement_type *hold_use; 945static lang_output_section_statement_type *hold_text; 946static lang_output_section_statement_type *hold_rodata; 947static lang_output_section_statement_type *hold_data; 948static lang_output_section_statement_type *hold_bss; 949static lang_output_section_statement_type *hold_rel; 950static lang_output_section_statement_type *hold_interp; 951 952/*ARGSUSED*/ 953static boolean 954gld${EMULATION_NAME}_place_orphan (file, s) 955 lang_input_statement_type *file; 956 asection *s; 957{ 958 lang_output_section_statement_type *place; 959 asection *snew, **pps; 960 lang_statement_list_type *old; 961 lang_statement_list_type add; 962 etree_type *address; 963 const char *secname, *ps; 964 const char *outsecname; 965 lang_output_section_statement_type *os; 966 967 if ((s->flags & SEC_ALLOC) == 0) 968 return false; 969 970 /* Look through the script to see where to place this section. */ 971 hold_section = s; 972 hold_use = NULL; 973 lang_for_each_statement (gld${EMULATION_NAME}_place_section); 974 975 if (hold_use != NULL) 976 { 977 /* We have already placed a section with this name. */ 978 wild_doit (&hold_use->children, s, hold_use, file); 979 return true; 980 } 981 982 secname = bfd_get_section_name (s->owner, s); 983 984 /* If this is a final link, then always put .gnu.warning.SYMBOL 985 sections into the .text section to get them out of the way. */ 986 if (! link_info.shared 987 && ! link_info.relocateable 988 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0 989 && hold_text != NULL) 990 { 991 wild_doit (&hold_text->children, s, hold_text, file); 992 return true; 993 } 994 995 /* Decide which segment the section should go in based on the 996 section name and section flags. We put loadable .note sections 997 right after the .interp section, so that the PT_NOTE segment is 998 stored right after the program headers where the OS can read it 999 in the first page. */ 1000 place = NULL; 1001 if (s->flags & SEC_EXCLUDE) 1002 return false; 1003 else if ((s->flags & SEC_LOAD) != 0 1004 && strncmp (secname, ".note", 4) == 0 1005 && hold_interp != NULL) 1006 place = hold_interp; 1007 else if ((s->flags & SEC_HAS_CONTENTS) == 0 1008 && hold_bss != NULL) 1009 place = hold_bss; 1010 else if ((s->flags & SEC_READONLY) == 0 1011 && hold_data != NULL) 1012 place = hold_data; 1013 else if (strncmp (secname, ".rel", 4) == 0 1014 && hold_rel != NULL) 1015 place = hold_rel; 1016 else if ((s->flags & SEC_CODE) == 0 1017 && (s->flags & SEC_READONLY) != 0 1018 && hold_rodata != NULL) 1019 place = hold_rodata; 1020 else if ((s->flags & SEC_READONLY) != 0 1021 && hold_text != NULL) 1022 place = hold_text; 1023 if (place == NULL) 1024 return false; 1025 1026 /* Choose a unique name for the section. This will be needed if the 1027 same section name appears in the input file with different 1028 loadable or allocateable characteristics. */ 1029 outsecname = secname; 1030 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL) 1031 { 1032 unsigned int len; 1033 char *newname; 1034 unsigned int i; 1035 1036 len = strlen (outsecname); 1037 newname = xmalloc (len + 5); 1038 strcpy (newname, outsecname); 1039 i = 0; 1040 do 1041 { 1042 sprintf (newname + len, "%d", i); 1043 ++i; 1044 } 1045 while (bfd_get_section_by_name (output_bfd, newname) != NULL); 1046 1047 outsecname = newname; 1048 } 1049 1050 /* Create the section in the output file, and put it in the right 1051 place. This shuffling is to make the output file look neater. */ 1052 snew = bfd_make_section (output_bfd, outsecname); 1053 if (snew == NULL) 1054 einfo ("%P%F: output format %s cannot represent section called %s\n", 1055 output_bfd->xvec->name, outsecname); 1056 if (place->bfd_section != NULL) 1057 { 1058 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next) 1059 ; 1060 *pps = snew->next; 1061 snew->next = place->bfd_section->next; 1062 place->bfd_section->next = snew; 1063 } 1064 1065 /* Start building a list of statements for this section. */ 1066 old = stat_ptr; 1067 stat_ptr = &add; 1068 lang_list_init (stat_ptr); 1069 1070 /* If the name of the section is representable in C, then create 1071 symbols to mark the start and the end of the section. */ 1072 for (ps = outsecname; *ps != '\0'; ps++) 1073 if (! isalnum ((unsigned char) *ps) && *ps != '_') 1074 break; 1075 if (*ps == '\0' && config.build_constructors) 1076 { 1077 char *symname; 1078 1079 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_"); 1080 sprintf (symname, "__start_%s", outsecname); 1081 lang_add_assignment (exp_assop ('=', symname, 1082 exp_unop (ALIGN_K, 1083 exp_intop ((bfd_vma) 1 1084 << s->alignment_power)))); 1085 } 1086 1087 if (! link_info.relocateable) 1088 address = NULL; 1089 else 1090 address = exp_intop ((bfd_vma) 0); 1091 1092 lang_enter_output_section_statement (outsecname, address, 0, 1093 (bfd_vma) 0, 1094 (etree_type *) NULL, 1095 (etree_type *) NULL, 1096 (etree_type *) NULL); 1097 1098 os = lang_output_section_statement_lookup (outsecname); 1099 wild_doit (&os->children, s, os, file); 1100 1101 lang_leave_output_section_statement 1102 ((bfd_vma) 0, "*default*", (struct lang_output_section_phdr_list *) NULL, 1103 "*default*"); 1104 stat_ptr = &add; 1105 1106 if (*ps == '\0' && config.build_constructors) 1107 { 1108 char *symname; 1109 1110 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_"); 1111 sprintf (symname, "__stop_%s", outsecname); 1112 lang_add_assignment (exp_assop ('=', symname, 1113 exp_nameop (NAME, "."))); 1114 } 1115 1116 /* Now stick the new statement list right after PLACE. */ 1117 *add.tail = place->header.next; 1118 place->header.next = add.head; 1119 1120 stat_ptr = old; 1121 1122 return true; 1123} 1124 1125static void 1126gld${EMULATION_NAME}_place_section (s) 1127 lang_statement_union_type *s; 1128{ 1129 lang_output_section_statement_type *os; 1130 1131 if (s->header.type != lang_output_section_statement_enum) 1132 return; 1133 1134 os = &s->output_section_statement; 1135 1136 if (strcmp (os->name, hold_section->name) == 0 1137 && os->bfd_section != NULL 1138 && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC)) 1139 == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC)))) 1140 hold_use = os; 1141 1142 if (strcmp (os->name, ".text") == 0) 1143 hold_text = os; 1144 else if (strcmp (os->name, ".rodata") == 0) 1145 hold_rodata = os; 1146 else if (strcmp (os->name, ".data") == 0) 1147 hold_data = os; 1148 else if (strcmp (os->name, ".bss") == 0) 1149 hold_bss = os; 1150 else if (hold_rel == NULL 1151 && os->bfd_section != NULL 1152 && (os->bfd_section->flags & SEC_ALLOC) != 0 1153 && strncmp (os->name, ".rel", 4) == 0) 1154 hold_rel = os; 1155 else if (strcmp (os->name, ".interp") == 0) 1156 hold_interp = os; 1157} 1158 1159static char * 1160gld${EMULATION_NAME}_get_script(isfile) 1161 int *isfile; 1162EOF 1163 1164if test -n "$COMPILE_IN" 1165then 1166# Scripts compiled in. 1167 1168# sed commands to quote an ld script as a C string. 1169sc="-f stringify.sed" 1170 1171cat >>e${EMULATION_NAME}.c <<EOF 1172{ 1173 *isfile = 0; 1174 1175 if (link_info.relocateable == true && config.build_constructors == true) 1176 return 1177EOF 1178sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c 1179echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c 1180sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c 1181echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c 1182sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c 1183echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c 1184sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c 1185 1186if test -n "$GENERATE_SHLIB_SCRIPT" ; then 1187echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c 1188sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c 1189fi 1190 1191echo ' ; else return' >> e${EMULATION_NAME}.c 1192sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c 1193echo '; }' >> e${EMULATION_NAME}.c 1194 1195else 1196# Scripts read from the filesystem. 1197 1198cat >>e${EMULATION_NAME}.c <<EOF 1199{ 1200 *isfile = 1; 1201 1202 if (link_info.relocateable == true && config.build_constructors == true) 1203 return "ldscripts/${EMULATION_NAME}.xu"; 1204 else if (link_info.relocateable == true) 1205 return "ldscripts/${EMULATION_NAME}.xr"; 1206 else if (!config.text_read_only) 1207 return "ldscripts/${EMULATION_NAME}.xbn"; 1208 else if (!config.magic_demand_paged) 1209 return "ldscripts/${EMULATION_NAME}.xn"; 1210 else if (link_info.shared) 1211 return "ldscripts/${EMULATION_NAME}.xs"; 1212 else 1213 return "ldscripts/${EMULATION_NAME}.x"; 1214} 1215EOF 1216 1217fi 1218 1219if test -n "$PARSE_AND_LIST_ARGS" ; then 1220cat >>e${EMULATION_NAME}.c <<EOF 1221static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **)); 1222static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE * file)); 1223 1224 $PARSE_AND_LIST_ARGS 1225EOF 1226else 1227 1228cat >>e${EMULATION_NAME}.c <<EOF 1229#define gld_${EMULATION_NAME}_parse_args NULL 1230#define gld_${EMULATION_NAME}_list_options NULL 1231EOF 1232 1233fi 1234 1235cat >>e${EMULATION_NAME}.c <<EOF 1236 1237struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 1238{ 1239 gld${EMULATION_NAME}_before_parse, 1240 syslib_default, 1241 hll_default, 1242 after_parse_default, 1243 gld${EMULATION_NAME}_after_open, 1244 after_allocation_default, 1245 set_output_arch_default, 1246 ldemul_default_target, 1247 gld${EMULATION_NAME}_before_allocation, 1248 gld${EMULATION_NAME}_get_script, 1249 "${EMULATION_NAME}", 1250 "${OUTPUT_FORMAT}", 1251 NULL, /* finish */ 1252 NULL, /* create output section statements */ 1253 gld${EMULATION_NAME}_open_dynamic_archive, 1254 gld${EMULATION_NAME}_place_orphan, 1255 NULL, /* set_symbols */ 1256 gld_${EMULATION_NAME}_parse_args, 1257 NULL, /* unrecognized_file */ 1258 gld_${EMULATION_NAME}_list_options, 1259 NULL, /* recognized_file */ 1260 NULL /* find_potential_libraries */ 1261}; 1262EOF 1263