]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* POWER/PowerPC XCOFF linker support. |
eea6121a | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | Written by Ian Lance Taylor <[email protected]>, Cygnus Support. |
5 | ||
eb1e0e80 | 6 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 7 | |
eb1e0e80 NC |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
252b5132 | 12 | |
eb1e0e80 NC |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
eb1e0e80 NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
252b5132 RH |
21 | |
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
24 | #include "bfdlink.h" | |
25 | #include "libbfd.h" | |
26 | #include "coff/internal.h" | |
beb1bf64 | 27 | #include "coff/xcoff.h" |
252b5132 | 28 | #include "libcoff.h" |
beb1bf64 | 29 | #include "libxcoff.h" |
252b5132 RH |
30 | |
31 | /* This file holds the XCOFF linker code. */ | |
32 | ||
33 | #define STRING_SIZE_SIZE (4) | |
34 | ||
252b5132 RH |
35 | /* We reuse the SEC_ROM flag as a mark flag for garbage collection. |
36 | This flag will only be used on input sections. */ | |
37 | ||
38 | #define SEC_MARK (SEC_ROM) | |
39 | ||
252b5132 RH |
40 | /* The list of import files. */ |
41 | ||
dc810e39 AM |
42 | struct xcoff_import_file |
43 | { | |
252b5132 RH |
44 | /* The next entry in the list. */ |
45 | struct xcoff_import_file *next; | |
46 | /* The path. */ | |
47 | const char *path; | |
48 | /* The file name. */ | |
49 | const char *file; | |
50 | /* The member name. */ | |
51 | const char *member; | |
52 | }; | |
53 | ||
252b5132 RH |
54 | /* Information we keep for each section in the output file during the |
55 | final link phase. */ | |
56 | ||
dc810e39 AM |
57 | struct xcoff_link_section_info |
58 | { | |
252b5132 RH |
59 | /* The relocs to be output. */ |
60 | struct internal_reloc *relocs; | |
61 | /* For each reloc against a global symbol whose index was not known | |
62 | when the reloc was handled, the global hash table entry. */ | |
63 | struct xcoff_link_hash_entry **rel_hashes; | |
64 | /* If there is a TOC relative reloc against a global symbol, and the | |
65 | index of the TOC symbol is not known when the reloc was handled, | |
66 | an entry is added to this linked list. This is not an array, | |
67 | like rel_hashes, because this case is quite uncommon. */ | |
fbc4fff4 KH |
68 | struct xcoff_toc_rel_hash { |
69 | struct xcoff_toc_rel_hash *next; | |
70 | struct xcoff_link_hash_entry *h; | |
71 | struct internal_reloc *rel; | |
72 | } *toc_rel_hashes; | |
252b5132 RH |
73 | }; |
74 | ||
75 | /* Information that we pass around while doing the final link step. */ | |
76 | ||
dc810e39 AM |
77 | struct xcoff_final_link_info |
78 | { | |
252b5132 RH |
79 | /* General link information. */ |
80 | struct bfd_link_info *info; | |
81 | /* Output BFD. */ | |
82 | bfd *output_bfd; | |
83 | /* Hash table for long symbol names. */ | |
84 | struct bfd_strtab_hash *strtab; | |
85 | /* Array of information kept for each output section, indexed by the | |
86 | target_index field. */ | |
87 | struct xcoff_link_section_info *section_info; | |
88 | /* Symbol index of last C_FILE symbol (-1 if none). */ | |
89 | long last_file_index; | |
90 | /* Contents of last C_FILE symbol. */ | |
91 | struct internal_syment last_file; | |
92 | /* Symbol index of TOC symbol. */ | |
93 | long toc_symindx; | |
94 | /* Start of .loader symbols. */ | |
beb1bf64 | 95 | bfd_byte *ldsym; |
252b5132 | 96 | /* Next .loader reloc to swap out. */ |
beb1bf64 | 97 | bfd_byte *ldrel; |
252b5132 RH |
98 | /* File position of start of line numbers. */ |
99 | file_ptr line_filepos; | |
100 | /* Buffer large enough to hold swapped symbols of any input file. */ | |
101 | struct internal_syment *internal_syms; | |
102 | /* Buffer large enough to hold output indices of symbols of any | |
103 | input file. */ | |
104 | long *sym_indices; | |
105 | /* Buffer large enough to hold output symbols for any input file. */ | |
106 | bfd_byte *outsyms; | |
107 | /* Buffer large enough to hold external line numbers for any input | |
108 | section. */ | |
109 | bfd_byte *linenos; | |
110 | /* Buffer large enough to hold any input section. */ | |
111 | bfd_byte *contents; | |
112 | /* Buffer large enough to hold external relocs of any input section. */ | |
113 | bfd_byte *external_relocs; | |
114 | }; | |
115 | ||
252b5132 RH |
116 | static struct bfd_hash_entry *xcoff_link_hash_newfunc |
117 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
b34976b6 | 118 | static bfd_boolean xcoff_get_section_contents PARAMS ((bfd *, asection *)); |
252b5132 | 119 | static struct internal_reloc *xcoff_read_internal_relocs |
b34976b6 | 120 | PARAMS ((bfd *, asection *, bfd_boolean, bfd_byte *, bfd_boolean, |
252b5132 | 121 | struct internal_reloc *)); |
b34976b6 | 122 | static bfd_boolean xcoff_link_add_object_symbols |
252b5132 | 123 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 AM |
124 | static bfd_boolean xcoff_link_check_archive_element |
125 | PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *)); | |
126 | static bfd_boolean xcoff_link_check_ar_symbols | |
127 | PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *)); | |
128 | static bfd_boolean xcoff_link_check_dynamic_ar_symbols | |
129 | PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *)); | |
252b5132 RH |
130 | static bfd_size_type xcoff_find_reloc |
131 | PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma)); | |
b34976b6 | 132 | static bfd_boolean xcoff_link_add_symbols |
252b5132 | 133 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 AM |
134 | static bfd_boolean xcoff_link_add_dynamic_symbols |
135 | PARAMS ((bfd *, struct bfd_link_info *)); | |
136 | static bfd_boolean xcoff_mark_symbol | |
252b5132 | 137 | PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *)); |
b34976b6 | 138 | static bfd_boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *)); |
252b5132 | 139 | static void xcoff_sweep PARAMS ((struct bfd_link_info *)); |
b34976b6 | 140 | static bfd_boolean xcoff_build_ldsyms |
252b5132 | 141 | PARAMS ((struct xcoff_link_hash_entry *, PTR)); |
b34976b6 | 142 | static bfd_boolean xcoff_link_input_bfd |
252b5132 | 143 | PARAMS ((struct xcoff_final_link_info *, bfd *)); |
b34976b6 | 144 | static bfd_boolean xcoff_write_global_symbol |
252b5132 | 145 | PARAMS ((struct xcoff_link_hash_entry *, PTR)); |
b34976b6 | 146 | static bfd_boolean xcoff_reloc_link_order |
252b5132 RH |
147 | PARAMS ((bfd *, struct xcoff_final_link_info *, asection *, |
148 | struct bfd_link_order *)); | |
149 | static int xcoff_sort_relocs PARAMS ((const PTR, const PTR)); | |
150 | \f | |
252b5132 | 151 | |
252b5132 RH |
152 | /* Routines to read XCOFF dynamic information. This don't really |
153 | belong here, but we already have the ldsym manipulation routines | |
154 | here. */ | |
155 | ||
156 | /* Read the contents of a section. */ | |
157 | ||
b34976b6 | 158 | static bfd_boolean |
252b5132 RH |
159 | xcoff_get_section_contents (abfd, sec) |
160 | bfd *abfd; | |
161 | asection *sec; | |
162 | { | |
beb1bf64 | 163 | |
252b5132 RH |
164 | if (coff_section_data (abfd, sec) == NULL) |
165 | { | |
dc810e39 AM |
166 | bfd_size_type amt = sizeof (struct coff_section_tdata); |
167 | sec->used_by_bfd = bfd_zalloc (abfd, amt); | |
252b5132 | 168 | if (sec->used_by_bfd == NULL) |
b34976b6 | 169 | return FALSE; |
252b5132 RH |
170 | } |
171 | ||
172 | if (coff_section_data (abfd, sec)->contents == NULL) | |
173 | { | |
eea6121a AM |
174 | bfd_byte *contents; |
175 | if (!bfd_malloc_and_get_section (abfd, sec, &contents)) | |
176 | { | |
177 | if (contents != NULL) | |
178 | free (contents); | |
179 | return FALSE; | |
180 | } | |
181 | coff_section_data (abfd, sec)->contents = contents; | |
252b5132 RH |
182 | } |
183 | ||
b34976b6 | 184 | return TRUE; |
252b5132 RH |
185 | } |
186 | ||
187 | /* Get the size required to hold the dynamic symbols. */ | |
188 | ||
189 | long | |
190 | _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd) | |
191 | bfd *abfd; | |
192 | { | |
193 | asection *lsec; | |
194 | bfd_byte *contents; | |
195 | struct internal_ldhdr ldhdr; | |
196 | ||
197 | if ((abfd->flags & DYNAMIC) == 0) | |
198 | { | |
199 | bfd_set_error (bfd_error_invalid_operation); | |
200 | return -1; | |
201 | } | |
202 | ||
203 | lsec = bfd_get_section_by_name (abfd, ".loader"); | |
204 | if (lsec == NULL) | |
205 | { | |
206 | bfd_set_error (bfd_error_no_symbols); | |
207 | return -1; | |
208 | } | |
209 | ||
210 | if (! xcoff_get_section_contents (abfd, lsec)) | |
211 | return -1; | |
212 | contents = coff_section_data (abfd, lsec)->contents; | |
213 | ||
beb1bf64 | 214 | bfd_xcoff_swap_ldhdr_in (abfd, (PTR) contents, &ldhdr); |
252b5132 RH |
215 | |
216 | return (ldhdr.l_nsyms + 1) * sizeof (asymbol *); | |
217 | } | |
218 | ||
219 | /* Get the dynamic symbols. */ | |
220 | ||
221 | long | |
222 | _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms) | |
223 | bfd *abfd; | |
224 | asymbol **psyms; | |
225 | { | |
226 | asection *lsec; | |
227 | bfd_byte *contents; | |
228 | struct internal_ldhdr ldhdr; | |
229 | const char *strings; | |
beb1bf64 | 230 | bfd_byte *elsym, *elsymend; |
252b5132 RH |
231 | coff_symbol_type *symbuf; |
232 | ||
233 | if ((abfd->flags & DYNAMIC) == 0) | |
234 | { | |
235 | bfd_set_error (bfd_error_invalid_operation); | |
236 | return -1; | |
237 | } | |
238 | ||
239 | lsec = bfd_get_section_by_name (abfd, ".loader"); | |
240 | if (lsec == NULL) | |
241 | { | |
242 | bfd_set_error (bfd_error_no_symbols); | |
243 | return -1; | |
244 | } | |
245 | ||
246 | if (! xcoff_get_section_contents (abfd, lsec)) | |
247 | return -1; | |
248 | contents = coff_section_data (abfd, lsec)->contents; | |
249 | ||
b34976b6 | 250 | coff_section_data (abfd, lsec)->keep_contents = TRUE; |
252b5132 | 251 | |
beb1bf64 | 252 | bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); |
252b5132 RH |
253 | |
254 | strings = (char *) contents + ldhdr.l_stoff; | |
255 | ||
256 | symbuf = ((coff_symbol_type *) | |
257 | bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type))); | |
258 | if (symbuf == NULL) | |
259 | return -1; | |
260 | ||
beb1bf64 TR |
261 | elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr); |
262 | ||
263 | elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd); | |
264 | for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++) | |
252b5132 RH |
265 | { |
266 | struct internal_ldsym ldsym; | |
267 | ||
beb1bf64 | 268 | bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym); |
252b5132 RH |
269 | |
270 | symbuf->symbol.the_bfd = abfd; | |
271 | ||
272 | if (ldsym._l._l_l._l_zeroes == 0) | |
273 | symbuf->symbol.name = strings + ldsym._l._l_l._l_offset; | |
274 | else | |
275 | { | |
beb1bf64 TR |
276 | char *c; |
277 | ||
dc810e39 | 278 | c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1); |
beb1bf64 TR |
279 | if (c == NULL) |
280 | return -1; | |
281 | memcpy (c, ldsym._l._l_name, SYMNMLEN); | |
282 | c[SYMNMLEN] = '\0'; | |
283 | symbuf->symbol.name = c; | |
252b5132 RH |
284 | } |
285 | ||
286 | if (ldsym.l_smclas == XMC_XO) | |
287 | symbuf->symbol.section = bfd_abs_section_ptr; | |
288 | else | |
289 | symbuf->symbol.section = coff_section_from_bfd_index (abfd, | |
290 | ldsym.l_scnum); | |
291 | symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma; | |
292 | ||
293 | symbuf->symbol.flags = BSF_NO_FLAGS; | |
294 | if ((ldsym.l_smtype & L_EXPORT) != 0) | |
295 | symbuf->symbol.flags |= BSF_GLOBAL; | |
296 | ||
297 | /* FIXME: We have no way to record the other information stored | |
b34976b6 | 298 | with the loader symbol. */ |
252b5132 RH |
299 | |
300 | *psyms = (asymbol *) symbuf; | |
301 | } | |
302 | ||
303 | *psyms = NULL; | |
304 | ||
305 | return ldhdr.l_nsyms; | |
306 | } | |
307 | ||
308 | /* Get the size required to hold the dynamic relocs. */ | |
309 | ||
310 | long | |
311 | _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd) | |
312 | bfd *abfd; | |
313 | { | |
314 | asection *lsec; | |
315 | bfd_byte *contents; | |
316 | struct internal_ldhdr ldhdr; | |
317 | ||
318 | if ((abfd->flags & DYNAMIC) == 0) | |
319 | { | |
320 | bfd_set_error (bfd_error_invalid_operation); | |
321 | return -1; | |
322 | } | |
323 | ||
324 | lsec = bfd_get_section_by_name (abfd, ".loader"); | |
325 | if (lsec == NULL) | |
326 | { | |
327 | bfd_set_error (bfd_error_no_symbols); | |
328 | return -1; | |
329 | } | |
330 | ||
331 | if (! xcoff_get_section_contents (abfd, lsec)) | |
332 | return -1; | |
333 | contents = coff_section_data (abfd, lsec)->contents; | |
334 | ||
beb1bf64 | 335 | bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr); |
252b5132 RH |
336 | |
337 | return (ldhdr.l_nreloc + 1) * sizeof (arelent *); | |
338 | } | |
339 | ||
252b5132 RH |
340 | /* Get the dynamic relocs. */ |
341 | ||
342 | long | |
343 | _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms) | |
344 | bfd *abfd; | |
345 | arelent **prelocs; | |
346 | asymbol **syms; | |
347 | { | |
348 | asection *lsec; | |
349 | bfd_byte *contents; | |
350 | struct internal_ldhdr ldhdr; | |
351 | arelent *relbuf; | |
beb1bf64 | 352 | bfd_byte *elrel, *elrelend; |
252b5132 RH |
353 | |
354 | if ((abfd->flags & DYNAMIC) == 0) | |
355 | { | |
356 | bfd_set_error (bfd_error_invalid_operation); | |
357 | return -1; | |
358 | } | |
359 | ||
360 | lsec = bfd_get_section_by_name (abfd, ".loader"); | |
361 | if (lsec == NULL) | |
362 | { | |
363 | bfd_set_error (bfd_error_no_symbols); | |
364 | return -1; | |
365 | } | |
366 | ||
367 | if (! xcoff_get_section_contents (abfd, lsec)) | |
368 | return -1; | |
369 | contents = coff_section_data (abfd, lsec)->contents; | |
370 | ||
beb1bf64 | 371 | bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); |
252b5132 RH |
372 | |
373 | relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent)); | |
374 | if (relbuf == NULL) | |
375 | return -1; | |
376 | ||
beb1bf64 TR |
377 | elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr); |
378 | ||
379 | elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd); | |
dc810e39 AM |
380 | for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++, |
381 | prelocs++) | |
252b5132 RH |
382 | { |
383 | struct internal_ldrel ldrel; | |
384 | ||
beb1bf64 | 385 | bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel); |
252b5132 RH |
386 | |
387 | if (ldrel.l_symndx >= 3) | |
388 | relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3); | |
389 | else | |
390 | { | |
391 | const char *name; | |
392 | asection *sec; | |
393 | ||
394 | switch (ldrel.l_symndx) | |
395 | { | |
396 | case 0: | |
397 | name = ".text"; | |
398 | break; | |
399 | case 1: | |
400 | name = ".data"; | |
401 | break; | |
402 | case 2: | |
403 | name = ".bss"; | |
404 | break; | |
405 | default: | |
406 | abort (); | |
407 | break; | |
408 | } | |
409 | ||
410 | sec = bfd_get_section_by_name (abfd, name); | |
411 | if (sec == NULL) | |
412 | { | |
413 | bfd_set_error (bfd_error_bad_value); | |
414 | return -1; | |
415 | } | |
416 | ||
417 | relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr; | |
418 | } | |
419 | ||
420 | relbuf->address = ldrel.l_vaddr; | |
421 | relbuf->addend = 0; | |
422 | ||
423 | /* Most dynamic relocs have the same type. FIXME: This is only | |
b34976b6 AM |
424 | correct if ldrel.l_rtype == 0. In other cases, we should use |
425 | a different howto. */ | |
beb1bf64 | 426 | relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd); |
252b5132 RH |
427 | |
428 | /* FIXME: We have no way to record the l_rsecnm field. */ | |
429 | ||
430 | *prelocs = relbuf; | |
431 | } | |
432 | ||
433 | *prelocs = NULL; | |
434 | ||
435 | return ldhdr.l_nreloc; | |
436 | } | |
437 | \f | |
438 | /* Routine to create an entry in an XCOFF link hash table. */ | |
439 | ||
440 | static struct bfd_hash_entry * | |
441 | xcoff_link_hash_newfunc (entry, table, string) | |
442 | struct bfd_hash_entry *entry; | |
443 | struct bfd_hash_table *table; | |
444 | const char *string; | |
445 | { | |
446 | struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry; | |
447 | ||
448 | /* Allocate the structure if it has not already been allocated by a | |
449 | subclass. */ | |
450 | if (ret == (struct xcoff_link_hash_entry *) NULL) | |
451 | ret = ((struct xcoff_link_hash_entry *) | |
452 | bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry))); | |
453 | if (ret == (struct xcoff_link_hash_entry *) NULL) | |
454 | return (struct bfd_hash_entry *) ret; | |
455 | ||
456 | /* Call the allocation method of the superclass. */ | |
457 | ret = ((struct xcoff_link_hash_entry *) | |
458 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
459 | table, string)); | |
460 | if (ret != NULL) | |
461 | { | |
462 | /* Set local fields. */ | |
463 | ret->indx = -1; | |
464 | ret->toc_section = NULL; | |
465 | ret->u.toc_indx = -1; | |
466 | ret->descriptor = NULL; | |
467 | ret->ldsym = NULL; | |
468 | ret->ldindx = -1; | |
469 | ret->flags = 0; | |
470 | ret->smclas = XMC_UA; | |
471 | } | |
472 | ||
473 | return (struct bfd_hash_entry *) ret; | |
474 | } | |
475 | ||
476 | /* Create a XCOFF link hash table. */ | |
477 | ||
478 | struct bfd_link_hash_table * | |
479 | _bfd_xcoff_bfd_link_hash_table_create (abfd) | |
480 | bfd *abfd; | |
481 | { | |
482 | struct xcoff_link_hash_table *ret; | |
dc810e39 | 483 | bfd_size_type amt = sizeof (struct xcoff_link_hash_table); |
252b5132 | 484 | |
e2d34d7d | 485 | ret = (struct xcoff_link_hash_table *) bfd_malloc (amt); |
252b5132 RH |
486 | if (ret == (struct xcoff_link_hash_table *) NULL) |
487 | return (struct bfd_link_hash_table *) NULL; | |
488 | if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc)) | |
489 | { | |
e2d34d7d | 490 | free (ret); |
252b5132 RH |
491 | return (struct bfd_link_hash_table *) NULL; |
492 | } | |
493 | ||
494 | ret->debug_strtab = _bfd_xcoff_stringtab_init (); | |
495 | ret->debug_section = NULL; | |
496 | ret->loader_section = NULL; | |
497 | ret->ldrel_count = 0; | |
498 | memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr)); | |
499 | ret->linkage_section = NULL; | |
500 | ret->toc_section = NULL; | |
501 | ret->descriptor_section = NULL; | |
502 | ret->imports = NULL; | |
503 | ret->file_align = 0; | |
b34976b6 AM |
504 | ret->textro = FALSE; |
505 | ret->gc = FALSE; | |
252b5132 RH |
506 | memset (ret->special_sections, 0, sizeof ret->special_sections); |
507 | ||
508 | /* The linker will always generate a full a.out header. We need to | |
509 | record that fact now, before the sizeof_headers routine could be | |
510 | called. */ | |
b34976b6 | 511 | xcoff_data (abfd)->full_aouthdr = TRUE; |
252b5132 RH |
512 | |
513 | return &ret->root; | |
514 | } | |
515 | ||
e2d34d7d DJ |
516 | /* Free a XCOFF link hash table. */ |
517 | ||
518 | void | |
519 | _bfd_xcoff_bfd_link_hash_table_free (hash) | |
520 | struct bfd_link_hash_table *hash; | |
521 | { | |
522 | struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash; | |
523 | ||
524 | _bfd_stringtab_free (ret->debug_strtab); | |
525 | bfd_hash_table_free (&ret->root.table); | |
526 | free (ret); | |
527 | } | |
252b5132 RH |
528 | \f |
529 | /* Read internal relocs for an XCOFF csect. This is a wrapper around | |
530 | _bfd_coff_read_internal_relocs which tries to take advantage of any | |
531 | relocs which may have been cached for the enclosing section. */ | |
532 | ||
533 | static struct internal_reloc * | |
534 | xcoff_read_internal_relocs (abfd, sec, cache, external_relocs, | |
535 | require_internal, internal_relocs) | |
536 | bfd *abfd; | |
537 | asection *sec; | |
b34976b6 | 538 | bfd_boolean cache; |
252b5132 | 539 | bfd_byte *external_relocs; |
b34976b6 | 540 | bfd_boolean require_internal; |
252b5132 RH |
541 | struct internal_reloc *internal_relocs; |
542 | { | |
beb1bf64 | 543 | |
252b5132 RH |
544 | if (coff_section_data (abfd, sec) != NULL |
545 | && coff_section_data (abfd, sec)->relocs == NULL | |
546 | && xcoff_section_data (abfd, sec) != NULL) | |
547 | { | |
548 | asection *enclosing; | |
549 | ||
550 | enclosing = xcoff_section_data (abfd, sec)->enclosing; | |
551 | ||
552 | if (enclosing != NULL | |
553 | && (coff_section_data (abfd, enclosing) == NULL | |
554 | || coff_section_data (abfd, enclosing)->relocs == NULL) | |
555 | && cache | |
556 | && enclosing->reloc_count > 0) | |
557 | { | |
b34976b6 AM |
558 | if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE, |
559 | external_relocs, FALSE, | |
252b5132 RH |
560 | (struct internal_reloc *) NULL) |
561 | == NULL) | |
562 | return NULL; | |
563 | } | |
564 | ||
565 | if (enclosing != NULL | |
566 | && coff_section_data (abfd, enclosing) != NULL | |
567 | && coff_section_data (abfd, enclosing)->relocs != NULL) | |
568 | { | |
569 | size_t off; | |
570 | ||
571 | off = ((sec->rel_filepos - enclosing->rel_filepos) | |
572 | / bfd_coff_relsz (abfd)); | |
beb1bf64 | 573 | |
252b5132 RH |
574 | if (! require_internal) |
575 | return coff_section_data (abfd, enclosing)->relocs + off; | |
576 | memcpy (internal_relocs, | |
577 | coff_section_data (abfd, enclosing)->relocs + off, | |
578 | sec->reloc_count * sizeof (struct internal_reloc)); | |
579 | return internal_relocs; | |
580 | } | |
581 | } | |
582 | ||
583 | return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs, | |
584 | require_internal, internal_relocs); | |
585 | } | |
586 | \f | |
587 | /* Given an XCOFF BFD, add symbols to the global hash table as | |
588 | appropriate. */ | |
589 | ||
b34976b6 | 590 | bfd_boolean |
252b5132 RH |
591 | _bfd_xcoff_bfd_link_add_symbols (abfd, info) |
592 | bfd *abfd; | |
593 | struct bfd_link_info *info; | |
594 | { | |
beb1bf64 | 595 | |
252b5132 RH |
596 | switch (bfd_get_format (abfd)) |
597 | { | |
598 | case bfd_object: | |
599 | return xcoff_link_add_object_symbols (abfd, info); | |
600 | ||
601 | case bfd_archive: | |
602 | /* If the archive has a map, do the usual search. We then need | |
b34976b6 AM |
603 | to check the archive for dynamic objects, because they may not |
604 | appear in the archive map even though they should, perhaps, be | |
605 | included. If the archive has no map, we just consider each object | |
606 | file in turn, since that apparently is what the AIX native linker | |
789c90aa | 607 | does. */ |
252b5132 RH |
608 | if (bfd_has_map (abfd)) |
609 | { | |
610 | if (! (_bfd_generic_link_add_archive_symbols | |
611 | (abfd, info, xcoff_link_check_archive_element))) | |
b34976b6 | 612 | return FALSE; |
252b5132 RH |
613 | } |
614 | ||
615 | { | |
616 | bfd *member; | |
b34976b6 | 617 | |
252b5132 RH |
618 | member = bfd_openr_next_archived_file (abfd, (bfd *) NULL); |
619 | while (member != NULL) | |
620 | { | |
621 | if (bfd_check_format (member, bfd_object) | |
789c90aa TR |
622 | && (info->hash->creator == member->xvec) |
623 | && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0)) | |
252b5132 | 624 | { |
b34976b6 AM |
625 | bfd_boolean needed; |
626 | ||
627 | if (! xcoff_link_check_archive_element (member, info, | |
789c90aa | 628 | &needed)) |
b34976b6 | 629 | return FALSE; |
252b5132 RH |
630 | if (needed) |
631 | member->archive_pass = -1; | |
632 | } | |
633 | member = bfd_openr_next_archived_file (abfd, member); | |
634 | } | |
635 | } | |
636 | ||
b34976b6 | 637 | return TRUE; |
252b5132 RH |
638 | |
639 | default: | |
640 | bfd_set_error (bfd_error_wrong_format); | |
b34976b6 | 641 | return FALSE; |
252b5132 RH |
642 | } |
643 | } | |
644 | ||
645 | /* Add symbols from an XCOFF object file. */ | |
646 | ||
b34976b6 | 647 | static bfd_boolean |
252b5132 RH |
648 | xcoff_link_add_object_symbols (abfd, info) |
649 | bfd *abfd; | |
650 | struct bfd_link_info *info; | |
651 | { | |
beb1bf64 | 652 | |
252b5132 | 653 | if (! _bfd_coff_get_external_symbols (abfd)) |
b34976b6 | 654 | return FALSE; |
252b5132 | 655 | if (! xcoff_link_add_symbols (abfd, info)) |
b34976b6 | 656 | return FALSE; |
252b5132 RH |
657 | if (! info->keep_memory) |
658 | { | |
659 | if (! _bfd_coff_free_symbols (abfd)) | |
b34976b6 | 660 | return FALSE; |
252b5132 | 661 | } |
b34976b6 | 662 | return TRUE; |
252b5132 RH |
663 | } |
664 | ||
665 | /* Check a single archive element to see if we need to include it in | |
666 | the link. *PNEEDED is set according to whether this element is | |
667 | needed in the link or not. This is called via | |
668 | _bfd_generic_link_add_archive_symbols. */ | |
669 | ||
b34976b6 | 670 | static bfd_boolean |
252b5132 RH |
671 | xcoff_link_check_archive_element (abfd, info, pneeded) |
672 | bfd *abfd; | |
673 | struct bfd_link_info *info; | |
b34976b6 | 674 | bfd_boolean *pneeded; |
252b5132 | 675 | { |
beb1bf64 | 676 | |
252b5132 | 677 | if (! _bfd_coff_get_external_symbols (abfd)) |
b34976b6 | 678 | return FALSE; |
252b5132 RH |
679 | |
680 | if (! xcoff_link_check_ar_symbols (abfd, info, pneeded)) | |
b34976b6 | 681 | return FALSE; |
252b5132 RH |
682 | |
683 | if (*pneeded) | |
684 | { | |
685 | if (! xcoff_link_add_symbols (abfd, info)) | |
b34976b6 | 686 | return FALSE; |
252b5132 RH |
687 | } |
688 | ||
689 | if (! info->keep_memory || ! *pneeded) | |
690 | { | |
691 | if (! _bfd_coff_free_symbols (abfd)) | |
b34976b6 | 692 | return FALSE; |
252b5132 RH |
693 | } |
694 | ||
b34976b6 | 695 | return TRUE; |
252b5132 RH |
696 | } |
697 | ||
698 | /* Look through the symbols to see if this object file should be | |
699 | included in the link. */ | |
700 | ||
b34976b6 | 701 | static bfd_boolean |
252b5132 RH |
702 | xcoff_link_check_ar_symbols (abfd, info, pneeded) |
703 | bfd *abfd; | |
704 | struct bfd_link_info *info; | |
b34976b6 | 705 | bfd_boolean *pneeded; |
252b5132 RH |
706 | { |
707 | bfd_size_type symesz; | |
708 | bfd_byte *esym; | |
709 | bfd_byte *esym_end; | |
710 | ||
b34976b6 | 711 | *pneeded = FALSE; |
252b5132 RH |
712 | |
713 | if ((abfd->flags & DYNAMIC) != 0 | |
714 | && ! info->static_link | |
715 | && info->hash->creator == abfd->xvec) | |
716 | return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded); | |
717 | ||
718 | symesz = bfd_coff_symesz (abfd); | |
719 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
720 | esym_end = esym + obj_raw_syment_count (abfd) * symesz; | |
721 | while (esym < esym_end) | |
722 | { | |
723 | struct internal_syment sym; | |
724 | ||
725 | bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym); | |
726 | ||
727 | if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF) | |
728 | { | |
729 | const char *name; | |
730 | char buf[SYMNMLEN + 1]; | |
731 | struct bfd_link_hash_entry *h; | |
732 | ||
733 | /* This symbol is externally visible, and is defined by this | |
b34976b6 | 734 | object file. */ |
252b5132 RH |
735 | |
736 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
beb1bf64 | 737 | |
252b5132 | 738 | if (name == NULL) |
b34976b6 AM |
739 | return FALSE; |
740 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); | |
252b5132 RH |
741 | |
742 | /* We are only interested in symbols that are currently | |
743 | undefined. If a symbol is currently known to be common, | |
744 | XCOFF linkers do not bring in an object file which | |
745 | defines it. We also don't bring in symbols to satisfy | |
746 | undefined references in shared objects. */ | |
747 | if (h != (struct bfd_link_hash_entry *) NULL | |
748 | && h->type == bfd_link_hash_undefined | |
dc810e39 | 749 | && (info->hash->creator != abfd->xvec |
252b5132 RH |
750 | || (((struct xcoff_link_hash_entry *) h)->flags |
751 | & XCOFF_DEF_DYNAMIC) == 0)) | |
752 | { | |
753 | if (! (*info->callbacks->add_archive_element) (info, abfd, name)) | |
b34976b6 AM |
754 | return FALSE; |
755 | *pneeded = TRUE; | |
756 | return TRUE; | |
252b5132 RH |
757 | } |
758 | } | |
759 | ||
760 | esym += (sym.n_numaux + 1) * symesz; | |
761 | } | |
762 | ||
763 | /* We do not need this object file. */ | |
b34976b6 | 764 | return TRUE; |
252b5132 RH |
765 | } |
766 | ||
767 | /* Look through the loader symbols to see if this dynamic object | |
768 | should be included in the link. The native linker uses the loader | |
769 | symbols, not the normal symbol table, so we do too. */ | |
770 | ||
b34976b6 | 771 | static bfd_boolean |
252b5132 RH |
772 | xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded) |
773 | bfd *abfd; | |
774 | struct bfd_link_info *info; | |
b34976b6 | 775 | bfd_boolean *pneeded; |
252b5132 RH |
776 | { |
777 | asection *lsec; | |
beb1bf64 | 778 | bfd_byte *contents; |
252b5132 RH |
779 | struct internal_ldhdr ldhdr; |
780 | const char *strings; | |
beb1bf64 | 781 | bfd_byte *elsym, *elsymend; |
252b5132 | 782 | |
b34976b6 | 783 | *pneeded = FALSE; |
252b5132 RH |
784 | |
785 | lsec = bfd_get_section_by_name (abfd, ".loader"); | |
786 | if (lsec == NULL) | |
787 | { | |
788 | /* There are no symbols, so don't try to include it. */ | |
b34976b6 | 789 | return TRUE; |
252b5132 RH |
790 | } |
791 | ||
792 | if (! xcoff_get_section_contents (abfd, lsec)) | |
b34976b6 | 793 | return FALSE; |
beb1bf64 TR |
794 | contents = coff_section_data (abfd, lsec)->contents; |
795 | ||
796 | bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); | |
252b5132 | 797 | |
beb1bf64 | 798 | strings = (char *) contents + ldhdr.l_stoff; |
252b5132 | 799 | |
beb1bf64 | 800 | elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr); |
252b5132 | 801 | |
beb1bf64 TR |
802 | elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd); |
803 | for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd)) | |
252b5132 RH |
804 | { |
805 | struct internal_ldsym ldsym; | |
806 | char nambuf[SYMNMLEN + 1]; | |
807 | const char *name; | |
808 | struct bfd_link_hash_entry *h; | |
809 | ||
beb1bf64 | 810 | bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym); |
252b5132 RH |
811 | |
812 | /* We are only interested in exported symbols. */ | |
813 | if ((ldsym.l_smtype & L_EXPORT) == 0) | |
814 | continue; | |
815 | ||
816 | if (ldsym._l._l_l._l_zeroes == 0) | |
817 | name = strings + ldsym._l._l_l._l_offset; | |
818 | else | |
819 | { | |
820 | memcpy (nambuf, ldsym._l._l_name, SYMNMLEN); | |
821 | nambuf[SYMNMLEN] = '\0'; | |
822 | name = nambuf; | |
823 | } | |
824 | ||
b34976b6 | 825 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); |
252b5132 RH |
826 | |
827 | /* We are only interested in symbols that are currently | |
b34976b6 AM |
828 | undefined. At this point we know that we are using an XCOFF |
829 | hash table. */ | |
252b5132 RH |
830 | if (h != NULL |
831 | && h->type == bfd_link_hash_undefined | |
832 | && (((struct xcoff_link_hash_entry *) h)->flags | |
833 | & XCOFF_DEF_DYNAMIC) == 0) | |
834 | { | |
835 | if (! (*info->callbacks->add_archive_element) (info, abfd, name)) | |
b34976b6 AM |
836 | return FALSE; |
837 | *pneeded = TRUE; | |
838 | return TRUE; | |
252b5132 RH |
839 | } |
840 | } | |
841 | ||
842 | /* We do not need this shared object. */ | |
843 | ||
beb1bf64 | 844 | if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents) |
252b5132 RH |
845 | { |
846 | free (coff_section_data (abfd, lsec)->contents); | |
847 | coff_section_data (abfd, lsec)->contents = NULL; | |
848 | } | |
849 | ||
b34976b6 | 850 | return TRUE; |
252b5132 RH |
851 | } |
852 | ||
853 | /* Returns the index of reloc in RELOCS with the least address greater | |
854 | than or equal to ADDRESS. The relocs are sorted by address. */ | |
855 | ||
856 | static bfd_size_type | |
857 | xcoff_find_reloc (relocs, count, address) | |
858 | struct internal_reloc *relocs; | |
859 | bfd_size_type count; | |
860 | bfd_vma address; | |
861 | { | |
862 | bfd_size_type min, max, this; | |
863 | ||
864 | if (count < 2) | |
865 | { | |
866 | if (count == 1 && relocs[0].r_vaddr < address) | |
867 | return 1; | |
868 | else | |
869 | return 0; | |
870 | } | |
871 | ||
872 | min = 0; | |
873 | max = count; | |
874 | ||
875 | /* Do a binary search over (min,max]. */ | |
876 | while (min + 1 < max) | |
877 | { | |
878 | bfd_vma raddr; | |
879 | ||
880 | this = (max + min) / 2; | |
881 | raddr = relocs[this].r_vaddr; | |
882 | if (raddr > address) | |
883 | max = this; | |
884 | else if (raddr < address) | |
885 | min = this; | |
886 | else | |
887 | { | |
888 | min = this; | |
889 | break; | |
890 | } | |
891 | } | |
892 | ||
893 | if (relocs[min].r_vaddr < address) | |
894 | return min + 1; | |
895 | ||
896 | while (min > 0 | |
897 | && relocs[min - 1].r_vaddr == address) | |
898 | --min; | |
899 | ||
900 | return min; | |
901 | } | |
902 | ||
beb1bf64 | 903 | |
dc810e39 AM |
904 | /* xcoff_link_create_extra_sections |
905 | ||
906 | Takes care of creating the .loader, .gl, .ds, .debug and sections. */ | |
907 | ||
b34976b6 | 908 | static bfd_boolean |
dc810e39 AM |
909 | xcoff_link_create_extra_sections(bfd * abfd, struct bfd_link_info *info) |
910 | { | |
beb1bf64 | 911 | |
b34976b6 | 912 | bfd_boolean return_value = FALSE; |
beb1bf64 | 913 | |
dc810e39 AM |
914 | if (info->hash->creator == abfd->xvec) |
915 | { | |
beb1bf64 TR |
916 | |
917 | /* We need to build a .loader section, so we do it here. This | |
918 | won't work if we're producing an XCOFF output file with no | |
919 | XCOFF input files. FIXME. */ | |
920 | ||
dc810e39 AM |
921 | if (xcoff_hash_table (info)->loader_section == NULL) |
922 | { | |
923 | asection *lsec; | |
924 | ||
925 | lsec = bfd_make_section_anyway (abfd, ".loader"); | |
926 | if (lsec == NULL) | |
927 | { | |
928 | goto end_return; | |
929 | } | |
930 | xcoff_hash_table (info)->loader_section = lsec; | |
931 | lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
beb1bf64 | 932 | } |
beb1bf64 TR |
933 | |
934 | /* Likewise for the linkage section. */ | |
dc810e39 AM |
935 | if (xcoff_hash_table (info)->linkage_section == NULL) |
936 | { | |
937 | asection *lsec; | |
beb1bf64 | 938 | |
dc810e39 AM |
939 | lsec = bfd_make_section_anyway (abfd, ".gl"); |
940 | if (lsec == NULL) | |
941 | { | |
942 | goto end_return; | |
943 | } | |
beb1bf64 | 944 | |
dc810e39 AM |
945 | xcoff_hash_table (info)->linkage_section = lsec; |
946 | lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
947 | | SEC_IN_MEMORY); | |
948 | lsec->alignment_power = 2; | |
949 | } | |
beb1bf64 TR |
950 | |
951 | /* Likewise for the TOC section. */ | |
dc810e39 AM |
952 | if (xcoff_hash_table (info)->toc_section == NULL) |
953 | { | |
954 | asection *tsec; | |
beb1bf64 | 955 | |
dc810e39 AM |
956 | tsec = bfd_make_section_anyway (abfd, ".tc"); |
957 | if (tsec == NULL) | |
958 | { | |
959 | goto end_return; | |
960 | } | |
beb1bf64 | 961 | |
dc810e39 AM |
962 | xcoff_hash_table (info)->toc_section = tsec; |
963 | tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
964 | | SEC_IN_MEMORY); | |
965 | tsec->alignment_power = 2; | |
beb1bf64 TR |
966 | } |
967 | ||
dc810e39 AM |
968 | /* Likewise for the descriptor section. */ |
969 | if (xcoff_hash_table (info)->descriptor_section == NULL) | |
970 | { | |
971 | asection *dsec; | |
972 | ||
973 | dsec = bfd_make_section_anyway (abfd, ".ds"); | |
974 | if (dsec == NULL) | |
975 | { | |
976 | goto end_return; | |
977 | } | |
978 | ||
979 | xcoff_hash_table (info)->descriptor_section = dsec; | |
980 | dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
981 | | SEC_IN_MEMORY); | |
982 | dsec->alignment_power = 2; | |
983 | } | |
beb1bf64 TR |
984 | |
985 | /* Likewise for the .debug section. */ | |
986 | if (xcoff_hash_table (info)->debug_section == NULL | |
dc810e39 AM |
987 | && info->strip != strip_all) |
988 | { | |
989 | asection *dsec; | |
beb1bf64 | 990 | |
dc810e39 AM |
991 | dsec = bfd_make_section_anyway (abfd, ".debug"); |
992 | if (dsec == NULL) | |
993 | { | |
994 | goto end_return; | |
995 | } | |
996 | xcoff_hash_table (info)->debug_section = dsec; | |
997 | dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
beb1bf64 | 998 | } |
dc810e39 AM |
999 | } |
1000 | ||
b34976b6 | 1001 | return_value = TRUE; |
beb1bf64 TR |
1002 | |
1003 | end_return: | |
1004 | ||
1005 | return return_value; | |
1006 | } | |
1007 | ||
252b5132 RH |
1008 | /* Add all the symbols from an object file to the hash table. |
1009 | ||
1010 | XCOFF is a weird format. A normal XCOFF .o files will have three | |
1011 | COFF sections--.text, .data, and .bss--but each COFF section will | |
1012 | contain many csects. These csects are described in the symbol | |
1013 | table. From the linker's point of view, each csect must be | |
1014 | considered a section in its own right. For example, a TOC entry is | |
1015 | handled as a small XMC_TC csect. The linker must be able to merge | |
1016 | different TOC entries together, which means that it must be able to | |
1017 | extract the XMC_TC csects from the .data section of the input .o | |
1018 | file. | |
1019 | ||
1020 | From the point of view of our linker, this is, of course, a hideous | |
1021 | nightmare. We cope by actually creating sections for each csect, | |
1022 | and discarding the original sections. We then have to handle the | |
1023 | relocation entries carefully, since the only way to tell which | |
1024 | csect they belong to is to examine the address. */ | |
1025 | ||
b34976b6 | 1026 | static bfd_boolean |
252b5132 RH |
1027 | xcoff_link_add_symbols (abfd, info) |
1028 | bfd *abfd; | |
1029 | struct bfd_link_info *info; | |
1030 | { | |
1031 | unsigned int n_tmask; | |
1032 | unsigned int n_btshft; | |
b34976b6 | 1033 | bfd_boolean default_copy; |
252b5132 RH |
1034 | bfd_size_type symcount; |
1035 | struct xcoff_link_hash_entry **sym_hash; | |
1036 | asection **csect_cache; | |
1037 | bfd_size_type linesz; | |
1038 | asection *o; | |
1039 | asection *last_real; | |
b34976b6 | 1040 | bfd_boolean keep_syms; |
252b5132 RH |
1041 | asection *csect; |
1042 | unsigned int csect_index; | |
1043 | asection *first_csect; | |
1044 | bfd_size_type symesz; | |
1045 | bfd_byte *esym; | |
1046 | bfd_byte *esym_end; | |
dc810e39 | 1047 | struct reloc_info_struct |
beb1bf64 TR |
1048 | { |
1049 | struct internal_reloc *relocs; | |
1050 | asection **csects; | |
1051 | bfd_byte *linenos; | |
1052 | } *reloc_info = NULL; | |
dc810e39 | 1053 | bfd_size_type amt; |
252b5132 RH |
1054 | |
1055 | keep_syms = obj_coff_keep_syms (abfd); | |
1056 | ||
1057 | if ((abfd->flags & DYNAMIC) != 0 | |
dc810e39 AM |
1058 | && ! info->static_link) |
1059 | { | |
1060 | if (! xcoff_link_add_dynamic_symbols (abfd, info)) | |
b34976b6 | 1061 | return FALSE; |
252b5132 RH |
1062 | } |
1063 | ||
beb1bf64 | 1064 | /* create the loader, toc, gl, ds and debug sections, if needed */ |
b34976b6 | 1065 | if (! xcoff_link_create_extra_sections (abfd, info)) |
69f284c7 | 1066 | goto error_return; |
252b5132 RH |
1067 | |
1068 | if ((abfd->flags & DYNAMIC) != 0 | |
1069 | && ! info->static_link) | |
b34976b6 | 1070 | return TRUE; |
252b5132 RH |
1071 | |
1072 | n_tmask = coff_data (abfd)->local_n_tmask; | |
1073 | n_btshft = coff_data (abfd)->local_n_btshft; | |
1074 | ||
1075 | /* Define macros so that ISFCN, et. al., macros work correctly. */ | |
1076 | #define N_TMASK n_tmask | |
1077 | #define N_BTSHFT n_btshft | |
1078 | ||
1079 | if (info->keep_memory) | |
b34976b6 | 1080 | default_copy = FALSE; |
252b5132 | 1081 | else |
b34976b6 | 1082 | default_copy = TRUE; |
252b5132 | 1083 | |
dc810e39 | 1084 | symcount = obj_raw_syment_count (abfd); |
252b5132 RH |
1085 | |
1086 | /* We keep a list of the linker hash table entries that correspond | |
1087 | to each external symbol. */ | |
dc810e39 | 1088 | amt = symcount * sizeof (struct xcoff_link_hash_entry *); |
9bab7074 | 1089 | sym_hash = (struct xcoff_link_hash_entry **) bfd_zalloc (abfd, amt); |
252b5132 RH |
1090 | if (sym_hash == NULL && symcount != 0) |
1091 | goto error_return; | |
1092 | coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash; | |
252b5132 RH |
1093 | |
1094 | /* Because of the weird stuff we are doing with XCOFF csects, we can | |
1095 | not easily determine which section a symbol is in, so we store | |
1096 | the information in the tdata for the input file. */ | |
dc810e39 | 1097 | amt = symcount * sizeof (asection *); |
9bab7074 | 1098 | csect_cache = (asection **) bfd_zalloc (abfd, amt); |
252b5132 RH |
1099 | if (csect_cache == NULL && symcount != 0) |
1100 | goto error_return; | |
1101 | xcoff_data (abfd)->csects = csect_cache; | |
252b5132 RH |
1102 | |
1103 | /* While splitting sections into csects, we need to assign the | |
1104 | relocs correctly. The relocs and the csects must both be in | |
1105 | order by VMA within a given section, so we handle this by | |
1106 | scanning along the relocs as we process the csects. We index | |
1107 | into reloc_info using the section target_index. */ | |
dc810e39 AM |
1108 | amt = abfd->section_count + 1; |
1109 | amt *= sizeof (struct reloc_info_struct); | |
9bab7074 | 1110 | reloc_info = (struct reloc_info_struct *) bfd_zmalloc (amt); |
252b5132 RH |
1111 | if (reloc_info == NULL) |
1112 | goto error_return; | |
252b5132 RH |
1113 | |
1114 | /* Read in the relocs and line numbers for each section. */ | |
1115 | linesz = bfd_coff_linesz (abfd); | |
1116 | last_real = NULL; | |
dc810e39 AM |
1117 | for (o = abfd->sections; o != NULL; o = o->next) |
1118 | { | |
252b5132 | 1119 | |
dc810e39 AM |
1120 | last_real = o; |
1121 | if ((o->flags & SEC_RELOC) != 0) | |
1122 | { | |
252b5132 | 1123 | |
dc810e39 | 1124 | reloc_info[o->target_index].relocs = |
b34976b6 AM |
1125 | xcoff_read_internal_relocs (abfd, o, TRUE, (bfd_byte *) NULL, |
1126 | FALSE, (struct internal_reloc *) NULL); | |
dc810e39 AM |
1127 | amt = o->reloc_count; |
1128 | amt *= sizeof (asection *); | |
9bab7074 | 1129 | reloc_info[o->target_index].csects = (asection **) bfd_zmalloc (amt); |
dc810e39 AM |
1130 | if (reloc_info[o->target_index].csects == NULL) |
1131 | goto error_return; | |
dc810e39 | 1132 | } |
beb1bf64 | 1133 | |
dc810e39 AM |
1134 | if ((info->strip == strip_none || info->strip == strip_some) |
1135 | && o->lineno_count > 0) | |
1136 | { | |
beb1bf64 | 1137 | |
dc810e39 AM |
1138 | bfd_byte *linenos; |
1139 | ||
1140 | amt = linesz * o->lineno_count; | |
1141 | linenos = (bfd_byte *) bfd_malloc (amt); | |
1142 | if (linenos == NULL) | |
1143 | goto error_return; | |
1144 | reloc_info[o->target_index].linenos = linenos; | |
1145 | if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0 | |
1146 | || bfd_bread (linenos, amt, abfd) != amt) | |
1147 | goto error_return; | |
1148 | ||
1149 | } | |
252b5132 | 1150 | } |
beb1bf64 | 1151 | |
252b5132 | 1152 | /* Don't let the linker relocation routines discard the symbols. */ |
b34976b6 | 1153 | obj_coff_keep_syms (abfd) = TRUE; |
252b5132 RH |
1154 | |
1155 | csect = NULL; | |
1156 | csect_index = 0; | |
1157 | first_csect = NULL; | |
1158 | ||
1159 | symesz = bfd_coff_symesz (abfd); | |
1160 | BFD_ASSERT (symesz == bfd_coff_auxesz (abfd)); | |
1161 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
1162 | esym_end = esym + symcount * symesz; | |
252b5132 | 1163 | |
dc810e39 AM |
1164 | while (esym < esym_end) |
1165 | { | |
1166 | struct internal_syment sym; | |
1167 | union internal_auxent aux; | |
1168 | const char *name; | |
1169 | char buf[SYMNMLEN + 1]; | |
1170 | int smtyp; | |
1171 | flagword flags; | |
1172 | asection *section; | |
1173 | bfd_vma value; | |
1174 | struct xcoff_link_hash_entry *set_toc; | |
252b5132 | 1175 | |
dc810e39 AM |
1176 | bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym); |
1177 | ||
1178 | /* In this pass we are only interested in symbols with csect | |
1179 | information. */ | |
1180 | if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT) | |
1181 | { | |
252b5132 | 1182 | |
dc810e39 AM |
1183 | /* Set csect_cache, |
1184 | Normally csect is a .pr, .rw etc. created in the loop | |
1185 | If C_FILE or first time, handle special | |
252b5132 | 1186 | |
dc810e39 AM |
1187 | Advance esym, sym_hash, csect_hash ptr's |
1188 | Keep track of the last_symndx for the current file. */ | |
1189 | if (sym.n_sclass == C_FILE && csect != NULL) | |
1190 | { | |
1191 | xcoff_section_data (abfd, csect)->last_symndx = | |
1192 | ((esym | |
1193 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1194 | / symesz); | |
1195 | csect = NULL; | |
1196 | } | |
1197 | ||
1198 | if (csect != NULL) | |
1199 | *csect_cache = csect; | |
1200 | else if (first_csect == NULL || sym.n_sclass == C_FILE) | |
1201 | *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum); | |
1202 | else | |
1203 | *csect_cache = NULL; | |
1204 | esym += (sym.n_numaux + 1) * symesz; | |
1205 | sym_hash += sym.n_numaux + 1; | |
1206 | csect_cache += sym.n_numaux + 1; | |
1207 | ||
1208 | continue; | |
1209 | } | |
1210 | ||
1211 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
1212 | ||
1213 | if (name == NULL) | |
1214 | goto error_return; | |
252b5132 RH |
1215 | |
1216 | /* If this symbol has line number information attached to it, | |
b34976b6 AM |
1217 | and we're not stripping it, count the number of entries and |
1218 | add them to the count for this csect. In the final link pass | |
1219 | we are going to attach line number information by symbol, | |
1220 | rather than by section, in order to more easily handle | |
1221 | garbage collection. */ | |
dc810e39 AM |
1222 | if ((info->strip == strip_none || info->strip == strip_some) |
1223 | && sym.n_numaux > 1 | |
1224 | && csect != NULL | |
1225 | && ISFCN (sym.n_type)) | |
1226 | { | |
252b5132 | 1227 | |
dc810e39 | 1228 | union internal_auxent auxlin; |
252b5132 | 1229 | |
dc810e39 AM |
1230 | bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz), |
1231 | sym.n_type, sym.n_sclass, | |
1232 | 0, sym.n_numaux, (PTR) &auxlin); | |
1233 | ||
1234 | if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) | |
1235 | { | |
1236 | asection *enclosing; | |
1237 | bfd_signed_vma linoff; | |
1238 | ||
1239 | enclosing = xcoff_section_data (abfd, csect)->enclosing; | |
1240 | if (enclosing == NULL) | |
1241 | { | |
1242 | (*_bfd_error_handler) | |
d003868e AM |
1243 | (_("%B: `%s' has line numbers but no enclosing section"), |
1244 | abfd, name); | |
dc810e39 AM |
1245 | bfd_set_error (bfd_error_bad_value); |
1246 | goto error_return; | |
1247 | } | |
1248 | linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr | |
1249 | - enclosing->line_filepos); | |
7dee875e | 1250 | /* explicit cast to bfd_signed_vma for compiler */ |
dc810e39 AM |
1251 | if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz)) |
1252 | { | |
1253 | struct internal_lineno lin; | |
1254 | bfd_byte *linpstart; | |
1255 | ||
1256 | linpstart = (reloc_info[enclosing->target_index].linenos | |
1257 | + linoff); | |
1258 | bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin); | |
1259 | if (lin.l_lnno == 0 | |
1260 | && ((bfd_size_type) lin.l_addr.l_symndx | |
1261 | == ((esym | |
1262 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1263 | / symesz))) | |
1264 | { | |
1265 | bfd_byte *linpend, *linp; | |
1266 | ||
1267 | linpend = (reloc_info[enclosing->target_index].linenos | |
1268 | + enclosing->lineno_count * linesz); | |
1269 | for (linp = linpstart + linesz; | |
1270 | linp < linpend; | |
1271 | linp += linesz) | |
1272 | { | |
1273 | bfd_coff_swap_lineno_in (abfd, (PTR) linp, | |
1274 | (PTR) &lin); | |
1275 | if (lin.l_lnno == 0) | |
1276 | break; | |
1277 | } | |
1278 | csect->lineno_count += (linp - linpstart) / linesz; | |
1279 | /* The setting of line_filepos will only be | |
1280 | useful if all the line number entries for a | |
1281 | csect are contiguous; this only matters for | |
1282 | error reporting. */ | |
1283 | if (csect->line_filepos == 0) | |
1284 | csect->line_filepos = | |
1285 | auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr; | |
1286 | } | |
1287 | } | |
beb1bf64 | 1288 | } |
beb1bf64 | 1289 | } |
252b5132 | 1290 | |
dc810e39 | 1291 | /* Pick up the csect auxiliary information. */ |
beb1bf64 | 1292 | |
dc810e39 AM |
1293 | if (sym.n_numaux == 0) |
1294 | { | |
1295 | (*_bfd_error_handler) | |
d003868e AM |
1296 | (_("%B: class %d symbol `%s' has no aux entries"), |
1297 | abfd, sym.n_sclass, name); | |
dc810e39 AM |
1298 | bfd_set_error (bfd_error_bad_value); |
1299 | goto error_return; | |
1300 | } | |
beb1bf64 | 1301 | |
dc810e39 AM |
1302 | bfd_coff_swap_aux_in (abfd, |
1303 | (PTR) (esym + symesz * sym.n_numaux), | |
1304 | sym.n_type, sym.n_sclass, | |
252b5132 RH |
1305 | sym.n_numaux - 1, sym.n_numaux, |
1306 | (PTR) &aux); | |
1307 | ||
1308 | smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); | |
1309 | ||
1310 | flags = BSF_GLOBAL; | |
1311 | section = NULL; | |
1312 | value = 0; | |
1313 | set_toc = NULL; | |
1314 | ||
1315 | switch (smtyp) | |
1316 | { | |
1317 | default: | |
1318 | (*_bfd_error_handler) | |
d003868e AM |
1319 | (_("%B: symbol `%s' has unrecognized csect type %d"), |
1320 | abfd, name, smtyp); | |
252b5132 RH |
1321 | bfd_set_error (bfd_error_bad_value); |
1322 | goto error_return; | |
1323 | ||
1324 | case XTY_ER: | |
1325 | /* This is an external reference. */ | |
1326 | if (sym.n_sclass == C_HIDEXT | |
1327 | || sym.n_scnum != N_UNDEF | |
1328 | || aux.x_csect.x_scnlen.l != 0) | |
1329 | { | |
1330 | (*_bfd_error_handler) | |
d003868e AM |
1331 | (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"), |
1332 | abfd, name, sym.n_sclass, sym.n_scnum, | |
252b5132 RH |
1333 | aux.x_csect.x_scnlen.l); |
1334 | bfd_set_error (bfd_error_bad_value); | |
1335 | goto error_return; | |
1336 | } | |
1337 | ||
1338 | /* An XMC_XO external reference is actually a reference to | |
b34976b6 | 1339 | an absolute location. */ |
252b5132 RH |
1340 | if (aux.x_csect.x_smclas != XMC_XO) |
1341 | section = bfd_und_section_ptr; | |
1342 | else | |
1343 | { | |
1344 | section = bfd_abs_section_ptr; | |
1345 | value = sym.n_value; | |
1346 | } | |
1347 | break; | |
1348 | ||
1349 | case XTY_SD: | |
1350 | /* This is a csect definition. */ | |
252b5132 RH |
1351 | if (csect != NULL) |
1352 | { | |
1353 | xcoff_section_data (abfd, csect)->last_symndx = | |
dc810e39 | 1354 | ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz); |
252b5132 RH |
1355 | } |
1356 | ||
1357 | csect = NULL; | |
dc810e39 | 1358 | csect_index = -(unsigned) 1; |
252b5132 RH |
1359 | |
1360 | /* When we see a TOC anchor, we record the TOC value. */ | |
1361 | if (aux.x_csect.x_smclas == XMC_TC0) | |
1362 | { | |
1363 | if (sym.n_sclass != C_HIDEXT | |
1364 | || aux.x_csect.x_scnlen.l != 0) | |
1365 | { | |
1366 | (*_bfd_error_handler) | |
d003868e AM |
1367 | (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"), |
1368 | abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l); | |
252b5132 RH |
1369 | bfd_set_error (bfd_error_bad_value); |
1370 | goto error_return; | |
1371 | } | |
1372 | xcoff_data (abfd)->toc = sym.n_value; | |
1373 | } | |
1374 | ||
dc810e39 AM |
1375 | /* We must merge TOC entries for the same symbol. We can |
1376 | merge two TOC entries if they are both C_HIDEXT, they | |
1377 | both have the same name, they are both 4 or 8 bytes long, and | |
1378 | they both have a relocation table entry for an external | |
1379 | symbol with the same name. Unfortunately, this means | |
1380 | that we must look through the relocations. Ick. | |
1381 | ||
1382 | Logic for 32 bit vs 64 bit. | |
1383 | 32 bit has a csect length of 4 for TOC | |
1384 | 64 bit has a csect length of 8 for TOC | |
1385 | ||
1386 | The conditions to get past the if-check are not that bad. | |
1387 | They are what is used to create the TOC csects in the first | |
1388 | place. */ | |
1389 | if (aux.x_csect.x_smclas == XMC_TC | |
1390 | && sym.n_sclass == C_HIDEXT | |
1391 | && info->hash->creator == abfd->xvec | |
1392 | && ((bfd_xcoff_is_xcoff32 (abfd) | |
1393 | && aux.x_csect.x_scnlen.l == 4) | |
1394 | || (bfd_xcoff_is_xcoff64 (abfd) | |
1395 | && aux.x_csect.x_scnlen.l == 8))) | |
1396 | { | |
1397 | asection *enclosing; | |
1398 | struct internal_reloc *relocs; | |
1399 | bfd_size_type relindx; | |
1400 | struct internal_reloc *rel; | |
252b5132 | 1401 | |
dc810e39 AM |
1402 | enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); |
1403 | if (enclosing == NULL) | |
1404 | goto error_return; | |
252b5132 | 1405 | |
dc810e39 AM |
1406 | relocs = reloc_info[enclosing->target_index].relocs; |
1407 | amt = enclosing->reloc_count; | |
1408 | relindx = xcoff_find_reloc (relocs, amt, sym.n_value); | |
1409 | rel = relocs + relindx; | |
252b5132 | 1410 | |
dc810e39 AM |
1411 | /* 32 bit R_POS r_size is 31 |
1412 | 64 bit R_POS r_size is 63 */ | |
1413 | if (relindx < enclosing->reloc_count | |
1414 | && rel->r_vaddr == (bfd_vma) sym.n_value | |
1415 | && rel->r_type == R_POS | |
1416 | && ((bfd_xcoff_is_xcoff32 (abfd) | |
1417 | && rel->r_size == 31) | |
1418 | || (bfd_xcoff_is_xcoff64 (abfd) | |
1419 | && rel->r_size == 63))) | |
1420 | { | |
1421 | bfd_byte *erelsym; | |
1422 | ||
1423 | struct internal_syment relsym; | |
1424 | ||
1425 | erelsym = ((bfd_byte *) obj_coff_external_syms (abfd) | |
1426 | + rel->r_symndx * symesz); | |
1427 | bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym); | |
1428 | if (relsym.n_sclass == C_EXT) | |
1429 | { | |
1430 | const char *relname; | |
1431 | char relbuf[SYMNMLEN + 1]; | |
b34976b6 | 1432 | bfd_boolean copy; |
dc810e39 AM |
1433 | struct xcoff_link_hash_entry *h; |
1434 | ||
1435 | /* At this point we know that the TOC entry is | |
1436 | for an externally visible symbol. */ | |
1437 | ||
1438 | relname = _bfd_coff_internal_syment_name (abfd, &relsym, | |
1439 | relbuf); | |
1440 | if (relname == NULL) | |
1441 | goto error_return; | |
1442 | ||
1443 | /* We only merge TOC entries if the TC name is | |
1444 | the same as the symbol name. This handles | |
1445 | the normal case, but not common cases like | |
1446 | SYM.P4 which gcc generates to store SYM + 4 | |
1447 | in the TOC. FIXME. */ | |
1448 | ||
1449 | if (strcmp (name, relname) == 0) | |
1450 | { | |
1451 | copy = (! info->keep_memory | |
1452 | || relsym._n._n_n._n_zeroes != 0 | |
1453 | || relsym._n._n_n._n_offset == 0); | |
1454 | h = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
b34976b6 AM |
1455 | relname, TRUE, copy, |
1456 | FALSE); | |
dc810e39 AM |
1457 | if (h == NULL) |
1458 | goto error_return; | |
1459 | ||
1460 | /* At this point h->root.type could be | |
1461 | bfd_link_hash_new. That should be OK, | |
1462 | since we know for sure that we will come | |
1463 | across this symbol as we step through the | |
1464 | file. */ | |
1465 | ||
1466 | /* We store h in *sym_hash for the | |
1467 | convenience of the relocate_section | |
1468 | function. */ | |
1469 | *sym_hash = h; | |
1470 | ||
1471 | if (h->toc_section != NULL) | |
1472 | { | |
1473 | asection **rel_csects; | |
1474 | ||
1475 | /* We already have a TOC entry for this | |
1476 | symbol, so we can just ignore this | |
1477 | one. */ | |
1478 | rel_csects = | |
1479 | reloc_info[enclosing->target_index].csects; | |
1480 | rel_csects[relindx] = bfd_und_section_ptr; | |
1481 | break; | |
1482 | } | |
1483 | ||
1484 | /* We are about to create a TOC entry for | |
1485 | this symbol. */ | |
1486 | set_toc = h; | |
1487 | } /* merge toc reloc */ | |
1488 | } /* c_ext */ | |
1489 | } /* reloc */ | |
1490 | } /* merge toc */ | |
252b5132 | 1491 | |
252b5132 | 1492 | { |
beb1bf64 | 1493 | |
252b5132 RH |
1494 | asection *enclosing; |
1495 | ||
beb1bf64 TR |
1496 | /* We need to create a new section. We get the name from |
1497 | the csect storage mapping class, so that the linker can | |
1498 | accumulate similar csects together. */ | |
252b5132 | 1499 | |
beb1bf64 | 1500 | csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name); |
dc810e39 AM |
1501 | if (NULL == csect) |
1502 | { | |
1503 | goto error_return; | |
1504 | } | |
beb1bf64 | 1505 | |
dc810e39 AM |
1506 | /* The enclosing section is the main section : .data, .text |
1507 | or .bss that the csect is coming from. */ | |
252b5132 RH |
1508 | enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); |
1509 | if (enclosing == NULL) | |
1510 | goto error_return; | |
beb1bf64 | 1511 | |
dc810e39 AM |
1512 | if (! bfd_is_abs_section (enclosing) |
1513 | && ((bfd_vma) sym.n_value < enclosing->vma | |
1514 | || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l | |
eea6121a | 1515 | > enclosing->vma + enclosing->size))) |
dc810e39 AM |
1516 | { |
1517 | (*_bfd_error_handler) | |
d003868e AM |
1518 | (_("%B: csect `%s' not in enclosing section"), |
1519 | abfd, name); | |
dc810e39 AM |
1520 | bfd_set_error (bfd_error_bad_value); |
1521 | goto error_return; | |
1522 | } | |
252b5132 RH |
1523 | csect->vma = sym.n_value; |
1524 | csect->filepos = (enclosing->filepos | |
1525 | + sym.n_value | |
1526 | - enclosing->vma); | |
eea6121a | 1527 | csect->size = aux.x_csect.x_scnlen.l; |
252b5132 RH |
1528 | csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; |
1529 | csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); | |
1530 | ||
1531 | /* Record the enclosing section in the tdata for this new | |
1532 | section. */ | |
dc810e39 AM |
1533 | amt = sizeof (struct coff_section_tdata); |
1534 | csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt); | |
252b5132 RH |
1535 | if (csect->used_by_bfd == NULL) |
1536 | goto error_return; | |
dc810e39 AM |
1537 | amt = sizeof (struct xcoff_section_tdata); |
1538 | coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt); | |
252b5132 RH |
1539 | if (coff_section_data (abfd, csect)->tdata == NULL) |
1540 | goto error_return; | |
1541 | xcoff_section_data (abfd, csect)->enclosing = enclosing; | |
1542 | xcoff_section_data (abfd, csect)->lineno_count = | |
1543 | enclosing->lineno_count; | |
1544 | ||
dc810e39 AM |
1545 | if (enclosing->owner == abfd) |
1546 | { | |
1547 | struct internal_reloc *relocs; | |
1548 | bfd_size_type relindx; | |
1549 | struct internal_reloc *rel; | |
1550 | asection **rel_csect; | |
1551 | ||
1552 | relocs = reloc_info[enclosing->target_index].relocs; | |
1553 | amt = enclosing->reloc_count; | |
1554 | relindx = xcoff_find_reloc (relocs, amt, csect->vma); | |
1555 | ||
1556 | rel = relocs + relindx; | |
1557 | rel_csect = (reloc_info[enclosing->target_index].csects | |
1558 | + relindx); | |
1559 | ||
1560 | csect->rel_filepos = (enclosing->rel_filepos | |
1561 | + relindx * bfd_coff_relsz (abfd)); | |
1562 | while (relindx < enclosing->reloc_count | |
1563 | && *rel_csect == NULL | |
eea6121a | 1564 | && rel->r_vaddr < csect->vma + csect->size) |
dc810e39 | 1565 | { |
beb1bf64 | 1566 | |
dc810e39 AM |
1567 | *rel_csect = csect; |
1568 | csect->flags |= SEC_RELOC; | |
1569 | ++csect->reloc_count; | |
1570 | ++relindx; | |
1571 | ++rel; | |
1572 | ++rel_csect; | |
1573 | } | |
252b5132 RH |
1574 | } |
1575 | ||
1576 | /* There are a number of other fields and section flags | |
1577 | which we do not bother to set. */ | |
1578 | ||
1579 | csect_index = ((esym | |
1580 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1581 | / symesz); | |
1582 | ||
1583 | xcoff_section_data (abfd, csect)->first_symndx = csect_index; | |
1584 | ||
1585 | if (first_csect == NULL) | |
1586 | first_csect = csect; | |
1587 | ||
1588 | /* If this symbol is C_EXT, we treat it as starting at the | |
1589 | beginning of the newly created section. */ | |
1590 | if (sym.n_sclass == C_EXT) | |
1591 | { | |
1592 | section = csect; | |
1593 | value = 0; | |
1594 | } | |
1595 | ||
1596 | /* If this is a TOC section for a symbol, record it. */ | |
1597 | if (set_toc != NULL) | |
1598 | set_toc->toc_section = csect; | |
1599 | } | |
1600 | break; | |
1601 | ||
1602 | case XTY_LD: | |
1603 | /* This is a label definition. The x_scnlen field is the | |
dc810e39 | 1604 | symbol index of the csect. Usually the XTY_LD symbol will |
8df8c619 TR |
1605 | follow its appropriate XTY_SD symbol. The .set pseudo op can |
1606 | cause the XTY_LD to not follow the XTY_SD symbol. */ | |
252b5132 | 1607 | { |
b34976b6 | 1608 | bfd_boolean bad; |
252b5132 | 1609 | |
b34976b6 | 1610 | bad = FALSE; |
252b5132 RH |
1611 | if (aux.x_csect.x_scnlen.l < 0 |
1612 | || (aux.x_csect.x_scnlen.l | |
1613 | >= esym - (bfd_byte *) obj_coff_external_syms (abfd))) | |
b34976b6 | 1614 | bad = TRUE; |
252b5132 RH |
1615 | if (! bad) |
1616 | { | |
1617 | section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l]; | |
1618 | if (section == NULL | |
1619 | || (section->flags & SEC_HAS_CONTENTS) == 0) | |
b34976b6 | 1620 | bad = TRUE; |
252b5132 RH |
1621 | } |
1622 | if (bad) | |
1623 | { | |
1624 | (*_bfd_error_handler) | |
d003868e AM |
1625 | (_("%B: misplaced XTY_LD `%s'"), |
1626 | abfd, name); | |
252b5132 RH |
1627 | bfd_set_error (bfd_error_bad_value); |
1628 | goto error_return; | |
1629 | } | |
8df8c619 | 1630 | csect = section; |
252b5132 RH |
1631 | value = sym.n_value - csect->vma; |
1632 | } | |
1633 | break; | |
1634 | ||
1635 | case XTY_CM: | |
1636 | /* This is an unitialized csect. We could base the name on | |
b34976b6 AM |
1637 | the storage mapping class, but we don't bother except for |
1638 | an XMC_TD symbol. If this csect is externally visible, | |
1639 | it is a common symbol. We put XMC_TD symbols in sections | |
1640 | named .tocbss, and rely on the linker script to put that | |
1641 | in the TOC area. */ | |
252b5132 RH |
1642 | |
1643 | if (csect != NULL) | |
1644 | { | |
1645 | xcoff_section_data (abfd, csect)->last_symndx = | |
1646 | ((esym | |
1647 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1648 | / symesz); | |
1649 | } | |
1650 | ||
dc810e39 AM |
1651 | if (aux.x_csect.x_smclas == XMC_TD) |
1652 | { | |
1653 | /* The linker script puts the .td section in the data | |
1654 | section after the .tc section. */ | |
1655 | csect = bfd_make_section_anyway (abfd, ".td"); | |
beb1bf64 | 1656 | |
dc810e39 AM |
1657 | } |
1658 | else | |
1659 | { | |
1660 | csect = bfd_make_section_anyway (abfd, ".bss"); | |
1661 | } | |
252b5132 RH |
1662 | if (csect == NULL) |
1663 | goto error_return; | |
1664 | csect->vma = sym.n_value; | |
eea6121a | 1665 | csect->size = aux.x_csect.x_scnlen.l; |
252b5132 RH |
1666 | csect->flags |= SEC_ALLOC; |
1667 | csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); | |
1668 | /* There are a number of other fields and section flags | |
1669 | which we do not bother to set. */ | |
1670 | ||
1671 | csect_index = ((esym | |
1672 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1673 | / symesz); | |
1674 | ||
dc810e39 AM |
1675 | amt = sizeof (struct coff_section_tdata); |
1676 | csect->used_by_bfd = (PTR) bfd_zalloc (abfd, amt); | |
252b5132 RH |
1677 | if (csect->used_by_bfd == NULL) |
1678 | goto error_return; | |
dc810e39 AM |
1679 | amt = sizeof (struct xcoff_section_tdata); |
1680 | coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt); | |
252b5132 RH |
1681 | if (coff_section_data (abfd, csect)->tdata == NULL) |
1682 | goto error_return; | |
1683 | xcoff_section_data (abfd, csect)->first_symndx = csect_index; | |
1684 | ||
1685 | if (first_csect == NULL) | |
1686 | first_csect = csect; | |
1687 | ||
1688 | if (sym.n_sclass == C_EXT) | |
1689 | { | |
1690 | csect->flags |= SEC_IS_COMMON; | |
eea6121a | 1691 | csect->size = 0; |
252b5132 RH |
1692 | section = csect; |
1693 | value = aux.x_csect.x_scnlen.l; | |
1694 | } | |
1695 | ||
1696 | break; | |
1697 | } | |
1698 | ||
1699 | /* Check for magic symbol names. */ | |
dc810e39 AM |
1700 | if ((smtyp == XTY_SD || smtyp == XTY_CM) |
1701 | && aux.x_csect.x_smclas != XMC_TC | |
1702 | && aux.x_csect.x_smclas != XMC_TD) | |
1703 | { | |
1704 | ||
1705 | int i = -1; | |
1706 | ||
1707 | if (name[0] == '_') | |
1708 | { | |
1709 | if (strcmp (name, "_text") == 0) | |
1710 | i = XCOFF_SPECIAL_SECTION_TEXT; | |
1711 | else if (strcmp (name, "_etext") == 0) | |
1712 | i = XCOFF_SPECIAL_SECTION_ETEXT; | |
1713 | else if (strcmp (name, "_data") == 0) | |
1714 | i = XCOFF_SPECIAL_SECTION_DATA; | |
1715 | else if (strcmp (name, "_edata") == 0) | |
1716 | i = XCOFF_SPECIAL_SECTION_EDATA; | |
1717 | else if (strcmp (name, "_end") == 0) | |
1718 | i = XCOFF_SPECIAL_SECTION_END; | |
1719 | } | |
1720 | else if (name[0] == 'e' && strcmp (name, "end") == 0) | |
1721 | { | |
1722 | i = XCOFF_SPECIAL_SECTION_END2; | |
1723 | } | |
1724 | ||
1725 | if (i != -1) | |
1726 | { | |
1727 | xcoff_hash_table (info)->special_sections[i] = csect; | |
1728 | } | |
1729 | } | |
252b5132 RH |
1730 | |
1731 | /* Now we have enough information to add the symbol to the | |
b34976b6 | 1732 | linker hash table. */ |
252b5132 RH |
1733 | |
1734 | if (sym.n_sclass == C_EXT) | |
1735 | { | |
b34976b6 | 1736 | bfd_boolean copy; |
252b5132 RH |
1737 | |
1738 | BFD_ASSERT (section != NULL); | |
1739 | ||
1740 | /* We must copy the name into memory if we got it from the | |
b34976b6 | 1741 | syment itself, rather than the string table. */ |
252b5132 RH |
1742 | copy = default_copy; |
1743 | if (sym._n._n_n._n_zeroes != 0 | |
1744 | || sym._n._n_n._n_offset == 0) | |
b34976b6 | 1745 | copy = TRUE; |
252b5132 RH |
1746 | |
1747 | /* The AIX linker appears to only detect multiple symbol | |
1748 | definitions when there is a reference to the symbol. If | |
1749 | a symbol is defined multiple times, and the only | |
1750 | references are from the same object file, the AIX linker | |
1751 | appears to permit it. It does not merge the different | |
1752 | definitions, but handles them independently. On the | |
1753 | other hand, if there is a reference, the linker reports | |
1754 | an error. | |
1755 | ||
1756 | This matters because the AIX <net/net_globals.h> header | |
1757 | file actually defines an initialized array, so we have to | |
1758 | actually permit that to work. | |
1759 | ||
1760 | Just to make matters even more confusing, the AIX linker | |
1761 | appears to permit multiple symbol definitions whenever | |
1762 | the second definition is in an archive rather than an | |
1763 | object file. This may be a consequence of the manner in | |
1764 | which it handles archives: I think it may load the entire | |
1765 | archive in as separate csects, and then let garbage | |
1766 | collection discard symbols. | |
1767 | ||
1768 | We also have to handle the case of statically linking a | |
1769 | shared object, which will cause symbol redefinitions, | |
1770 | although this is an easier case to detect. */ | |
1771 | ||
dc810e39 | 1772 | if (info->hash->creator == abfd->xvec) |
252b5132 RH |
1773 | { |
1774 | if (! bfd_is_und_section (section)) | |
07b678c0 TR |
1775 | { |
1776 | *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
b34976b6 | 1777 | name, TRUE, copy, FALSE); |
07b678c0 | 1778 | } |
252b5132 | 1779 | else |
07b678c0 TR |
1780 | { |
1781 | /* Make a copy of the symbol name to prevent problems with | |
1782 | merging symbols. */ | |
1783 | *sym_hash = ((struct xcoff_link_hash_entry *) | |
1784 | bfd_wrapped_link_hash_lookup (abfd, info, name, | |
b34976b6 AM |
1785 | TRUE, TRUE, |
1786 | FALSE)); | |
07b678c0 | 1787 | } |
252b5132 RH |
1788 | if (*sym_hash == NULL) |
1789 | goto error_return; | |
1790 | if (((*sym_hash)->root.type == bfd_link_hash_defined | |
1791 | || (*sym_hash)->root.type == bfd_link_hash_defweak) | |
1792 | && ! bfd_is_und_section (section) | |
1793 | && ! bfd_is_com_section (section)) | |
1794 | { | |
1795 | /* This is a second definition of a defined symbol. */ | |
1796 | if ((abfd->flags & DYNAMIC) != 0 | |
1797 | && ((*sym_hash)->smclas != XMC_GL | |
1798 | || aux.x_csect.x_smclas == XMC_GL | |
1799 | || ((*sym_hash)->root.u.def.section->owner->flags | |
1800 | & DYNAMIC) == 0)) | |
1801 | { | |
1802 | /* The new symbol is from a shared library, and | |
b34976b6 AM |
1803 | either the existing symbol is not global |
1804 | linkage code or this symbol is global linkage | |
1805 | code. If the existing symbol is global | |
1806 | linkage code and the new symbol is not, then | |
1807 | we want to use the new symbol. */ | |
252b5132 RH |
1808 | section = bfd_und_section_ptr; |
1809 | value = 0; | |
1810 | } | |
1811 | else if (((*sym_hash)->root.u.def.section->owner->flags | |
1812 | & DYNAMIC) != 0) | |
1813 | { | |
1814 | /* The existing symbol is from a shared library. | |
b34976b6 | 1815 | Replace it. */ |
252b5132 RH |
1816 | (*sym_hash)->root.type = bfd_link_hash_undefined; |
1817 | (*sym_hash)->root.u.undef.abfd = | |
1818 | (*sym_hash)->root.u.def.section->owner; | |
1819 | } | |
1820 | else if (abfd->my_archive != NULL) | |
1821 | { | |
1822 | /* This is a redefinition in an object contained | |
b34976b6 AM |
1823 | in an archive. Just ignore it. See the |
1824 | comment above. */ | |
252b5132 RH |
1825 | section = bfd_und_section_ptr; |
1826 | value = 0; | |
1827 | } | |
f6e332e6 | 1828 | else if ((*sym_hash)->root.u.undef.next != NULL |
252b5132 RH |
1829 | || info->hash->undefs_tail == &(*sym_hash)->root) |
1830 | { | |
1831 | /* This symbol has been referenced. In this | |
b34976b6 AM |
1832 | case, we just continue and permit the |
1833 | multiple definition error. See the comment | |
1834 | above about the behaviour of the AIX linker. */ | |
252b5132 RH |
1835 | } |
1836 | else if ((*sym_hash)->smclas == aux.x_csect.x_smclas) | |
1837 | { | |
1838 | /* The symbols are both csects of the same | |
b34976b6 AM |
1839 | class. There is at least a chance that this |
1840 | is a semi-legitimate redefinition. */ | |
252b5132 RH |
1841 | section = bfd_und_section_ptr; |
1842 | value = 0; | |
1843 | (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED; | |
1844 | } | |
1845 | } | |
1846 | else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0 | |
1847 | && ((*sym_hash)->root.type == bfd_link_hash_defined | |
1848 | || (*sym_hash)->root.type == bfd_link_hash_defweak) | |
1849 | && (bfd_is_und_section (section) | |
1850 | || bfd_is_com_section (section))) | |
1851 | { | |
1852 | /* This is a reference to a multiply defined symbol. | |
1853 | Report the error now. See the comment above | |
1854 | about the behaviour of the AIX linker. We could | |
1855 | also do this with warning symbols, but I'm not | |
1856 | sure the XCOFF linker is wholly prepared to | |
1857 | handle them, and that would only be a warning, | |
1858 | not an error. */ | |
1859 | if (! ((*info->callbacks->multiple_definition) | |
1860 | (info, (*sym_hash)->root.root.string, | |
dc810e39 | 1861 | (bfd *) NULL, (asection *) NULL, (bfd_vma) 0, |
252b5132 RH |
1862 | (*sym_hash)->root.u.def.section->owner, |
1863 | (*sym_hash)->root.u.def.section, | |
1864 | (*sym_hash)->root.u.def.value))) | |
1865 | goto error_return; | |
1866 | /* Try not to give this error too many times. */ | |
1867 | (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED; | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | /* _bfd_generic_link_add_one_symbol may call the linker to | |
1872 | generate an error message, and the linker may try to read | |
1873 | the symbol table to give a good error. Right now, the | |
1874 | line numbers are in an inconsistent state, since they are | |
1875 | counted both in the real sections and in the new csects. | |
1876 | We need to leave the count in the real sections so that | |
1877 | the linker can report the line number of the error | |
1878 | correctly, so temporarily clobber the link to the csects | |
1879 | so that the linker will not try to read the line numbers | |
1880 | a second time from the csects. */ | |
1881 | BFD_ASSERT (last_real->next == first_csect); | |
1882 | last_real->next = NULL; | |
1883 | if (! (_bfd_generic_link_add_one_symbol | |
1884 | (info, abfd, name, flags, section, value, | |
b34976b6 | 1885 | (const char *) NULL, copy, TRUE, |
252b5132 RH |
1886 | (struct bfd_link_hash_entry **) sym_hash))) |
1887 | goto error_return; | |
1888 | last_real->next = first_csect; | |
1889 | ||
1890 | if (smtyp == XTY_CM) | |
1891 | { | |
1892 | if ((*sym_hash)->root.type != bfd_link_hash_common | |
1893 | || (*sym_hash)->root.u.c.p->section != csect) | |
1894 | { | |
1895 | /* We don't need the common csect we just created. */ | |
eea6121a | 1896 | csect->size = 0; |
252b5132 RH |
1897 | } |
1898 | else | |
1899 | { | |
1900 | (*sym_hash)->root.u.c.p->alignment_power | |
dc810e39 | 1901 | = csect->alignment_power; |
252b5132 RH |
1902 | } |
1903 | } | |
1904 | ||
dc810e39 | 1905 | if (info->hash->creator == abfd->xvec) |
252b5132 RH |
1906 | { |
1907 | int flag; | |
1908 | ||
1909 | if (smtyp == XTY_ER || smtyp == XTY_CM) | |
1910 | flag = XCOFF_REF_REGULAR; | |
1911 | else | |
1912 | flag = XCOFF_DEF_REGULAR; | |
1913 | (*sym_hash)->flags |= flag; | |
1914 | ||
1915 | if ((*sym_hash)->smclas == XMC_UA | |
1916 | || flag == XCOFF_DEF_REGULAR) | |
1917 | (*sym_hash)->smclas = aux.x_csect.x_smclas; | |
1918 | } | |
1919 | } | |
1920 | ||
1921 | *csect_cache = csect; | |
1922 | ||
1923 | esym += (sym.n_numaux + 1) * symesz; | |
1924 | sym_hash += sym.n_numaux + 1; | |
1925 | csect_cache += sym.n_numaux + 1; | |
1926 | } | |
1927 | ||
1928 | BFD_ASSERT (last_real == NULL || last_real->next == first_csect); | |
1929 | ||
1930 | /* Make sure that we have seen all the relocs. */ | |
1931 | for (o = abfd->sections; o != first_csect; o = o->next) | |
1932 | { | |
1933 | /* Reset the section size and the line number count, since the | |
1934 | data is now attached to the csects. Don't reset the size of | |
1935 | the .debug section, since we need to read it below in | |
1936 | bfd_xcoff_size_dynamic_sections. */ | |
1937 | if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0) | |
eea6121a | 1938 | o->size = 0; |
252b5132 RH |
1939 | o->lineno_count = 0; |
1940 | ||
1941 | if ((o->flags & SEC_RELOC) != 0) | |
1942 | { | |
1943 | bfd_size_type i; | |
1944 | struct internal_reloc *rel; | |
1945 | asection **rel_csect; | |
1946 | ||
1947 | rel = reloc_info[o->target_index].relocs; | |
1948 | rel_csect = reloc_info[o->target_index].csects; | |
beb1bf64 | 1949 | |
252b5132 RH |
1950 | for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++) |
1951 | { | |
beb1bf64 | 1952 | |
252b5132 RH |
1953 | if (*rel_csect == NULL) |
1954 | { | |
1955 | (*_bfd_error_handler) | |
d003868e AM |
1956 | (_("%B: reloc %s:%d not in csect"), |
1957 | abfd, o->name, i); | |
252b5132 RH |
1958 | bfd_set_error (bfd_error_bad_value); |
1959 | goto error_return; | |
1960 | } | |
1961 | ||
1962 | /* We identify all symbols which are called, so that we | |
1963 | can create glue code for calls to functions imported | |
1964 | from dynamic objects. */ | |
dc810e39 | 1965 | if (info->hash->creator == abfd->xvec |
252b5132 RH |
1966 | && *rel_csect != bfd_und_section_ptr |
1967 | && (rel->r_type == R_BR | |
1968 | || rel->r_type == R_RBR) | |
1969 | && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL) | |
1970 | { | |
1971 | struct xcoff_link_hash_entry *h; | |
1972 | ||
1973 | h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx]; | |
1974 | h->flags |= XCOFF_CALLED; | |
1975 | /* If the symbol name starts with a period, it is | |
b34976b6 AM |
1976 | the code of a function. If the symbol is |
1977 | currently undefined, then add an undefined symbol | |
1978 | for the function descriptor. This should do no | |
1979 | harm, because any regular object that defines the | |
1980 | function should also define the function | |
1981 | descriptor. It helps, because it means that we | |
1982 | will identify the function descriptor with a | |
1983 | dynamic object if a dynamic object defines it. */ | |
252b5132 RH |
1984 | if (h->root.root.string[0] == '.' |
1985 | && h->descriptor == NULL) | |
1986 | { | |
1987 | struct xcoff_link_hash_entry *hds; | |
14a793b2 | 1988 | struct bfd_link_hash_entry *bh; |
252b5132 RH |
1989 | |
1990 | hds = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
1991 | h->root.root.string + 1, | |
b34976b6 | 1992 | TRUE, FALSE, TRUE); |
252b5132 RH |
1993 | if (hds == NULL) |
1994 | goto error_return; | |
1995 | if (hds->root.type == bfd_link_hash_new) | |
1996 | { | |
14a793b2 | 1997 | bh = &hds->root; |
252b5132 RH |
1998 | if (! (_bfd_generic_link_add_one_symbol |
1999 | (info, abfd, hds->root.root.string, | |
2000 | (flagword) 0, bfd_und_section_ptr, | |
b34976b6 AM |
2001 | (bfd_vma) 0, (const char *) NULL, FALSE, |
2002 | TRUE, &bh))) | |
252b5132 | 2003 | goto error_return; |
14a793b2 | 2004 | hds = (struct xcoff_link_hash_entry *) bh; |
252b5132 RH |
2005 | } |
2006 | hds->flags |= XCOFF_DESCRIPTOR; | |
2007 | BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0 | |
2008 | && (h->flags & XCOFF_DESCRIPTOR) == 0); | |
2009 | hds->descriptor = h; | |
2010 | h->descriptor = hds; | |
2011 | } | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | free (reloc_info[o->target_index].csects); | |
2016 | reloc_info[o->target_index].csects = NULL; | |
2017 | ||
2018 | /* Reset SEC_RELOC and the reloc_count, since the reloc | |
2019 | information is now attached to the csects. */ | |
beb1bf64 | 2020 | o->flags &=~ SEC_RELOC; |
252b5132 RH |
2021 | o->reloc_count = 0; |
2022 | ||
2023 | /* If we are not keeping memory, free the reloc information. */ | |
2024 | if (! info->keep_memory | |
2025 | && coff_section_data (abfd, o) != NULL | |
2026 | && coff_section_data (abfd, o)->relocs != NULL | |
2027 | && ! coff_section_data (abfd, o)->keep_relocs) | |
2028 | { | |
2029 | free (coff_section_data (abfd, o)->relocs); | |
2030 | coff_section_data (abfd, o)->relocs = NULL; | |
2031 | } | |
2032 | } | |
2033 | ||
2034 | /* Free up the line numbers. FIXME: We could cache these | |
b34976b6 | 2035 | somewhere for the final link, to avoid reading them again. */ |
252b5132 RH |
2036 | if (reloc_info[o->target_index].linenos != NULL) |
2037 | { | |
2038 | free (reloc_info[o->target_index].linenos); | |
2039 | reloc_info[o->target_index].linenos = NULL; | |
2040 | } | |
2041 | } | |
2042 | ||
2043 | free (reloc_info); | |
2044 | ||
2045 | obj_coff_keep_syms (abfd) = keep_syms; | |
2046 | ||
b34976b6 | 2047 | return TRUE; |
252b5132 RH |
2048 | |
2049 | error_return: | |
2050 | if (reloc_info != NULL) | |
2051 | { | |
2052 | for (o = abfd->sections; o != NULL; o = o->next) | |
2053 | { | |
2054 | if (reloc_info[o->target_index].csects != NULL) | |
2055 | free (reloc_info[o->target_index].csects); | |
2056 | if (reloc_info[o->target_index].linenos != NULL) | |
2057 | free (reloc_info[o->target_index].linenos); | |
2058 | } | |
dc810e39 | 2059 | free (reloc_info); |
252b5132 RH |
2060 | } |
2061 | obj_coff_keep_syms (abfd) = keep_syms; | |
b34976b6 | 2062 | return FALSE; |
252b5132 RH |
2063 | } |
2064 | ||
2065 | #undef N_TMASK | |
2066 | #undef N_BTSHFT | |
2067 | ||
2068 | /* This function is used to add symbols from a dynamic object to the | |
2069 | global symbol table. */ | |
2070 | ||
b34976b6 | 2071 | static bfd_boolean |
252b5132 RH |
2072 | xcoff_link_add_dynamic_symbols (abfd, info) |
2073 | bfd *abfd; | |
2074 | struct bfd_link_info *info; | |
2075 | { | |
2076 | asection *lsec; | |
beb1bf64 | 2077 | bfd_byte *contents; |
252b5132 RH |
2078 | struct internal_ldhdr ldhdr; |
2079 | const char *strings; | |
beb1bf64 | 2080 | bfd_byte *elsym, *elsymend; |
252b5132 RH |
2081 | struct xcoff_import_file *n; |
2082 | const char *bname; | |
2083 | const char *mname; | |
2084 | const char *s; | |
2085 | unsigned int c; | |
2086 | struct xcoff_import_file **pp; | |
2087 | ||
2088 | /* We can only handle a dynamic object if we are generating an XCOFF | |
2089 | output file. */ | |
dc810e39 | 2090 | if (info->hash->creator != abfd->xvec) |
252b5132 RH |
2091 | { |
2092 | (*_bfd_error_handler) | |
2093 | (_("%s: XCOFF shared object when not producing XCOFF output"), | |
2094 | bfd_get_filename (abfd)); | |
dc810e39 | 2095 | bfd_set_error (bfd_error_invalid_operation); |
b34976b6 | 2096 | return FALSE; |
252b5132 | 2097 | } |
dc810e39 | 2098 | |
252b5132 RH |
2099 | /* The symbols we use from a dynamic object are not the symbols in |
2100 | the normal symbol table, but, rather, the symbols in the export | |
2101 | table. If there is a global symbol in a dynamic object which is | |
2102 | not in the export table, the loader will not be able to find it, | |
2103 | so we don't want to find it either. Also, on AIX 4.1.3, shr.o in | |
2104 | libc.a has symbols in the export table which are not in the | |
2105 | symbol table. */ | |
2106 | ||
2107 | /* Read in the .loader section. FIXME: We should really use the | |
2108 | o_snloader field in the a.out header, rather than grabbing the | |
2109 | section by name. */ | |
2110 | lsec = bfd_get_section_by_name (abfd, ".loader"); | |
2111 | if (lsec == NULL) | |
2112 | { | |
2113 | (*_bfd_error_handler) | |
2114 | (_("%s: dynamic object with no .loader section"), | |
2115 | bfd_get_filename (abfd)); | |
2116 | bfd_set_error (bfd_error_no_symbols); | |
b34976b6 | 2117 | return FALSE; |
252b5132 RH |
2118 | } |
2119 | ||
beb1bf64 | 2120 | |
252b5132 | 2121 | if (! xcoff_get_section_contents (abfd, lsec)) |
b34976b6 | 2122 | return FALSE; |
beb1bf64 | 2123 | contents = coff_section_data (abfd, lsec)->contents; |
252b5132 RH |
2124 | |
2125 | /* Remove the sections from this object, so that they do not get | |
2126 | included in the link. */ | |
e54fdaa5 | 2127 | bfd_section_list_clear (abfd); |
252b5132 | 2128 | |
beb1bf64 TR |
2129 | bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); |
2130 | ||
2131 | strings = (char *) contents + ldhdr.l_stoff; | |
2132 | ||
2133 | elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr); | |
252b5132 | 2134 | |
beb1bf64 | 2135 | elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd); |
252b5132 | 2136 | |
beb1bf64 | 2137 | for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd)) |
252b5132 RH |
2138 | { |
2139 | struct internal_ldsym ldsym; | |
2140 | char nambuf[SYMNMLEN + 1]; | |
2141 | const char *name; | |
2142 | struct xcoff_link_hash_entry *h; | |
2143 | ||
beb1bf64 | 2144 | bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym); |
252b5132 RH |
2145 | |
2146 | /* We are only interested in exported symbols. */ | |
2147 | if ((ldsym.l_smtype & L_EXPORT) == 0) | |
2148 | continue; | |
2149 | ||
2150 | if (ldsym._l._l_l._l_zeroes == 0) | |
2151 | name = strings + ldsym._l._l_l._l_offset; | |
2152 | else | |
2153 | { | |
2154 | memcpy (nambuf, ldsym._l._l_name, SYMNMLEN); | |
2155 | nambuf[SYMNMLEN] = '\0'; | |
2156 | name = nambuf; | |
2157 | } | |
2158 | ||
2159 | /* Normally we could not call xcoff_link_hash_lookup in an add | |
2160 | symbols routine, since we might not be using an XCOFF hash | |
2161 | table. However, we verified above that we are using an XCOFF | |
2162 | hash table. */ | |
2163 | ||
b34976b6 AM |
2164 | h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, |
2165 | TRUE, TRUE); | |
252b5132 | 2166 | if (h == NULL) |
b34976b6 | 2167 | return FALSE; |
252b5132 RH |
2168 | |
2169 | h->flags |= XCOFF_DEF_DYNAMIC; | |
2170 | ||
2171 | /* If the symbol is undefined, and the BFD it was found in is | |
2172 | not a dynamic object, change the BFD to this dynamic object, | |
2173 | so that we can get the correct import file ID. */ | |
2174 | if ((h->root.type == bfd_link_hash_undefined | |
2175 | || h->root.type == bfd_link_hash_undefweak) | |
2176 | && (h->root.u.undef.abfd == NULL | |
2177 | || (h->root.u.undef.abfd->flags & DYNAMIC) == 0)) | |
2178 | h->root.u.undef.abfd = abfd; | |
2179 | ||
2180 | if (h->root.type == bfd_link_hash_new) | |
2181 | { | |
2182 | h->root.type = bfd_link_hash_undefined; | |
2183 | h->root.u.undef.abfd = abfd; | |
2184 | /* We do not want to add this to the undefined symbol list. */ | |
2185 | } | |
2186 | ||
2187 | if (h->smclas == XMC_UA | |
2188 | || h->root.type == bfd_link_hash_undefined | |
2189 | || h->root.type == bfd_link_hash_undefweak) | |
2190 | h->smclas = ldsym.l_smclas; | |
2191 | ||
2192 | /* Unless this is an XMC_XO symbol, we don't bother to actually | |
b34976b6 AM |
2193 | define it, since we don't have a section to put it in anyhow. |
2194 | Instead, the relocation routines handle the DEF_DYNAMIC flag | |
2195 | correctly. */ | |
252b5132 RH |
2196 | |
2197 | if (h->smclas == XMC_XO | |
2198 | && (h->root.type == bfd_link_hash_undefined | |
2199 | || h->root.type == bfd_link_hash_undefweak)) | |
2200 | { | |
2201 | /* This symbol has an absolute value. */ | |
2202 | h->root.type = bfd_link_hash_defined; | |
2203 | h->root.u.def.section = bfd_abs_section_ptr; | |
2204 | h->root.u.def.value = ldsym.l_value; | |
2205 | } | |
2206 | ||
2207 | /* If this symbol defines a function descriptor, then it | |
2208 | implicitly defines the function code as well. */ | |
2209 | if (h->smclas == XMC_DS | |
2210 | || (h->smclas == XMC_XO && name[0] != '.')) | |
2211 | h->flags |= XCOFF_DESCRIPTOR; | |
2212 | if ((h->flags & XCOFF_DESCRIPTOR) != 0) | |
2213 | { | |
2214 | struct xcoff_link_hash_entry *hds; | |
2215 | ||
2216 | hds = h->descriptor; | |
2217 | if (hds == NULL) | |
2218 | { | |
2219 | char *dsnm; | |
2220 | ||
dc810e39 | 2221 | dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2); |
252b5132 | 2222 | if (dsnm == NULL) |
b34976b6 | 2223 | return FALSE; |
252b5132 RH |
2224 | dsnm[0] = '.'; |
2225 | strcpy (dsnm + 1, name); | |
2226 | hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm, | |
b34976b6 | 2227 | TRUE, TRUE, TRUE); |
252b5132 RH |
2228 | free (dsnm); |
2229 | if (hds == NULL) | |
b34976b6 | 2230 | return FALSE; |
252b5132 RH |
2231 | |
2232 | if (hds->root.type == bfd_link_hash_new) | |
2233 | { | |
2234 | hds->root.type = bfd_link_hash_undefined; | |
2235 | hds->root.u.undef.abfd = abfd; | |
2236 | /* We do not want to add this to the undefined | |
b34976b6 | 2237 | symbol list. */ |
252b5132 RH |
2238 | } |
2239 | ||
2240 | hds->descriptor = h; | |
2241 | h->descriptor = hds; | |
2242 | } | |
2243 | ||
2244 | hds->flags |= XCOFF_DEF_DYNAMIC; | |
2245 | if (hds->smclas == XMC_UA) | |
2246 | hds->smclas = XMC_PR; | |
2247 | ||
2248 | /* An absolute symbol appears to actually define code, not a | |
2249 | function descriptor. This is how some math functions are | |
2250 | implemented on AIX 4.1. */ | |
2251 | if (h->smclas == XMC_XO | |
2252 | && (hds->root.type == bfd_link_hash_undefined | |
2253 | || hds->root.type == bfd_link_hash_undefweak)) | |
2254 | { | |
2255 | hds->smclas = XMC_XO; | |
2256 | hds->root.type = bfd_link_hash_defined; | |
2257 | hds->root.u.def.section = bfd_abs_section_ptr; | |
2258 | hds->root.u.def.value = ldsym.l_value; | |
2259 | } | |
2260 | } | |
2261 | } | |
2262 | ||
beb1bf64 | 2263 | if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents) |
252b5132 RH |
2264 | { |
2265 | free (coff_section_data (abfd, lsec)->contents); | |
2266 | coff_section_data (abfd, lsec)->contents = NULL; | |
2267 | } | |
2268 | ||
2269 | /* Record this file in the import files. */ | |
2270 | ||
2271 | n = ((struct xcoff_import_file *) | |
dc810e39 | 2272 | bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file))); |
252b5132 | 2273 | if (n == NULL) |
b34976b6 | 2274 | return FALSE; |
252b5132 RH |
2275 | n->next = NULL; |
2276 | ||
2277 | /* For some reason, the path entry in the import file list for a | |
2278 | shared object appears to always be empty. The file name is the | |
2279 | base name. */ | |
2280 | n->path = ""; | |
2281 | if (abfd->my_archive == NULL) | |
2282 | { | |
2283 | bname = bfd_get_filename (abfd); | |
2284 | mname = ""; | |
2285 | } | |
2286 | else | |
2287 | { | |
2288 | bname = bfd_get_filename (abfd->my_archive); | |
2289 | mname = bfd_get_filename (abfd); | |
2290 | } | |
2291 | s = strrchr (bname, '/'); | |
2292 | if (s != NULL) | |
2293 | bname = s + 1; | |
2294 | n->file = bname; | |
2295 | n->member = mname; | |
2296 | ||
2297 | /* We start c at 1 because the first import file number is reserved | |
2298 | for LIBPATH. */ | |
2299 | for (pp = &xcoff_hash_table (info)->imports, c = 1; | |
2300 | *pp != NULL; | |
2301 | pp = &(*pp)->next, ++c) | |
2302 | ; | |
2303 | *pp = n; | |
2304 | ||
2305 | xcoff_data (abfd)->import_file_id = c; | |
2306 | ||
b34976b6 | 2307 | return TRUE; |
252b5132 RH |
2308 | } |
2309 | \f | |
2310 | /* Routines that are called after all the input files have been | |
2311 | handled, but before the sections are laid out in memory. */ | |
2312 | ||
2313 | /* Mark a symbol as not being garbage, including the section in which | |
2314 | it is defined. */ | |
2315 | ||
b34976b6 | 2316 | static INLINE bfd_boolean |
252b5132 RH |
2317 | xcoff_mark_symbol (info, h) |
2318 | struct bfd_link_info *info; | |
2319 | struct xcoff_link_hash_entry *h; | |
2320 | { | |
beb1bf64 | 2321 | |
252b5132 | 2322 | if ((h->flags & XCOFF_MARK) != 0) |
b34976b6 | 2323 | return TRUE; |
252b5132 RH |
2324 | |
2325 | h->flags |= XCOFF_MARK; | |
2326 | if (h->root.type == bfd_link_hash_defined | |
2327 | || h->root.type == bfd_link_hash_defweak) | |
2328 | { | |
2329 | asection *hsec; | |
2330 | ||
2331 | hsec = h->root.u.def.section; | |
2332 | if (! bfd_is_abs_section (hsec) | |
2333 | && (hsec->flags & SEC_MARK) == 0) | |
2334 | { | |
2335 | if (! xcoff_mark (info, hsec)) | |
b34976b6 | 2336 | return FALSE; |
252b5132 RH |
2337 | } |
2338 | } | |
2339 | ||
2340 | if (h->toc_section != NULL | |
2341 | && (h->toc_section->flags & SEC_MARK) == 0) | |
2342 | { | |
2343 | if (! xcoff_mark (info, h->toc_section)) | |
b34976b6 | 2344 | return FALSE; |
252b5132 RH |
2345 | } |
2346 | ||
b34976b6 | 2347 | return TRUE; |
252b5132 RH |
2348 | } |
2349 | ||
2350 | /* The mark phase of garbage collection. For a given section, mark | |
2351 | it, and all the sections which define symbols to which it refers. | |
2352 | Because this function needs to look at the relocs, we also count | |
2353 | the number of relocs which need to be copied into the .loader | |
2354 | section. */ | |
2355 | ||
b34976b6 | 2356 | static bfd_boolean |
252b5132 RH |
2357 | xcoff_mark (info, sec) |
2358 | struct bfd_link_info *info; | |
2359 | asection *sec; | |
2360 | { | |
2361 | if (bfd_is_abs_section (sec) | |
2362 | || (sec->flags & SEC_MARK) != 0) | |
b34976b6 | 2363 | return TRUE; |
252b5132 RH |
2364 | |
2365 | sec->flags |= SEC_MARK; | |
2366 | ||
2367 | if (sec->owner->xvec == info->hash->creator | |
2368 | && coff_section_data (sec->owner, sec) != NULL | |
2369 | && xcoff_section_data (sec->owner, sec) != NULL) | |
2370 | { | |
2371 | register struct xcoff_link_hash_entry **hp, **hpend; | |
2372 | struct internal_reloc *rel, *relend; | |
2373 | ||
2374 | /* Mark all the symbols in this section. */ | |
2375 | ||
2376 | hp = (obj_xcoff_sym_hashes (sec->owner) | |
2377 | + xcoff_section_data (sec->owner, sec)->first_symndx); | |
2378 | hpend = (obj_xcoff_sym_hashes (sec->owner) | |
2379 | + xcoff_section_data (sec->owner, sec)->last_symndx); | |
2380 | for (; hp < hpend; hp++) | |
2381 | { | |
2382 | register struct xcoff_link_hash_entry *h; | |
2383 | ||
2384 | h = *hp; | |
2385 | if (h != NULL | |
2386 | && (h->flags & XCOFF_MARK) == 0) | |
2387 | { | |
2388 | if (! xcoff_mark_symbol (info, h)) | |
b34976b6 | 2389 | return FALSE; |
252b5132 RH |
2390 | } |
2391 | } | |
2392 | ||
2393 | /* Look through the section relocs. */ | |
2394 | ||
2395 | if ((sec->flags & SEC_RELOC) != 0 | |
2396 | && sec->reloc_count > 0) | |
2397 | { | |
b34976b6 AM |
2398 | rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE, |
2399 | (bfd_byte *) NULL, FALSE, | |
252b5132 RH |
2400 | (struct internal_reloc *) NULL); |
2401 | if (rel == NULL) | |
b34976b6 | 2402 | return FALSE; |
252b5132 RH |
2403 | relend = rel + sec->reloc_count; |
2404 | for (; rel < relend; rel++) | |
2405 | { | |
2406 | asection *rsec; | |
2407 | struct xcoff_link_hash_entry *h; | |
2408 | ||
2409 | if ((unsigned int) rel->r_symndx | |
2410 | > obj_raw_syment_count (sec->owner)) | |
2411 | continue; | |
2412 | ||
2413 | h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx]; | |
2414 | if (h != NULL | |
2415 | && (h->flags & XCOFF_MARK) == 0) | |
2416 | { | |
2417 | if (! xcoff_mark_symbol (info, h)) | |
b34976b6 | 2418 | return FALSE; |
252b5132 RH |
2419 | } |
2420 | ||
2421 | rsec = xcoff_data (sec->owner)->csects[rel->r_symndx]; | |
2422 | if (rsec != NULL | |
2423 | && (rsec->flags & SEC_MARK) == 0) | |
2424 | { | |
2425 | if (! xcoff_mark (info, rsec)) | |
b34976b6 | 2426 | return FALSE; |
252b5132 RH |
2427 | } |
2428 | ||
2429 | /* See if this reloc needs to be copied into the .loader | |
b34976b6 | 2430 | section. */ |
252b5132 RH |
2431 | switch (rel->r_type) |
2432 | { | |
2433 | default: | |
2434 | if (h == NULL | |
2435 | || h->root.type == bfd_link_hash_defined | |
2436 | || h->root.type == bfd_link_hash_defweak | |
2437 | || h->root.type == bfd_link_hash_common | |
2438 | || ((h->flags & XCOFF_CALLED) != 0 | |
2439 | && (h->root.type == bfd_link_hash_undefined | |
2440 | || h->root.type == bfd_link_hash_undefweak) | |
2441 | && h->root.root.string[0] == '.' | |
2442 | && h->descriptor != NULL | |
2443 | && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0 | |
2444 | || ((h->descriptor->flags & XCOFF_IMPORT) != 0 | |
2445 | && (h->descriptor->flags | |
2446 | & XCOFF_DEF_REGULAR) == 0)))) | |
2447 | break; | |
2448 | /* Fall through. */ | |
2449 | case R_POS: | |
2450 | case R_NEG: | |
2451 | case R_RL: | |
2452 | case R_RLA: | |
2453 | ++xcoff_hash_table (info)->ldrel_count; | |
2454 | if (h != NULL) | |
2455 | h->flags |= XCOFF_LDREL; | |
2456 | break; | |
2457 | case R_TOC: | |
2458 | case R_GL: | |
2459 | case R_TCL: | |
2460 | case R_TRL: | |
2461 | case R_TRLA: | |
2462 | /* We should never need a .loader reloc for a TOC | |
2463 | relative reloc. */ | |
2464 | break; | |
2465 | } | |
2466 | } | |
2467 | ||
2468 | if (! info->keep_memory | |
2469 | && coff_section_data (sec->owner, sec) != NULL | |
2470 | && coff_section_data (sec->owner, sec)->relocs != NULL | |
2471 | && ! coff_section_data (sec->owner, sec)->keep_relocs) | |
2472 | { | |
2473 | free (coff_section_data (sec->owner, sec)->relocs); | |
2474 | coff_section_data (sec->owner, sec)->relocs = NULL; | |
2475 | } | |
2476 | } | |
2477 | } | |
2478 | ||
b34976b6 | 2479 | return TRUE; |
252b5132 RH |
2480 | } |
2481 | ||
2482 | /* The sweep phase of garbage collection. Remove all garbage | |
2483 | sections. */ | |
2484 | ||
2485 | static void | |
2486 | xcoff_sweep (info) | |
2487 | struct bfd_link_info *info; | |
2488 | { | |
2489 | bfd *sub; | |
2490 | ||
2491 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
2492 | { | |
2493 | asection *o; | |
2494 | ||
2495 | for (o = sub->sections; o != NULL; o = o->next) | |
2496 | { | |
2497 | if ((o->flags & SEC_MARK) == 0) | |
2498 | { | |
2499 | /* Keep all sections from non-XCOFF input files. Keep | |
b34976b6 AM |
2500 | special sections. Keep .debug sections for the |
2501 | moment. */ | |
252b5132 RH |
2502 | if (sub->xvec != info->hash->creator |
2503 | || o == xcoff_hash_table (info)->debug_section | |
2504 | || o == xcoff_hash_table (info)->loader_section | |
2505 | || o == xcoff_hash_table (info)->linkage_section | |
2506 | || o == xcoff_hash_table (info)->toc_section | |
2507 | || o == xcoff_hash_table (info)->descriptor_section | |
2508 | || strcmp (o->name, ".debug") == 0) | |
2509 | o->flags |= SEC_MARK; | |
2510 | else | |
2511 | { | |
eea6121a | 2512 | o->size = 0; |
252b5132 RH |
2513 | o->reloc_count = 0; |
2514 | o->lineno_count = 0; | |
2515 | } | |
2516 | } | |
2517 | } | |
2518 | } | |
2519 | } | |
2520 | ||
2521 | /* Record the number of elements in a set. This is used to output the | |
2522 | correct csect length. */ | |
2523 | ||
b34976b6 | 2524 | bfd_boolean |
252b5132 RH |
2525 | bfd_xcoff_link_record_set (output_bfd, info, harg, size) |
2526 | bfd *output_bfd; | |
2527 | struct bfd_link_info *info; | |
2528 | struct bfd_link_hash_entry *harg; | |
2529 | bfd_size_type size; | |
2530 | { | |
2531 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
2532 | struct xcoff_link_size_list *n; | |
dc810e39 | 2533 | bfd_size_type amt; |
252b5132 | 2534 | |
9bd09e22 | 2535 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 2536 | return TRUE; |
252b5132 RH |
2537 | |
2538 | /* This will hardly ever be called. I don't want to burn four bytes | |
2539 | per global symbol, so instead the size is kept on a linked list | |
2540 | attached to the hash table. */ | |
2541 | ||
dc810e39 AM |
2542 | amt = sizeof (struct xcoff_link_size_list); |
2543 | n = (struct xcoff_link_size_list *) bfd_alloc (output_bfd, amt); | |
252b5132 | 2544 | if (n == NULL) |
b34976b6 | 2545 | return FALSE; |
252b5132 RH |
2546 | n->next = xcoff_hash_table (info)->size_list; |
2547 | n->h = h; | |
2548 | n->size = size; | |
2549 | xcoff_hash_table (info)->size_list = n; | |
2550 | ||
2551 | h->flags |= XCOFF_HAS_SIZE; | |
2552 | ||
b34976b6 | 2553 | return TRUE; |
252b5132 RH |
2554 | } |
2555 | ||
2556 | /* Import a symbol. */ | |
2557 | ||
b34976b6 | 2558 | bfd_boolean |
252b5132 | 2559 | bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile, |
1fdf0249 | 2560 | impmember, syscall_flag) |
252b5132 RH |
2561 | bfd *output_bfd; |
2562 | struct bfd_link_info *info; | |
2563 | struct bfd_link_hash_entry *harg; | |
2564 | bfd_vma val; | |
2565 | const char *imppath; | |
2566 | const char *impfile; | |
2567 | const char *impmember; | |
1fdf0249 | 2568 | unsigned int syscall_flag; |
252b5132 RH |
2569 | { |
2570 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
2571 | ||
9bd09e22 | 2572 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 2573 | return TRUE; |
252b5132 RH |
2574 | |
2575 | /* A symbol name which starts with a period is the code for a | |
2576 | function. If the symbol is undefined, then add an undefined | |
2577 | symbol for the function descriptor, and import that instead. */ | |
2578 | if (h->root.root.string[0] == '.' | |
2579 | && h->root.type == bfd_link_hash_undefined | |
2580 | && val == (bfd_vma) -1) | |
2581 | { | |
2582 | struct xcoff_link_hash_entry *hds; | |
2583 | ||
2584 | hds = h->descriptor; | |
2585 | if (hds == NULL) | |
2586 | { | |
2587 | hds = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
2588 | h->root.root.string + 1, | |
b34976b6 | 2589 | TRUE, FALSE, TRUE); |
252b5132 | 2590 | if (hds == NULL) |
b34976b6 | 2591 | return FALSE; |
252b5132 RH |
2592 | if (hds->root.type == bfd_link_hash_new) |
2593 | { | |
2594 | hds->root.type = bfd_link_hash_undefined; | |
2595 | hds->root.u.undef.abfd = h->root.u.undef.abfd; | |
2596 | } | |
2597 | hds->flags |= XCOFF_DESCRIPTOR; | |
2598 | BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0 | |
2599 | && (h->flags & XCOFF_DESCRIPTOR) == 0); | |
2600 | hds->descriptor = h; | |
2601 | h->descriptor = hds; | |
2602 | } | |
2603 | ||
2604 | /* Now, if the descriptor is undefined, import the descriptor | |
b34976b6 AM |
2605 | rather than the symbol we were told to import. FIXME: Is |
2606 | this correct in all cases? */ | |
252b5132 RH |
2607 | if (hds->root.type == bfd_link_hash_undefined) |
2608 | h = hds; | |
2609 | } | |
2610 | ||
1fdf0249 | 2611 | h->flags |= (XCOFF_IMPORT | syscall_flag); |
252b5132 RH |
2612 | |
2613 | if (val != (bfd_vma) -1) | |
2614 | { | |
2615 | if (h->root.type == bfd_link_hash_defined | |
2616 | && (! bfd_is_abs_section (h->root.u.def.section) | |
2617 | || h->root.u.def.value != val)) | |
2618 | { | |
2619 | if (! ((*info->callbacks->multiple_definition) | |
2620 | (info, h->root.root.string, h->root.u.def.section->owner, | |
2621 | h->root.u.def.section, h->root.u.def.value, | |
2622 | output_bfd, bfd_abs_section_ptr, val))) | |
b34976b6 | 2623 | return FALSE; |
252b5132 RH |
2624 | } |
2625 | ||
2626 | h->root.type = bfd_link_hash_defined; | |
2627 | h->root.u.def.section = bfd_abs_section_ptr; | |
2628 | h->root.u.def.value = val; | |
2629 | } | |
2630 | ||
2631 | /* We overload the ldindx field to hold the l_ifile value for this | |
2632 | symbol. */ | |
2633 | BFD_ASSERT (h->ldsym == NULL); | |
2634 | BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0); | |
2635 | if (imppath == NULL) | |
2636 | h->ldindx = -1; | |
2637 | else | |
2638 | { | |
2639 | unsigned int c; | |
2640 | struct xcoff_import_file **pp; | |
2641 | ||
2642 | /* We start c at 1 because the first entry in the import list is | |
b34976b6 | 2643 | reserved for the library search path. */ |
252b5132 RH |
2644 | for (pp = &xcoff_hash_table (info)->imports, c = 1; |
2645 | *pp != NULL; | |
2646 | pp = &(*pp)->next, ++c) | |
2647 | { | |
2648 | if (strcmp ((*pp)->path, imppath) == 0 | |
2649 | && strcmp ((*pp)->file, impfile) == 0 | |
2650 | && strcmp ((*pp)->member, impmember) == 0) | |
2651 | break; | |
2652 | } | |
2653 | ||
2654 | if (*pp == NULL) | |
2655 | { | |
2656 | struct xcoff_import_file *n; | |
dc810e39 | 2657 | bfd_size_type amt = sizeof (struct xcoff_import_file); |
252b5132 | 2658 | |
dc810e39 | 2659 | n = (struct xcoff_import_file *) bfd_alloc (output_bfd, amt); |
252b5132 | 2660 | if (n == NULL) |
b34976b6 | 2661 | return FALSE; |
252b5132 RH |
2662 | n->next = NULL; |
2663 | n->path = imppath; | |
2664 | n->file = impfile; | |
2665 | n->member = impmember; | |
2666 | *pp = n; | |
2667 | } | |
2668 | ||
2669 | h->ldindx = c; | |
2670 | } | |
2671 | ||
b34976b6 | 2672 | return TRUE; |
252b5132 RH |
2673 | } |
2674 | ||
2675 | /* Export a symbol. */ | |
2676 | ||
b34976b6 | 2677 | bfd_boolean |
1fdf0249 | 2678 | bfd_xcoff_export_symbol (output_bfd, info, harg) |
252b5132 RH |
2679 | bfd *output_bfd; |
2680 | struct bfd_link_info *info; | |
2681 | struct bfd_link_hash_entry *harg; | |
252b5132 RH |
2682 | { |
2683 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
2684 | ||
9bd09e22 | 2685 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 2686 | return TRUE; |
252b5132 RH |
2687 | |
2688 | h->flags |= XCOFF_EXPORT; | |
2689 | ||
2690 | /* FIXME: I'm not at all sure what syscall is supposed to mean, so | |
2691 | I'm just going to ignore it until somebody explains it. */ | |
2692 | ||
2693 | /* See if this is a function descriptor. It may be one even though | |
2694 | it is not so marked. */ | |
2695 | if ((h->flags & XCOFF_DESCRIPTOR) == 0 | |
2696 | && h->root.root.string[0] != '.') | |
2697 | { | |
2698 | char *fnname; | |
2699 | struct xcoff_link_hash_entry *hfn; | |
dc810e39 | 2700 | bfd_size_type amt = strlen (h->root.root.string) + 2; |
252b5132 | 2701 | |
dc810e39 | 2702 | fnname = (char *) bfd_malloc (amt); |
252b5132 | 2703 | if (fnname == NULL) |
b34976b6 | 2704 | return FALSE; |
252b5132 RH |
2705 | fnname[0] = '.'; |
2706 | strcpy (fnname + 1, h->root.root.string); | |
2707 | hfn = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
b34976b6 | 2708 | fnname, FALSE, FALSE, TRUE); |
252b5132 RH |
2709 | free (fnname); |
2710 | if (hfn != NULL | |
2711 | && hfn->smclas == XMC_PR | |
2712 | && (hfn->root.type == bfd_link_hash_defined | |
2713 | || hfn->root.type == bfd_link_hash_defweak)) | |
2714 | { | |
2715 | h->flags |= XCOFF_DESCRIPTOR; | |
2716 | h->descriptor = hfn; | |
2717 | hfn->descriptor = h; | |
2718 | } | |
2719 | } | |
2720 | ||
2721 | /* Make sure we don't garbage collect this symbol. */ | |
2722 | if (! xcoff_mark_symbol (info, h)) | |
b34976b6 | 2723 | return FALSE; |
252b5132 RH |
2724 | |
2725 | /* If this is a function descriptor, make sure we don't garbage | |
2726 | collect the associated function code. We normally don't have to | |
2727 | worry about this, because the descriptor will be attached to a | |
2728 | section with relocs, but if we are creating the descriptor | |
2729 | ourselves those relocs will not be visible to the mark code. */ | |
2730 | if ((h->flags & XCOFF_DESCRIPTOR) != 0) | |
2731 | { | |
2732 | if (! xcoff_mark_symbol (info, h->descriptor)) | |
b34976b6 | 2733 | return FALSE; |
252b5132 RH |
2734 | } |
2735 | ||
b34976b6 | 2736 | return TRUE; |
252b5132 RH |
2737 | } |
2738 | ||
2739 | /* Count a reloc against a symbol. This is called for relocs | |
2740 | generated by the linker script, typically for global constructors | |
2741 | and destructors. */ | |
2742 | ||
b34976b6 | 2743 | bfd_boolean |
252b5132 RH |
2744 | bfd_xcoff_link_count_reloc (output_bfd, info, name) |
2745 | bfd *output_bfd; | |
2746 | struct bfd_link_info *info; | |
2747 | const char *name; | |
2748 | { | |
2749 | struct xcoff_link_hash_entry *h; | |
2750 | ||
9bd09e22 | 2751 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 2752 | return TRUE; |
252b5132 RH |
2753 | |
2754 | h = ((struct xcoff_link_hash_entry *) | |
b34976b6 AM |
2755 | bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE, |
2756 | FALSE)); | |
252b5132 RH |
2757 | if (h == NULL) |
2758 | { | |
2759 | (*_bfd_error_handler) (_("%s: no such symbol"), name); | |
2760 | bfd_set_error (bfd_error_no_symbols); | |
b34976b6 | 2761 | return FALSE; |
252b5132 RH |
2762 | } |
2763 | ||
2764 | h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL; | |
2765 | ++xcoff_hash_table (info)->ldrel_count; | |
fbc4fff4 | 2766 | |
252b5132 RH |
2767 | /* Mark the symbol to avoid garbage collection. */ |
2768 | if (! xcoff_mark_symbol (info, h)) | |
b34976b6 | 2769 | return FALSE; |
252b5132 | 2770 | |
b34976b6 | 2771 | return TRUE; |
252b5132 RH |
2772 | } |
2773 | ||
2774 | /* This function is called for each symbol to which the linker script | |
2775 | assigns a value. */ | |
2776 | ||
b34976b6 | 2777 | bfd_boolean |
252b5132 RH |
2778 | bfd_xcoff_record_link_assignment (output_bfd, info, name) |
2779 | bfd *output_bfd; | |
2780 | struct bfd_link_info *info; | |
2781 | const char *name; | |
2782 | { | |
2783 | struct xcoff_link_hash_entry *h; | |
2784 | ||
9bd09e22 | 2785 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 2786 | return TRUE; |
252b5132 | 2787 | |
b34976b6 AM |
2788 | h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE, |
2789 | FALSE); | |
252b5132 | 2790 | if (h == NULL) |
b34976b6 | 2791 | return FALSE; |
252b5132 RH |
2792 | |
2793 | h->flags |= XCOFF_DEF_REGULAR; | |
2794 | ||
b34976b6 | 2795 | return TRUE; |
252b5132 RH |
2796 | } |
2797 | ||
252b5132 RH |
2798 | /* Build the .loader section. This is called by the XCOFF linker |
2799 | emulation before_allocation routine. We must set the size of the | |
2800 | .loader section before the linker lays out the output file. | |
2801 | LIBPATH is the library path to search for shared objects; this is | |
2802 | normally built from the -L arguments passed to the linker. ENTRY | |
2803 | is the name of the entry point symbol (the -e linker option). | |
2804 | FILE_ALIGN is the alignment to use for sections within the file | |
2805 | (the -H linker option). MAXSTACK is the maximum stack size (the | |
2806 | -bmaxstack linker option). MAXDATA is the maximum data size (the | |
2807 | -bmaxdata linker option). GC is whether to do garbage collection | |
2808 | (the -bgc linker option). MODTYPE is the module type (the | |
2809 | -bmodtype linker option). TEXTRO is whether the text section must | |
2810 | be read only (the -btextro linker option). EXPORT_DEFINEDS is | |
2811 | whether all defined symbols should be exported (the -unix linker | |
2812 | option). SPECIAL_SECTIONS is set by this routine to csects with | |
2813 | magic names like _end. */ | |
2814 | ||
b34976b6 | 2815 | bfd_boolean |
252b5132 RH |
2816 | bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry, |
2817 | file_align, maxstack, maxdata, gc, | |
2818 | modtype, textro, export_defineds, | |
69f284c7 | 2819 | special_sections, rtld) |
252b5132 RH |
2820 | bfd *output_bfd; |
2821 | struct bfd_link_info *info; | |
2822 | const char *libpath; | |
2823 | const char *entry; | |
2824 | unsigned long file_align; | |
2825 | unsigned long maxstack; | |
2826 | unsigned long maxdata; | |
b34976b6 | 2827 | bfd_boolean gc; |
252b5132 | 2828 | int modtype; |
b34976b6 AM |
2829 | bfd_boolean textro; |
2830 | bfd_boolean export_defineds; | |
252b5132 | 2831 | asection **special_sections; |
b34976b6 | 2832 | bfd_boolean rtld; |
252b5132 RH |
2833 | { |
2834 | struct xcoff_link_hash_entry *hentry; | |
2835 | asection *lsec; | |
2836 | struct xcoff_loader_info ldinfo; | |
2837 | int i; | |
2838 | size_t impsize, impcount; | |
2839 | struct xcoff_import_file *fl; | |
2840 | struct internal_ldhdr *ldhdr; | |
2841 | bfd_size_type stoff; | |
2842 | register char *out; | |
2843 | asection *sec; | |
2844 | bfd *sub; | |
2845 | struct bfd_strtab_hash *debug_strtab; | |
2846 | bfd_byte *debug_contents = NULL; | |
dc810e39 | 2847 | bfd_size_type amt; |
252b5132 | 2848 | |
dc810e39 AM |
2849 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
2850 | { | |
dc810e39 AM |
2851 | for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++) |
2852 | special_sections[i] = NULL; | |
b34976b6 | 2853 | return TRUE; |
dc810e39 | 2854 | } |
252b5132 | 2855 | |
b34976b6 | 2856 | ldinfo.failed = FALSE; |
252b5132 RH |
2857 | ldinfo.output_bfd = output_bfd; |
2858 | ldinfo.info = info; | |
2859 | ldinfo.export_defineds = export_defineds; | |
2860 | ldinfo.ldsym_count = 0; | |
2861 | ldinfo.string_size = 0; | |
2862 | ldinfo.strings = NULL; | |
2863 | ldinfo.string_alc = 0; | |
2864 | ||
2865 | xcoff_data (output_bfd)->maxstack = maxstack; | |
2866 | xcoff_data (output_bfd)->maxdata = maxdata; | |
2867 | xcoff_data (output_bfd)->modtype = modtype; | |
2868 | ||
2869 | xcoff_hash_table (info)->file_align = file_align; | |
2870 | xcoff_hash_table (info)->textro = textro; | |
2871 | ||
69f284c7 TR |
2872 | hentry = NULL; |
2873 | if (entry != NULL) | |
dc810e39 AM |
2874 | { |
2875 | hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry, | |
b34976b6 | 2876 | FALSE, FALSE, TRUE); |
dc810e39 AM |
2877 | if (hentry != NULL) |
2878 | hentry->flags |= XCOFF_ENTRY; | |
2879 | } | |
beb1bf64 TR |
2880 | |
2881 | /* __rtinit */ | |
b34976b6 | 2882 | if (info->init_function || info->fini_function || rtld) |
69f284c7 TR |
2883 | { |
2884 | struct xcoff_link_hash_entry *hsym; | |
2885 | struct internal_ldsym *ldsym; | |
b34976b6 | 2886 | |
69f284c7 | 2887 | hsym = xcoff_link_hash_lookup (xcoff_hash_table (info), |
b34976b6 | 2888 | "__rtinit", FALSE, FALSE, TRUE); |
69f284c7 TR |
2889 | if (hsym == NULL) |
2890 | { | |
2891 | (*_bfd_error_handler) | |
2892 | (_("error: undefined symbol __rtinit")); | |
b34976b6 | 2893 | return FALSE; |
69f284c7 | 2894 | } |
b34976b6 | 2895 | |
69f284c7 TR |
2896 | xcoff_mark_symbol (info, hsym); |
2897 | hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT); | |
2898 | ||
7dee875e | 2899 | /* __rtinit initialized */ |
69f284c7 TR |
2900 | amt = sizeof (struct internal_ldsym); |
2901 | ldsym = (struct internal_ldsym *) bfd_malloc (amt); | |
b34976b6 AM |
2902 | |
2903 | ldsym->l_value = 0; /* will be filled in later */ | |
2904 | ldsym->l_scnum = 2; /* data section */ | |
2905 | ldsym->l_smtype = XTY_SD; /* csect section definition */ | |
2906 | ldsym->l_smclas = 5; /* .rw */ | |
2907 | ldsym->l_ifile = 0; /* special system loader symbol */ | |
2908 | ldsym->l_parm = 0; /* NA */ | |
2909 | ||
69f284c7 TR |
2910 | /* Force __rtinit to be the first symbol in the loader symbol table |
2911 | See xcoff_build_ldsyms | |
b34976b6 | 2912 | |
69f284c7 TR |
2913 | The first 3 symbol table indices are reserved to indicate the data, |
2914 | text and bss sections. */ | |
2915 | BFD_ASSERT (0 == ldinfo.ldsym_count); | |
b34976b6 | 2916 | |
69f284c7 TR |
2917 | hsym->ldindx = 3; |
2918 | ldinfo.ldsym_count = 1; | |
2919 | hsym->ldsym = ldsym; | |
b34976b6 AM |
2920 | |
2921 | if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo, | |
2922 | hsym->ldsym, hsym->root.root.string)) | |
2923 | return FALSE; | |
2924 | ||
69f284c7 TR |
2925 | /* This symbol is written out by xcoff_write_global_symbol |
2926 | Set stuff up so xcoff_write_global_symbol logic works. */ | |
2927 | hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK; | |
2928 | hsym->root.type = bfd_link_hash_defined; | |
2929 | hsym->root.u.def.value = 0; | |
2930 | } | |
beb1bf64 | 2931 | |
252b5132 | 2932 | /* Garbage collect unused sections. */ |
1049f94e | 2933 | if (info->relocatable |
252b5132 RH |
2934 | || ! gc |
2935 | || hentry == NULL | |
2936 | || (hentry->root.type != bfd_link_hash_defined | |
2937 | && hentry->root.type != bfd_link_hash_defweak)) | |
2938 | { | |
b34976b6 AM |
2939 | gc = FALSE; |
2940 | xcoff_hash_table (info)->gc = FALSE; | |
252b5132 RH |
2941 | |
2942 | /* We still need to call xcoff_mark, in order to set ldrel_count | |
b34976b6 | 2943 | correctly. */ |
252b5132 RH |
2944 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
2945 | { | |
2946 | asection *o; | |
2947 | ||
2948 | for (o = sub->sections; o != NULL; o = o->next) | |
2949 | { | |
2950 | if ((o->flags & SEC_MARK) == 0) | |
2951 | { | |
2952 | if (! xcoff_mark (info, o)) | |
2953 | goto error_return; | |
2954 | } | |
2955 | } | |
2956 | } | |
2957 | } | |
2958 | else | |
2959 | { | |
2960 | if (! xcoff_mark (info, hentry->root.u.def.section)) | |
2961 | goto error_return; | |
2962 | xcoff_sweep (info); | |
b34976b6 | 2963 | xcoff_hash_table (info)->gc = TRUE; |
252b5132 RH |
2964 | } |
2965 | ||
2966 | /* Return special sections to the caller. */ | |
dc810e39 AM |
2967 | for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++) |
2968 | { | |
2969 | sec = xcoff_hash_table (info)->special_sections[i]; | |
2970 | ||
2971 | if (sec != NULL | |
2972 | && gc | |
2973 | && (sec->flags & SEC_MARK) == 0) | |
2974 | { | |
2975 | sec = NULL; | |
2976 | } | |
2977 | special_sections[i] = sec; | |
252b5132 RH |
2978 | } |
2979 | ||
dc810e39 AM |
2980 | if (info->input_bfds == NULL) |
2981 | { | |
2982 | /* I'm not sure what to do in this bizarre case. */ | |
b34976b6 | 2983 | return TRUE; |
dc810e39 | 2984 | } |
252b5132 RH |
2985 | |
2986 | xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms, | |
2987 | (PTR) &ldinfo); | |
2988 | if (ldinfo.failed) | |
2989 | goto error_return; | |
2990 | ||
2991 | /* Work out the size of the import file names. Each import file ID | |
2992 | consists of three null terminated strings: the path, the file | |
2993 | name, and the archive member name. The first entry in the list | |
2994 | of names is the path to use to find objects, which the linker has | |
2995 | passed in as the libpath argument. For some reason, the path | |
2996 | entry in the other import file names appears to always be empty. */ | |
2997 | impsize = strlen (libpath) + 3; | |
2998 | impcount = 1; | |
2999 | for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next) | |
3000 | { | |
3001 | ++impcount; | |
3002 | impsize += (strlen (fl->path) | |
3003 | + strlen (fl->file) | |
3004 | + strlen (fl->member) | |
3005 | + 3); | |
3006 | } | |
3007 | ||
3008 | /* Set up the .loader section header. */ | |
3009 | ldhdr = &xcoff_hash_table (info)->ldhdr; | |
beb1bf64 | 3010 | ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd); |
252b5132 RH |
3011 | ldhdr->l_nsyms = ldinfo.ldsym_count; |
3012 | ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count; | |
3013 | ldhdr->l_istlen = impsize; | |
3014 | ldhdr->l_nimpid = impcount; | |
beb1bf64 TR |
3015 | ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd) |
3016 | + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd) | |
3017 | + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd)); | |
252b5132 RH |
3018 | ldhdr->l_stlen = ldinfo.string_size; |
3019 | stoff = ldhdr->l_impoff + impsize; | |
3020 | if (ldinfo.string_size == 0) | |
3021 | ldhdr->l_stoff = 0; | |
3022 | else | |
3023 | ldhdr->l_stoff = stoff; | |
3024 | ||
dc810e39 AM |
3025 | /* 64 bit elements to ldhdr |
3026 | The swap out routine for 32 bit will ignore them. | |
3027 | Nothing fancy, symbols come after the header and relocs come | |
3028 | after symbols. */ | |
3029 | ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd); | |
3030 | ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd) | |
3031 | + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)); | |
beb1bf64 | 3032 | |
252b5132 RH |
3033 | /* We now know the final size of the .loader section. Allocate |
3034 | space for it. */ | |
3035 | lsec = xcoff_hash_table (info)->loader_section; | |
eea6121a AM |
3036 | lsec->size = stoff + ldhdr->l_stlen; |
3037 | lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->size); | |
252b5132 RH |
3038 | if (lsec->contents == NULL) |
3039 | goto error_return; | |
3040 | ||
3041 | /* Set up the header. */ | |
beb1bf64 | 3042 | bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents); |
252b5132 RH |
3043 | |
3044 | /* Set up the import file names. */ | |
3045 | out = (char *) lsec->contents + ldhdr->l_impoff; | |
3046 | strcpy (out, libpath); | |
3047 | out += strlen (libpath) + 1; | |
3048 | *out++ = '\0'; | |
3049 | *out++ = '\0'; | |
3050 | for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next) | |
3051 | { | |
3052 | register const char *s; | |
3053 | ||
3054 | s = fl->path; | |
3055 | while ((*out++ = *s++) != '\0') | |
3056 | ; | |
3057 | s = fl->file; | |
3058 | while ((*out++ = *s++) != '\0') | |
3059 | ; | |
3060 | s = fl->member; | |
3061 | while ((*out++ = *s++) != '\0') | |
3062 | ; | |
3063 | } | |
3064 | ||
3065 | BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff); | |
3066 | ||
3067 | /* Set up the symbol string table. */ | |
3068 | if (ldinfo.string_size > 0) | |
3069 | { | |
3070 | memcpy (out, ldinfo.strings, ldinfo.string_size); | |
3071 | free (ldinfo.strings); | |
3072 | ldinfo.strings = NULL; | |
3073 | } | |
3074 | ||
3075 | /* We can't set up the symbol table or the relocs yet, because we | |
3076 | don't yet know the final position of the various sections. The | |
3077 | .loader symbols are written out when the corresponding normal | |
3078 | symbols are written out in xcoff_link_input_bfd or | |
3079 | xcoff_write_global_symbol. The .loader relocs are written out | |
3080 | when the corresponding normal relocs are handled in | |
dc810e39 | 3081 | xcoff_link_input_bfd. |
beb1bf64 | 3082 | */ |
252b5132 RH |
3083 | |
3084 | /* Allocate space for the magic sections. */ | |
3085 | sec = xcoff_hash_table (info)->linkage_section; | |
eea6121a | 3086 | if (sec->size > 0) |
252b5132 | 3087 | { |
eea6121a | 3088 | sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->size); |
252b5132 RH |
3089 | if (sec->contents == NULL) |
3090 | goto error_return; | |
3091 | } | |
3092 | sec = xcoff_hash_table (info)->toc_section; | |
eea6121a | 3093 | if (sec->size > 0) |
252b5132 | 3094 | { |
eea6121a | 3095 | sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->size); |
252b5132 RH |
3096 | if (sec->contents == NULL) |
3097 | goto error_return; | |
3098 | } | |
3099 | sec = xcoff_hash_table (info)->descriptor_section; | |
eea6121a | 3100 | if (sec->size > 0) |
252b5132 | 3101 | { |
eea6121a | 3102 | sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->size); |
252b5132 RH |
3103 | if (sec->contents == NULL) |
3104 | goto error_return; | |
3105 | } | |
3106 | ||
3107 | /* Now that we've done garbage collection, figure out the contents | |
3108 | of the .debug section. */ | |
3109 | debug_strtab = xcoff_hash_table (info)->debug_strtab; | |
3110 | ||
3111 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
3112 | { | |
3113 | asection *subdeb; | |
3114 | bfd_size_type symcount; | |
3115 | unsigned long *debug_index; | |
3116 | asection **csectpp; | |
3117 | bfd_byte *esym, *esymend; | |
3118 | bfd_size_type symesz; | |
3119 | ||
3120 | if (sub->xvec != info->hash->creator) | |
3121 | continue; | |
3122 | subdeb = bfd_get_section_by_name (sub, ".debug"); | |
eea6121a | 3123 | if (subdeb == NULL || subdeb->size == 0) |
252b5132 RH |
3124 | continue; |
3125 | ||
3126 | if (info->strip == strip_all | |
3127 | || info->strip == strip_debugger | |
3128 | || info->discard == discard_all) | |
3129 | { | |
eea6121a | 3130 | subdeb->size = 0; |
252b5132 RH |
3131 | continue; |
3132 | } | |
3133 | ||
3134 | if (! _bfd_coff_get_external_symbols (sub)) | |
3135 | goto error_return; | |
3136 | ||
3137 | symcount = obj_raw_syment_count (sub); | |
3138 | debug_index = ((unsigned long *) | |
3139 | bfd_zalloc (sub, symcount * sizeof (unsigned long))); | |
3140 | if (debug_index == NULL) | |
3141 | goto error_return; | |
3142 | xcoff_data (sub)->debug_indices = debug_index; | |
3143 | ||
3144 | /* Grab the contents of the .debug section. We use malloc and | |
3145 | copy the names into the debug stringtab, rather than | |
3146 | bfd_alloc, because I expect that, when linking many files | |
3147 | together, many of the strings will be the same. Storing the | |
3148 | strings in the hash table should save space in this case. */ | |
eea6121a | 3149 | if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents)) |
252b5132 RH |
3150 | goto error_return; |
3151 | ||
3152 | csectpp = xcoff_data (sub)->csects; | |
3153 | ||
eb1e0e80 | 3154 | /* Dynamic object do not have csectpp's. */ |
b34976b6 | 3155 | if (NULL != csectpp) |
252b5132 | 3156 | { |
eb1e0e80 NC |
3157 | symesz = bfd_coff_symesz (sub); |
3158 | esym = (bfd_byte *) obj_coff_external_syms (sub); | |
3159 | esymend = esym + symcount * symesz; | |
252b5132 | 3160 | |
eb1e0e80 NC |
3161 | while (esym < esymend) |
3162 | { | |
3163 | struct internal_syment sym; | |
252b5132 | 3164 | |
eb1e0e80 | 3165 | bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym); |
252b5132 | 3166 | |
eb1e0e80 | 3167 | *debug_index = (unsigned long) -1; |
252b5132 | 3168 | |
eb1e0e80 NC |
3169 | if (sym._n._n_n._n_zeroes == 0 |
3170 | && *csectpp != NULL | |
3171 | && (! gc | |
3172 | || ((*csectpp)->flags & SEC_MARK) != 0 | |
3173 | || *csectpp == bfd_abs_section_ptr) | |
3174 | && bfd_coff_symname_in_debug (sub, &sym)) | |
3175 | { | |
3176 | char *name; | |
3177 | bfd_size_type indx; | |
252b5132 | 3178 | |
eb1e0e80 | 3179 | name = (char *) debug_contents + sym._n._n_n._n_offset; |
b34976b6 | 3180 | indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE); |
eb1e0e80 NC |
3181 | if (indx == (bfd_size_type) -1) |
3182 | goto error_return; | |
3183 | *debug_index = indx; | |
3184 | } | |
3185 | ||
3186 | esym += (sym.n_numaux + 1) * symesz; | |
3187 | csectpp += sym.n_numaux + 1; | |
3188 | debug_index += sym.n_numaux + 1; | |
3189 | } | |
252b5132 RH |
3190 | } |
3191 | ||
3192 | free (debug_contents); | |
3193 | debug_contents = NULL; | |
3194 | ||
3195 | /* Clear the size of subdeb, so that it is not included directly | |
3196 | in the output file. */ | |
eea6121a | 3197 | subdeb->size = 0; |
252b5132 RH |
3198 | |
3199 | if (! info->keep_memory) | |
3200 | { | |
3201 | if (! _bfd_coff_free_symbols (sub)) | |
3202 | goto error_return; | |
3203 | } | |
3204 | } | |
3205 | ||
3206 | if (info->strip != strip_all) | |
eea6121a | 3207 | xcoff_hash_table (info)->debug_section->size = |
252b5132 RH |
3208 | _bfd_stringtab_size (debug_strtab); |
3209 | ||
b34976b6 | 3210 | return TRUE; |
252b5132 RH |
3211 | |
3212 | error_return: | |
3213 | if (ldinfo.strings != NULL) | |
3214 | free (ldinfo.strings); | |
3215 | if (debug_contents != NULL) | |
3216 | free (debug_contents); | |
b34976b6 | 3217 | return FALSE; |
252b5132 RH |
3218 | } |
3219 | ||
b34976b6 | 3220 | bfd_boolean |
69f284c7 | 3221 | bfd_xcoff_link_generate_rtinit (abfd, init, fini, rtld) |
9a4c7f16 TR |
3222 | bfd *abfd; |
3223 | const char *init; | |
3224 | const char *fini; | |
b34976b6 | 3225 | bfd_boolean rtld; |
9a4c7f16 TR |
3226 | { |
3227 | struct bfd_in_memory *bim; | |
b34976b6 | 3228 | |
9a4c7f16 TR |
3229 | bim = ((struct bfd_in_memory *) |
3230 | bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory))); | |
3231 | if (bim == NULL) | |
b34976b6 | 3232 | return FALSE; |
9a4c7f16 TR |
3233 | |
3234 | bim->size = 0; | |
3235 | bim->buffer = 0; | |
3236 | ||
3237 | abfd->link_next = 0; | |
3238 | abfd->format = bfd_object; | |
3239 | abfd->iostream = (PTR) bim; | |
3240 | abfd->flags = BFD_IN_MEMORY; | |
3241 | abfd->direction = write_direction; | |
3242 | abfd->where = 0; | |
3243 | ||
b34976b6 AM |
3244 | if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld)) |
3245 | return FALSE; | |
9a4c7f16 TR |
3246 | |
3247 | /* need to reset to unknown or it will not be read back in correctly */ | |
3248 | abfd->format = bfd_unknown; | |
3249 | abfd->direction = read_direction; | |
3250 | abfd->where = 0; | |
3251 | ||
b34976b6 | 3252 | return TRUE; |
9a4c7f16 TR |
3253 | } |
3254 | ||
3255 | ||
252b5132 RH |
3256 | /* Add a symbol to the .loader symbols, if necessary. */ |
3257 | ||
b34976b6 | 3258 | static bfd_boolean |
252b5132 RH |
3259 | xcoff_build_ldsyms (h, p) |
3260 | struct xcoff_link_hash_entry *h; | |
3261 | PTR p; | |
3262 | { | |
3263 | struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p; | |
dc810e39 | 3264 | bfd_size_type amt; |
252b5132 | 3265 | |
e92d460e AM |
3266 | if (h->root.type == bfd_link_hash_warning) |
3267 | h = (struct xcoff_link_hash_entry *) h->root.u.i.link; | |
3268 | ||
69f284c7 | 3269 | /* __rtinit, this symbol has special handling. */ |
dc810e39 | 3270 | if (h->flags & XCOFF_RTINIT) |
b34976b6 | 3271 | return TRUE; |
dc810e39 AM |
3272 | |
3273 | /* If this is a final link, and the symbol was defined as a common | |
252b5132 RH |
3274 | symbol in a regular object file, and there was no definition in |
3275 | any dynamic object, then the linker will have allocated space for | |
3276 | the symbol in a common section but the XCOFF_DEF_REGULAR flag | |
3277 | will not have been set. */ | |
3278 | if (h->root.type == bfd_link_hash_defined | |
3279 | && (h->flags & XCOFF_DEF_REGULAR) == 0 | |
3280 | && (h->flags & XCOFF_REF_REGULAR) != 0 | |
3281 | && (h->flags & XCOFF_DEF_DYNAMIC) == 0 | |
3282 | && (bfd_is_abs_section (h->root.u.def.section) | |
3283 | || (h->root.u.def.section->owner->flags & DYNAMIC) == 0)) | |
3284 | h->flags |= XCOFF_DEF_REGULAR; | |
3285 | ||
3286 | /* If all defined symbols should be exported, mark them now. We | |
3287 | don't want to export the actual functions, just the function | |
3288 | descriptors. */ | |
3289 | if (ldinfo->export_defineds | |
3290 | && (h->flags & XCOFF_DEF_REGULAR) != 0 | |
3291 | && h->root.root.string[0] != '.') | |
3292 | { | |
b34976b6 | 3293 | bfd_boolean export; |
252b5132 RH |
3294 | |
3295 | /* We don't export a symbol which is being defined by an object | |
3296 | included from an archive which contains a shared object. The | |
3297 | rationale is that if an archive contains both an unshared and | |
3298 | a shared object, then there must be some reason that the | |
3299 | unshared object is unshared, and we don't want to start | |
3300 | providing a shared version of it. In particular, this solves | |
3301 | a bug involving the _savefNN set of functions. gcc will call | |
3302 | those functions without providing a slot to restore the TOC, | |
3303 | so it is essential that these functions be linked in directly | |
3304 | and not from a shared object, which means that a shared | |
3305 | object which also happens to link them in must not export | |
3306 | them. This is confusing, but I haven't been able to think of | |
3307 | a different approach. Note that the symbols can, of course, | |
3308 | be exported explicitly. */ | |
b34976b6 | 3309 | export = TRUE; |
252b5132 RH |
3310 | if ((h->root.type == bfd_link_hash_defined |
3311 | || h->root.type == bfd_link_hash_defweak) | |
3312 | && h->root.u.def.section->owner != NULL | |
3313 | && h->root.u.def.section->owner->my_archive != NULL) | |
3314 | { | |
3315 | bfd *arbfd, *member; | |
3316 | ||
3317 | arbfd = h->root.u.def.section->owner->my_archive; | |
3318 | member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL); | |
3319 | while (member != NULL) | |
3320 | { | |
3321 | if ((member->flags & DYNAMIC) != 0) | |
3322 | { | |
b34976b6 | 3323 | export = FALSE; |
252b5132 RH |
3324 | break; |
3325 | } | |
3326 | member = bfd_openr_next_archived_file (arbfd, member); | |
3327 | } | |
3328 | } | |
3329 | ||
3330 | if (export) | |
3331 | h->flags |= XCOFF_EXPORT; | |
3332 | } | |
3333 | ||
3334 | /* We don't want to garbage collect symbols which are not defined in | |
3335 | XCOFF files. This is a convenient place to mark them. */ | |
3336 | if (xcoff_hash_table (ldinfo->info)->gc | |
3337 | && (h->flags & XCOFF_MARK) == 0 | |
3338 | && (h->root.type == bfd_link_hash_defined | |
3339 | || h->root.type == bfd_link_hash_defweak) | |
3340 | && (h->root.u.def.section->owner == NULL | |
3341 | || (h->root.u.def.section->owner->xvec | |
3342 | != ldinfo->info->hash->creator))) | |
3343 | h->flags |= XCOFF_MARK; | |
3344 | ||
3345 | /* If this symbol is called and defined in a dynamic object, or it | |
3346 | is imported, then we need to set up global linkage code for it. | |
3347 | (Unless we did garbage collection and we didn't need this | |
3348 | symbol.) */ | |
3349 | if ((h->flags & XCOFF_CALLED) != 0 | |
3350 | && (h->root.type == bfd_link_hash_undefined | |
3351 | || h->root.type == bfd_link_hash_undefweak) | |
3352 | && h->root.root.string[0] == '.' | |
3353 | && h->descriptor != NULL | |
3354 | && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0 | |
3355 | || ((h->descriptor->flags & XCOFF_IMPORT) != 0 | |
3356 | && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0)) | |
3357 | && (! xcoff_hash_table (ldinfo->info)->gc | |
3358 | || (h->flags & XCOFF_MARK) != 0)) | |
3359 | { | |
3360 | asection *sec; | |
3361 | struct xcoff_link_hash_entry *hds; | |
3362 | ||
3363 | sec = xcoff_hash_table (ldinfo->info)->linkage_section; | |
3364 | h->root.type = bfd_link_hash_defined; | |
3365 | h->root.u.def.section = sec; | |
eea6121a | 3366 | h->root.u.def.value = sec->size; |
252b5132 RH |
3367 | h->smclas = XMC_GL; |
3368 | h->flags |= XCOFF_DEF_REGULAR; | |
eea6121a | 3369 | sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd); |
252b5132 RH |
3370 | |
3371 | /* The global linkage code requires a TOC entry for the | |
b34976b6 | 3372 | descriptor. */ |
252b5132 RH |
3373 | hds = h->descriptor; |
3374 | BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined | |
3375 | || hds->root.type == bfd_link_hash_undefweak) | |
3376 | && (hds->flags & XCOFF_DEF_REGULAR) == 0); | |
3377 | hds->flags |= XCOFF_MARK; | |
dc810e39 AM |
3378 | if (hds->toc_section == NULL) |
3379 | { | |
3380 | int byte_size; | |
beb1bf64 | 3381 | |
dc810e39 AM |
3382 | /* 32 vs 64 |
3383 | xcoff32 uses 4 bytes in the toc. | |
3384 | xcoff64 uses 8 bytes in the toc. */ | |
3385 | if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd)) | |
69f284c7 | 3386 | byte_size = 8; |
dc810e39 | 3387 | else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd)) |
69f284c7 | 3388 | byte_size = 4; |
dc810e39 | 3389 | else |
b34976b6 | 3390 | return FALSE; |
dc810e39 AM |
3391 | |
3392 | hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section; | |
eea6121a AM |
3393 | hds->u.toc_offset = hds->toc_section->size; |
3394 | hds->toc_section->size += byte_size; | |
dc810e39 AM |
3395 | ++xcoff_hash_table (ldinfo->info)->ldrel_count; |
3396 | ++hds->toc_section->reloc_count; | |
3397 | hds->indx = -2; | |
3398 | hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL; | |
3399 | ||
3400 | /* We need to call xcoff_build_ldsyms recursively here, | |
3401 | because we may already have passed hds on the traversal. */ | |
3402 | xcoff_build_ldsyms (hds, p); | |
3403 | } | |
252b5132 RH |
3404 | } |
3405 | ||
3406 | /* If this symbol is exported, but not defined, we need to try to | |
3407 | define it. */ | |
3408 | if ((h->flags & XCOFF_EXPORT) != 0 | |
3409 | && (h->flags & XCOFF_IMPORT) == 0 | |
3410 | && (h->flags & XCOFF_DEF_REGULAR) == 0 | |
3411 | && (h->flags & XCOFF_DEF_DYNAMIC) == 0 | |
3412 | && (h->root.type == bfd_link_hash_undefined | |
3413 | || h->root.type == bfd_link_hash_undefweak)) | |
3414 | { | |
3415 | if ((h->flags & XCOFF_DESCRIPTOR) != 0 | |
3416 | && (h->descriptor->root.type == bfd_link_hash_defined | |
3417 | || h->descriptor->root.type == bfd_link_hash_defweak)) | |
3418 | { | |
3419 | asection *sec; | |
3420 | ||
3421 | /* This is an undefined function descriptor associated with | |
b34976b6 AM |
3422 | a defined entry point. We can build up a function |
3423 | descriptor ourselves. Believe it or not, the AIX linker | |
3424 | actually does this, and there are cases where we need to | |
3425 | do it as well. */ | |
252b5132 RH |
3426 | sec = xcoff_hash_table (ldinfo->info)->descriptor_section; |
3427 | h->root.type = bfd_link_hash_defined; | |
3428 | h->root.u.def.section = sec; | |
eea6121a | 3429 | h->root.u.def.value = sec->size; |
252b5132 RH |
3430 | h->smclas = XMC_DS; |
3431 | h->flags |= XCOFF_DEF_REGULAR; | |
252b5132 | 3432 | |
dc810e39 AM |
3433 | /* The size of the function descriptor depends if this is an |
3434 | xcoff32 (12) or xcoff64 (24). */ | |
eea6121a | 3435 | sec->size += |
beb1bf64 TR |
3436 | bfd_xcoff_function_descriptor_size(ldinfo->output_bfd); |
3437 | ||
dc810e39 AM |
3438 | /* A function descriptor uses two relocs: one for the |
3439 | associated code, and one for the TOC address. */ | |
252b5132 RH |
3440 | xcoff_hash_table (ldinfo->info)->ldrel_count += 2; |
3441 | sec->reloc_count += 2; | |
3442 | ||
dc810e39 AM |
3443 | /* We handle writing out the contents of the descriptor in |
3444 | xcoff_write_global_symbol. */ | |
252b5132 RH |
3445 | } |
3446 | else | |
3447 | { | |
3448 | (*_bfd_error_handler) | |
3449 | (_("warning: attempt to export undefined symbol `%s'"), | |
3450 | h->root.root.string); | |
3451 | h->ldsym = NULL; | |
b34976b6 | 3452 | return TRUE; |
252b5132 RH |
3453 | } |
3454 | } | |
3455 | ||
3456 | /* If this is still a common symbol, and it wasn't garbage | |
3457 | collected, we need to actually allocate space for it in the .bss | |
3458 | section. */ | |
3459 | if (h->root.type == bfd_link_hash_common | |
3460 | && (! xcoff_hash_table (ldinfo->info)->gc | |
3461 | || (h->flags & XCOFF_MARK) != 0) | |
eea6121a | 3462 | && h->root.u.c.p->section->size == 0) |
252b5132 RH |
3463 | { |
3464 | BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section)); | |
eea6121a | 3465 | h->root.u.c.p->section->size = h->root.u.c.size; |
252b5132 RH |
3466 | } |
3467 | ||
3468 | /* We need to add a symbol to the .loader section if it is mentioned | |
3469 | in a reloc which we are copying to the .loader section and it was | |
3470 | not defined or common, or if it is the entry point, or if it is | |
3471 | being exported. */ | |
3472 | ||
3473 | if (((h->flags & XCOFF_LDREL) == 0 | |
3474 | || h->root.type == bfd_link_hash_defined | |
3475 | || h->root.type == bfd_link_hash_defweak | |
3476 | || h->root.type == bfd_link_hash_common) | |
3477 | && (h->flags & XCOFF_ENTRY) == 0 | |
3478 | && (h->flags & XCOFF_EXPORT) == 0) | |
3479 | { | |
3480 | h->ldsym = NULL; | |
b34976b6 | 3481 | return TRUE; |
252b5132 RH |
3482 | } |
3483 | ||
3484 | /* We don't need to add this symbol if we did garbage collection and | |
3485 | we did not mark this symbol. */ | |
3486 | if (xcoff_hash_table (ldinfo->info)->gc | |
3487 | && (h->flags & XCOFF_MARK) == 0) | |
3488 | { | |
3489 | h->ldsym = NULL; | |
b34976b6 | 3490 | return TRUE; |
252b5132 RH |
3491 | } |
3492 | ||
3493 | /* We may have already processed this symbol due to the recursive | |
3494 | call above. */ | |
3495 | if ((h->flags & XCOFF_BUILT_LDSYM) != 0) | |
b34976b6 | 3496 | return TRUE; |
252b5132 RH |
3497 | |
3498 | /* We need to add this symbol to the .loader symbols. */ | |
3499 | ||
3500 | BFD_ASSERT (h->ldsym == NULL); | |
dc810e39 AM |
3501 | amt = sizeof (struct internal_ldsym); |
3502 | h->ldsym = (struct internal_ldsym *) bfd_zalloc (ldinfo->output_bfd, amt); | |
252b5132 RH |
3503 | if (h->ldsym == NULL) |
3504 | { | |
b34976b6 AM |
3505 | ldinfo->failed = TRUE; |
3506 | return FALSE; | |
252b5132 RH |
3507 | } |
3508 | ||
3509 | if ((h->flags & XCOFF_IMPORT) != 0) | |
3510 | h->ldsym->l_ifile = h->ldindx; | |
3511 | ||
dc810e39 AM |
3512 | /* The first 3 symbol table indices are reserved to indicate the |
3513 | data, text and bss sections. */ | |
252b5132 RH |
3514 | h->ldindx = ldinfo->ldsym_count + 3; |
3515 | ||
3516 | ++ldinfo->ldsym_count; | |
3517 | ||
b34976b6 AM |
3518 | if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo, |
3519 | h->ldsym, h->root.root.string)) | |
dc810e39 | 3520 | { |
b34976b6 | 3521 | return FALSE; |
dc810e39 | 3522 | } |
252b5132 RH |
3523 | |
3524 | h->flags |= XCOFF_BUILT_LDSYM; | |
3525 | ||
b34976b6 | 3526 | return TRUE; |
252b5132 RH |
3527 | } |
3528 | \f | |
3529 | /* Do the final link step. */ | |
3530 | ||
b34976b6 | 3531 | bfd_boolean |
252b5132 RH |
3532 | _bfd_xcoff_bfd_final_link (abfd, info) |
3533 | bfd *abfd; | |
3534 | struct bfd_link_info *info; | |
3535 | { | |
3536 | bfd_size_type symesz; | |
3537 | struct xcoff_final_link_info finfo; | |
3538 | asection *o; | |
3539 | struct bfd_link_order *p; | |
dc810e39 AM |
3540 | bfd_size_type max_contents_size; |
3541 | bfd_size_type max_sym_count; | |
3542 | bfd_size_type max_lineno_count; | |
3543 | bfd_size_type max_reloc_count; | |
3544 | bfd_size_type max_output_reloc_count; | |
252b5132 RH |
3545 | file_ptr rel_filepos; |
3546 | unsigned int relsz; | |
3547 | file_ptr line_filepos; | |
3548 | unsigned int linesz; | |
3549 | bfd *sub; | |
3550 | bfd_byte *external_relocs = NULL; | |
3551 | char strbuf[STRING_SIZE_SIZE]; | |
dc810e39 AM |
3552 | file_ptr pos; |
3553 | bfd_size_type amt; | |
252b5132 RH |
3554 | |
3555 | if (info->shared) | |
3556 | abfd->flags |= DYNAMIC; | |
3557 | ||
3558 | symesz = bfd_coff_symesz (abfd); | |
3559 | ||
3560 | finfo.info = info; | |
3561 | finfo.output_bfd = abfd; | |
3562 | finfo.strtab = NULL; | |
3563 | finfo.section_info = NULL; | |
3564 | finfo.last_file_index = -1; | |
3565 | finfo.toc_symindx = -1; | |
3566 | finfo.internal_syms = NULL; | |
3567 | finfo.sym_indices = NULL; | |
3568 | finfo.outsyms = NULL; | |
3569 | finfo.linenos = NULL; | |
3570 | finfo.contents = NULL; | |
3571 | finfo.external_relocs = NULL; | |
3572 | ||
dc810e39 AM |
3573 | finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents |
3574 | + bfd_xcoff_ldhdrsz (abfd)); | |
3575 | finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents | |
3576 | + bfd_xcoff_ldhdrsz(abfd) | |
3577 | + (xcoff_hash_table (info)->ldhdr.l_nsyms | |
3578 | * bfd_xcoff_ldsymsz(abfd))); | |
252b5132 RH |
3579 | |
3580 | xcoff_data (abfd)->coff.link_info = info; | |
3581 | ||
3582 | finfo.strtab = _bfd_stringtab_init (); | |
3583 | if (finfo.strtab == NULL) | |
3584 | goto error_return; | |
3585 | ||
3586 | /* Count the line number and relocation entries required for the | |
3587 | output file. Determine a few maximum sizes. */ | |
3588 | max_contents_size = 0; | |
3589 | max_lineno_count = 0; | |
3590 | max_reloc_count = 0; | |
3591 | for (o = abfd->sections; o != NULL; o = o->next) | |
3592 | { | |
3593 | o->reloc_count = 0; | |
3594 | o->lineno_count = 0; | |
3595 | for (p = o->link_order_head; p != NULL; p = p->next) | |
3596 | { | |
3597 | if (p->type == bfd_indirect_link_order) | |
3598 | { | |
3599 | asection *sec; | |
3600 | ||
3601 | sec = p->u.indirect.section; | |
3602 | ||
3603 | /* Mark all sections which are to be included in the | |
3604 | link. This will normally be every section. We need | |
3605 | to do this so that we can identify any sections which | |
3606 | the linker has decided to not include. */ | |
b34976b6 | 3607 | sec->linker_mark = TRUE; |
252b5132 RH |
3608 | |
3609 | if (info->strip == strip_none | |
3610 | || info->strip == strip_some) | |
3611 | o->lineno_count += sec->lineno_count; | |
3612 | ||
3613 | o->reloc_count += sec->reloc_count; | |
3614 | ||
eea6121a AM |
3615 | if (sec->rawsize > max_contents_size) |
3616 | max_contents_size = sec->rawsize; | |
3617 | if (sec->size > max_contents_size) | |
3618 | max_contents_size = sec->size; | |
252b5132 RH |
3619 | if (sec->lineno_count > max_lineno_count) |
3620 | max_lineno_count = sec->lineno_count; | |
3621 | if (coff_section_data (sec->owner, sec) != NULL | |
3622 | && xcoff_section_data (sec->owner, sec) != NULL | |
3623 | && (xcoff_section_data (sec->owner, sec)->lineno_count | |
3624 | > max_lineno_count)) | |
3625 | max_lineno_count = | |
3626 | xcoff_section_data (sec->owner, sec)->lineno_count; | |
3627 | if (sec->reloc_count > max_reloc_count) | |
3628 | max_reloc_count = sec->reloc_count; | |
3629 | } | |
3630 | else if (p->type == bfd_section_reloc_link_order | |
3631 | || p->type == bfd_symbol_reloc_link_order) | |
3632 | ++o->reloc_count; | |
3633 | } | |
3634 | } | |
3635 | ||
3636 | /* Compute the file positions for all the sections. */ | |
3637 | if (abfd->output_has_begun) | |
3638 | { | |
3639 | if (xcoff_hash_table (info)->file_align != 0) | |
3640 | abort (); | |
3641 | } | |
3642 | else | |
3643 | { | |
3644 | bfd_vma file_align; | |
3645 | ||
3646 | file_align = xcoff_hash_table (info)->file_align; | |
3647 | if (file_align != 0) | |
3648 | { | |
b34976b6 | 3649 | bfd_boolean saw_contents; |
252b5132 | 3650 | int indx; |
9e7b37b3 | 3651 | asection **op; |
252b5132 | 3652 | file_ptr sofar; |
b34976b6 | 3653 | |
252b5132 | 3654 | /* Insert .pad sections before every section which has |
b34976b6 AM |
3655 | contents and is loaded, if it is preceded by some other |
3656 | section which has contents and is loaded. */ | |
3657 | saw_contents = TRUE; | |
9e7b37b3 | 3658 | for (op = &abfd->sections; *op != NULL; op = &(*op)->next) |
252b5132 RH |
3659 | { |
3660 | if (strcmp ((*op)->name, ".pad") == 0) | |
b34976b6 | 3661 | saw_contents = FALSE; |
252b5132 RH |
3662 | else if (((*op)->flags & SEC_HAS_CONTENTS) != 0 |
3663 | && ((*op)->flags & SEC_LOAD) != 0) | |
3664 | { | |
3665 | if (! saw_contents) | |
b34976b6 | 3666 | saw_contents = TRUE; |
252b5132 RH |
3667 | else |
3668 | { | |
9e7b37b3 | 3669 | asection *n, **st; |
b34976b6 | 3670 | |
04b5af89 | 3671 | /* Create a pad section and place it before the section |
b34976b6 | 3672 | that needs padding. This requires unlinking and |
9e7b37b3 | 3673 | relinking the bfd's section list. */ |
b34976b6 | 3674 | |
04b5af89 | 3675 | st = abfd->section_tail; |
252b5132 | 3676 | n = bfd_make_section_anyway (abfd, ".pad"); |
252b5132 | 3677 | n->flags = SEC_HAS_CONTENTS; |
b34976b6 | 3678 | n->alignment_power = 0; |
04b5af89 | 3679 | |
9e7b37b3 AM |
3680 | BFD_ASSERT (*st == n); |
3681 | bfd_section_list_remove (abfd, st); | |
3682 | bfd_section_list_insert (abfd, op, n); | |
3683 | ||
3684 | op = &n->next; | |
b34976b6 | 3685 | saw_contents = FALSE; |
252b5132 RH |
3686 | } |
3687 | } | |
3688 | } | |
b34976b6 | 3689 | |
252b5132 | 3690 | /* Reset the section indices after inserting the new |
b34976b6 | 3691 | sections. */ |
252b5132 RH |
3692 | indx = 0; |
3693 | for (o = abfd->sections; o != NULL; o = o->next) | |
3694 | { | |
3695 | ++indx; | |
3696 | o->target_index = indx; | |
3697 | } | |
3698 | BFD_ASSERT ((unsigned int) indx == abfd->section_count); | |
3699 | ||
3700 | /* Work out appropriate sizes for the .pad sections to force | |
b34976b6 AM |
3701 | each section to land on a page boundary. This bit of |
3702 | code knows what compute_section_file_positions is going | |
3703 | to do. */ | |
252b5132 RH |
3704 | sofar = bfd_coff_filhsz (abfd); |
3705 | sofar += bfd_coff_aoutsz (abfd); | |
3706 | sofar += abfd->section_count * bfd_coff_scnhsz (abfd); | |
dc810e39 AM |
3707 | for (o = abfd->sections; o != NULL; o = o->next) |
3708 | if ((bfd_xcoff_is_reloc_count_overflow | |
3709 | (abfd, (bfd_vma) o->reloc_count)) | |
3710 | || (bfd_xcoff_is_lineno_count_overflow | |
3711 | (abfd, (bfd_vma) o->lineno_count))) | |
beb1bf64 TR |
3712 | /* 64 does not overflow, need to check if 32 does */ |
3713 | sofar += bfd_coff_scnhsz (abfd); | |
dc810e39 | 3714 | |
252b5132 RH |
3715 | for (o = abfd->sections; o != NULL; o = o->next) |
3716 | { | |
3717 | if (strcmp (o->name, ".pad") == 0) | |
3718 | { | |
3719 | bfd_vma pageoff; | |
3720 | ||
eea6121a | 3721 | BFD_ASSERT (o->size == 0); |
252b5132 RH |
3722 | pageoff = sofar & (file_align - 1); |
3723 | if (pageoff != 0) | |
3724 | { | |
eea6121a | 3725 | o->size = file_align - pageoff; |
252b5132 RH |
3726 | sofar += file_align - pageoff; |
3727 | o->flags |= SEC_HAS_CONTENTS; | |
3728 | } | |
3729 | } | |
3730 | else | |
3731 | { | |
3732 | if ((o->flags & SEC_HAS_CONTENTS) != 0) | |
eea6121a | 3733 | sofar += BFD_ALIGN (o->size, |
252b5132 RH |
3734 | 1 << o->alignment_power); |
3735 | } | |
3736 | } | |
3737 | } | |
3738 | ||
3739 | if (! bfd_coff_compute_section_file_positions (abfd)) | |
3740 | goto error_return; | |
3741 | } | |
3742 | ||
3743 | /* Allocate space for the pointers we need to keep for the relocs. */ | |
3744 | { | |
3745 | unsigned int i; | |
3746 | ||
3747 | /* We use section_count + 1, rather than section_count, because | |
3748 | the target_index fields are 1 based. */ | |
dc810e39 AM |
3749 | amt = abfd->section_count + 1; |
3750 | amt *= sizeof (struct xcoff_link_section_info); | |
3751 | finfo.section_info = (struct xcoff_link_section_info *) bfd_malloc (amt); | |
252b5132 RH |
3752 | if (finfo.section_info == NULL) |
3753 | goto error_return; | |
3754 | for (i = 0; i <= abfd->section_count; i++) | |
3755 | { | |
3756 | finfo.section_info[i].relocs = NULL; | |
3757 | finfo.section_info[i].rel_hashes = NULL; | |
3758 | finfo.section_info[i].toc_rel_hashes = NULL; | |
3759 | } | |
3760 | } | |
3761 | ||
3762 | /* Set the file positions for the relocs. */ | |
3763 | rel_filepos = obj_relocbase (abfd); | |
3764 | relsz = bfd_coff_relsz (abfd); | |
3765 | max_output_reloc_count = 0; | |
3766 | for (o = abfd->sections; o != NULL; o = o->next) | |
3767 | { | |
3768 | if (o->reloc_count == 0) | |
3769 | o->rel_filepos = 0; | |
3770 | else | |
3771 | { | |
3772 | /* A stripped file has no relocs. However, we still | |
b34976b6 AM |
3773 | allocate the buffers, so that later code doesn't have to |
3774 | worry about whether we are stripping or not. */ | |
252b5132 RH |
3775 | if (info->strip == strip_all) |
3776 | o->rel_filepos = 0; | |
3777 | else | |
3778 | { | |
3779 | o->flags |= SEC_RELOC; | |
3780 | o->rel_filepos = rel_filepos; | |
3781 | rel_filepos += o->reloc_count * relsz; | |
3782 | } | |
3783 | ||
3784 | /* We don't know the indices of global symbols until we have | |
b34976b6 AM |
3785 | written out all the local symbols. For each section in |
3786 | the output file, we keep an array of pointers to hash | |
3787 | table entries. Each entry in the array corresponds to a | |
3788 | reloc. When we find a reloc against a global symbol, we | |
3789 | set the corresponding entry in this array so that we can | |
3790 | fix up the symbol index after we have written out all the | |
3791 | local symbols. | |
252b5132 RH |
3792 | |
3793 | Because of this problem, we also keep the relocs in | |
3794 | memory until the end of the link. This wastes memory. | |
3795 | We could backpatch the file later, I suppose, although it | |
3796 | would be slow. */ | |
dc810e39 AM |
3797 | amt = o->reloc_count; |
3798 | amt *= sizeof (struct internal_reloc); | |
252b5132 | 3799 | finfo.section_info[o->target_index].relocs = |
dc810e39 AM |
3800 | (struct internal_reloc *) bfd_malloc (amt); |
3801 | ||
3802 | amt = o->reloc_count; | |
3803 | amt *= sizeof (struct xcoff_link_hash_entry *); | |
252b5132 | 3804 | finfo.section_info[o->target_index].rel_hashes = |
dc810e39 AM |
3805 | (struct xcoff_link_hash_entry **) bfd_malloc (amt); |
3806 | ||
252b5132 RH |
3807 | if (finfo.section_info[o->target_index].relocs == NULL |
3808 | || finfo.section_info[o->target_index].rel_hashes == NULL) | |
3809 | goto error_return; | |
3810 | ||
3811 | if (o->reloc_count > max_output_reloc_count) | |
3812 | max_output_reloc_count = o->reloc_count; | |
3813 | } | |
3814 | } | |
3815 | ||
3816 | /* We now know the size of the relocs, so we can determine the file | |
3817 | positions of the line numbers. */ | |
3818 | line_filepos = rel_filepos; | |
3819 | finfo.line_filepos = line_filepos; | |
3820 | linesz = bfd_coff_linesz (abfd); | |
3821 | for (o = abfd->sections; o != NULL; o = o->next) | |
3822 | { | |
3823 | if (o->lineno_count == 0) | |
3824 | o->line_filepos = 0; | |
3825 | else | |
3826 | { | |
3827 | o->line_filepos = line_filepos; | |
3828 | line_filepos += o->lineno_count * linesz; | |
3829 | } | |
3830 | ||
3831 | /* Reset the reloc and lineno counts, so that we can use them to | |
3832 | count the number of entries we have output so far. */ | |
3833 | o->reloc_count = 0; | |
3834 | o->lineno_count = 0; | |
3835 | } | |
3836 | ||
3837 | obj_sym_filepos (abfd) = line_filepos; | |
3838 | ||
3839 | /* Figure out the largest number of symbols in an input BFD. Take | |
3840 | the opportunity to clear the output_has_begun fields of all the | |
3841 | input BFD's. We want at least 6 symbols, since that is the | |
3842 | number which xcoff_write_global_symbol may need. */ | |
3843 | max_sym_count = 6; | |
3844 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
3845 | { | |
dc810e39 | 3846 | bfd_size_type sz; |
252b5132 | 3847 | |
b34976b6 | 3848 | sub->output_has_begun = FALSE; |
252b5132 RH |
3849 | sz = obj_raw_syment_count (sub); |
3850 | if (sz > max_sym_count) | |
3851 | max_sym_count = sz; | |
3852 | } | |
3853 | ||
3854 | /* Allocate some buffers used while linking. */ | |
dc810e39 AM |
3855 | amt = max_sym_count * sizeof (struct internal_syment); |
3856 | finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt); | |
3857 | ||
3858 | amt = max_sym_count * sizeof (long); | |
3859 | finfo.sym_indices = (long *) bfd_malloc (amt); | |
3860 | ||
3861 | amt = (max_sym_count + 1) * symesz; | |
3862 | finfo.outsyms = (bfd_byte *) bfd_malloc (amt); | |
3863 | ||
3864 | amt = max_lineno_count * bfd_coff_linesz (abfd); | |
3865 | finfo.linenos = (bfd_byte *) bfd_malloc (amt); | |
3866 | ||
3867 | amt = max_contents_size; | |
3868 | finfo.contents = (bfd_byte *) bfd_malloc (amt); | |
3869 | ||
3870 | amt = max_reloc_count * relsz; | |
3871 | finfo.external_relocs = (bfd_byte *) bfd_malloc (amt); | |
3872 | ||
252b5132 RH |
3873 | if ((finfo.internal_syms == NULL && max_sym_count > 0) |
3874 | || (finfo.sym_indices == NULL && max_sym_count > 0) | |
3875 | || finfo.outsyms == NULL | |
3876 | || (finfo.linenos == NULL && max_lineno_count > 0) | |
3877 | || (finfo.contents == NULL && max_contents_size > 0) | |
3878 | || (finfo.external_relocs == NULL && max_reloc_count > 0)) | |
3879 | goto error_return; | |
3880 | ||
3881 | obj_raw_syment_count (abfd) = 0; | |
3882 | xcoff_data (abfd)->toc = (bfd_vma) -1; | |
3883 | ||
3884 | /* We now know the position of everything in the file, except that | |
3885 | we don't know the size of the symbol table and therefore we don't | |
3886 | know where the string table starts. We just build the string | |
3887 | table in memory as we go along. We process all the relocations | |
3888 | for a single input file at once. */ | |
3889 | for (o = abfd->sections; o != NULL; o = o->next) | |
3890 | { | |
3891 | for (p = o->link_order_head; p != NULL; p = p->next) | |
3892 | { | |
3893 | if (p->type == bfd_indirect_link_order | |
3894 | && p->u.indirect.section->owner->xvec == abfd->xvec) | |
3895 | { | |
3896 | sub = p->u.indirect.section->owner; | |
3897 | if (! sub->output_has_begun) | |
3898 | { | |
3899 | if (! xcoff_link_input_bfd (&finfo, sub)) | |
3900 | goto error_return; | |
b34976b6 | 3901 | sub->output_has_begun = TRUE; |
252b5132 RH |
3902 | } |
3903 | } | |
3904 | else if (p->type == bfd_section_reloc_link_order | |
3905 | || p->type == bfd_symbol_reloc_link_order) | |
3906 | { | |
3907 | if (! xcoff_reloc_link_order (abfd, &finfo, o, p)) | |
3908 | goto error_return; | |
3909 | } | |
3910 | else | |
3911 | { | |
3912 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
3913 | goto error_return; | |
3914 | } | |
3915 | } | |
3916 | } | |
3917 | ||
beb1bf64 | 3918 | |
252b5132 RH |
3919 | /* Free up the buffers used by xcoff_link_input_bfd. */ |
3920 | ||
dc810e39 AM |
3921 | if (finfo.internal_syms != NULL) |
3922 | { | |
3923 | free (finfo.internal_syms); | |
3924 | finfo.internal_syms = NULL; | |
3925 | } | |
3926 | if (finfo.sym_indices != NULL) | |
3927 | { | |
3928 | free (finfo.sym_indices); | |
3929 | finfo.sym_indices = NULL; | |
3930 | } | |
3931 | if (finfo.linenos != NULL) | |
3932 | { | |
3933 | free (finfo.linenos); | |
3934 | finfo.linenos = NULL; | |
3935 | } | |
3936 | if (finfo.contents != NULL) | |
3937 | { | |
3938 | free (finfo.contents); | |
3939 | finfo.contents = NULL; | |
3940 | } | |
3941 | if (finfo.external_relocs != NULL) | |
3942 | { | |
3943 | free (finfo.external_relocs); | |
3944 | finfo.external_relocs = NULL; | |
3945 | } | |
252b5132 RH |
3946 | |
3947 | /* The value of the last C_FILE symbol is supposed to be -1. Write | |
3948 | it out again. */ | |
dc810e39 AM |
3949 | if (finfo.last_file_index != -1) |
3950 | { | |
3951 | finfo.last_file.n_value = -(bfd_vma) 1; | |
3952 | bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file, | |
3953 | (PTR) finfo.outsyms); | |
3954 | pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz; | |
3955 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
3956 | || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz) | |
3957 | goto error_return; | |
3958 | } | |
beb1bf64 | 3959 | |
252b5132 RH |
3960 | /* Write out all the global symbols which do not come from XCOFF |
3961 | input files. */ | |
3962 | xcoff_link_hash_traverse (xcoff_hash_table (info), | |
3963 | xcoff_write_global_symbol, | |
3964 | (PTR) &finfo); | |
3965 | ||
3966 | if (finfo.outsyms != NULL) | |
3967 | { | |
3968 | free (finfo.outsyms); | |
3969 | finfo.outsyms = NULL; | |
3970 | } | |
3971 | ||
3972 | /* Now that we have written out all the global symbols, we know the | |
3973 | symbol indices to use for relocs against them, and we can finally | |
3974 | write out the relocs. */ | |
dc810e39 AM |
3975 | amt = max_output_reloc_count * relsz; |
3976 | external_relocs = (bfd_byte *) bfd_malloc (amt); | |
252b5132 RH |
3977 | if (external_relocs == NULL && max_output_reloc_count != 0) |
3978 | goto error_return; | |
3979 | ||
dc810e39 AM |
3980 | for (o = abfd->sections; o != NULL; o = o->next) |
3981 | { | |
3982 | struct internal_reloc *irel; | |
3983 | struct internal_reloc *irelend; | |
3984 | struct xcoff_link_hash_entry **rel_hash; | |
3985 | struct xcoff_toc_rel_hash *toc_rel_hash; | |
3986 | bfd_byte *erel; | |
3987 | bfd_size_type rel_size; | |
3988 | ||
3989 | /* A stripped file has no relocs. */ | |
3990 | if (info->strip == strip_all) | |
3991 | { | |
3992 | o->reloc_count = 0; | |
3993 | continue; | |
252b5132 | 3994 | } |
252b5132 | 3995 | |
dc810e39 AM |
3996 | if (o->reloc_count == 0) |
3997 | continue; | |
252b5132 | 3998 | |
dc810e39 AM |
3999 | irel = finfo.section_info[o->target_index].relocs; |
4000 | irelend = irel + o->reloc_count; | |
4001 | rel_hash = finfo.section_info[o->target_index].rel_hashes; | |
4002 | for (; irel < irelend; irel++, rel_hash++, erel += relsz) | |
4003 | { | |
4004 | if (*rel_hash != NULL) | |
4005 | { | |
4006 | if ((*rel_hash)->indx < 0) | |
4007 | { | |
4008 | if (! ((*info->callbacks->unattached_reloc) | |
4009 | (info, (*rel_hash)->root.root.string, | |
4010 | (bfd *) NULL, o, irel->r_vaddr))) | |
4011 | goto error_return; | |
4012 | (*rel_hash)->indx = 0; | |
4013 | } | |
4014 | irel->r_symndx = (*rel_hash)->indx; | |
4015 | } | |
4016 | } | |
beb1bf64 | 4017 | |
dc810e39 AM |
4018 | for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes; |
4019 | toc_rel_hash != NULL; | |
4020 | toc_rel_hash = toc_rel_hash->next) | |
4021 | { | |
4022 | if (toc_rel_hash->h->u.toc_indx < 0) | |
4023 | { | |
4024 | if (! ((*info->callbacks->unattached_reloc) | |
4025 | (info, toc_rel_hash->h->root.root.string, | |
4026 | (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr))) | |
4027 | goto error_return; | |
4028 | toc_rel_hash->h->u.toc_indx = 0; | |
4029 | } | |
4030 | toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx; | |
4031 | } | |
4032 | ||
4033 | /* XCOFF requires that the relocs be sorted by address. We tend | |
4034 | to produce them in the order in which their containing csects | |
4035 | appear in the symbol table, which is not necessarily by | |
4036 | address. So we sort them here. There may be a better way to | |
4037 | do this. */ | |
4038 | qsort ((PTR) finfo.section_info[o->target_index].relocs, | |
4039 | o->reloc_count, sizeof (struct internal_reloc), | |
4040 | xcoff_sort_relocs); | |
4041 | ||
4042 | irel = finfo.section_info[o->target_index].relocs; | |
4043 | irelend = irel + o->reloc_count; | |
4044 | erel = external_relocs; | |
4045 | for (; irel < irelend; irel++, rel_hash++, erel += relsz) | |
4046 | bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel); | |
4047 | ||
4048 | rel_size = relsz * o->reloc_count; | |
4049 | if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0 | |
4050 | || bfd_bwrite ((PTR) external_relocs, rel_size, abfd) != rel_size) | |
4051 | goto error_return; | |
4052 | } | |
4053 | ||
4054 | if (external_relocs != NULL) | |
4055 | { | |
4056 | free (external_relocs); | |
4057 | external_relocs = NULL; | |
4058 | } | |
beb1bf64 | 4059 | |
252b5132 | 4060 | /* Free up the section information. */ |
dc810e39 AM |
4061 | if (finfo.section_info != NULL) |
4062 | { | |
4063 | unsigned int i; | |
252b5132 | 4064 | |
dc810e39 AM |
4065 | for (i = 0; i < abfd->section_count; i++) |
4066 | { | |
4067 | if (finfo.section_info[i].relocs != NULL) | |
4068 | free (finfo.section_info[i].relocs); | |
4069 | if (finfo.section_info[i].rel_hashes != NULL) | |
4070 | free (finfo.section_info[i].rel_hashes); | |
4071 | } | |
4072 | free (finfo.section_info); | |
4073 | finfo.section_info = NULL; | |
252b5132 RH |
4074 | } |
4075 | ||
4076 | /* Write out the loader section contents. */ | |
4077 | BFD_ASSERT ((bfd_byte *) finfo.ldrel | |
4078 | == (xcoff_hash_table (info)->loader_section->contents | |
4079 | + xcoff_hash_table (info)->ldhdr.l_impoff)); | |
4080 | o = xcoff_hash_table (info)->loader_section; | |
dc810e39 | 4081 | if (! bfd_set_section_contents (abfd, o->output_section, o->contents, |
eea6121a | 4082 | (file_ptr) o->output_offset, o->size)) |
252b5132 RH |
4083 | goto error_return; |
4084 | ||
4085 | /* Write out the magic sections. */ | |
4086 | o = xcoff_hash_table (info)->linkage_section; | |
eea6121a | 4087 | if (o->size > 0 |
252b5132 | 4088 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, |
dc810e39 | 4089 | (file_ptr) o->output_offset, |
eea6121a | 4090 | o->size)) |
252b5132 RH |
4091 | goto error_return; |
4092 | o = xcoff_hash_table (info)->toc_section; | |
eea6121a | 4093 | if (o->size > 0 |
252b5132 | 4094 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, |
dc810e39 | 4095 | (file_ptr) o->output_offset, |
eea6121a | 4096 | o->size)) |
252b5132 RH |
4097 | goto error_return; |
4098 | o = xcoff_hash_table (info)->descriptor_section; | |
eea6121a | 4099 | if (o->size > 0 |
252b5132 | 4100 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, |
dc810e39 | 4101 | (file_ptr) o->output_offset, |
eea6121a | 4102 | o->size)) |
252b5132 RH |
4103 | goto error_return; |
4104 | ||
4105 | /* Write out the string table. */ | |
dc810e39 AM |
4106 | pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz; |
4107 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
252b5132 | 4108 | goto error_return; |
dc810e39 AM |
4109 | H_PUT_32 (abfd, |
4110 | _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE, | |
4111 | strbuf); | |
4112 | amt = STRING_SIZE_SIZE; | |
4113 | if (bfd_bwrite (strbuf, amt, abfd) != amt) | |
252b5132 RH |
4114 | goto error_return; |
4115 | if (! _bfd_stringtab_emit (abfd, finfo.strtab)) | |
4116 | goto error_return; | |
4117 | ||
4118 | _bfd_stringtab_free (finfo.strtab); | |
4119 | ||
4120 | /* Write out the debugging string table. */ | |
4121 | o = xcoff_hash_table (info)->debug_section; | |
dc810e39 AM |
4122 | if (o != NULL) |
4123 | { | |
4124 | struct bfd_strtab_hash *debug_strtab; | |
4125 | ||
4126 | debug_strtab = xcoff_hash_table (info)->debug_strtab; | |
eea6121a | 4127 | BFD_ASSERT (o->output_section->size - o->output_offset |
dc810e39 AM |
4128 | >= _bfd_stringtab_size (debug_strtab)); |
4129 | pos = o->output_section->filepos + o->output_offset; | |
4130 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4131 | goto error_return; | |
4132 | if (! _bfd_stringtab_emit (abfd, debug_strtab)) | |
4133 | goto error_return; | |
4134 | } | |
4135 | ||
252b5132 RH |
4136 | /* Setting bfd_get_symcount to 0 will cause write_object_contents to |
4137 | not try to write out the symbols. */ | |
4138 | bfd_get_symcount (abfd) = 0; | |
dc810e39 | 4139 | |
b34976b6 | 4140 | return TRUE; |
252b5132 RH |
4141 | |
4142 | error_return: | |
4143 | if (finfo.strtab != NULL) | |
4144 | _bfd_stringtab_free (finfo.strtab); | |
dc810e39 AM |
4145 | |
4146 | if (finfo.section_info != NULL) | |
4147 | { | |
4148 | unsigned int i; | |
4149 | ||
4150 | for (i = 0; i < abfd->section_count; i++) | |
4151 | { | |
4152 | if (finfo.section_info[i].relocs != NULL) | |
4153 | free (finfo.section_info[i].relocs); | |
4154 | if (finfo.section_info[i].rel_hashes != NULL) | |
4155 | free (finfo.section_info[i].rel_hashes); | |
4156 | } | |
4157 | free (finfo.section_info); | |
252b5132 | 4158 | } |
dc810e39 | 4159 | |
252b5132 RH |
4160 | if (finfo.internal_syms != NULL) |
4161 | free (finfo.internal_syms); | |
4162 | if (finfo.sym_indices != NULL) | |
4163 | free (finfo.sym_indices); | |
4164 | if (finfo.outsyms != NULL) | |
4165 | free (finfo.outsyms); | |
4166 | if (finfo.linenos != NULL) | |
4167 | free (finfo.linenos); | |
4168 | if (finfo.contents != NULL) | |
4169 | free (finfo.contents); | |
4170 | if (finfo.external_relocs != NULL) | |
4171 | free (finfo.external_relocs); | |
4172 | if (external_relocs != NULL) | |
4173 | free (external_relocs); | |
b34976b6 | 4174 | return FALSE; |
252b5132 RH |
4175 | } |
4176 | ||
4177 | /* Link an input file into the linker output file. This function | |
4178 | handles all the sections and relocations of the input file at once. */ | |
4179 | ||
b34976b6 | 4180 | static bfd_boolean |
252b5132 RH |
4181 | xcoff_link_input_bfd (finfo, input_bfd) |
4182 | struct xcoff_final_link_info *finfo; | |
4183 | bfd *input_bfd; | |
4184 | { | |
4185 | bfd *output_bfd; | |
4186 | const char *strings; | |
4187 | bfd_size_type syment_base; | |
4188 | unsigned int n_tmask; | |
4189 | unsigned int n_btshft; | |
b34976b6 | 4190 | bfd_boolean copy, hash; |
252b5132 RH |
4191 | bfd_size_type isymesz; |
4192 | bfd_size_type osymesz; | |
4193 | bfd_size_type linesz; | |
4194 | bfd_byte *esym; | |
4195 | bfd_byte *esym_end; | |
4196 | struct xcoff_link_hash_entry **sym_hash; | |
4197 | struct internal_syment *isymp; | |
4198 | asection **csectpp; | |
4199 | unsigned long *debug_index; | |
4200 | long *indexp; | |
4201 | unsigned long output_index; | |
4202 | bfd_byte *outsym; | |
4203 | unsigned int incls; | |
4204 | asection *oline; | |
b34976b6 | 4205 | bfd_boolean keep_syms; |
252b5132 RH |
4206 | asection *o; |
4207 | ||
4208 | /* We can just skip DYNAMIC files, unless this is a static link. */ | |
4209 | if ((input_bfd->flags & DYNAMIC) != 0 | |
4210 | && ! finfo->info->static_link) | |
b34976b6 | 4211 | return TRUE; |
252b5132 RH |
4212 | |
4213 | /* Move all the symbols to the output file. */ | |
4214 | ||
4215 | output_bfd = finfo->output_bfd; | |
4216 | strings = NULL; | |
4217 | syment_base = obj_raw_syment_count (output_bfd); | |
4218 | isymesz = bfd_coff_symesz (input_bfd); | |
4219 | osymesz = bfd_coff_symesz (output_bfd); | |
4220 | linesz = bfd_coff_linesz (input_bfd); | |
4221 | BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd)); | |
4222 | ||
4223 | n_tmask = coff_data (input_bfd)->local_n_tmask; | |
4224 | n_btshft = coff_data (input_bfd)->local_n_btshft; | |
4225 | ||
4226 | /* Define macros so that ISFCN, et. al., macros work correctly. */ | |
4227 | #define N_TMASK n_tmask | |
4228 | #define N_BTSHFT n_btshft | |
4229 | ||
b34976b6 | 4230 | copy = FALSE; |
252b5132 | 4231 | if (! finfo->info->keep_memory) |
b34976b6 AM |
4232 | copy = TRUE; |
4233 | hash = TRUE; | |
252b5132 | 4234 | if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0) |
b34976b6 | 4235 | hash = FALSE; |
252b5132 RH |
4236 | |
4237 | if (! _bfd_coff_get_external_symbols (input_bfd)) | |
b34976b6 | 4238 | return FALSE; |
252b5132 RH |
4239 | |
4240 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); | |
4241 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
4242 | sym_hash = obj_xcoff_sym_hashes (input_bfd); | |
4243 | csectpp = xcoff_data (input_bfd)->csects; | |
4244 | debug_index = xcoff_data (input_bfd)->debug_indices; | |
4245 | isymp = finfo->internal_syms; | |
4246 | indexp = finfo->sym_indices; | |
4247 | output_index = syment_base; | |
4248 | outsym = finfo->outsyms; | |
4249 | incls = 0; | |
4250 | oline = NULL; | |
4251 | ||
4252 | while (esym < esym_end) | |
4253 | { | |
beb1bf64 | 4254 | |
252b5132 RH |
4255 | struct internal_syment isym; |
4256 | union internal_auxent aux; | |
4257 | int smtyp = 0; | |
b34976b6 AM |
4258 | bfd_boolean skip; |
4259 | bfd_boolean require; | |
252b5132 RH |
4260 | int add; |
4261 | ||
4262 | bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp); | |
4263 | ||
4264 | /* If this is a C_EXT or C_HIDEXT symbol, we need the csect | |
b34976b6 | 4265 | information. */ |
252b5132 RH |
4266 | if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT) |
4267 | { | |
4268 | BFD_ASSERT (isymp->n_numaux > 0); | |
4269 | bfd_coff_swap_aux_in (input_bfd, | |
4270 | (PTR) (esym + isymesz * isymp->n_numaux), | |
4271 | isymp->n_type, isymp->n_sclass, | |
4272 | isymp->n_numaux - 1, isymp->n_numaux, | |
4273 | (PTR) &aux); | |
beb1bf64 | 4274 | |
252b5132 RH |
4275 | smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); |
4276 | } | |
4277 | ||
4278 | /* Make a copy of *isymp so that the relocate_section function | |
4279 | always sees the original values. This is more reliable than | |
4280 | always recomputing the symbol value even if we are stripping | |
4281 | the symbol. */ | |
4282 | isym = *isymp; | |
4283 | ||
4284 | /* If this symbol is in the .loader section, swap out the | |
b34976b6 AM |
4285 | .loader symbol information. If this is an external symbol |
4286 | reference to a defined symbol, though, then wait until we get | |
4287 | to the definition. */ | |
252b5132 RH |
4288 | if (isym.n_sclass == C_EXT |
4289 | && *sym_hash != NULL | |
4290 | && (*sym_hash)->ldsym != NULL | |
4291 | && (smtyp != XTY_ER | |
4292 | || (*sym_hash)->root.type == bfd_link_hash_undefined)) | |
4293 | { | |
4294 | struct xcoff_link_hash_entry *h; | |
4295 | struct internal_ldsym *ldsym; | |
4296 | ||
4297 | h = *sym_hash; | |
4298 | ldsym = h->ldsym; | |
4299 | if (isym.n_scnum > 0) | |
4300 | { | |
4301 | ldsym->l_scnum = (*csectpp)->output_section->target_index; | |
4302 | ldsym->l_value = (isym.n_value | |
4303 | + (*csectpp)->output_section->vma | |
4304 | + (*csectpp)->output_offset | |
4305 | - (*csectpp)->vma); | |
4306 | } | |
4307 | else | |
4308 | { | |
4309 | ldsym->l_scnum = isym.n_scnum; | |
4310 | ldsym->l_value = isym.n_value; | |
4311 | } | |
4312 | ||
4313 | ldsym->l_smtype = smtyp; | |
4314 | if (((h->flags & XCOFF_DEF_REGULAR) == 0 | |
4315 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
4316 | || (h->flags & XCOFF_IMPORT) != 0) | |
4317 | ldsym->l_smtype |= L_IMPORT; | |
4318 | if (((h->flags & XCOFF_DEF_REGULAR) != 0 | |
4319 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
4320 | || (h->flags & XCOFF_EXPORT) != 0) | |
4321 | ldsym->l_smtype |= L_EXPORT; | |
4322 | if ((h->flags & XCOFF_ENTRY) != 0) | |
4323 | ldsym->l_smtype |= L_ENTRY; | |
4324 | ||
4325 | ldsym->l_smclas = aux.x_csect.x_smclas; | |
4326 | ||
4327 | if (ldsym->l_ifile == (bfd_size_type) -1) | |
4328 | ldsym->l_ifile = 0; | |
4329 | else if (ldsym->l_ifile == 0) | |
4330 | { | |
4331 | if ((ldsym->l_smtype & L_IMPORT) == 0) | |
4332 | ldsym->l_ifile = 0; | |
4333 | else | |
4334 | { | |
4335 | bfd *impbfd; | |
4336 | ||
4337 | if (h->root.type == bfd_link_hash_defined | |
4338 | || h->root.type == bfd_link_hash_defweak) | |
4339 | impbfd = h->root.u.def.section->owner; | |
4340 | else if (h->root.type == bfd_link_hash_undefined | |
4341 | || h->root.type == bfd_link_hash_undefweak) | |
4342 | impbfd = h->root.u.undef.abfd; | |
4343 | else | |
4344 | impbfd = NULL; | |
4345 | ||
4346 | if (impbfd == NULL) | |
4347 | ldsym->l_ifile = 0; | |
4348 | else | |
4349 | { | |
4350 | BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec); | |
4351 | ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; | |
4352 | } | |
4353 | } | |
4354 | } | |
4355 | ||
4356 | ldsym->l_parm = 0; | |
4357 | ||
4358 | BFD_ASSERT (h->ldindx >= 0); | |
beb1bf64 | 4359 | bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym, |
dc810e39 AM |
4360 | (finfo->ldsym |
4361 | + ((h->ldindx - 3) | |
4362 | * bfd_xcoff_ldsymsz (finfo->output_bfd)))); | |
252b5132 RH |
4363 | h->ldsym = NULL; |
4364 | ||
4365 | /* Fill in snentry now that we know the target_index. */ | |
dc810e39 AM |
4366 | if ((h->flags & XCOFF_ENTRY) != 0 |
4367 | && (h->root.type == bfd_link_hash_defined | |
4368 | || h->root.type == bfd_link_hash_defweak)) | |
4369 | { | |
4370 | xcoff_data (output_bfd)->snentry = | |
4371 | h->root.u.def.section->output_section->target_index; | |
4372 | } | |
252b5132 RH |
4373 | } |
4374 | ||
4375 | *indexp = -1; | |
4376 | ||
b34976b6 AM |
4377 | skip = FALSE; |
4378 | require = FALSE; | |
252b5132 RH |
4379 | add = 1 + isym.n_numaux; |
4380 | ||
4381 | /* If we are skipping this csect, we want to skip this symbol. */ | |
4382 | if (*csectpp == NULL) | |
b34976b6 | 4383 | skip = TRUE; |
252b5132 RH |
4384 | |
4385 | /* If we garbage collected this csect, we want to skip this | |
b34976b6 | 4386 | symbol. */ |
252b5132 RH |
4387 | if (! skip |
4388 | && xcoff_hash_table (finfo->info)->gc | |
4389 | && ((*csectpp)->flags & SEC_MARK) == 0 | |
4390 | && *csectpp != bfd_abs_section_ptr) | |
b34976b6 | 4391 | skip = TRUE; |
252b5132 RH |
4392 | |
4393 | /* An XCOFF linker always skips C_STAT symbols. */ | |
4394 | if (! skip | |
4395 | && isymp->n_sclass == C_STAT) | |
b34976b6 | 4396 | skip = TRUE; |
252b5132 RH |
4397 | |
4398 | /* We skip all but the first TOC anchor. */ | |
4399 | if (! skip | |
4400 | && isymp->n_sclass == C_HIDEXT | |
4401 | && aux.x_csect.x_smclas == XMC_TC0) | |
4402 | { | |
4403 | if (finfo->toc_symindx != -1) | |
b34976b6 | 4404 | skip = TRUE; |
252b5132 RH |
4405 | else |
4406 | { | |
4407 | bfd_vma tocval, tocend; | |
492055e6 | 4408 | bfd *inp; |
252b5132 RH |
4409 | |
4410 | tocval = ((*csectpp)->output_section->vma | |
4411 | + (*csectpp)->output_offset | |
4412 | + isym.n_value | |
4413 | - (*csectpp)->vma); | |
492055e6 | 4414 | |
252b5132 | 4415 | /* We want to find out if tocval is a good value to use |
b34976b6 AM |
4416 | as the TOC anchor--that is, whether we can access all |
4417 | of the TOC using a 16 bit offset from tocval. This | |
4418 | test assumes that the TOC comes at the end of the | |
4419 | output section, as it does in the default linker | |
4420 | script. */ | |
252b5132 | 4421 | tocend = ((*csectpp)->output_section->vma |
eea6121a | 4422 | + (*csectpp)->output_section->size); |
fbc4fff4 KH |
4423 | for (inp = finfo->info->input_bfds; |
4424 | inp != NULL; | |
492055e6 GK |
4425 | inp = inp->link_next) |
4426 | { | |
fbc4fff4 | 4427 | |
492055e6 GK |
4428 | for (o = inp->sections; o != NULL; o = o->next) |
4429 | if (strcmp (o->name, ".tocbss") == 0) | |
4430 | { | |
4431 | bfd_vma new_toc_end; | |
4432 | new_toc_end = (o->output_section->vma | |
4433 | + o->output_offset | |
eea6121a | 4434 | + o->size); |
492055e6 GK |
4435 | if (new_toc_end > tocend) |
4436 | tocend = new_toc_end; | |
4437 | } | |
fbc4fff4 | 4438 | |
492055e6 | 4439 | } |
fbc4fff4 | 4440 | |
252b5132 RH |
4441 | if (tocval + 0x10000 < tocend) |
4442 | { | |
4443 | (*_bfd_error_handler) | |
4444 | (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"), | |
4445 | (unsigned long) (tocend - tocval)); | |
4446 | bfd_set_error (bfd_error_file_too_big); | |
b34976b6 | 4447 | return FALSE; |
252b5132 RH |
4448 | } |
4449 | ||
4450 | if (tocval + 0x8000 < tocend) | |
4451 | { | |
4452 | bfd_vma tocadd; | |
4453 | ||
4454 | tocadd = tocend - (tocval + 0x8000); | |
4455 | tocval += tocadd; | |
4456 | isym.n_value += tocadd; | |
4457 | } | |
4458 | ||
4459 | finfo->toc_symindx = output_index; | |
4460 | xcoff_data (finfo->output_bfd)->toc = tocval; | |
4461 | xcoff_data (finfo->output_bfd)->sntoc = | |
4462 | (*csectpp)->output_section->target_index; | |
b34976b6 | 4463 | require = TRUE; |
dc810e39 | 4464 | |
252b5132 RH |
4465 | } |
4466 | } | |
4467 | ||
4468 | /* If we are stripping all symbols, we want to skip this one. */ | |
4469 | if (! skip | |
4470 | && finfo->info->strip == strip_all) | |
b34976b6 | 4471 | skip = TRUE; |
252b5132 RH |
4472 | |
4473 | /* We can skip resolved external references. */ | |
4474 | if (! skip | |
4475 | && isym.n_sclass == C_EXT | |
4476 | && smtyp == XTY_ER | |
4477 | && (*sym_hash)->root.type != bfd_link_hash_undefined) | |
b34976b6 | 4478 | skip = TRUE; |
252b5132 RH |
4479 | |
4480 | /* We can skip common symbols if they got defined somewhere | |
b34976b6 | 4481 | else. */ |
252b5132 RH |
4482 | if (! skip |
4483 | && isym.n_sclass == C_EXT | |
4484 | && smtyp == XTY_CM | |
4485 | && ((*sym_hash)->root.type != bfd_link_hash_common | |
4486 | || (*sym_hash)->root.u.c.p->section != *csectpp) | |
4487 | && ((*sym_hash)->root.type != bfd_link_hash_defined | |
4488 | || (*sym_hash)->root.u.def.section != *csectpp)) | |
b34976b6 | 4489 | skip = TRUE; |
252b5132 RH |
4490 | |
4491 | /* Skip local symbols if we are discarding them. */ | |
4492 | if (! skip | |
4493 | && finfo->info->discard == discard_all | |
4494 | && isym.n_sclass != C_EXT | |
4495 | && (isym.n_sclass != C_HIDEXT | |
4496 | || smtyp != XTY_SD)) | |
b34976b6 | 4497 | skip = TRUE; |
252b5132 RH |
4498 | |
4499 | /* If we stripping debugging symbols, and this is a debugging | |
b34976b6 | 4500 | symbol, then skip it. */ |
252b5132 RH |
4501 | if (! skip |
4502 | && finfo->info->strip == strip_debugger | |
4503 | && isym.n_scnum == N_DEBUG) | |
b34976b6 | 4504 | skip = TRUE; |
252b5132 RH |
4505 | |
4506 | /* If some symbols are stripped based on the name, work out the | |
4507 | name and decide whether to skip this symbol. We don't handle | |
4508 | this correctly for symbols whose names are in the .debug | |
4509 | section; to get it right we would need a new bfd_strtab_hash | |
4510 | function to return the string given the index. */ | |
4511 | if (! skip | |
4512 | && (finfo->info->strip == strip_some | |
4513 | || finfo->info->discard == discard_l) | |
4514 | && (debug_index == NULL || *debug_index == (unsigned long) -1)) | |
4515 | { | |
4516 | const char *name; | |
4517 | char buf[SYMNMLEN + 1]; | |
4518 | ||
4519 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf); | |
beb1bf64 | 4520 | |
252b5132 | 4521 | if (name == NULL) |
b34976b6 | 4522 | return FALSE; |
252b5132 RH |
4523 | |
4524 | if ((finfo->info->strip == strip_some | |
b34976b6 AM |
4525 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, |
4526 | FALSE) == NULL)) | |
252b5132 RH |
4527 | || (finfo->info->discard == discard_l |
4528 | && (isym.n_sclass != C_EXT | |
4529 | && (isym.n_sclass != C_HIDEXT | |
4530 | || smtyp != XTY_SD)) | |
4531 | && bfd_is_local_label_name (input_bfd, name))) | |
b34976b6 | 4532 | skip = TRUE; |
252b5132 RH |
4533 | } |
4534 | ||
4535 | /* We can not skip the first TOC anchor. */ | |
4536 | if (skip | |
4537 | && require | |
4538 | && finfo->info->strip != strip_all) | |
b34976b6 | 4539 | skip = FALSE; |
252b5132 RH |
4540 | |
4541 | /* We now know whether we are to skip this symbol or not. */ | |
4542 | if (! skip) | |
4543 | { | |
4544 | /* Adjust the symbol in order to output it. */ | |
4545 | ||
4546 | if (isym._n._n_n._n_zeroes == 0 | |
4547 | && isym._n._n_n._n_offset != 0) | |
4548 | { | |
4549 | /* This symbol has a long name. Enter it in the string | |
4550 | table we are building. If *debug_index != -1, the | |
4551 | name has already been entered in the .debug section. */ | |
4552 | if (debug_index != NULL && *debug_index != (unsigned long) -1) | |
4553 | isym._n._n_n._n_offset = *debug_index; | |
4554 | else | |
4555 | { | |
4556 | const char *name; | |
4557 | bfd_size_type indx; | |
4558 | ||
4559 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, | |
4560 | (char *) NULL); | |
beb1bf64 | 4561 | |
252b5132 | 4562 | if (name == NULL) |
b34976b6 | 4563 | return FALSE; |
252b5132 RH |
4564 | indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy); |
4565 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 4566 | return FALSE; |
252b5132 RH |
4567 | isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; |
4568 | } | |
4569 | } | |
4570 | ||
4571 | if (isym.n_sclass != C_BSTAT | |
4572 | && isym.n_sclass != C_ESTAT | |
4573 | && isym.n_sclass != C_DECL | |
4574 | && isym.n_scnum > 0) | |
4575 | { | |
4576 | isym.n_scnum = (*csectpp)->output_section->target_index; | |
4577 | isym.n_value += ((*csectpp)->output_section->vma | |
4578 | + (*csectpp)->output_offset | |
4579 | - (*csectpp)->vma); | |
4580 | } | |
4581 | ||
4582 | /* The value of a C_FILE symbol is the symbol index of the | |
4583 | next C_FILE symbol. The value of the last C_FILE symbol | |
4584 | is -1. We try to get this right, below, just before we | |
4585 | write the symbols out, but in the general case we may | |
4586 | have to write the symbol out twice. */ | |
4587 | if (isym.n_sclass == C_FILE) | |
4588 | { | |
4589 | if (finfo->last_file_index != -1 | |
beb1bf64 | 4590 | && finfo->last_file.n_value != (bfd_vma) output_index) |
252b5132 RH |
4591 | { |
4592 | /* We must correct the value of the last C_FILE entry. */ | |
4593 | finfo->last_file.n_value = output_index; | |
4594 | if ((bfd_size_type) finfo->last_file_index >= syment_base) | |
4595 | { | |
4596 | /* The last C_FILE symbol is in this input file. */ | |
4597 | bfd_coff_swap_sym_out (output_bfd, | |
4598 | (PTR) &finfo->last_file, | |
4599 | (PTR) (finfo->outsyms | |
4600 | + ((finfo->last_file_index | |
4601 | - syment_base) | |
4602 | * osymesz))); | |
4603 | } | |
4604 | else | |
4605 | { | |
4606 | /* We have already written out the last C_FILE | |
4607 | symbol. We need to write it out again. We | |
4608 | borrow *outsym temporarily. */ | |
dc810e39 AM |
4609 | file_ptr pos; |
4610 | ||
252b5132 RH |
4611 | bfd_coff_swap_sym_out (output_bfd, |
4612 | (PTR) &finfo->last_file, | |
4613 | (PTR) outsym); | |
dc810e39 AM |
4614 | |
4615 | pos = obj_sym_filepos (output_bfd); | |
4616 | pos += finfo->last_file_index * osymesz; | |
4617 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 | |
4618 | || (bfd_bwrite (outsym, osymesz, output_bfd) | |
252b5132 | 4619 | != osymesz)) |
b34976b6 | 4620 | return FALSE; |
252b5132 RH |
4621 | } |
4622 | } | |
4623 | ||
4624 | finfo->last_file_index = output_index; | |
4625 | finfo->last_file = isym; | |
4626 | } | |
4627 | ||
4628 | /* The value of a C_BINCL or C_EINCL symbol is a file offset | |
b34976b6 AM |
4629 | into the line numbers. We update the symbol values when |
4630 | we handle the line numbers. */ | |
252b5132 RH |
4631 | if (isym.n_sclass == C_BINCL |
4632 | || isym.n_sclass == C_EINCL) | |
4633 | { | |
4634 | isym.n_value = finfo->line_filepos; | |
4635 | ++incls; | |
4636 | } | |
4637 | ||
4638 | /* Output the symbol. */ | |
4639 | ||
4640 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym); | |
4641 | ||
4642 | *indexp = output_index; | |
4643 | ||
4644 | if (isym.n_sclass == C_EXT) | |
4645 | { | |
4646 | long indx; | |
4647 | struct xcoff_link_hash_entry *h; | |
4648 | ||
4649 | indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd)) | |
4650 | / isymesz); | |
4651 | h = obj_xcoff_sym_hashes (input_bfd)[indx]; | |
4652 | BFD_ASSERT (h != NULL); | |
4653 | h->indx = output_index; | |
4654 | } | |
4655 | ||
4656 | /* If this is a symbol in the TOC which we may have merged | |
b34976b6 AM |
4657 | (class XMC_TC), remember the symbol index of the TOC |
4658 | symbol. */ | |
252b5132 RH |
4659 | if (isym.n_sclass == C_HIDEXT |
4660 | && aux.x_csect.x_smclas == XMC_TC | |
4661 | && *sym_hash != NULL) | |
4662 | { | |
4663 | BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0); | |
4664 | BFD_ASSERT ((*sym_hash)->toc_section != NULL); | |
4665 | (*sym_hash)->u.toc_indx = output_index; | |
4666 | } | |
4667 | ||
4668 | output_index += add; | |
4669 | outsym += add * osymesz; | |
4670 | } | |
4671 | ||
4672 | esym += add * isymesz; | |
4673 | isymp += add; | |
4674 | csectpp += add; | |
4675 | sym_hash += add; | |
4676 | if (debug_index != NULL) | |
4677 | debug_index += add; | |
4678 | ++indexp; | |
4679 | for (--add; add > 0; --add) | |
4680 | *indexp++ = -1; | |
4681 | } | |
4682 | ||
4683 | /* Fix up the aux entries and the C_BSTAT symbols. This must be | |
4684 | done in a separate pass, because we don't know the correct symbol | |
4685 | indices until we have already decided which symbols we are going | |
4686 | to keep. */ | |
4687 | ||
4688 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); | |
4689 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
4690 | isymp = finfo->internal_syms; | |
4691 | indexp = finfo->sym_indices; | |
4692 | csectpp = xcoff_data (input_bfd)->csects; | |
4693 | outsym = finfo->outsyms; | |
4694 | while (esym < esym_end) | |
4695 | { | |
4696 | int add; | |
4697 | ||
4698 | add = 1 + isymp->n_numaux; | |
4699 | ||
4700 | if (*indexp < 0) | |
4701 | esym += add * isymesz; | |
4702 | else | |
4703 | { | |
4704 | int i; | |
4705 | ||
4706 | if (isymp->n_sclass == C_BSTAT) | |
4707 | { | |
4708 | struct internal_syment isym; | |
beb1bf64 TR |
4709 | |
4710 | bfd_vma indx; | |
252b5132 RH |
4711 | |
4712 | /* The value of a C_BSTAT symbol is the symbol table | |
b34976b6 | 4713 | index of the containing csect. */ |
252b5132 RH |
4714 | bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym); |
4715 | indx = isym.n_value; | |
4716 | if (indx < obj_raw_syment_count (input_bfd)) | |
4717 | { | |
4718 | long symindx; | |
4719 | ||
4720 | symindx = finfo->sym_indices[indx]; | |
4721 | if (symindx < 0) | |
4722 | isym.n_value = 0; | |
4723 | else | |
4724 | isym.n_value = symindx; | |
4725 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, | |
4726 | (PTR) outsym); | |
4727 | } | |
4728 | } | |
4729 | ||
4730 | esym += isymesz; | |
4731 | outsym += osymesz; | |
4732 | ||
4733 | for (i = 0; i < isymp->n_numaux && esym < esym_end; i++) | |
4734 | { | |
4735 | union internal_auxent aux; | |
4736 | ||
4737 | bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type, | |
4738 | isymp->n_sclass, i, isymp->n_numaux, | |
4739 | (PTR) &aux); | |
4740 | ||
4741 | if (isymp->n_sclass == C_FILE) | |
4742 | { | |
4743 | /* This is the file name (or some comment put in by | |
4744 | the compiler). If it is long, we must put it in | |
4745 | the string table. */ | |
4746 | if (aux.x_file.x_n.x_zeroes == 0 | |
4747 | && aux.x_file.x_n.x_offset != 0) | |
4748 | { | |
4749 | const char *filename; | |
4750 | bfd_size_type indx; | |
4751 | ||
4752 | BFD_ASSERT (aux.x_file.x_n.x_offset | |
4753 | >= STRING_SIZE_SIZE); | |
4754 | if (strings == NULL) | |
4755 | { | |
4756 | strings = _bfd_coff_read_string_table (input_bfd); | |
4757 | if (strings == NULL) | |
b34976b6 | 4758 | return FALSE; |
252b5132 RH |
4759 | } |
4760 | filename = strings + aux.x_file.x_n.x_offset; | |
4761 | indx = _bfd_stringtab_add (finfo->strtab, filename, | |
4762 | hash, copy); | |
4763 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 4764 | return FALSE; |
252b5132 RH |
4765 | aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx; |
4766 | } | |
4767 | } | |
4768 | else if ((isymp->n_sclass == C_EXT | |
4769 | || isymp->n_sclass == C_HIDEXT) | |
4770 | && i + 1 == isymp->n_numaux) | |
4771 | { | |
beb1bf64 | 4772 | |
252b5132 | 4773 | /* We don't support type checking. I don't know if |
b34976b6 | 4774 | anybody does. */ |
252b5132 RH |
4775 | aux.x_csect.x_parmhash = 0; |
4776 | /* I don't think anybody uses these fields, but we'd | |
b34976b6 | 4777 | better clobber them just in case. */ |
252b5132 RH |
4778 | aux.x_csect.x_stab = 0; |
4779 | aux.x_csect.x_snstab = 0; | |
beb1bf64 | 4780 | |
252b5132 RH |
4781 | if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD) |
4782 | { | |
dc810e39 | 4783 | unsigned long indx; |
252b5132 | 4784 | |
dc810e39 | 4785 | indx = aux.x_csect.x_scnlen.l; |
252b5132 RH |
4786 | if (indx < obj_raw_syment_count (input_bfd)) |
4787 | { | |
4788 | long symindx; | |
4789 | ||
4790 | symindx = finfo->sym_indices[indx]; | |
dc810e39 AM |
4791 | if (symindx < 0) |
4792 | { | |
4793 | aux.x_csect.x_scnlen.l = 0; | |
4794 | } | |
4795 | else | |
4796 | { | |
4797 | aux.x_csect.x_scnlen.l = symindx; | |
4798 | } | |
252b5132 RH |
4799 | } |
4800 | } | |
4801 | } | |
4802 | else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL) | |
4803 | { | |
4804 | unsigned long indx; | |
4805 | ||
4806 | if (ISFCN (isymp->n_type) | |
4807 | || ISTAG (isymp->n_sclass) | |
4808 | || isymp->n_sclass == C_BLOCK | |
4809 | || isymp->n_sclass == C_FCN) | |
4810 | { | |
4811 | indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l; | |
4812 | if (indx > 0 | |
4813 | && indx < obj_raw_syment_count (input_bfd)) | |
4814 | { | |
4815 | /* We look forward through the symbol for | |
b34976b6 AM |
4816 | the index of the next symbol we are going |
4817 | to include. I don't know if this is | |
4818 | entirely right. */ | |
252b5132 RH |
4819 | while (finfo->sym_indices[indx] < 0 |
4820 | && indx < obj_raw_syment_count (input_bfd)) | |
4821 | ++indx; | |
4822 | if (indx >= obj_raw_syment_count (input_bfd)) | |
4823 | indx = output_index; | |
4824 | else | |
4825 | indx = finfo->sym_indices[indx]; | |
4826 | aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx; | |
beb1bf64 | 4827 | |
252b5132 RH |
4828 | } |
4829 | } | |
4830 | ||
4831 | indx = aux.x_sym.x_tagndx.l; | |
4832 | if (indx > 0 && indx < obj_raw_syment_count (input_bfd)) | |
4833 | { | |
4834 | long symindx; | |
4835 | ||
4836 | symindx = finfo->sym_indices[indx]; | |
4837 | if (symindx < 0) | |
4838 | aux.x_sym.x_tagndx.l = 0; | |
4839 | else | |
4840 | aux.x_sym.x_tagndx.l = symindx; | |
4841 | } | |
beb1bf64 | 4842 | |
252b5132 RH |
4843 | } |
4844 | ||
4845 | /* Copy over the line numbers, unless we are stripping | |
4846 | them. We do this on a symbol by symbol basis in | |
4847 | order to more easily handle garbage collection. */ | |
4848 | if ((isymp->n_sclass == C_EXT | |
4849 | || isymp->n_sclass == C_HIDEXT) | |
4850 | && i == 0 | |
4851 | && isymp->n_numaux > 1 | |
4852 | && ISFCN (isymp->n_type) | |
4853 | && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) | |
4854 | { | |
4855 | if (finfo->info->strip != strip_none | |
4856 | && finfo->info->strip != strip_some) | |
4857 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0; | |
4858 | else | |
4859 | { | |
4860 | asection *enclosing; | |
4861 | unsigned int enc_count; | |
beb1bf64 | 4862 | bfd_signed_vma linoff; |
252b5132 RH |
4863 | struct internal_lineno lin; |
4864 | ||
4865 | o = *csectpp; | |
4866 | enclosing = xcoff_section_data (abfd, o)->enclosing; | |
4867 | enc_count = xcoff_section_data (abfd, o)->lineno_count; | |
4868 | if (oline != enclosing) | |
4869 | { | |
dc810e39 AM |
4870 | file_ptr pos = enclosing->line_filepos; |
4871 | bfd_size_type amt = linesz * enc_count; | |
4872 | if (bfd_seek (input_bfd, pos, SEEK_SET) != 0 | |
4873 | || (bfd_bread (finfo->linenos, amt, input_bfd) | |
4874 | != amt)) | |
b34976b6 | 4875 | return FALSE; |
252b5132 RH |
4876 | oline = enclosing; |
4877 | } | |
4878 | ||
4879 | linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr | |
4880 | - enclosing->line_filepos); | |
4881 | ||
4882 | bfd_coff_swap_lineno_in (input_bfd, | |
4883 | (PTR) (finfo->linenos + linoff), | |
4884 | (PTR) &lin); | |
4885 | if (lin.l_lnno != 0 | |
4886 | || ((bfd_size_type) lin.l_addr.l_symndx | |
4887 | != ((esym | |
4888 | - isymesz | |
4889 | - ((bfd_byte *) | |
4890 | obj_coff_external_syms (input_bfd))) | |
4891 | / isymesz))) | |
4892 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0; | |
4893 | else | |
4894 | { | |
4895 | bfd_byte *linpend, *linp; | |
4896 | bfd_vma offset; | |
4897 | bfd_size_type count; | |
4898 | ||
4899 | lin.l_addr.l_symndx = *indexp; | |
4900 | bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin, | |
4901 | (PTR) (finfo->linenos | |
4902 | + linoff)); | |
4903 | ||
4904 | linpend = (finfo->linenos | |
4905 | + enc_count * linesz); | |
4906 | offset = (o->output_section->vma | |
4907 | + o->output_offset | |
4908 | - o->vma); | |
4909 | for (linp = finfo->linenos + linoff + linesz; | |
4910 | linp < linpend; | |
4911 | linp += linesz) | |
4912 | { | |
4913 | bfd_coff_swap_lineno_in (input_bfd, (PTR) linp, | |
4914 | (PTR) &lin); | |
4915 | if (lin.l_lnno == 0) | |
4916 | break; | |
4917 | lin.l_addr.l_paddr += offset; | |
4918 | bfd_coff_swap_lineno_out (output_bfd, | |
4919 | (PTR) &lin, | |
4920 | (PTR) linp); | |
4921 | } | |
4922 | ||
4923 | count = (linp - (finfo->linenos + linoff)) / linesz; | |
4924 | ||
4925 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = | |
4926 | (o->output_section->line_filepos | |
4927 | + o->output_section->lineno_count * linesz); | |
4928 | ||
4929 | if (bfd_seek (output_bfd, | |
4930 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr, | |
4931 | SEEK_SET) != 0 | |
dc810e39 AM |
4932 | || (bfd_bwrite (finfo->linenos + linoff, |
4933 | linesz * count, output_bfd) | |
252b5132 | 4934 | != linesz * count)) |
b34976b6 | 4935 | return FALSE; |
252b5132 RH |
4936 | |
4937 | o->output_section->lineno_count += count; | |
4938 | ||
4939 | if (incls > 0) | |
4940 | { | |
4941 | struct internal_syment *iisp, *iispend; | |
4942 | long *iindp; | |
4943 | bfd_byte *oos; | |
4944 | int iiadd; | |
4945 | ||
4946 | /* Update any C_BINCL or C_EINCL symbols | |
b34976b6 AM |
4947 | that refer to a line number in the |
4948 | range we just output. */ | |
252b5132 RH |
4949 | iisp = finfo->internal_syms; |
4950 | iispend = (iisp | |
4951 | + obj_raw_syment_count (input_bfd)); | |
4952 | iindp = finfo->sym_indices; | |
4953 | oos = finfo->outsyms; | |
4954 | while (iisp < iispend) | |
4955 | { | |
4956 | if (*iindp >= 0 | |
4957 | && (iisp->n_sclass == C_BINCL | |
4958 | || iisp->n_sclass == C_EINCL) | |
4959 | && ((bfd_size_type) iisp->n_value | |
beb1bf64 | 4960 | >= (bfd_size_type)(enclosing->line_filepos + linoff)) |
252b5132 RH |
4961 | && ((bfd_size_type) iisp->n_value |
4962 | < (enclosing->line_filepos | |
4963 | + enc_count * linesz))) | |
4964 | { | |
4965 | struct internal_syment iis; | |
4966 | ||
4967 | bfd_coff_swap_sym_in (output_bfd, | |
4968 | (PTR) oos, | |
4969 | (PTR) &iis); | |
4970 | iis.n_value = | |
4971 | (iisp->n_value | |
4972 | - enclosing->line_filepos | |
4973 | - linoff | |
4974 | + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr); | |
4975 | bfd_coff_swap_sym_out (output_bfd, | |
4976 | (PTR) &iis, | |
4977 | (PTR) oos); | |
4978 | --incls; | |
4979 | } | |
4980 | ||
4981 | iiadd = 1 + iisp->n_numaux; | |
4982 | if (*iindp >= 0) | |
4983 | oos += iiadd * osymesz; | |
4984 | iisp += iiadd; | |
4985 | iindp += iiadd; | |
4986 | } | |
4987 | } | |
4988 | } | |
4989 | } | |
4990 | } | |
4991 | ||
4992 | bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type, | |
4993 | isymp->n_sclass, i, isymp->n_numaux, | |
4994 | (PTR) outsym); | |
4995 | outsym += osymesz; | |
4996 | esym += isymesz; | |
4997 | } | |
4998 | } | |
4999 | ||
5000 | indexp += add; | |
5001 | isymp += add; | |
5002 | csectpp += add; | |
5003 | } | |
5004 | ||
5005 | /* If we swapped out a C_FILE symbol, guess that the next C_FILE | |
5006 | symbol will be the first symbol in the next input file. In the | |
5007 | normal case, this will save us from writing out the C_FILE symbol | |
5008 | again. */ | |
5009 | if (finfo->last_file_index != -1 | |
5010 | && (bfd_size_type) finfo->last_file_index >= syment_base) | |
5011 | { | |
5012 | finfo->last_file.n_value = output_index; | |
5013 | bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file, | |
5014 | (PTR) (finfo->outsyms | |
5015 | + ((finfo->last_file_index - syment_base) | |
5016 | * osymesz))); | |
5017 | } | |
5018 | ||
5019 | /* Write the modified symbols to the output file. */ | |
5020 | if (outsym > finfo->outsyms) | |
5021 | { | |
dc810e39 AM |
5022 | file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz; |
5023 | bfd_size_type amt = outsym - finfo->outsyms; | |
5024 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 | |
5025 | || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt) | |
b34976b6 | 5026 | return FALSE; |
252b5132 RH |
5027 | |
5028 | BFD_ASSERT ((obj_raw_syment_count (output_bfd) | |
5029 | + (outsym - finfo->outsyms) / osymesz) | |
5030 | == output_index); | |
5031 | ||
5032 | obj_raw_syment_count (output_bfd) = output_index; | |
5033 | } | |
5034 | ||
5035 | /* Don't let the linker relocation routines discard the symbols. */ | |
5036 | keep_syms = obj_coff_keep_syms (input_bfd); | |
b34976b6 | 5037 | obj_coff_keep_syms (input_bfd) = TRUE; |
252b5132 RH |
5038 | |
5039 | /* Relocate the contents of each section. */ | |
dc810e39 AM |
5040 | for (o = input_bfd->sections; o != NULL; o = o->next) |
5041 | { | |
252b5132 | 5042 | |
dc810e39 | 5043 | bfd_byte *contents; |
252b5132 | 5044 | |
dc810e39 AM |
5045 | if (! o->linker_mark) |
5046 | { | |
5047 | /* This section was omitted from the link. */ | |
5048 | continue; | |
5049 | } | |
252b5132 | 5050 | |
dc810e39 | 5051 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 5052 | || o->size == 0 |
dc810e39 AM |
5053 | || (o->flags & SEC_IN_MEMORY) != 0) |
5054 | continue; | |
252b5132 | 5055 | |
dc810e39 AM |
5056 | /* We have set filepos correctly for the sections we created to |
5057 | represent csects, so bfd_get_section_contents should work. */ | |
5058 | if (coff_section_data (input_bfd, o) != NULL | |
5059 | && coff_section_data (input_bfd, o)->contents != NULL) | |
5060 | contents = coff_section_data (input_bfd, o)->contents; | |
eea6121a AM |
5061 | else |
5062 | { | |
5063 | bfd_size_type sz = o->rawsize ? o->rawsize : o->size; | |
5064 | if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz)) | |
5065 | return FALSE; | |
5066 | contents = finfo->contents; | |
5067 | } | |
252b5132 | 5068 | |
dc810e39 AM |
5069 | if ((o->flags & SEC_RELOC) != 0) |
5070 | { | |
5071 | int target_index; | |
5072 | struct internal_reloc *internal_relocs; | |
5073 | struct internal_reloc *irel; | |
5074 | bfd_vma offset; | |
5075 | struct internal_reloc *irelend; | |
5076 | struct xcoff_link_hash_entry **rel_hash; | |
5077 | long r_symndx; | |
5078 | ||
5079 | /* Read in the relocs. */ | |
5080 | target_index = o->output_section->target_index; | |
5081 | internal_relocs = (xcoff_read_internal_relocs | |
b34976b6 AM |
5082 | (input_bfd, o, FALSE, finfo->external_relocs, |
5083 | TRUE, | |
dc810e39 AM |
5084 | (finfo->section_info[target_index].relocs |
5085 | + o->output_section->reloc_count))); | |
5086 | if (internal_relocs == NULL) | |
b34976b6 | 5087 | return FALSE; |
252b5132 | 5088 | |
dc810e39 AM |
5089 | /* Call processor specific code to relocate the section |
5090 | contents. */ | |
5091 | if (! bfd_coff_relocate_section (output_bfd, finfo->info, | |
5092 | input_bfd, o, | |
5093 | contents, | |
5094 | internal_relocs, | |
5095 | finfo->internal_syms, | |
5096 | xcoff_data (input_bfd)->csects)) | |
b34976b6 | 5097 | return FALSE; |
252b5132 | 5098 | |
dc810e39 AM |
5099 | offset = o->output_section->vma + o->output_offset - o->vma; |
5100 | irel = internal_relocs; | |
5101 | irelend = irel + o->reloc_count; | |
5102 | rel_hash = (finfo->section_info[target_index].rel_hashes | |
5103 | + o->output_section->reloc_count); | |
5104 | for (; irel < irelend; irel++, rel_hash++) | |
5105 | { | |
5106 | struct xcoff_link_hash_entry *h = NULL; | |
5107 | struct internal_ldrel ldrel; | |
b34976b6 | 5108 | bfd_boolean quiet; |
252b5132 | 5109 | |
dc810e39 | 5110 | *rel_hash = NULL; |
252b5132 | 5111 | |
dc810e39 | 5112 | /* Adjust the reloc address and symbol index. */ |
252b5132 | 5113 | |
dc810e39 | 5114 | irel->r_vaddr += offset; |
252b5132 | 5115 | |
dc810e39 | 5116 | r_symndx = irel->r_symndx; |
252b5132 | 5117 | |
dc810e39 AM |
5118 | if (r_symndx == -1) |
5119 | h = NULL; | |
5120 | else | |
5121 | h = obj_xcoff_sym_hashes (input_bfd)[r_symndx]; | |
252b5132 | 5122 | |
dc810e39 AM |
5123 | if (r_symndx != -1 && finfo->info->strip != strip_all) |
5124 | { | |
5125 | if (h != NULL | |
5126 | && h->smclas != XMC_TD | |
5127 | && (irel->r_type == R_TOC | |
5128 | || irel->r_type == R_GL | |
5129 | || irel->r_type == R_TCL | |
5130 | || irel->r_type == R_TRL | |
5131 | || irel->r_type == R_TRLA)) | |
5132 | { | |
5133 | /* This is a TOC relative reloc with a symbol | |
5134 | attached. The symbol should be the one which | |
5135 | this reloc is for. We want to make this | |
5136 | reloc against the TOC address of the symbol, | |
5137 | not the symbol itself. */ | |
5138 | BFD_ASSERT (h->toc_section != NULL); | |
5139 | BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0); | |
5140 | if (h->u.toc_indx != -1) | |
5141 | irel->r_symndx = h->u.toc_indx; | |
5142 | else | |
5143 | { | |
5144 | struct xcoff_toc_rel_hash *n; | |
5145 | struct xcoff_link_section_info *si; | |
5146 | bfd_size_type amt; | |
5147 | ||
5148 | amt = sizeof (struct xcoff_toc_rel_hash); | |
5149 | n = ((struct xcoff_toc_rel_hash *) | |
5150 | bfd_alloc (finfo->output_bfd, amt)); | |
5151 | if (n == NULL) | |
b34976b6 | 5152 | return FALSE; |
dc810e39 AM |
5153 | si = finfo->section_info + target_index; |
5154 | n->next = si->toc_rel_hashes; | |
5155 | n->h = h; | |
5156 | n->rel = irel; | |
5157 | si->toc_rel_hashes = n; | |
5158 | } | |
5159 | } | |
5160 | else if (h != NULL) | |
5161 | { | |
5162 | /* This is a global symbol. */ | |
5163 | if (h->indx >= 0) | |
5164 | irel->r_symndx = h->indx; | |
5165 | else | |
5166 | { | |
5167 | /* This symbol is being written at the end | |
5168 | of the file, and we do not yet know the | |
5169 | symbol index. We save the pointer to the | |
5170 | hash table entry in the rel_hash list. | |
5171 | We set the indx field to -2 to indicate | |
5172 | that this symbol must not be stripped. */ | |
5173 | *rel_hash = h; | |
5174 | h->indx = -2; | |
5175 | } | |
5176 | } | |
5177 | else | |
5178 | { | |
5179 | long indx; | |
252b5132 | 5180 | |
dc810e39 | 5181 | indx = finfo->sym_indices[r_symndx]; |
252b5132 | 5182 | |
dc810e39 AM |
5183 | if (indx == -1) |
5184 | { | |
5185 | struct internal_syment *is; | |
5186 | ||
5187 | /* Relocations against a TC0 TOC anchor are | |
5188 | automatically transformed to be against | |
5189 | the TOC anchor in the output file. */ | |
5190 | is = finfo->internal_syms + r_symndx; | |
5191 | if (is->n_sclass == C_HIDEXT | |
5192 | && is->n_numaux > 0) | |
5193 | { | |
5194 | PTR auxptr; | |
5195 | union internal_auxent aux; | |
5196 | ||
5197 | auxptr = ((PTR) | |
5198 | (((bfd_byte *) | |
5199 | obj_coff_external_syms (input_bfd)) | |
5200 | + ((r_symndx + is->n_numaux) | |
5201 | * isymesz))); | |
5202 | bfd_coff_swap_aux_in (input_bfd, auxptr, | |
5203 | is->n_type, is->n_sclass, | |
5204 | is->n_numaux - 1, | |
5205 | is->n_numaux, | |
5206 | (PTR) &aux); | |
5207 | if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD | |
5208 | && aux.x_csect.x_smclas == XMC_TC0) | |
5209 | indx = finfo->toc_symindx; | |
5210 | } | |
5211 | } | |
252b5132 | 5212 | |
dc810e39 AM |
5213 | if (indx != -1) |
5214 | irel->r_symndx = indx; | |
5215 | else | |
5216 | { | |
252b5132 | 5217 | |
dc810e39 | 5218 | struct internal_syment *is; |
252b5132 | 5219 | |
dc810e39 AM |
5220 | const char *name; |
5221 | char buf[SYMNMLEN + 1]; | |
252b5132 | 5222 | |
dc810e39 AM |
5223 | /* This reloc is against a symbol we are |
5224 | stripping. It would be possible to handle | |
5225 | this case, but I don't think it's worth it. */ | |
5226 | is = finfo->internal_syms + r_symndx; | |
beb1bf64 | 5227 | |
dc810e39 AM |
5228 | name = (_bfd_coff_internal_syment_name |
5229 | (input_bfd, is, buf)); | |
5230 | ||
5231 | if (name == NULL) | |
b34976b6 | 5232 | return FALSE; |
dc810e39 AM |
5233 | |
5234 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
5235 | (finfo->info, name, input_bfd, o, | |
5236 | irel->r_vaddr))) | |
b34976b6 | 5237 | return FALSE; |
dc810e39 AM |
5238 | } |
5239 | } | |
5240 | } | |
252b5132 | 5241 | |
b34976b6 | 5242 | quiet = FALSE; |
dc810e39 AM |
5243 | switch (irel->r_type) |
5244 | { | |
5245 | default: | |
5246 | if (h == NULL | |
5247 | || h->root.type == bfd_link_hash_defined | |
5248 | || h->root.type == bfd_link_hash_defweak | |
5249 | || h->root.type == bfd_link_hash_common) | |
5250 | break; | |
5251 | /* Fall through. */ | |
5252 | case R_POS: | |
5253 | case R_NEG: | |
5254 | case R_RL: | |
5255 | case R_RLA: | |
5256 | /* This reloc needs to be copied into the .loader | |
5257 | section. */ | |
5258 | ldrel.l_vaddr = irel->r_vaddr; | |
5259 | if (r_symndx == -1) | |
5260 | ldrel.l_symndx = -(bfd_size_type ) 1; | |
5261 | else if (h == NULL | |
5262 | || (h->root.type == bfd_link_hash_defined | |
5263 | || h->root.type == bfd_link_hash_defweak | |
5264 | || h->root.type == bfd_link_hash_common)) | |
5265 | { | |
5266 | asection *sec; | |
5267 | ||
5268 | if (h == NULL) | |
5269 | sec = xcoff_data (input_bfd)->csects[r_symndx]; | |
5270 | else if (h->root.type == bfd_link_hash_common) | |
5271 | sec = h->root.u.c.p->section; | |
5272 | else | |
5273 | sec = h->root.u.def.section; | |
5274 | sec = sec->output_section; | |
5275 | ||
5276 | if (strcmp (sec->name, ".text") == 0) | |
5277 | ldrel.l_symndx = 0; | |
5278 | else if (strcmp (sec->name, ".data") == 0) | |
5279 | ldrel.l_symndx = 1; | |
5280 | else if (strcmp (sec->name, ".bss") == 0) | |
5281 | ldrel.l_symndx = 2; | |
5282 | else | |
5283 | { | |
5284 | (*_bfd_error_handler) | |
d003868e AM |
5285 | (_("%B: loader reloc in unrecognized section `%A'"), |
5286 | input_bfd, sec); | |
dc810e39 | 5287 | bfd_set_error (bfd_error_nonrepresentable_section); |
b34976b6 | 5288 | return FALSE; |
dc810e39 AM |
5289 | } |
5290 | } | |
5291 | else | |
5292 | { | |
1049f94e | 5293 | if (! finfo->info->relocatable |
dc810e39 AM |
5294 | && (h->flags & XCOFF_DEF_DYNAMIC) == 0 |
5295 | && (h->flags & XCOFF_IMPORT) == 0) | |
5296 | { | |
5297 | /* We already called the undefined_symbol | |
5298 | callback for this relocation, in | |
5299 | _bfd_ppc_xcoff_relocate_section. Don't | |
5300 | issue any more warnings. */ | |
b34976b6 | 5301 | quiet = TRUE; |
dc810e39 AM |
5302 | } |
5303 | if (h->ldindx < 0 && ! quiet) | |
5304 | { | |
5305 | (*_bfd_error_handler) | |
d003868e AM |
5306 | (_("%B: `%s' in loader reloc but not loader sym"), |
5307 | input_bfd, | |
dc810e39 AM |
5308 | h->root.root.string); |
5309 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 5310 | return FALSE; |
dc810e39 AM |
5311 | } |
5312 | ldrel.l_symndx = h->ldindx; | |
5313 | } | |
5314 | ldrel.l_rtype = (irel->r_size << 8) | irel->r_type; | |
5315 | ldrel.l_rsecnm = o->output_section->target_index; | |
5316 | if (xcoff_hash_table (finfo->info)->textro | |
5317 | && strcmp (o->output_section->name, ".text") == 0 | |
5318 | && ! quiet) | |
5319 | { | |
5320 | (*_bfd_error_handler) | |
d003868e AM |
5321 | (_("%B: loader reloc in read-only section %A"), |
5322 | input_bfd, o->output_section); | |
dc810e39 | 5323 | bfd_set_error (bfd_error_invalid_operation); |
b34976b6 | 5324 | return FALSE; |
dc810e39 AM |
5325 | } |
5326 | bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, | |
5327 | finfo->ldrel); | |
252b5132 | 5328 | |
dc810e39 AM |
5329 | finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd); |
5330 | break; | |
252b5132 | 5331 | |
dc810e39 AM |
5332 | case R_TOC: |
5333 | case R_GL: | |
5334 | case R_TCL: | |
5335 | case R_TRL: | |
5336 | case R_TRLA: | |
5337 | /* We should never need a .loader reloc for a TOC | |
5338 | relative reloc. */ | |
5339 | break; | |
5340 | } | |
5341 | } | |
beb1bf64 | 5342 | |
dc810e39 AM |
5343 | o->output_section->reloc_count += o->reloc_count; |
5344 | } | |
beb1bf64 | 5345 | |
dc810e39 AM |
5346 | /* Write out the modified section contents. */ |
5347 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
5348 | contents, (file_ptr) o->output_offset, | |
eea6121a | 5349 | o->size)) |
b34976b6 | 5350 | return FALSE; |
252b5132 RH |
5351 | } |
5352 | ||
5353 | obj_coff_keep_syms (input_bfd) = keep_syms; | |
5354 | ||
5355 | if (! finfo->info->keep_memory) | |
5356 | { | |
5357 | if (! _bfd_coff_free_symbols (input_bfd)) | |
b34976b6 | 5358 | return FALSE; |
252b5132 RH |
5359 | } |
5360 | ||
b34976b6 | 5361 | return TRUE; |
252b5132 RH |
5362 | } |
5363 | ||
5364 | #undef N_TMASK | |
5365 | #undef N_BTSHFT | |
5366 | ||
5367 | /* Write out a non-XCOFF global symbol. */ | |
5368 | ||
beb1bf64 | 5369 | |
b34976b6 | 5370 | static bfd_boolean |
dc810e39 | 5371 | xcoff_write_global_symbol (h, inf) |
252b5132 | 5372 | struct xcoff_link_hash_entry *h; |
dc810e39 | 5373 | PTR inf; |
252b5132 | 5374 | { |
dc810e39 | 5375 | struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf; |
252b5132 RH |
5376 | bfd *output_bfd; |
5377 | bfd_byte *outsym; | |
5378 | struct internal_syment isym; | |
5379 | union internal_auxent aux; | |
b34976b6 | 5380 | bfd_boolean result; |
dc810e39 AM |
5381 | file_ptr pos; |
5382 | bfd_size_type amt; | |
252b5132 RH |
5383 | |
5384 | output_bfd = finfo->output_bfd; | |
5385 | outsym = finfo->outsyms; | |
5386 | ||
e92d460e AM |
5387 | if (h->root.type == bfd_link_hash_warning) |
5388 | { | |
5389 | h = (struct xcoff_link_hash_entry *) h->root.u.i.link; | |
5390 | if (h->root.type == bfd_link_hash_new) | |
b34976b6 | 5391 | return TRUE; |
e92d460e AM |
5392 | } |
5393 | ||
252b5132 RH |
5394 | /* If this symbol was garbage collected, just skip it. */ |
5395 | if (xcoff_hash_table (finfo->info)->gc | |
5396 | && (h->flags & XCOFF_MARK) == 0) | |
b34976b6 | 5397 | return TRUE; |
252b5132 RH |
5398 | |
5399 | /* If we need a .loader section entry, write it out. */ | |
dc810e39 AM |
5400 | if (h->ldsym != NULL) |
5401 | { | |
5402 | struct internal_ldsym *ldsym; | |
5403 | bfd *impbfd; | |
252b5132 | 5404 | |
dc810e39 | 5405 | ldsym = h->ldsym; |
252b5132 | 5406 | |
dc810e39 AM |
5407 | if (h->root.type == bfd_link_hash_undefined |
5408 | || h->root.type == bfd_link_hash_undefweak) | |
5409 | { | |
252b5132 | 5410 | |
dc810e39 AM |
5411 | ldsym->l_value = 0; |
5412 | ldsym->l_scnum = N_UNDEF; | |
5413 | ldsym->l_smtype = XTY_ER; | |
5414 | impbfd = h->root.u.undef.abfd; | |
252b5132 | 5415 | |
dc810e39 AM |
5416 | } |
5417 | else if (h->root.type == bfd_link_hash_defined | |
5418 | || h->root.type == bfd_link_hash_defweak) | |
5419 | { | |
252b5132 | 5420 | |
dc810e39 | 5421 | asection *sec; |
252b5132 | 5422 | |
dc810e39 AM |
5423 | sec = h->root.u.def.section; |
5424 | ldsym->l_value = (sec->output_section->vma | |
5425 | + sec->output_offset | |
5426 | + h->root.u.def.value); | |
5427 | ldsym->l_scnum = sec->output_section->target_index; | |
5428 | ldsym->l_smtype = XTY_SD; | |
5429 | impbfd = sec->owner; | |
252b5132 | 5430 | |
dc810e39 AM |
5431 | } |
5432 | else | |
5433 | abort (); | |
252b5132 | 5434 | |
dc810e39 AM |
5435 | if (((h->flags & XCOFF_DEF_REGULAR) == 0 |
5436 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
5437 | || (h->flags & XCOFF_IMPORT) != 0) | |
5438 | { | |
5439 | /* Clear l_smtype | |
5440 | Import symbols are defined so the check above will make | |
5441 | the l_smtype XTY_SD. But this is not correct, it should | |
5442 | be cleared. */ | |
5443 | ldsym->l_smtype |= L_IMPORT; | |
5444 | } | |
252b5132 | 5445 | |
dc810e39 AM |
5446 | if (((h->flags & XCOFF_DEF_REGULAR) != 0 |
5447 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
5448 | || (h->flags & XCOFF_EXPORT) != 0) | |
5449 | { | |
5450 | ldsym->l_smtype |= L_EXPORT; | |
5451 | } | |
beb1bf64 | 5452 | |
dc810e39 AM |
5453 | if ((h->flags & XCOFF_ENTRY) != 0) |
5454 | { | |
5455 | ldsym->l_smtype |= L_ENTRY; | |
5456 | } | |
5457 | ||
5458 | if ((h->flags & XCOFF_RTINIT) != 0) | |
5459 | { | |
5460 | ldsym->l_smtype = XTY_SD; | |
5461 | } | |
5462 | ||
5463 | ldsym->l_smclas = h->smclas; | |
5464 | ||
5465 | if (ldsym->l_smtype & L_IMPORT) | |
5466 | { | |
5467 | if ((h->root.type == bfd_link_hash_defined | |
5468 | || h->root.type == bfd_link_hash_defweak) | |
5469 | && (h->root.u.def.value != 0)) | |
5470 | { | |
5471 | ldsym->l_smclas = XMC_XO; | |
5472 | } | |
5473 | else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) == | |
5474 | (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) | |
5475 | { | |
5476 | ldsym->l_smclas = XMC_SV3264; | |
5477 | } | |
5478 | else if (h->flags & XCOFF_SYSCALL32) | |
5479 | { | |
5480 | ldsym->l_smclas = XMC_SV; | |
5481 | } | |
5482 | else if (h->flags & XCOFF_SYSCALL64) | |
5483 | { | |
5484 | ldsym->l_smclas = XMC_SV64; | |
5485 | } | |
5486 | } | |
5487 | ||
5488 | if (ldsym->l_ifile == -(bfd_size_type) 1) | |
5489 | { | |
5490 | ldsym->l_ifile = 0; | |
5491 | } | |
5492 | else if (ldsym->l_ifile == 0) | |
5493 | { | |
5494 | if ((ldsym->l_smtype & L_IMPORT) == 0) | |
5495 | { | |
5496 | ldsym->l_ifile = 0; | |
5497 | } | |
5498 | else if (impbfd == NULL) | |
5499 | { | |
5500 | ldsym->l_ifile = 0; | |
5501 | } | |
5502 | else | |
5503 | { | |
5504 | BFD_ASSERT (impbfd->xvec == output_bfd->xvec); | |
5505 | ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; | |
5506 | } | |
5507 | } | |
5508 | ||
5509 | ldsym->l_parm = 0; | |
5510 | ||
5511 | BFD_ASSERT (h->ldindx >= 0); | |
5512 | ||
5513 | bfd_xcoff_swap_ldsym_out (output_bfd, ldsym, | |
5514 | (finfo->ldsym + | |
5515 | (h->ldindx - 3) | |
5516 | * bfd_xcoff_ldsymsz(finfo->output_bfd))); | |
5517 | h->ldsym = NULL; | |
beb1bf64 | 5518 | } |
dc810e39 | 5519 | |
252b5132 | 5520 | /* If this symbol needs global linkage code, write it out. */ |
dc810e39 AM |
5521 | if (h->root.type == bfd_link_hash_defined |
5522 | && (h->root.u.def.section | |
5523 | == xcoff_hash_table (finfo->info)->linkage_section)) | |
5524 | { | |
5525 | bfd_byte *p; | |
5526 | bfd_vma tocoff; | |
5527 | unsigned int i; | |
beb1bf64 | 5528 | |
dc810e39 | 5529 | p = h->root.u.def.section->contents + h->root.u.def.value; |
beb1bf64 | 5530 | |
dc810e39 AM |
5531 | /* The first instruction in the global linkage code loads a |
5532 | specific TOC element. */ | |
5533 | tocoff = (h->descriptor->toc_section->output_section->vma | |
5534 | + h->descriptor->toc_section->output_offset | |
5535 | - xcoff_data (output_bfd)->toc); | |
beb1bf64 | 5536 | |
dc810e39 AM |
5537 | if ((h->descriptor->flags & XCOFF_SET_TOC) != 0) |
5538 | { | |
5539 | tocoff += h->descriptor->u.toc_offset; | |
5540 | } | |
5541 | ||
5542 | ||
5543 | /* The first instruction in the glink code needs to be | |
5544 | cooked to to hold the correct offset in the toc. The | |
5545 | rest are just output raw. */ | |
5546 | bfd_put_32 (output_bfd, | |
5547 | bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p); | |
5548 | ||
5549 | /* Start with i == 1 to get past the first instruction done above | |
5550 | The /4 is because the glink code is in bytes and we are going | |
5551 | 4 at a pop. */ | |
5552 | for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++) | |
5553 | { | |
5554 | bfd_put_32 (output_bfd, | |
5555 | (bfd_vma) bfd_xcoff_glink_code(output_bfd, i), | |
5556 | &p[4 * i]); | |
5557 | } | |
beb1bf64 | 5558 | } |
252b5132 RH |
5559 | |
5560 | /* If we created a TOC entry for this symbol, write out the required | |
5561 | relocs. */ | |
5562 | if ((h->flags & XCOFF_SET_TOC) != 0) | |
5563 | { | |
5564 | asection *tocsec; | |
5565 | asection *osec; | |
5566 | int oindx; | |
5567 | struct internal_reloc *irel; | |
5568 | struct internal_ldrel ldrel; | |
5569 | struct internal_syment irsym; | |
5570 | union internal_auxent iraux; | |
5571 | ||
5572 | tocsec = h->toc_section; | |
5573 | osec = tocsec->output_section; | |
5574 | oindx = osec->target_index; | |
5575 | irel = finfo->section_info[oindx].relocs + osec->reloc_count; | |
5576 | irel->r_vaddr = (osec->vma | |
5577 | + tocsec->output_offset | |
5578 | + h->u.toc_offset); | |
beb1bf64 TR |
5579 | |
5580 | ||
dc810e39 AM |
5581 | if (h->indx >= 0) |
5582 | { | |
5583 | irel->r_symndx = h->indx; | |
5584 | } | |
5585 | else | |
5586 | { | |
5587 | h->indx = -2; | |
5588 | irel->r_symndx = obj_raw_syment_count (output_bfd); | |
5589 | } | |
5590 | ||
beb1bf64 TR |
5591 | BFD_ASSERT (h->ldindx >= 0); |
5592 | ||
dc810e39 AM |
5593 | /* Initialize the aux union here instead of closer to when it is |
5594 | written out below because the length of the csect depends on | |
5595 | whether the output is 32 or 64 bit. */ | |
beb1bf64 TR |
5596 | memset (&iraux, 0, sizeof iraux); |
5597 | iraux.x_csect.x_smtyp = XTY_SD; | |
5598 | /* iraux.x_csect.x_scnlen.l = 4 or 8, see below */ | |
5599 | iraux.x_csect.x_smclas = XMC_TC; | |
5600 | ||
dc810e39 AM |
5601 | /* 32 bit uses a 32 bit R_POS to do the relocations |
5602 | 64 bit uses a 64 bit R_POS to do the relocations | |
beb1bf64 | 5603 | |
dc810e39 AM |
5604 | Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit |
5605 | ||
5606 | Which one is determined by the backend. */ | |
5607 | if (bfd_xcoff_is_xcoff64 (output_bfd)) | |
5608 | { | |
5609 | irel->r_size = 63; | |
5610 | iraux.x_csect.x_scnlen.l = 8; | |
5611 | } | |
5612 | else if (bfd_xcoff_is_xcoff32 (output_bfd)) | |
5613 | { | |
5614 | irel->r_size = 31; | |
5615 | iraux.x_csect.x_scnlen.l = 4; | |
5616 | } | |
5617 | else | |
5618 | { | |
b34976b6 | 5619 | return FALSE; |
dc810e39 | 5620 | } |
252b5132 | 5621 | irel->r_type = R_POS; |
252b5132 RH |
5622 | finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; |
5623 | ++osec->reloc_count; | |
5624 | ||
252b5132 RH |
5625 | ldrel.l_vaddr = irel->r_vaddr; |
5626 | ldrel.l_symndx = h->ldindx; | |
beb1bf64 | 5627 | ldrel.l_rtype = (irel->r_size << 8) | R_POS; |
252b5132 | 5628 | ldrel.l_rsecnm = oindx; |
beb1bf64 TR |
5629 | bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel); |
5630 | finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd); | |
252b5132 | 5631 | |
dc810e39 AM |
5632 | /* We need to emit a symbol to define a csect which holds |
5633 | the reloc. */ | |
5634 | if (finfo->info->strip != strip_all) | |
5635 | { | |
252b5132 | 5636 | |
dc810e39 AM |
5637 | result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, |
5638 | &irsym, h->root.root.string); | |
b34976b6 AM |
5639 | if (!result) |
5640 | return FALSE; | |
252b5132 | 5641 | |
dc810e39 AM |
5642 | irsym.n_value = irel->r_vaddr; |
5643 | irsym.n_scnum = osec->target_index; | |
5644 | irsym.n_sclass = C_HIDEXT; | |
5645 | irsym.n_type = T_NULL; | |
5646 | irsym.n_numaux = 1; | |
252b5132 | 5647 | |
dc810e39 AM |
5648 | bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym); |
5649 | outsym += bfd_coff_symesz (output_bfd); | |
252b5132 | 5650 | |
dc810e39 AM |
5651 | /* note : iraux is initialized above */ |
5652 | bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT, | |
5653 | 0, 1, (PTR) outsym); | |
5654 | outsym += bfd_coff_auxesz (output_bfd); | |
252b5132 | 5655 | |
dc810e39 AM |
5656 | if (h->indx >= 0) |
5657 | { | |
5658 | /* We aren't going to write out the symbols below, so we | |
252b5132 | 5659 | need to write them out now. */ |
dc810e39 AM |
5660 | pos = obj_sym_filepos (output_bfd); |
5661 | pos += (obj_raw_syment_count (output_bfd) | |
5662 | * bfd_coff_symesz (output_bfd)); | |
5663 | amt = outsym - finfo->outsyms; | |
5664 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 | |
5665 | || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt) | |
b34976b6 | 5666 | return FALSE; |
252b5132 RH |
5667 | obj_raw_syment_count (output_bfd) += |
5668 | (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd); | |
5669 | ||
5670 | outsym = finfo->outsyms; | |
5671 | } | |
5672 | } | |
5673 | } | |
5674 | ||
dc810e39 AM |
5675 | /* If this symbol is a specially defined function descriptor, write |
5676 | it out. The first word is the address of the function code | |
5677 | itself, the second word is the address of the TOC, and the third | |
5678 | word is zero. | |
5679 | ||
5680 | 32 bit vs 64 bit | |
5681 | The addresses for the 32 bit will take 4 bytes and the addresses | |
5682 | for 64 bit will take 8 bytes. Similar for the relocs. This type | |
5683 | of logic was also done above to create a TOC entry in | |
5684 | xcoff_write_global_symbol. */ | |
252b5132 RH |
5685 | if ((h->flags & XCOFF_DESCRIPTOR) != 0 |
5686 | && h->root.type == bfd_link_hash_defined | |
5687 | && (h->root.u.def.section | |
5688 | == xcoff_hash_table (finfo->info)->descriptor_section)) | |
5689 | { | |
5690 | asection *sec; | |
5691 | asection *osec; | |
5692 | int oindx; | |
5693 | bfd_byte *p; | |
5694 | struct xcoff_link_hash_entry *hentry; | |
5695 | asection *esec; | |
5696 | struct internal_reloc *irel; | |
5697 | struct internal_ldrel ldrel; | |
5698 | asection *tsec; | |
beb1bf64 TR |
5699 | unsigned int reloc_size, byte_size; |
5700 | ||
dc810e39 AM |
5701 | if (bfd_xcoff_is_xcoff64 (output_bfd)) |
5702 | { | |
5703 | reloc_size = 63; | |
5704 | byte_size = 8; | |
5705 | } | |
5706 | else if (bfd_xcoff_is_xcoff32 (output_bfd)) | |
5707 | { | |
5708 | reloc_size = 31; | |
5709 | byte_size = 4; | |
5710 | } | |
5711 | else | |
5712 | { | |
b34976b6 | 5713 | return FALSE; |
dc810e39 | 5714 | } |
252b5132 RH |
5715 | |
5716 | sec = h->root.u.def.section; | |
5717 | osec = sec->output_section; | |
5718 | oindx = osec->target_index; | |
5719 | p = sec->contents + h->root.u.def.value; | |
5720 | ||
5721 | hentry = h->descriptor; | |
5722 | BFD_ASSERT (hentry != NULL | |
5723 | && (hentry->root.type == bfd_link_hash_defined | |
5724 | || hentry->root.type == bfd_link_hash_defweak)); | |
5725 | esec = hentry->root.u.def.section; | |
252b5132 RH |
5726 | |
5727 | irel = finfo->section_info[oindx].relocs + osec->reloc_count; | |
5728 | irel->r_vaddr = (osec->vma | |
5729 | + sec->output_offset | |
5730 | + h->root.u.def.value); | |
5731 | irel->r_symndx = esec->output_section->target_index; | |
5732 | irel->r_type = R_POS; | |
beb1bf64 | 5733 | irel->r_size = reloc_size; |
252b5132 RH |
5734 | finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; |
5735 | ++osec->reloc_count; | |
5736 | ||
5737 | ldrel.l_vaddr = irel->r_vaddr; | |
5738 | if (strcmp (esec->output_section->name, ".text") == 0) | |
5739 | ldrel.l_symndx = 0; | |
5740 | else if (strcmp (esec->output_section->name, ".data") == 0) | |
5741 | ldrel.l_symndx = 1; | |
5742 | else if (strcmp (esec->output_section->name, ".bss") == 0) | |
5743 | ldrel.l_symndx = 2; | |
5744 | else | |
5745 | { | |
5746 | (*_bfd_error_handler) | |
5747 | (_("%s: loader reloc in unrecognized section `%s'"), | |
5748 | bfd_get_filename (output_bfd), | |
5749 | esec->output_section->name); | |
5750 | bfd_set_error (bfd_error_nonrepresentable_section); | |
b34976b6 | 5751 | return FALSE; |
252b5132 | 5752 | } |
beb1bf64 | 5753 | ldrel.l_rtype = (reloc_size << 8) | R_POS; |
252b5132 | 5754 | ldrel.l_rsecnm = oindx; |
beb1bf64 TR |
5755 | bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel); |
5756 | finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd); | |
5757 | ||
dc810e39 AM |
5758 | /* There are three items to write out, |
5759 | the address of the code | |
5760 | the address of the toc anchor | |
5761 | the environment pointer. | |
5762 | We are ignoring the environment pointer. So set it to zero. */ | |
5763 | if (bfd_xcoff_is_xcoff64 (output_bfd)) | |
5764 | { | |
5765 | bfd_put_64 (output_bfd, | |
5766 | (esec->output_section->vma + esec->output_offset | |
5767 | + hentry->root.u.def.value), | |
5768 | p); | |
5769 | bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8); | |
5770 | bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16); | |
5771 | } | |
5772 | else | |
5773 | { | |
5774 | /* 32 bit backend | |
5775 | This logic was already called above so the error case where | |
5776 | the backend is neither has already been checked. */ | |
5777 | bfd_put_32 (output_bfd, | |
5778 | (esec->output_section->vma + esec->output_offset | |
5779 | + hentry->root.u.def.value), | |
5780 | p); | |
5781 | bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4); | |
5782 | bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8); | |
5783 | } | |
5784 | ||
252b5132 RH |
5785 | tsec = coff_section_from_bfd_index (output_bfd, |
5786 | xcoff_data (output_bfd)->sntoc); | |
5787 | ||
5788 | ++irel; | |
5789 | irel->r_vaddr = (osec->vma | |
5790 | + sec->output_offset | |
5791 | + h->root.u.def.value | |
beb1bf64 | 5792 | + byte_size); |
252b5132 RH |
5793 | irel->r_symndx = tsec->output_section->target_index; |
5794 | irel->r_type = R_POS; | |
beb1bf64 | 5795 | irel->r_size = reloc_size; |
252b5132 RH |
5796 | finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; |
5797 | ++osec->reloc_count; | |
5798 | ||
5799 | ldrel.l_vaddr = irel->r_vaddr; | |
5800 | if (strcmp (tsec->output_section->name, ".text") == 0) | |
5801 | ldrel.l_symndx = 0; | |
5802 | else if (strcmp (tsec->output_section->name, ".data") == 0) | |
5803 | ldrel.l_symndx = 1; | |
5804 | else if (strcmp (tsec->output_section->name, ".bss") == 0) | |
5805 | ldrel.l_symndx = 2; | |
5806 | else | |
5807 | { | |
5808 | (*_bfd_error_handler) | |
5809 | (_("%s: loader reloc in unrecognized section `%s'"), | |
5810 | bfd_get_filename (output_bfd), | |
5811 | tsec->output_section->name); | |
5812 | bfd_set_error (bfd_error_nonrepresentable_section); | |
b34976b6 | 5813 | return FALSE; |
252b5132 | 5814 | } |
beb1bf64 | 5815 | ldrel.l_rtype = (reloc_size << 8) | R_POS; |
252b5132 | 5816 | ldrel.l_rsecnm = oindx; |
beb1bf64 TR |
5817 | bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel); |
5818 | finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd); | |
252b5132 RH |
5819 | } |
5820 | ||
5821 | if (h->indx >= 0 || finfo->info->strip == strip_all) | |
5822 | { | |
5823 | BFD_ASSERT (outsym == finfo->outsyms); | |
b34976b6 | 5824 | return TRUE; |
252b5132 RH |
5825 | } |
5826 | ||
5827 | if (h->indx != -2 | |
5828 | && (finfo->info->strip == strip_all | |
5829 | || (finfo->info->strip == strip_some | |
dc810e39 | 5830 | && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string, |
b34976b6 | 5831 | FALSE, FALSE) == NULL))) |
252b5132 RH |
5832 | { |
5833 | BFD_ASSERT (outsym == finfo->outsyms); | |
b34976b6 | 5834 | return TRUE; |
252b5132 RH |
5835 | } |
5836 | ||
5837 | if (h->indx != -2 | |
5838 | && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0) | |
5839 | { | |
5840 | BFD_ASSERT (outsym == finfo->outsyms); | |
b34976b6 | 5841 | return TRUE; |
252b5132 RH |
5842 | } |
5843 | ||
5844 | memset (&aux, 0, sizeof aux); | |
5845 | ||
5846 | h->indx = obj_raw_syment_count (output_bfd); | |
5847 | ||
dc810e39 | 5848 | result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym, |
beb1bf64 | 5849 | h->root.root.string); |
b34976b6 AM |
5850 | if (!result) |
5851 | return FALSE; | |
252b5132 RH |
5852 | |
5853 | if (h->root.type == bfd_link_hash_undefined | |
5854 | || h->root.type == bfd_link_hash_undefweak) | |
5855 | { | |
5856 | isym.n_value = 0; | |
5857 | isym.n_scnum = N_UNDEF; | |
5858 | isym.n_sclass = C_EXT; | |
5859 | aux.x_csect.x_smtyp = XTY_ER; | |
5860 | } | |
5861 | else if ((h->root.type == bfd_link_hash_defined | |
5862 | || h->root.type == bfd_link_hash_defweak) | |
5863 | && h->smclas == XMC_XO) | |
5864 | { | |
5865 | BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section)); | |
5866 | isym.n_value = h->root.u.def.value; | |
5867 | isym.n_scnum = N_UNDEF; | |
5868 | isym.n_sclass = C_EXT; | |
5869 | aux.x_csect.x_smtyp = XTY_ER; | |
5870 | } | |
5871 | else if (h->root.type == bfd_link_hash_defined | |
5872 | || h->root.type == bfd_link_hash_defweak) | |
5873 | { | |
5874 | struct xcoff_link_size_list *l; | |
5875 | ||
5876 | isym.n_value = (h->root.u.def.section->output_section->vma | |
5877 | + h->root.u.def.section->output_offset | |
5878 | + h->root.u.def.value); | |
0121a3da AM |
5879 | if (bfd_is_abs_section (h->root.u.def.section->output_section)) |
5880 | isym.n_scnum = N_ABS; | |
5881 | else | |
5882 | isym.n_scnum = h->root.u.def.section->output_section->target_index; | |
252b5132 RH |
5883 | isym.n_sclass = C_HIDEXT; |
5884 | aux.x_csect.x_smtyp = XTY_SD; | |
5885 | ||
5886 | if ((h->flags & XCOFF_HAS_SIZE) != 0) | |
5887 | { | |
5888 | for (l = xcoff_hash_table (finfo->info)->size_list; | |
5889 | l != NULL; | |
5890 | l = l->next) | |
5891 | { | |
5892 | if (l->h == h) | |
5893 | { | |
5894 | aux.x_csect.x_scnlen.l = l->size; | |
5895 | break; | |
5896 | } | |
5897 | } | |
5898 | } | |
5899 | } | |
5900 | else if (h->root.type == bfd_link_hash_common) | |
5901 | { | |
5902 | isym.n_value = (h->root.u.c.p->section->output_section->vma | |
5903 | + h->root.u.c.p->section->output_offset); | |
5904 | isym.n_scnum = h->root.u.c.p->section->output_section->target_index; | |
5905 | isym.n_sclass = C_EXT; | |
5906 | aux.x_csect.x_smtyp = XTY_CM; | |
5907 | aux.x_csect.x_scnlen.l = h->root.u.c.size; | |
5908 | } | |
5909 | else | |
5910 | abort (); | |
5911 | ||
5912 | isym.n_type = T_NULL; | |
5913 | isym.n_numaux = 1; | |
5914 | ||
5915 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym); | |
5916 | outsym += bfd_coff_symesz (output_bfd); | |
5917 | ||
5918 | aux.x_csect.x_smclas = h->smclas; | |
252b5132 RH |
5919 | bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1, |
5920 | (PTR) outsym); | |
5921 | outsym += bfd_coff_auxesz (output_bfd); | |
5922 | ||
5923 | if ((h->root.type == bfd_link_hash_defined | |
5924 | || h->root.type == bfd_link_hash_defweak) | |
5925 | && h->smclas != XMC_XO) | |
5926 | { | |
5927 | /* We just output an SD symbol. Now output an LD symbol. */ | |
5928 | ||
5929 | h->indx += 2; | |
5930 | ||
5931 | isym.n_sclass = C_EXT; | |
5932 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym); | |
5933 | outsym += bfd_coff_symesz (output_bfd); | |
5934 | ||
5935 | aux.x_csect.x_smtyp = XTY_LD; | |
5936 | aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd); | |
252b5132 RH |
5937 | bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1, |
5938 | (PTR) outsym); | |
5939 | outsym += bfd_coff_auxesz (output_bfd); | |
5940 | } | |
5941 | ||
dc810e39 AM |
5942 | pos = obj_sym_filepos (output_bfd); |
5943 | pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd); | |
5944 | amt = outsym - finfo->outsyms; | |
5945 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 | |
5946 | || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt) | |
b34976b6 | 5947 | return FALSE; |
252b5132 RH |
5948 | obj_raw_syment_count (output_bfd) += |
5949 | (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd); | |
5950 | ||
b34976b6 | 5951 | return TRUE; |
252b5132 RH |
5952 | } |
5953 | ||
5954 | /* Handle a link order which is supposed to generate a reloc. */ | |
5955 | ||
b34976b6 | 5956 | static bfd_boolean |
252b5132 RH |
5957 | xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order) |
5958 | bfd *output_bfd; | |
5959 | struct xcoff_final_link_info *finfo; | |
5960 | asection *output_section; | |
5961 | struct bfd_link_order *link_order; | |
5962 | { | |
5963 | reloc_howto_type *howto; | |
5964 | struct xcoff_link_hash_entry *h; | |
5965 | asection *hsec; | |
5966 | bfd_vma hval; | |
5967 | bfd_vma addend; | |
5968 | struct internal_reloc *irel; | |
5969 | struct xcoff_link_hash_entry **rel_hash_ptr; | |
5970 | struct internal_ldrel ldrel; | |
5971 | ||
5972 | if (link_order->type == bfd_section_reloc_link_order) | |
5973 | { | |
5974 | /* We need to somehow locate a symbol in the right section. The | |
b34976b6 AM |
5975 | symbol must either have a value of zero, or we must adjust |
5976 | the addend by the value of the symbol. FIXME: Write this | |
5977 | when we need it. The old linker couldn't handle this anyhow. */ | |
252b5132 RH |
5978 | abort (); |
5979 | } | |
5980 | ||
5981 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
5982 | if (howto == NULL) | |
5983 | { | |
5984 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 5985 | return FALSE; |
252b5132 RH |
5986 | } |
5987 | ||
5988 | h = ((struct xcoff_link_hash_entry *) | |
5989 | bfd_wrapped_link_hash_lookup (output_bfd, finfo->info, | |
5990 | link_order->u.reloc.p->u.name, | |
b34976b6 | 5991 | FALSE, FALSE, TRUE)); |
252b5132 RH |
5992 | if (h == NULL) |
5993 | { | |
5994 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
5995 | (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
5996 | (asection *) NULL, (bfd_vma) 0))) | |
b34976b6 AM |
5997 | return FALSE; |
5998 | return TRUE; | |
252b5132 RH |
5999 | } |
6000 | ||
6001 | if (h->root.type == bfd_link_hash_common) | |
6002 | { | |
6003 | hsec = h->root.u.c.p->section; | |
6004 | hval = 0; | |
6005 | } | |
6006 | else if (h->root.type == bfd_link_hash_defined | |
6007 | || h->root.type == bfd_link_hash_defweak) | |
6008 | { | |
6009 | hsec = h->root.u.def.section; | |
6010 | hval = h->root.u.def.value; | |
6011 | } | |
6012 | else | |
6013 | { | |
6014 | hsec = NULL; | |
6015 | hval = 0; | |
6016 | } | |
6017 | ||
6018 | addend = link_order->u.reloc.p->addend; | |
6019 | if (hsec != NULL) | |
6020 | addend += (hsec->output_section->vma | |
6021 | + hsec->output_offset | |
6022 | + hval); | |
6023 | ||
6024 | if (addend != 0) | |
6025 | { | |
6026 | bfd_size_type size; | |
6027 | bfd_byte *buf; | |
6028 | bfd_reloc_status_type rstat; | |
b34976b6 | 6029 | bfd_boolean ok; |
252b5132 RH |
6030 | |
6031 | size = bfd_get_reloc_size (howto); | |
6032 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6033 | if (buf == NULL) | |
b34976b6 | 6034 | return FALSE; |
252b5132 RH |
6035 | |
6036 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
6037 | switch (rstat) | |
6038 | { | |
6039 | case bfd_reloc_ok: | |
6040 | break; | |
6041 | default: | |
6042 | case bfd_reloc_outofrange: | |
6043 | abort (); | |
6044 | case bfd_reloc_overflow: | |
6045 | if (! ((*finfo->info->callbacks->reloc_overflow) | |
6046 | (finfo->info, link_order->u.reloc.p->u.name, | |
6047 | howto->name, addend, (bfd *) NULL, (asection *) NULL, | |
6048 | (bfd_vma) 0))) | |
6049 | { | |
6050 | free (buf); | |
b34976b6 | 6051 | return FALSE; |
252b5132 RH |
6052 | } |
6053 | break; | |
6054 | } | |
6055 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
6056 | (file_ptr) link_order->offset, size); | |
6057 | free (buf); | |
6058 | if (! ok) | |
b34976b6 | 6059 | return FALSE; |
252b5132 RH |
6060 | } |
6061 | ||
6062 | /* Store the reloc information in the right place. It will get | |
6063 | swapped and written out at the end of the final_link routine. */ | |
6064 | ||
6065 | irel = (finfo->section_info[output_section->target_index].relocs | |
6066 | + output_section->reloc_count); | |
6067 | rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes | |
6068 | + output_section->reloc_count); | |
6069 | ||
6070 | memset (irel, 0, sizeof (struct internal_reloc)); | |
6071 | *rel_hash_ptr = NULL; | |
6072 | ||
6073 | irel->r_vaddr = output_section->vma + link_order->offset; | |
6074 | ||
6075 | if (h->indx >= 0) | |
6076 | irel->r_symndx = h->indx; | |
6077 | else | |
6078 | { | |
6079 | /* Set the index to -2 to force this symbol to get written out. */ | |
6080 | h->indx = -2; | |
6081 | *rel_hash_ptr = h; | |
6082 | irel->r_symndx = 0; | |
6083 | } | |
6084 | ||
6085 | irel->r_type = howto->type; | |
6086 | irel->r_size = howto->bitsize - 1; | |
6087 | if (howto->complain_on_overflow == complain_overflow_signed) | |
6088 | irel->r_size |= 0x80; | |
6089 | ||
6090 | ++output_section->reloc_count; | |
6091 | ||
6092 | /* Now output the reloc to the .loader section. */ | |
6093 | ||
6094 | ldrel.l_vaddr = irel->r_vaddr; | |
6095 | ||
6096 | if (hsec != NULL) | |
6097 | { | |
6098 | const char *secname; | |
6099 | ||
6100 | secname = hsec->output_section->name; | |
6101 | ||
6102 | if (strcmp (secname, ".text") == 0) | |
6103 | ldrel.l_symndx = 0; | |
6104 | else if (strcmp (secname, ".data") == 0) | |
6105 | ldrel.l_symndx = 1; | |
6106 | else if (strcmp (secname, ".bss") == 0) | |
6107 | ldrel.l_symndx = 2; | |
6108 | else | |
6109 | { | |
6110 | (*_bfd_error_handler) | |
6111 | (_("%s: loader reloc in unrecognized section `%s'"), | |
6112 | bfd_get_filename (output_bfd), secname); | |
6113 | bfd_set_error (bfd_error_nonrepresentable_section); | |
b34976b6 | 6114 | return FALSE; |
252b5132 RH |
6115 | } |
6116 | } | |
6117 | else | |
6118 | { | |
6119 | if (h->ldindx < 0) | |
6120 | { | |
6121 | (*_bfd_error_handler) | |
6122 | (_("%s: `%s' in loader reloc but not loader sym"), | |
6123 | bfd_get_filename (output_bfd), | |
6124 | h->root.root.string); | |
6125 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 6126 | return FALSE; |
252b5132 RH |
6127 | } |
6128 | ldrel.l_symndx = h->ldindx; | |
6129 | } | |
6130 | ||
6131 | ldrel.l_rtype = (irel->r_size << 8) | irel->r_type; | |
6132 | ldrel.l_rsecnm = output_section->target_index; | |
beb1bf64 TR |
6133 | bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel); |
6134 | finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd); | |
252b5132 | 6135 | |
b34976b6 | 6136 | return TRUE; |
252b5132 RH |
6137 | } |
6138 | ||
6139 | /* Sort relocs by VMA. This is called via qsort. */ | |
6140 | ||
6141 | static int | |
6142 | xcoff_sort_relocs (p1, p2) | |
6143 | const PTR p1; | |
6144 | const PTR p2; | |
6145 | { | |
6146 | const struct internal_reloc *r1 = (const struct internal_reloc *) p1; | |
6147 | const struct internal_reloc *r2 = (const struct internal_reloc *) p2; | |
6148 | ||
6149 | if (r1->r_vaddr > r2->r_vaddr) | |
6150 | return 1; | |
6151 | else if (r1->r_vaddr < r2->r_vaddr) | |
6152 | return -1; | |
6153 | else | |
6154 | return 0; | |
6155 | } | |
6156 | ||
252b5132 | 6157 | |
252b5132 | 6158 | |
252b5132 | 6159 |