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