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