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