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