Subversion Repositories Kolibri OS

Rev

Rev 5211 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5211 Rev 6324
Line 1... Line 1...
1
/* Main program of GNU linker.
1
/* Main program of GNU linker.
2
   Copyright 1991-2013 Free Software Foundation, Inc.
2
   Copyright (C) 1991-2015 Free Software Foundation, Inc.
3
   Written by Steve Chamberlain steve@cygnus.com
3
   Written by Steve Chamberlain steve@cygnus.com
Line 4... Line 4...
4
 
4
 
Line 5... Line 5...
5
   This file is part of the GNU Binutils.
5
   This file is part of the GNU Binutils.
Line 39... Line 39...
39
#include "ldemul.h"
39
#include "ldemul.h"
40
#include "ldctor.h"
40
#include "ldctor.h"
41
#ifdef ENABLE_PLUGINS
41
#ifdef ENABLE_PLUGINS
42
#include "plugin.h"
42
#include "plugin.h"
43
#include "plugin-api.h"
43
#include "plugin-api.h"
44
#include "libbfd.h"
-
 
45
#endif /* ENABLE_PLUGINS */
44
#endif /* ENABLE_PLUGINS */
Line 46... Line 45...
46
 
45
 
47
/* Somewhere above, sys/stat.h got included.  */
46
/* Somewhere above, sys/stat.h got included.  */
48
#if !defined(S_ISDIR) && defined(S_IFDIR)
47
#if !defined(S_ISDIR) && defined(S_IFDIR)
Line 135... Line 134...
135
  (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
134
  (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
136
static bfd_boolean unattached_reloc
135
static bfd_boolean unattached_reloc
137
  (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
136
  (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
138
static bfd_boolean notice
137
static bfd_boolean notice
139
  (struct bfd_link_info *, struct bfd_link_hash_entry *,
138
  (struct bfd_link_info *, struct bfd_link_hash_entry *,
140
   bfd *, asection *, bfd_vma, flagword, const char *);
139
   struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
Line 141... Line 140...
141
 
140
 
142
static struct bfd_link_callbacks link_callbacks =
141
static struct bfd_link_callbacks link_callbacks =
143
{
142
{
144
  add_archive_element,
143
  add_archive_element,
Line 221... Line 220...
221
  xatexit (ld_cleanup);
220
  xatexit (ld_cleanup);
Line 222... Line 221...
222
 
221
 
223
  /* Set up the sysroot directory.  */
222
  /* Set up the sysroot directory.  */
224
  ld_sysroot = get_sysroot (argc, argv);
223
  ld_sysroot = get_sysroot (argc, argv);
225
  if (*ld_sysroot)
-
 
226
    {
-
 
227
      if (*TARGET_SYSTEM_ROOT == 0)
-
 
228
	{
-
 
229
	  einfo ("%P%F: this linker was not configured to use sysroots\n");
-
 
230
	  ld_sysroot = "";
-
 
231
	}
-
 
232
      else
224
  if (*ld_sysroot)
233
	ld_canon_sysroot = lrealpath (ld_sysroot);
-
 
234
    }
225
	ld_canon_sysroot = lrealpath (ld_sysroot);
235
  if (ld_canon_sysroot)
226
  if (ld_canon_sysroot)
236
    ld_canon_sysroot_len = strlen (ld_canon_sysroot);
227
    ld_canon_sysroot_len = strlen (ld_canon_sysroot);
237
  else
228
  else
Line 284... Line 275...
284
  /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
275
  /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
285
     and _fini symbols.  We are compatible.  */
276
     and _fini symbols.  We are compatible.  */
286
  link_info.init_function = "_init";
277
  link_info.init_function = "_init";
287
  link_info.fini_function = "_fini";
278
  link_info.fini_function = "_fini";
288
  link_info.relax_pass = 1;
279
  link_info.relax_pass = 1;
-
 
280
  link_info.extern_protected_data = -1;
289
  link_info.pei386_auto_import = -1;
281
  link_info.pei386_auto_import = -1;
290
  link_info.spare_dynamic_tags = 5;
282
  link_info.spare_dynamic_tags = 5;
291
  link_info.path_separator = ':';
283
  link_info.path_separator = ':';
-
 
284
#ifdef DEFAULT_FLAG_COMPRESS_DEBUG
-
 
285
  link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
-
 
286
#endif
Line 292... Line 287...
292
 
287
 
293
  ldfile_add_arch ("");
288
  ldfile_add_arch ("");
294
  emulation = get_emulation (argc, argv);
289
  emulation = get_emulation (argc, argv);
295
  ldemul_choose_mode (emulation);
290
  ldemul_choose_mode (emulation);
296
  default_target = ldemul_choose_target (argc, argv);
291
  default_target = ldemul_choose_target (argc, argv);
297
  config.maxpagesize = bfd_emul_get_maxpagesize (default_target);
292
  config.maxpagesize = bfd_emul_get_maxpagesize (default_target);
298
  config.commonpagesize = bfd_emul_get_commonpagesize (default_target);
293
  config.commonpagesize = bfd_emul_get_commonpagesize (default_target);
-
 
294
  lang_init ();
299
  lang_init ();
295
  ldexp_init ();
300
  ldemul_before_parse ();
296
  ldemul_before_parse ();
301
  lang_has_input_file = FALSE;
297
  lang_has_input_file = FALSE;
Line 302... Line 298...
302
  parse_args (argc, argv);
298
  parse_args (argc, argv);
Line 376... Line 372...
376
  if (command_line.print_output_format)
372
  if (command_line.print_output_format)
377
    info_msg ("%s\n", lang_get_output_target ());
373
    info_msg ("%s\n", lang_get_output_target ());
Line 378... Line 374...
378
 
374
 
Line -... Line 375...
-
 
375
  lang_final ();
-
 
376
 
-
 
377
  /* If the only command line argument has been -v or --version or --verbose
-
 
378
     then ignore any input files provided by linker scripts and exit now.
-
 
379
     We do not want to create an output file when the linker is just invoked
-
 
380
     to provide version information.  */
-
 
381
  if (argc == 2 && version_printed)
379
  lang_final ();
382
    xexit (0);
380
 
383
 
381
  if (!lang_has_input_file)
384
  if (!lang_has_input_file)
382
    {
385
    {
383
      if (version_printed || command_line.print_output_format)
386
      if (version_printed || command_line.print_output_format)
Line 410... Line 413...
410
 
413
 
Line 411... Line 414...
411
  lang_process ();
414
  lang_process ();
412
 
415
 
413
  /* Print error messages for any missing symbols, for any warning
416
  /* Print error messages for any missing symbols, for any warning
414
     symbols, and possibly multiple definitions.  */
417
     symbols, and possibly multiple definitions.  */
415
  if (link_info.relocatable)
418
  if (bfd_link_relocatable (&link_info))
416
    link_info.output_bfd->flags &= ~EXEC_P;
419
    link_info.output_bfd->flags &= ~EXEC_P;
Line -... Line 420...
-
 
420
  else
-
 
421
    link_info.output_bfd->flags |= EXEC_P;
-
 
422
 
-
 
423
  if ((link_info.compress_debug & COMPRESS_DEBUG))
-
 
424
    {
-
 
425
      link_info.output_bfd->flags |= BFD_COMPRESS;
-
 
426
      if (link_info.compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
417
  else
427
	link_info.output_bfd->flags |= BFD_COMPRESS_GABI;
Line 418... Line 428...
418
    link_info.output_bfd->flags |= EXEC_P;
428
    }
419
 
429
 
420
  ldwrite ();
430
  ldwrite ();
421
 
431
 
422
  if (config.map_file != NULL)
432
  if (config.map_file != NULL)
423
    lang_map ();
433
    lang_map ();
-
 
434
  if (command_line.cref)
-
 
435
    output_cref (config.map_file != NULL ? config.map_file : stdout);
-
 
436
  if (nocrossref_list != NULL)
-
 
437
    check_nocrossrefs ();
-
 
438
  if (command_line.print_memory_usage)
Line -... Line 439...
-
 
439
    lang_print_memory_usage ();
-
 
440
#if 0
-
 
441
  {
-
 
442
    struct bfd_link_hash_entry * h;
-
 
443
 
424
  if (command_line.cref)
444
    h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
Line 425... Line 445...
425
    output_cref (config.map_file != NULL ? config.map_file : stdout);
445
    fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
426
  if (nocrossref_list != NULL)
446
  }
427
    check_nocrossrefs ();
447
#endif
Line 446... Line 466...
446
	einfo (_("%F%B: final close failed: %E\n"), link_info.output_bfd);
466
	einfo (_("%F%B: final close failed: %E\n"), link_info.output_bfd);
Line 447... Line 467...
447
 
467
 
448
      /* If the --force-exe-suffix is enabled, and we're making an
468
      /* If the --force-exe-suffix is enabled, and we're making an
449
	 executable file and it doesn't end in .exe, copy it to one
469
	 executable file and it doesn't end in .exe, copy it to one
450
	 which does.  */
470
	 which does.  */
-
 
471
      if (!bfd_link_relocatable (&link_info)
451
      if (! link_info.relocatable && command_line.force_exe_suffix)
472
	  && command_line.force_exe_suffix)
452
	{
473
	{
Line 453... Line 474...
453
	  int len = strlen (output_filename);
474
	  int len = strlen (output_filename);
454
 
475
 
Line 467... Line 488...
467
	      strcat (dst_name, ".exe");
488
	      strcat (dst_name, ".exe");
468
	      src = fopen (output_filename, FOPEN_RB);
489
	      src = fopen (output_filename, FOPEN_RB);
469
	      dst = fopen (dst_name, FOPEN_WB);
490
	      dst = fopen (dst_name, FOPEN_WB);
Line 470... Line 491...
470
 
491
 
471
	      if (!src)
492
	      if (!src)
472
		einfo (_("%X%P: unable to open for source of copy `%s'\n"),
493
		einfo (_("%P%F: unable to open for source of copy `%s'\n"),
473
		       output_filename);
494
		       output_filename);
474
	      if (!dst)
495
	      if (!dst)
475
		einfo (_("%X%P: unable to open for destination of copy `%s'\n"),
496
		einfo (_("%P%F: unable to open for destination of copy `%s'\n"),
476
		       dst_name);
497
		       dst_name);
477
	      while ((l = fread (buf, 1, bsize, src)) > 0)
498
	      while ((l = fread (buf, 1, bsize, src)) > 0)
478
		{
499
		{
Line 595... Line 616...
595
		   || strcmp (argv[i], "-mips3") == 0
616
		   || strcmp (argv[i], "-mips3") == 0
596
		   || strcmp (argv[i], "-mips4") == 0
617
		   || strcmp (argv[i], "-mips4") == 0
597
		   || strcmp (argv[i], "-mips5") == 0
618
		   || strcmp (argv[i], "-mips5") == 0
598
		   || strcmp (argv[i], "-mips32") == 0
619
		   || strcmp (argv[i], "-mips32") == 0
599
		   || strcmp (argv[i], "-mips32r2") == 0
620
		   || strcmp (argv[i], "-mips32r2") == 0
-
 
621
		   || strcmp (argv[i], "-mips32r6") == 0
600
		   || strcmp (argv[i], "-mips64") == 0
622
		   || strcmp (argv[i], "-mips64") == 0
-
 
623
		   || strcmp (argv[i], "-mips64r2") == 0
601
		   || strcmp (argv[i], "-mips64r2") == 0)
624
		   || strcmp (argv[i], "-mips64r6") == 0)
602
	    {
625
	    {
603
	      /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
626
	      /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
604
		 passed to the linker by some MIPS compilers.  They
627
		 passed to the linker by some MIPS compilers.  They
605
		 generally tell the linker to use a slightly different
628
		 generally tell the linker to use a slightly different
606
		 library path.  Perhaps someday these should be
629
		 library path.  Perhaps someday these should be
Line 742... Line 765...
742
  if (link_info.strip != strip_none)
765
  if (link_info.strip != strip_none)
743
    einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
766
    einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
Line 744... Line 767...
744
 
767
 
745
  free (buf);
768
  free (buf);
-
 
769
  link_info.strip = strip_some;
746
  link_info.strip = strip_some;
770
  fclose (file);
747
}
771
}
748

772

Line 749... Line 773...
749
/* Callbacks from the BFD linker routines.  */
773
/* Callbacks from the BFD linker routines.  */
Line 769... Line 793...
769
  /* Save the original data for trace files/tries below, as plugins
793
  /* Save the original data for trace files/tries below, as plugins
770
     (if enabled) may possibly alter it to point to a replacement
794
     (if enabled) may possibly alter it to point to a replacement
771
     BFD, but we still want to output the original BFD filename.  */
795
     BFD, but we still want to output the original BFD filename.  */
772
  orig_input = *input;
796
  orig_input = *input;
773
#ifdef ENABLE_PLUGINS
797
#ifdef ENABLE_PLUGINS
774
  if (plugin_active_plugins_p () && !no_more_claiming)
798
  if (link_info.lto_plugin_active && !no_more_claiming)
775
    {
799
    {
776
      /* We must offer this archive member to the plugins to claim.  */
800
      /* We must offer this archive member to the plugins to claim.  */
777
      const char *filename = (bfd_my_archive (abfd) != NULL
-
 
778
			      ? bfd_my_archive (abfd)->filename : abfd->filename);
-
 
779
      int fd = open (filename, O_RDONLY | O_BINARY);
-
 
780
      if (fd >= 0)
-
 
781
	{
-
 
782
	  struct ld_plugin_input_file file;
-
 
783
 
-
 
784
	  /* Offset and filesize must refer to the individual archive
-
 
785
	     member, not the whole file, and must exclude the header.
-
 
786
	     Fortunately for us, that is how the data is stored in the
-
 
787
	     origin field of the bfd and in the arelt_data.  */
-
 
788
	  file.name = filename;
-
 
789
	  file.offset = abfd->origin;
-
 
790
	  file.filesize = arelt_size (abfd);
-
 
791
	  file.fd = fd;
-
 
792
	  plugin_maybe_claim (&file, input);
801
      plugin_maybe_claim (input);
793
	  if (input->flags.claimed)
802
	  if (input->flags.claimed)
794
	    {
803
	    {
795
	      input->flags.claim_archive = TRUE;
804
	      input->flags.claim_archive = TRUE;
796
	      *subsbfd = input->the_bfd;
805
	      *subsbfd = input->the_bfd;
797
	    }
806
	    }
798
	}
807
	}
799
    }
-
 
800
#endif /* ENABLE_PLUGINS */
808
#endif /* ENABLE_PLUGINS */
Line 801... Line 809...
801
 
809
 
Line 802... Line 810...
802
  ldlang_add_file (input);
810
  ldlang_add_file (input);
Line 838... Line 846...
838
 
846
 
839
      if (! header_printed)
847
      if (! header_printed)
840
	{
848
	{
Line 841... Line 849...
841
	  char buf[100];
849
	  char buf[100];
-
 
850
 
842
 
851
	  sprintf (buf, _("Archive member included "
843
	  sprintf (buf, _("Archive member included because of file (symbol)\n\n"));
852
			  "to satisfy reference by file (symbol)\n\n"));
844
	  minfo ("%s", buf);
853
	  minfo ("%s", buf);
Line 845... Line 854...
845
	  header_printed = TRUE;
854
	  header_printed = TRUE;
Line 1107... Line 1116...
1107
    return TRUE;
1116
    return TRUE;
Line 1108... Line 1117...
1108
 
1117
 
1109
  /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1118
  /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
1110
     useful error message.  */
1119
     useful error message.  */
1111
  if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
1120
  if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
1112
      && (info->relocatable
1121
      && (bfd_link_relocatable (info)
1113
	  || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1122
	  || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
Line 1114... Line 1123...
1114
    einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1123
    einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1115
 
1124
 
Line 1146... Line 1155...
1146
  const char *warning;
1155
  const char *warning;
1147
  const char *symbol;
1156
  const char *symbol;
1148
  asymbol **asymbols;
1157
  asymbol **asymbols;
1149
};
1158
};
Line -... Line 1159...
-
 
1159
 
-
 
1160
/* Look through the relocs to see if we can find a plausible address
-
 
1161
   for SYMBOL in ABFD.  Return TRUE if found.  Otherwise return FALSE.  */
-
 
1162
 
-
 
1163
static bfd_boolean
-
 
1164
symbol_warning (const char *warning, const char *symbol, bfd *abfd)
-
 
1165
{
-
 
1166
  struct warning_callback_info cinfo;
-
 
1167
 
-
 
1168
  if (!bfd_generic_link_read_symbols (abfd))
-
 
1169
    einfo (_("%B%F: could not read symbols: %E\n"), abfd);
-
 
1170
 
-
 
1171
  cinfo.found = FALSE;
-
 
1172
  cinfo.warning = warning;
-
 
1173
  cinfo.symbol = symbol;
-
 
1174
  cinfo.asymbols = bfd_get_outsymbols (abfd);
-
 
1175
  bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
-
 
1176
  return cinfo.found;
-
 
1177
}
1150
 
1178
 
Line 1151... Line 1179...
1151
/* This is called when there is a reference to a warning symbol.  */
1179
/* This is called when there is a reference to a warning symbol.  */
1152
 
1180
 
1153
static bfd_boolean
1181
static bfd_boolean
Line 1168... Line 1196...
1168
    einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning);
1196
    einfo ("%C: %s%s\n", abfd, section, address, _("warning: "), warning);
1169
  else if (abfd == NULL)
1197
  else if (abfd == NULL)
1170
    einfo ("%P: %s%s\n", _("warning: "), warning);
1198
    einfo ("%P: %s%s\n", _("warning: "), warning);
1171
  else if (symbol == NULL)
1199
  else if (symbol == NULL)
1172
    einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1200
    einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1173
  else
1201
  else if (! symbol_warning (warning, symbol, abfd))
1174
    {
1202
    {
1175
      struct warning_callback_info cinfo;
-
 
1176
 
-
 
1177
      /* Look through the relocs to see if we can find a plausible
-
 
1178
	 address.  */
1203
      bfd *b;
1179
 
-
 
1180
      if (!bfd_generic_link_read_symbols (abfd))
1204
      /* Search all input files for a reference to SYMBOL.  */
1181
	einfo (_("%B%F: could not read symbols: %E\n"), abfd);
-
 
1182
 
-
 
1183
      cinfo.found = FALSE;
-
 
1184
      cinfo.warning = warning;
-
 
1185
      cinfo.symbol = symbol;
-
 
1186
      cinfo.asymbols = bfd_get_outsymbols (abfd);
1205
      for (b = info->input_bfds; b; b = b->link.next)
1187
      bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
1206
	if (b != abfd && symbol_warning (warning, symbol, b))
1188
 
-
 
1189
      if (! cinfo.found)
1207
	  return TRUE;
1190
	einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1208
	einfo ("%B: %s%s\n", abfd, _("warning: "), warning);
1191
    }
1209
    }
Line 1192... Line 1210...
1192
 
1210
 
1193
  return TRUE;
1211
  return TRUE;
Line 1434... Line 1452...
1434
   all references from non-IR files.  */
1452
   all references from non-IR files.  */
Line 1435... Line 1453...
1435
 
1453
 
1436
static bfd_boolean
1454
static bfd_boolean
1437
notice (struct bfd_link_info *info,
1455
notice (struct bfd_link_info *info,
-
 
1456
	struct bfd_link_hash_entry *h,
1438
	struct bfd_link_hash_entry *h,
1457
	struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
1439
	bfd *abfd,
1458
	bfd *abfd,
1440
	asection *section,
1459
	asection *section,
1441
	bfd_vma value,
1460
	bfd_vma value,
1442
	flagword flags ATTRIBUTE_UNUSED,
-
 
1443
	const char *string ATTRIBUTE_UNUSED)
1461
	flagword flags ATTRIBUTE_UNUSED)
1444
{
1462
{
Line 1445... Line 1463...
1445
  const char *name;
1463
  const char *name;
1446
 
1464