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