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