]>
Commit | Line | Data |
---|---|---|
2fa0b342 | 1 | /* Copyright (C) 1991 Free Software Foundation, Inc. |
ce4d59e2 | 2 | Written by Steve Chamberlain [email protected] |
e47bfa63 | 3 | |
2fa0b342 DHW |
4 | This file is part of GLD, the Gnu Linker. |
5 | ||
6 | GLD is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
ce4d59e2 | 8 | the Free Software Foundation; either version 2, or (at your option) |
2fa0b342 DHW |
9 | any later version. |
10 | ||
11 | GLD is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GLD; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
2fa0b342 | 20 | |
2fa0b342 | 21 | #include "bfd.h" |
f177a611 | 22 | #include "sysdep.h" |
2fa0b342 DHW |
23 | |
24 | #include "config.h" | |
25 | #include "ld.h" | |
26 | #include "ldmain.h" | |
27 | #include "ldmisc.h" | |
28 | #include "ldwrite.h" | |
29 | #include "ldgram.h" | |
30 | #include "ldsym.h" | |
31 | #include "ldlang.h" | |
8c514453 | 32 | #include "ldemul.h" |
2fa0b342 DHW |
33 | #include "ldlex.h" |
34 | #include "ldfile.h" | |
c611e285 SC |
35 | #include "ldindr.h" |
36 | #include "ldwarn.h" | |
37 | #include "ldctor.h" | |
9d1fe8a4 SC |
38 | #include "lderror.h" |
39 | ||
2fa0b342 DHW |
40 | /* IMPORTS */ |
41 | extern boolean lang_has_input_file; | |
1418c83b | 42 | extern boolean trace_files; |
e47bfa63 | 43 | |
2fa0b342 DHW |
44 | /* EXPORTS */ |
45 | ||
46 | char *default_target; | |
47 | char *output_filename = "a.out"; | |
e47bfa63 | 48 | |
2fa0b342 DHW |
49 | /* Name this program was invoked by. */ |
50 | char *program_name; | |
51 | ||
52 | /* The file that we're creating */ | |
b6316534 | 53 | bfd *output_bfd = 0; |
2fa0b342 DHW |
54 | |
55 | extern boolean option_v; | |
56 | ||
8a045e50 ILT |
57 | /* set if -y on the command line */ |
58 | int had_y; | |
59 | ||
2fa0b342 DHW |
60 | /* The local symbol prefix */ |
61 | char lprefix = 'L'; | |
62 | ||
63 | /* Count the number of global symbols multiply defined. */ | |
64 | int multiple_def_count; | |
65 | ||
2fa0b342 DHW |
66 | /* Count the number of symbols defined through common declarations. |
67 | This count is referenced in symdef_library, linear_library, and | |
68 | modified by enter_global_ref. | |
69 | ||
70 | It is incremented when a symbol is created as a common, and | |
71 | decremented when the common declaration is overridden | |
72 | ||
73 | Another way of thinking of it is that this is a count of | |
3e4c643d | 74 | all ldsym_types with a ->scoms field */ |
2fa0b342 | 75 | |
3e4c643d | 76 | unsigned int commons_pending; |
2fa0b342 | 77 | |
e47bfa63 | 78 | /* Count the number of global symbols referenced and not defined. |
3e4c643d | 79 | common symbols are not included in this count. */ |
2fa0b342 DHW |
80 | |
81 | unsigned int undefined_global_sym_count; | |
82 | ||
2fa0b342 DHW |
83 | /* Count the number of warning symbols encountered. */ |
84 | int warning_count; | |
85 | ||
86 | /* have we had a load script ? */ | |
87 | extern boolean had_script; | |
88 | ||
2fa0b342 DHW |
89 | /* Nonzero means print names of input files as processed. */ |
90 | boolean trace_files; | |
91 | ||
2fa0b342 DHW |
92 | /* 1 => write load map. */ |
93 | boolean write_map; | |
94 | ||
95 | ||
96 | int unix_relocate; | |
e47bfa63 | 97 | |
81016051 SC |
98 | #ifdef GNU960 |
99 | /* Indicates whether output file will be b.out (default) or coff */ | |
f177a611 | 100 | enum target_flavour output_flavor = BFD_BOUT_FORMAT; |
e47bfa63 | 101 | |
81016051 | 102 | #endif |
2fa0b342 | 103 | |
2fa0b342 DHW |
104 | /* Force the make_executable to be output, even if there are non-fatal |
105 | errors */ | |
106 | boolean force_make_executable; | |
107 | ||
2fa0b342 DHW |
108 | /* A count of the total number of local symbols ever seen - by adding |
109 | the symbol_count field of each newly read afile.*/ | |
110 | ||
2fa0b342 DHW |
111 | unsigned int total_symbols_seen; |
112 | ||
113 | /* A count of the number of read files - the same as the number of elements | |
114 | in file_chain | |
115 | */ | |
116 | unsigned int total_files_seen; | |
117 | ||
2fa0b342 DHW |
118 | /* IMPORTS */ |
119 | args_type command_line; | |
120 | ld_config_type config; | |
9d1fe8a4 | 121 | void |
2fa0b342 DHW |
122 | main (argc, argv) |
123 | char **argv; | |
124 | int argc; | |
125 | { | |
126 | char *emulation; | |
e47bfa63 | 127 | |
2fa0b342 DHW |
128 | program_name = argv[0]; |
129 | output_filename = "a.out"; | |
99fe4553 | 130 | |
e47bfa63 | 131 | bfd_init (); |
bfbdc80f | 132 | |
99fe4553 | 133 | #ifdef GNU960 |
e47bfa63 SC |
134 | { |
135 | int i; | |
136 | ||
137 | check_v960 (argc, argv); | |
138 | emulation = "gld960"; | |
139 | for (i = 1; i < argc; i++) | |
140 | { | |
141 | if (!strcmp (argv[i], "-Fcoff")) | |
142 | { | |
143 | emulation = "lnk960"; | |
144 | output_flavor = BFD_COFF_FORMAT; | |
145 | break; | |
146 | } | |
147 | } | |
148 | } | |
81016051 | 149 | #else |
e47bfa63 | 150 | emulation = (char *) getenv (EMULATION_ENVIRON); |
99fe4553 SC |
151 | #endif |
152 | ||
2fa0b342 | 153 | /* Initialize the data about options. */ |
76855794 | 154 | |
9d1fe8a4 | 155 | |
2fa0b342 | 156 | trace_files = false; |
2fa0b342 DHW |
157 | write_map = false; |
158 | config.relocateable_output = false; | |
159 | unix_relocate = 0; | |
160 | command_line.force_common_definition = false; | |
161 | ||
e47bfa63 SC |
162 | init_bfd_error_vector (); |
163 | ldsym_init (); | |
164 | ldfile_add_arch (""); | |
a72f4e5f | 165 | |
2fa0b342 DHW |
166 | config.make_executable = true; |
167 | force_make_executable = false; | |
168 | ||
169 | ||
170 | /* Initialize the cumulative counts of symbols. */ | |
171 | undefined_global_sym_count = 0; | |
172 | warning_count = 0; | |
173 | multiple_def_count = 0; | |
174 | commons_pending = 0; | |
175 | ||
ce4d59e2 SC |
176 | config.magic_demand_paged = true; |
177 | config.text_read_only = true; | |
2fa0b342 | 178 | config.make_executable = true; |
e47bfa63 SC |
179 | if (emulation == (char *) NULL) |
180 | { | |
181 | emulation = DEFAULT_EMULATION; | |
870f54b2 | 182 | } |
1418c83b | 183 | |
e47bfa63 SC |
184 | ldemul_choose_mode (emulation); |
185 | default_target = ldemul_choose_target (); | |
186 | lang_init (); | |
187 | ldemul_before_parse (); | |
2fa0b342 | 188 | lang_has_input_file = false; |
e47bfa63 | 189 | parse_args (argc, argv); |
f3739bc3 | 190 | |
29f33467 SC |
191 | if (config.relocateable_output && command_line.relax) |
192 | { | |
f3739bc3 | 193 | einfo ("%P%F: -relax and -r may not be used together\n"); |
29f33467 | 194 | } |
e47bfa63 | 195 | lang_final (); |
9d1fe8a4 | 196 | |
e47bfa63 SC |
197 | if (trace_files) |
198 | { | |
e47bfa63 | 199 | info ("%P: mode %s\n", emulation); |
870f54b2 | 200 | } |
e47bfa63 SC |
201 | if (lang_has_input_file == false) |
202 | { | |
203 | einfo ("%P%F: No input files\n"); | |
870f54b2 | 204 | } |
2fa0b342 | 205 | |
e47bfa63 | 206 | ldemul_after_parse (); |
870f54b2 | 207 | |
9d1fe8a4 | 208 | |
e47bfa63 | 209 | if (config.map_filename) |
870f54b2 | 210 | { |
e47bfa63 | 211 | if (strcmp (config.map_filename, "-") == 0) |
2e2bf962 | 212 | { |
e47bfa63 | 213 | config.map_file = stdout; |
2e2bf962 | 214 | } |
e47bfa63 SC |
215 | else |
216 | { | |
217 | config.map_file = fopen (config.map_filename, FOPEN_WT); | |
218 | if (config.map_file == (FILE *) NULL) | |
219 | { | |
220 | einfo ("%P%F: can't open map file %s\n", | |
221 | config.map_filename); | |
222 | } | |
223 | } | |
224 | } | |
870f54b2 | 225 | |
2e2bf962 | 226 | |
e47bfa63 | 227 | lang_process (); |
2fa0b342 | 228 | |
2fa0b342 DHW |
229 | /* Print error messages for any missing symbols, for any warning |
230 | symbols, and possibly multiple definitions */ | |
231 | ||
2fa0b342 | 232 | |
e47bfa63 SC |
233 | if (config.text_read_only) |
234 | { | |
235 | /* Look for a text section and mark the readonly attribute in it */ | |
236 | asection *found = bfd_get_section_by_name (output_bfd, ".text"); | |
237 | ||
238 | if (found != (asection *) NULL) | |
239 | { | |
240 | found->flags |= SEC_READONLY; | |
241 | } | |
3e4c643d | 242 | } |
2fa0b342 | 243 | |
e47bfa63 | 244 | if (config.relocateable_output) |
f3739bc3 | 245 | output_bfd->flags &= ~EXEC_P; |
a72f4e5f | 246 | else |
f3739bc3 | 247 | output_bfd->flags |= EXEC_P; |
b257477f | 248 | |
f3739bc3 | 249 | ldwrite (); |
b257477f | 250 | |
f3739bc3 SC |
251 | /* Even if we're producing relocateable output, some non-fatal errors should |
252 | be reported in the exit status. (What non-fatal errors, if any, do we | |
253 | want to ignore for relocateable output?) */ | |
a72f4e5f | 254 | |
f3739bc3 SC |
255 | if (config.make_executable == false && force_make_executable == false) |
256 | { | |
257 | if (trace_files == true) | |
e47bfa63 | 258 | { |
f3739bc3 SC |
259 | einfo ("%P: Link errors found, deleting executable `%s'\n", |
260 | output_filename); | |
e47bfa63 | 261 | } |
a72f4e5f | 262 | |
f3739bc3 SC |
263 | if (output_bfd->iostream) |
264 | fclose ((FILE *) (output_bfd->iostream)); | |
a72f4e5f | 265 | |
f3739bc3 SC |
266 | unlink (output_filename); |
267 | exit (1); | |
268 | } | |
269 | else | |
270 | { | |
271 | bfd_close (output_bfd); | |
272 | } | |
2fa0b342 | 273 | |
e47bfa63 | 274 | exit (0); |
870f54b2 | 275 | } /* main() */ |
2fa0b342 | 276 | |
2fa0b342 DHW |
277 | void |
278 | Q_read_entry_symbols (desc, entry) | |
279 | bfd *desc; | |
280 | struct lang_input_statement_struct *entry; | |
281 | { | |
e47bfa63 SC |
282 | if (entry->asymbols == (asymbol **) NULL) |
283 | { | |
284 | bfd_size_type table_size = get_symtab_upper_bound (desc); | |
2fa0b342 | 285 | |
e47bfa63 SC |
286 | entry->asymbols = (asymbol **) ldmalloc (table_size); |
287 | entry->symbol_count = bfd_canonicalize_symtab (desc, entry->asymbols); | |
288 | } | |
289 | } | |
2fa0b342 DHW |
290 | |
291 | /* | |
e47bfa63 | 292 | * turn this item into a reference |
2fa0b342 | 293 | */ |
e47bfa63 SC |
294 | void |
295 | refize (sp, nlist_p) | |
296 | ldsym_type *sp; | |
297 | asymbol **nlist_p; | |
2fa0b342 DHW |
298 | { |
299 | asymbol *sym = *nlist_p; | |
e47bfa63 | 300 | |
2fa0b342 | 301 | sym->value = 0; |
c611e285 SC |
302 | sym->flags = 0; |
303 | sym->section = &bfd_und_section; | |
e47bfa63 | 304 | sym->udata = (PTR) (sp->srefs_chain); |
2fa0b342 DHW |
305 | sp->srefs_chain = nlist_p; |
306 | } | |
e47bfa63 | 307 | |
2fa0b342 DHW |
308 | /* |
309 | This function is called for each name which is seen which has a global | |
310 | scope. It enters the name into the global symbol table in the correct | |
311 | symbol on the correct chain. Remember that each ldsym_type has three | |
312 | chains attatched, one of all definitions of a symbol, one of all | |
313 | references of a symbol and one of all common definitions of a symbol. | |
314 | ||
315 | When the function is over, the supplied is left connected to the bfd | |
316 | to which is was born, with its udata field pointing to the next member | |
317 | on the chain in which it has been inserted. | |
318 | ||
319 | A certain amount of jigery pokery is necessary since commons come | |
320 | along and upset things, we only keep one item in the common chain; the | |
321 | one with the biggest size seen sofar. When another common comes along | |
322 | it either bumps the previous definition into the ref chain, since it | |
323 | is bigger, or gets turned into a ref on the spot since the one on the | |
324 | common chain is already bigger. If a real definition comes along then | |
325 | the common gets bumped off anyway. | |
326 | ||
327 | Whilst all this is going on we keep a count of the number of multiple | |
328 | definitions seen, undefined global symbols and pending commons. | |
329 | */ | |
330 | ||
c611e285 | 331 | extern boolean relaxing; |
2fa0b342 DHW |
332 | |
333 | void | |
e47bfa63 SC |
334 | DEFUN (Q_enter_global_ref, (nlist_p, name), |
335 | asymbol ** nlist_p AND /* pointer into symbol table from incoming bfd */ | |
336 | CONST char *name /* name of symbol in linker table */ ) | |
2fa0b342 DHW |
337 | { |
338 | asymbol *sym = *nlist_p; | |
e47bfa63 | 339 | ldsym_type *sp; |
b257477f SC |
340 | |
341 | /* Lookup the name from the incoming bfd's symbol table in the | |
342 | linker's global symbol table */ | |
2fa0b342 | 343 | |
2fa0b342 | 344 | |
b257477f | 345 | flagword this_symbol_flags = sym->flags; |
e47bfa63 | 346 | |
b257477f | 347 | sp = ldsym_get (name); |
8a045e50 ILT |
348 | |
349 | ||
29f33467 | 350 | /* If this symbol already has udata, it means that something strange |
be1627d3 | 351 | has happened. |
29f33467 | 352 | |
be1627d3 SC |
353 | The strange thing is that we've had an undefined symbol resolved by |
354 | an alias, but the thing the alias defined wasn't in the file. So | |
355 | the symbol got a udata entry, but the file wasn't loaded. Then | |
356 | later on the file was loaded, but we don't need to do this | |
357 | processing again */ | |
2fa0b342 | 358 | |
be1627d3 SC |
359 | |
360 | if (sym->udata) | |
29f33467 | 361 | return; |
2fa0b342 | 362 | |
3e4c643d | 363 | |
e47bfa63 SC |
364 | if (flag_is_constructor (this_symbol_flags)) |
365 | { | |
29f33467 SC |
366 | /* Add this constructor to the list we keep */ |
367 | ldlang_add_constructor (sp); | |
368 | /* Turn any commons into refs */ | |
369 | if (sp->scoms_chain != (asymbol **) NULL) | |
370 | { | |
371 | refize (sp, sp->scoms_chain); | |
372 | sp->scoms_chain = 0; | |
373 | } | |
3e4c643d | 374 | |
81016051 | 375 | |
29f33467 | 376 | } |
e47bfa63 SC |
377 | else |
378 | { | |
29f33467 SC |
379 | if (bfd_is_com_section (sym->section)) |
380 | { | |
381 | /* If we have a definition of this symbol already then | |
be1627d3 SC |
382 | this common turns into a reference. Also we only |
383 | ever point to the largest common, so if we | |
384 | have a common, but it's bigger that the new symbol | |
385 | the turn this into a reference too. */ | |
29f33467 SC |
386 | if (sp->sdefs_chain) |
387 | { | |
388 | /* This is a common symbol, but we already have a definition | |
be1627d3 SC |
389 | for it, so just link it into the ref chain as if |
390 | it were a reference */ | |
29f33467 SC |
391 | refize (sp, nlist_p); |
392 | } | |
393 | else if (sp->scoms_chain) | |
394 | { | |
395 | /* If we have a previous common, keep only the biggest */ | |
396 | if ((*(sp->scoms_chain))->value > sym->value) | |
397 | { | |
398 | /* other common is bigger, throw this one away */ | |
399 | refize (sp, nlist_p); | |
400 | } | |
401 | else if (sp->scoms_chain != nlist_p) | |
402 | { | |
403 | /* other common is smaller, throw that away */ | |
404 | refize (sp, sp->scoms_chain); | |
405 | sp->scoms_chain = nlist_p; | |
406 | } | |
407 | } | |
408 | else | |
409 | { | |
410 | /* This is the first time we've seen a common, so remember it | |
be1627d3 SC |
411 | - if it was undefined before, we know it's defined now. If |
412 | the symbol has been marked as really being a constructor, | |
413 | then treat this as a ref | |
414 | */ | |
29f33467 SC |
415 | if (sp->flags & SYM_CONSTRUCTOR) |
416 | { | |
417 | /* Turn this into a ref */ | |
418 | refize (sp, nlist_p); | |
419 | } | |
420 | else | |
421 | { | |
422 | /* treat like a common */ | |
423 | if (sp->srefs_chain) | |
424 | undefined_global_sym_count--; | |
be1627d3 | 425 | |
29f33467 SC |
426 | commons_pending++; |
427 | sp->scoms_chain = nlist_p; | |
428 | } | |
429 | } | |
be1627d3 | 430 | } |
2fa0b342 | 431 | |
29f33467 SC |
432 | else if (sym->section != &bfd_und_section) |
433 | { | |
434 | /* This is the definition of a symbol, add to def chain */ | |
435 | if (sp->sdefs_chain && (*(sp->sdefs_chain))->section != sym->section) | |
436 | { | |
437 | /* Multiple definition */ | |
438 | asymbol *sy = *(sp->sdefs_chain); | |
439 | lang_input_statement_type *stat = | |
440 | (lang_input_statement_type *) bfd_asymbol_bfd (sy)->usrdata; | |
441 | lang_input_statement_type *stat1 = | |
442 | (lang_input_statement_type *) bfd_asymbol_bfd (sym)->usrdata; | |
443 | asymbol **stat1_symbols = stat1 ? stat1->asymbols : 0; | |
444 | asymbol **stat_symbols = stat ? stat->asymbols : 0; | |
445 | ||
446 | multiple_def_count++; | |
447 | einfo ("%X%C: multiple definition of `%T'\n", | |
448 | bfd_asymbol_bfd (sym), sym->section, stat1_symbols, sym->value, sym); | |
449 | ||
450 | einfo ("%X%C: first seen here\n", | |
451 | bfd_asymbol_bfd (sy), sy->section, stat_symbols, sy->value); | |
452 | } | |
453 | else | |
454 | { | |
455 | sym->udata = (PTR) (sp->sdefs_chain); | |
456 | sp->sdefs_chain = nlist_p; | |
457 | } | |
458 | /* A definition overrides a common symbol */ | |
459 | if (sp->scoms_chain) | |
460 | { | |
461 | refize (sp, sp->scoms_chain); | |
462 | sp->scoms_chain = 0; | |
463 | commons_pending--; | |
464 | } | |
465 | else if (sp->srefs_chain && relaxing == false) | |
466 | { | |
467 | /* If previously was undefined, then remember as defined */ | |
468 | undefined_global_sym_count--; | |
469 | } | |
470 | } | |
be1627d3 | 471 | else |
29f33467 SC |
472 | { |
473 | if (sp->scoms_chain == (asymbol **) NULL | |
474 | && sp->srefs_chain == (asymbol **) NULL | |
475 | && sp->sdefs_chain == (asymbol **) NULL) | |
476 | { | |
477 | /* And it's the first time we've seen it */ | |
478 | undefined_global_sym_count++; | |
be1627d3 | 479 | |
29f33467 | 480 | } |
be1627d3 | 481 | |
29f33467 SC |
482 | refize (sp, nlist_p); |
483 | } | |
be1627d3 | 484 | } |
be1627d3 | 485 | |
e47bfa63 SC |
486 | ASSERT (sp->sdefs_chain == 0 || sp->scoms_chain == 0); |
487 | ASSERT (sp->scoms_chain == 0 || (*(sp->scoms_chain))->udata == 0); | |
2fa0b342 DHW |
488 | |
489 | ||
490 | } | |
491 | ||
492 | static void | |
493 | Q_enter_file_symbols (entry) | |
e47bfa63 | 494 | lang_input_statement_type *entry; |
2fa0b342 | 495 | { |
e47bfa63 | 496 | asymbol **q; |
c611e285 | 497 | |
2fa0b342 | 498 | entry->common_section = |
29f33467 SC |
499 | bfd_make_section_old_way (entry->the_bfd, "COMMON"); |
500 | entry->common_section->flags = SEC_NEVER_LOAD; | |
e47bfa63 | 501 | ldlang_add_file (entry); |
2fa0b342 | 502 | |
fe563ffe | 503 | |
e47bfa63 | 504 | if (trace_files || option_v) |
29f33467 SC |
505 | { |
506 | info ("%I\n", entry); | |
507 | } | |
e47bfa63 SC |
508 | |
509 | total_symbols_seen += entry->symbol_count; | |
510 | total_files_seen++; | |
511 | if (entry->symbol_count) | |
fe563ffe | 512 | { |
29f33467 SC |
513 | for (q = entry->asymbols; *q; q++) |
514 | { | |
515 | asymbol *p = *q; | |
8a045e50 | 516 | |
29f33467 SC |
517 | if (had_y && p->name) |
518 | { | |
519 | /* look up the symbol anyway to see if the trace bit was | |
8a045e50 | 520 | set */ |
29f33467 SC |
521 | ldsym_type *s = ldsym_get (p->name); |
522 | if (s->flags & SYM_Y) | |
523 | { | |
524 | einfo ("%B: %s %T\n", entry->the_bfd, | |
525 | p->section == &bfd_und_section ? "reference to" : "definition of ", | |
526 | p); | |
527 | } | |
528 | } | |
81016051 | 529 | |
29f33467 SC |
530 | if (p->section == &bfd_ind_section) |
531 | { | |
532 | add_indirect (q); | |
533 | } | |
534 | else if (p->flags & BSF_WARNING) | |
535 | { | |
536 | add_warning (p); | |
537 | } | |
538 | else if (p->section == &bfd_und_section | |
539 | || (p->flags & BSF_GLOBAL) | |
540 | || bfd_is_com_section (p->section) | |
541 | || (p->flags & BSF_CONSTRUCTOR)) | |
c611e285 | 542 | |
29f33467 | 543 | { |
2fa0b342 | 544 | |
29f33467 | 545 | asymbol *p = *q; |
2fa0b342 | 546 | |
29f33467 SC |
547 | if (p->flags & BSF_INDIRECT) |
548 | { | |
549 | add_indirect (q); | |
550 | } | |
551 | else if (p->flags & BSF_WARNING) | |
552 | { | |
553 | add_warning (p); | |
554 | } | |
555 | else if (p->section == &bfd_und_section | |
556 | || (p->flags & BSF_GLOBAL) | |
557 | || bfd_is_com_section (p->section) | |
558 | || (p->flags & BSF_CONSTRUCTOR)) | |
559 | { | |
560 | Q_enter_global_ref (q, p->name); | |
561 | } | |
8a045e50 | 562 | |
29f33467 | 563 | } |
8a045e50 | 564 | |
29f33467 | 565 | } |
e47bfa63 SC |
566 | } |
567 | } | |
2fa0b342 | 568 | |
8a045e50 | 569 | |
2fa0b342 DHW |
570 | /* Searching libraries */ |
571 | ||
572 | struct lang_input_statement_struct *decode_library_subfile (); | |
573 | void linear_library (), symdef_library (); | |
574 | ||
575 | /* Search the library ENTRY, already open on descriptor DESC. | |
576 | This means deciding which library members to load, | |
577 | making a chain of `struct lang_input_statement_struct' for those members, | |
578 | and entering their global symbols in the hash table. */ | |
579 | ||
580 | void | |
581 | search_library (entry) | |
582 | struct lang_input_statement_struct *entry; | |
583 | { | |
584 | ||
585 | /* No need to load a library if no undefined symbols */ | |
e47bfa63 SC |
586 | if (!undefined_global_sym_count) |
587 | return; | |
2fa0b342 | 588 | |
e47bfa63 | 589 | if (bfd_has_map (entry->the_bfd)) |
2fa0b342 DHW |
590 | symdef_library (entry); |
591 | else | |
592 | linear_library (entry); | |
593 | ||
594 | } | |
595 | ||
99fe4553 SC |
596 | #ifdef GNU960 |
597 | static | |
29f33467 | 598 | boolean |
99fe4553 | 599 | gnu960_check_format (abfd, format) |
e47bfa63 SC |
600 | bfd *abfd; |
601 | bfd_format format; | |
99fe4553 SC |
602 | { |
603 | boolean retval; | |
604 | ||
e47bfa63 SC |
605 | if ((bfd_check_format (abfd, format) == true) |
606 | && (abfd->xvec->flavour == output_flavor)) | |
607 | { | |
608 | return true; | |
609 | } | |
81016051 SC |
610 | |
611 | ||
99fe4553 SC |
612 | return false; |
613 | } | |
e47bfa63 | 614 | |
99fe4553 SC |
615 | #endif |
616 | ||
2fa0b342 | 617 | void |
1418c83b | 618 | ldmain_open_file_read_symbol (entry) |
e47bfa63 | 619 | struct lang_input_statement_struct *entry; |
2fa0b342 | 620 | { |
e47bfa63 SC |
621 | if (entry->asymbols == (asymbol **) NULL |
622 | && entry->real == true | |
623 | && entry->filename != (char *) NULL) | |
2fa0b342 DHW |
624 | { |
625 | ldfile_open_file (entry); | |
626 | ||
ce4d59e2 | 627 | |
99fe4553 | 628 | #ifdef GNU960 |
e47bfa63 | 629 | if (gnu960_check_format (entry->the_bfd, bfd_object)) |
99fe4553 | 630 | #else |
e47bfa63 | 631 | if (bfd_check_format (entry->the_bfd, bfd_object)) |
99fe4553 | 632 | #endif |
2fa0b342 | 633 | { |
e47bfa63 | 634 | entry->the_bfd->usrdata = (PTR) entry; |
2fa0b342 DHW |
635 | |
636 | ||
637 | Q_read_entry_symbols (entry->the_bfd, entry); | |
ce4d59e2 SC |
638 | |
639 | /* look through the sections in the file and see if any of them | |
640 | are constructors */ | |
641 | ldlang_check_for_constructors (entry); | |
642 | ||
2fa0b342 DHW |
643 | Q_enter_file_symbols (entry); |
644 | } | |
99fe4553 | 645 | #ifdef GNU960 |
e47bfa63 | 646 | else if (gnu960_check_format (entry->the_bfd, bfd_archive)) |
99fe4553 | 647 | #else |
e47bfa63 | 648 | else if (bfd_check_format (entry->the_bfd, bfd_archive)) |
99fe4553 | 649 | #endif |
2fa0b342 | 650 | { |
e47bfa63 | 651 | entry->the_bfd->usrdata = (PTR) entry; |
2fa0b342 | 652 | |
e47bfa63 | 653 | entry->subfiles = (lang_input_statement_type *) NULL; |
2fa0b342 DHW |
654 | search_library (entry); |
655 | } | |
e47bfa63 | 656 | else |
2fa0b342 | 657 | { |
e47bfa63 SC |
658 | einfo ("%F%B: malformed input file (not rel or archive) \n", |
659 | entry->the_bfd); | |
2fa0b342 DHW |
660 | } |
661 | } | |
662 | ||
663 | } | |
664 | ||
2fa0b342 DHW |
665 | /* Construct and return a lang_input_statement_struct for a library member. |
666 | The library's lang_input_statement_struct is library_entry, | |
667 | and the library is open on DESC. | |
668 | SUBFILE_OFFSET is the byte index in the library of this member's header. | |
669 | We store the length of the member into *LENGTH_LOC. */ | |
670 | ||
671 | lang_input_statement_type * | |
672 | decode_library_subfile (library_entry, subfile_offset) | |
673 | struct lang_input_statement_struct *library_entry; | |
674 | bfd *subfile_offset; | |
675 | { | |
676 | register struct lang_input_statement_struct *subentry; | |
e47bfa63 | 677 | |
1418c83b | 678 | |
8a045e50 ILT |
679 | /* First, check if we already have a loaded |
680 | lang_input_statement_struct for this library subfile. If so, | |
681 | just return it. Otherwise, allocate some space and build a new one. */ | |
2fa0b342 | 682 | |
29f33467 SC |
683 | if (subfile_offset->usrdata |
684 | && ((struct lang_input_statement_struct *) subfile_offset->usrdata)-> | |
685 | loaded == true) | |
686 | { | |
687 | subentry = (struct lang_input_statement_struct *) subfile_offset->usrdata; | |
688 | } | |
689 | else | |
690 | { | |
691 | subentry = | |
692 | (struct lang_input_statement_struct *) | |
693 | ldmalloc ((bfd_size_type) (sizeof (struct lang_input_statement_struct))); | |
694 | ||
695 | subentry->filename = subfile_offset->filename; | |
696 | subentry->local_sym_name = subfile_offset->filename; | |
697 | subentry->asymbols = 0; | |
698 | subentry->the_bfd = subfile_offset; | |
699 | subentry->subfiles = 0; | |
700 | subentry->next = 0; | |
701 | subentry->superfile = library_entry; | |
702 | subentry->is_archive = false; | |
703 | ||
704 | subentry->just_syms_flag = false; | |
705 | subentry->loaded = false; | |
706 | subentry->chain = 0; | |
707 | } | |
2fa0b342 DHW |
708 | return subentry; |
709 | } | |
710 | ||
e47bfa63 | 711 | boolean subfile_wanted_p (); |
2fa0b342 | 712 | void |
e47bfa63 SC |
713 | clear_syms (entry, offset) |
714 | struct lang_input_statement_struct *entry; | |
715 | file_ptr offset; | |
2fa0b342 DHW |
716 | { |
717 | carsym *car; | |
e47bfa63 SC |
718 | unsigned long indx = bfd_get_next_mapent (entry->the_bfd, |
719 | BFD_NO_MORE_SYMBOLS, | |
720 | &car); | |
721 | ||
722 | while (indx != BFD_NO_MORE_SYMBOLS) | |
723 | { | |
724 | if (car->file_offset == offset) | |
725 | { | |
726 | car->name = 0; | |
727 | } | |
728 | indx = bfd_get_next_mapent (entry->the_bfd, indx, &car); | |
2fa0b342 | 729 | } |
2fa0b342 DHW |
730 | |
731 | } | |
732 | ||
733 | /* Search a library that has a map | |
734 | */ | |
735 | void | |
736 | symdef_library (entry) | |
737 | struct lang_input_statement_struct *entry; | |
738 | ||
739 | { | |
740 | register struct lang_input_statement_struct *prev = 0; | |
741 | ||
742 | boolean not_finished = true; | |
743 | ||
2fa0b342 | 744 | while (not_finished == true) |
e47bfa63 SC |
745 | { |
746 | carsym *exported_library_name; | |
747 | bfd *prev_archive_member_bfd = 0; | |
2fa0b342 | 748 | |
e47bfa63 SC |
749 | int idx = bfd_get_next_mapent (entry->the_bfd, |
750 | BFD_NO_MORE_SYMBOLS, | |
751 | &exported_library_name); | |
2fa0b342 | 752 | |
e47bfa63 | 753 | not_finished = false; |
2fa0b342 | 754 | |
e47bfa63 SC |
755 | while (idx != BFD_NO_MORE_SYMBOLS && undefined_global_sym_count) |
756 | { | |
2fa0b342 | 757 | |
e47bfa63 SC |
758 | if (exported_library_name->name) |
759 | { | |
2fa0b342 | 760 | |
e47bfa63 | 761 | ldsym_type *sp = ldsym_get_soft (exported_library_name->name); |
3e4c643d | 762 | |
e47bfa63 SC |
763 | /* If we find a symbol that appears to be needed, think carefully |
764 | about the archive member that the symbol is in. */ | |
765 | /* So - if it exists, and is referenced somewhere and is | |
766 | undefined or */ | |
767 | if (sp && sp->srefs_chain && !sp->sdefs_chain) | |
768 | { | |
769 | bfd *archive_member_bfd = bfd_get_elt_at_index (entry->the_bfd, idx); | |
770 | struct lang_input_statement_struct *archive_member_lang_input_statement_struct; | |
2fa0b342 | 771 | |
99fe4553 | 772 | #ifdef GNU960 |
e47bfa63 | 773 | if (archive_member_bfd && gnu960_check_format (archive_member_bfd, bfd_object)) |
99fe4553 | 774 | #else |
e47bfa63 | 775 | if (archive_member_bfd && bfd_check_format (archive_member_bfd, bfd_object)) |
99fe4553 | 776 | #endif |
e47bfa63 | 777 | { |
2fa0b342 | 778 | |
e47bfa63 SC |
779 | /* Don't think carefully about any archive member |
780 | more than once in a given pass. */ | |
781 | if (prev_archive_member_bfd != archive_member_bfd) | |
782 | { | |
2fa0b342 | 783 | |
e47bfa63 | 784 | prev_archive_member_bfd = archive_member_bfd; |
2fa0b342 | 785 | |
e47bfa63 | 786 | /* Read the symbol table of the archive member. */ |
2fa0b342 | 787 | |
e47bfa63 SC |
788 | if (archive_member_bfd->usrdata != (PTR) NULL) |
789 | { | |
2fa0b342 | 790 | |
e47bfa63 SC |
791 | archive_member_lang_input_statement_struct = (lang_input_statement_type *) archive_member_bfd->usrdata; |
792 | } | |
793 | else | |
794 | { | |
2fa0b342 | 795 | |
e47bfa63 SC |
796 | archive_member_lang_input_statement_struct = |
797 | decode_library_subfile (entry, archive_member_bfd); | |
798 | archive_member_bfd->usrdata = (PTR) archive_member_lang_input_statement_struct; | |
2fa0b342 | 799 | |
e47bfa63 | 800 | } |
2fa0b342 | 801 | |
e47bfa63 SC |
802 | if (archive_member_lang_input_statement_struct == 0) |
803 | { | |
804 | einfo ("%F%I contains invalid archive member %s\n", | |
805 | entry, sp->name); | |
806 | } | |
2fa0b342 | 807 | |
e47bfa63 SC |
808 | if (archive_member_lang_input_statement_struct->loaded == false) |
809 | { | |
2fa0b342 | 810 | |
e47bfa63 SC |
811 | Q_read_entry_symbols (archive_member_bfd, archive_member_lang_input_statement_struct); |
812 | /* Now scan the symbol table and decide whether to load. */ | |
2fa0b342 DHW |
813 | |
814 | ||
e47bfa63 | 815 | if (subfile_wanted_p (archive_member_lang_input_statement_struct) == true) |
2fa0b342 | 816 | |
e47bfa63 SC |
817 | { |
818 | /* This member is needed; load it. | |
819 | Since we are loading something on this pass, | |
820 | we must make another pass through the symdef data. */ | |
2fa0b342 | 821 | |
e47bfa63 | 822 | not_finished = true; |
2fa0b342 | 823 | |
e47bfa63 | 824 | Q_enter_file_symbols (archive_member_lang_input_statement_struct); |
2fa0b342 | 825 | |
e47bfa63 SC |
826 | if (prev) |
827 | prev->chain = archive_member_lang_input_statement_struct; | |
828 | else | |
829 | entry->subfiles = archive_member_lang_input_statement_struct; | |
2fa0b342 DHW |
830 | |
831 | ||
e47bfa63 | 832 | prev = archive_member_lang_input_statement_struct; |
2fa0b342 DHW |
833 | |
834 | ||
e47bfa63 SC |
835 | /* Clear out this member's symbols from the symdef data |
836 | so that following passes won't waste time on them. */ | |
837 | clear_syms (entry, exported_library_name->file_offset); | |
838 | archive_member_lang_input_statement_struct->loaded = true; | |
2fa0b342 | 839 | } |
e47bfa63 | 840 | } |
2fa0b342 | 841 | } |
e47bfa63 SC |
842 | } |
843 | } | |
2fa0b342 | 844 | } |
e47bfa63 SC |
845 | idx = bfd_get_next_mapent (entry->the_bfd, idx, &exported_library_name); |
846 | } | |
847 | } | |
2fa0b342 DHW |
848 | } |
849 | ||
850 | void | |
851 | linear_library (entry) | |
e47bfa63 | 852 | struct lang_input_statement_struct *entry; |
2fa0b342 DHW |
853 | { |
854 | boolean more_to_do = true; | |
855 | register struct lang_input_statement_struct *prev = 0; | |
856 | ||
e47bfa63 SC |
857 | if (entry->complained == false) |
858 | { | |
29f33467 SC |
859 | if (entry->the_bfd->xvec->flavour != bfd_target_ieee_flavour) |
860 | ||
861 | { | |
862 | /* IEEE can use table of contents, so this message is bogus */ | |
863 | einfo ("%P: library %s has bad table of contents, rerun ranlib\n", | |
864 | entry->the_bfd->filename); | |
865 | } | |
e47bfa63 | 866 | entry->complained = true; |
8a045e50 | 867 | |
e47bfa63 SC |
868 | } |
869 | while (more_to_do) | |
870 | { | |
2fa0b342 | 871 | |
e47bfa63 | 872 | bfd *archive = bfd_openr_next_archived_file (entry->the_bfd, 0); |
2fa0b342 | 873 | |
e47bfa63 SC |
874 | more_to_do = false; |
875 | while (archive) | |
876 | { | |
f3739bc3 SC |
877 | /* Don't check this file if it's already been read in |
878 | once */ | |
879 | ||
880 | if (!archive->usrdata || | |
29f33467 SC |
881 | !((lang_input_statement_type *) (archive->usrdata))->loaded) |
882 | { | |
99fe4553 | 883 | #ifdef GNU960 |
29f33467 | 884 | if (gnu960_check_format (archive, bfd_object)) |
99fe4553 | 885 | #else |
29f33467 | 886 | if (bfd_check_format (archive, bfd_object)) |
99fe4553 | 887 | #endif |
29f33467 SC |
888 | { |
889 | register struct lang_input_statement_struct *subentry; | |
890 | ||
891 | subentry = decode_library_subfile (entry, | |
892 | archive); | |
893 | ||
894 | archive->usrdata = (PTR) subentry; | |
895 | if (!subentry) | |
896 | return; | |
897 | if (subentry->loaded == false) | |
898 | { | |
899 | Q_read_entry_symbols (archive, subentry); | |
900 | ||
901 | if (subfile_wanted_p (subentry) == true) | |
902 | { | |
903 | Q_enter_file_symbols (subentry); | |
904 | ||
905 | if (prev) | |
906 | prev->chain = subentry; | |
907 | else | |
908 | entry->subfiles = subentry; | |
909 | prev = subentry; | |
910 | ||
911 | more_to_do = true; | |
912 | subentry->loaded = true; | |
913 | } | |
914 | } | |
915 | } | |
916 | } | |
e47bfa63 | 917 | archive = bfd_openr_next_archived_file (entry->the_bfd, archive); |
2fa0b342 | 918 | |
2fa0b342 | 919 | } |
2fa0b342 | 920 | |
e47bfa63 | 921 | } |
2fa0b342 DHW |
922 | } |
923 | ||
29f33467 | 924 | /* ENTRY is an entry for a file inside an archive |
e47bfa63 SC |
925 | Its symbols have been read into core, but not entered into the |
926 | linker ymbol table | |
927 | Return nonzero if we ought to load this file */ | |
2fa0b342 DHW |
928 | |
929 | boolean | |
930 | subfile_wanted_p (entry) | |
e47bfa63 | 931 | struct lang_input_statement_struct *entry; |
2fa0b342 DHW |
932 | { |
933 | asymbol **q; | |
934 | ||
935 | for (q = entry->asymbols; *q; q++) | |
936 | { | |
937 | asymbol *p = *q; | |
938 | ||
939 | /* If the symbol has an interesting definition, we could | |
940 | potentially want it. */ | |
941 | ||
e47bfa63 SC |
942 | if (p->flags & BSF_INDIRECT) |
943 | { | |
29f33467 | 944 | /** add_indirect(q);*/ |
e47bfa63 | 945 | } |
3e4c643d | 946 | |
8a045e50 | 947 | if (bfd_is_com_section (p->section) |
9d1fe8a4 SC |
948 | || (p->flags & BSF_GLOBAL) |
949 | || (p->flags & BSF_INDIRECT)) | |
2fa0b342 DHW |
950 | { |
951 | register ldsym_type *sp = ldsym_get_soft (p->name); | |
952 | ||
2fa0b342 DHW |
953 | /* If this symbol has not been hashed, |
954 | we can't be looking for it. */ | |
e47bfa63 SC |
955 | if (sp != (ldsym_type *) NULL |
956 | && sp->sdefs_chain == (asymbol **) NULL) | |
957 | { | |
958 | if (sp->srefs_chain != (asymbol **) NULL | |
959 | || sp->scoms_chain != (asymbol **) NULL) | |
960 | { | |
f31cb329 ILT |
961 | /* This is a symbol we are looking for. It is |
962 | either not yet defined or common. If this is a | |
963 | common symbol, then if the symbol in the object | |
964 | file is common, we need to combine sizes. But if | |
965 | we already have a common symbol, and the symbol | |
966 | in the object file is not common, we don't want | |
967 | the object file: it is providing a definition for | |
968 | a symbol that we already have a definition for | |
969 | (this is the else condition below). */ | |
8a045e50 | 970 | if (bfd_is_com_section (p->section)) |
e47bfa63 SC |
971 | { |
972 | ||
973 | /* If the symbol in the table is a constructor, we won't to | |
974 | anything fancy with it */ | |
975 | if ((sp->flags & SYM_CONSTRUCTOR) == 0) | |
976 | { | |
977 | /* This libary member has something to | |
978 | say about this element. We should | |
979 | remember if its a new size */ | |
980 | /* Move something from the ref list to the com list */ | |
981 | if (sp->scoms_chain) | |
982 | { | |
983 | /* Already a common symbol, maybe update it */ | |
984 | if (p->value > (*(sp->scoms_chain))->value) | |
985 | { | |
986 | (*(sp->scoms_chain))->value = p->value; | |
987 | } | |
988 | } | |
989 | else | |
990 | { | |
991 | /* Take a value from the ref chain | |
992 | Here we are moving a symbol from the owning bfd | |
993 | to another bfd. We must set up the | |
994 | common_section portion of the bfd thing */ | |
995 | ||
996 | ||
997 | ||
998 | sp->scoms_chain = sp->srefs_chain; | |
999 | sp->srefs_chain = | |
1000 | (asymbol **) ((*(sp->srefs_chain))->udata); | |
1001 | (*(sp->scoms_chain))->udata = (PTR) NULL; | |
1002 | ||
29f33467 | 1003 | (*(sp->scoms_chain))->section = p->section; |
e47bfa63 SC |
1004 | (*(sp->scoms_chain))->flags = 0; |
1005 | /* Remember the size of this item */ | |
1006 | sp->scoms_chain[0]->value = p->value; | |
1007 | commons_pending++; | |
1008 | undefined_global_sym_count--; | |
1009 | } | |
1010 | { | |
1011 | asymbol *com = *(sp->scoms_chain); | |
1012 | ||
1013 | if (((lang_input_statement_type *) | |
29f33467 | 1014 | (bfd_asymbol_bfd (com)->usrdata))->common_section == |
e47bfa63 SC |
1015 | (asection *) NULL) |
1016 | { | |
1017 | ((lang_input_statement_type *) | |
29f33467 SC |
1018 | (bfd_asymbol_bfd (com)->usrdata))->common_section = |
1019 | bfd_make_section_old_way (bfd_asymbol_bfd (com), "COMMON"); | |
e47bfa63 SC |
1020 | } |
1021 | } | |
1022 | } | |
1023 | ASSERT (p->udata == 0); | |
2fa0b342 | 1024 | } |
f31cb329 | 1025 | else if (sp->scoms_chain == (asymbol **) NULL) |
2fa0b342 | 1026 | { |
e47bfa63 SC |
1027 | if (write_map) |
1028 | { | |
1029 | info ("%I needed due to %s\n", entry, sp->name); | |
1030 | } | |
1031 | return true; | |
2fa0b342 | 1032 | } |
2fa0b342 | 1033 | } |
e47bfa63 | 1034 | } |
2fa0b342 DHW |
1035 | } |
1036 | } | |
1037 | ||
1038 | return false; | |
1039 | } | |
8a045e50 ILT |
1040 | |
1041 | void | |
29f33467 SC |
1042 | add_ysym (text) |
1043 | char *text; | |
8a045e50 | 1044 | { |
29f33467 | 1045 | ldsym_type *lookup = ldsym_get (text); |
8a045e50 ILT |
1046 | lookup->flags |= SYM_Y; |
1047 | had_y = 1; | |
1048 | } |