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