Update release README with new version numbers
[binutils-gdb.git] / bfd / coff-rs6000.c
blob6ad77ba77c417434d947e82c204def4beebfcd7b
1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2 Copyright (C) 1990-2022 Free Software Foundation, Inc.
3 Written by Metin G. Ozisik, Mimi Phuong-Thao Vo, and John Gilmore.
4 Archive support from Damon A. Permezel.
5 Contributed by IBM Corporation and Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #include "sysdep.h"
25 #include "libiberty.h"
26 #include "bfd.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "coff/internal.h"
30 #include "coff/xcoff.h"
31 #include "coff/rs6000.h"
32 #include "libcoff.h"
33 #include "libxcoff.h"
35 extern bool _bfd_xcoff_mkobject (bfd *);
36 extern bool _bfd_xcoff_copy_private_bfd_data (bfd *, bfd *);
37 extern bool _bfd_xcoff_is_local_label_name (bfd *, const char *);
38 extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
39 (bfd *, bfd_reloc_code_real_type);
40 extern bool _bfd_xcoff_slurp_armap (bfd *);
41 extern bfd_cleanup _bfd_xcoff_archive_p (bfd *);
42 extern void * _bfd_xcoff_read_ar_hdr (bfd *);
43 extern bfd *_bfd_xcoff_openr_next_archived_file (bfd *, bfd *);
44 extern int _bfd_xcoff_stat_arch_elt (bfd *, struct stat *);
45 extern bool _bfd_xcoff_write_armap
46 (bfd *, unsigned int, struct orl *, unsigned int, int);
47 extern bool _bfd_xcoff_write_archive_contents (bfd *);
48 extern int _bfd_xcoff_sizeof_headers (bfd *, struct bfd_link_info *);
49 extern void _bfd_xcoff_swap_sym_in (bfd *, void *, void *);
50 extern unsigned int _bfd_xcoff_swap_sym_out (bfd *, void *, void *);
51 extern void _bfd_xcoff_swap_aux_in (bfd *, void *, int, int, int, int, void *);
52 extern unsigned int _bfd_xcoff_swap_aux_out
53 (bfd *, void *, int, int, int, int, void *);
54 static void xcoff_swap_reloc_in (bfd *, void *, void *);
55 static unsigned int xcoff_swap_reloc_out (bfd *, void *, void *);
57 /* Forward declare xcoff_rtype2howto for coffcode.h macro. */
58 void xcoff_rtype2howto (arelent *, struct internal_reloc *);
60 /* coffcode.h needs these to be defined. */
61 #define RS6000COFF_C 1
63 #define SELECT_RELOC(internal, howto) \
64 { \
65 internal.r_type = howto->type; \
66 internal.r_size = \
67 ((howto->complain_on_overflow == complain_overflow_signed \
68 ? 0x80 \
69 : 0) \
70 | (howto->bitsize - 1)); \
73 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
74 #define COFF_LONG_FILENAMES
75 #define NO_COFF_SYMBOLS
76 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
77 #define coff_mkobject _bfd_xcoff_mkobject
78 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
79 #ifdef AIX_CORE
80 extern bfd_cleanup rs6000coff_core_p (bfd *abfd);
81 extern bool rs6000coff_core_file_matches_executable_p
82 (bfd *cbfd, bfd *ebfd);
83 extern char *rs6000coff_core_file_failing_command (bfd *abfd);
84 extern int rs6000coff_core_file_failing_signal (bfd *abfd);
85 #define CORE_FILE_P rs6000coff_core_p
86 #define coff_core_file_failing_command \
87 rs6000coff_core_file_failing_command
88 #define coff_core_file_failing_signal \
89 rs6000coff_core_file_failing_signal
90 #define coff_core_file_matches_executable_p \
91 rs6000coff_core_file_matches_executable_p
92 #define coff_core_file_pid \
93 _bfd_nocore_core_file_pid
94 #else
95 #define CORE_FILE_P _bfd_dummy_target
96 #define coff_core_file_failing_command \
97 _bfd_nocore_core_file_failing_command
98 #define coff_core_file_failing_signal \
99 _bfd_nocore_core_file_failing_signal
100 #define coff_core_file_matches_executable_p \
101 _bfd_nocore_core_file_matches_executable_p
102 #define coff_core_file_pid \
103 _bfd_nocore_core_file_pid
104 #endif
105 #define coff_SWAP_sym_in _bfd_xcoff_swap_sym_in
106 #define coff_SWAP_sym_out _bfd_xcoff_swap_sym_out
107 #define coff_SWAP_aux_in _bfd_xcoff_swap_aux_in
108 #define coff_SWAP_aux_out _bfd_xcoff_swap_aux_out
109 #define coff_swap_reloc_in xcoff_swap_reloc_in
110 #define coff_swap_reloc_out xcoff_swap_reloc_out
111 #define NO_COFF_RELOCS
113 #ifndef bfd_pe_print_pdata
114 #define bfd_pe_print_pdata NULL
115 #endif
117 #include "coffcode.h"
119 /* The main body of code is in coffcode.h. */
121 static const char *normalize_filename (bfd *);
122 static bool xcoff_write_armap_old
123 (bfd *, unsigned int, struct orl *, unsigned int, int);
124 static bool xcoff_write_armap_big
125 (bfd *, unsigned int, struct orl *, unsigned int, int);
126 static bool xcoff_write_archive_contents_old (bfd *);
127 static bool xcoff_write_archive_contents_big (bfd *);
128 static void xcoff_swap_ldhdr_in (bfd *, const void *, struct internal_ldhdr *);
129 static void xcoff_swap_ldhdr_out (bfd *, const struct internal_ldhdr *, void *);
130 static void xcoff_swap_ldsym_in (bfd *, const void *, struct internal_ldsym *);
131 static void xcoff_swap_ldsym_out (bfd *, const struct internal_ldsym *, void *);
132 static void xcoff_swap_ldrel_in (bfd *, const void *, struct internal_ldrel *);
133 static void xcoff_swap_ldrel_out (bfd *, const struct internal_ldrel *, void *);
134 static bool xcoff_ppc_relocate_section
135 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
136 struct internal_reloc *, struct internal_syment *, asection **);
137 static bool _bfd_xcoff_put_ldsymbol_name
138 (bfd *, struct xcoff_loader_info *, struct internal_ldsym *, const char *);
139 static asection *xcoff_create_csect_from_smclas
140 (bfd *, union internal_auxent *, const char *);
141 static bool xcoff_is_lineno_count_overflow (bfd *, bfd_vma);
142 static bool xcoff_is_reloc_count_overflow (bfd *, bfd_vma);
143 static bfd_vma xcoff_loader_symbol_offset (bfd *, struct internal_ldhdr *);
144 static bfd_vma xcoff_loader_reloc_offset (bfd *, struct internal_ldhdr *);
145 static bool xcoff_generate_rtinit
146 (bfd *, const char *, const char *, bool);
147 static bool do_pad (bfd *, unsigned int);
148 static bool do_copy (bfd *, bfd *);
150 /* Relocation functions */
151 static xcoff_reloc_function xcoff_reloc_type_br;
153 static xcoff_complain_function xcoff_complain_overflow_dont_func;
154 static xcoff_complain_function xcoff_complain_overflow_bitfield_func;
155 static xcoff_complain_function xcoff_complain_overflow_signed_func;
156 static xcoff_complain_function xcoff_complain_overflow_unsigned_func;
158 xcoff_reloc_function *const
159 xcoff_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION] =
161 xcoff_reloc_type_pos, /* R_POS (0x00) */
162 xcoff_reloc_type_neg, /* R_NEG (0x01) */
163 xcoff_reloc_type_rel, /* R_REL (0x02) */
164 xcoff_reloc_type_toc, /* R_TOC (0x03) */
165 xcoff_reloc_type_toc, /* R_TRL (0x04) */
166 xcoff_reloc_type_toc, /* R_GL (0x05) */
167 xcoff_reloc_type_toc, /* R_TCL (0x06) */
168 xcoff_reloc_type_fail, /* (0x07) */
169 xcoff_reloc_type_ba, /* R_BA (0x08) */
170 xcoff_reloc_type_fail, /* (0x09) */
171 xcoff_reloc_type_br, /* R_BR (0x0a) */
172 xcoff_reloc_type_fail, /* (0x0b) */
173 xcoff_reloc_type_pos, /* R_RL (0x0c) */
174 xcoff_reloc_type_pos, /* R_RLA (0x0d) */
175 xcoff_reloc_type_fail, /* (0x0e) */
176 xcoff_reloc_type_noop, /* R_REF (0x0f) */
177 xcoff_reloc_type_fail, /* (0x10) */
178 xcoff_reloc_type_fail, /* (0x11) */
179 xcoff_reloc_type_fail, /* (0x12) */
180 xcoff_reloc_type_toc, /* R_TRLA (0x13) */
181 xcoff_reloc_type_fail, /* R_RRTBI (0x14) */
182 xcoff_reloc_type_fail, /* R_RRTBA (0x15) */
183 xcoff_reloc_type_ba, /* R_CAI (0x16) */
184 xcoff_reloc_type_crel, /* R_CREL (0x17) */
185 xcoff_reloc_type_ba, /* R_RBA (0x18) */
186 xcoff_reloc_type_ba, /* R_RBAC (0x19) */
187 xcoff_reloc_type_br, /* R_RBR (0x1a) */
188 xcoff_reloc_type_ba, /* R_RBRC (0x1b) */
189 xcoff_reloc_type_fail, /* (0x1c) */
190 xcoff_reloc_type_fail, /* (0x1d) */
191 xcoff_reloc_type_fail, /* (0x1e) */
192 xcoff_reloc_type_fail, /* (0x1f) */
193 xcoff_reloc_type_tls, /* R_TLS (0x20) */
194 xcoff_reloc_type_tls, /* R_TLS_IE (0x21) */
195 xcoff_reloc_type_tls, /* R_TLS_LD (0x22) */
196 xcoff_reloc_type_tls, /* R_TLS_LE (0x23) */
197 xcoff_reloc_type_tls, /* R_TLSM (0x24) */
198 xcoff_reloc_type_tls, /* R_TLSML (0x25) */
199 xcoff_reloc_type_fail, /* (0x26) */
200 xcoff_reloc_type_fail, /* (0x27) */
201 xcoff_reloc_type_fail, /* (0x28) */
202 xcoff_reloc_type_fail, /* (0x29) */
203 xcoff_reloc_type_fail, /* (0x2a) */
204 xcoff_reloc_type_fail, /* (0x2b) */
205 xcoff_reloc_type_fail, /* (0x2c) */
206 xcoff_reloc_type_fail, /* (0x2d) */
207 xcoff_reloc_type_fail, /* (0x2e) */
208 xcoff_reloc_type_fail, /* (0x2f) */
209 xcoff_reloc_type_toc, /* R_TOCU (0x30) */
210 xcoff_reloc_type_toc, /* R_TOCL (0x31) */
213 xcoff_complain_function *const
214 xcoff_complain_overflow[XCOFF_MAX_COMPLAIN_OVERFLOW] =
216 xcoff_complain_overflow_dont_func,
217 xcoff_complain_overflow_bitfield_func,
218 xcoff_complain_overflow_signed_func,
219 xcoff_complain_overflow_unsigned_func,
222 /* Information about one member of an archive. */
223 struct member_layout
225 /* The archive member that this structure describes. */
226 bfd *member;
228 /* The number of bytes of padding that must be inserted before the
229 start of the member in order to ensure that the section contents
230 are correctly aligned. */
231 unsigned int leading_padding;
233 /* The offset of MEMBER from the start of the archive (i.e. the end
234 of the leading padding). */
235 file_ptr offset;
237 /* The normalized name of MEMBER. */
238 const char *name;
240 /* The length of NAME, without padding. */
241 bfd_size_type namlen;
243 /* The length of NAME, with padding. */
244 bfd_size_type padded_namlen;
246 /* The size of MEMBER's header, including the name and magic sequence. */
247 bfd_size_type header_size;
249 /* The size of the MEMBER's contents. */
250 bfd_size_type contents_size;
252 /* The number of bytes of padding that must be inserted after MEMBER
253 in order to preserve even alignment. */
254 bfd_size_type trailing_padding;
257 /* A structure used for iterating over the members of an archive. */
258 struct archive_iterator
260 /* The archive itself. */
261 bfd *archive;
263 /* Information about the current archive member. */
264 struct member_layout current;
266 /* Information about the next archive member. MEMBER is null if there
267 are no more archive members, in which case OFFSET is the offset of
268 the first unused byte. */
269 struct member_layout next;
272 /* Initialize INFO so that it describes member MEMBER of archive ARCHIVE.
273 OFFSET is the even-padded offset of MEMBER, not including any leading
274 padding needed for section alignment. */
276 static void
277 member_layout_init (struct member_layout *info, bfd *archive,
278 bfd *member, file_ptr offset)
280 info->member = member;
281 info->leading_padding = 0;
282 if (member)
284 info->name = normalize_filename (member);
285 info->namlen = strlen (info->name);
286 info->padded_namlen = info->namlen + (info->namlen & 1);
287 if (xcoff_big_format_p (archive))
288 info->header_size = SIZEOF_AR_HDR_BIG;
289 else
290 info->header_size = SIZEOF_AR_HDR;
291 info->header_size += info->padded_namlen + SXCOFFARFMAG;
292 info->contents_size = arelt_size (member);
293 info->trailing_padding = info->contents_size & 1;
295 if (bfd_check_format (member, bfd_object)
296 && bfd_get_flavour (member) == bfd_target_xcoff_flavour
297 && (member->flags & DYNAMIC) != 0)
298 info->leading_padding
299 = (-(offset + info->header_size)
300 & ((1 << bfd_xcoff_text_align_power (member)) - 1));
302 info->offset = offset + info->leading_padding;
305 /* Set up ITERATOR to iterate through archive ARCHIVE. */
307 static void
308 archive_iterator_begin (struct archive_iterator *iterator,
309 bfd *archive)
311 iterator->archive = archive;
312 member_layout_init (&iterator->next, archive, archive->archive_head,
313 xcoff_big_format_p (archive)
314 ? SIZEOF_AR_FILE_HDR_BIG
315 : SIZEOF_AR_FILE_HDR);
318 /* Make ITERATOR visit the first unvisited archive member. Return true
319 on success; return false if all members have been visited. */
321 static bool
322 archive_iterator_next (struct archive_iterator *iterator)
324 if (!iterator->next.member)
325 return false;
327 iterator->current = iterator->next;
328 member_layout_init (&iterator->next, iterator->archive,
329 iterator->current.member->archive_next,
330 iterator->current.offset
331 + iterator->current.header_size
332 + iterator->current.contents_size
333 + iterator->current.trailing_padding);
334 return true;
337 /* We use our own tdata type. Its first field is the COFF tdata type,
338 so the COFF routines are compatible. */
340 bool
341 _bfd_xcoff_mkobject (bfd *abfd)
343 coff_data_type *coff;
344 size_t amt = sizeof (struct xcoff_tdata);
346 abfd->tdata.xcoff_obj_data = (struct xcoff_tdata *) bfd_zalloc (abfd, amt);
347 if (abfd->tdata.xcoff_obj_data == NULL)
348 return false;
349 coff = coff_data (abfd);
350 coff->symbols = (coff_symbol_type *) NULL;
351 coff->conversion_table = (unsigned int *) NULL;
352 coff->raw_syments = (struct coff_ptr_struct *) NULL;
353 coff->relocbase = 0;
355 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
357 /* We set cputype to -1 to indicate that it has not been
358 initialized. */
359 xcoff_data (abfd)->cputype = -1;
361 xcoff_data (abfd)->csects = NULL;
362 xcoff_data (abfd)->debug_indices = NULL;
364 /* text section alignment is different than the default */
365 bfd_xcoff_text_align_power (abfd) = 2;
367 return true;
370 /* Copy XCOFF data from one BFD to another. */
372 bool
373 _bfd_xcoff_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
375 struct xcoff_tdata *ix, *ox;
376 asection *sec;
378 if (ibfd->xvec != obfd->xvec)
379 return true;
380 ix = xcoff_data (ibfd);
381 ox = xcoff_data (obfd);
382 ox->full_aouthdr = ix->full_aouthdr;
383 ox->toc = ix->toc;
384 if (ix->sntoc == 0)
385 ox->sntoc = 0;
386 else
388 sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
389 if (sec == NULL || sec->output_section == NULL)
390 ox->sntoc = 0;
391 else
392 ox->sntoc = sec->output_section->target_index;
394 if (ix->snentry == 0)
395 ox->snentry = 0;
396 else
398 sec = coff_section_from_bfd_index (ibfd, ix->snentry);
399 if (sec == NULL || sec->output_section == NULL)
400 ox->snentry = 0;
401 else
402 ox->snentry = sec->output_section->target_index;
404 bfd_xcoff_text_align_power (obfd) = bfd_xcoff_text_align_power (ibfd);
405 bfd_xcoff_data_align_power (obfd) = bfd_xcoff_data_align_power (ibfd);
406 ox->modtype = ix->modtype;
407 ox->cputype = ix->cputype;
408 ox->maxdata = ix->maxdata;
409 ox->maxstack = ix->maxstack;
410 return true;
413 /* I don't think XCOFF really has a notion of local labels based on
414 name. This will mean that ld -X doesn't actually strip anything.
415 The AIX native linker does not have a -X option, and it ignores the
416 -x option. */
418 bool
419 _bfd_xcoff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
420 const char *name ATTRIBUTE_UNUSED)
422 return false;
425 void
426 _bfd_xcoff_swap_sym_in (bfd *abfd, void * ext1, void * in1)
428 SYMENT *ext = (SYMENT *)ext1;
429 struct internal_syment * in = (struct internal_syment *)in1;
431 if (ext->e.e_name[0] != 0)
433 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
435 else
437 in->_n._n_n._n_zeroes = 0;
438 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
441 in->n_value = H_GET_32 (abfd, ext->e_value);
442 in->n_scnum = (short) H_GET_16 (abfd, ext->e_scnum);
443 in->n_type = H_GET_16 (abfd, ext->e_type);
444 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
445 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
448 unsigned int
449 _bfd_xcoff_swap_sym_out (bfd *abfd, void * inp, void * extp)
451 struct internal_syment *in = (struct internal_syment *)inp;
452 SYMENT *ext =(SYMENT *)extp;
454 if (in->_n._n_name[0] != 0)
456 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
458 else
460 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
461 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
464 H_PUT_32 (abfd, in->n_value, ext->e_value);
465 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
466 H_PUT_16 (abfd, in->n_type, ext->e_type);
467 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
468 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
469 return bfd_coff_symesz (abfd);
472 void
473 _bfd_xcoff_swap_aux_in (bfd *abfd, void * ext1, int type ATTRIBUTE_UNUSED,
474 int in_class, int indx, int numaux, void * in1)
476 AUXENT * ext = (AUXENT *)ext1;
477 union internal_auxent *in = (union internal_auxent *)in1;
479 switch (in_class)
481 default:
482 _bfd_error_handler
483 /* xgettext: c-format */
484 (_("%pB: unsupported swap_aux_in for storage class %#x"),
485 abfd, (unsigned int) in_class);
486 bfd_set_error (bfd_error_bad_value);
487 break;
489 case C_FILE:
490 if (ext->x_file.x_n.x_fname[0] == 0)
492 in->x_file.x_n.x_n.x_zeroes = 0;
493 in->x_file.x_n.x_n.x_offset =
494 H_GET_32 (abfd, ext->x_file.x_n.x_n.x_offset);
496 else
497 memcpy (in->x_file.x_n.x_fname, ext->x_file.x_n.x_fname, FILNMLEN);
498 in->x_file.x_ftype = H_GET_8 (abfd, ext->x_file.x_ftype);
499 break;
501 /* RS/6000 "csect" auxents.
502 There is always a CSECT auxiliary entry. But functions can
503 have FCN ones too. In this case, CSECT is always the last
504 one. */
505 case C_EXT:
506 case C_AIX_WEAKEXT:
507 case C_HIDEXT:
508 if (indx + 1 == numaux)
510 in->x_csect.x_scnlen.l = H_GET_32 (abfd, ext->x_csect.x_scnlen);
511 in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
512 in->x_csect.x_snhash = H_GET_16 (abfd, ext->x_csect.x_snhash);
513 /* We don't have to hack bitfields in x_smtyp because it's
514 defined by shifts-and-ands, which are equivalent on all
515 byte orders. */
516 in->x_csect.x_smtyp = H_GET_8 (abfd, ext->x_csect.x_smtyp);
517 in->x_csect.x_smclas = H_GET_8 (abfd, ext->x_csect.x_smclas);
518 in->x_csect.x_stab = H_GET_32 (abfd, ext->x_csect.x_stab);
519 in->x_csect.x_snstab = H_GET_16 (abfd, ext->x_csect.x_snstab);
521 else
523 /* x_exptr isn't supported. */
524 in->x_sym.x_misc.x_fsize
525 = H_GET_32 (abfd, ext->x_fcn.x_fsize);
526 in->x_sym.x_fcnary.x_fcn.x_lnnoptr
527 = H_GET_32 (abfd, ext->x_fcn.x_lnnoptr);
528 in->x_sym.x_fcnary.x_fcn.x_endndx.l
529 = H_GET_32 (abfd, ext->x_fcn.x_endndx);
531 break;
533 case C_STAT:
534 in->x_scn.x_scnlen = H_GET_32 (abfd, ext->x_scn.x_scnlen);
535 in->x_scn.x_nreloc = H_GET_16 (abfd, ext->x_scn.x_nreloc);
536 in->x_scn.x_nlinno = H_GET_16 (abfd, ext->x_scn.x_nlinno);
537 /* PE defines some extra fields; we zero them out for
538 safety. */
539 in->x_scn.x_checksum = 0;
540 in->x_scn.x_associated = 0;
541 in->x_scn.x_comdat = 0;
542 break;
544 case C_BLOCK:
545 case C_FCN:
546 in->x_sym.x_misc.x_lnsz.x_lnno
547 = H_GET_32 (abfd, ext->x_sym.x_lnno);
548 break;
550 case C_DWARF:
551 in->x_sect.x_scnlen = H_GET_32 (abfd, ext->x_sect.x_scnlen);
552 in->x_sect.x_nreloc = H_GET_32 (abfd, ext->x_sect.x_nreloc);
553 break;
558 unsigned int
559 _bfd_xcoff_swap_aux_out (bfd *abfd, void * inp, int type ATTRIBUTE_UNUSED,
560 int in_class, int indx, int numaux, void * extp)
562 union internal_auxent *in = (union internal_auxent *)inp;
563 AUXENT *ext = (AUXENT *)extp;
565 memset (ext, 0, bfd_coff_auxesz (abfd));
566 switch (in_class)
568 default:
569 _bfd_error_handler
570 /* xgettext: c-format */
571 (_("%pB: unsupported swap_aux_out for storage class %#x"),
572 abfd, (unsigned int) in_class);
573 bfd_set_error (bfd_error_bad_value);
574 break;
576 case C_FILE:
577 if (in->x_file.x_n.x_fname[0] == 0)
579 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_n.x_zeroes);
580 H_PUT_32 (abfd, in->x_file.x_n.x_n.x_offset,
581 ext->x_file.x_n.x_n.x_offset);
583 else
584 memcpy (ext->x_file.x_n.x_fname, in->x_file.x_n.x_fname, FILNMLEN);
585 H_PUT_8 (abfd, in->x_file.x_ftype, ext->x_file.x_ftype);
586 break;
588 /* RS/6000 "csect" auxents */
589 case C_EXT:
590 case C_AIX_WEAKEXT:
591 case C_HIDEXT:
592 if (indx + 1 == numaux)
594 H_PUT_32 (abfd, in->x_csect.x_scnlen.l, ext->x_csect.x_scnlen);
595 H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
596 H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
597 /* We don't have to hack bitfields in x_smtyp because it's
598 defined by shifts-and-ands, which are equivalent on all
599 byte orders. */
600 H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
601 H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
602 H_PUT_32 (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
603 H_PUT_16 (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
605 else
607 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_fcn.x_fsize);
608 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
609 ext->x_fcn.x_lnnoptr);
610 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
611 ext->x_fcn.x_endndx);
613 break;
615 case C_STAT:
616 H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen);
617 H_PUT_16 (abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc);
618 H_PUT_16 (abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno);
619 break;
621 case C_BLOCK:
622 case C_FCN:
623 H_PUT_32 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext->x_sym.x_lnno);
624 break;
626 case C_DWARF:
627 H_PUT_32 (abfd, in->x_sect.x_scnlen, ext->x_sect.x_scnlen);
628 H_PUT_32 (abfd, in->x_sect.x_nreloc, ext->x_sect.x_nreloc);
629 break;
632 return bfd_coff_auxesz (abfd);
635 /* The XCOFF reloc table.
636 XCOFF relocations aren't defined only by the type field r_type.
637 The bitsize and whether they are signed or not, are defined by
638 r_size field. Thus, it's complicated to create a constant
639 table reference every possible relocation.
640 This table contains the "default" relocation and few modified
641 relocations what were already there. It's enough when
642 xcoff_rtype2howto is called.
643 For relocations from an input bfd to an output bfd, the default
644 relocation is retrieved and when manually adapted.
646 For now, it seems to be enought. */
648 reloc_howto_type xcoff_howto_table[] =
650 /* 0x00: Standard 32 bit relocation. */
651 HOWTO (R_POS, /* type */
652 0, /* rightshift */
653 4, /* size */
654 32, /* bitsize */
655 false, /* pc_relative */
656 0, /* bitpos */
657 complain_overflow_bitfield, /* complain_on_overflow */
658 0, /* special_function */
659 "R_POS", /* name */
660 true, /* partial_inplace */
661 0xffffffff, /* src_mask */
662 0xffffffff, /* dst_mask */
663 false), /* pcrel_offset */
665 /* 0x01: 32 bit relocation, but store negative value. */
666 HOWTO (R_NEG, /* type */
667 0, /* rightshift */
668 -4, /* size */
669 32, /* bitsize */
670 false, /* pc_relative */
671 0, /* bitpos */
672 complain_overflow_bitfield, /* complain_on_overflow */
673 0, /* special_function */
674 "R_NEG", /* name */
675 true, /* partial_inplace */
676 0xffffffff, /* src_mask */
677 0xffffffff, /* dst_mask */
678 false), /* pcrel_offset */
680 /* 0x02: 32 bit PC relative relocation. */
681 HOWTO (R_REL, /* type */
682 0, /* rightshift */
683 4, /* size */
684 32, /* bitsize */
685 true, /* pc_relative */
686 0, /* bitpos */
687 complain_overflow_signed, /* complain_on_overflow */
688 0, /* special_function */
689 "R_REL", /* name */
690 true, /* partial_inplace */
691 0xffffffff, /* src_mask */
692 0xffffffff, /* dst_mask */
693 false), /* pcrel_offset */
695 /* 0x03: 16 bit TOC relative relocation. */
696 HOWTO (R_TOC, /* type */
697 0, /* rightshift */
698 2, /* size */
699 16, /* bitsize */
700 false, /* pc_relative */
701 0, /* bitpos */
702 complain_overflow_bitfield, /* complain_on_overflow */
703 0, /* special_function */
704 "R_TOC", /* name */
705 true, /* partial_inplace */
706 0, /* src_mask */
707 0xffff, /* dst_mask */
708 false), /* pcrel_offset */
710 /* 0x04: Same as R_TOC */
711 HOWTO (R_TRL, /* type */
712 0, /* rightshift */
713 2, /* size */
714 16, /* bitsize */
715 false, /* pc_relative */
716 0, /* bitpos */
717 complain_overflow_bitfield, /* complain_on_overflow */
718 0, /* special_function */
719 "R_TRL", /* name */
720 true, /* partial_inplace */
721 0, /* src_mask */
722 0xffff, /* dst_mask */
723 false), /* pcrel_offset */
725 /* 0x05: External TOC relative symbol. */
726 HOWTO (R_GL, /* type */
727 0, /* rightshift */
728 2, /* size */
729 16, /* bitsize */
730 false, /* pc_relative */
731 0, /* bitpos */
732 complain_overflow_bitfield, /* complain_on_overflow */
733 0, /* special_function */
734 "R_GL", /* name */
735 true, /* partial_inplace */
736 0, /* src_mask */
737 0xffff, /* dst_mask */
738 false), /* pcrel_offset */
740 /* 0x06: Local TOC relative symbol. */
741 HOWTO (R_TCL, /* type */
742 0, /* rightshift */
743 2, /* size */
744 16, /* bitsize */
745 false, /* pc_relative */
746 0, /* bitpos */
747 complain_overflow_bitfield, /* complain_on_overflow */
748 0, /* special_function */
749 "R_TCL", /* name */
750 true, /* partial_inplace */
751 0, /* src_mask */
752 0xffff, /* dst_mask */
753 false), /* pcrel_offset */
755 EMPTY_HOWTO (7),
757 /* 0x08: Same as R_RBA. */
758 HOWTO (R_BA, /* type */
759 0, /* rightshift */
760 4, /* size */
761 26, /* bitsize */
762 false, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_bitfield, /* complain_on_overflow */
765 0, /* special_function */
766 "R_BA_26", /* name */
767 true, /* partial_inplace */
768 0x03fffffc, /* src_mask */
769 0x03fffffc, /* dst_mask */
770 false), /* pcrel_offset */
772 EMPTY_HOWTO (9),
774 /* 0x0a: Same as R_RBR. */
775 HOWTO (R_BR, /* type */
776 0, /* rightshift */
777 4, /* size */
778 26, /* bitsize */
779 true, /* pc_relative */
780 0, /* bitpos */
781 complain_overflow_signed, /* complain_on_overflow */
782 0, /* special_function */
783 "R_BR", /* name */
784 true, /* partial_inplace */
785 0x03fffffc, /* src_mask */
786 0x03fffffc, /* dst_mask */
787 false), /* pcrel_offset */
789 EMPTY_HOWTO (0xb),
791 /* 0x0c: Same as R_POS. */
792 HOWTO (R_RL, /* type */
793 0, /* rightshift */
794 4, /* size */
795 32, /* bitsize */
796 false, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_bitfield, /* complain_on_overflow */
799 0, /* special_function */
800 "R_RL", /* name */
801 true, /* partial_inplace */
802 0xffffffff, /* src_mask */
803 0xffffffff, /* dst_mask */
804 false), /* pcrel_offset */
806 /* 0x0d: Same as R_POS. */
807 HOWTO (R_RLA, /* type */
808 0, /* rightshift */
809 4, /* size */
810 32, /* bitsize */
811 false, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_bitfield, /* complain_on_overflow */
814 0, /* special_function */
815 "R_RLA", /* name */
816 true, /* partial_inplace */
817 0xffffffff, /* src_mask */
818 0xffffffff, /* dst_mask */
819 false), /* pcrel_offset */
821 EMPTY_HOWTO (0xe),
823 /* 0x0f: Non-relocating reference. Bitsize is 1 so that r_rsize is 0. */
824 HOWTO (R_REF, /* type */
825 0, /* rightshift */
826 1, /* size */
827 1, /* bitsize */
828 false, /* pc_relative */
829 0, /* bitpos */
830 complain_overflow_dont, /* complain_on_overflow */
831 0, /* special_function */
832 "R_REF", /* name */
833 false, /* partial_inplace */
834 0, /* src_mask */
835 0, /* dst_mask */
836 false), /* pcrel_offset */
838 EMPTY_HOWTO (0x10),
839 EMPTY_HOWTO (0x11),
840 EMPTY_HOWTO (0x12),
842 /* 0x13: Same as R_TOC. */
843 HOWTO (R_TRLA, /* type */
844 0, /* rightshift */
845 2, /* size */
846 16, /* bitsize */
847 false, /* pc_relative */
848 0, /* bitpos */
849 complain_overflow_bitfield, /* complain_on_overflow */
850 0, /* special_function */
851 "R_TRLA", /* name */
852 true, /* partial_inplace */
853 0, /* src_mask */
854 0xffff, /* dst_mask */
855 false), /* pcrel_offset */
857 /* 0x14: Modifiable relative branch. */
858 HOWTO (R_RRTBI, /* type */
859 1, /* rightshift */
860 4, /* size */
861 32, /* bitsize */
862 false, /* pc_relative */
863 0, /* bitpos */
864 complain_overflow_bitfield, /* complain_on_overflow */
865 0, /* special_function */
866 "R_RRTBI", /* name */
867 true, /* partial_inplace */
868 0xffffffff, /* src_mask */
869 0xffffffff, /* dst_mask */
870 false), /* pcrel_offset */
872 /* 0x15: Modifiable absolute branch. */
873 HOWTO (R_RRTBA, /* type */
874 1, /* rightshift */
875 4, /* size */
876 32, /* bitsize */
877 false, /* pc_relative */
878 0, /* bitpos */
879 complain_overflow_bitfield, /* complain_on_overflow */
880 0, /* special_function */
881 "R_RRTBA", /* name */
882 true, /* partial_inplace */
883 0xffffffff, /* src_mask */
884 0xffffffff, /* dst_mask */
885 false), /* pcrel_offset */
887 /* 0x16: Modifiable call absolute indirect. */
888 HOWTO (R_CAI, /* type */
889 0, /* rightshift */
890 2, /* size */
891 16, /* bitsize */
892 false, /* pc_relative */
893 0, /* bitpos */
894 complain_overflow_bitfield, /* complain_on_overflow */
895 0, /* special_function */
896 "R_CAI", /* name */
897 true, /* partial_inplace */
898 0xffff, /* src_mask */
899 0xffff, /* dst_mask */
900 false), /* pcrel_offset */
902 /* 0x17: Modifiable call relative. */
903 HOWTO (R_CREL, /* type */
904 0, /* rightshift */
905 2, /* size */
906 16, /* bitsize */
907 false, /* pc_relative */
908 0, /* bitpos */
909 complain_overflow_bitfield, /* complain_on_overflow */
910 0, /* special_function */
911 "R_CREL", /* name */
912 true, /* partial_inplace */
913 0xffff, /* src_mask */
914 0xffff, /* dst_mask */
915 false), /* pcrel_offset */
917 /* 0x18: Modifiable branch absolute. */
918 HOWTO (R_RBA, /* type */
919 0, /* rightshift */
920 4, /* size */
921 26, /* bitsize */
922 false, /* pc_relative */
923 0, /* bitpos */
924 complain_overflow_bitfield, /* complain_on_overflow */
925 0, /* special_function */
926 "R_RBA", /* name */
927 true, /* partial_inplace */
928 0x03fffffc, /* src_mask */
929 0x03fffffc, /* dst_mask */
930 false), /* pcrel_offset */
932 /* 0x19: Modifiable branch absolute. */
933 HOWTO (R_RBAC, /* type */
934 0, /* rightshift */
935 4, /* size */
936 32, /* bitsize */
937 false, /* pc_relative */
938 0, /* bitpos */
939 complain_overflow_bitfield, /* complain_on_overflow */
940 0, /* special_function */
941 "R_RBAC", /* name */
942 true, /* partial_inplace */
943 0xffffffff, /* src_mask */
944 0xffffffff, /* dst_mask */
945 false), /* pcrel_offset */
947 /* 0x1a: Modifiable branch relative. */
948 HOWTO (R_RBR, /* type */
949 0, /* rightshift */
950 4, /* size */
951 26, /* bitsize */
952 false, /* pc_relative */
953 0, /* bitpos */
954 complain_overflow_signed, /* complain_on_overflow */
955 0, /* special_function */
956 "R_RBR_26", /* name */
957 true, /* partial_inplace */
958 0x03fffffc, /* src_mask */
959 0x03fffffc, /* dst_mask */
960 false), /* pcrel_offset */
962 /* 0x1b: Modifiable branch absolute. */
963 HOWTO (R_RBRC, /* type */
964 0, /* rightshift */
965 2, /* size */
966 16, /* bitsize */
967 false, /* pc_relative */
968 0, /* bitpos */
969 complain_overflow_bitfield, /* complain_on_overflow */
970 0, /* special_function */
971 "R_RBRC", /* name */
972 true, /* partial_inplace */
973 0xffff, /* src_mask */
974 0xffff, /* dst_mask */
975 false), /* pcrel_offset */
977 /* 0x1c: 16 bit Non modifiable absolute branch. */
978 HOWTO (R_BA, /* type */
979 0, /* rightshift */
980 2, /* size */
981 16, /* bitsize */
982 false, /* pc_relative */
983 0, /* bitpos */
984 complain_overflow_bitfield, /* complain_on_overflow */
985 0, /* special_function */
986 "R_BA_16", /* name */
987 true, /* partial_inplace */
988 0xfffc, /* src_mask */
989 0xfffc, /* dst_mask */
990 false), /* pcrel_offset */
992 /* 0x1d: Modifiable branch relative. */
993 HOWTO (R_RBR, /* type */
994 0, /* rightshift */
995 2, /* size */
996 16, /* bitsize */
997 true, /* pc_relative */
998 0, /* bitpos */
999 complain_overflow_signed, /* complain_on_overflow */
1000 0, /* special_function */
1001 "R_RBR_16", /* name */
1002 true, /* partial_inplace */
1003 0xfffc, /* src_mask */
1004 0xfffc, /* dst_mask */
1005 false), /* pcrel_offset */
1007 /* 0x1e: Modifiable branch relative. */
1008 HOWTO (R_RBA, /* type */
1009 0, /* rightshift */
1010 2, /* size */
1011 16, /* bitsize */
1012 false, /* pc_relative */
1013 0, /* bitpos */
1014 complain_overflow_signed, /* complain_on_overflow */
1015 0, /* special_function */
1016 "R_RBA_16", /* name */
1017 true, /* partial_inplace */
1018 0xffff, /* src_mask */
1019 0xffff, /* dst_mask */
1020 false), /* pcrel_offset */
1022 EMPTY_HOWTO (0x1f),
1024 /* 0x20: General-dynamic TLS relocation. */
1025 HOWTO (R_TLS, /* type */
1026 0, /* rightshift */
1027 4, /* size */
1028 32, /* bitsize */
1029 false, /* pc_relative */
1030 0, /* bitpos */
1031 complain_overflow_bitfield, /* complain_on_overflow */
1032 0, /* special_function */
1033 "R_TLS", /* name */
1034 true, /* partial_inplace */
1035 0xffffffff, /* src_mask */
1036 0xffffffff, /* dst_mask */
1037 false), /* pcrel_offset */
1039 /* 0x21: Initial-exec TLS relocation. */
1040 HOWTO (R_TLS_IE, /* type */
1041 0, /* rightshift */
1042 4, /* size */
1043 32, /* bitsize */
1044 false, /* pc_relative */
1045 0, /* bitpos */
1046 complain_overflow_bitfield, /* complain_on_overflow */
1047 0, /* special_function */
1048 "R_TLS_IE", /* name */
1049 true, /* partial_inplace */
1050 0xffffffff, /* src_mask */
1051 0xffffffff, /* dst_mask */
1052 false), /* pcrel_offset */
1054 /* 0x22: Local-dynamic TLS relocation. */
1055 HOWTO (R_TLS_LD, /* type */
1056 0, /* rightshift */
1057 4, /* size */
1058 32, /* bitsize */
1059 false, /* pc_relative */
1060 0, /* bitpos */
1061 complain_overflow_bitfield, /* complain_on_overflow */
1062 0, /* special_function */
1063 "R_TLS_LD", /* name */
1064 true, /* partial_inplace */
1065 0xffffffff, /* src_mask */
1066 0xffffffff, /* dst_mask */
1067 false), /* pcrel_offset */
1069 /* 0x23: Local-exec TLS relocation. */
1070 HOWTO (R_TLS_LE, /* type */
1071 0, /* rightshift */
1072 4, /* size */
1073 32, /* bitsize */
1074 false, /* pc_relative */
1075 0, /* bitpos */
1076 complain_overflow_bitfield, /* complain_on_overflow */
1077 0, /* special_function */
1078 "R_TLS_LE", /* name */
1079 true, /* partial_inplace */
1080 0xffffffff, /* src_mask */
1081 0xffffffff, /* dst_mask */
1082 false), /* pcrel_offset */
1084 /* 0x24: TLS relocation. */
1085 HOWTO (R_TLSM, /* type */
1086 0, /* rightshift */
1087 4, /* size */
1088 32, /* bitsize */
1089 false, /* pc_relative */
1090 0, /* bitpos */
1091 complain_overflow_bitfield, /* complain_on_overflow */
1092 0, /* special_function */
1093 "R_TLSM", /* name */
1094 true, /* partial_inplace */
1095 0xffffffff, /* src_mask */
1096 0xffffffff, /* dst_mask */
1097 false), /* pcrel_offset */
1100 /* 0x25: TLS module relocation. */
1101 HOWTO (R_TLSML, /* type */
1102 0, /* rightshift */
1103 4, /* size */
1104 32, /* bitsize */
1105 false, /* pc_relative */
1106 0, /* bitpos */
1107 complain_overflow_bitfield, /* complain_on_overflow */
1108 0, /* special_function */
1109 "R_TLSML", /* name */
1110 true, /* partial_inplace */
1111 0xffffffff, /* src_mask */
1112 0xffffffff, /* dst_mask */
1113 false), /* pcrel_offset */
1115 EMPTY_HOWTO(0x26),
1116 EMPTY_HOWTO(0x27),
1117 EMPTY_HOWTO(0x28),
1118 EMPTY_HOWTO(0x29),
1119 EMPTY_HOWTO(0x2a),
1120 EMPTY_HOWTO(0x2b),
1121 EMPTY_HOWTO(0x2c),
1122 EMPTY_HOWTO(0x2d),
1123 EMPTY_HOWTO(0x2e),
1124 EMPTY_HOWTO(0x2f),
1126 /* 0x30: High-order 16 bit TOC relative relocation. */
1127 HOWTO (R_TOCU, /* type */
1128 16, /* rightshift */
1129 2, /* size */
1130 16, /* bitsize */
1131 false, /* pc_relative */
1132 0, /* bitpos */
1133 complain_overflow_bitfield, /* complain_on_overflow */
1134 0, /* special_function */
1135 "R_TOCU", /* name */
1136 true, /* partial_inplace */
1137 0, /* src_mask */
1138 0xffff, /* dst_mask */
1139 false), /* pcrel_offset */
1141 /* 0x31: Low-order 16 bit TOC relative relocation. */
1142 HOWTO (R_TOCL, /* type */
1143 0, /* rightshift */
1144 2, /* size */
1145 16, /* bitsize */
1146 false, /* pc_relative */
1147 0, /* bitpos */
1148 complain_overflow_dont, /* complain_on_overflow */
1149 0, /* special_function */
1150 "R_TOCL", /* name */
1151 true, /* partial_inplace */
1152 0, /* src_mask */
1153 0xffff, /* dst_mask */
1154 false), /* pcrel_offset */
1158 void
1159 xcoff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1161 if (internal->r_type > R_TOCL)
1162 abort ();
1164 /* Default howto layout works most of the time */
1165 relent->howto = &xcoff_howto_table[internal->r_type];
1167 /* Special case some 16 bit reloc */
1168 if (15 == (internal->r_size & 0x1f))
1170 if (R_BA == internal->r_type)
1171 relent->howto = &xcoff_howto_table[0x1c];
1172 else if (R_RBR == internal->r_type)
1173 relent->howto = &xcoff_howto_table[0x1d];
1174 else if (R_RBA == internal->r_type)
1175 relent->howto = &xcoff_howto_table[0x1e];
1178 /* The r_size field of an XCOFF reloc encodes the bitsize of the
1179 relocation, as well as indicating whether it is signed or not.
1180 Doublecheck that the relocation information gathered from the
1181 type matches this information. The bitsize is not significant
1182 for R_REF relocs. */
1183 if (relent->howto->dst_mask != 0
1184 && (relent->howto->bitsize
1185 != ((unsigned int) internal->r_size & 0x1f) + 1))
1186 abort ();
1189 reloc_howto_type *
1190 _bfd_xcoff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1191 bfd_reloc_code_real_type code)
1193 switch (code)
1195 case BFD_RELOC_PPC_B26:
1196 return &xcoff_howto_table[0xa];
1197 case BFD_RELOC_PPC_BA16:
1198 return &xcoff_howto_table[0x1c];
1199 case BFD_RELOC_PPC_BA26:
1200 return &xcoff_howto_table[8];
1201 case BFD_RELOC_PPC_TOC16:
1202 return &xcoff_howto_table[3];
1203 case BFD_RELOC_PPC_TOC16_HI:
1204 return &xcoff_howto_table[0x30];
1205 case BFD_RELOC_PPC_TOC16_LO:
1206 return &xcoff_howto_table[0x31];
1207 case BFD_RELOC_PPC_B16:
1208 return &xcoff_howto_table[0x1d];
1209 case BFD_RELOC_32:
1210 case BFD_RELOC_CTOR:
1211 return &xcoff_howto_table[0];
1212 case BFD_RELOC_NONE:
1213 return &xcoff_howto_table[0xf];
1214 case BFD_RELOC_PPC_NEG:
1215 return &xcoff_howto_table[0x1];
1216 case BFD_RELOC_PPC_TLSGD:
1217 return &xcoff_howto_table[0x20];
1218 case BFD_RELOC_PPC_TLSIE:
1219 return &xcoff_howto_table[0x21];
1220 case BFD_RELOC_PPC_TLSLD:
1221 return &xcoff_howto_table[0x22];
1222 case BFD_RELOC_PPC_TLSLE:
1223 return &xcoff_howto_table[0x23];
1224 case BFD_RELOC_PPC_TLSM:
1225 return &xcoff_howto_table[0x24];
1226 case BFD_RELOC_PPC_TLSML:
1227 return &xcoff_howto_table[0x25];
1228 default:
1229 return NULL;
1233 static reloc_howto_type *
1234 _bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1235 const char *r_name)
1237 unsigned int i;
1239 for (i = 0;
1240 i < sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]);
1241 i++)
1242 if (xcoff_howto_table[i].name != NULL
1243 && strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
1244 return &xcoff_howto_table[i];
1246 return NULL;
1249 /* XCOFF archive support. The original version of this code was by
1250 Damon A. Permezel. It was enhanced to permit cross support, and
1251 writing archive files, by Ian Lance Taylor, Cygnus Support.
1253 XCOFF uses its own archive format. Everything is hooked together
1254 with file offset links, so it is possible to rapidly update an
1255 archive in place. Of course, we don't do that. An XCOFF archive
1256 has a real file header, not just an ARMAG string. The structure of
1257 the file header and of each archive header appear below.
1259 An XCOFF archive also has a member table, which is a list of
1260 elements in the archive (you can get that by looking through the
1261 linked list, but you have to read a lot more of the file). The
1262 member table has a normal archive header with an empty name. It is
1263 normally (and perhaps must be) the second to last entry in the
1264 archive. The member table data is almost printable ASCII. It
1265 starts with a 12 character decimal string which is the number of
1266 entries in the table. For each entry it has a 12 character decimal
1267 string which is the offset in the archive of that member. These
1268 entries are followed by a series of null terminated strings which
1269 are the member names for each entry.
1271 Finally, an XCOFF archive has a global symbol table, which is what
1272 we call the armap. The global symbol table has a normal archive
1273 header with an empty name. It is normally (and perhaps must be)
1274 the last entry in the archive. The contents start with a four byte
1275 binary number which is the number of entries. This is followed by
1276 a that many four byte binary numbers; each is the file offset of an
1277 entry in the archive. These numbers are followed by a series of
1278 null terminated strings, which are symbol names.
1280 AIX 4.3 introduced a new archive format which can handle larger
1281 files and also 32- and 64-bit objects in the same archive. The
1282 things said above remain true except that there is now more than
1283 one global symbol table. The one is used to index 32-bit objects,
1284 the other for 64-bit objects.
1286 The new archives (recognizable by the new ARMAG string) has larger
1287 field lengths so that we cannot really share any code. Also we have
1288 to take care that we are not generating the new form of archives
1289 on AIX 4.2 or earlier systems. */
1291 /* PR 21786: The PE/COFF standard does not require NUL termination for any of
1292 the ASCII fields in the archive headers. So in order to be able to extract
1293 numerical values we provide our own versions of strtol and strtoll which
1294 take a maximum length as an additional parameter. Also - just to save space,
1295 we omit the endptr return parameter, since we know that it is never used. */
1297 static long
1298 _bfd_strntol (const char * nptr, int base, unsigned int maxlen)
1300 char buf[24]; /* Should be enough. */
1302 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1304 memcpy (buf, nptr, maxlen);
1305 buf[maxlen] = 0;
1306 return strtol (buf, NULL, base);
1309 static long long
1310 _bfd_strntoll (const char * nptr, int base, unsigned int maxlen)
1312 char buf[32]; /* Should be enough. */
1314 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1316 memcpy (buf, nptr, maxlen);
1317 buf[maxlen] = 0;
1318 return strtoll (buf, NULL, base);
1321 /* Macro to read an ASCII value stored in an archive header field. */
1322 #define GET_VALUE_IN_FIELD(VAR, FIELD, BASE) \
1323 do \
1325 (VAR) = (sizeof (VAR) > sizeof (long) \
1326 ? _bfd_strntoll (FIELD, BASE, sizeof FIELD) \
1327 : _bfd_strntol (FIELD, BASE, sizeof FIELD)); \
1329 while (0)
1331 #define EQ_VALUE_IN_FIELD(VAR, FIELD, BASE) \
1332 (sizeof (VAR) > sizeof (long) \
1333 ? (VAR) == _bfd_strntoll (FIELD, BASE, sizeof FIELD) \
1334 : (VAR) == _bfd_strntol (FIELD, BASE, sizeof FIELD))
1336 /* Read in the armap of an XCOFF archive. */
1338 bool
1339 _bfd_xcoff_slurp_armap (bfd *abfd)
1341 file_ptr off;
1342 size_t namlen;
1343 bfd_size_type sz;
1344 bfd_byte *contents, *cend;
1345 bfd_vma c, i;
1346 carsym *arsym;
1347 bfd_byte *p;
1349 if (xcoff_ardata (abfd) == NULL)
1351 abfd->has_armap = false;
1352 return true;
1355 if (! xcoff_big_format_p (abfd))
1357 /* This is for the old format. */
1358 struct xcoff_ar_hdr hdr;
1360 GET_VALUE_IN_FIELD (off, xcoff_ardata (abfd)->symoff, 10);
1361 if (off == 0)
1363 abfd->has_armap = false;
1364 return true;
1367 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1368 return false;
1370 /* The symbol table starts with a normal archive header. */
1371 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1372 != SIZEOF_AR_HDR)
1373 return false;
1375 /* Skip the name (normally empty). */
1376 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1377 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1378 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1379 return false;
1381 GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1382 if (sz + 1 < 5)
1384 bfd_set_error (bfd_error_bad_value);
1385 return false;
1388 /* Read in the entire symbol table. */
1389 contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
1390 if (contents == NULL)
1391 return false;
1393 /* Ensure strings are NULL terminated so we don't wander off the
1394 end of the buffer. */
1395 contents[sz] = 0;
1397 /* The symbol table starts with a four byte count. */
1398 c = H_GET_32 (abfd, contents);
1400 if (c >= sz / 4)
1402 bfd_set_error (bfd_error_bad_value);
1403 return false;
1406 bfd_ardata (abfd)->symdefs =
1407 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1408 if (bfd_ardata (abfd)->symdefs == NULL)
1409 return false;
1411 /* After the count comes a list of four byte file offsets. */
1412 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1413 i < c;
1414 ++i, ++arsym, p += 4)
1415 arsym->file_offset = H_GET_32 (abfd, p);
1417 else
1419 /* This is for the new format. */
1420 struct xcoff_ar_hdr_big hdr;
1422 GET_VALUE_IN_FIELD (off, xcoff_ardata_big (abfd)->symoff, 10);
1423 if (off == 0)
1425 abfd->has_armap = false;
1426 return true;
1429 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1430 return false;
1432 /* The symbol table starts with a normal archive header. */
1433 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1434 != SIZEOF_AR_HDR_BIG)
1435 return false;
1437 /* Skip the name (normally empty). */
1438 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1439 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1440 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1441 return false;
1443 GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1444 if (sz + 1 < 9)
1446 bfd_set_error (bfd_error_bad_value);
1447 return false;
1450 /* Read in the entire symbol table. */
1451 contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
1452 if (contents == NULL)
1453 return false;
1455 /* Ensure strings are NULL terminated so we don't wander off the
1456 end of the buffer. */
1457 contents[sz] = 0;
1459 /* The symbol table starts with an eight byte count. */
1460 c = H_GET_64 (abfd, contents);
1462 if (c >= sz / 8)
1464 bfd_set_error (bfd_error_bad_value);
1465 return false;
1468 bfd_ardata (abfd)->symdefs =
1469 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1470 if (bfd_ardata (abfd)->symdefs == NULL)
1471 return false;
1473 /* After the count comes a list of eight byte file offsets. */
1474 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1475 i < c;
1476 ++i, ++arsym, p += 8)
1477 arsym->file_offset = H_GET_64 (abfd, p);
1480 /* After the file offsets come null terminated symbol names. */
1481 cend = contents + sz;
1482 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1483 i < c;
1484 ++i, ++arsym, p += strlen ((char *) p) + 1)
1486 if (p >= cend)
1488 bfd_set_error (bfd_error_bad_value);
1489 return false;
1491 arsym->name = (char *) p;
1494 bfd_ardata (abfd)->symdef_count = c;
1495 abfd->has_armap = true;
1497 return true;
1500 /* See if this is an XCOFF archive. */
1502 bfd_cleanup
1503 _bfd_xcoff_archive_p (bfd *abfd)
1505 struct artdata *tdata_hold;
1506 char magic[SXCOFFARMAG];
1507 size_t amt = SXCOFFARMAG;
1509 if (bfd_bread (magic, amt, abfd) != amt)
1511 if (bfd_get_error () != bfd_error_system_call)
1512 bfd_set_error (bfd_error_wrong_format);
1513 return NULL;
1516 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1517 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1519 bfd_set_error (bfd_error_wrong_format);
1520 return NULL;
1523 tdata_hold = bfd_ardata (abfd);
1525 amt = sizeof (struct artdata);
1526 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
1527 if (bfd_ardata (abfd) == (struct artdata *) NULL)
1528 goto error_ret_restore;
1530 /* Cleared by bfd_zalloc above.
1531 bfd_ardata (abfd)->cache = NULL;
1532 bfd_ardata (abfd)->archive_head = NULL;
1533 bfd_ardata (abfd)->symdefs = NULL;
1534 bfd_ardata (abfd)->extended_names = NULL;
1535 bfd_ardata (abfd)->extended_names_size = 0; */
1537 /* Now handle the two formats. */
1538 if (magic[1] != 'b')
1540 /* This is the old format. */
1541 struct xcoff_ar_file_hdr hdr;
1543 /* Copy over the magic string. */
1544 memcpy (hdr.magic, magic, SXCOFFARMAG);
1546 /* Now read the rest of the file header. */
1547 amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
1548 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1550 if (bfd_get_error () != bfd_error_system_call)
1551 bfd_set_error (bfd_error_wrong_format);
1552 goto error_ret;
1555 GET_VALUE_IN_FIELD (bfd_ardata (abfd)->first_file_filepos,
1556 hdr.firstmemoff, 10);
1558 amt = SIZEOF_AR_FILE_HDR;
1559 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1560 if (bfd_ardata (abfd)->tdata == NULL)
1561 goto error_ret;
1563 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1565 else
1567 /* This is the new format. */
1568 struct xcoff_ar_file_hdr_big hdr;
1570 /* Copy over the magic string. */
1571 memcpy (hdr.magic, magic, SXCOFFARMAG);
1573 /* Now read the rest of the file header. */
1574 amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
1575 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1577 if (bfd_get_error () != bfd_error_system_call)
1578 bfd_set_error (bfd_error_wrong_format);
1579 goto error_ret;
1582 bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
1583 (const char **) 0,
1584 10);
1586 amt = SIZEOF_AR_FILE_HDR_BIG;
1587 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1588 if (bfd_ardata (abfd)->tdata == NULL)
1589 goto error_ret;
1591 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1594 if (! _bfd_xcoff_slurp_armap (abfd))
1596 error_ret:
1597 bfd_release (abfd, bfd_ardata (abfd));
1598 error_ret_restore:
1599 bfd_ardata (abfd) = tdata_hold;
1600 return NULL;
1603 return _bfd_no_cleanup;
1606 /* Read the archive header in an XCOFF archive. */
1608 void *
1609 _bfd_xcoff_read_ar_hdr (bfd *abfd)
1611 bfd_size_type namlen;
1612 struct areltdata *ret;
1613 bfd_size_type amt;
1615 if (! xcoff_big_format_p (abfd))
1617 struct xcoff_ar_hdr hdr;
1618 struct xcoff_ar_hdr *hdrp;
1620 if (bfd_bread (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
1621 return NULL;
1623 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1624 if (namlen > bfd_get_file_size (abfd))
1625 return NULL;
1626 amt = sizeof (struct areltdata) + SIZEOF_AR_HDR + namlen + 1;
1627 ret = (struct areltdata *) bfd_malloc (amt);
1628 if (ret == NULL)
1629 return ret;
1631 hdrp = (struct xcoff_ar_hdr *) (ret + 1);
1632 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1633 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
1635 free (ret);
1636 return NULL;
1638 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1640 ret->arch_header = (char *) hdrp;
1641 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1642 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1644 else
1646 struct xcoff_ar_hdr_big hdr;
1647 struct xcoff_ar_hdr_big *hdrp;
1649 if (bfd_bread (&hdr, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG)
1650 return NULL;
1652 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1653 if (namlen > bfd_get_file_size (abfd))
1654 return NULL;
1655 amt = sizeof (struct areltdata) + SIZEOF_AR_HDR_BIG + namlen + 1;
1656 ret = (struct areltdata *) bfd_malloc (amt);
1657 if (ret == NULL)
1658 return ret;
1660 hdrp = (struct xcoff_ar_hdr_big *) (ret + 1);
1661 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1662 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
1664 free (ret);
1665 return NULL;
1667 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1669 ret->arch_header = (char *) hdrp;
1670 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1671 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1674 /* Size occupied by the header above that covered in the fixed
1675 SIZEOF_AR_HDR or SIZEOF_AR_HDR_BIG. */
1676 ret->extra_size = namlen + (namlen & 1) + SXCOFFARFMAG;
1678 /* Skip over the XCOFFARFMAG at the end of the file name. */
1679 if (bfd_seek (abfd, (file_ptr) ((namlen & 1) + SXCOFFARFMAG), SEEK_CUR) != 0)
1680 return NULL;
1682 return ret;
1685 /* Open the next element in an XCOFF archive. */
1687 bfd *
1688 _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
1690 file_ptr filestart;
1691 file_ptr laststart, lastend;
1693 if (xcoff_ardata (archive) == NULL)
1695 bfd_set_error (bfd_error_invalid_operation);
1696 return NULL;
1699 if (! xcoff_big_format_p (archive))
1701 if (last_file == NULL)
1703 filestart = bfd_ardata (archive)->first_file_filepos;
1704 laststart = 0;
1705 lastend = SIZEOF_AR_FILE_HDR;
1707 else
1709 struct areltdata *arel = arch_eltdata (last_file);
1711 GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff, 10);
1712 laststart = last_file->proxy_origin;
1713 lastend = laststart + arel->parsed_size;
1714 laststart -= SIZEOF_AR_HDR + arel->extra_size;
1717 /* Sanity check that we aren't pointing into the previous element. */
1718 if (filestart != 0 && filestart >= laststart && filestart < lastend)
1720 bfd_set_error (bfd_error_malformed_archive);
1721 return NULL;
1724 if (filestart == 0
1725 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->memoff, 10)
1726 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->symoff, 10))
1728 bfd_set_error (bfd_error_no_more_archived_files);
1729 return NULL;
1732 else
1734 if (last_file == NULL)
1736 filestart = bfd_ardata (archive)->first_file_filepos;
1737 laststart = 0;
1738 lastend = SIZEOF_AR_FILE_HDR_BIG;
1740 else
1742 struct areltdata *arel = arch_eltdata (last_file);
1744 GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff, 10);
1745 laststart = last_file->proxy_origin;
1746 lastend = laststart + arel->parsed_size;
1747 laststart -= SIZEOF_AR_HDR_BIG + arel->extra_size;
1750 /* Sanity check that we aren't pointing into the previous element. */
1751 if (filestart != 0 && filestart >= laststart && filestart < lastend)
1753 bfd_set_error (bfd_error_malformed_archive);
1754 return NULL;
1757 if (filestart == 0
1758 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->memoff, 10)
1759 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->symoff, 10))
1761 bfd_set_error (bfd_error_no_more_archived_files);
1762 return NULL;
1766 return _bfd_get_elt_at_filepos (archive, filestart, NULL);
1769 /* Stat an element in an XCOFF archive. */
1772 _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
1774 if (abfd->arelt_data == NULL)
1776 bfd_set_error (bfd_error_invalid_operation);
1777 return -1;
1780 if (! xcoff_big_format_p (abfd->my_archive))
1782 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1784 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1785 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1786 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1787 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1788 s->st_size = arch_eltdata (abfd)->parsed_size;
1790 else
1792 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1794 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1795 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1796 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1797 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1798 s->st_size = arch_eltdata (abfd)->parsed_size;
1801 return 0;
1804 /* Normalize a file name for inclusion in an archive. */
1806 static const char *
1807 normalize_filename (bfd *abfd)
1809 const char *file;
1810 const char *filename;
1812 file = bfd_get_filename (abfd);
1813 filename = strrchr (file, '/');
1814 if (filename != NULL)
1815 filename++;
1816 else
1817 filename = file;
1818 return filename;
1821 /* Write out an XCOFF armap. */
1823 static bool
1824 xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1825 struct orl *map, unsigned int orl_count, int stridx)
1827 struct archive_iterator iterator;
1828 struct xcoff_ar_hdr hdr;
1829 char *p;
1830 unsigned char buf[4];
1831 unsigned int i;
1833 memset (&hdr, 0, sizeof hdr);
1834 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1835 sprintf (hdr.nextoff, "%d", 0);
1836 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, XCOFFARMAG_ELEMENT_SIZE);
1837 sprintf (hdr.date, "%d", 0);
1838 sprintf (hdr.uid, "%d", 0);
1839 sprintf (hdr.gid, "%d", 0);
1840 sprintf (hdr.mode, "%d", 0);
1841 sprintf (hdr.namlen, "%d", 0);
1843 /* We need spaces, not null bytes, in the header. */
1844 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1845 if (*p == '\0')
1846 *p = ' ';
1848 if (bfd_bwrite (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1849 != SIZEOF_AR_HDR
1850 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
1851 != SXCOFFARFMAG))
1852 return false;
1854 H_PUT_32 (abfd, orl_count, buf);
1855 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1856 return false;
1858 i = 0;
1859 archive_iterator_begin (&iterator, abfd);
1860 while (i < orl_count && archive_iterator_next (&iterator))
1861 while (map[i].u.abfd == iterator.current.member)
1863 H_PUT_32 (abfd, iterator.current.offset, buf);
1864 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1865 return false;
1866 ++i;
1869 for (i = 0; i < orl_count; i++)
1871 const char *name;
1872 size_t namlen;
1874 name = *map[i].name;
1875 namlen = strlen (name);
1876 if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
1877 return false;
1880 if ((stridx & 1) != 0)
1882 char b;
1884 b = '\0';
1885 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1886 return false;
1889 return true;
1892 static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
1893 #define FMT20 "%-20" PRId64
1894 #define FMT12 "%-12d"
1895 #define FMT12_OCTAL "%-12o"
1896 #define FMT4 "%-4d"
1897 #define PRINT20(d, v) \
1898 sprintf (buff20, FMT20, (uint64_t) (v)), \
1899 memcpy ((void *) (d), buff20, 20)
1901 #define PRINT12(d, v) \
1902 sprintf (buff20, FMT12, (int)(v)), \
1903 memcpy ((void *) (d), buff20, 12)
1905 #define PRINT12_OCTAL(d, v) \
1906 sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
1907 memcpy ((void *) (d), buff20, 12)
1909 #define PRINT4(d, v) \
1910 sprintf (buff20, FMT4, (int)(v)), \
1911 memcpy ((void *) (d), buff20, 4)
1913 #define READ20(d, v) \
1914 buff20[20] = 0, \
1915 memcpy (buff20, (d), 20), \
1916 (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
1918 static bool
1919 do_pad (bfd *abfd, unsigned int number)
1921 bfd_byte b = 0;
1923 /* Limit pad to <= 4096. */
1924 if (number > 4096)
1925 return false;
1927 while (number--)
1928 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1929 return false;
1931 return true;
1934 static bool
1935 do_copy (bfd *out_bfd, bfd *in_bfd)
1937 bfd_size_type remaining;
1938 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1940 if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
1941 return false;
1943 remaining = arelt_size (in_bfd);
1945 while (remaining >= DEFAULT_BUFFERSIZE)
1947 if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
1948 || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
1949 return false;
1951 remaining -= DEFAULT_BUFFERSIZE;
1954 if (remaining)
1956 if (bfd_bread (buffer, remaining, in_bfd) != remaining
1957 || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
1958 return false;
1961 return true;
1964 static bool
1965 xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1966 struct orl *map, unsigned int orl_count, int stridx)
1968 struct archive_iterator iterator;
1969 struct xcoff_ar_file_hdr_big *fhdr;
1970 bfd_vma i, sym_32, sym_64, str_32, str_64;
1971 const bfd_arch_info_type *arch_info;
1972 bfd *current_bfd;
1973 size_t string_length;
1974 file_ptr nextoff, prevoff;
1976 /* First, we look through the symbols and work out which are
1977 from 32-bit objects and which from 64-bit ones. */
1978 sym_32 = sym_64 = str_32 = str_64 = 0;
1980 i = 0;
1981 for (current_bfd = abfd->archive_head;
1982 current_bfd != NULL && i < orl_count;
1983 current_bfd = current_bfd->archive_next)
1985 arch_info = bfd_get_arch_info (current_bfd);
1986 while (map[i].u.abfd == current_bfd)
1988 string_length = strlen (*map[i].name) + 1;
1989 if (arch_info->bits_per_address == 64)
1991 sym_64++;
1992 str_64 += string_length;
1994 else
1996 sym_32++;
1997 str_32 += string_length;
1999 i++;
2003 /* A quick sanity check... */
2004 BFD_ASSERT (sym_64 + sym_32 == orl_count);
2005 /* Explicit cast to int for compiler. */
2006 BFD_ASSERT ((int)(str_64 + str_32) == stridx);
2008 fhdr = xcoff_ardata_big (abfd);
2010 /* xcoff_write_archive_contents_big passes nextoff in symoff. */
2011 READ20 (fhdr->memoff, prevoff);
2012 READ20 (fhdr->symoff, nextoff);
2014 BFD_ASSERT (nextoff == bfd_tell (abfd));
2016 /* Write out the symbol table.
2017 Layout :
2019 standard big archive header
2020 0x0000 ar_size [0x14]
2021 0x0014 ar_nxtmem [0x14]
2022 0x0028 ar_prvmem [0x14]
2023 0x003C ar_date [0x0C]
2024 0x0048 ar_uid [0x0C]
2025 0x0054 ar_gid [0x0C]
2026 0x0060 ar_mod [0x0C]
2027 0x006C ar_namelen[0x04]
2028 0x0070 ar_fmag [SXCOFFARFMAG]
2030 Symbol table
2031 0x0072 num_syms [0x08], binary
2032 0x0078 offsets [0x08 * num_syms], binary
2033 0x0086 + 0x08 * num_syms names [??]
2034 ?? pad to even bytes.
2037 if (sym_32)
2039 struct xcoff_ar_hdr_big *hdr;
2040 char *symbol_table;
2041 char *st;
2043 bfd_vma symbol_table_size =
2044 SIZEOF_AR_HDR_BIG
2045 + SXCOFFARFMAG
2047 + 8 * sym_32
2048 + str_32 + (str_32 & 1);
2050 symbol_table = bfd_zmalloc (symbol_table_size);
2051 if (symbol_table == NULL)
2052 return false;
2054 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2056 PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
2058 if (sym_64)
2059 PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
2060 else
2061 PRINT20 (hdr->nextoff, 0);
2063 PRINT20 (hdr->prevoff, prevoff);
2064 PRINT12 (hdr->date, 0);
2065 PRINT12 (hdr->uid, 0);
2066 PRINT12 (hdr->gid, 0);
2067 PRINT12 (hdr->mode, 0);
2068 PRINT4 (hdr->namlen, 0) ;
2070 st = symbol_table + SIZEOF_AR_HDR_BIG;
2071 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2072 st += SXCOFFARFMAG;
2074 bfd_h_put_64 (abfd, sym_32, st);
2075 st += 8;
2077 /* loop over the 32 bit offsets */
2078 i = 0;
2079 archive_iterator_begin (&iterator, abfd);
2080 while (i < orl_count && archive_iterator_next (&iterator))
2082 arch_info = bfd_get_arch_info (iterator.current.member);
2083 while (map[i].u.abfd == iterator.current.member)
2085 if (arch_info->bits_per_address == 32)
2087 bfd_h_put_64 (abfd, iterator.current.offset, st);
2088 st += 8;
2090 i++;
2094 /* loop over the 32 bit symbol names */
2095 i = 0;
2096 for (current_bfd = abfd->archive_head;
2097 current_bfd != NULL && i < orl_count;
2098 current_bfd = current_bfd->archive_next)
2100 arch_info = bfd_get_arch_info (current_bfd);
2101 while (map[i].u.abfd == current_bfd)
2103 if (arch_info->bits_per_address == 32)
2105 string_length = sprintf (st, "%s", *map[i].name);
2106 st += string_length + 1;
2108 i++;
2112 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2114 free (symbol_table);
2116 prevoff = nextoff;
2117 nextoff = nextoff + symbol_table_size;
2119 else
2120 PRINT20 (fhdr->symoff, 0);
2122 if (sym_64)
2124 struct xcoff_ar_hdr_big *hdr;
2125 char *symbol_table;
2126 char *st;
2128 bfd_vma symbol_table_size =
2129 SIZEOF_AR_HDR_BIG
2130 + SXCOFFARFMAG
2132 + 8 * sym_64
2133 + str_64 + (str_64 & 1);
2135 symbol_table = bfd_zmalloc (symbol_table_size);
2136 if (symbol_table == NULL)
2137 return false;
2139 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2141 PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
2142 PRINT20 (hdr->nextoff, 0);
2143 PRINT20 (hdr->prevoff, prevoff);
2144 PRINT12 (hdr->date, 0);
2145 PRINT12 (hdr->uid, 0);
2146 PRINT12 (hdr->gid, 0);
2147 PRINT12 (hdr->mode, 0);
2148 PRINT4 (hdr->namlen, 0);
2150 st = symbol_table + SIZEOF_AR_HDR_BIG;
2151 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2152 st += SXCOFFARFMAG;
2154 bfd_h_put_64 (abfd, sym_64, st);
2155 st += 8;
2157 /* loop over the 64 bit offsets */
2158 i = 0;
2159 archive_iterator_begin (&iterator, abfd);
2160 while (i < orl_count && archive_iterator_next (&iterator))
2162 arch_info = bfd_get_arch_info (iterator.current.member);
2163 while (map[i].u.abfd == iterator.current.member)
2165 if (arch_info->bits_per_address == 64)
2167 bfd_h_put_64 (abfd, iterator.current.offset, st);
2168 st += 8;
2170 i++;
2174 /* loop over the 64 bit symbol names */
2175 i = 0;
2176 for (current_bfd = abfd->archive_head;
2177 current_bfd != NULL && i < orl_count;
2178 current_bfd = current_bfd->archive_next)
2180 arch_info = bfd_get_arch_info (current_bfd);
2181 while (map[i].u.abfd == current_bfd)
2183 if (arch_info->bits_per_address == 64)
2185 string_length = sprintf (st, "%s", *map[i].name);
2186 st += string_length + 1;
2188 i++;
2192 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2194 free (symbol_table);
2196 PRINT20 (fhdr->symoff64, nextoff);
2198 else
2199 PRINT20 (fhdr->symoff64, 0);
2201 return true;
2204 bool
2205 _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2206 struct orl *map, unsigned int orl_count, int stridx)
2208 if (! xcoff_big_format_p (abfd))
2209 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
2210 else
2211 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
2214 /* Write out an XCOFF archive. We always write an entire archive,
2215 rather than fussing with the freelist and so forth. */
2217 static bool
2218 xcoff_write_archive_contents_old (bfd *abfd)
2220 struct archive_iterator iterator;
2221 struct xcoff_ar_file_hdr fhdr;
2222 bfd_size_type count;
2223 bfd_size_type total_namlen;
2224 file_ptr *offsets;
2225 bool makemap;
2226 bool hasobjects;
2227 file_ptr prevoff, nextoff;
2228 bfd *sub;
2229 size_t i;
2230 struct xcoff_ar_hdr ahdr;
2231 bfd_size_type size;
2232 char *p;
2233 char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
2235 memset (&fhdr, 0, sizeof fhdr);
2236 (void) memcpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
2237 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
2238 sprintf (fhdr.freeoff, "%d", 0);
2240 count = 0;
2241 total_namlen = 0;
2242 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2244 ++count;
2245 total_namlen += strlen (normalize_filename (sub)) + 1;
2246 if (sub->arelt_data == NULL)
2248 sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
2249 if (sub->arelt_data == NULL)
2250 return false;
2252 if (arch_xhdr (sub) == NULL)
2254 struct xcoff_ar_hdr *ahdrp;
2255 struct stat s;
2257 if ((sub->flags & BFD_IN_MEMORY) != 0)
2259 /* Assume we just "made" the member, and fake it. */
2260 struct bfd_in_memory *bim
2261 = (struct bfd_in_memory *) sub->iostream;
2262 time (&s.st_mtime);
2263 s.st_uid = getuid ();
2264 s.st_gid = getgid ();
2265 s.st_mode = 0644;
2266 s.st_size = bim->size;
2268 else if (stat (bfd_get_filename (sub), &s) != 0)
2270 bfd_set_input_error (sub, bfd_error_system_call);
2271 return false;
2274 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2276 s.st_mtime = 0;
2277 s.st_uid = 0;
2278 s.st_gid = 0;
2279 s.st_mode = 0644;
2282 ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
2283 if (ahdrp == NULL)
2284 return false;
2286 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2287 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2288 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2289 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2290 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2292 arch_eltdata (sub)->arch_header = (char *) ahdrp;
2293 arch_eltdata (sub)->parsed_size = s.st_size;
2296 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2297 if (offsets == NULL)
2298 return false;
2300 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
2301 return false;
2303 makemap = bfd_has_map (abfd);
2304 hasobjects = false;
2305 prevoff = 0;
2306 for (archive_iterator_begin (&iterator, abfd), i = 0;
2307 archive_iterator_next (&iterator);
2308 i++)
2310 bfd_size_type namlen;
2311 struct xcoff_ar_hdr *ahdrp;
2313 if (makemap && ! hasobjects)
2315 if (bfd_check_format (iterator.current.member, bfd_object))
2316 hasobjects = true;
2319 ahdrp = arch_xhdr (iterator.current.member);
2320 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2321 sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
2322 sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
2324 /* We need spaces, not null bytes, in the header. */
2325 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
2326 if (*p == '\0')
2327 *p = ' ';
2329 if (!do_pad (abfd, iterator.current.leading_padding))
2330 return false;
2332 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2333 namlen = iterator.current.padded_namlen;
2334 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2335 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2336 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2337 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2338 || !do_copy (abfd, iterator.current.member)
2339 || !do_pad (abfd, iterator.current.trailing_padding))
2340 return false;
2342 offsets[i] = iterator.current.offset;
2343 prevoff = iterator.current.offset;
2346 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2348 /* Write out the member table. */
2350 nextoff = iterator.next.offset;
2351 BFD_ASSERT (nextoff == bfd_tell (abfd));
2352 sprintf (fhdr.memoff, "%ld", (long) nextoff);
2354 memset (&ahdr, 0, sizeof ahdr);
2355 sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
2356 + count * XCOFFARMAG_ELEMENT_SIZE
2357 + total_namlen));
2358 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2359 sprintf (ahdr.date, "%d", 0);
2360 sprintf (ahdr.uid, "%d", 0);
2361 sprintf (ahdr.gid, "%d", 0);
2362 sprintf (ahdr.mode, "%d", 0);
2363 sprintf (ahdr.namlen, "%d", 0);
2365 size = (SIZEOF_AR_HDR
2366 + XCOFFARMAG_ELEMENT_SIZE
2367 + count * XCOFFARMAG_ELEMENT_SIZE
2368 + total_namlen
2369 + SXCOFFARFMAG);
2371 prevoff = nextoff;
2372 nextoff += size + (size & 1);
2374 if (makemap && hasobjects)
2375 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2376 else
2377 sprintf (ahdr.nextoff, "%d", 0);
2379 /* We need spaces, not null bytes, in the header. */
2380 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2381 if (*p == '\0')
2382 *p = ' ';
2384 if ((bfd_bwrite (&ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
2385 != SIZEOF_AR_HDR)
2386 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
2387 != SXCOFFARFMAG))
2388 return false;
2390 sprintf (decbuf, "%-12ld", (long) count);
2391 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
2392 != XCOFFARMAG_ELEMENT_SIZE)
2393 return false;
2394 for (i = 0; i < (size_t) count; i++)
2396 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2397 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
2398 abfd) != XCOFFARMAG_ELEMENT_SIZE)
2399 return false;
2401 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2403 const char *name;
2404 bfd_size_type namlen;
2406 name = normalize_filename (sub);
2407 namlen = strlen (name);
2408 if (bfd_bwrite (name, namlen + 1, abfd) != namlen + 1)
2409 return false;
2412 if (! do_pad (abfd, size & 1))
2413 return false;
2415 /* Write out the armap, if appropriate. */
2416 if (! makemap || ! hasobjects)
2417 sprintf (fhdr.symoff, "%d", 0);
2418 else
2420 BFD_ASSERT (nextoff == bfd_tell (abfd));
2421 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2422 bfd_ardata (abfd)->tdata = &fhdr;
2423 if (! _bfd_compute_and_write_armap (abfd, 0))
2424 return false;
2427 /* Write out the archive file header. */
2429 /* We need spaces, not null bytes, in the header. */
2430 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2431 if (*p == '\0')
2432 *p = ' ';
2434 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2435 || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR, abfd)
2436 != SIZEOF_AR_FILE_HDR))
2437 return false;
2439 return true;
2442 static bool
2443 xcoff_write_archive_contents_big (bfd *abfd)
2445 struct xcoff_ar_file_hdr_big fhdr;
2446 bfd_size_type count;
2447 bfd_size_type total_namlen;
2448 file_ptr *offsets;
2449 bool makemap;
2450 bool hasobjects;
2451 file_ptr prevoff, nextoff;
2452 bfd *current_bfd;
2453 size_t i;
2454 struct xcoff_ar_hdr_big *hdr;
2455 bfd_size_type size;
2456 char *member_table, *mt;
2457 bfd_vma member_table_size;
2458 struct archive_iterator iterator;
2460 memset (&fhdr, 0, SIZEOF_AR_FILE_HDR_BIG);
2461 memcpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
2463 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2464 return false;
2466 /* Calculate count and total_namlen. */
2467 makemap = bfd_has_map (abfd);
2468 hasobjects = false;
2469 for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
2470 current_bfd != NULL;
2471 current_bfd = current_bfd->archive_next, count++)
2473 total_namlen += strlen (normalize_filename (current_bfd)) + 1;
2475 if (makemap
2476 && ! hasobjects
2477 && bfd_check_format (current_bfd, bfd_object))
2478 hasobjects = true;
2480 if (current_bfd->arelt_data == NULL)
2482 size = sizeof (struct areltdata);
2483 current_bfd->arelt_data = bfd_zmalloc (size);
2484 if (current_bfd->arelt_data == NULL)
2485 return false;
2488 if (arch_xhdr_big (current_bfd) == NULL)
2490 struct xcoff_ar_hdr_big *ahdrp;
2491 struct stat s;
2493 if ((current_bfd->flags & BFD_IN_MEMORY) != 0)
2495 /* Assume we just "made" the member, and fake it. */
2496 struct bfd_in_memory *bim
2497 = (struct bfd_in_memory *) current_bfd->iostream;
2498 time (&s.st_mtime);
2499 s.st_uid = getuid ();
2500 s.st_gid = getgid ();
2501 s.st_mode = 0644;
2502 s.st_size = bim->size;
2504 else if (stat (bfd_get_filename (current_bfd), &s) != 0)
2506 bfd_set_input_error (current_bfd, bfd_error_system_call);
2507 return false;
2510 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2512 s.st_mtime = 0;
2513 s.st_uid = 0;
2514 s.st_gid = 0;
2515 s.st_mode = 0644;
2518 ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
2519 if (ahdrp == NULL)
2520 return false;
2522 PRINT20 (ahdrp->size, s.st_size);
2523 PRINT12 (ahdrp->date, s.st_mtime);
2524 PRINT12 (ahdrp->uid, s.st_uid);
2525 PRINT12 (ahdrp->gid, s.st_gid);
2526 PRINT12_OCTAL (ahdrp->mode, s.st_mode);
2528 arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
2529 arch_eltdata (current_bfd)->parsed_size = s.st_size;
2533 offsets = NULL;
2534 if (count)
2536 offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
2537 if (offsets == NULL)
2538 return false;
2541 prevoff = 0;
2542 for (archive_iterator_begin (&iterator, abfd), i = 0;
2543 archive_iterator_next (&iterator);
2544 i++)
2546 bfd_size_type namlen;
2547 struct xcoff_ar_hdr_big *ahdrp;
2549 ahdrp = arch_xhdr_big (iterator.current.member);
2550 PRINT20 (ahdrp->prevoff, prevoff);
2551 PRINT4 (ahdrp->namlen, iterator.current.namlen);
2552 PRINT20 (ahdrp->nextoff, iterator.next.offset);
2554 if (!do_pad (abfd, iterator.current.leading_padding))
2556 free (offsets);
2557 return false;
2560 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2561 namlen = iterator.current.padded_namlen;
2562 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2563 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2564 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2565 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2566 || !do_copy (abfd, iterator.current.member)
2567 || !do_pad (abfd, iterator.current.trailing_padding))
2569 free (offsets);
2570 return false;
2573 offsets[i] = iterator.current.offset;
2574 prevoff = iterator.current.offset;
2577 if (count)
2579 PRINT20 (fhdr.firstmemoff, offsets[0]);
2580 PRINT20 (fhdr.lastmemoff, prevoff);
2583 /* Write out the member table.
2584 Layout :
2586 standard big archive header
2587 0x0000 ar_size [0x14]
2588 0x0014 ar_nxtmem [0x14]
2589 0x0028 ar_prvmem [0x14]
2590 0x003C ar_date [0x0C]
2591 0x0048 ar_uid [0x0C]
2592 0x0054 ar_gid [0x0C]
2593 0x0060 ar_mod [0x0C]
2594 0x006C ar_namelen[0x04]
2595 0x0070 ar_fmag [0x02]
2597 Member table
2598 0x0072 count [0x14]
2599 0x0086 offsets [0x14 * counts]
2600 0x0086 + 0x14 * counts names [??]
2601 ?? pad to even bytes.
2604 nextoff = iterator.next.offset;
2605 BFD_ASSERT (nextoff == bfd_tell (abfd));
2607 member_table_size = (SIZEOF_AR_HDR_BIG
2608 + SXCOFFARFMAG
2609 + XCOFFARMAGBIG_ELEMENT_SIZE
2610 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2611 + total_namlen);
2613 member_table_size += member_table_size & 1;
2614 member_table = bfd_zmalloc (member_table_size);
2615 if (member_table == NULL)
2617 free (offsets);
2618 return false;
2621 hdr = (struct xcoff_ar_hdr_big *) member_table;
2623 PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
2624 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2625 + total_namlen + (total_namlen & 1)));
2626 if (makemap && hasobjects)
2627 PRINT20 (hdr->nextoff, nextoff + member_table_size);
2628 else
2629 PRINT20 (hdr->nextoff, 0);
2630 PRINT20 (hdr->prevoff, prevoff);
2631 PRINT12 (hdr->date, 0);
2632 PRINT12 (hdr->uid, 0);
2633 PRINT12 (hdr->gid, 0);
2634 PRINT12 (hdr->mode, 0);
2635 PRINT4 (hdr->namlen, 0);
2637 mt = member_table + SIZEOF_AR_HDR_BIG;
2638 memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
2639 mt += SXCOFFARFMAG;
2641 PRINT20 (mt, count);
2642 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2643 for (i = 0; i < (size_t) count; i++)
2645 PRINT20 (mt, offsets[i]);
2646 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2649 if (count)
2651 free (offsets);
2652 offsets = NULL;
2655 for (current_bfd = abfd->archive_head;
2656 current_bfd != NULL;
2657 current_bfd = current_bfd->archive_next)
2659 const char *name;
2660 size_t namlen;
2662 name = normalize_filename (current_bfd);
2663 namlen = sprintf (mt, "%s", name);
2664 mt += namlen + 1;
2667 if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
2668 return false;
2670 free (member_table);
2672 PRINT20 (fhdr.memoff, nextoff);
2674 prevoff = nextoff;
2675 nextoff += member_table_size;
2677 /* Write out the armap, if appropriate. */
2679 if (! makemap || ! hasobjects)
2680 PRINT20 (fhdr.symoff, 0);
2681 else
2683 BFD_ASSERT (nextoff == bfd_tell (abfd));
2685 /* Save nextoff in fhdr.symoff so the armap routine can use it. */
2686 PRINT20 (fhdr.symoff, nextoff);
2688 bfd_ardata (abfd)->tdata = &fhdr;
2689 if (! _bfd_compute_and_write_armap (abfd, 0))
2690 return false;
2693 /* Write out the archive file header. */
2695 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2696 || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR_BIG,
2697 abfd) != SIZEOF_AR_FILE_HDR_BIG))
2698 return false;
2700 return true;
2703 bool
2704 _bfd_xcoff_write_archive_contents (bfd *abfd)
2706 if (! xcoff_big_format_p (abfd))
2707 return xcoff_write_archive_contents_old (abfd);
2708 else
2709 return xcoff_write_archive_contents_big (abfd);
2712 /* We can't use the usual coff_sizeof_headers routine, because AIX
2713 always uses an a.out header. */
2716 _bfd_xcoff_sizeof_headers (bfd *abfd,
2717 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2719 int size;
2721 size = FILHSZ;
2722 if (xcoff_data (abfd)->full_aouthdr)
2723 size += AOUTSZ;
2724 else
2725 size += SMALL_AOUTSZ;
2726 size += abfd->section_count * SCNHSZ;
2728 if (info->strip != strip_all)
2730 /* There can be additional sections just for dealing with overflow in
2731 reloc and lineno counts. But the numbers of relocs and lineno aren't
2732 known when bfd_sizeof_headers is called, so we compute them by
2733 summing the numbers from input sections. */
2734 struct nbr_reloc_lineno
2736 unsigned int reloc_count;
2737 unsigned int lineno_count;
2739 struct nbr_reloc_lineno *n_rl;
2740 bfd *sub;
2741 unsigned int max_index;
2742 asection *s;
2744 /* Although the number of sections is known, the maximum value of
2745 section->index isn't (because some sections may have been removed).
2746 Don't try to renumber sections, just compute the upper bound. */
2747 max_index = 0;
2748 for (s = abfd->sections; s != NULL; s = s->next)
2749 if (s->index > max_index)
2750 max_index = s->index;
2752 /* Allocate the per section counters. It could be possible to use a
2753 preallocated array as the number of sections is limited on XCOFF,
2754 but this creates a maintainance issue. */
2755 n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
2756 if (n_rl == NULL)
2757 return -1;
2759 /* Sum. */
2760 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2761 for (s = sub->sections; s != NULL; s = s->next)
2762 if (s->output_section->owner == abfd
2763 && !bfd_section_removed_from_list (abfd, s->output_section))
2765 struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
2766 e->reloc_count += s->reloc_count;
2767 e->lineno_count += s->lineno_count;
2770 /* Add the size of a section for each section with an overflow. */
2771 for (s = abfd->sections; s != NULL; s = s->next)
2773 struct nbr_reloc_lineno *e = &n_rl[s->index];
2775 if (e->reloc_count >= 0xffff
2776 || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
2777 size += SCNHSZ;
2780 free (n_rl);
2783 return size;
2786 /* Routines to swap information in the XCOFF .loader section. If we
2787 ever need to write an XCOFF loader, this stuff will need to be
2788 moved to another file shared by the linker (which XCOFF calls the
2789 ``binder'') and the loader. */
2791 /* Swap in the ldhdr structure. */
2793 static void
2794 xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
2796 const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2798 dst->l_version = bfd_get_32 (abfd, src->l_version);
2799 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2800 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2801 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2802 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2803 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2804 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2805 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2808 /* Swap out the ldhdr structure. */
2810 static void
2811 xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
2813 struct external_ldhdr *dst = (struct external_ldhdr *) d;
2815 bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
2816 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2817 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2818 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2819 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2820 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2821 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2822 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2825 /* Swap in the ldsym structure. */
2827 static void
2828 xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
2830 const struct external_ldsym *src = (const struct external_ldsym *) s;
2832 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2833 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2834 } else {
2835 dst->_l._l_l._l_zeroes = 0;
2836 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2838 dst->l_value = bfd_get_32 (abfd, src->l_value);
2839 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2840 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2841 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2842 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2843 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2846 /* Swap out the ldsym structure. */
2848 static void
2849 xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
2851 struct external_ldsym *dst = (struct external_ldsym *) d;
2853 if (src->_l._l_l._l_zeroes != 0)
2854 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2855 else
2857 bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2858 bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2859 dst->_l._l_l._l_offset);
2861 bfd_put_32 (abfd, src->l_value, dst->l_value);
2862 bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
2863 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2864 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2865 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2866 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2869 static void
2870 xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
2872 struct external_reloc *src = (struct external_reloc *) s;
2873 struct internal_reloc *dst = (struct internal_reloc *) d;
2875 memset (dst, 0, sizeof (struct internal_reloc));
2877 dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2878 dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2879 dst->r_size = bfd_get_8 (abfd, src->r_size);
2880 dst->r_type = bfd_get_8 (abfd, src->r_type);
2883 static unsigned int
2884 xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
2886 struct internal_reloc *src = (struct internal_reloc *) s;
2887 struct external_reloc *dst = (struct external_reloc *) d;
2889 bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2890 bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2891 bfd_put_8 (abfd, src->r_type, dst->r_type);
2892 bfd_put_8 (abfd, src->r_size, dst->r_size);
2894 return bfd_coff_relsz (abfd);
2897 /* Swap in the ldrel structure. */
2899 static void
2900 xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
2902 const struct external_ldrel *src = (const struct external_ldrel *) s;
2904 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2905 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2906 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2907 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2910 /* Swap out the ldrel structure. */
2912 static void
2913 xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
2915 struct external_ldrel *dst = (struct external_ldrel *) d;
2917 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2918 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
2919 bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
2920 bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
2924 bool
2925 xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
2926 asection *input_section ATTRIBUTE_UNUSED,
2927 bfd *output_bfd ATTRIBUTE_UNUSED,
2928 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2929 struct internal_syment *sym ATTRIBUTE_UNUSED,
2930 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2931 bfd_vma val ATTRIBUTE_UNUSED,
2932 bfd_vma addend ATTRIBUTE_UNUSED,
2933 bfd_vma *relocation ATTRIBUTE_UNUSED,
2934 bfd_byte *contents ATTRIBUTE_UNUSED,
2935 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2937 return true;
2940 bool
2941 xcoff_reloc_type_fail (bfd *input_bfd,
2942 asection *input_section ATTRIBUTE_UNUSED,
2943 bfd *output_bfd ATTRIBUTE_UNUSED,
2944 struct internal_reloc *rel,
2945 struct internal_syment *sym ATTRIBUTE_UNUSED,
2946 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2947 bfd_vma val ATTRIBUTE_UNUSED,
2948 bfd_vma addend ATTRIBUTE_UNUSED,
2949 bfd_vma *relocation ATTRIBUTE_UNUSED,
2950 bfd_byte *contents ATTRIBUTE_UNUSED,
2951 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2953 _bfd_error_handler
2954 /* xgettext: c-format */
2955 (_("%pB: unsupported relocation type %#x"),
2956 input_bfd, (unsigned int) rel->r_type);
2957 bfd_set_error (bfd_error_bad_value);
2958 return false;
2961 bool
2962 xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
2963 asection *input_section ATTRIBUTE_UNUSED,
2964 bfd *output_bfd ATTRIBUTE_UNUSED,
2965 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2966 struct internal_syment *sym ATTRIBUTE_UNUSED,
2967 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2968 bfd_vma val,
2969 bfd_vma addend,
2970 bfd_vma *relocation,
2971 bfd_byte *contents ATTRIBUTE_UNUSED,
2972 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2974 *relocation = val + addend;
2975 return true;
2978 bool
2979 xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
2980 asection *input_section ATTRIBUTE_UNUSED,
2981 bfd *output_bfd ATTRIBUTE_UNUSED,
2982 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2983 struct internal_syment *sym ATTRIBUTE_UNUSED,
2984 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2985 bfd_vma val,
2986 bfd_vma addend,
2987 bfd_vma *relocation,
2988 bfd_byte *contents ATTRIBUTE_UNUSED,
2989 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2991 *relocation = - val - addend;
2992 return true;
2995 bool
2996 xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
2997 asection *input_section,
2998 bfd *output_bfd ATTRIBUTE_UNUSED,
2999 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3000 struct internal_syment *sym ATTRIBUTE_UNUSED,
3001 struct reloc_howto_struct *howto,
3002 bfd_vma val,
3003 bfd_vma addend,
3004 bfd_vma *relocation,
3005 bfd_byte *contents ATTRIBUTE_UNUSED,
3006 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3008 howto->pc_relative = true;
3010 /* A PC relative reloc includes the section address. */
3011 addend += input_section->vma;
3013 *relocation = val + addend;
3014 *relocation -= (input_section->output_section->vma
3015 + input_section->output_offset);
3016 return true;
3019 bool
3020 xcoff_reloc_type_toc (bfd *input_bfd,
3021 asection *input_section ATTRIBUTE_UNUSED,
3022 bfd *output_bfd,
3023 struct internal_reloc *rel,
3024 struct internal_syment *sym ATTRIBUTE_UNUSED,
3025 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3026 bfd_vma val,
3027 bfd_vma addend ATTRIBUTE_UNUSED,
3028 bfd_vma *relocation,
3029 bfd_byte *contents ATTRIBUTE_UNUSED,
3030 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3032 struct xcoff_link_hash_entry *h;
3034 if (0 > rel->r_symndx)
3035 return false;
3037 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3039 if (h != NULL && h->smclas != XMC_TD)
3041 if (h->toc_section == NULL)
3043 _bfd_error_handler
3044 /* xgettext: c-format */
3045 (_("%pB: TOC reloc at %#" PRIx64 " to symbol `%s' with no TOC entry"),
3046 input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
3047 bfd_set_error (bfd_error_bad_value);
3048 return false;
3051 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
3052 val = (h->toc_section->output_section->vma
3053 + h->toc_section->output_offset);
3056 /* We can't use the preexisting value written down by the
3057 assembly, as R_TOCU needs to be adjusted when the final
3058 R_TOCL value is signed. */
3059 *relocation = val - xcoff_data (output_bfd)->toc;
3061 if (rel->r_type == R_TOCU)
3062 *relocation = ((*relocation + 0x8000) >> 16) & 0xffff;
3063 if (rel->r_type == R_TOCL)
3064 *relocation = *relocation & 0x0000ffff;
3066 return true;
3069 bool
3070 xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
3071 asection *input_section ATTRIBUTE_UNUSED,
3072 bfd *output_bfd ATTRIBUTE_UNUSED,
3073 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3074 struct internal_syment *sym ATTRIBUTE_UNUSED,
3075 struct reloc_howto_struct *howto,
3076 bfd_vma val,
3077 bfd_vma addend,
3078 bfd_vma *relocation,
3079 bfd_byte *contents ATTRIBUTE_UNUSED,
3080 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3082 howto->src_mask &= ~3;
3083 howto->dst_mask = howto->src_mask;
3085 *relocation = val + addend;
3087 return true;
3090 static bool
3091 xcoff_reloc_type_br (bfd *input_bfd,
3092 asection *input_section,
3093 bfd *output_bfd ATTRIBUTE_UNUSED,
3094 struct internal_reloc *rel,
3095 struct internal_syment *sym ATTRIBUTE_UNUSED,
3096 struct reloc_howto_struct *howto,
3097 bfd_vma val,
3098 bfd_vma addend,
3099 bfd_vma *relocation,
3100 bfd_byte *contents,
3101 struct bfd_link_info *info)
3103 struct xcoff_link_hash_entry *h;
3104 bfd_vma section_offset;
3105 struct xcoff_stub_hash_entry *stub_entry = NULL;
3106 enum xcoff_stub_type stub_type;
3108 if (0 > rel->r_symndx)
3109 return false;
3111 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3112 section_offset = rel->r_vaddr - input_section->vma;
3114 /* If we see an R_BR or R_RBR reloc which is jumping to global
3115 linkage code, and it is followed by an appropriate cror nop
3116 instruction, we replace the cror with lwz r2,20(r1). This
3117 restores the TOC after the glink code. Contrariwise, if the
3118 call is followed by a lwz r2,20(r1), but the call is not
3119 going to global linkage code, we can replace the load with a
3120 cror. */
3121 if (NULL != h
3122 && (bfd_link_hash_defined == h->root.type
3123 || bfd_link_hash_defweak == h->root.type)
3124 && section_offset + 8 <= input_section->size)
3126 bfd_byte *pnext;
3127 unsigned long next;
3129 pnext = contents + section_offset + 4;
3130 next = bfd_get_32 (input_bfd, pnext);
3132 /* The _ptrgl function is magic. It is used by the AIX
3133 compiler to call a function through a pointer. */
3134 if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
3136 if (next == 0x4def7b82 /* cror 15,15,15 */
3137 || next == 0x4ffffb82 /* cror 31,31,31 */
3138 || next == 0x60000000) /* ori r0,r0,0 */
3139 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r2,20(r1) */
3142 else
3144 if (next == 0x80410014) /* lwz r2,20(r1) */
3145 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
3148 else if (NULL != h && bfd_link_hash_undefined == h->root.type)
3150 /* Normally, this relocation is against a defined symbol. In the
3151 case where this is a partial link and the output section offset
3152 is greater than 2^25, the linker will return an invalid error
3153 message that the relocation has been truncated. Yes it has been
3154 truncated but no it not important. For this case, disable the
3155 overflow checking. */
3157 howto->complain_on_overflow = complain_overflow_dont;
3160 /* Check if a stub is needed. */
3161 stub_type = bfd_xcoff_type_of_stub (input_section, rel, val, h);
3162 if (stub_type != xcoff_stub_none)
3164 asection *stub_csect;
3166 stub_entry = bfd_xcoff_get_stub_entry (input_section, h, info);
3167 if (stub_entry == NULL)
3169 _bfd_error_handler (_("Unable to find the stub entry targeting %s"),
3170 h->root.root.string);
3171 bfd_set_error (bfd_error_bad_value);
3172 return false;
3175 stub_csect = stub_entry->hcsect->root.u.def.section;
3176 val = (stub_entry->stub_offset
3177 + stub_csect->output_section->vma
3178 + stub_csect->output_offset);
3181 /* The original PC-relative relocation is biased by -r_vaddr, so adding
3182 the value below will give the absolute target address. */
3183 *relocation = val + addend + rel->r_vaddr;
3185 howto->src_mask &= ~3;
3186 howto->dst_mask = howto->src_mask;
3188 if (h != NULL
3189 && (h->root.type == bfd_link_hash_defined
3190 || h->root.type == bfd_link_hash_defweak)
3191 && bfd_is_abs_section (h->root.u.def.section)
3192 && section_offset + 4 <= input_section->size)
3194 bfd_byte *ptr;
3195 bfd_vma insn;
3197 /* Turn the relative branch into an absolute one by setting the
3198 AA bit. */
3199 ptr = contents + section_offset;
3200 insn = bfd_get_32 (input_bfd, ptr);
3201 insn |= 2;
3202 bfd_put_32 (input_bfd, insn, ptr);
3204 /* Make the howto absolute too. */
3205 howto->pc_relative = false;
3206 howto->complain_on_overflow = complain_overflow_bitfield;
3208 else
3210 /* Use a PC-relative howto and subtract the instruction's address
3211 from the target address we calculated above. */
3212 howto->pc_relative = true;
3213 *relocation -= (input_section->output_section->vma
3214 + input_section->output_offset
3215 + section_offset);
3217 return true;
3220 bool
3221 xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
3222 asection *input_section,
3223 bfd *output_bfd ATTRIBUTE_UNUSED,
3224 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3225 struct internal_syment *sym ATTRIBUTE_UNUSED,
3226 struct reloc_howto_struct *howto,
3227 bfd_vma val ATTRIBUTE_UNUSED,
3228 bfd_vma addend,
3229 bfd_vma *relocation,
3230 bfd_byte *contents ATTRIBUTE_UNUSED,
3231 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3233 howto->pc_relative = true;
3234 howto->src_mask &= ~3;
3235 howto->dst_mask = howto->src_mask;
3237 /* A PC relative reloc includes the section address. */
3238 addend += input_section->vma;
3240 *relocation = val + addend;
3241 *relocation -= (input_section->output_section->vma
3242 + input_section->output_offset);
3243 return true;
3246 bool
3247 xcoff_reloc_type_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
3248 asection *input_section ATTRIBUTE_UNUSED,
3249 bfd *output_bfd ATTRIBUTE_UNUSED,
3250 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3251 struct internal_syment *sym ATTRIBUTE_UNUSED,
3252 struct reloc_howto_struct *howto,
3253 bfd_vma val,
3254 bfd_vma addend,
3255 bfd_vma *relocation,
3256 bfd_byte *contents ATTRIBUTE_UNUSED,
3257 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3259 struct xcoff_link_hash_entry *h;
3261 if (0 > rel->r_symndx)
3262 return false;
3264 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3266 /* R_TLSML is handled by the loader but must be from a
3267 TOC entry targeting itslef. This is already verified in
3268 xcoff_link_add_symbols.
3269 The value must be 0. */
3270 if (howto->type == R_TLSML)
3272 *relocation = 0;
3273 return true;
3276 /* The target symbol should always be available even if it's not
3277 exported. */
3278 BFD_ASSERT (h != NULL);
3280 /* TLS relocations must target a TLS symbol. */
3281 if (h->smclas != XMC_TL && h->smclas != XMC_UL)
3283 char vaddr_buf[128];
3285 sprintf_vma (vaddr_buf, rel->r_vaddr);
3286 _bfd_error_handler
3287 (_("%pB: TLS relocation at 0x%s over non-TLS symbol %s (0x%x)\n"),
3288 input_bfd, vaddr_buf, h->root.root.string, h->smclas);
3289 return false;
3292 /* Local TLS relocations must target a local symbol, ie
3293 non-imported. */
3294 if ((rel->r_type == R_TLS_LD || rel->r_type == R_TLS_LE)
3295 && (((h->flags & XCOFF_DEF_REGULAR) == 0
3296 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3297 || (h->flags & XCOFF_IMPORT) != 0))
3299 char vaddr_buf[128];
3301 sprintf_vma (vaddr_buf, rel->r_vaddr);
3302 _bfd_error_handler
3303 (_("%pB: TLS local relocation at 0x%s over imported symbol %s\n"),
3304 input_bfd, vaddr_buf, h->root.root.string);
3305 return false;
3308 /* R_TLSM are relocations used by the loader.
3309 The value must be 0. */
3310 if (howto->type == R_TLSM)
3312 *relocation = 0;
3313 return true;
3316 /* Other TLS relocations aims to put offsets from TLS pointers
3317 starting at -0x7c00 (or -0x7800 in XCOFF64). It becomes a
3318 simple R_POS relocation as long as .tdata and .tbss addresses
3319 start at the same value. This is done in aix ld scripts.
3320 TODO: implement optimization when tls size is < 62K. */
3321 *relocation = val + addend;
3323 return true;
3326 static bool
3327 xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
3328 bfd_vma val ATTRIBUTE_UNUSED,
3329 bfd_vma relocation ATTRIBUTE_UNUSED,
3330 struct reloc_howto_struct *
3331 howto ATTRIBUTE_UNUSED)
3333 return false;
3336 static bool
3337 xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
3338 bfd_vma val,
3339 bfd_vma relocation,
3340 struct reloc_howto_struct *howto)
3342 bfd_vma fieldmask, signmask, ss;
3343 bfd_vma a, b, sum;
3345 /* Get the values to be added together. For signed and unsigned
3346 relocations, we assume that all values should be truncated to
3347 the size of an address. For bitfields, all the bits matter.
3348 See also bfd_check_overflow. */
3349 fieldmask = N_ONES (howto->bitsize);
3350 a = relocation;
3351 b = val & howto->src_mask;
3353 /* Much like unsigned, except no trimming with addrmask. In
3354 addition, the sum overflows if there is a carry out of
3355 the bfd_vma, i.e., the sum is less than either input
3356 operand. */
3357 a >>= howto->rightshift;
3358 b >>= howto->bitpos;
3360 /* Bitfields are sometimes used for signed numbers; for
3361 example, a 13-bit field sometimes represents values in
3362 0..8191 and sometimes represents values in -4096..4095.
3363 If the field is signed and a is -4095 (0x1001) and b is
3364 -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3365 0x1fff is 0x3000). It's not clear how to handle this
3366 everywhere, since there is not way to know how many bits
3367 are significant in the relocation, but the original code
3368 assumed that it was fully sign extended, and we will keep
3369 that assumption. */
3370 signmask = (fieldmask >> 1) + 1;
3372 if ((a & ~ fieldmask) != 0)
3374 /* Some bits out of the field are set. This might not
3375 be a problem: if this is a signed bitfield, it is OK
3376 iff all the high bits are set, including the sign
3377 bit. We'll try setting all but the most significant
3378 bit in the original relocation value: if this is all
3379 ones, we are OK, assuming a signed bitfield. */
3380 ss = (signmask << howto->rightshift) - 1;
3381 if ((ss | relocation) != ~ (bfd_vma) 0)
3382 return true;
3383 a &= fieldmask;
3386 /* We just assume (b & ~ fieldmask) == 0. */
3388 /* We explicitly permit wrap around if this relocation
3389 covers the high bit of an address. The Linux kernel
3390 relies on it, and it is the only way to write assembler
3391 code which can run when loaded at a location 0x80000000
3392 away from the location at which it is linked. */
3393 if ((unsigned) howto->bitsize + howto->rightshift
3394 == bfd_arch_bits_per_address (input_bfd))
3395 return false;
3397 sum = a + b;
3398 if (sum < a || (sum & ~ fieldmask) != 0)
3400 /* There was a carry out, or the field overflow. Test
3401 for signed operands again. Here is the overflow test
3402 is as for complain_overflow_signed. */
3403 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3404 return true;
3407 return false;
3410 static bool
3411 xcoff_complain_overflow_signed_func (bfd *input_bfd,
3412 bfd_vma val,
3413 bfd_vma relocation,
3414 struct reloc_howto_struct *howto)
3416 bfd_vma addrmask, fieldmask, signmask, ss;
3417 bfd_vma a, b, sum;
3419 /* Get the values to be added together. For signed and unsigned
3420 relocations, we assume that all values should be truncated to
3421 the size of an address. For bitfields, all the bits matter.
3422 See also bfd_check_overflow. */
3423 fieldmask = N_ONES (howto->bitsize);
3424 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3425 a = relocation;
3426 b = val & howto->src_mask;
3428 a = (a & addrmask) >> howto->rightshift;
3430 /* If any sign bits are set, all sign bits must be set.
3431 That is, A must be a valid negative address after
3432 shifting. */
3433 signmask = ~ (fieldmask >> 1);
3434 ss = a & signmask;
3435 if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
3436 return true;
3438 /* We only need this next bit of code if the sign bit of B
3439 is below the sign bit of A. This would only happen if
3440 SRC_MASK had fewer bits than BITSIZE. Note that if
3441 SRC_MASK has more bits than BITSIZE, we can get into
3442 trouble; we would need to verify that B is in range, as
3443 we do for A above. */
3444 signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3445 if ((b & signmask) != 0)
3447 /* Set all the bits above the sign bit. */
3448 b -= signmask <<= 1;
3451 b = (b & addrmask) >> howto->bitpos;
3453 /* Now we can do the addition. */
3454 sum = a + b;
3456 /* See if the result has the correct sign. Bits above the
3457 sign bit are junk now; ignore them. If the sum is
3458 positive, make sure we did not have all negative inputs;
3459 if the sum is negative, make sure we did not have all
3460 positive inputs. The test below looks only at the sign
3461 bits, and it really just
3462 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3464 signmask = (fieldmask >> 1) + 1;
3465 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3466 return true;
3468 return false;
3471 static bool
3472 xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
3473 bfd_vma val,
3474 bfd_vma relocation,
3475 struct reloc_howto_struct *howto)
3477 bfd_vma addrmask, fieldmask;
3478 bfd_vma a, b, sum;
3480 /* Get the values to be added together. For signed and unsigned
3481 relocations, we assume that all values should be truncated to
3482 the size of an address. For bitfields, all the bits matter.
3483 See also bfd_check_overflow. */
3484 fieldmask = N_ONES (howto->bitsize);
3485 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3486 a = relocation;
3487 b = val & howto->src_mask;
3489 /* Checking for an unsigned overflow is relatively easy:
3490 trim the addresses and add, and trim the result as well.
3491 Overflow is normally indicated when the result does not
3492 fit in the field. However, we also need to consider the
3493 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3494 input is 0x80000000, and bfd_vma is only 32 bits; then we
3495 will get sum == 0, but there is an overflow, since the
3496 inputs did not fit in the field. Instead of doing a
3497 separate test, we can check for this by or-ing in the
3498 operands when testing for the sum overflowing its final
3499 field. */
3500 a = (a & addrmask) >> howto->rightshift;
3501 b = (b & addrmask) >> howto->bitpos;
3502 sum = (a + b) & addrmask;
3503 if ((a | b | sum) & ~ fieldmask)
3504 return true;
3506 return false;
3509 /* This is the relocation function for the RS/6000/POWER/PowerPC.
3510 This is currently the only processor which uses XCOFF; I hope that
3511 will never change.
3513 The original version was based on two documents:
3514 the PowerPC AIX Version 4 Application Binary Interface, First
3515 Edition (April 1992), and the PowerOpen ABI, Big-Endian
3516 32-Bit Hardware Implementation (June 30, 1994). Differences
3517 between the documents are noted below.
3518 Now, IBM has released an official documentation about XCOFF
3519 format:
3520 https://www.ibm.com/support/knowledgecenter/ssw_aix_72/filesreference/XCOFF.html
3522 Unsupported r_type's
3524 R_RTB:
3525 R_RRTBI:
3526 R_RRTBA:
3528 These relocs are defined by the PowerPC ABI to be
3529 relative branches which use half of the difference
3530 between the symbol and the program counter. I can't
3531 quite figure out when this is useful. These relocs are
3532 not defined by the PowerOpen ABI.
3534 Supported r_type's
3536 R_POS:
3537 Simple positive relocation.
3539 R_NEG:
3540 Simple negative relocation.
3542 R_REL:
3543 Simple PC relative relocation.
3545 R_TOC:
3546 TOC relative relocation. The value in the instruction in
3547 the input file is the offset from the input file TOC to
3548 the desired location. We want the offset from the final
3549 TOC to the desired location. We have:
3550 isym = iTOC + in
3551 iinsn = in + o
3552 osym = oTOC + on
3553 oinsn = on + o
3554 so we must change insn by on - in.
3555 This relocation allows the linker to perform optimizations
3556 by transforming a load instruction into a add-immediate
3557 when possible. The relocation is, then, changed to R_TRLA
3558 in the output file.
3559 TODO: Currently, the optimisation isn't implemented.
3561 R_TRL:
3562 TOC relative relocation. Same as R_TOC, except that
3563 the optimization isn't allowed
3565 R_TRLA:
3566 TOC relative relocation. This is a TOC relative load
3567 address instruction which have been changed to an add-
3568 immediate instruction.
3570 R_GL:
3571 GL linkage relocation. The value of this relocation
3572 is the address of the external symbol in the TOC
3573 section.
3575 R_TCL:
3576 Local object TOC address. I can't figure out the
3577 difference between this and case R_GL.
3579 R_RL:
3580 The PowerPC AIX ABI describes this as a load which may be
3581 changed to a load address. The PowerOpen ABI says this
3582 is the same as case R_POS.
3584 R_RLA:
3585 The PowerPC AIX ABI describes this as a load address
3586 which may be changed to a load. The PowerOpen ABI says
3587 this is the same as R_POS.
3589 R_REF:
3590 Not a relocation but a way to prevent the garbage
3591 collector of AIX linker to remove symbols.
3592 This is not needed in our case.
3594 R_BA:
3595 The PowerOpen ABI says this is the same as R_RBA.
3597 R_RBA:
3598 Absolute branch which may be modified to become a
3599 relative branch.
3601 R_BR:
3602 The PowerOpen ABI says this is the same as R_RBR.
3604 R_RBR:
3605 A relative branch which may be modified to become an
3606 absolute branch.
3608 R_CAI:
3609 The PowerPC ABI defines this as an absolute call which
3610 may be modified to become a relative call. The PowerOpen
3611 ABI does not define this relocation type.
3613 R_CREL:
3614 The PowerPC ABI defines this as a relative call which may
3615 be modified to become an absolute call. The PowerOpen
3616 ABI does not define this relocation type.
3618 R_RBAC:
3619 The PowerPC ABI defines this as an absolute branch to a
3620 fixed address which may be modified to an absolute branch
3621 to a symbol. The PowerOpen ABI does not define this
3622 relocation type.
3624 R_RBRC:
3625 The PowerPC ABI defines this as an absolute branch to a
3626 fixed address which may be modified to a relative branch.
3627 The PowerOpen ABI does not define this relocation type.
3629 R_TLS:
3630 Thread-local storage relocation using general-dynamic
3631 model.
3633 R_TLS_IE:
3634 Thread-local storage relocation using initial-exec model.
3636 R_TLS_LD:
3637 Thread-local storage relocation using local-dynamic model.
3639 R_TLS_LE:
3640 Thread-local storage relocation using local-exec model.
3642 R_TLSM:
3643 Tread-local storage relocation used by the loader.
3645 R_TLSML:
3646 Tread-local storage relocation used by the loader.
3648 R_TOCU:
3649 Upper TOC relative relocation. The value is the
3650 high-order 16 bit of a TOC relative relocation.
3652 R_TOCL:
3653 Lower TOC relative relocation. The value is the
3654 low-order 16 bit of a TOC relative relocation.
3657 bool
3658 xcoff_ppc_relocate_section (bfd *output_bfd,
3659 struct bfd_link_info *info,
3660 bfd *input_bfd,
3661 asection *input_section,
3662 bfd_byte *contents,
3663 struct internal_reloc *relocs,
3664 struct internal_syment *syms,
3665 asection **sections)
3667 struct internal_reloc *rel;
3668 struct internal_reloc *relend;
3670 rel = relocs;
3671 relend = rel + input_section->reloc_count;
3672 for (; rel < relend; rel++)
3674 long symndx;
3675 struct xcoff_link_hash_entry *h;
3676 struct internal_syment *sym;
3677 bfd_vma addend;
3678 bfd_vma val;
3679 struct reloc_howto_struct howto;
3680 bfd_vma relocation;
3681 bfd_vma value_to_relocate;
3682 bfd_vma address;
3683 bfd_byte *location;
3685 /* Relocation type R_REF is a special relocation type which is
3686 merely used to prevent garbage collection from occurring for
3687 the csect including the symbol which it references. */
3688 if (rel->r_type == R_REF)
3689 continue;
3691 /* Retrieve default value in HOWTO table and fix up according
3692 to r_size field, if it can be different.
3693 This should be made during relocation reading but the algorithms
3694 are expecting constant howtos. */
3695 memcpy (&howto, &xcoff_howto_table[rel->r_type], sizeof (howto));
3696 if (howto.bitsize != (rel->r_size & 0x1f) + 1)
3698 switch (rel->r_type)
3700 case R_POS:
3701 case R_NEG:
3702 howto.bitsize = (rel->r_size & 0x1f) + 1;
3703 howto.size = HOWTO_RSIZE (howto.bitsize > 16 ? 4 : 2);
3704 howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
3705 break;
3707 default:
3709 char vaddr_buf[128];
3711 sprintf_vma (vaddr_buf, rel->r_vaddr);
3712 _bfd_error_handler
3713 (_("%pB: relocation (%d) at 0x%s has wrong r_rsize (0x%x)\n"),
3714 input_bfd, rel->r_type, vaddr_buf, rel->r_size);
3715 return false;
3720 howto.complain_on_overflow = (rel->r_size & 0x80
3721 ? complain_overflow_signed
3722 : complain_overflow_bitfield);
3724 /* symbol */
3725 val = 0;
3726 addend = 0;
3727 h = NULL;
3728 sym = NULL;
3729 symndx = rel->r_symndx;
3731 if (-1 != symndx)
3733 asection *sec;
3735 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3736 sym = syms + symndx;
3737 addend = - sym->n_value;
3739 if (NULL == h)
3741 sec = sections[symndx];
3742 /* Hack to make sure we use the right TOC anchor value
3743 if this reloc is against the TOC anchor. */
3744 if (sec->name[3] == '0'
3745 && strcmp (sec->name, ".tc0") == 0)
3746 val = xcoff_data (output_bfd)->toc;
3747 else
3748 val = (sec->output_section->vma
3749 + sec->output_offset
3750 + sym->n_value
3751 - sec->vma);
3753 else
3755 if (info->unresolved_syms_in_objects != RM_IGNORE
3756 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3757 (*info->callbacks->undefined_symbol)
3758 (info, h->root.root.string,
3759 input_bfd, input_section,
3760 rel->r_vaddr - input_section->vma,
3761 info->unresolved_syms_in_objects == RM_DIAGNOSE &&
3762 !info->warn_unresolved_syms);
3764 if (h->root.type == bfd_link_hash_defined
3765 || h->root.type == bfd_link_hash_defweak)
3767 sec = h->root.u.def.section;
3768 val = (h->root.u.def.value
3769 + sec->output_section->vma
3770 + sec->output_offset);
3772 else if (h->root.type == bfd_link_hash_common)
3774 sec = h->root.u.c.p->section;
3775 val = (sec->output_section->vma
3776 + sec->output_offset);
3779 else
3781 BFD_ASSERT (bfd_link_relocatable (info)
3782 || (info->static_link
3783 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3784 || (h->flags & XCOFF_DEF_DYNAMIC) != 0
3785 || (h->flags & XCOFF_IMPORT) != 0);
3790 if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
3791 || !((*xcoff_calculate_relocation[rel->r_type])
3792 (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
3793 addend, &relocation, contents, info)))
3794 return false;
3796 /* address */
3797 address = rel->r_vaddr - input_section->vma;
3798 location = contents + address;
3800 if (address > input_section->size)
3801 abort ();
3803 /* Get the value we are going to relocate. */
3804 if (2 == bfd_get_reloc_size (&howto))
3805 value_to_relocate = bfd_get_16 (input_bfd, location);
3806 else
3807 value_to_relocate = bfd_get_32 (input_bfd, location);
3809 /* overflow.
3811 FIXME: We may drop bits during the addition
3812 which we don't check for. We must either check at every single
3813 operation, which would be tedious, or we must do the computations
3814 in a type larger than bfd_vma, which would be inefficient. */
3816 if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3817 (input_bfd, value_to_relocate, relocation, &howto)))
3819 const char *name;
3820 char buf[SYMNMLEN + 1];
3821 char reloc_type_name[10];
3823 if (symndx == -1)
3825 name = "*ABS*";
3827 else if (h != NULL)
3829 name = NULL;
3831 else
3833 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3834 if (name == NULL)
3835 name = "UNKNOWN";
3837 sprintf (reloc_type_name, "0x%02x", rel->r_type);
3839 (*info->callbacks->reloc_overflow)
3840 (info, (h ? &h->root : NULL), name, reloc_type_name,
3841 (bfd_vma) 0, input_bfd, input_section,
3842 rel->r_vaddr - input_section->vma);
3845 /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE. */
3846 value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3847 | (((value_to_relocate & howto.src_mask)
3848 + relocation) & howto.dst_mask));
3850 /* Put the value back in the object file. */
3851 if (2 == bfd_get_reloc_size (&howto))
3852 bfd_put_16 (input_bfd, value_to_relocate, location);
3853 else
3854 bfd_put_32 (input_bfd, value_to_relocate, location);
3857 return true;
3860 /* gcc-8 warns (*) on all the strncpy calls in this function about
3861 possible string truncation. The "truncation" is not a bug. We
3862 have an external representation of structs with fields that are not
3863 necessarily NULL terminated and corresponding internal
3864 representation fields that are one larger so that they can always
3865 be NULL terminated.
3866 gcc versions between 4.2 and 4.6 do not allow pragma control of
3867 diagnostics inside functions, giving a hard error if you try to use
3868 the finer control available with later versions.
3869 gcc prior to 4.2 warns about diagnostic push and pop.
3870 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
3871 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
3872 (*) Depending on your system header files! */
3873 #if GCC_VERSION >= 8000
3874 # pragma GCC diagnostic push
3875 # pragma GCC diagnostic ignored "-Wstringop-truncation"
3876 #endif
3877 static bool
3878 _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
3879 struct xcoff_loader_info *ldinfo,
3880 struct internal_ldsym *ldsym,
3881 const char *name)
3883 size_t len;
3884 len = strlen (name);
3886 if (len <= SYMNMLEN)
3887 strncpy (ldsym->_l._l_name, name, SYMNMLEN);
3888 else
3890 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3892 bfd_size_type newalc;
3893 char *newstrings;
3895 newalc = ldinfo->string_alc * 2;
3896 if (newalc == 0)
3897 newalc = 32;
3898 while (ldinfo->string_size + len + 3 > newalc)
3899 newalc *= 2;
3901 newstrings = bfd_realloc (ldinfo->strings, newalc);
3902 if (newstrings == NULL)
3904 ldinfo->failed = true;
3905 return false;
3907 ldinfo->string_alc = newalc;
3908 ldinfo->strings = newstrings;
3911 ldinfo->strings[ldinfo->string_size] = ((len + 1) >> 8) & 0xff;
3912 ldinfo->strings[ldinfo->string_size + 1] = ((len + 1)) & 0xff;
3913 strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
3914 ldsym->_l._l_l._l_zeroes = 0;
3915 ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3916 ldinfo->string_size += len + 3;
3919 return true;
3922 static bool
3923 _bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
3924 struct bfd_strtab_hash *strtab,
3925 struct internal_syment *sym,
3926 const char *name)
3928 if (strlen (name) <= SYMNMLEN)
3930 strncpy (sym->_n._n_name, name, SYMNMLEN);
3932 else
3934 bool hash;
3935 bfd_size_type indx;
3937 hash = !info->traditional_format;
3938 indx = _bfd_stringtab_add (strtab, name, hash, false);
3939 if (indx == (bfd_size_type) -1)
3940 return false;
3941 sym->_n._n_n._n_zeroes = 0;
3942 sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3944 return true;
3946 #if GCC_VERSION >= 8000
3947 # pragma GCC diagnostic pop
3948 #endif
3950 static asection *
3951 xcoff_create_csect_from_smclas (bfd *abfd,
3952 union internal_auxent *aux,
3953 const char *symbol_name)
3955 asection *return_value = NULL;
3957 /* .sv64 = x_smclas == 17
3958 This is an invalid csect for 32 bit apps. */
3959 static const char * const names[] =
3961 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
3962 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
3963 ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
3966 if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
3967 && (NULL != names[aux->x_csect.x_smclas]))
3969 return_value = bfd_make_section_anyway
3970 (abfd, names[aux->x_csect.x_smclas]);
3972 else
3974 _bfd_error_handler
3975 /* xgettext: c-format */
3976 (_("%pB: symbol `%s' has unrecognized smclas %d"),
3977 abfd, symbol_name, aux->x_csect.x_smclas);
3978 bfd_set_error (bfd_error_bad_value);
3981 return return_value;
3984 static bool
3985 xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3987 if (0xffff <= value)
3988 return true;
3990 return false;
3993 static bool
3994 xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3996 if (0xffff <= value)
3997 return true;
3999 return false;
4002 static bfd_vma
4003 xcoff_loader_symbol_offset (bfd *abfd,
4004 struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
4006 return bfd_xcoff_ldhdrsz (abfd);
4009 static bfd_vma
4010 xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
4012 return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
4015 static bool
4016 xcoff_generate_rtinit (bfd *abfd, const char *init, const char *fini,
4017 bool rtld)
4019 bfd_byte filehdr_ext[FILHSZ];
4020 bfd_byte scnhdr_ext[SCNHSZ];
4021 bfd_byte syment_ext[SYMESZ * 10];
4022 bfd_byte reloc_ext[RELSZ * 3];
4023 bfd_byte *data_buffer;
4024 bfd_size_type data_buffer_size;
4025 bfd_byte *string_table = NULL, *st_tmp = NULL;
4026 bfd_size_type string_table_size;
4027 bfd_vma val;
4028 size_t initsz, finisz;
4029 struct internal_filehdr filehdr;
4030 struct internal_scnhdr scnhdr;
4031 struct internal_syment syment;
4032 union internal_auxent auxent;
4033 struct internal_reloc reloc;
4035 char *data_name = ".data";
4036 char *rtinit_name = "__rtinit";
4037 char *rtld_name = "__rtld";
4039 if (! bfd_xcoff_rtinit_size (abfd))
4040 return false;
4042 initsz = (init == NULL ? 0 : 1 + strlen (init));
4043 finisz = (fini == NULL ? 0 : 1 + strlen (fini));
4045 /* file header */
4046 memset (filehdr_ext, 0, FILHSZ);
4047 memset (&filehdr, 0, sizeof (struct internal_filehdr));
4048 filehdr.f_magic = bfd_xcoff_magic_number (abfd);
4049 filehdr.f_nscns = 1;
4050 filehdr.f_timdat = 0;
4051 filehdr.f_nsyms = 0; /* at least 6, no more than 10 */
4052 filehdr.f_symptr = 0; /* set below */
4053 filehdr.f_opthdr = 0;
4054 filehdr.f_flags = 0;
4056 /* section header */
4057 memset (scnhdr_ext, 0, SCNHSZ);
4058 memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
4059 memcpy (scnhdr.s_name, data_name, strlen (data_name));
4060 scnhdr.s_paddr = 0;
4061 scnhdr.s_vaddr = 0;
4062 scnhdr.s_size = 0; /* set below */
4063 scnhdr.s_scnptr = FILHSZ + SCNHSZ;
4064 scnhdr.s_relptr = 0; /* set below */
4065 scnhdr.s_lnnoptr = 0;
4066 scnhdr.s_nreloc = 0; /* either 1 or 2 */
4067 scnhdr.s_nlnno = 0;
4068 scnhdr.s_flags = STYP_DATA;
4070 /* .data
4071 0x0000 0x00000000 : rtl
4072 0x0004 0x00000010 : offset to init, or 0
4073 0x0008 0x00000028 : offset to fini, or 0
4074 0x000C 0x0000000C : size of descriptor
4075 0x0010 0x00000000 : init, needs a reloc
4076 0x0014 0x00000040 : offset to init name
4077 0x0018 0x00000000 : flags, padded to a word
4078 0x001C 0x00000000 : empty init
4079 0x0020 0x00000000 :
4080 0x0024 0x00000000 :
4081 0x0028 0x00000000 : fini, needs a reloc
4082 0x002C 0x00000??? : offset to fini name
4083 0x0030 0x00000000 : flags, padded to a word
4084 0x0034 0x00000000 : empty fini
4085 0x0038 0x00000000 :
4086 0x003C 0x00000000 :
4087 0x0040 init name
4088 0x0040 + initsz fini name */
4090 data_buffer_size = 0x0040 + initsz + finisz;
4091 data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
4092 data_buffer = NULL;
4093 data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
4094 if (data_buffer == NULL)
4095 return false;
4097 if (initsz)
4099 val = 0x10;
4100 bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
4101 val = 0x40;
4102 bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
4103 memcpy (&data_buffer[val], init, initsz);
4106 if (finisz)
4108 val = 0x28;
4109 bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
4110 val = 0x40 + initsz;
4111 bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
4112 memcpy (&data_buffer[val], fini, finisz);
4115 val = 0x0C;
4116 bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
4118 scnhdr.s_size = data_buffer_size;
4120 /* string table */
4121 string_table_size = 0;
4122 if (initsz > 9)
4123 string_table_size += initsz;
4124 if (finisz > 9)
4125 string_table_size += finisz;
4126 if (string_table_size)
4128 string_table_size += 4;
4129 string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
4130 if (string_table == NULL)
4131 return false;
4133 val = string_table_size;
4134 bfd_h_put_32 (abfd, val, &string_table[0]);
4135 st_tmp = string_table + 4;
4138 /* symbols
4139 0. .data csect
4140 2. __rtinit
4141 4. init function
4142 6. fini function
4143 8. __rtld */
4144 memset (syment_ext, 0, 10 * SYMESZ);
4145 memset (reloc_ext, 0, 3 * RELSZ);
4147 /* .data csect */
4148 memset (&syment, 0, sizeof (struct internal_syment));
4149 memset (&auxent, 0, sizeof (union internal_auxent));
4150 memcpy (syment._n._n_name, data_name, strlen (data_name));
4151 syment.n_scnum = 1;
4152 syment.n_sclass = C_HIDEXT;
4153 syment.n_numaux = 1;
4154 auxent.x_csect.x_scnlen.l = data_buffer_size;
4155 auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
4156 auxent.x_csect.x_smclas = XMC_RW;
4157 bfd_coff_swap_sym_out (abfd, &syment,
4158 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4159 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4160 syment.n_numaux,
4161 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4162 filehdr.f_nsyms += 2;
4164 /* __rtinit */
4165 memset (&syment, 0, sizeof (struct internal_syment));
4166 memset (&auxent, 0, sizeof (union internal_auxent));
4167 memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
4168 syment.n_scnum = 1;
4169 syment.n_sclass = C_EXT;
4170 syment.n_numaux = 1;
4171 auxent.x_csect.x_smtyp = XTY_LD;
4172 auxent.x_csect.x_smclas = XMC_RW;
4173 bfd_coff_swap_sym_out (abfd, &syment,
4174 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4175 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4176 syment.n_numaux,
4177 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4178 filehdr.f_nsyms += 2;
4180 /* init */
4181 if (initsz)
4183 memset (&syment, 0, sizeof (struct internal_syment));
4184 memset (&auxent, 0, sizeof (union internal_auxent));
4186 if (initsz > 9)
4188 syment._n._n_n._n_offset = st_tmp - string_table;
4189 memcpy (st_tmp, init, initsz);
4190 st_tmp += initsz;
4192 else
4193 memcpy (syment._n._n_name, init, initsz - 1);
4195 syment.n_sclass = C_EXT;
4196 syment.n_numaux = 1;
4197 bfd_coff_swap_sym_out (abfd, &syment,
4198 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4199 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4200 syment.n_numaux,
4201 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4203 /* reloc */
4204 memset (&reloc, 0, sizeof (struct internal_reloc));
4205 reloc.r_vaddr = 0x0010;
4206 reloc.r_symndx = filehdr.f_nsyms;
4207 reloc.r_type = R_POS;
4208 reloc.r_size = 31;
4209 bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
4211 filehdr.f_nsyms += 2;
4212 scnhdr.s_nreloc += 1;
4215 /* fini */
4216 if (finisz)
4218 memset (&syment, 0, sizeof (struct internal_syment));
4219 memset (&auxent, 0, sizeof (union internal_auxent));
4221 if (finisz > 9)
4223 syment._n._n_n._n_offset = st_tmp - string_table;
4224 memcpy (st_tmp, fini, finisz);
4225 st_tmp += finisz;
4227 else
4228 memcpy (syment._n._n_name, fini, finisz - 1);
4230 syment.n_sclass = C_EXT;
4231 syment.n_numaux = 1;
4232 bfd_coff_swap_sym_out (abfd, &syment,
4233 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4234 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4235 syment.n_numaux,
4236 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4238 /* reloc */
4239 memset (&reloc, 0, sizeof (struct internal_reloc));
4240 reloc.r_vaddr = 0x0028;
4241 reloc.r_symndx = filehdr.f_nsyms;
4242 reloc.r_type = R_POS;
4243 reloc.r_size = 31;
4244 bfd_coff_swap_reloc_out (abfd, &reloc,
4245 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4247 filehdr.f_nsyms += 2;
4248 scnhdr.s_nreloc += 1;
4251 if (rtld)
4253 memset (&syment, 0, sizeof (struct internal_syment));
4254 memset (&auxent, 0, sizeof (union internal_auxent));
4255 memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
4256 syment.n_sclass = C_EXT;
4257 syment.n_numaux = 1;
4258 bfd_coff_swap_sym_out (abfd, &syment,
4259 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4260 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4261 syment.n_numaux,
4262 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4264 /* reloc */
4265 memset (&reloc, 0, sizeof (struct internal_reloc));
4266 reloc.r_vaddr = 0x0000;
4267 reloc.r_symndx = filehdr.f_nsyms;
4268 reloc.r_type = R_POS;
4269 reloc.r_size = 31;
4270 bfd_coff_swap_reloc_out (abfd, &reloc,
4271 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4273 filehdr.f_nsyms += 2;
4274 scnhdr.s_nreloc += 1;
4277 scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
4278 filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
4280 bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
4281 bfd_bwrite (filehdr_ext, FILHSZ, abfd);
4282 bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
4283 bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
4284 bfd_bwrite (data_buffer, data_buffer_size, abfd);
4285 bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
4286 bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
4287 bfd_bwrite (string_table, string_table_size, abfd);
4289 free (data_buffer);
4290 data_buffer = NULL;
4292 return true;
4296 static reloc_howto_type xcoff_dynamic_reloc =
4297 HOWTO (0, /* type */
4298 0, /* rightshift */
4299 4, /* size */
4300 32, /* bitsize */
4301 false, /* pc_relative */
4302 0, /* bitpos */
4303 complain_overflow_bitfield, /* complain_on_overflow */
4304 0, /* special_function */
4305 "R_POS", /* name */
4306 true, /* partial_inplace */
4307 0xffffffff, /* src_mask */
4308 0xffffffff, /* dst_mask */
4309 false); /* pcrel_offset */
4311 /* Indirect call stub
4312 The first word of the code must be modified by filling in
4313 the correct TOC offset. */
4315 static const unsigned long xcoff_stub_indirect_call_code[4] =
4317 0x81820000, /* lwz r12,0(r2) */
4318 0x800c0000, /* lwz r0,0(r12) */
4319 0x7c0903a6, /* mtctr r0 */
4320 0x4e800420, /* bctr */
4323 /* Shared call stub
4324 The first word of the code must be modified by filling in
4325 the correct TOC offset.
4326 This is exactly as the glink code but without the traceback,
4327 as it won't be an independent function. */
4329 static const unsigned long xcoff_stub_shared_call_code[6] =
4331 0x81820000, /* lwz r12,0(r2) */
4332 0x90410014, /* stw r2,20(r1) */
4333 0x800c0000, /* lwz r0,0(r12) */
4334 0x804c0004, /* lwz r2,4(r12) */
4335 0x7c0903a6, /* mtctr r0 */
4336 0x4e800420, /* bctr */
4339 /* glink
4341 The first word of global linkage code must be modified by filling in
4342 the correct TOC offset. */
4344 static const unsigned long xcoff_glink_code[9] =
4346 0x81820000, /* lwz r12,0(r2) */
4347 0x90410014, /* stw r2,20(r1) */
4348 0x800c0000, /* lwz r0,0(r12) */
4349 0x804c0004, /* lwz r2,4(r12) */
4350 0x7c0903a6, /* mtctr r0 */
4351 0x4e800420, /* bctr */
4352 0x00000000, /* start of traceback table */
4353 0x000c8000, /* traceback table */
4354 0x00000000, /* traceback table */
4357 /* Table to convert DWARF flags to section names.
4358 Remember to update binutils/dwarf.c:debug_displays
4359 if new DWARF sections are supported by XCOFF. */
4361 const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
4362 { SSUBTYP_DWINFO, ".dwinfo", ".debug_info", true },
4363 { SSUBTYP_DWLINE, ".dwline", ".debug_line", true },
4364 { SSUBTYP_DWPBNMS, ".dwpbnms", ".debug_pubnames", true },
4365 { SSUBTYP_DWPBTYP, ".dwpbtyp", ".debug_pubtypes", true },
4366 { SSUBTYP_DWARNGE, ".dwarnge", ".debug_aranges", true },
4367 { SSUBTYP_DWABREV, ".dwabrev", ".debug_abbrev", false },
4368 { SSUBTYP_DWSTR, ".dwstr", ".debug_str", true },
4369 { SSUBTYP_DWRNGES, ".dwrnges", ".debug_ranges", true },
4370 { SSUBTYP_DWLOC, ".dwloc", ".debug_loc", true },
4371 { SSUBTYP_DWFRAME, ".dwframe", ".debug_frame", true },
4372 { SSUBTYP_DWMAC, ".dwmac", ".debug_macro", true }
4375 /* For generic entry points. */
4376 #define _bfd_xcoff_close_and_cleanup _bfd_archive_close_and_cleanup
4377 #define _bfd_xcoff_bfd_free_cached_info _bfd_bool_bfd_true
4378 #define _bfd_xcoff_new_section_hook coff_new_section_hook
4379 #define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
4380 #define _bfd_xcoff_get_section_contents_in_window \
4381 _bfd_generic_get_section_contents_in_window
4383 /* For copy private data entry points. */
4384 #define _bfd_xcoff_bfd_copy_private_bfd_data \
4385 _bfd_xcoff_copy_private_bfd_data
4386 #define _bfd_xcoff_bfd_merge_private_bfd_data \
4387 _bfd_generic_bfd_merge_private_bfd_data
4388 #define _bfd_xcoff_bfd_copy_private_section_data \
4389 _bfd_generic_bfd_copy_private_section_data
4390 #define _bfd_xcoff_bfd_copy_private_symbol_data \
4391 _bfd_generic_bfd_copy_private_symbol_data
4392 #define _bfd_xcoff_bfd_copy_private_header_data \
4393 _bfd_generic_bfd_copy_private_header_data
4394 #define _bfd_xcoff_bfd_set_private_flags \
4395 _bfd_generic_bfd_set_private_flags
4396 #define _bfd_xcoff_bfd_print_private_bfd_data \
4397 _bfd_generic_bfd_print_private_bfd_data
4399 /* For archive entry points. */
4400 #define _bfd_xcoff_slurp_extended_name_table \
4401 _bfd_noarchive_slurp_extended_name_table
4402 #define _bfd_xcoff_construct_extended_name_table \
4403 _bfd_noarchive_construct_extended_name_table
4404 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
4405 #define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
4406 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
4407 #define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
4408 #define _bfd_xcoff_update_armap_timestamp _bfd_bool_bfd_true
4410 /* For symbols entry points. */
4411 #define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
4412 #define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
4413 #define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
4414 #define _bfd_xcoff_print_symbol coff_print_symbol
4415 #define _bfd_xcoff_get_symbol_info coff_get_symbol_info
4416 #define _bfd_xcoff_get_symbol_version_string \
4417 _bfd_nosymbols_get_symbol_version_string
4418 #define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
4419 #define _bfd_xcoff_bfd_is_target_special_symbol \
4420 coff_bfd_is_target_special_symbol
4421 #define _bfd_xcoff_get_lineno coff_get_lineno
4422 #define _bfd_xcoff_find_nearest_line coff_find_nearest_line
4423 #define _bfd_xcoff_find_line coff_find_line
4424 #define _bfd_xcoff_find_inliner_info coff_find_inliner_info
4425 #define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
4426 #define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
4427 #define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
4429 /* For reloc entry points. */
4430 #define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
4431 #define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
4432 #define _bfd_xcoff_set_reloc _bfd_generic_set_reloc
4433 #define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
4434 #define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
4436 /* For link entry points. */
4437 #define _bfd_xcoff_bfd_get_relocated_section_contents \
4438 bfd_generic_get_relocated_section_contents
4439 #define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
4440 #define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
4441 #define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
4442 #define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
4443 _bfd_generic_copy_link_hash_symbol_type
4444 #define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
4445 #define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
4446 #define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
4447 #define _bfd_xcoff_bfd_merge_sections bfd_generic_merge_sections
4448 #define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
4449 #define _bfd_xcoff_bfd_group_name bfd_generic_group_name
4450 #define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
4451 #define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
4452 #define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
4453 #define _bfd_xcoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
4454 #define _bfd_xcoff_bfd_define_start_stop bfd_generic_define_start_stop
4455 #define _bfd_xcoff_bfd_link_check_relocs _bfd_generic_link_check_relocs
4457 /* For dynamic symbols and relocs entry points. */
4458 #define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
4460 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
4462 { /* COFF backend, defined in libcoff.h. */
4463 _bfd_xcoff_swap_aux_in,
4464 _bfd_xcoff_swap_sym_in,
4465 coff_swap_lineno_in,
4466 _bfd_xcoff_swap_aux_out,
4467 _bfd_xcoff_swap_sym_out,
4468 coff_swap_lineno_out,
4469 xcoff_swap_reloc_out,
4470 coff_swap_filehdr_out,
4471 coff_swap_aouthdr_out,
4472 coff_swap_scnhdr_out,
4473 FILHSZ,
4474 AOUTSZ,
4475 SCNHSZ,
4476 SYMESZ,
4477 AUXESZ,
4478 RELSZ,
4479 LINESZ,
4480 FILNMLEN,
4481 true, /* _bfd_coff_long_filenames */
4482 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4483 3, /* _bfd_coff_default_section_alignment_power */
4484 false, /* _bfd_coff_force_symnames_in_strings */
4485 2, /* _bfd_coff_debug_string_prefix_length */
4486 32768, /* _bfd_coff_max_nscns */
4487 coff_swap_filehdr_in,
4488 coff_swap_aouthdr_in,
4489 coff_swap_scnhdr_in,
4490 xcoff_swap_reloc_in,
4491 coff_bad_format_hook,
4492 coff_set_arch_mach_hook,
4493 coff_mkobject_hook,
4494 styp_to_sec_flags,
4495 coff_set_alignment_hook,
4496 coff_slurp_symbol_table,
4497 symname_in_debug_hook,
4498 coff_pointerize_aux_hook,
4499 coff_print_aux,
4500 dummy_reloc16_extra_cases,
4501 dummy_reloc16_estimate,
4502 NULL, /* bfd_coff_sym_is_global */
4503 coff_compute_section_file_positions,
4504 NULL, /* _bfd_coff_start_final_link */
4505 xcoff_ppc_relocate_section,
4506 coff_rtype_to_howto,
4507 NULL, /* _bfd_coff_adjust_symndx */
4508 _bfd_generic_link_add_one_symbol,
4509 coff_link_output_has_begun,
4510 coff_final_link_postscript,
4511 NULL /* print_pdata. */
4514 0x01DF, /* magic number */
4515 bfd_arch_rs6000,
4516 bfd_mach_rs6k,
4518 /* Function pointers to xcoff specific swap routines. */
4519 xcoff_swap_ldhdr_in,
4520 xcoff_swap_ldhdr_out,
4521 xcoff_swap_ldsym_in,
4522 xcoff_swap_ldsym_out,
4523 xcoff_swap_ldrel_in,
4524 xcoff_swap_ldrel_out,
4526 /* Sizes. */
4527 LDHDRSZ,
4528 LDSYMSZ,
4529 LDRELSZ,
4530 12, /* _xcoff_function_descriptor_size */
4531 SMALL_AOUTSZ,
4533 /* Versions. */
4534 1, /* _xcoff_ldhdr_version */
4536 _bfd_xcoff_put_symbol_name,
4537 _bfd_xcoff_put_ldsymbol_name,
4538 &xcoff_dynamic_reloc,
4539 xcoff_create_csect_from_smclas,
4541 /* Lineno and reloc count overflow. */
4542 xcoff_is_lineno_count_overflow,
4543 xcoff_is_reloc_count_overflow,
4545 xcoff_loader_symbol_offset,
4546 xcoff_loader_reloc_offset,
4548 /* glink. */
4549 &xcoff_glink_code[0],
4550 36, /* _xcoff_glink_size */
4552 /* rtinit */
4553 64, /* _xcoff_rtinit_size */
4554 xcoff_generate_rtinit,
4556 /* Stub indirect call. */
4557 &xcoff_stub_indirect_call_code[0],
4558 16, /* _xcoff_stub_indirect_call_size */
4560 /* Stub shared call. */
4561 &xcoff_stub_shared_call_code[0],
4562 24, /* _xcoff_stub_shared_call_size */
4565 /* The transfer vector that leads the outside world to all of the above. */
4566 const bfd_target rs6000_xcoff_vec =
4568 "aixcoff-rs6000",
4569 bfd_target_xcoff_flavour,
4570 BFD_ENDIAN_BIG, /* data byte order is big */
4571 BFD_ENDIAN_BIG, /* header byte order is big */
4573 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4574 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4576 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4577 0, /* leading char */
4578 '/', /* ar_pad_char */
4579 15, /* ar_max_namelen */
4580 0, /* match priority. */
4581 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
4583 /* data */
4584 bfd_getb64,
4585 bfd_getb_signed_64,
4586 bfd_putb64,
4587 bfd_getb32,
4588 bfd_getb_signed_32,
4589 bfd_putb32,
4590 bfd_getb16,
4591 bfd_getb_signed_16,
4592 bfd_putb16,
4594 /* hdrs */
4595 bfd_getb64,
4596 bfd_getb_signed_64,
4597 bfd_putb64,
4598 bfd_getb32,
4599 bfd_getb_signed_32,
4600 bfd_putb32,
4601 bfd_getb16,
4602 bfd_getb_signed_16,
4603 bfd_putb16,
4605 { /* bfd_check_format */
4606 _bfd_dummy_target,
4607 coff_object_p,
4608 _bfd_xcoff_archive_p,
4609 CORE_FILE_P
4612 { /* bfd_set_format */
4613 _bfd_bool_bfd_false_error,
4614 coff_mkobject,
4615 _bfd_generic_mkarchive,
4616 _bfd_bool_bfd_false_error
4619 {/* bfd_write_contents */
4620 _bfd_bool_bfd_false_error,
4621 coff_write_object_contents,
4622 _bfd_xcoff_write_archive_contents,
4623 _bfd_bool_bfd_false_error
4626 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4627 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4628 BFD_JUMP_TABLE_CORE (coff),
4629 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4630 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4631 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4632 BFD_JUMP_TABLE_WRITE (coff),
4633 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4634 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4636 /* Opposite endian version, none exists */
4637 NULL,
4639 & bfd_xcoff_backend_data,
4642 /* xcoff-powermac target
4643 Old target.
4644 Only difference between this target and the rs6000 target is the
4645 the default architecture and machine type used in coffcode.h
4647 PowerPC Macs use the same magic numbers as RS/6000
4648 (because that's how they were bootstrapped originally),
4649 but they are always PowerPC architecture. */
4650 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
4652 { /* COFF backend, defined in libcoff.h. */
4653 _bfd_xcoff_swap_aux_in,
4654 _bfd_xcoff_swap_sym_in,
4655 coff_swap_lineno_in,
4656 _bfd_xcoff_swap_aux_out,
4657 _bfd_xcoff_swap_sym_out,
4658 coff_swap_lineno_out,
4659 xcoff_swap_reloc_out,
4660 coff_swap_filehdr_out,
4661 coff_swap_aouthdr_out,
4662 coff_swap_scnhdr_out,
4663 FILHSZ,
4664 AOUTSZ,
4665 SCNHSZ,
4666 SYMESZ,
4667 AUXESZ,
4668 RELSZ,
4669 LINESZ,
4670 FILNMLEN,
4671 true, /* _bfd_coff_long_filenames */
4672 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4673 3, /* _bfd_coff_default_section_alignment_power */
4674 false, /* _bfd_coff_force_symnames_in_strings */
4675 2, /* _bfd_coff_debug_string_prefix_length */
4676 32768, /* _bfd_coff_max_nscns */
4677 coff_swap_filehdr_in,
4678 coff_swap_aouthdr_in,
4679 coff_swap_scnhdr_in,
4680 xcoff_swap_reloc_in,
4681 coff_bad_format_hook,
4682 coff_set_arch_mach_hook,
4683 coff_mkobject_hook,
4684 styp_to_sec_flags,
4685 coff_set_alignment_hook,
4686 coff_slurp_symbol_table,
4687 symname_in_debug_hook,
4688 coff_pointerize_aux_hook,
4689 coff_print_aux,
4690 dummy_reloc16_extra_cases,
4691 dummy_reloc16_estimate,
4692 NULL, /* bfd_coff_sym_is_global */
4693 coff_compute_section_file_positions,
4694 NULL, /* _bfd_coff_start_final_link */
4695 xcoff_ppc_relocate_section,
4696 coff_rtype_to_howto,
4697 NULL, /* _bfd_coff_adjust_symndx */
4698 _bfd_generic_link_add_one_symbol,
4699 coff_link_output_has_begun,
4700 coff_final_link_postscript,
4701 NULL /* print_pdata. */
4704 0x01DF, /* magic number */
4705 bfd_arch_powerpc,
4706 bfd_mach_ppc,
4708 /* Function pointers to xcoff specific swap routines. */
4709 xcoff_swap_ldhdr_in,
4710 xcoff_swap_ldhdr_out,
4711 xcoff_swap_ldsym_in,
4712 xcoff_swap_ldsym_out,
4713 xcoff_swap_ldrel_in,
4714 xcoff_swap_ldrel_out,
4716 /* Sizes. */
4717 LDHDRSZ,
4718 LDSYMSZ,
4719 LDRELSZ,
4720 12, /* _xcoff_function_descriptor_size */
4721 SMALL_AOUTSZ,
4723 /* Versions. */
4724 1, /* _xcoff_ldhdr_version */
4726 _bfd_xcoff_put_symbol_name,
4727 _bfd_xcoff_put_ldsymbol_name,
4728 &xcoff_dynamic_reloc,
4729 xcoff_create_csect_from_smclas,
4731 /* Lineno and reloc count overflow. */
4732 xcoff_is_lineno_count_overflow,
4733 xcoff_is_reloc_count_overflow,
4735 xcoff_loader_symbol_offset,
4736 xcoff_loader_reloc_offset,
4738 /* glink. */
4739 &xcoff_glink_code[0],
4740 36, /* _xcoff_glink_size */
4742 /* rtinit */
4743 0, /* _xcoff_rtinit_size */
4744 xcoff_generate_rtinit,
4746 /* Stub indirect call. */
4747 &xcoff_stub_indirect_call_code[0],
4748 16, /* _xcoff_stub_indirect_call_size */
4750 /* Stub shared call. */
4751 &xcoff_stub_shared_call_code[0],
4752 24, /* _xcoff_stub_shared_call_size */
4755 /* The transfer vector that leads the outside world to all of the above. */
4756 const bfd_target powerpc_xcoff_vec =
4758 "xcoff-powermac",
4759 bfd_target_xcoff_flavour,
4760 BFD_ENDIAN_BIG, /* data byte order is big */
4761 BFD_ENDIAN_BIG, /* header byte order is big */
4763 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4764 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4766 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4767 0, /* leading char */
4768 '/', /* ar_pad_char */
4769 15, /* ar_max_namelen */
4770 0, /* match priority. */
4771 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
4773 /* data */
4774 bfd_getb64,
4775 bfd_getb_signed_64,
4776 bfd_putb64,
4777 bfd_getb32,
4778 bfd_getb_signed_32,
4779 bfd_putb32,
4780 bfd_getb16,
4781 bfd_getb_signed_16,
4782 bfd_putb16,
4784 /* hdrs */
4785 bfd_getb64,
4786 bfd_getb_signed_64,
4787 bfd_putb64,
4788 bfd_getb32,
4789 bfd_getb_signed_32,
4790 bfd_putb32,
4791 bfd_getb16,
4792 bfd_getb_signed_16,
4793 bfd_putb16,
4795 { /* bfd_check_format */
4796 _bfd_dummy_target,
4797 coff_object_p,
4798 _bfd_xcoff_archive_p,
4799 CORE_FILE_P
4802 { /* bfd_set_format */
4803 _bfd_bool_bfd_false_error,
4804 coff_mkobject,
4805 _bfd_generic_mkarchive,
4806 _bfd_bool_bfd_false_error
4809 {/* bfd_write_contents */
4810 _bfd_bool_bfd_false_error,
4811 coff_write_object_contents,
4812 _bfd_xcoff_write_archive_contents,
4813 _bfd_bool_bfd_false_error
4816 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4817 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4818 BFD_JUMP_TABLE_CORE (coff),
4819 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4820 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4821 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4822 BFD_JUMP_TABLE_WRITE (coff),
4823 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4824 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4826 /* Opposite endian version, none exists */
4827 NULL,
4829 & bfd_pmac_xcoff_backend_data,