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