]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* COFF specific linker code. |
5f771d47 | 2 | Copyright 1994, 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Ian Lance Taylor, Cygnus Support. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program 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 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program 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 this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* This file contains the COFF backend linker code. */ | |
22 | ||
23 | #include "bfd.h" | |
24 | #include "sysdep.h" | |
25 | #include "bfdlink.h" | |
26 | #include "libbfd.h" | |
27 | #include "coff/internal.h" | |
28 | #include "libcoff.h" | |
29 | ||
30 | static boolean coff_link_add_object_symbols | |
31 | PARAMS ((bfd *, struct bfd_link_info *)); | |
32 | static boolean coff_link_check_archive_element | |
33 | PARAMS ((bfd *, struct bfd_link_info *, boolean *)); | |
34 | static boolean coff_link_check_ar_symbols | |
35 | PARAMS ((bfd *, struct bfd_link_info *, boolean *)); | |
36 | static boolean coff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *)); | |
37 | static char *dores_com PARAMS ((char *, bfd *, int)); | |
38 | static char *get_name PARAMS ((char *, char **)); | |
39 | static int process_embedded_commands | |
40 | PARAMS ((bfd *, struct bfd_link_info *, bfd *)); | |
41 | static void mark_relocs PARAMS ((struct coff_final_link_info *, bfd *)); | |
42 | ||
e4202681 NC |
43 | /* Return true if SYM is a weak, external symbol. */ |
44 | #define IS_WEAK_EXTERNAL(abfd, sym) \ | |
45 | ((sym).n_sclass == C_WEAKEXT \ | |
46 | || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK)) | |
47 | ||
48 | /* Return true if SYM is an external symbol. */ | |
49 | #define IS_EXTERNAL(abfd, sym) \ | |
50 | ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym)) | |
51 | ||
b2d638c7 ILT |
52 | /* Define macros so that the ISFCN, et. al., macros work correctly. |
53 | These macros are defined in include/coff/internal.h in terms of | |
54 | N_TMASK, etc. These definitions require a user to define local | |
55 | variables with the appropriate names, and with values from the | |
56 | coff_data (abfd) structure. */ | |
57 | ||
58 | #define N_TMASK n_tmask | |
59 | #define N_BTSHFT n_btshft | |
60 | #define N_BTMASK n_btmask | |
61 | ||
252b5132 RH |
62 | /* Create an entry in a COFF linker hash table. */ |
63 | ||
64 | struct bfd_hash_entry * | |
65 | _bfd_coff_link_hash_newfunc (entry, table, string) | |
66 | struct bfd_hash_entry *entry; | |
67 | struct bfd_hash_table *table; | |
68 | const char *string; | |
69 | { | |
70 | struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry; | |
71 | ||
72 | /* Allocate the structure if it has not already been allocated by a | |
73 | subclass. */ | |
74 | if (ret == (struct coff_link_hash_entry *) NULL) | |
75 | ret = ((struct coff_link_hash_entry *) | |
76 | bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry))); | |
77 | if (ret == (struct coff_link_hash_entry *) NULL) | |
78 | return (struct bfd_hash_entry *) ret; | |
79 | ||
80 | /* Call the allocation method of the superclass. */ | |
81 | ret = ((struct coff_link_hash_entry *) | |
82 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
83 | table, string)); | |
84 | if (ret != (struct coff_link_hash_entry *) NULL) | |
85 | { | |
86 | /* Set local fields. */ | |
87 | ret->indx = -1; | |
88 | ret->type = T_NULL; | |
89 | ret->class = C_NULL; | |
90 | ret->numaux = 0; | |
91 | ret->auxbfd = NULL; | |
92 | ret->aux = NULL; | |
93 | } | |
94 | ||
95 | return (struct bfd_hash_entry *) ret; | |
96 | } | |
97 | ||
98 | /* Initialize a COFF linker hash table. */ | |
99 | ||
100 | boolean | |
101 | _bfd_coff_link_hash_table_init (table, abfd, newfunc) | |
102 | struct coff_link_hash_table *table; | |
103 | bfd *abfd; | |
104 | struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, | |
105 | struct bfd_hash_table *, | |
106 | const char *)); | |
107 | { | |
108 | table->stab_info = NULL; | |
109 | return _bfd_link_hash_table_init (&table->root, abfd, newfunc); | |
110 | } | |
111 | ||
112 | /* Create a COFF linker hash table. */ | |
113 | ||
114 | struct bfd_link_hash_table * | |
115 | _bfd_coff_link_hash_table_create (abfd) | |
116 | bfd *abfd; | |
117 | { | |
118 | struct coff_link_hash_table *ret; | |
119 | ||
120 | ret = ((struct coff_link_hash_table *) | |
121 | bfd_alloc (abfd, sizeof (struct coff_link_hash_table))); | |
122 | if (ret == NULL) | |
123 | return NULL; | |
124 | if (! _bfd_coff_link_hash_table_init (ret, abfd, | |
125 | _bfd_coff_link_hash_newfunc)) | |
126 | { | |
127 | bfd_release (abfd, ret); | |
128 | return (struct bfd_link_hash_table *) NULL; | |
129 | } | |
130 | return &ret->root; | |
131 | } | |
132 | ||
133 | /* Create an entry in a COFF debug merge hash table. */ | |
134 | ||
135 | struct bfd_hash_entry * | |
136 | _bfd_coff_debug_merge_hash_newfunc (entry, table, string) | |
137 | struct bfd_hash_entry *entry; | |
138 | struct bfd_hash_table *table; | |
139 | const char *string; | |
140 | { | |
141 | struct coff_debug_merge_hash_entry *ret = | |
142 | (struct coff_debug_merge_hash_entry *) entry; | |
143 | ||
144 | /* Allocate the structure if it has not already been allocated by a | |
145 | subclass. */ | |
146 | if (ret == (struct coff_debug_merge_hash_entry *) NULL) | |
147 | ret = ((struct coff_debug_merge_hash_entry *) | |
148 | bfd_hash_allocate (table, | |
149 | sizeof (struct coff_debug_merge_hash_entry))); | |
150 | if (ret == (struct coff_debug_merge_hash_entry *) NULL) | |
151 | return (struct bfd_hash_entry *) ret; | |
152 | ||
153 | /* Call the allocation method of the superclass. */ | |
154 | ret = ((struct coff_debug_merge_hash_entry *) | |
155 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); | |
156 | if (ret != (struct coff_debug_merge_hash_entry *) NULL) | |
157 | { | |
158 | /* Set local fields. */ | |
159 | ret->types = NULL; | |
160 | } | |
161 | ||
162 | return (struct bfd_hash_entry *) ret; | |
163 | } | |
164 | ||
165 | /* Given a COFF BFD, add symbols to the global hash table as | |
166 | appropriate. */ | |
167 | ||
168 | boolean | |
169 | _bfd_coff_link_add_symbols (abfd, info) | |
170 | bfd *abfd; | |
171 | struct bfd_link_info *info; | |
172 | { | |
173 | switch (bfd_get_format (abfd)) | |
174 | { | |
175 | case bfd_object: | |
176 | return coff_link_add_object_symbols (abfd, info); | |
177 | case bfd_archive: | |
178 | return (_bfd_generic_link_add_archive_symbols | |
179 | (abfd, info, coff_link_check_archive_element)); | |
180 | default: | |
181 | bfd_set_error (bfd_error_wrong_format); | |
182 | return false; | |
183 | } | |
184 | } | |
185 | ||
186 | /* Add symbols from a COFF object file. */ | |
187 | ||
188 | static boolean | |
189 | coff_link_add_object_symbols (abfd, info) | |
190 | bfd *abfd; | |
191 | struct bfd_link_info *info; | |
192 | { | |
193 | if (! _bfd_coff_get_external_symbols (abfd)) | |
194 | return false; | |
195 | if (! coff_link_add_symbols (abfd, info)) | |
196 | return false; | |
197 | ||
198 | if (! info->keep_memory) | |
199 | { | |
200 | if (! _bfd_coff_free_symbols (abfd)) | |
201 | return false; | |
202 | } | |
203 | return true; | |
204 | } | |
205 | ||
206 | /* Check a single archive element to see if we need to include it in | |
207 | the link. *PNEEDED is set according to whether this element is | |
208 | needed in the link or not. This is called via | |
209 | _bfd_generic_link_add_archive_symbols. */ | |
210 | ||
211 | static boolean | |
212 | coff_link_check_archive_element (abfd, info, pneeded) | |
213 | bfd *abfd; | |
214 | struct bfd_link_info *info; | |
215 | boolean *pneeded; | |
216 | { | |
217 | if (! _bfd_coff_get_external_symbols (abfd)) | |
218 | return false; | |
219 | ||
220 | if (! coff_link_check_ar_symbols (abfd, info, pneeded)) | |
221 | return false; | |
222 | ||
223 | if (*pneeded) | |
224 | { | |
225 | if (! coff_link_add_symbols (abfd, info)) | |
226 | return false; | |
227 | } | |
228 | ||
229 | if (! info->keep_memory || ! *pneeded) | |
230 | { | |
231 | if (! _bfd_coff_free_symbols (abfd)) | |
232 | return false; | |
233 | } | |
234 | ||
235 | return true; | |
236 | } | |
237 | ||
238 | /* Look through the symbols to see if this object file should be | |
239 | included in the link. */ | |
240 | ||
241 | static boolean | |
242 | coff_link_check_ar_symbols (abfd, info, pneeded) | |
243 | bfd *abfd; | |
244 | struct bfd_link_info *info; | |
245 | boolean *pneeded; | |
246 | { | |
252b5132 RH |
247 | bfd_size_type symesz; |
248 | bfd_byte *esym; | |
249 | bfd_byte *esym_end; | |
250 | ||
251 | *pneeded = false; | |
252 | ||
252b5132 RH |
253 | symesz = bfd_coff_symesz (abfd); |
254 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
255 | esym_end = esym + obj_raw_syment_count (abfd) * symesz; | |
256 | while (esym < esym_end) | |
257 | { | |
258 | struct internal_syment sym; | |
5d54c628 | 259 | enum coff_symbol_classification classification; |
252b5132 RH |
260 | |
261 | bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym); | |
262 | ||
5d54c628 ILT |
263 | classification = bfd_coff_classify_symbol (abfd, &sym); |
264 | if (classification == COFF_SYMBOL_GLOBAL | |
265 | || classification == COFF_SYMBOL_COMMON) | |
252b5132 RH |
266 | { |
267 | const char *name; | |
268 | char buf[SYMNMLEN + 1]; | |
269 | struct bfd_link_hash_entry *h; | |
270 | ||
271 | /* This symbol is externally visible, and is defined by this | |
272 | object file. */ | |
273 | ||
274 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
275 | if (name == NULL) | |
276 | return false; | |
277 | h = bfd_link_hash_lookup (info->hash, name, false, false, true); | |
278 | ||
279 | /* We are only interested in symbols that are currently | |
280 | undefined. If a symbol is currently known to be common, | |
281 | COFF linkers do not bring in an object file which defines | |
282 | it. */ | |
283 | if (h != (struct bfd_link_hash_entry *) NULL | |
284 | && h->type == bfd_link_hash_undefined) | |
285 | { | |
286 | if (! (*info->callbacks->add_archive_element) (info, abfd, name)) | |
287 | return false; | |
288 | *pneeded = true; | |
289 | return true; | |
290 | } | |
291 | } | |
292 | ||
293 | esym += (sym.n_numaux + 1) * symesz; | |
294 | } | |
295 | ||
296 | /* We do not need this object file. */ | |
297 | return true; | |
298 | } | |
299 | ||
300 | /* Add all the symbols from an object file to the hash table. */ | |
301 | ||
302 | static boolean | |
303 | coff_link_add_symbols (abfd, info) | |
304 | bfd *abfd; | |
305 | struct bfd_link_info *info; | |
306 | { | |
b2d638c7 ILT |
307 | unsigned int n_tmask = coff_data (abfd)->local_n_tmask; |
308 | unsigned int n_btshft = coff_data (abfd)->local_n_btshft; | |
309 | unsigned int n_btmask = coff_data (abfd)->local_n_btmask; | |
252b5132 RH |
310 | boolean keep_syms; |
311 | boolean default_copy; | |
312 | bfd_size_type symcount; | |
313 | struct coff_link_hash_entry **sym_hash; | |
314 | bfd_size_type symesz; | |
315 | bfd_byte *esym; | |
316 | bfd_byte *esym_end; | |
317 | ||
318 | /* Keep the symbols during this function, in case the linker needs | |
319 | to read the generic symbols in order to report an error message. */ | |
320 | keep_syms = obj_coff_keep_syms (abfd); | |
321 | obj_coff_keep_syms (abfd) = true; | |
322 | ||
252b5132 RH |
323 | if (info->keep_memory) |
324 | default_copy = false; | |
325 | else | |
326 | default_copy = true; | |
327 | ||
328 | symcount = obj_raw_syment_count (abfd); | |
329 | ||
330 | /* We keep a list of the linker hash table entries that correspond | |
331 | to particular symbols. */ | |
332 | sym_hash = ((struct coff_link_hash_entry **) | |
333 | bfd_alloc (abfd, | |
334 | ((size_t) symcount | |
335 | * sizeof (struct coff_link_hash_entry *)))); | |
336 | if (sym_hash == NULL && symcount != 0) | |
337 | goto error_return; | |
338 | obj_coff_sym_hashes (abfd) = sym_hash; | |
339 | memset (sym_hash, 0, | |
340 | (size_t) symcount * sizeof (struct coff_link_hash_entry *)); | |
341 | ||
342 | symesz = bfd_coff_symesz (abfd); | |
343 | BFD_ASSERT (symesz == bfd_coff_auxesz (abfd)); | |
344 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
345 | esym_end = esym + symcount * symesz; | |
346 | while (esym < esym_end) | |
347 | { | |
348 | struct internal_syment sym; | |
5d54c628 | 349 | enum coff_symbol_classification classification; |
252b5132 RH |
350 | boolean copy; |
351 | ||
352 | bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym); | |
353 | ||
5d54c628 ILT |
354 | classification = bfd_coff_classify_symbol (abfd, &sym); |
355 | if (classification != COFF_SYMBOL_LOCAL) | |
252b5132 RH |
356 | { |
357 | const char *name; | |
358 | char buf[SYMNMLEN + 1]; | |
359 | flagword flags; | |
360 | asection *section; | |
361 | bfd_vma value; | |
7fd9c191 | 362 | boolean addit; |
252b5132 RH |
363 | |
364 | /* This symbol is externally visible. */ | |
365 | ||
366 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
367 | if (name == NULL) | |
368 | goto error_return; | |
369 | ||
370 | /* We must copy the name into memory if we got it from the | |
371 | syment itself, rather than the string table. */ | |
372 | copy = default_copy; | |
373 | if (sym._n._n_n._n_zeroes != 0 | |
374 | || sym._n._n_n._n_offset == 0) | |
375 | copy = true; | |
376 | ||
377 | value = sym.n_value; | |
378 | ||
5d54c628 | 379 | switch (classification) |
252b5132 | 380 | { |
5d54c628 ILT |
381 | default: |
382 | abort (); | |
383 | ||
384 | case COFF_SYMBOL_GLOBAL: | |
252b5132 RH |
385 | flags = BSF_EXPORT | BSF_GLOBAL; |
386 | section = coff_section_from_bfd_index (abfd, sym.n_scnum); | |
387 | if (! obj_pe (abfd)) | |
388 | value -= section->vma; | |
5d54c628 ILT |
389 | break; |
390 | ||
391 | case COFF_SYMBOL_UNDEFINED: | |
392 | flags = 0; | |
393 | section = bfd_und_section_ptr; | |
394 | break; | |
395 | ||
396 | case COFF_SYMBOL_COMMON: | |
397 | flags = BSF_GLOBAL; | |
398 | section = bfd_com_section_ptr; | |
399 | break; | |
400 | ||
401 | case COFF_SYMBOL_PE_SECTION: | |
402 | flags = BSF_SECTION_SYM | BSF_GLOBAL; | |
403 | section = coff_section_from_bfd_index (abfd, sym.n_scnum); | |
404 | break; | |
252b5132 RH |
405 | } |
406 | ||
e4202681 | 407 | if (IS_WEAK_EXTERNAL (abfd, sym)) |
252b5132 RH |
408 | flags = BSF_WEAK; |
409 | ||
7fd9c191 ILT |
410 | addit = true; |
411 | ||
412 | /* In the PE format, section symbols actually refer to the | |
413 | start of the output section. We handle them specially | |
414 | here. */ | |
415 | if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0) | |
416 | { | |
417 | *sym_hash = coff_link_hash_lookup (coff_hash_table (info), | |
418 | name, false, copy, false); | |
419 | if (*sym_hash != NULL) | |
420 | { | |
421 | if (((*sym_hash)->coff_link_hash_flags | |
422 | & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0 | |
423 | && (*sym_hash)->root.type != bfd_link_hash_undefined | |
424 | && (*sym_hash)->root.type != bfd_link_hash_undefweak) | |
425 | (*_bfd_error_handler) | |
426 | ("Warning: symbol `%s' is both section and non-section", | |
427 | name); | |
428 | ||
429 | addit = false; | |
430 | } | |
431 | } | |
432 | ||
49147fca ILT |
433 | /* The Microsoft Visual C compiler does string pooling by |
434 | hashing the constants to an internal symbol name, and | |
435 | relying on the the linker comdat support to discard | |
436 | duplicate names. However, if one string is a literal and | |
437 | one is a data initializer, one will end up in the .data | |
438 | section and one will end up in the .rdata section. The | |
439 | Microsoft linker will combine them into the .data | |
440 | section, which seems to be wrong since it might cause the | |
441 | literal to change. | |
442 | ||
443 | As long as there are no external references to the | |
444 | symbols, which there shouldn't be, we can treat the .data | |
445 | and .rdata instances as separate symbols. The comdat | |
446 | code in the linker will do the appropriate merging. Here | |
447 | we avoid getting a multiple definition error for one of | |
448 | these special symbols. | |
449 | ||
450 | FIXME: I don't think this will work in the case where | |
451 | there are two object files which use the constants as a | |
452 | literal and two object files which use it as a data | |
453 | initializer. One or the other of the second object files | |
454 | is going to wind up with an inappropriate reference. */ | |
455 | if (obj_pe (abfd) | |
456 | && (classification == COFF_SYMBOL_GLOBAL | |
457 | || classification == COFF_SYMBOL_PE_SECTION) | |
458 | && section->comdat != NULL | |
459 | && strncmp (name, "??_", 3) == 0 | |
460 | && strcmp (name, section->comdat->name) == 0) | |
461 | { | |
462 | if (*sym_hash == NULL) | |
463 | *sym_hash = coff_link_hash_lookup (coff_hash_table (info), | |
464 | name, false, copy, false); | |
465 | if (*sym_hash != NULL | |
466 | && (*sym_hash)->root.type == bfd_link_hash_defined | |
467 | && (*sym_hash)->root.u.def.section->comdat != NULL | |
468 | && strcmp ((*sym_hash)->root.u.def.section->comdat->name, | |
469 | section->comdat->name) == 0) | |
470 | addit = false; | |
471 | } | |
472 | ||
7fd9c191 ILT |
473 | if (addit) |
474 | { | |
475 | if (! (bfd_coff_link_add_one_symbol | |
476 | (info, abfd, name, flags, section, value, | |
477 | (const char *) NULL, copy, false, | |
478 | (struct bfd_link_hash_entry **) sym_hash))) | |
479 | goto error_return; | |
480 | } | |
481 | ||
482 | if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0) | |
483 | (*sym_hash)->coff_link_hash_flags |= | |
484 | COFF_LINK_HASH_PE_SECTION_SYMBOL; | |
252b5132 | 485 | |
2820a0b7 ILT |
486 | /* Limit the alignment of a common symbol to the possible |
487 | alignment of a section. There is no point to permitting | |
488 | a higher alignment for a common symbol: we can not | |
489 | guarantee it, and it may cause us to allocate extra space | |
490 | in the common section. */ | |
252b5132 RH |
491 | if (section == bfd_com_section_ptr |
492 | && (*sym_hash)->root.type == bfd_link_hash_common | |
493 | && ((*sym_hash)->root.u.c.p->alignment_power | |
494 | > bfd_coff_default_section_alignment_power (abfd))) | |
495 | (*sym_hash)->root.u.c.p->alignment_power | |
496 | = bfd_coff_default_section_alignment_power (abfd); | |
497 | ||
498 | if (info->hash->creator->flavour == bfd_get_flavour (abfd)) | |
499 | { | |
5d3aaa74 ILT |
500 | /* If we don't have any symbol information currently in |
501 | the hash table, or if we are looking at a symbol | |
502 | definition, then update the symbol class and type in | |
503 | the hash table. */ | |
504 | if (((*sym_hash)->class == C_NULL | |
505 | && (*sym_hash)->type == T_NULL) | |
506 | || sym.n_scnum != 0 | |
507 | || (sym.n_value != 0 | |
508 | && (*sym_hash)->root.type != bfd_link_hash_defined | |
509 | && (*sym_hash)->root.type != bfd_link_hash_defweak)) | |
510 | { | |
511 | (*sym_hash)->class = sym.n_sclass; | |
512 | if (sym.n_type != T_NULL) | |
513 | { | |
514 | /* We want to warn if the type changed, but not | |
515 | if it changed from an unspecified type. | |
516 | Testing the whole type byte may work, but the | |
517 | change from (e.g.) a function of unspecified | |
518 | type to function of known type also wants to | |
519 | skip the warning. */ | |
520 | if ((*sym_hash)->type != T_NULL | |
521 | && (*sym_hash)->type != sym.n_type | |
522 | && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type) | |
523 | && (BTYPE ((*sym_hash)->type) == T_NULL | |
524 | || BTYPE (sym.n_type) == T_NULL))) | |
525 | (*_bfd_error_handler) | |
526 | (_("Warning: type of symbol `%s' changed from %d to %d in %s"), | |
527 | name, (*sym_hash)->type, sym.n_type, | |
528 | bfd_get_filename (abfd)); | |
529 | ||
530 | /* We don't want to change from a meaningful | |
531 | base type to a null one, but if we know | |
532 | nothing, take what little we might now know. */ | |
533 | if (BTYPE (sym.n_type) != T_NULL | |
534 | || (*sym_hash)->type == T_NULL) | |
535 | (*sym_hash)->type = sym.n_type; | |
536 | } | |
537 | (*sym_hash)->auxbfd = abfd; | |
252b5132 RH |
538 | if (sym.n_numaux != 0) |
539 | { | |
540 | union internal_auxent *alloc; | |
541 | unsigned int i; | |
542 | bfd_byte *eaux; | |
543 | union internal_auxent *iaux; | |
544 | ||
545 | (*sym_hash)->numaux = sym.n_numaux; | |
546 | alloc = ((union internal_auxent *) | |
547 | bfd_hash_allocate (&info->hash->table, | |
548 | (sym.n_numaux | |
549 | * sizeof (*alloc)))); | |
550 | if (alloc == NULL) | |
551 | goto error_return; | |
552 | for (i = 0, eaux = esym + symesz, iaux = alloc; | |
553 | i < sym.n_numaux; | |
554 | i++, eaux += symesz, iaux++) | |
555 | bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type, | |
556 | sym.n_sclass, i, sym.n_numaux, | |
557 | (PTR) iaux); | |
558 | (*sym_hash)->aux = alloc; | |
559 | } | |
560 | } | |
561 | } | |
5d54c628 ILT |
562 | |
563 | if (classification == COFF_SYMBOL_PE_SECTION | |
564 | && (*sym_hash)->numaux != 0) | |
565 | { | |
566 | /* Some PE sections (such as .bss) have a zero size in | |
567 | the section header, but a non-zero size in the AUX | |
568 | record. Correct that here. | |
569 | ||
570 | FIXME: This is not at all the right place to do this. | |
571 | For example, it won't help objdump. This needs to be | |
572 | done when we swap in the section header. */ | |
573 | ||
574 | BFD_ASSERT ((*sym_hash)->numaux == 1); | |
575 | if (section->_raw_size == 0) | |
576 | section->_raw_size = (*sym_hash)->aux[0].x_scn.x_scnlen; | |
577 | ||
578 | /* FIXME: We could test whether the section sizes | |
579 | matches the size in the aux entry, but apparently | |
580 | that sometimes fails unexpectedly. */ | |
581 | } | |
252b5132 RH |
582 | } |
583 | ||
584 | esym += (sym.n_numaux + 1) * symesz; | |
585 | sym_hash += sym.n_numaux + 1; | |
586 | } | |
587 | ||
588 | /* If this is a non-traditional, non-relocateable link, try to | |
589 | optimize the handling of any .stab/.stabstr sections. */ | |
590 | if (! info->relocateable | |
591 | && ! info->traditional_format | |
592 | && info->hash->creator->flavour == bfd_get_flavour (abfd) | |
593 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
594 | { | |
595 | asection *stab, *stabstr; | |
596 | ||
597 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
598 | if (stab != NULL) | |
599 | { | |
600 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
601 | ||
602 | if (stabstr != NULL) | |
603 | { | |
604 | struct coff_link_hash_table *table; | |
605 | struct coff_section_tdata *secdata; | |
606 | ||
607 | secdata = coff_section_data (abfd, stab); | |
608 | if (secdata == NULL) | |
609 | { | |
610 | stab->used_by_bfd = | |
611 | (PTR) bfd_zalloc (abfd, | |
612 | sizeof (struct coff_section_tdata)); | |
613 | if (stab->used_by_bfd == NULL) | |
614 | goto error_return; | |
615 | secdata = coff_section_data (abfd, stab); | |
616 | } | |
617 | ||
618 | table = coff_hash_table (info); | |
619 | ||
620 | if (! _bfd_link_section_stabs (abfd, &table->stab_info, | |
621 | stab, stabstr, | |
622 | &secdata->stab_info)) | |
623 | goto error_return; | |
624 | } | |
625 | } | |
626 | } | |
627 | ||
628 | obj_coff_keep_syms (abfd) = keep_syms; | |
629 | ||
630 | return true; | |
631 | ||
632 | error_return: | |
633 | obj_coff_keep_syms (abfd) = keep_syms; | |
634 | return false; | |
635 | } | |
636 | \f | |
637 | /* Do the final link step. */ | |
638 | ||
639 | boolean | |
640 | _bfd_coff_final_link (abfd, info) | |
641 | bfd *abfd; | |
642 | struct bfd_link_info *info; | |
643 | { | |
644 | bfd_size_type symesz; | |
645 | struct coff_final_link_info finfo; | |
646 | boolean debug_merge_allocated; | |
647 | boolean long_section_names; | |
648 | asection *o; | |
649 | struct bfd_link_order *p; | |
650 | size_t max_sym_count; | |
651 | size_t max_lineno_count; | |
652 | size_t max_reloc_count; | |
653 | size_t max_output_reloc_count; | |
654 | size_t max_contents_size; | |
655 | file_ptr rel_filepos; | |
656 | unsigned int relsz; | |
657 | file_ptr line_filepos; | |
658 | unsigned int linesz; | |
659 | bfd *sub; | |
660 | bfd_byte *external_relocs = NULL; | |
661 | char strbuf[STRING_SIZE_SIZE]; | |
662 | ||
663 | symesz = bfd_coff_symesz (abfd); | |
664 | ||
665 | finfo.info = info; | |
666 | finfo.output_bfd = abfd; | |
667 | finfo.strtab = NULL; | |
668 | finfo.section_info = NULL; | |
669 | finfo.last_file_index = -1; | |
670 | finfo.last_bf_index = -1; | |
671 | finfo.internal_syms = NULL; | |
672 | finfo.sec_ptrs = NULL; | |
673 | finfo.sym_indices = NULL; | |
674 | finfo.outsyms = NULL; | |
675 | finfo.linenos = NULL; | |
676 | finfo.contents = NULL; | |
677 | finfo.external_relocs = NULL; | |
678 | finfo.internal_relocs = NULL; | |
679 | finfo.global_to_static = false; | |
680 | debug_merge_allocated = false; | |
681 | ||
682 | coff_data (abfd)->link_info = info; | |
683 | ||
684 | finfo.strtab = _bfd_stringtab_init (); | |
685 | if (finfo.strtab == NULL) | |
686 | goto error_return; | |
687 | ||
688 | if (! coff_debug_merge_hash_table_init (&finfo.debug_merge)) | |
689 | goto error_return; | |
690 | debug_merge_allocated = true; | |
691 | ||
692 | /* Compute the file positions for all the sections. */ | |
693 | if (! abfd->output_has_begun) | |
694 | { | |
695 | if (! bfd_coff_compute_section_file_positions (abfd)) | |
696 | goto error_return; | |
697 | } | |
698 | ||
699 | /* Count the line numbers and relocation entries required for the | |
700 | output file. Set the file positions for the relocs. */ | |
701 | rel_filepos = obj_relocbase (abfd); | |
702 | relsz = bfd_coff_relsz (abfd); | |
703 | max_contents_size = 0; | |
704 | max_lineno_count = 0; | |
705 | max_reloc_count = 0; | |
706 | ||
707 | long_section_names = false; | |
708 | for (o = abfd->sections; o != NULL; o = o->next) | |
709 | { | |
710 | o->reloc_count = 0; | |
711 | o->lineno_count = 0; | |
712 | for (p = o->link_order_head; p != NULL; p = p->next) | |
713 | { | |
714 | if (p->type == bfd_indirect_link_order) | |
715 | { | |
716 | asection *sec; | |
717 | ||
718 | sec = p->u.indirect.section; | |
719 | ||
720 | /* Mark all sections which are to be included in the | |
721 | link. This will normally be every section. We need | |
722 | to do this so that we can identify any sections which | |
723 | the linker has decided to not include. */ | |
724 | sec->linker_mark = true; | |
725 | ||
726 | if (info->strip == strip_none | |
727 | || info->strip == strip_some) | |
728 | o->lineno_count += sec->lineno_count; | |
729 | ||
730 | if (info->relocateable) | |
731 | o->reloc_count += sec->reloc_count; | |
732 | ||
733 | if (sec->_raw_size > max_contents_size) | |
734 | max_contents_size = sec->_raw_size; | |
735 | if (sec->lineno_count > max_lineno_count) | |
736 | max_lineno_count = sec->lineno_count; | |
737 | if (sec->reloc_count > max_reloc_count) | |
738 | max_reloc_count = sec->reloc_count; | |
739 | } | |
740 | else if (info->relocateable | |
741 | && (p->type == bfd_section_reloc_link_order | |
742 | || p->type == bfd_symbol_reloc_link_order)) | |
743 | ++o->reloc_count; | |
744 | } | |
745 | if (o->reloc_count == 0) | |
746 | o->rel_filepos = 0; | |
747 | else | |
748 | { | |
749 | o->flags |= SEC_RELOC; | |
750 | o->rel_filepos = rel_filepos; | |
751 | rel_filepos += o->reloc_count * relsz; | |
752 | } | |
753 | ||
754 | if (bfd_coff_long_section_names (abfd) | |
755 | && strlen (o->name) > SCNNMLEN) | |
756 | { | |
757 | /* This section has a long name which must go in the string | |
758 | table. This must correspond to the code in | |
759 | coff_write_object_contents which puts the string index | |
760 | into the s_name field of the section header. That is why | |
761 | we pass hash as false. */ | |
762 | if (_bfd_stringtab_add (finfo.strtab, o->name, false, false) | |
763 | == (bfd_size_type) -1) | |
764 | goto error_return; | |
765 | long_section_names = true; | |
766 | } | |
767 | } | |
768 | ||
769 | /* If doing a relocateable link, allocate space for the pointers we | |
770 | need to keep. */ | |
771 | if (info->relocateable) | |
772 | { | |
773 | unsigned int i; | |
774 | ||
775 | /* We use section_count + 1, rather than section_count, because | |
776 | the target_index fields are 1 based. */ | |
777 | finfo.section_info = | |
778 | ((struct coff_link_section_info *) | |
779 | bfd_malloc ((abfd->section_count + 1) | |
780 | * sizeof (struct coff_link_section_info))); | |
781 | if (finfo.section_info == NULL) | |
782 | goto error_return; | |
783 | for (i = 0; i <= abfd->section_count; i++) | |
784 | { | |
785 | finfo.section_info[i].relocs = NULL; | |
786 | finfo.section_info[i].rel_hashes = NULL; | |
787 | } | |
788 | } | |
789 | ||
790 | /* We now know the size of the relocs, so we can determine the file | |
791 | positions of the line numbers. */ | |
792 | line_filepos = rel_filepos; | |
793 | linesz = bfd_coff_linesz (abfd); | |
794 | max_output_reloc_count = 0; | |
795 | for (o = abfd->sections; o != NULL; o = o->next) | |
796 | { | |
797 | if (o->lineno_count == 0) | |
798 | o->line_filepos = 0; | |
799 | else | |
800 | { | |
801 | o->line_filepos = line_filepos; | |
802 | line_filepos += o->lineno_count * linesz; | |
803 | } | |
804 | ||
805 | if (o->reloc_count != 0) | |
806 | { | |
807 | /* We don't know the indices of global symbols until we have | |
808 | written out all the local symbols. For each section in | |
809 | the output file, we keep an array of pointers to hash | |
810 | table entries. Each entry in the array corresponds to a | |
811 | reloc. When we find a reloc against a global symbol, we | |
812 | set the corresponding entry in this array so that we can | |
813 | fix up the symbol index after we have written out all the | |
814 | local symbols. | |
815 | ||
816 | Because of this problem, we also keep the relocs in | |
817 | memory until the end of the link. This wastes memory, | |
818 | but only when doing a relocateable link, which is not the | |
819 | common case. */ | |
820 | BFD_ASSERT (info->relocateable); | |
821 | finfo.section_info[o->target_index].relocs = | |
822 | ((struct internal_reloc *) | |
823 | bfd_malloc (o->reloc_count * sizeof (struct internal_reloc))); | |
824 | finfo.section_info[o->target_index].rel_hashes = | |
825 | ((struct coff_link_hash_entry **) | |
826 | bfd_malloc (o->reloc_count | |
827 | * sizeof (struct coff_link_hash_entry *))); | |
828 | if (finfo.section_info[o->target_index].relocs == NULL | |
829 | || finfo.section_info[o->target_index].rel_hashes == NULL) | |
830 | goto error_return; | |
831 | ||
832 | if (o->reloc_count > max_output_reloc_count) | |
833 | max_output_reloc_count = o->reloc_count; | |
834 | } | |
835 | ||
836 | /* Reset the reloc and lineno counts, so that we can use them to | |
837 | count the number of entries we have output so far. */ | |
838 | o->reloc_count = 0; | |
839 | o->lineno_count = 0; | |
840 | } | |
841 | ||
842 | obj_sym_filepos (abfd) = line_filepos; | |
843 | ||
844 | /* Figure out the largest number of symbols in an input BFD. Take | |
845 | the opportunity to clear the output_has_begun fields of all the | |
846 | input BFD's. */ | |
847 | max_sym_count = 0; | |
848 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
849 | { | |
850 | size_t sz; | |
851 | ||
852 | sub->output_has_begun = false; | |
853 | sz = obj_raw_syment_count (sub); | |
854 | if (sz > max_sym_count) | |
855 | max_sym_count = sz; | |
856 | } | |
857 | ||
858 | /* Allocate some buffers used while linking. */ | |
859 | finfo.internal_syms = ((struct internal_syment *) | |
860 | bfd_malloc (max_sym_count | |
861 | * sizeof (struct internal_syment))); | |
862 | finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count | |
863 | * sizeof (asection *)); | |
864 | finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long)); | |
865 | finfo.outsyms = ((bfd_byte *) | |
866 | bfd_malloc ((size_t) ((max_sym_count + 1) * symesz))); | |
867 | finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count | |
868 | * bfd_coff_linesz (abfd)); | |
869 | finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); | |
870 | finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz); | |
871 | if (! info->relocateable) | |
872 | finfo.internal_relocs = ((struct internal_reloc *) | |
873 | bfd_malloc (max_reloc_count | |
874 | * sizeof (struct internal_reloc))); | |
875 | if ((finfo.internal_syms == NULL && max_sym_count > 0) | |
876 | || (finfo.sec_ptrs == NULL && max_sym_count > 0) | |
877 | || (finfo.sym_indices == NULL && max_sym_count > 0) | |
878 | || finfo.outsyms == NULL | |
879 | || (finfo.linenos == NULL && max_lineno_count > 0) | |
880 | || (finfo.contents == NULL && max_contents_size > 0) | |
881 | || (finfo.external_relocs == NULL && max_reloc_count > 0) | |
882 | || (! info->relocateable | |
883 | && finfo.internal_relocs == NULL | |
884 | && max_reloc_count > 0)) | |
885 | goto error_return; | |
886 | ||
887 | /* We now know the position of everything in the file, except that | |
888 | we don't know the size of the symbol table and therefore we don't | |
889 | know where the string table starts. We just build the string | |
890 | table in memory as we go along. We process all the relocations | |
891 | for a single input file at once. */ | |
892 | obj_raw_syment_count (abfd) = 0; | |
893 | ||
894 | if (coff_backend_info (abfd)->_bfd_coff_start_final_link) | |
895 | { | |
896 | if (! bfd_coff_start_final_link (abfd, info)) | |
897 | goto error_return; | |
898 | } | |
899 | ||
900 | for (o = abfd->sections; o != NULL; o = o->next) | |
901 | { | |
902 | for (p = o->link_order_head; p != NULL; p = p->next) | |
903 | { | |
904 | if (p->type == bfd_indirect_link_order | |
9bd09e22 | 905 | && bfd_family_coff (p->u.indirect.section->owner)) |
252b5132 RH |
906 | { |
907 | sub = p->u.indirect.section->owner; | |
908 | if (! bfd_coff_link_output_has_begun (sub, & finfo)) | |
909 | { | |
910 | if (! _bfd_coff_link_input_bfd (&finfo, sub)) | |
911 | goto error_return; | |
912 | sub->output_has_begun = true; | |
913 | } | |
914 | } | |
915 | else if (p->type == bfd_section_reloc_link_order | |
916 | || p->type == bfd_symbol_reloc_link_order) | |
917 | { | |
918 | if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p)) | |
919 | goto error_return; | |
920 | } | |
921 | else | |
922 | { | |
923 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
924 | goto error_return; | |
925 | } | |
926 | } | |
927 | } | |
928 | ||
929 | if (! bfd_coff_final_link_postscript (abfd, & finfo)) | |
930 | goto error_return; | |
931 | ||
932 | /* Free up the buffers used by _bfd_coff_link_input_bfd. */ | |
933 | ||
934 | coff_debug_merge_hash_table_free (&finfo.debug_merge); | |
935 | debug_merge_allocated = false; | |
936 | ||
937 | if (finfo.internal_syms != NULL) | |
938 | { | |
939 | free (finfo.internal_syms); | |
940 | finfo.internal_syms = NULL; | |
941 | } | |
942 | if (finfo.sec_ptrs != NULL) | |
943 | { | |
944 | free (finfo.sec_ptrs); | |
945 | finfo.sec_ptrs = NULL; | |
946 | } | |
947 | if (finfo.sym_indices != NULL) | |
948 | { | |
949 | free (finfo.sym_indices); | |
950 | finfo.sym_indices = NULL; | |
951 | } | |
952 | if (finfo.linenos != NULL) | |
953 | { | |
954 | free (finfo.linenos); | |
955 | finfo.linenos = NULL; | |
956 | } | |
957 | if (finfo.contents != NULL) | |
958 | { | |
959 | free (finfo.contents); | |
960 | finfo.contents = NULL; | |
961 | } | |
962 | if (finfo.external_relocs != NULL) | |
963 | { | |
964 | free (finfo.external_relocs); | |
965 | finfo.external_relocs = NULL; | |
966 | } | |
967 | if (finfo.internal_relocs != NULL) | |
968 | { | |
969 | free (finfo.internal_relocs); | |
970 | finfo.internal_relocs = NULL; | |
971 | } | |
972 | ||
973 | /* The value of the last C_FILE symbol is supposed to be the symbol | |
974 | index of the first external symbol. Write it out again if | |
975 | necessary. */ | |
976 | if (finfo.last_file_index != -1 | |
977 | && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd)) | |
978 | { | |
979 | finfo.last_file.n_value = obj_raw_syment_count (abfd); | |
980 | bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file, | |
981 | (PTR) finfo.outsyms); | |
982 | if (bfd_seek (abfd, | |
983 | (obj_sym_filepos (abfd) | |
984 | + finfo.last_file_index * symesz), | |
985 | SEEK_SET) != 0 | |
986 | || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz) | |
987 | return false; | |
988 | } | |
989 | ||
990 | /* If doing task linking (ld --task-link) then make a pass through the | |
991 | global symbols, writing out any that are defined, and making them | |
992 | static. */ | |
993 | if (info->task_link) | |
994 | { | |
995 | finfo.failed = false; | |
996 | coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_task_globals, | |
997 | (PTR) &finfo); | |
998 | if (finfo.failed) | |
999 | goto error_return; | |
1000 | } | |
1001 | ||
1002 | /* Write out the global symbols. */ | |
1003 | finfo.failed = false; | |
1004 | coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym, | |
1005 | (PTR) &finfo); | |
1006 | if (finfo.failed) | |
1007 | goto error_return; | |
1008 | ||
1009 | /* The outsyms buffer is used by _bfd_coff_write_global_sym. */ | |
1010 | if (finfo.outsyms != NULL) | |
1011 | { | |
1012 | free (finfo.outsyms); | |
1013 | finfo.outsyms = NULL; | |
1014 | } | |
1015 | ||
1016 | if (info->relocateable && max_output_reloc_count > 0) | |
1017 | { | |
1018 | /* Now that we have written out all the global symbols, we know | |
1019 | the symbol indices to use for relocs against them, and we can | |
1020 | finally write out the relocs. */ | |
1021 | external_relocs = ((bfd_byte *) | |
1022 | bfd_malloc (max_output_reloc_count * relsz)); | |
1023 | if (external_relocs == NULL) | |
1024 | goto error_return; | |
1025 | ||
1026 | for (o = abfd->sections; o != NULL; o = o->next) | |
1027 | { | |
1028 | struct internal_reloc *irel; | |
1029 | struct internal_reloc *irelend; | |
1030 | struct coff_link_hash_entry **rel_hash; | |
1031 | bfd_byte *erel; | |
1032 | ||
1033 | if (o->reloc_count == 0) | |
1034 | continue; | |
1035 | ||
1036 | irel = finfo.section_info[o->target_index].relocs; | |
1037 | irelend = irel + o->reloc_count; | |
1038 | rel_hash = finfo.section_info[o->target_index].rel_hashes; | |
1039 | erel = external_relocs; | |
1040 | for (; irel < irelend; irel++, rel_hash++, erel += relsz) | |
1041 | { | |
1042 | if (*rel_hash != NULL) | |
1043 | { | |
1044 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
1045 | irel->r_symndx = (*rel_hash)->indx; | |
1046 | } | |
1047 | bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel); | |
1048 | } | |
1049 | ||
1050 | if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0 | |
1051 | || bfd_write ((PTR) external_relocs, relsz, o->reloc_count, | |
1052 | abfd) != relsz * o->reloc_count) | |
1053 | goto error_return; | |
1054 | } | |
1055 | ||
1056 | free (external_relocs); | |
1057 | external_relocs = NULL; | |
1058 | } | |
1059 | ||
1060 | /* Free up the section information. */ | |
1061 | if (finfo.section_info != NULL) | |
1062 | { | |
1063 | unsigned int i; | |
1064 | ||
1065 | for (i = 0; i < abfd->section_count; i++) | |
1066 | { | |
1067 | if (finfo.section_info[i].relocs != NULL) | |
1068 | free (finfo.section_info[i].relocs); | |
1069 | if (finfo.section_info[i].rel_hashes != NULL) | |
1070 | free (finfo.section_info[i].rel_hashes); | |
1071 | } | |
1072 | free (finfo.section_info); | |
1073 | finfo.section_info = NULL; | |
1074 | } | |
1075 | ||
1076 | /* If we have optimized stabs strings, output them. */ | |
1077 | if (coff_hash_table (info)->stab_info != NULL) | |
1078 | { | |
1079 | if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info)) | |
1080 | return false; | |
1081 | } | |
1082 | ||
1083 | /* Write out the string table. */ | |
1084 | if (obj_raw_syment_count (abfd) != 0 || long_section_names) | |
1085 | { | |
1086 | if (bfd_seek (abfd, | |
1087 | (obj_sym_filepos (abfd) | |
1088 | + obj_raw_syment_count (abfd) * symesz), | |
1089 | SEEK_SET) != 0) | |
1090 | return false; | |
1091 | ||
1092 | #if STRING_SIZE_SIZE == 4 | |
1093 | bfd_h_put_32 (abfd, | |
1094 | _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE, | |
1095 | (bfd_byte *) strbuf); | |
1096 | #else | |
1097 | #error Change bfd_h_put_32 | |
1098 | #endif | |
1099 | ||
1100 | if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE) | |
1101 | return false; | |
1102 | ||
1103 | if (! _bfd_stringtab_emit (abfd, finfo.strtab)) | |
1104 | return false; | |
1105 | } | |
1106 | ||
1107 | _bfd_stringtab_free (finfo.strtab); | |
1108 | ||
1109 | /* Setting bfd_get_symcount to 0 will cause write_object_contents to | |
1110 | not try to write out the symbols. */ | |
1111 | bfd_get_symcount (abfd) = 0; | |
1112 | ||
1113 | return true; | |
1114 | ||
1115 | error_return: | |
1116 | if (debug_merge_allocated) | |
1117 | coff_debug_merge_hash_table_free (&finfo.debug_merge); | |
1118 | if (finfo.strtab != NULL) | |
1119 | _bfd_stringtab_free (finfo.strtab); | |
1120 | if (finfo.section_info != NULL) | |
1121 | { | |
1122 | unsigned int i; | |
1123 | ||
1124 | for (i = 0; i < abfd->section_count; i++) | |
1125 | { | |
1126 | if (finfo.section_info[i].relocs != NULL) | |
1127 | free (finfo.section_info[i].relocs); | |
1128 | if (finfo.section_info[i].rel_hashes != NULL) | |
1129 | free (finfo.section_info[i].rel_hashes); | |
1130 | } | |
1131 | free (finfo.section_info); | |
1132 | } | |
1133 | if (finfo.internal_syms != NULL) | |
1134 | free (finfo.internal_syms); | |
1135 | if (finfo.sec_ptrs != NULL) | |
1136 | free (finfo.sec_ptrs); | |
1137 | if (finfo.sym_indices != NULL) | |
1138 | free (finfo.sym_indices); | |
1139 | if (finfo.outsyms != NULL) | |
1140 | free (finfo.outsyms); | |
1141 | if (finfo.linenos != NULL) | |
1142 | free (finfo.linenos); | |
1143 | if (finfo.contents != NULL) | |
1144 | free (finfo.contents); | |
1145 | if (finfo.external_relocs != NULL) | |
1146 | free (finfo.external_relocs); | |
1147 | if (finfo.internal_relocs != NULL) | |
1148 | free (finfo.internal_relocs); | |
1149 | if (external_relocs != NULL) | |
1150 | free (external_relocs); | |
1151 | return false; | |
1152 | } | |
1153 | ||
1154 | /* parse out a -heap <reserved>,<commit> line */ | |
1155 | ||
1156 | static char * | |
1157 | dores_com (ptr, output_bfd, heap) | |
1158 | char *ptr; | |
1159 | bfd *output_bfd; | |
1160 | int heap; | |
1161 | { | |
1162 | if (coff_data(output_bfd)->pe) | |
1163 | { | |
1164 | int val = strtoul (ptr, &ptr, 0); | |
1165 | if (heap) | |
1166 | pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve =val; | |
1167 | else | |
1168 | pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve =val; | |
1169 | ||
1170 | if (ptr[0] == ',') | |
1171 | { | |
1172 | int val = strtoul (ptr+1, &ptr, 0); | |
1173 | if (heap) | |
1174 | pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit =val; | |
1175 | else | |
1176 | pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit =val; | |
1177 | } | |
1178 | } | |
1179 | return ptr; | |
1180 | } | |
1181 | ||
1182 | static char *get_name(ptr, dst) | |
1183 | char *ptr; | |
1184 | char **dst; | |
1185 | { | |
1186 | while (*ptr == ' ') | |
1187 | ptr++; | |
1188 | *dst = ptr; | |
1189 | while (*ptr && *ptr != ' ') | |
1190 | ptr++; | |
1191 | *ptr = 0; | |
1192 | return ptr+1; | |
1193 | } | |
1194 | ||
1195 | /* Process any magic embedded commands in a section called .drectve */ | |
1196 | ||
1197 | static int | |
1198 | process_embedded_commands (output_bfd, info, abfd) | |
1199 | bfd *output_bfd; | |
5f771d47 | 1200 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1201 | bfd *abfd; |
1202 | { | |
1203 | asection *sec = bfd_get_section_by_name (abfd, ".drectve"); | |
1204 | char *s; | |
1205 | char *e; | |
1206 | char *copy; | |
1207 | if (!sec) | |
1208 | return 1; | |
1209 | ||
1210 | copy = bfd_malloc ((size_t) sec->_raw_size); | |
1211 | if (!copy) | |
1212 | return 0; | |
1213 | if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size)) | |
1214 | { | |
1215 | free (copy); | |
1216 | return 0; | |
1217 | } | |
1218 | e = copy + sec->_raw_size; | |
1219 | for (s = copy; s < e ; ) | |
1220 | { | |
1221 | if (s[0]!= '-') { | |
1222 | s++; | |
1223 | continue; | |
1224 | } | |
1225 | if (strncmp (s,"-attr", 5) == 0) | |
1226 | { | |
1227 | char *name; | |
1228 | char *attribs; | |
1229 | asection *asec; | |
1230 | ||
1231 | int loop = 1; | |
1232 | int had_write = 0; | |
1233 | int had_read = 0; | |
1234 | int had_exec= 0; | |
1235 | int had_shared= 0; | |
1236 | s += 5; | |
1237 | s = get_name(s, &name); | |
1238 | s = get_name(s, &attribs); | |
1239 | while (loop) { | |
1240 | switch (*attribs++) | |
1241 | { | |
1242 | case 'W': | |
1243 | had_write = 1; | |
1244 | break; | |
1245 | case 'R': | |
1246 | had_read = 1; | |
1247 | break; | |
1248 | case 'S': | |
1249 | had_shared = 1; | |
1250 | break; | |
1251 | case 'X': | |
1252 | had_exec = 1; | |
1253 | break; | |
1254 | default: | |
1255 | loop = 0; | |
1256 | } | |
1257 | } | |
1258 | asec = bfd_get_section_by_name (abfd, name); | |
1259 | if (asec) { | |
1260 | if (had_exec) | |
1261 | asec->flags |= SEC_CODE; | |
1262 | if (!had_write) | |
1263 | asec->flags |= SEC_READONLY; | |
1264 | } | |
1265 | } | |
1266 | else if (strncmp (s,"-heap", 5) == 0) | |
1267 | { | |
1268 | s = dores_com (s+5, output_bfd, 1); | |
1269 | } | |
1270 | else if (strncmp (s,"-stack", 6) == 0) | |
1271 | { | |
1272 | s = dores_com (s+6, output_bfd, 0); | |
1273 | } | |
1274 | else | |
1275 | s++; | |
1276 | } | |
1277 | free (copy); | |
1278 | return 1; | |
1279 | } | |
1280 | ||
1281 | /* Place a marker against all symbols which are used by relocations. | |
1282 | This marker can be picked up by the 'do we skip this symbol ?' | |
1283 | loop in _bfd_coff_link_input_bfd() and used to prevent skipping | |
1284 | that symbol. | |
1285 | */ | |
1286 | ||
1287 | static void | |
1288 | mark_relocs (finfo, input_bfd) | |
1289 | struct coff_final_link_info * finfo; | |
1290 | bfd * input_bfd; | |
1291 | { | |
1292 | asection * a; | |
1293 | ||
1294 | if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0) | |
1295 | return; | |
1296 | ||
1297 | for (a = input_bfd->sections; a != (asection *) NULL; a = a->next) | |
1298 | { | |
1299 | struct internal_reloc * internal_relocs; | |
1300 | struct internal_reloc * irel; | |
1301 | struct internal_reloc * irelend; | |
1302 | ||
1303 | ||
1304 | if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1) | |
1305 | continue; | |
1306 | ||
1307 | /* Read in the relocs. */ | |
1308 | internal_relocs = _bfd_coff_read_internal_relocs | |
1309 | (input_bfd, a, false, | |
1310 | finfo->external_relocs, | |
1311 | finfo->info->relocateable, | |
1312 | (finfo->info->relocateable | |
1313 | ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count) | |
1314 | : finfo->internal_relocs) | |
1315 | ); | |
1316 | ||
1317 | if (internal_relocs == NULL) | |
1318 | continue; | |
1319 | ||
1320 | irel = internal_relocs; | |
1321 | irelend = irel + a->reloc_count; | |
1322 | ||
1323 | /* Place a mark in the sym_indices array (whose entries have | |
1324 | been initialised to 0) for all of the symbols that are used | |
1325 | in the relocation table. This will then be picked up in the | |
1326 | skip/don't pass */ | |
1327 | ||
1328 | for (; irel < irelend; irel++) | |
1329 | { | |
1330 | finfo->sym_indices[ irel->r_symndx ] = -1; | |
1331 | } | |
1332 | } | |
1333 | } | |
1334 | ||
1335 | /* Link an input file into the linker output file. This function | |
1336 | handles all the sections and relocations of the input file at once. */ | |
1337 | ||
1338 | boolean | |
1339 | _bfd_coff_link_input_bfd (finfo, input_bfd) | |
1340 | struct coff_final_link_info *finfo; | |
1341 | bfd *input_bfd; | |
1342 | { | |
b2d638c7 ILT |
1343 | unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask; |
1344 | unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft; | |
1345 | #if 0 | |
1346 | unsigned int n_btmask = coff_data (input_bfd)->local_n_btmask; | |
1347 | #endif | |
252b5132 RH |
1348 | boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *, |
1349 | asection *, struct internal_reloc *, | |
1350 | boolean *)); | |
1351 | bfd *output_bfd; | |
1352 | const char *strings; | |
1353 | bfd_size_type syment_base; | |
252b5132 RH |
1354 | boolean copy, hash; |
1355 | bfd_size_type isymesz; | |
1356 | bfd_size_type osymesz; | |
1357 | bfd_size_type linesz; | |
1358 | bfd_byte *esym; | |
1359 | bfd_byte *esym_end; | |
1360 | struct internal_syment *isymp; | |
1361 | asection **secpp; | |
1362 | long *indexp; | |
1363 | unsigned long output_index; | |
1364 | bfd_byte *outsym; | |
1365 | struct coff_link_hash_entry **sym_hash; | |
1366 | asection *o; | |
1367 | ||
1368 | /* Move all the symbols to the output file. */ | |
1369 | ||
1370 | output_bfd = finfo->output_bfd; | |
252b5132 RH |
1371 | strings = NULL; |
1372 | syment_base = obj_raw_syment_count (output_bfd); | |
1373 | isymesz = bfd_coff_symesz (input_bfd); | |
1374 | osymesz = bfd_coff_symesz (output_bfd); | |
1375 | linesz = bfd_coff_linesz (input_bfd); | |
1376 | BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd)); | |
1377 | ||
252b5132 RH |
1378 | copy = false; |
1379 | if (! finfo->info->keep_memory) | |
1380 | copy = true; | |
1381 | hash = true; | |
1382 | if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0) | |
1383 | hash = false; | |
1384 | ||
1385 | if (! _bfd_coff_get_external_symbols (input_bfd)) | |
1386 | return false; | |
1387 | ||
1388 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); | |
1389 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
1390 | isymp = finfo->internal_syms; | |
1391 | secpp = finfo->sec_ptrs; | |
1392 | indexp = finfo->sym_indices; | |
1393 | output_index = syment_base; | |
1394 | outsym = finfo->outsyms; | |
1395 | ||
1396 | if (coff_data (output_bfd)->pe) | |
1397 | { | |
1398 | if (! process_embedded_commands (output_bfd, finfo->info, input_bfd)) | |
1399 | return false; | |
1400 | } | |
1401 | ||
1402 | /* If we are going to perform relocations and also strip/discard some symbols | |
1403 | then we must make sure that we do not strip/discard those symbols that are | |
1404 | going to be involved in the relocations */ | |
1405 | if (( finfo->info->strip != strip_none | |
1406 | || finfo->info->discard != discard_none) | |
1407 | && finfo->info->relocateable) | |
1408 | { | |
1409 | /* mark the symbol array as 'not-used' */ | |
1410 | memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp); | |
1411 | ||
1412 | mark_relocs (finfo, input_bfd); | |
1413 | } | |
1414 | ||
1415 | while (esym < esym_end) | |
1416 | { | |
1417 | struct internal_syment isym; | |
5d54c628 | 1418 | enum coff_symbol_classification classification; |
252b5132 RH |
1419 | boolean skip; |
1420 | boolean global; | |
1421 | boolean dont_skip_symbol; | |
1422 | int add; | |
1423 | ||
1424 | bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp); | |
1425 | ||
1426 | /* Make a copy of *isymp so that the relocate_section function | |
1427 | always sees the original values. This is more reliable than | |
1428 | always recomputing the symbol value even if we are stripping | |
1429 | the symbol. */ | |
1430 | isym = *isymp; | |
1431 | ||
5d54c628 ILT |
1432 | classification = bfd_coff_classify_symbol (input_bfd, &isym); |
1433 | switch (classification) | |
252b5132 | 1434 | { |
5d54c628 ILT |
1435 | default: |
1436 | abort (); | |
1437 | case COFF_SYMBOL_GLOBAL: | |
1438 | case COFF_SYMBOL_PE_SECTION: | |
1439 | case COFF_SYMBOL_LOCAL: | |
1440 | *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum); | |
1441 | break; | |
1442 | case COFF_SYMBOL_COMMON: | |
1443 | *secpp = bfd_com_section_ptr; | |
1444 | break; | |
1445 | case COFF_SYMBOL_UNDEFINED: | |
1446 | *secpp = bfd_und_section_ptr; | |
1447 | break; | |
252b5132 RH |
1448 | } |
1449 | ||
1450 | /* Extract the flag indicating if this symbol is used by a | |
1451 | relocation. */ | |
1452 | if ((finfo->info->strip != strip_none | |
1453 | || finfo->info->discard != discard_none) | |
1454 | && finfo->info->relocateable) | |
1455 | dont_skip_symbol = *indexp; | |
1456 | else | |
1457 | dont_skip_symbol = false; | |
1458 | ||
1459 | *indexp = -1; | |
1460 | ||
1461 | skip = false; | |
1462 | global = false; | |
1463 | add = 1 + isym.n_numaux; | |
1464 | ||
1465 | /* If we are stripping all symbols, we want to skip this one. */ | |
1466 | if (finfo->info->strip == strip_all && ! dont_skip_symbol) | |
1467 | skip = true; | |
1468 | ||
1469 | if (! skip) | |
1470 | { | |
5d54c628 | 1471 | switch (classification) |
252b5132 | 1472 | { |
5d54c628 ILT |
1473 | default: |
1474 | abort (); | |
1475 | case COFF_SYMBOL_GLOBAL: | |
1476 | case COFF_SYMBOL_COMMON: | |
1477 | case COFF_SYMBOL_PE_SECTION: | |
252b5132 RH |
1478 | /* This is a global symbol. Global symbols come at the |
1479 | end of the symbol table, so skip them for now. | |
1480 | Locally defined function symbols, however, are an | |
1481 | exception, and are not moved to the end. */ | |
1482 | global = true; | |
5d54c628 | 1483 | if (! ISFCN (isym.n_type)) |
252b5132 | 1484 | skip = true; |
5d54c628 ILT |
1485 | break; |
1486 | ||
1487 | case COFF_SYMBOL_UNDEFINED: | |
1488 | /* Undefined symbols are left for the end. */ | |
1489 | global = true; | |
1490 | skip = true; | |
1491 | break; | |
1492 | ||
1493 | case COFF_SYMBOL_LOCAL: | |
252b5132 RH |
1494 | /* This is a local symbol. Skip it if we are discarding |
1495 | local symbols. */ | |
1496 | if (finfo->info->discard == discard_all && ! dont_skip_symbol) | |
1497 | skip = true; | |
5d54c628 | 1498 | break; |
252b5132 RH |
1499 | } |
1500 | } | |
1501 | ||
1502 | /* If we stripping debugging symbols, and this is a debugging | |
1503 | symbol, then skip it. FIXME: gas sets the section to N_ABS | |
1504 | for some types of debugging symbols; I don't know if this is | |
1505 | a bug or not. In any case, we handle it here. */ | |
1506 | if (! skip | |
1507 | && finfo->info->strip == strip_debugger | |
1508 | && ! dont_skip_symbol | |
1509 | && (isym.n_scnum == N_DEBUG | |
1510 | || (isym.n_scnum == N_ABS | |
1511 | && (isym.n_sclass == C_AUTO | |
1512 | || isym.n_sclass == C_REG | |
1513 | || isym.n_sclass == C_MOS | |
1514 | || isym.n_sclass == C_MOE | |
1515 | || isym.n_sclass == C_MOU | |
1516 | || isym.n_sclass == C_ARG | |
1517 | || isym.n_sclass == C_REGPARM | |
1518 | || isym.n_sclass == C_FIELD | |
1519 | || isym.n_sclass == C_EOS)))) | |
1520 | skip = true; | |
1521 | ||
1522 | /* If some symbols are stripped based on the name, work out the | |
1523 | name and decide whether to skip this symbol. */ | |
1524 | if (! skip | |
1525 | && (finfo->info->strip == strip_some | |
1526 | || finfo->info->discard == discard_l)) | |
1527 | { | |
1528 | const char *name; | |
1529 | char buf[SYMNMLEN + 1]; | |
1530 | ||
1531 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf); | |
1532 | if (name == NULL) | |
1533 | return false; | |
1534 | ||
1535 | if (! dont_skip_symbol | |
1536 | && ((finfo->info->strip == strip_some | |
1537 | && (bfd_hash_lookup (finfo->info->keep_hash, name, false, | |
1538 | false) == NULL)) | |
1539 | || (! global | |
1540 | && finfo->info->discard == discard_l | |
1541 | && bfd_is_local_label_name (input_bfd, name)))) | |
1542 | skip = true; | |
1543 | } | |
1544 | ||
1545 | /* If this is an enum, struct, or union tag, see if we have | |
1546 | already output an identical type. */ | |
1547 | if (! skip | |
1548 | && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0 | |
1549 | && (isym.n_sclass == C_ENTAG | |
1550 | || isym.n_sclass == C_STRTAG | |
1551 | || isym.n_sclass == C_UNTAG) | |
1552 | && isym.n_numaux == 1) | |
1553 | { | |
1554 | const char *name; | |
1555 | char buf[SYMNMLEN + 1]; | |
1556 | struct coff_debug_merge_hash_entry *mh; | |
1557 | struct coff_debug_merge_type *mt; | |
1558 | union internal_auxent aux; | |
1559 | struct coff_debug_merge_element **epp; | |
1560 | bfd_byte *esl, *eslend; | |
1561 | struct internal_syment *islp; | |
1562 | ||
1563 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf); | |
1564 | if (name == NULL) | |
1565 | return false; | |
1566 | ||
1567 | /* Ignore fake names invented by compiler; treat them all as | |
1568 | the same name. */ | |
1569 | if (*name == '~' || *name == '.' || *name == '$' | |
1570 | || (*name == bfd_get_symbol_leading_char (input_bfd) | |
1571 | && (name[1] == '~' || name[1] == '.' || name[1] == '$'))) | |
1572 | name = ""; | |
1573 | ||
1574 | mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name, | |
1575 | true, true); | |
1576 | if (mh == NULL) | |
1577 | return false; | |
1578 | ||
1579 | /* Allocate memory to hold type information. If this turns | |
1580 | out to be a duplicate, we pass this address to | |
1581 | bfd_release. */ | |
1582 | mt = ((struct coff_debug_merge_type *) | |
1583 | bfd_alloc (input_bfd, | |
1584 | sizeof (struct coff_debug_merge_type))); | |
1585 | if (mt == NULL) | |
1586 | return false; | |
1587 | mt->class = isym.n_sclass; | |
1588 | ||
1589 | /* Pick up the aux entry, which points to the end of the tag | |
1590 | entries. */ | |
1591 | bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz), | |
1592 | isym.n_type, isym.n_sclass, 0, isym.n_numaux, | |
1593 | (PTR) &aux); | |
1594 | ||
1595 | /* Gather the elements. */ | |
1596 | epp = &mt->elements; | |
1597 | mt->elements = NULL; | |
1598 | islp = isymp + 2; | |
1599 | esl = esym + 2 * isymesz; | |
1600 | eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd) | |
1601 | + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz); | |
1602 | while (esl < eslend) | |
1603 | { | |
1604 | const char *elename; | |
1605 | char elebuf[SYMNMLEN + 1]; | |
00692651 | 1606 | char *name_copy; |
252b5132 RH |
1607 | |
1608 | bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp); | |
1609 | ||
1610 | *epp = ((struct coff_debug_merge_element *) | |
1611 | bfd_alloc (input_bfd, | |
1612 | sizeof (struct coff_debug_merge_element))); | |
1613 | if (*epp == NULL) | |
1614 | return false; | |
1615 | ||
1616 | elename = _bfd_coff_internal_syment_name (input_bfd, islp, | |
1617 | elebuf); | |
1618 | if (elename == NULL) | |
1619 | return false; | |
1620 | ||
00692651 ILT |
1621 | name_copy = (char *) bfd_alloc (input_bfd, |
1622 | strlen (elename) + 1); | |
1623 | if (name_copy == NULL) | |
252b5132 | 1624 | return false; |
00692651 | 1625 | strcpy (name_copy, elename); |
252b5132 | 1626 | |
00692651 | 1627 | (*epp)->name = name_copy; |
252b5132 RH |
1628 | (*epp)->type = islp->n_type; |
1629 | (*epp)->tagndx = 0; | |
1630 | if (islp->n_numaux >= 1 | |
1631 | && islp->n_type != T_NULL | |
1632 | && islp->n_sclass != C_EOS) | |
1633 | { | |
1634 | union internal_auxent eleaux; | |
1635 | long indx; | |
1636 | ||
1637 | bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz), | |
1638 | islp->n_type, islp->n_sclass, 0, | |
1639 | islp->n_numaux, (PTR) &eleaux); | |
1640 | indx = eleaux.x_sym.x_tagndx.l; | |
1641 | ||
1642 | /* FIXME: If this tagndx entry refers to a symbol | |
1643 | defined later in this file, we just ignore it. | |
1644 | Handling this correctly would be tedious, and may | |
1645 | not be required. */ | |
1646 | ||
1647 | if (indx > 0 | |
1648 | && (indx | |
1649 | < ((esym - | |
1650 | (bfd_byte *) obj_coff_external_syms (input_bfd)) | |
1651 | / (long) isymesz))) | |
1652 | { | |
1653 | (*epp)->tagndx = finfo->sym_indices[indx]; | |
1654 | if ((*epp)->tagndx < 0) | |
1655 | (*epp)->tagndx = 0; | |
1656 | } | |
1657 | } | |
1658 | epp = &(*epp)->next; | |
1659 | *epp = NULL; | |
1660 | ||
1661 | esl += (islp->n_numaux + 1) * isymesz; | |
1662 | islp += islp->n_numaux + 1; | |
1663 | } | |
1664 | ||
1665 | /* See if we already have a definition which matches this | |
1666 | type. We always output the type if it has no elements, | |
1667 | for simplicity. */ | |
1668 | if (mt->elements == NULL) | |
1669 | bfd_release (input_bfd, (PTR) mt); | |
1670 | else | |
1671 | { | |
1672 | struct coff_debug_merge_type *mtl; | |
1673 | ||
1674 | for (mtl = mh->types; mtl != NULL; mtl = mtl->next) | |
1675 | { | |
1676 | struct coff_debug_merge_element *me, *mel; | |
1677 | ||
1678 | if (mtl->class != mt->class) | |
1679 | continue; | |
1680 | ||
1681 | for (me = mt->elements, mel = mtl->elements; | |
1682 | me != NULL && mel != NULL; | |
1683 | me = me->next, mel = mel->next) | |
1684 | { | |
1685 | if (strcmp (me->name, mel->name) != 0 | |
1686 | || me->type != mel->type | |
1687 | || me->tagndx != mel->tagndx) | |
1688 | break; | |
1689 | } | |
1690 | ||
1691 | if (me == NULL && mel == NULL) | |
1692 | break; | |
1693 | } | |
1694 | ||
1695 | if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base) | |
1696 | { | |
1697 | /* This is the first definition of this type. */ | |
1698 | mt->indx = output_index; | |
1699 | mt->next = mh->types; | |
1700 | mh->types = mt; | |
1701 | } | |
1702 | else | |
1703 | { | |
1704 | /* This is a redefinition which can be merged. */ | |
1705 | bfd_release (input_bfd, (PTR) mt); | |
1706 | *indexp = mtl->indx; | |
1707 | add = (eslend - esym) / isymesz; | |
1708 | skip = true; | |
1709 | } | |
1710 | } | |
1711 | } | |
1712 | ||
1713 | /* We now know whether we are to skip this symbol or not. */ | |
1714 | if (! skip) | |
1715 | { | |
1716 | /* Adjust the symbol in order to output it. */ | |
1717 | ||
1718 | if (isym._n._n_n._n_zeroes == 0 | |
1719 | && isym._n._n_n._n_offset != 0) | |
1720 | { | |
1721 | const char *name; | |
1722 | bfd_size_type indx; | |
1723 | ||
1724 | /* This symbol has a long name. Enter it in the string | |
1725 | table we are building. Note that we do not check | |
1726 | bfd_coff_symname_in_debug. That is only true for | |
1727 | XCOFF, and XCOFF requires different linking code | |
1728 | anyhow. */ | |
1729 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, | |
1730 | (char *) NULL); | |
1731 | if (name == NULL) | |
1732 | return false; | |
1733 | indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy); | |
1734 | if (indx == (bfd_size_type) -1) | |
1735 | return false; | |
1736 | isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; | |
1737 | } | |
1738 | ||
8add8e30 | 1739 | switch (isym.n_sclass) |
252b5132 | 1740 | { |
8add8e30 ILT |
1741 | case C_AUTO: |
1742 | case C_MOS: | |
1743 | case C_EOS: | |
1744 | case C_MOE: | |
1745 | case C_MOU: | |
1746 | case C_UNTAG: | |
1747 | case C_STRTAG: | |
1748 | case C_ENTAG: | |
1749 | case C_TPDEF: | |
1750 | case C_ARG: | |
1751 | case C_USTATIC: | |
1752 | case C_REG: | |
1753 | case C_REGPARM: | |
1754 | case C_FIELD: | |
1755 | /* The symbol value should not be modified. */ | |
1756 | break; | |
1757 | ||
1758 | case C_FCN: | |
1759 | if (obj_pe (input_bfd) | |
1760 | && strcmp (isym.n_name, ".bf") != 0 | |
1761 | && isym.n_scnum > 0) | |
1762 | { | |
1763 | /* For PE, .lf and .ef get their value left alone, | |
1764 | while .bf gets relocated. However, they all have | |
1765 | "real" section numbers, and need to be moved into | |
1766 | the new section. */ | |
1767 | isym.n_scnum = (*secpp)->output_section->target_index; | |
1768 | break; | |
1769 | } | |
1770 | /* Fall through. */ | |
1771 | default: | |
1772 | case C_LABEL: /* Not completely sure about these 2 */ | |
1773 | case C_EXTDEF: | |
1774 | case C_BLOCK: | |
1775 | case C_EFCN: | |
1776 | case C_NULL: | |
1777 | case C_EXT: | |
1778 | case C_STAT: | |
1779 | case C_SECTION: | |
1780 | case C_NT_WEAK: | |
1781 | /* Compute new symbol location. */ | |
1782 | if (isym.n_scnum > 0) | |
1783 | { | |
1784 | isym.n_scnum = (*secpp)->output_section->target_index; | |
1785 | isym.n_value += (*secpp)->output_offset; | |
1786 | if (! obj_pe (input_bfd)) | |
1787 | isym.n_value -= (*secpp)->vma; | |
1788 | if (! obj_pe (finfo->output_bfd)) | |
1789 | isym.n_value += (*secpp)->output_section->vma; | |
1790 | } | |
1791 | break; | |
1792 | ||
1793 | case C_FILE: | |
1794 | /* The value of a C_FILE symbol is the symbol index of | |
1795 | the next C_FILE symbol. The value of the last C_FILE | |
1796 | symbol is the symbol index to the first external | |
1797 | symbol (actually, coff_renumber_symbols does not get | |
1798 | this right--it just sets the value of the last C_FILE | |
1799 | symbol to zero--and nobody has ever complained about | |
1800 | it). We try to get this right, below, just before we | |
1801 | write the symbols out, but in the general case we may | |
1802 | have to write the symbol out twice. */ | |
252b5132 | 1803 | |
252b5132 RH |
1804 | if (finfo->last_file_index != -1 |
1805 | && finfo->last_file.n_value != (long) output_index) | |
1806 | { | |
8add8e30 ILT |
1807 | /* We must correct the value of the last C_FILE |
1808 | entry. */ | |
252b5132 RH |
1809 | finfo->last_file.n_value = output_index; |
1810 | if ((bfd_size_type) finfo->last_file_index >= syment_base) | |
1811 | { | |
1812 | /* The last C_FILE symbol is in this input file. */ | |
1813 | bfd_coff_swap_sym_out (output_bfd, | |
1814 | (PTR) &finfo->last_file, | |
1815 | (PTR) (finfo->outsyms | |
1816 | + ((finfo->last_file_index | |
1817 | - syment_base) | |
1818 | * osymesz))); | |
1819 | } | |
1820 | else | |
1821 | { | |
1822 | /* We have already written out the last C_FILE | |
1823 | symbol. We need to write it out again. We | |
1824 | borrow *outsym temporarily. */ | |
1825 | bfd_coff_swap_sym_out (output_bfd, | |
1826 | (PTR) &finfo->last_file, | |
1827 | (PTR) outsym); | |
1828 | if (bfd_seek (output_bfd, | |
1829 | (obj_sym_filepos (output_bfd) | |
1830 | + finfo->last_file_index * osymesz), | |
1831 | SEEK_SET) != 0 | |
1832 | || (bfd_write (outsym, osymesz, 1, output_bfd) | |
1833 | != osymesz)) | |
1834 | return false; | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | finfo->last_file_index = output_index; | |
1839 | finfo->last_file = isym; | |
8add8e30 | 1840 | break; |
252b5132 RH |
1841 | } |
1842 | ||
1843 | /* If doing task linking, convert normal global function symbols to | |
e4202681 NC |
1844 | static functions. */ |
1845 | if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym)) | |
252b5132 RH |
1846 | isym.n_sclass = C_STAT; |
1847 | ||
1848 | /* Output the symbol. */ | |
1849 | ||
1850 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym); | |
1851 | ||
1852 | *indexp = output_index; | |
1853 | ||
1854 | if (global) | |
1855 | { | |
1856 | long indx; | |
1857 | struct coff_link_hash_entry *h; | |
1858 | ||
1859 | indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd)) | |
1860 | / isymesz); | |
1861 | h = obj_coff_sym_hashes (input_bfd)[indx]; | |
1862 | if (h == NULL) | |
1863 | { | |
1864 | /* This can happen if there were errors earlier in | |
1865 | the link. */ | |
1866 | bfd_set_error (bfd_error_bad_value); | |
1867 | return false; | |
1868 | } | |
1869 | h->indx = output_index; | |
1870 | } | |
1871 | ||
1872 | output_index += add; | |
1873 | outsym += add * osymesz; | |
1874 | } | |
1875 | ||
1876 | esym += add * isymesz; | |
1877 | isymp += add; | |
1878 | ++secpp; | |
1879 | ++indexp; | |
1880 | for (--add; add > 0; --add) | |
1881 | { | |
1882 | *secpp++ = NULL; | |
1883 | *indexp++ = -1; | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | /* Fix up the aux entries. This must be done in a separate pass, | |
1888 | because we don't know the correct symbol indices until we have | |
1889 | already decided which symbols we are going to keep. */ | |
1890 | ||
1891 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); | |
1892 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
1893 | isymp = finfo->internal_syms; | |
1894 | indexp = finfo->sym_indices; | |
1895 | sym_hash = obj_coff_sym_hashes (input_bfd); | |
1896 | outsym = finfo->outsyms; | |
1897 | while (esym < esym_end) | |
1898 | { | |
1899 | int add; | |
1900 | ||
1901 | add = 1 + isymp->n_numaux; | |
1902 | ||
1903 | if ((*indexp < 0 | |
1904 | || (bfd_size_type) *indexp < syment_base) | |
1905 | && (*sym_hash == NULL | |
1906 | || (*sym_hash)->auxbfd != input_bfd)) | |
1907 | esym += add * isymesz; | |
1908 | else | |
1909 | { | |
1910 | struct coff_link_hash_entry *h; | |
1911 | int i; | |
1912 | ||
1913 | h = NULL; | |
1914 | if (*indexp < 0) | |
1915 | { | |
1916 | h = *sym_hash; | |
1917 | ||
1918 | /* The m68k-motorola-sysv assembler will sometimes | |
1919 | generate two symbols with the same name, but only one | |
1920 | will have aux entries. */ | |
1921 | BFD_ASSERT (isymp->n_numaux == 0 | |
1922 | || h->numaux == isymp->n_numaux); | |
1923 | } | |
1924 | ||
1925 | esym += isymesz; | |
1926 | ||
1927 | if (h == NULL) | |
1928 | outsym += osymesz; | |
1929 | ||
1930 | /* Handle the aux entries. This handling is based on | |
1931 | coff_pointerize_aux. I don't know if it always correct. */ | |
1932 | for (i = 0; i < isymp->n_numaux && esym < esym_end; i++) | |
1933 | { | |
1934 | union internal_auxent aux; | |
1935 | union internal_auxent *auxp; | |
1936 | ||
1937 | if (h != NULL) | |
1938 | auxp = h->aux + i; | |
1939 | else | |
1940 | { | |
1941 | bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type, | |
1942 | isymp->n_sclass, i, isymp->n_numaux, | |
1943 | (PTR) &aux); | |
1944 | auxp = &aux; | |
1945 | } | |
1946 | ||
1947 | if (isymp->n_sclass == C_FILE) | |
1948 | { | |
1949 | /* If this is a long filename, we must put it in the | |
1950 | string table. */ | |
1951 | if (auxp->x_file.x_n.x_zeroes == 0 | |
1952 | && auxp->x_file.x_n.x_offset != 0) | |
1953 | { | |
1954 | const char *filename; | |
1955 | bfd_size_type indx; | |
1956 | ||
1957 | BFD_ASSERT (auxp->x_file.x_n.x_offset | |
1958 | >= STRING_SIZE_SIZE); | |
1959 | if (strings == NULL) | |
1960 | { | |
1961 | strings = _bfd_coff_read_string_table (input_bfd); | |
1962 | if (strings == NULL) | |
1963 | return false; | |
1964 | } | |
1965 | filename = strings + auxp->x_file.x_n.x_offset; | |
1966 | indx = _bfd_stringtab_add (finfo->strtab, filename, | |
1967 | hash, copy); | |
1968 | if (indx == (bfd_size_type) -1) | |
1969 | return false; | |
1970 | auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx; | |
1971 | } | |
1972 | } | |
1973 | else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL) | |
1974 | { | |
1975 | unsigned long indx; | |
1976 | ||
1977 | if (ISFCN (isymp->n_type) | |
1978 | || ISTAG (isymp->n_sclass) | |
1979 | || isymp->n_sclass == C_BLOCK | |
1980 | || isymp->n_sclass == C_FCN) | |
1981 | { | |
1982 | indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l; | |
1983 | if (indx > 0 | |
1984 | && indx < obj_raw_syment_count (input_bfd)) | |
1985 | { | |
1986 | /* We look forward through the symbol for | |
1987 | the index of the next symbol we are going | |
1988 | to include. I don't know if this is | |
1989 | entirely right. */ | |
1990 | while ((finfo->sym_indices[indx] < 0 | |
1991 | || ((bfd_size_type) finfo->sym_indices[indx] | |
1992 | < syment_base)) | |
1993 | && indx < obj_raw_syment_count (input_bfd)) | |
1994 | ++indx; | |
1995 | if (indx >= obj_raw_syment_count (input_bfd)) | |
1996 | indx = output_index; | |
1997 | else | |
1998 | indx = finfo->sym_indices[indx]; | |
1999 | auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx; | |
2000 | } | |
2001 | } | |
2002 | ||
2003 | indx = auxp->x_sym.x_tagndx.l; | |
2004 | if (indx > 0 && indx < obj_raw_syment_count (input_bfd)) | |
2005 | { | |
2006 | long symindx; | |
2007 | ||
2008 | symindx = finfo->sym_indices[indx]; | |
2009 | if (symindx < 0) | |
2010 | auxp->x_sym.x_tagndx.l = 0; | |
2011 | else | |
2012 | auxp->x_sym.x_tagndx.l = symindx; | |
2013 | } | |
2014 | ||
2015 | /* The .bf symbols are supposed to be linked through | |
2016 | the endndx field. We need to carry this list | |
2017 | across object files. */ | |
2018 | if (i == 0 | |
2019 | && h == NULL | |
2020 | && isymp->n_sclass == C_FCN | |
2021 | && (isymp->_n._n_n._n_zeroes != 0 | |
2022 | || isymp->_n._n_n._n_offset == 0) | |
2023 | && isymp->_n._n_name[0] == '.' | |
2024 | && isymp->_n._n_name[1] == 'b' | |
2025 | && isymp->_n._n_name[2] == 'f' | |
2026 | && isymp->_n._n_name[3] == '\0') | |
2027 | { | |
2028 | if (finfo->last_bf_index != -1) | |
2029 | { | |
2030 | finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l = | |
2031 | *indexp; | |
2032 | ||
2033 | if ((bfd_size_type) finfo->last_bf_index | |
2034 | >= syment_base) | |
2035 | { | |
2036 | PTR auxout; | |
2037 | ||
2038 | /* The last .bf symbol is in this input | |
2039 | file. This will only happen if the | |
2040 | assembler did not set up the .bf | |
2041 | endndx symbols correctly. */ | |
2042 | auxout = (PTR) (finfo->outsyms | |
2043 | + ((finfo->last_bf_index | |
2044 | - syment_base) | |
2045 | * osymesz)); | |
2046 | bfd_coff_swap_aux_out (output_bfd, | |
2047 | (PTR) &finfo->last_bf, | |
2048 | isymp->n_type, | |
2049 | isymp->n_sclass, | |
2050 | 0, isymp->n_numaux, | |
2051 | auxout); | |
2052 | } | |
2053 | else | |
2054 | { | |
2055 | /* We have already written out the last | |
2056 | .bf aux entry. We need to write it | |
2057 | out again. We borrow *outsym | |
2058 | temporarily. FIXME: This case should | |
2059 | be made faster. */ | |
2060 | bfd_coff_swap_aux_out (output_bfd, | |
2061 | (PTR) &finfo->last_bf, | |
2062 | isymp->n_type, | |
2063 | isymp->n_sclass, | |
2064 | 0, isymp->n_numaux, | |
2065 | (PTR) outsym); | |
2066 | if (bfd_seek (output_bfd, | |
2067 | (obj_sym_filepos (output_bfd) | |
2068 | + finfo->last_bf_index * osymesz), | |
2069 | SEEK_SET) != 0 | |
2070 | || bfd_write (outsym, osymesz, 1, | |
2071 | output_bfd) != osymesz) | |
2072 | return false; | |
2073 | } | |
2074 | } | |
2075 | ||
2076 | if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0) | |
2077 | finfo->last_bf_index = -1; | |
2078 | else | |
2079 | { | |
2080 | /* The endndx field of this aux entry must | |
2081 | be updated with the symbol number of the | |
2082 | next .bf symbol. */ | |
2083 | finfo->last_bf = *auxp; | |
2084 | finfo->last_bf_index = (((outsym - finfo->outsyms) | |
2085 | / osymesz) | |
2086 | + syment_base); | |
2087 | } | |
2088 | } | |
2089 | } | |
2090 | ||
2091 | if (h == NULL) | |
2092 | { | |
2093 | bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type, | |
2094 | isymp->n_sclass, i, isymp->n_numaux, | |
2095 | (PTR) outsym); | |
2096 | outsym += osymesz; | |
2097 | } | |
2098 | ||
2099 | esym += isymesz; | |
2100 | } | |
2101 | } | |
2102 | ||
2103 | indexp += add; | |
2104 | isymp += add; | |
2105 | sym_hash += add; | |
2106 | } | |
2107 | ||
2108 | /* Relocate the line numbers, unless we are stripping them. */ | |
2109 | if (finfo->info->strip == strip_none | |
2110 | || finfo->info->strip == strip_some) | |
2111 | { | |
2112 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
2113 | { | |
2114 | bfd_vma offset; | |
2115 | bfd_byte *eline; | |
2116 | bfd_byte *elineend; | |
b545f675 ILT |
2117 | bfd_byte *oeline; |
2118 | boolean skipping; | |
252b5132 RH |
2119 | |
2120 | /* FIXME: If SEC_HAS_CONTENTS is not for the section, then | |
2121 | build_link_order in ldwrite.c will not have created a | |
2122 | link order, which means that we will not have seen this | |
2123 | input section in _bfd_coff_final_link, which means that | |
2124 | we will not have allocated space for the line numbers of | |
2125 | this section. I don't think line numbers can be | |
2126 | meaningful for a section which does not have | |
2127 | SEC_HAS_CONTENTS set, but, if they do, this must be | |
2128 | changed. */ | |
2129 | if (o->lineno_count == 0 | |
2130 | || (o->output_section->flags & SEC_HAS_CONTENTS) == 0) | |
2131 | continue; | |
2132 | ||
2133 | if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0 | |
2134 | || bfd_read (finfo->linenos, linesz, o->lineno_count, | |
2135 | input_bfd) != linesz * o->lineno_count) | |
2136 | return false; | |
2137 | ||
2138 | offset = o->output_section->vma + o->output_offset - o->vma; | |
2139 | eline = finfo->linenos; | |
b545f675 | 2140 | oeline = finfo->linenos; |
252b5132 | 2141 | elineend = eline + linesz * o->lineno_count; |
b545f675 | 2142 | skipping = false; |
252b5132 RH |
2143 | for (; eline < elineend; eline += linesz) |
2144 | { | |
2145 | struct internal_lineno iline; | |
2146 | ||
2147 | bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline); | |
2148 | ||
2149 | if (iline.l_lnno != 0) | |
2150 | iline.l_addr.l_paddr += offset; | |
2151 | else if (iline.l_addr.l_symndx >= 0 | |
2152 | && ((unsigned long) iline.l_addr.l_symndx | |
2153 | < obj_raw_syment_count (input_bfd))) | |
2154 | { | |
2155 | long indx; | |
2156 | ||
2157 | indx = finfo->sym_indices[iline.l_addr.l_symndx]; | |
2158 | ||
2159 | if (indx < 0) | |
2160 | { | |
2161 | /* These line numbers are attached to a symbol | |
b545f675 ILT |
2162 | which we are stripping. We must discard the |
2163 | line numbers because reading them back with | |
2164 | no associated symbol (or associating them all | |
2165 | with symbol #0) will fail. We can't regain | |
2166 | the space in the output file, but at least | |
2167 | they're dense. */ | |
2168 | skipping = true; | |
252b5132 RH |
2169 | } |
2170 | else | |
2171 | { | |
2172 | struct internal_syment is; | |
2173 | union internal_auxent ia; | |
2174 | ||
2175 | /* Fix up the lnnoptr field in the aux entry of | |
2176 | the symbol. It turns out that we can't do | |
2177 | this when we modify the symbol aux entries, | |
2178 | because gas sometimes screws up the lnnoptr | |
2179 | field and makes it an offset from the start | |
2180 | of the line numbers rather than an absolute | |
2181 | file index. */ | |
2182 | bfd_coff_swap_sym_in (output_bfd, | |
2183 | (PTR) (finfo->outsyms | |
2184 | + ((indx - syment_base) | |
2185 | * osymesz)), | |
2186 | (PTR) &is); | |
2187 | if ((ISFCN (is.n_type) | |
2188 | || is.n_sclass == C_BLOCK) | |
2189 | && is.n_numaux >= 1) | |
2190 | { | |
2191 | PTR auxptr; | |
2192 | ||
2193 | auxptr = (PTR) (finfo->outsyms | |
2194 | + ((indx - syment_base + 1) | |
2195 | * osymesz)); | |
2196 | bfd_coff_swap_aux_in (output_bfd, auxptr, | |
2197 | is.n_type, is.n_sclass, | |
2198 | 0, is.n_numaux, (PTR) &ia); | |
2199 | ia.x_sym.x_fcnary.x_fcn.x_lnnoptr = | |
2200 | (o->output_section->line_filepos | |
2201 | + o->output_section->lineno_count * linesz | |
2202 | + eline - finfo->linenos); | |
2203 | bfd_coff_swap_aux_out (output_bfd, (PTR) &ia, | |
2204 | is.n_type, is.n_sclass, 0, | |
2205 | is.n_numaux, auxptr); | |
2206 | } | |
b545f675 | 2207 | |
60bcf0fa | 2208 | skipping = false; |
252b5132 RH |
2209 | } |
2210 | ||
2211 | iline.l_addr.l_symndx = indx; | |
2212 | } | |
2213 | ||
b545f675 ILT |
2214 | if (!skipping) |
2215 | { | |
2216 | bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, | |
2217 | (PTR) oeline); | |
2218 | oeline += linesz; | |
2219 | } | |
252b5132 RH |
2220 | } |
2221 | ||
2222 | if (bfd_seek (output_bfd, | |
2223 | (o->output_section->line_filepos | |
2224 | + o->output_section->lineno_count * linesz), | |
2225 | SEEK_SET) != 0 | |
b545f675 ILT |
2226 | || (bfd_write (finfo->linenos, 1, oeline - finfo->linenos, |
2227 | output_bfd) | |
2228 | != (bfd_size_type) (oeline - finfo->linenos))) | |
252b5132 RH |
2229 | return false; |
2230 | ||
b545f675 ILT |
2231 | o->output_section->lineno_count += |
2232 | (oeline - finfo->linenos) / linesz; | |
252b5132 RH |
2233 | } |
2234 | } | |
2235 | ||
2236 | /* If we swapped out a C_FILE symbol, guess that the next C_FILE | |
2237 | symbol will be the first symbol in the next input file. In the | |
2238 | normal case, this will save us from writing out the C_FILE symbol | |
2239 | again. */ | |
2240 | if (finfo->last_file_index != -1 | |
2241 | && (bfd_size_type) finfo->last_file_index >= syment_base) | |
2242 | { | |
2243 | finfo->last_file.n_value = output_index; | |
2244 | bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file, | |
2245 | (PTR) (finfo->outsyms | |
2246 | + ((finfo->last_file_index - syment_base) | |
2247 | * osymesz))); | |
2248 | } | |
2249 | ||
2250 | /* Write the modified symbols to the output file. */ | |
2251 | if (outsym > finfo->outsyms) | |
2252 | { | |
2253 | if (bfd_seek (output_bfd, | |
2254 | obj_sym_filepos (output_bfd) + syment_base * osymesz, | |
2255 | SEEK_SET) != 0 | |
2256 | || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, | |
2257 | output_bfd) | |
2258 | != (bfd_size_type) (outsym - finfo->outsyms))) | |
2259 | return false; | |
2260 | ||
2261 | BFD_ASSERT ((obj_raw_syment_count (output_bfd) | |
2262 | + (outsym - finfo->outsyms) / osymesz) | |
2263 | == output_index); | |
2264 | ||
2265 | obj_raw_syment_count (output_bfd) = output_index; | |
2266 | } | |
2267 | ||
2268 | /* Relocate the contents of each section. */ | |
2269 | adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx; | |
2270 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
2271 | { | |
2272 | bfd_byte *contents; | |
2273 | struct coff_section_tdata *secdata; | |
2274 | ||
2275 | if (! o->linker_mark) | |
2276 | { | |
2277 | /* This section was omitted from the link. */ | |
2278 | continue; | |
2279 | } | |
2280 | ||
2281 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
2282 | || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0)) | |
2283 | { | |
2284 | if ((o->flags & SEC_RELOC) != 0 | |
2285 | && o->reloc_count != 0) | |
2286 | { | |
2287 | ((*_bfd_error_handler) | |
2288 | (_("%s: relocs in section `%s', but it has no contents"), | |
2289 | bfd_get_filename (input_bfd), | |
2290 | bfd_get_section_name (input_bfd, o))); | |
2291 | bfd_set_error (bfd_error_no_contents); | |
2292 | return false; | |
2293 | } | |
2294 | ||
2295 | continue; | |
2296 | } | |
2297 | ||
2298 | secdata = coff_section_data (input_bfd, o); | |
2299 | if (secdata != NULL && secdata->contents != NULL) | |
2300 | contents = secdata->contents; | |
2301 | else | |
2302 | { | |
2303 | if (! bfd_get_section_contents (input_bfd, o, finfo->contents, | |
2304 | (file_ptr) 0, o->_raw_size)) | |
2305 | return false; | |
2306 | contents = finfo->contents; | |
2307 | } | |
2308 | ||
2309 | if ((o->flags & SEC_RELOC) != 0) | |
2310 | { | |
2311 | int target_index; | |
2312 | struct internal_reloc *internal_relocs; | |
2313 | struct internal_reloc *irel; | |
2314 | ||
2315 | /* Read in the relocs. */ | |
2316 | target_index = o->output_section->target_index; | |
2317 | internal_relocs = (_bfd_coff_read_internal_relocs | |
2318 | (input_bfd, o, false, finfo->external_relocs, | |
2319 | finfo->info->relocateable, | |
2320 | (finfo->info->relocateable | |
2321 | ? (finfo->section_info[target_index].relocs | |
2322 | + o->output_section->reloc_count) | |
2323 | : finfo->internal_relocs))); | |
2324 | if (internal_relocs == NULL) | |
2325 | return false; | |
2326 | ||
2327 | /* Call processor specific code to relocate the section | |
2328 | contents. */ | |
2329 | if (! bfd_coff_relocate_section (output_bfd, finfo->info, | |
2330 | input_bfd, o, | |
2331 | contents, | |
2332 | internal_relocs, | |
2333 | finfo->internal_syms, | |
2334 | finfo->sec_ptrs)) | |
2335 | return false; | |
2336 | ||
2337 | if (finfo->info->relocateable) | |
2338 | { | |
2339 | bfd_vma offset; | |
2340 | struct internal_reloc *irelend; | |
2341 | struct coff_link_hash_entry **rel_hash; | |
2342 | ||
2343 | offset = o->output_section->vma + o->output_offset - o->vma; | |
2344 | irel = internal_relocs; | |
2345 | irelend = irel + o->reloc_count; | |
2346 | rel_hash = (finfo->section_info[target_index].rel_hashes | |
2347 | + o->output_section->reloc_count); | |
2348 | for (; irel < irelend; irel++, rel_hash++) | |
2349 | { | |
2350 | struct coff_link_hash_entry *h; | |
2351 | boolean adjusted; | |
2352 | ||
2353 | *rel_hash = NULL; | |
2354 | ||
2355 | /* Adjust the reloc address and symbol index. */ | |
2356 | ||
2357 | irel->r_vaddr += offset; | |
2358 | ||
2359 | if (irel->r_symndx == -1) | |
2360 | continue; | |
2361 | ||
2362 | if (adjust_symndx) | |
2363 | { | |
2364 | if (! (*adjust_symndx) (output_bfd, finfo->info, | |
2365 | input_bfd, o, irel, | |
2366 | &adjusted)) | |
2367 | return false; | |
2368 | if (adjusted) | |
2369 | continue; | |
2370 | } | |
2371 | ||
2372 | h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx]; | |
2373 | if (h != NULL) | |
2374 | { | |
2375 | /* This is a global symbol. */ | |
2376 | if (h->indx >= 0) | |
2377 | irel->r_symndx = h->indx; | |
2378 | else | |
2379 | { | |
2380 | /* This symbol is being written at the end | |
2381 | of the file, and we do not yet know the | |
2382 | symbol index. We save the pointer to the | |
2383 | hash table entry in the rel_hash list. | |
2384 | We set the indx field to -2 to indicate | |
2385 | that this symbol must not be stripped. */ | |
2386 | *rel_hash = h; | |
2387 | h->indx = -2; | |
2388 | } | |
2389 | } | |
2390 | else | |
2391 | { | |
2392 | long indx; | |
2393 | ||
2394 | indx = finfo->sym_indices[irel->r_symndx]; | |
2395 | if (indx != -1) | |
2396 | irel->r_symndx = indx; | |
2397 | else | |
2398 | { | |
2399 | struct internal_syment *is; | |
2400 | const char *name; | |
2401 | char buf[SYMNMLEN + 1]; | |
2402 | ||
2403 | /* This reloc is against a symbol we are | |
2404 | stripping. This should have been handled | |
2405 | by the 'dont_skip_symbol' code in the while | |
2406 | loop at the top of this function. */ | |
2407 | ||
2408 | is = finfo->internal_syms + irel->r_symndx; | |
2409 | ||
2410 | name = (_bfd_coff_internal_syment_name | |
2411 | (input_bfd, is, buf)); | |
2412 | if (name == NULL) | |
2413 | return false; | |
2414 | ||
2415 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
2416 | (finfo->info, name, input_bfd, o, | |
2417 | irel->r_vaddr))) | |
2418 | return false; | |
2419 | } | |
2420 | } | |
2421 | } | |
2422 | ||
2423 | o->output_section->reloc_count += o->reloc_count; | |
2424 | } | |
2425 | } | |
2426 | ||
2427 | /* Write out the modified section contents. */ | |
2428 | if (secdata == NULL || secdata->stab_info == NULL) | |
2429 | { | |
2430 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
9a968f43 NC |
2431 | contents, |
2432 | (file_ptr) | |
2433 | (o->output_offset * | |
2434 | bfd_octets_per_byte (output_bfd)), | |
252b5132 RH |
2435 | (o->_cooked_size != 0 |
2436 | ? o->_cooked_size | |
2437 | : o->_raw_size))) | |
2438 | return false; | |
2439 | } | |
2440 | else | |
2441 | { | |
2442 | if (! (_bfd_write_section_stabs | |
2443 | (output_bfd, &coff_hash_table (finfo->info)->stab_info, | |
2444 | o, &secdata->stab_info, contents))) | |
2445 | return false; | |
2446 | } | |
2447 | } | |
2448 | ||
2449 | if (! finfo->info->keep_memory) | |
2450 | { | |
2451 | if (! _bfd_coff_free_symbols (input_bfd)) | |
2452 | return false; | |
2453 | } | |
2454 | ||
2455 | return true; | |
2456 | } | |
2457 | ||
2458 | /* Write out a global symbol. Called via coff_link_hash_traverse. */ | |
2459 | ||
2460 | boolean | |
2461 | _bfd_coff_write_global_sym (h, data) | |
2462 | struct coff_link_hash_entry *h; | |
2463 | PTR data; | |
2464 | { | |
2465 | struct coff_final_link_info *finfo = (struct coff_final_link_info *) data; | |
2466 | bfd *output_bfd; | |
2467 | struct internal_syment isym; | |
2468 | bfd_size_type symesz; | |
2469 | unsigned int i; | |
2470 | ||
2471 | output_bfd = finfo->output_bfd; | |
2472 | ||
2473 | if (h->indx >= 0) | |
2474 | return true; | |
2475 | ||
2476 | if (h->indx != -2 | |
2477 | && (finfo->info->strip == strip_all | |
2478 | || (finfo->info->strip == strip_some | |
2479 | && (bfd_hash_lookup (finfo->info->keep_hash, | |
2480 | h->root.root.string, false, false) | |
2481 | == NULL)))) | |
2482 | return true; | |
2483 | ||
2484 | switch (h->root.type) | |
2485 | { | |
2486 | default: | |
2487 | case bfd_link_hash_new: | |
2488 | abort (); | |
2489 | return false; | |
2490 | ||
2491 | case bfd_link_hash_undefined: | |
2492 | case bfd_link_hash_undefweak: | |
2493 | isym.n_scnum = N_UNDEF; | |
2494 | isym.n_value = 0; | |
2495 | break; | |
2496 | ||
2497 | case bfd_link_hash_defined: | |
2498 | case bfd_link_hash_defweak: | |
2499 | { | |
2500 | asection *sec; | |
2501 | ||
2502 | sec = h->root.u.def.section->output_section; | |
2503 | if (bfd_is_abs_section (sec)) | |
2504 | isym.n_scnum = N_ABS; | |
2505 | else | |
2506 | isym.n_scnum = sec->target_index; | |
2507 | isym.n_value = (h->root.u.def.value | |
2508 | + h->root.u.def.section->output_offset); | |
2509 | if (! obj_pe (finfo->output_bfd)) | |
2510 | isym.n_value += sec->vma; | |
2511 | } | |
2512 | break; | |
2513 | ||
2514 | case bfd_link_hash_common: | |
2515 | isym.n_scnum = N_UNDEF; | |
2516 | isym.n_value = h->root.u.c.size; | |
2517 | break; | |
2518 | ||
2519 | case bfd_link_hash_indirect: | |
2520 | case bfd_link_hash_warning: | |
2521 | /* Just ignore these. They can't be handled anyhow. */ | |
2522 | return true; | |
2523 | } | |
2524 | ||
2525 | if (strlen (h->root.root.string) <= SYMNMLEN) | |
2526 | strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN); | |
2527 | else | |
2528 | { | |
2529 | boolean hash; | |
2530 | bfd_size_type indx; | |
2531 | ||
2532 | hash = true; | |
2533 | if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0) | |
2534 | hash = false; | |
2535 | indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash, | |
2536 | false); | |
2537 | if (indx == (bfd_size_type) -1) | |
2538 | { | |
2539 | finfo->failed = true; | |
2540 | return false; | |
2541 | } | |
2542 | isym._n._n_n._n_zeroes = 0; | |
2543 | isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; | |
2544 | } | |
2545 | ||
2546 | isym.n_sclass = h->class; | |
2547 | isym.n_type = h->type; | |
2548 | ||
2549 | if (isym.n_sclass == C_NULL) | |
2550 | isym.n_sclass = C_EXT; | |
2551 | ||
2552 | /* If doing task linking and this is the pass where we convert | |
2553 | defined globals to statics, then do that conversion now. If the | |
2554 | symbol is not being converted, just ignore it and it will be | |
e4202681 | 2555 | output during a later pass. */ |
252b5132 RH |
2556 | if (finfo->global_to_static) |
2557 | { | |
e4202681 NC |
2558 | if (! IS_EXTERNAL (output_bfd, isym)) |
2559 | return true; | |
2560 | ||
252b5132 RH |
2561 | isym.n_sclass = C_STAT; |
2562 | } | |
2563 | ||
e4202681 NC |
2564 | /* When a weak symbol is not overriden by a strong one, |
2565 | turn it into an external symbol when not building a | |
2566 | shared or relocateable object. */ | |
2567 | if (! finfo->info->shared | |
2568 | && ! finfo->info->relocateable | |
2569 | && IS_WEAK_EXTERNAL (finfo->output_bfd, isym)) | |
2570 | isym.n_sclass = C_EXT; | |
2571 | ||
252b5132 RH |
2572 | isym.n_numaux = h->numaux; |
2573 | ||
2574 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms); | |
2575 | ||
2576 | symesz = bfd_coff_symesz (output_bfd); | |
2577 | ||
2578 | if (bfd_seek (output_bfd, | |
2579 | (obj_sym_filepos (output_bfd) | |
2580 | + obj_raw_syment_count (output_bfd) * symesz), | |
2581 | SEEK_SET) != 0 | |
2582 | || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz) | |
2583 | { | |
2584 | finfo->failed = true; | |
2585 | return false; | |
2586 | } | |
2587 | ||
2588 | h->indx = obj_raw_syment_count (output_bfd); | |
2589 | ||
2590 | ++obj_raw_syment_count (output_bfd); | |
2591 | ||
2c546cd8 ILT |
2592 | /* Write out any associated aux entries. Most of the aux entries |
2593 | will have been modified in _bfd_coff_link_input_bfd. We have to | |
2594 | handle section aux entries here, now that we have the final | |
2595 | relocation and line number counts. */ | |
252b5132 RH |
2596 | for (i = 0; i < isym.n_numaux; i++) |
2597 | { | |
2c546cd8 ILT |
2598 | union internal_auxent *auxp; |
2599 | ||
2600 | auxp = h->aux + i; | |
2601 | ||
2602 | /* Look for a section aux entry here using the same tests that | |
2603 | coff_swap_aux_out uses. */ | |
2604 | if (i == 0 | |
2605 | && (isym.n_sclass == C_STAT | |
2606 | || isym.n_sclass == C_HIDDEN) | |
2607 | && isym.n_type == T_NULL | |
2608 | && (h->root.type == bfd_link_hash_defined | |
2609 | || h->root.type == bfd_link_hash_defweak)) | |
2610 | { | |
2611 | asection *sec; | |
2612 | ||
2613 | sec = h->root.u.def.section->output_section; | |
2614 | if (sec != NULL) | |
2615 | { | |
2616 | auxp->x_scn.x_scnlen = (sec->_cooked_size != 0 | |
2617 | ? sec->_cooked_size | |
2618 | : sec->_raw_size); | |
2619 | ||
2620 | /* For PE, an overflow on the final link reportedly does | |
2621 | not matter. FIXME: Why not? */ | |
2622 | ||
2623 | if (sec->reloc_count > 0xffff | |
2624 | && (! obj_pe (output_bfd) | |
2625 | || finfo->info->relocateable)) | |
2626 | (*_bfd_error_handler) | |
2627 | (_("%s: %s: reloc overflow: 0x%lx > 0xffff"), | |
2628 | bfd_get_filename (output_bfd), | |
2629 | bfd_get_section_name (output_bfd, sec), | |
2630 | sec->reloc_count); | |
2631 | ||
2632 | if (sec->lineno_count > 0xffff | |
2633 | && (! obj_pe (output_bfd) | |
2634 | || finfo->info->relocateable)) | |
2635 | (*_bfd_error_handler) | |
2636 | (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"), | |
2637 | bfd_get_filename (output_bfd), | |
2638 | bfd_get_section_name (output_bfd, sec), | |
2639 | sec->lineno_count); | |
2640 | ||
2641 | auxp->x_scn.x_nreloc = sec->reloc_count; | |
2642 | auxp->x_scn.x_nlinno = sec->lineno_count; | |
2643 | auxp->x_scn.x_checksum = 0; | |
2644 | auxp->x_scn.x_associated = 0; | |
2645 | auxp->x_scn.x_comdat = 0; | |
2646 | } | |
2647 | } | |
2648 | ||
2649 | bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isym.n_type, | |
252b5132 RH |
2650 | isym.n_sclass, i, isym.n_numaux, |
2651 | (PTR) finfo->outsyms); | |
2652 | if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz) | |
2653 | { | |
2654 | finfo->failed = true; | |
2655 | return false; | |
2656 | } | |
2657 | ++obj_raw_syment_count (output_bfd); | |
2658 | } | |
2659 | ||
2660 | return true; | |
2661 | } | |
2662 | ||
2663 | /* Write out task global symbols, converting them to statics. Called | |
2664 | via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do | |
2665 | the dirty work, if the symbol we are processing needs conversion. */ | |
2666 | ||
2667 | boolean | |
2668 | _bfd_coff_write_task_globals (h, data) | |
2669 | struct coff_link_hash_entry *h; | |
2670 | PTR data; | |
2671 | { | |
2672 | struct coff_final_link_info *finfo = (struct coff_final_link_info *) data; | |
2673 | boolean rtnval = true; | |
2674 | boolean save_global_to_static; | |
2675 | ||
2676 | if (h->indx < 0) | |
2677 | { | |
2678 | switch (h->root.type) | |
2679 | { | |
2680 | case bfd_link_hash_defined: | |
2681 | case bfd_link_hash_defweak: | |
2682 | save_global_to_static = finfo->global_to_static; | |
2683 | finfo->global_to_static = true; | |
2684 | rtnval = _bfd_coff_write_global_sym (h, data); | |
2685 | finfo->global_to_static = save_global_to_static; | |
2686 | break; | |
2687 | default: | |
2688 | break; | |
2689 | } | |
2690 | } | |
2691 | return (rtnval); | |
2692 | } | |
2693 | ||
2694 | /* Handle a link order which is supposed to generate a reloc. */ | |
2695 | ||
2696 | boolean | |
2697 | _bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order) | |
2698 | bfd *output_bfd; | |
2699 | struct coff_final_link_info *finfo; | |
2700 | asection *output_section; | |
2701 | struct bfd_link_order *link_order; | |
2702 | { | |
2703 | reloc_howto_type *howto; | |
2704 | struct internal_reloc *irel; | |
2705 | struct coff_link_hash_entry **rel_hash_ptr; | |
2706 | ||
2707 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
2708 | if (howto == NULL) | |
2709 | { | |
2710 | bfd_set_error (bfd_error_bad_value); | |
2711 | return false; | |
2712 | } | |
2713 | ||
2714 | if (link_order->u.reloc.p->addend != 0) | |
2715 | { | |
2716 | bfd_size_type size; | |
2717 | bfd_byte *buf; | |
2718 | bfd_reloc_status_type rstat; | |
2719 | boolean ok; | |
2720 | ||
2721 | size = bfd_get_reloc_size (howto); | |
2722 | buf = (bfd_byte *) bfd_zmalloc (size); | |
2723 | if (buf == NULL) | |
2724 | return false; | |
2725 | ||
2726 | rstat = _bfd_relocate_contents (howto, output_bfd, | |
2727 | link_order->u.reloc.p->addend, buf); | |
2728 | switch (rstat) | |
2729 | { | |
2730 | case bfd_reloc_ok: | |
2731 | break; | |
2732 | default: | |
2733 | case bfd_reloc_outofrange: | |
2734 | abort (); | |
2735 | case bfd_reloc_overflow: | |
2736 | if (! ((*finfo->info->callbacks->reloc_overflow) | |
2737 | (finfo->info, | |
2738 | (link_order->type == bfd_section_reloc_link_order | |
2739 | ? bfd_section_name (output_bfd, | |
2740 | link_order->u.reloc.p->u.section) | |
2741 | : link_order->u.reloc.p->u.name), | |
2742 | howto->name, link_order->u.reloc.p->addend, | |
2743 | (bfd *) NULL, (asection *) NULL, (bfd_vma) 0))) | |
2744 | { | |
2745 | free (buf); | |
2746 | return false; | |
2747 | } | |
2748 | break; | |
2749 | } | |
2750 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
9a968f43 NC |
2751 | (file_ptr) |
2752 | (link_order->offset * | |
2753 | bfd_octets_per_byte (output_bfd)), size); | |
252b5132 RH |
2754 | free (buf); |
2755 | if (! ok) | |
2756 | return false; | |
2757 | } | |
2758 | ||
2759 | /* Store the reloc information in the right place. It will get | |
2760 | swapped and written out at the end of the final_link routine. */ | |
2761 | ||
2762 | irel = (finfo->section_info[output_section->target_index].relocs | |
2763 | + output_section->reloc_count); | |
2764 | rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes | |
2765 | + output_section->reloc_count); | |
2766 | ||
2767 | memset (irel, 0, sizeof (struct internal_reloc)); | |
2768 | *rel_hash_ptr = NULL; | |
2769 | ||
2770 | irel->r_vaddr = output_section->vma + link_order->offset; | |
2771 | ||
2772 | if (link_order->type == bfd_section_reloc_link_order) | |
2773 | { | |
2774 | /* We need to somehow locate a symbol in the right section. The | |
2775 | symbol must either have a value of zero, or we must adjust | |
2776 | the addend by the value of the symbol. FIXME: Write this | |
2777 | when we need it. The old linker couldn't handle this anyhow. */ | |
2778 | abort (); | |
2779 | *rel_hash_ptr = NULL; | |
2780 | irel->r_symndx = 0; | |
2781 | } | |
2782 | else | |
2783 | { | |
2784 | struct coff_link_hash_entry *h; | |
2785 | ||
2786 | h = ((struct coff_link_hash_entry *) | |
2787 | bfd_wrapped_link_hash_lookup (output_bfd, finfo->info, | |
2788 | link_order->u.reloc.p->u.name, | |
2789 | false, false, true)); | |
2790 | if (h != NULL) | |
2791 | { | |
2792 | if (h->indx >= 0) | |
2793 | irel->r_symndx = h->indx; | |
2794 | else | |
2795 | { | |
2796 | /* Set the index to -2 to force this symbol to get | |
2797 | written out. */ | |
2798 | h->indx = -2; | |
2799 | *rel_hash_ptr = h; | |
2800 | irel->r_symndx = 0; | |
2801 | } | |
2802 | } | |
2803 | else | |
2804 | { | |
2805 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
2806 | (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
2807 | (asection *) NULL, (bfd_vma) 0))) | |
2808 | return false; | |
2809 | irel->r_symndx = 0; | |
2810 | } | |
2811 | } | |
2812 | ||
2813 | /* FIXME: Is this always right? */ | |
2814 | irel->r_type = howto->type; | |
2815 | ||
2816 | /* r_size is only used on the RS/6000, which needs its own linker | |
2817 | routines anyhow. r_extern is only used for ECOFF. */ | |
2818 | ||
2819 | /* FIXME: What is the right value for r_offset? Is zero OK? */ | |
2820 | ||
2821 | ++output_section->reloc_count; | |
2822 | ||
2823 | return true; | |
2824 | } | |
2825 | ||
2826 | /* A basic reloc handling routine which may be used by processors with | |
2827 | simple relocs. */ | |
2828 | ||
2829 | boolean | |
2830 | _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd, | |
2831 | input_section, contents, relocs, syms, | |
2832 | sections) | |
2833 | bfd *output_bfd; | |
2834 | struct bfd_link_info *info; | |
2835 | bfd *input_bfd; | |
2836 | asection *input_section; | |
2837 | bfd_byte *contents; | |
2838 | struct internal_reloc *relocs; | |
2839 | struct internal_syment *syms; | |
2840 | asection **sections; | |
2841 | { | |
2842 | struct internal_reloc *rel; | |
2843 | struct internal_reloc *relend; | |
2844 | ||
2845 | rel = relocs; | |
2846 | relend = rel + input_section->reloc_count; | |
2847 | for (; rel < relend; rel++) | |
2848 | { | |
2849 | long symndx; | |
2850 | struct coff_link_hash_entry *h; | |
2851 | struct internal_syment *sym; | |
2852 | bfd_vma addend; | |
2853 | bfd_vma val; | |
2854 | reloc_howto_type *howto; | |
2855 | bfd_reloc_status_type rstat; | |
2856 | ||
2857 | symndx = rel->r_symndx; | |
2858 | ||
2859 | if (symndx == -1) | |
2860 | { | |
2861 | h = NULL; | |
2862 | sym = NULL; | |
2863 | } | |
75987f83 ILT |
2864 | else if (symndx < 0 |
2865 | || (unsigned long) symndx >= obj_raw_syment_count (input_bfd)) | |
2866 | { | |
2867 | (*_bfd_error_handler) | |
2868 | ("%s: illegal symbol index %ld in relocs", | |
2869 | bfd_get_filename (input_bfd), symndx); | |
2870 | return false; | |
2871 | } | |
252b5132 RH |
2872 | else |
2873 | { | |
2874 | h = obj_coff_sym_hashes (input_bfd)[symndx]; | |
2875 | sym = syms + symndx; | |
2876 | } | |
2877 | ||
2878 | /* COFF treats common symbols in one of two ways. Either the | |
2879 | size of the symbol is included in the section contents, or it | |
2880 | is not. We assume that the size is not included, and force | |
2881 | the rtype_to_howto function to adjust the addend as needed. */ | |
2882 | ||
2883 | if (sym != NULL && sym->n_scnum != 0) | |
2884 | addend = - sym->n_value; | |
2885 | else | |
2886 | addend = 0; | |
2887 | ||
2888 | ||
2889 | howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h, | |
2890 | sym, &addend); | |
2891 | if (howto == NULL) | |
2892 | return false; | |
2893 | ||
2894 | /* If we are doing a relocateable link, then we can just ignore | |
2895 | a PC relative reloc that is pcrel_offset. It will already | |
2896 | have the correct value. If this is not a relocateable link, | |
2897 | then we should ignore the symbol value. */ | |
2898 | if (howto->pc_relative && howto->pcrel_offset) | |
2899 | { | |
2900 | if (info->relocateable) | |
2901 | continue; | |
2902 | if (sym != NULL && sym->n_scnum != 0) | |
2903 | addend += sym->n_value; | |
2904 | } | |
2905 | ||
2906 | val = 0; | |
2907 | ||
2908 | if (h == NULL) | |
2909 | { | |
2910 | asection *sec; | |
2911 | ||
2912 | if (symndx == -1) | |
2913 | { | |
2914 | sec = bfd_abs_section_ptr; | |
2915 | val = 0; | |
2916 | } | |
2917 | else | |
2918 | { | |
2919 | sec = sections[symndx]; | |
2920 | val = (sec->output_section->vma | |
2921 | + sec->output_offset | |
2922 | + sym->n_value); | |
2923 | if (! obj_pe (input_bfd)) | |
2924 | val -= sec->vma; | |
2925 | } | |
2926 | } | |
2927 | else | |
2928 | { | |
2929 | if (h->root.type == bfd_link_hash_defined | |
2930 | || h->root.type == bfd_link_hash_defweak) | |
2931 | { | |
2932 | asection *sec; | |
2933 | ||
2934 | sec = h->root.u.def.section; | |
2935 | val = (h->root.u.def.value | |
2936 | + sec->output_section->vma | |
2937 | + sec->output_offset); | |
2938 | } | |
2939 | ||
2940 | else if (! info->relocateable) | |
2941 | { | |
2942 | if (! ((*info->callbacks->undefined_symbol) | |
2943 | (info, h->root.root.string, input_bfd, input_section, | |
5cc7c785 | 2944 | rel->r_vaddr - input_section->vma, true))) |
252b5132 RH |
2945 | return false; |
2946 | } | |
2947 | } | |
2948 | ||
2949 | if (info->base_file) | |
2950 | { | |
2951 | /* Emit a reloc if the backend thinks it needs it. */ | |
2952 | if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto)) | |
2953 | { | |
2954 | /* Relocation to a symbol in a section which isn't | |
2955 | absolute. We output the address here to a file. | |
2956 | This file is then read by dlltool when generating the | |
2957 | reloc section. Note that the base file is not | |
2958 | portable between systems. We write out a long here, | |
2959 | and dlltool reads in a long. */ | |
2960 | long addr = (rel->r_vaddr | |
2961 | - input_section->vma | |
2962 | + input_section->output_offset | |
2963 | + input_section->output_section->vma); | |
2964 | if (coff_data (output_bfd)->pe) | |
2965 | addr -= pe_data(output_bfd)->pe_opthdr.ImageBase; | |
2966 | if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file) | |
2967 | != sizeof (long)) | |
2968 | { | |
2969 | bfd_set_error (bfd_error_system_call); | |
2970 | return false; | |
2971 | } | |
2972 | } | |
2973 | } | |
2974 | ||
2975 | rstat = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
2976 | contents, | |
2977 | rel->r_vaddr - input_section->vma, | |
2978 | val, addend); | |
2979 | ||
2980 | switch (rstat) | |
2981 | { | |
2982 | default: | |
2983 | abort (); | |
2984 | case bfd_reloc_ok: | |
2985 | break; | |
2986 | case bfd_reloc_outofrange: | |
2987 | (*_bfd_error_handler) | |
2988 | (_("%s: bad reloc address 0x%lx in section `%s'"), | |
2989 | bfd_get_filename (input_bfd), | |
2990 | (unsigned long) rel->r_vaddr, | |
2991 | bfd_get_section_name (input_bfd, input_section)); | |
2992 | return false; | |
2993 | case bfd_reloc_overflow: | |
2994 | { | |
2995 | const char *name; | |
2996 | char buf[SYMNMLEN + 1]; | |
2997 | ||
2998 | if (symndx == -1) | |
2999 | name = "*ABS*"; | |
3000 | else if (h != NULL) | |
3001 | name = h->root.root.string; | |
3002 | else | |
3003 | { | |
3004 | name = _bfd_coff_internal_syment_name (input_bfd, sym, buf); | |
3005 | if (name == NULL) | |
3006 | return false; | |
3007 | } | |
3008 | ||
3009 | if (! ((*info->callbacks->reloc_overflow) | |
3010 | (info, name, howto->name, (bfd_vma) 0, input_bfd, | |
3011 | input_section, rel->r_vaddr - input_section->vma))) | |
3012 | return false; | |
3013 | } | |
3014 | } | |
3015 | } | |
3016 | return true; | |
3017 | } | |
3018 |