]>
Commit | Line | Data |
---|---|---|
aadf04f7 SS |
1 | /* POWER/PowerPC XCOFF linker support. |
2 | Copyright 1995 Free Software Foundation, Inc. | |
3 | Written by Ian Lance Taylor <[email protected]>, Cygnus Support. | |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #include "coff/internal.h" | |
26 | #include "libcoff.h" | |
27 | ||
867d923d | 28 | /* This file holds the XCOFF linker code. */ |
aadf04f7 SS |
29 | |
30 | #define STRING_SIZE_SIZE (4) | |
31 | ||
32 | /* Get the XCOFF hash table entries for a BFD. */ | |
33 | #define obj_xcoff_sym_hashes(bfd) \ | |
34 | ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd)) | |
35 | ||
28a0c103 ILT |
36 | /* XCOFF relocation types. These probably belong in a header file |
37 | somewhere. The relocations are described in the function | |
38 | _bfd_ppc_xcoff_relocate_section in this file. */ | |
39 | ||
40 | #define R_POS (0x00) | |
41 | #define R_NEG (0x01) | |
42 | #define R_REL (0x02) | |
43 | #define R_TOC (0x03) | |
44 | #define R_RTB (0x04) | |
45 | #define R_GL (0x05) | |
46 | #define R_TCL (0x06) | |
47 | #define R_BA (0x08) | |
48 | #define R_BR (0x0a) | |
49 | #define R_RL (0x0c) | |
50 | #define R_RLA (0x0d) | |
51 | #define R_REF (0x0f) | |
52 | #define R_TRL (0x12) | |
53 | #define R_TRLA (0x13) | |
54 | #define R_RRTBI (0x14) | |
55 | #define R_RRTBA (0x15) | |
56 | #define R_CAI (0x16) | |
57 | #define R_CREL (0x17) | |
58 | #define R_RBA (0x18) | |
59 | #define R_RBAC (0x19) | |
60 | #define R_RBR (0x1a) | |
61 | #define R_RBRC (0x1b) | |
62 | ||
63 | /* The first word of global linkage code. This must be modified by | |
64 | filling in the correct TOC offset. */ | |
65 | ||
66 | #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */ | |
67 | ||
68 | /* The remaining words of global linkage code. */ | |
69 | ||
70 | static unsigned long xcoff_glink_code[] = | |
71 | { | |
72 | 0x90410014, /* stw r2,20(r1) */ | |
73 | 0x800c0000, /* lwz r0,0(r12) */ | |
74 | 0x804c0004, /* lwz r2,4(r12) */ | |
75 | 0x7c0903a6, /* mtctr r0 */ | |
76 | 0x4e800420, /* bctr */ | |
77 | 0x0, /* start of traceback table */ | |
78 | 0x000c8000, /* traceback table */ | |
79 | 0x0 /* traceback table */ | |
80 | }; | |
81 | ||
82 | #define XCOFF_GLINK_SIZE \ | |
83 | (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4) | |
84 | ||
85 | /* We reuse the SEC_ROM flag as a mark flag for garbage collection. | |
86 | This flag will only be used on input sections. */ | |
87 | ||
88 | #define SEC_MARK (SEC_ROM) | |
89 | ||
90 | /* The ldhdr structure. This appears at the start of the .loader | |
91 | section. */ | |
92 | ||
93 | struct internal_ldhdr | |
94 | { | |
95 | /* The version number: currently always 1. */ | |
96 | unsigned long l_version; | |
97 | /* The number of symbol table entries. */ | |
98 | bfd_size_type l_nsyms; | |
99 | /* The number of relocation table entries. */ | |
100 | bfd_size_type l_nreloc; | |
101 | /* The length of the import file string table. */ | |
102 | bfd_size_type l_istlen; | |
103 | /* The number of import files. */ | |
104 | bfd_size_type l_nimpid; | |
105 | /* The offset from the start of the .loader section to the first | |
106 | entry in the import file table. */ | |
107 | bfd_size_type l_impoff; | |
108 | /* The length of the string table. */ | |
109 | bfd_size_type l_stlen; | |
110 | /* The offset from the start of the .loader section to the first | |
111 | entry in the string table. */ | |
112 | bfd_size_type l_stoff; | |
113 | }; | |
114 | ||
115 | struct external_ldhdr | |
116 | { | |
117 | bfd_byte l_version[4]; | |
118 | bfd_byte l_nsyms[4]; | |
119 | bfd_byte l_nreloc[4]; | |
120 | bfd_byte l_istlen[4]; | |
121 | bfd_byte l_nimpid[4]; | |
122 | bfd_byte l_impoff[4]; | |
123 | bfd_byte l_stlen[4]; | |
124 | bfd_byte l_stoff[4]; | |
125 | }; | |
126 | ||
127 | #define LDHDRSZ (8 * 4) | |
128 | ||
129 | /* The ldsym structure. This is used to represent a symbol in the | |
130 | .loader section. */ | |
131 | ||
132 | struct internal_ldsym | |
133 | { | |
134 | union | |
135 | { | |
136 | /* The symbol name if <= SYMNMLEN characters. */ | |
137 | char _l_name[SYMNMLEN]; | |
138 | struct | |
139 | { | |
140 | /* Zero if the symbol name is more than SYMNMLEN characters. */ | |
141 | long _l_zeroes; | |
142 | /* The offset in the string table if the symbol name is more | |
143 | than SYMNMLEN characters. */ | |
144 | long _l_offset; | |
145 | } _l_l; | |
146 | } _l; | |
147 | /* The symbol value. */ | |
148 | bfd_vma l_value; | |
149 | /* The symbol section number. */ | |
150 | short l_scnum; | |
151 | /* The symbol type and flags. */ | |
152 | char l_smtype; | |
153 | /* The symbol storage class. */ | |
154 | char l_smclas; | |
155 | /* The import file ID. */ | |
156 | bfd_size_type l_ifile; | |
157 | /* Offset to the parameter type check string. */ | |
158 | bfd_size_type l_parm; | |
159 | }; | |
160 | ||
161 | struct external_ldsym | |
162 | { | |
163 | union | |
164 | { | |
165 | bfd_byte _l_name[SYMNMLEN]; | |
166 | struct | |
167 | { | |
168 | bfd_byte _l_zeroes[4]; | |
169 | bfd_byte _l_offset[4]; | |
170 | } _l_l; | |
171 | } _l; | |
172 | bfd_byte l_value[4]; | |
173 | bfd_byte l_scnum[2]; | |
174 | bfd_byte l_smtype[1]; | |
175 | bfd_byte l_smclas[1]; | |
176 | bfd_byte l_ifile[4]; | |
177 | bfd_byte l_parm[4]; | |
178 | }; | |
179 | ||
180 | #define LDSYMSZ (8 + 3 * 4 + 2 + 2) | |
181 | ||
182 | /* These flags are for the l_smtype field (the lower three bits are an | |
183 | XTY_* value). */ | |
184 | ||
185 | /* Imported symbol. */ | |
186 | #define L_IMPORT (0x40) | |
187 | /* Entry point. */ | |
188 | #define L_ENTRY (0x20) | |
189 | /* Exported symbol. */ | |
190 | #define L_EXPORT (0x10) | |
191 | ||
192 | /* The ldrel structure. This is used to represent a reloc in the | |
193 | .loader section. */ | |
194 | ||
195 | struct internal_ldrel | |
196 | { | |
197 | /* The reloc address. */ | |
198 | bfd_vma l_vaddr; | |
199 | /* The symbol table index in the .loader section symbol table. */ | |
200 | bfd_size_type l_symndx; | |
201 | /* The relocation type and size. */ | |
202 | short l_rtype; | |
203 | /* The section number this relocation applies to. */ | |
204 | short l_rsecnm; | |
205 | }; | |
206 | ||
207 | struct external_ldrel | |
208 | { | |
209 | bfd_byte l_vaddr[4]; | |
210 | bfd_byte l_symndx[4]; | |
211 | bfd_byte l_rtype[2]; | |
212 | bfd_byte l_rsecnm[2]; | |
213 | }; | |
214 | ||
215 | #define LDRELSZ (2 * 4 + 2 * 2) | |
216 | ||
217 | /* The list of import files. */ | |
218 | ||
219 | struct xcoff_import_file | |
220 | { | |
221 | /* The next entry in the list. */ | |
222 | struct xcoff_import_file *next; | |
223 | /* The path. */ | |
224 | const char *path; | |
225 | /* The file name. */ | |
226 | const char *file; | |
227 | /* The member name. */ | |
228 | const char *member; | |
229 | }; | |
230 | ||
aadf04f7 SS |
231 | /* An entry in the XCOFF linker hash table. */ |
232 | ||
233 | struct xcoff_link_hash_entry | |
234 | { | |
235 | struct bfd_link_hash_entry root; | |
236 | ||
aadf04f7 SS |
237 | /* Symbol index in output file. Set to -1 initially. Set to -2 if |
238 | there is a reloc against this symbol. */ | |
239 | long indx; | |
240 | ||
28a0c103 ILT |
241 | /* If we have created a TOC entry for this symbol, this is the .tc |
242 | section which holds it. */ | |
243 | asection *toc_section; | |
244 | ||
245 | /* If we have created a TOC entry, this is the offset in | |
246 | toc_section. */ | |
247 | bfd_vma toc_offset; | |
248 | ||
249 | /* If this symbol is a function entry point which is called, this | |
250 | field holds a pointer to the function descriptor. */ | |
251 | struct xcoff_link_hash_entry *descriptor; | |
252 | ||
253 | /* The .loader symbol table entry, if there is one. */ | |
254 | struct internal_ldsym *ldsym; | |
255 | ||
256 | /* The .loader symbol table index. */ | |
257 | long ldindx; | |
258 | ||
259 | /* Some linker flags. */ | |
260 | unsigned short flags; | |
261 | /* Symbol is referenced by a regular object. */ | |
262 | #define XCOFF_REF_REGULAR (01) | |
263 | /* Symbol is defined by a regular object. */ | |
264 | #define XCOFF_DEF_REGULAR (02) | |
265 | /* Symbol is referenced by a dynamic object. */ | |
266 | #define XCOFF_REF_DYNAMIC (04) | |
267 | /* Symbol is used in a reloc being copied into the .loader section. */ | |
268 | #define XCOFF_LDREL (010) | |
269 | /* Symbol is the entry point. */ | |
270 | #define XCOFF_ENTRY (020) | |
271 | /* Symbol is called; this is, it appears in a R_BR reloc. */ | |
272 | #define XCOFF_CALLED (040) | |
273 | /* Symbol needs the TOC entry filled in. */ | |
274 | #define XCOFF_SET_TOC (0100) | |
275 | /* Symbol is explicitly imported. */ | |
276 | #define XCOFF_IMPORT (0200) | |
277 | /* Symbol is explicitly exported. */ | |
278 | #define XCOFF_EXPORT (0400) | |
279 | /* Symbol has been processed by xcoff_build_ldsyms. */ | |
280 | #define XCOFF_BUILT_LDSYM (01000) | |
281 | /* Symbol is mentioned by a section which was not garbage collected. */ | |
282 | #define XCOFF_MARK (02000) | |
283 | ||
284 | /* The storage mapping class. */ | |
285 | unsigned char smclas; | |
aadf04f7 SS |
286 | }; |
287 | ||
288 | /* The XCOFF linker hash table. */ | |
289 | ||
290 | struct xcoff_link_hash_table | |
291 | { | |
292 | struct bfd_link_hash_table root; | |
293 | ||
294 | /* The .debug string hash table. We need to compute this while | |
295 | reading the input files, so that we know how large the .debug | |
296 | section will be before we assign section positions. */ | |
297 | struct bfd_strtab_hash *debug_strtab; | |
298 | ||
299 | /* The .debug section we will use for the final output. */ | |
300 | asection *debug_section; | |
28a0c103 ILT |
301 | |
302 | /* The .loader section we will use for the final output. */ | |
303 | asection *loader_section; | |
304 | ||
305 | /* A count of non TOC relative relocs which will need to be | |
306 | allocated in the .loader section. */ | |
307 | size_t ldrel_count; | |
308 | ||
309 | /* The .loader section header. */ | |
310 | struct internal_ldhdr ldhdr; | |
311 | ||
312 | /* The .gl section we use to hold global linkage code. */ | |
313 | asection *linkage_section; | |
314 | ||
315 | /* The .tc section we use to hold toc entries we build for global | |
316 | linkage code. */ | |
317 | asection *toc_section; | |
318 | ||
319 | /* The list of import files. */ | |
320 | struct xcoff_import_file *imports; | |
321 | ||
322 | /* Required alignment of sections within the output file. */ | |
323 | unsigned long file_align; | |
324 | ||
325 | /* Whether the .text section must be read-only. */ | |
326 | boolean textro; | |
327 | ||
328 | /* Whether garbage collection was done. */ | |
329 | boolean gc; | |
aadf04f7 SS |
330 | }; |
331 | ||
28a0c103 ILT |
332 | /* Information we keep for each section in the output file during the |
333 | final link phase. */ | |
aadf04f7 SS |
334 | |
335 | struct xcoff_link_section_info | |
336 | { | |
337 | /* The relocs to be output. */ | |
338 | struct internal_reloc *relocs; | |
339 | /* For each reloc against a global symbol whose index was not known | |
340 | when the reloc was handled, the global hash table entry. */ | |
341 | struct xcoff_link_hash_entry **rel_hashes; | |
342 | }; | |
343 | ||
344 | /* Information that we pass around while doing the final link step. */ | |
345 | ||
346 | struct xcoff_final_link_info | |
347 | { | |
348 | /* General link information. */ | |
349 | struct bfd_link_info *info; | |
350 | /* Output BFD. */ | |
351 | bfd *output_bfd; | |
352 | /* Hash table for long symbol names. */ | |
353 | struct bfd_strtab_hash *strtab; | |
354 | /* Array of information kept for each output section, indexed by the | |
355 | target_index field. */ | |
356 | struct xcoff_link_section_info *section_info; | |
357 | /* Symbol index of last C_FILE symbol (-1 if none). */ | |
358 | long last_file_index; | |
359 | /* Contents of last C_FILE symbol. */ | |
360 | struct internal_syment last_file; | |
361 | /* Symbol index of TOC symbol. */ | |
362 | long toc_symindx; | |
28a0c103 ILT |
363 | /* Start of .loader symbols. */ |
364 | struct external_ldsym *ldsym; | |
365 | /* Next .loader reloc to swap out. */ | |
366 | struct external_ldrel *ldrel; | |
aadf04f7 SS |
367 | /* Buffer large enough to hold swapped symbols of any input file. */ |
368 | struct internal_syment *internal_syms; | |
369 | /* Buffer large enough to hold output indices of symbols of any | |
370 | input file. */ | |
371 | long *sym_indices; | |
372 | /* Buffer large enough to hold output symbols for any input file. */ | |
373 | bfd_byte *outsyms; | |
374 | /* Buffer large enough to hold external line numbers for any input | |
375 | section. */ | |
376 | bfd_byte *linenos; | |
377 | /* Buffer large enough to hold any input section. */ | |
378 | bfd_byte *contents; | |
379 | /* Buffer large enough to hold external relocs of any input section. */ | |
380 | bfd_byte *external_relocs; | |
381 | }; | |
382 | ||
28a0c103 ILT |
383 | static void xcoff_swap_ldhdr_out |
384 | PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *)); | |
385 | static void xcoff_swap_ldsym_out | |
386 | PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *)); | |
387 | static void xcoff_swap_ldrel_out | |
388 | PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *)); | |
aadf04f7 SS |
389 | static struct bfd_hash_entry *xcoff_link_hash_newfunc |
390 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
28a0c103 ILT |
391 | static struct internal_reloc *xcoff_read_internal_relocs |
392 | PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean, | |
393 | struct internal_reloc *)); | |
aadf04f7 SS |
394 | static boolean xcoff_link_add_object_symbols |
395 | PARAMS ((bfd *, struct bfd_link_info *)); | |
396 | static boolean xcoff_link_check_archive_element | |
397 | PARAMS ((bfd *, struct bfd_link_info *, boolean *)); | |
398 | static boolean xcoff_link_check_ar_symbols | |
399 | PARAMS ((bfd *, struct bfd_link_info *, boolean *)); | |
400 | static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *)); | |
28a0c103 ILT |
401 | static boolean xcoff_link_add_dynamic_symbols |
402 | PARAMS ((bfd *, struct bfd_link_info *)); | |
403 | static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *)); | |
404 | static void xcoff_sweep PARAMS ((struct bfd_link_info *)); | |
405 | static boolean xcoff_build_ldsyms | |
406 | PARAMS ((struct xcoff_link_hash_entry *, PTR)); | |
aadf04f7 SS |
407 | static boolean xcoff_link_input_bfd |
408 | PARAMS ((struct xcoff_final_link_info *, bfd *)); | |
409 | static boolean xcoff_write_global_symbol | |
410 | PARAMS ((struct xcoff_link_hash_entry *, PTR)); | |
411 | static boolean xcoff_reloc_link_order | |
412 | PARAMS ((bfd *, struct xcoff_final_link_info *, asection *, | |
413 | struct bfd_link_order *)); | |
414 | static int xcoff_sort_relocs PARAMS ((const PTR, const PTR)); | |
28a0c103 ILT |
415 | \f |
416 | /* Routines to swap information in the XCOFF .loader section. We only | |
417 | need to swap this information out, not in. I believe that only the | |
418 | loader needs to swap this information in. If we ever need to write | |
419 | an XCOFF loader, this stuff will need to be moved to another file | |
420 | shared by the linker (which XCOFF calls the ``binder'') and the | |
421 | loader. */ | |
422 | ||
423 | /* Swap out the ldhdr structure. */ | |
424 | ||
425 | static void | |
426 | xcoff_swap_ldhdr_out (abfd, src, dst) | |
427 | bfd *abfd; | |
428 | const struct internal_ldhdr *src; | |
429 | struct external_ldhdr *dst; | |
430 | { | |
431 | bfd_put_32 (abfd, src->l_version, dst->l_version); | |
432 | bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms); | |
433 | bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc); | |
434 | bfd_put_32 (abfd, src->l_istlen, dst->l_istlen); | |
435 | bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid); | |
436 | bfd_put_32 (abfd, src->l_impoff, dst->l_impoff); | |
437 | bfd_put_32 (abfd, src->l_stlen, dst->l_stlen); | |
438 | bfd_put_32 (abfd, src->l_stoff, dst->l_stoff); | |
439 | } | |
440 | ||
441 | /* Swap out the ldsym structure. */ | |
442 | ||
443 | static void | |
444 | xcoff_swap_ldsym_out (abfd, src, dst) | |
445 | bfd *abfd; | |
446 | const struct internal_ldsym *src; | |
447 | struct external_ldsym *dst; | |
448 | { | |
449 | if (src->_l._l_l._l_zeroes != 0) | |
450 | memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN); | |
451 | else | |
452 | { | |
453 | bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes); | |
454 | bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset); | |
455 | } | |
456 | bfd_put_32 (abfd, src->l_value, dst->l_value); | |
457 | bfd_put_16 (abfd, src->l_scnum, dst->l_scnum); | |
458 | bfd_put_8 (abfd, src->l_smtype, dst->l_smtype); | |
459 | bfd_put_8 (abfd, src->l_smclas, dst->l_smclas); | |
460 | bfd_put_32 (abfd, src->l_ifile, dst->l_ifile); | |
461 | bfd_put_32 (abfd, src->l_parm, dst->l_parm); | |
462 | } | |
aadf04f7 | 463 | |
28a0c103 ILT |
464 | /* Swap out the ldrel structure. */ |
465 | ||
466 | static void | |
467 | xcoff_swap_ldrel_out (abfd, src, dst) | |
468 | bfd *abfd; | |
469 | const struct internal_ldrel *src; | |
470 | struct external_ldrel *dst; | |
471 | { | |
472 | bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr); | |
473 | bfd_put_32 (abfd, src->l_symndx, dst->l_symndx); | |
474 | bfd_put_16 (abfd, src->l_rtype, dst->l_rtype); | |
475 | bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm); | |
476 | } | |
477 | \f | |
aadf04f7 SS |
478 | /* Routine to create an entry in an XCOFF link hash table. */ |
479 | ||
480 | static struct bfd_hash_entry * | |
481 | xcoff_link_hash_newfunc (entry, table, string) | |
482 | struct bfd_hash_entry *entry; | |
483 | struct bfd_hash_table *table; | |
484 | const char *string; | |
485 | { | |
486 | struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry; | |
487 | ||
488 | /* Allocate the structure if it has not already been allocated by a | |
489 | subclass. */ | |
490 | if (ret == (struct xcoff_link_hash_entry *) NULL) | |
491 | ret = ((struct xcoff_link_hash_entry *) | |
492 | bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry))); | |
493 | if (ret == (struct xcoff_link_hash_entry *) NULL) | |
494 | { | |
495 | bfd_set_error (bfd_error_no_memory); | |
496 | return (struct bfd_hash_entry *) ret; | |
497 | } | |
498 | ||
499 | /* Call the allocation method of the superclass. */ | |
500 | ret = ((struct xcoff_link_hash_entry *) | |
501 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
502 | table, string)); | |
503 | if (ret != NULL) | |
504 | { | |
505 | /* Set local fields. */ | |
aadf04f7 | 506 | ret->indx = -1; |
28a0c103 ILT |
507 | ret->toc_section = NULL; |
508 | ret->toc_offset = 0; | |
509 | ret->descriptor = NULL; | |
510 | ret->ldsym = NULL; | |
511 | ret->ldindx = -1; | |
512 | ret->flags = 0; | |
513 | ret->smclas = XMC_UA; | |
aadf04f7 SS |
514 | } |
515 | ||
516 | return (struct bfd_hash_entry *) ret; | |
517 | } | |
518 | ||
519 | /* Create a XCOFF link hash table. */ | |
520 | ||
521 | struct bfd_link_hash_table * | |
522 | _bfd_xcoff_bfd_link_hash_table_create (abfd) | |
523 | bfd *abfd; | |
524 | { | |
525 | struct xcoff_link_hash_table *ret; | |
526 | ||
527 | ret = ((struct xcoff_link_hash_table *) | |
528 | bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table))); | |
529 | if (ret == (struct xcoff_link_hash_table *) NULL) | |
530 | { | |
531 | bfd_set_error (bfd_error_no_memory); | |
532 | return (struct bfd_link_hash_table *) NULL; | |
533 | } | |
534 | if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc)) | |
535 | { | |
536 | bfd_release (abfd, ret); | |
537 | return (struct bfd_link_hash_table *) NULL; | |
538 | } | |
539 | ||
540 | ret->debug_strtab = _bfd_xcoff_stringtab_init (); | |
541 | ret->debug_section = NULL; | |
28a0c103 ILT |
542 | ret->loader_section = NULL; |
543 | ret->ldrel_count = 0; | |
544 | memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr)); | |
545 | ret->linkage_section = NULL; | |
546 | ret->toc_section = NULL; | |
547 | ret->imports = NULL; | |
548 | ret->file_align = 0; | |
549 | ret->textro = false; | |
550 | ret->gc = false; | |
aadf04f7 SS |
551 | |
552 | return &ret->root; | |
553 | } | |
554 | ||
555 | /* Look up an entry in an XCOFF link hash table. */ | |
556 | ||
557 | #define xcoff_link_hash_lookup(table, string, create, copy, follow) \ | |
558 | ((struct xcoff_link_hash_entry *) \ | |
559 | bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\ | |
560 | (follow))) | |
561 | ||
562 | /* Traverse an XCOFF link hash table. */ | |
563 | ||
564 | #define xcoff_link_hash_traverse(table, func, info) \ | |
565 | (bfd_link_hash_traverse \ | |
566 | (&(table)->root, \ | |
567 | (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \ | |
568 | (info))) | |
569 | ||
570 | /* Get the XCOFF link hash table from the info structure. This is | |
571 | just a cast. */ | |
572 | ||
573 | #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash)) | |
28a0c103 ILT |
574 | \f |
575 | /* Read internal relocs for an XCOFF csect. This is a wrapper around | |
576 | _bfd_coff_read_internal_relocs which tries to take advantage of any | |
577 | relocs which may have been cached for the enclosing section. */ | |
578 | ||
579 | static struct internal_reloc * | |
580 | xcoff_read_internal_relocs (abfd, sec, cache, external_relocs, | |
581 | require_internal, internal_relocs) | |
582 | bfd *abfd; | |
583 | asection *sec; | |
584 | boolean cache; | |
585 | bfd_byte *external_relocs; | |
586 | boolean require_internal; | |
587 | struct internal_reloc *internal_relocs; | |
588 | { | |
589 | if (coff_section_data (abfd, sec) != NULL | |
590 | && coff_section_data (abfd, sec)->relocs == NULL | |
591 | && xcoff_section_data (abfd, sec) != NULL) | |
592 | { | |
593 | asection *enclosing; | |
594 | ||
595 | enclosing = xcoff_section_data (abfd, sec)->enclosing; | |
596 | ||
597 | if (enclosing != NULL | |
598 | && (coff_section_data (abfd, enclosing) == NULL | |
599 | || coff_section_data (abfd, enclosing)->relocs == NULL) | |
600 | && cache) | |
601 | { | |
602 | if (_bfd_coff_read_internal_relocs (abfd, enclosing, true, | |
603 | external_relocs, false, | |
604 | (struct internal_reloc *) NULL) | |
605 | == NULL) | |
606 | return NULL; | |
607 | } | |
aadf04f7 | 608 | |
28a0c103 ILT |
609 | if (enclosing != NULL |
610 | && coff_section_data (abfd, enclosing) != NULL | |
611 | && coff_section_data (abfd, enclosing)->relocs != NULL) | |
612 | { | |
613 | size_t off; | |
614 | ||
615 | off = ((sec->rel_filepos - enclosing->rel_filepos) | |
616 | / bfd_coff_relsz (abfd)); | |
617 | if (! require_internal) | |
618 | return coff_section_data (abfd, enclosing)->relocs + off; | |
619 | memcpy (internal_relocs, | |
620 | coff_section_data (abfd, enclosing)->relocs + off, | |
621 | sec->reloc_count * sizeof (struct internal_reloc)); | |
622 | return internal_relocs; | |
623 | } | |
624 | } | |
625 | ||
626 | return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs, | |
627 | require_internal, internal_relocs); | |
628 | } | |
629 | \f | |
aadf04f7 SS |
630 | /* Given an XCOFF BFD, add symbols to the global hash table as |
631 | appropriate. */ | |
632 | ||
633 | boolean | |
634 | _bfd_xcoff_bfd_link_add_symbols (abfd, info) | |
635 | bfd *abfd; | |
636 | struct bfd_link_info *info; | |
637 | { | |
638 | switch (bfd_get_format (abfd)) | |
639 | { | |
640 | case bfd_object: | |
641 | return xcoff_link_add_object_symbols (abfd, info); | |
642 | case bfd_archive: | |
643 | return (_bfd_generic_link_add_archive_symbols | |
644 | (abfd, info, xcoff_link_check_archive_element)); | |
645 | default: | |
646 | bfd_set_error (bfd_error_wrong_format); | |
647 | return false; | |
648 | } | |
649 | } | |
650 | ||
651 | /* Add symbols from an XCOFF object file. */ | |
652 | ||
653 | static boolean | |
654 | xcoff_link_add_object_symbols (abfd, info) | |
655 | bfd *abfd; | |
656 | struct bfd_link_info *info; | |
657 | { | |
658 | if (! _bfd_coff_get_external_symbols (abfd)) | |
659 | return false; | |
660 | if (! xcoff_link_add_symbols (abfd, info)) | |
661 | return false; | |
662 | if (! info->keep_memory) | |
663 | { | |
664 | if (! _bfd_coff_free_symbols (abfd)) | |
665 | return false; | |
666 | } | |
667 | return true; | |
668 | } | |
669 | ||
670 | /* Check a single archive element to see if we need to include it in | |
671 | the link. *PNEEDED is set according to whether this element is | |
672 | needed in the link or not. This is called via | |
673 | _bfd_generic_link_add_archive_symbols. */ | |
674 | ||
675 | static boolean | |
676 | xcoff_link_check_archive_element (abfd, info, pneeded) | |
677 | bfd *abfd; | |
678 | struct bfd_link_info *info; | |
679 | boolean *pneeded; | |
680 | { | |
681 | if (! _bfd_coff_get_external_symbols (abfd)) | |
682 | return false; | |
683 | ||
684 | if (! xcoff_link_check_ar_symbols (abfd, info, pneeded)) | |
685 | return false; | |
686 | ||
687 | if (*pneeded) | |
688 | { | |
689 | if (! xcoff_link_add_symbols (abfd, info)) | |
690 | return false; | |
691 | } | |
692 | ||
693 | if (! info->keep_memory || ! *pneeded) | |
694 | { | |
695 | if (! _bfd_coff_free_symbols (abfd)) | |
696 | return false; | |
697 | } | |
698 | ||
699 | return true; | |
700 | } | |
701 | ||
702 | /* Look through the symbols to see if this object file should be | |
703 | included in the link. */ | |
704 | ||
705 | static boolean | |
706 | xcoff_link_check_ar_symbols (abfd, info, pneeded) | |
707 | bfd *abfd; | |
708 | struct bfd_link_info *info; | |
709 | boolean *pneeded; | |
710 | { | |
711 | bfd_size_type symesz; | |
712 | bfd_byte *esym; | |
713 | bfd_byte *esym_end; | |
714 | ||
715 | *pneeded = false; | |
716 | ||
717 | symesz = bfd_coff_symesz (abfd); | |
718 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
719 | esym_end = esym + obj_raw_syment_count (abfd) * symesz; | |
720 | while (esym < esym_end) | |
721 | { | |
722 | struct internal_syment sym; | |
723 | ||
724 | bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym); | |
725 | ||
726 | if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF) | |
727 | { | |
728 | const char *name; | |
729 | char buf[SYMNMLEN + 1]; | |
730 | struct bfd_link_hash_entry *h; | |
731 | ||
732 | /* This symbol is externally visible, and is defined by this | |
733 | object file. */ | |
734 | ||
735 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
736 | if (name == NULL) | |
737 | return false; | |
738 | h = bfd_link_hash_lookup (info->hash, name, false, false, true); | |
739 | ||
740 | /* We are only interested in symbols that are currently | |
741 | undefined. If a symbol is currently known to be common, | |
742 | XCOFF linkers do not bring in an object file which | |
28a0c103 ILT |
743 | defines it. We also don't bring in symbols to satisfy |
744 | undefined references in shared objects. */ | |
aadf04f7 SS |
745 | if (h != (struct bfd_link_hash_entry *) NULL |
746 | && h->type == bfd_link_hash_undefined) | |
747 | { | |
748 | if (! (*info->callbacks->add_archive_element) (info, abfd, name)) | |
749 | return false; | |
750 | *pneeded = true; | |
751 | return true; | |
752 | } | |
753 | } | |
754 | ||
755 | esym += (sym.n_numaux + 1) * symesz; | |
756 | } | |
757 | ||
758 | /* We do not need this object file. */ | |
759 | return true; | |
760 | } | |
761 | ||
762 | /* Add all the symbols from an object file to the hash table. | |
763 | ||
764 | XCOFF is a weird format. A normal XCOFF .o files will have three | |
765 | COFF sections--.text, .data, and .bss--but each COFF section will | |
766 | contain many csects. These csects are described in the symbol | |
767 | table. From the linker's point of view, each csect must be | |
768 | considered a section in its own right. For example, a TOC entry is | |
769 | handled as a small XMC_TC csect. The linker must be able to merge | |
770 | different TOC entries together, which means that it must be able to | |
771 | extract the XMC_TC csects from the .data section of the input .o | |
772 | file. | |
773 | ||
774 | From the point of view of our linker, this is, of course, a hideous | |
775 | nightmare. We cope by actually creating sections for each csect, | |
776 | and discarding the original sections. We then have to handle the | |
777 | relocation entries carefully, since the only way to tell which | |
778 | csect they belong to is to examine the address. */ | |
779 | ||
780 | static boolean | |
781 | xcoff_link_add_symbols (abfd, info) | |
782 | bfd *abfd; | |
783 | struct bfd_link_info *info; | |
784 | { | |
28a0c103 ILT |
785 | unsigned int n_tmask; |
786 | unsigned int n_btshft; | |
aadf04f7 SS |
787 | boolean default_copy; |
788 | bfd_size_type symcount; | |
789 | struct xcoff_link_hash_entry **sym_hash; | |
790 | asection **csect_cache; | |
28a0c103 | 791 | bfd_size_type linesz; |
aadf04f7 | 792 | asection *sub; |
28a0c103 | 793 | boolean keep_syms; |
aadf04f7 SS |
794 | asection *csect; |
795 | unsigned int csect_index; | |
796 | asection *first_csect; | |
aadf04f7 SS |
797 | bfd_size_type symesz; |
798 | bfd_byte *esym; | |
799 | bfd_byte *esym_end; | |
800 | struct reloc_info_struct | |
801 | { | |
802 | struct internal_reloc *relocs; | |
28a0c103 ILT |
803 | asection **csects; |
804 | bfd_byte *linenos; | |
aadf04f7 SS |
805 | } *reloc_info = NULL; |
806 | ||
28a0c103 ILT |
807 | if ((abfd->flags & DYNAMIC) != 0 |
808 | && ! info->static_link) | |
809 | return xcoff_link_add_dynamic_symbols (abfd, info); | |
810 | ||
811 | n_tmask = coff_data (abfd)->local_n_tmask; | |
812 | n_btshft = coff_data (abfd)->local_n_btshft; | |
813 | ||
814 | /* Define macros so that ISFCN, et. al., macros work correctly. */ | |
815 | #define N_TMASK n_tmask | |
816 | #define N_BTSHFT n_btshft | |
817 | ||
818 | /* We need to build a .loader section, so we do it here. This won't | |
819 | work if we're producing an XCOFF output file with no non dynamic | |
820 | XCOFF input files. FIXME. */ | |
821 | if (xcoff_hash_table (info)->loader_section == NULL) | |
822 | { | |
823 | asection *lsec; | |
824 | ||
825 | lsec = bfd_make_section_anyway (abfd, ".loader"); | |
826 | if (lsec == NULL) | |
827 | goto error_return; | |
828 | xcoff_hash_table (info)->loader_section = lsec; | |
829 | lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
830 | } | |
831 | /* Likewise for the linkage section. */ | |
832 | if (xcoff_hash_table (info)->linkage_section == NULL) | |
833 | { | |
834 | asection *lsec; | |
835 | ||
836 | lsec = bfd_make_section_anyway (abfd, ".gl"); | |
837 | if (lsec == NULL) | |
838 | goto error_return; | |
839 | xcoff_hash_table (info)->linkage_section = lsec; | |
840 | lsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
841 | } | |
842 | /* Likewise for the TOC section. */ | |
843 | if (xcoff_hash_table (info)->toc_section == NULL) | |
844 | { | |
845 | asection *tsec; | |
846 | ||
847 | tsec = bfd_make_section_anyway (abfd, ".tc"); | |
848 | if (tsec == NULL) | |
849 | goto error_return; | |
850 | xcoff_hash_table (info)->toc_section = tsec; | |
851 | tsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
852 | } | |
853 | /* Likewise for the .debug section. */ | |
854 | if (xcoff_hash_table (info)->debug_section == NULL) | |
855 | { | |
856 | asection *dsec; | |
857 | ||
858 | dsec = bfd_make_section_anyway (abfd, ".debug"); | |
859 | if (dsec == NULL) | |
860 | goto error_return; | |
861 | xcoff_hash_table (info)->debug_section = dsec; | |
862 | dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
863 | } | |
864 | ||
aadf04f7 SS |
865 | if (info->keep_memory) |
866 | default_copy = false; | |
867 | else | |
868 | default_copy = true; | |
869 | ||
870 | symcount = obj_raw_syment_count (abfd); | |
871 | ||
872 | /* We keep a list of the linker hash table entries that correspond | |
873 | to each external symbol. */ | |
874 | sym_hash = ((struct xcoff_link_hash_entry **) | |
875 | bfd_alloc (abfd, | |
876 | (symcount | |
877 | * sizeof (struct xcoff_link_hash_entry *)))); | |
878 | if (sym_hash == NULL && symcount != 0) | |
879 | { | |
880 | bfd_set_error (bfd_error_no_memory); | |
881 | goto error_return; | |
882 | } | |
883 | coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash; | |
884 | memset (sym_hash, 0, | |
885 | (size_t) symcount * sizeof (struct xcoff_link_hash_entry *)); | |
886 | ||
887 | /* Because of the weird stuff we are doing with XCOFF csects, we can | |
888 | not easily determine which section a symbol is in, so we store | |
889 | the information in the tdata for the input file. */ | |
890 | csect_cache = ((asection **) | |
891 | bfd_alloc (abfd, symcount * sizeof (asection *))); | |
892 | if (csect_cache == NULL && symcount != 0) | |
893 | { | |
894 | bfd_set_error (bfd_error_no_memory); | |
895 | goto error_return; | |
896 | } | |
897 | xcoff_data (abfd)->csects = csect_cache; | |
898 | memset (csect_cache, 0, (size_t) symcount * sizeof (asection *)); | |
899 | ||
aadf04f7 SS |
900 | /* While splitting sections into csects, we need to assign the |
901 | relocs correctly. The relocs and the csects must both be in | |
902 | order by VMA within a given section, so we handle this by | |
903 | scanning along the relocs as we process the csects. We index | |
904 | into reloc_info using the section target_index. */ | |
905 | reloc_info = ((struct reloc_info_struct *) | |
906 | malloc ((abfd->section_count + 1) | |
907 | * sizeof (struct reloc_info_struct))); | |
908 | if (reloc_info == NULL) | |
909 | { | |
910 | bfd_set_error (bfd_error_no_memory); | |
911 | goto error_return; | |
912 | } | |
913 | memset ((PTR) reloc_info, 0, | |
914 | (abfd->section_count + 1) * sizeof (struct reloc_info_struct)); | |
915 | ||
28a0c103 ILT |
916 | /* Read in the relocs and line numbers for each section. */ |
917 | linesz = bfd_coff_linesz (abfd); | |
aadf04f7 SS |
918 | for (sub = abfd->sections; sub != NULL; sub = sub->next) |
919 | { | |
920 | if ((sub->flags & SEC_RELOC) != 0) | |
921 | { | |
922 | reloc_info[sub->target_index].relocs = | |
28a0c103 ILT |
923 | xcoff_read_internal_relocs (abfd, sub, true, (bfd_byte *) NULL, |
924 | false, (struct internal_reloc *) NULL); | |
925 | reloc_info[sub->target_index].csects = | |
926 | (asection **) malloc (sub->reloc_count * sizeof (asection *)); | |
927 | if (reloc_info[sub->target_index].csects == NULL) | |
928 | { | |
929 | bfd_set_error (bfd_error_no_memory); | |
930 | goto error_return; | |
931 | } | |
932 | memset (reloc_info[sub->target_index].csects, 0, | |
933 | sub->reloc_count * sizeof (asection *)); | |
934 | } | |
935 | ||
936 | if ((info->strip == strip_none || info->strip == strip_some) | |
937 | && sub->lineno_count > 0) | |
938 | { | |
939 | bfd_byte *linenos; | |
940 | ||
941 | linenos = (bfd_byte *) malloc (sub->lineno_count * linesz); | |
942 | if (linenos == NULL) | |
aadf04f7 SS |
943 | { |
944 | bfd_set_error (bfd_error_no_memory); | |
945 | goto error_return; | |
946 | } | |
28a0c103 ILT |
947 | reloc_info[sub->target_index].linenos = linenos; |
948 | if (bfd_seek (abfd, sub->line_filepos, SEEK_SET) != 0 | |
949 | || (bfd_read (linenos, linesz, sub->lineno_count, abfd) | |
950 | != linesz * sub->lineno_count)) | |
951 | goto error_return; | |
aadf04f7 SS |
952 | } |
953 | } | |
954 | ||
28a0c103 ILT |
955 | /* Don't let the linker relocation routines discard the symbols. */ |
956 | keep_syms = obj_coff_keep_syms (abfd); | |
957 | obj_coff_keep_syms (abfd) = true; | |
958 | ||
aadf04f7 SS |
959 | csect = NULL; |
960 | csect_index = 0; | |
961 | first_csect = NULL; | |
aadf04f7 SS |
962 | |
963 | symesz = bfd_coff_symesz (abfd); | |
964 | BFD_ASSERT (symesz == bfd_coff_auxesz (abfd)); | |
965 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
966 | esym_end = esym + symcount * symesz; | |
967 | while (esym < esym_end) | |
968 | { | |
969 | struct internal_syment sym; | |
970 | union internal_auxent aux; | |
28a0c103 | 971 | const char *name; |
aadf04f7 SS |
972 | char buf[SYMNMLEN + 1]; |
973 | int smtyp; | |
974 | flagword flags; | |
975 | asection *section; | |
976 | bfd_vma value; | |
28a0c103 | 977 | struct xcoff_link_hash_entry *set_toc; |
aadf04f7 SS |
978 | |
979 | bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym); | |
980 | ||
aadf04f7 SS |
981 | /* In this pass we are only interested in symbols with csect |
982 | information. */ | |
983 | if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT) | |
984 | { | |
28a0c103 ILT |
985 | if (sym.n_sclass == C_FILE && csect != NULL) |
986 | { | |
987 | xcoff_section_data (abfd, csect)->last_symndx = | |
988 | ((esym | |
989 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
990 | / symesz); | |
991 | csect = NULL; | |
992 | } | |
993 | ||
aadf04f7 SS |
994 | if (csect != NULL) |
995 | *csect_cache = csect; | |
28a0c103 | 996 | else if (first_csect == NULL || sym.n_sclass == C_FILE) |
aadf04f7 SS |
997 | *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum); |
998 | else | |
999 | *csect_cache = NULL; | |
1000 | esym += (sym.n_numaux + 1) * symesz; | |
1001 | sym_hash += sym.n_numaux + 1; | |
1002 | csect_cache += sym.n_numaux + 1; | |
aadf04f7 SS |
1003 | continue; |
1004 | } | |
1005 | ||
1006 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
1007 | if (name == NULL) | |
1008 | goto error_return; | |
1009 | ||
28a0c103 ILT |
1010 | /* If this symbol has line number information attached to it, |
1011 | and we're not stripping it, count the number of entries and | |
1012 | add them to the count for this csect. In the final link pass | |
1013 | we are going to attach line number information by symbol, | |
1014 | rather than by section, in order to more easily handle | |
1015 | garbage collection. */ | |
1016 | if ((info->strip == strip_none || info->strip == strip_some) | |
1017 | && sym.n_numaux > 1 | |
1018 | && csect != NULL | |
1019 | && ISFCN (sym.n_type)) | |
1020 | { | |
1021 | union internal_auxent auxlin; | |
1022 | ||
1023 | bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz), | |
1024 | sym.n_type, sym.n_sclass, | |
1025 | 0, sym.n_numaux, (PTR) &auxlin); | |
1026 | if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) | |
1027 | { | |
1028 | asection *enclosing; | |
1029 | bfd_size_type linoff; | |
1030 | ||
1031 | enclosing = xcoff_section_data (abfd, csect)->enclosing; | |
b73322d9 ILT |
1032 | if (enclosing == NULL) |
1033 | { | |
1034 | (*_bfd_error_handler) | |
1035 | ("%s: `%s' has line numbers but no enclosing section", | |
1036 | bfd_get_filename (abfd), name); | |
1037 | bfd_set_error (bfd_error_bad_value); | |
1038 | goto error_return; | |
1039 | } | |
28a0c103 ILT |
1040 | linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr |
1041 | - enclosing->line_filepos); | |
1042 | if (linoff < enclosing->lineno_count * linesz) | |
1043 | { | |
1044 | struct internal_lineno lin; | |
1045 | bfd_byte *linpstart; | |
1046 | ||
1047 | linpstart = (reloc_info[enclosing->target_index].linenos | |
1048 | + linoff); | |
1049 | bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin); | |
1050 | if (lin.l_lnno == 0 | |
1051 | && ((bfd_size_type) lin.l_addr.l_symndx | |
1052 | == ((esym | |
1053 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1054 | / symesz))) | |
1055 | { | |
1056 | bfd_byte *linpend, *linp; | |
1057 | ||
1058 | linpend = (reloc_info[enclosing->target_index].linenos | |
1059 | + enclosing->lineno_count * linesz); | |
1060 | for (linp = linpstart + linesz; | |
1061 | linp < linpend; | |
1062 | linp += linesz) | |
1063 | { | |
1064 | bfd_coff_swap_lineno_in (abfd, (PTR) linp, | |
1065 | (PTR) &lin); | |
1066 | if (lin.l_lnno == 0) | |
1067 | break; | |
1068 | } | |
1069 | csect->lineno_count += (linp - linpstart) / linesz; | |
f78195df ILT |
1070 | /* The setting of line_filepos will only be |
1071 | useful if all the line number entries for a | |
1072 | csect are contiguous; this only matters for | |
1073 | error reporting. */ | |
1074 | if (csect->line_filepos == 0) | |
1075 | csect->line_filepos = | |
1076 | auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr; | |
28a0c103 ILT |
1077 | } |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | ||
aadf04f7 SS |
1082 | /* Pick up the csect auxiliary information. */ |
1083 | ||
1084 | if (sym.n_numaux == 0) | |
1085 | { | |
1086 | (*_bfd_error_handler) | |
1087 | ("%s: class %d symbol `%s' has no aux entries", | |
1088 | bfd_get_filename (abfd), sym.n_sclass, name); | |
1089 | bfd_set_error (bfd_error_bad_value); | |
1090 | goto error_return; | |
1091 | } | |
1092 | ||
1093 | bfd_coff_swap_aux_in (abfd, | |
1094 | (PTR) (esym + symesz * sym.n_numaux), | |
1095 | sym.n_type, sym.n_sclass, | |
1096 | sym.n_numaux - 1, sym.n_numaux, | |
1097 | (PTR) &aux); | |
1098 | ||
1099 | smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); | |
1100 | ||
1101 | flags = BSF_GLOBAL; | |
1102 | section = NULL; | |
1103 | value = 0; | |
28a0c103 | 1104 | set_toc = NULL; |
aadf04f7 SS |
1105 | |
1106 | switch (smtyp) | |
1107 | { | |
1108 | default: | |
1109 | (*_bfd_error_handler) | |
1110 | ("%s: symbol `%s' has unrecognized csect type %d", | |
1111 | bfd_get_filename (abfd), name, smtyp); | |
1112 | bfd_set_error (bfd_error_bad_value); | |
1113 | goto error_return; | |
1114 | ||
1115 | case XTY_ER: | |
1116 | /* This is an external reference. */ | |
1117 | if (sym.n_sclass == C_HIDEXT | |
1118 | || sym.n_scnum != N_UNDEF | |
1119 | || aux.x_csect.x_scnlen.l != 0) | |
1120 | { | |
1121 | (*_bfd_error_handler) | |
1122 | ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d", | |
1123 | bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum, | |
1124 | aux.x_csect.x_scnlen.l); | |
1125 | bfd_set_error (bfd_error_bad_value); | |
1126 | goto error_return; | |
1127 | } | |
1128 | section = bfd_und_section_ptr; | |
1129 | break; | |
1130 | ||
1131 | case XTY_SD: | |
1132 | /* This is a csect definition. */ | |
1133 | ||
28a0c103 ILT |
1134 | if (csect != NULL) |
1135 | { | |
1136 | xcoff_section_data (abfd, csect)->last_symndx = | |
1137 | ((esym | |
1138 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1139 | / symesz); | |
1140 | } | |
1141 | ||
aadf04f7 SS |
1142 | csect = NULL; |
1143 | csect_index = -1; | |
1144 | ||
1145 | /* When we see a TOC anchor, we record the TOC value. */ | |
1146 | if (aux.x_csect.x_smclas == XMC_TC0) | |
1147 | { | |
1148 | if (sym.n_sclass != C_HIDEXT | |
1149 | || aux.x_csect.x_scnlen.l != 0) | |
1150 | { | |
1151 | (*_bfd_error_handler) | |
1152 | ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d", | |
1153 | bfd_get_filename (abfd), name, sym.n_sclass, | |
1154 | aux.x_csect.x_scnlen.l); | |
1155 | bfd_set_error (bfd_error_bad_value); | |
1156 | goto error_return; | |
1157 | } | |
1158 | xcoff_data (abfd)->toc = sym.n_value; | |
1159 | } | |
1160 | ||
1161 | /* We must merge TOC entries for the same symbol. We can | |
1162 | merge two TOC entries if they are both C_HIDEXT, they | |
1163 | both have the same name, they are both 4 bytes long, and | |
1164 | they both have a relocation table entry for an external | |
1165 | symbol with the same name. Unfortunately, this means | |
1166 | that we must look through the relocations. Ick. */ | |
1167 | if (aux.x_csect.x_smclas == XMC_TC | |
1168 | && sym.n_sclass == C_HIDEXT | |
28a0c103 ILT |
1169 | && aux.x_csect.x_scnlen.l == 4 |
1170 | && info->hash->creator == abfd->xvec) | |
aadf04f7 SS |
1171 | { |
1172 | asection *enclosing; | |
1173 | bfd_size_type relindx; | |
1174 | struct internal_reloc *rel; | |
28a0c103 | 1175 | asection **rel_csect; |
aadf04f7 SS |
1176 | |
1177 | enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); | |
1178 | if (enclosing == NULL) | |
28a0c103 | 1179 | goto error_return; |
aadf04f7 SS |
1180 | |
1181 | /* XCOFF requires that relocs be sorted by address, so | |
1182 | we could do a binary search here. FIXME. */ | |
1183 | rel = reloc_info[enclosing->target_index].relocs; | |
28a0c103 | 1184 | rel_csect = reloc_info[enclosing->target_index].csects; |
aadf04f7 SS |
1185 | for (relindx = 0; |
1186 | relindx < enclosing->reloc_count; | |
28a0c103 | 1187 | relindx++, rel++, rel_csect++) |
aadf04f7 | 1188 | { |
28a0c103 | 1189 | if (*rel_csect == NULL |
aadf04f7 SS |
1190 | && rel->r_vaddr == (bfd_vma) sym.n_value |
1191 | && rel->r_size == 31 | |
28a0c103 | 1192 | && rel->r_type == R_POS) |
aadf04f7 SS |
1193 | break; |
1194 | } | |
1195 | if (relindx < enclosing->reloc_count) | |
1196 | { | |
1197 | bfd_byte *erelsym; | |
1198 | struct internal_syment relsym; | |
1199 | ||
1200 | erelsym = ((bfd_byte *) obj_coff_external_syms (abfd) | |
1201 | + rel->r_symndx * symesz); | |
1202 | bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym); | |
1203 | if (relsym.n_sclass == C_EXT) | |
1204 | { | |
1205 | const char *relname; | |
1206 | char relbuf[SYMNMLEN + 1]; | |
1207 | boolean copy; | |
1208 | struct xcoff_link_hash_entry *h; | |
1209 | ||
1210 | /* At this point we know that the TOC entry is | |
1211 | for an externally visible symbol. */ | |
1212 | relname = _bfd_coff_internal_syment_name (abfd, &relsym, | |
1213 | relbuf); | |
1214 | if (relname == NULL) | |
28a0c103 | 1215 | goto error_return; |
aadf04f7 | 1216 | |
867d923d ILT |
1217 | /* We only merge TOC entries if the TC name is |
1218 | the same as the symbol name. This handles | |
1219 | the normal case, but not common cases like | |
1220 | SYM.P4 which gcc generates to store SYM + 4 | |
1221 | in the TOC. FIXME. */ | |
1222 | if (strcmp (name, relname) == 0) | |
aadf04f7 | 1223 | { |
867d923d ILT |
1224 | copy = (! info->keep_memory |
1225 | || relsym._n._n_n._n_zeroes != 0 | |
1226 | || relsym._n._n_n._n_offset == 0); | |
1227 | h = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
1228 | relname, true, copy, | |
1229 | false); | |
1230 | if (h == NULL) | |
1231 | goto error_return; | |
1232 | ||
1233 | /* At this point h->root.type could be | |
1234 | bfd_link_hash_new. That should be OK, | |
1235 | since we know for sure that we will come | |
1236 | across this symbol as we step through the | |
1237 | file. */ | |
1238 | ||
1239 | /* We store h in *sym_hash for the | |
1240 | convenience of the relocate_section | |
1241 | function. */ | |
1242 | *sym_hash = h; | |
1243 | ||
1244 | if (h->toc_section != NULL) | |
1245 | { | |
1246 | /* We already have a TOC entry for this | |
1247 | symbol, so we can just ignore this | |
1248 | one. */ | |
1249 | *rel_csect = bfd_und_section_ptr; | |
1250 | break; | |
1251 | } | |
aadf04f7 | 1252 | |
867d923d ILT |
1253 | /* We are about to create a TOC entry for |
1254 | this symbol. */ | |
1255 | set_toc = h; | |
1256 | } | |
aadf04f7 SS |
1257 | } |
1258 | } | |
1259 | } | |
1260 | ||
1261 | /* We need to create a new section. We get the name from | |
1262 | the csect storage mapping class, so that the linker can | |
1263 | accumulate similar csects together. */ | |
1264 | { | |
1265 | static const char *csect_name_by_class[] = | |
1266 | { | |
1267 | ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", | |
1268 | ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", | |
1269 | ".td" | |
1270 | }; | |
1271 | const char *csect_name; | |
1272 | asection *enclosing; | |
1273 | struct internal_reloc *rel; | |
1274 | bfd_size_type relindx; | |
28a0c103 | 1275 | asection **rel_csect; |
aadf04f7 SS |
1276 | |
1277 | if ((aux.x_csect.x_smclas >= | |
1278 | sizeof csect_name_by_class / sizeof csect_name_by_class[0]) | |
1279 | || csect_name_by_class[aux.x_csect.x_smclas] == NULL) | |
1280 | { | |
1281 | (*_bfd_error_handler) | |
1282 | ("%s: symbol `%s' has unrecognized smclas %d", | |
1283 | bfd_get_filename (abfd), name, aux.x_csect.x_smclas); | |
1284 | bfd_set_error (bfd_error_bad_value); | |
1285 | goto error_return; | |
1286 | } | |
1287 | ||
1288 | csect_name = csect_name_by_class[aux.x_csect.x_smclas]; | |
1289 | csect = bfd_make_section_anyway (abfd, csect_name); | |
1290 | if (csect == NULL) | |
1291 | goto error_return; | |
1292 | enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); | |
1293 | if (enclosing == NULL) | |
1294 | goto error_return; | |
1295 | if ((bfd_vma) sym.n_value < enclosing->vma | |
1296 | || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l | |
1297 | > enclosing->vma + enclosing->_raw_size)) | |
1298 | { | |
1299 | (*_bfd_error_handler) | |
1300 | ("%s: csect `%s' not in enclosing section", | |
1301 | bfd_get_filename (abfd), name); | |
1302 | bfd_set_error (bfd_error_bad_value); | |
1303 | goto error_return; | |
1304 | } | |
1305 | csect->vma = sym.n_value; | |
1306 | csect->filepos = (enclosing->filepos | |
1307 | + sym.n_value | |
1308 | - enclosing->vma); | |
1309 | csect->_raw_size = aux.x_csect.x_scnlen.l; | |
1310 | csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; | |
1311 | csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); | |
1312 | ||
28a0c103 ILT |
1313 | /* Record the enclosing section in the tdata for this new |
1314 | section. */ | |
1315 | csect->used_by_bfd = | |
1316 | ((struct coff_section_tdata *) | |
1317 | bfd_zalloc (abfd, sizeof (struct coff_section_tdata))); | |
1318 | if (csect->used_by_bfd == NULL) | |
1319 | { | |
1320 | bfd_set_error (bfd_error_no_memory); | |
1321 | goto error_return; | |
1322 | } | |
1323 | coff_section_data (abfd, csect)->tdata = | |
1324 | bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata)); | |
1325 | if (coff_section_data (abfd, csect)->tdata == NULL) | |
1326 | { | |
1327 | bfd_set_error (bfd_error_no_memory); | |
1328 | goto error_return; | |
1329 | } | |
1330 | xcoff_section_data (abfd, csect)->enclosing = enclosing; | |
b73322d9 ILT |
1331 | xcoff_section_data (abfd, csect)->lineno_count = |
1332 | enclosing->lineno_count; | |
28a0c103 | 1333 | |
aadf04f7 SS |
1334 | /* XCOFF requires that relocs be sorted by address, so we |
1335 | could do a binary search here. FIXME. (XCOFF | |
1336 | unfortunately does not require that symbols be sorted | |
1337 | by address, or this would be a simple merge). */ | |
1338 | rel = reloc_info[enclosing->target_index].relocs; | |
28a0c103 | 1339 | rel_csect = reloc_info[enclosing->target_index].csects; |
aadf04f7 SS |
1340 | for (relindx = 0; |
1341 | relindx < enclosing->reloc_count; | |
28a0c103 | 1342 | relindx++, rel++, rel_csect++) |
aadf04f7 | 1343 | { |
28a0c103 | 1344 | if (*rel_csect == NULL |
aadf04f7 SS |
1345 | && rel->r_vaddr >= csect->vma |
1346 | && rel->r_vaddr < csect->vma + csect->_raw_size) | |
1347 | { | |
1348 | csect->rel_filepos = (enclosing->rel_filepos | |
1349 | + relindx * bfd_coff_relsz (abfd)); | |
1350 | break; | |
1351 | } | |
1352 | } | |
1353 | while (relindx < enclosing->reloc_count | |
28a0c103 | 1354 | && *rel_csect == NULL |
aadf04f7 SS |
1355 | && rel->r_vaddr >= csect->vma |
1356 | && rel->r_vaddr < csect->vma + csect->_raw_size) | |
1357 | { | |
28a0c103 | 1358 | *rel_csect = csect; |
aadf04f7 SS |
1359 | csect->flags |= SEC_RELOC; |
1360 | ++csect->reloc_count; | |
1361 | ++relindx; | |
1362 | ++rel; | |
28a0c103 | 1363 | ++rel_csect; |
aadf04f7 SS |
1364 | } |
1365 | ||
1366 | /* There are a number of other fields and section flags | |
1367 | which we do not bother to set. */ | |
1368 | ||
aadf04f7 SS |
1369 | csect_index = ((esym |
1370 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1371 | / symesz); | |
1372 | ||
28a0c103 ILT |
1373 | xcoff_section_data (abfd, csect)->first_symndx = csect_index; |
1374 | ||
aadf04f7 SS |
1375 | if (first_csect == NULL) |
1376 | first_csect = csect; | |
1377 | ||
1378 | /* If this symbol is C_EXT, we treat it as starting at the | |
1379 | beginning of the newly created section. */ | |
1380 | if (sym.n_sclass == C_EXT) | |
1381 | { | |
1382 | section = csect; | |
1383 | value = 0; | |
1384 | } | |
28a0c103 ILT |
1385 | |
1386 | /* If this is a TOC section for a symbol, record it. */ | |
1387 | if (set_toc != NULL) | |
1388 | { | |
1389 | set_toc->toc_section = csect; | |
1390 | set_toc->toc_offset = 0; | |
1391 | } | |
aadf04f7 SS |
1392 | } |
1393 | break; | |
1394 | ||
1395 | case XTY_LD: | |
1396 | /* This is a label definition. The x_scnlen field is the | |
1397 | symbol index of the csect. I believe that this must | |
1398 | always follow the appropriate XTY_SD symbol, so I will | |
1399 | insist on it. */ | |
1400 | { | |
1401 | boolean bad; | |
1402 | ||
1403 | bad = false; | |
28a0c103 | 1404 | if (aux.x_csect.x_scnlen.l < 0 |
aadf04f7 SS |
1405 | || (aux.x_csect.x_scnlen.l |
1406 | >= esym - (bfd_byte *) obj_coff_external_syms (abfd))) | |
1407 | bad = true; | |
1408 | if (! bad) | |
1409 | { | |
1410 | section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l]; | |
1411 | if (section == NULL | |
1412 | || (section->flags & SEC_HAS_CONTENTS) == 0) | |
1413 | bad = true; | |
1414 | } | |
1415 | if (bad) | |
1416 | { | |
1417 | (*_bfd_error_handler) | |
1418 | ("%s: misplaced XTY_LD `%s'", | |
1419 | bfd_get_filename (abfd), name); | |
1420 | bfd_set_error (bfd_error_bad_value); | |
1421 | goto error_return; | |
1422 | } | |
1423 | ||
1424 | value = sym.n_value - csect->vma; | |
1425 | } | |
1426 | break; | |
1427 | ||
1428 | case XTY_CM: | |
1429 | /* This is an unitialized csect. We could base the name on | |
1430 | the storage mapping class, but we don't bother. If this | |
1431 | csect is externally visible, it is a common symbol. */ | |
28a0c103 ILT |
1432 | |
1433 | if (csect != NULL) | |
aadf04f7 | 1434 | { |
28a0c103 ILT |
1435 | xcoff_section_data (abfd, csect)->last_symndx = |
1436 | ((esym | |
1437 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1438 | / symesz); | |
aadf04f7 | 1439 | } |
28a0c103 ILT |
1440 | |
1441 | csect = bfd_make_section_anyway (abfd, ".bss"); | |
1442 | if (csect == NULL) | |
1443 | goto error_return; | |
867d923d | 1444 | csect->vma = sym.n_value; |
28a0c103 ILT |
1445 | csect->_raw_size = aux.x_csect.x_scnlen.l; |
1446 | csect->flags |= SEC_ALLOC; | |
1447 | csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); | |
1448 | /* There are a number of other fields and section flags | |
1449 | which we do not bother to set. */ | |
1450 | ||
1451 | csect_index = ((esym | |
1452 | - (bfd_byte *) obj_coff_external_syms (abfd)) | |
1453 | / symesz); | |
1454 | ||
1455 | csect->used_by_bfd = | |
1456 | ((struct coff_section_tdata *) | |
1457 | bfd_zalloc (abfd, sizeof (struct coff_section_tdata))); | |
1458 | if (csect->used_by_bfd == NULL) | |
aadf04f7 | 1459 | { |
28a0c103 ILT |
1460 | bfd_set_error (bfd_error_no_memory); |
1461 | goto error_return; | |
1462 | } | |
1463 | coff_section_data (abfd, csect)->tdata = | |
1464 | bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata)); | |
1465 | if (coff_section_data (abfd, csect)->tdata == NULL) | |
1466 | { | |
1467 | bfd_set_error (bfd_error_no_memory); | |
1468 | goto error_return; | |
1469 | } | |
1470 | xcoff_section_data (abfd, csect)->first_symndx = csect_index; | |
1471 | ||
1472 | if (first_csect == NULL) | |
1473 | first_csect = csect; | |
1474 | ||
1475 | if (sym.n_sclass == C_EXT) | |
1476 | { | |
1477 | csect->flags |= SEC_IS_COMMON; | |
1478 | section = csect; | |
1479 | value = aux.x_csect.x_scnlen.l; | |
aadf04f7 | 1480 | } |
28a0c103 | 1481 | |
aadf04f7 SS |
1482 | break; |
1483 | } | |
1484 | ||
1485 | /* Now we have enough information to add the symbol to the | |
1486 | linker hash table. */ | |
1487 | ||
1488 | if (sym.n_sclass == C_EXT) | |
1489 | { | |
1490 | boolean copy; | |
1491 | ||
1492 | BFD_ASSERT (section != NULL); | |
1493 | ||
1494 | /* We must copy the name into memory if we got it from the | |
1495 | syment itself, rather than the string table. */ | |
1496 | copy = default_copy; | |
1497 | if (sym._n._n_n._n_zeroes != 0 | |
1498 | || sym._n._n_n._n_offset == 0) | |
1499 | copy = true; | |
1500 | ||
28a0c103 ILT |
1501 | if (info->hash->creator == abfd->xvec) |
1502 | { | |
1503 | /* If we are statically linking a shared object, it is | |
1504 | OK for symbol redefinitions to occur. I can't figure | |
1505 | out just what the XCOFF linker is doing, but | |
1506 | something like this is required for -bnso to work. */ | |
1507 | *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
1508 | name, true, copy, false); | |
1509 | if (*sym_hash == NULL) | |
1510 | goto error_return; | |
1511 | if (((*sym_hash)->root.type == bfd_link_hash_defined | |
1512 | || (*sym_hash)->root.type == bfd_link_hash_defweak) | |
1513 | && ! bfd_is_und_section (section) | |
1514 | && ! bfd_is_com_section (section)) | |
1515 | { | |
1516 | if ((abfd->flags & DYNAMIC) != 0) | |
1517 | { | |
1518 | section = bfd_und_section_ptr; | |
1519 | value = 0; | |
1520 | } | |
1521 | else if (((*sym_hash)->root.u.def.section->owner->flags | |
1522 | & DYNAMIC) != 0) | |
1523 | { | |
1524 | (*sym_hash)->root.type = bfd_link_hash_undefined; | |
1525 | (*sym_hash)->root.u.undef.abfd = | |
1526 | (*sym_hash)->root.u.def.section->owner; | |
1527 | } | |
1528 | } | |
1529 | } | |
1530 | ||
aadf04f7 SS |
1531 | if (! (_bfd_generic_link_add_one_symbol |
1532 | (info, abfd, name, flags, section, value, | |
1533 | (const char *) NULL, copy, false, | |
1534 | (struct bfd_link_hash_entry **) sym_hash))) | |
1535 | goto error_return; | |
1536 | ||
867d923d ILT |
1537 | if (smtyp == XTY_CM) |
1538 | { | |
1539 | if ((*sym_hash)->root.type != bfd_link_hash_common | |
1540 | || (*sym_hash)->root.u.c.p->section != csect) | |
1541 | { | |
1542 | /* We don't need the common csect we just created. */ | |
1543 | csect->_raw_size = 0; | |
1544 | } | |
1545 | else | |
1546 | { | |
1547 | (*sym_hash)->root.u.c.p->alignment_power | |
1548 | = csect->alignment_power; | |
1549 | } | |
1550 | } | |
1551 | ||
aadf04f7 | 1552 | if (info->hash->creator == abfd->xvec) |
28a0c103 ILT |
1553 | { |
1554 | int flag; | |
1555 | ||
1556 | if (smtyp == XTY_ER || smtyp == XTY_CM) | |
1557 | flag = XCOFF_REF_REGULAR; | |
1558 | else | |
1559 | flag = XCOFF_DEF_REGULAR; | |
1560 | (*sym_hash)->flags |= flag; | |
1561 | ||
1562 | if ((*sym_hash)->smclas == XMC_UA) | |
1563 | (*sym_hash)->smclas = aux.x_csect.x_smclas; | |
1564 | } | |
aadf04f7 SS |
1565 | } |
1566 | ||
1567 | *csect_cache = csect; | |
1568 | ||
1569 | esym += (sym.n_numaux + 1) * symesz; | |
1570 | sym_hash += sym.n_numaux + 1; | |
1571 | csect_cache += sym.n_numaux + 1; | |
aadf04f7 SS |
1572 | } |
1573 | ||
28a0c103 ILT |
1574 | /* Make sure that we have seen all the relocs. */ |
1575 | for (sub = abfd->sections; sub != first_csect; sub = sub->next) | |
aadf04f7 | 1576 | { |
28a0c103 ILT |
1577 | /* Reset the section size, since the data is now attached to the |
1578 | csects. Don't reset the size of the .debug section, since we | |
1579 | need to read it below in bfd_xcoff_size_dynamic_sections. */ | |
1580 | if (strcmp (bfd_get_section_name (abfd, sub), ".debug") != 0) | |
1581 | sub->_raw_size = 0; | |
aadf04f7 SS |
1582 | |
1583 | if ((sub->flags & SEC_RELOC) != 0) | |
1584 | { | |
1585 | bfd_size_type i; | |
28a0c103 ILT |
1586 | struct internal_reloc *rel; |
1587 | asection **rel_csect; | |
aadf04f7 | 1588 | |
28a0c103 ILT |
1589 | rel = reloc_info[sub->target_index].relocs; |
1590 | rel_csect = reloc_info[sub->target_index].csects; | |
1591 | for (i = 0; i < sub->reloc_count; i++, rel++, rel_csect++) | |
aadf04f7 | 1592 | { |
28a0c103 | 1593 | if (*rel_csect == NULL) |
aadf04f7 SS |
1594 | { |
1595 | (*_bfd_error_handler) | |
1596 | ("%s: reloc %s:%d not in csect", | |
1597 | bfd_get_filename (abfd), sub->name, i); | |
1598 | bfd_set_error (bfd_error_bad_value); | |
1599 | goto error_return; | |
1600 | } | |
28a0c103 | 1601 | |
867d923d ILT |
1602 | /* We identify all symbols which are called, so that we |
1603 | can create glue code for calls to functions imported | |
1604 | from dynamic objects. */ | |
28a0c103 | 1605 | if (info->hash->creator == abfd->xvec |
867d923d ILT |
1606 | && *rel_csect != bfd_und_section_ptr |
1607 | && (rel->r_type == R_BR | |
1608 | || rel->r_type == R_RBR) | |
1609 | && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL) | |
28a0c103 ILT |
1610 | { |
1611 | struct xcoff_link_hash_entry *h; | |
1612 | ||
867d923d ILT |
1613 | h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx]; |
1614 | h->flags |= XCOFF_CALLED; | |
1615 | /* If the symbol name starts with a period, it is | |
1616 | the code of a function. If the symbol is | |
1617 | currently undefined, then add an undefined symbol | |
1618 | for the function descriptor. This should do no | |
1619 | harm, because any regular object that defines the | |
1620 | function should also define the function | |
1621 | descriptor. It helps, because it means that we | |
1622 | will identify the function descriptor with a | |
1623 | dynamic object if a dynamic object defines it. */ | |
1624 | if (h->root.root.string[0] == '.' | |
1625 | && h->descriptor == NULL) | |
28a0c103 | 1626 | { |
867d923d ILT |
1627 | struct xcoff_link_hash_entry *hds; |
1628 | ||
1629 | hds = xcoff_link_hash_lookup (xcoff_hash_table (info), | |
1630 | h->root.root.string + 1, | |
1631 | true, false, true); | |
1632 | if (hds == NULL) | |
1633 | goto error_return; | |
1634 | if (hds->root.type == bfd_link_hash_new) | |
28a0c103 | 1635 | { |
867d923d ILT |
1636 | if (! (_bfd_generic_link_add_one_symbol |
1637 | (info, abfd, hds->root.root.string, | |
1638 | (flagword) 0, bfd_und_section_ptr, | |
1639 | (bfd_vma) 0, (const char *) NULL, false, | |
1640 | false, | |
1641 | (struct bfd_link_hash_entry **) NULL))) | |
1642 | goto error_return; | |
28a0c103 | 1643 | } |
867d923d | 1644 | h->descriptor = hds; |
28a0c103 ILT |
1645 | } |
1646 | } | |
aadf04f7 SS |
1647 | } |
1648 | ||
28a0c103 ILT |
1649 | free (reloc_info[sub->target_index].csects); |
1650 | reloc_info[sub->target_index].csects = NULL; | |
aadf04f7 | 1651 | |
28a0c103 ILT |
1652 | /* Reset SEC_RELOC, the reloc_count, and the lineno_count, |
1653 | since the reloc and lineno information is now attached to | |
1654 | the csects. */ | |
aadf04f7 SS |
1655 | sub->flags &=~ SEC_RELOC; |
1656 | sub->reloc_count = 0; | |
28a0c103 | 1657 | sub->lineno_count = 0; |
aadf04f7 SS |
1658 | |
1659 | /* If we are not keeping memory, free the reloc information. */ | |
1660 | if (! info->keep_memory | |
1661 | && coff_section_data (abfd, sub) != NULL | |
28a0c103 ILT |
1662 | && coff_section_data (abfd, sub)->relocs != NULL |
1663 | && ! coff_section_data (abfd, sub)->keep_relocs) | |
aadf04f7 SS |
1664 | { |
1665 | free (coff_section_data (abfd, sub)->relocs); | |
1666 | coff_section_data (abfd, sub)->relocs = NULL; | |
1667 | } | |
1668 | } | |
28a0c103 ILT |
1669 | |
1670 | /* Free up the line numbers. FIXME: We could cache these | |
1671 | somewhere for the final link, to avoid reading them again. */ | |
1672 | if (reloc_info[sub->target_index].linenos != NULL) | |
1673 | { | |
1674 | free (reloc_info[sub->target_index].linenos); | |
1675 | reloc_info[sub->target_index].linenos = NULL; | |
1676 | } | |
aadf04f7 SS |
1677 | } |
1678 | ||
1679 | free (reloc_info); | |
1680 | ||
28a0c103 ILT |
1681 | obj_coff_keep_syms (abfd) = keep_syms; |
1682 | ||
1683 | return true; | |
1684 | ||
1685 | error_return: | |
1686 | if (reloc_info != NULL) | |
1687 | { | |
1688 | for (sub = abfd->sections; sub != NULL; sub = sub->next) | |
1689 | { | |
1690 | if (reloc_info[sub->target_index].csects != NULL) | |
1691 | free (reloc_info[sub->target_index].csects); | |
1692 | if (reloc_info[sub->target_index].linenos != NULL) | |
1693 | free (reloc_info[sub->target_index].linenos); | |
1694 | } | |
1695 | free (reloc_info); | |
1696 | } | |
1697 | obj_coff_keep_syms (abfd) = keep_syms; | |
1698 | return false; | |
1699 | } | |
1700 | ||
1701 | #undef N_TMASK | |
1702 | #undef N_BTSHFT | |
1703 | ||
1704 | /* This function is used to add symbols from a dynamic object to the | |
1705 | global symbol table. */ | |
1706 | ||
1707 | static boolean | |
1708 | xcoff_link_add_dynamic_symbols (abfd, info) | |
1709 | bfd *abfd; | |
1710 | struct bfd_link_info *info; | |
1711 | { | |
1712 | bfd_size_type symesz; | |
1713 | bfd_byte *esym; | |
1714 | bfd_byte *esym_end; | |
1715 | struct xcoff_import_file *n; | |
1716 | const char *bname; | |
1717 | const char *mname; | |
1718 | const char *s; | |
1719 | unsigned int c; | |
1720 | struct xcoff_import_file **pp; | |
1721 | ||
1722 | /* We can only handle a dynamic object if we are generating an XCOFF | |
1723 | output file. */ | |
1724 | if (info->hash->creator != abfd->xvec) | |
1725 | { | |
1726 | (*_bfd_error_handler) | |
1727 | ("%s: XCOFF shared object when not producing XCOFF output", | |
1728 | bfd_get_filename (abfd)); | |
1729 | bfd_set_error (bfd_error_invalid_operation); | |
1730 | return false; | |
1731 | } | |
1732 | ||
1733 | /* Remove the sections from this object, so that they do not get | |
1734 | included in the link. */ | |
1735 | abfd->sections = NULL; | |
1736 | ||
1737 | symesz = bfd_coff_symesz (abfd); | |
1738 | esym = (bfd_byte *) obj_coff_external_syms (abfd); | |
1739 | esym_end = esym + obj_raw_syment_count (abfd) * symesz; | |
1740 | while (esym < esym_end) | |
1741 | { | |
1742 | struct internal_syment sym; | |
1743 | ||
1744 | bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym); | |
1745 | ||
1746 | /* I think that every symbol mentioned in a dynamic object must | |
1747 | be defined by that object, perhaps by importing it from | |
1748 | another dynamic object. All we have to do is look up each | |
1749 | external symbol. If we have already put it in the hash | |
1750 | table, we simply set a flag indicating that it appears in a | |
1751 | dynamic object. */ | |
1752 | ||
1753 | if (sym.n_sclass == C_EXT) | |
1754 | { | |
1755 | const char *name; | |
1756 | char buf[SYMNMLEN + 1]; | |
1757 | struct xcoff_link_hash_entry *h; | |
1758 | ||
1759 | name = _bfd_coff_internal_syment_name (abfd, &sym, buf); | |
1760 | if (name == NULL) | |
1761 | return false; | |
1762 | ||
1763 | /* Normally we could not xcoff_link_hash_lookup in an add | |
1764 | symbols routine, since we might not be using an XCOFF | |
1765 | hash table. However, we verified above that we are using | |
1766 | an XCOFF hash table. */ | |
1767 | h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, | |
1768 | false, false, true); | |
1769 | if (h != NULL) | |
1770 | { | |
1771 | h->flags |= XCOFF_REF_DYNAMIC; | |
1772 | ||
1773 | /* If the symbol is undefined, and the current BFD is | |
1774 | not a dynamic object, change the BFD to this dynamic | |
867d923d ILT |
1775 | object, so that we can get the correct import file |
1776 | ID. */ | |
1777 | if ((h->root.type == bfd_link_hash_undefined | |
1778 | || h->root.type == bfd_link_hash_undefweak) | |
1779 | && (h->root.u.undef.abfd == NULL | |
1780 | || (h->root.u.undef.abfd->flags & DYNAMIC) == 0)) | |
28a0c103 ILT |
1781 | h->root.u.undef.abfd = abfd; |
1782 | ||
1783 | if (h->smclas == XMC_UA | |
1784 | && sym.n_numaux > 0) | |
1785 | { | |
1786 | union internal_auxent aux; | |
1787 | ||
1788 | bfd_coff_swap_aux_in (abfd, | |
1789 | (PTR) (esym + symesz * sym.n_numaux), | |
1790 | sym.n_type, sym.n_sclass, | |
1791 | sym.n_numaux - 1, sym.n_numaux, | |
1792 | (PTR) &aux); | |
1793 | h->smclas = aux.x_csect.x_smclas; | |
1794 | } | |
1795 | } | |
1796 | } | |
1797 | ||
1798 | esym += (sym.n_numaux + 1) * symesz; | |
1799 | } | |
1800 | ||
1801 | /* Record this file in the import files. */ | |
1802 | ||
1803 | n = ((struct xcoff_import_file *) | |
1804 | bfd_alloc (abfd, sizeof (struct xcoff_import_file))); | |
1805 | if (n == NULL) | |
1806 | { | |
1807 | bfd_set_error (bfd_error_no_memory); | |
1808 | return false; | |
1809 | } | |
1810 | n->next = NULL; | |
1811 | ||
1812 | /* For some reason, the path entry in the import file list for a | |
1813 | shared object appears to always be empty. The file name is the | |
1814 | base name. */ | |
1815 | n->path = ""; | |
1816 | if (abfd->my_archive == NULL) | |
1817 | { | |
1818 | bname = bfd_get_filename (abfd); | |
1819 | mname = ""; | |
1820 | } | |
1821 | else | |
1822 | { | |
1823 | bname = bfd_get_filename (abfd->my_archive); | |
1824 | mname = bfd_get_filename (abfd); | |
1825 | } | |
1826 | s = strrchr (bname, '/'); | |
1827 | if (s != NULL) | |
1828 | bname = s + 1; | |
1829 | n->file = bname; | |
1830 | n->member = mname; | |
1831 | ||
1832 | /* We start c at 1 because the first import file number is reserved | |
1833 | for LIBPATH. */ | |
1834 | for (pp = &xcoff_hash_table (info)->imports, c = 1; | |
1835 | *pp != NULL; | |
1836 | pp = &(*pp)->next, ++c) | |
1837 | ; | |
1838 | *pp = n; | |
1839 | ||
1840 | xcoff_data (abfd)->import_file_id = c; | |
1841 | ||
1842 | return true; | |
1843 | } | |
1844 | \f | |
1845 | /* Routines that are called after all the input files have been | |
1846 | handled, but before the sections are laid out in memory. */ | |
1847 | ||
1848 | /* Import a symbol. */ | |
1849 | ||
1850 | boolean | |
1851 | bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile, | |
1852 | impmember) | |
1853 | bfd *output_bfd; | |
1854 | struct bfd_link_info *info; | |
1855 | struct bfd_link_hash_entry *harg; | |
1856 | bfd_vma val; | |
1857 | const char *imppath; | |
1858 | const char *impfile; | |
1859 | const char *impmember; | |
1860 | { | |
1861 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
1862 | ||
1863 | h->flags |= XCOFF_IMPORT; | |
1864 | ||
1865 | if (val != (bfd_vma) -1) | |
1866 | { | |
1867 | if (h->root.type == bfd_link_hash_defined) | |
1868 | { | |
1869 | if (! ((*info->callbacks->multiple_definition) | |
1870 | (info, h->root.root.string, h->root.u.def.section->owner, | |
1871 | h->root.u.def.section, h->root.u.def.value, | |
1872 | output_bfd, bfd_abs_section_ptr, val))) | |
1873 | return false; | |
1874 | } | |
1875 | ||
1876 | h->root.type = bfd_link_hash_defined; | |
1877 | h->root.u.def.section = bfd_abs_section_ptr; | |
1878 | h->root.u.def.value = val; | |
1879 | } | |
1880 | ||
1881 | if (h->ldsym == NULL) | |
1882 | { | |
1883 | h->ldsym = ((struct internal_ldsym *) | |
1884 | bfd_zalloc (output_bfd, sizeof (struct internal_ldsym))); | |
1885 | if (h->ldsym == NULL) | |
1886 | { | |
1887 | bfd_set_error (bfd_error_no_memory); | |
1888 | return false; | |
1889 | } | |
1890 | } | |
1891 | ||
1892 | if (imppath == NULL) | |
1893 | h->ldsym->l_ifile = (bfd_size_type) -1; | |
1894 | else | |
1895 | { | |
1896 | unsigned int c; | |
1897 | struct xcoff_import_file **pp; | |
1898 | ||
1899 | /* We start c at 1 because the first entry in the import list is | |
1900 | reserved for the library search path. */ | |
1901 | for (pp = &xcoff_hash_table (info)->imports, c = 1; | |
1902 | *pp != NULL; | |
1903 | pp = &(*pp)->next, ++c) | |
1904 | { | |
1905 | if (strcmp ((*pp)->path, imppath) == 0 | |
1906 | && strcmp ((*pp)->file, impfile) == 0 | |
1907 | && strcmp ((*pp)->member, impmember) == 0) | |
1908 | break; | |
1909 | } | |
1910 | ||
1911 | if (*pp == NULL) | |
1912 | { | |
1913 | struct xcoff_import_file *n; | |
1914 | ||
1915 | n = ((struct xcoff_import_file *) | |
1916 | bfd_alloc (output_bfd, sizeof (struct xcoff_import_file))); | |
1917 | if (n == NULL) | |
1918 | { | |
1919 | bfd_set_error (bfd_error_no_memory); | |
1920 | return false; | |
1921 | } | |
1922 | n->next = NULL; | |
1923 | n->path = imppath; | |
1924 | n->file = impfile; | |
1925 | n->member = impmember; | |
1926 | *pp = n; | |
1927 | } | |
1928 | ||
1929 | h->ldsym->l_ifile = c; | |
1930 | } | |
1931 | ||
1932 | return true; | |
1933 | } | |
1934 | ||
1935 | /* Export a symbol. */ | |
1936 | ||
1937 | boolean | |
1938 | bfd_xcoff_export_symbol (output_bfd, info, harg, syscall) | |
1939 | bfd *output_bfd; | |
1940 | struct bfd_link_info *info; | |
1941 | struct bfd_link_hash_entry *harg; | |
1942 | boolean syscall; | |
1943 | { | |
1944 | struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; | |
1945 | ||
1946 | h->flags |= XCOFF_EXPORT; | |
1947 | ||
1948 | /* FIXME: I'm not at all sure what syscall is supposed to mean, so | |
1949 | I'm just going to ignore it until somebody explains it. */ | |
1950 | ||
1951 | return true; | |
1952 | } | |
1953 | ||
1954 | /* This structure is used to pass information through | |
1955 | xcoff_link_hash_traverse. */ | |
1956 | ||
1957 | struct xcoff_loader_info | |
1958 | { | |
1959 | /* Set if a problem occurred. */ | |
1960 | boolean failed; | |
1961 | /* Output BFD. */ | |
1962 | bfd *output_bfd; | |
1963 | /* Link information structure. */ | |
1964 | struct bfd_link_info *info; | |
1965 | /* Number of ldsym structures. */ | |
1966 | size_t ldsym_count; | |
1967 | /* Size of string table. */ | |
1968 | size_t string_size; | |
1969 | /* String table. */ | |
1970 | bfd_byte *strings; | |
1971 | /* Allocated size of string table. */ | |
1972 | size_t string_alc; | |
1973 | }; | |
1974 | ||
1975 | /* Build the .loader section. This is called by the XCOFF linker | |
1976 | emulation before_allocation routine. We must set the size of the | |
1977 | .loader section before the linker lays out the output file. | |
1978 | LIBPATH is the library path to search for shared objects; this is | |
1979 | normally built from the -L arguments passed to the linker. ENTRY | |
1980 | is the name of the entry point symbol. */ | |
1981 | ||
1982 | boolean | |
1983 | bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry, | |
1984 | file_align, maxstack, maxdata, gc, | |
1985 | modtype, textro) | |
1986 | bfd *output_bfd; | |
1987 | struct bfd_link_info *info; | |
1988 | const char *libpath; | |
1989 | const char *entry; | |
1990 | unsigned long file_align; | |
1991 | unsigned long maxstack; | |
1992 | unsigned long maxdata; | |
1993 | boolean gc; | |
1994 | int modtype; | |
1995 | boolean textro; | |
1996 | { | |
1997 | struct xcoff_link_hash_entry *hentry; | |
1998 | asection *lsec; | |
1999 | struct xcoff_loader_info ldinfo; | |
2000 | size_t impsize, impcount; | |
2001 | struct xcoff_import_file *fl; | |
2002 | struct internal_ldhdr *ldhdr; | |
9c234e29 | 2003 | bfd_size_type stoff; |
28a0c103 ILT |
2004 | register char *out; |
2005 | asection *sec; | |
2006 | bfd *sub; | |
2007 | struct bfd_strtab_hash *debug_strtab; | |
2008 | bfd_byte *debug_contents = NULL; | |
2009 | ||
2010 | ldinfo.failed = false; | |
2011 | ldinfo.output_bfd = output_bfd; | |
2012 | ldinfo.info = info; | |
2013 | ldinfo.ldsym_count = 0; | |
2014 | ldinfo.string_size = 0; | |
2015 | ldinfo.strings = NULL; | |
2016 | ldinfo.string_alc = 0; | |
2017 | ||
2018 | xcoff_data (output_bfd)->maxstack = maxstack; | |
2019 | xcoff_data (output_bfd)->maxdata = maxdata; | |
2020 | xcoff_data (output_bfd)->modtype = modtype; | |
2021 | ||
2022 | xcoff_hash_table (info)->file_align = file_align; | |
2023 | xcoff_hash_table (info)->textro = textro; | |
2024 | ||
2025 | hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry, | |
2026 | false, false, true); | |
2027 | if (hentry != NULL) | |
867d923d ILT |
2028 | { |
2029 | hentry->flags |= XCOFF_ENTRY; | |
2030 | if (hentry->root.type == bfd_link_hash_defined | |
2031 | || hentry->root.type == bfd_link_hash_defweak) | |
2032 | xcoff_data (output_bfd)->entry_section = | |
2033 | hentry->root.u.def.section->output_section; | |
2034 | } | |
28a0c103 ILT |
2035 | |
2036 | /* Garbage collect unused sections. */ | |
2037 | if (info->relocateable | |
2038 | || ! gc | |
2039 | || hentry == NULL | |
2040 | || (hentry->root.type != bfd_link_hash_defined | |
2041 | && hentry->root.type != bfd_link_hash_defweak)) | |
f78195df ILT |
2042 | { |
2043 | gc = false; | |
2044 | xcoff_hash_table (info)->gc = false; | |
867d923d ILT |
2045 | |
2046 | /* We still need to call xcoff_mark, in order to set ldrel_count | |
2047 | correctly. */ | |
2048 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
2049 | { | |
2050 | asection *o; | |
2051 | ||
2052 | for (o = sub->sections; o != NULL; o = o->next) | |
2053 | { | |
2054 | if ((o->flags & SEC_MARK) == 0) | |
2055 | { | |
2056 | if (! xcoff_mark (info, o)) | |
2057 | goto error_return; | |
2058 | } | |
2059 | } | |
2060 | } | |
f78195df | 2061 | } |
28a0c103 ILT |
2062 | else |
2063 | { | |
2064 | if (! xcoff_mark (info, hentry->root.u.def.section)) | |
2065 | goto error_return; | |
2066 | xcoff_sweep (info); | |
2067 | xcoff_hash_table (info)->gc = true; | |
2068 | } | |
2069 | ||
2070 | if (info->input_bfds == NULL) | |
2071 | { | |
2072 | /* I'm not sure what to do in this bizarre case. */ | |
2073 | return true; | |
2074 | } | |
2075 | ||
2076 | xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms, | |
2077 | (PTR) &ldinfo); | |
2078 | if (ldinfo.failed) | |
2079 | goto error_return; | |
2080 | ||
2081 | /* Work out the size of the import file names. Each import file ID | |
2082 | consists of three null terminated strings: the path, the file | |
2083 | name, and the archive member name. The first entry in the list | |
2084 | of names is the path to use to find objects, which the linker has | |
2085 | passed in as the libpath argument. For some reason, the path | |
2086 | entry in the other import file names appears to always be empty. */ | |
2087 | impsize = strlen (libpath) + 3; | |
2088 | impcount = 1; | |
2089 | for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next) | |
2090 | { | |
2091 | ++impcount; | |
2092 | impsize += (strlen (fl->path) | |
2093 | + strlen (fl->file) | |
2094 | + strlen (fl->member) | |
2095 | + 3); | |
2096 | } | |
2097 | ||
2098 | /* Set up the .loader section header. */ | |
2099 | ldhdr = &xcoff_hash_table (info)->ldhdr; | |
2100 | ldhdr->l_version = 1; | |
2101 | ldhdr->l_nsyms = ldinfo.ldsym_count; | |
2102 | ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count; | |
2103 | ldhdr->l_istlen = impsize; | |
2104 | ldhdr->l_nimpid = impcount; | |
2105 | ldhdr->l_impoff = (LDHDRSZ | |
2106 | + ldhdr->l_nsyms * LDSYMSZ | |
2107 | + ldhdr->l_nreloc * LDRELSZ); | |
2108 | ldhdr->l_stlen = ldinfo.string_size; | |
9c234e29 ILT |
2109 | stoff = ldhdr->l_impoff + impsize; |
2110 | if (ldinfo.string_size == 0) | |
2111 | ldhdr->l_stoff = 0; | |
2112 | else | |
2113 | ldhdr->l_stoff = stoff; | |
28a0c103 ILT |
2114 | |
2115 | /* We now know the final size of the .loader section. Allocate | |
2116 | space for it. */ | |
2117 | lsec = xcoff_hash_table (info)->loader_section; | |
9c234e29 | 2118 | lsec->_raw_size = stoff + ldhdr->l_stlen; |
28a0c103 ILT |
2119 | lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size); |
2120 | if (lsec->contents == NULL) | |
2121 | { | |
2122 | bfd_set_error (bfd_error_no_memory); | |
2123 | goto error_return; | |
2124 | } | |
2125 | ||
2126 | /* Set up the header. */ | |
2127 | xcoff_swap_ldhdr_out (output_bfd, ldhdr, | |
2128 | (struct external_ldhdr *) lsec->contents); | |
2129 | ||
2130 | /* Set up the import file names. */ | |
2131 | out = (char *) lsec->contents + ldhdr->l_impoff; | |
2132 | strcpy (out, libpath); | |
2133 | out += strlen (libpath) + 1; | |
2134 | *out++ = '\0'; | |
2135 | *out++ = '\0'; | |
2136 | for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next) | |
2137 | { | |
2138 | register const char *s; | |
2139 | ||
2140 | s = fl->path; | |
2141 | while ((*out++ = *s++) != '\0') | |
2142 | ; | |
2143 | s = fl->file; | |
2144 | while ((*out++ = *s++) != '\0') | |
2145 | ; | |
2146 | s = fl->member; | |
2147 | while ((*out++ = *s++) != '\0') | |
2148 | ; | |
2149 | } | |
2150 | ||
9c234e29 | 2151 | BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff); |
28a0c103 ILT |
2152 | |
2153 | /* Set up the symbol string table. */ | |
2154 | if (ldinfo.string_size > 0) | |
aadf04f7 | 2155 | { |
28a0c103 ILT |
2156 | memcpy (out, ldinfo.strings, ldinfo.string_size); |
2157 | free (ldinfo.strings); | |
2158 | ldinfo.strings = NULL; | |
2159 | } | |
2160 | ||
2161 | /* We can't set up the symbol table or the relocs yet, because we | |
2162 | don't yet know the final position of the various sections. The | |
2163 | .loader symbols are written out when the corresponding normal | |
2164 | symbols are written out in xcoff_link_input_bfd or | |
2165 | xcoff_write_global_symbol. The .loader relocs are written out | |
2166 | when the corresponding normal relocs are handled in | |
2167 | xcoff_link_input_bfd. */ | |
2168 | ||
2169 | /* Allocate space for the global linkage section and the global toc | |
2170 | section. */ | |
2171 | sec = xcoff_hash_table (info)->linkage_section; | |
2172 | if (sec->_raw_size > 0) | |
2173 | { | |
2174 | sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size); | |
2175 | if (sec->contents == NULL) | |
2176 | { | |
2177 | bfd_set_error (bfd_error_no_memory); | |
2178 | goto error_return; | |
2179 | } | |
2180 | } | |
2181 | sec = xcoff_hash_table (info)->toc_section; | |
2182 | if (sec->_raw_size > 0) | |
2183 | { | |
2184 | sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size); | |
2185 | if (sec->contents == NULL) | |
2186 | { | |
2187 | bfd_set_error (bfd_error_no_memory); | |
2188 | goto error_return; | |
2189 | } | |
2190 | } | |
2191 | ||
2192 | /* Now that we've done garbage collection, figure out the contents | |
2193 | of the .debug section. */ | |
2194 | debug_strtab = xcoff_hash_table (info)->debug_strtab; | |
2195 | ||
2196 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
2197 | { | |
2198 | asection *subdeb; | |
2199 | bfd_size_type symcount; | |
2200 | unsigned long *debug_index; | |
2201 | asection **csectpp; | |
2202 | bfd_byte *esym, *esymend; | |
2203 | bfd_size_type symesz; | |
2204 | ||
2205 | if (sub->xvec != info->hash->creator) | |
2206 | continue; | |
2207 | subdeb = bfd_get_section_by_name (sub, ".debug"); | |
2208 | if (subdeb == NULL || subdeb->_raw_size == 0) | |
2209 | continue; | |
2210 | ||
2211 | if (info->strip == strip_all | |
2212 | || info->strip == strip_debugger | |
2213 | || info->discard == discard_all) | |
2214 | { | |
2215 | subdeb->_raw_size = 0; | |
2216 | continue; | |
2217 | } | |
2218 | ||
2219 | if (! _bfd_coff_get_external_symbols (sub)) | |
2220 | goto error_return; | |
2221 | ||
2222 | symcount = obj_raw_syment_count (sub); | |
2223 | debug_index = ((unsigned long *) | |
2224 | bfd_zalloc (sub, symcount * sizeof (unsigned long))); | |
2225 | if (debug_index == NULL) | |
2226 | { | |
2227 | bfd_set_error (bfd_error_no_memory); | |
2228 | goto error_return; | |
2229 | } | |
2230 | xcoff_data (sub)->debug_indices = debug_index; | |
2231 | ||
2232 | /* Grab the contents of the .debug section. We use malloc and | |
2233 | copy the neams into the debug stringtab, rather than | |
2234 | bfd_alloc, because I expect that, when linking many files | |
2235 | together, many of the strings will be the same. Storing the | |
2236 | strings in the hash table should save space in this case. */ | |
2237 | debug_contents = (bfd_byte *) malloc (subdeb->_raw_size); | |
2238 | if (debug_contents == NULL) | |
2239 | { | |
2240 | bfd_set_error (bfd_error_no_memory); | |
2241 | goto error_return; | |
2242 | } | |
2243 | if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents, | |
2244 | (file_ptr) 0, subdeb->_raw_size)) | |
2245 | goto error_return; | |
2246 | ||
2247 | csectpp = xcoff_data (sub)->csects; | |
2248 | ||
2249 | symesz = bfd_coff_symesz (sub); | |
2250 | esym = (bfd_byte *) obj_coff_external_syms (sub); | |
2251 | esymend = esym + symcount * symesz; | |
2252 | while (esym < esymend) | |
2253 | { | |
2254 | struct internal_syment sym; | |
2255 | ||
2256 | bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym); | |
2257 | ||
2258 | *debug_index = (unsigned long) -1; | |
2259 | ||
2260 | if (sym._n._n_n._n_zeroes == 0 | |
2261 | && *csectpp != NULL | |
2262 | && (! gc | |
2263 | || ((*csectpp)->flags & SEC_MARK) != 0 | |
2264 | || *csectpp == bfd_abs_section_ptr) | |
2265 | && bfd_coff_symname_in_debug (sub, &sym)) | |
2266 | { | |
2267 | char *name; | |
2268 | bfd_size_type indx; | |
2269 | ||
2270 | name = (char *) debug_contents + sym._n._n_n._n_offset; | |
2271 | indx = _bfd_stringtab_add (debug_strtab, name, true, true); | |
2272 | if (indx == (bfd_size_type) -1) | |
2273 | goto error_return; | |
2274 | *debug_index = indx; | |
2275 | } | |
2276 | ||
2277 | esym += (sym.n_numaux + 1) * symesz; | |
2278 | csectpp += sym.n_numaux + 1; | |
2279 | debug_index += sym.n_numaux + 1; | |
2280 | } | |
2281 | ||
aadf04f7 SS |
2282 | free (debug_contents); |
2283 | debug_contents = NULL; | |
28a0c103 ILT |
2284 | |
2285 | /* Clear the size of subdeb, so that it is not included directly | |
2286 | in the output file. */ | |
2287 | subdeb->_raw_size = 0; | |
2288 | ||
2289 | if (! info->keep_memory) | |
2290 | { | |
2291 | if (! _bfd_coff_free_symbols (sub)) | |
2292 | goto error_return; | |
2293 | } | |
aadf04f7 SS |
2294 | } |
2295 | ||
28a0c103 ILT |
2296 | xcoff_hash_table (info)->debug_section->_raw_size = |
2297 | _bfd_stringtab_size (debug_strtab); | |
2298 | ||
aadf04f7 SS |
2299 | return true; |
2300 | ||
2301 | error_return: | |
28a0c103 ILT |
2302 | if (ldinfo.strings != NULL) |
2303 | free (ldinfo.strings); | |
aadf04f7 SS |
2304 | if (debug_contents != NULL) |
2305 | free (debug_contents); | |
28a0c103 ILT |
2306 | return false; |
2307 | } | |
2308 | ||
2309 | /* The mark phase of garbage collection. For a given section, mark | |
867d923d ILT |
2310 | it, and all the sections which define symbols to which it refers. |
2311 | Because this function needs to look at the relocs, we also count | |
2312 | the number of relocs which need to be copied into the .loader | |
2313 | section. */ | |
28a0c103 ILT |
2314 | |
2315 | static boolean | |
2316 | xcoff_mark (info, sec) | |
2317 | struct bfd_link_info *info; | |
2318 | asection *sec; | |
2319 | { | |
2320 | if ((sec->flags & SEC_MARK) != 0) | |
2321 | return true; | |
2322 | ||
2323 | sec->flags |= SEC_MARK; | |
2324 | ||
2325 | if (sec->owner->xvec == info->hash->creator | |
2326 | && coff_section_data (sec->owner, sec) != NULL | |
2327 | && xcoff_section_data (sec->owner, sec) != NULL) | |
aadf04f7 | 2328 | { |
28a0c103 ILT |
2329 | register struct xcoff_link_hash_entry **hp, **hpend; |
2330 | struct internal_reloc *rel, *relend; | |
2331 | ||
2332 | /* Mark all the symbols in this section. */ | |
2333 | ||
2334 | hp = (obj_xcoff_sym_hashes (sec->owner) | |
2335 | + xcoff_section_data (sec->owner, sec)->first_symndx); | |
2336 | hpend = (obj_xcoff_sym_hashes (sec->owner) | |
2337 | + xcoff_section_data (sec->owner, sec)->last_symndx); | |
2338 | for (; hp < hpend; hp++) | |
2339 | { | |
2340 | register struct xcoff_link_hash_entry *h; | |
2341 | ||
2342 | h = *hp; | |
2343 | if (h != NULL | |
2344 | && (h->flags & XCOFF_MARK) == 0) | |
2345 | { | |
2346 | h->flags |= XCOFF_MARK; | |
2347 | if (h->root.type == bfd_link_hash_defined | |
2348 | || h->root.type == bfd_link_hash_defweak) | |
2349 | { | |
2350 | asection *hsec; | |
2351 | ||
2352 | hsec = h->root.u.def.section; | |
2353 | if ((hsec->flags & SEC_MARK) == 0) | |
2354 | { | |
2355 | if (! xcoff_mark (info, hsec)) | |
2356 | return false; | |
2357 | } | |
2358 | } | |
2359 | ||
2360 | if (h->toc_section != NULL | |
2361 | && (h->toc_section->flags & SEC_MARK) == 0) | |
2362 | { | |
2363 | if (! xcoff_mark (info, h->toc_section)) | |
2364 | return false; | |
2365 | } | |
2366 | } | |
2367 | } | |
2368 | ||
2369 | /* Look through the section relocs. */ | |
2370 | ||
afe07862 ILT |
2371 | if ((sec->flags & SEC_RELOC) != 0 |
2372 | && sec->reloc_count > 0) | |
aadf04f7 | 2373 | { |
afe07862 ILT |
2374 | rel = xcoff_read_internal_relocs (sec->owner, sec, true, |
2375 | (bfd_byte *) NULL, false, | |
2376 | (struct internal_reloc *) NULL); | |
2377 | if (rel == NULL) | |
2378 | return false; | |
2379 | relend = rel + sec->reloc_count; | |
2380 | for (; rel < relend; rel++) | |
2381 | { | |
2382 | asection *rsec; | |
2383 | struct xcoff_link_hash_entry *h; | |
28a0c103 | 2384 | |
afe07862 ILT |
2385 | if ((unsigned int) rel->r_symndx |
2386 | > obj_raw_syment_count (sec->owner)) | |
2387 | continue; | |
28a0c103 | 2388 | |
afe07862 ILT |
2389 | h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx]; |
2390 | if (h != NULL | |
2391 | && (h->flags & XCOFF_MARK) == 0) | |
28a0c103 | 2392 | { |
afe07862 ILT |
2393 | h->flags |= XCOFF_MARK; |
2394 | if (h->root.type == bfd_link_hash_defined | |
2395 | || h->root.type == bfd_link_hash_defweak) | |
2396 | { | |
2397 | asection *hsec; | |
28a0c103 | 2398 | |
afe07862 ILT |
2399 | hsec = h->root.u.def.section; |
2400 | if ((hsec->flags & SEC_MARK) == 0) | |
2401 | { | |
2402 | if (! xcoff_mark (info, hsec)) | |
2403 | return false; | |
2404 | } | |
2405 | } | |
2406 | ||
2407 | if (h->toc_section != NULL | |
2408 | && (h->toc_section->flags & SEC_MARK) == 0) | |
28a0c103 | 2409 | { |
afe07862 | 2410 | if (! xcoff_mark (info, h->toc_section)) |
28a0c103 ILT |
2411 | return false; |
2412 | } | |
2413 | } | |
2414 | ||
afe07862 ILT |
2415 | rsec = xcoff_data (sec->owner)->csects[rel->r_symndx]; |
2416 | if (rsec != NULL | |
2417 | && (rsec->flags & SEC_MARK) == 0) | |
28a0c103 | 2418 | { |
afe07862 | 2419 | if (! xcoff_mark (info, rsec)) |
28a0c103 ILT |
2420 | return false; |
2421 | } | |
867d923d ILT |
2422 | |
2423 | /* See if this reloc needs to be copied into the .loader | |
2424 | section. */ | |
2425 | switch (rel->r_type) | |
2426 | { | |
2427 | default: | |
2428 | if (h == NULL | |
2429 | || h->root.type == bfd_link_hash_defined | |
2430 | || h->root.type == bfd_link_hash_defweak | |
e1371ad1 ILT |
2431 | || h->root.type == bfd_link_hash_common |
2432 | || ((h->flags & XCOFF_CALLED) != 0 | |
2433 | && (h->flags & XCOFF_DEF_REGULAR) == 0 | |
2434 | && (h->flags & XCOFF_REF_DYNAMIC) != 0 | |
2435 | && (h->root.type == bfd_link_hash_undefined | |
2436 | || h->root.type == bfd_link_hash_undefweak) | |
2437 | && h->root.root.string[0] == '.')) | |
867d923d ILT |
2438 | break; |
2439 | /* Fall through. */ | |
2440 | case R_POS: | |
2441 | case R_NEG: | |
2442 | case R_RL: | |
2443 | case R_RLA: | |
2444 | ++xcoff_hash_table (info)->ldrel_count; | |
2445 | if (h != NULL) | |
2446 | h->flags |= XCOFF_LDREL; | |
2447 | break; | |
2448 | case R_TOC: | |
2449 | case R_GL: | |
2450 | case R_TCL: | |
2451 | case R_TRL: | |
2452 | case R_TRLA: | |
2453 | /* We should never need a .loader reloc for a TOC | |
2454 | relative reloc. */ | |
2455 | break; | |
2456 | } | |
28a0c103 ILT |
2457 | } |
2458 | ||
afe07862 ILT |
2459 | if (! info->keep_memory |
2460 | && coff_section_data (sec->owner, sec) != NULL | |
2461 | && coff_section_data (sec->owner, sec)->relocs != NULL | |
2462 | && ! coff_section_data (sec->owner, sec)->keep_relocs) | |
28a0c103 | 2463 | { |
afe07862 ILT |
2464 | free (coff_section_data (sec->owner, sec)->relocs); |
2465 | coff_section_data (sec->owner, sec)->relocs = NULL; | |
28a0c103 ILT |
2466 | } |
2467 | } | |
28a0c103 ILT |
2468 | } |
2469 | ||
2470 | return true; | |
2471 | } | |
2472 | ||
2473 | /* The sweep phase of garbage collection. Remove all garbage | |
2474 | sections. */ | |
2475 | ||
2476 | static void | |
2477 | xcoff_sweep (info) | |
2478 | struct bfd_link_info *info; | |
2479 | { | |
2480 | bfd *sub; | |
2481 | ||
2482 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
2483 | { | |
2484 | asection *o; | |
2485 | ||
2486 | for (o = sub->sections; o != NULL; o = o->next) | |
2487 | { | |
2488 | if ((o->flags & SEC_MARK) == 0) | |
2489 | { | |
2490 | /* Keep all sections from non-XCOFF input files. Keep | |
2491 | special sections. Keep .debug sections for the | |
2492 | moment. */ | |
2493 | if (sub->xvec != info->hash->creator | |
2494 | || o == xcoff_hash_table (info)->debug_section | |
2495 | || o == xcoff_hash_table (info)->loader_section | |
2496 | || o == xcoff_hash_table (info)->linkage_section | |
2497 | || o == xcoff_hash_table (info)->toc_section | |
2498 | || strcmp (o->name, ".debug") == 0) | |
2499 | o->flags |= SEC_MARK; | |
2500 | else | |
2501 | { | |
2502 | o->_raw_size = 0; | |
2503 | o->reloc_count = 0; | |
2504 | o->lineno_count = 0; | |
28a0c103 ILT |
2505 | } |
2506 | } | |
2507 | } | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | /* Add a symbol to the .loader symbols, if necessary. */ | |
2512 | ||
2513 | static boolean | |
2514 | xcoff_build_ldsyms (h, p) | |
2515 | struct xcoff_link_hash_entry *h; | |
2516 | PTR p; | |
2517 | { | |
2518 | struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p; | |
2519 | size_t len; | |
2520 | ||
2521 | /* We don't want to garbage collect symbols which are not defined in | |
2522 | XCOFF files. This is a convenient place to mark them. */ | |
2523 | if (xcoff_hash_table (ldinfo->info)->gc | |
2524 | && (h->flags & XCOFF_MARK) == 0 | |
2525 | && (h->root.type == bfd_link_hash_defined | |
2526 | || h->root.type == bfd_link_hash_defweak) | |
2527 | && (h->root.u.def.section->owner == NULL | |
2528 | || (h->root.u.def.section->owner->xvec | |
2529 | != ldinfo->info->hash->creator))) | |
2530 | h->flags |= XCOFF_MARK; | |
2531 | ||
2532 | /* If this symbol is called, and it is defined in a dynamic object, | |
2533 | then we need to set up global linkage code for it. (Unless we | |
2534 | did garbage collection and we didn't need this symbol.) */ | |
2535 | if ((h->flags & XCOFF_CALLED) != 0 | |
2536 | && (h->flags & XCOFF_DEF_REGULAR) == 0 | |
2537 | && (h->flags & XCOFF_REF_DYNAMIC) != 0 | |
2538 | && (h->root.type == bfd_link_hash_undefined | |
2539 | || h->root.type == bfd_link_hash_undefweak) | |
2540 | && h->root.root.string[0] == '.' | |
2541 | && (! xcoff_hash_table (ldinfo->info)->gc | |
2542 | || (h->flags & XCOFF_MARK) != 0)) | |
2543 | { | |
2544 | asection *sec; | |
2545 | struct xcoff_link_hash_entry *hds; | |
2546 | ||
2547 | sec = xcoff_hash_table (ldinfo->info)->linkage_section; | |
2548 | h->root.type = bfd_link_hash_defined; | |
2549 | h->root.u.def.section = sec; | |
2550 | h->root.u.def.value = sec->_raw_size; | |
2551 | h->smclas = XMC_GL; | |
2552 | sec->_raw_size += XCOFF_GLINK_SIZE; | |
2553 | ||
2554 | /* The global linkage code requires a TOC entry for the | |
2555 | descriptor. */ | |
2556 | hds = h->descriptor; | |
2557 | BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined | |
2558 | || hds->root.type == bfd_link_hash_undefweak) | |
2559 | && (hds->flags & XCOFF_DEF_REGULAR) == 0 | |
2560 | && (hds->flags & XCOFF_REF_DYNAMIC) != 0); | |
2561 | hds->flags |= XCOFF_MARK; | |
2562 | if (hds->toc_section == NULL) | |
2563 | { | |
2564 | hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section; | |
2565 | hds->toc_offset = hds->toc_section->_raw_size; | |
2566 | hds->toc_section->_raw_size += 4; | |
2567 | ++xcoff_hash_table (ldinfo->info)->ldrel_count; | |
2568 | ++hds->toc_section->reloc_count; | |
2569 | hds->indx = -2; | |
2570 | hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL; | |
2571 | ||
2572 | /* We need to call xcoff_build_ldsyms recursively here, | |
2573 | because we may already have passed hds on the traversal. */ | |
2574 | xcoff_build_ldsyms (hds, p); | |
2575 | } | |
2576 | } | |
2577 | ||
2578 | /* We need to add a symbol to the .loader section if it is mentioned | |
2579 | in a reloc which we are copying to the .loader section and it was | |
867d923d | 2580 | not defined or common, or if it is the entry point. */ |
28a0c103 ILT |
2581 | |
2582 | if (((h->flags & XCOFF_LDREL) == 0 | |
2583 | || h->root.type == bfd_link_hash_defined | |
867d923d ILT |
2584 | || h->root.type == bfd_link_hash_defweak |
2585 | || h->root.type == bfd_link_hash_common) | |
28a0c103 ILT |
2586 | && (h->flags & XCOFF_ENTRY) == 0) |
2587 | { | |
2588 | h->ldsym = NULL; | |
2589 | return true; | |
2590 | } | |
2591 | ||
2592 | /* We don't need to add this symbol if we did garbage collection and | |
2593 | we did not mark this symbol. */ | |
2594 | if (xcoff_hash_table (ldinfo->info)->gc | |
2595 | && (h->flags & XCOFF_MARK) == 0) | |
2596 | { | |
2597 | h->ldsym = NULL; | |
2598 | return true; | |
2599 | } | |
2600 | ||
2601 | /* We may have already processed this symbol due to the recursive | |
2602 | call above. */ | |
2603 | if ((h->flags & XCOFF_BUILT_LDSYM) != 0) | |
2604 | return true; | |
2605 | ||
2606 | /* We need to add this symbol to the .loader symbols. */ | |
2607 | ||
2608 | /* h->ldsym will already have been allocated for an explicitly | |
2609 | imported symbol. */ | |
2610 | if (h->ldsym == NULL) | |
2611 | { | |
2612 | h->ldsym = ((struct internal_ldsym *) | |
2613 | bfd_zalloc (ldinfo->output_bfd, | |
2614 | sizeof (struct internal_ldsym))); | |
2615 | if (h->ldsym == NULL) | |
2616 | { | |
2617 | ldinfo->failed = true; | |
2618 | bfd_set_error (bfd_error_no_memory); | |
2619 | return false; | |
2620 | } | |
2621 | } | |
2622 | ||
2623 | /* The first 3 symbol table indices are reserved to indicate the | |
2624 | sections. */ | |
2625 | h->ldindx = ldinfo->ldsym_count + 3; | |
2626 | ||
2627 | ++ldinfo->ldsym_count; | |
2628 | ||
2629 | len = strlen (h->root.root.string); | |
2630 | if (len <= SYMNMLEN) | |
2631 | strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN); | |
2632 | else | |
2633 | { | |
2634 | if (ldinfo->string_size + len + 3 > ldinfo->string_alc) | |
2635 | { | |
2636 | size_t newalc; | |
2637 | bfd_byte *newstrings; | |
2638 | ||
2639 | newalc = ldinfo->string_alc * 2; | |
2640 | if (newalc == 0) | |
2641 | newalc = 32; | |
2642 | while (ldinfo->string_size + len + 3 > newalc) | |
2643 | newalc *= 2; | |
2644 | ||
2645 | if (ldinfo->strings == NULL) | |
2646 | newstrings = (bfd_byte *) malloc (newalc); | |
2647 | else | |
2648 | newstrings = ((bfd_byte *) | |
2649 | realloc ((PTR) ldinfo->strings, newalc)); | |
2650 | if (newstrings == NULL) | |
2651 | { | |
2652 | ldinfo->failed = true; | |
2653 | bfd_set_error (bfd_error_no_memory); | |
2654 | return false; | |
2655 | } | |
2656 | ldinfo->string_alc = newalc; | |
2657 | ldinfo->strings = newstrings; | |
aadf04f7 | 2658 | } |
28a0c103 ILT |
2659 | |
2660 | bfd_put_16 (ldinfo->output_bfd, len + 1, | |
2661 | ldinfo->strings + ldinfo->string_size); | |
2662 | strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string); | |
2663 | h->ldsym->_l._l_l._l_zeroes = 0; | |
2664 | h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2; | |
2665 | ldinfo->string_size += len + 3; | |
aadf04f7 | 2666 | } |
28a0c103 ILT |
2667 | |
2668 | h->flags |= XCOFF_BUILT_LDSYM; | |
2669 | ||
2670 | return true; | |
aadf04f7 SS |
2671 | } |
2672 | \f | |
2673 | /* Do the final link step. */ | |
2674 | ||
2675 | boolean | |
2676 | _bfd_xcoff_bfd_final_link (abfd, info) | |
2677 | bfd *abfd; | |
2678 | struct bfd_link_info *info; | |
2679 | { | |
2680 | bfd_size_type symesz; | |
2681 | struct xcoff_final_link_info finfo; | |
2682 | asection *o; | |
2683 | struct bfd_link_order *p; | |
2684 | size_t max_contents_size; | |
2685 | size_t max_sym_count; | |
2686 | size_t max_lineno_count; | |
2687 | size_t max_reloc_count; | |
2688 | size_t max_output_reloc_count; | |
2689 | file_ptr rel_filepos; | |
2690 | unsigned int relsz; | |
2691 | file_ptr line_filepos; | |
2692 | unsigned int linesz; | |
2693 | bfd *sub; | |
2694 | bfd_byte *external_relocs = NULL; | |
2695 | char strbuf[STRING_SIZE_SIZE]; | |
2696 | ||
2697 | symesz = bfd_coff_symesz (abfd); | |
2698 | ||
2699 | finfo.info = info; | |
2700 | finfo.output_bfd = abfd; | |
2701 | finfo.strtab = NULL; | |
2702 | finfo.section_info = NULL; | |
2703 | finfo.last_file_index = -1; | |
2704 | finfo.toc_symindx = -1; | |
2705 | finfo.internal_syms = NULL; | |
2706 | finfo.sym_indices = NULL; | |
2707 | finfo.outsyms = NULL; | |
2708 | finfo.linenos = NULL; | |
2709 | finfo.contents = NULL; | |
2710 | finfo.external_relocs = NULL; | |
2711 | ||
28a0c103 ILT |
2712 | finfo.ldsym = ((struct external_ldsym *) |
2713 | (xcoff_hash_table (info)->loader_section->contents | |
2714 | + LDHDRSZ)); | |
2715 | finfo.ldrel = ((struct external_ldrel *) | |
2716 | (xcoff_hash_table (info)->loader_section->contents | |
2717 | + LDHDRSZ | |
2718 | + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ)); | |
2719 | ||
aadf04f7 | 2720 | xcoff_data (abfd)->coff.link_info = info; |
867d923d | 2721 | xcoff_data (abfd)->full_aouthdr = true; |
aadf04f7 SS |
2722 | |
2723 | finfo.strtab = _bfd_stringtab_init (); | |
2724 | if (finfo.strtab == NULL) | |
2725 | goto error_return; | |
2726 | ||
2727 | /* Compute the file positions for all the sections. */ | |
28a0c103 ILT |
2728 | if (abfd->output_has_begun) |
2729 | { | |
2730 | if (xcoff_hash_table (info)->file_align != 0) | |
2731 | abort (); | |
2732 | } | |
2733 | else | |
2734 | { | |
2735 | bfd_vma file_align; | |
2736 | ||
2737 | file_align = xcoff_hash_table (info)->file_align; | |
2738 | if (file_align != 0) | |
2739 | { | |
2740 | boolean saw_contents; | |
2741 | int indx; | |
2742 | asection **op; | |
2743 | file_ptr sofar; | |
2744 | ||
2745 | /* Insert .pad sections before every section which has | |
2746 | contents and is loaded, if it is preceded by some other | |
2747 | section which has contents and is loaded. */ | |
2748 | saw_contents = true; | |
2749 | for (op = &abfd->sections; *op != NULL; op = &(*op)->next) | |
2750 | { | |
2751 | (*op)->target_index = indx; | |
2752 | if (strcmp ((*op)->name, ".pad") == 0) | |
2753 | saw_contents = false; | |
2754 | else if (((*op)->flags & SEC_HAS_CONTENTS) != 0 | |
2755 | && ((*op)->flags & SEC_LOAD) != 0) | |
2756 | { | |
2757 | if (! saw_contents) | |
2758 | saw_contents = true; | |
2759 | else | |
2760 | { | |
2761 | asection *n, *hold; | |
2762 | ||
2763 | hold = *op; | |
2764 | *op = NULL; | |
2765 | n = bfd_make_section_anyway (abfd, ".pad"); | |
2766 | BFD_ASSERT (*op == n); | |
2767 | n->next = hold; | |
2768 | n->flags = SEC_HAS_CONTENTS; | |
2769 | n->alignment_power = 0; | |
2770 | saw_contents = false; | |
2771 | } | |
2772 | } | |
2773 | } | |
2774 | ||
2775 | /* Reset the section indices after inserting the new | |
2776 | sections. */ | |
2777 | indx = 0; | |
2778 | for (o = abfd->sections; o != NULL; o = o->next) | |
2779 | { | |
2780 | ++indx; | |
2781 | o->target_index = indx; | |
2782 | } | |
2783 | BFD_ASSERT ((unsigned int) indx == abfd->section_count); | |
2784 | ||
2785 | /* Work out appropriate sizes for the .pad sections to force | |
2786 | each section to land on a page boundary. This bit of | |
2787 | code knows what compute_section_file_positions is going | |
2788 | to do. */ | |
2789 | sofar = bfd_coff_filhsz (abfd); | |
867d923d | 2790 | sofar += bfd_coff_aoutsz (abfd); |
28a0c103 ILT |
2791 | sofar += abfd->section_count * bfd_coff_scnhsz (abfd); |
2792 | ||
2793 | for (o = abfd->sections; o != NULL; o = o->next) | |
2794 | { | |
2795 | if (strcmp (o->name, ".pad") == 0) | |
2796 | { | |
2797 | bfd_vma pageoff; | |
2798 | ||
2799 | BFD_ASSERT (o->_raw_size == 0); | |
2800 | pageoff = sofar & (file_align - 1); | |
2801 | if (pageoff != 0) | |
2802 | { | |
2803 | o->_raw_size = file_align - pageoff; | |
2804 | sofar += file_align - pageoff; | |
2805 | o->flags |= SEC_HAS_CONTENTS; | |
2806 | } | |
2807 | } | |
2808 | else | |
2809 | { | |
2810 | if ((o->flags & SEC_HAS_CONTENTS) != 0) | |
2811 | sofar += BFD_ALIGN (o->_raw_size, | |
2812 | 1 << o->alignment_power); | |
2813 | } | |
2814 | } | |
2815 | } | |
2816 | ||
2817 | bfd_coff_compute_section_file_positions (abfd); | |
2818 | } | |
aadf04f7 SS |
2819 | |
2820 | /* Count the line numbers and relocation entries required for the | |
2821 | output file. Set the file positions for the relocs. */ | |
2822 | rel_filepos = obj_relocbase (abfd); | |
2823 | relsz = bfd_coff_relsz (abfd); | |
2824 | max_contents_size = 0; | |
2825 | max_lineno_count = 0; | |
2826 | max_reloc_count = 0; | |
2827 | for (o = abfd->sections; o != NULL; o = o->next) | |
2828 | { | |
2829 | o->reloc_count = 0; | |
2830 | o->lineno_count = 0; | |
2831 | for (p = o->link_order_head; p != NULL; p = p->next) | |
2832 | { | |
2833 | if (p->type == bfd_indirect_link_order) | |
2834 | { | |
2835 | asection *sec; | |
2836 | ||
2837 | sec = p->u.indirect.section; | |
2838 | ||
2839 | if (info->strip == strip_none | |
2840 | || info->strip == strip_some) | |
2841 | o->lineno_count += sec->lineno_count; | |
2842 | ||
2843 | o->reloc_count += sec->reloc_count; | |
2844 | ||
2845 | if (sec->_raw_size > max_contents_size) | |
2846 | max_contents_size = sec->_raw_size; | |
2847 | if (sec->lineno_count > max_lineno_count) | |
2848 | max_lineno_count = sec->lineno_count; | |
b73322d9 ILT |
2849 | if (coff_section_data (sec->owner, sec) != NULL |
2850 | && xcoff_section_data (sec->owner, sec) != NULL | |
2851 | && (xcoff_section_data (sec->owner, sec)->lineno_count | |
2852 | > max_lineno_count)) | |
2853 | max_lineno_count = | |
2854 | xcoff_section_data (sec->owner, sec)->lineno_count; | |
aadf04f7 SS |
2855 | if (sec->reloc_count > max_reloc_count) |
2856 | max_reloc_count = sec->reloc_count; | |
2857 | } | |
2858 | else if (p->type == bfd_section_reloc_link_order | |
2859 | || p->type == bfd_symbol_reloc_link_order) | |
2860 | ++o->reloc_count; | |
2861 | } | |
2862 | if (o->reloc_count == 0) | |
2863 | o->rel_filepos = 0; | |
2864 | else | |
2865 | { | |
2866 | o->flags |= SEC_RELOC; | |
2867 | o->rel_filepos = rel_filepos; | |
2868 | rel_filepos += o->reloc_count * relsz; | |
2869 | } | |
2870 | } | |
2871 | ||
2872 | /* Allocate space for the pointers we need to keep for the relocs. */ | |
2873 | { | |
2874 | unsigned int i; | |
2875 | ||
2876 | /* We use section_count + 1, rather than section_count, because | |
2877 | the target_index fields are 1 based. */ | |
2878 | finfo.section_info = ((struct xcoff_link_section_info *) | |
2879 | malloc ((abfd->section_count + 1) | |
2880 | * sizeof (struct xcoff_link_section_info))); | |
2881 | if (finfo.section_info == NULL) | |
2882 | { | |
2883 | bfd_set_error (bfd_error_no_memory); | |
2884 | goto error_return; | |
2885 | } | |
2886 | for (i = 0; i <= abfd->section_count; i++) | |
2887 | { | |
2888 | finfo.section_info[i].relocs = NULL; | |
2889 | finfo.section_info[i].rel_hashes = NULL; | |
2890 | } | |
2891 | } | |
2892 | ||
2893 | /* We now know the size of the relocs, so we can determine the file | |
2894 | positions of the line numbers. */ | |
2895 | line_filepos = rel_filepos; | |
2896 | linesz = bfd_coff_linesz (abfd); | |
2897 | max_output_reloc_count = 0; | |
2898 | for (o = abfd->sections; o != NULL; o = o->next) | |
2899 | { | |
2900 | if (o->lineno_count == 0) | |
2901 | o->line_filepos = 0; | |
2902 | else | |
2903 | { | |
2904 | o->line_filepos = line_filepos; | |
2905 | line_filepos += o->lineno_count * linesz; | |
2906 | } | |
2907 | ||
2908 | if (o->reloc_count != 0) | |
2909 | { | |
2910 | /* We don't know the indices of global symbols until we have | |
2911 | written out all the local symbols. For each section in | |
2912 | the output file, we keep an array of pointers to hash | |
2913 | table entries. Each entry in the array corresponds to a | |
2914 | reloc. When we find a reloc against a global symbol, we | |
2915 | set the corresponding entry in this array so that we can | |
2916 | fix up the symbol index after we have written out all the | |
2917 | local symbols. | |
2918 | ||
2919 | Because of this problem, we also keep the relocs in | |
2920 | memory until the end of the link. This wastes memory. | |
2921 | We could backpatch the file later, I suppose, although it | |
2922 | would be slow. */ | |
2923 | finfo.section_info[o->target_index].relocs = | |
2924 | ((struct internal_reloc *) | |
2925 | malloc (o->reloc_count * sizeof (struct internal_reloc))); | |
2926 | finfo.section_info[o->target_index].rel_hashes = | |
2927 | ((struct xcoff_link_hash_entry **) | |
2928 | malloc (o->reloc_count | |
2929 | * sizeof (struct xcoff_link_hash_entry *))); | |
2930 | if (finfo.section_info[o->target_index].relocs == NULL | |
2931 | || finfo.section_info[o->target_index].rel_hashes == NULL) | |
2932 | { | |
2933 | bfd_set_error (bfd_error_no_memory); | |
2934 | goto error_return; | |
2935 | } | |
2936 | ||
2937 | if (o->reloc_count > max_output_reloc_count) | |
2938 | max_output_reloc_count = o->reloc_count; | |
2939 | } | |
2940 | ||
2941 | /* Reset the reloc and lineno counts, so that we can use them to | |
2942 | count the number of entries we have output so far. */ | |
2943 | o->reloc_count = 0; | |
2944 | o->lineno_count = 0; | |
2945 | } | |
2946 | ||
2947 | obj_sym_filepos (abfd) = line_filepos; | |
2948 | ||
2949 | /* Figure out the largest number of symbols in an input BFD. Take | |
2950 | the opportunity to clear the output_has_begun fields of all the | |
2951 | input BFD's. We want at least 4 symbols, since that is the | |
2952 | number which xcoff_write_global_symbol may need. */ | |
2953 | max_sym_count = 4; | |
2954 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
2955 | { | |
2956 | size_t sz; | |
2957 | ||
2958 | sub->output_has_begun = false; | |
2959 | sz = obj_raw_syment_count (sub); | |
2960 | if (sz > max_sym_count) | |
2961 | max_sym_count = sz; | |
2962 | } | |
2963 | ||
2964 | /* Allocate some buffers used while linking. */ | |
2965 | finfo.internal_syms = ((struct internal_syment *) | |
2966 | malloc (max_sym_count | |
2967 | * sizeof (struct internal_syment))); | |
2968 | finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long)); | |
2969 | finfo.outsyms = ((bfd_byte *) | |
2970 | malloc ((size_t) ((max_sym_count + 1) * symesz))); | |
2971 | finfo.linenos = (bfd_byte *) malloc (max_lineno_count | |
2972 | * bfd_coff_linesz (abfd)); | |
2973 | finfo.contents = (bfd_byte *) malloc (max_contents_size); | |
2974 | finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz); | |
2975 | if ((finfo.internal_syms == NULL && max_sym_count > 0) | |
2976 | || (finfo.sym_indices == NULL && max_sym_count > 0) | |
2977 | || finfo.outsyms == NULL | |
2978 | || (finfo.linenos == NULL && max_lineno_count > 0) | |
2979 | || (finfo.contents == NULL && max_contents_size > 0) | |
2980 | || (finfo.external_relocs == NULL && max_reloc_count > 0)) | |
2981 | { | |
2982 | bfd_set_error (bfd_error_no_memory); | |
2983 | goto error_return; | |
2984 | } | |
2985 | ||
2986 | obj_raw_syment_count (abfd) = 0; | |
2987 | xcoff_data (abfd)->toc = (bfd_vma) -1; | |
2988 | ||
aadf04f7 SS |
2989 | /* We now know the position of everything in the file, except that |
2990 | we don't know the size of the symbol table and therefore we don't | |
2991 | know where the string table starts. We just build the string | |
2992 | table in memory as we go along. We process all the relocations | |
2993 | for a single input file at once. */ | |
2994 | for (o = abfd->sections; o != NULL; o = o->next) | |
2995 | { | |
2996 | for (p = o->link_order_head; p != NULL; p = p->next) | |
2997 | { | |
2998 | if (p->type == bfd_indirect_link_order | |
2999 | && p->u.indirect.section->owner->xvec == abfd->xvec) | |
3000 | { | |
3001 | sub = p->u.indirect.section->owner; | |
3002 | if (! sub->output_has_begun) | |
3003 | { | |
3004 | if (! xcoff_link_input_bfd (&finfo, sub)) | |
3005 | goto error_return; | |
3006 | sub->output_has_begun = true; | |
3007 | } | |
3008 | } | |
3009 | else if (p->type == bfd_section_reloc_link_order | |
3010 | || p->type == bfd_symbol_reloc_link_order) | |
3011 | { | |
3012 | if (! xcoff_reloc_link_order (abfd, &finfo, o, p)) | |
3013 | goto error_return; | |
3014 | } | |
3015 | else | |
3016 | { | |
3017 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
3018 | goto error_return; | |
3019 | } | |
3020 | } | |
3021 | } | |
3022 | ||
3023 | /* Free up the buffers used by xcoff_link_input_bfd. */ | |
3024 | ||
3025 | if (finfo.internal_syms != NULL) | |
3026 | { | |
3027 | free (finfo.internal_syms); | |
3028 | finfo.internal_syms = NULL; | |
3029 | } | |
3030 | if (finfo.sym_indices != NULL) | |
3031 | { | |
3032 | free (finfo.sym_indices); | |
3033 | finfo.sym_indices = NULL; | |
3034 | } | |
3035 | if (finfo.linenos != NULL) | |
3036 | { | |
3037 | free (finfo.linenos); | |
3038 | finfo.linenos = NULL; | |
3039 | } | |
3040 | if (finfo.contents != NULL) | |
3041 | { | |
3042 | free (finfo.contents); | |
3043 | finfo.contents = NULL; | |
3044 | } | |
3045 | if (finfo.external_relocs != NULL) | |
3046 | { | |
3047 | free (finfo.external_relocs); | |
3048 | finfo.external_relocs = NULL; | |
3049 | } | |
3050 | ||
3051 | /* The value of the last C_FILE symbol is supposed to be -1. Write | |
3052 | it out again. */ | |
3053 | if (finfo.last_file_index != -1) | |
3054 | { | |
3055 | finfo.last_file.n_value = -1; | |
3056 | bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file, | |
3057 | (PTR) finfo.outsyms); | |
3058 | if (bfd_seek (abfd, | |
3059 | (obj_sym_filepos (abfd) | |
3060 | + finfo.last_file_index * symesz), | |
3061 | SEEK_SET) != 0 | |
3062 | || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz) | |
3063 | goto error_return; | |
3064 | } | |
3065 | ||
28a0c103 ILT |
3066 | /* Write out all the global symbols which do not come from XCOFF |
3067 | input files. */ | |
3068 | xcoff_link_hash_traverse (xcoff_hash_table (info), | |
3069 | xcoff_write_global_symbol, | |
3070 | (PTR) &finfo); | |
3071 | ||
aadf04f7 SS |
3072 | if (finfo.outsyms != NULL) |
3073 | { | |
3074 | free (finfo.outsyms); | |
3075 | finfo.outsyms = NULL; | |
3076 | } | |
3077 | ||
3078 | /* Now that we have written out all the global symbols, we know the | |
3079 | symbol indices to use for relocs against them, and we can finally | |
3080 | write out the relocs. */ | |
3081 | external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz); | |
3082 | if (external_relocs == NULL && max_output_reloc_count != 0) | |
3083 | { | |
3084 | bfd_set_error (bfd_error_no_memory); | |
3085 | goto error_return; | |
3086 | } | |
3087 | ||
3088 | for (o = abfd->sections; o != NULL; o = o->next) | |
3089 | { | |
3090 | struct internal_reloc *irel; | |
3091 | struct internal_reloc *irelend; | |
3092 | struct xcoff_link_hash_entry **rel_hash; | |
3093 | bfd_byte *erel; | |
3094 | ||
3095 | if (o->reloc_count == 0) | |
3096 | continue; | |
3097 | ||
aadf04f7 SS |
3098 | irel = finfo.section_info[o->target_index].relocs; |
3099 | irelend = irel + o->reloc_count; | |
3100 | rel_hash = finfo.section_info[o->target_index].rel_hashes; | |
aadf04f7 SS |
3101 | for (; irel < irelend; irel++, rel_hash++, erel += relsz) |
3102 | { | |
3103 | if (*rel_hash != NULL) | |
3104 | { | |
3105 | if ((*rel_hash)->indx < 0) | |
3106 | { | |
3107 | if (! ((*info->callbacks->unattached_reloc) | |
28a0c103 ILT |
3108 | (info, (*rel_hash)->root.root.string, |
3109 | (bfd *) NULL, o, irel->r_vaddr))) | |
aadf04f7 SS |
3110 | goto error_return; |
3111 | (*rel_hash)->indx = 0; | |
3112 | } | |
3113 | irel->r_symndx = (*rel_hash)->indx; | |
3114 | } | |
aadf04f7 SS |
3115 | } |
3116 | ||
28a0c103 ILT |
3117 | /* XCOFF requires that the relocs be sorted by address. We tend |
3118 | to produce them in the order in which their containing csects | |
3119 | appear in the symbol table, which is not necessarily by | |
3120 | address. So we sort them here. There may be a better way to | |
3121 | do this. */ | |
3122 | qsort ((PTR) finfo.section_info[o->target_index].relocs, | |
3123 | o->reloc_count, sizeof (struct internal_reloc), | |
3124 | xcoff_sort_relocs); | |
3125 | ||
3126 | irel = finfo.section_info[o->target_index].relocs; | |
3127 | irelend = irel + o->reloc_count; | |
3128 | erel = external_relocs; | |
3129 | for (; irel < irelend; irel++, rel_hash++, erel += relsz) | |
3130 | bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel); | |
3131 | ||
aadf04f7 SS |
3132 | if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0 |
3133 | || bfd_write ((PTR) external_relocs, relsz, o->reloc_count, | |
3134 | abfd) != relsz * o->reloc_count) | |
3135 | goto error_return; | |
3136 | } | |
3137 | ||
3138 | if (external_relocs != NULL) | |
3139 | { | |
3140 | free (external_relocs); | |
3141 | external_relocs = NULL; | |
3142 | } | |
3143 | ||
3144 | /* Free up the section information. */ | |
3145 | if (finfo.section_info != NULL) | |
3146 | { | |
3147 | unsigned int i; | |
3148 | ||
3149 | for (i = 0; i < abfd->section_count; i++) | |
3150 | { | |
3151 | if (finfo.section_info[i].relocs != NULL) | |
3152 | free (finfo.section_info[i].relocs); | |
3153 | if (finfo.section_info[i].rel_hashes != NULL) | |
3154 | free (finfo.section_info[i].rel_hashes); | |
3155 | } | |
3156 | free (finfo.section_info); | |
3157 | finfo.section_info = NULL; | |
3158 | } | |
3159 | ||
28a0c103 ILT |
3160 | /* Write out the loader section contents. */ |
3161 | BFD_ASSERT ((bfd_byte *) finfo.ldrel | |
3162 | == (xcoff_hash_table (info)->loader_section->contents | |
3163 | + xcoff_hash_table (info)->ldhdr.l_impoff)); | |
3164 | o = xcoff_hash_table (info)->loader_section; | |
3165 | if (! bfd_set_section_contents (abfd, o->output_section, | |
3166 | o->contents, o->output_offset, | |
3167 | o->_raw_size)) | |
3168 | goto error_return; | |
3169 | ||
3170 | /* Write out the global linkage section and the toc section. */ | |
3171 | o = xcoff_hash_table (info)->linkage_section; | |
3172 | if (o->_raw_size > 0 | |
3173 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, | |
3174 | o->output_offset, o->_raw_size)) | |
3175 | goto error_return; | |
3176 | o = xcoff_hash_table (info)->toc_section; | |
3177 | if (o->_raw_size > 0 | |
3178 | && ! bfd_set_section_contents (abfd, o->output_section, o->contents, | |
3179 | o->output_offset, o->_raw_size)) | |
3180 | goto error_return; | |
3181 | ||
aadf04f7 SS |
3182 | /* Write out the string table. */ |
3183 | if (bfd_seek (abfd, | |
3184 | (obj_sym_filepos (abfd) | |
3185 | + obj_raw_syment_count (abfd) * symesz), | |
3186 | SEEK_SET) != 0) | |
3187 | goto error_return; | |
3188 | bfd_h_put_32 (abfd, | |
3189 | _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE, | |
3190 | (bfd_byte *) strbuf); | |
3191 | if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE) | |
3192 | goto error_return; | |
3193 | if (! _bfd_stringtab_emit (abfd, finfo.strtab)) | |
3194 | goto error_return; | |
3195 | ||
3196 | _bfd_stringtab_free (finfo.strtab); | |
3197 | ||
3198 | /* Write out the debugging string table. */ | |
3199 | o = xcoff_hash_table (info)->debug_section; | |
3200 | if (o != NULL) | |
3201 | { | |
3202 | struct bfd_strtab_hash *debug_strtab; | |
3203 | ||
3204 | debug_strtab = xcoff_hash_table (info)->debug_strtab; | |
3205 | BFD_ASSERT (o->output_section->_raw_size - o->output_offset | |
3206 | >= _bfd_stringtab_size (debug_strtab)); | |
3207 | if (bfd_seek (abfd, | |
3208 | o->output_section->filepos + o->output_offset, | |
3209 | SEEK_SET) != 0) | |
3210 | goto error_return; | |
3211 | if (! _bfd_stringtab_emit (abfd, debug_strtab)) | |
3212 | goto error_return; | |
3213 | } | |
3214 | ||
3215 | /* Setting bfd_get_symcount to 0 will cause write_object_contents to | |
3216 | not try to write out the symbols. */ | |
3217 | bfd_get_symcount (abfd) = 0; | |
3218 | ||
3219 | return true; | |
3220 | ||
3221 | error_return: | |
3222 | if (finfo.strtab != NULL) | |
3223 | _bfd_stringtab_free (finfo.strtab); | |
3224 | if (finfo.section_info != NULL) | |
3225 | { | |
3226 | unsigned int i; | |
3227 | ||
3228 | for (i = 0; i < abfd->section_count; i++) | |
3229 | { | |
3230 | if (finfo.section_info[i].relocs != NULL) | |
3231 | free (finfo.section_info[i].relocs); | |
3232 | if (finfo.section_info[i].rel_hashes != NULL) | |
3233 | free (finfo.section_info[i].rel_hashes); | |
3234 | } | |
3235 | free (finfo.section_info); | |
3236 | } | |
3237 | if (finfo.internal_syms != NULL) | |
3238 | free (finfo.internal_syms); | |
3239 | if (finfo.sym_indices != NULL) | |
3240 | free (finfo.sym_indices); | |
3241 | if (finfo.outsyms != NULL) | |
3242 | free (finfo.outsyms); | |
3243 | if (finfo.linenos != NULL) | |
3244 | free (finfo.linenos); | |
3245 | if (finfo.contents != NULL) | |
3246 | free (finfo.contents); | |
3247 | if (finfo.external_relocs != NULL) | |
3248 | free (finfo.external_relocs); | |
3249 | if (external_relocs != NULL) | |
3250 | free (external_relocs); | |
3251 | return false; | |
3252 | } | |
3253 | ||
3254 | /* Link an input file into the linker output file. This function | |
3255 | handles all the sections and relocations of the input file at once. */ | |
3256 | ||
3257 | static boolean | |
3258 | xcoff_link_input_bfd (finfo, input_bfd) | |
3259 | struct xcoff_final_link_info *finfo; | |
3260 | bfd *input_bfd; | |
3261 | { | |
3262 | bfd *output_bfd; | |
3263 | const char *strings; | |
3264 | bfd_size_type syment_base; | |
3265 | unsigned int n_tmask; | |
3266 | unsigned int n_btshft; | |
3267 | boolean copy, hash; | |
3268 | bfd_size_type isymesz; | |
3269 | bfd_size_type osymesz; | |
3270 | bfd_size_type linesz; | |
3271 | bfd_byte *esym; | |
3272 | bfd_byte *esym_end; | |
b73322d9 | 3273 | struct xcoff_link_hash_entry **sym_hash; |
aadf04f7 SS |
3274 | struct internal_syment *isymp; |
3275 | asection **csectpp; | |
3276 | unsigned long *debug_index; | |
3277 | long *indexp; | |
3278 | unsigned long output_index; | |
3279 | bfd_byte *outsym; | |
b73322d9 | 3280 | asection *oline; |
aadf04f7 SS |
3281 | boolean keep_syms; |
3282 | asection *o; | |
3283 | ||
28a0c103 ILT |
3284 | /* We can just skip DYNAMIC files, unless this is a static link. */ |
3285 | if ((input_bfd->flags & DYNAMIC) != 0 | |
3286 | && ! finfo->info->static_link) | |
3287 | return true; | |
3288 | ||
aadf04f7 SS |
3289 | /* Move all the symbols to the output file. */ |
3290 | ||
3291 | output_bfd = finfo->output_bfd; | |
3292 | strings = NULL; | |
3293 | syment_base = obj_raw_syment_count (output_bfd); | |
3294 | isymesz = bfd_coff_symesz (input_bfd); | |
3295 | osymesz = bfd_coff_symesz (output_bfd); | |
3296 | linesz = bfd_coff_linesz (input_bfd); | |
3297 | BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd)); | |
3298 | ||
3299 | n_tmask = coff_data (input_bfd)->local_n_tmask; | |
3300 | n_btshft = coff_data (input_bfd)->local_n_btshft; | |
3301 | ||
3302 | /* Define macros so that ISFCN, et. al., macros work correctly. */ | |
3303 | #define N_TMASK n_tmask | |
3304 | #define N_BTSHFT n_btshft | |
3305 | ||
3306 | copy = false; | |
3307 | if (! finfo->info->keep_memory) | |
3308 | copy = true; | |
3309 | hash = true; | |
3310 | if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0) | |
3311 | hash = false; | |
3312 | ||
3313 | if (! _bfd_coff_get_external_symbols (input_bfd)) | |
3314 | return false; | |
3315 | ||
3316 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); | |
3317 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
3318 | sym_hash = obj_xcoff_sym_hashes (input_bfd); | |
3319 | csectpp = xcoff_data (input_bfd)->csects; | |
3320 | debug_index = xcoff_data (input_bfd)->debug_indices; | |
3321 | isymp = finfo->internal_syms; | |
3322 | indexp = finfo->sym_indices; | |
3323 | output_index = syment_base; | |
3324 | outsym = finfo->outsyms; | |
b73322d9 | 3325 | oline = NULL; |
aadf04f7 SS |
3326 | |
3327 | while (esym < esym_end) | |
3328 | { | |
3329 | struct internal_syment isym; | |
3330 | union internal_auxent aux; | |
3331 | int smtyp = 0; | |
3332 | boolean skip; | |
3333 | boolean require; | |
3334 | int add; | |
3335 | ||
3336 | bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp); | |
3337 | ||
3338 | /* If this is a C_EXT or C_HIDEXT symbol, we need the csect | |
3339 | information. */ | |
3340 | if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT) | |
3341 | { | |
3342 | BFD_ASSERT (isymp->n_numaux > 0); | |
3343 | bfd_coff_swap_aux_in (input_bfd, | |
3344 | (PTR) (esym + isymesz * isymp->n_numaux), | |
3345 | isymp->n_type, isymp->n_sclass, | |
3346 | isymp->n_numaux - 1, isymp->n_numaux, | |
3347 | (PTR) &aux); | |
3348 | smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); | |
3349 | } | |
3350 | ||
3351 | /* Make a copy of *isymp so that the relocate_section function | |
3352 | always sees the original values. This is more reliable than | |
3353 | always recomputing the symbol value even if we are stripping | |
3354 | the symbol. */ | |
3355 | isym = *isymp; | |
3356 | ||
28a0c103 ILT |
3357 | /* If this symbol is in the .loader section, swap out the |
3358 | .loader symbol information. If this is an external symbol | |
3359 | reference to a defined symbol, though, then wait until we get | |
3360 | to the definition. */ | |
3361 | if (isym.n_sclass == C_EXT | |
3362 | && *sym_hash != NULL | |
3363 | && (*sym_hash)->ldsym != NULL | |
3364 | && (smtyp != XTY_ER | |
3365 | || (*sym_hash)->root.type == bfd_link_hash_undefined)) | |
3366 | { | |
3367 | struct xcoff_link_hash_entry *h; | |
3368 | struct internal_ldsym *ldsym; | |
3369 | ||
3370 | h = *sym_hash; | |
3371 | ldsym = h->ldsym; | |
3372 | if (isym.n_scnum > 0) | |
3373 | { | |
3374 | ldsym->l_scnum = (*csectpp)->output_section->target_index; | |
3375 | ldsym->l_value = (isym.n_value | |
3376 | + (*csectpp)->output_section->vma | |
3377 | + (*csectpp)->output_offset | |
3378 | - (*csectpp)->vma); | |
3379 | } | |
3380 | else | |
3381 | { | |
3382 | ldsym->l_scnum = isym.n_scnum; | |
3383 | ldsym->l_value = isym.n_value; | |
3384 | } | |
3385 | ||
3386 | ldsym->l_smtype = smtyp; | |
3387 | if (((h->flags & XCOFF_DEF_REGULAR) == 0 | |
3388 | && (h->flags & XCOFF_REF_DYNAMIC) != 0) | |
3389 | || (h->flags & XCOFF_IMPORT) != 0) | |
3390 | ldsym->l_smtype |= L_IMPORT; | |
3391 | if (((h->flags & XCOFF_DEF_REGULAR) != 0 | |
3392 | && (h->flags & XCOFF_REF_DYNAMIC) != 0) | |
3393 | || (h->flags & XCOFF_EXPORT) != 0) | |
3394 | ldsym->l_smtype |= L_EXPORT; | |
3395 | if ((h->flags & XCOFF_ENTRY) != 0) | |
3396 | ldsym->l_smtype |= L_ENTRY; | |
3397 | ||
3398 | ldsym->l_smclas = aux.x_csect.x_smclas; | |
3399 | ||
3400 | if (ldsym->l_ifile == (bfd_size_type) -1) | |
3401 | ldsym->l_ifile = 0; | |
3402 | else if (ldsym->l_ifile == 0) | |
3403 | { | |
3404 | if ((ldsym->l_smtype & L_IMPORT) == 0) | |
3405 | ldsym->l_ifile = 0; | |
3406 | else | |
3407 | { | |
3408 | bfd *impbfd; | |
3409 | ||
3410 | if (h->root.type == bfd_link_hash_defined | |
3411 | || h->root.type == bfd_link_hash_defweak) | |
3412 | impbfd = h->root.u.def.section->owner; | |
3413 | else if (h->root.type == bfd_link_hash_undefined | |
3414 | || h->root.type == bfd_link_hash_undefweak) | |
3415 | impbfd = h->root.u.undef.abfd; | |
3416 | else | |
3417 | impbfd = NULL; | |
3418 | ||
3419 | if (impbfd == NULL) | |
3420 | ldsym->l_ifile = 0; | |
3421 | else | |
3422 | { | |
3423 | BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec); | |
3424 | ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; | |
3425 | } | |
3426 | } | |
3427 | } | |
3428 | ||
3429 | ldsym->l_parm = 0; | |
3430 | ||
3431 | BFD_ASSERT (h->ldindx >= 0); | |
3432 | BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym)); | |
3433 | xcoff_swap_ldsym_out (finfo->output_bfd, ldsym, | |
3434 | finfo->ldsym + h->ldindx - 3); | |
3435 | h->ldsym = NULL; | |
3436 | } | |
3437 | ||
aadf04f7 SS |
3438 | *indexp = -1; |
3439 | ||
3440 | skip = false; | |
3441 | require = false; | |
3442 | add = 1 + isym.n_numaux; | |
3443 | ||
3444 | /* If we are skipping this csect, we want to skip this symbol. */ | |
3445 | if (*csectpp == NULL) | |
3446 | skip = true; | |
3447 | ||
28a0c103 ILT |
3448 | /* If we garbage collected this csect, we want to skip this |
3449 | symbol. */ | |
3450 | if (! skip | |
3451 | && xcoff_hash_table (finfo->info)->gc | |
3452 | && ((*csectpp)->flags & SEC_MARK) == 0 | |
3453 | && *csectpp != bfd_abs_section_ptr) | |
3454 | skip = true; | |
3455 | ||
aadf04f7 SS |
3456 | /* An XCOFF linker always skips C_STAT symbols. */ |
3457 | if (! skip | |
3458 | && isymp->n_sclass == C_STAT) | |
3459 | skip = true; | |
3460 | ||
3461 | /* We skip all but the first TOC anchor. */ | |
3462 | if (! skip | |
3463 | && isymp->n_sclass == C_HIDEXT | |
3464 | && aux.x_csect.x_smclas == XMC_TC0) | |
3465 | { | |
3466 | if (finfo->toc_symindx != -1) | |
3467 | skip = true; | |
3468 | else | |
3469 | { | |
3470 | finfo->toc_symindx = output_index; | |
3471 | xcoff_data (finfo->output_bfd)->toc = | |
3472 | ((*csectpp)->output_section->vma | |
3473 | + (*csectpp)->output_offset | |
3474 | + isym.n_value | |
3475 | - (*csectpp)->vma); | |
867d923d ILT |
3476 | xcoff_data (finfo->output_bfd)->toc_section = |
3477 | (*csectpp)->output_section; | |
aadf04f7 SS |
3478 | require = true; |
3479 | } | |
3480 | } | |
3481 | ||
3482 | /* If we are stripping all symbols, we want to skip this one. */ | |
3483 | if (! skip | |
3484 | && finfo->info->strip == strip_all) | |
3485 | skip = true; | |
3486 | ||
3487 | /* We can skip resolved external references. */ | |
3488 | if (! skip | |
3489 | && isym.n_sclass == C_EXT | |
3490 | && smtyp == XTY_ER | |
3491 | && (*sym_hash)->root.type != bfd_link_hash_undefined) | |
3492 | skip = true; | |
3493 | ||
28a0c103 ILT |
3494 | /* We can skip common symbols if they got defined somewhere |
3495 | else. */ | |
3496 | if (! skip | |
3497 | && isym.n_sclass == C_EXT | |
3498 | && smtyp == XTY_CM | |
867d923d | 3499 | && (*sym_hash)->root.type != bfd_link_hash_common) |
28a0c103 ILT |
3500 | skip = true; |
3501 | ||
aadf04f7 SS |
3502 | /* Skip local symbols if we are discarding them. */ |
3503 | if (! skip | |
3504 | && finfo->info->discard == discard_all | |
3505 | && isym.n_sclass != C_EXT | |
3506 | && (isym.n_sclass != C_HIDEXT | |
3507 | || smtyp != XTY_SD)) | |
3508 | skip = true; | |
3509 | ||
3510 | /* If we stripping debugging symbols, and this is a debugging | |
3511 | symbol, then skip it. */ | |
3512 | if (! skip | |
3513 | && finfo->info->strip == strip_debugger | |
3514 | && isym.n_scnum == N_DEBUG) | |
3515 | skip = true; | |
3516 | ||
3517 | /* If some symbols are stripped based on the name, work out the | |
3518 | name and decide whether to skip this symbol. We don't handle | |
3519 | this correctly for symbols whose names are in the .debug | |
3520 | section; to get it right we would need a new bfd_strtab_hash | |
3521 | function to return the string given the index. */ | |
3522 | if (! skip | |
3523 | && (finfo->info->strip == strip_some | |
3524 | || finfo->info->discard == discard_l) | |
3525 | && (debug_index == NULL || *debug_index == (unsigned long) -1)) | |
3526 | { | |
3527 | const char *name; | |
3528 | char buf[SYMNMLEN + 1]; | |
3529 | ||
3530 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf); | |
3531 | if (name == NULL) | |
3532 | return false; | |
3533 | ||
3534 | if ((finfo->info->strip == strip_some | |
3535 | && (bfd_hash_lookup (finfo->info->keep_hash, name, false, | |
3536 | false) == NULL)) | |
3537 | || (finfo->info->discard == discard_l | |
3538 | && (isym.n_sclass != C_EXT | |
3539 | && (isym.n_sclass != C_HIDEXT | |
3540 | || smtyp != XTY_SD)) | |
3541 | && strncmp (name, finfo->info->lprefix, | |
3542 | finfo->info->lprefix_len) == 0)) | |
3543 | skip = true; | |
3544 | } | |
3545 | ||
3546 | /* On the other hand, we can't skip global symbols which have | |
28a0c103 | 3547 | relocs against them. */ |
aadf04f7 | 3548 | if (skip |
28a0c103 | 3549 | && isym.n_sclass == C_EXT |
aadf04f7 SS |
3550 | && (*sym_hash)->indx == -2 |
3551 | && finfo->info->strip != strip_all) | |
3552 | skip = false; | |
3553 | ||
3554 | /* We can not skip the first TOC anchor. */ | |
3555 | if (skip | |
3556 | && require | |
3557 | && finfo->info->strip != strip_all) | |
3558 | skip = false; | |
3559 | ||
3560 | /* We now know whether we are to skip this symbol or not. */ | |
3561 | if (! skip) | |
3562 | { | |
3563 | /* Adjust the symbol in order to output it. */ | |
3564 | ||
3565 | if (isym._n._n_n._n_zeroes == 0 | |
3566 | && isym._n._n_n._n_offset != 0) | |
3567 | { | |
3568 | /* This symbol has a long name. Enter it in the string | |
3569 | table we are building. If *debug_index != -1, the | |
3570 | name has already been entered in the .debug section. */ | |
3571 | if (debug_index != NULL && *debug_index != (unsigned long) -1) | |
3572 | isym._n._n_n._n_offset = *debug_index; | |
3573 | else | |
3574 | { | |
3575 | const char *name; | |
3576 | bfd_size_type indx; | |
3577 | ||
3578 | name = _bfd_coff_internal_syment_name (input_bfd, &isym, | |
3579 | (char *) NULL); | |
3580 | if (name == NULL) | |
3581 | return false; | |
3582 | indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy); | |
3583 | if (indx == (bfd_size_type) -1) | |
3584 | return false; | |
3585 | isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; | |
3586 | } | |
3587 | } | |
3588 | ||
867d923d ILT |
3589 | if (isym.n_sclass != C_BSTAT |
3590 | && isym.n_sclass != C_ESTAT | |
3591 | && isym.n_scnum > 0) | |
aadf04f7 SS |
3592 | { |
3593 | isym.n_scnum = (*csectpp)->output_section->target_index; | |
3594 | isym.n_value += ((*csectpp)->output_section->vma | |
3595 | + (*csectpp)->output_offset | |
3596 | - (*csectpp)->vma); | |
3597 | } | |
3598 | ||
3599 | /* The value of a C_FILE symbol is the symbol index of the | |
3600 | next C_FILE symbol. The value of the last C_FILE symbol | |
3601 | is -1. We try to get this right, below, just before we | |
3602 | write the symbols out, but in the general case we may | |
3603 | have to write the symbol out twice. */ | |
3604 | if (isym.n_sclass == C_FILE) | |
3605 | { | |
3606 | if (finfo->last_file_index != -1 | |
3607 | && finfo->last_file.n_value != (long) output_index) | |
3608 | { | |
3609 | /* We must correct the value of the last C_FILE entry. */ | |
3610 | finfo->last_file.n_value = output_index; | |
3611 | if ((bfd_size_type) finfo->last_file_index >= syment_base) | |
3612 | { | |
3613 | /* The last C_FILE symbol is in this input file. */ | |
3614 | bfd_coff_swap_sym_out (output_bfd, | |
3615 | (PTR) &finfo->last_file, | |
3616 | (PTR) (finfo->outsyms | |
3617 | + ((finfo->last_file_index | |
3618 | - syment_base) | |
3619 | * osymesz))); | |
3620 | } | |
3621 | else | |
3622 | { | |
3623 | /* We have already written out the last C_FILE | |
3624 | symbol. We need to write it out again. We | |
3625 | borrow *outsym temporarily. */ | |
3626 | bfd_coff_swap_sym_out (output_bfd, | |
3627 | (PTR) &finfo->last_file, | |
3628 | (PTR) outsym); | |
3629 | if (bfd_seek (output_bfd, | |
3630 | (obj_sym_filepos (output_bfd) | |
3631 | + finfo->last_file_index * osymesz), | |
3632 | SEEK_SET) != 0 | |
3633 | || (bfd_write (outsym, osymesz, 1, output_bfd) | |
3634 | != osymesz)) | |
3635 | return false; | |
3636 | } | |
3637 | } | |
3638 | ||
3639 | finfo->last_file_index = output_index; | |
3640 | finfo->last_file = isym; | |
3641 | } | |
3642 | ||
3643 | /* Output the symbol. */ | |
3644 | ||
3645 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym); | |
3646 | ||
3647 | *indexp = output_index; | |
3648 | ||
3649 | if (isym.n_sclass == C_EXT) | |
3650 | { | |
3651 | long indx; | |
3652 | struct xcoff_link_hash_entry *h; | |
3653 | ||
3654 | indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd)) | |
3655 | / isymesz); | |
3656 | h = obj_xcoff_sym_hashes (input_bfd)[indx]; | |
3657 | BFD_ASSERT (h != NULL); | |
3658 | h->indx = output_index; | |
3659 | } | |
3660 | ||
3661 | output_index += add; | |
3662 | outsym += add * osymesz; | |
3663 | } | |
3664 | ||
3665 | esym += add * isymesz; | |
3666 | isymp += add; | |
3667 | csectpp += add; | |
3668 | sym_hash += add; | |
3669 | if (debug_index != NULL) | |
3670 | debug_index += add; | |
3671 | ++indexp; | |
3672 | for (--add; add > 0; --add) | |
3673 | *indexp++ = -1; | |
3674 | } | |
3675 | ||
867d923d ILT |
3676 | /* Fix up the aux entries and the C_BSTAT symbols. This must be |
3677 | done in a separate pass, because we don't know the correct symbol | |
3678 | indices until we have already decided which symbols we are going | |
3679 | to keep. */ | |
aadf04f7 SS |
3680 | |
3681 | esym = (bfd_byte *) obj_coff_external_syms (input_bfd); | |
3682 | esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; | |
3683 | isymp = finfo->internal_syms; | |
3684 | indexp = finfo->sym_indices; | |
28a0c103 | 3685 | csectpp = xcoff_data (input_bfd)->csects; |
aadf04f7 SS |
3686 | outsym = finfo->outsyms; |
3687 | while (esym < esym_end) | |
3688 | { | |
3689 | int add; | |
3690 | ||
3691 | add = 1 + isymp->n_numaux; | |
3692 | ||
3693 | if (*indexp < 0) | |
3694 | esym += add * isymesz; | |
3695 | else | |
3696 | { | |
3697 | int i; | |
3698 | ||
867d923d ILT |
3699 | if (isymp->n_sclass == C_BSTAT) |
3700 | { | |
3701 | unsigned long indx; | |
3702 | ||
3703 | /* The value of a C_BSTAT symbol is the symbol table | |
3704 | index of the containing csect. */ | |
3705 | indx = isymp->n_value; | |
3706 | if (indx < obj_raw_syment_count (input_bfd)) | |
3707 | { | |
3708 | long symindx; | |
3709 | ||
3710 | symindx = finfo->sym_indices[indx]; | |
3711 | if (symindx < 0) | |
3712 | isymp->n_value = 0; | |
3713 | else | |
3714 | isymp->n_value = symindx; | |
3715 | bfd_coff_swap_sym_out (output_bfd, (PTR) isymp, | |
3716 | (PTR) outsym); | |
3717 | } | |
3718 | } | |
3719 | ||
aadf04f7 SS |
3720 | esym += isymesz; |
3721 | outsym += osymesz; | |
3722 | ||
3723 | for (i = 0; i < isymp->n_numaux && esym < esym_end; i++) | |
3724 | { | |
3725 | union internal_auxent aux; | |
3726 | ||
3727 | bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type, | |
3728 | isymp->n_sclass, i, isymp->n_numaux, | |
3729 | (PTR) &aux); | |
3730 | ||
3731 | if (isymp->n_sclass == C_FILE) | |
3732 | { | |
3733 | /* This is the file name (or some comment put in by | |
3734 | the compiler). If it is long, we must put it in | |
3735 | the string table. */ | |
3736 | if (aux.x_file.x_n.x_zeroes == 0 | |
3737 | && aux.x_file.x_n.x_offset != 0) | |
3738 | { | |
3739 | const char *filename; | |
3740 | bfd_size_type indx; | |
3741 | ||
3742 | BFD_ASSERT (aux.x_file.x_n.x_offset | |
3743 | >= STRING_SIZE_SIZE); | |
3744 | if (strings == NULL) | |
3745 | { | |
3746 | strings = _bfd_coff_read_string_table (input_bfd); | |
3747 | if (strings == NULL) | |
3748 | return false; | |
3749 | } | |
3750 | filename = strings + aux.x_file.x_n.x_offset; | |
3751 | indx = _bfd_stringtab_add (finfo->strtab, filename, | |
3752 | hash, copy); | |
3753 | if (indx == (bfd_size_type) -1) | |
3754 | return false; | |
3755 | aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx; | |
3756 | } | |
3757 | } | |
3758 | else if ((isymp->n_sclass == C_EXT | |
3759 | || isymp->n_sclass == C_HIDEXT) | |
3760 | && i + 1 == isymp->n_numaux) | |
3761 | { | |
3762 | /* We don't support type checking. I don't know if | |
3763 | anybody does. */ | |
3764 | aux.x_csect.x_parmhash = 0; | |
3765 | /* I don't think anybody uses these fields, but we'd | |
3766 | better clobber them just in case. */ | |
3767 | aux.x_csect.x_stab = 0; | |
3768 | aux.x_csect.x_snstab = 0; | |
3769 | if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD) | |
3770 | { | |
3771 | unsigned long indx; | |
3772 | ||
3773 | indx = aux.x_csect.x_scnlen.l; | |
3774 | if (indx < obj_raw_syment_count (input_bfd)) | |
3775 | { | |
3776 | long symindx; | |
3777 | ||
3778 | symindx = finfo->sym_indices[indx]; | |
3779 | if (symindx < 0) | |
3780 | aux.x_sym.x_tagndx.l = 0; | |
3781 | else | |
3782 | aux.x_sym.x_tagndx.l = symindx; | |
3783 | } | |
3784 | } | |
3785 | } | |
3786 | else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL) | |
3787 | { | |
3788 | unsigned long indx; | |
3789 | ||
3790 | if (ISFCN (isymp->n_type) | |
3791 | || ISTAG (isymp->n_sclass) | |
3792 | || isymp->n_sclass == C_BLOCK) | |
3793 | { | |
3794 | indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l; | |
3795 | if (indx > 0 | |
3796 | && indx < obj_raw_syment_count (input_bfd)) | |
3797 | { | |
3798 | /* We look forward through the symbol for | |
3799 | the index of the next symbol we are going | |
3800 | to include. I don't know if this is | |
3801 | entirely right. */ | |
3802 | while (finfo->sym_indices[indx] < 0 | |
3803 | && indx < obj_raw_syment_count (input_bfd)) | |
3804 | ++indx; | |
3805 | if (indx >= obj_raw_syment_count (input_bfd)) | |
3806 | indx = output_index; | |
3807 | else | |
3808 | indx = finfo->sym_indices[indx]; | |
3809 | aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx; | |
3810 | } | |
3811 | } | |
3812 | ||
3813 | indx = aux.x_sym.x_tagndx.l; | |
3814 | if (indx > 0 && indx < obj_raw_syment_count (input_bfd)) | |
3815 | { | |
3816 | long symindx; | |
3817 | ||
3818 | symindx = finfo->sym_indices[indx]; | |
3819 | if (symindx < 0) | |
3820 | aux.x_sym.x_tagndx.l = 0; | |
3821 | else | |
3822 | aux.x_sym.x_tagndx.l = symindx; | |
3823 | } | |
3824 | } | |
3825 | ||
28a0c103 ILT |
3826 | /* Copy over the line numbers, unless we are stripping |
3827 | them. We do this on a symbol by symbol basis in | |
3828 | order to more easily handle garbage collection. */ | |
3829 | if ((isymp->n_sclass == C_EXT | |
3830 | || isymp->n_sclass == C_HIDEXT) | |
3831 | && i == 0 | |
3832 | && isymp->n_numaux > 1 | |
3833 | && ISFCN (isymp->n_type) | |
3834 | && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) | |
aadf04f7 | 3835 | { |
28a0c103 ILT |
3836 | if (finfo->info->strip != strip_none |
3837 | && finfo->info->strip != strip_some) | |
3838 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0; | |
aadf04f7 SS |
3839 | else |
3840 | { | |
28a0c103 | 3841 | asection *enclosing; |
b73322d9 | 3842 | unsigned int enc_count; |
28a0c103 ILT |
3843 | bfd_size_type linoff; |
3844 | struct internal_lineno lin; | |
3845 | ||
3846 | o = *csectpp; | |
3847 | enclosing = xcoff_section_data (abfd, o)->enclosing; | |
b73322d9 ILT |
3848 | enc_count = xcoff_section_data (abfd, o)->lineno_count; |
3849 | if (oline != enclosing) | |
3850 | { | |
3851 | if (bfd_seek (input_bfd, | |
3852 | enclosing->line_filepos, | |
3853 | SEEK_SET) != 0 | |
3854 | || (bfd_read (finfo->linenos, linesz, | |
3855 | enc_count, input_bfd) | |
3856 | != linesz * enc_count)) | |
3857 | return false; | |
3858 | oline = enclosing; | |
3859 | } | |
3860 | ||
28a0c103 ILT |
3861 | linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr |
3862 | - enclosing->line_filepos); | |
3863 | ||
28a0c103 | 3864 | bfd_coff_swap_lineno_in (input_bfd, |
b73322d9 | 3865 | (PTR) (finfo->linenos + linoff), |
28a0c103 ILT |
3866 | (PTR) &lin); |
3867 | if (lin.l_lnno != 0 | |
3868 | || ((bfd_size_type) lin.l_addr.l_symndx | |
3869 | != ((esym | |
3870 | - isymesz | |
3871 | - ((bfd_byte *) | |
3872 | obj_coff_external_syms (input_bfd))) | |
3873 | / isymesz))) | |
3874 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0; | |
3875 | else | |
aadf04f7 | 3876 | { |
28a0c103 ILT |
3877 | bfd_byte *linpend, *linp; |
3878 | bfd_vma offset; | |
3879 | bfd_size_type count; | |
3880 | ||
3881 | lin.l_addr.l_symndx = *indexp; | |
3882 | bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin, | |
b73322d9 ILT |
3883 | (PTR) (finfo->linenos |
3884 | + linoff)); | |
28a0c103 ILT |
3885 | |
3886 | linpend = (finfo->linenos | |
b73322d9 | 3887 | + enc_count * linesz); |
28a0c103 ILT |
3888 | offset = (o->output_section->vma |
3889 | + o->output_offset | |
3890 | - o->vma); | |
b73322d9 | 3891 | for (linp = finfo->linenos + linoff + linesz; |
28a0c103 ILT |
3892 | linp < linpend; |
3893 | linp += linesz) | |
3894 | { | |
3895 | bfd_coff_swap_lineno_in (input_bfd, (PTR) linp, | |
3896 | (PTR) &lin); | |
3897 | if (lin.l_lnno == 0) | |
3898 | break; | |
3899 | lin.l_addr.l_paddr += offset; | |
3900 | bfd_coff_swap_lineno_out (output_bfd, | |
3901 | (PTR) &lin, | |
3902 | (PTR) linp); | |
3903 | } | |
3904 | ||
b73322d9 | 3905 | count = (linp - (finfo->linenos + linoff)) / linesz; |
28a0c103 ILT |
3906 | |
3907 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = | |
aadf04f7 | 3908 | (o->output_section->line_filepos |
28a0c103 ILT |
3909 | + o->output_section->lineno_count * linesz); |
3910 | ||
3911 | if (bfd_seek (output_bfd, | |
3912 | aux.x_sym.x_fcnary.x_fcn.x_lnnoptr, | |
3913 | SEEK_SET) != 0 | |
b73322d9 ILT |
3914 | || (bfd_write (finfo->linenos + linoff, |
3915 | linesz, count, output_bfd) | |
28a0c103 ILT |
3916 | != linesz * count)) |
3917 | return false; | |
3918 | ||
3919 | o->output_section->lineno_count += count; | |
aadf04f7 SS |
3920 | } |
3921 | } | |
aadf04f7 SS |
3922 | } |
3923 | ||
28a0c103 ILT |
3924 | bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type, |
3925 | isymp->n_sclass, i, isymp->n_numaux, | |
3926 | (PTR) outsym); | |
3927 | outsym += osymesz; | |
3928 | esym += isymesz; | |
aadf04f7 | 3929 | } |
aadf04f7 | 3930 | } |
28a0c103 ILT |
3931 | |
3932 | indexp += add; | |
3933 | isymp += add; | |
3934 | csectpp += add; | |
aadf04f7 SS |
3935 | } |
3936 | ||
3937 | /* If we swapped out a C_FILE symbol, guess that the next C_FILE | |
3938 | symbol will be the first symbol in the next input file. In the | |
3939 | normal case, this will save us from writing out the C_FILE symbol | |
3940 | again. */ | |
3941 | if (finfo->last_file_index != -1 | |
3942 | && (bfd_size_type) finfo->last_file_index >= syment_base) | |
3943 | { | |
3944 | finfo->last_file.n_value = output_index; | |
3945 | bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file, | |
3946 | (PTR) (finfo->outsyms | |
3947 | + ((finfo->last_file_index - syment_base) | |
3948 | * osymesz))); | |
3949 | } | |
3950 | ||
3951 | /* Write the modified symbols to the output file. */ | |
3952 | if (outsym > finfo->outsyms) | |
3953 | { | |
3954 | if (bfd_seek (output_bfd, | |
3955 | obj_sym_filepos (output_bfd) + syment_base * osymesz, | |
3956 | SEEK_SET) != 0 | |
3957 | || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, | |
3958 | output_bfd) | |
3959 | != (bfd_size_type) (outsym - finfo->outsyms))) | |
3960 | return false; | |
3961 | ||
3962 | BFD_ASSERT ((obj_raw_syment_count (output_bfd) | |
3963 | + (outsym - finfo->outsyms) / osymesz) | |
3964 | == output_index); | |
3965 | ||
3966 | obj_raw_syment_count (output_bfd) = output_index; | |
3967 | } | |
3968 | ||
3969 | /* Don't let the linker relocation routines discard the symbols. */ | |
3970 | keep_syms = obj_coff_keep_syms (input_bfd); | |
3971 | obj_coff_keep_syms (input_bfd) = true; | |
3972 | ||
3973 | /* Relocate the contents of each section. */ | |
3974 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
3975 | { | |
3976 | bfd_byte *contents; | |
3977 | ||
3978 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
28a0c103 ILT |
3979 | || o->_raw_size == 0 |
3980 | || (o->flags & SEC_IN_MEMORY) != 0) | |
aadf04f7 SS |
3981 | continue; |
3982 | ||
3983 | /* We have set filepos correctly for the sections we created to | |
3984 | represent csects, so bfd_get_section_contents should work. */ | |
3985 | if (coff_section_data (input_bfd, o) != NULL | |
3986 | && coff_section_data (input_bfd, o)->contents != NULL) | |
3987 | contents = coff_section_data (input_bfd, o)->contents; | |
3988 | else | |
3989 | { | |
3990 | if (! bfd_get_section_contents (input_bfd, o, finfo->contents, | |
3991 | (file_ptr) 0, o->_raw_size)) | |
3992 | return false; | |
3993 | contents = finfo->contents; | |
3994 | } | |
3995 | ||
3996 | if ((o->flags & SEC_RELOC) != 0) | |
3997 | { | |
3998 | int target_index; | |
3999 | struct internal_reloc *internal_relocs; | |
4000 | struct internal_reloc *irel; | |
4001 | bfd_vma offset; | |
4002 | struct internal_reloc *irelend; | |
4003 | struct xcoff_link_hash_entry **rel_hash; | |
28a0c103 | 4004 | long r_symndx; |
aadf04f7 | 4005 | |
28a0c103 | 4006 | /* Read in the relocs. */ |
aadf04f7 | 4007 | target_index = o->output_section->target_index; |
28a0c103 | 4008 | internal_relocs = (xcoff_read_internal_relocs |
aadf04f7 SS |
4009 | (input_bfd, o, false, finfo->external_relocs, |
4010 | true, | |
4011 | (finfo->section_info[target_index].relocs | |
4012 | + o->output_section->reloc_count))); | |
4013 | if (internal_relocs == NULL) | |
4014 | return false; | |
4015 | ||
4016 | /* Call processor specific code to relocate the section | |
4017 | contents. */ | |
4018 | if (! bfd_coff_relocate_section (output_bfd, finfo->info, | |
4019 | input_bfd, o, | |
4020 | contents, | |
4021 | internal_relocs, | |
4022 | finfo->internal_syms, | |
4023 | xcoff_data (input_bfd)->csects)) | |
4024 | return false; | |
4025 | ||
4026 | offset = o->output_section->vma + o->output_offset - o->vma; | |
4027 | irel = internal_relocs; | |
4028 | irelend = irel + o->reloc_count; | |
4029 | rel_hash = (finfo->section_info[target_index].rel_hashes | |
4030 | + o->output_section->reloc_count); | |
4031 | for (; irel < irelend; irel++, rel_hash++) | |
4032 | { | |
28a0c103 ILT |
4033 | struct xcoff_link_hash_entry *h = NULL; |
4034 | struct internal_ldrel ldrel; | |
aadf04f7 SS |
4035 | |
4036 | *rel_hash = NULL; | |
4037 | ||
4038 | /* Adjust the reloc address and symbol index. */ | |
4039 | ||
4040 | irel->r_vaddr += offset; | |
4041 | ||
28a0c103 | 4042 | r_symndx = irel->r_symndx; |
aadf04f7 | 4043 | |
28a0c103 | 4044 | if (r_symndx != -1) |
aadf04f7 | 4045 | { |
28a0c103 ILT |
4046 | h = obj_xcoff_sym_hashes (input_bfd)[r_symndx]; |
4047 | if (h != NULL) | |
4048 | { | |
4049 | /* This is a global symbol. */ | |
4050 | if (h->indx >= 0) | |
4051 | irel->r_symndx = h->indx; | |
4052 | else | |
4053 | { | |
4054 | /* This symbol is being written at the end | |
4055 | of the file, and we do not yet know the | |
4056 | symbol index. We save the pointer to the | |
4057 | hash table entry in the rel_hash list. | |
4058 | We set the indx field to -2 to indicate | |
4059 | that this symbol must not be stripped. */ | |
4060 | *rel_hash = h; | |
4061 | h->indx = -2; | |
4062 | } | |
4063 | } | |
aadf04f7 SS |
4064 | else |
4065 | { | |
28a0c103 ILT |
4066 | long indx; |
4067 | ||
4068 | indx = finfo->sym_indices[r_symndx]; | |
4069 | ||
4070 | if (indx == -1) | |
4071 | { | |
4072 | struct internal_syment *is; | |
4073 | ||
4074 | /* Relocations against a TC0 TOC anchor are | |
4075 | automatically transformed to be against | |
4076 | the TOC anchor in the output file. */ | |
4077 | is = finfo->internal_syms + r_symndx; | |
4078 | if (is->n_sclass == C_HIDEXT | |
4079 | && is->n_numaux > 0) | |
4080 | { | |
4081 | PTR auxptr; | |
4082 | union internal_auxent aux; | |
4083 | ||
4084 | auxptr = ((PTR) | |
4085 | (((bfd_byte *) | |
4086 | obj_coff_external_syms (input_bfd)) | |
4087 | + ((r_symndx + is->n_numaux) | |
4088 | * isymesz))); | |
4089 | bfd_coff_swap_aux_in (input_bfd, auxptr, | |
4090 | is->n_type, is->n_sclass, | |
4091 | is->n_numaux - 1, | |
4092 | is->n_numaux, | |
4093 | (PTR) &aux); | |
4094 | if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD | |
4095 | && aux.x_csect.x_smclas == XMC_TC0) | |
4096 | indx = finfo->toc_symindx; | |
4097 | } | |
4098 | } | |
4099 | ||
4100 | if (indx != -1) | |
4101 | irel->r_symndx = indx; | |
4102 | else | |
4103 | { | |
4104 | struct internal_syment *is; | |
4105 | const char *name; | |
4106 | char buf[SYMNMLEN + 1]; | |
4107 | ||
4108 | /* This reloc is against a symbol we are | |
4109 | stripping. It would be possible to handle | |
4110 | this case, but I don't think it's worth it. */ | |
4111 | is = finfo->internal_syms + r_symndx; | |
4112 | ||
4113 | name = (_bfd_coff_internal_syment_name | |
4114 | (input_bfd, is, buf)); | |
4115 | if (name == NULL) | |
4116 | return false; | |
4117 | ||
4118 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
4119 | (finfo->info, name, input_bfd, o, | |
4120 | irel->r_vaddr))) | |
4121 | return false; | |
4122 | } | |
aadf04f7 SS |
4123 | } |
4124 | } | |
28a0c103 ILT |
4125 | |
4126 | switch (irel->r_type) | |
aadf04f7 | 4127 | { |
28a0c103 | 4128 | default: |
867d923d ILT |
4129 | if (h == NULL |
4130 | || h->root.type == bfd_link_hash_defined | |
4131 | || h->root.type == bfd_link_hash_defweak | |
4132 | || h->root.type == bfd_link_hash_common) | |
4133 | break; | |
4134 | /* Fall through. */ | |
28a0c103 ILT |
4135 | case R_POS: |
4136 | case R_NEG: | |
4137 | case R_RL: | |
4138 | case R_RLA: | |
4139 | /* This reloc needs to be copied into the .loader | |
4140 | section. */ | |
4141 | ldrel.l_vaddr = irel->r_vaddr; | |
4142 | if (r_symndx == -1) | |
4143 | ldrel.l_symndx = -1; | |
867d923d ILT |
4144 | else if (h == NULL |
4145 | || (h->root.type == bfd_link_hash_defined | |
4146 | || h->root.type == bfd_link_hash_defweak | |
4147 | || h->root.type == bfd_link_hash_common)) | |
28a0c103 ILT |
4148 | { |
4149 | asection *sec; | |
aadf04f7 | 4150 | |
867d923d ILT |
4151 | if (h == NULL) |
4152 | sec = xcoff_data (input_bfd)->csects[r_symndx]; | |
4153 | else if (h->root.type == bfd_link_hash_common) | |
4154 | sec = h->root.u.c.p->section; | |
28a0c103 | 4155 | else |
867d923d ILT |
4156 | sec = h->root.u.def.section; |
4157 | sec = sec->output_section; | |
aadf04f7 | 4158 | |
867d923d | 4159 | if (strcmp (sec->name, ".text") == 0) |
28a0c103 | 4160 | ldrel.l_symndx = 0; |
867d923d | 4161 | else if (strcmp (sec->name, ".data") == 0) |
28a0c103 | 4162 | ldrel.l_symndx = 1; |
867d923d | 4163 | else if (strcmp (sec->name, ".bss") == 0) |
28a0c103 | 4164 | ldrel.l_symndx = 2; |
867d923d ILT |
4165 | else |
4166 | { | |
4167 | (*_bfd_error_handler) | |
4168 | ("%s: loader reloc in unrecognized section `%s'", | |
4169 | bfd_get_filename (input_bfd), | |
4170 | sec->name); | |
4171 | bfd_set_error (bfd_error_nonrepresentable_section); | |
4172 | return false; | |
4173 | } | |
28a0c103 ILT |
4174 | } |
4175 | else | |
aadf04f7 | 4176 | { |
28a0c103 | 4177 | if (h->ldindx < 0) |
aadf04f7 | 4178 | { |
28a0c103 ILT |
4179 | (*_bfd_error_handler) |
4180 | ("%s: `%s' in loader reloc but not loader sym", | |
4181 | bfd_get_filename (input_bfd), | |
4182 | h->root.root.string); | |
4183 | bfd_set_error (bfd_error_bad_value); | |
4184 | return false; | |
aadf04f7 | 4185 | } |
28a0c103 | 4186 | ldrel.l_symndx = h->ldindx; |
aadf04f7 | 4187 | } |
28a0c103 ILT |
4188 | ldrel.l_rtype = (irel->r_size << 8) | irel->r_type; |
4189 | ldrel.l_rsecnm = o->output_section->target_index; | |
4190 | if (xcoff_hash_table (finfo->info)->textro | |
867d923d | 4191 | && strcmp (o->output_section->name, ".text") == 0) |
aadf04f7 | 4192 | { |
28a0c103 ILT |
4193 | (*_bfd_error_handler) |
4194 | ("%s: loader reloc in read-only section %s", | |
4195 | bfd_get_filename (input_bfd), | |
4196 | bfd_get_section_name (finfo->output_bfd, | |
4197 | o->output_section)); | |
4198 | bfd_set_error (bfd_error_invalid_operation); | |
4199 | return false; | |
aadf04f7 | 4200 | } |
28a0c103 ILT |
4201 | xcoff_swap_ldrel_out (output_bfd, &ldrel, |
4202 | finfo->ldrel); | |
4203 | BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ); | |
4204 | ++finfo->ldrel; | |
867d923d ILT |
4205 | break; |
4206 | ||
4207 | case R_TOC: | |
4208 | case R_GL: | |
4209 | case R_TCL: | |
4210 | case R_TRL: | |
4211 | case R_TRLA: | |
4212 | /* We should never need a .loader reloc for a TOC | |
4213 | relative reloc. */ | |
4214 | break; | |
aadf04f7 SS |
4215 | } |
4216 | } | |
4217 | ||
4218 | o->output_section->reloc_count += o->reloc_count; | |
4219 | } | |
4220 | ||
4221 | /* Write out the modified section contents. */ | |
4222 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
4223 | contents, o->output_offset, | |
4224 | (o->_cooked_size != 0 | |
4225 | ? o->_cooked_size | |
4226 | : o->_raw_size))) | |
4227 | return false; | |
4228 | } | |
4229 | ||
4230 | obj_coff_keep_syms (input_bfd) = keep_syms; | |
4231 | ||
4232 | if (! finfo->info->keep_memory) | |
4233 | { | |
4234 | if (! _bfd_coff_free_symbols (input_bfd)) | |
4235 | return false; | |
4236 | } | |
4237 | ||
4238 | return true; | |
4239 | } | |
4240 | ||
28a0c103 ILT |
4241 | #undef N_TMASK |
4242 | #undef N_BTSHFT | |
4243 | ||
aadf04f7 SS |
4244 | /* Write out a non-XCOFF global symbol. */ |
4245 | ||
4246 | static boolean | |
4247 | xcoff_write_global_symbol (h, p) | |
4248 | struct xcoff_link_hash_entry *h; | |
4249 | PTR p; | |
4250 | { | |
4251 | struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p; | |
4252 | bfd *output_bfd; | |
4253 | bfd_byte *outsym; | |
4254 | struct internal_syment isym; | |
4255 | union internal_auxent aux; | |
4256 | ||
28a0c103 ILT |
4257 | output_bfd = finfo->output_bfd; |
4258 | ||
4259 | /* If this symbol was garbage collected, just skip it. */ | |
4260 | if (xcoff_hash_table (finfo->info)->gc | |
4261 | && (h->flags & XCOFF_MARK) == 0) | |
4262 | return true; | |
4263 | ||
4264 | /* If we need a .loader section entry, write it out. */ | |
4265 | if (h->ldsym != NULL) | |
4266 | { | |
4267 | struct internal_ldsym *ldsym; | |
4268 | bfd *impbfd; | |
4269 | ||
4270 | ldsym = h->ldsym; | |
4271 | ||
4272 | if (h->root.type == bfd_link_hash_undefined | |
4273 | || h->root.type == bfd_link_hash_undefweak) | |
4274 | { | |
4275 | ldsym->l_value = 0; | |
4276 | ldsym->l_scnum = N_UNDEF; | |
4277 | ldsym->l_smtype = XTY_ER; | |
4278 | impbfd = h->root.u.undef.abfd; | |
4279 | } | |
4280 | else if (h->root.type == bfd_link_hash_defined | |
4281 | || h->root.type == bfd_link_hash_defweak) | |
4282 | { | |
4283 | asection *sec; | |
4284 | ||
4285 | sec = h->root.u.def.section; | |
4286 | ldsym->l_value = (sec->output_section->vma | |
4287 | + sec->output_offset | |
4288 | + h->root.u.def.value); | |
4289 | ldsym->l_scnum = sec->output_section->target_index; | |
4290 | ldsym->l_smtype = XTY_SD; | |
4291 | impbfd = sec->owner; | |
4292 | } | |
4293 | else | |
4294 | abort (); | |
4295 | ||
4296 | if (((h->flags & XCOFF_DEF_REGULAR) == 0 | |
4297 | && (h->flags & XCOFF_REF_DYNAMIC) != 0) | |
4298 | || (h->flags & XCOFF_IMPORT) != 0) | |
4299 | ldsym->l_smtype |= L_IMPORT; | |
4300 | if (((h->flags & XCOFF_DEF_REGULAR) != 0 | |
4301 | && (h->flags & XCOFF_REF_DYNAMIC) != 0) | |
4302 | || (h->flags & XCOFF_EXPORT) != 0) | |
4303 | ldsym->l_smtype |= L_EXPORT; | |
4304 | if ((h->flags & XCOFF_ENTRY) != 0) | |
4305 | ldsym->l_smtype |= L_ENTRY; | |
4306 | ||
4307 | ldsym->l_smclas = h->smclas; | |
4308 | ||
4309 | if (ldsym->l_ifile == (bfd_size_type) -1) | |
4310 | ldsym->l_ifile = 0; | |
4311 | else if (ldsym->l_ifile == 0) | |
4312 | { | |
4313 | if ((ldsym->l_smtype & L_IMPORT) == 0) | |
4314 | ldsym->l_ifile = 0; | |
4315 | else if (impbfd == NULL) | |
4316 | ldsym->l_ifile = 0; | |
4317 | else | |
4318 | { | |
4319 | BFD_ASSERT (impbfd->xvec == output_bfd->xvec); | |
4320 | ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; | |
4321 | } | |
4322 | } | |
4323 | ||
4324 | ldsym->l_parm = 0; | |
4325 | ||
4326 | BFD_ASSERT (h->ldindx >= 0); | |
4327 | BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym)); | |
4328 | xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3); | |
4329 | h->ldsym = NULL; | |
4330 | } | |
4331 | ||
4332 | /* If this symbol needs global linkage code, write it out. */ | |
4333 | if (h->root.type == bfd_link_hash_defined | |
4334 | && (h->root.u.def.section | |
4335 | == xcoff_hash_table (finfo->info)->linkage_section)) | |
4336 | { | |
4337 | bfd_byte *p; | |
4338 | bfd_vma tocoff; | |
4339 | unsigned int i; | |
4340 | ||
4341 | p = h->root.u.def.section->contents + h->root.u.def.value; | |
4342 | ||
4343 | /* The first instruction in the global linkage code loads a | |
4344 | specific TOC element. */ | |
4345 | tocoff = (h->descriptor->toc_section->output_section->vma | |
4346 | + h->descriptor->toc_section->output_offset | |
4347 | + h->descriptor->toc_offset | |
4348 | - xcoff_data (output_bfd)->toc); | |
4349 | bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p); | |
4350 | for (i = 0, p += 4; | |
4351 | i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]; | |
4352 | i++, p += 4) | |
4353 | bfd_put_32 (output_bfd, xcoff_glink_code[i], p); | |
4354 | } | |
4355 | ||
4356 | /* If we created a TOC entry for this symbol, write out the required | |
4357 | relocs. */ | |
4358 | if ((h->flags & XCOFF_SET_TOC) != 0) | |
4359 | { | |
4360 | asection *tocsec; | |
4361 | asection *osec; | |
4362 | int oindx; | |
4363 | struct internal_reloc *irel; | |
4364 | struct internal_ldrel ldrel; | |
4365 | ||
4366 | tocsec = h->toc_section; | |
4367 | osec = tocsec->output_section; | |
4368 | oindx = osec->target_index; | |
4369 | irel = finfo->section_info[oindx].relocs + osec->reloc_count; | |
4370 | irel->r_vaddr = (osec->vma | |
4371 | + tocsec->output_offset | |
4372 | + h->toc_offset); | |
4373 | if (h->indx >= 0) | |
4374 | irel->r_symndx = h->indx; | |
4375 | else | |
4376 | { | |
4377 | h->indx = -2; | |
4378 | irel->r_symndx = obj_raw_syment_count (output_bfd); | |
4379 | } | |
4380 | irel->r_type = R_POS; | |
4381 | irel->r_size = 31; | |
4382 | finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; | |
4383 | ++osec->reloc_count; | |
4384 | ||
4385 | BFD_ASSERT (h->ldindx >= 0); | |
4386 | ldrel.l_vaddr = irel->r_vaddr; | |
4387 | ldrel.l_symndx = h->ldindx; | |
4388 | ldrel.l_rtype = (31 << 8) | R_POS; | |
4389 | ldrel.l_rsecnm = oindx; | |
4390 | xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel); | |
4391 | ++finfo->ldrel; | |
4392 | } | |
4393 | ||
4394 | if (h->indx >= 0) | |
4395 | return true; | |
4396 | ||
4397 | if (h->indx != -2 | |
4398 | && (finfo->info->strip == strip_all | |
4399 | || (finfo->info->strip == strip_some | |
4400 | && (bfd_hash_lookup (finfo->info->keep_hash, | |
4401 | h->root.root.string, false, false) | |
4402 | == NULL)))) | |
4403 | return true; | |
4404 | ||
4405 | if (h->indx != -2 | |
4406 | && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0) | |
aadf04f7 SS |
4407 | return true; |
4408 | ||
aadf04f7 SS |
4409 | outsym = finfo->outsyms; |
4410 | ||
4411 | memset (&aux, 0, sizeof aux); | |
4412 | ||
4413 | h->indx = obj_raw_syment_count (output_bfd); | |
4414 | ||
4415 | if (strlen (h->root.root.string) <= SYMNMLEN) | |
4416 | strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN); | |
4417 | else | |
4418 | { | |
4419 | boolean hash; | |
4420 | bfd_size_type indx; | |
4421 | ||
4422 | hash = true; | |
4423 | if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0) | |
4424 | hash = false; | |
4425 | indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash, | |
4426 | false); | |
4427 | if (indx == (bfd_size_type) -1) | |
4428 | return false; | |
4429 | isym._n._n_n._n_zeroes = 0; | |
4430 | isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; | |
4431 | } | |
4432 | ||
4433 | if (h->root.type == bfd_link_hash_undefined | |
4434 | || h->root.type == bfd_link_hash_undefweak) | |
4435 | { | |
4436 | isym.n_value = 0; | |
4437 | isym.n_scnum = N_UNDEF; | |
4438 | isym.n_sclass = C_EXT; | |
4439 | aux.x_csect.x_smtyp = XTY_ER; | |
4440 | } | |
4441 | else if (h->root.type == bfd_link_hash_defined | |
4442 | || h->root.type == bfd_link_hash_defweak) | |
4443 | { | |
4444 | isym.n_value = (h->root.u.def.section->output_section->vma | |
4445 | + h->root.u.def.section->output_offset | |
4446 | + h->root.u.def.value); | |
4447 | isym.n_scnum = h->root.u.def.section->output_section->target_index; | |
4448 | isym.n_sclass = C_HIDEXT; | |
4449 | aux.x_csect.x_smtyp = XTY_SD; | |
4450 | /* I don't know what the csect length should be in this case. */ | |
4451 | } | |
4452 | else | |
4453 | abort (); | |
4454 | ||
4455 | isym.n_type = T_NULL; | |
4456 | isym.n_numaux = 1; | |
4457 | ||
4458 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym); | |
4459 | outsym += bfd_coff_symesz (output_bfd); | |
4460 | ||
28a0c103 | 4461 | aux.x_csect.x_smclas = h->smclas; |
aadf04f7 SS |
4462 | |
4463 | bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1, | |
4464 | (PTR) outsym); | |
4465 | outsym += bfd_coff_auxesz (output_bfd); | |
4466 | ||
28a0c103 ILT |
4467 | if (h->root.type == bfd_link_hash_defined |
4468 | || h->root.type == bfd_link_hash_defweak) | |
aadf04f7 SS |
4469 | { |
4470 | /* We just output an SD symbol. Now output an LD symbol. */ | |
4471 | ||
4472 | h->indx += 2; | |
4473 | ||
4474 | isym.n_sclass = C_EXT; | |
4475 | bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym); | |
4476 | outsym += bfd_coff_symesz (output_bfd); | |
4477 | ||
4478 | aux.x_csect.x_smtyp = XTY_LD; | |
4479 | aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd); | |
4480 | ||
4481 | bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1, | |
4482 | (PTR) outsym); | |
4483 | outsym += bfd_coff_auxesz (output_bfd); | |
4484 | } | |
4485 | ||
4486 | if (bfd_seek (output_bfd, | |
4487 | (obj_sym_filepos (output_bfd) | |
4488 | + (obj_raw_syment_count (output_bfd) | |
4489 | * bfd_coff_symesz (output_bfd))), | |
4490 | SEEK_SET) != 0 | |
4491 | || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd) | |
4492 | != (bfd_size_type) (outsym - finfo->outsyms))) | |
4493 | return false; | |
4494 | obj_raw_syment_count (output_bfd) += | |
4495 | (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd); | |
4496 | ||
4497 | return true; | |
4498 | } | |
4499 | ||
4500 | /* Handle a link order which is supposed to generate a reloc. */ | |
4501 | ||
4502 | static boolean | |
4503 | xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order) | |
4504 | bfd *output_bfd; | |
4505 | struct xcoff_final_link_info *finfo; | |
4506 | asection *output_section; | |
4507 | struct bfd_link_order *link_order; | |
4508 | { | |
4509 | reloc_howto_type *howto; | |
4510 | struct internal_reloc *irel; | |
4511 | struct xcoff_link_hash_entry **rel_hash_ptr; | |
4512 | ||
4513 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
4514 | if (howto == NULL) | |
4515 | { | |
4516 | bfd_set_error (bfd_error_bad_value); | |
4517 | return false; | |
4518 | } | |
4519 | ||
4520 | if (link_order->u.reloc.p->addend != 0) | |
4521 | { | |
4522 | bfd_size_type size; | |
4523 | bfd_byte *buf; | |
4524 | bfd_reloc_status_type rstat; | |
4525 | boolean ok; | |
4526 | ||
4527 | size = bfd_get_reloc_size (howto); | |
4528 | buf = (bfd_byte *) bfd_zmalloc (size); | |
4529 | if (buf == NULL) | |
4530 | { | |
4531 | bfd_set_error (bfd_error_no_memory); | |
4532 | return false; | |
4533 | } | |
4534 | ||
4535 | rstat = _bfd_relocate_contents (howto, output_bfd, | |
4536 | link_order->u.reloc.p->addend, buf); | |
4537 | switch (rstat) | |
4538 | { | |
4539 | case bfd_reloc_ok: | |
4540 | break; | |
4541 | default: | |
4542 | case bfd_reloc_outofrange: | |
4543 | abort (); | |
4544 | case bfd_reloc_overflow: | |
4545 | if (! ((*finfo->info->callbacks->reloc_overflow) | |
4546 | (finfo->info, | |
4547 | (link_order->type == bfd_section_reloc_link_order | |
4548 | ? bfd_section_name (output_bfd, | |
4549 | link_order->u.reloc.p->u.section) | |
4550 | : link_order->u.reloc.p->u.name), | |
4551 | howto->name, link_order->u.reloc.p->addend, | |
4552 | (bfd *) NULL, (asection *) NULL, (bfd_vma) 0))) | |
4553 | { | |
4554 | free (buf); | |
4555 | return false; | |
4556 | } | |
4557 | break; | |
4558 | } | |
4559 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
4560 | (file_ptr) link_order->offset, size); | |
4561 | free (buf); | |
4562 | if (! ok) | |
4563 | return false; | |
4564 | } | |
4565 | ||
4566 | /* Store the reloc information in the right place. It will get | |
4567 | swapped and written out at the end of the final_link routine. */ | |
4568 | ||
4569 | irel = (finfo->section_info[output_section->target_index].relocs | |
4570 | + output_section->reloc_count); | |
4571 | rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes | |
4572 | + output_section->reloc_count); | |
4573 | ||
4574 | memset (irel, 0, sizeof (struct internal_reloc)); | |
4575 | *rel_hash_ptr = NULL; | |
4576 | ||
4577 | irel->r_vaddr = output_section->vma + link_order->offset; | |
4578 | ||
4579 | if (link_order->type == bfd_section_reloc_link_order) | |
4580 | { | |
4581 | /* We need to somehow locate a symbol in the right section. The | |
4582 | symbol must either have a value of zero, or we must adjust | |
4583 | the addend by the value of the symbol. FIXME: Write this | |
4584 | when we need it. The old linker couldn't handle this anyhow. */ | |
4585 | abort (); | |
4586 | *rel_hash_ptr = NULL; | |
4587 | irel->r_symndx = 0; | |
4588 | } | |
4589 | else | |
4590 | { | |
4591 | struct xcoff_link_hash_entry *h; | |
4592 | ||
4593 | h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info), | |
4594 | link_order->u.reloc.p->u.name, | |
4595 | false, false, true); | |
4596 | if (h != NULL) | |
4597 | { | |
4598 | if (h->indx >= 0) | |
4599 | irel->r_symndx = h->indx; | |
4600 | else | |
4601 | { | |
4602 | /* Set the index to -2 to force this symbol to get | |
4603 | written out. */ | |
4604 | h->indx = -2; | |
4605 | *rel_hash_ptr = h; | |
4606 | irel->r_symndx = 0; | |
4607 | } | |
4608 | } | |
4609 | else | |
4610 | { | |
4611 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
4612 | (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
4613 | (asection *) NULL, (bfd_vma) 0))) | |
4614 | return false; | |
4615 | irel->r_symndx = 0; | |
4616 | } | |
4617 | } | |
4618 | ||
4619 | irel->r_type = howto->type; | |
4620 | irel->r_size = howto->bitsize - 1; | |
4621 | if (howto->complain_on_overflow == complain_overflow_signed) | |
4622 | irel->r_size |= 0x80; | |
4623 | ||
4624 | ++output_section->reloc_count; | |
4625 | ||
4626 | return true; | |
4627 | } | |
4628 | ||
4629 | /* Sort relocs by VMA. This is called via qsort. */ | |
4630 | ||
4631 | static int | |
4632 | xcoff_sort_relocs (p1, p2) | |
4633 | const PTR p1; | |
4634 | const PTR p2; | |
4635 | { | |
4636 | const struct internal_reloc *r1 = (const struct internal_reloc *) p1; | |
4637 | const struct internal_reloc *r2 = (const struct internal_reloc *) p2; | |
4638 | ||
4639 | if (r1->r_vaddr > r2->r_vaddr) | |
4640 | return 1; | |
4641 | else if (r1->r_vaddr < r2->r_vaddr) | |
4642 | return -1; | |
4643 | else | |
4644 | return 0; | |
4645 | } | |
4646 | ||
4647 | /* This is the relocation function for the RS/6000/POWER/PowerPC. | |
4648 | This is currently the only processor which uses XCOFF; I hope that | |
4649 | will never change. */ | |
4650 | ||
4651 | boolean | |
4652 | _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd, | |
4653 | input_section, contents, relocs, syms, | |
4654 | sections) | |
4655 | bfd *output_bfd; | |
4656 | struct bfd_link_info *info; | |
4657 | bfd *input_bfd; | |
4658 | asection *input_section; | |
4659 | bfd_byte *contents; | |
4660 | struct internal_reloc *relocs; | |
4661 | struct internal_syment *syms; | |
4662 | asection **sections; | |
4663 | { | |
4664 | struct internal_reloc *rel; | |
4665 | struct internal_reloc *relend; | |
4666 | ||
4667 | rel = relocs; | |
4668 | relend = rel + input_section->reloc_count; | |
4669 | for (; rel < relend; rel++) | |
4670 | { | |
4671 | long symndx; | |
4672 | struct xcoff_link_hash_entry *h; | |
4673 | struct internal_syment *sym; | |
4674 | bfd_vma addend; | |
4675 | bfd_vma val; | |
4676 | struct reloc_howto_struct howto; | |
4677 | bfd_reloc_status_type rstat; | |
4678 | ||
28a0c103 | 4679 | /* Relocation type R_REF is a special relocation type which is |
aadf04f7 SS |
4680 | merely used to prevent garbage collection from occurring for |
4681 | the csect including the symbol which it references. */ | |
28a0c103 | 4682 | if (rel->r_type == R_REF) |
aadf04f7 SS |
4683 | continue; |
4684 | ||
4685 | symndx = rel->r_symndx; | |
4686 | ||
4687 | if (symndx == -1) | |
4688 | { | |
4689 | h = NULL; | |
4690 | sym = NULL; | |
4691 | addend = 0; | |
4692 | } | |
4693 | else | |
4694 | { | |
4695 | h = obj_xcoff_sym_hashes (input_bfd)[symndx]; | |
4696 | sym = syms + symndx; | |
4697 | addend = - sym->n_value; | |
4698 | } | |
4699 | ||
4700 | /* We build the howto information on the fly. */ | |
4701 | ||
4702 | howto.type = rel->r_type; | |
4703 | howto.rightshift = 0; | |
4704 | howto.size = 2; | |
4705 | howto.bitsize = (rel->r_size & 0x1f) + 1; | |
4706 | howto.pc_relative = false; | |
4707 | howto.bitpos = 0; | |
4708 | if ((rel->r_size & 0x80) != 0) | |
4709 | howto.complain_on_overflow = complain_overflow_signed; | |
4710 | else | |
4711 | howto.complain_on_overflow = complain_overflow_bitfield; | |
4712 | howto.special_function = NULL; | |
4713 | howto.name = "internal"; | |
4714 | howto.partial_inplace = true; | |
4715 | if (howto.bitsize == 32) | |
4716 | howto.src_mask = howto.dst_mask = 0xffffffff; | |
4717 | else | |
4718 | { | |
4719 | howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1; | |
4720 | if (howto.bitsize == 16) | |
4721 | howto.size = 1; | |
4722 | } | |
4723 | howto.pcrel_offset = false; | |
4724 | ||
4725 | val = 0; | |
4726 | ||
4727 | if (h == NULL) | |
4728 | { | |
4729 | asection *sec; | |
4730 | ||
4731 | if (symndx == -1) | |
4732 | { | |
4733 | sec = bfd_abs_section_ptr; | |
4734 | val = 0; | |
4735 | } | |
4736 | else | |
4737 | { | |
4738 | sec = sections[symndx]; | |
4739 | val = (sec->output_section->vma | |
4740 | + sec->output_offset | |
4741 | + sym->n_value | |
4742 | - sec->vma); | |
4743 | } | |
4744 | } | |
4745 | else | |
4746 | { | |
4747 | if (h->root.type == bfd_link_hash_defined | |
4748 | || h->root.type == bfd_link_hash_defweak) | |
4749 | { | |
4750 | asection *sec; | |
4751 | ||
4752 | sec = h->root.u.def.section; | |
4753 | val = (h->root.u.def.value | |
4754 | + sec->output_section->vma | |
4755 | + sec->output_offset); | |
4756 | } | |
28a0c103 ILT |
4757 | else if ((h->flags & XCOFF_REF_DYNAMIC) != 0 |
4758 | || (h->flags & XCOFF_IMPORT) != 0) | |
4759 | { | |
4760 | /* Every symbol in a shared object is defined somewhere. */ | |
4761 | val = 0; | |
4762 | } | |
aadf04f7 SS |
4763 | else if (! info->relocateable) |
4764 | { | |
4765 | if (! ((*info->callbacks->undefined_symbol) | |
4766 | (info, h->root.root.string, input_bfd, input_section, | |
4767 | rel->r_vaddr - input_section->vma))) | |
4768 | return false; | |
4769 | } | |
4770 | } | |
4771 | ||
4772 | /* I took the relocation type definitions from two documents: | |
4773 | the PowerPC AIX Version 4 Application Binary Interface, First | |
4774 | Edition (April 1992), and the PowerOpen ABI, Big-Endian | |
4775 | 32-Bit Hardware Implementation (June 30, 1994). Differences | |
4776 | between the documents are noted below. */ | |
4777 | ||
4778 | switch (rel->r_type) | |
4779 | { | |
28a0c103 ILT |
4780 | case R_RTB: |
4781 | case R_RRTBI: | |
4782 | case R_RRTBA: | |
aadf04f7 SS |
4783 | /* These relocs are defined by the PowerPC ABI to be |
4784 | relative branches which use half of the difference | |
4785 | between the symbol and the program counter. I can't | |
4786 | quite figure out when this is useful. These relocs are | |
4787 | not defined by the PowerOpen ABI. */ | |
4788 | default: | |
4789 | (*_bfd_error_handler) | |
4790 | ("%s: unsupported relocation type 0x%02x", | |
4791 | bfd_get_filename (input_bfd), (unsigned int) rel->r_type); | |
4792 | bfd_set_error (bfd_error_bad_value); | |
4793 | return false; | |
28a0c103 | 4794 | case R_POS: |
aadf04f7 SS |
4795 | /* Simple positive relocation. */ |
4796 | break; | |
28a0c103 | 4797 | case R_NEG: |
aadf04f7 SS |
4798 | /* Simple negative relocation. */ |
4799 | val = - val; | |
4800 | break; | |
28a0c103 | 4801 | case R_REL: |
aadf04f7 SS |
4802 | /* Simple PC relative relocation. */ |
4803 | howto.pc_relative = true; | |
4804 | break; | |
28a0c103 | 4805 | case R_TOC: |
aadf04f7 SS |
4806 | /* TOC relative relocation. The value in the instruction in |
4807 | the input file is the offset from the input file TOC to | |
4808 | the desired location. We want the offset from the final | |
4809 | TOC to the desired location. We have: | |
4810 | isym = iTOC + in | |
4811 | iinsn = in + o | |
4812 | osym = oTOC + on | |
4813 | oinsn = on + o | |
4814 | so we must change insn by on - in. | |
4815 | */ | |
28a0c103 | 4816 | case R_GL: |
aadf04f7 SS |
4817 | /* Global linkage relocation. The value of this relocation |
4818 | is the address of the entry in the TOC section. */ | |
28a0c103 | 4819 | case R_TCL: |
aadf04f7 | 4820 | /* Local object TOC address. I can't figure out the |
28a0c103 ILT |
4821 | difference between this and case R_GL. */ |
4822 | case R_TRL: | |
aadf04f7 SS |
4823 | /* TOC relative relocation. A TOC relative load instruction |
4824 | which may be changed to a load address instruction. | |
4825 | FIXME: We don't currently implement this optimization. */ | |
28a0c103 | 4826 | case R_TRLA: |
aadf04f7 SS |
4827 | /* TOC relative relocation. This is a TOC relative load |
4828 | address instruction which may be changed to a load | |
4829 | instruction. FIXME: I don't know if this is the correct | |
4830 | implementation. */ | |
28a0c103 ILT |
4831 | if (h != NULL && h->toc_section == NULL) |
4832 | { | |
4833 | (*_bfd_error_handler) | |
4834 | ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry", | |
4835 | bfd_get_filename (input_bfd), rel->r_vaddr, | |
4836 | h->root.root.string); | |
4837 | bfd_set_error (bfd_error_bad_value); | |
4838 | return false; | |
4839 | } | |
4840 | if (h != NULL) | |
4841 | val = (h->toc_section->output_section->vma | |
4842 | + h->toc_section->output_offset | |
4843 | + h->toc_offset); | |
aadf04f7 SS |
4844 | val = ((val - xcoff_data (output_bfd)->toc) |
4845 | - (sym->n_value - xcoff_data (input_bfd)->toc)); | |
4846 | addend = 0; | |
4847 | break; | |
28a0c103 | 4848 | case R_BA: |
aadf04f7 SS |
4849 | /* Absolute branch. We don't want to mess with the lower |
4850 | two bits of the instruction. */ | |
28a0c103 | 4851 | case R_CAI: |
aadf04f7 SS |
4852 | /* The PowerPC ABI defines this as an absolute call which |
4853 | may be modified to become a relative call. The PowerOpen | |
4854 | ABI does not define this relocation type. */ | |
28a0c103 | 4855 | case R_RBA: |
aadf04f7 SS |
4856 | /* Absolute branch which may be modified to become a |
4857 | relative branch. */ | |
28a0c103 | 4858 | case R_RBAC: |
aadf04f7 SS |
4859 | /* The PowerPC ABI defines this as an absolute branch to a |
4860 | fixed address which may be modified to an absolute branch | |
4861 | to a symbol. The PowerOpen ABI does not define this | |
4862 | relocation type. */ | |
28a0c103 | 4863 | case R_RBRC: |
aadf04f7 SS |
4864 | /* The PowerPC ABI defines this as an absolute branch to a |
4865 | fixed address which may be modified to a relative branch. | |
4866 | The PowerOpen ABI does not define this relocation type. */ | |
4867 | howto.src_mask &= ~3; | |
4868 | howto.dst_mask = howto.src_mask; | |
4869 | break; | |
28a0c103 | 4870 | case R_BR: |
aadf04f7 SS |
4871 | /* Relative branch. We don't want to mess with the lower |
4872 | two bits of the instruction. */ | |
28a0c103 | 4873 | case R_CREL: |
aadf04f7 SS |
4874 | /* The PowerPC ABI defines this as a relative call which may |
4875 | be modified to become an absolute call. The PowerOpen | |
4876 | ABI does not define this relocation type. */ | |
28a0c103 | 4877 | case R_RBR: |
aadf04f7 SS |
4878 | /* A relative branch which may be modified to become an |
4879 | absolute branch. FIXME: We don't implement this, | |
4880 | although we should for symbols of storage mapping class | |
4881 | XMC_XO. */ | |
4882 | howto.pc_relative = true; | |
4883 | howto.src_mask &= ~3; | |
4884 | howto.dst_mask = howto.src_mask; | |
4885 | break; | |
28a0c103 | 4886 | case R_RL: |
aadf04f7 SS |
4887 | /* The PowerPC AIX ABI describes this as a load which may be |
4888 | changed to a load address. The PowerOpen ABI says this | |
28a0c103 | 4889 | is the same as case R_POS. */ |
aadf04f7 | 4890 | break; |
28a0c103 | 4891 | case R_RLA: |
aadf04f7 SS |
4892 | /* The PowerPC AIX ABI describes this as a load address |
4893 | which may be changed to a load. The PowerOpen ABI says | |
28a0c103 | 4894 | this is the same as R_POS. */ |
aadf04f7 SS |
4895 | break; |
4896 | } | |
4897 | ||
28a0c103 ILT |
4898 | /* If we see an R_BR or R_RBR reloc which is jumping to global |
4899 | linkage code, and it is followed by an appropriate cror nop | |
4900 | instruction, we replace the cror with lwz r2,20(r1). This | |
4901 | restores the TOC after the glink code. Contrariwise, if the | |
4902 | call is followed by a lwz r2,20(r1), but the call is not | |
4903 | going to global linkage code, we can replace the load with a | |
4904 | cror. */ | |
4905 | if ((rel->r_type == R_BR || rel->r_type == R_RBR) | |
4906 | && h != NULL | |
4907 | && h->root.type == bfd_link_hash_defined | |
4908 | && (rel->r_vaddr - input_section->vma + 8 | |
4909 | <= input_section->_cooked_size)) | |
4910 | { | |
4911 | bfd_byte *pnext; | |
4912 | unsigned long next; | |
4913 | ||
4914 | pnext = contents + (rel->r_vaddr - input_section->vma) + 4; | |
4915 | next = bfd_get_32 (input_bfd, pnext); | |
4916 | if (h->smclas == XMC_GL) | |
4917 | { | |
4918 | if (next == 0x4def7b82 /* cror 15,15,15 */ | |
4919 | || next == 0x4ffffb82) /* cror 31,31,31 */ | |
4920 | bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */ | |
4921 | } | |
4922 | else | |
4923 | { | |
4924 | if (next == 0x80410014) /* lwz r1,20(r1) */ | |
4925 | bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */ | |
4926 | } | |
4927 | } | |
4928 | ||
4929 | /* A PC relative reloc includes the section address. */ | |
4930 | if (howto.pc_relative) | |
4931 | addend += input_section->vma; | |
4932 | ||
aadf04f7 SS |
4933 | rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section, |
4934 | contents, | |
4935 | rel->r_vaddr - input_section->vma, | |
4936 | val, addend); | |
4937 | ||
4938 | switch (rstat) | |
4939 | { | |
4940 | default: | |
4941 | abort (); | |
4942 | case bfd_reloc_ok: | |
4943 | break; | |
4944 | case bfd_reloc_overflow: | |
4945 | { | |
4946 | const char *name; | |
4947 | char buf[SYMNMLEN + 1]; | |
4948 | char howto_name[10]; | |
4949 | ||
4950 | if (symndx == -1) | |
4951 | name = "*ABS*"; | |
4952 | else if (h != NULL) | |
4953 | name = h->root.root.string; | |
4954 | else | |
4955 | { | |
4956 | name = _bfd_coff_internal_syment_name (input_bfd, sym, buf); | |
4957 | if (name == NULL) | |
4958 | return false; | |
4959 | } | |
4960 | sprintf (howto_name, "0x%02x", rel->r_type); | |
4961 | ||
4962 | if (! ((*info->callbacks->reloc_overflow) | |
4963 | (info, name, howto_name, (bfd_vma) 0, input_bfd, | |
4964 | input_section, rel->r_vaddr - input_section->vma))) | |
4965 | return false; | |
4966 | } | |
4967 | } | |
4968 | } | |
4969 | ||
4970 | return true; | |
4971 | } |