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