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