]>
Commit | Line | Data |
---|---|---|
da6b2d99 ILT |
1 | /* linker.c -- BFD linker routines |
2 | Copyright 1993 Free Software Foundation, Inc. | |
3 | Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support | |
4 | ||
5 | This file is part of BFD | |
6 | ||
7 | GLD is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GLD is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GLD; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "libbfd.h" | |
24 | #include "bfdlink.h" | |
25 | #include "genlink.h" | |
26 | ||
27 | static struct bfd_hash_entry *generic_link_hash_newfunc | |
28 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, | |
29 | const char *)); | |
30 | static boolean generic_link_add_object_symbols | |
31 | PARAMS ((bfd *, struct bfd_link_info *)); | |
32 | static boolean generic_link_check_archive_element | |
33 | PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded)); | |
34 | static boolean generic_link_add_symbol_list | |
35 | PARAMS ((bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **)); | |
36 | static boolean generic_add_output_symbol | |
37 | PARAMS ((bfd *, size_t *psymalloc, asymbol *)); | |
38 | static boolean default_fill_link_order | |
39 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
40 | struct bfd_link_order *)); | |
41 | ||
42 | /* The link hash table structure is defined in bfdlink.h. It provides | |
43 | a base hash table which the backend specific hash tables are built | |
44 | upon. */ | |
45 | ||
46 | /* Routine to create an entry in the link hash table. */ | |
47 | ||
48 | struct bfd_hash_entry * | |
49 | _bfd_link_hash_newfunc (entry, table, string) | |
50 | struct bfd_hash_entry *entry; | |
51 | struct bfd_hash_table *table; | |
52 | const char *string; | |
53 | { | |
54 | struct bfd_link_hash_entry *ret = (struct bfd_link_hash_entry *) entry; | |
55 | ||
56 | /* Allocate the structure if it has not already been allocated by a | |
57 | subclass. */ | |
58 | if (ret == (struct bfd_link_hash_entry *) NULL) | |
59 | ret = ((struct bfd_link_hash_entry *) | |
60 | bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry))); | |
61 | ||
62 | /* Call the allocation method of the superclass. */ | |
63 | ret = ((struct bfd_link_hash_entry *) | |
64 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); | |
65 | ||
66 | /* Initialize the local fields. */ | |
67 | ret->type = bfd_link_hash_new; | |
68 | ret->written = false; | |
69 | ret->next = NULL; | |
70 | ||
71 | return (struct bfd_hash_entry *) ret; | |
72 | } | |
73 | ||
74 | /* Initialize a link hash table. The BFD argument is the one | |
75 | responsible for creating this table. */ | |
76 | ||
77 | boolean | |
78 | _bfd_link_hash_table_init (table, abfd, newfunc) | |
79 | struct bfd_link_hash_table *table; | |
80 | bfd *abfd; | |
81 | struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, | |
82 | struct bfd_hash_table *, | |
83 | const char *)); | |
84 | { | |
85 | table->creator = abfd->xvec; | |
86 | table->undefs = NULL; | |
87 | table->undefs_tail = NULL; | |
88 | return bfd_hash_table_init (&table->table, newfunc); | |
89 | } | |
90 | ||
91 | /* Look up a symbol in a link hash table. If follow is true, we | |
92 | follow bfd_link_hash_indirect and bfd_link_hash_warning links to | |
93 | the real symbol. */ | |
94 | ||
95 | struct bfd_link_hash_entry * | |
96 | bfd_link_hash_lookup (table, string, create, copy, follow) | |
97 | struct bfd_link_hash_table *table; | |
98 | const char *string; | |
99 | boolean create; | |
100 | boolean copy; | |
101 | boolean follow; | |
102 | { | |
103 | struct bfd_link_hash_entry *ret; | |
104 | ||
105 | ret = ((struct bfd_link_hash_entry *) | |
106 | bfd_hash_lookup (&table->table, string, create, copy)); | |
107 | ||
108 | if (follow && ret != (struct bfd_link_hash_entry *) NULL) | |
109 | { | |
110 | while (ret->type == bfd_link_hash_indirect | |
111 | || ret->type == bfd_link_hash_warning) | |
112 | ret = ret->u.i.link; | |
113 | } | |
114 | ||
115 | return ret; | |
116 | } | |
117 | ||
118 | /* Traverse a generic link hash table. The only reason this is not a | |
119 | macro is to do better type checking. This code presumes that an | |
120 | argument passed as a struct bfd_hash_entry * may be cause as a | |
121 | struct bfd_link_hash_entry * with no explicit cast required on the | |
122 | call. */ | |
123 | ||
124 | void | |
125 | bfd_link_hash_traverse (table, func, info) | |
126 | struct bfd_link_hash_table *table; | |
127 | boolean (*func) PARAMS ((struct bfd_link_hash_entry *, PTR)); | |
128 | PTR info; | |
129 | { | |
130 | bfd_hash_traverse (&table->table, | |
131 | ((boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) | |
132 | func), | |
133 | info); | |
134 | } | |
135 | ||
136 | /* Add a symbol to the linker hash table undefs list. */ | |
137 | ||
138 | INLINE void | |
139 | bfd_link_add_undef (table, h) | |
140 | struct bfd_link_hash_table *table; | |
141 | struct bfd_link_hash_entry *h; | |
142 | { | |
143 | BFD_ASSERT (h->next == NULL); | |
144 | if (table->undefs_tail != (struct bfd_link_hash_entry *) NULL) | |
145 | table->undefs_tail->next = h; | |
146 | if (table->undefs == (struct bfd_link_hash_entry *) NULL) | |
147 | table->undefs = h; | |
148 | table->undefs_tail = h; | |
149 | } | |
150 | \f | |
151 | /* Routine to create an entry in an generic link hash table. */ | |
152 | ||
153 | static struct bfd_hash_entry * | |
154 | generic_link_hash_newfunc (entry, table, string) | |
155 | struct bfd_hash_entry *entry; | |
156 | struct bfd_hash_table *table; | |
157 | const char *string; | |
158 | { | |
159 | struct generic_link_hash_entry *ret = | |
160 | (struct generic_link_hash_entry *) entry; | |
161 | ||
162 | /* Allocate the structure if it has not already been allocated by a | |
163 | subclass. */ | |
164 | if (ret == (struct generic_link_hash_entry *) NULL) | |
165 | ret = ((struct generic_link_hash_entry *) | |
166 | bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry))); | |
167 | ||
168 | /* Call the allocation method of the superclass. */ | |
169 | ret = ((struct generic_link_hash_entry *) | |
170 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
171 | table, string)); | |
172 | ||
173 | /* Set local fields. */ | |
174 | ret->sym = NULL; | |
175 | ||
176 | return (struct bfd_hash_entry *) ret; | |
177 | } | |
178 | ||
179 | /* Create an generic link hash table. */ | |
180 | ||
181 | struct bfd_link_hash_table * | |
182 | _bfd_generic_link_hash_table_create (abfd) | |
183 | bfd *abfd; | |
184 | { | |
185 | struct generic_link_hash_table *ret; | |
186 | ||
187 | ret = ((struct generic_link_hash_table *) | |
188 | bfd_xmalloc (sizeof (struct generic_link_hash_table))); | |
189 | if (! _bfd_link_hash_table_init (&ret->root, abfd, | |
190 | generic_link_hash_newfunc)) | |
191 | { | |
192 | free (ret); | |
193 | return (struct bfd_link_hash_table *) NULL; | |
194 | } | |
195 | return &ret->root; | |
196 | } | |
197 | \f | |
198 | /* Generic function to add symbols from an object file to the global | |
199 | hash table. */ | |
200 | ||
201 | boolean | |
202 | _bfd_generic_link_add_symbols (abfd, info) | |
203 | bfd *abfd; | |
204 | struct bfd_link_info *info; | |
205 | { | |
206 | boolean ret; | |
207 | ||
208 | switch (bfd_get_format (abfd)) | |
209 | { | |
210 | case bfd_object: | |
211 | ret = generic_link_add_object_symbols (abfd, info); | |
212 | break; | |
213 | case bfd_archive: | |
214 | ret = _bfd_generic_link_add_archive_symbols | |
215 | (abfd, info, generic_link_check_archive_element); | |
216 | break; | |
217 | default: | |
218 | bfd_error = wrong_format; | |
219 | ret = false; | |
220 | } | |
221 | ||
222 | /* If we might be using the C based alloca function, make sure we | |
223 | have dumped the symbol tables we just allocated. */ | |
224 | #ifndef __GNUC__ | |
225 | #ifndef alloca | |
226 | alloca (0); | |
227 | #endif | |
228 | #endif | |
229 | ||
230 | return ret; | |
231 | } | |
232 | ||
233 | /* Add symbols from an object file to the global hash table. */ | |
234 | ||
235 | static boolean | |
236 | generic_link_add_object_symbols (abfd, info) | |
237 | bfd *abfd; | |
238 | struct bfd_link_info *info; | |
239 | { | |
240 | size_t symsize; | |
241 | asymbol **symbols; | |
242 | bfd_size_type symbol_count; | |
243 | ||
244 | symsize = get_symtab_upper_bound (abfd); | |
245 | symbols = (asymbol **) alloca (symsize); | |
246 | symbol_count = bfd_canonicalize_symtab (abfd, symbols); | |
247 | ||
248 | return generic_link_add_symbol_list (abfd, info, symbol_count, symbols); | |
249 | } | |
250 | \f | |
251 | /* We build a hash table of all symbols defined in an archive. */ | |
252 | ||
253 | /* An archive symbol may be defined by multiple archive elements. | |
254 | This linked list is used to hold the elements. */ | |
255 | ||
256 | struct archive_list | |
257 | { | |
258 | struct archive_list *next; | |
259 | int indx; | |
260 | }; | |
261 | ||
262 | /* An entry in an archive hash table. */ | |
263 | ||
264 | struct archive_hash_entry | |
265 | { | |
266 | struct bfd_hash_entry root; | |
267 | /* Where the symbol is defined. */ | |
268 | struct archive_list *defs; | |
269 | }; | |
270 | ||
271 | /* An archive hash table itself. */ | |
272 | ||
273 | struct archive_hash_table | |
274 | { | |
275 | struct bfd_hash_table table; | |
276 | }; | |
277 | ||
278 | static struct bfd_hash_entry *archive_hash_newfunc | |
279 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
280 | static boolean archive_hash_table_init | |
281 | PARAMS ((struct archive_hash_table *, | |
282 | struct bfd_hash_entry *(*) (struct bfd_hash_entry *, | |
283 | struct bfd_hash_table *, | |
284 | const char *))); | |
285 | ||
286 | /* Create a new entry for an archive hash table. */ | |
287 | ||
288 | static struct bfd_hash_entry * | |
289 | archive_hash_newfunc (entry, table, string) | |
290 | struct bfd_hash_entry *entry; | |
291 | struct bfd_hash_table *table; | |
292 | const char *string; | |
293 | { | |
294 | struct archive_hash_entry *ret = (struct archive_hash_entry *) entry; | |
295 | ||
296 | /* Allocate the structure if it has not already been allocated by a | |
297 | subclass. */ | |
298 | if (ret == (struct archive_hash_entry *) NULL) | |
299 | ret = ((struct archive_hash_entry *) | |
300 | bfd_hash_allocate (table, sizeof (struct archive_hash_entry))); | |
301 | ||
302 | /* Call the allocation method of the superclass. */ | |
303 | ret = ((struct archive_hash_entry *) | |
304 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); | |
305 | ||
306 | /* Initialize the local fields. */ | |
307 | ret->defs = (struct archive_list *) NULL; | |
308 | ||
309 | return (struct bfd_hash_entry *) ret; | |
310 | } | |
311 | ||
312 | /* Initialize an archive hash table. */ | |
313 | ||
314 | static boolean | |
315 | archive_hash_table_init (table, newfunc) | |
316 | struct archive_hash_table *table; | |
317 | struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, | |
318 | struct bfd_hash_table *, | |
319 | const char *)); | |
320 | { | |
321 | return bfd_hash_table_init (&table->table, newfunc); | |
322 | } | |
323 | ||
324 | /* Look up an entry in an archive hash table. */ | |
325 | ||
326 | #define archive_hash_lookup(t, string, create, copy) \ | |
327 | ((struct archive_hash_entry *) \ | |
328 | bfd_hash_lookup (&(t)->table, (string), (create), (copy))) | |
329 | ||
330 | /* Free an archive hash table. */ | |
331 | ||
332 | #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table) | |
333 | ||
334 | /* Generic function to add symbols from an archive file to the global | |
335 | hash file. This function presumes that the archive symbol table | |
336 | has already been read in (this is normally done by the | |
337 | bfd_check_format entry point). It looks through the undefined and | |
338 | common symbols and searches the archive symbol table for them. If | |
339 | it finds an entry, it includes the associated object file in the | |
340 | link. | |
341 | ||
342 | The old linker looked through the archive symbol table for | |
343 | undefined symbols. We do it the other way around, looking through | |
344 | undefined symbols for symbols defined in the archive. The | |
345 | advantage of the newer scheme is that we only have to look through | |
346 | the list of undefined symbols once, whereas the old method had to | |
347 | re-search the symbol table each time a new object file was added. | |
348 | ||
349 | The CHECKFN argument is used to see if an object file should be | |
350 | included. CHECKFN should set *PNEEDED to true if the object file | |
351 | should be included, and must also call the bfd_link_info | |
352 | add_archive_element callback function and handle adding the symbols | |
353 | to the global hash table. CHECKFN should only return false if some | |
354 | sort of error occurs. | |
355 | ||
356 | For some formats, such as a.out, it is possible to look through an | |
357 | object file but not actually include it in the link. The | |
358 | archive_pass field in a BFD is used to avoid checking the symbols | |
359 | of an object files too many times. When an object is included in | |
360 | the link, archive_pass is set to -1. If an object is scanned but | |
361 | not included, archive_pass is set to the pass number. The pass | |
362 | number is incremented each time a new object file is included. The | |
363 | pass number is used because when a new object file is included it | |
364 | may create new undefined symbols which cause a previously examined | |
365 | object file to be included. */ | |
366 | ||
367 | boolean | |
368 | _bfd_generic_link_add_archive_symbols (abfd, info, checkfn) | |
369 | bfd *abfd; | |
370 | struct bfd_link_info *info; | |
371 | boolean (*checkfn) PARAMS ((bfd *, struct bfd_link_info *, | |
372 | boolean *pneeded)); | |
373 | { | |
374 | carsym *arsyms; | |
375 | carsym *arsym_end; | |
376 | register carsym *arsym; | |
377 | int pass; | |
378 | struct archive_hash_table arsym_hash; | |
379 | int indx; | |
380 | struct bfd_link_hash_entry **pundef; | |
381 | ||
382 | if (! bfd_has_map (abfd)) | |
383 | { | |
384 | bfd_error = no_symbols; | |
385 | return false; | |
386 | } | |
387 | ||
388 | arsyms = bfd_ardata (abfd)->symdefs; | |
389 | arsym_end = arsyms + bfd_ardata (abfd)->symdef_count; | |
390 | ||
391 | /* In order to quickly determine whether an symbol is defined in | |
392 | this archive, we build a hash table of the symbols. */ | |
393 | if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc)) | |
394 | return false; | |
395 | for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++) | |
396 | { | |
397 | struct archive_hash_entry *arh; | |
398 | struct archive_list *l; | |
399 | ||
400 | arh = archive_hash_lookup (&arsym_hash, arsym->name, true, false); | |
401 | if (arh == (struct archive_hash_entry *) NULL) | |
402 | return false; | |
403 | l = (struct archive_list *) alloca (sizeof (struct archive_list)); | |
404 | l->next = arh->defs; | |
405 | arh->defs = l; | |
406 | l->indx = indx; | |
407 | } | |
408 | ||
409 | pass = 1; | |
410 | ||
411 | /* New undefined symbols are added to the end of the list, so we | |
412 | only need to look through it once. */ | |
413 | pundef = &info->hash->undefs; | |
414 | while (*pundef != (struct bfd_link_hash_entry *) NULL) | |
415 | { | |
416 | struct bfd_link_hash_entry *h; | |
417 | struct archive_hash_entry *arh; | |
418 | struct archive_list *l; | |
419 | ||
420 | h = *pundef; | |
421 | ||
422 | /* When a symbol is defined, it is not necessarily removed from | |
423 | the list. */ | |
424 | if (h->type != bfd_link_hash_undefined | |
425 | && h->type != bfd_link_hash_common) | |
426 | { | |
427 | /* Remove this entry from the list, for general cleanliness | |
428 | and because we are going to look through the list again | |
429 | if we search any more libraries. We can't remove the | |
430 | entry if it is the tail, because that would lose any | |
431 | entries we add to the list later on. */ | |
432 | if (*pundef != info->hash->undefs_tail) | |
433 | *pundef = (*pundef)->next; | |
434 | else | |
435 | pundef = &(*pundef)->next; | |
436 | continue; | |
437 | } | |
438 | ||
439 | /* Look for this symbol in the archive symbol map. */ | |
440 | arh = archive_hash_lookup (&arsym_hash, h->root.string, false, false); | |
441 | if (arh == (struct archive_hash_entry *) NULL) | |
442 | { | |
443 | pundef = &(*pundef)->next; | |
444 | continue; | |
445 | } | |
446 | ||
447 | /* Look at all the objects which define this symbol. */ | |
448 | for (l = arh->defs; l != (struct archive_list *) NULL; l = l->next) | |
449 | { | |
450 | bfd *element; | |
451 | boolean needed; | |
452 | ||
453 | /* If the symbol has gotten defined along the way, quit. */ | |
454 | if (h->type != bfd_link_hash_undefined | |
455 | && h->type != bfd_link_hash_common) | |
456 | break; | |
457 | ||
458 | element = bfd_get_elt_at_index (abfd, l->indx); | |
459 | if (element == (bfd *) NULL) | |
460 | return false; | |
461 | ||
462 | /* If we've already included this element, or if we've | |
463 | already checked it on this pass, continue. */ | |
464 | if (element->archive_pass == -1 | |
465 | || element->archive_pass == pass) | |
466 | continue; | |
467 | ||
468 | /* If we can't figure this element out, just ignore it. */ | |
469 | if (! bfd_check_format (element, bfd_object)) | |
470 | { | |
471 | element->archive_pass = -1; | |
472 | continue; | |
473 | } | |
474 | ||
475 | /* CHECKFN will see if this element should be included, and | |
476 | go ahead and include it if appropriate. */ | |
477 | if (! (*checkfn) (element, info, &needed)) | |
478 | return false; | |
479 | ||
480 | if (! needed) | |
481 | element->archive_pass = pass; | |
482 | else | |
483 | { | |
484 | element->archive_pass = -1; | |
485 | ||
486 | /* Increment the pass count to show that we may need to | |
487 | recheck object files which were already checked. */ | |
488 | ++pass; | |
489 | } | |
490 | } | |
491 | ||
492 | pundef = &(*pundef)->next; | |
493 | } | |
494 | ||
495 | archive_hash_table_free (&arsym_hash); | |
496 | ||
497 | return true; | |
498 | } | |
499 | \f | |
500 | /* See if we should include an archive element. */ | |
501 | ||
502 | static boolean | |
503 | generic_link_check_archive_element (abfd, info, pneeded) | |
504 | bfd *abfd; | |
505 | struct bfd_link_info *info; | |
506 | boolean *pneeded; | |
507 | { | |
508 | size_t symsize; | |
509 | asymbol **symbols; | |
510 | bfd_size_type symbol_count; | |
511 | asymbol **pp, **ppend; | |
512 | ||
513 | *pneeded = false; | |
514 | ||
515 | symsize = get_symtab_upper_bound (abfd); | |
516 | symbols = (asymbol **) alloca (symsize); | |
517 | symbol_count = bfd_canonicalize_symtab (abfd, symbols); | |
518 | ||
519 | pp = symbols; | |
520 | ppend = symbols + symbol_count; | |
521 | for (; pp < ppend; pp++) | |
522 | { | |
523 | asymbol *p; | |
524 | struct bfd_link_hash_entry *h; | |
525 | ||
526 | p = *pp; | |
527 | ||
528 | /* We are only interested in globally visible symbols. */ | |
529 | if (! bfd_is_com_section (p->section) | |
530 | && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0) | |
531 | continue; | |
532 | ||
533 | /* We are only interested if we know something about this | |
534 | symbol, and it is undefined or common. An undefined weak | |
535 | symbol (type bfd_link_hash_weak) is not considered to be a | |
536 | reference when pulling files out of an archive. See the SVR4 | |
537 | ABI, p. 4-27. */ | |
538 | h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), false, | |
539 | false, true); | |
540 | if (h == (struct bfd_link_hash_entry *) NULL | |
541 | || (h->type != bfd_link_hash_undefined | |
542 | && h->type != bfd_link_hash_common)) | |
543 | continue; | |
544 | ||
545 | /* P is a symbol we are looking for. */ | |
546 | ||
547 | if (! bfd_is_com_section (p->section)) | |
548 | { | |
549 | /* This object file defines this symbol, so pull it in. */ | |
550 | if (! (*info->callbacks->add_archive_element) (info, abfd, | |
551 | bfd_asymbol_name (p))) | |
552 | return false; | |
553 | if (! generic_link_add_symbol_list (abfd, info, symbol_count, | |
554 | symbols)) | |
555 | return false; | |
556 | *pneeded = true; | |
557 | return true; | |
558 | } | |
559 | ||
560 | /* P is a common symbol. */ | |
561 | ||
562 | if (h->type == bfd_link_hash_undefined) | |
563 | { | |
564 | bfd *symbfd; | |
565 | ||
566 | symbfd = h->u.undef.abfd; | |
567 | if (symbfd == (bfd *) NULL) | |
568 | { | |
569 | /* This symbol was created as undefined from outside | |
570 | BFD. We assume that we should link in the object | |
571 | file. This is for the -u option in the linker. */ | |
572 | if (! (*info->callbacks->add_archive_element) | |
573 | (info, abfd, bfd_asymbol_name (p))) | |
574 | return false; | |
575 | *pneeded = true; | |
576 | return true; | |
577 | } | |
578 | ||
579 | /* Turn the symbol into a common symbol but do not link in | |
580 | the object file. This is how a.out works. Object | |
581 | formats that require different semantics must implement | |
582 | this function differently. This symbol is already on the | |
583 | undefs list. */ | |
584 | h->type = bfd_link_hash_common; | |
585 | h->u.c.size = bfd_asymbol_value (p); | |
586 | h->u.c.section = bfd_make_section_old_way (symbfd, | |
587 | "COMMON"); | |
588 | } | |
589 | else | |
590 | { | |
591 | /* Adjust the size of the common symbol if necessary. This | |
592 | is how a.out works. Object formats that require | |
593 | different semantics must implement this function | |
594 | differently. */ | |
595 | if (bfd_asymbol_value (p) > h->u.c.size) | |
596 | h->u.c.size = bfd_asymbol_value (p); | |
597 | } | |
598 | } | |
599 | ||
600 | /* This archive element is not needed. */ | |
601 | return true; | |
602 | } | |
603 | ||
604 | /* Add the symbol from an object file to the global hash table. */ | |
605 | ||
606 | static boolean | |
607 | generic_link_add_symbol_list (abfd, info, symbol_count, symbols) | |
608 | bfd *abfd; | |
609 | struct bfd_link_info *info; | |
610 | bfd_size_type symbol_count; | |
611 | asymbol **symbols; | |
612 | { | |
613 | asymbol **pp, **ppend; | |
614 | ||
615 | pp = symbols; | |
616 | ppend = symbols + symbol_count; | |
617 | for (; pp < ppend; pp++) | |
618 | { | |
619 | asymbol *p; | |
620 | ||
621 | p = *pp; | |
622 | ||
623 | if ((p->flags & (BSF_INDIRECT | |
624 | | BSF_WARNING | |
625 | | BSF_GLOBAL | |
626 | | BSF_CONSTRUCTOR | |
627 | | BSF_WEAK)) != 0 | |
628 | || bfd_get_section (p) == &bfd_und_section | |
629 | || bfd_is_com_section (bfd_get_section (p)) | |
630 | || bfd_get_section (p) == &bfd_ind_section) | |
631 | { | |
632 | const char *name; | |
633 | const char *string; | |
634 | struct generic_link_hash_entry *h; | |
635 | ||
636 | name = bfd_asymbol_name (p); | |
637 | if ((p->flags & BSF_INDIRECT) != 0 | |
638 | || p->section == &bfd_ind_section) | |
639 | string = bfd_asymbol_name ((asymbol *) p->value); | |
640 | else if ((p->flags & BSF_WARNING) != 0) | |
641 | { | |
642 | /* The name of P is actually the warning string, and the | |
643 | value is actually a pointer to the symbol to warn | |
644 | about. */ | |
645 | string = name; | |
646 | name = bfd_asymbol_name ((asymbol *) p->value); | |
647 | } | |
648 | else | |
649 | string = NULL; | |
650 | if (! (_bfd_generic_link_add_one_symbol | |
651 | (info, abfd, name, p->flags, bfd_get_section (p), | |
652 | p->value, string, false, | |
653 | (struct bfd_link_hash_entry **) &h))) | |
654 | return false; | |
655 | ||
656 | /* Save the BFD symbol so that we don't lose any backend | |
657 | specific information that may be attached to it. We only | |
658 | want this one if it gives more information than the | |
659 | existing one; we don't want to replace a defined symbol | |
660 | with an undefined one. This routine may be called with a | |
661 | hash table other than the generic hash table, so we only | |
662 | do this if we are certain that the hash table is a | |
663 | generic one. */ | |
664 | if (info->hash->creator == abfd->xvec) | |
665 | { | |
666 | if (h->sym == (asymbol *) NULL | |
667 | || (bfd_get_section (p) != &bfd_und_section | |
668 | && (! bfd_is_com_section (bfd_get_section (p)) | |
669 | || (bfd_get_section (h->sym) == &bfd_und_section)))) | |
670 | h->sym = p; | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | return true; | |
676 | } | |
677 | \f | |
678 | /* We use a state table to deal with adding symbols from an object | |
679 | file. The first index into the state table describes the symbol | |
680 | from the object file. The second index into the state table is the | |
681 | type of the symbol in the hash table. */ | |
682 | ||
683 | /* The symbol from the object file is turned into one of these row | |
684 | values. */ | |
685 | ||
686 | enum link_row | |
687 | { | |
688 | UNDEF_ROW, /* Undefined. */ | |
689 | UNDEFW_ROW, /* Weak undefined. */ | |
690 | DEF_ROW, /* Defined. */ | |
691 | DEFW_ROW, /* Weak defined. */ | |
692 | COMMON_ROW, /* Common. */ | |
693 | INDR_ROW, /* Indirect. */ | |
694 | WARN_ROW, /* Warning. */ | |
695 | SET_ROW /* Member of set. */ | |
696 | }; | |
697 | ||
698 | /* The actions to take in the state table. */ | |
699 | ||
700 | enum link_action | |
701 | { | |
702 | FAIL, /* Abort. */ | |
703 | UND, /* Mark symbol undefined. */ | |
704 | WEAK, /* Mark symbol weak undefined. */ | |
705 | DEF, /* Mark symbol defined. */ | |
706 | COM, /* Mark symbol common. */ | |
707 | CREF, /* Possibly warn about common reference to defined symbol. */ | |
708 | CDEF, /* Define existing common symbol. */ | |
709 | NOACT, /* No action. */ | |
710 | BIG, /* Mark symbol common using largest size. */ | |
711 | MDEF, /* Multiple definition error. */ | |
712 | IND, /* Make indirect symbol. */ | |
713 | SET, /* Add value to set. */ | |
714 | MWARN, /* Make warning symbol. */ | |
715 | WARN, /* Issue warning. */ | |
716 | CYCLE, /* Repeat with symbol pointed to. */ | |
717 | WARNC /* Issue warning and then CYCLE. */ | |
718 | }; | |
719 | ||
720 | /* The state table itself. The first index is a link_row and the | |
721 | second index is a bfd_link_hash_type. */ | |
722 | ||
723 | static const enum link_action link_action[8][7] = | |
724 | { | |
725 | /* current\prev new undef weak def com indr warn */ | |
726 | /* UNDEF_ROW */ {UND, NOACT, NOACT, NOACT, NOACT, CYCLE, WARNC }, | |
727 | /* UNDEFW_ROW */ {WEAK, WEAK, NOACT, NOACT, NOACT, CYCLE, WARNC }, | |
728 | /* DEF_ROW */ {DEF, DEF, DEF, MDEF, CDEF, CYCLE, CYCLE }, | |
729 | /* DEFW_ROW */ {DEF, DEF, DEF, NOACT, NOACT, CYCLE, CYCLE }, | |
730 | /* COMMON_ROW */ {COM, COM, COM, CREF, BIG, CYCLE, WARNC }, | |
731 | /* INDR_ROW */ {IND, IND, IND, MDEF, MDEF, MDEF, WARNC }, | |
732 | /* WARN_ROW */ {MWARN, WARN, WARN, MWARN, MWARN, MWARN, NOACT }, | |
733 | /* SET_ROW */ {SET, SET, SET, SET, SET, CYCLE, WARNC } | |
734 | }; | |
735 | ||
736 | /* Add a symbol to the global hash table. | |
737 | ABFD is the BFD the symbol comes from. | |
738 | NAME is the name of the symbol. | |
739 | FLAGS is the BSF_* bits associated with the symbol. | |
740 | SECTION is the section in which the symbol is defined; this may be | |
741 | bfd_und_section or bfd_com_section. | |
742 | VALUE is the value of the symbol, relative to the section. | |
743 | STRING is used for either an indirect symbol, in which case it is | |
744 | the name of the symbol to indirect to, or a warning symbol, in | |
745 | which case it is the warning string. | |
746 | COPY is true if NAME or STRING must be copied into locally | |
747 | allocated memory if they need to be saved. | |
748 | HASHP, if not NULL, is a place to store the created hash table | |
749 | entry. */ | |
750 | ||
751 | boolean | |
752 | _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, value, | |
753 | string, copy, hashp) | |
754 | struct bfd_link_info *info; | |
755 | bfd *abfd; | |
756 | const char *name; | |
757 | flagword flags; | |
758 | asection *section; | |
759 | bfd_vma value; | |
760 | const char *string; | |
761 | boolean copy; | |
762 | struct bfd_link_hash_entry **hashp; | |
763 | { | |
764 | enum link_row row; | |
765 | struct bfd_link_hash_entry *h; | |
766 | boolean cycle; | |
767 | ||
768 | if (section == &bfd_ind_section | |
769 | || (flags & BSF_INDIRECT) != 0) | |
770 | row = INDR_ROW; | |
771 | else if ((flags & BSF_WARNING) != 0) | |
772 | row = WARN_ROW; | |
773 | else if ((flags & BSF_CONSTRUCTOR) != 0) | |
774 | row = SET_ROW; | |
775 | else if (section == &bfd_und_section) | |
776 | { | |
777 | if ((flags & BSF_WEAK) != 0) | |
778 | row = UNDEFW_ROW; | |
779 | else | |
780 | row = UNDEF_ROW; | |
781 | } | |
782 | else if ((flags & BSF_WEAK) != 0) | |
783 | row = DEFW_ROW; | |
784 | else if (bfd_is_com_section (section)) | |
785 | row = COMMON_ROW; | |
786 | else | |
787 | row = DEF_ROW; | |
788 | ||
789 | h = bfd_link_hash_lookup (info->hash, name, true, copy, false); | |
790 | if (h == (struct bfd_link_hash_entry *) NULL) | |
791 | { | |
792 | if (hashp != (struct bfd_link_hash_entry **) NULL) | |
793 | *hashp = NULL; | |
794 | return false; | |
795 | } | |
796 | ||
797 | if (info->notice_hash != (struct bfd_hash_table *) NULL | |
798 | && (bfd_hash_lookup (info->notice_hash, name, false, false) | |
799 | != (struct bfd_hash_entry *) NULL)) | |
800 | { | |
801 | if (! (*info->callbacks->notice) (info, name, abfd, section, value)) | |
802 | return false; | |
803 | } | |
804 | ||
805 | if (hashp != (struct bfd_link_hash_entry **) NULL) | |
806 | *hashp = h; | |
807 | ||
808 | do | |
809 | { | |
810 | enum link_action action; | |
811 | ||
812 | cycle = false; | |
813 | action = link_action[(int) row][(int) h->type]; | |
814 | switch (action) | |
815 | { | |
816 | case FAIL: | |
817 | abort (); | |
818 | case UND: | |
819 | h->type = bfd_link_hash_undefined; | |
820 | h->u.undef.abfd = abfd; | |
821 | bfd_link_add_undef (info->hash, h); | |
822 | break; | |
823 | case WEAK: | |
824 | h->type = bfd_link_hash_weak; | |
825 | h->u.undef.abfd = abfd; | |
826 | break; | |
827 | case CDEF: | |
828 | BFD_ASSERT (h->type == bfd_link_hash_common); | |
829 | if (! ((*info->callbacks->multiple_common) | |
830 | (info, name, | |
831 | h->u.c.section->owner, bfd_link_hash_common, h->u.c.size, | |
832 | abfd, bfd_link_hash_defined, (bfd_vma) 0))) | |
833 | return false; | |
834 | /* Fall through. */ | |
835 | case DEF: | |
836 | h->type = bfd_link_hash_defined; | |
837 | h->u.def.section = section; | |
838 | h->u.def.value = value; | |
839 | break; | |
840 | case COM: | |
841 | if (h->type == bfd_link_hash_new) | |
842 | bfd_link_add_undef (info->hash, h); | |
843 | h->type = bfd_link_hash_common; | |
844 | h->u.c.size = value; | |
845 | if (section == &bfd_com_section) | |
846 | h->u.c.section = bfd_make_section_old_way (abfd, "COMMON"); | |
847 | else if (section->owner != abfd) | |
848 | h->u.c.section = bfd_make_section_old_way (abfd, section->name); | |
849 | else | |
850 | h->u.c.section = section; | |
851 | break; | |
852 | case NOACT: | |
853 | break; | |
854 | case BIG: | |
855 | BFD_ASSERT (h->type == bfd_link_hash_common); | |
856 | if (! ((*info->callbacks->multiple_common) | |
857 | (info, name, | |
858 | h->u.c.section->owner, bfd_link_hash_common, h->u.c.size, | |
859 | abfd, bfd_link_hash_common, value))) | |
860 | return false; | |
861 | if (value > h->u.c.size) | |
862 | h->u.c.size = value; | |
863 | if (h->u.c.section == (asection *) NULL) | |
864 | h->u.c.section = bfd_make_section_old_way (abfd, "COMMON"); | |
865 | break; | |
866 | case CREF: | |
867 | BFD_ASSERT (h->type == bfd_link_hash_defined); | |
868 | if (! ((*info->callbacks->multiple_common) | |
869 | (info, name, | |
870 | h->u.def.section->owner, bfd_link_hash_defined, (bfd_vma) 0, | |
871 | abfd, bfd_link_hash_common, value))) | |
872 | return false; | |
873 | break; | |
874 | case MDEF: | |
875 | { | |
876 | asection *msec; | |
877 | bfd_vma mval; | |
878 | ||
879 | switch (h->type) | |
880 | { | |
881 | case bfd_link_hash_defined: | |
882 | msec = h->u.def.section; | |
883 | mval = h->u.def.value; | |
884 | break; | |
885 | case bfd_link_hash_common: | |
886 | msec = &bfd_com_section; | |
887 | mval = h->u.c.size; | |
888 | break; | |
889 | case bfd_link_hash_indirect: | |
890 | msec = &bfd_ind_section; | |
891 | mval = 0; | |
892 | break; | |
893 | default: | |
894 | abort (); | |
895 | } | |
896 | ||
897 | if (! ((*info->callbacks->multiple_definition) | |
898 | (info, name, msec->owner, msec, mval, abfd, section, | |
899 | value))) | |
900 | return false; | |
901 | } | |
902 | break; | |
903 | case IND: | |
904 | { | |
905 | struct bfd_link_hash_entry *inh; | |
906 | ||
907 | /* STRING is the name of the symbol we want to indirect | |
908 | to. */ | |
909 | inh = bfd_link_hash_lookup (info->hash, string, true, copy, | |
910 | false); | |
911 | if (inh == (struct bfd_link_hash_entry *) NULL) | |
912 | return false; | |
913 | if (inh->type == bfd_link_hash_new) | |
914 | { | |
915 | inh->type = bfd_link_hash_undefined; | |
916 | inh->u.undef.abfd = abfd; | |
917 | bfd_link_add_undef (info->hash, inh); | |
918 | } | |
919 | h->type = bfd_link_hash_indirect; | |
920 | h->u.i.link = inh; | |
921 | } | |
922 | break; | |
923 | case SET: | |
924 | if (! (*info->callbacks->add_to_set) (info, h, abfd, section, value)) | |
925 | return false; | |
926 | break; | |
927 | case WARN: | |
928 | case WARNC: | |
929 | if (h->u.i.warning != NULL) | |
930 | { | |
931 | if (! (*info->callbacks->warning) (info, h->u.i.warning)) | |
932 | return false; | |
933 | /* Only issue a warning once. */ | |
934 | h->u.i.warning = NULL; | |
935 | } | |
936 | if (action == WARN) | |
937 | break; | |
938 | /* Fall through. */ | |
939 | case CYCLE: | |
940 | h = h->u.i.link; | |
941 | cycle = true; | |
942 | break; | |
943 | case MWARN: | |
944 | { | |
945 | struct bfd_link_hash_entry *sub; | |
946 | ||
947 | /* STRING is the warning to give. */ | |
948 | sub = ((struct bfd_link_hash_entry *) | |
949 | bfd_hash_allocate (&info->hash->table, | |
950 | sizeof (struct bfd_link_hash_entry))); | |
951 | *sub = *h; | |
952 | h->type = bfd_link_hash_warning; | |
953 | h->u.i.link = sub; | |
954 | if (! copy) | |
955 | h->u.i.warning = string; | |
956 | else | |
957 | { | |
958 | char *w; | |
959 | ||
960 | w = bfd_hash_allocate (&info->hash->table, | |
961 | strlen (string) + 1); | |
962 | strcpy (w, string); | |
963 | h->u.i.warning = w; | |
964 | } | |
965 | } | |
966 | break; | |
967 | } | |
968 | } | |
969 | while (cycle); | |
970 | ||
971 | return true; | |
972 | } | |
973 | \f | |
974 | /* Generic final link routine. */ | |
975 | ||
976 | boolean | |
977 | _bfd_generic_final_link (abfd, info) | |
978 | bfd *abfd; | |
979 | struct bfd_link_info *info; | |
980 | { | |
981 | bfd *sub; | |
982 | asection *o; | |
983 | struct bfd_link_order *p; | |
984 | size_t outsymalloc; | |
985 | struct generic_write_global_symbol_info wginfo; | |
986 | ||
987 | abfd->outsymbols = (asymbol **) NULL; | |
988 | abfd->symcount = 0; | |
989 | outsymalloc = 0; | |
990 | ||
991 | /* Build the output symbol table. This also reads in the symbols | |
992 | for all the input BFDs, keeping them in the outsymbols field. */ | |
993 | for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next) | |
994 | if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc)) | |
995 | return false; | |
996 | ||
997 | /* Accumulate the global symbols. */ | |
998 | wginfo.output_bfd = abfd; | |
999 | wginfo.psymalloc = &outsymalloc; | |
1000 | _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info), | |
1001 | _bfd_generic_link_write_global_symbol, | |
1002 | (PTR) &wginfo); | |
1003 | ||
1004 | if (info->relocateable) | |
1005 | { | |
1006 | /* Allocate space for the output relocs for each section. */ | |
1007 | for (o = abfd->sections; | |
1008 | o != (asection *) NULL; | |
1009 | o = o->next) | |
1010 | { | |
1011 | o->reloc_count = 0; | |
1012 | for (p = o->link_order_head; | |
1013 | p != (struct bfd_link_order *) NULL; | |
1014 | p = p->next) | |
1015 | { | |
1016 | if (p->type == bfd_indirect_link_order) | |
1017 | { | |
1018 | asection *input_section; | |
1019 | bfd *input_bfd; | |
1020 | bfd_size_type relsize; | |
1021 | arelent **relocs; | |
1022 | bfd_size_type reloc_count; | |
1023 | ||
1024 | input_section = p->u.indirect.section; | |
1025 | input_bfd = input_section->owner; | |
1026 | relsize = bfd_get_reloc_upper_bound (input_bfd, | |
1027 | input_section); | |
1028 | relocs = (arelent **) bfd_xmalloc (relsize); | |
1029 | reloc_count = | |
1030 | bfd_canonicalize_reloc (input_bfd, input_section, | |
1031 | relocs, | |
1032 | bfd_get_outsymbols (input_bfd)); | |
1033 | BFD_ASSERT (reloc_count == input_section->reloc_count); | |
1034 | o->reloc_count += reloc_count; | |
1035 | free (relocs); | |
1036 | } | |
1037 | } | |
1038 | if (o->reloc_count > 0) | |
1039 | { | |
1040 | o->orelocation = ((arelent **) | |
1041 | bfd_alloc (abfd, | |
1042 | (o->reloc_count | |
1043 | * sizeof (arelent *)))); | |
1044 | /* Reset the count so that it can be used as an index | |
1045 | when putting in the output relocs. */ | |
1046 | o->reloc_count = 0; | |
1047 | } | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | /* Handle all the link order information for the sections. */ | |
1052 | for (o = abfd->sections; | |
1053 | o != (asection *) NULL; | |
1054 | o = o->next) | |
1055 | { | |
1056 | for (p = o->link_order_head; | |
1057 | p != (struct bfd_link_order *) NULL; | |
1058 | p = p->next) | |
1059 | { | |
1060 | switch (p->type) | |
1061 | { | |
1062 | case bfd_indirect_link_order: | |
1063 | if (! _bfd_generic_indirect_link_order (abfd, info, o, p)) | |
1064 | return false; | |
1065 | break; | |
1066 | default: | |
1067 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
1068 | return false; | |
1069 | break; | |
1070 | } | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | return true; | |
1075 | } | |
1076 | ||
1077 | /* Add an output symbol to the output BFD. */ | |
1078 | ||
1079 | static boolean | |
1080 | generic_add_output_symbol (output_bfd, psymalloc, sym) | |
1081 | bfd *output_bfd; | |
1082 | size_t *psymalloc; | |
1083 | asymbol *sym; | |
1084 | { | |
1085 | if (output_bfd->symcount >= *psymalloc) | |
1086 | { | |
1087 | asymbol **newsyms; | |
1088 | ||
1089 | if (*psymalloc == 0) | |
1090 | *psymalloc = 124; | |
1091 | else | |
1092 | *psymalloc *= 2; | |
1093 | if (output_bfd->outsymbols == (asymbol **) NULL) | |
1094 | newsyms = (asymbol **) malloc (*psymalloc * sizeof (asymbol *)); | |
1095 | else | |
1096 | newsyms = (asymbol **) realloc (output_bfd->outsymbols, | |
1097 | *psymalloc * sizeof (asymbol *)); | |
1098 | if (newsyms == (asymbol **) NULL) | |
1099 | { | |
1100 | bfd_error = no_memory; | |
1101 | return false; | |
1102 | } | |
1103 | output_bfd->outsymbols = newsyms; | |
1104 | } | |
1105 | ||
1106 | output_bfd->outsymbols[output_bfd->symcount] = sym; | |
1107 | ++output_bfd->symcount; | |
1108 | ||
1109 | return true; | |
1110 | } | |
1111 | ||
1112 | /* Handle the symbols for an input BFD. */ | |
1113 | ||
1114 | boolean | |
1115 | _bfd_generic_link_output_symbols (output_bfd, input_bfd, info, psymalloc) | |
1116 | bfd *output_bfd; | |
1117 | bfd *input_bfd; | |
1118 | struct bfd_link_info *info; | |
1119 | size_t *psymalloc; | |
1120 | { | |
1121 | size_t symsize; | |
1122 | asymbol **sym_ptr; | |
1123 | asymbol **sym_end; | |
1124 | ||
1125 | symsize = get_symtab_upper_bound (input_bfd); | |
1126 | input_bfd->outsymbols = (asymbol **) bfd_alloc (input_bfd, symsize); | |
1127 | input_bfd->symcount = bfd_canonicalize_symtab (input_bfd, | |
1128 | input_bfd->outsymbols); | |
1129 | ||
1130 | /* Create a filename symbol if we are supposed to. */ | |
1131 | if (info->create_object_symbols_section != (asection *) NULL) | |
1132 | { | |
1133 | asection *sec; | |
1134 | ||
1135 | for (sec = input_bfd->sections; | |
1136 | sec != (asection *) NULL; | |
1137 | sec = sec->next) | |
1138 | { | |
1139 | if (sec->output_section == info->create_object_symbols_section) | |
1140 | { | |
1141 | asymbol *newsym; | |
1142 | ||
1143 | newsym = bfd_make_empty_symbol (input_bfd); | |
1144 | newsym->name = input_bfd->filename; | |
1145 | newsym->value = 0; | |
1146 | newsym->flags = BSF_LOCAL | BSF_FILE; | |
1147 | newsym->section = sec; | |
1148 | ||
1149 | if (! generic_add_output_symbol (output_bfd, psymalloc, | |
1150 | newsym)) | |
1151 | return false; | |
1152 | ||
1153 | break; | |
1154 | } | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | /* Adjust the values of the globally visible symbols, and write out | |
1159 | local symbols. */ | |
1160 | sym_ptr = bfd_get_outsymbols (input_bfd); | |
1161 | sym_end = sym_ptr + bfd_get_symcount (input_bfd); | |
1162 | for (; sym_ptr < sym_end; sym_ptr++) | |
1163 | { | |
1164 | asymbol *sym; | |
1165 | struct generic_link_hash_entry *h; | |
1166 | boolean output; | |
1167 | ||
1168 | h = (struct generic_link_hash_entry *) NULL; | |
1169 | sym = *sym_ptr; | |
1170 | if ((sym->flags & (BSF_INDIRECT | |
1171 | | BSF_WARNING | |
1172 | | BSF_GLOBAL | |
1173 | | BSF_CONSTRUCTOR | |
1174 | | BSF_WEAK)) != 0 | |
1175 | || bfd_get_section (sym) == &bfd_und_section | |
1176 | || bfd_is_com_section (bfd_get_section (sym)) | |
1177 | || bfd_get_section (sym) == &bfd_ind_section) | |
1178 | { | |
1179 | h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info), | |
1180 | bfd_asymbol_name (sym), | |
1181 | false, false, true); | |
1182 | if (h != (struct generic_link_hash_entry *) NULL) | |
1183 | { | |
1184 | /* Force all references to this symbol to point to | |
1185 | the same area in memory. It is possible that | |
1186 | this routine will be called with a hash table | |
1187 | other than a generic hash table, so we double | |
1188 | check that. */ | |
1189 | if (info->hash->creator == input_bfd->xvec) | |
1190 | { | |
1191 | if (h->sym != (asymbol *) NULL) | |
1192 | *sym_ptr = sym = h->sym; | |
1193 | } | |
1194 | ||
1195 | switch (h->root.type) | |
1196 | { | |
1197 | default: | |
1198 | case bfd_link_hash_new: | |
1199 | abort (); | |
1200 | case bfd_link_hash_undefined: | |
1201 | case bfd_link_hash_weak: | |
1202 | break; | |
1203 | case bfd_link_hash_defined: | |
1204 | sym->value = h->root.u.def.value; | |
1205 | sym->section = h->root.u.def.section; | |
1206 | sym->flags |= BSF_GLOBAL; | |
1207 | break; | |
1208 | case bfd_link_hash_common: | |
1209 | sym->value = h->root.u.c.size; | |
1210 | sym->flags |= BSF_GLOBAL; | |
1211 | /* We do not set the section of the symbol to | |
1212 | c.section. c.section is saved so that we know | |
1213 | where to allocate the symbol if we define it. In | |
1214 | this case the type is still bfd_link_hash_common, | |
1215 | so we did not define it, so we do not want to use | |
1216 | that section. */ | |
1217 | BFD_ASSERT (bfd_is_com_section (sym->section)); | |
1218 | break; | |
1219 | } | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | /* This switch is straight from the old code in | |
1224 | write_file_locals in ldsym.c. */ | |
1225 | if (info->strip == strip_some | |
1226 | && (bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym), | |
1227 | false, false) | |
1228 | == (struct bfd_hash_entry *) NULL)) | |
1229 | output = false; | |
1230 | else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0) | |
1231 | { | |
1232 | /* If this symbol is marked as occurring now, rather | |
1233 | than at the end, output it now. This is used for | |
1234 | COFF C_EXT FCN symbols. FIXME: There must be a | |
1235 | better way. */ | |
1236 | if (bfd_asymbol_bfd (sym) == input_bfd | |
1237 | && (sym->flags & BSF_NOT_AT_END) != 0) | |
1238 | output = true; | |
1239 | else | |
1240 | output = false; | |
1241 | } | |
1242 | else if (sym->section == &bfd_ind_section) | |
1243 | output = false; | |
1244 | else if ((sym->flags & BSF_DEBUGGING) != 0) | |
1245 | { | |
1246 | if (info->strip == strip_none) | |
1247 | output = true; | |
1248 | else | |
1249 | output = false; | |
1250 | } | |
1251 | else if (sym->section == &bfd_und_section | |
1252 | || bfd_is_com_section (sym->section)) | |
1253 | output = false; | |
1254 | else if ((sym->flags & BSF_LOCAL) != 0) | |
1255 | { | |
1256 | if ((sym->flags & BSF_WARNING) != 0) | |
1257 | output = false; | |
1258 | else | |
1259 | { | |
1260 | switch (info->discard) | |
1261 | { | |
1262 | default: | |
1263 | case discard_all: | |
1264 | output = false; | |
1265 | break; | |
1266 | case discard_l: | |
1267 | if (bfd_asymbol_name (sym)[0] == info->lprefix[0] | |
1268 | && (info->lprefix_len == 1 | |
1269 | || strncmp (bfd_asymbol_name (sym), info->lprefix, | |
1270 | info->lprefix_len) == 0)) | |
1271 | output = false; | |
1272 | else | |
1273 | output = true; | |
1274 | break; | |
1275 | case discard_none: | |
1276 | output = true; | |
1277 | break; | |
1278 | } | |
1279 | } | |
1280 | } | |
1281 | else if ((sym->flags & BSF_CONSTRUCTOR)) | |
1282 | { | |
1283 | if (info->strip != strip_all) | |
1284 | output = true; | |
1285 | else | |
1286 | output = false; | |
1287 | } | |
1288 | else | |
1289 | abort (); | |
1290 | ||
1291 | if (output) | |
1292 | { | |
1293 | if (! generic_add_output_symbol (output_bfd, psymalloc, sym)) | |
1294 | return false; | |
1295 | if (h != (struct generic_link_hash_entry *) NULL) | |
1296 | h->root.written = true; | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | return true; | |
1301 | } | |
1302 | ||
1303 | /* Write out a global symbol, if it hasn't already been written out. | |
1304 | This is called for each symbol in the hash table. */ | |
1305 | ||
1306 | boolean | |
1307 | _bfd_generic_link_write_global_symbol (h, data) | |
1308 | struct generic_link_hash_entry *h; | |
1309 | PTR data; | |
1310 | { | |
1311 | struct generic_write_global_symbol_info *wginfo = | |
1312 | (struct generic_write_global_symbol_info *) data; | |
1313 | asymbol *sym; | |
1314 | ||
1315 | if (h->root.written) | |
1316 | return true; | |
1317 | ||
1318 | if (h->sym != (asymbol *) NULL) | |
1319 | { | |
1320 | sym = h->sym; | |
1321 | BFD_ASSERT (strcmp (bfd_asymbol_name (sym), h->root.root.string) == 0); | |
1322 | } | |
1323 | else | |
1324 | { | |
1325 | sym = bfd_make_empty_symbol (wginfo->output_bfd); | |
1326 | sym->name = h->root.root.string; | |
1327 | sym->flags = 0; | |
1328 | } | |
1329 | ||
1330 | switch (h->root.type) | |
1331 | { | |
1332 | default: | |
1333 | case bfd_link_hash_new: | |
1334 | abort (); | |
1335 | case bfd_link_hash_undefined: | |
1336 | sym->section = &bfd_und_section; | |
1337 | sym->value = 0; | |
1338 | break; | |
1339 | case bfd_link_hash_weak: | |
1340 | sym->section = &bfd_und_section; | |
1341 | sym->value = 0; | |
1342 | sym->flags |= BSF_WEAK; | |
1343 | case bfd_link_hash_defined: | |
1344 | sym->section = h->root.u.def.section; | |
1345 | sym->value = h->root.u.def.value; | |
1346 | break; | |
1347 | case bfd_link_hash_common: | |
1348 | sym->value = h->root.u.c.size; | |
1349 | /* Do not set the section; see _bfd_generic_link_output_symbols. */ | |
1350 | BFD_ASSERT (bfd_is_com_section (sym->section)); | |
1351 | break; | |
1352 | case bfd_link_hash_indirect: | |
1353 | case bfd_link_hash_warning: | |
1354 | /* FIXME: What should we do here? */ | |
1355 | break; | |
1356 | } | |
1357 | ||
1358 | sym->flags |= BSF_GLOBAL; | |
1359 | ||
1360 | if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc, | |
1361 | sym)) | |
1362 | { | |
1363 | /* FIXME: No way to return failure. */ | |
1364 | abort (); | |
1365 | } | |
1366 | ||
1367 | h->root.written = true; | |
1368 | ||
1369 | return true; | |
1370 | } | |
1371 | ||
1372 | /* Handle an indirect section when doing a generic link. */ | |
1373 | ||
1374 | boolean | |
1375 | _bfd_generic_indirect_link_order (output_bfd, info, output_section, link_order) | |
1376 | bfd *output_bfd; | |
1377 | struct bfd_link_info *info; | |
1378 | asection *output_section; | |
1379 | struct bfd_link_order *link_order; | |
1380 | { | |
1381 | asection *input_section; | |
1382 | bfd *input_bfd; | |
1383 | bfd_byte *contents; | |
1384 | ||
1385 | BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0); | |
1386 | ||
1387 | if (link_order->size == 0) | |
1388 | return true; | |
1389 | ||
1390 | input_section = link_order->u.indirect.section; | |
1391 | input_bfd = input_section->owner; | |
1392 | ||
1393 | BFD_ASSERT (input_section->output_section == output_section); | |
1394 | BFD_ASSERT (input_section->output_offset == link_order->offset); | |
1395 | BFD_ASSERT (bfd_section_size (input_bfd, input_section) == link_order->size); | |
1396 | ||
1397 | /* Get and relocate the section contents. */ | |
1398 | contents = (bfd_byte *) alloca (bfd_section_size (input_bfd, input_section)); | |
1399 | contents = (bfd_get_relocated_section_contents | |
1400 | (output_bfd, info, link_order, contents, info->relocateable, | |
1401 | bfd_get_outsymbols (input_bfd))); | |
1402 | ||
1403 | /* Output the section contents. */ | |
1404 | if (! bfd_set_section_contents (output_bfd, output_section, contents, | |
1405 | link_order->offset, link_order->size)) | |
1406 | return false; | |
1407 | ||
1408 | return true; | |
1409 | } | |
1410 | \f | |
1411 | /* Allocate a new link_order for a section. */ | |
1412 | ||
1413 | struct bfd_link_order * | |
1414 | bfd_new_link_order (abfd, section) | |
1415 | bfd *abfd; | |
1416 | asection *section; | |
1417 | { | |
1418 | struct bfd_link_order *new; | |
1419 | ||
1420 | new = ((struct bfd_link_order *) | |
1421 | bfd_alloc_by_size_t (abfd, sizeof (struct bfd_link_order))); | |
1422 | ||
1423 | new->type = bfd_undefined_link_order; | |
1424 | new->offset = 0; | |
1425 | new->size = 0; | |
1426 | new->next = (struct bfd_link_order *) NULL; | |
1427 | ||
1428 | if (section->link_order_tail != (struct bfd_link_order *) NULL) | |
1429 | section->link_order_tail->next = new; | |
1430 | else | |
1431 | section->link_order_head = new; | |
1432 | section->link_order_tail = new; | |
1433 | ||
1434 | return new; | |
1435 | } | |
1436 | ||
1437 | /* Default link order processing routine. */ | |
1438 | ||
1439 | boolean | |
1440 | _bfd_default_link_order (abfd, info, sec, link_order) | |
1441 | bfd *abfd; | |
1442 | struct bfd_link_info *info; | |
1443 | asection *sec; | |
1444 | struct bfd_link_order *link_order; | |
1445 | { | |
1446 | switch (link_order->type) | |
1447 | { | |
1448 | case bfd_undefined_link_order: | |
1449 | default: | |
1450 | abort (); | |
1451 | case bfd_indirect_link_order: | |
1452 | abort (); | |
1453 | case bfd_fill_link_order: | |
1454 | return default_fill_link_order (abfd, info, sec, link_order); | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | /* Default routine to handle a bfd_fill_link_order. */ | |
1459 | ||
1460 | static boolean | |
1461 | default_fill_link_order (abfd, info, sec, link_order) | |
1462 | bfd *abfd; | |
1463 | struct bfd_link_info *info; | |
1464 | asection *sec; | |
1465 | struct bfd_link_order *link_order; | |
1466 | { | |
1467 | size_t size; | |
1468 | char *space; | |
1469 | size_t i; | |
1470 | int fill; | |
1471 | ||
1472 | BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0); | |
1473 | ||
1474 | size = (size_t) link_order->size; | |
1475 | space = (char *) alloca (size); | |
1476 | fill = link_order->u.fill.value; | |
1477 | for (i = 0; i < size; i += 2) | |
1478 | space[i] = fill >> 8; | |
1479 | for (i = 1; i < size; i += 2) | |
1480 | space[i] = fill; | |
1481 | return bfd_set_section_contents (abfd, sec, space, | |
1482 | (file_ptr) link_order->offset, | |
1483 | link_order->size); | |
1484 | } |