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