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