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