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