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