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