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