#include "ldemul.h"
#include "ldctor.h"
-/* Somewhere above, sys/stat.h got included . . . . */
+/* Somewhere above, sys/stat.h got included. */
#if !defined(S_ISDIR) && defined(S_IFDIR)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
int main PARAMS ((int, char **));
-static char *get_emulation PARAMS ((int, char **));
-static void set_scripts_dir PARAMS ((void));
-
/* EXPORTS */
char *default_target;
/* The prefix for system library directories. */
char *ld_sysroot;
+/* The canonical representation of ld_sysroot. */
+char * ld_canon_sysroot;
+int ld_canon_sysroot_len;
+
/* The file that we're creating. */
bfd *output_bfd = 0;
ld_config_type config;
-static void remove_output PARAMS ((void));
-static bfd_boolean check_for_scripts_dir PARAMS ((char *dir));
-static bfd_boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
- const char *));
-static bfd_boolean multiple_definition PARAMS ((struct bfd_link_info *,
- const char *,
- bfd *, asection *, bfd_vma,
- bfd *, asection *, bfd_vma));
-static bfd_boolean multiple_common PARAMS ((struct bfd_link_info *,
- const char *, bfd *,
- enum bfd_link_hash_type, bfd_vma,
- bfd *, enum bfd_link_hash_type,
- bfd_vma));
-static bfd_boolean add_to_set PARAMS ((struct bfd_link_info *,
- struct bfd_link_hash_entry *,
- bfd_reloc_code_real_type,
- bfd *, asection *, bfd_vma));
-static bfd_boolean constructor_callback PARAMS ((struct bfd_link_info *,
- bfd_boolean constructor,
- const char *name,
- bfd *, asection *, bfd_vma));
-static bfd_boolean warning_callback PARAMS ((struct bfd_link_info *,
- const char *, const char *, bfd *,
- asection *, bfd_vma));
-static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
-static bfd_boolean undefined_symbol PARAMS ((struct bfd_link_info *,
- const char *, bfd *,
- asection *, bfd_vma, bfd_boolean));
-static bfd_boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
- const char *, bfd_vma,
- bfd *, asection *, bfd_vma));
-static bfd_boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
- bfd *, asection *, bfd_vma));
-static bfd_boolean unattached_reloc PARAMS ((struct bfd_link_info *,
- const char *, bfd *, asection *,
- bfd_vma));
-static bfd_boolean notice PARAMS ((struct bfd_link_info *, const char *,
- bfd *, asection *, bfd_vma));
-
-static struct bfd_link_callbacks link_callbacks = {
+static char *get_emulation
+ PARAMS ((int, char **));
+static void set_scripts_dir
+ PARAMS ((void));
+static void remove_output
+ PARAMS ((void));
+static bfd_boolean check_for_scripts_dir
+ PARAMS ((char *));
+static bfd_boolean add_archive_element
+ PARAMS ((struct bfd_link_info *, bfd *, const char *));
+static bfd_boolean multiple_definition
+ PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
+ bfd *, asection *, bfd_vma));
+static bfd_boolean multiple_common
+ PARAMS ((struct bfd_link_info *, const char *, bfd *,
+ enum bfd_link_hash_type, bfd_vma, bfd *, enum bfd_link_hash_type,
+ bfd_vma));
+static bfd_boolean add_to_set
+ PARAMS ((struct bfd_link_info *, struct bfd_link_hash_entry *,
+ bfd_reloc_code_real_type, bfd *, asection *, bfd_vma));
+static bfd_boolean constructor_callback
+ PARAMS ((struct bfd_link_info *, bfd_boolean, const char *, bfd *,
+ asection *, bfd_vma));
+static bfd_boolean warning_callback
+ PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *,
+ asection *, bfd_vma));
+static void warning_find_reloc
+ PARAMS ((bfd *, asection *, PTR));
+static bfd_boolean undefined_symbol
+ PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
+ bfd_boolean));
+static bfd_boolean reloc_overflow
+ PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma,
+ bfd *, asection *, bfd_vma));
+static bfd_boolean reloc_dangerous
+ PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
+static bfd_boolean unattached_reloc
+ PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
+static bfd_boolean notice
+ PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
+
+static struct bfd_link_callbacks link_callbacks =
+{
add_archive_element,
multiple_definition,
multiple_common,
reloc_overflow,
reloc_dangerous,
unattached_reloc,
- notice
+ notice,
+ error_handler
};
struct bfd_link_info link_info;
#ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
ld_sysroot = make_relative_prefix (program_name, BINDIR,
TARGET_SYSTEM_ROOT);
+
if (ld_sysroot)
{
struct stat s;
int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode);
+
if (!res)
{
free (ld_sysroot);
- ld_sysroot = TARGET_SYSTEM_ROOT;
+ ld_sysroot = NULL;
}
}
- else
- ld_sysroot = TARGET_SYSTEM_ROOT;
-#else
- ld_sysroot = TARGET_SYSTEM_ROOT;
+
+ if (! ld_sysroot)
+ {
+ ld_sysroot = make_relative_prefix (program_name, TOOLBINDIR,
+ TARGET_SYSTEM_ROOT);
+
+ if (ld_sysroot)
+ {
+ struct stat s;
+ int res = stat (ld_sysroot, &s) == 0 && S_ISDIR (s.st_mode);
+
+ if (!res)
+ {
+ free (ld_sysroot);
+ ld_sysroot = NULL;
+ }
+ }
+ }
+
+ if (! ld_sysroot)
#endif
+ ld_sysroot = TARGET_SYSTEM_ROOT;
+
+ if (ld_sysroot && *ld_sysroot)
+ ld_canon_sysroot = lrealpath (ld_sysroot);
+
+ if (ld_canon_sysroot)
+ ld_canon_sysroot_len = strlen (ld_canon_sysroot);
+ else
+ ld_canon_sysroot_len = -1;
/* Set the default BFD target based on the configured target. Doing
this permits the linker to be configured for a particular target,
link_info.emitrelocations = FALSE;
link_info.task_link = FALSE;
link_info.shared = FALSE;
+ link_info.pie = FALSE;
+ link_info.executable = FALSE;
link_info.symbolic = FALSE;
link_info.export_dynamic = FALSE;
link_info.static_link = FALSE;
link_info.traditional_format = FALSE;
link_info.optimize = FALSE;
link_info.no_undefined = FALSE;
- link_info.allow_shlib_undefined = FALSE;
+ link_info.allow_shlib_undefined = TRUE;
link_info.allow_multiple_definition = FALSE;
link_info.allow_undefined_version = TRUE;
link_info.keep_memory = TRUE;
link_info.spare_dynamic_tags = 5;
link_info.flags = (bfd_vma) 0;
link_info.flags_1 = (bfd_vma) 0;
+ link_info.relax_finalizing = FALSE;
ldfile_add_arch ("");
einfo (_("%P%F: -f may not be used without -shared\n"));
}
+ if (! link_info.shared || link_info.pie)
+ link_info.executable = TRUE;
+
/* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
don't see how else this can be handled, since in this case we
must preserve all externally visible symbols. */
}
if (trace_files)
- {
- info_msg (_("%P: mode %s\n"), emulation);
- }
+ info_msg (_("%P: mode %s\n"), emulation);
ldemul_after_parse ();
/* Print error messages for any missing symbols, for any warning
symbols, and possibly multiple definitions. */
-
if (link_info.relocateable)
output_bfd->flags &= ~EXEC_P;
else
/* Even if we're producing relocateable output, some non-fatal errors should
be reported in the exit status. (What non-fatal errors, if any, do we
want to ignore for relocateable output?) */
-
if (!config.make_executable && !force_make_executable)
{
if (trace_files)
- {
- einfo (_("%P: link errors found, deleting executable `%s'\n"),
- output_filename);
- }
+ einfo (_("%P: link errors found, deleting executable `%s'\n"),
+ output_filename);
/* The file will be removed by remove_output. */
-
xexit (1);
}
else
if (! link_info.relocateable && command_line.force_exe_suffix)
{
int len = strlen (output_filename);
+
if (len < 4
|| (strcasecmp (output_filename + len - 4, ".exe") != 0
&& strcasecmp (output_filename + len - 4, ".dll") != 0))
char *buf = xmalloc (bsize);
int l;
char *dst_name = xmalloc (len + 5);
+
strcpy (dst_name, output_filename);
strcat (dst_name, ".exe");
src = fopen (output_filename, FOPEN_RB);
while ((l = fread (buf, 1, bsize, src)) > 0)
{
int done = fwrite (buf, 1, l, dst);
+
if (done != l)
- {
- einfo (_("%P: Error writing file `%s'\n"), dst_name);
- }
+ einfo (_("%P: Error writing file `%s'\n"), dst_name);
}
+
fclose (src);
if (fclose (dst) == EOF)
- {
- einfo (_("%P: Error closing file `%s'\n"), dst_name);
- }
+ einfo (_("%P: Error closing file `%s'\n"), dst_name);
free (dst_name);
free (buf);
}
i++;
}
else
- {
- einfo (_("%P%F: missing argument to -m\n"));
- }
+ einfo (_("%P%F: missing argument to -m\n"));
}
else if (strcmp (argv[i], "-mips1") == 0
|| strcmp (argv[i], "-mips2") == 0
|| strcmp (argv[i], "-mips3") == 0
- || strcmp (argv[i], "-mips32") == 0
- || strcmp (argv[i], "-mips64") == 0
|| strcmp (argv[i], "-mips4") == 0
- || strcmp (argv[i], "-mips5") == 0)
+ || strcmp (argv[i], "-mips5") == 0
+ || strcmp (argv[i], "-mips32") == 0
+ || strcmp (argv[i], "-mips32r2") == 0
+ || strcmp (argv[i], "-mips64") == 0)
{
/* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
passed to the linker by some MIPS compilers. They
SCRIPTDIR (passed from Makefile)
the dir where this program is (for using it from the build tree)
the dir where this program is/../lib
- (for installing the tool suite elsewhere) */
+ (for installing the tool suite elsewhere). */
static void
set_scripts_dir ()
{
char *end, *dir;
size_t dirlen;
+ bfd_boolean found;
dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
- if (dir && check_for_scripts_dir (dir))
- /* Success. Don't free dir. */
- return;
+ if (dir)
+ {
+ found = check_for_scripts_dir (dir);
+ free (dir);
+ if (found)
+ return;
+ }
+ dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
if (dir)
- free (dir);
+ {
+ found = check_for_scripts_dir (dir);
+ free (dir);
+ if (found)
+ return;
+ }
if (check_for_scripts_dir (SCRIPTDIR))
/* We've been installed normally. */
{
/* We could have \foo\bar, or /foo\bar. */
char *bslash = strrchr (program_name, '\\');
+
if (end == NULL || (bslash != NULL && bslash > end))
end = bslash;
}
#endif
if (end == NULL)
- {
- /* Don't look for ldscripts in the current directory. There is
- too much potential for confusion. */
- return;
- }
+ /* Don't look for ldscripts in the current directory. There is
+ too much potential for confusion. */
+ return;
dirlen = end - program_name;
/* Make a copy of program_name in dir.
dir[dirlen] = '\0';
if (check_for_scripts_dir (dir))
- /* Don't free dir. */
- return;
+ {
+ free (dir);
+ return;
+ }
/* Look for "ldscripts" in <the dir where our binary is>/../lib. */
strcpy (dir + dirlen, "/../lib");
- if (check_for_scripts_dir (dir))
- return;
-
- /* Well, we tried. */
+ check_for_scripts_dir (dir);
free (dir);
}
61))
einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
}
+
if (bfd_hash_lookup (link_info.wrap_hash, name, TRUE, TRUE) == NULL)
einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
}
if (link_info.strip != strip_none)
einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
+ free (buf);
link_info.strip = strip_some;
}
\f
/* A structure used by warning_callback to pass information through
bfd_map_over_sections. */
-struct warning_callback_info {
+struct warning_callback_info
+{
bfd_boolean found;
const char *warning;
const char *symbol;
/* Look through the relocs to see if we can find a plausible
address. */
-
entry = (lang_input_statement_type *) abfd->usrdata;
if (entry != NULL && entry->asymbols != NULL)
asymbols = entry->asymbols;
static struct bfd_hash_table *hash;
/* Only warn once about a particular undefined symbol. */
-
if (hash == NULL)
{
hash = ((struct bfd_hash_table *)
return TRUE;
}
+/* Counter to limit the number of relocation overflow error messages
+ to print. Errors are printed as it is decremented. When it's
+ called and the counter is zero, a final message is printed
+ indicating more relocations were omitted. When it gets to -1, no
+ such errors are printed. If it's initially set to a value less
+ than -1, all such errors will be printed (--verbose does this). */
+
+int overflow_cutoff_limit = 10;
+
/* This is called when a reloc overflows. */
static bfd_boolean
asection *section;
bfd_vma address;
{
+ if (overflow_cutoff_limit == -1)
+ return TRUE;
+
if (abfd == (bfd *) NULL)
einfo (_("%P%X: generated"));
else
einfo ("%X%C:", abfd, section, address);
+
+ if (overflow_cutoff_limit >= 0
+ && overflow_cutoff_limit-- == 0)
+ {
+ einfo (_(" additional relocation overflows omitted from the output\n"));
+ return TRUE;
+ }
+
einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name);
if (addend != 0)
einfo ("+%v", addend);