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