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