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