Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* POWER/PowerPC XCOFF linker support. |
219d1afa | 2 | Copyright (C) 1995-2018 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" |
252b5132 RH |
31 | |
32 | /* This file holds the XCOFF linker code. */ | |
33 | ||
116c20d2 NC |
34 | #undef STRING_SIZE_SIZE |
35 | #define STRING_SIZE_SIZE 4 | |
252b5132 | 36 | |
252b5132 RH |
37 | /* We reuse the SEC_ROM flag as a mark flag for garbage collection. |
38 | This flag will only be used on input sections. */ | |
39 | ||
40 | #define SEC_MARK (SEC_ROM) | |
41 | ||
252b5132 RH |
42 | /* The list of import files. */ |
43 | ||
dc810e39 AM |
44 | struct xcoff_import_file |
45 | { | |
252b5132 RH |
46 | /* The next entry in the list. */ |
47 | struct xcoff_import_file *next; | |
48 | /* The path. */ | |
49 | const char *path; | |
50 | /* The file name. */ | |
51 | const char *file; | |
52 | /* The member name. */ | |
53 | const char *member; | |
54 | }; | |
55 | ||
252b5132 RH |
56 | /* Information we keep for each section in the output file during the |
57 | final link phase. */ | |
58 | ||
dc810e39 AM |
59 | struct xcoff_link_section_info |
60 | { | |
252b5132 RH |
61 | /* The relocs to be output. */ |
62 | struct internal_reloc *relocs; | |
63 | /* For each reloc against a global symbol whose index was not known | |
64 | when the reloc was handled, the global hash table entry. */ | |
65 | struct xcoff_link_hash_entry **rel_hashes; | |
66 | /* If there is a TOC relative reloc against a global symbol, and the | |
67 | index of the TOC symbol is not known when the reloc was handled, | |
68 | an entry is added to this linked list. This is not an array, | |
69 | like rel_hashes, because this case is quite uncommon. */ | |
116c20d2 NC |
70 | struct xcoff_toc_rel_hash |
71 | { | |
fbc4fff4 KH |
72 | struct xcoff_toc_rel_hash *next; |
73 | struct xcoff_link_hash_entry *h; | |
74 | struct internal_reloc *rel; | |
75 | } *toc_rel_hashes; | |
252b5132 RH |
76 | }; |
77 | ||
24c611d1 RS |
78 | /* Information that the XCOFF linker collects about an archive. */ |
79 | struct xcoff_archive_info | |
80 | { | |
81 | /* The archive described by this entry. */ | |
82 | bfd *archive; | |
83 | ||
84 | /* The import path and import filename to use when referring to | |
85 | this archive in the .loader section. */ | |
86 | const char *imppath; | |
87 | const char *impfile; | |
ee698300 RS |
88 | |
89 | /* True if the archive contains a dynamic object. */ | |
90 | unsigned int contains_shared_object_p : 1; | |
91 | ||
92 | /* True if the previous field is valid. */ | |
93 | unsigned int know_contains_shared_object_p : 1; | |
24c611d1 RS |
94 | }; |
95 | ||
d286971a RS |
96 | struct xcoff_link_hash_table |
97 | { | |
98 | struct bfd_link_hash_table root; | |
99 | ||
100 | /* The .debug string hash table. We need to compute this while | |
101 | reading the input files, so that we know how large the .debug | |
102 | section will be before we assign section positions. */ | |
103 | struct bfd_strtab_hash *debug_strtab; | |
104 | ||
105 | /* The .debug section we will use for the final output. */ | |
106 | asection *debug_section; | |
107 | ||
108 | /* The .loader section we will use for the final output. */ | |
109 | asection *loader_section; | |
110 | ||
111 | /* A count of non TOC relative relocs which will need to be | |
112 | allocated in the .loader section. */ | |
113 | size_t ldrel_count; | |
114 | ||
115 | /* The .loader section header. */ | |
116 | struct internal_ldhdr ldhdr; | |
117 | ||
118 | /* The .gl section we use to hold global linkage code. */ | |
119 | asection *linkage_section; | |
120 | ||
121 | /* The .tc section we use to hold toc entries we build for global | |
122 | linkage code. */ | |
123 | asection *toc_section; | |
124 | ||
125 | /* The .ds section we use to hold function descriptors which we | |
126 | create for exported symbols. */ | |
127 | asection *descriptor_section; | |
128 | ||
129 | /* The list of import files. */ | |
130 | struct xcoff_import_file *imports; | |
131 | ||
132 | /* Required alignment of sections within the output file. */ | |
133 | unsigned long file_align; | |
134 | ||
135 | /* Whether the .text section must be read-only. */ | |
136 | bfd_boolean textro; | |
137 | ||
138 | /* Whether -brtl was specified. */ | |
139 | bfd_boolean rtld; | |
140 | ||
141 | /* Whether garbage collection was done. */ | |
142 | bfd_boolean gc; | |
143 | ||
144 | /* A linked list of symbols for which we have size information. */ | |
145 | struct xcoff_link_size_list | |
146 | { | |
147 | struct xcoff_link_size_list *next; | |
148 | struct xcoff_link_hash_entry *h; | |
149 | bfd_size_type size; | |
68ffbac6 | 150 | } |
d286971a RS |
151 | *size_list; |
152 | ||
24c611d1 RS |
153 | /* Information about archives. */ |
154 | htab_t archive_info; | |
155 | ||
d286971a RS |
156 | /* Magic sections: _text, _etext, _data, _edata, _end, end. */ |
157 | asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS]; | |
158 | }; | |
159 | ||
252b5132 RH |
160 | /* Information that we pass around while doing the final link step. */ |
161 | ||
dc810e39 AM |
162 | struct xcoff_final_link_info |
163 | { | |
252b5132 RH |
164 | /* General link information. */ |
165 | struct bfd_link_info *info; | |
166 | /* Output BFD. */ | |
167 | bfd *output_bfd; | |
168 | /* Hash table for long symbol names. */ | |
169 | struct bfd_strtab_hash *strtab; | |
170 | /* Array of information kept for each output section, indexed by the | |
171 | target_index field. */ | |
172 | struct xcoff_link_section_info *section_info; | |
173 | /* Symbol index of last C_FILE symbol (-1 if none). */ | |
174 | long last_file_index; | |
175 | /* Contents of last C_FILE symbol. */ | |
176 | struct internal_syment last_file; | |
177 | /* Symbol index of TOC symbol. */ | |
178 | long toc_symindx; | |
179 | /* Start of .loader symbols. */ | |
beb1bf64 | 180 | bfd_byte *ldsym; |
252b5132 | 181 | /* Next .loader reloc to swap out. */ |
beb1bf64 | 182 | bfd_byte *ldrel; |
252b5132 RH |
183 | /* File position of start of line numbers. */ |
184 | file_ptr line_filepos; | |
185 | /* Buffer large enough to hold swapped symbols of any input file. */ | |
186 | struct internal_syment *internal_syms; | |
187 | /* Buffer large enough to hold output indices of symbols of any | |
188 | input file. */ | |
189 | long *sym_indices; | |
190 | /* Buffer large enough to hold output symbols for any input file. */ | |
191 | bfd_byte *outsyms; | |
192 | /* Buffer large enough to hold external line numbers for any input | |
193 | section. */ | |
194 | bfd_byte *linenos; | |
195 | /* Buffer large enough to hold any input section. */ | |
196 | bfd_byte *contents; | |
197 | /* Buffer large enough to hold external relocs of any input section. */ | |
198 | bfd_byte *external_relocs; | |
199 | }; | |
200 | ||
116c20d2 NC |
201 | static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *); |
202 | ||
252b5132 | 203 | \f |
252b5132 | 204 | |
252b5132 RH |
205 | /* Routines to read XCOFF dynamic information. This don't really |
206 | belong here, but we already have the ldsym manipulation routines | |
207 | here. */ | |
208 | ||
209 | /* Read the contents of a section. */ | |
210 | ||
b34976b6 | 211 | static bfd_boolean |
116c20d2 | 212 | xcoff_get_section_contents (bfd *abfd, asection *sec) |
252b5132 RH |
213 | { |
214 | if (coff_section_data (abfd, sec) == NULL) | |
215 | { | |
dc810e39 | 216 | bfd_size_type amt = sizeof (struct coff_section_tdata); |
116c20d2 | 217 | |
dc810e39 | 218 | sec->used_by_bfd = bfd_zalloc (abfd, amt); |
252b5132 | 219 | if (sec->used_by_bfd == NULL) |
b34976b6 | 220 | return FALSE; |
252b5132 RH |
221 | } |
222 | ||
223 | if (coff_section_data (abfd, sec)->contents == NULL) | |
224 | { | |
eea6121a | 225 | bfd_byte *contents; |
116c20d2 NC |
226 | |
227 | if (! bfd_malloc_and_get_section (abfd, sec, &contents)) | |
eea6121a AM |
228 | { |
229 | if (contents != NULL) | |
230 | free (contents); | |
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; | |
116c20d2 | 595 | bfd_size_type 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; | |
766 | bfd_size_type amt = sizeof (* n); | |
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 | { |
24c611d1 RS |
1002 | if (!bfd_xcoff_split_import_path (abfd, abfd->filename, |
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, | |
1015 | archive_info->archive->filename, | |
1016 | &archive_info->imppath, | |
1017 | &archive_info->impfile)) | |
1018 | return FALSE; | |
1019 | } | |
1020 | n->path = archive_info->imppath; | |
1021 | n->file = archive_info->impfile; | |
1022 | n->member = bfd_get_filename (abfd); | |
252b5132 RH |
1023 | } |
1024 | ||
116c20d2 NC |
1025 | /* We start c at 1 because the first import file number is reserved |
1026 | for LIBPATH. */ | |
1027 | for (pp = &xcoff_hash_table (info)->imports, c = 1; | |
1028 | *pp != NULL; | |
1029 | pp = &(*pp)->next, ++c) | |
1030 | ; | |
1031 | *pp = n; | |
252b5132 | 1032 | |
116c20d2 | 1033 | xcoff_data (abfd)->import_file_id = c; |
252b5132 | 1034 | |
116c20d2 | 1035 | return TRUE; |
252b5132 RH |
1036 | } |
1037 | ||
dc810e39 AM |
1038 | /* xcoff_link_create_extra_sections |
1039 | ||
1040 | Takes care of creating the .loader, .gl, .ds, .debug and sections. */ | |
1041 | ||
b34976b6 | 1042 | static bfd_boolean |
116c20d2 | 1043 | xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info) |
dc810e39 | 1044 | { |
b34976b6 | 1045 | bfd_boolean return_value = FALSE; |
beb1bf64 | 1046 | |
f13a99db | 1047 | if (info->output_bfd->xvec == abfd->xvec) |
dc810e39 | 1048 | { |
beb1bf64 TR |
1049 | /* We need to build a .loader section, so we do it here. This |
1050 | won't work if we're producing an XCOFF output file with no | |
1051 | XCOFF input files. FIXME. */ | |
1052 | ||
0e1862bb | 1053 | if (!bfd_link_relocatable (info) |
b3d1832c | 1054 | && xcoff_hash_table (info)->loader_section == NULL) |
dc810e39 AM |
1055 | { |
1056 | asection *lsec; | |
117ed4f8 | 1057 | flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY; |
dc810e39 | 1058 | |
117ed4f8 | 1059 | lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags); |
dc810e39 | 1060 | if (lsec == NULL) |
116c20d2 NC |
1061 | goto end_return; |
1062 | ||
dc810e39 | 1063 | xcoff_hash_table (info)->loader_section = lsec; |
beb1bf64 | 1064 | } |
beb1bf64 TR |
1065 | |
1066 | /* Likewise for the linkage section. */ | |
dc810e39 AM |
1067 | if (xcoff_hash_table (info)->linkage_section == NULL) |
1068 | { | |
1069 | asection *lsec; | |
117ed4f8 AM |
1070 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
1071 | | SEC_IN_MEMORY); | |
beb1bf64 | 1072 | |
117ed4f8 | 1073 | lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags); |
dc810e39 | 1074 | if (lsec == NULL) |
116c20d2 | 1075 | goto end_return; |
beb1bf64 | 1076 | |
dc810e39 | 1077 | xcoff_hash_table (info)->linkage_section = lsec; |
dc810e39 AM |
1078 | lsec->alignment_power = 2; |
1079 | } | |
beb1bf64 TR |
1080 | |
1081 | /* Likewise for the TOC section. */ | |
dc810e39 AM |
1082 | if (xcoff_hash_table (info)->toc_section == NULL) |
1083 | { | |
1084 | asection *tsec; | |
117ed4f8 AM |
1085 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
1086 | | SEC_IN_MEMORY); | |
beb1bf64 | 1087 | |
117ed4f8 | 1088 | tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags); |
dc810e39 | 1089 | if (tsec == NULL) |
116c20d2 | 1090 | goto end_return; |
beb1bf64 | 1091 | |
dc810e39 | 1092 | xcoff_hash_table (info)->toc_section = tsec; |
dc810e39 | 1093 | tsec->alignment_power = 2; |
beb1bf64 TR |
1094 | } |
1095 | ||
dc810e39 AM |
1096 | /* Likewise for the descriptor section. */ |
1097 | if (xcoff_hash_table (info)->descriptor_section == NULL) | |
1098 | { | |
1099 | asection *dsec; | |
117ed4f8 AM |
1100 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
1101 | | SEC_IN_MEMORY); | |
dc810e39 | 1102 | |
117ed4f8 | 1103 | dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags); |
dc810e39 | 1104 | if (dsec == NULL) |
116c20d2 | 1105 | goto end_return; |
dc810e39 AM |
1106 | |
1107 | xcoff_hash_table (info)->descriptor_section = dsec; | |
dc810e39 AM |
1108 | dsec->alignment_power = 2; |
1109 | } | |
beb1bf64 TR |
1110 | |
1111 | /* Likewise for the .debug section. */ | |
1112 | if (xcoff_hash_table (info)->debug_section == NULL | |
dc810e39 AM |
1113 | && info->strip != strip_all) |
1114 | { | |
1115 | asection *dsec; | |
117ed4f8 | 1116 | flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY; |
beb1bf64 | 1117 | |
117ed4f8 | 1118 | dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags); |
dc810e39 | 1119 | if (dsec == NULL) |
116c20d2 NC |
1120 | goto end_return; |
1121 | ||
dc810e39 | 1122 | xcoff_hash_table (info)->debug_section = dsec; |
beb1bf64 | 1123 | } |
dc810e39 AM |
1124 | } |
1125 | ||
b34976b6 | 1126 | return_value = TRUE; |
beb1bf64 TR |
1127 | |
1128 | end_return: | |
1129 | ||
1130 | return return_value; | |
1131 | } | |
1132 | ||
116c20d2 NC |
1133 | /* Returns the index of reloc in RELOCS with the least address greater |
1134 | than or equal to ADDRESS. The relocs are sorted by address. */ | |
1135 | ||
1136 | static bfd_size_type | |
1137 | xcoff_find_reloc (struct internal_reloc *relocs, | |
1138 | bfd_size_type count, | |
1139 | bfd_vma address) | |
1140 | { | |
1141 | bfd_size_type min, max, this; | |
1142 | ||
1143 | if (count < 2) | |
1144 | { | |
1145 | if (count == 1 && relocs[0].r_vaddr < address) | |
1146 | return 1; | |
1147 | else | |
1148 | return 0; | |
1149 | } | |
1150 | ||
1151 | min = 0; | |
1152 | max = count; | |
1153 | ||
1154 | /* Do a binary search over (min,max]. */ | |
1155 | while (min + 1 < max) | |
1156 | { | |
1157 | bfd_vma raddr; | |
1158 | ||
1159 | this = (max + min) / 2; | |
1160 | raddr = relocs[this].r_vaddr; | |
1161 | if (raddr > address) | |
1162 | max = this; | |
1163 | else if (raddr < address) | |
1164 | min = this; | |
1165 | else | |
1166 | { | |
1167 | min = this; | |
1168 | break; | |
1169 | } | |
1170 | } | |
1171 | ||
1172 | if (relocs[min].r_vaddr < address) | |
1173 | return min + 1; | |
1174 | ||
1175 | while (min > 0 | |
1176 | && relocs[min - 1].r_vaddr == address) | |
1177 | --min; | |
1178 | ||
1179 | return min; | |
1180 | } | |
1181 | ||
252b5132 RH |
1182 | /* Add all the symbols from an object file to the hash table. |
1183 | ||
1184 | XCOFF is a weird format. A normal XCOFF .o files will have three | |
1185 | COFF sections--.text, .data, and .bss--but each COFF section will | |
1186 | contain many csects. These csects are described in the symbol | |
1187 | table. From the linker's point of view, each csect must be | |
1188 | considered a section in its own right. For example, a TOC entry is | |
1189 | handled as a small XMC_TC csect. The linker must be able to merge | |
1190 | different TOC entries together, which means that it must be able to | |
1191 | extract the XMC_TC csects from the .data section of the input .o | |
1192 | file. | |
1193 | ||
1194 | From the point of view of our linker, this is, of course, a hideous | |
1195 | nightmare. We cope by actually creating sections for each csect, | |
1196 | and discarding the original sections. We then have to handle the | |
1197 | relocation entries carefully, since the only way to tell which | |
1198 | csect they belong to is to examine the address. */ | |
1199 | ||
b34976b6 | 1200 | static bfd_boolean |
116c20d2 | 1201 | xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
1202 | { |
1203 | unsigned int n_tmask; | |
1204 | unsigned int n_btshft; | |
b34976b6 | 1205 | bfd_boolean default_copy; |
252b5132 RH |
1206 | bfd_size_type symcount; |
1207 | struct xcoff_link_hash_entry **sym_hash; | |
1208 | asection **csect_cache; | |
3df13c4a | 1209 | unsigned int *lineno_counts; |
252b5132 RH |
1210 | bfd_size_type linesz; |
1211 | asection *o; | |
1212 | asection *last_real; | |
b34976b6 | 1213 | bfd_boolean keep_syms; |
252b5132 RH |
1214 | asection *csect; |
1215 | unsigned int csect_index; | |
1216 | asection *first_csect; | |
1217 | bfd_size_type symesz; | |
1218 | bfd_byte *esym; | |
1219 | bfd_byte *esym_end; | |
dc810e39 | 1220 | struct reloc_info_struct |
beb1bf64 TR |
1221 | { |
1222 | struct internal_reloc *relocs; | |
1223 | asection **csects; | |
1224 | bfd_byte *linenos; | |
1225 | } *reloc_info = NULL; | |
dc810e39 | 1226 | bfd_size_type amt; |
252b5132 RH |
1227 | |
1228 | keep_syms = obj_coff_keep_syms (abfd); | |
1229 | ||
1230 | if ((abfd->flags & DYNAMIC) != 0 | |
dc810e39 AM |
1231 | && ! info->static_link) |
1232 | { | |
1233 | if (! xcoff_link_add_dynamic_symbols (abfd, info)) | |
b34976b6 | 1234 | return FALSE; |
252b5132 RH |
1235 | } |
1236 | ||
116c20d2 | 1237 | /* Create the loader, toc, gl, ds and debug sections, if needed. */ |
b34976b6 | 1238 | if (! xcoff_link_create_extra_sections (abfd, info)) |
69f284c7 | 1239 | goto error_return; |
252b5132 RH |
1240 | |
1241 | if ((abfd->flags & DYNAMIC) != 0 | |
1242 | && ! info->static_link) | |
b34976b6 | 1243 | return TRUE; |
252b5132 RH |
1244 | |
1245 | n_tmask = coff_data (abfd)->local_n_tmask; | |
1246 | n_btshft = coff_data (abfd)->local_n_btshft; | |
1247 | ||
1248 | /* Define macros so that ISFCN, et. al., macros work correctly. */ | |
1249 | #define N_TMASK n_tmask | |
1250 | #define N_BTSHFT n_btshft | |
1251 | ||
1252 | if (info->keep_memory) | |
b34976b6 | 1253 | default_copy = FALSE; |
252b5132 | 1254 | else |
b34976b6 | 1255 | default_copy = TRUE; |
252b5132 | 1256 | |
dc810e39 | 1257 | symcount = obj_raw_syment_count (abfd); |
252b5132 RH |
1258 | |
1259 | /* We keep a list of the linker hash table entries that correspond | |
1260 | to each external symbol. */ | |
dc810e39 | 1261 | amt = symcount * sizeof (struct xcoff_link_hash_entry *); |
116c20d2 | 1262 | sym_hash = bfd_zalloc (abfd, amt); |
252b5132 RH |
1263 | if (sym_hash == NULL && symcount != 0) |
1264 | goto error_return; | |
1265 | coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash; | |
252b5132 RH |
1266 | |
1267 | /* Because of the weird stuff we are doing with XCOFF csects, we can | |
1268 | not easily determine which section a symbol is in, so we store | |
1269 | the information in the tdata for the input file. */ | |
dc810e39 | 1270 | amt = symcount * sizeof (asection *); |
116c20d2 | 1271 | csect_cache = bfd_zalloc (abfd, amt); |
252b5132 RH |
1272 | if (csect_cache == NULL && symcount != 0) |
1273 | goto error_return; | |
1274 | xcoff_data (abfd)->csects = csect_cache; | |
252b5132 | 1275 | |
3df13c4a RS |
1276 | /* We garbage-collect line-number information on a symbol-by-symbol |
1277 | basis, so we need to have quick access to the number of entries | |
1278 | per symbol. */ | |
1279 | amt = symcount * sizeof (unsigned int); | |
1280 | lineno_counts = bfd_zalloc (abfd, amt); | |
1281 | if (lineno_counts == NULL && symcount != 0) | |
1282 | goto error_return; | |
1283 | xcoff_data (abfd)->lineno_counts = lineno_counts; | |
1284 | ||
252b5132 RH |
1285 | /* While splitting sections into csects, we need to assign the |
1286 | relocs correctly. The relocs and the csects must both be in | |
1287 | order by VMA within a given section, so we handle this by | |
1288 | scanning along the relocs as we process the csects. We index | |
1289 | into reloc_info using the section target_index. */ | |
dc810e39 AM |
1290 | amt = abfd->section_count + 1; |
1291 | amt *= sizeof (struct reloc_info_struct); | |
116c20d2 | 1292 | reloc_info = bfd_zmalloc (amt); |
252b5132 RH |
1293 | if (reloc_info == NULL) |
1294 | goto error_return; | |
252b5132 RH |
1295 | |
1296 | /* Read in the relocs and line numbers for each section. */ | |
1297 | linesz = bfd_coff_linesz (abfd); | |
1298 | last_real = NULL; | |
dc810e39 AM |
1299 | for (o = abfd->sections; o != NULL; o = o->next) |
1300 | { | |
dc810e39 | 1301 | last_real = o; |
116c20d2 | 1302 | |
dc810e39 AM |
1303 | if ((o->flags & SEC_RELOC) != 0) |
1304 | { | |
dc810e39 | 1305 | reloc_info[o->target_index].relocs = |
116c20d2 | 1306 | xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL); |
dc810e39 AM |
1307 | amt = o->reloc_count; |
1308 | amt *= sizeof (asection *); | |
116c20d2 | 1309 | reloc_info[o->target_index].csects = bfd_zmalloc (amt); |
dc810e39 AM |
1310 | if (reloc_info[o->target_index].csects == NULL) |
1311 | goto error_return; | |
dc810e39 | 1312 | } |
beb1bf64 | 1313 | |
dc810e39 AM |
1314 | if ((info->strip == strip_none || info->strip == strip_some) |
1315 | && o->lineno_count > 0) | |
1316 | { | |
dc810e39 AM |
1317 | bfd_byte *linenos; |
1318 | ||
1319 | amt = linesz * o->lineno_count; | |
116c20d2 | 1320 | linenos = bfd_malloc (amt); |
dc810e39 AM |
1321 | if (linenos == NULL) |
1322 | goto error_return; | |
1323 | reloc_info[o->target_index].linenos = linenos; | |
1324 | if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0 | |
1325 | || bfd_bread (linenos, amt, abfd) != amt) | |
1326 | goto error_return; | |
dc810e39 | 1327 | } |
252b5132 | 1328 | } |
beb1bf64 | 1329 | |
252b5132 | 1330 | /* Don't let the linker relocation routines discard the symbols. */ |
b34976b6 | 1331 | obj_coff_keep_syms (abfd) = TRUE; |
252b5132 RH |
1332 | |
1333 | csect = NULL; | |
1334 | csect_index = 0; | |
1335 | first_csect = NULL; | |
1336 | ||
1337 | symesz = bfd_coff_symesz (abfd); | |
1338 | BFD_ASSERT (symesz == bfd_coff_auxesz (abfd)); | |
1339 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
1340 | esym_end = esym + symcount * symesz; | |
252b5132 | 1341 | |
dc810e39 AM |
1342 | while (esym < esym_end) |
1343 | { | |
1344 | struct internal_syment sym; | |
1345 | union internal_auxent aux; | |
1346 | const char *name; | |
1347 | char buf[SYMNMLEN + 1]; | |
1348 | int smtyp; | |
dc810e39 AM |
1349 | asection *section; |
1350 | bfd_vma value; | |
1351 | struct xcoff_link_hash_entry *set_toc; | |
252b5132 | 1352 | |
116c20d2 | 1353 | bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym); |
dc810e39 AM |
1354 | |
1355 | /* In this pass we are only interested in symbols with csect | |
1356 | information. */ | |
8602d4fe | 1357 | if (!CSECT_SYM_P (sym.n_sclass)) |
dc810e39 | 1358 | { |
dc810e39 AM |
1359 | /* Set csect_cache, |
1360 | Normally csect is a .pr, .rw etc. created in the loop | |
1361 | If C_FILE or first time, handle special | |
252b5132 | 1362 | |
4cc02a02 | 1363 | Advance esym, sym_hash, csect_hash ptrs. */ |
5ccfed9b | 1364 | if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF) |
4cc02a02 | 1365 | csect = NULL; |
dc810e39 AM |
1366 | if (csect != NULL) |
1367 | *csect_cache = csect; | |
5ccfed9b | 1368 | else if (first_csect == NULL |
07d6d2b8 | 1369 | || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF) |
dc810e39 AM |
1370 | *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum); |
1371 | else | |
1372 | *csect_cache = NULL; | |
1373 | esym += (sym.n_numaux + 1) * symesz; | |
1374 | sym_hash += sym.n_numaux + 1; | |
1375 | csect_cache += sym.n_numaux + 1; | |
3df13c4a | 1376 | lineno_counts += sym.n_numaux + 1; |
dc810e39 AM |
1377 | |
1378 | continue; | |
1379 | } | |
1380 | ||
1381 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
1382 | ||
1383 | if (name == NULL) | |
1384 | goto error_return; | |
252b5132 RH |
1385 | |
1386 | /* If this symbol has line number information attached to it, | |
b34976b6 AM |
1387 | and we're not stripping it, count the number of entries and |
1388 | add them to the count for this csect. In the final link pass | |
1389 | we are going to attach line number information by symbol, | |
1390 | rather than by section, in order to more easily handle | |
1391 | garbage collection. */ | |
dc810e39 AM |
1392 | if ((info->strip == strip_none || info->strip == strip_some) |
1393 | && sym.n_numaux > 1 | |
1394 | && csect != NULL | |
1395 | && ISFCN (sym.n_type)) | |
1396 | { | |
dc810e39 | 1397 | union internal_auxent auxlin; |
252b5132 | 1398 | |
116c20d2 | 1399 | bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz), |
dc810e39 | 1400 | sym.n_type, sym.n_sclass, |
116c20d2 | 1401 | 0, sym.n_numaux, (void *) &auxlin); |
dc810e39 AM |
1402 | |
1403 | if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) | |
1404 | { | |
1405 | asection *enclosing; | |
1406 | bfd_signed_vma linoff; | |
1407 | ||
1408 | enclosing = xcoff_section_data (abfd, csect)->enclosing; | |
1409 | if (enclosing == NULL) | |
1410 | { | |
4eca0228 | 1411 | _bfd_error_handler |
695344c0 | 1412 | /* xgettext:c-format */ |
871b3ab2 | 1413 | (_("%pB: `%s' has line numbers but no enclosing section"), |
d003868e | 1414 | abfd, name); |
dc810e39 AM |
1415 | bfd_set_error (bfd_error_bad_value); |
1416 | goto error_return; | |
1417 | } | |
1418 | linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr | |
1419 | - enclosing->line_filepos); | |
116c20d2 | 1420 | /* Explicit cast to bfd_signed_vma for compiler. */ |
dc810e39 AM |
1421 | if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz)) |
1422 | { | |
1423 | struct internal_lineno lin; | |
1424 | bfd_byte *linpstart; | |
1425 | ||
1426 | linpstart = (reloc_info[enclosing->target_index].linenos | |
1427 | + linoff); | |
116c20d2 | 1428 | bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin); |
dc810e39 AM |
1429 | if (lin.l_lnno == 0 |
1430 | && ((bfd_size_type) lin.l_addr.l_symndx | |
1431 | == ((esym | |
1432 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1433 | / symesz))) | |
1434 | { | |
1435 | bfd_byte *linpend, *linp; | |
1436 | ||
1437 | linpend = (reloc_info[enclosing->target_index].linenos | |
1438 | + enclosing->lineno_count * linesz); | |
1439 | for (linp = linpstart + linesz; | |
1440 | linp < linpend; | |
1441 | linp += linesz) | |
1442 | { | |
116c20d2 NC |
1443 | bfd_coff_swap_lineno_in (abfd, (void *) linp, |
1444 | (void *) &lin); | |
dc810e39 AM |
1445 | if (lin.l_lnno == 0) |
1446 | break; | |
1447 | } | |
3df13c4a | 1448 | *lineno_counts = (linp - linpstart) / linesz; |
dc810e39 AM |
1449 | /* The setting of line_filepos will only be |
1450 | useful if all the line number entries for a | |
1451 | csect are contiguous; this only matters for | |
1452 | error reporting. */ | |
1453 | if (csect->line_filepos == 0) | |
1454 | csect->line_filepos = | |
1455 | auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr; | |
1456 | } | |
1457 | } | |
beb1bf64 | 1458 | } |
beb1bf64 | 1459 | } |
252b5132 | 1460 | |
dc810e39 | 1461 | /* Pick up the csect auxiliary information. */ |
dc810e39 AM |
1462 | if (sym.n_numaux == 0) |
1463 | { | |
4eca0228 | 1464 | _bfd_error_handler |
695344c0 | 1465 | /* xgettext:c-format */ |
871b3ab2 | 1466 | (_("%pB: class %d symbol `%s' has no aux entries"), |
d003868e | 1467 | abfd, sym.n_sclass, name); |
dc810e39 AM |
1468 | bfd_set_error (bfd_error_bad_value); |
1469 | goto error_return; | |
1470 | } | |
beb1bf64 | 1471 | |
dc810e39 | 1472 | bfd_coff_swap_aux_in (abfd, |
116c20d2 | 1473 | (void *) (esym + symesz * sym.n_numaux), |
dc810e39 | 1474 | sym.n_type, sym.n_sclass, |
252b5132 | 1475 | sym.n_numaux - 1, sym.n_numaux, |
116c20d2 | 1476 | (void *) &aux); |
252b5132 RH |
1477 | |
1478 | smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); | |
1479 | ||
252b5132 RH |
1480 | section = NULL; |
1481 | value = 0; | |
1482 | set_toc = NULL; | |
1483 | ||
1484 | switch (smtyp) | |
1485 | { | |
1486 | default: | |
4eca0228 | 1487 | _bfd_error_handler |
695344c0 | 1488 | /* xgettext:c-format */ |
871b3ab2 | 1489 | (_("%pB: symbol `%s' has unrecognized csect type %d"), |
d003868e | 1490 | abfd, name, smtyp); |
252b5132 RH |
1491 | bfd_set_error (bfd_error_bad_value); |
1492 | goto error_return; | |
1493 | ||
1494 | case XTY_ER: | |
1495 | /* This is an external reference. */ | |
1496 | if (sym.n_sclass == C_HIDEXT | |
1497 | || sym.n_scnum != N_UNDEF | |
1498 | || aux.x_csect.x_scnlen.l != 0) | |
1499 | { | |
4eca0228 | 1500 | _bfd_error_handler |
695344c0 | 1501 | /* xgettext:c-format */ |
2dcf00ce AM |
1502 | (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d " |
1503 | "scnlen %" PRId64), | |
d003868e | 1504 | abfd, name, sym.n_sclass, sym.n_scnum, |
2dcf00ce | 1505 | (int64_t) aux.x_csect.x_scnlen.l); |
252b5132 RH |
1506 | bfd_set_error (bfd_error_bad_value); |
1507 | goto error_return; | |
1508 | } | |
1509 | ||
1510 | /* An XMC_XO external reference is actually a reference to | |
b34976b6 | 1511 | an absolute location. */ |
252b5132 RH |
1512 | if (aux.x_csect.x_smclas != XMC_XO) |
1513 | section = bfd_und_section_ptr; | |
1514 | else | |
1515 | { | |
1516 | section = bfd_abs_section_ptr; | |
1517 | value = sym.n_value; | |
1518 | } | |
1519 | break; | |
1520 | ||
1521 | case XTY_SD: | |
252b5132 | 1522 | csect = NULL; |
dc810e39 | 1523 | csect_index = -(unsigned) 1; |
252b5132 RH |
1524 | |
1525 | /* When we see a TOC anchor, we record the TOC value. */ | |
1526 | if (aux.x_csect.x_smclas == XMC_TC0) | |
1527 | { | |
1528 | if (sym.n_sclass != C_HIDEXT | |
1529 | || aux.x_csect.x_scnlen.l != 0) | |
1530 | { | |
4eca0228 | 1531 | _bfd_error_handler |
695344c0 | 1532 | /* xgettext:c-format */ |
2dcf00ce AM |
1533 | (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64), |
1534 | abfd, name, sym.n_sclass, (int64_t) aux.x_csect.x_scnlen.l); | |
252b5132 RH |
1535 | bfd_set_error (bfd_error_bad_value); |
1536 | goto error_return; | |
1537 | } | |
1538 | xcoff_data (abfd)->toc = sym.n_value; | |
1539 | } | |
1540 | ||
dc810e39 AM |
1541 | /* We must merge TOC entries for the same symbol. We can |
1542 | merge two TOC entries if they are both C_HIDEXT, they | |
1543 | both have the same name, they are both 4 or 8 bytes long, and | |
1544 | they both have a relocation table entry for an external | |
1545 | symbol with the same name. Unfortunately, this means | |
1546 | that we must look through the relocations. Ick. | |
1547 | ||
1548 | Logic for 32 bit vs 64 bit. | |
1549 | 32 bit has a csect length of 4 for TOC | |
1550 | 64 bit has a csect length of 8 for TOC | |
1551 | ||
1552 | The conditions to get past the if-check are not that bad. | |
1553 | They are what is used to create the TOC csects in the first | |
1554 | place. */ | |
1555 | if (aux.x_csect.x_smclas == XMC_TC | |
1556 | && sym.n_sclass == C_HIDEXT | |
f13a99db | 1557 | && info->output_bfd->xvec == abfd->xvec |
dc810e39 AM |
1558 | && ((bfd_xcoff_is_xcoff32 (abfd) |
1559 | && aux.x_csect.x_scnlen.l == 4) | |
1560 | || (bfd_xcoff_is_xcoff64 (abfd) | |
1561 | && aux.x_csect.x_scnlen.l == 8))) | |
1562 | { | |
1563 | asection *enclosing; | |
1564 | struct internal_reloc *relocs; | |
1565 | bfd_size_type relindx; | |
1566 | struct internal_reloc *rel; | |
252b5132 | 1567 | |
dc810e39 AM |
1568 | enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); |
1569 | if (enclosing == NULL) | |
1570 | goto error_return; | |
252b5132 | 1571 | |
dc810e39 AM |
1572 | relocs = reloc_info[enclosing->target_index].relocs; |
1573 | amt = enclosing->reloc_count; | |
1574 | relindx = xcoff_find_reloc (relocs, amt, sym.n_value); | |
1575 | rel = relocs + relindx; | |
252b5132 | 1576 | |
dc810e39 AM |
1577 | /* 32 bit R_POS r_size is 31 |
1578 | 64 bit R_POS r_size is 63 */ | |
1579 | if (relindx < enclosing->reloc_count | |
1580 | && rel->r_vaddr == (bfd_vma) sym.n_value | |
1581 | && rel->r_type == R_POS | |
1582 | && ((bfd_xcoff_is_xcoff32 (abfd) | |
1583 | && rel->r_size == 31) | |
1584 | || (bfd_xcoff_is_xcoff64 (abfd) | |
1585 | && rel->r_size == 63))) | |
1586 | { | |
1587 | bfd_byte *erelsym; | |
1588 | ||
1589 | struct internal_syment relsym; | |
1590 | ||
1591 | erelsym = ((bfd_byte *) obj_coff_external_syms (abfd) | |
1592 | + rel->r_symndx * symesz); | |
116c20d2 | 1593 | bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym); |
8602d4fe | 1594 | if (EXTERN_SYM_P (relsym.n_sclass)) |
dc810e39 AM |
1595 | { |
1596 | const char *relname; | |
1597 | char relbuf[SYMNMLEN + 1]; | |
b34976b6 | 1598 | bfd_boolean copy; |
dc810e39 AM |
1599 | struct xcoff_link_hash_entry *h; |
1600 | ||
1601 | /* At this point we know that the TOC entry is | |
1602 | for an externally visible symbol. */ | |
dc810e39 AM |
1603 | relname = _bfd_coff_internal_syment_name (abfd, &relsym, |
1604 | relbuf); | |
1605 | if (relname == NULL) | |
1606 | goto error_return; | |
1607 | ||
1608 | /* We only merge TOC entries if the TC name is | |
1609 | the same as the symbol name. This handles | |
1610 | the normal case, but not common cases like | |
1611 | SYM.P4 which gcc generates to store SYM + 4 | |
1612 | in the TOC. FIXME. */ | |
dc810e39 AM |
1613 | if (strcmp (name, relname) == 0) |
1614 | { | |
1615 | copy = (! info->keep_memory | |
1616 | || relsym._n._n_n._n_zeroes != 0 | |
1617 | || relsym._n._n_n._n_offset == 0); | |
1618 | h = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
b34976b6 AM |
1619 | relname, TRUE, copy, |
1620 | FALSE); | |
dc810e39 AM |
1621 | if (h == NULL) |
1622 | goto error_return; | |
1623 | ||
1624 | /* At this point h->root.type could be | |
1625 | bfd_link_hash_new. That should be OK, | |
1626 | since we know for sure that we will come | |
1627 | across this symbol as we step through the | |
1628 | file. */ | |
1629 | ||
1630 | /* We store h in *sym_hash for the | |
1631 | convenience of the relocate_section | |
1632 | function. */ | |
1633 | *sym_hash = h; | |
1634 | ||
1635 | if (h->toc_section != NULL) | |
1636 | { | |
1637 | asection **rel_csects; | |
1638 | ||
1639 | /* We already have a TOC entry for this | |
1640 | symbol, so we can just ignore this | |
1641 | one. */ | |
1642 | rel_csects = | |
1643 | reloc_info[enclosing->target_index].csects; | |
1644 | rel_csects[relindx] = bfd_und_section_ptr; | |
1645 | break; | |
1646 | } | |
1647 | ||
1648 | /* We are about to create a TOC entry for | |
1649 | this symbol. */ | |
1650 | set_toc = h; | |
116c20d2 NC |
1651 | } |
1652 | } | |
1653 | } | |
1654 | } | |
252b5132 | 1655 | |
252b5132 | 1656 | { |
252b5132 RH |
1657 | asection *enclosing; |
1658 | ||
beb1bf64 TR |
1659 | /* We need to create a new section. We get the name from |
1660 | the csect storage mapping class, so that the linker can | |
1661 | accumulate similar csects together. */ | |
252b5132 | 1662 | |
beb1bf64 | 1663 | csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name); |
dc810e39 | 1664 | if (NULL == csect) |
116c20d2 | 1665 | goto error_return; |
beb1bf64 | 1666 | |
dc810e39 AM |
1667 | /* The enclosing section is the main section : .data, .text |
1668 | or .bss that the csect is coming from. */ | |
252b5132 RH |
1669 | enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); |
1670 | if (enclosing == NULL) | |
1671 | goto error_return; | |
beb1bf64 | 1672 | |
dc810e39 AM |
1673 | if (! bfd_is_abs_section (enclosing) |
1674 | && ((bfd_vma) sym.n_value < enclosing->vma | |
1675 | || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l | |
eea6121a | 1676 | > enclosing->vma + enclosing->size))) |
dc810e39 | 1677 | { |
4eca0228 | 1678 | _bfd_error_handler |
695344c0 | 1679 | /* xgettext:c-format */ |
871b3ab2 | 1680 | (_("%pB: csect `%s' not in enclosing section"), |
d003868e | 1681 | abfd, name); |
dc810e39 AM |
1682 | bfd_set_error (bfd_error_bad_value); |
1683 | goto error_return; | |
1684 | } | |
252b5132 RH |
1685 | csect->vma = sym.n_value; |
1686 | csect->filepos = (enclosing->filepos | |
1687 | + sym.n_value | |
1688 | - enclosing->vma); | |
eea6121a | 1689 | csect->size = aux.x_csect.x_scnlen.l; |
252b5132 RH |
1690 | csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; |
1691 | csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); | |
1692 | ||
1693 | /* Record the enclosing section in the tdata for this new | |
1694 | section. */ | |
dc810e39 | 1695 | amt = sizeof (struct coff_section_tdata); |
116c20d2 | 1696 | csect->used_by_bfd = bfd_zalloc (abfd, amt); |
252b5132 RH |
1697 | if (csect->used_by_bfd == NULL) |
1698 | goto error_return; | |
dc810e39 AM |
1699 | amt = sizeof (struct xcoff_section_tdata); |
1700 | coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt); | |
252b5132 RH |
1701 | if (coff_section_data (abfd, csect)->tdata == NULL) |
1702 | goto error_return; | |
1703 | xcoff_section_data (abfd, csect)->enclosing = enclosing; | |
1704 | xcoff_section_data (abfd, csect)->lineno_count = | |
1705 | enclosing->lineno_count; | |
1706 | ||
dc810e39 AM |
1707 | if (enclosing->owner == abfd) |
1708 | { | |
1709 | struct internal_reloc *relocs; | |
1710 | bfd_size_type relindx; | |
1711 | struct internal_reloc *rel; | |
1712 | asection **rel_csect; | |
1713 | ||
1714 | relocs = reloc_info[enclosing->target_index].relocs; | |
1715 | amt = enclosing->reloc_count; | |
1716 | relindx = xcoff_find_reloc (relocs, amt, csect->vma); | |
1717 | ||
1718 | rel = relocs + relindx; | |
1719 | rel_csect = (reloc_info[enclosing->target_index].csects | |
1720 | + relindx); | |
1721 | ||
1722 | csect->rel_filepos = (enclosing->rel_filepos | |
1723 | + relindx * bfd_coff_relsz (abfd)); | |
1724 | while (relindx < enclosing->reloc_count | |
1725 | && *rel_csect == NULL | |
eea6121a | 1726 | && rel->r_vaddr < csect->vma + csect->size) |
dc810e39 | 1727 | { |
beb1bf64 | 1728 | |
dc810e39 AM |
1729 | *rel_csect = csect; |
1730 | csect->flags |= SEC_RELOC; | |
1731 | ++csect->reloc_count; | |
1732 | ++relindx; | |
1733 | ++rel; | |
1734 | ++rel_csect; | |
1735 | } | |
252b5132 RH |
1736 | } |
1737 | ||
1738 | /* There are a number of other fields and section flags | |
1739 | which we do not bother to set. */ | |
1740 | ||
1741 | csect_index = ((esym | |
1742 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1743 | / symesz); | |
1744 | ||
1745 | xcoff_section_data (abfd, csect)->first_symndx = csect_index; | |
1746 | ||
1747 | if (first_csect == NULL) | |
1748 | first_csect = csect; | |
1749 | ||
8602d4fe | 1750 | /* If this symbol is external, we treat it as starting at the |
252b5132 | 1751 | beginning of the newly created section. */ |
8602d4fe | 1752 | if (EXTERN_SYM_P (sym.n_sclass)) |
252b5132 RH |
1753 | { |
1754 | section = csect; | |
1755 | value = 0; | |
1756 | } | |
1757 | ||
1758 | /* If this is a TOC section for a symbol, record it. */ | |
1759 | if (set_toc != NULL) | |
1760 | set_toc->toc_section = csect; | |
1761 | } | |
1762 | break; | |
1763 | ||
1764 | case XTY_LD: | |
1765 | /* This is a label definition. The x_scnlen field is the | |
dc810e39 | 1766 | symbol index of the csect. Usually the XTY_LD symbol will |
8df8c619 TR |
1767 | follow its appropriate XTY_SD symbol. The .set pseudo op can |
1768 | cause the XTY_LD to not follow the XTY_SD symbol. */ | |
252b5132 | 1769 | { |
b34976b6 | 1770 | bfd_boolean bad; |
252b5132 | 1771 | |
b34976b6 | 1772 | bad = FALSE; |
252b5132 RH |
1773 | if (aux.x_csect.x_scnlen.l < 0 |
1774 | || (aux.x_csect.x_scnlen.l | |
1775 | >= esym - (bfd_byte *) obj_coff_external_syms (abfd))) | |
b34976b6 | 1776 | bad = TRUE; |
252b5132 RH |
1777 | if (! bad) |
1778 | { | |
1779 | section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l]; | |
1780 | if (section == NULL | |
1781 | || (section->flags & SEC_HAS_CONTENTS) == 0) | |
b34976b6 | 1782 | bad = TRUE; |
252b5132 RH |
1783 | } |
1784 | if (bad) | |
1785 | { | |
4eca0228 | 1786 | _bfd_error_handler |
695344c0 | 1787 | /* xgettext:c-format */ |
871b3ab2 | 1788 | (_("%pB: misplaced XTY_LD `%s'"), |
d003868e | 1789 | abfd, name); |
252b5132 RH |
1790 | bfd_set_error (bfd_error_bad_value); |
1791 | goto error_return; | |
1792 | } | |
07d6d2b8 | 1793 | csect = section; |
252b5132 RH |
1794 | value = sym.n_value - csect->vma; |
1795 | } | |
1796 | break; | |
1797 | ||
1798 | case XTY_CM: | |
1799 | /* This is an unitialized csect. We could base the name on | |
b34976b6 AM |
1800 | the storage mapping class, but we don't bother except for |
1801 | an XMC_TD symbol. If this csect is externally visible, | |
1802 | it is a common symbol. We put XMC_TD symbols in sections | |
1803 | named .tocbss, and rely on the linker script to put that | |
1804 | in the TOC area. */ | |
252b5132 | 1805 | |
dc810e39 AM |
1806 | if (aux.x_csect.x_smclas == XMC_TD) |
1807 | { | |
1808 | /* The linker script puts the .td section in the data | |
1809 | section after the .tc section. */ | |
117ed4f8 AM |
1810 | csect = bfd_make_section_anyway_with_flags (abfd, ".td", |
1811 | SEC_ALLOC); | |
dc810e39 AM |
1812 | } |
1813 | else | |
117ed4f8 AM |
1814 | csect = bfd_make_section_anyway_with_flags (abfd, ".bss", |
1815 | SEC_ALLOC); | |
116c20d2 | 1816 | |
252b5132 RH |
1817 | if (csect == NULL) |
1818 | goto error_return; | |
1819 | csect->vma = sym.n_value; | |
eea6121a | 1820 | csect->size = aux.x_csect.x_scnlen.l; |
252b5132 RH |
1821 | csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); |
1822 | /* There are a number of other fields and section flags | |
1823 | which we do not bother to set. */ | |
1824 | ||
1825 | csect_index = ((esym | |
1826 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1827 | / symesz); | |
1828 | ||
dc810e39 | 1829 | amt = sizeof (struct coff_section_tdata); |
116c20d2 | 1830 | csect->used_by_bfd = bfd_zalloc (abfd, amt); |
252b5132 RH |
1831 | if (csect->used_by_bfd == NULL) |
1832 | goto error_return; | |
dc810e39 AM |
1833 | amt = sizeof (struct xcoff_section_tdata); |
1834 | coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt); | |
252b5132 RH |
1835 | if (coff_section_data (abfd, csect)->tdata == NULL) |
1836 | goto error_return; | |
1837 | xcoff_section_data (abfd, csect)->first_symndx = csect_index; | |
1838 | ||
1839 | if (first_csect == NULL) | |
1840 | first_csect = csect; | |
1841 | ||
8602d4fe | 1842 | if (EXTERN_SYM_P (sym.n_sclass)) |
252b5132 RH |
1843 | { |
1844 | csect->flags |= SEC_IS_COMMON; | |
eea6121a | 1845 | csect->size = 0; |
252b5132 RH |
1846 | section = csect; |
1847 | value = aux.x_csect.x_scnlen.l; | |
1848 | } | |
1849 | ||
1850 | break; | |
1851 | } | |
1852 | ||
1853 | /* Check for magic symbol names. */ | |
dc810e39 AM |
1854 | if ((smtyp == XTY_SD || smtyp == XTY_CM) |
1855 | && aux.x_csect.x_smclas != XMC_TC | |
1856 | && aux.x_csect.x_smclas != XMC_TD) | |
1857 | { | |
dc810e39 AM |
1858 | int i = -1; |
1859 | ||
1860 | if (name[0] == '_') | |
1861 | { | |
1862 | if (strcmp (name, "_text") == 0) | |
1863 | i = XCOFF_SPECIAL_SECTION_TEXT; | |
1864 | else if (strcmp (name, "_etext") == 0) | |
1865 | i = XCOFF_SPECIAL_SECTION_ETEXT; | |
1866 | else if (strcmp (name, "_data") == 0) | |
1867 | i = XCOFF_SPECIAL_SECTION_DATA; | |
1868 | else if (strcmp (name, "_edata") == 0) | |
1869 | i = XCOFF_SPECIAL_SECTION_EDATA; | |
1870 | else if (strcmp (name, "_end") == 0) | |
1871 | i = XCOFF_SPECIAL_SECTION_END; | |
1872 | } | |
1873 | else if (name[0] == 'e' && strcmp (name, "end") == 0) | |
116c20d2 | 1874 | i = XCOFF_SPECIAL_SECTION_END2; |
dc810e39 AM |
1875 | |
1876 | if (i != -1) | |
116c20d2 | 1877 | xcoff_hash_table (info)->special_sections[i] = csect; |
dc810e39 | 1878 | } |
252b5132 RH |
1879 | |
1880 | /* Now we have enough information to add the symbol to the | |
b34976b6 | 1881 | linker hash table. */ |
252b5132 | 1882 | |
8602d4fe | 1883 | if (EXTERN_SYM_P (sym.n_sclass)) |
252b5132 | 1884 | { |
b34976b6 | 1885 | bfd_boolean copy; |
8602d4fe | 1886 | flagword flags; |
252b5132 RH |
1887 | |
1888 | BFD_ASSERT (section != NULL); | |
1889 | ||
1890 | /* We must copy the name into memory if we got it from the | |
b34976b6 | 1891 | syment itself, rather than the string table. */ |
252b5132 RH |
1892 | copy = default_copy; |
1893 | if (sym._n._n_n._n_zeroes != 0 | |
1894 | || sym._n._n_n._n_offset == 0) | |
b34976b6 | 1895 | copy = TRUE; |
252b5132 | 1896 | |
94313f36 RS |
1897 | /* Ignore global linkage code when linking statically. */ |
1898 | if (info->static_link | |
1899 | && (smtyp == XTY_SD || smtyp == XTY_LD) | |
1900 | && aux.x_csect.x_smclas == XMC_GL) | |
1901 | { | |
1902 | section = bfd_und_section_ptr; | |
1903 | value = 0; | |
1904 | } | |
1905 | ||
252b5132 RH |
1906 | /* The AIX linker appears to only detect multiple symbol |
1907 | definitions when there is a reference to the symbol. If | |
1908 | a symbol is defined multiple times, and the only | |
1909 | references are from the same object file, the AIX linker | |
1910 | appears to permit it. It does not merge the different | |
1911 | definitions, but handles them independently. On the | |
1912 | other hand, if there is a reference, the linker reports | |
1913 | an error. | |
1914 | ||
1915 | This matters because the AIX <net/net_globals.h> header | |
1916 | file actually defines an initialized array, so we have to | |
1917 | actually permit that to work. | |
1918 | ||
1919 | Just to make matters even more confusing, the AIX linker | |
1920 | appears to permit multiple symbol definitions whenever | |
1921 | the second definition is in an archive rather than an | |
1922 | object file. This may be a consequence of the manner in | |
1923 | which it handles archives: I think it may load the entire | |
1924 | archive in as separate csects, and then let garbage | |
1925 | collection discard symbols. | |
1926 | ||
1927 | We also have to handle the case of statically linking a | |
1928 | shared object, which will cause symbol redefinitions, | |
1929 | although this is an easier case to detect. */ | |
07d6d2b8 | 1930 | else if (info->output_bfd->xvec == abfd->xvec) |
252b5132 RH |
1931 | { |
1932 | if (! bfd_is_und_section (section)) | |
116c20d2 NC |
1933 | *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info), |
1934 | name, TRUE, copy, FALSE); | |
252b5132 | 1935 | else |
116c20d2 NC |
1936 | /* Make a copy of the symbol name to prevent problems with |
1937 | merging symbols. */ | |
1938 | *sym_hash = ((struct xcoff_link_hash_entry *) | |
1939 | bfd_wrapped_link_hash_lookup (abfd, info, name, | |
1940 | TRUE, TRUE, FALSE)); | |
1941 | ||
252b5132 RH |
1942 | if (*sym_hash == NULL) |
1943 | goto error_return; | |
1944 | if (((*sym_hash)->root.type == bfd_link_hash_defined | |
1945 | || (*sym_hash)->root.type == bfd_link_hash_defweak) | |
1946 | && ! bfd_is_und_section (section) | |
1947 | && ! bfd_is_com_section (section)) | |
1948 | { | |
1949 | /* This is a second definition of a defined symbol. */ | |
94313f36 RS |
1950 | if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0 |
1951 | && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0) | |
252b5132 RH |
1952 | { |
1953 | /* The existing symbol is from a shared library. | |
b34976b6 | 1954 | Replace it. */ |
252b5132 RH |
1955 | (*sym_hash)->root.type = bfd_link_hash_undefined; |
1956 | (*sym_hash)->root.u.undef.abfd = | |
1957 | (*sym_hash)->root.u.def.section->owner; | |
1958 | } | |
1959 | else if (abfd->my_archive != NULL) | |
1960 | { | |
1961 | /* This is a redefinition in an object contained | |
b34976b6 AM |
1962 | in an archive. Just ignore it. See the |
1963 | comment above. */ | |
252b5132 RH |
1964 | section = bfd_und_section_ptr; |
1965 | value = 0; | |
1966 | } | |
8602d4fe RS |
1967 | else if (sym.n_sclass == C_AIX_WEAKEXT |
1968 | || (*sym_hash)->root.type == bfd_link_hash_defweak) | |
1969 | { | |
1970 | /* At least one of the definitions is weak. | |
1971 | Allow the normal rules to take effect. */ | |
1972 | } | |
f6e332e6 | 1973 | else if ((*sym_hash)->root.u.undef.next != NULL |
252b5132 RH |
1974 | || info->hash->undefs_tail == &(*sym_hash)->root) |
1975 | { | |
1976 | /* This symbol has been referenced. In this | |
b34976b6 AM |
1977 | case, we just continue and permit the |
1978 | multiple definition error. See the comment | |
1979 | above about the behaviour of the AIX linker. */ | |
252b5132 RH |
1980 | } |
1981 | else if ((*sym_hash)->smclas == aux.x_csect.x_smclas) | |
1982 | { | |
1983 | /* The symbols are both csects of the same | |
b34976b6 AM |
1984 | class. There is at least a chance that this |
1985 | is a semi-legitimate redefinition. */ | |
252b5132 RH |
1986 | section = bfd_und_section_ptr; |
1987 | value = 0; | |
1988 | (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED; | |
1989 | } | |
1990 | } | |
1991 | else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0 | |
8602d4fe | 1992 | && (*sym_hash)->root.type == bfd_link_hash_defined |
252b5132 RH |
1993 | && (bfd_is_und_section (section) |
1994 | || bfd_is_com_section (section))) | |
1995 | { | |
1996 | /* This is a reference to a multiply defined symbol. | |
1997 | Report the error now. See the comment above | |
1998 | about the behaviour of the AIX linker. We could | |
1999 | also do this with warning symbols, but I'm not | |
2000 | sure the XCOFF linker is wholly prepared to | |
2001 | handle them, and that would only be a warning, | |
2002 | not an error. */ | |
1a72702b AM |
2003 | (*info->callbacks->multiple_definition) (info, |
2004 | &(*sym_hash)->root, | |
2005 | NULL, NULL, | |
2006 | (bfd_vma) 0); | |
252b5132 RH |
2007 | /* Try not to give this error too many times. */ |
2008 | (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED; | |
2009 | } | |
2010 | } | |
2011 | ||
2012 | /* _bfd_generic_link_add_one_symbol may call the linker to | |
2013 | generate an error message, and the linker may try to read | |
2014 | the symbol table to give a good error. Right now, the | |
2015 | line numbers are in an inconsistent state, since they are | |
2016 | counted both in the real sections and in the new csects. | |
2017 | We need to leave the count in the real sections so that | |
2018 | the linker can report the line number of the error | |
2019 | correctly, so temporarily clobber the link to the csects | |
2020 | so that the linker will not try to read the line numbers | |
2021 | a second time from the csects. */ | |
2022 | BFD_ASSERT (last_real->next == first_csect); | |
2023 | last_real->next = NULL; | |
8602d4fe | 2024 | flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK); |
252b5132 RH |
2025 | if (! (_bfd_generic_link_add_one_symbol |
2026 | (info, abfd, name, flags, section, value, | |
116c20d2 | 2027 | NULL, copy, TRUE, |
252b5132 RH |
2028 | (struct bfd_link_hash_entry **) sym_hash))) |
2029 | goto error_return; | |
2030 | last_real->next = first_csect; | |
2031 | ||
2032 | if (smtyp == XTY_CM) | |
2033 | { | |
2034 | if ((*sym_hash)->root.type != bfd_link_hash_common | |
2035 | || (*sym_hash)->root.u.c.p->section != csect) | |
116c20d2 NC |
2036 | /* We don't need the common csect we just created. */ |
2037 | csect->size = 0; | |
252b5132 | 2038 | else |
116c20d2 NC |
2039 | (*sym_hash)->root.u.c.p->alignment_power |
2040 | = csect->alignment_power; | |
252b5132 RH |
2041 | } |
2042 | ||
07d6d2b8 | 2043 | if (info->output_bfd->xvec == abfd->xvec) |
252b5132 RH |
2044 | { |
2045 | int flag; | |
2046 | ||
94313f36 RS |
2047 | if (smtyp == XTY_ER |
2048 | || smtyp == XTY_CM | |
2049 | || section == bfd_und_section_ptr) | |
252b5132 RH |
2050 | flag = XCOFF_REF_REGULAR; |
2051 | else | |
2052 | flag = XCOFF_DEF_REGULAR; | |
2053 | (*sym_hash)->flags |= flag; | |
2054 | ||
2055 | if ((*sym_hash)->smclas == XMC_UA | |
2056 | || flag == XCOFF_DEF_REGULAR) | |
2057 | (*sym_hash)->smclas = aux.x_csect.x_smclas; | |
2058 | } | |
2059 | } | |
2060 | ||
4cc02a02 RS |
2061 | if (smtyp == XTY_ER) |
2062 | *csect_cache = section; | |
2063 | else | |
2064 | { | |
2065 | *csect_cache = csect; | |
2066 | if (csect != NULL) | |
2067 | xcoff_section_data (abfd, csect)->last_symndx | |
2068 | = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz; | |
2069 | } | |
252b5132 RH |
2070 | |
2071 | esym += (sym.n_numaux + 1) * symesz; | |
2072 | sym_hash += sym.n_numaux + 1; | |
2073 | csect_cache += sym.n_numaux + 1; | |
3df13c4a | 2074 | lineno_counts += sym.n_numaux + 1; |
252b5132 RH |
2075 | } |
2076 | ||
2077 | BFD_ASSERT (last_real == NULL || last_real->next == first_csect); | |
2078 | ||
2079 | /* Make sure that we have seen all the relocs. */ | |
2080 | for (o = abfd->sections; o != first_csect; o = o->next) | |
2081 | { | |
5ccfed9b TG |
2082 | /* Debugging sections have no csects. */ |
2083 | if (bfd_get_section_flags (abfd, o) & SEC_DEBUGGING) | |
07d6d2b8 | 2084 | continue; |
5ccfed9b | 2085 | |
252b5132 RH |
2086 | /* Reset the section size and the line number count, since the |
2087 | data is now attached to the csects. Don't reset the size of | |
2088 | the .debug section, since we need to read it below in | |
2089 | bfd_xcoff_size_dynamic_sections. */ | |
2090 | if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0) | |
eea6121a | 2091 | o->size = 0; |
252b5132 RH |
2092 | o->lineno_count = 0; |
2093 | ||
2094 | if ((o->flags & SEC_RELOC) != 0) | |
2095 | { | |
2096 | bfd_size_type i; | |
2097 | struct internal_reloc *rel; | |
2098 | asection **rel_csect; | |
2099 | ||
2100 | rel = reloc_info[o->target_index].relocs; | |
2101 | rel_csect = reloc_info[o->target_index].csects; | |
beb1bf64 | 2102 | |
252b5132 RH |
2103 | for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++) |
2104 | { | |
2105 | if (*rel_csect == NULL) | |
2106 | { | |
4eca0228 | 2107 | _bfd_error_handler |
695344c0 | 2108 | /* xgettext:c-format */ |
2dcf00ce AM |
2109 | (_("%pB: reloc %s:%" PRId64 " not in csect"), |
2110 | abfd, o->name, (int64_t) i); | |
252b5132 RH |
2111 | bfd_set_error (bfd_error_bad_value); |
2112 | goto error_return; | |
2113 | } | |
2114 | ||
858ef0ce RS |
2115 | /* We identify all function symbols that are the target |
2116 | of a relocation, so that we can create glue code for | |
2117 | functions imported from dynamic objects. */ | |
07d6d2b8 | 2118 | if (info->output_bfd->xvec == abfd->xvec |
252b5132 | 2119 | && *rel_csect != bfd_und_section_ptr |
252b5132 RH |
2120 | && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL) |
2121 | { | |
2122 | struct xcoff_link_hash_entry *h; | |
2123 | ||
2124 | h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx]; | |
252b5132 | 2125 | /* If the symbol name starts with a period, it is |
b34976b6 AM |
2126 | the code of a function. If the symbol is |
2127 | currently undefined, then add an undefined symbol | |
2128 | for the function descriptor. This should do no | |
2129 | harm, because any regular object that defines the | |
2130 | function should also define the function | |
2131 | descriptor. It helps, because it means that we | |
2132 | will identify the function descriptor with a | |
2133 | dynamic object if a dynamic object defines it. */ | |
252b5132 RH |
2134 | if (h->root.root.string[0] == '.' |
2135 | && h->descriptor == NULL) | |
2136 | { | |
2137 | struct xcoff_link_hash_entry *hds; | |
14a793b2 | 2138 | struct bfd_link_hash_entry *bh; |
252b5132 RH |
2139 | |
2140 | hds = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
2141 | h->root.root.string + 1, | |
b34976b6 | 2142 | TRUE, FALSE, TRUE); |
252b5132 RH |
2143 | if (hds == NULL) |
2144 | goto error_return; | |
2145 | if (hds->root.type == bfd_link_hash_new) | |
2146 | { | |
14a793b2 | 2147 | bh = &hds->root; |
252b5132 RH |
2148 | if (! (_bfd_generic_link_add_one_symbol |
2149 | (info, abfd, hds->root.root.string, | |
2150 | (flagword) 0, bfd_und_section_ptr, | |
116c20d2 | 2151 | (bfd_vma) 0, NULL, FALSE, |
b34976b6 | 2152 | TRUE, &bh))) |
252b5132 | 2153 | goto error_return; |
14a793b2 | 2154 | hds = (struct xcoff_link_hash_entry *) bh; |
252b5132 RH |
2155 | } |
2156 | hds->flags |= XCOFF_DESCRIPTOR; | |
858ef0ce | 2157 | BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0); |
252b5132 RH |
2158 | hds->descriptor = h; |
2159 | h->descriptor = hds; | |
2160 | } | |
858ef0ce RS |
2161 | if (h->root.root.string[0] == '.') |
2162 | h->flags |= XCOFF_CALLED; | |
252b5132 RH |
2163 | } |
2164 | } | |
2165 | ||
2166 | free (reloc_info[o->target_index].csects); | |
2167 | reloc_info[o->target_index].csects = NULL; | |
2168 | ||
2169 | /* Reset SEC_RELOC and the reloc_count, since the reloc | |
2170 | information is now attached to the csects. */ | |
beb1bf64 | 2171 | o->flags &=~ SEC_RELOC; |
252b5132 RH |
2172 | o->reloc_count = 0; |
2173 | ||
2174 | /* If we are not keeping memory, free the reloc information. */ | |
2175 | if (! info->keep_memory | |
2176 | && coff_section_data (abfd, o) != NULL | |
2177 | && coff_section_data (abfd, o)->relocs != NULL | |
2178 | && ! coff_section_data (abfd, o)->keep_relocs) | |
2179 | { | |
2180 | free (coff_section_data (abfd, o)->relocs); | |
2181 | coff_section_data (abfd, o)->relocs = NULL; | |
2182 | } | |
2183 | } | |
2184 | ||
2185 | /* Free up the line numbers. FIXME: We could cache these | |
b34976b6 | 2186 | somewhere for the final link, to avoid reading them again. */ |
252b5132 RH |
2187 | if (reloc_info[o->target_index].linenos != NULL) |
2188 | { | |
2189 | free (reloc_info[o->target_index].linenos); | |
2190 | reloc_info[o->target_index].linenos = NULL; | |
2191 | } | |
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 | { | |
2205 | if (reloc_info[o->target_index].csects != NULL) | |
2206 | free (reloc_info[o->target_index].csects); | |
2207 | if (reloc_info[o->target_index].linenos != NULL) | |
2208 | free (reloc_info[o->target_index].linenos); | |
2209 | } | |
dc810e39 | 2210 | free (reloc_info); |
252b5132 RH |
2211 | } |
2212 | obj_coff_keep_syms (abfd) = keep_syms; | |
b34976b6 | 2213 | return FALSE; |
252b5132 RH |
2214 | } |
2215 | ||
2216 | #undef N_TMASK | |
2217 | #undef N_BTSHFT | |
2218 | ||
116c20d2 | 2219 | /* Add symbols from an XCOFF object file. */ |
252b5132 | 2220 | |
b34976b6 | 2221 | static bfd_boolean |
116c20d2 | 2222 | xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 2223 | { |
116c20d2 NC |
2224 | if (! _bfd_coff_get_external_symbols (abfd)) |
2225 | return FALSE; | |
2226 | if (! xcoff_link_add_symbols (abfd, info)) | |
2227 | return FALSE; | |
2228 | if (! info->keep_memory) | |
252b5132 | 2229 | { |
116c20d2 NC |
2230 | if (! _bfd_coff_free_symbols (abfd)) |
2231 | return FALSE; | |
252b5132 | 2232 | } |
116c20d2 NC |
2233 | return TRUE; |
2234 | } | |
dc810e39 | 2235 | |
116c20d2 NC |
2236 | /* Look through the loader symbols to see if this dynamic object |
2237 | should be included in the link. The native linker uses the loader | |
2238 | symbols, not the normal symbol table, so we do too. */ | |
2239 | ||
2240 | static bfd_boolean | |
2241 | xcoff_link_check_dynamic_ar_symbols (bfd *abfd, | |
2242 | struct bfd_link_info *info, | |
5d3236ee DK |
2243 | bfd_boolean *pneeded, |
2244 | bfd **subsbfd) | |
116c20d2 NC |
2245 | { |
2246 | asection *lsec; | |
2247 | bfd_byte *contents; | |
2248 | struct internal_ldhdr ldhdr; | |
2249 | const char *strings; | |
2250 | bfd_byte *elsym, *elsymend; | |
2251 | ||
2252 | *pneeded = FALSE; | |
252b5132 | 2253 | |
252b5132 RH |
2254 | lsec = bfd_get_section_by_name (abfd, ".loader"); |
2255 | if (lsec == NULL) | |
116c20d2 NC |
2256 | /* There are no symbols, so don't try to include it. */ |
2257 | return TRUE; | |
beb1bf64 | 2258 | |
252b5132 | 2259 | if (! xcoff_get_section_contents (abfd, lsec)) |
b34976b6 | 2260 | return FALSE; |
beb1bf64 | 2261 | contents = coff_section_data (abfd, lsec)->contents; |
252b5132 | 2262 | |
beb1bf64 TR |
2263 | bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); |
2264 | ||
2265 | strings = (char *) contents + ldhdr.l_stoff; | |
2266 | ||
116c20d2 | 2267 | elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr); |
252b5132 | 2268 | |
116c20d2 NC |
2269 | elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd); |
2270 | for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd)) | |
252b5132 RH |
2271 | { |
2272 | struct internal_ldsym ldsym; | |
2273 | char nambuf[SYMNMLEN + 1]; | |
2274 | const char *name; | |
116c20d2 | 2275 | struct bfd_link_hash_entry *h; |
252b5132 | 2276 | |
beb1bf64 | 2277 | bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym); |
252b5132 RH |
2278 | |
2279 | /* We are only interested in exported symbols. */ | |
2280 | if ((ldsym.l_smtype & L_EXPORT) == 0) | |
2281 | continue; | |
2282 | ||
2283 | if (ldsym._l._l_l._l_zeroes == 0) | |
2284 | name = strings + ldsym._l._l_l._l_offset; | |
2285 | else | |
2286 | { | |
2287 | memcpy (nambuf, ldsym._l._l_name, SYMNMLEN); | |
2288 | nambuf[SYMNMLEN] = '\0'; | |
2289 | name = nambuf; | |
2290 | } | |
2291 | ||
116c20d2 | 2292 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); |
252b5132 | 2293 | |
116c20d2 NC |
2294 | /* We are only interested in symbols that are currently |
2295 | undefined. At this point we know that we are using an XCOFF | |
2296 | hash table. */ | |
2297 | if (h != NULL | |
2298 | && h->type == bfd_link_hash_undefined | |
2299 | && (((struct xcoff_link_hash_entry *) h)->flags | |
2300 | & XCOFF_DEF_DYNAMIC) == 0) | |
252b5132 | 2301 | { |
0e144ba7 AM |
2302 | if (!(*info->callbacks |
2303 | ->add_archive_element) (info, abfd, name, subsbfd)) | |
b95a0a31 | 2304 | continue; |
116c20d2 NC |
2305 | *pneeded = TRUE; |
2306 | return TRUE; | |
252b5132 | 2307 | } |
116c20d2 | 2308 | } |
252b5132 | 2309 | |
116c20d2 NC |
2310 | /* We do not need this shared object. */ |
2311 | if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents) | |
2312 | { | |
2313 | free (coff_section_data (abfd, lsec)->contents); | |
2314 | coff_section_data (abfd, lsec)->contents = NULL; | |
2315 | } | |
252b5132 | 2316 | |
116c20d2 NC |
2317 | return TRUE; |
2318 | } | |
252b5132 | 2319 | |
116c20d2 NC |
2320 | /* Look through the symbols to see if this object file should be |
2321 | included in the link. */ | |
252b5132 | 2322 | |
116c20d2 NC |
2323 | static bfd_boolean |
2324 | xcoff_link_check_ar_symbols (bfd *abfd, | |
2325 | struct bfd_link_info *info, | |
5d3236ee DK |
2326 | bfd_boolean *pneeded, |
2327 | bfd **subsbfd) | |
116c20d2 NC |
2328 | { |
2329 | bfd_size_type symesz; | |
2330 | bfd_byte *esym; | |
2331 | bfd_byte *esym_end; | |
252b5132 | 2332 | |
116c20d2 | 2333 | *pneeded = FALSE; |
252b5132 | 2334 | |
116c20d2 NC |
2335 | if ((abfd->flags & DYNAMIC) != 0 |
2336 | && ! info->static_link | |
f13a99db | 2337 | && info->output_bfd->xvec == abfd->xvec) |
5d3236ee | 2338 | return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd); |
252b5132 | 2339 | |
116c20d2 NC |
2340 | symesz = bfd_coff_symesz (abfd); |
2341 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
2342 | esym_end = esym + obj_raw_syment_count (abfd) * symesz; | |
2343 | while (esym < esym_end) | |
2344 | { | |
2345 | struct internal_syment sym; | |
252b5132 | 2346 | |
116c20d2 | 2347 | bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym); |
252b5132 | 2348 | |
8602d4fe | 2349 | if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF) |
116c20d2 NC |
2350 | { |
2351 | const char *name; | |
2352 | char buf[SYMNMLEN + 1]; | |
2353 | struct bfd_link_hash_entry *h; | |
252b5132 | 2354 | |
116c20d2 NC |
2355 | /* This symbol is externally visible, and is defined by this |
2356 | object file. */ | |
2357 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
2358 | ||
2359 | if (name == NULL) | |
2360 | return FALSE; | |
2361 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); | |
2362 | ||
2363 | /* We are only interested in symbols that are currently | |
2364 | undefined. If a symbol is currently known to be common, | |
2365 | XCOFF linkers do not bring in an object file which | |
2366 | defines it. We also don't bring in symbols to satisfy | |
2367 | undefined references in shared objects. */ | |
2368 | if (h != NULL | |
2369 | && h->type == bfd_link_hash_undefined | |
07d6d2b8 | 2370 | && (info->output_bfd->xvec != abfd->xvec |
116c20d2 NC |
2371 | || (((struct xcoff_link_hash_entry *) h)->flags |
2372 | & XCOFF_DEF_DYNAMIC) == 0)) | |
252b5132 | 2373 | { |
0e144ba7 AM |
2374 | if (!(*info->callbacks |
2375 | ->add_archive_element) (info, abfd, name, subsbfd)) | |
b95a0a31 | 2376 | continue; |
116c20d2 NC |
2377 | *pneeded = TRUE; |
2378 | return TRUE; | |
252b5132 RH |
2379 | } |
2380 | } | |
252b5132 | 2381 | |
116c20d2 | 2382 | esym += (sym.n_numaux + 1) * symesz; |
252b5132 RH |
2383 | } |
2384 | ||
116c20d2 NC |
2385 | /* We do not need this object file. */ |
2386 | return TRUE; | |
2387 | } | |
252b5132 | 2388 | |
116c20d2 NC |
2389 | /* Check a single archive element to see if we need to include it in |
2390 | the link. *PNEEDED is set according to whether this element is | |
2391 | needed in the link or not. This is called via | |
2392 | _bfd_generic_link_add_archive_symbols. */ | |
2393 | ||
2394 | static bfd_boolean | |
2395 | xcoff_link_check_archive_element (bfd *abfd, | |
2396 | struct bfd_link_info *info, | |
13e570f8 AM |
2397 | struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED, |
2398 | const char *name ATTRIBUTE_UNUSED, | |
116c20d2 NC |
2399 | bfd_boolean *pneeded) |
2400 | { | |
f95942a3 | 2401 | bfd_boolean keep_syms_p; |
0e144ba7 | 2402 | bfd *oldbfd; |
f95942a3 RS |
2403 | |
2404 | keep_syms_p = (obj_coff_external_syms (abfd) != NULL); | |
0e144ba7 | 2405 | if (!_bfd_coff_get_external_symbols (abfd)) |
b34976b6 | 2406 | return FALSE; |
252b5132 | 2407 | |
0e144ba7 AM |
2408 | oldbfd = abfd; |
2409 | if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd)) | |
116c20d2 NC |
2410 | return FALSE; |
2411 | ||
2412 | if (*pneeded) | |
252b5132 | 2413 | { |
5d3236ee DK |
2414 | /* Potentially, the add_archive_element hook may have set a |
2415 | substitute BFD for us. */ | |
0e144ba7 AM |
2416 | if (abfd != oldbfd) |
2417 | { | |
2418 | if (!keep_syms_p | |
2419 | && !_bfd_coff_free_symbols (oldbfd)) | |
2420 | return FALSE; | |
2421 | keep_syms_p = (obj_coff_external_syms (abfd) != NULL); | |
2422 | if (!_bfd_coff_get_external_symbols (abfd)) | |
2423 | return FALSE; | |
2424 | } | |
2425 | if (!xcoff_link_add_symbols (abfd, info)) | |
116c20d2 | 2426 | return FALSE; |
f95942a3 RS |
2427 | if (info->keep_memory) |
2428 | keep_syms_p = TRUE; | |
252b5132 | 2429 | } |
116c20d2 | 2430 | |
f95942a3 | 2431 | if (!keep_syms_p) |
252b5132 | 2432 | { |
0e144ba7 | 2433 | if (!_bfd_coff_free_symbols (abfd)) |
116c20d2 | 2434 | return FALSE; |
252b5132 | 2435 | } |
252b5132 | 2436 | |
116c20d2 NC |
2437 | return TRUE; |
2438 | } | |
252b5132 | 2439 | |
116c20d2 NC |
2440 | /* Given an XCOFF BFD, add symbols to the global hash table as |
2441 | appropriate. */ | |
252b5132 | 2442 | |
116c20d2 NC |
2443 | bfd_boolean |
2444 | _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
2445 | { | |
2446 | switch (bfd_get_format (abfd)) | |
2447 | { | |
2448 | case bfd_object: | |
2449 | return xcoff_link_add_object_symbols (abfd, info); | |
2450 | ||
2451 | case bfd_archive: | |
2452 | /* If the archive has a map, do the usual search. We then need | |
2453 | to check the archive for dynamic objects, because they may not | |
2454 | appear in the archive map even though they should, perhaps, be | |
2455 | included. If the archive has no map, we just consider each object | |
2456 | file in turn, since that apparently is what the AIX native linker | |
2457 | does. */ | |
2458 | if (bfd_has_map (abfd)) | |
2459 | { | |
2460 | if (! (_bfd_generic_link_add_archive_symbols | |
2461 | (abfd, info, xcoff_link_check_archive_element))) | |
2462 | return FALSE; | |
2463 | } | |
2464 | ||
2465 | { | |
2466 | bfd *member; | |
2467 | ||
2468 | member = bfd_openr_next_archived_file (abfd, NULL); | |
2469 | while (member != NULL) | |
2470 | { | |
2471 | if (bfd_check_format (member, bfd_object) | |
f13a99db | 2472 | && (info->output_bfd->xvec == member->xvec) |
116c20d2 NC |
2473 | && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0)) |
2474 | { | |
2475 | bfd_boolean needed; | |
2476 | ||
2477 | if (! xcoff_link_check_archive_element (member, info, | |
13e570f8 | 2478 | NULL, NULL, &needed)) |
116c20d2 NC |
2479 | return FALSE; |
2480 | if (needed) | |
2481 | member->archive_pass = -1; | |
2482 | } | |
2483 | member = bfd_openr_next_archived_file (abfd, member); | |
2484 | } | |
2485 | } | |
2486 | ||
2487 | return TRUE; | |
2488 | ||
2489 | default: | |
2490 | bfd_set_error (bfd_error_wrong_format); | |
2491 | return FALSE; | |
2492 | } | |
252b5132 RH |
2493 | } |
2494 | \f | |
3023e3f6 RS |
2495 | bfd_boolean |
2496 | _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED, | |
2497 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
2498 | struct bfd_link_hash_entry *harg) | |
2499 | { | |
2500 | struct xcoff_link_hash_entry *h; | |
2501 | ||
2502 | if (!bfd_generic_define_common_symbol (output_bfd, info, harg)) | |
2503 | return FALSE; | |
2504 | ||
2505 | h = (struct xcoff_link_hash_entry *) harg; | |
2506 | h->flags |= XCOFF_DEF_REGULAR; | |
2507 | return TRUE; | |
2508 | } | |
2509 | \f | |
858ef0ce RS |
2510 | /* If symbol H has not been interpreted as a function descriptor, |
2511 | see whether it should be. Set up its descriptor information if so. */ | |
2512 | ||
2513 | static bfd_boolean | |
2514 | xcoff_find_function (struct bfd_link_info *info, | |
2515 | struct xcoff_link_hash_entry *h) | |
2516 | { | |
2517 | if ((h->flags & XCOFF_DESCRIPTOR) == 0 | |
2518 | && h->root.root.string[0] != '.') | |
2519 | { | |
2520 | char *fnname; | |
2521 | struct xcoff_link_hash_entry *hfn; | |
2522 | bfd_size_type amt; | |
2523 | ||
2524 | amt = strlen (h->root.root.string) + 2; | |
2525 | fnname = bfd_malloc (amt); | |
2526 | if (fnname == NULL) | |
2527 | return FALSE; | |
2528 | fnname[0] = '.'; | |
2529 | strcpy (fnname + 1, h->root.root.string); | |
2530 | hfn = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
2531 | fnname, FALSE, FALSE, TRUE); | |
2532 | free (fnname); | |
2533 | if (hfn != NULL | |
2534 | && hfn->smclas == XMC_PR | |
2535 | && (hfn->root.type == bfd_link_hash_defined | |
2536 | || hfn->root.type == bfd_link_hash_defweak)) | |
2537 | { | |
2538 | h->flags |= XCOFF_DESCRIPTOR; | |
2539 | h->descriptor = hfn; | |
2540 | hfn->descriptor = h; | |
2541 | } | |
2542 | } | |
2543 | return TRUE; | |
2544 | } | |
858ef0ce | 2545 | \f |
b64232cc RS |
2546 | /* Return true if the given bfd contains at least one shared object. */ |
2547 | ||
2548 | static bfd_boolean | |
ee698300 RS |
2549 | xcoff_archive_contains_shared_object_p (struct bfd_link_info *info, |
2550 | bfd *archive) | |
b64232cc | 2551 | { |
ee698300 | 2552 | struct xcoff_archive_info *archive_info; |
b64232cc RS |
2553 | bfd *member; |
2554 | ||
ee698300 RS |
2555 | archive_info = xcoff_get_archive_info (info, archive); |
2556 | if (!archive_info->know_contains_shared_object_p) | |
2557 | { | |
2558 | member = bfd_openr_next_archived_file (archive, NULL); | |
2559 | while (member != NULL && (member->flags & DYNAMIC) == 0) | |
2560 | member = bfd_openr_next_archived_file (archive, member); | |
2561 | ||
2562 | archive_info->contains_shared_object_p = (member != NULL); | |
2563 | archive_info->know_contains_shared_object_p = 1; | |
2564 | } | |
2565 | return archive_info->contains_shared_object_p; | |
b64232cc RS |
2566 | } |
2567 | ||
2568 | /* Symbol H qualifies for export by -bexpfull. Return true if it also | |
2569 | qualifies for export by -bexpall. */ | |
2570 | ||
2571 | static bfd_boolean | |
2572 | xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h) | |
2573 | { | |
2574 | /* Exclude symbols beginning with '_'. */ | |
2575 | if (h->root.root.string[0] == '_') | |
2576 | return FALSE; | |
2577 | ||
2578 | /* Exclude archive members that would otherwise be unreferenced. */ | |
2579 | if ((h->flags & XCOFF_MARK) == 0 | |
2580 | && (h->root.type == bfd_link_hash_defined | |
2581 | || h->root.type == bfd_link_hash_defweak) | |
2582 | && h->root.u.def.section->owner != NULL | |
2583 | && h->root.u.def.section->owner->my_archive != NULL) | |
2584 | return FALSE; | |
2585 | ||
2586 | return TRUE; | |
2587 | } | |
2588 | ||
2589 | /* Return true if symbol H qualifies for the forms of automatic export | |
2590 | specified by AUTO_EXPORT_FLAGS. */ | |
2591 | ||
2592 | static bfd_boolean | |
ee698300 RS |
2593 | xcoff_auto_export_p (struct bfd_link_info *info, |
2594 | struct xcoff_link_hash_entry *h, | |
b64232cc RS |
2595 | unsigned int auto_export_flags) |
2596 | { | |
2597 | /* Don't automatically export things that were explicitly exported. */ | |
2598 | if ((h->flags & XCOFF_EXPORT) != 0) | |
2599 | return FALSE; | |
2600 | ||
2601 | /* Don't export things that we don't define. */ | |
2602 | if ((h->flags & XCOFF_DEF_REGULAR) == 0) | |
2603 | return FALSE; | |
2604 | ||
2605 | /* Don't export functions; export their descriptors instead. */ | |
2606 | if (h->root.root.string[0] == '.') | |
2607 | return FALSE; | |
2608 | ||
2609 | /* We don't export a symbol which is being defined by an object | |
2610 | included from an archive which contains a shared object. The | |
2611 | rationale is that if an archive contains both an unshared and | |
2612 | a shared object, then there must be some reason that the | |
2613 | unshared object is unshared, and we don't want to start | |
2614 | providing a shared version of it. In particular, this solves | |
2615 | a bug involving the _savefNN set of functions. gcc will call | |
2616 | those functions without providing a slot to restore the TOC, | |
2617 | so it is essential that these functions be linked in directly | |
2618 | and not from a shared object, which means that a shared | |
2619 | object which also happens to link them in must not export | |
2620 | them. This is confusing, but I haven't been able to think of | |
2621 | a different approach. Note that the symbols can, of course, | |
2622 | be exported explicitly. */ | |
2623 | if (h->root.type == bfd_link_hash_defined | |
2624 | || h->root.type == bfd_link_hash_defweak) | |
2625 | { | |
2626 | bfd *owner; | |
2627 | ||
2628 | owner = h->root.u.def.section->owner; | |
2629 | if (owner != NULL | |
2630 | && owner->my_archive != NULL | |
ee698300 | 2631 | && xcoff_archive_contains_shared_object_p (info, owner->my_archive)) |
b64232cc RS |
2632 | return FALSE; |
2633 | } | |
2634 | ||
2635 | /* Otherwise, all symbols are exported by -bexpfull. */ | |
2636 | if ((auto_export_flags & XCOFF_EXPFULL) != 0) | |
2637 | return TRUE; | |
2638 | ||
2639 | /* Despite its name, -bexpall exports most but not all symbols. */ | |
2640 | if ((auto_export_flags & XCOFF_EXPALL) != 0 | |
2641 | && xcoff_covered_by_expall_p (h)) | |
2642 | return TRUE; | |
2643 | ||
2644 | return FALSE; | |
2645 | } | |
2646 | \f | |
b3d1832c RS |
2647 | /* Return true if relocation REL needs to be copied to the .loader section. |
2648 | If REL is against a global symbol, H is that symbol, otherwise it | |
2649 | is null. */ | |
2650 | ||
2651 | static bfd_boolean | |
2652 | xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel, | |
2653 | struct xcoff_link_hash_entry *h) | |
2654 | { | |
2655 | if (!xcoff_hash_table (info)->loader_section) | |
2656 | return FALSE; | |
2657 | ||
2658 | switch (rel->r_type) | |
2659 | { | |
2660 | case R_TOC: | |
2661 | case R_GL: | |
2662 | case R_TCL: | |
2663 | case R_TRL: | |
2664 | case R_TRLA: | |
2665 | /* We should never need a .loader reloc for a TOC-relative reloc. */ | |
2666 | return FALSE; | |
2667 | ||
2668 | default: | |
2669 | /* In this case, relocations against defined symbols can be resolved | |
2670 | statically. */ | |
2671 | if (h == NULL | |
2672 | || h->root.type == bfd_link_hash_defined | |
2673 | || h->root.type == bfd_link_hash_defweak | |
2674 | || h->root.type == bfd_link_hash_common) | |
2675 | return FALSE; | |
2676 | ||
2677 | /* We will always provide a local definition of function symbols, | |
2678 | even if we don't have one yet. */ | |
2679 | if ((h->flags & XCOFF_CALLED) != 0) | |
2680 | return FALSE; | |
2681 | ||
2682 | return TRUE; | |
2683 | ||
2684 | case R_POS: | |
2685 | case R_NEG: | |
2686 | case R_RL: | |
2687 | case R_RLA: | |
2688 | /* Absolute relocations against absolute symbols can be | |
2689 | resolved statically. */ | |
2690 | if (h != NULL | |
2691 | && (h->root.type == bfd_link_hash_defined | |
2692 | || h->root.type == bfd_link_hash_defweak) | |
2693 | && bfd_is_abs_section (h->root.u.def.section)) | |
2694 | return FALSE; | |
2695 | ||
2696 | return TRUE; | |
2697 | } | |
2698 | } | |
2699 | \f | |
252b5132 RH |
2700 | /* Mark a symbol as not being garbage, including the section in which |
2701 | it is defined. */ | |
2702 | ||
116c20d2 NC |
2703 | static inline bfd_boolean |
2704 | xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h) | |
252b5132 RH |
2705 | { |
2706 | if ((h->flags & XCOFF_MARK) != 0) | |
b34976b6 | 2707 | return TRUE; |
252b5132 RH |
2708 | |
2709 | h->flags |= XCOFF_MARK; | |
858ef0ce RS |
2710 | |
2711 | /* If we're marking an undefined symbol, try find some way of | |
2712 | defining it. */ | |
0e1862bb | 2713 | if (!bfd_link_relocatable (info) |
858ef0ce RS |
2714 | && (h->flags & XCOFF_IMPORT) == 0 |
2715 | && (h->flags & XCOFF_DEF_REGULAR) == 0 | |
2716 | && (h->root.type == bfd_link_hash_undefined | |
2717 | || h->root.type == bfd_link_hash_undefweak)) | |
2718 | { | |
2719 | /* First check whether this symbol can be interpreted as an | |
2720 | undefined function descriptor for a defined function symbol. */ | |
2721 | if (!xcoff_find_function (info, h)) | |
2722 | return FALSE; | |
2723 | ||
2724 | if ((h->flags & XCOFF_DESCRIPTOR) != 0 | |
2725 | && (h->descriptor->root.type == bfd_link_hash_defined | |
2726 | || h->descriptor->root.type == bfd_link_hash_defweak)) | |
2727 | { | |
2728 | /* This is a descriptor for a defined symbol, but the input | |
2729 | objects have not defined the descriptor itself. Fill in | |
2730 | the definition automatically. | |
2731 | ||
2732 | Note that we do this even if we found a dynamic definition | |
2733 | of H. The local function definition logically overrides | |
2734 | the dynamic one. */ | |
2735 | asection *sec; | |
2736 | ||
2737 | sec = xcoff_hash_table (info)->descriptor_section; | |
2738 | h->root.type = bfd_link_hash_defined; | |
2739 | h->root.u.def.section = sec; | |
2740 | h->root.u.def.value = sec->size; | |
2741 | h->smclas = XMC_DS; | |
2742 | h->flags |= XCOFF_DEF_REGULAR; | |
2743 | ||
2744 | /* The size of the function descriptor depends on whether this | |
2745 | is xcoff32 (12) or xcoff64 (24). */ | |
2746 | sec->size += bfd_xcoff_function_descriptor_size (sec->owner); | |
2747 | ||
2748 | /* A function descriptor uses two relocs: one for the | |
2749 | associated code, and one for the TOC address. */ | |
2750 | xcoff_hash_table (info)->ldrel_count += 2; | |
2751 | sec->reloc_count += 2; | |
2752 | ||
2753 | /* Mark the function itself. */ | |
2754 | if (!xcoff_mark_symbol (info, h->descriptor)) | |
2755 | return FALSE; | |
2756 | ||
47dfb2ca RS |
2757 | /* Mark the TOC section, so that we get an anchor |
2758 | to relocate against. */ | |
2759 | if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section)) | |
2760 | return FALSE; | |
2761 | ||
858ef0ce RS |
2762 | /* We handle writing out the contents of the descriptor in |
2763 | xcoff_write_global_symbol. */ | |
2764 | } | |
94313f36 RS |
2765 | else if (info->static_link) |
2766 | /* We can't get a symbol value dynamically, so just assume | |
2767 | that it's undefined. */ | |
2768 | h->flags |= XCOFF_WAS_UNDEFINED; | |
858ef0ce RS |
2769 | else if ((h->flags & XCOFF_CALLED) != 0) |
2770 | { | |
2771 | /* This is a function symbol for which we need to create | |
2772 | linkage code. */ | |
2773 | asection *sec; | |
2774 | struct xcoff_link_hash_entry *hds; | |
2775 | ||
2776 | /* Mark the descriptor (and its TOC section). */ | |
2777 | hds = h->descriptor; | |
2778 | BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined | |
2779 | || hds->root.type == bfd_link_hash_undefweak) | |
2780 | && (hds->flags & XCOFF_DEF_REGULAR) == 0); | |
2781 | if (!xcoff_mark_symbol (info, hds)) | |
2782 | return FALSE; | |
2783 | ||
2784 | /* Treat this symbol as undefined if the descriptor was. */ | |
2785 | if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0) | |
2786 | h->flags |= XCOFF_WAS_UNDEFINED; | |
2787 | ||
2788 | /* Allocate room for the global linkage code itself. */ | |
2789 | sec = xcoff_hash_table (info)->linkage_section; | |
2790 | h->root.type = bfd_link_hash_defined; | |
2791 | h->root.u.def.section = sec; | |
2792 | h->root.u.def.value = sec->size; | |
2793 | h->smclas = XMC_GL; | |
2794 | h->flags |= XCOFF_DEF_REGULAR; | |
2795 | sec->size += bfd_xcoff_glink_code_size (info->output_bfd); | |
2796 | ||
2797 | /* The global linkage code requires a TOC entry for the | |
2798 | descriptor. */ | |
2799 | if (hds->toc_section == NULL) | |
2800 | { | |
2801 | int byte_size; | |
2802 | ||
2803 | /* 32 vs 64 | |
2804 | xcoff32 uses 4 bytes in the toc. | |
2805 | xcoff64 uses 8 bytes in the toc. */ | |
2806 | if (bfd_xcoff_is_xcoff64 (info->output_bfd)) | |
2807 | byte_size = 8; | |
2808 | else if (bfd_xcoff_is_xcoff32 (info->output_bfd)) | |
2809 | byte_size = 4; | |
2810 | else | |
2811 | return FALSE; | |
2812 | ||
2813 | /* Allocate room in the fallback TOC section. */ | |
2814 | hds->toc_section = xcoff_hash_table (info)->toc_section; | |
2815 | hds->u.toc_offset = hds->toc_section->size; | |
2816 | hds->toc_section->size += byte_size; | |
2817 | if (!xcoff_mark (info, hds->toc_section)) | |
2818 | return FALSE; | |
2819 | ||
2820 | /* Allocate room for a static and dynamic R_TOC | |
2821 | relocation. */ | |
2822 | ++xcoff_hash_table (info)->ldrel_count; | |
2823 | ++hds->toc_section->reloc_count; | |
2824 | ||
2825 | /* Set the index to -2 to force this symbol to | |
2826 | get written out. */ | |
2827 | hds->indx = -2; | |
2828 | hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL; | |
2829 | } | |
2830 | } | |
2831 | else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0) | |
2832 | { | |
2833 | /* Record that the symbol was undefined, then import it. | |
2834 | -brtl links use a special fake import file. */ | |
2835 | h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT; | |
2836 | if (xcoff_hash_table (info)->rtld) | |
2837 | { | |
2838 | if (!xcoff_set_import_path (info, h, "", "..", "")) | |
2839 | return FALSE; | |
2840 | } | |
2841 | else | |
2842 | { | |
2843 | if (!xcoff_set_import_path (info, h, NULL, NULL, NULL)) | |
2844 | return FALSE; | |
2845 | } | |
2846 | } | |
2847 | } | |
2848 | ||
252b5132 RH |
2849 | if (h->root.type == bfd_link_hash_defined |
2850 | || h->root.type == bfd_link_hash_defweak) | |
2851 | { | |
2852 | asection *hsec; | |
2853 | ||
2854 | hsec = h->root.u.def.section; | |
2855 | if (! bfd_is_abs_section (hsec) | |
2856 | && (hsec->flags & SEC_MARK) == 0) | |
2857 | { | |
2858 | if (! xcoff_mark (info, hsec)) | |
b34976b6 | 2859 | return FALSE; |
252b5132 RH |
2860 | } |
2861 | } | |
2862 | ||
2863 | if (h->toc_section != NULL | |
2864 | && (h->toc_section->flags & SEC_MARK) == 0) | |
2865 | { | |
2866 | if (! xcoff_mark (info, h->toc_section)) | |
b34976b6 | 2867 | return FALSE; |
252b5132 RH |
2868 | } |
2869 | ||
b34976b6 | 2870 | return TRUE; |
252b5132 RH |
2871 | } |
2872 | ||
7d504122 RS |
2873 | /* Look for a symbol called NAME. If the symbol is defined, mark it. |
2874 | If the symbol exists, set FLAGS. */ | |
2875 | ||
2876 | static bfd_boolean | |
2877 | xcoff_mark_symbol_by_name (struct bfd_link_info *info, | |
2878 | const char *name, unsigned int flags) | |
2879 | { | |
2880 | struct xcoff_link_hash_entry *h; | |
2881 | ||
2882 | h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, | |
2883 | FALSE, FALSE, TRUE); | |
2884 | if (h != NULL) | |
2885 | { | |
2886 | h->flags |= flags; | |
2887 | if (h->root.type == bfd_link_hash_defined | |
2888 | || h->root.type == bfd_link_hash_defweak) | |
2889 | { | |
2890 | if (!xcoff_mark (info, h->root.u.def.section)) | |
2891 | return FALSE; | |
2892 | } | |
2893 | } | |
2894 | return TRUE; | |
2895 | } | |
2896 | ||
252b5132 RH |
2897 | /* The mark phase of garbage collection. For a given section, mark |
2898 | it, and all the sections which define symbols to which it refers. | |
2899 | Because this function needs to look at the relocs, we also count | |
2900 | the number of relocs which need to be copied into the .loader | |
2901 | section. */ | |
2902 | ||
b34976b6 | 2903 | static bfd_boolean |
116c20d2 | 2904 | xcoff_mark (struct bfd_link_info *info, asection *sec) |
252b5132 RH |
2905 | { |
2906 | if (bfd_is_abs_section (sec) | |
2907 | || (sec->flags & SEC_MARK) != 0) | |
b34976b6 | 2908 | return TRUE; |
252b5132 RH |
2909 | |
2910 | sec->flags |= SEC_MARK; | |
2911 | ||
f13a99db | 2912 | if (sec->owner->xvec == info->output_bfd->xvec |
252b5132 RH |
2913 | && coff_section_data (sec->owner, sec) != NULL |
2914 | && xcoff_section_data (sec->owner, sec) != NULL) | |
2915 | { | |
4cc02a02 | 2916 | struct xcoff_link_hash_entry **syms; |
252b5132 | 2917 | struct internal_reloc *rel, *relend; |
4cc02a02 RS |
2918 | asection **csects; |
2919 | unsigned long i, first, last; | |
252b5132 RH |
2920 | |
2921 | /* Mark all the symbols in this section. */ | |
4cc02a02 RS |
2922 | syms = obj_xcoff_sym_hashes (sec->owner); |
2923 | csects = xcoff_data (sec->owner)->csects; | |
2924 | first = xcoff_section_data (sec->owner, sec)->first_symndx; | |
2925 | last = xcoff_section_data (sec->owner, sec)->last_symndx; | |
2926 | for (i = first; i <= last; i++) | |
2927 | if (csects[i] == sec | |
2928 | && syms[i] != NULL | |
2929 | && (syms[i]->flags & XCOFF_MARK) == 0) | |
2930 | { | |
2931 | if (!xcoff_mark_symbol (info, syms[i])) | |
2932 | return FALSE; | |
2933 | } | |
252b5132 RH |
2934 | |
2935 | /* Look through the section relocs. */ | |
252b5132 RH |
2936 | if ((sec->flags & SEC_RELOC) != 0 |
2937 | && sec->reloc_count > 0) | |
2938 | { | |
b34976b6 | 2939 | rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE, |
116c20d2 | 2940 | NULL, FALSE, NULL); |
252b5132 | 2941 | if (rel == NULL) |
b34976b6 | 2942 | return FALSE; |
252b5132 RH |
2943 | relend = rel + sec->reloc_count; |
2944 | for (; rel < relend; rel++) | |
2945 | { | |
252b5132 RH |
2946 | struct xcoff_link_hash_entry *h; |
2947 | ||
2948 | if ((unsigned int) rel->r_symndx | |
2949 | > obj_raw_syment_count (sec->owner)) | |
2950 | continue; | |
2951 | ||
2952 | h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx]; | |
5b49f6dc | 2953 | if (h != NULL) |
252b5132 | 2954 | { |
5b49f6dc RS |
2955 | if ((h->flags & XCOFF_MARK) == 0) |
2956 | { | |
2957 | if (!xcoff_mark_symbol (info, h)) | |
2958 | return FALSE; | |
2959 | } | |
252b5132 | 2960 | } |
5b49f6dc | 2961 | else |
252b5132 | 2962 | { |
5b49f6dc RS |
2963 | asection *rsec; |
2964 | ||
2965 | rsec = xcoff_data (sec->owner)->csects[rel->r_symndx]; | |
2966 | if (rsec != NULL | |
2967 | && (rsec->flags & SEC_MARK) == 0) | |
2968 | { | |
2969 | if (!xcoff_mark (info, rsec)) | |
2970 | return FALSE; | |
2971 | } | |
252b5132 RH |
2972 | } |
2973 | ||
2974 | /* See if this reloc needs to be copied into the .loader | |
b34976b6 | 2975 | section. */ |
b3d1832c | 2976 | if (xcoff_need_ldrel_p (info, rel, h)) |
252b5132 | 2977 | { |
252b5132 RH |
2978 | ++xcoff_hash_table (info)->ldrel_count; |
2979 | if (h != NULL) | |
2980 | h->flags |= XCOFF_LDREL; | |
252b5132 RH |
2981 | } |
2982 | } | |
2983 | ||
2984 | if (! info->keep_memory | |
2985 | && coff_section_data (sec->owner, sec) != NULL | |
2986 | && coff_section_data (sec->owner, sec)->relocs != NULL | |
2987 | && ! coff_section_data (sec->owner, sec)->keep_relocs) | |
2988 | { | |
2989 | free (coff_section_data (sec->owner, sec)->relocs); | |
2990 | coff_section_data (sec->owner, sec)->relocs = NULL; | |
2991 | } | |
2992 | } | |
2993 | } | |
2994 | ||
b34976b6 | 2995 | return TRUE; |
252b5132 RH |
2996 | } |
2997 | ||
116c20d2 NC |
2998 | /* Routines that are called after all the input files have been |
2999 | handled, but before the sections are laid out in memory. */ | |
3000 | ||
252b5132 RH |
3001 | /* The sweep phase of garbage collection. Remove all garbage |
3002 | sections. */ | |
3003 | ||
3004 | static void | |
116c20d2 | 3005 | xcoff_sweep (struct bfd_link_info *info) |
252b5132 RH |
3006 | { |
3007 | bfd *sub; | |
3008 | ||
c72f2fb2 | 3009 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
252b5132 RH |
3010 | { |
3011 | asection *o; | |
3012 | ||
3013 | for (o = sub->sections; o != NULL; o = o->next) | |
3014 | { | |
3015 | if ((o->flags & SEC_MARK) == 0) | |
3016 | { | |
3017 | /* Keep all sections from non-XCOFF input files. Keep | |
b34976b6 AM |
3018 | special sections. Keep .debug sections for the |
3019 | moment. */ | |
f13a99db | 3020 | if (sub->xvec != info->output_bfd->xvec |
252b5132 RH |
3021 | || o == xcoff_hash_table (info)->debug_section |
3022 | || o == xcoff_hash_table (info)->loader_section | |
3023 | || o == xcoff_hash_table (info)->linkage_section | |
252b5132 | 3024 | || o == xcoff_hash_table (info)->descriptor_section |
07d6d2b8 | 3025 | || (bfd_get_section_flags (sub, o) & SEC_DEBUGGING) |
252b5132 RH |
3026 | || strcmp (o->name, ".debug") == 0) |
3027 | o->flags |= SEC_MARK; | |
3028 | else | |
3029 | { | |
eea6121a | 3030 | o->size = 0; |
252b5132 | 3031 | o->reloc_count = 0; |
252b5132 RH |
3032 | } |
3033 | } | |
3034 | } | |
3035 | } | |
3036 | } | |
3037 | ||
3038 | /* Record the number of elements in a set. This is used to output the | |
3039 | correct csect length. */ | |
3040 | ||
b34976b6 | 3041 | bfd_boolean |
116c20d2 NC |
3042 | bfd_xcoff_link_record_set (bfd *output_bfd, |
3043 | struct bfd_link_info *info, | |
3044 | struct bfd_link_hash_entry *harg, | |
3045 | bfd_size_type size) | |
252b5132 RH |
3046 | { |
3047 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
3048 | struct xcoff_link_size_list *n; | |
dc810e39 | 3049 | bfd_size_type amt; |
252b5132 | 3050 | |
9bd09e22 | 3051 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 3052 | return TRUE; |
252b5132 RH |
3053 | |
3054 | /* This will hardly ever be called. I don't want to burn four bytes | |
3055 | per global symbol, so instead the size is kept on a linked list | |
3056 | attached to the hash table. */ | |
116c20d2 NC |
3057 | amt = sizeof (* n); |
3058 | n = bfd_alloc (output_bfd, amt); | |
252b5132 | 3059 | if (n == NULL) |
b34976b6 | 3060 | return FALSE; |
252b5132 RH |
3061 | n->next = xcoff_hash_table (info)->size_list; |
3062 | n->h = h; | |
3063 | n->size = size; | |
3064 | xcoff_hash_table (info)->size_list = n; | |
3065 | ||
3066 | h->flags |= XCOFF_HAS_SIZE; | |
3067 | ||
b34976b6 | 3068 | return TRUE; |
252b5132 RH |
3069 | } |
3070 | ||
3071 | /* Import a symbol. */ | |
3072 | ||
b34976b6 | 3073 | bfd_boolean |
116c20d2 NC |
3074 | bfd_xcoff_import_symbol (bfd *output_bfd, |
3075 | struct bfd_link_info *info, | |
3076 | struct bfd_link_hash_entry *harg, | |
3077 | bfd_vma val, | |
3078 | const char *imppath, | |
3079 | const char *impfile, | |
3080 | const char *impmember, | |
3081 | unsigned int syscall_flag) | |
252b5132 RH |
3082 | { |
3083 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
3084 | ||
9bd09e22 | 3085 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 3086 | return TRUE; |
252b5132 RH |
3087 | |
3088 | /* A symbol name which starts with a period is the code for a | |
3089 | function. If the symbol is undefined, then add an undefined | |
3090 | symbol for the function descriptor, and import that instead. */ | |
3091 | if (h->root.root.string[0] == '.' | |
3092 | && h->root.type == bfd_link_hash_undefined | |
3093 | && val == (bfd_vma) -1) | |
3094 | { | |
3095 | struct xcoff_link_hash_entry *hds; | |
3096 | ||
3097 | hds = h->descriptor; | |
3098 | if (hds == NULL) | |
3099 | { | |
3100 | hds = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
3101 | h->root.root.string + 1, | |
b34976b6 | 3102 | TRUE, FALSE, TRUE); |
252b5132 | 3103 | if (hds == NULL) |
b34976b6 | 3104 | return FALSE; |
252b5132 RH |
3105 | if (hds->root.type == bfd_link_hash_new) |
3106 | { | |
3107 | hds->root.type = bfd_link_hash_undefined; | |
3108 | hds->root.u.undef.abfd = h->root.u.undef.abfd; | |
3109 | } | |
3110 | hds->flags |= XCOFF_DESCRIPTOR; | |
858ef0ce | 3111 | BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0); |
252b5132 RH |
3112 | hds->descriptor = h; |
3113 | h->descriptor = hds; | |
3114 | } | |
3115 | ||
3116 | /* Now, if the descriptor is undefined, import the descriptor | |
b34976b6 AM |
3117 | rather than the symbol we were told to import. FIXME: Is |
3118 | this correct in all cases? */ | |
252b5132 RH |
3119 | if (hds->root.type == bfd_link_hash_undefined) |
3120 | h = hds; | |
3121 | } | |
3122 | ||
1fdf0249 | 3123 | h->flags |= (XCOFF_IMPORT | syscall_flag); |
252b5132 RH |
3124 | |
3125 | if (val != (bfd_vma) -1) | |
3126 | { | |
3127 | if (h->root.type == bfd_link_hash_defined | |
3128 | && (! bfd_is_abs_section (h->root.u.def.section) | |
3129 | || h->root.u.def.value != val)) | |
1a72702b AM |
3130 | (*info->callbacks->multiple_definition) (info, &h->root, output_bfd, |
3131 | bfd_abs_section_ptr, val); | |
252b5132 RH |
3132 | |
3133 | h->root.type = bfd_link_hash_defined; | |
3134 | h->root.u.def.section = bfd_abs_section_ptr; | |
3135 | h->root.u.def.value = val; | |
c4037431 | 3136 | h->smclas = XMC_XO; |
252b5132 RH |
3137 | } |
3138 | ||
858ef0ce RS |
3139 | if (!xcoff_set_import_path (info, h, imppath, impfile, impmember)) |
3140 | return FALSE; | |
252b5132 | 3141 | |
b34976b6 | 3142 | return TRUE; |
252b5132 RH |
3143 | } |
3144 | ||
3145 | /* Export a symbol. */ | |
3146 | ||
b34976b6 | 3147 | bfd_boolean |
116c20d2 NC |
3148 | bfd_xcoff_export_symbol (bfd *output_bfd, |
3149 | struct bfd_link_info *info, | |
3150 | struct bfd_link_hash_entry *harg) | |
252b5132 RH |
3151 | { |
3152 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
3153 | ||
9bd09e22 | 3154 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 3155 | return TRUE; |
252b5132 RH |
3156 | |
3157 | h->flags |= XCOFF_EXPORT; | |
3158 | ||
3159 | /* FIXME: I'm not at all sure what syscall is supposed to mean, so | |
3160 | I'm just going to ignore it until somebody explains it. */ | |
3161 | ||
252b5132 RH |
3162 | /* Make sure we don't garbage collect this symbol. */ |
3163 | if (! xcoff_mark_symbol (info, h)) | |
b34976b6 | 3164 | return FALSE; |
252b5132 RH |
3165 | |
3166 | /* If this is a function descriptor, make sure we don't garbage | |
3167 | collect the associated function code. We normally don't have to | |
3168 | worry about this, because the descriptor will be attached to a | |
3169 | section with relocs, but if we are creating the descriptor | |
3170 | ourselves those relocs will not be visible to the mark code. */ | |
3171 | if ((h->flags & XCOFF_DESCRIPTOR) != 0) | |
3172 | { | |
3173 | if (! xcoff_mark_symbol (info, h->descriptor)) | |
b34976b6 | 3174 | return FALSE; |
252b5132 RH |
3175 | } |
3176 | ||
b34976b6 | 3177 | return TRUE; |
252b5132 RH |
3178 | } |
3179 | ||
3180 | /* Count a reloc against a symbol. This is called for relocs | |
3181 | generated by the linker script, typically for global constructors | |
3182 | and destructors. */ | |
3183 | ||
b34976b6 | 3184 | bfd_boolean |
116c20d2 NC |
3185 | bfd_xcoff_link_count_reloc (bfd *output_bfd, |
3186 | struct bfd_link_info *info, | |
3187 | const char *name) | |
252b5132 RH |
3188 | { |
3189 | struct xcoff_link_hash_entry *h; | |
3190 | ||
9bd09e22 | 3191 | if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) |
b34976b6 | 3192 | return TRUE; |
252b5132 RH |
3193 | |
3194 | h = ((struct xcoff_link_hash_entry *) | |
b34976b6 AM |
3195 | bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE, |
3196 | FALSE)); | |
252b5132 RH |
3197 | if (h == NULL) |
3198 | { | |
4eca0228 | 3199 | _bfd_error_handler (_("%s: no such symbol"), name); |
252b5132 | 3200 | bfd_set_error (bfd_error_no_symbols); |
b34976b6 | 3201 | return FALSE; |
252b5132 RH |
3202 | } |
3203 | ||
b3d1832c RS |
3204 | h->flags |= XCOFF_REF_REGULAR; |
3205 | if (xcoff_hash_table (info)->loader_section) | |
3206 | { | |
3207 | h->flags |= XCOFF_LDREL; | |
3208 | ++xcoff_hash_table (info)->ldrel_count; | |
3209 | } | |
fbc4fff4 | 3210 | |
252b5132 RH |
3211 | /* Mark the symbol to avoid garbage collection. */ |
3212 | if (! xcoff_mark_symbol (info, h)) | |
b34976b6 | 3213 | return FALSE; |
252b5132 | 3214 | |
b34976b6 | 3215 | return TRUE; |
252b5132 RH |
3216 | } |
3217 | ||
3218 | /* This function is called for each symbol to which the linker script | |
3219 | assigns a value. */ | |
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 | { |
dc810e39 | 3297 | bfd_size_type 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; | |
3645 | bfd_size_type 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: | |
3972 | if (ldinfo.strings != NULL) | |
3973 | free (ldinfo.strings); | |
3974 | if (debug_contents != NULL) | |
3975 | free (debug_contents); | |
3976 | return FALSE; | |
252b5132 | 3977 | } |
252b5132 | 3978 | |
b34976b6 | 3979 | bfd_boolean |
116c20d2 NC |
3980 | bfd_xcoff_link_generate_rtinit (bfd *abfd, |
3981 | const char *init, | |
3982 | const char *fini, | |
3983 | bfd_boolean rtld) | |
252b5132 | 3984 | { |
116c20d2 | 3985 | struct bfd_in_memory *bim; |
252b5132 | 3986 | |
116c20d2 NC |
3987 | bim = bfd_malloc ((bfd_size_type) sizeof (* bim)); |
3988 | if (bim == NULL) | |
3989 | return FALSE; | |
252b5132 | 3990 | |
116c20d2 NC |
3991 | bim->size = 0; |
3992 | bim->buffer = 0; | |
252b5132 | 3993 | |
c72f2fb2 | 3994 | abfd->link.next = 0; |
116c20d2 NC |
3995 | abfd->format = bfd_object; |
3996 | abfd->iostream = (void *) bim; | |
3997 | abfd->flags = BFD_IN_MEMORY; | |
65077aa8 | 3998 | abfd->iovec = &_bfd_memory_iovec; |
116c20d2 | 3999 | abfd->direction = write_direction; |
65077aa8 | 4000 | abfd->origin = 0; |
116c20d2 | 4001 | abfd->where = 0; |
252b5132 | 4002 | |
116c20d2 NC |
4003 | if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld)) |
4004 | return FALSE; | |
252b5132 | 4005 | |
116c20d2 NC |
4006 | /* need to reset to unknown or it will not be read back in correctly */ |
4007 | abfd->format = bfd_unknown; | |
4008 | abfd->direction = read_direction; | |
4009 | abfd->where = 0; | |
252b5132 | 4010 | |
116c20d2 NC |
4011 | return TRUE; |
4012 | } | |
4013 | \f | |
b3d1832c RS |
4014 | /* Return the section that defines H. Return null if no section does. */ |
4015 | ||
4016 | static asection * | |
4017 | xcoff_symbol_section (struct xcoff_link_hash_entry *h) | |
4018 | { | |
4019 | switch (h->root.type) | |
4020 | { | |
4021 | case bfd_link_hash_defined: | |
4022 | case bfd_link_hash_defweak: | |
4023 | return h->root.u.def.section; | |
4024 | ||
4025 | case bfd_link_hash_common: | |
4026 | return h->root.u.c.p->section; | |
4027 | ||
4028 | default: | |
4029 | return NULL; | |
4030 | } | |
4031 | } | |
4032 | ||
4033 | /* Add a .loader relocation for input relocation IREL. If the loader | |
4034 | relocation should be against an output section, HSEC points to the | |
4035 | input section that IREL is against, otherwise HSEC is null. H is the | |
4036 | symbol that IREL is against, or null if it isn't against a global symbol. | |
4037 | REFERENCE_BFD is the bfd to use in error messages about the relocation. */ | |
4038 | ||
4039 | static bfd_boolean | |
baf9bed3 | 4040 | xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo, |
b3d1832c RS |
4041 | asection *output_section, bfd *reference_bfd, |
4042 | struct internal_reloc *irel, asection *hsec, | |
4043 | struct xcoff_link_hash_entry *h) | |
4044 | { | |
4045 | struct internal_ldrel ldrel; | |
4046 | ||
4047 | ldrel.l_vaddr = irel->r_vaddr; | |
4048 | if (hsec != NULL) | |
4049 | { | |
4050 | const char *secname; | |
4051 | ||
4052 | secname = hsec->output_section->name; | |
4053 | if (strcmp (secname, ".text") == 0) | |
4054 | ldrel.l_symndx = 0; | |
4055 | else if (strcmp (secname, ".data") == 0) | |
4056 | ldrel.l_symndx = 1; | |
4057 | else if (strcmp (secname, ".bss") == 0) | |
4058 | ldrel.l_symndx = 2; | |
4059 | else | |
4060 | { | |
4eca0228 | 4061 | _bfd_error_handler |
695344c0 | 4062 | /* xgettext:c-format */ |
871b3ab2 | 4063 | (_("%pB: loader reloc in unrecognized section `%s'"), |
b3d1832c RS |
4064 | reference_bfd, secname); |
4065 | bfd_set_error (bfd_error_nonrepresentable_section); | |
4066 | return FALSE; | |
4067 | } | |
4068 | } | |
4069 | else if (h != NULL) | |
4070 | { | |
4071 | if (h->ldindx < 0) | |
4072 | { | |
4eca0228 | 4073 | _bfd_error_handler |
695344c0 | 4074 | /* xgettext:c-format */ |
871b3ab2 | 4075 | (_("%pB: `%s' in loader reloc but not loader sym"), |
b3d1832c RS |
4076 | reference_bfd, h->root.root.string); |
4077 | bfd_set_error (bfd_error_bad_value); | |
4078 | return FALSE; | |
4079 | } | |
4080 | ldrel.l_symndx = h->ldindx; | |
4081 | } | |
4082 | else | |
4083 | ldrel.l_symndx = -(bfd_size_type) 1; | |
4084 | ||
4085 | ldrel.l_rtype = (irel->r_size << 8) | irel->r_type; | |
4086 | ldrel.l_rsecnm = output_section->target_index; | |
baf9bed3 | 4087 | if (xcoff_hash_table (flinfo->info)->textro |
b3d1832c RS |
4088 | && strcmp (output_section->name, ".text") == 0) |
4089 | { | |
4eca0228 | 4090 | _bfd_error_handler |
695344c0 | 4091 | /* xgettext:c-format */ |
871b3ab2 | 4092 | (_("%pB: loader reloc in read-only section %pA"), |
b3d1832c RS |
4093 | reference_bfd, output_section); |
4094 | bfd_set_error (bfd_error_invalid_operation); | |
4095 | return FALSE; | |
4096 | } | |
baf9bed3 JB |
4097 | bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel); |
4098 | flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd); | |
b3d1832c RS |
4099 | return TRUE; |
4100 | } | |
4101 | ||
116c20d2 NC |
4102 | /* Link an input file into the linker output file. This function |
4103 | handles all the sections and relocations of the input file at once. */ | |
252b5132 | 4104 | |
116c20d2 | 4105 | static bfd_boolean |
baf9bed3 | 4106 | xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo, |
116c20d2 NC |
4107 | bfd *input_bfd) |
4108 | { | |
4109 | bfd *output_bfd; | |
4110 | const char *strings; | |
4111 | bfd_size_type syment_base; | |
4112 | unsigned int n_tmask; | |
4113 | unsigned int n_btshft; | |
4114 | bfd_boolean copy, hash; | |
4115 | bfd_size_type isymesz; | |
4116 | bfd_size_type osymesz; | |
4117 | bfd_size_type linesz; | |
4118 | bfd_byte *esym; | |
4119 | bfd_byte *esym_end; | |
4120 | struct xcoff_link_hash_entry **sym_hash; | |
4121 | struct internal_syment *isymp; | |
4122 | asection **csectpp; | |
3df13c4a | 4123 | unsigned int *lineno_counts; |
e450936a | 4124 | long *debug_index; |
116c20d2 NC |
4125 | long *indexp; |
4126 | unsigned long output_index; | |
4127 | bfd_byte *outsym; | |
4128 | unsigned int incls; | |
4129 | asection *oline; | |
4130 | bfd_boolean keep_syms; | |
4131 | asection *o; | |
252b5132 | 4132 | |
116c20d2 NC |
4133 | /* We can just skip DYNAMIC files, unless this is a static link. */ |
4134 | if ((input_bfd->flags & DYNAMIC) != 0 | |
baf9bed3 | 4135 | && ! flinfo->info->static_link) |
116c20d2 | 4136 | return TRUE; |
252b5132 | 4137 | |
116c20d2 | 4138 | /* Move all the symbols to the output file. */ |
baf9bed3 | 4139 | output_bfd = flinfo->output_bfd; |
116c20d2 NC |
4140 | strings = NULL; |
4141 | syment_base = obj_raw_syment_count (output_bfd); | |
4142 | isymesz = bfd_coff_symesz (input_bfd); | |
4143 | osymesz = bfd_coff_symesz (output_bfd); | |
4144 | linesz = bfd_coff_linesz (input_bfd); | |
4145 | BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd)); | |
252b5132 | 4146 | |
116c20d2 NC |
4147 | n_tmask = coff_data (input_bfd)->local_n_tmask; |
4148 | n_btshft = coff_data (input_bfd)->local_n_btshft; | |
252b5132 | 4149 | |
116c20d2 NC |
4150 | /* Define macros so that ISFCN, et. al., macros work correctly. */ |
4151 | #define N_TMASK n_tmask | |
4152 | #define N_BTSHFT n_btshft | |
252b5132 | 4153 | |
116c20d2 | 4154 | copy = FALSE; |
baf9bed3 | 4155 | if (! flinfo->info->keep_memory) |
116c20d2 NC |
4156 | copy = TRUE; |
4157 | hash = TRUE; | |
b560e2ac | 4158 | if (flinfo->info->traditional_format) |
116c20d2 | 4159 | hash = FALSE; |
252b5132 | 4160 | |
116c20d2 NC |
4161 | if (! _bfd_coff_get_external_symbols (input_bfd)) |
4162 | return FALSE; | |
4163 | ||
e450936a RS |
4164 | /* Make one pass over the symbols and assign indices to symbols that |
4165 | we have decided to keep. Also use create .loader symbol information | |
4166 | and update information in hash table entries. */ | |
116c20d2 NC |
4167 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); |
4168 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
4169 | sym_hash = obj_xcoff_sym_hashes (input_bfd); | |
4170 | csectpp = xcoff_data (input_bfd)->csects; | |
4171 | debug_index = xcoff_data (input_bfd)->debug_indices; | |
baf9bed3 JB |
4172 | isymp = flinfo->internal_syms; |
4173 | indexp = flinfo->sym_indices; | |
116c20d2 | 4174 | output_index = syment_base; |
116c20d2 | 4175 | while (esym < esym_end) |
252b5132 | 4176 | { |
116c20d2 NC |
4177 | union internal_auxent aux; |
4178 | int smtyp = 0; | |
116c20d2 | 4179 | int add; |
252b5132 | 4180 | |
116c20d2 | 4181 | bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp); |
b34976b6 | 4182 | |
8602d4fe RS |
4183 | /* Read in the csect information, if any. */ |
4184 | if (CSECT_SYM_P (isymp->n_sclass)) | |
116c20d2 NC |
4185 | { |
4186 | BFD_ASSERT (isymp->n_numaux > 0); | |
4187 | bfd_coff_swap_aux_in (input_bfd, | |
4188 | (void *) (esym + isymesz * isymp->n_numaux), | |
4189 | isymp->n_type, isymp->n_sclass, | |
4190 | isymp->n_numaux - 1, isymp->n_numaux, | |
4191 | (void *) &aux); | |
b34976b6 | 4192 | |
116c20d2 NC |
4193 | smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); |
4194 | } | |
b34976b6 | 4195 | |
116c20d2 NC |
4196 | /* If this symbol is in the .loader section, swap out the |
4197 | .loader symbol information. If this is an external symbol | |
4198 | reference to a defined symbol, though, then wait until we get | |
4199 | to the definition. */ | |
8602d4fe | 4200 | if (EXTERN_SYM_P (isymp->n_sclass) |
116c20d2 NC |
4201 | && *sym_hash != NULL |
4202 | && (*sym_hash)->ldsym != NULL | |
5b49f6dc | 4203 | && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp)) |
116c20d2 NC |
4204 | { |
4205 | struct xcoff_link_hash_entry *h; | |
4206 | struct internal_ldsym *ldsym; | |
9e7b37b3 | 4207 | |
116c20d2 NC |
4208 | h = *sym_hash; |
4209 | ldsym = h->ldsym; | |
e450936a | 4210 | if (isymp->n_scnum > 0) |
116c20d2 NC |
4211 | { |
4212 | ldsym->l_scnum = (*csectpp)->output_section->target_index; | |
e450936a | 4213 | ldsym->l_value = (isymp->n_value |
116c20d2 NC |
4214 | + (*csectpp)->output_section->vma |
4215 | + (*csectpp)->output_offset | |
4216 | - (*csectpp)->vma); | |
252b5132 | 4217 | } |
116c20d2 | 4218 | else |
252b5132 | 4219 | { |
e450936a RS |
4220 | ldsym->l_scnum = isymp->n_scnum; |
4221 | ldsym->l_value = isymp->n_value; | |
252b5132 | 4222 | } |
252b5132 | 4223 | |
116c20d2 NC |
4224 | ldsym->l_smtype = smtyp; |
4225 | if (((h->flags & XCOFF_DEF_REGULAR) == 0 | |
4226 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
4227 | || (h->flags & XCOFF_IMPORT) != 0) | |
4228 | ldsym->l_smtype |= L_IMPORT; | |
4229 | if (((h->flags & XCOFF_DEF_REGULAR) != 0 | |
4230 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
4231 | || (h->flags & XCOFF_EXPORT) != 0) | |
4232 | ldsym->l_smtype |= L_EXPORT; | |
4233 | if ((h->flags & XCOFF_ENTRY) != 0) | |
4234 | ldsym->l_smtype |= L_ENTRY; | |
8602d4fe RS |
4235 | if (isymp->n_sclass == C_AIX_WEAKEXT) |
4236 | ldsym->l_smtype |= L_WEAK; | |
dc810e39 | 4237 | |
116c20d2 NC |
4238 | ldsym->l_smclas = aux.x_csect.x_smclas; |
4239 | ||
4240 | if (ldsym->l_ifile == (bfd_size_type) -1) | |
4241 | ldsym->l_ifile = 0; | |
4242 | else if (ldsym->l_ifile == 0) | |
252b5132 | 4243 | { |
116c20d2 NC |
4244 | if ((ldsym->l_smtype & L_IMPORT) == 0) |
4245 | ldsym->l_ifile = 0; | |
4246 | else | |
252b5132 | 4247 | { |
116c20d2 | 4248 | bfd *impbfd; |
252b5132 | 4249 | |
116c20d2 NC |
4250 | if (h->root.type == bfd_link_hash_defined |
4251 | || h->root.type == bfd_link_hash_defweak) | |
4252 | impbfd = h->root.u.def.section->owner; | |
4253 | else if (h->root.type == bfd_link_hash_undefined | |
4254 | || h->root.type == bfd_link_hash_undefweak) | |
4255 | impbfd = h->root.u.undef.abfd; | |
4256 | else | |
4257 | impbfd = NULL; | |
4258 | ||
4259 | if (impbfd == NULL) | |
4260 | ldsym->l_ifile = 0; | |
4261 | else | |
252b5132 | 4262 | { |
baf9bed3 | 4263 | BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec); |
116c20d2 | 4264 | ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; |
252b5132 RH |
4265 | } |
4266 | } | |
116c20d2 NC |
4267 | } |
4268 | ||
4269 | ldsym->l_parm = 0; | |
4270 | ||
4271 | BFD_ASSERT (h->ldindx >= 0); | |
baf9bed3 JB |
4272 | bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym, |
4273 | (flinfo->ldsym | |
116c20d2 | 4274 | + ((h->ldindx - 3) |
baf9bed3 | 4275 | * bfd_xcoff_ldsymsz (flinfo->output_bfd)))); |
116c20d2 NC |
4276 | h->ldsym = NULL; |
4277 | ||
4278 | /* Fill in snentry now that we know the target_index. */ | |
4279 | if ((h->flags & XCOFF_ENTRY) != 0 | |
4280 | && (h->root.type == bfd_link_hash_defined | |
4281 | || h->root.type == bfd_link_hash_defweak)) | |
4282 | { | |
4283 | xcoff_data (output_bfd)->snentry = | |
4284 | h->root.u.def.section->output_section->target_index; | |
252b5132 RH |
4285 | } |
4286 | } | |
4287 | ||
e450936a RS |
4288 | add = 1 + isymp->n_numaux; |
4289 | ||
4290 | if (*debug_index == -2) | |
4291 | /* We've decided to strip this symbol. */ | |
4292 | *indexp = -1; | |
4293 | else | |
116c20d2 | 4294 | { |
e450936a RS |
4295 | /* Assign the next unused index to this symbol. */ |
4296 | *indexp = output_index; | |
dc810e39 | 4297 | |
8602d4fe | 4298 | if (EXTERN_SYM_P (isymp->n_sclass)) |
e450936a RS |
4299 | { |
4300 | BFD_ASSERT (*sym_hash != NULL); | |
4301 | (*sym_hash)->indx = output_index; | |
4302 | } | |
dc810e39 | 4303 | |
e450936a RS |
4304 | /* If this is a symbol in the TOC which we may have merged |
4305 | (class XMC_TC), remember the symbol index of the TOC | |
4306 | symbol. */ | |
4307 | if (isymp->n_sclass == C_HIDEXT | |
4308 | && aux.x_csect.x_smclas == XMC_TC | |
4309 | && *sym_hash != NULL) | |
4310 | { | |
4311 | BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0); | |
4312 | BFD_ASSERT ((*sym_hash)->toc_section != NULL); | |
4313 | (*sym_hash)->u.toc_indx = output_index; | |
4314 | } | |
252b5132 | 4315 | |
e450936a | 4316 | output_index += add; |
116c20d2 | 4317 | } |
252b5132 | 4318 | |
e450936a RS |
4319 | esym += add * isymesz; |
4320 | isymp += add; | |
4321 | csectpp += add; | |
4322 | sym_hash += add; | |
4323 | debug_index += add; | |
4324 | ++indexp; | |
4325 | for (--add; add > 0; --add) | |
4326 | *indexp++ = -1; | |
4327 | } | |
4328 | ||
4329 | /* Now write out the symbols that we decided to keep. */ | |
4330 | ||
4331 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); | |
4332 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
54e2dbe0 | 4333 | sym_hash = obj_xcoff_sym_hashes (input_bfd); |
baf9bed3 JB |
4334 | isymp = flinfo->internal_syms; |
4335 | indexp = flinfo->sym_indices; | |
e450936a | 4336 | csectpp = xcoff_data (input_bfd)->csects; |
3df13c4a | 4337 | lineno_counts = xcoff_data (input_bfd)->lineno_counts; |
e450936a | 4338 | debug_index = xcoff_data (input_bfd)->debug_indices; |
baf9bed3 | 4339 | outsym = flinfo->outsyms; |
e450936a RS |
4340 | incls = 0; |
4341 | oline = NULL; | |
4342 | while (esym < esym_end) | |
4343 | { | |
4344 | int add; | |
4345 | ||
4346 | add = 1 + isymp->n_numaux; | |
4347 | ||
4348 | if (*indexp < 0) | |
4349 | esym += add * isymesz; | |
4350 | else | |
252b5132 | 4351 | { |
e450936a RS |
4352 | struct internal_syment isym; |
4353 | int i; | |
116c20d2 | 4354 | |
e450936a RS |
4355 | /* Adjust the symbol in order to output it. */ |
4356 | isym = *isymp; | |
116c20d2 NC |
4357 | if (isym._n._n_n._n_zeroes == 0 |
4358 | && isym._n._n_n._n_offset != 0) | |
252b5132 | 4359 | { |
116c20d2 NC |
4360 | /* This symbol has a long name. Enter it in the string |
4361 | table we are building. If *debug_index != -1, the | |
4362 | name has already been entered in the .debug section. */ | |
e450936a | 4363 | if (*debug_index >= 0) |
116c20d2 NC |
4364 | isym._n._n_n._n_offset = *debug_index; |
4365 | else | |
4366 | { | |
4367 | const char *name; | |
4368 | bfd_size_type indx; | |
4369 | ||
4370 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL); | |
4371 | ||
4372 | if (name == NULL) | |
4373 | return FALSE; | |
baf9bed3 | 4374 | indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy); |
116c20d2 NC |
4375 | if (indx == (bfd_size_type) -1) |
4376 | return FALSE; | |
4377 | isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; | |
252b5132 RH |
4378 | } |
4379 | } | |
116c20d2 | 4380 | |
54e2dbe0 RS |
4381 | /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids |
4382 | multiple definition problems when linking a shared object | |
4383 | statically. (The native linker doesn't enter __rtinit into | |
4384 | the normal table at all, but having a local symbol can make | |
4385 | the objdump output easier to read.) */ | |
4386 | if (isym.n_sclass == C_EXT | |
4387 | && *sym_hash | |
4388 | && ((*sym_hash)->flags & XCOFF_RTINIT) != 0) | |
4389 | isym.n_sclass = C_HIDEXT; | |
4390 | ||
116c20d2 NC |
4391 | /* The value of a C_FILE symbol is the symbol index of the |
4392 | next C_FILE symbol. The value of the last C_FILE symbol | |
4393 | is -1. We try to get this right, below, just before we | |
4394 | write the symbols out, but in the general case we may | |
4395 | have to write the symbol out twice. */ | |
4396 | if (isym.n_sclass == C_FILE) | |
4397 | { | |
baf9bed3 JB |
4398 | if (flinfo->last_file_index != -1 |
4399 | && flinfo->last_file.n_value != (bfd_vma) *indexp) | |
116c20d2 NC |
4400 | { |
4401 | /* We must correct the value of the last C_FILE entry. */ | |
baf9bed3 JB |
4402 | flinfo->last_file.n_value = *indexp; |
4403 | if ((bfd_size_type) flinfo->last_file_index >= syment_base) | |
116c20d2 NC |
4404 | { |
4405 | /* The last C_FILE symbol is in this input file. */ | |
4406 | bfd_coff_swap_sym_out (output_bfd, | |
baf9bed3 JB |
4407 | (void *) &flinfo->last_file, |
4408 | (void *) (flinfo->outsyms | |
4409 | + ((flinfo->last_file_index | |
116c20d2 NC |
4410 | - syment_base) |
4411 | * osymesz))); | |
4412 | } | |
4413 | else | |
4414 | { | |
4415 | /* We have already written out the last C_FILE | |
4416 | symbol. We need to write it out again. We | |
4417 | borrow *outsym temporarily. */ | |
4418 | file_ptr pos; | |
252b5132 | 4419 | |
116c20d2 | 4420 | bfd_coff_swap_sym_out (output_bfd, |
baf9bed3 | 4421 | (void *) &flinfo->last_file, |
116c20d2 | 4422 | (void *) outsym); |
beb1bf64 | 4423 | |
116c20d2 | 4424 | pos = obj_sym_filepos (output_bfd); |
baf9bed3 | 4425 | pos += flinfo->last_file_index * osymesz; |
116c20d2 NC |
4426 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 |
4427 | || (bfd_bwrite (outsym, osymesz, output_bfd) | |
4428 | != osymesz)) | |
4429 | return FALSE; | |
4430 | } | |
4431 | } | |
252b5132 | 4432 | |
baf9bed3 JB |
4433 | flinfo->last_file_index = *indexp; |
4434 | flinfo->last_file = isym; | |
116c20d2 | 4435 | } |
252b5132 | 4436 | |
116c20d2 NC |
4437 | /* The value of a C_BINCL or C_EINCL symbol is a file offset |
4438 | into the line numbers. We update the symbol values when | |
4439 | we handle the line numbers. */ | |
4440 | if (isym.n_sclass == C_BINCL | |
4441 | || isym.n_sclass == C_EINCL) | |
4442 | { | |
baf9bed3 | 4443 | isym.n_value = flinfo->line_filepos; |
116c20d2 NC |
4444 | ++incls; |
4445 | } | |
e450936a RS |
4446 | /* The value of a C_BSTAT symbol is the symbol table |
4447 | index of the containing csect. */ | |
4448 | else if (isym.n_sclass == C_BSTAT) | |
116c20d2 | 4449 | { |
116c20d2 | 4450 | bfd_vma indx; |
252b5132 | 4451 | |
116c20d2 NC |
4452 | indx = isym.n_value; |
4453 | if (indx < obj_raw_syment_count (input_bfd)) | |
4454 | { | |
4455 | long symindx; | |
252b5132 | 4456 | |
baf9bed3 | 4457 | symindx = flinfo->sym_indices[indx]; |
116c20d2 NC |
4458 | if (symindx < 0) |
4459 | isym.n_value = 0; | |
4460 | else | |
4461 | isym.n_value = symindx; | |
116c20d2 NC |
4462 | } |
4463 | } | |
e450936a RS |
4464 | else if (isym.n_sclass != C_ESTAT |
4465 | && isym.n_sclass != C_DECL | |
4466 | && isym.n_scnum > 0) | |
4467 | { | |
4468 | isym.n_scnum = (*csectpp)->output_section->target_index; | |
4469 | isym.n_value += ((*csectpp)->output_section->vma | |
4470 | + (*csectpp)->output_offset | |
4471 | - (*csectpp)->vma); | |
4472 | } | |
4473 | ||
4474 | /* Output the symbol. */ | |
4475 | bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym); | |
dc810e39 | 4476 | |
116c20d2 NC |
4477 | esym += isymesz; |
4478 | outsym += osymesz; | |
dc810e39 | 4479 | |
116c20d2 NC |
4480 | for (i = 0; i < isymp->n_numaux && esym < esym_end; i++) |
4481 | { | |
4482 | union internal_auxent aux; | |
dc810e39 | 4483 | |
116c20d2 NC |
4484 | bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type, |
4485 | isymp->n_sclass, i, isymp->n_numaux, | |
4486 | (void *) &aux); | |
252b5132 | 4487 | |
116c20d2 NC |
4488 | if (isymp->n_sclass == C_FILE) |
4489 | { | |
4490 | /* This is the file name (or some comment put in by | |
4491 | the compiler). If it is long, we must put it in | |
4492 | the string table. */ | |
4493 | if (aux.x_file.x_n.x_zeroes == 0 | |
4494 | && aux.x_file.x_n.x_offset != 0) | |
4495 | { | |
4496 | const char *filename; | |
4497 | bfd_size_type indx; | |
dc810e39 | 4498 | |
116c20d2 NC |
4499 | BFD_ASSERT (aux.x_file.x_n.x_offset |
4500 | >= STRING_SIZE_SIZE); | |
4501 | if (strings == NULL) | |
4502 | { | |
4503 | strings = _bfd_coff_read_string_table (input_bfd); | |
4504 | if (strings == NULL) | |
4505 | return FALSE; | |
4506 | } | |
5a3f568b NC |
4507 | if ((bfd_size_type) aux.x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd)) |
4508 | filename = _("<corrupt>"); | |
4509 | else | |
4510 | filename = strings + aux.x_file.x_n.x_offset; | |
baf9bed3 | 4511 | indx = _bfd_stringtab_add (flinfo->strtab, filename, |
116c20d2 NC |
4512 | hash, copy); |
4513 | if (indx == (bfd_size_type) -1) | |
4514 | return FALSE; | |
4515 | aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx; | |
4516 | } | |
4517 | } | |
8602d4fe | 4518 | else if (CSECT_SYM_P (isymp->n_sclass) |
116c20d2 NC |
4519 | && i + 1 == isymp->n_numaux) |
4520 | { | |
dc810e39 | 4521 | |
116c20d2 NC |
4522 | /* We don't support type checking. I don't know if |
4523 | anybody does. */ | |
4524 | aux.x_csect.x_parmhash = 0; | |
4525 | /* I don't think anybody uses these fields, but we'd | |
4526 | better clobber them just in case. */ | |
4527 | aux.x_csect.x_stab = 0; | |
4528 | aux.x_csect.x_snstab = 0; | |
dc810e39 | 4529 | |
116c20d2 NC |
4530 | if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD) |
4531 | { | |
4532 | unsigned long indx; | |
252b5132 | 4533 | |
116c20d2 NC |
4534 | indx = aux.x_csect.x_scnlen.l; |
4535 | if (indx < obj_raw_syment_count (input_bfd)) | |
4536 | { | |
4537 | long symindx; | |
252b5132 | 4538 | |
baf9bed3 | 4539 | symindx = flinfo->sym_indices[indx]; |
116c20d2 NC |
4540 | if (symindx < 0) |
4541 | { | |
4542 | aux.x_csect.x_scnlen.l = 0; | |
4543 | } | |
4544 | else | |
4545 | { | |
4546 | aux.x_csect.x_scnlen.l = symindx; | |
4547 | } | |
4548 | } | |
4549 | } | |
4550 | } | |
4551 | else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL) | |
4552 | { | |
4553 | unsigned long indx; | |
252b5132 | 4554 | |
116c20d2 NC |
4555 | if (ISFCN (isymp->n_type) |
4556 | || ISTAG (isymp->n_sclass) | |
4557 | || isymp->n_sclass == C_BLOCK | |
4558 | || isymp->n_sclass == C_FCN) | |
4559 | { | |
4560 | indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l; | |
4561 | if (indx > 0 | |
4562 | && indx < obj_raw_syment_count (input_bfd)) | |
4563 | { | |
4564 | /* We look forward through the symbol for | |
4565 | the index of the next symbol we are going | |
4566 | to include. I don't know if this is | |
4567 | entirely right. */ | |
baf9bed3 | 4568 | while (flinfo->sym_indices[indx] < 0 |
116c20d2 NC |
4569 | && indx < obj_raw_syment_count (input_bfd)) |
4570 | ++indx; | |
4571 | if (indx >= obj_raw_syment_count (input_bfd)) | |
4572 | indx = output_index; | |
4573 | else | |
baf9bed3 | 4574 | indx = flinfo->sym_indices[indx]; |
116c20d2 | 4575 | aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx; |
252b5132 | 4576 | |
116c20d2 NC |
4577 | } |
4578 | } | |
252b5132 | 4579 | |
116c20d2 NC |
4580 | indx = aux.x_sym.x_tagndx.l; |
4581 | if (indx > 0 && indx < obj_raw_syment_count (input_bfd)) | |
4582 | { | |
4583 | long symindx; | |
252b5132 | 4584 | |
baf9bed3 | 4585 | symindx = flinfo->sym_indices[indx]; |
116c20d2 NC |
4586 | if (symindx < 0) |
4587 | aux.x_sym.x_tagndx.l = 0; | |
4588 | else | |
4589 | aux.x_sym.x_tagndx.l = symindx; | |
4590 | } | |
252b5132 | 4591 | |
116c20d2 | 4592 | } |
252b5132 | 4593 | |
116c20d2 NC |
4594 | /* Copy over the line numbers, unless we are stripping |
4595 | them. We do this on a symbol by symbol basis in | |
4596 | order to more easily handle garbage collection. */ | |
8602d4fe | 4597 | if (CSECT_SYM_P (isymp->n_sclass) |
116c20d2 NC |
4598 | && i == 0 |
4599 | && isymp->n_numaux > 1 | |
4600 | && ISFCN (isymp->n_type) | |
4601 | && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) | |
4602 | { | |
3df13c4a | 4603 | if (*lineno_counts == 0) |
116c20d2 NC |
4604 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0; |
4605 | else | |
4606 | { | |
4607 | asection *enclosing; | |
4608 | unsigned int enc_count; | |
4609 | bfd_signed_vma linoff; | |
4610 | struct internal_lineno lin; | |
3df13c4a RS |
4611 | bfd_byte *linp; |
4612 | bfd_byte *linpend; | |
4613 | bfd_vma offset; | |
4614 | file_ptr pos; | |
4615 | bfd_size_type amt; | |
252b5132 | 4616 | |
3df13c4a RS |
4617 | /* Read in the enclosing section's line-number |
4618 | information, if we haven't already. */ | |
116c20d2 NC |
4619 | o = *csectpp; |
4620 | enclosing = xcoff_section_data (abfd, o)->enclosing; | |
4621 | enc_count = xcoff_section_data (abfd, o)->lineno_count; | |
4622 | if (oline != enclosing) | |
4623 | { | |
3df13c4a RS |
4624 | pos = enclosing->line_filepos; |
4625 | amt = linesz * enc_count; | |
116c20d2 | 4626 | if (bfd_seek (input_bfd, pos, SEEK_SET) != 0 |
baf9bed3 | 4627 | || (bfd_bread (flinfo->linenos, amt, input_bfd) |
116c20d2 NC |
4628 | != amt)) |
4629 | return FALSE; | |
4630 | oline = enclosing; | |
4631 | } | |
beb1bf64 | 4632 | |
3df13c4a RS |
4633 | /* Copy across the first entry, adjusting its |
4634 | symbol index. */ | |
116c20d2 NC |
4635 | linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr |
4636 | - enclosing->line_filepos); | |
baf9bed3 | 4637 | linp = flinfo->linenos + linoff; |
3df13c4a RS |
4638 | bfd_coff_swap_lineno_in (input_bfd, linp, &lin); |
4639 | lin.l_addr.l_symndx = *indexp; | |
4640 | bfd_coff_swap_lineno_out (output_bfd, &lin, linp); | |
3df13c4a RS |
4641 | |
4642 | /* Copy the other entries, adjusting their addresses. */ | |
4643 | linpend = linp + *lineno_counts * linesz; | |
4644 | offset = (o->output_section->vma | |
4645 | + o->output_offset | |
4646 | - o->vma); | |
8707bb87 | 4647 | for (linp += linesz; linp < linpend; linp += linesz) |
116c20d2 | 4648 | { |
3df13c4a RS |
4649 | bfd_coff_swap_lineno_in (input_bfd, linp, &lin); |
4650 | lin.l_addr.l_paddr += offset; | |
4651 | bfd_coff_swap_lineno_out (output_bfd, &lin, linp); | |
4652 | } | |
252b5132 | 4653 | |
3df13c4a RS |
4654 | /* Write out the entries we've just processed. */ |
4655 | pos = (o->output_section->line_filepos | |
116c20d2 | 4656 | + o->output_section->lineno_count * linesz); |
3df13c4a RS |
4657 | amt = linesz * *lineno_counts; |
4658 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 | |
baf9bed3 | 4659 | || bfd_bwrite (flinfo->linenos + linoff, |
3df13c4a RS |
4660 | amt, output_bfd) != amt) |
4661 | return FALSE; | |
4662 | o->output_section->lineno_count += *lineno_counts; | |
252b5132 | 4663 | |
3df13c4a RS |
4664 | /* Record the offset of the symbol's line numbers |
4665 | in the output file. */ | |
4666 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos; | |
252b5132 | 4667 | |
3df13c4a RS |
4668 | if (incls > 0) |
4669 | { | |
4670 | struct internal_syment *iisp, *iispend; | |
4671 | long *iindp; | |
4672 | bfd_byte *oos; | |
4673 | bfd_vma range_start, range_end; | |
4674 | int iiadd; | |
4675 | ||
4676 | /* Update any C_BINCL or C_EINCL symbols | |
4677 | that refer to a line number in the | |
4678 | range we just output. */ | |
baf9bed3 | 4679 | iisp = flinfo->internal_syms; |
3df13c4a | 4680 | iispend = iisp + obj_raw_syment_count (input_bfd); |
baf9bed3 JB |
4681 | iindp = flinfo->sym_indices; |
4682 | oos = flinfo->outsyms; | |
3df13c4a RS |
4683 | range_start = enclosing->line_filepos + linoff; |
4684 | range_end = range_start + *lineno_counts * linesz; | |
4685 | while (iisp < iispend) | |
116c20d2 | 4686 | { |
3df13c4a RS |
4687 | if (*iindp >= 0 |
4688 | && (iisp->n_sclass == C_BINCL | |
4689 | || iisp->n_sclass == C_EINCL) | |
4690 | && iisp->n_value >= range_start | |
4691 | && iisp->n_value < range_end) | |
116c20d2 | 4692 | { |
3df13c4a RS |
4693 | struct internal_syment iis; |
4694 | ||
4695 | bfd_coff_swap_sym_in (output_bfd, oos, &iis); | |
4696 | iis.n_value = (iisp->n_value | |
4697 | - range_start | |
4698 | + pos); | |
4699 | bfd_coff_swap_sym_out (output_bfd, | |
4700 | &iis, oos); | |
4701 | --incls; | |
116c20d2 | 4702 | } |
3df13c4a RS |
4703 | |
4704 | iiadd = 1 + iisp->n_numaux; | |
4705 | if (*iindp >= 0) | |
4706 | oos += iiadd * osymesz; | |
4707 | iisp += iiadd; | |
4708 | iindp += iiadd; | |
116c20d2 NC |
4709 | } |
4710 | } | |
252b5132 RH |
4711 | } |
4712 | } | |
252b5132 | 4713 | |
116c20d2 NC |
4714 | bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type, |
4715 | isymp->n_sclass, i, isymp->n_numaux, | |
4716 | (void *) outsym); | |
4717 | outsym += osymesz; | |
4718 | esym += isymesz; | |
dc810e39 | 4719 | } |
252b5132 RH |
4720 | } |
4721 | ||
54e2dbe0 | 4722 | sym_hash += add; |
116c20d2 NC |
4723 | indexp += add; |
4724 | isymp += add; | |
4725 | csectpp += add; | |
3df13c4a | 4726 | lineno_counts += add; |
e450936a | 4727 | debug_index += add; |
116c20d2 | 4728 | } |
252b5132 | 4729 | |
116c20d2 NC |
4730 | /* If we swapped out a C_FILE symbol, guess that the next C_FILE |
4731 | symbol will be the first symbol in the next input file. In the | |
4732 | normal case, this will save us from writing out the C_FILE symbol | |
4733 | again. */ | |
baf9bed3 JB |
4734 | if (flinfo->last_file_index != -1 |
4735 | && (bfd_size_type) flinfo->last_file_index >= syment_base) | |
116c20d2 | 4736 | { |
baf9bed3 JB |
4737 | flinfo->last_file.n_value = output_index; |
4738 | bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file, | |
4739 | (void *) (flinfo->outsyms | |
07d6d2b8 AM |
4740 | + ((flinfo->last_file_index - syment_base) |
4741 | * osymesz))); | |
116c20d2 | 4742 | } |
492055e6 | 4743 | |
116c20d2 | 4744 | /* Write the modified symbols to the output file. */ |
baf9bed3 | 4745 | if (outsym > flinfo->outsyms) |
116c20d2 NC |
4746 | { |
4747 | file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz; | |
baf9bed3 | 4748 | bfd_size_type amt = outsym - flinfo->outsyms; |
116c20d2 | 4749 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 |
baf9bed3 | 4750 | || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt) |
116c20d2 | 4751 | return FALSE; |
fbc4fff4 | 4752 | |
116c20d2 | 4753 | BFD_ASSERT ((obj_raw_syment_count (output_bfd) |
baf9bed3 | 4754 | + (outsym - flinfo->outsyms) / osymesz) |
116c20d2 | 4755 | == output_index); |
fbc4fff4 | 4756 | |
116c20d2 NC |
4757 | obj_raw_syment_count (output_bfd) = output_index; |
4758 | } | |
fbc4fff4 | 4759 | |
116c20d2 NC |
4760 | /* Don't let the linker relocation routines discard the symbols. */ |
4761 | keep_syms = obj_coff_keep_syms (input_bfd); | |
4762 | obj_coff_keep_syms (input_bfd) = TRUE; | |
252b5132 | 4763 | |
116c20d2 NC |
4764 | /* Relocate the contents of each section. */ |
4765 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
4766 | { | |
4767 | bfd_byte *contents; | |
252b5132 | 4768 | |
116c20d2 NC |
4769 | if (! o->linker_mark) |
4770 | /* This section was omitted from the link. */ | |
4771 | continue; | |
252b5132 | 4772 | |
116c20d2 NC |
4773 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
4774 | || o->size == 0 | |
4775 | || (o->flags & SEC_IN_MEMORY) != 0) | |
4776 | continue; | |
dc810e39 | 4777 | |
116c20d2 NC |
4778 | /* We have set filepos correctly for the sections we created to |
4779 | represent csects, so bfd_get_section_contents should work. */ | |
4780 | if (coff_section_data (input_bfd, o) != NULL | |
4781 | && coff_section_data (input_bfd, o)->contents != NULL) | |
4782 | contents = coff_section_data (input_bfd, o)->contents; | |
4783 | else | |
4784 | { | |
4785 | bfd_size_type sz = o->rawsize ? o->rawsize : o->size; | |
baf9bed3 | 4786 | if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz)) |
116c20d2 | 4787 | return FALSE; |
baf9bed3 | 4788 | contents = flinfo->contents; |
252b5132 RH |
4789 | } |
4790 | ||
116c20d2 | 4791 | if ((o->flags & SEC_RELOC) != 0) |
252b5132 | 4792 | { |
116c20d2 NC |
4793 | int target_index; |
4794 | struct internal_reloc *internal_relocs; | |
4795 | struct internal_reloc *irel; | |
4796 | bfd_vma offset; | |
4797 | struct internal_reloc *irelend; | |
4798 | struct xcoff_link_hash_entry **rel_hash; | |
4799 | long r_symndx; | |
252b5132 | 4800 | |
116c20d2 NC |
4801 | /* Read in the relocs. */ |
4802 | target_index = o->output_section->target_index; | |
4803 | internal_relocs = (xcoff_read_internal_relocs | |
baf9bed3 | 4804 | (input_bfd, o, FALSE, flinfo->external_relocs, |
116c20d2 | 4805 | TRUE, |
baf9bed3 | 4806 | (flinfo->section_info[target_index].relocs |
116c20d2 NC |
4807 | + o->output_section->reloc_count))); |
4808 | if (internal_relocs == NULL) | |
4809 | return FALSE; | |
beb1bf64 | 4810 | |
116c20d2 NC |
4811 | /* Call processor specific code to relocate the section |
4812 | contents. */ | |
baf9bed3 | 4813 | if (! bfd_coff_relocate_section (output_bfd, flinfo->info, |
116c20d2 NC |
4814 | input_bfd, o, |
4815 | contents, | |
4816 | internal_relocs, | |
baf9bed3 | 4817 | flinfo->internal_syms, |
116c20d2 | 4818 | xcoff_data (input_bfd)->csects)) |
b34976b6 | 4819 | return FALSE; |
252b5132 | 4820 | |
116c20d2 NC |
4821 | offset = o->output_section->vma + o->output_offset - o->vma; |
4822 | irel = internal_relocs; | |
4823 | irelend = irel + o->reloc_count; | |
baf9bed3 | 4824 | rel_hash = (flinfo->section_info[target_index].rel_hashes |
116c20d2 NC |
4825 | + o->output_section->reloc_count); |
4826 | for (; irel < irelend; irel++, rel_hash++) | |
4827 | { | |
4828 | struct xcoff_link_hash_entry *h = NULL; | |
252b5132 | 4829 | |
116c20d2 | 4830 | *rel_hash = NULL; |
252b5132 | 4831 | |
116c20d2 | 4832 | /* Adjust the reloc address and symbol index. */ |
252b5132 | 4833 | |
116c20d2 | 4834 | irel->r_vaddr += offset; |
252b5132 | 4835 | |
116c20d2 | 4836 | r_symndx = irel->r_symndx; |
beb1bf64 | 4837 | |
116c20d2 NC |
4838 | if (r_symndx == -1) |
4839 | h = NULL; | |
4840 | else | |
4841 | h = obj_xcoff_sym_hashes (input_bfd)[r_symndx]; | |
252b5132 | 4842 | |
baf9bed3 | 4843 | if (r_symndx != -1 && flinfo->info->strip != strip_all) |
252b5132 | 4844 | { |
116c20d2 NC |
4845 | if (h != NULL |
4846 | && h->smclas != XMC_TD | |
4847 | && (irel->r_type == R_TOC | |
4848 | || irel->r_type == R_GL | |
4849 | || irel->r_type == R_TCL | |
4850 | || irel->r_type == R_TRL | |
4851 | || irel->r_type == R_TRLA)) | |
252b5132 | 4852 | { |
116c20d2 NC |
4853 | /* This is a TOC relative reloc with a symbol |
4854 | attached. The symbol should be the one which | |
4855 | this reloc is for. We want to make this | |
4856 | reloc against the TOC address of the symbol, | |
4857 | not the symbol itself. */ | |
4858 | BFD_ASSERT (h->toc_section != NULL); | |
4859 | BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0); | |
4860 | if (h->u.toc_indx != -1) | |
4861 | irel->r_symndx = h->u.toc_indx; | |
4862 | else | |
4863 | { | |
4864 | struct xcoff_toc_rel_hash *n; | |
4865 | struct xcoff_link_section_info *si; | |
4866 | bfd_size_type amt; | |
4867 | ||
4868 | amt = sizeof (* n); | |
baf9bed3 | 4869 | n = bfd_alloc (flinfo->output_bfd, amt); |
116c20d2 NC |
4870 | if (n == NULL) |
4871 | return FALSE; | |
baf9bed3 | 4872 | si = flinfo->section_info + target_index; |
116c20d2 NC |
4873 | n->next = si->toc_rel_hashes; |
4874 | n->h = h; | |
4875 | n->rel = irel; | |
4876 | si->toc_rel_hashes = n; | |
4877 | } | |
252b5132 | 4878 | } |
116c20d2 | 4879 | else if (h != NULL) |
252b5132 | 4880 | { |
116c20d2 NC |
4881 | /* This is a global symbol. */ |
4882 | if (h->indx >= 0) | |
4883 | irel->r_symndx = h->indx; | |
4884 | else | |
4885 | { | |
4886 | /* This symbol is being written at the end | |
4887 | of the file, and we do not yet know the | |
4888 | symbol index. We save the pointer to the | |
4889 | hash table entry in the rel_hash list. | |
4890 | We set the indx field to -2 to indicate | |
4891 | that this symbol must not be stripped. */ | |
4892 | *rel_hash = h; | |
4893 | h->indx = -2; | |
4894 | } | |
252b5132 | 4895 | } |
116c20d2 NC |
4896 | else |
4897 | { | |
4898 | long indx; | |
252b5132 | 4899 | |
baf9bed3 | 4900 | indx = flinfo->sym_indices[r_symndx]; |
252b5132 | 4901 | |
116c20d2 NC |
4902 | if (indx == -1) |
4903 | { | |
4904 | struct internal_syment *is; | |
252b5132 | 4905 | |
116c20d2 NC |
4906 | /* Relocations against a TC0 TOC anchor are |
4907 | automatically transformed to be against | |
4908 | the TOC anchor in the output file. */ | |
baf9bed3 | 4909 | is = flinfo->internal_syms + r_symndx; |
116c20d2 NC |
4910 | if (is->n_sclass == C_HIDEXT |
4911 | && is->n_numaux > 0) | |
4912 | { | |
4913 | void * auxptr; | |
4914 | union internal_auxent aux; | |
252b5132 | 4915 | |
116c20d2 NC |
4916 | auxptr = ((void *) |
4917 | (((bfd_byte *) | |
4918 | obj_coff_external_syms (input_bfd)) | |
4919 | + ((r_symndx + is->n_numaux) | |
4920 | * isymesz))); | |
4921 | bfd_coff_swap_aux_in (input_bfd, auxptr, | |
4922 | is->n_type, is->n_sclass, | |
4923 | is->n_numaux - 1, | |
4924 | is->n_numaux, | |
4925 | (void *) &aux); | |
4926 | if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD | |
4927 | && aux.x_csect.x_smclas == XMC_TC0) | |
baf9bed3 | 4928 | indx = flinfo->toc_symindx; |
116c20d2 NC |
4929 | } |
4930 | } | |
252b5132 | 4931 | |
116c20d2 NC |
4932 | if (indx != -1) |
4933 | irel->r_symndx = indx; | |
4934 | else | |
4935 | { | |
252b5132 | 4936 | |
116c20d2 | 4937 | struct internal_syment *is; |
252b5132 | 4938 | |
116c20d2 NC |
4939 | const char *name; |
4940 | char buf[SYMNMLEN + 1]; | |
252b5132 | 4941 | |
116c20d2 NC |
4942 | /* This reloc is against a symbol we are |
4943 | stripping. It would be possible to handle | |
4944 | this case, but I don't think it's worth it. */ | |
baf9bed3 | 4945 | is = flinfo->internal_syms + r_symndx; |
beb1bf64 | 4946 | |
07d6d2b8 AM |
4947 | if (is->n_sclass != C_DWARF) |
4948 | { | |
4949 | name = (_bfd_coff_internal_syment_name | |
4950 | (input_bfd, is, buf)); | |
252b5132 | 4951 | |
07d6d2b8 AM |
4952 | if (name == NULL) |
4953 | return FALSE; | |
252b5132 | 4954 | |
1a72702b AM |
4955 | (*flinfo->info->callbacks->unattached_reloc) |
4956 | (flinfo->info, name, | |
4957 | input_bfd, o, irel->r_vaddr); | |
07d6d2b8 | 4958 | } |
116c20d2 NC |
4959 | } |
4960 | } | |
252b5132 | 4961 | } |
252b5132 | 4962 | |
5ccfed9b | 4963 | if ((o->flags & SEC_DEBUGGING) == 0 |
07d6d2b8 | 4964 | && xcoff_need_ldrel_p (flinfo->info, irel, h)) |
252b5132 | 4965 | { |
b3d1832c | 4966 | asection *sec; |
252b5132 | 4967 | |
b3d1832c RS |
4968 | if (r_symndx == -1) |
4969 | sec = NULL; | |
4970 | else if (h == NULL) | |
4971 | sec = xcoff_data (input_bfd)->csects[r_symndx]; | |
116c20d2 | 4972 | else |
b3d1832c | 4973 | sec = xcoff_symbol_section (h); |
baf9bed3 | 4974 | if (!xcoff_create_ldrel (output_bfd, flinfo, |
b3d1832c RS |
4975 | o->output_section, input_bfd, |
4976 | irel, sec, h)) | |
4977 | return FALSE; | |
116c20d2 NC |
4978 | } |
4979 | } | |
252b5132 | 4980 | |
116c20d2 NC |
4981 | o->output_section->reloc_count += o->reloc_count; |
4982 | } | |
252b5132 | 4983 | |
116c20d2 NC |
4984 | /* Write out the modified section contents. */ |
4985 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
4986 | contents, (file_ptr) o->output_offset, | |
4987 | o->size)) | |
4988 | return FALSE; | |
4989 | } | |
252b5132 | 4990 | |
116c20d2 | 4991 | obj_coff_keep_syms (input_bfd) = keep_syms; |
252b5132 | 4992 | |
baf9bed3 | 4993 | if (! flinfo->info->keep_memory) |
116c20d2 NC |
4994 | { |
4995 | if (! _bfd_coff_free_symbols (input_bfd)) | |
4996 | return FALSE; | |
4997 | } | |
252b5132 | 4998 | |
116c20d2 NC |
4999 | return TRUE; |
5000 | } | |
252b5132 | 5001 | |
116c20d2 NC |
5002 | #undef N_TMASK |
5003 | #undef N_BTSHFT | |
252b5132 | 5004 | |
116c20d2 | 5005 | /* Sort relocs by VMA. This is called via qsort. */ |
252b5132 | 5006 | |
116c20d2 NC |
5007 | static int |
5008 | xcoff_sort_relocs (const void * p1, const void * p2) | |
5009 | { | |
5010 | const struct internal_reloc *r1 = (const struct internal_reloc *) p1; | |
5011 | const struct internal_reloc *r2 = (const struct internal_reloc *) p2; | |
252b5132 | 5012 | |
116c20d2 NC |
5013 | if (r1->r_vaddr > r2->r_vaddr) |
5014 | return 1; | |
5015 | else if (r1->r_vaddr < r2->r_vaddr) | |
5016 | return -1; | |
5017 | else | |
5018 | return 0; | |
5019 | } | |
252b5132 | 5020 | |
47dfb2ca RS |
5021 | /* Return true if section SEC is a TOC section. */ |
5022 | ||
5023 | static inline bfd_boolean | |
5024 | xcoff_toc_section_p (asection *sec) | |
5025 | { | |
5026 | const char *name; | |
5027 | ||
5028 | name = sec->name; | |
5029 | if (name[0] == '.' && name[1] == 't') | |
5030 | { | |
5031 | if (name[2] == 'c') | |
5032 | { | |
5033 | if (name[3] == '0' && name[4] == 0) | |
5034 | return TRUE; | |
5035 | if (name[3] == 0) | |
5036 | return TRUE; | |
5037 | } | |
5038 | if (name[2] == 'd' && name[3] == 0) | |
5039 | return TRUE; | |
5040 | } | |
5041 | return FALSE; | |
5042 | } | |
5043 | ||
5044 | /* See if the link requires a TOC (it usually does!). If so, find a | |
5045 | good place to put the TOC anchor csect, and write out the associated | |
5046 | symbol. */ | |
5047 | ||
5048 | static bfd_boolean | |
baf9bed3 | 5049 | xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo) |
47dfb2ca RS |
5050 | { |
5051 | bfd_vma toc_start, toc_end, start, end, best_address; | |
5052 | asection *sec; | |
5053 | bfd *input_bfd; | |
5054 | int section_index; | |
5055 | struct internal_syment irsym; | |
5056 | union internal_auxent iraux; | |
5057 | file_ptr pos; | |
5058 | size_t size; | |
5059 | ||
5060 | /* Set [TOC_START, TOC_END) to the range of the TOC. Record the | |
5061 | index of a csect at the beginning of the TOC. */ | |
5062 | toc_start = ~(bfd_vma) 0; | |
5063 | toc_end = 0; | |
5064 | section_index = -1; | |
baf9bed3 | 5065 | for (input_bfd = flinfo->info->input_bfds; |
47dfb2ca | 5066 | input_bfd != NULL; |
c72f2fb2 | 5067 | input_bfd = input_bfd->link.next) |
47dfb2ca RS |
5068 | for (sec = input_bfd->sections; sec != NULL; sec = sec->next) |
5069 | if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec)) | |
5070 | { | |
5071 | start = sec->output_section->vma + sec->output_offset; | |
5072 | if (toc_start > start) | |
5073 | { | |
5074 | toc_start = start; | |
5075 | section_index = sec->output_section->target_index; | |
5076 | } | |
5077 | ||
5078 | end = start + sec->size; | |
5079 | if (toc_end < end) | |
5080 | toc_end = end; | |
5081 | } | |
5082 | ||
5083 | /* There's no need for a TC0 symbol if we don't have a TOC. */ | |
5084 | if (toc_end < toc_start) | |
5085 | { | |
5086 | xcoff_data (output_bfd)->toc = toc_start; | |
5087 | return TRUE; | |
5088 | } | |
5089 | ||
5090 | if (toc_end - toc_start < 0x8000) | |
5091 | /* Every TOC csect can be accessed from TOC_START. */ | |
5092 | best_address = toc_start; | |
5093 | else | |
5094 | { | |
5095 | /* Find the lowest TOC csect that is still within range of TOC_END. */ | |
5096 | best_address = toc_end; | |
baf9bed3 | 5097 | for (input_bfd = flinfo->info->input_bfds; |
47dfb2ca | 5098 | input_bfd != NULL; |
c72f2fb2 | 5099 | input_bfd = input_bfd->link.next) |
47dfb2ca RS |
5100 | for (sec = input_bfd->sections; sec != NULL; sec = sec->next) |
5101 | if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec)) | |
5102 | { | |
5103 | start = sec->output_section->vma + sec->output_offset; | |
5104 | if (start < best_address | |
5105 | && start + 0x8000 >= toc_end) | |
5106 | { | |
5107 | best_address = start; | |
5108 | section_index = sec->output_section->target_index; | |
5109 | } | |
5110 | } | |
5111 | ||
5112 | /* Make sure that the start of the TOC is also within range. */ | |
5113 | if (best_address > toc_start + 0x8000) | |
5114 | { | |
4eca0228 | 5115 | _bfd_error_handler |
2dcf00ce | 5116 | (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc " |
47dfb2ca | 5117 | "when compiling"), |
2dcf00ce | 5118 | (uint64_t) (toc_end - toc_start)); |
47dfb2ca RS |
5119 | bfd_set_error (bfd_error_file_too_big); |
5120 | return FALSE; | |
5121 | } | |
5122 | } | |
5123 | ||
5124 | /* Record the chosen TOC value. */ | |
baf9bed3 | 5125 | flinfo->toc_symindx = obj_raw_syment_count (output_bfd); |
47dfb2ca RS |
5126 | xcoff_data (output_bfd)->toc = best_address; |
5127 | xcoff_data (output_bfd)->sntoc = section_index; | |
5128 | ||
5129 | /* Fill out the TC0 symbol. */ | |
b560e2ac AM |
5130 | if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab, |
5131 | &irsym, "TOC")) | |
47dfb2ca RS |
5132 | return FALSE; |
5133 | irsym.n_value = best_address; | |
5134 | irsym.n_scnum = section_index; | |
5135 | irsym.n_sclass = C_HIDEXT; | |
5136 | irsym.n_type = T_NULL; | |
5137 | irsym.n_numaux = 1; | |
baf9bed3 | 5138 | bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms); |
47dfb2ca RS |
5139 | |
5140 | /* Fill out the auxillary csect information. */ | |
5141 | memset (&iraux, 0, sizeof iraux); | |
5142 | iraux.x_csect.x_smtyp = XTY_SD; | |
5143 | iraux.x_csect.x_smclas = XMC_TC0; | |
5144 | iraux.x_csect.x_scnlen.l = 0; | |
5145 | bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1, | |
baf9bed3 | 5146 | flinfo->outsyms + bfd_coff_symesz (output_bfd)); |
47dfb2ca RS |
5147 | |
5148 | /* Write the contents to the file. */ | |
5149 | pos = obj_sym_filepos (output_bfd); | |
5150 | pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd); | |
5151 | size = 2 * bfd_coff_symesz (output_bfd); | |
5152 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 | |
baf9bed3 | 5153 | || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size) |
47dfb2ca RS |
5154 | return FALSE; |
5155 | obj_raw_syment_count (output_bfd) += 2; | |
5156 | ||
5157 | return TRUE; | |
5158 | } | |
5159 | ||
116c20d2 | 5160 | /* Write out a non-XCOFF global symbol. */ |
252b5132 | 5161 | |
116c20d2 | 5162 | static bfd_boolean |
7686d77d | 5163 | xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf) |
116c20d2 | 5164 | { |
7686d77d | 5165 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh; |
baf9bed3 | 5166 | struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf; |
116c20d2 NC |
5167 | bfd *output_bfd; |
5168 | bfd_byte *outsym; | |
5169 | struct internal_syment isym; | |
5170 | union internal_auxent aux; | |
5171 | bfd_boolean result; | |
5172 | file_ptr pos; | |
5173 | bfd_size_type amt; | |
252b5132 | 5174 | |
baf9bed3 JB |
5175 | output_bfd = flinfo->output_bfd; |
5176 | outsym = flinfo->outsyms; | |
252b5132 | 5177 | |
116c20d2 | 5178 | if (h->root.type == bfd_link_hash_warning) |
252b5132 | 5179 | { |
116c20d2 NC |
5180 | h = (struct xcoff_link_hash_entry *) h->root.u.i.link; |
5181 | if (h->root.type == bfd_link_hash_new) | |
5182 | return TRUE; | |
252b5132 RH |
5183 | } |
5184 | ||
116c20d2 | 5185 | /* If this symbol was garbage collected, just skip it. */ |
baf9bed3 | 5186 | if (xcoff_hash_table (flinfo->info)->gc |
116c20d2 NC |
5187 | && (h->flags & XCOFF_MARK) == 0) |
5188 | return TRUE; | |
5189 | ||
5190 | /* If we need a .loader section entry, write it out. */ | |
5191 | if (h->ldsym != NULL) | |
252b5132 | 5192 | { |
116c20d2 NC |
5193 | struct internal_ldsym *ldsym; |
5194 | bfd *impbfd; | |
252b5132 | 5195 | |
116c20d2 | 5196 | ldsym = h->ldsym; |
252b5132 | 5197 | |
116c20d2 NC |
5198 | if (h->root.type == bfd_link_hash_undefined |
5199 | || h->root.type == bfd_link_hash_undefweak) | |
5200 | { | |
252b5132 | 5201 | |
116c20d2 NC |
5202 | ldsym->l_value = 0; |
5203 | ldsym->l_scnum = N_UNDEF; | |
5204 | ldsym->l_smtype = XTY_ER; | |
5205 | impbfd = h->root.u.undef.abfd; | |
252b5132 | 5206 | |
116c20d2 NC |
5207 | } |
5208 | else if (h->root.type == bfd_link_hash_defined | |
5209 | || h->root.type == bfd_link_hash_defweak) | |
5210 | { | |
5211 | asection *sec; | |
252b5132 | 5212 | |
116c20d2 NC |
5213 | sec = h->root.u.def.section; |
5214 | ldsym->l_value = (sec->output_section->vma | |
5215 | + sec->output_offset | |
5216 | + h->root.u.def.value); | |
5217 | ldsym->l_scnum = sec->output_section->target_index; | |
5218 | ldsym->l_smtype = XTY_SD; | |
5219 | impbfd = sec->owner; | |
252b5132 | 5220 | |
dc810e39 | 5221 | } |
116c20d2 NC |
5222 | else |
5223 | abort (); | |
252b5132 | 5224 | |
116c20d2 NC |
5225 | if (((h->flags & XCOFF_DEF_REGULAR) == 0 |
5226 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
5227 | || (h->flags & XCOFF_IMPORT) != 0) | |
5228 | /* Clear l_smtype | |
5229 | Import symbols are defined so the check above will make | |
5230 | the l_smtype XTY_SD. But this is not correct, it should | |
5231 | be cleared. */ | |
5232 | ldsym->l_smtype |= L_IMPORT; | |
252b5132 | 5233 | |
116c20d2 NC |
5234 | if (((h->flags & XCOFF_DEF_REGULAR) != 0 |
5235 | && (h->flags & XCOFF_DEF_DYNAMIC) != 0) | |
5236 | || (h->flags & XCOFF_EXPORT) != 0) | |
5237 | ldsym->l_smtype |= L_EXPORT; | |
252b5132 | 5238 | |
116c20d2 NC |
5239 | if ((h->flags & XCOFF_ENTRY) != 0) |
5240 | ldsym->l_smtype |= L_ENTRY; | |
5241 | ||
5242 | if ((h->flags & XCOFF_RTINIT) != 0) | |
5243 | ldsym->l_smtype = XTY_SD; | |
5244 | ||
5245 | ldsym->l_smclas = h->smclas; | |
5246 | ||
5247 | if (ldsym->l_smtype & L_IMPORT) | |
dc810e39 | 5248 | { |
116c20d2 NC |
5249 | if ((h->root.type == bfd_link_hash_defined |
5250 | || h->root.type == bfd_link_hash_defweak) | |
5251 | && (h->root.u.def.value != 0)) | |
5252 | ldsym->l_smclas = XMC_XO; | |
dc810e39 | 5253 | |
116c20d2 NC |
5254 | else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) == |
5255 | (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) | |
5256 | ldsym->l_smclas = XMC_SV3264; | |
252b5132 | 5257 | |
116c20d2 NC |
5258 | else if (h->flags & XCOFF_SYSCALL32) |
5259 | ldsym->l_smclas = XMC_SV; | |
252b5132 | 5260 | |
116c20d2 NC |
5261 | else if (h->flags & XCOFF_SYSCALL64) |
5262 | ldsym->l_smclas = XMC_SV64; | |
5263 | } | |
5264 | ||
5265 | if (ldsym->l_ifile == -(bfd_size_type) 1) | |
5266 | { | |
5267 | ldsym->l_ifile = 0; | |
5268 | } | |
5269 | else if (ldsym->l_ifile == 0) | |
5270 | { | |
5271 | if ((ldsym->l_smtype & L_IMPORT) == 0) | |
5272 | ldsym->l_ifile = 0; | |
5273 | else if (impbfd == NULL) | |
5274 | ldsym->l_ifile = 0; | |
5275 | else | |
dc810e39 | 5276 | { |
116c20d2 NC |
5277 | BFD_ASSERT (impbfd->xvec == output_bfd->xvec); |
5278 | ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; | |
5279 | } | |
5280 | } | |
252b5132 | 5281 | |
116c20d2 | 5282 | ldsym->l_parm = 0; |
252b5132 | 5283 | |
116c20d2 | 5284 | BFD_ASSERT (h->ldindx >= 0); |
252b5132 | 5285 | |
116c20d2 | 5286 | bfd_xcoff_swap_ldsym_out (output_bfd, ldsym, |
baf9bed3 | 5287 | (flinfo->ldsym + |
116c20d2 | 5288 | (h->ldindx - 3) |
baf9bed3 | 5289 | * bfd_xcoff_ldsymsz(flinfo->output_bfd))); |
116c20d2 NC |
5290 | h->ldsym = NULL; |
5291 | } | |
252b5132 | 5292 | |
116c20d2 NC |
5293 | /* If this symbol needs global linkage code, write it out. */ |
5294 | if (h->root.type == bfd_link_hash_defined | |
5295 | && (h->root.u.def.section | |
baf9bed3 | 5296 | == xcoff_hash_table (flinfo->info)->linkage_section)) |
116c20d2 NC |
5297 | { |
5298 | bfd_byte *p; | |
5299 | bfd_vma tocoff; | |
5300 | unsigned int i; | |
252b5132 | 5301 | |
116c20d2 | 5302 | p = h->root.u.def.section->contents + h->root.u.def.value; |
252b5132 | 5303 | |
116c20d2 NC |
5304 | /* The first instruction in the global linkage code loads a |
5305 | specific TOC element. */ | |
5306 | tocoff = (h->descriptor->toc_section->output_section->vma | |
5307 | + h->descriptor->toc_section->output_offset | |
5308 | - xcoff_data (output_bfd)->toc); | |
dc810e39 | 5309 | |
116c20d2 NC |
5310 | if ((h->descriptor->flags & XCOFF_SET_TOC) != 0) |
5311 | tocoff += h->descriptor->u.toc_offset; | |
252b5132 | 5312 | |
116c20d2 | 5313 | /* The first instruction in the glink code needs to be |
de194d85 | 5314 | cooked to hold the correct offset in the toc. The |
116c20d2 NC |
5315 | rest are just output raw. */ |
5316 | bfd_put_32 (output_bfd, | |
5317 | bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p); | |
252b5132 | 5318 | |
116c20d2 NC |
5319 | /* Start with i == 1 to get past the first instruction done above |
5320 | The /4 is because the glink code is in bytes and we are going | |
5321 | 4 at a pop. */ | |
5322 | for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++) | |
5323 | bfd_put_32 (output_bfd, | |
5324 | (bfd_vma) bfd_xcoff_glink_code(output_bfd, i), | |
5325 | &p[4 * i]); | |
5326 | } | |
dc810e39 | 5327 | |
116c20d2 NC |
5328 | /* If we created a TOC entry for this symbol, write out the required |
5329 | relocs. */ | |
5330 | if ((h->flags & XCOFF_SET_TOC) != 0) | |
5331 | { | |
5332 | asection *tocsec; | |
5333 | asection *osec; | |
5334 | int oindx; | |
5335 | struct internal_reloc *irel; | |
116c20d2 NC |
5336 | struct internal_syment irsym; |
5337 | union internal_auxent iraux; | |
dc810e39 | 5338 | |
116c20d2 NC |
5339 | tocsec = h->toc_section; |
5340 | osec = tocsec->output_section; | |
5341 | oindx = osec->target_index; | |
baf9bed3 | 5342 | irel = flinfo->section_info[oindx].relocs + osec->reloc_count; |
116c20d2 NC |
5343 | irel->r_vaddr = (osec->vma |
5344 | + tocsec->output_offset | |
5345 | + h->u.toc_offset); | |
5346 | ||
5347 | if (h->indx >= 0) | |
5348 | irel->r_symndx = h->indx; | |
5349 | else | |
5350 | { | |
5351 | h->indx = -2; | |
5352 | irel->r_symndx = obj_raw_syment_count (output_bfd); | |
5353 | } | |
5354 | ||
5355 | BFD_ASSERT (h->ldindx >= 0); | |
5356 | ||
5357 | /* Initialize the aux union here instead of closer to when it is | |
5358 | written out below because the length of the csect depends on | |
5359 | whether the output is 32 or 64 bit. */ | |
5360 | memset (&iraux, 0, sizeof iraux); | |
5361 | iraux.x_csect.x_smtyp = XTY_SD; | |
5362 | /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */ | |
5363 | iraux.x_csect.x_smclas = XMC_TC; | |
5364 | ||
5365 | /* 32 bit uses a 32 bit R_POS to do the relocations | |
5366 | 64 bit uses a 64 bit R_POS to do the relocations | |
5367 | ||
5368 | Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit | |
5369 | ||
5370 | Which one is determined by the backend. */ | |
5371 | if (bfd_xcoff_is_xcoff64 (output_bfd)) | |
5372 | { | |
5373 | irel->r_size = 63; | |
5374 | iraux.x_csect.x_scnlen.l = 8; | |
5375 | } | |
5376 | else if (bfd_xcoff_is_xcoff32 (output_bfd)) | |
5377 | { | |
5378 | irel->r_size = 31; | |
5379 | iraux.x_csect.x_scnlen.l = 4; | |
5380 | } | |
5381 | else | |
5382 | return FALSE; | |
5383 | ||
5384 | irel->r_type = R_POS; | |
baf9bed3 | 5385 | flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; |
116c20d2 NC |
5386 | ++osec->reloc_count; |
5387 | ||
baf9bed3 | 5388 | if (!xcoff_create_ldrel (output_bfd, flinfo, osec, |
b3d1832c RS |
5389 | output_bfd, irel, NULL, h)) |
5390 | return FALSE; | |
116c20d2 NC |
5391 | |
5392 | /* We need to emit a symbol to define a csect which holds | |
5393 | the reloc. */ | |
baf9bed3 | 5394 | if (flinfo->info->strip != strip_all) |
116c20d2 | 5395 | { |
b560e2ac AM |
5396 | result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, |
5397 | flinfo->strtab, | |
116c20d2 NC |
5398 | &irsym, h->root.root.string); |
5399 | if (!result) | |
5400 | return FALSE; | |
5401 | ||
5402 | irsym.n_value = irel->r_vaddr; | |
5403 | irsym.n_scnum = osec->target_index; | |
5404 | irsym.n_sclass = C_HIDEXT; | |
5405 | irsym.n_type = T_NULL; | |
5406 | irsym.n_numaux = 1; | |
5407 | ||
5408 | bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym); | |
5409 | outsym += bfd_coff_symesz (output_bfd); | |
5410 | ||
5411 | /* Note : iraux is initialized above. */ | |
5412 | bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT, | |
5413 | 0, 1, (void *) outsym); | |
5414 | outsym += bfd_coff_auxesz (output_bfd); | |
5415 | ||
5416 | if (h->indx >= 0) | |
5417 | { | |
5418 | /* We aren't going to write out the symbols below, so we | |
5419 | need to write them out now. */ | |
5420 | pos = obj_sym_filepos (output_bfd); | |
5421 | pos += (obj_raw_syment_count (output_bfd) | |
5422 | * bfd_coff_symesz (output_bfd)); | |
baf9bed3 | 5423 | amt = outsym - flinfo->outsyms; |
116c20d2 | 5424 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 |
baf9bed3 | 5425 | || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt) |
116c20d2 NC |
5426 | return FALSE; |
5427 | obj_raw_syment_count (output_bfd) += | |
baf9bed3 | 5428 | (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd); |
116c20d2 | 5429 | |
baf9bed3 | 5430 | outsym = flinfo->outsyms; |
116c20d2 NC |
5431 | } |
5432 | } | |
5433 | } | |
5434 | ||
5435 | /* If this symbol is a specially defined function descriptor, write | |
5436 | it out. The first word is the address of the function code | |
5437 | itself, the second word is the address of the TOC, and the third | |
5438 | word is zero. | |
5439 | ||
5440 | 32 bit vs 64 bit | |
5441 | The addresses for the 32 bit will take 4 bytes and the addresses | |
5442 | for 64 bit will take 8 bytes. Similar for the relocs. This type | |
5443 | of logic was also done above to create a TOC entry in | |
5444 | xcoff_write_global_symbol. */ | |
5445 | if ((h->flags & XCOFF_DESCRIPTOR) != 0 | |
5446 | && h->root.type == bfd_link_hash_defined | |
5447 | && (h->root.u.def.section | |
baf9bed3 | 5448 | == xcoff_hash_table (flinfo->info)->descriptor_section)) |
116c20d2 NC |
5449 | { |
5450 | asection *sec; | |
5451 | asection *osec; | |
5452 | int oindx; | |
5453 | bfd_byte *p; | |
5454 | struct xcoff_link_hash_entry *hentry; | |
5455 | asection *esec; | |
5456 | struct internal_reloc *irel; | |
116c20d2 NC |
5457 | asection *tsec; |
5458 | unsigned int reloc_size, byte_size; | |
5459 | ||
5460 | if (bfd_xcoff_is_xcoff64 (output_bfd)) | |
5461 | { | |
5462 | reloc_size = 63; | |
5463 | byte_size = 8; | |
5464 | } | |
5465 | else if (bfd_xcoff_is_xcoff32 (output_bfd)) | |
5466 | { | |
5467 | reloc_size = 31; | |
5468 | byte_size = 4; | |
5469 | } | |
5470 | else | |
5471 | return FALSE; | |
5472 | ||
5473 | sec = h->root.u.def.section; | |
5474 | osec = sec->output_section; | |
5475 | oindx = osec->target_index; | |
5476 | p = sec->contents + h->root.u.def.value; | |
5477 | ||
5478 | hentry = h->descriptor; | |
5479 | BFD_ASSERT (hentry != NULL | |
5480 | && (hentry->root.type == bfd_link_hash_defined | |
5481 | || hentry->root.type == bfd_link_hash_defweak)); | |
5482 | esec = hentry->root.u.def.section; | |
5483 | ||
baf9bed3 | 5484 | irel = flinfo->section_info[oindx].relocs + osec->reloc_count; |
116c20d2 NC |
5485 | irel->r_vaddr = (osec->vma |
5486 | + sec->output_offset | |
5487 | + h->root.u.def.value); | |
5488 | irel->r_symndx = esec->output_section->target_index; | |
5489 | irel->r_type = R_POS; | |
5490 | irel->r_size = reloc_size; | |
baf9bed3 | 5491 | flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; |
116c20d2 NC |
5492 | ++osec->reloc_count; |
5493 | ||
baf9bed3 | 5494 | if (!xcoff_create_ldrel (output_bfd, flinfo, osec, |
b3d1832c RS |
5495 | output_bfd, irel, esec, NULL)) |
5496 | return FALSE; | |
116c20d2 NC |
5497 | |
5498 | /* There are three items to write out, | |
5499 | the address of the code | |
5500 | the address of the toc anchor | |
5501 | the environment pointer. | |
5502 | We are ignoring the environment pointer. So set it to zero. */ | |
5503 | if (bfd_xcoff_is_xcoff64 (output_bfd)) | |
5504 | { | |
5505 | bfd_put_64 (output_bfd, | |
5506 | (esec->output_section->vma + esec->output_offset | |
5507 | + hentry->root.u.def.value), | |
5508 | p); | |
5509 | bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8); | |
5510 | bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16); | |
5511 | } | |
5512 | else | |
5513 | { | |
5514 | /* 32 bit backend | |
5515 | This logic was already called above so the error case where | |
5516 | the backend is neither has already been checked. */ | |
5517 | bfd_put_32 (output_bfd, | |
5518 | (esec->output_section->vma + esec->output_offset | |
5519 | + hentry->root.u.def.value), | |
5520 | p); | |
5521 | bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4); | |
5522 | bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8); | |
5523 | } | |
252b5132 | 5524 | |
116c20d2 NC |
5525 | tsec = coff_section_from_bfd_index (output_bfd, |
5526 | xcoff_data (output_bfd)->sntoc); | |
252b5132 | 5527 | |
116c20d2 NC |
5528 | ++irel; |
5529 | irel->r_vaddr = (osec->vma | |
5530 | + sec->output_offset | |
5531 | + h->root.u.def.value | |
5532 | + byte_size); | |
5533 | irel->r_symndx = tsec->output_section->target_index; | |
5534 | irel->r_type = R_POS; | |
5535 | irel->r_size = reloc_size; | |
baf9bed3 | 5536 | flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; |
116c20d2 | 5537 | ++osec->reloc_count; |
252b5132 | 5538 | |
baf9bed3 | 5539 | if (!xcoff_create_ldrel (output_bfd, flinfo, osec, |
b3d1832c RS |
5540 | output_bfd, irel, tsec, NULL)) |
5541 | return FALSE; | |
116c20d2 | 5542 | } |
252b5132 | 5543 | |
baf9bed3 | 5544 | if (h->indx >= 0 || flinfo->info->strip == strip_all) |
116c20d2 | 5545 | { |
baf9bed3 | 5546 | BFD_ASSERT (outsym == flinfo->outsyms); |
116c20d2 NC |
5547 | return TRUE; |
5548 | } | |
beb1bf64 | 5549 | |
116c20d2 | 5550 | if (h->indx != -2 |
baf9bed3 JB |
5551 | && (flinfo->info->strip == strip_all |
5552 | || (flinfo->info->strip == strip_some | |
5553 | && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string, | |
116c20d2 NC |
5554 | FALSE, FALSE) == NULL))) |
5555 | { | |
baf9bed3 | 5556 | BFD_ASSERT (outsym == flinfo->outsyms); |
116c20d2 NC |
5557 | return TRUE; |
5558 | } | |
dc810e39 | 5559 | |
116c20d2 NC |
5560 | if (h->indx != -2 |
5561 | && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0) | |
5562 | { | |
baf9bed3 | 5563 | BFD_ASSERT (outsym == flinfo->outsyms); |
116c20d2 NC |
5564 | return TRUE; |
5565 | } | |
dc810e39 | 5566 | |
116c20d2 | 5567 | memset (&aux, 0, sizeof aux); |
252b5132 | 5568 | |
116c20d2 | 5569 | h->indx = obj_raw_syment_count (output_bfd); |
dc810e39 | 5570 | |
b560e2ac AM |
5571 | result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab, |
5572 | &isym, h->root.root.string); | |
116c20d2 NC |
5573 | if (!result) |
5574 | return FALSE; | |
dc810e39 | 5575 | |
116c20d2 NC |
5576 | if (h->root.type == bfd_link_hash_undefined |
5577 | || h->root.type == bfd_link_hash_undefweak) | |
5578 | { | |
5579 | isym.n_value = 0; | |
5580 | isym.n_scnum = N_UNDEF; | |
8602d4fe RS |
5581 | if (h->root.type == bfd_link_hash_undefweak |
5582 | && C_WEAKEXT == C_AIX_WEAKEXT) | |
5583 | isym.n_sclass = C_WEAKEXT; | |
5584 | else | |
5585 | isym.n_sclass = C_EXT; | |
116c20d2 NC |
5586 | aux.x_csect.x_smtyp = XTY_ER; |
5587 | } | |
5588 | else if ((h->root.type == bfd_link_hash_defined | |
5589 | || h->root.type == bfd_link_hash_defweak) | |
5590 | && h->smclas == XMC_XO) | |
5591 | { | |
5592 | BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section)); | |
5593 | isym.n_value = h->root.u.def.value; | |
5594 | isym.n_scnum = N_UNDEF; | |
8602d4fe RS |
5595 | if (h->root.type == bfd_link_hash_undefweak |
5596 | && C_WEAKEXT == C_AIX_WEAKEXT) | |
5597 | isym.n_sclass = C_WEAKEXT; | |
5598 | else | |
5599 | isym.n_sclass = C_EXT; | |
116c20d2 NC |
5600 | aux.x_csect.x_smtyp = XTY_ER; |
5601 | } | |
5602 | else if (h->root.type == bfd_link_hash_defined | |
5603 | || h->root.type == bfd_link_hash_defweak) | |
5604 | { | |
5605 | struct xcoff_link_size_list *l; | |
252b5132 | 5606 | |
116c20d2 NC |
5607 | isym.n_value = (h->root.u.def.section->output_section->vma |
5608 | + h->root.u.def.section->output_offset | |
5609 | + h->root.u.def.value); | |
5610 | if (bfd_is_abs_section (h->root.u.def.section->output_section)) | |
5611 | isym.n_scnum = N_ABS; | |
5612 | else | |
5613 | isym.n_scnum = h->root.u.def.section->output_section->target_index; | |
5614 | isym.n_sclass = C_HIDEXT; | |
5615 | aux.x_csect.x_smtyp = XTY_SD; | |
252b5132 | 5616 | |
116c20d2 NC |
5617 | if ((h->flags & XCOFF_HAS_SIZE) != 0) |
5618 | { | |
baf9bed3 | 5619 | for (l = xcoff_hash_table (flinfo->info)->size_list; |
116c20d2 NC |
5620 | l != NULL; |
5621 | l = l->next) | |
5622 | { | |
5623 | if (l->h == h) | |
5624 | { | |
5625 | aux.x_csect.x_scnlen.l = l->size; | |
dc810e39 AM |
5626 | break; |
5627 | } | |
5628 | } | |
dc810e39 | 5629 | } |
252b5132 | 5630 | } |
116c20d2 NC |
5631 | else if (h->root.type == bfd_link_hash_common) |
5632 | { | |
5633 | isym.n_value = (h->root.u.c.p->section->output_section->vma | |
5634 | + h->root.u.c.p->section->output_offset); | |
5635 | isym.n_scnum = h->root.u.c.p->section->output_section->target_index; | |
5636 | isym.n_sclass = C_EXT; | |
5637 | aux.x_csect.x_smtyp = XTY_CM; | |
5638 | aux.x_csect.x_scnlen.l = h->root.u.c.size; | |
5639 | } | |
5640 | else | |
5641 | abort (); | |
5642 | ||
5643 | isym.n_type = T_NULL; | |
5644 | isym.n_numaux = 1; | |
5645 | ||
5646 | bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym); | |
5647 | outsym += bfd_coff_symesz (output_bfd); | |
5648 | ||
5649 | aux.x_csect.x_smclas = h->smclas; | |
5650 | bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1, | |
5651 | (void *) outsym); | |
5652 | outsym += bfd_coff_auxesz (output_bfd); | |
5653 | ||
5654 | if ((h->root.type == bfd_link_hash_defined | |
5655 | || h->root.type == bfd_link_hash_defweak) | |
5656 | && h->smclas != XMC_XO) | |
5657 | { | |
5658 | /* We just output an SD symbol. Now output an LD symbol. */ | |
5659 | h->indx += 2; | |
252b5132 | 5660 | |
8602d4fe RS |
5661 | if (h->root.type == bfd_link_hash_undefweak |
5662 | && C_WEAKEXT == C_AIX_WEAKEXT) | |
5663 | isym.n_sclass = C_WEAKEXT; | |
5664 | else | |
5665 | isym.n_sclass = C_EXT; | |
116c20d2 NC |
5666 | bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym); |
5667 | outsym += bfd_coff_symesz (output_bfd); | |
252b5132 | 5668 | |
116c20d2 NC |
5669 | aux.x_csect.x_smtyp = XTY_LD; |
5670 | aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd); | |
5671 | bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1, | |
5672 | (void *) outsym); | |
5673 | outsym += bfd_coff_auxesz (output_bfd); | |
252b5132 RH |
5674 | } |
5675 | ||
116c20d2 NC |
5676 | pos = obj_sym_filepos (output_bfd); |
5677 | pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd); | |
baf9bed3 | 5678 | amt = outsym - flinfo->outsyms; |
116c20d2 | 5679 | if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 |
baf9bed3 | 5680 | || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt) |
116c20d2 NC |
5681 | return FALSE; |
5682 | obj_raw_syment_count (output_bfd) += | |
baf9bed3 | 5683 | (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd); |
116c20d2 | 5684 | |
b34976b6 | 5685 | return TRUE; |
252b5132 RH |
5686 | } |
5687 | ||
116c20d2 | 5688 | /* Handle a link order which is supposed to generate a reloc. */ |
beb1bf64 | 5689 | |
b34976b6 | 5690 | static bfd_boolean |
116c20d2 | 5691 | xcoff_reloc_link_order (bfd *output_bfd, |
baf9bed3 | 5692 | struct xcoff_final_link_info *flinfo, |
116c20d2 NC |
5693 | asection *output_section, |
5694 | struct bfd_link_order *link_order) | |
252b5132 | 5695 | { |
116c20d2 NC |
5696 | reloc_howto_type *howto; |
5697 | struct xcoff_link_hash_entry *h; | |
5698 | asection *hsec; | |
5699 | bfd_vma hval; | |
5700 | bfd_vma addend; | |
5701 | struct internal_reloc *irel; | |
5702 | struct xcoff_link_hash_entry **rel_hash_ptr; | |
252b5132 | 5703 | |
116c20d2 NC |
5704 | if (link_order->type == bfd_section_reloc_link_order) |
5705 | /* We need to somehow locate a symbol in the right section. The | |
5706 | symbol must either have a value of zero, or we must adjust | |
5707 | the addend by the value of the symbol. FIXME: Write this | |
5708 | when we need it. The old linker couldn't handle this anyhow. */ | |
5709 | abort (); | |
252b5132 | 5710 | |
116c20d2 NC |
5711 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); |
5712 | if (howto == NULL) | |
e92d460e | 5713 | { |
116c20d2 NC |
5714 | bfd_set_error (bfd_error_bad_value); |
5715 | return FALSE; | |
e92d460e AM |
5716 | } |
5717 | ||
116c20d2 | 5718 | h = ((struct xcoff_link_hash_entry *) |
baf9bed3 | 5719 | bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info, |
116c20d2 NC |
5720 | link_order->u.reloc.p->u.name, |
5721 | FALSE, FALSE, TRUE)); | |
5722 | if (h == NULL) | |
5723 | { | |
1a72702b AM |
5724 | (*flinfo->info->callbacks->unattached_reloc) |
5725 | (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0); | |
116c20d2 NC |
5726 | return TRUE; |
5727 | } | |
252b5132 | 5728 | |
b3d1832c RS |
5729 | hsec = xcoff_symbol_section (h); |
5730 | if (h->root.type == bfd_link_hash_defined | |
5731 | || h->root.type == bfd_link_hash_defweak) | |
5732 | hval = h->root.u.def.value; | |
116c20d2 | 5733 | else |
b3d1832c | 5734 | hval = 0; |
252b5132 | 5735 | |
116c20d2 NC |
5736 | addend = link_order->u.reloc.p->addend; |
5737 | if (hsec != NULL) | |
5738 | addend += (hsec->output_section->vma | |
5739 | + hsec->output_offset | |
5740 | + hval); | |
252b5132 | 5741 | |
116c20d2 NC |
5742 | if (addend != 0) |
5743 | { | |
5744 | bfd_size_type size; | |
5745 | bfd_byte *buf; | |
5746 | bfd_reloc_status_type rstat; | |
5747 | bfd_boolean ok; | |
252b5132 | 5748 | |
116c20d2 NC |
5749 | size = bfd_get_reloc_size (howto); |
5750 | buf = bfd_zmalloc (size); | |
6346d5ca | 5751 | if (buf == NULL && size != 0) |
116c20d2 | 5752 | return FALSE; |
252b5132 | 5753 | |
116c20d2 NC |
5754 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); |
5755 | switch (rstat) | |
dc810e39 | 5756 | { |
116c20d2 NC |
5757 | case bfd_reloc_ok: |
5758 | break; | |
5759 | default: | |
5760 | case bfd_reloc_outofrange: | |
5761 | abort (); | |
5762 | case bfd_reloc_overflow: | |
1a72702b AM |
5763 | (*flinfo->info->callbacks->reloc_overflow) |
5764 | (flinfo->info, NULL, link_order->u.reloc.p->u.name, | |
5765 | howto->name, addend, NULL, NULL, (bfd_vma) 0); | |
116c20d2 NC |
5766 | break; |
5767 | } | |
5768 | ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf, | |
5769 | (file_ptr) link_order->offset, size); | |
5770 | free (buf); | |
5771 | if (! ok) | |
5772 | return FALSE; | |
5773 | } | |
252b5132 | 5774 | |
116c20d2 NC |
5775 | /* Store the reloc information in the right place. It will get |
5776 | swapped and written out at the end of the final_link routine. */ | |
baf9bed3 | 5777 | irel = (flinfo->section_info[output_section->target_index].relocs |
116c20d2 | 5778 | + output_section->reloc_count); |
baf9bed3 | 5779 | rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes |
116c20d2 | 5780 | + output_section->reloc_count); |
252b5132 | 5781 | |
116c20d2 NC |
5782 | memset (irel, 0, sizeof (struct internal_reloc)); |
5783 | *rel_hash_ptr = NULL; | |
252b5132 | 5784 | |
116c20d2 | 5785 | irel->r_vaddr = output_section->vma + link_order->offset; |
252b5132 | 5786 | |
116c20d2 NC |
5787 | if (h->indx >= 0) |
5788 | irel->r_symndx = h->indx; | |
5789 | else | |
5790 | { | |
5791 | /* Set the index to -2 to force this symbol to get written out. */ | |
5792 | h->indx = -2; | |
5793 | *rel_hash_ptr = h; | |
5794 | irel->r_symndx = 0; | |
5795 | } | |
252b5132 | 5796 | |
116c20d2 NC |
5797 | irel->r_type = howto->type; |
5798 | irel->r_size = howto->bitsize - 1; | |
5799 | if (howto->complain_on_overflow == complain_overflow_signed) | |
5800 | irel->r_size |= 0x80; | |
beb1bf64 | 5801 | |
116c20d2 | 5802 | ++output_section->reloc_count; |
dc810e39 | 5803 | |
116c20d2 | 5804 | /* Now output the reloc to the .loader section. */ |
baf9bed3 | 5805 | if (xcoff_hash_table (flinfo->info)->loader_section) |
116c20d2 | 5806 | { |
baf9bed3 | 5807 | if (!xcoff_create_ldrel (output_bfd, flinfo, output_section, |
b3d1832c RS |
5808 | output_bfd, irel, hsec, h)) |
5809 | return FALSE; | |
beb1bf64 | 5810 | } |
dc810e39 | 5811 | |
116c20d2 NC |
5812 | return TRUE; |
5813 | } | |
beb1bf64 | 5814 | |
116c20d2 | 5815 | /* Do the final link step. */ |
beb1bf64 | 5816 | |
116c20d2 NC |
5817 | bfd_boolean |
5818 | _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info) | |
5819 | { | |
5820 | bfd_size_type symesz; | |
baf9bed3 | 5821 | struct xcoff_final_link_info flinfo; |
116c20d2 NC |
5822 | asection *o; |
5823 | struct bfd_link_order *p; | |
5824 | bfd_size_type max_contents_size; | |
5825 | bfd_size_type max_sym_count; | |
5826 | bfd_size_type max_lineno_count; | |
5827 | bfd_size_type max_reloc_count; | |
5828 | bfd_size_type max_output_reloc_count; | |
5829 | file_ptr rel_filepos; | |
5830 | unsigned int relsz; | |
5831 | file_ptr line_filepos; | |
5832 | unsigned int linesz; | |
5833 | bfd *sub; | |
5834 | bfd_byte *external_relocs = NULL; | |
5835 | char strbuf[STRING_SIZE_SIZE]; | |
5836 | file_ptr pos; | |
5837 | bfd_size_type amt; | |
dc810e39 | 5838 | |
0e1862bb | 5839 | if (bfd_link_pic (info)) |
116c20d2 | 5840 | abfd->flags |= DYNAMIC; |
dc810e39 | 5841 | |
116c20d2 | 5842 | symesz = bfd_coff_symesz (abfd); |
dc810e39 | 5843 | |
baf9bed3 JB |
5844 | flinfo.info = info; |
5845 | flinfo.output_bfd = abfd; | |
5846 | flinfo.strtab = NULL; | |
5847 | flinfo.section_info = NULL; | |
5848 | flinfo.last_file_index = -1; | |
5849 | flinfo.toc_symindx = -1; | |
5850 | flinfo.internal_syms = NULL; | |
5851 | flinfo.sym_indices = NULL; | |
5852 | flinfo.outsyms = NULL; | |
5853 | flinfo.linenos = NULL; | |
5854 | flinfo.contents = NULL; | |
5855 | flinfo.external_relocs = NULL; | |
252b5132 | 5856 | |
b3d1832c RS |
5857 | if (xcoff_hash_table (info)->loader_section) |
5858 | { | |
baf9bed3 | 5859 | flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents |
b3d1832c | 5860 | + bfd_xcoff_ldhdrsz (abfd)); |
baf9bed3 | 5861 | flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents |
b3d1832c RS |
5862 | + bfd_xcoff_ldhdrsz (abfd) |
5863 | + (xcoff_hash_table (info)->ldhdr.l_nsyms | |
5864 | * bfd_xcoff_ldsymsz (abfd))); | |
5865 | } | |
5866 | else | |
5867 | { | |
baf9bed3 JB |
5868 | flinfo.ldsym = NULL; |
5869 | flinfo.ldrel = NULL; | |
b3d1832c | 5870 | } |
252b5132 | 5871 | |
116c20d2 | 5872 | xcoff_data (abfd)->coff.link_info = info; |
beb1bf64 | 5873 | |
baf9bed3 JB |
5874 | flinfo.strtab = _bfd_stringtab_init (); |
5875 | if (flinfo.strtab == NULL) | |
116c20d2 | 5876 | goto error_return; |
beb1bf64 | 5877 | |
3df13c4a RS |
5878 | /* Count the relocation entries required for the output file. |
5879 | (We've already counted the line numbers.) Determine a few | |
5880 | maximum sizes. */ | |
116c20d2 NC |
5881 | max_contents_size = 0; |
5882 | max_lineno_count = 0; | |
5883 | max_reloc_count = 0; | |
5884 | for (o = abfd->sections; o != NULL; o = o->next) | |
5885 | { | |
5886 | o->reloc_count = 0; | |
8423293d | 5887 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
dc810e39 | 5888 | { |
116c20d2 NC |
5889 | if (p->type == bfd_indirect_link_order) |
5890 | { | |
5891 | asection *sec; | |
dc810e39 | 5892 | |
116c20d2 | 5893 | sec = p->u.indirect.section; |
beb1bf64 | 5894 | |
116c20d2 NC |
5895 | /* Mark all sections which are to be included in the |
5896 | link. This will normally be every section. We need | |
5897 | to do this so that we can identify any sections which | |
5898 | the linker has decided to not include. */ | |
5899 | sec->linker_mark = TRUE; | |
beb1bf64 | 5900 | |
116c20d2 | 5901 | o->reloc_count += sec->reloc_count; |
dc810e39 | 5902 | |
9ea2b942 RS |
5903 | if ((sec->flags & SEC_IN_MEMORY) == 0) |
5904 | { | |
5905 | if (sec->rawsize > max_contents_size) | |
5906 | max_contents_size = sec->rawsize; | |
5907 | if (sec->size > max_contents_size) | |
5908 | max_contents_size = sec->size; | |
5909 | } | |
116c20d2 NC |
5910 | if (coff_section_data (sec->owner, sec) != NULL |
5911 | && xcoff_section_data (sec->owner, sec) != NULL | |
5912 | && (xcoff_section_data (sec->owner, sec)->lineno_count | |
5913 | > max_lineno_count)) | |
5914 | max_lineno_count = | |
5915 | xcoff_section_data (sec->owner, sec)->lineno_count; | |
5916 | if (sec->reloc_count > max_reloc_count) | |
5917 | max_reloc_count = sec->reloc_count; | |
5918 | } | |
5919 | else if (p->type == bfd_section_reloc_link_order | |
5920 | || p->type == bfd_symbol_reloc_link_order) | |
5921 | ++o->reloc_count; | |
dc810e39 | 5922 | } |
116c20d2 | 5923 | } |
252b5132 | 5924 | |
116c20d2 NC |
5925 | /* Compute the file positions for all the sections. */ |
5926 | if (abfd->output_has_begun) | |
5927 | { | |
5928 | if (xcoff_hash_table (info)->file_align != 0) | |
5929 | abort (); | |
5930 | } | |
5931 | else | |
5932 | { | |
5933 | bfd_vma file_align; | |
252b5132 | 5934 | |
116c20d2 NC |
5935 | file_align = xcoff_hash_table (info)->file_align; |
5936 | if (file_align != 0) | |
dc810e39 | 5937 | { |
116c20d2 NC |
5938 | bfd_boolean saw_contents; |
5939 | int indx; | |
116c20d2 | 5940 | file_ptr sofar; |
252b5132 | 5941 | |
116c20d2 NC |
5942 | /* Insert .pad sections before every section which has |
5943 | contents and is loaded, if it is preceded by some other | |
5944 | section which has contents and is loaded. */ | |
5945 | saw_contents = TRUE; | |
04dd1667 | 5946 | for (o = abfd->sections; o != NULL; o = o->next) |
116c20d2 | 5947 | { |
04dd1667 | 5948 | if (strcmp (o->name, ".pad") == 0) |
116c20d2 | 5949 | saw_contents = FALSE; |
04dd1667 AM |
5950 | else if ((o->flags & SEC_HAS_CONTENTS) != 0 |
5951 | && (o->flags & SEC_LOAD) != 0) | |
116c20d2 NC |
5952 | { |
5953 | if (! saw_contents) | |
5954 | saw_contents = TRUE; | |
5955 | else | |
5956 | { | |
5daa8fe7 | 5957 | asection *n; |
252b5132 | 5958 | |
116c20d2 NC |
5959 | /* Create a pad section and place it before the section |
5960 | that needs padding. This requires unlinking and | |
5961 | relinking the bfd's section list. */ | |
252b5132 | 5962 | |
117ed4f8 AM |
5963 | n = bfd_make_section_anyway_with_flags (abfd, ".pad", |
5964 | SEC_HAS_CONTENTS); | |
116c20d2 | 5965 | n->alignment_power = 0; |
252b5132 | 5966 | |
5daa8fe7 | 5967 | bfd_section_list_remove (abfd, n); |
04dd1667 | 5968 | bfd_section_list_insert_before (abfd, o, n); |
116c20d2 NC |
5969 | saw_contents = FALSE; |
5970 | } | |
5971 | } | |
5972 | } | |
5973 | ||
5974 | /* Reset the section indices after inserting the new | |
5975 | sections. */ | |
5976 | indx = 0; | |
5977 | for (o = abfd->sections; o != NULL; o = o->next) | |
5978 | { | |
5979 | ++indx; | |
5980 | o->target_index = indx; | |
5981 | } | |
5982 | BFD_ASSERT ((unsigned int) indx == abfd->section_count); | |
5983 | ||
5984 | /* Work out appropriate sizes for the .pad sections to force | |
5985 | each section to land on a page boundary. This bit of | |
5986 | code knows what compute_section_file_positions is going | |
5987 | to do. */ | |
5988 | sofar = bfd_coff_filhsz (abfd); | |
5989 | sofar += bfd_coff_aoutsz (abfd); | |
5990 | sofar += abfd->section_count * bfd_coff_scnhsz (abfd); | |
5991 | for (o = abfd->sections; o != NULL; o = o->next) | |
5992 | if ((bfd_xcoff_is_reloc_count_overflow | |
5993 | (abfd, (bfd_vma) o->reloc_count)) | |
5994 | || (bfd_xcoff_is_lineno_count_overflow | |
5995 | (abfd, (bfd_vma) o->lineno_count))) | |
5996 | /* 64 does not overflow, need to check if 32 does */ | |
5997 | sofar += bfd_coff_scnhsz (abfd); | |
252b5132 | 5998 | |
116c20d2 | 5999 | for (o = abfd->sections; o != NULL; o = o->next) |
dc810e39 | 6000 | { |
116c20d2 NC |
6001 | if (strcmp (o->name, ".pad") == 0) |
6002 | { | |
6003 | bfd_vma pageoff; | |
252b5132 | 6004 | |
116c20d2 NC |
6005 | BFD_ASSERT (o->size == 0); |
6006 | pageoff = sofar & (file_align - 1); | |
6007 | if (pageoff != 0) | |
6008 | { | |
6009 | o->size = file_align - pageoff; | |
6010 | sofar += file_align - pageoff; | |
6011 | o->flags |= SEC_HAS_CONTENTS; | |
6012 | } | |
6013 | } | |
6014 | else | |
6015 | { | |
6016 | if ((o->flags & SEC_HAS_CONTENTS) != 0) | |
6017 | sofar += BFD_ALIGN (o->size, | |
6018 | 1 << o->alignment_power); | |
6019 | } | |
252b5132 RH |
6020 | } |
6021 | } | |
116c20d2 NC |
6022 | |
6023 | if (! bfd_coff_compute_section_file_positions (abfd)) | |
6024 | goto error_return; | |
252b5132 RH |
6025 | } |
6026 | ||
116c20d2 NC |
6027 | /* Allocate space for the pointers we need to keep for the relocs. */ |
6028 | { | |
6029 | unsigned int i; | |
dc810e39 | 6030 | |
116c20d2 NC |
6031 | /* We use section_count + 1, rather than section_count, because |
6032 | the target_index fields are 1 based. */ | |
6033 | amt = abfd->section_count + 1; | |
6034 | amt *= sizeof (struct xcoff_link_section_info); | |
baf9bed3 JB |
6035 | flinfo.section_info = bfd_malloc (amt); |
6036 | if (flinfo.section_info == NULL) | |
116c20d2 NC |
6037 | goto error_return; |
6038 | for (i = 0; i <= abfd->section_count; i++) | |
6039 | { | |
baf9bed3 JB |
6040 | flinfo.section_info[i].relocs = NULL; |
6041 | flinfo.section_info[i].rel_hashes = NULL; | |
6042 | flinfo.section_info[i].toc_rel_hashes = NULL; | |
116c20d2 NC |
6043 | } |
6044 | } | |
beb1bf64 | 6045 | |
116c20d2 NC |
6046 | /* Set the file positions for the relocs. */ |
6047 | rel_filepos = obj_relocbase (abfd); | |
6048 | relsz = bfd_coff_relsz (abfd); | |
6049 | max_output_reloc_count = 0; | |
6050 | for (o = abfd->sections; o != NULL; o = o->next) | |
6051 | { | |
6052 | if (o->reloc_count == 0) | |
6053 | o->rel_filepos = 0; | |
dc810e39 AM |
6054 | else |
6055 | { | |
116c20d2 NC |
6056 | /* A stripped file has no relocs. However, we still |
6057 | allocate the buffers, so that later code doesn't have to | |
6058 | worry about whether we are stripping or not. */ | |
6059 | if (info->strip == strip_all) | |
6060 | o->rel_filepos = 0; | |
6061 | else | |
6062 | { | |
6063 | o->flags |= SEC_RELOC; | |
6064 | o->rel_filepos = rel_filepos; | |
6065 | rel_filepos += o->reloc_count * relsz; | |
6066 | } | |
252b5132 | 6067 | |
116c20d2 NC |
6068 | /* We don't know the indices of global symbols until we have |
6069 | written out all the local symbols. For each section in | |
6070 | the output file, we keep an array of pointers to hash | |
6071 | table entries. Each entry in the array corresponds to a | |
6072 | reloc. When we find a reloc against a global symbol, we | |
6073 | set the corresponding entry in this array so that we can | |
6074 | fix up the symbol index after we have written out all the | |
6075 | local symbols. | |
252b5132 | 6076 | |
116c20d2 NC |
6077 | Because of this problem, we also keep the relocs in |
6078 | memory until the end of the link. This wastes memory. | |
6079 | We could backpatch the file later, I suppose, although it | |
6080 | would be slow. */ | |
6081 | amt = o->reloc_count; | |
6082 | amt *= sizeof (struct internal_reloc); | |
baf9bed3 | 6083 | flinfo.section_info[o->target_index].relocs = bfd_malloc (amt); |
252b5132 | 6084 | |
116c20d2 NC |
6085 | amt = o->reloc_count; |
6086 | amt *= sizeof (struct xcoff_link_hash_entry *); | |
baf9bed3 | 6087 | flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt); |
252b5132 | 6088 | |
baf9bed3 JB |
6089 | if (flinfo.section_info[o->target_index].relocs == NULL |
6090 | || flinfo.section_info[o->target_index].rel_hashes == NULL) | |
116c20d2 | 6091 | goto error_return; |
beb1bf64 | 6092 | |
116c20d2 NC |
6093 | if (o->reloc_count > max_output_reloc_count) |
6094 | max_output_reloc_count = o->reloc_count; | |
dc810e39 | 6095 | } |
116c20d2 | 6096 | } |
dc810e39 | 6097 | |
116c20d2 NC |
6098 | /* We now know the size of the relocs, so we can determine the file |
6099 | positions of the line numbers. */ | |
6100 | line_filepos = rel_filepos; | |
baf9bed3 | 6101 | flinfo.line_filepos = line_filepos; |
116c20d2 NC |
6102 | linesz = bfd_coff_linesz (abfd); |
6103 | for (o = abfd->sections; o != NULL; o = o->next) | |
6104 | { | |
6105 | if (o->lineno_count == 0) | |
6106 | o->line_filepos = 0; | |
252b5132 RH |
6107 | else |
6108 | { | |
116c20d2 NC |
6109 | o->line_filepos = line_filepos; |
6110 | line_filepos += o->lineno_count * linesz; | |
252b5132 | 6111 | } |
252b5132 | 6112 | |
116c20d2 NC |
6113 | /* Reset the reloc and lineno counts, so that we can use them to |
6114 | count the number of entries we have output so far. */ | |
6115 | o->reloc_count = 0; | |
6116 | o->lineno_count = 0; | |
252b5132 RH |
6117 | } |
6118 | ||
116c20d2 | 6119 | obj_sym_filepos (abfd) = line_filepos; |
252b5132 | 6120 | |
116c20d2 NC |
6121 | /* Figure out the largest number of symbols in an input BFD. Take |
6122 | the opportunity to clear the output_has_begun fields of all the | |
6123 | input BFD's. We want at least 6 symbols, since that is the | |
6124 | number which xcoff_write_global_symbol may need. */ | |
6125 | max_sym_count = 6; | |
c72f2fb2 | 6126 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
252b5132 | 6127 | { |
116c20d2 NC |
6128 | bfd_size_type sz; |
6129 | ||
6130 | sub->output_has_begun = FALSE; | |
6131 | sz = obj_raw_syment_count (sub); | |
6132 | if (sz > max_sym_count) | |
6133 | max_sym_count = sz; | |
252b5132 RH |
6134 | } |
6135 | ||
116c20d2 NC |
6136 | /* Allocate some buffers used while linking. */ |
6137 | amt = max_sym_count * sizeof (struct internal_syment); | |
baf9bed3 | 6138 | flinfo.internal_syms = bfd_malloc (amt); |
252b5132 | 6139 | |
116c20d2 | 6140 | amt = max_sym_count * sizeof (long); |
baf9bed3 | 6141 | flinfo.sym_indices = bfd_malloc (amt); |
252b5132 | 6142 | |
116c20d2 | 6143 | amt = (max_sym_count + 1) * symesz; |
baf9bed3 | 6144 | flinfo.outsyms = bfd_malloc (amt); |
252b5132 | 6145 | |
116c20d2 | 6146 | amt = max_lineno_count * bfd_coff_linesz (abfd); |
baf9bed3 | 6147 | flinfo.linenos = bfd_malloc (amt); |
252b5132 | 6148 | |
116c20d2 | 6149 | amt = max_contents_size; |
baf9bed3 | 6150 | flinfo.contents = bfd_malloc (amt); |
252b5132 | 6151 | |
116c20d2 | 6152 | amt = max_reloc_count * relsz; |
baf9bed3 JB |
6153 | flinfo.external_relocs = bfd_malloc (amt); |
6154 | ||
6155 | if ((flinfo.internal_syms == NULL && max_sym_count > 0) | |
6156 | || (flinfo.sym_indices == NULL && max_sym_count > 0) | |
6157 | || flinfo.outsyms == NULL | |
6158 | || (flinfo.linenos == NULL && max_lineno_count > 0) | |
6159 | || (flinfo.contents == NULL && max_contents_size > 0) | |
6160 | || (flinfo.external_relocs == NULL && max_reloc_count > 0)) | |
116c20d2 NC |
6161 | goto error_return; |
6162 | ||
6163 | obj_raw_syment_count (abfd) = 0; | |
47dfb2ca RS |
6164 | |
6165 | /* Find a TOC symbol, if we need one. */ | |
baf9bed3 | 6166 | if (!xcoff_find_tc0 (abfd, &flinfo)) |
47dfb2ca | 6167 | goto error_return; |
116c20d2 NC |
6168 | |
6169 | /* We now know the position of everything in the file, except that | |
6170 | we don't know the size of the symbol table and therefore we don't | |
6171 | know where the string table starts. We just build the string | |
6172 | table in memory as we go along. We process all the relocations | |
6173 | for a single input file at once. */ | |
6174 | for (o = abfd->sections; o != NULL; o = o->next) | |
6175 | { | |
8423293d | 6176 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
252b5132 | 6177 | { |
116c20d2 NC |
6178 | if (p->type == bfd_indirect_link_order |
6179 | && p->u.indirect.section->owner->xvec == abfd->xvec) | |
252b5132 | 6180 | { |
116c20d2 NC |
6181 | sub = p->u.indirect.section->owner; |
6182 | if (! sub->output_has_begun) | |
252b5132 | 6183 | { |
baf9bed3 | 6184 | if (! xcoff_link_input_bfd (&flinfo, sub)) |
116c20d2 NC |
6185 | goto error_return; |
6186 | sub->output_has_begun = TRUE; | |
252b5132 RH |
6187 | } |
6188 | } | |
116c20d2 NC |
6189 | else if (p->type == bfd_section_reloc_link_order |
6190 | || p->type == bfd_symbol_reloc_link_order) | |
6191 | { | |
baf9bed3 | 6192 | if (! xcoff_reloc_link_order (abfd, &flinfo, o, p)) |
116c20d2 NC |
6193 | goto error_return; |
6194 | } | |
6195 | else | |
6196 | { | |
6197 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
6198 | goto error_return; | |
6199 | } | |
252b5132 RH |
6200 | } |
6201 | } | |
116c20d2 NC |
6202 | |
6203 | /* Free up the buffers used by xcoff_link_input_bfd. */ | |
baf9bed3 | 6204 | if (flinfo.internal_syms != NULL) |
252b5132 | 6205 | { |
baf9bed3 JB |
6206 | free (flinfo.internal_syms); |
6207 | flinfo.internal_syms = NULL; | |
116c20d2 | 6208 | } |
baf9bed3 | 6209 | if (flinfo.sym_indices != NULL) |
116c20d2 | 6210 | { |
baf9bed3 JB |
6211 | free (flinfo.sym_indices); |
6212 | flinfo.sym_indices = NULL; | |
116c20d2 | 6213 | } |
baf9bed3 | 6214 | if (flinfo.linenos != NULL) |
116c20d2 | 6215 | { |
baf9bed3 JB |
6216 | free (flinfo.linenos); |
6217 | flinfo.linenos = NULL; | |
116c20d2 | 6218 | } |
baf9bed3 | 6219 | if (flinfo.contents != NULL) |
116c20d2 | 6220 | { |
baf9bed3 JB |
6221 | free (flinfo.contents); |
6222 | flinfo.contents = NULL; | |
116c20d2 | 6223 | } |
baf9bed3 | 6224 | if (flinfo.external_relocs != NULL) |
116c20d2 | 6225 | { |
baf9bed3 JB |
6226 | free (flinfo.external_relocs); |
6227 | flinfo.external_relocs = NULL; | |
252b5132 | 6228 | } |
252b5132 | 6229 | |
116c20d2 NC |
6230 | /* The value of the last C_FILE symbol is supposed to be -1. Write |
6231 | it out again. */ | |
baf9bed3 | 6232 | if (flinfo.last_file_index != -1) |
116c20d2 | 6233 | { |
baf9bed3 JB |
6234 | flinfo.last_file.n_value = -(bfd_vma) 1; |
6235 | bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file, | |
6236 | (void *) flinfo.outsyms); | |
6237 | pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz; | |
116c20d2 | 6238 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 |
baf9bed3 | 6239 | || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz) |
116c20d2 NC |
6240 | goto error_return; |
6241 | } | |
252b5132 | 6242 | |
116c20d2 NC |
6243 | /* Write out all the global symbols which do not come from XCOFF |
6244 | input files. */ | |
7686d77d | 6245 | bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo); |
252b5132 | 6246 | |
baf9bed3 | 6247 | if (flinfo.outsyms != NULL) |
252b5132 | 6248 | { |
baf9bed3 JB |
6249 | free (flinfo.outsyms); |
6250 | flinfo.outsyms = NULL; | |
116c20d2 | 6251 | } |
252b5132 | 6252 | |
116c20d2 NC |
6253 | /* Now that we have written out all the global symbols, we know the |
6254 | symbol indices to use for relocs against them, and we can finally | |
6255 | write out the relocs. */ | |
6256 | amt = max_output_reloc_count * relsz; | |
6257 | external_relocs = bfd_malloc (amt); | |
6258 | if (external_relocs == NULL && max_output_reloc_count != 0) | |
6259 | goto error_return; | |
252b5132 | 6260 | |
116c20d2 NC |
6261 | for (o = abfd->sections; o != NULL; o = o->next) |
6262 | { | |
6263 | struct internal_reloc *irel; | |
6264 | struct internal_reloc *irelend; | |
6265 | struct xcoff_link_hash_entry **rel_hash; | |
6266 | struct xcoff_toc_rel_hash *toc_rel_hash; | |
6267 | bfd_byte *erel; | |
6268 | bfd_size_type rel_size; | |
252b5132 | 6269 | |
116c20d2 NC |
6270 | /* A stripped file has no relocs. */ |
6271 | if (info->strip == strip_all) | |
6272 | { | |
6273 | o->reloc_count = 0; | |
6274 | continue; | |
6275 | } | |
252b5132 | 6276 | |
116c20d2 NC |
6277 | if (o->reloc_count == 0) |
6278 | continue; | |
252b5132 | 6279 | |
baf9bed3 | 6280 | irel = flinfo.section_info[o->target_index].relocs; |
116c20d2 | 6281 | irelend = irel + o->reloc_count; |
baf9bed3 | 6282 | rel_hash = flinfo.section_info[o->target_index].rel_hashes; |
1c1479bf | 6283 | for (; irel < irelend; irel++, rel_hash++) |
116c20d2 NC |
6284 | { |
6285 | if (*rel_hash != NULL) | |
6286 | { | |
6287 | if ((*rel_hash)->indx < 0) | |
6288 | { | |
1a72702b AM |
6289 | (*info->callbacks->unattached_reloc) |
6290 | (info, (*rel_hash)->root.root.string, | |
6291 | NULL, o, irel->r_vaddr); | |
116c20d2 NC |
6292 | (*rel_hash)->indx = 0; |
6293 | } | |
6294 | irel->r_symndx = (*rel_hash)->indx; | |
6295 | } | |
6296 | } | |
252b5132 | 6297 | |
baf9bed3 | 6298 | for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes; |
116c20d2 NC |
6299 | toc_rel_hash != NULL; |
6300 | toc_rel_hash = toc_rel_hash->next) | |
6301 | { | |
6302 | if (toc_rel_hash->h->u.toc_indx < 0) | |
6303 | { | |
1a72702b AM |
6304 | (*info->callbacks->unattached_reloc) |
6305 | (info, toc_rel_hash->h->root.root.string, | |
6306 | NULL, o, toc_rel_hash->rel->r_vaddr); | |
116c20d2 NC |
6307 | toc_rel_hash->h->u.toc_indx = 0; |
6308 | } | |
6309 | toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx; | |
6310 | } | |
252b5132 | 6311 | |
116c20d2 NC |
6312 | /* XCOFF requires that the relocs be sorted by address. We tend |
6313 | to produce them in the order in which their containing csects | |
6314 | appear in the symbol table, which is not necessarily by | |
6315 | address. So we sort them here. There may be a better way to | |
6316 | do this. */ | |
baf9bed3 | 6317 | qsort ((void *) flinfo.section_info[o->target_index].relocs, |
116c20d2 NC |
6318 | o->reloc_count, sizeof (struct internal_reloc), |
6319 | xcoff_sort_relocs); | |
252b5132 | 6320 | |
baf9bed3 | 6321 | irel = flinfo.section_info[o->target_index].relocs; |
116c20d2 NC |
6322 | irelend = irel + o->reloc_count; |
6323 | erel = external_relocs; | |
6324 | for (; irel < irelend; irel++, rel_hash++, erel += relsz) | |
6325 | bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel); | |
252b5132 | 6326 | |
116c20d2 NC |
6327 | rel_size = relsz * o->reloc_count; |
6328 | if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0 | |
6329 | || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size) | |
6330 | goto error_return; | |
252b5132 RH |
6331 | } |
6332 | ||
116c20d2 | 6333 | if (external_relocs != NULL) |
252b5132 | 6334 | { |
116c20d2 NC |
6335 | free (external_relocs); |
6336 | external_relocs = NULL; | |
252b5132 RH |
6337 | } |
6338 | ||
116c20d2 | 6339 | /* Free up the section information. */ |
baf9bed3 | 6340 | if (flinfo.section_info != NULL) |
252b5132 | 6341 | { |
116c20d2 | 6342 | unsigned int i; |
252b5132 | 6343 | |
116c20d2 | 6344 | for (i = 0; i < abfd->section_count; i++) |
252b5132 | 6345 | { |
baf9bed3 JB |
6346 | if (flinfo.section_info[i].relocs != NULL) |
6347 | free (flinfo.section_info[i].relocs); | |
6348 | if (flinfo.section_info[i].rel_hashes != NULL) | |
6349 | free (flinfo.section_info[i].rel_hashes); | |
252b5132 | 6350 | } |
baf9bed3 JB |
6351 | free (flinfo.section_info); |
6352 | flinfo.section_info = NULL; | |
252b5132 RH |
6353 | } |
6354 | ||
116c20d2 | 6355 | /* Write out the loader section contents. */ |
116c20d2 | 6356 | o = xcoff_hash_table (info)->loader_section; |
b3d1832c RS |
6357 | if (o) |
6358 | { | |
baf9bed3 | 6359 | BFD_ASSERT ((bfd_byte *) flinfo.ldrel |
b3d1832c RS |
6360 | == (xcoff_hash_table (info)->loader_section->contents |
6361 | + xcoff_hash_table (info)->ldhdr.l_impoff)); | |
6362 | if (!bfd_set_section_contents (abfd, o->output_section, o->contents, | |
6363 | (file_ptr) o->output_offset, o->size)) | |
6364 | goto error_return; | |
6365 | } | |
252b5132 | 6366 | |
116c20d2 NC |
6367 | /* Write out the magic sections. */ |
6368 | o = xcoff_hash_table (info)->linkage_section; | |
6369 | if (o->size > 0 | |
6370 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, | |
6371 | (file_ptr) o->output_offset, | |
6372 | o->size)) | |
6373 | goto error_return; | |
6374 | o = xcoff_hash_table (info)->toc_section; | |
6375 | if (o->size > 0 | |
6376 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, | |
6377 | (file_ptr) o->output_offset, | |
6378 | o->size)) | |
6379 | goto error_return; | |
6380 | o = xcoff_hash_table (info)->descriptor_section; | |
6381 | if (o->size > 0 | |
6382 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, | |
6383 | (file_ptr) o->output_offset, | |
6384 | o->size)) | |
6385 | goto error_return; | |
252b5132 | 6386 | |
116c20d2 NC |
6387 | /* Write out the string table. */ |
6388 | pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz; | |
6389 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
6390 | goto error_return; | |
6391 | H_PUT_32 (abfd, | |
baf9bed3 | 6392 | _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE, |
116c20d2 NC |
6393 | strbuf); |
6394 | amt = STRING_SIZE_SIZE; | |
6395 | if (bfd_bwrite (strbuf, amt, abfd) != amt) | |
6396 | goto error_return; | |
baf9bed3 | 6397 | if (! _bfd_stringtab_emit (abfd, flinfo.strtab)) |
116c20d2 | 6398 | goto error_return; |
252b5132 | 6399 | |
baf9bed3 | 6400 | _bfd_stringtab_free (flinfo.strtab); |
252b5132 | 6401 | |
116c20d2 NC |
6402 | /* Write out the debugging string table. */ |
6403 | o = xcoff_hash_table (info)->debug_section; | |
6404 | if (o != NULL) | |
252b5132 | 6405 | { |
116c20d2 | 6406 | struct bfd_strtab_hash *debug_strtab; |
252b5132 | 6407 | |
116c20d2 NC |
6408 | debug_strtab = xcoff_hash_table (info)->debug_strtab; |
6409 | BFD_ASSERT (o->output_section->size - o->output_offset | |
6410 | >= _bfd_stringtab_size (debug_strtab)); | |
6411 | pos = o->output_section->filepos + o->output_offset; | |
6412 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
6413 | goto error_return; | |
6414 | if (! _bfd_stringtab_emit (abfd, debug_strtab)) | |
6415 | goto error_return; | |
6416 | } | |
252b5132 | 6417 | |
116c20d2 NC |
6418 | /* Setting bfd_get_symcount to 0 will cause write_object_contents to |
6419 | not try to write out the symbols. */ | |
6420 | bfd_get_symcount (abfd) = 0; | |
252b5132 | 6421 | |
116c20d2 | 6422 | return TRUE; |
252b5132 | 6423 | |
116c20d2 | 6424 | error_return: |
baf9bed3 JB |
6425 | if (flinfo.strtab != NULL) |
6426 | _bfd_stringtab_free (flinfo.strtab); | |
252b5132 | 6427 | |
baf9bed3 | 6428 | if (flinfo.section_info != NULL) |
252b5132 | 6429 | { |
116c20d2 | 6430 | unsigned int i; |
252b5132 | 6431 | |
116c20d2 | 6432 | for (i = 0; i < abfd->section_count; i++) |
252b5132 | 6433 | { |
baf9bed3 JB |
6434 | if (flinfo.section_info[i].relocs != NULL) |
6435 | free (flinfo.section_info[i].relocs); | |
6436 | if (flinfo.section_info[i].rel_hashes != NULL) | |
6437 | free (flinfo.section_info[i].rel_hashes); | |
252b5132 | 6438 | } |
baf9bed3 JB |
6439 | free (flinfo.section_info); |
6440 | } | |
6441 | ||
6442 | if (flinfo.internal_syms != NULL) | |
6443 | free (flinfo.internal_syms); | |
6444 | if (flinfo.sym_indices != NULL) | |
6445 | free (flinfo.sym_indices); | |
6446 | if (flinfo.outsyms != NULL) | |
6447 | free (flinfo.outsyms); | |
6448 | if (flinfo.linenos != NULL) | |
6449 | free (flinfo.linenos); | |
6450 | if (flinfo.contents != NULL) | |
6451 | free (flinfo.contents); | |
6452 | if (flinfo.external_relocs != NULL) | |
6453 | free (flinfo.external_relocs); | |
116c20d2 NC |
6454 | if (external_relocs != NULL) |
6455 | free (external_relocs); | |
6456 | return FALSE; | |
252b5132 | 6457 | } |