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