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