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