Commit | Line | Data |
---|---|---|
8594f568 | 1 | /* Main program of GNU linker. |
0b2f8d2e | 2 | Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc. |
ce4d59e2 | 3 | Written by Steve Chamberlain steve@cygnus.com |
e47bfa63 | 4 | |
2fa0b342 DHW |
5 | This file is part of GLD, the Gnu Linker. |
6 | ||
7 | GLD is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
ce4d59e2 | 9 | the Free Software Foundation; either version 2, or (at your option) |
2fa0b342 DHW |
10 | any later version. |
11 | ||
12 | GLD is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GLD; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
2fa0b342 | 21 | |
2fa0b342 | 22 | #include "bfd.h" |
f177a611 | 23 | #include "sysdep.h" |
b9395be3 ILT |
24 | #include <stdio.h> |
25 | #include "bfdlink.h" | |
2fa0b342 DHW |
26 | |
27 | #include "config.h" | |
28 | #include "ld.h" | |
29 | #include "ldmain.h" | |
30 | #include "ldmisc.h" | |
31 | #include "ldwrite.h" | |
32 | #include "ldgram.h" | |
fcf276c4 | 33 | #include "ldexp.h" |
2fa0b342 | 34 | #include "ldlang.h" |
8c514453 | 35 | #include "ldemul.h" |
2fa0b342 DHW |
36 | #include "ldlex.h" |
37 | #include "ldfile.h" | |
c611e285 | 38 | #include "ldctor.h" |
9d1fe8a4 | 39 | |
922018a1 | 40 | /* Somewhere above, sys/stat.h got included . . . . */ |
d723cd17 DM |
41 | #if !defined(S_ISDIR) && defined(S_IFDIR) |
42 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | |
43 | #endif | |
44 | ||
45 | #include <string.h> | |
46 | ||
fcf276c4 ILT |
47 | static char *get_emulation PARAMS ((int, char **)); |
48 | static void set_scripts_dir PARAMS ((void)); | |
e47bfa63 | 49 | |
2fa0b342 DHW |
50 | /* EXPORTS */ |
51 | ||
52 | char *default_target; | |
fcf276c4 | 53 | const char *output_filename = "a.out"; |
e47bfa63 | 54 | |
2fa0b342 DHW |
55 | /* Name this program was invoked by. */ |
56 | char *program_name; | |
57 | ||
58 | /* The file that we're creating */ | |
b6316534 | 59 | bfd *output_bfd = 0; |
2fa0b342 | 60 | |
173a0c3d DM |
61 | /* Set by -G argument, for MIPS ECOFF target. */ |
62 | int g_switch_value = 8; | |
63 | ||
2fa0b342 DHW |
64 | /* Nonzero means print names of input files as processed. */ |
65 | boolean trace_files; | |
66 | ||
bbd2521f DM |
67 | /* Nonzero means same, but note open failures, too. */ |
68 | boolean trace_file_tries; | |
69 | ||
7b40f2b1 DM |
70 | /* Nonzero means version number was printed, so exit successfully |
71 | instead of complaining if no input files are given. */ | |
72 | boolean version_printed; | |
73 | ||
2fa0b342 DHW |
74 | /* 1 => write load map. */ |
75 | boolean write_map; | |
76 | ||
2fa0b342 | 77 | args_type command_line; |
173a0c3d | 78 | |
2fa0b342 | 79 | ld_config_type config; |
9f629407 ILT |
80 | |
81 | static boolean check_for_scripts_dir PARAMS ((char *dir)); | |
b9395be3 ILT |
82 | static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *, |
83 | const char *)); | |
84 | static boolean multiple_definition PARAMS ((struct bfd_link_info *, | |
85 | const char *, | |
86 | bfd *, asection *, bfd_vma, | |
87 | bfd *, asection *, bfd_vma)); | |
88 | static boolean multiple_common PARAMS ((struct bfd_link_info *, | |
89 | const char *, bfd *, | |
90 | enum bfd_link_hash_type, bfd_vma, | |
91 | bfd *, enum bfd_link_hash_type, | |
92 | bfd_vma)); | |
93 | static boolean add_to_set PARAMS ((struct bfd_link_info *, | |
94 | struct bfd_link_hash_entry *, | |
95 | unsigned int bitsize, | |
96 | bfd *, asection *, bfd_vma)); | |
97 | static boolean constructor_callback PARAMS ((struct bfd_link_info *, | |
98 | boolean constructor, | |
99 | unsigned int bitsize, | |
100 | const char *name, | |
101 | bfd *, asection *, bfd_vma)); | |
102 | static boolean warning_callback PARAMS ((struct bfd_link_info *, | |
103 | const char *)); | |
104 | static boolean undefined_symbol PARAMS ((struct bfd_link_info *, | |
105 | const char *, bfd *, | |
106 | asection *, bfd_vma)); | |
5dad4c97 ILT |
107 | static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *, |
108 | const char *, bfd_vma, | |
109 | bfd *, asection *, bfd_vma)); | |
b9395be3 ILT |
110 | static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *, |
111 | bfd *, asection *, bfd_vma)); | |
112 | static boolean unattached_reloc PARAMS ((struct bfd_link_info *, | |
113 | const char *, bfd *, asection *, | |
114 | bfd_vma)); | |
115 | static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *, | |
116 | bfd *, asection *, bfd_vma)); | |
117 | ||
118 | static struct bfd_link_callbacks link_callbacks = | |
119 | { | |
120 | add_archive_element, | |
121 | multiple_definition, | |
122 | multiple_common, | |
123 | add_to_set, | |
124 | constructor_callback, | |
125 | warning_callback, | |
126 | undefined_symbol, | |
127 | reloc_overflow, | |
128 | reloc_dangerous, | |
129 | unattached_reloc, | |
130 | notice_ysym | |
131 | }; | |
132 | ||
133 | struct bfd_link_info link_info; | |
173a0c3d | 134 | \f |
0b2f8d2e DM |
135 | static void |
136 | remove_output () | |
137 | { | |
138 | if (output_filename) | |
139 | { | |
140 | if (output_bfd && output_bfd->iostream) | |
141 | fclose((FILE *)(output_bfd->iostream)); | |
142 | if (delete_output_file_on_failure) | |
143 | unlink (output_filename); | |
144 | } | |
145 | } | |
146 | ||
147 | /* Prevent remove_output from doing anything. | |
148 | Called after a successful link. */ | |
149 | ||
150 | static void | |
151 | preserve_output () | |
152 | { | |
153 | output_filename = NULL; | |
154 | } | |
9f629407 ILT |
155 | |
156 | int | |
2fa0b342 | 157 | main (argc, argv) |
2fa0b342 | 158 | int argc; |
9f629407 | 159 | char **argv; |
2fa0b342 DHW |
160 | { |
161 | char *emulation; | |
8594f568 | 162 | long start_time = get_run_time (); |
e47bfa63 | 163 | |
2fa0b342 | 164 | program_name = argv[0]; |
99fe4553 | 165 | |
e47bfa63 | 166 | bfd_init (); |
bfbdc80f | 167 | |
0b2f8d2e DM |
168 | atexit (remove_output); |
169 | ||
2fa0b342 | 170 | /* Initialize the data about options. */ |
7b40f2b1 | 171 | trace_files = trace_file_tries = version_printed = false; |
2fa0b342 | 172 | write_map = false; |
b9395be3 | 173 | config.build_constructors = true; |
2fa0b342 DHW |
174 | command_line.force_common_definition = false; |
175 | ||
b9395be3 ILT |
176 | link_info.callbacks = &link_callbacks; |
177 | link_info.relocateable = false; | |
178 | link_info.strip = strip_none; | |
179 | link_info.discard = discard_none; | |
180 | link_info.lprefix_len = 1; | |
181 | link_info.lprefix = "L"; | |
182 | link_info.keep_memory = true; | |
183 | link_info.input_bfds = NULL; | |
184 | link_info.create_object_symbols_section = NULL; | |
185 | link_info.hash = NULL; | |
186 | link_info.keep_hash = NULL; | |
187 | link_info.notice_hash = NULL; | |
188 | ||
e47bfa63 | 189 | ldfile_add_arch (""); |
a72f4e5f | 190 | |
2fa0b342 DHW |
191 | config.make_executable = true; |
192 | force_make_executable = false; | |
ce4d59e2 SC |
193 | config.magic_demand_paged = true; |
194 | config.text_read_only = true; | |
2fa0b342 | 195 | config.make_executable = true; |
1418c83b | 196 | |
d723cd17 | 197 | emulation = get_emulation (argc, argv); |
e47bfa63 SC |
198 | ldemul_choose_mode (emulation); |
199 | default_target = ldemul_choose_target (); | |
200 | lang_init (); | |
201 | ldemul_before_parse (); | |
2fa0b342 | 202 | lang_has_input_file = false; |
e47bfa63 | 203 | parse_args (argc, argv); |
f3739bc3 | 204 | |
973e421e ILT |
205 | /* This essentially adds another -L directory so this must be done after |
206 | the -L's in argv have been processed. */ | |
207 | set_scripts_dir (); | |
208 | ||
bbd2521f DM |
209 | if (had_script == false) |
210 | { | |
211 | /* Read the emulation's appropriate default script. */ | |
2a28d8b0 DM |
212 | int isfile; |
213 | char *s = ldemul_get_script (&isfile); | |
214 | ||
215 | if (isfile) | |
216 | { | |
217 | /* sizeof counts the terminating NUL. */ | |
218 | size_t size = strlen (s) + sizeof ("-T "); | |
0b2f8d2e | 219 | char *buf = (char *) xmalloc(size); |
2a28d8b0 DM |
220 | sprintf (buf, "-T %s", s); |
221 | parse_line (buf, 0); | |
222 | free (buf); | |
223 | } | |
224 | else | |
225 | parse_line (s, 1); | |
bbd2521f DM |
226 | } |
227 | ||
b9395be3 | 228 | if (link_info.relocateable && command_line.relax) |
29f33467 | 229 | { |
f3739bc3 | 230 | einfo ("%P%F: -relax and -r may not be used together\n"); |
29f33467 | 231 | } |
e47bfa63 | 232 | lang_final (); |
9d1fe8a4 | 233 | |
e47bfa63 SC |
234 | if (lang_has_input_file == false) |
235 | { | |
7b40f2b1 | 236 | if (version_printed) |
0b2f8d2e | 237 | xexit (0); |
973e421e | 238 | einfo ("%P%F: no input files\n"); |
870f54b2 | 239 | } |
2fa0b342 | 240 | |
bbd2521f DM |
241 | if (trace_files) |
242 | { | |
973e421e | 243 | info_msg ("%P: mode %s\n", emulation); |
bbd2521f DM |
244 | } |
245 | ||
e47bfa63 | 246 | ldemul_after_parse (); |
870f54b2 | 247 | |
9d1fe8a4 | 248 | |
e47bfa63 | 249 | if (config.map_filename) |
870f54b2 | 250 | { |
e47bfa63 | 251 | if (strcmp (config.map_filename, "-") == 0) |
2e2bf962 | 252 | { |
e47bfa63 | 253 | config.map_file = stdout; |
2e2bf962 | 254 | } |
e47bfa63 SC |
255 | else |
256 | { | |
257 | config.map_file = fopen (config.map_filename, FOPEN_WT); | |
258 | if (config.map_file == (FILE *) NULL) | |
259 | { | |
bbd2521f | 260 | einfo ("%P%F: cannot open map file %s: %E\n", |
e47bfa63 SC |
261 | config.map_filename); |
262 | } | |
263 | } | |
264 | } | |
870f54b2 | 265 | |
2e2bf962 | 266 | |
e47bfa63 | 267 | lang_process (); |
2fa0b342 | 268 | |
2fa0b342 DHW |
269 | /* Print error messages for any missing symbols, for any warning |
270 | symbols, and possibly multiple definitions */ | |
271 | ||
2fa0b342 | 272 | |
e47bfa63 SC |
273 | if (config.text_read_only) |
274 | { | |
275 | /* Look for a text section and mark the readonly attribute in it */ | |
276 | asection *found = bfd_get_section_by_name (output_bfd, ".text"); | |
277 | ||
278 | if (found != (asection *) NULL) | |
279 | { | |
280 | found->flags |= SEC_READONLY; | |
281 | } | |
3e4c643d | 282 | } |
2fa0b342 | 283 | |
b9395be3 | 284 | if (link_info.relocateable) |
f3739bc3 | 285 | output_bfd->flags &= ~EXEC_P; |
a72f4e5f | 286 | else |
f3739bc3 | 287 | output_bfd->flags |= EXEC_P; |
b257477f | 288 | |
f3739bc3 | 289 | ldwrite (); |
b257477f | 290 | |
f3739bc3 SC |
291 | /* Even if we're producing relocateable output, some non-fatal errors should |
292 | be reported in the exit status. (What non-fatal errors, if any, do we | |
293 | want to ignore for relocateable output?) */ | |
a72f4e5f | 294 | |
f3739bc3 SC |
295 | if (config.make_executable == false && force_make_executable == false) |
296 | { | |
297 | if (trace_files == true) | |
e47bfa63 | 298 | { |
973e421e | 299 | einfo ("%P: link errors found, deleting executable `%s'\n", |
f3739bc3 | 300 | output_filename); |
e47bfa63 | 301 | } |
a72f4e5f | 302 | |
f3739bc3 SC |
303 | if (output_bfd->iostream) |
304 | fclose ((FILE *) (output_bfd->iostream)); | |
a72f4e5f | 305 | |
f3739bc3 | 306 | unlink (output_filename); |
0b2f8d2e | 307 | xexit (1); |
f3739bc3 SC |
308 | } |
309 | else | |
310 | { | |
311 | bfd_close (output_bfd); | |
312 | } | |
2fa0b342 | 313 | |
b9395be3 ILT |
314 | if (config.stats) |
315 | { | |
316 | extern char **environ; | |
317 | char *lim = (char *) sbrk (0); | |
8594f568 | 318 | long run_time = get_run_time () - start_time; |
b9395be3 | 319 | |
8594f568 SS |
320 | fprintf (stderr, "%s: total time in link: %d.%06d\n", |
321 | program_name, run_time / 1000000, run_time % 1000000); | |
b9395be3 ILT |
322 | fprintf (stderr, "%s: data size %ld\n", program_name, |
323 | (long) (lim - (char *) &environ)); | |
324 | } | |
325 | ||
0b2f8d2e DM |
326 | atexit (preserve_output); |
327 | xexit (0); | |
9f629407 | 328 | return 0; |
d723cd17 DM |
329 | } |
330 | ||
331 | /* We need to find any explicitly given emulation in order to initialize the | |
332 | state that's needed by the lex&yacc argument parser (parse_args). */ | |
333 | ||
334 | static char * | |
335 | get_emulation (argc, argv) | |
336 | int argc; | |
337 | char **argv; | |
338 | { | |
339 | char *emulation; | |
340 | int i; | |
341 | ||
d723cd17 DM |
342 | emulation = (char *) getenv (EMULATION_ENVIRON); |
343 | if (emulation == NULL) | |
344 | emulation = DEFAULT_EMULATION; | |
d723cd17 DM |
345 | |
346 | for (i = 1; i < argc; i++) | |
347 | { | |
348 | if (!strncmp (argv[i], "-m", 2)) | |
349 | { | |
350 | if (argv[i][2] == '\0') | |
351 | { | |
352 | /* -m EMUL */ | |
353 | if (i < argc - 1) | |
354 | { | |
355 | emulation = argv[i + 1]; | |
356 | i++; | |
357 | } | |
358 | else | |
359 | { | |
973e421e | 360 | einfo("%P%F: missing argument to -m\n"); |
d723cd17 DM |
361 | } |
362 | } | |
973e421e ILT |
363 | else if (strcmp (argv[i], "-mips1") == 0 |
364 | || strcmp (argv[i], "-mips2") == 0 | |
365 | || strcmp (argv[i], "-mips3") == 0) | |
366 | { | |
367 | /* FIXME: The arguments -mips1, -mips2 and -mips3 are | |
368 | passed to the linker by some MIPS compilers. They | |
369 | generally tell the linker to use a slightly different | |
370 | library path. Perhaps someday these should be | |
371 | implemented as emulations; until then, we just ignore | |
372 | the arguments and hope that nobody ever creates | |
373 | emulations named ips1, ips2 or ips3. */ | |
374 | } | |
d723cd17 DM |
375 | else |
376 | { | |
377 | /* -mEMUL */ | |
378 | emulation = &argv[i][2]; | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | return emulation; | |
384 | } | |
385 | ||
386 | /* If directory DIR contains an "ldscripts" subdirectory, | |
387 | add DIR to the library search path and return true, | |
388 | else return false. */ | |
389 | ||
390 | static boolean | |
391 | check_for_scripts_dir (dir) | |
392 | char *dir; | |
393 | { | |
394 | size_t dirlen; | |
395 | char *buf; | |
396 | struct stat s; | |
397 | boolean res; | |
398 | ||
399 | dirlen = strlen (dir); | |
400 | /* sizeof counts the terminating NUL. */ | |
0b2f8d2e | 401 | buf = (char *) xmalloc (dirlen + sizeof("/ldscripts")); |
d723cd17 DM |
402 | sprintf (buf, "%s/ldscripts", dir); |
403 | ||
404 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
f4208462 | 405 | free (buf); |
d723cd17 | 406 | if (res) |
f4208462 | 407 | ldfile_add_library_path (dir); |
d723cd17 DM |
408 | return res; |
409 | } | |
410 | ||
411 | /* Set the default directory for finding script files. | |
f4208462 DM |
412 | Libraries will be searched for here too, but that's ok. |
413 | We look for the "ldscripts" directory in: | |
414 | ||
f4208462 | 415 | SCRIPTDIR (passed from Makefile) |
bbd2521f DM |
416 | the dir where this program is (for using it from the build tree) |
417 | the dir where this program is/../lib (for installing the tool suite elsewhere) */ | |
d723cd17 DM |
418 | |
419 | static void | |
420 | set_scripts_dir () | |
421 | { | |
f4208462 DM |
422 | char *end, *dir; |
423 | size_t dirlen; | |
424 | ||
d723cd17 | 425 | if (check_for_scripts_dir (SCRIPTDIR)) |
f4208462 | 426 | return; /* We've been installed normally. */ |
d723cd17 DM |
427 | |
428 | /* Look for "ldscripts" in the dir where our binary is. */ | |
429 | end = strrchr (program_name, '/'); | |
bbd2521f DM |
430 | if (end) |
431 | { | |
432 | dirlen = end - program_name; | |
433 | /* Make a copy of program_name in dir. | |
434 | Leave room for later "/../lib". */ | |
0b2f8d2e | 435 | dir = (char *) xmalloc (dirlen + 8); |
bbd2521f DM |
436 | strncpy (dir, program_name, dirlen); |
437 | dir[dirlen] = '\0'; | |
438 | } | |
439 | else | |
440 | { | |
441 | dirlen = 1; | |
0b2f8d2e | 442 | dir = (char *) xmalloc (dirlen + 8); |
bbd2521f DM |
443 | strcpy (dir, "."); |
444 | } | |
f4208462 | 445 | |
f4208462 DM |
446 | if (check_for_scripts_dir (dir)) |
447 | return; /* Don't free dir. */ | |
448 | ||
449 | /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ | |
450 | strcpy (dir + dirlen, "/../lib"); | |
451 | if (check_for_scripts_dir (dir)) | |
452 | return; | |
453 | ||
454 | free (dir); /* Well, we tried. */ | |
d723cd17 | 455 | } |
2fa0b342 | 456 | |
e47bfa63 | 457 | void |
b9395be3 ILT |
458 | add_ysym (name) |
459 | const char *name; | |
2fa0b342 | 460 | { |
b9395be3 ILT |
461 | if (link_info.notice_hash == (struct bfd_hash_table *) NULL) |
462 | { | |
463 | link_info.notice_hash = ((struct bfd_hash_table *) | |
0b2f8d2e | 464 | xmalloc (sizeof (struct bfd_hash_table))); |
b9395be3 ILT |
465 | if (! bfd_hash_table_init_n (link_info.notice_hash, |
466 | bfd_hash_newfunc, | |
467 | 61)) | |
468 | einfo ("%P%F: bfd_hash_table_init failed: %E\n"); | |
469 | } | |
470 | ||
471 | if (bfd_hash_lookup (link_info.notice_hash, name, true, true) | |
472 | == (struct bfd_hash_entry *) NULL) | |
473 | einfo ("%P%F: bfd_hash_lookup failed: %E\n"); | |
2fa0b342 | 474 | } |
e47bfa63 | 475 | |
b9395be3 | 476 | /* Handle the -retain-symbols-file option. */ |
2fa0b342 | 477 | |
2fa0b342 | 478 | void |
b9395be3 ILT |
479 | add_keepsyms_file (filename) |
480 | const char *filename; | |
2fa0b342 | 481 | { |
b9395be3 ILT |
482 | FILE *file; |
483 | char *buf; | |
484 | size_t bufsize; | |
485 | int c; | |
2fa0b342 | 486 | |
b9395be3 ILT |
487 | if (link_info.strip == strip_some) |
488 | einfo ("%X%P: error: duplicate retain-symbols-file\n"); | |
3e4c643d | 489 | |
b9395be3 ILT |
490 | file = fopen (filename, "r"); |
491 | if (file == (FILE *) NULL) | |
e47bfa63 | 492 | { |
b9395be3 ILT |
493 | bfd_error = system_call_error; |
494 | einfo ("%X%P: %s: %E", filename); | |
495 | return; | |
be1627d3 | 496 | } |
be1627d3 | 497 | |
b9395be3 | 498 | link_info.keep_hash = ((struct bfd_hash_table *) |
0b2f8d2e | 499 | xmalloc (sizeof (struct bfd_hash_table))); |
b9395be3 ILT |
500 | if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) |
501 | einfo ("%P%F: bfd_hash_table_init failed: %E\n"); | |
2fa0b342 | 502 | |
b9395be3 | 503 | bufsize = 100; |
0b2f8d2e | 504 | buf = (char *) xmalloc (bufsize); |
c611e285 | 505 | |
b9395be3 ILT |
506 | c = getc (file); |
507 | while (c != EOF) | |
29f33467 | 508 | { |
b9395be3 ILT |
509 | while (isspace (c)) |
510 | c = getc (file); | |
e47bfa63 | 511 | |
b9395be3 | 512 | if (c != EOF) |
29f33467 | 513 | { |
b9395be3 | 514 | size_t len = 0; |
8a045e50 | 515 | |
b9395be3 | 516 | while (! isspace (c) && c != EOF) |
29f33467 | 517 | { |
b9395be3 ILT |
518 | buf[len] = c; |
519 | ++len; | |
520 | if (len >= bufsize) | |
29f33467 | 521 | { |
b9395be3 | 522 | bufsize *= 2; |
0b2f8d2e | 523 | buf = xrealloc (buf, bufsize); |
29f33467 | 524 | } |
b9395be3 | 525 | c = getc (file); |
29f33467 | 526 | } |
81016051 | 527 | |
b9395be3 ILT |
528 | buf[len] = '\0'; |
529 | ||
530 | if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) | |
531 | == (struct bfd_hash_entry *) NULL) | |
532 | einfo ("%P%F: bfd_hash_lookup for insertion failed: %E"); | |
29f33467 | 533 | } |
e47bfa63 | 534 | } |
2fa0b342 | 535 | |
b9395be3 ILT |
536 | if (link_info.strip != strip_none) |
537 | einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"); | |
8a045e50 | 538 | |
b9395be3 ILT |
539 | link_info.strip = strip_some; |
540 | } | |
541 | \f | |
542 | /* Callbacks from the BFD linker routines. */ | |
2fa0b342 | 543 | |
b9395be3 ILT |
544 | /* This is called when BFD has decided to include an archive member in |
545 | a link. */ | |
2fa0b342 | 546 | |
b9395be3 ILT |
547 | /*ARGSUSED*/ |
548 | static boolean | |
549 | add_archive_element (info, abfd, name) | |
550 | struct bfd_link_info *info; | |
551 | bfd *abfd; | |
552 | const char *name; | |
2fa0b342 | 553 | { |
b9395be3 ILT |
554 | lang_input_statement_type *input; |
555 | ||
556 | input = ((lang_input_statement_type *) | |
0b2f8d2e | 557 | xmalloc ((bfd_size_type) sizeof (lang_input_statement_type))); |
b9395be3 ILT |
558 | input->filename = abfd->filename; |
559 | input->local_sym_name = abfd->filename; | |
560 | input->the_bfd = abfd; | |
561 | input->asymbols = NULL; | |
562 | input->subfiles = NULL; | |
563 | input->next = NULL; | |
564 | input->just_syms_flag = false; | |
565 | input->loaded = false; | |
566 | input->chain = NULL; | |
567 | ||
568 | /* FIXME: This is wrong. It should point to an entry for the | |
569 | archive itself. However, it doesn't seem to matter. */ | |
570 | input->superfile = NULL; | |
571 | ||
572 | /* FIXME: The following fields are not set: header.next, | |
573 | header.type, closed, passive_position, symbol_count, total_size, | |
574 | next_real_file, is_archive, search_dirs_flag, target, real, | |
575 | common_section, common_output_section, complained. This bit of | |
576 | code is from the old decode_library_subfile function. I don't | |
577 | know whether any of those fields matters. */ | |
578 | ||
579 | ldlang_add_file (input); | |
580 | ||
581 | if (write_map) | |
582 | info_msg ("%s needed due to %T\n", abfd->filename, name); | |
583 | ||
5dad4c97 ILT |
584 | if (trace_files || trace_file_tries) |
585 | info_msg ("%I\n", input); | |
586 | ||
b9395be3 ILT |
587 | return true; |
588 | } | |
2fa0b342 | 589 | |
b9395be3 ILT |
590 | /* This is called when BFD has discovered a symbol which is defined |
591 | multiple times. */ | |
2fa0b342 | 592 | |
b9395be3 ILT |
593 | /*ARGSUSED*/ |
594 | static boolean | |
595 | multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval) | |
596 | struct bfd_link_info *info; | |
597 | const char *name; | |
598 | bfd *obfd; | |
599 | asection *osec; | |
600 | bfd_vma oval; | |
601 | bfd *nbfd; | |
602 | asection *nsec; | |
603 | bfd_vma nval; | |
604 | { | |
605 | einfo ("%X%C: multiple definition of `%T'\n", | |
606 | nbfd, nsec, nval, name); | |
607 | if (obfd != (bfd *) NULL) | |
608 | einfo ("%C: first defined here\n", obfd, osec, oval); | |
609 | return true; | |
2fa0b342 DHW |
610 | } |
611 | ||
b9395be3 ILT |
612 | /* This is called when there is a definition of a common symbol, or |
613 | when a common symbol is found for a symbol that is already defined, | |
614 | or when two common symbols are found. We only do something if | |
615 | -warn-common was used. */ | |
616 | ||
617 | /*ARGSUSED*/ | |
618 | static boolean | |
619 | multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize) | |
620 | struct bfd_link_info *info; | |
621 | const char *name; | |
622 | bfd *obfd; | |
623 | enum bfd_link_hash_type otype; | |
624 | bfd_vma osize; | |
625 | bfd *nbfd; | |
626 | enum bfd_link_hash_type ntype; | |
627 | bfd_vma nsize; | |
99fe4553 | 628 | { |
b9395be3 ILT |
629 | if (! config.warn_common) |
630 | return true; | |
99fe4553 | 631 | |
b9395be3 | 632 | if (ntype == bfd_link_hash_defined) |
e47bfa63 | 633 | { |
b9395be3 ILT |
634 | ASSERT (otype == bfd_link_hash_common); |
635 | einfo ("%B: warning: definition of `%T' overriding common\n", | |
636 | nbfd, name); | |
637 | einfo ("%B: warning: common is here\n", obfd); | |
e47bfa63 | 638 | } |
b9395be3 | 639 | else if (otype == bfd_link_hash_defined) |
2fa0b342 | 640 | { |
b9395be3 ILT |
641 | ASSERT (ntype == bfd_link_hash_common); |
642 | einfo ("%B: warning: common of `%T' overridden by definition\n", | |
643 | nbfd, name); | |
644 | einfo ("%B: warning: defined here\n", obfd); | |
645 | } | |
646 | else | |
647 | { | |
648 | ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); | |
649 | if (osize > nsize) | |
2fa0b342 | 650 | { |
b9395be3 ILT |
651 | einfo ("%B: warning: common of `%T' overridden by larger common\n", |
652 | nbfd, name); | |
653 | einfo ("%B: warning: larger common is here\n", obfd); | |
2fa0b342 | 654 | } |
b9395be3 | 655 | else if (nsize > osize) |
2fa0b342 | 656 | { |
b9395be3 ILT |
657 | einfo ("%B: warning: common of `%T' overriding smaller common\n", |
658 | nbfd, name); | |
659 | einfo ("%B: warning: smaller common is here\n", obfd); | |
2fa0b342 | 660 | } |
e47bfa63 | 661 | else |
2fa0b342 | 662 | { |
b9395be3 ILT |
663 | einfo ("%B: warning: multiple common of `%T'\n", nbfd, name); |
664 | einfo ("%B: warning: previous common is here\n", obfd); | |
2fa0b342 DHW |
665 | } |
666 | } | |
667 | ||
b9395be3 | 668 | return true; |
2fa0b342 DHW |
669 | } |
670 | ||
b9395be3 ILT |
671 | /* This is called when BFD has discovered a set element. H is the |
672 | entry in the linker hash table for the set. SECTION and VALUE | |
673 | represent a value which should be added to the set. */ | |
2fa0b342 | 674 | |
b9395be3 ILT |
675 | /*ARGSUSED*/ |
676 | static boolean | |
677 | add_to_set (info, h, bitsize, abfd, section, value) | |
678 | struct bfd_link_info *info; | |
679 | struct bfd_link_hash_entry *h; | |
680 | unsigned int bitsize; | |
681 | bfd *abfd; | |
682 | asection *section; | |
683 | bfd_vma value; | |
2fa0b342 | 684 | { |
b9395be3 ILT |
685 | ldctor_add_set_entry (h, bitsize, section, value); |
686 | return true; | |
687 | } | |
1418c83b | 688 | |
b9395be3 ILT |
689 | /* This is called when BFD has discovered a constructor. This is only |
690 | called for some object file formats--those which do not handle | |
691 | constructors in some more clever fashion. This is similar to | |
692 | adding an element to a set, but less general. */ | |
2fa0b342 | 693 | |
b9395be3 ILT |
694 | static boolean |
695 | constructor_callback (info, constructor, bitsize, name, abfd, section, value) | |
696 | struct bfd_link_info *info; | |
697 | boolean constructor; | |
698 | unsigned int bitsize; | |
699 | const char *name; | |
700 | bfd *abfd; | |
701 | asection *section; | |
702 | bfd_vma value; | |
703 | { | |
704 | char *set_name; | |
705 | char *s; | |
706 | struct bfd_link_hash_entry *h; | |
707 | ||
708 | if (! config.build_constructors) | |
709 | return true; | |
710 | ||
711 | set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__"); | |
712 | s = set_name; | |
713 | if (bfd_get_symbol_leading_char (abfd) != '\0') | |
714 | *s++ = bfd_get_symbol_leading_char (abfd); | |
715 | if (constructor) | |
716 | strcpy (s, "__CTOR_LIST__"); | |
29f33467 | 717 | else |
b9395be3 | 718 | strcpy (s, "__DTOR_LIST__"); |
2fa0b342 | 719 | |
b9395be3 ILT |
720 | if (write_map) |
721 | info_msg ("Adding %s to constructor/destructor set %s\n", name, set_name); | |
e47bfa63 | 722 | |
b9395be3 ILT |
723 | h = bfd_link_hash_lookup (info->hash, set_name, true, true, true); |
724 | if (h == (struct bfd_link_hash_entry *) NULL) | |
725 | einfo ("%P%F: bfd_link_hash_lookup failed: %E"); | |
726 | if (h->type == bfd_link_hash_new) | |
e47bfa63 | 727 | { |
b9395be3 ILT |
728 | h->type = bfd_link_hash_undefined; |
729 | h->u.undef.abfd = abfd; | |
730 | /* We don't call bfd_link_add_undef to add this to the list of | |
731 | undefined symbols because we are going to define it | |
732 | ourselves. */ | |
2fa0b342 | 733 | } |
2fa0b342 | 734 | |
b9395be3 ILT |
735 | ldctor_add_set_entry (h, bitsize, section, value); |
736 | return true; | |
2fa0b342 DHW |
737 | } |
738 | ||
b9395be3 | 739 | /* This is called when there is a reference to a warning symbol. */ |
2fa0b342 | 740 | |
b9395be3 ILT |
741 | /*ARGSUSED*/ |
742 | static boolean | |
743 | warning_callback (info, warning) | |
744 | struct bfd_link_info *info; | |
745 | const char *warning; | |
2fa0b342 | 746 | { |
b9395be3 ILT |
747 | einfo ("%P: %s\n", warning); |
748 | return true; | |
2fa0b342 DHW |
749 | } |
750 | ||
b9395be3 | 751 | /* This is called when an undefined symbol is found. */ |
2fa0b342 | 752 | |
b9395be3 ILT |
753 | /*ARGSUSED*/ |
754 | static boolean | |
755 | undefined_symbol (info, name, abfd, section, address) | |
756 | struct bfd_link_info *info; | |
757 | const char *name; | |
758 | bfd *abfd; | |
759 | asection *section; | |
760 | bfd_vma address; | |
761 | { | |
762 | static char *error_name; | |
763 | static unsigned int error_count; | |
29f33467 | 764 | |
b9395be3 | 765 | #define MAX_ERRORS_IN_A_ROW 5 |
8a045e50 | 766 | |
b9395be3 ILT |
767 | /* We never print more than a reasonable number of errors in a row |
768 | for a single symbol. */ | |
769 | if (error_name != (char *) NULL | |
770 | && strcmp (name, error_name) == 0) | |
771 | ++error_count; | |
772 | else | |
e47bfa63 | 773 | { |
b9395be3 ILT |
774 | error_count = 0; |
775 | if (error_name != (char *) NULL) | |
776 | free (error_name); | |
777 | error_name = buystring (name); | |
778 | } | |
2fa0b342 | 779 | |
b9395be3 ILT |
780 | if (error_count < MAX_ERRORS_IN_A_ROW) |
781 | einfo ("%X%C: undefined reference to `%T'\n", | |
782 | abfd, section, address, name); | |
783 | else if (error_count == MAX_ERRORS_IN_A_ROW) | |
784 | einfo ("%C: more undefined references to `%T' follow\n", | |
785 | abfd, section, address, name); | |
2fa0b342 | 786 | |
b9395be3 | 787 | return true; |
2fa0b342 DHW |
788 | } |
789 | ||
b9395be3 | 790 | /* This is called when a reloc overflows. */ |
2fa0b342 | 791 | |
b9395be3 | 792 | /*ARGSUSED*/ |
9f629407 | 793 | static boolean |
5dad4c97 | 794 | reloc_overflow (info, name, reloc_name, addend, abfd, section, address) |
b9395be3 | 795 | struct bfd_link_info *info; |
5dad4c97 ILT |
796 | const char *name; |
797 | const char *reloc_name; | |
798 | bfd_vma addend; | |
b9395be3 ILT |
799 | bfd *abfd; |
800 | asection *section; | |
801 | bfd_vma address; | |
2fa0b342 | 802 | { |
5dad4c97 ILT |
803 | einfo ("%X%C: relocation truncated to fit: %s %T", abfd, section, |
804 | address, reloc_name, name); | |
805 | if (addend != 0) | |
806 | einfo ("+%v", addend); | |
807 | einfo ("\n"); | |
b9395be3 ILT |
808 | return true; |
809 | } | |
2fa0b342 | 810 | |
b9395be3 | 811 | /* This is called when a dangerous relocation is made. */ |
3e4c643d | 812 | |
b9395be3 ILT |
813 | /*ARGSUSED*/ |
814 | static boolean | |
815 | reloc_dangerous (info, message, abfd, section, address) | |
816 | struct bfd_link_info *info; | |
817 | const char *message; | |
818 | bfd *abfd; | |
819 | asection *section; | |
820 | bfd_vma address; | |
821 | { | |
822 | einfo ("%X%C: dangerous relocation: %s\n", abfd, section, address, message); | |
823 | return true; | |
824 | } | |
973e421e | 825 | |
b9395be3 ILT |
826 | /* This is called when a reloc is being generated attached to a symbol |
827 | that is not being output. */ | |
2fa0b342 | 828 | |
b9395be3 ILT |
829 | /*ARGSUSED*/ |
830 | static boolean | |
831 | unattached_reloc (info, name, abfd, section, address) | |
832 | struct bfd_link_info *info; | |
833 | const char *name; | |
834 | bfd *abfd; | |
835 | asection *section; | |
836 | bfd_vma address; | |
837 | { | |
838 | einfo ("%X%C: reloc refers to symbol `%T' which is not being output\n", | |
839 | abfd, section, address, name); | |
840 | return true; | |
2fa0b342 | 841 | } |
8a045e50 | 842 | |
b9395be3 ILT |
843 | /* This is called when a symbol in notice_hash is found. Symbols are |
844 | put in notice_hash using the -y option. */ | |
845 | ||
846 | /*ARGSUSED*/ | |
847 | static boolean | |
848 | notice_ysym (info, name, abfd, section, value) | |
849 | struct bfd_link_info *info; | |
850 | const char *name; | |
851 | bfd *abfd; | |
852 | asection *section; | |
853 | bfd_vma value; | |
8a045e50 | 854 | { |
b9395be3 ILT |
855 | einfo ("%B: %s %s\n", abfd, |
856 | section != &bfd_und_section ? "definition of" : "reference to", | |
857 | name); | |
858 | return true; | |
8a045e50 | 859 | } |