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