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