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