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