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