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