1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24: #include "sysdep.h"
25: #include "bfd.h"
26: #include "safe-ctype.h"
27: #include "libiberty.h"
28: #include "progress.h"
29: #include "bfdlink.h"
30: #include "filenames.h"
31:
32: #include "ld.h"
33: #include "ldmain.h"
34: #include "ldmisc.h"
35: #include "ldwrite.h"
36: #include "ldexp.h"
37: #include "ldlang.h"
38: #include <ldgram.h>
39: #include "ldlex.h"
40: #include "ldfile.h"
41: #include "ldemul.h"
42: #include "ldctor.h"
43:
44:
45: #if !defined(S_ISDIR) && defined(S_IFDIR)
46: #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47: #endif
48:
49: #include <string.h>
50:
51: #ifdef HAVE_SBRK
52: #if !HAVE_DECL_SBRK
53: extern void *sbrk ();
54: #endif
55: #endif
56:
57: #ifndef TARGET_SYSTEM_ROOT
58: #define TARGET_SYSTEM_ROOT ""
59: #endif
60:
61:
62:
63: char *default_target;
64: const char *output_filename = "a.out";
65:
66:
67: char *program_name;
68:
69:
70: const char *ld_sysroot;
71:
72:
73: char * ld_canon_sysroot;
74: int ld_canon_sysroot_len;
75:
76:
77: bfd *output_bfd = 0;
78:
79:
80: int g_switch_value = 8;
81:
82:
83: bfd_boolean trace_files;
84:
85:
86: bfd_boolean trace_file_tries;
87:
88:
89:
90: bfd_boolean version_printed;
91:
92:
93: bfd_boolean whole_archive;
94:
95:
96:
97: bfd_boolean as_needed;
98:
99:
100:
101: bfd_boolean add_needed = TRUE;
102:
103:
104: bfd_boolean demangling;
105:
106: args_type command_line;
107:
108: ld_config_type config;
109:
110: sort_type sort_section;
111:
112: static const char *get_sysroot
113: (int, char **);
114: static char *get_emulation
115: (int, char **);
116: static void set_scripts_dir
117: (void);
118: static bfd_boolean add_archive_element
119: (struct bfd_link_info *, bfd *, const char *);
120: static bfd_boolean multiple_definition
121: (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
122: bfd *, asection *, bfd_vma);
123: static bfd_boolean multiple_common
124: (struct bfd_link_info *, const char *, bfd *, enum bfd_link_hash_type,
125: bfd_vma, bfd *, enum bfd_link_hash_type, bfd_vma);
126: static bfd_boolean add_to_set
127: (struct bfd_link_info *, struct bfd_link_hash_entry *,
128: bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
129: static bfd_boolean constructor_callback
130: (struct bfd_link_info *, bfd_boolean, const char *, bfd *,
131: asection *, bfd_vma);
132: static bfd_boolean warning_callback
133: (struct bfd_link_info *, const char *, const char *, bfd *,
134: asection *, bfd_vma);
135: static void warning_find_reloc
136: (bfd *, asection *, void *);
137: static bfd_boolean undefined_symbol
138: (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
139: bfd_boolean);
140: static bfd_boolean reloc_overflow
141: (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
142: const char *, bfd_vma, bfd *, asection *, bfd_vma);
143: static bfd_boolean reloc_dangerous
144: (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
145: static bfd_boolean unattached_reloc
146: (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
147: static bfd_boolean notice
148: (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
149:
150: static struct bfd_link_callbacks link_callbacks =
151: {
152: add_archive_element,
153: multiple_definition,
154: multiple_common,
155: add_to_set,
156: constructor_callback,
157: warning_callback,
158: undefined_symbol,
159: reloc_overflow,
160: reloc_dangerous,
161: unattached_reloc,
162: notice,
163: einfo,
164: info_msg,
165: minfo,
166: ldlang_override_segment_assignment
167: };
168:
169: struct bfd_link_info link_info;
170: ^L
171: static void
172: remove_output (void)
173: {
174: if (output_filename)
175: {
176: if (output_bfd)
177: bfd_cache_close (output_bfd);
178: if (delete_output_file_on_failure)
179: unlink_if_ordinary (output_filename);
180: }
181: }
182:
183: int
184: main (int argc, char **argv)
185: {
186: char *emulation;
187: long start_time = get_run_time ();
188:
189: #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
190: setlocale (LC_MESSAGES, "");
191: #endif
192: #if defined (HAVE_SETLOCALE)
193: setlocale (LC_CTYPE, "");
194: #endif
195: bindtextdomain (PACKAGE, LOCALEDIR);
196: textdomain (PACKAGE);
197:
198: program_name = argv[0];
199: xmalloc_set_program_name (program_name);
200:
201: START_PROGRESS (program_name, 0);
202:
203: expandargv (&argc, &argv);
204:
205: bfd_init ();
206:
207: bfd_set_error_program_name (program_name);
208:
209: xatexit (remove_output);
210:
211:
212: ld_sysroot = get_sysroot (argc, argv);
213: if (*ld_sysroot)
214: {
215: if (*TARGET_SYSTEM_ROOT == 0)
216: {
217: einfo ("%P%F: this linker was not configured to use sysroots\n");
218: ld_sysroot = "";
219: }
220: else
221: ld_canon_sysroot = lrealpath (ld_sysroot);
222: }
223: if (ld_canon_sysroot)
224: ld_canon_sysroot_len = strlen (ld_canon_sysroot);
225: else
226: ld_canon_sysroot_len = -1;
227:
228:
229:
230:
231:
232: if (! bfd_set_default_target (TARGET))
233: {
234: einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
235: xexit (1);
236: }
237:
238: #if YYDEBUG
239: {
240: extern int yydebug;
241: yydebug = 1;
242: }
243: #endif
244:
245: config.build_constructors = TRUE;
246: config.rpath_separator = ':';
247: config.split_by_reloc = (unsigned) -1;
248: config.split_by_file = (bfd_size_type) -1;
249: config.make_executable = TRUE;
250: config.magic_demand_paged = TRUE;
251: config.text_read_only = TRUE;
252:
253: command_line.warn_mismatch = TRUE;
254: command_line.warn_search_mismatch = TRUE;
255: command_line.check_section_addresses = TRUE;
256:
257:
258:
259:
260:
261:
262: demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
263:
264: link_info.allow_undefined_version = TRUE;
265: link_info.keep_memory = TRUE;
266: link_info.combreloc = TRUE;
267: link_info.strip_discarded = TRUE;
268: link_info.emit_hash = TRUE;
269: link_info.callbacks = &link_callbacks;
270: link_info.input_bfds_tail = &link_info.input_bfds;
271:
272:
273: link_info.init_function = "_init";
274: link_info.fini_function = "_fini";
275: link_info.relax_pass = 1;
276: link_info.pei386_auto_import = -1;
277: link_info.spare_dynamic_tags = 5;
278:
279: ldfile_add_arch ("");
280: emulation = get_emulation (argc, argv);
281: ldemul_choose_mode (emulation);
282: default_target = ldemul_choose_target (argc, argv);
283: lang_init ();
284: ldemul_before_parse ();
285: lang_has_input_file = FALSE;
286: parse_args (argc, argv);
287:
288: if (config.hash_table_size != 0)
289: bfd_hash_set_default_size (config.hash_table_size);
290:
291: ldemul_set_symbols ();
292:
293: if (link_info.relocatable)
294: {
295: if (link_info.gc_sections)
296: einfo ("%P%F: --gc-sections and -r may not be used together\n");
297: else if (command_line.relax)
298: einfo (_("%P%F: --relax and -r may not be used together\n"));
299: if (link_info.shared)
300: einfo (_("%P%F: -r and -shared may not be used together\n"));
301: }
302:
303:
304:
305:
306:
307:
308: switch (command_line.symbolic)
309: {
310: case symbolic_unset:
311: break;
312: case symbolic:
313:
314: if (link_info.shared)
315: {
316: link_info.symbolic = TRUE;
317:
318: link_info.dynamic_list = NULL;
319: command_line.dynamic_list = dynamic_list_unset;
320: }
321: break;
322: case symbolic_functions:
323:
324: if (link_info.shared)
325: command_line.dynamic_list = dynamic_list_data;
326: break;
327: }
328:
329: switch (command_line.dynamic_list)
330: {
331: case dynamic_list_unset:
332: break;
333: case dynamic_list_data:
334: link_info.dynamic_data = TRUE;
335: case dynamic_list:
336: link_info.dynamic = TRUE;
337: break;
338: }
339:
340: if (! link_info.shared)
341: {
342: if (command_line.filter_shlib)
343: einfo (_("%P%F: -F may not be used without -shared\n"));
344: if (command_line.auxiliary_filters)
345: einfo (_("%P%F: -f may not be used without -shared\n"));
346: }
347:
348: if (! link_info.shared || link_info.pie)
349: link_info.executable = TRUE;
350:
351:
352:
353:
354: if (link_info.relocatable && link_info.strip == strip_all)
355: {
356: link_info.strip = strip_debugger;
357: if (link_info.discard == discard_sec_merge)
358: link_info.discard = discard_all;
359: }
360:
361:
362:
363: set_scripts_dir ();
364:
365:
366:
367: if (saved_script_handle == NULL
368: && command_line.default_script != NULL)
369: {
370: ldfile_open_command_file (command_line.default_script);
371: parser_input = input_script;
372: yyparse ();
373: }
374:
375:
376:
377: if (saved_script_handle == NULL)
378: {
379: int isfile;
380: char *s = ldemul_get_script (&isfile);
381:
382: if (isfile)
383: ldfile_open_command_file (s);
384: else
385: {
386: lex_string = s;
387: lex_redirect (s);
388: }
389: parser_input = input_script;
390: yyparse ();
391: lex_string = NULL;
392: }
393:
394: if (trace_file_tries)
395: {
396: if (saved_script_handle)
397: info_msg (_("using external linker script:"));
398: else
399: info_msg (_("using internal linker script:"));
400: info_msg ("\n==================================================\n");
401:
402: if (saved_script_handle)
403: {
404: static const int ld_bufsz = 8193;
405: size_t n;
406: char *buf = xmalloc (ld_bufsz);
407:
408: rewind (saved_script_handle);
409: while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
410: {
411: buf[n] = 0;
412: info_msg (buf);
413: }
414: rewind (saved_script_handle);
415: free (buf);
416: }
417: else
418: {
419: int isfile;
420:
421: info_msg (ldemul_get_script (&isfile));
422: }
423:
424: info_msg ("\n==================================================\n");
425: }
426:
427: lang_final ();
428:
429: if (!lang_has_input_file)
430: {
431: if (version_printed)
432: xexit (0);
433: einfo (_("%P%F: no input files\n"));
434: }
435:
436: if (trace_files)
437: info_msg (_("%P: mode %s\n"), emulation);
438:
439: ldemul_after_parse ();
440:
441: if (config.map_filename)
442: {
443: if (strcmp (config.map_filename, "-") == 0)
444: {
445: config.map_file = stdout;
446: }
447: else
448: {
449: config.map_file = fopen (config.map_filename, FOPEN_WT);
450: if (config.map_file == (FILE *) NULL)
451: {
452: bfd_set_error (bfd_error_system_call);
453: einfo (_("%P%F: cannot open map file %s: %E\n"),
454: config.map_filename);
455: }
456: }
457: }
458:
459: lang_process ();
460:
461:
462:
463: if (link_info.relocatable)
464: output_bfd->flags &= ~EXEC_P;
465: else
466: output_bfd->flags |= EXEC_P;
467:
468: ldwrite ();
469:
470: if (config.map_file != NULL)
471: lang_map ();
472: if (command_line.cref)
473: output_cref (config.map_file != NULL ? config.map_file : stdout);
474: if (nocrossref_list != NULL)
475: check_nocrossrefs ();
476:
477: lang_finish ();
478:
479:
480:
481:
482: if (!config.make_executable && !force_make_executable)
483: {
484: if (trace_files)
485: einfo (_("%P: link errors found, deleting executable `%s'\n"),
486: output_filename);
487:
488:
489: xexit (1);
490: }
491: else
492: {
493: if (! bfd_close (output_bfd))
494: einfo (_("%F%B: final close failed: %E\n"), output_bfd);
495:
496:
497:
498:
499: if (! link_info.relocatable && command_line.force_exe_suffix)
500: {
501: int len = strlen (output_filename);
502:
503: if (len < 4
504: || (strcasecmp (output_filename + len - 4, ".exe") != 0
505: && strcasecmp (output_filename + len - 4, ".dll") != 0))
506: {
507: FILE *src;
508: FILE *dst;
509: const int bsize = 4096;
510: char *buf = xmalloc (bsize);
511: int l;
512: char *dst_name = xmalloc (len + 5);
513:
514: strcpy (dst_name, output_filename);
515: strcat (dst_name, ".exe");
516: src = fopen (output_filename, FOPEN_RB);
517: dst = fopen (dst_name, FOPEN_WB);
518:
519: if (!src)
520: einfo (_("%X%P: unable to open for source of copy `%s'\n"),
521: output_filename);
522: if (!dst)
523: einfo (_("%X%P: unable to open for destination of copy `%s'\n"),
524: dst_name);
525: while ((l = fread (buf, 1, bsize, src)) > 0)
526: {
527: int done = fwrite (buf, 1, l, dst);
528:
529: if (done != l)
530: einfo (_("%P: Error writing file `%s'\n"), dst_name);
531: }
532:
533: fclose (src);
534: if (fclose (dst) == EOF)
535: einfo (_("%P: Error closing file `%s'\n"), dst_name);
536: free (dst_name);
537: free (buf);
538: }
539: }
540: }
541:
542: END_PROGRESS (program_name);
543:
544: if (config.stats)
545: {
546: #ifdef HAVE_SBRK
547: char *lim = sbrk (0);
548: #endif
549: long run_time = get_run_time () - start_time;
550:
551: fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
552: program_name, run_time / 1000000, run_time % 1000000);
553: #ifdef HAVE_SBRK
554: fprintf (stderr, _("%s: data size %ld\n"), program_name,
555: (long) (lim - (char *) &environ));
556: #endif
557: }
558:
559:
560: output_filename = NULL;
561:
562: xexit (0);
563: return 0;
564: }
565:
566:
567:
568:
569:
570: static char *
571: get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
572: {
573: #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
574: char *path;
575: struct stat s;
576:
577: path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
578: if (path)
579: {
580: if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
581: return path;
582: free (path);
583: }
584: #endif
585: return 0;
586: }
587:
588:
589:
590: static const char *
591: get_sysroot (int argc, char **argv)
592: {
593: int i;
594: const char *path;
595:
596: for (i = 1; i < argc; i++)
597: if (CONST_STRNEQ (argv[i], "--sysroot="))
598: return argv[i] + strlen ("--sysroot=");
599:
600: path = get_relative_sysroot (BINDIR);
601: if (path)
602: return path;
603:
604: path = get_relative_sysroot (TOOLBINDIR);
605: if (path)
606: return path;
607: