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