Keeping track of rs6000-coff archive element pointers
[binutils-gdb.git] / bfd / coff-rs6000.c
1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2 Copyright (C) 1990-2023 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.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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. */
23
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"
34
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 *);
56
57 /* Forward declare xcoff_rtype2howto for coffcode.h macro. */
58 void xcoff_rtype2howto (arelent *, struct internal_reloc *);
59
60 /* coffcode.h needs these to be defined. */
61 #define RS6000COFF_C 1
62
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)); \
71 }
72
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
112
113 #ifndef bfd_pe_print_pdata
114 #define bfd_pe_print_pdata NULL
115 #endif
116
117 #include "coffcode.h"
118
119 /* The main body of code is in coffcode.h. */
120
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 *);
149
150 /* Relocation functions */
151 static xcoff_reloc_function xcoff_reloc_type_br;
152
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;
157
158 xcoff_reloc_function *const
159 xcoff_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION] =
160 {
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) */
211 };
212
213 xcoff_complain_function *const
214 xcoff_complain_overflow[XCOFF_MAX_COMPLAIN_OVERFLOW] =
215 {
216 xcoff_complain_overflow_dont_func,
217 xcoff_complain_overflow_bitfield_func,
218 xcoff_complain_overflow_signed_func,
219 xcoff_complain_overflow_unsigned_func,
220 };
221
222 /* Information about one member of an archive. */
223 struct member_layout
224 {
225 /* The archive member that this structure describes. */
226 bfd *member;
227
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;
232
233 /* The offset of MEMBER from the start of the archive (i.e. the end
234 of the leading padding). */
235 file_ptr offset;
236
237 /* The normalized name of MEMBER. */
238 const char *name;
239
240 /* The length of NAME, without padding. */
241 bfd_size_type namlen;
242
243 /* The length of NAME, with padding. */
244 bfd_size_type padded_namlen;
245
246 /* The size of MEMBER's header, including the name and magic sequence. */
247 bfd_size_type header_size;
248
249 /* The size of the MEMBER's contents. */
250 bfd_size_type contents_size;
251
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;
255 };
256
257 /* A structure used for iterating over the members of an archive. */
258 struct archive_iterator
259 {
260 /* The archive itself. */
261 bfd *archive;
262
263 /* Information about the current archive member. */
264 struct member_layout current;
265
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;
270 };
271
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. */
275
276 static void
277 member_layout_init (struct member_layout *info, bfd *archive,
278 bfd *member, file_ptr offset)
279 {
280 info->member = member;
281 info->leading_padding = 0;
282 if (member)
283 {
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;
294
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));
301 }
302 info->offset = offset + info->leading_padding;
303 }
304
305 /* Set up ITERATOR to iterate through archive ARCHIVE. */
306
307 static void
308 archive_iterator_begin (struct archive_iterator *iterator,
309 bfd *archive)
310 {
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);
316 }
317
318 /* Make ITERATOR visit the first unvisited archive member. Return true
319 on success; return false if all members have been visited. */
320
321 static bool
322 archive_iterator_next (struct archive_iterator *iterator)
323 {
324 if (!iterator->next.member)
325 return false;
326
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;
335 }
336
337 /* We use our own tdata type. Its first field is the COFF tdata type,
338 so the COFF routines are compatible. */
339
340 bool
341 _bfd_xcoff_mkobject (bfd *abfd)
342 {
343 coff_data_type *coff;
344 size_t amt = sizeof (struct xcoff_tdata);
345
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;
354
355 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
356
357 /* We set cputype to -1 to indicate that it has not been
358 initialized. */
359 xcoff_data (abfd)->cputype = -1;
360
361 xcoff_data (abfd)->csects = NULL;
362 xcoff_data (abfd)->debug_indices = NULL;
363
364 /* text section alignment is different than the default */
365 bfd_xcoff_text_align_power (abfd) = 2;
366
367 return true;
368 }
369
370 /* Copy XCOFF data from one BFD to another. */
371
372 bool
373 _bfd_xcoff_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
374 {
375 struct xcoff_tdata *ix, *ox;
376 asection *sec;
377
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
387 {
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;
393 }
394 if (ix->snentry == 0)
395 ox->snentry = 0;
396 else
397 {
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;
403 }
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;
411 }
412
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. */
417
418 bool
419 _bfd_xcoff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
420 const char *name ATTRIBUTE_UNUSED)
421 {
422 return false;
423 }
424 \f
425 void
426 _bfd_xcoff_swap_sym_in (bfd *abfd, void * ext1, void * in1)
427 {
428 SYMENT *ext = (SYMENT *)ext1;
429 struct internal_syment * in = (struct internal_syment *)in1;
430
431 if (ext->e.e_name[0] != 0)
432 {
433 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
434 }
435 else
436 {
437 in->_n._n_n._n_zeroes = 0;
438 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
439 }
440
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);
446 }
447
448 unsigned int
449 _bfd_xcoff_swap_sym_out (bfd *abfd, void * inp, void * extp)
450 {
451 struct internal_syment *in = (struct internal_syment *)inp;
452 SYMENT *ext =(SYMENT *)extp;
453
454 if (in->_n._n_name[0] != 0)
455 {
456 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
457 }
458 else
459 {
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);
462 }
463
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);
470 }
471
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)
475 {
476 AUXENT * ext = (AUXENT *)ext1;
477 union internal_auxent *in = (union internal_auxent *)in1;
478
479 switch (in_class)
480 {
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;
488
489 case C_FILE:
490 if (ext->x_file.x_n.x_fname[0] == 0)
491 {
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);
495 }
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;
500
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)
509 {
510 in->x_csect.x_scnlen.u64 = 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);
520 }
521 else
522 {
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.u32
529 = H_GET_32 (abfd, ext->x_fcn.x_endndx);
530 }
531 break;
532
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;
543
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;
549
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;
554
555 }
556 }
557
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)
561 {
562 union internal_auxent *in = (union internal_auxent *)inp;
563 AUXENT *ext = (AUXENT *)extp;
564
565 memset (ext, 0, bfd_coff_auxesz (abfd));
566 switch (in_class)
567 {
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;
575
576 case C_FILE:
577 if (in->x_file.x_n.x_fname[0] == 0)
578 {
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);
582 }
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;
587
588 /* RS/6000 "csect" auxents */
589 case C_EXT:
590 case C_AIX_WEAKEXT:
591 case C_HIDEXT:
592 if (indx + 1 == numaux)
593 {
594 H_PUT_32 (abfd, in->x_csect.x_scnlen.u64, 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);
604 }
605 else
606 {
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.u32,
611 ext->x_fcn.x_endndx);
612 }
613 break;
614
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;
620
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;
625
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;
630 }
631
632 return bfd_coff_auxesz (abfd);
633 }
634 \f
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.
645
646 For now, it seems to be enought. */
647
648 reloc_howto_type xcoff_howto_table[] =
649 {
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 */
664
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 */
679
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 */
694
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 */
709
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 */
724
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 */
739
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 */
754
755 EMPTY_HOWTO (7),
756
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 */
771
772 EMPTY_HOWTO (9),
773
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 */
788
789 EMPTY_HOWTO (0xb),
790
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 */
805
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 */
820
821 EMPTY_HOWTO (0xe),
822
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 */
837
838 EMPTY_HOWTO (0x10),
839 EMPTY_HOWTO (0x11),
840 EMPTY_HOWTO (0x12),
841
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 */
856
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 */
871
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 */
886
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 */
901
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 */
916
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 */
931
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 */
946
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 */
961
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 */
976
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 */
991
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 */
1006
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 */
1021
1022 EMPTY_HOWTO (0x1f),
1023
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 */
1038
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 */
1053
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 */
1068
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 */
1083
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 */
1098
1099
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 */
1114
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),
1125
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 */
1140
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 */
1155
1156 };
1157
1158 void
1159 xcoff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1160 {
1161 if (internal->r_type > R_TOCL)
1162 abort ();
1163
1164 /* Default howto layout works most of the time */
1165 relent->howto = &xcoff_howto_table[internal->r_type];
1166
1167 /* Special case some 16 bit reloc */
1168 if (15 == (internal->r_size & 0x1f))
1169 {
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];
1176 }
1177
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 ();
1187 }
1188
1189 reloc_howto_type *
1190 _bfd_xcoff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1191 bfd_reloc_code_real_type code)
1192 {
1193 switch (code)
1194 {
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;
1230 }
1231 }
1232
1233 static reloc_howto_type *
1234 _bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1235 const char *r_name)
1236 {
1237 unsigned int i;
1238
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];
1245
1246 return NULL;
1247 }
1248 \f
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.
1252
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.
1258
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.
1270
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.
1279
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.
1285
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. */
1290
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. */
1296
1297 static long
1298 _bfd_strntol (const char * nptr, int base, unsigned int maxlen)
1299 {
1300 char buf[24]; /* Should be enough. */
1301
1302 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1303
1304 memcpy (buf, nptr, maxlen);
1305 buf[maxlen] = 0;
1306 return strtol (buf, NULL, base);
1307 }
1308
1309 static long long
1310 _bfd_strntoll (const char * nptr, int base, unsigned int maxlen)
1311 {
1312 char buf[32]; /* Should be enough. */
1313
1314 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1315
1316 memcpy (buf, nptr, maxlen);
1317 buf[maxlen] = 0;
1318 return strtoll (buf, NULL, base);
1319 }
1320
1321 /* Macro to read an ASCII value stored in an archive header field. */
1322 #define GET_VALUE_IN_FIELD(VAR, FIELD, BASE) \
1323 do \
1324 { \
1325 (VAR) = (sizeof (VAR) > sizeof (long) \
1326 ? _bfd_strntoll (FIELD, BASE, sizeof FIELD) \
1327 : _bfd_strntol (FIELD, BASE, sizeof FIELD)); \
1328 } \
1329 while (0)
1330
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))
1335
1336 /* Read in the armap of an XCOFF archive. */
1337
1338 bool
1339 _bfd_xcoff_slurp_armap (bfd *abfd)
1340 {
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;
1348
1349 if (x_artdata (abfd) == NULL)
1350 {
1351 abfd->has_armap = false;
1352 return true;
1353 }
1354
1355 if (! xcoff_big_format_p (abfd))
1356 {
1357 /* This is for the old format. */
1358 struct xcoff_ar_hdr hdr;
1359
1360 GET_VALUE_IN_FIELD (off, x_artdata (abfd)->u.hdr.symoff, 10);
1361 if (off == 0)
1362 {
1363 abfd->has_armap = false;
1364 return true;
1365 }
1366
1367 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1368 return false;
1369
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;
1374
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;
1380
1381 GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1382 if (sz + 1 < 5)
1383 {
1384 bfd_set_error (bfd_error_bad_value);
1385 return false;
1386 }
1387
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;
1392
1393 /* Ensure strings are NULL terminated so we don't wander off the
1394 end of the buffer. */
1395 contents[sz] = 0;
1396
1397 /* The symbol table starts with a four byte count. */
1398 c = H_GET_32 (abfd, contents);
1399
1400 if (c >= sz / 4)
1401 {
1402 bfd_set_error (bfd_error_bad_value);
1403 return false;
1404 }
1405
1406 bfd_ardata (abfd)->symdefs =
1407 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1408 if (bfd_ardata (abfd)->symdefs == NULL)
1409 return false;
1410
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);
1416 }
1417 else
1418 {
1419 /* This is for the new format. */
1420 struct xcoff_ar_hdr_big hdr;
1421
1422 GET_VALUE_IN_FIELD (off, x_artdata (abfd)->u.bhdr.symoff, 10);
1423 if (off == 0)
1424 {
1425 abfd->has_armap = false;
1426 return true;
1427 }
1428
1429 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1430 return false;
1431
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;
1436
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;
1442
1443 GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1444 if (sz + 1 < 9)
1445 {
1446 bfd_set_error (bfd_error_bad_value);
1447 return false;
1448 }
1449
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;
1454
1455 /* Ensure strings are NULL terminated so we don't wander off the
1456 end of the buffer. */
1457 contents[sz] = 0;
1458
1459 /* The symbol table starts with an eight byte count. */
1460 c = H_GET_64 (abfd, contents);
1461
1462 if (c >= sz / 8)
1463 {
1464 bfd_set_error (bfd_error_bad_value);
1465 return false;
1466 }
1467
1468 bfd_ardata (abfd)->symdefs =
1469 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1470 if (bfd_ardata (abfd)->symdefs == NULL)
1471 return false;
1472
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);
1478 }
1479
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)
1485 {
1486 if (p >= cend)
1487 {
1488 bfd_set_error (bfd_error_bad_value);
1489 return false;
1490 }
1491 arsym->name = (char *) p;
1492 }
1493
1494 bfd_ardata (abfd)->symdef_count = c;
1495 abfd->has_armap = true;
1496
1497 return true;
1498 }
1499
1500 /* See if this is an XCOFF archive. */
1501
1502 bfd_cleanup
1503 _bfd_xcoff_archive_p (bfd *abfd)
1504 {
1505 struct artdata *tdata_hold;
1506 char magic[SXCOFFARMAG];
1507 size_t amt = SXCOFFARMAG;
1508
1509 if (bfd_bread (magic, amt, abfd) != amt)
1510 {
1511 if (bfd_get_error () != bfd_error_system_call)
1512 bfd_set_error (bfd_error_wrong_format);
1513 return NULL;
1514 }
1515
1516 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1517 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1518 {
1519 bfd_set_error (bfd_error_wrong_format);
1520 return NULL;
1521 }
1522
1523 tdata_hold = bfd_ardata (abfd);
1524
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;
1529
1530 /* Now handle the two formats. */
1531 if (magic[1] != 'b')
1532 {
1533 /* This is the old format. */
1534 struct xcoff_ar_file_hdr hdr;
1535
1536 /* Copy over the magic string. */
1537 memcpy (hdr.magic, magic, SXCOFFARMAG);
1538
1539 /* Now read the rest of the file header. */
1540 amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
1541 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1542 {
1543 if (bfd_get_error () != bfd_error_system_call)
1544 bfd_set_error (bfd_error_wrong_format);
1545 goto error_ret;
1546 }
1547
1548 GET_VALUE_IN_FIELD (bfd_ardata (abfd)->first_file_filepos,
1549 hdr.firstmemoff, 10);
1550
1551 amt = sizeof (struct xcoff_artdata);
1552 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1553 if (bfd_ardata (abfd)->tdata == NULL)
1554 goto error_ret;
1555
1556 memcpy (&x_artdata (abfd)->u.hdr, &hdr, SIZEOF_AR_FILE_HDR);
1557 }
1558 else
1559 {
1560 /* This is the new format. */
1561 struct xcoff_ar_file_hdr_big hdr;
1562
1563 /* Copy over the magic string. */
1564 memcpy (hdr.magic, magic, SXCOFFARMAG);
1565
1566 /* Now read the rest of the file header. */
1567 amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
1568 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1569 {
1570 if (bfd_get_error () != bfd_error_system_call)
1571 bfd_set_error (bfd_error_wrong_format);
1572 goto error_ret;
1573 }
1574
1575 bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
1576 (const char **) 0,
1577 10);
1578
1579 amt = sizeof (struct xcoff_artdata);
1580 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1581 if (bfd_ardata (abfd)->tdata == NULL)
1582 goto error_ret;
1583
1584 memcpy (&x_artdata (abfd)->u.bhdr, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1585 }
1586
1587 if (! _bfd_xcoff_slurp_armap (abfd))
1588 {
1589 error_ret:
1590 bfd_release (abfd, bfd_ardata (abfd));
1591 error_ret_restore:
1592 bfd_ardata (abfd) = tdata_hold;
1593 return NULL;
1594 }
1595
1596 return _bfd_no_cleanup;
1597 }
1598
1599 /* Track file ranges occupied by elements. Add [START,END) to the
1600 list of ranges and return TRUE if there is no overlap between the
1601 new and any other element or the archive file header. Note that
1602 this would seem to preclude calling _bfd_get_elt_at_filepos twice
1603 for the same element, but we won't get to _bfd_xcoff_read_ar_hdr if
1604 an element is read more than once. See _bfd_get_elt_at_filepos use
1605 of _bfd_look_for_bfd_in_cache. Also, the xcoff archive code
1606 doesn't call _bfd_read_ar_hdr when reading the armap, nor does it
1607 need to use extended name tables. So those other routines in
1608 archive.c that call _bfd_read_ar_hdr are unused. */
1609
1610 static bool
1611 add_range (bfd *abfd, ufile_ptr start, ufile_ptr end)
1612 {
1613 if (end <= start)
1614 {
1615 err:
1616 bfd_set_error (bfd_error_malformed_archive);
1617 return false;
1618 }
1619
1620 /* This list is kept sorted by address. Find the highest address
1621 range on the list that ends before the new range starts. Exit
1622 the loop with that range in LO, and the mext higher range in HI. */
1623 struct ar_ranges *hi = &x_artdata (abfd)->ranges;
1624 struct ar_ranges *lo = NULL;
1625 while (hi && hi->end <= start)
1626 {
1627 lo = hi;
1628 hi = hi->next;
1629 }
1630
1631 if (lo == NULL)
1632 /* Start overlaps the file header or elements adjacent to it. */
1633 goto err;
1634
1635 if (hi && hi->start < end)
1636 /* Overlap with another element. */
1637 goto err;
1638
1639 unsigned min_elt = x_artdata (abfd)->min_elt;
1640 if (start - lo->end < min_elt)
1641 {
1642 /* Merge into an existing range. */
1643 lo->end = end;
1644 if (hi && hi->start - end < min_elt)
1645 {
1646 /* In fact, we can merge two ranges. */
1647 lo->end = hi->end;
1648 lo->next = hi->next;
1649 /* The list uses bfd_alloc so don't free HI. */
1650 }
1651 return true;
1652 }
1653
1654 if (hi && hi->start - end < min_elt)
1655 {
1656 /* Merge into an existing range. */
1657 hi->start = start;
1658 return true;
1659 }
1660
1661 struct ar_ranges *newr = bfd_alloc (abfd, sizeof (*newr));
1662 if (newr == NULL)
1663 return false;
1664 newr->start = start;
1665 newr->end = end;
1666 newr->next = hi;
1667 lo->next = newr;
1668 return true;
1669 }
1670
1671 /* Read the archive header in an XCOFF archive. */
1672
1673 void *
1674 _bfd_xcoff_read_ar_hdr (bfd *abfd)
1675 {
1676 bfd_size_type namlen;
1677 struct areltdata *ret;
1678 bfd_size_type amt;
1679 ufile_ptr start = abfd->where;
1680
1681 if (! xcoff_big_format_p (abfd))
1682 {
1683 struct xcoff_ar_hdr hdr;
1684 struct xcoff_ar_hdr *hdrp;
1685
1686 if (bfd_bread (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
1687 return NULL;
1688
1689 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1690 if (namlen > bfd_get_file_size (abfd))
1691 return NULL;
1692 amt = sizeof (struct areltdata) + SIZEOF_AR_HDR + namlen + 1;
1693 ret = (struct areltdata *) bfd_malloc (amt);
1694 if (ret == NULL)
1695 return ret;
1696
1697 hdrp = (struct xcoff_ar_hdr *) (ret + 1);
1698 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1699 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
1700 {
1701 free (ret);
1702 return NULL;
1703 }
1704 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1705
1706 ret->arch_header = (char *) hdrp;
1707 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1708 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1709 }
1710 else
1711 {
1712 struct xcoff_ar_hdr_big hdr;
1713 struct xcoff_ar_hdr_big *hdrp;
1714
1715 if (bfd_bread (&hdr, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG)
1716 return NULL;
1717
1718 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1719 if (namlen > bfd_get_file_size (abfd))
1720 return NULL;
1721 amt = sizeof (struct areltdata) + SIZEOF_AR_HDR_BIG + namlen + 1;
1722 ret = (struct areltdata *) bfd_malloc (amt);
1723 if (ret == NULL)
1724 return ret;
1725
1726 hdrp = (struct xcoff_ar_hdr_big *) (ret + 1);
1727 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1728 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
1729 {
1730 free (ret);
1731 return NULL;
1732 }
1733 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1734
1735 ret->arch_header = (char *) hdrp;
1736 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1737 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1738 }
1739
1740 /* Size occupied by the header above that covered in the fixed
1741 SIZEOF_AR_HDR or SIZEOF_AR_HDR_BIG. */
1742 ret->extra_size = namlen + (namlen & 1) + SXCOFFARFMAG;
1743
1744 /* Skip over the XCOFFARFMAG at the end of the file name. */
1745 if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0
1746 || !add_range (abfd, start, abfd->where + ret->parsed_size))
1747 {
1748 free (ret);
1749 return NULL;
1750 }
1751
1752 return ret;
1753 }
1754
1755 /* Open the next element in an XCOFF archive. */
1756
1757 bfd *
1758 _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
1759 {
1760 file_ptr filestart;
1761
1762 if (x_artdata (archive) == NULL)
1763 {
1764 bfd_set_error (bfd_error_invalid_operation);
1765 return NULL;
1766 }
1767
1768 if (! xcoff_big_format_p (archive))
1769 {
1770 if (last_file == NULL)
1771 {
1772 filestart = bfd_ardata (archive)->first_file_filepos;
1773 if (x_artdata (archive)->min_elt == 0)
1774 {
1775 x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR;
1776 x_artdata (archive)->min_elt = SIZEOF_AR_HDR + SXCOFFARFMAG + 2;
1777 }
1778 }
1779 else
1780 GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff, 10);
1781
1782 if (filestart == 0
1783 || EQ_VALUE_IN_FIELD (filestart,
1784 x_artdata (archive)->u.hdr.memoff, 10)
1785 || EQ_VALUE_IN_FIELD (filestart,
1786 x_artdata (archive)->u.hdr.symoff, 10))
1787 {
1788 bfd_set_error (bfd_error_no_more_archived_files);
1789 return NULL;
1790 }
1791 }
1792 else
1793 {
1794 if (last_file == NULL)
1795 {
1796 filestart = bfd_ardata (archive)->first_file_filepos;
1797 if (x_artdata (archive)->min_elt == 0)
1798 {
1799 x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR_BIG;
1800 x_artdata (archive)->min_elt = SIZEOF_AR_HDR_BIG + SXCOFFARFMAG + 2;
1801 }
1802 }
1803 else
1804 GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff, 10);
1805
1806 if (filestart == 0
1807 || EQ_VALUE_IN_FIELD (filestart,
1808 x_artdata (archive)->u.bhdr.memoff, 10)
1809 || EQ_VALUE_IN_FIELD (filestart,
1810 x_artdata (archive)->u.bhdr.symoff, 10))
1811 {
1812 bfd_set_error (bfd_error_no_more_archived_files);
1813 return NULL;
1814 }
1815 }
1816
1817 return _bfd_get_elt_at_filepos (archive, filestart, NULL);
1818 }
1819
1820 /* Stat an element in an XCOFF archive. */
1821
1822 int
1823 _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
1824 {
1825 if (abfd->arelt_data == NULL)
1826 {
1827 bfd_set_error (bfd_error_invalid_operation);
1828 return -1;
1829 }
1830
1831 if (! xcoff_big_format_p (abfd->my_archive))
1832 {
1833 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1834
1835 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1836 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1837 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1838 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1839 s->st_size = arch_eltdata (abfd)->parsed_size;
1840 }
1841 else
1842 {
1843 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1844
1845 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1846 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1847 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1848 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1849 s->st_size = arch_eltdata (abfd)->parsed_size;
1850 }
1851
1852 return 0;
1853 }
1854
1855 /* Normalize a file name for inclusion in an archive. */
1856
1857 static const char *
1858 normalize_filename (bfd *abfd)
1859 {
1860 const char *file;
1861 const char *filename;
1862
1863 file = bfd_get_filename (abfd);
1864 filename = strrchr (file, '/');
1865 if (filename != NULL)
1866 filename++;
1867 else
1868 filename = file;
1869 return filename;
1870 }
1871
1872 /* Write out an XCOFF armap. */
1873
1874 static bool
1875 xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1876 struct orl *map, unsigned int orl_count, int stridx)
1877 {
1878 struct archive_iterator iterator;
1879 struct xcoff_ar_hdr hdr;
1880 char *p;
1881 unsigned char buf[4];
1882 unsigned int i;
1883
1884 memset (&hdr, 0, sizeof hdr);
1885 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1886 sprintf (hdr.nextoff, "%d", 0);
1887 memcpy (hdr.prevoff, x_artdata (abfd)->u.hdr.memoff,
1888 XCOFFARMAG_ELEMENT_SIZE);
1889 sprintf (hdr.date, "%d", 0);
1890 sprintf (hdr.uid, "%d", 0);
1891 sprintf (hdr.gid, "%d", 0);
1892 sprintf (hdr.mode, "%d", 0);
1893 sprintf (hdr.namlen, "%d", 0);
1894
1895 /* We need spaces, not null bytes, in the header. */
1896 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1897 if (*p == '\0')
1898 *p = ' ';
1899
1900 if (bfd_bwrite (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1901 != SIZEOF_AR_HDR
1902 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
1903 != SXCOFFARFMAG))
1904 return false;
1905
1906 H_PUT_32 (abfd, orl_count, buf);
1907 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1908 return false;
1909
1910 i = 0;
1911 archive_iterator_begin (&iterator, abfd);
1912 while (i < orl_count && archive_iterator_next (&iterator))
1913 while (map[i].u.abfd == iterator.current.member)
1914 {
1915 H_PUT_32 (abfd, iterator.current.offset, buf);
1916 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1917 return false;
1918 ++i;
1919 }
1920
1921 for (i = 0; i < orl_count; i++)
1922 {
1923 const char *name;
1924 size_t namlen;
1925
1926 name = *map[i].name;
1927 namlen = strlen (name);
1928 if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
1929 return false;
1930 }
1931
1932 if ((stridx & 1) != 0)
1933 {
1934 char b;
1935
1936 b = '\0';
1937 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1938 return false;
1939 }
1940
1941 return true;
1942 }
1943
1944 static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
1945 #define FMT20 "%-20" PRId64
1946 #define FMT12 "%-12d"
1947 #define FMT12_OCTAL "%-12o"
1948 #define FMT4 "%-4d"
1949 #define PRINT20(d, v) \
1950 sprintf (buff20, FMT20, (uint64_t) (v)), \
1951 memcpy ((void *) (d), buff20, 20)
1952
1953 #define PRINT12(d, v) \
1954 sprintf (buff20, FMT12, (int)(v)), \
1955 memcpy ((void *) (d), buff20, 12)
1956
1957 #define PRINT12_OCTAL(d, v) \
1958 sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
1959 memcpy ((void *) (d), buff20, 12)
1960
1961 #define PRINT4(d, v) \
1962 sprintf (buff20, FMT4, (int)(v)), \
1963 memcpy ((void *) (d), buff20, 4)
1964
1965 #define READ20(d, v) \
1966 buff20[20] = 0, \
1967 memcpy (buff20, (d), 20), \
1968 (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
1969
1970 static bool
1971 do_pad (bfd *abfd, unsigned int number)
1972 {
1973 bfd_byte b = 0;
1974
1975 /* Limit pad to <= 4096. */
1976 if (number > 4096)
1977 return false;
1978
1979 while (number--)
1980 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1981 return false;
1982
1983 return true;
1984 }
1985
1986 static bool
1987 do_copy (bfd *out_bfd, bfd *in_bfd)
1988 {
1989 bfd_size_type remaining;
1990 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1991
1992 if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
1993 return false;
1994
1995 remaining = arelt_size (in_bfd);
1996
1997 while (remaining >= DEFAULT_BUFFERSIZE)
1998 {
1999 if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
2000 || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
2001 return false;
2002
2003 remaining -= DEFAULT_BUFFERSIZE;
2004 }
2005
2006 if (remaining)
2007 {
2008 if (bfd_bread (buffer, remaining, in_bfd) != remaining
2009 || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
2010 return false;
2011 }
2012
2013 return true;
2014 }
2015
2016 static bool
2017 xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2018 struct orl *map, unsigned int orl_count, int stridx)
2019 {
2020 struct archive_iterator iterator;
2021 struct xcoff_ar_file_hdr_big *fhdr;
2022 bfd_vma i, sym_32, sym_64, str_32, str_64;
2023 const bfd_arch_info_type *arch_info;
2024 bfd *current_bfd;
2025 size_t string_length;
2026 file_ptr nextoff, prevoff;
2027
2028 /* First, we look through the symbols and work out which are
2029 from 32-bit objects and which from 64-bit ones. */
2030 sym_32 = sym_64 = str_32 = str_64 = 0;
2031
2032 i = 0;
2033 for (current_bfd = abfd->archive_head;
2034 current_bfd != NULL && i < orl_count;
2035 current_bfd = current_bfd->archive_next)
2036 {
2037 arch_info = bfd_get_arch_info (current_bfd);
2038 while (map[i].u.abfd == current_bfd)
2039 {
2040 string_length = strlen (*map[i].name) + 1;
2041 if (arch_info->bits_per_address == 64)
2042 {
2043 sym_64++;
2044 str_64 += string_length;
2045 }
2046 else
2047 {
2048 sym_32++;
2049 str_32 += string_length;
2050 }
2051 i++;
2052 }
2053 }
2054
2055 /* A quick sanity check... */
2056 BFD_ASSERT (sym_64 + sym_32 == orl_count);
2057 /* Explicit cast to int for compiler. */
2058 BFD_ASSERT ((int)(str_64 + str_32) == stridx);
2059
2060 fhdr = &x_artdata (abfd)->u.bhdr;
2061
2062 /* xcoff_write_archive_contents_big passes nextoff in symoff. */
2063 READ20 (fhdr->memoff, prevoff);
2064 READ20 (fhdr->symoff, nextoff);
2065
2066 BFD_ASSERT (nextoff == bfd_tell (abfd));
2067
2068 /* Write out the symbol table.
2069 Layout :
2070
2071 standard big archive header
2072 0x0000 ar_size [0x14]
2073 0x0014 ar_nxtmem [0x14]
2074 0x0028 ar_prvmem [0x14]
2075 0x003C ar_date [0x0C]
2076 0x0048 ar_uid [0x0C]
2077 0x0054 ar_gid [0x0C]
2078 0x0060 ar_mod [0x0C]
2079 0x006C ar_namelen[0x04]
2080 0x0070 ar_fmag [SXCOFFARFMAG]
2081
2082 Symbol table
2083 0x0072 num_syms [0x08], binary
2084 0x0078 offsets [0x08 * num_syms], binary
2085 0x0086 + 0x08 * num_syms names [??]
2086 ?? pad to even bytes.
2087 */
2088
2089 if (sym_32)
2090 {
2091 struct xcoff_ar_hdr_big *hdr;
2092 char *symbol_table;
2093 char *st;
2094
2095 bfd_vma symbol_table_size =
2096 SIZEOF_AR_HDR_BIG
2097 + SXCOFFARFMAG
2098 + 8
2099 + 8 * sym_32
2100 + str_32 + (str_32 & 1);
2101
2102 symbol_table = bfd_zmalloc (symbol_table_size);
2103 if (symbol_table == NULL)
2104 return false;
2105
2106 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2107
2108 PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
2109
2110 if (sym_64)
2111 PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
2112 else
2113 PRINT20 (hdr->nextoff, 0);
2114
2115 PRINT20 (hdr->prevoff, prevoff);
2116 PRINT12 (hdr->date, 0);
2117 PRINT12 (hdr->uid, 0);
2118 PRINT12 (hdr->gid, 0);
2119 PRINT12 (hdr->mode, 0);
2120 PRINT4 (hdr->namlen, 0) ;
2121
2122 st = symbol_table + SIZEOF_AR_HDR_BIG;
2123 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2124 st += SXCOFFARFMAG;
2125
2126 bfd_h_put_64 (abfd, sym_32, st);
2127 st += 8;
2128
2129 /* loop over the 32 bit offsets */
2130 i = 0;
2131 archive_iterator_begin (&iterator, abfd);
2132 while (i < orl_count && archive_iterator_next (&iterator))
2133 {
2134 arch_info = bfd_get_arch_info (iterator.current.member);
2135 while (map[i].u.abfd == iterator.current.member)
2136 {
2137 if (arch_info->bits_per_address == 32)
2138 {
2139 bfd_h_put_64 (abfd, iterator.current.offset, st);
2140 st += 8;
2141 }
2142 i++;
2143 }
2144 }
2145
2146 /* loop over the 32 bit symbol names */
2147 i = 0;
2148 for (current_bfd = abfd->archive_head;
2149 current_bfd != NULL && i < orl_count;
2150 current_bfd = current_bfd->archive_next)
2151 {
2152 arch_info = bfd_get_arch_info (current_bfd);
2153 while (map[i].u.abfd == current_bfd)
2154 {
2155 if (arch_info->bits_per_address == 32)
2156 {
2157 string_length = sprintf (st, "%s", *map[i].name);
2158 st += string_length + 1;
2159 }
2160 i++;
2161 }
2162 }
2163
2164 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2165
2166 free (symbol_table);
2167
2168 prevoff = nextoff;
2169 nextoff = nextoff + symbol_table_size;
2170 }
2171 else
2172 PRINT20 (fhdr->symoff, 0);
2173
2174 if (sym_64)
2175 {
2176 struct xcoff_ar_hdr_big *hdr;
2177 char *symbol_table;
2178 char *st;
2179
2180 bfd_vma symbol_table_size =
2181 SIZEOF_AR_HDR_BIG
2182 + SXCOFFARFMAG
2183 + 8
2184 + 8 * sym_64
2185 + str_64 + (str_64 & 1);
2186
2187 symbol_table = bfd_zmalloc (symbol_table_size);
2188 if (symbol_table == NULL)
2189 return false;
2190
2191 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2192
2193 PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
2194 PRINT20 (hdr->nextoff, 0);
2195 PRINT20 (hdr->prevoff, prevoff);
2196 PRINT12 (hdr->date, 0);
2197 PRINT12 (hdr->uid, 0);
2198 PRINT12 (hdr->gid, 0);
2199 PRINT12 (hdr->mode, 0);
2200 PRINT4 (hdr->namlen, 0);
2201
2202 st = symbol_table + SIZEOF_AR_HDR_BIG;
2203 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2204 st += SXCOFFARFMAG;
2205
2206 bfd_h_put_64 (abfd, sym_64, st);
2207 st += 8;
2208
2209 /* loop over the 64 bit offsets */
2210 i = 0;
2211 archive_iterator_begin (&iterator, abfd);
2212 while (i < orl_count && archive_iterator_next (&iterator))
2213 {
2214 arch_info = bfd_get_arch_info (iterator.current.member);
2215 while (map[i].u.abfd == iterator.current.member)
2216 {
2217 if (arch_info->bits_per_address == 64)
2218 {
2219 bfd_h_put_64 (abfd, iterator.current.offset, st);
2220 st += 8;
2221 }
2222 i++;
2223 }
2224 }
2225
2226 /* loop over the 64 bit symbol names */
2227 i = 0;
2228 for (current_bfd = abfd->archive_head;
2229 current_bfd != NULL && i < orl_count;
2230 current_bfd = current_bfd->archive_next)
2231 {
2232 arch_info = bfd_get_arch_info (current_bfd);
2233 while (map[i].u.abfd == current_bfd)
2234 {
2235 if (arch_info->bits_per_address == 64)
2236 {
2237 string_length = sprintf (st, "%s", *map[i].name);
2238 st += string_length + 1;
2239 }
2240 i++;
2241 }
2242 }
2243
2244 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2245
2246 free (symbol_table);
2247
2248 PRINT20 (fhdr->symoff64, nextoff);
2249 }
2250 else
2251 PRINT20 (fhdr->symoff64, 0);
2252
2253 return true;
2254 }
2255
2256 bool
2257 _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2258 struct orl *map, unsigned int orl_count, int stridx)
2259 {
2260 if (! xcoff_big_format_p (abfd))
2261 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
2262 else
2263 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
2264 }
2265
2266 /* Write out an XCOFF archive. We always write an entire archive,
2267 rather than fussing with the freelist and so forth. */
2268
2269 static bool
2270 xcoff_write_archive_contents_old (bfd *abfd)
2271 {
2272 struct archive_iterator iterator;
2273 struct xcoff_artdata xtdata;
2274 struct xcoff_ar_file_hdr *fhdr = &xtdata.u.hdr;
2275 bfd_size_type count;
2276 bfd_size_type total_namlen;
2277 file_ptr *offsets;
2278 bool makemap;
2279 bool hasobjects;
2280 file_ptr prevoff, nextoff;
2281 bfd *sub;
2282 size_t i;
2283 struct xcoff_ar_hdr ahdr;
2284 bfd_size_type size;
2285 char *p;
2286 char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
2287
2288 memset (&xtdata, 0, sizeof (xtdata));
2289 memcpy (fhdr->magic, XCOFFARMAG, SXCOFFARMAG);
2290 sprintf (fhdr->firstmemoff, "%zu", SIZEOF_AR_FILE_HDR);
2291 sprintf (fhdr->freeoff, "%d", 0);
2292
2293 count = 0;
2294 total_namlen = 0;
2295 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2296 {
2297 ++count;
2298 total_namlen += strlen (normalize_filename (sub)) + 1;
2299 if (sub->arelt_data == NULL)
2300 {
2301 sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
2302 if (sub->arelt_data == NULL)
2303 return false;
2304 }
2305 if (arch_xhdr (sub) == NULL)
2306 {
2307 struct xcoff_ar_hdr *ahdrp;
2308 struct stat s;
2309
2310 if ((sub->flags & BFD_IN_MEMORY) != 0)
2311 {
2312 /* Assume we just "made" the member, and fake it. */
2313 struct bfd_in_memory *bim
2314 = (struct bfd_in_memory *) sub->iostream;
2315 time (&s.st_mtime);
2316 s.st_uid = getuid ();
2317 s.st_gid = getgid ();
2318 s.st_mode = 0644;
2319 s.st_size = bim->size;
2320 }
2321 else if (stat (bfd_get_filename (sub), &s) != 0)
2322 {
2323 bfd_set_input_error (sub, bfd_error_system_call);
2324 return false;
2325 }
2326
2327 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2328 {
2329 s.st_mtime = 0;
2330 s.st_uid = 0;
2331 s.st_gid = 0;
2332 s.st_mode = 0644;
2333 }
2334
2335 ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
2336 if (ahdrp == NULL)
2337 return false;
2338
2339 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2340 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2341 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2342 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2343 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2344
2345 arch_eltdata (sub)->arch_header = (char *) ahdrp;
2346 arch_eltdata (sub)->parsed_size = s.st_size;
2347 }
2348 }
2349 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2350 if (offsets == NULL)
2351 return false;
2352
2353 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
2354 return false;
2355
2356 makemap = bfd_has_map (abfd);
2357 hasobjects = false;
2358 prevoff = 0;
2359 for (archive_iterator_begin (&iterator, abfd), i = 0;
2360 archive_iterator_next (&iterator);
2361 i++)
2362 {
2363 bfd_size_type namlen;
2364 struct xcoff_ar_hdr *ahdrp;
2365
2366 if (makemap && ! hasobjects)
2367 {
2368 if (bfd_check_format (iterator.current.member, bfd_object))
2369 hasobjects = true;
2370 }
2371
2372 ahdrp = arch_xhdr (iterator.current.member);
2373 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2374 sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
2375 sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
2376
2377 /* We need spaces, not null bytes, in the header. */
2378 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
2379 if (*p == '\0')
2380 *p = ' ';
2381
2382 if (!do_pad (abfd, iterator.current.leading_padding))
2383 return false;
2384
2385 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2386 namlen = iterator.current.padded_namlen;
2387 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2388 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2389 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2390 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2391 || !do_copy (abfd, iterator.current.member)
2392 || !do_pad (abfd, iterator.current.trailing_padding))
2393 return false;
2394
2395 offsets[i] = iterator.current.offset;
2396 prevoff = iterator.current.offset;
2397 }
2398
2399 sprintf (fhdr->lastmemoff, "%ld", (long) prevoff);
2400
2401 /* Write out the member table. */
2402
2403 nextoff = iterator.next.offset;
2404 BFD_ASSERT (nextoff == bfd_tell (abfd));
2405 sprintf (fhdr->memoff, "%ld", (long) nextoff);
2406
2407 memset (&ahdr, 0, sizeof ahdr);
2408 sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
2409 + count * XCOFFARMAG_ELEMENT_SIZE
2410 + total_namlen));
2411 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2412 sprintf (ahdr.date, "%d", 0);
2413 sprintf (ahdr.uid, "%d", 0);
2414 sprintf (ahdr.gid, "%d", 0);
2415 sprintf (ahdr.mode, "%d", 0);
2416 sprintf (ahdr.namlen, "%d", 0);
2417
2418 size = (SIZEOF_AR_HDR
2419 + XCOFFARMAG_ELEMENT_SIZE
2420 + count * XCOFFARMAG_ELEMENT_SIZE
2421 + total_namlen
2422 + SXCOFFARFMAG);
2423
2424 prevoff = nextoff;
2425 nextoff += size + (size & 1);
2426
2427 if (makemap && hasobjects)
2428 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2429 else
2430 sprintf (ahdr.nextoff, "%d", 0);
2431
2432 /* We need spaces, not null bytes, in the header. */
2433 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2434 if (*p == '\0')
2435 *p = ' ';
2436
2437 if ((bfd_bwrite (&ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
2438 != SIZEOF_AR_HDR)
2439 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
2440 != SXCOFFARFMAG))
2441 return false;
2442
2443 sprintf (decbuf, "%-12ld", (long) count);
2444 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
2445 != XCOFFARMAG_ELEMENT_SIZE)
2446 return false;
2447 for (i = 0; i < (size_t) count; i++)
2448 {
2449 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2450 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
2451 abfd) != XCOFFARMAG_ELEMENT_SIZE)
2452 return false;
2453 }
2454 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2455 {
2456 const char *name;
2457 bfd_size_type namlen;
2458
2459 name = normalize_filename (sub);
2460 namlen = strlen (name);
2461 if (bfd_bwrite (name, namlen + 1, abfd) != namlen + 1)
2462 return false;
2463 }
2464
2465 if (! do_pad (abfd, size & 1))
2466 return false;
2467
2468 /* Write out the armap, if appropriate. */
2469 if (! makemap || ! hasobjects)
2470 sprintf (fhdr->symoff, "%d", 0);
2471 else
2472 {
2473 BFD_ASSERT (nextoff == bfd_tell (abfd));
2474 sprintf (fhdr->symoff, "%ld", (long) nextoff);
2475 bfd_ardata (abfd)->tdata = &xtdata;
2476 bool ret = _bfd_compute_and_write_armap (abfd, 0);
2477 bfd_ardata (abfd)->tdata = NULL;
2478 if (!ret)
2479 return false;
2480 }
2481
2482 /* Write out the archive file header. */
2483
2484 /* We need spaces, not null bytes, in the header. */
2485 for (p = (char *) fhdr; p < (char *) fhdr + SIZEOF_AR_FILE_HDR; p++)
2486 if (*p == '\0')
2487 *p = ' ';
2488
2489 if (bfd_seek (abfd, 0, SEEK_SET) != 0
2490 || (bfd_bwrite (fhdr, SIZEOF_AR_FILE_HDR, abfd) != SIZEOF_AR_FILE_HDR))
2491 return false;
2492
2493 return true;
2494 }
2495
2496 static bool
2497 xcoff_write_archive_contents_big (bfd *abfd)
2498 {
2499 struct xcoff_artdata xtdata;
2500 struct xcoff_ar_file_hdr_big *fhdr = &xtdata.u.bhdr;
2501 bfd_size_type count;
2502 bfd_size_type total_namlen;
2503 file_ptr *offsets;
2504 bool makemap;
2505 bool hasobjects;
2506 file_ptr prevoff, nextoff;
2507 bfd *current_bfd;
2508 size_t i;
2509 struct xcoff_ar_hdr_big *hdr;
2510 bfd_size_type size;
2511 char *member_table, *mt;
2512 bfd_vma member_table_size;
2513 struct archive_iterator iterator;
2514
2515 memset (&xtdata, 0, sizeof (xtdata));
2516 memcpy (fhdr->magic, XCOFFARMAGBIG, SXCOFFARMAG);
2517
2518 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2519 return false;
2520
2521 /* Calculate count and total_namlen. */
2522 makemap = bfd_has_map (abfd);
2523 hasobjects = false;
2524 for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
2525 current_bfd != NULL;
2526 current_bfd = current_bfd->archive_next, count++)
2527 {
2528 total_namlen += strlen (normalize_filename (current_bfd)) + 1;
2529
2530 if (makemap
2531 && ! hasobjects
2532 && bfd_check_format (current_bfd, bfd_object))
2533 hasobjects = true;
2534
2535 if (current_bfd->arelt_data == NULL)
2536 {
2537 size = sizeof (struct areltdata);
2538 current_bfd->arelt_data = bfd_zmalloc (size);
2539 if (current_bfd->arelt_data == NULL)
2540 return false;
2541 }
2542
2543 if (arch_xhdr_big (current_bfd) == NULL)
2544 {
2545 struct xcoff_ar_hdr_big *ahdrp;
2546 struct stat s;
2547
2548 if ((current_bfd->flags & BFD_IN_MEMORY) != 0)
2549 {
2550 /* Assume we just "made" the member, and fake it. */
2551 struct bfd_in_memory *bim
2552 = (struct bfd_in_memory *) current_bfd->iostream;
2553 time (&s.st_mtime);
2554 s.st_uid = getuid ();
2555 s.st_gid = getgid ();
2556 s.st_mode = 0644;
2557 s.st_size = bim->size;
2558 }
2559 else if (stat (bfd_get_filename (current_bfd), &s) != 0)
2560 {
2561 bfd_set_input_error (current_bfd, bfd_error_system_call);
2562 return false;
2563 }
2564
2565 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2566 {
2567 s.st_mtime = 0;
2568 s.st_uid = 0;
2569 s.st_gid = 0;
2570 s.st_mode = 0644;
2571 }
2572
2573 ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
2574 if (ahdrp == NULL)
2575 return false;
2576
2577 PRINT20 (ahdrp->size, s.st_size);
2578 PRINT12 (ahdrp->date, s.st_mtime);
2579 PRINT12 (ahdrp->uid, s.st_uid);
2580 PRINT12 (ahdrp->gid, s.st_gid);
2581 PRINT12_OCTAL (ahdrp->mode, s.st_mode);
2582
2583 arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
2584 arch_eltdata (current_bfd)->parsed_size = s.st_size;
2585 }
2586 }
2587
2588 offsets = NULL;
2589 if (count)
2590 {
2591 offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
2592 if (offsets == NULL)
2593 return false;
2594 }
2595
2596 prevoff = 0;
2597 for (archive_iterator_begin (&iterator, abfd), i = 0;
2598 archive_iterator_next (&iterator);
2599 i++)
2600 {
2601 bfd_size_type namlen;
2602 struct xcoff_ar_hdr_big *ahdrp;
2603
2604 ahdrp = arch_xhdr_big (iterator.current.member);
2605 PRINT20 (ahdrp->prevoff, prevoff);
2606 PRINT4 (ahdrp->namlen, iterator.current.namlen);
2607 PRINT20 (ahdrp->nextoff, iterator.next.offset);
2608
2609 if (!do_pad (abfd, iterator.current.leading_padding))
2610 {
2611 free (offsets);
2612 return false;
2613 }
2614
2615 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2616 namlen = iterator.current.padded_namlen;
2617 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2618 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2619 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2620 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2621 || !do_copy (abfd, iterator.current.member)
2622 || !do_pad (abfd, iterator.current.trailing_padding))
2623 {
2624 free (offsets);
2625 return false;
2626 }
2627
2628 offsets[i] = iterator.current.offset;
2629 prevoff = iterator.current.offset;
2630 }
2631
2632 if (count)
2633 {
2634 PRINT20 (fhdr->firstmemoff, offsets[0]);
2635 PRINT20 (fhdr->lastmemoff, prevoff);
2636 }
2637
2638 /* Write out the member table.
2639 Layout :
2640
2641 standard big archive header
2642 0x0000 ar_size [0x14]
2643 0x0014 ar_nxtmem [0x14]
2644 0x0028 ar_prvmem [0x14]
2645 0x003C ar_date [0x0C]
2646 0x0048 ar_uid [0x0C]
2647 0x0054 ar_gid [0x0C]
2648 0x0060 ar_mod [0x0C]
2649 0x006C ar_namelen[0x04]
2650 0x0070 ar_fmag [0x02]
2651
2652 Member table
2653 0x0072 count [0x14]
2654 0x0086 offsets [0x14 * counts]
2655 0x0086 + 0x14 * counts names [??]
2656 ?? pad to even bytes.
2657 */
2658
2659 nextoff = iterator.next.offset;
2660 BFD_ASSERT (nextoff == bfd_tell (abfd));
2661
2662 member_table_size = (SIZEOF_AR_HDR_BIG
2663 + SXCOFFARFMAG
2664 + XCOFFARMAGBIG_ELEMENT_SIZE
2665 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2666 + total_namlen);
2667
2668 member_table_size += member_table_size & 1;
2669 member_table = bfd_zmalloc (member_table_size);
2670 if (member_table == NULL)
2671 {
2672 free (offsets);
2673 return false;
2674 }
2675
2676 hdr = (struct xcoff_ar_hdr_big *) member_table;
2677
2678 PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
2679 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2680 + total_namlen + (total_namlen & 1)));
2681 if (makemap && hasobjects)
2682 PRINT20 (hdr->nextoff, nextoff + member_table_size);
2683 else
2684 PRINT20 (hdr->nextoff, 0);
2685 PRINT20 (hdr->prevoff, prevoff);
2686 PRINT12 (hdr->date, 0);
2687 PRINT12 (hdr->uid, 0);
2688 PRINT12 (hdr->gid, 0);
2689 PRINT12 (hdr->mode, 0);
2690 PRINT4 (hdr->namlen, 0);
2691
2692 mt = member_table + SIZEOF_AR_HDR_BIG;
2693 memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
2694 mt += SXCOFFARFMAG;
2695
2696 PRINT20 (mt, count);
2697 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2698 for (i = 0; i < (size_t) count; i++)
2699 {
2700 PRINT20 (mt, offsets[i]);
2701 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2702 }
2703
2704 if (count)
2705 {
2706 free (offsets);
2707 offsets = NULL;
2708 }
2709
2710 for (current_bfd = abfd->archive_head;
2711 current_bfd != NULL;
2712 current_bfd = current_bfd->archive_next)
2713 {
2714 const char *name;
2715 size_t namlen;
2716
2717 name = normalize_filename (current_bfd);
2718 namlen = sprintf (mt, "%s", name);
2719 mt += namlen + 1;
2720 }
2721
2722 if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
2723 return false;
2724
2725 free (member_table);
2726
2727 PRINT20 (fhdr->memoff, nextoff);
2728
2729 prevoff = nextoff;
2730 nextoff += member_table_size;
2731
2732 /* Write out the armap, if appropriate. */
2733
2734 if (! makemap || ! hasobjects)
2735 PRINT20 (fhdr->symoff, 0);
2736 else
2737 {
2738 BFD_ASSERT (nextoff == bfd_tell (abfd));
2739
2740 /* Save nextoff in fhdr->symoff so the armap routine can use it. */
2741 PRINT20 (fhdr->symoff, nextoff);
2742
2743 bfd_ardata (abfd)->tdata = &xtdata;
2744 bool ret = _bfd_compute_and_write_armap (abfd, 0);
2745 bfd_ardata (abfd)->tdata = NULL;
2746 if (!ret)
2747 return false;
2748 }
2749
2750 /* Write out the archive file header. */
2751
2752 if (bfd_seek (abfd, 0, SEEK_SET) != 0
2753 || bfd_bwrite (fhdr,
2754 SIZEOF_AR_FILE_HDR_BIG, abfd) != SIZEOF_AR_FILE_HDR_BIG)
2755 return false;
2756
2757 return true;
2758 }
2759
2760 bool
2761 _bfd_xcoff_write_archive_contents (bfd *abfd)
2762 {
2763 if (! xcoff_big_format_p (abfd))
2764 return xcoff_write_archive_contents_old (abfd);
2765 else
2766 return xcoff_write_archive_contents_big (abfd);
2767 }
2768 \f
2769 /* We can't use the usual coff_sizeof_headers routine, because AIX
2770 always uses an a.out header. */
2771
2772 int
2773 _bfd_xcoff_sizeof_headers (bfd *abfd,
2774 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2775 {
2776 int size;
2777
2778 size = FILHSZ;
2779 if (xcoff_data (abfd)->full_aouthdr)
2780 size += AOUTSZ;
2781 else
2782 size += SMALL_AOUTSZ;
2783 size += abfd->section_count * SCNHSZ;
2784
2785 if (info->strip != strip_all)
2786 {
2787 /* There can be additional sections just for dealing with overflow in
2788 reloc and lineno counts. But the numbers of relocs and lineno aren't
2789 known when bfd_sizeof_headers is called, so we compute them by
2790 summing the numbers from input sections. */
2791 struct nbr_reloc_lineno
2792 {
2793 unsigned int reloc_count;
2794 unsigned int lineno_count;
2795 };
2796 struct nbr_reloc_lineno *n_rl;
2797 bfd *sub;
2798 unsigned int max_index;
2799 asection *s;
2800
2801 /* Although the number of sections is known, the maximum value of
2802 section->index isn't (because some sections may have been removed).
2803 Don't try to renumber sections, just compute the upper bound. */
2804 max_index = 0;
2805 for (s = abfd->sections; s != NULL; s = s->next)
2806 if (s->index > max_index)
2807 max_index = s->index;
2808
2809 /* Allocate the per section counters. It could be possible to use a
2810 preallocated array as the number of sections is limited on XCOFF,
2811 but this creates a maintainance issue. */
2812 n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
2813 if (n_rl == NULL)
2814 return -1;
2815
2816 /* Sum. */
2817 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2818 for (s = sub->sections; s != NULL; s = s->next)
2819 if (s->output_section->owner == abfd
2820 && !bfd_section_removed_from_list (abfd, s->output_section))
2821 {
2822 struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
2823 e->reloc_count += s->reloc_count;
2824 e->lineno_count += s->lineno_count;
2825 }
2826
2827 /* Add the size of a section for each section with an overflow. */
2828 for (s = abfd->sections; s != NULL; s = s->next)
2829 {
2830 struct nbr_reloc_lineno *e = &n_rl[s->index];
2831
2832 if (e->reloc_count >= 0xffff
2833 || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
2834 size += SCNHSZ;
2835 }
2836
2837 free (n_rl);
2838 }
2839
2840 return size;
2841 }
2842 \f
2843 /* Routines to swap information in the XCOFF .loader section. If we
2844 ever need to write an XCOFF loader, this stuff will need to be
2845 moved to another file shared by the linker (which XCOFF calls the
2846 ``binder'') and the loader. */
2847
2848 /* Swap in the ldhdr structure. */
2849
2850 static void
2851 xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
2852 {
2853 const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2854
2855 dst->l_version = bfd_get_32 (abfd, src->l_version);
2856 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2857 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2858 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2859 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2860 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2861 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2862 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2863 }
2864
2865 /* Swap out the ldhdr structure. */
2866
2867 static void
2868 xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
2869 {
2870 struct external_ldhdr *dst = (struct external_ldhdr *) d;
2871
2872 bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
2873 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2874 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2875 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2876 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2877 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2878 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2879 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2880 }
2881
2882 /* Swap in the ldsym structure. */
2883
2884 static void
2885 xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
2886 {
2887 const struct external_ldsym *src = (const struct external_ldsym *) s;
2888
2889 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2890 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2891 } else {
2892 dst->_l._l_l._l_zeroes = 0;
2893 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2894 }
2895 dst->l_value = bfd_get_32 (abfd, src->l_value);
2896 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2897 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2898 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2899 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2900 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2901 }
2902
2903 /* Swap out the ldsym structure. */
2904
2905 static void
2906 xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
2907 {
2908 struct external_ldsym *dst = (struct external_ldsym *) d;
2909
2910 if (src->_l._l_l._l_zeroes != 0)
2911 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2912 else
2913 {
2914 bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2915 bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2916 dst->_l._l_l._l_offset);
2917 }
2918 bfd_put_32 (abfd, src->l_value, dst->l_value);
2919 bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
2920 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2921 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2922 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2923 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2924 }
2925
2926 static void
2927 xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
2928 {
2929 struct external_reloc *src = (struct external_reloc *) s;
2930 struct internal_reloc *dst = (struct internal_reloc *) d;
2931
2932 memset (dst, 0, sizeof (struct internal_reloc));
2933
2934 dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2935 dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2936 dst->r_size = bfd_get_8 (abfd, src->r_size);
2937 dst->r_type = bfd_get_8 (abfd, src->r_type);
2938 }
2939
2940 static unsigned int
2941 xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
2942 {
2943 struct internal_reloc *src = (struct internal_reloc *) s;
2944 struct external_reloc *dst = (struct external_reloc *) d;
2945
2946 bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2947 bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2948 bfd_put_8 (abfd, src->r_type, dst->r_type);
2949 bfd_put_8 (abfd, src->r_size, dst->r_size);
2950
2951 return bfd_coff_relsz (abfd);
2952 }
2953
2954 /* Swap in the ldrel structure. */
2955
2956 static void
2957 xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
2958 {
2959 const struct external_ldrel *src = (const struct external_ldrel *) s;
2960
2961 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2962 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2963 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2964 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2965 }
2966
2967 /* Swap out the ldrel structure. */
2968
2969 static void
2970 xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
2971 {
2972 struct external_ldrel *dst = (struct external_ldrel *) d;
2973
2974 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2975 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
2976 bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
2977 bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
2978 }
2979 \f
2980
2981 bool
2982 xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
2983 asection *input_section ATTRIBUTE_UNUSED,
2984 bfd *output_bfd ATTRIBUTE_UNUSED,
2985 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2986 struct internal_syment *sym ATTRIBUTE_UNUSED,
2987 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2988 bfd_vma val ATTRIBUTE_UNUSED,
2989 bfd_vma addend ATTRIBUTE_UNUSED,
2990 bfd_vma *relocation ATTRIBUTE_UNUSED,
2991 bfd_byte *contents ATTRIBUTE_UNUSED,
2992 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2993 {
2994 return true;
2995 }
2996
2997 bool
2998 xcoff_reloc_type_fail (bfd *input_bfd,
2999 asection *input_section ATTRIBUTE_UNUSED,
3000 bfd *output_bfd ATTRIBUTE_UNUSED,
3001 struct internal_reloc *rel,
3002 struct internal_syment *sym ATTRIBUTE_UNUSED,
3003 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3004 bfd_vma val ATTRIBUTE_UNUSED,
3005 bfd_vma addend ATTRIBUTE_UNUSED,
3006 bfd_vma *relocation ATTRIBUTE_UNUSED,
3007 bfd_byte *contents ATTRIBUTE_UNUSED,
3008 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3009 {
3010 _bfd_error_handler
3011 /* xgettext: c-format */
3012 (_("%pB: unsupported relocation type %#x"),
3013 input_bfd, (unsigned int) rel->r_type);
3014 bfd_set_error (bfd_error_bad_value);
3015 return false;
3016 }
3017
3018 bool
3019 xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
3020 asection *input_section ATTRIBUTE_UNUSED,
3021 bfd *output_bfd ATTRIBUTE_UNUSED,
3022 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3023 struct internal_syment *sym ATTRIBUTE_UNUSED,
3024 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3025 bfd_vma val,
3026 bfd_vma addend,
3027 bfd_vma *relocation,
3028 bfd_byte *contents ATTRIBUTE_UNUSED,
3029 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3030 {
3031 *relocation = val + addend;
3032 return true;
3033 }
3034
3035 bool
3036 xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
3037 asection *input_section ATTRIBUTE_UNUSED,
3038 bfd *output_bfd ATTRIBUTE_UNUSED,
3039 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3040 struct internal_syment *sym ATTRIBUTE_UNUSED,
3041 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3042 bfd_vma val,
3043 bfd_vma addend,
3044 bfd_vma *relocation,
3045 bfd_byte *contents ATTRIBUTE_UNUSED,
3046 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3047 {
3048 *relocation = - val - addend;
3049 return true;
3050 }
3051
3052 bool
3053 xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
3054 asection *input_section,
3055 bfd *output_bfd ATTRIBUTE_UNUSED,
3056 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3057 struct internal_syment *sym ATTRIBUTE_UNUSED,
3058 struct reloc_howto_struct *howto,
3059 bfd_vma val,
3060 bfd_vma addend,
3061 bfd_vma *relocation,
3062 bfd_byte *contents ATTRIBUTE_UNUSED,
3063 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3064 {
3065 howto->pc_relative = true;
3066
3067 /* A PC relative reloc includes the section address. */
3068 addend += input_section->vma;
3069
3070 *relocation = val + addend;
3071 *relocation -= (input_section->output_section->vma
3072 + input_section->output_offset);
3073 return true;
3074 }
3075
3076 bool
3077 xcoff_reloc_type_toc (bfd *input_bfd,
3078 asection *input_section ATTRIBUTE_UNUSED,
3079 bfd *output_bfd,
3080 struct internal_reloc *rel,
3081 struct internal_syment *sym ATTRIBUTE_UNUSED,
3082 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3083 bfd_vma val,
3084 bfd_vma addend ATTRIBUTE_UNUSED,
3085 bfd_vma *relocation,
3086 bfd_byte *contents ATTRIBUTE_UNUSED,
3087 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3088 {
3089 struct xcoff_link_hash_entry *h;
3090
3091 if (0 > rel->r_symndx)
3092 return false;
3093
3094 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3095
3096 if (h != NULL && h->smclas != XMC_TD)
3097 {
3098 if (h->toc_section == NULL)
3099 {
3100 _bfd_error_handler
3101 /* xgettext: c-format */
3102 (_("%pB: TOC reloc at %#" PRIx64 " to symbol `%s' with no TOC entry"),
3103 input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
3104 bfd_set_error (bfd_error_bad_value);
3105 return false;
3106 }
3107
3108 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
3109 val = (h->toc_section->output_section->vma
3110 + h->toc_section->output_offset);
3111 }
3112
3113 /* We can't use the preexisting value written down by the
3114 assembly, as R_TOCU needs to be adjusted when the final
3115 R_TOCL value is signed. */
3116 *relocation = val - xcoff_data (output_bfd)->toc;
3117
3118 if (rel->r_type == R_TOCU)
3119 *relocation = ((*relocation + 0x8000) >> 16) & 0xffff;
3120 if (rel->r_type == R_TOCL)
3121 *relocation = *relocation & 0x0000ffff;
3122
3123 return true;
3124 }
3125
3126 bool
3127 xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
3128 asection *input_section ATTRIBUTE_UNUSED,
3129 bfd *output_bfd ATTRIBUTE_UNUSED,
3130 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3131 struct internal_syment *sym ATTRIBUTE_UNUSED,
3132 struct reloc_howto_struct *howto,
3133 bfd_vma val,
3134 bfd_vma addend,
3135 bfd_vma *relocation,
3136 bfd_byte *contents ATTRIBUTE_UNUSED,
3137 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3138 {
3139 howto->src_mask &= ~3;
3140 howto->dst_mask = howto->src_mask;
3141
3142 *relocation = val + addend;
3143
3144 return true;
3145 }
3146
3147 static bool
3148 xcoff_reloc_type_br (bfd *input_bfd,
3149 asection *input_section,
3150 bfd *output_bfd ATTRIBUTE_UNUSED,
3151 struct internal_reloc *rel,
3152 struct internal_syment *sym ATTRIBUTE_UNUSED,
3153 struct reloc_howto_struct *howto,
3154 bfd_vma val,
3155 bfd_vma addend,
3156 bfd_vma *relocation,
3157 bfd_byte *contents,
3158 struct bfd_link_info *info)
3159 {
3160 struct xcoff_link_hash_entry *h;
3161 bfd_vma section_offset;
3162 struct xcoff_stub_hash_entry *stub_entry = NULL;
3163 enum xcoff_stub_type stub_type;
3164
3165 if (0 > rel->r_symndx)
3166 return false;
3167
3168 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3169 section_offset = rel->r_vaddr - input_section->vma;
3170
3171 /* If we see an R_BR or R_RBR reloc which is jumping to global
3172 linkage code, and it is followed by an appropriate cror nop
3173 instruction, we replace the cror with lwz r2,20(r1). This
3174 restores the TOC after the glink code. Contrariwise, if the
3175 call is followed by a lwz r2,20(r1), but the call is not
3176 going to global linkage code, we can replace the load with a
3177 cror. */
3178 if (NULL != h
3179 && (bfd_link_hash_defined == h->root.type
3180 || bfd_link_hash_defweak == h->root.type)
3181 && section_offset + 8 <= input_section->size)
3182 {
3183 bfd_byte *pnext;
3184 unsigned long next;
3185
3186 pnext = contents + section_offset + 4;
3187 next = bfd_get_32 (input_bfd, pnext);
3188
3189 /* The _ptrgl function is magic. It is used by the AIX
3190 compiler to call a function through a pointer. */
3191 if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
3192 {
3193 if (next == 0x4def7b82 /* cror 15,15,15 */
3194 || next == 0x4ffffb82 /* cror 31,31,31 */
3195 || next == 0x60000000) /* ori r0,r0,0 */
3196 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r2,20(r1) */
3197
3198 }
3199 else
3200 {
3201 if (next == 0x80410014) /* lwz r2,20(r1) */
3202 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
3203 }
3204 }
3205 else if (NULL != h && bfd_link_hash_undefined == h->root.type)
3206 {
3207 /* Normally, this relocation is against a defined symbol. In the
3208 case where this is a partial link and the output section offset
3209 is greater than 2^25, the linker will return an invalid error
3210 message that the relocation has been truncated. Yes it has been
3211 truncated but no it not important. For this case, disable the
3212 overflow checking. */
3213
3214 howto->complain_on_overflow = complain_overflow_dont;
3215 }
3216
3217 /* Check if a stub is needed. */
3218 stub_type = bfd_xcoff_type_of_stub (input_section, rel, val, h);
3219 if (stub_type != xcoff_stub_none)
3220 {
3221 asection *stub_csect;
3222
3223 stub_entry = bfd_xcoff_get_stub_entry (input_section, h, info);
3224 if (stub_entry == NULL)
3225 {
3226 _bfd_error_handler (_("Unable to find the stub entry targeting %s"),
3227 h->root.root.string);
3228 bfd_set_error (bfd_error_bad_value);
3229 return false;
3230 }
3231
3232 stub_csect = stub_entry->hcsect->root.u.def.section;
3233 val = (stub_entry->stub_offset
3234 + stub_csect->output_section->vma
3235 + stub_csect->output_offset);
3236 }
3237
3238 /* The original PC-relative relocation is biased by -r_vaddr, so adding
3239 the value below will give the absolute target address. */
3240 *relocation = val + addend + rel->r_vaddr;
3241
3242 howto->src_mask &= ~3;
3243 howto->dst_mask = howto->src_mask;
3244
3245 if (h != NULL
3246 && (h->root.type == bfd_link_hash_defined
3247 || h->root.type == bfd_link_hash_defweak)
3248 && bfd_is_abs_section (h->root.u.def.section)
3249 && section_offset + 4 <= input_section->size)
3250 {
3251 bfd_byte *ptr;
3252 bfd_vma insn;
3253
3254 /* Turn the relative branch into an absolute one by setting the
3255 AA bit. */
3256 ptr = contents + section_offset;
3257 insn = bfd_get_32 (input_bfd, ptr);
3258 insn |= 2;
3259 bfd_put_32 (input_bfd, insn, ptr);
3260
3261 /* Make the howto absolute too. */
3262 howto->pc_relative = false;
3263 howto->complain_on_overflow = complain_overflow_bitfield;
3264 }
3265 else
3266 {
3267 /* Use a PC-relative howto and subtract the instruction's address
3268 from the target address we calculated above. */
3269 howto->pc_relative = true;
3270 *relocation -= (input_section->output_section->vma
3271 + input_section->output_offset
3272 + section_offset);
3273 }
3274 return true;
3275 }
3276
3277 bool
3278 xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
3279 asection *input_section,
3280 bfd *output_bfd ATTRIBUTE_UNUSED,
3281 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3282 struct internal_syment *sym ATTRIBUTE_UNUSED,
3283 struct reloc_howto_struct *howto,
3284 bfd_vma val ATTRIBUTE_UNUSED,
3285 bfd_vma addend,
3286 bfd_vma *relocation,
3287 bfd_byte *contents ATTRIBUTE_UNUSED,
3288 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3289 {
3290 howto->pc_relative = true;
3291 howto->src_mask &= ~3;
3292 howto->dst_mask = howto->src_mask;
3293
3294 /* A PC relative reloc includes the section address. */
3295 addend += input_section->vma;
3296
3297 *relocation = val + addend;
3298 *relocation -= (input_section->output_section->vma
3299 + input_section->output_offset);
3300 return true;
3301 }
3302
3303 bool
3304 xcoff_reloc_type_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
3305 asection *input_section ATTRIBUTE_UNUSED,
3306 bfd *output_bfd ATTRIBUTE_UNUSED,
3307 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3308 struct internal_syment *sym ATTRIBUTE_UNUSED,
3309 struct reloc_howto_struct *howto,
3310 bfd_vma val,
3311 bfd_vma addend,
3312 bfd_vma *relocation,
3313 bfd_byte *contents ATTRIBUTE_UNUSED,
3314 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3315 {
3316 struct xcoff_link_hash_entry *h;
3317
3318 if (0 > rel->r_symndx)
3319 return false;
3320
3321 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3322
3323 /* R_TLSML is handled by the loader but must be from a
3324 TOC entry targeting itslef. This is already verified in
3325 xcoff_link_add_symbols.
3326 The value must be 0. */
3327 if (howto->type == R_TLSML)
3328 {
3329 *relocation = 0;
3330 return true;
3331 }
3332
3333 /* The target symbol should always be available even if it's not
3334 exported. */
3335 BFD_ASSERT (h != NULL);
3336
3337 /* TLS relocations must target a TLS symbol. */
3338 if (h->smclas != XMC_TL && h->smclas != XMC_UL)
3339 {
3340 _bfd_error_handler
3341 (_("%pB: TLS relocation at 0x%" PRIx64 " over non-TLS symbol %s (0x%x)\n"),
3342 input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string, h->smclas);
3343 return false;
3344 }
3345
3346 /* Local TLS relocations must target a local symbol, ie
3347 non-imported. */
3348 if ((rel->r_type == R_TLS_LD || rel->r_type == R_TLS_LE)
3349 && (((h->flags & XCOFF_DEF_REGULAR) == 0
3350 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3351 || (h->flags & XCOFF_IMPORT) != 0))
3352 {
3353 _bfd_error_handler
3354 (_("%pB: TLS local relocation at 0x%" PRIx64 " over imported symbol %s\n"),
3355 input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
3356 return false;
3357 }
3358
3359 /* R_TLSM are relocations used by the loader.
3360 The value must be 0. */
3361 if (howto->type == R_TLSM)
3362 {
3363 *relocation = 0;
3364 return true;
3365 }
3366
3367 /* Other TLS relocations aims to put offsets from TLS pointers
3368 starting at -0x7c00 (or -0x7800 in XCOFF64). It becomes a
3369 simple R_POS relocation as long as .tdata and .tbss addresses
3370 start at the same value. This is done in aix ld scripts.
3371 TODO: implement optimization when tls size is < 62K. */
3372 *relocation = val + addend;
3373
3374 return true;
3375 }
3376
3377 static bool
3378 xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
3379 bfd_vma val ATTRIBUTE_UNUSED,
3380 bfd_vma relocation ATTRIBUTE_UNUSED,
3381 struct reloc_howto_struct *
3382 howto ATTRIBUTE_UNUSED)
3383 {
3384 return false;
3385 }
3386
3387 static bool
3388 xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
3389 bfd_vma val,
3390 bfd_vma relocation,
3391 struct reloc_howto_struct *howto)
3392 {
3393 bfd_vma fieldmask, signmask, ss;
3394 bfd_vma a, b, sum;
3395
3396 /* Get the values to be added together. For signed and unsigned
3397 relocations, we assume that all values should be truncated to
3398 the size of an address. For bitfields, all the bits matter.
3399 See also bfd_check_overflow. */
3400 fieldmask = N_ONES (howto->bitsize);
3401 a = relocation;
3402 b = val & howto->src_mask;
3403
3404 /* Much like unsigned, except no trimming with addrmask. In
3405 addition, the sum overflows if there is a carry out of
3406 the bfd_vma, i.e., the sum is less than either input
3407 operand. */
3408 a >>= howto->rightshift;
3409 b >>= howto->bitpos;
3410
3411 /* Bitfields are sometimes used for signed numbers; for
3412 example, a 13-bit field sometimes represents values in
3413 0..8191 and sometimes represents values in -4096..4095.
3414 If the field is signed and a is -4095 (0x1001) and b is
3415 -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3416 0x1fff is 0x3000). It's not clear how to handle this
3417 everywhere, since there is not way to know how many bits
3418 are significant in the relocation, but the original code
3419 assumed that it was fully sign extended, and we will keep
3420 that assumption. */
3421 signmask = (fieldmask >> 1) + 1;
3422
3423 if ((a & ~ fieldmask) != 0)
3424 {
3425 /* Some bits out of the field are set. This might not
3426 be a problem: if this is a signed bitfield, it is OK
3427 iff all the high bits are set, including the sign
3428 bit. We'll try setting all but the most significant
3429 bit in the original relocation value: if this is all
3430 ones, we are OK, assuming a signed bitfield. */
3431 ss = (signmask << howto->rightshift) - 1;
3432 if ((ss | relocation) != ~ (bfd_vma) 0)
3433 return true;
3434 a &= fieldmask;
3435 }
3436
3437 /* We just assume (b & ~ fieldmask) == 0. */
3438
3439 /* We explicitly permit wrap around if this relocation
3440 covers the high bit of an address. The Linux kernel
3441 relies on it, and it is the only way to write assembler
3442 code which can run when loaded at a location 0x80000000
3443 away from the location at which it is linked. */
3444 if ((unsigned) howto->bitsize + howto->rightshift
3445 == bfd_arch_bits_per_address (input_bfd))
3446 return false;
3447
3448 sum = a + b;
3449 if (sum < a || (sum & ~ fieldmask) != 0)
3450 {
3451 /* There was a carry out, or the field overflow. Test
3452 for signed operands again. Here is the overflow test
3453 is as for complain_overflow_signed. */
3454 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3455 return true;
3456 }
3457
3458 return false;
3459 }
3460
3461 static bool
3462 xcoff_complain_overflow_signed_func (bfd *input_bfd,
3463 bfd_vma val,
3464 bfd_vma relocation,
3465 struct reloc_howto_struct *howto)
3466 {
3467 bfd_vma addrmask, fieldmask, signmask, ss;
3468 bfd_vma a, b, sum;
3469
3470 /* Get the values to be added together. For signed and unsigned
3471 relocations, we assume that all values should be truncated to
3472 the size of an address. For bitfields, all the bits matter.
3473 See also bfd_check_overflow. */
3474 fieldmask = N_ONES (howto->bitsize);
3475 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3476 a = relocation;
3477 b = val & howto->src_mask;
3478
3479 a = (a & addrmask) >> howto->rightshift;
3480
3481 /* If any sign bits are set, all sign bits must be set.
3482 That is, A must be a valid negative address after
3483 shifting. */
3484 signmask = ~ (fieldmask >> 1);
3485 ss = a & signmask;
3486 if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
3487 return true;
3488
3489 /* We only need this next bit of code if the sign bit of B
3490 is below the sign bit of A. This would only happen if
3491 SRC_MASK had fewer bits than BITSIZE. Note that if
3492 SRC_MASK has more bits than BITSIZE, we can get into
3493 trouble; we would need to verify that B is in range, as
3494 we do for A above. */
3495 signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3496 if ((b & signmask) != 0)
3497 {
3498 /* Set all the bits above the sign bit. */
3499 b -= signmask <<= 1;
3500 }
3501
3502 b = (b & addrmask) >> howto->bitpos;
3503
3504 /* Now we can do the addition. */
3505 sum = a + b;
3506
3507 /* See if the result has the correct sign. Bits above the
3508 sign bit are junk now; ignore them. If the sum is
3509 positive, make sure we did not have all negative inputs;
3510 if the sum is negative, make sure we did not have all
3511 positive inputs. The test below looks only at the sign
3512 bits, and it really just
3513 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3514 */
3515 signmask = (fieldmask >> 1) + 1;
3516 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3517 return true;
3518
3519 return false;
3520 }
3521
3522 static bool
3523 xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
3524 bfd_vma val,
3525 bfd_vma relocation,
3526 struct reloc_howto_struct *howto)
3527 {
3528 bfd_vma addrmask, fieldmask;
3529 bfd_vma a, b, sum;
3530
3531 /* Get the values to be added together. For signed and unsigned
3532 relocations, we assume that all values should be truncated to
3533 the size of an address. For bitfields, all the bits matter.
3534 See also bfd_check_overflow. */
3535 fieldmask = N_ONES (howto->bitsize);
3536 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3537 a = relocation;
3538 b = val & howto->src_mask;
3539
3540 /* Checking for an unsigned overflow is relatively easy:
3541 trim the addresses and add, and trim the result as well.
3542 Overflow is normally indicated when the result does not
3543 fit in the field. However, we also need to consider the
3544 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3545 input is 0x80000000, and bfd_vma is only 32 bits; then we
3546 will get sum == 0, but there is an overflow, since the
3547 inputs did not fit in the field. Instead of doing a
3548 separate test, we can check for this by or-ing in the
3549 operands when testing for the sum overflowing its final
3550 field. */
3551 a = (a & addrmask) >> howto->rightshift;
3552 b = (b & addrmask) >> howto->bitpos;
3553 sum = (a + b) & addrmask;
3554 if ((a | b | sum) & ~ fieldmask)
3555 return true;
3556
3557 return false;
3558 }
3559
3560 /* This is the relocation function for the RS/6000/POWER/PowerPC.
3561 This is currently the only processor which uses XCOFF; I hope that
3562 will never change.
3563
3564 The original version was based on two documents:
3565 the PowerPC AIX Version 4 Application Binary Interface, First
3566 Edition (April 1992), and the PowerOpen ABI, Big-Endian
3567 32-Bit Hardware Implementation (June 30, 1994). Differences
3568 between the documents are noted below.
3569 Now, IBM has released an official documentation about XCOFF
3570 format:
3571 https://www.ibm.com/support/knowledgecenter/ssw_aix_72/filesreference/XCOFF.html
3572
3573 Unsupported r_type's
3574
3575 R_RTB:
3576 R_RRTBI:
3577 R_RRTBA:
3578
3579 These relocs are defined by the PowerPC ABI to be
3580 relative branches which use half of the difference
3581 between the symbol and the program counter. I can't
3582 quite figure out when this is useful. These relocs are
3583 not defined by the PowerOpen ABI.
3584
3585 Supported r_type's
3586
3587 R_POS:
3588 Simple positive relocation.
3589
3590 R_NEG:
3591 Simple negative relocation.
3592
3593 R_REL:
3594 Simple PC relative relocation.
3595
3596 R_TOC:
3597 TOC relative relocation. The value in the instruction in
3598 the input file is the offset from the input file TOC to
3599 the desired location. We want the offset from the final
3600 TOC to the desired location. We have:
3601 isym = iTOC + in
3602 iinsn = in + o
3603 osym = oTOC + on
3604 oinsn = on + o
3605 so we must change insn by on - in.
3606 This relocation allows the linker to perform optimizations
3607 by transforming a load instruction into a add-immediate
3608 when possible. The relocation is, then, changed to R_TRLA
3609 in the output file.
3610 TODO: Currently, the optimisation isn't implemented.
3611
3612 R_TRL:
3613 TOC relative relocation. Same as R_TOC, except that
3614 the optimization isn't allowed
3615
3616 R_TRLA:
3617 TOC relative relocation. This is a TOC relative load
3618 address instruction which have been changed to an add-
3619 immediate instruction.
3620
3621 R_GL:
3622 GL linkage relocation. The value of this relocation
3623 is the address of the external symbol in the TOC
3624 section.
3625
3626 R_TCL:
3627 Local object TOC address. I can't figure out the
3628 difference between this and case R_GL.
3629
3630 R_RL:
3631 The PowerPC AIX ABI describes this as a load which may be
3632 changed to a load address. The PowerOpen ABI says this
3633 is the same as case R_POS.
3634
3635 R_RLA:
3636 The PowerPC AIX ABI describes this as a load address
3637 which may be changed to a load. The PowerOpen ABI says
3638 this is the same as R_POS.
3639
3640 R_REF:
3641 Not a relocation but a way to prevent the garbage
3642 collector of AIX linker to remove symbols.
3643 This is not needed in our case.
3644
3645 R_BA:
3646 The PowerOpen ABI says this is the same as R_RBA.
3647
3648 R_RBA:
3649 Absolute branch which may be modified to become a
3650 relative branch.
3651
3652 R_BR:
3653 The PowerOpen ABI says this is the same as R_RBR.
3654
3655 R_RBR:
3656 A relative branch which may be modified to become an
3657 absolute branch.
3658
3659 R_CAI:
3660 The PowerPC ABI defines this as an absolute call which
3661 may be modified to become a relative call. The PowerOpen
3662 ABI does not define this relocation type.
3663
3664 R_CREL:
3665 The PowerPC ABI defines this as a relative call which may
3666 be modified to become an absolute call. The PowerOpen
3667 ABI does not define this relocation type.
3668
3669 R_RBAC:
3670 The PowerPC ABI defines this as an absolute branch to a
3671 fixed address which may be modified to an absolute branch
3672 to a symbol. The PowerOpen ABI does not define this
3673 relocation type.
3674
3675 R_RBRC:
3676 The PowerPC ABI defines this as an absolute branch to a
3677 fixed address which may be modified to a relative branch.
3678 The PowerOpen ABI does not define this relocation type.
3679
3680 R_TLS:
3681 Thread-local storage relocation using general-dynamic
3682 model.
3683
3684 R_TLS_IE:
3685 Thread-local storage relocation using initial-exec model.
3686
3687 R_TLS_LD:
3688 Thread-local storage relocation using local-dynamic model.
3689
3690 R_TLS_LE:
3691 Thread-local storage relocation using local-exec model.
3692
3693 R_TLSM:
3694 Tread-local storage relocation used by the loader.
3695
3696 R_TLSML:
3697 Tread-local storage relocation used by the loader.
3698
3699 R_TOCU:
3700 Upper TOC relative relocation. The value is the
3701 high-order 16 bit of a TOC relative relocation.
3702
3703 R_TOCL:
3704 Lower TOC relative relocation. The value is the
3705 low-order 16 bit of a TOC relative relocation.
3706 */
3707
3708 bool
3709 xcoff_ppc_relocate_section (bfd *output_bfd,
3710 struct bfd_link_info *info,
3711 bfd *input_bfd,
3712 asection *input_section,
3713 bfd_byte *contents,
3714 struct internal_reloc *relocs,
3715 struct internal_syment *syms,
3716 asection **sections)
3717 {
3718 struct internal_reloc *rel;
3719 struct internal_reloc *relend;
3720
3721 rel = relocs;
3722 relend = rel + input_section->reloc_count;
3723 for (; rel < relend; rel++)
3724 {
3725 long symndx;
3726 struct xcoff_link_hash_entry *h;
3727 struct internal_syment *sym;
3728 bfd_vma addend;
3729 bfd_vma val;
3730 struct reloc_howto_struct howto;
3731 bfd_vma relocation;
3732 bfd_vma value_to_relocate;
3733 bfd_vma address;
3734 bfd_byte *location;
3735
3736 /* Relocation type R_REF is a special relocation type which is
3737 merely used to prevent garbage collection from occurring for
3738 the csect including the symbol which it references. */
3739 if (rel->r_type == R_REF)
3740 continue;
3741
3742 /* Retrieve default value in HOWTO table and fix up according
3743 to r_size field, if it can be different.
3744 This should be made during relocation reading but the algorithms
3745 are expecting constant howtos. */
3746 memcpy (&howto, &xcoff_howto_table[rel->r_type], sizeof (howto));
3747 if (howto.bitsize != (rel->r_size & 0x1f) + 1)
3748 {
3749 switch (rel->r_type)
3750 {
3751 case R_POS:
3752 case R_NEG:
3753 howto.bitsize = (rel->r_size & 0x1f) + 1;
3754 howto.size = HOWTO_RSIZE (howto.bitsize > 16 ? 4 : 2);
3755 howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
3756 break;
3757
3758 default:
3759 _bfd_error_handler
3760 (_("%pB: relocation (%d) at 0x%" PRIx64 " has wrong r_rsize (0x%x)\n"),
3761 input_bfd, rel->r_type, (uint64_t) rel->r_vaddr, rel->r_size);
3762 return false;
3763 }
3764 }
3765
3766 howto.complain_on_overflow = (rel->r_size & 0x80
3767 ? complain_overflow_signed
3768 : complain_overflow_bitfield);
3769
3770 /* symbol */
3771 val = 0;
3772 addend = 0;
3773 h = NULL;
3774 sym = NULL;
3775 symndx = rel->r_symndx;
3776
3777 if (-1 != symndx)
3778 {
3779 asection *sec;
3780
3781 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3782 sym = syms + symndx;
3783 addend = - sym->n_value;
3784
3785 if (NULL == h)
3786 {
3787 sec = sections[symndx];
3788 /* Hack to make sure we use the right TOC anchor value
3789 if this reloc is against the TOC anchor. */
3790 if (sec->name[3] == '0'
3791 && strcmp (sec->name, ".tc0") == 0)
3792 val = xcoff_data (output_bfd)->toc;
3793 else
3794 val = (sec->output_section->vma
3795 + sec->output_offset
3796 + sym->n_value
3797 - sec->vma);
3798 }
3799 else
3800 {
3801 if (info->unresolved_syms_in_objects != RM_IGNORE
3802 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3803 (*info->callbacks->undefined_symbol)
3804 (info, h->root.root.string,
3805 input_bfd, input_section,
3806 rel->r_vaddr - input_section->vma,
3807 info->unresolved_syms_in_objects == RM_DIAGNOSE &&
3808 !info->warn_unresolved_syms);
3809
3810 if (h->root.type == bfd_link_hash_defined
3811 || h->root.type == bfd_link_hash_defweak)
3812 {
3813 sec = h->root.u.def.section;
3814 val = (h->root.u.def.value
3815 + sec->output_section->vma
3816 + sec->output_offset);
3817 }
3818 else if (h->root.type == bfd_link_hash_common)
3819 {
3820 sec = h->root.u.c.p->section;
3821 val = (sec->output_section->vma
3822 + sec->output_offset);
3823
3824 }
3825 else
3826 {
3827 BFD_ASSERT (bfd_link_relocatable (info)
3828 || (info->static_link
3829 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3830 || (h->flags & XCOFF_DEF_DYNAMIC) != 0
3831 || (h->flags & XCOFF_IMPORT) != 0);
3832 }
3833 }
3834 }
3835
3836 if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
3837 || !((*xcoff_calculate_relocation[rel->r_type])
3838 (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
3839 addend, &relocation, contents, info)))
3840 return false;
3841
3842 /* address */
3843 address = rel->r_vaddr - input_section->vma;
3844 location = contents + address;
3845
3846 if (address > input_section->size)
3847 abort ();
3848
3849 /* Get the value we are going to relocate. */
3850 if (2 == bfd_get_reloc_size (&howto))
3851 value_to_relocate = bfd_get_16 (input_bfd, location);
3852 else
3853 value_to_relocate = bfd_get_32 (input_bfd, location);
3854
3855 /* overflow.
3856
3857 FIXME: We may drop bits during the addition
3858 which we don't check for. We must either check at every single
3859 operation, which would be tedious, or we must do the computations
3860 in a type larger than bfd_vma, which would be inefficient. */
3861
3862 if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3863 (input_bfd, value_to_relocate, relocation, &howto)))
3864 {
3865 const char *name;
3866 char buf[SYMNMLEN + 1];
3867 char reloc_type_name[10];
3868
3869 if (symndx == -1)
3870 {
3871 name = "*ABS*";
3872 }
3873 else if (h != NULL)
3874 {
3875 name = NULL;
3876 }
3877 else
3878 {
3879 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3880 if (name == NULL)
3881 name = "UNKNOWN";
3882 }
3883 sprintf (reloc_type_name, "0x%02x", rel->r_type);
3884
3885 (*info->callbacks->reloc_overflow)
3886 (info, (h ? &h->root : NULL), name, reloc_type_name,
3887 (bfd_vma) 0, input_bfd, input_section,
3888 rel->r_vaddr - input_section->vma);
3889 }
3890
3891 /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE. */
3892 value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3893 | (((value_to_relocate & howto.src_mask)
3894 + relocation) & howto.dst_mask));
3895
3896 /* Put the value back in the object file. */
3897 if (2 == bfd_get_reloc_size (&howto))
3898 bfd_put_16 (input_bfd, value_to_relocate, location);
3899 else
3900 bfd_put_32 (input_bfd, value_to_relocate, location);
3901 }
3902
3903 return true;
3904 }
3905
3906 /* gcc-8 warns (*) on all the strncpy calls in this function about
3907 possible string truncation. The "truncation" is not a bug. We
3908 have an external representation of structs with fields that are not
3909 necessarily NULL terminated and corresponding internal
3910 representation fields that are one larger so that they can always
3911 be NULL terminated.
3912 gcc versions between 4.2 and 4.6 do not allow pragma control of
3913 diagnostics inside functions, giving a hard error if you try to use
3914 the finer control available with later versions.
3915 gcc prior to 4.2 warns about diagnostic push and pop.
3916 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
3917 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
3918 (*) Depending on your system header files! */
3919 #if GCC_VERSION >= 8000
3920 # pragma GCC diagnostic push
3921 # pragma GCC diagnostic ignored "-Wstringop-truncation"
3922 #endif
3923 static bool
3924 _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
3925 struct xcoff_loader_info *ldinfo,
3926 struct internal_ldsym *ldsym,
3927 const char *name)
3928 {
3929 size_t len;
3930 len = strlen (name);
3931
3932 if (len <= SYMNMLEN)
3933 strncpy (ldsym->_l._l_name, name, SYMNMLEN);
3934 else
3935 {
3936 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3937 {
3938 bfd_size_type newalc;
3939 char *newstrings;
3940
3941 newalc = ldinfo->string_alc * 2;
3942 if (newalc == 0)
3943 newalc = 32;
3944 while (ldinfo->string_size + len + 3 > newalc)
3945 newalc *= 2;
3946
3947 newstrings = bfd_realloc (ldinfo->strings, newalc);
3948 if (newstrings == NULL)
3949 {
3950 ldinfo->failed = true;
3951 return false;
3952 }
3953 ldinfo->string_alc = newalc;
3954 ldinfo->strings = newstrings;
3955 }
3956
3957 ldinfo->strings[ldinfo->string_size] = ((len + 1) >> 8) & 0xff;
3958 ldinfo->strings[ldinfo->string_size + 1] = ((len + 1)) & 0xff;
3959 strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
3960 ldsym->_l._l_l._l_zeroes = 0;
3961 ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3962 ldinfo->string_size += len + 3;
3963 }
3964
3965 return true;
3966 }
3967
3968 static bool
3969 _bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
3970 struct bfd_strtab_hash *strtab,
3971 struct internal_syment *sym,
3972 const char *name)
3973 {
3974 if (strlen (name) <= SYMNMLEN)
3975 {
3976 strncpy (sym->_n._n_name, name, SYMNMLEN);
3977 }
3978 else
3979 {
3980 bool hash;
3981 bfd_size_type indx;
3982
3983 hash = !info->traditional_format;
3984 indx = _bfd_stringtab_add (strtab, name, hash, false);
3985 if (indx == (bfd_size_type) -1)
3986 return false;
3987 sym->_n._n_n._n_zeroes = 0;
3988 sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3989 }
3990 return true;
3991 }
3992 #if GCC_VERSION >= 8000
3993 # pragma GCC diagnostic pop
3994 #endif
3995
3996 static asection *
3997 xcoff_create_csect_from_smclas (bfd *abfd,
3998 union internal_auxent *aux,
3999 const char *symbol_name)
4000 {
4001 asection *return_value = NULL;
4002
4003 /* .sv64 = x_smclas == 17
4004 This is an invalid csect for 32 bit apps. */
4005 static const char * const names[] =
4006 {
4007 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
4008 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
4009 ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
4010 };
4011
4012 if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
4013 && (NULL != names[aux->x_csect.x_smclas]))
4014 {
4015 return_value = bfd_make_section_anyway
4016 (abfd, names[aux->x_csect.x_smclas]);
4017 }
4018 else
4019 {
4020 _bfd_error_handler
4021 /* xgettext: c-format */
4022 (_("%pB: symbol `%s' has unrecognized smclas %d"),
4023 abfd, symbol_name, aux->x_csect.x_smclas);
4024 bfd_set_error (bfd_error_bad_value);
4025 }
4026
4027 return return_value;
4028 }
4029
4030 static bool
4031 xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
4032 {
4033 if (0xffff <= value)
4034 return true;
4035
4036 return false;
4037 }
4038
4039 static bool
4040 xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
4041 {
4042 if (0xffff <= value)
4043 return true;
4044
4045 return false;
4046 }
4047
4048 static bfd_vma
4049 xcoff_loader_symbol_offset (bfd *abfd,
4050 struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
4051 {
4052 return bfd_xcoff_ldhdrsz (abfd);
4053 }
4054
4055 static bfd_vma
4056 xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
4057 {
4058 return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
4059 }
4060
4061 static bool
4062 xcoff_generate_rtinit (bfd *abfd, const char *init, const char *fini,
4063 bool rtld)
4064 {
4065 bfd_byte filehdr_ext[FILHSZ];
4066 bfd_byte scnhdr_ext[SCNHSZ];
4067 bfd_byte syment_ext[SYMESZ * 10];
4068 bfd_byte reloc_ext[RELSZ * 3];
4069 bfd_byte *data_buffer;
4070 bfd_size_type data_buffer_size;
4071 bfd_byte *string_table = NULL, *st_tmp = NULL;
4072 bfd_size_type string_table_size;
4073 bfd_vma val;
4074 size_t initsz, finisz;
4075 struct internal_filehdr filehdr;
4076 struct internal_scnhdr scnhdr;
4077 struct internal_syment syment;
4078 union internal_auxent auxent;
4079 struct internal_reloc reloc;
4080
4081 char *data_name = ".data";
4082 char *rtinit_name = "__rtinit";
4083 char *rtld_name = "__rtld";
4084
4085 if (! bfd_xcoff_rtinit_size (abfd))
4086 return false;
4087
4088 initsz = (init == NULL ? 0 : 1 + strlen (init));
4089 finisz = (fini == NULL ? 0 : 1 + strlen (fini));
4090
4091 /* file header */
4092 memset (filehdr_ext, 0, FILHSZ);
4093 memset (&filehdr, 0, sizeof (struct internal_filehdr));
4094 filehdr.f_magic = bfd_xcoff_magic_number (abfd);
4095 filehdr.f_nscns = 1;
4096 filehdr.f_timdat = 0;
4097 filehdr.f_nsyms = 0; /* at least 6, no more than 10 */
4098 filehdr.f_symptr = 0; /* set below */
4099 filehdr.f_opthdr = 0;
4100 filehdr.f_flags = 0;
4101
4102 /* section header */
4103 memset (scnhdr_ext, 0, SCNHSZ);
4104 memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
4105 memcpy (scnhdr.s_name, data_name, strlen (data_name));
4106 scnhdr.s_paddr = 0;
4107 scnhdr.s_vaddr = 0;
4108 scnhdr.s_size = 0; /* set below */
4109 scnhdr.s_scnptr = FILHSZ + SCNHSZ;
4110 scnhdr.s_relptr = 0; /* set below */
4111 scnhdr.s_lnnoptr = 0;
4112 scnhdr.s_nreloc = 0; /* either 1 or 2 */
4113 scnhdr.s_nlnno = 0;
4114 scnhdr.s_flags = STYP_DATA;
4115
4116 /* .data
4117 0x0000 0x00000000 : rtl
4118 0x0004 0x00000010 : offset to init, or 0
4119 0x0008 0x00000028 : offset to fini, or 0
4120 0x000C 0x0000000C : size of descriptor
4121 0x0010 0x00000000 : init, needs a reloc
4122 0x0014 0x00000040 : offset to init name
4123 0x0018 0x00000000 : flags, padded to a word
4124 0x001C 0x00000000 : empty init
4125 0x0020 0x00000000 :
4126 0x0024 0x00000000 :
4127 0x0028 0x00000000 : fini, needs a reloc
4128 0x002C 0x00000??? : offset to fini name
4129 0x0030 0x00000000 : flags, padded to a word
4130 0x0034 0x00000000 : empty fini
4131 0x0038 0x00000000 :
4132 0x003C 0x00000000 :
4133 0x0040 init name
4134 0x0040 + initsz fini name */
4135
4136 data_buffer_size = 0x0040 + initsz + finisz;
4137 data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
4138 data_buffer = NULL;
4139 data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
4140 if (data_buffer == NULL)
4141 return false;
4142
4143 if (initsz)
4144 {
4145 val = 0x10;
4146 bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
4147 val = 0x40;
4148 bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
4149 memcpy (&data_buffer[val], init, initsz);
4150 }
4151
4152 if (finisz)
4153 {
4154 val = 0x28;
4155 bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
4156 val = 0x40 + initsz;
4157 bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
4158 memcpy (&data_buffer[val], fini, finisz);
4159 }
4160
4161 val = 0x0C;
4162 bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
4163
4164 scnhdr.s_size = data_buffer_size;
4165
4166 /* string table */
4167 string_table_size = 0;
4168 if (initsz > 9)
4169 string_table_size += initsz;
4170 if (finisz > 9)
4171 string_table_size += finisz;
4172 if (string_table_size)
4173 {
4174 string_table_size += 4;
4175 string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
4176 if (string_table == NULL)
4177 return false;
4178
4179 val = string_table_size;
4180 bfd_h_put_32 (abfd, val, &string_table[0]);
4181 st_tmp = string_table + 4;
4182 }
4183
4184 /* symbols
4185 0. .data csect
4186 2. __rtinit
4187 4. init function
4188 6. fini function
4189 8. __rtld */
4190 memset (syment_ext, 0, 10 * SYMESZ);
4191 memset (reloc_ext, 0, 3 * RELSZ);
4192
4193 /* .data csect */
4194 memset (&syment, 0, sizeof (struct internal_syment));
4195 memset (&auxent, 0, sizeof (union internal_auxent));
4196 memcpy (syment._n._n_name, data_name, strlen (data_name));
4197 syment.n_scnum = 1;
4198 syment.n_sclass = C_HIDEXT;
4199 syment.n_numaux = 1;
4200 auxent.x_csect.x_scnlen.u64 = data_buffer_size;
4201 auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
4202 auxent.x_csect.x_smclas = XMC_RW;
4203 bfd_coff_swap_sym_out (abfd, &syment,
4204 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4205 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4206 syment.n_numaux,
4207 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4208 filehdr.f_nsyms += 2;
4209
4210 /* __rtinit */
4211 memset (&syment, 0, sizeof (struct internal_syment));
4212 memset (&auxent, 0, sizeof (union internal_auxent));
4213 memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
4214 syment.n_scnum = 1;
4215 syment.n_sclass = C_EXT;
4216 syment.n_numaux = 1;
4217 auxent.x_csect.x_smtyp = XTY_LD;
4218 auxent.x_csect.x_smclas = XMC_RW;
4219 bfd_coff_swap_sym_out (abfd, &syment,
4220 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4221 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4222 syment.n_numaux,
4223 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4224 filehdr.f_nsyms += 2;
4225
4226 /* init */
4227 if (initsz)
4228 {
4229 memset (&syment, 0, sizeof (struct internal_syment));
4230 memset (&auxent, 0, sizeof (union internal_auxent));
4231
4232 if (initsz > 9)
4233 {
4234 syment._n._n_n._n_offset = st_tmp - string_table;
4235 memcpy (st_tmp, init, initsz);
4236 st_tmp += initsz;
4237 }
4238 else
4239 memcpy (syment._n._n_name, init, initsz - 1);
4240
4241 syment.n_sclass = C_EXT;
4242 syment.n_numaux = 1;
4243 bfd_coff_swap_sym_out (abfd, &syment,
4244 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4245 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4246 syment.n_numaux,
4247 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4248
4249 /* reloc */
4250 memset (&reloc, 0, sizeof (struct internal_reloc));
4251 reloc.r_vaddr = 0x0010;
4252 reloc.r_symndx = filehdr.f_nsyms;
4253 reloc.r_type = R_POS;
4254 reloc.r_size = 31;
4255 bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
4256
4257 filehdr.f_nsyms += 2;
4258 scnhdr.s_nreloc += 1;
4259 }
4260
4261 /* fini */
4262 if (finisz)
4263 {
4264 memset (&syment, 0, sizeof (struct internal_syment));
4265 memset (&auxent, 0, sizeof (union internal_auxent));
4266
4267 if (finisz > 9)
4268 {
4269 syment._n._n_n._n_offset = st_tmp - string_table;
4270 memcpy (st_tmp, fini, finisz);
4271 st_tmp += finisz;
4272 }
4273 else
4274 memcpy (syment._n._n_name, fini, finisz - 1);
4275
4276 syment.n_sclass = C_EXT;
4277 syment.n_numaux = 1;
4278 bfd_coff_swap_sym_out (abfd, &syment,
4279 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4280 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4281 syment.n_numaux,
4282 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4283
4284 /* reloc */
4285 memset (&reloc, 0, sizeof (struct internal_reloc));
4286 reloc.r_vaddr = 0x0028;
4287 reloc.r_symndx = filehdr.f_nsyms;
4288 reloc.r_type = R_POS;
4289 reloc.r_size = 31;
4290 bfd_coff_swap_reloc_out (abfd, &reloc,
4291 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4292
4293 filehdr.f_nsyms += 2;
4294 scnhdr.s_nreloc += 1;
4295 }
4296
4297 if (rtld)
4298 {
4299 memset (&syment, 0, sizeof (struct internal_syment));
4300 memset (&auxent, 0, sizeof (union internal_auxent));
4301 memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
4302 syment.n_sclass = C_EXT;
4303 syment.n_numaux = 1;
4304 bfd_coff_swap_sym_out (abfd, &syment,
4305 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4306 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4307 syment.n_numaux,
4308 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4309
4310 /* reloc */
4311 memset (&reloc, 0, sizeof (struct internal_reloc));
4312 reloc.r_vaddr = 0x0000;
4313 reloc.r_symndx = filehdr.f_nsyms;
4314 reloc.r_type = R_POS;
4315 reloc.r_size = 31;
4316 bfd_coff_swap_reloc_out (abfd, &reloc,
4317 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4318
4319 filehdr.f_nsyms += 2;
4320 scnhdr.s_nreloc += 1;
4321 }
4322
4323 scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
4324 filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
4325
4326 bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
4327 bfd_bwrite (filehdr_ext, FILHSZ, abfd);
4328 bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
4329 bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
4330 bfd_bwrite (data_buffer, data_buffer_size, abfd);
4331 bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
4332 bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
4333 bfd_bwrite (string_table, string_table_size, abfd);
4334
4335 free (data_buffer);
4336 data_buffer = NULL;
4337
4338 return true;
4339 }
4340
4341
4342 static reloc_howto_type xcoff_dynamic_reloc =
4343 HOWTO (0, /* type */
4344 0, /* rightshift */
4345 4, /* size */
4346 32, /* bitsize */
4347 false, /* pc_relative */
4348 0, /* bitpos */
4349 complain_overflow_bitfield, /* complain_on_overflow */
4350 0, /* special_function */
4351 "R_POS", /* name */
4352 true, /* partial_inplace */
4353 0xffffffff, /* src_mask */
4354 0xffffffff, /* dst_mask */
4355 false); /* pcrel_offset */
4356
4357 /* Indirect call stub
4358 The first word of the code must be modified by filling in
4359 the correct TOC offset. */
4360
4361 static const unsigned long xcoff_stub_indirect_call_code[4] =
4362 {
4363 0x81820000, /* lwz r12,0(r2) */
4364 0x800c0000, /* lwz r0,0(r12) */
4365 0x7c0903a6, /* mtctr r0 */
4366 0x4e800420, /* bctr */
4367 };
4368
4369 /* Shared call stub
4370 The first word of the code must be modified by filling in
4371 the correct TOC offset.
4372 This is exactly as the glink code but without the traceback,
4373 as it won't be an independent function. */
4374
4375 static const unsigned long xcoff_stub_shared_call_code[6] =
4376 {
4377 0x81820000, /* lwz r12,0(r2) */
4378 0x90410014, /* stw r2,20(r1) */
4379 0x800c0000, /* lwz r0,0(r12) */
4380 0x804c0004, /* lwz r2,4(r12) */
4381 0x7c0903a6, /* mtctr r0 */
4382 0x4e800420, /* bctr */
4383 };
4384
4385 /* glink
4386
4387 The first word of global linkage code must be modified by filling in
4388 the correct TOC offset. */
4389
4390 static const unsigned long xcoff_glink_code[9] =
4391 {
4392 0x81820000, /* lwz r12,0(r2) */
4393 0x90410014, /* stw r2,20(r1) */
4394 0x800c0000, /* lwz r0,0(r12) */
4395 0x804c0004, /* lwz r2,4(r12) */
4396 0x7c0903a6, /* mtctr r0 */
4397 0x4e800420, /* bctr */
4398 0x00000000, /* start of traceback table */
4399 0x000c8000, /* traceback table */
4400 0x00000000, /* traceback table */
4401 };
4402
4403 /* Table to convert DWARF flags to section names.
4404 Remember to update binutils/dwarf.c:debug_displays
4405 if new DWARF sections are supported by XCOFF. */
4406
4407 const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
4408 { SSUBTYP_DWINFO, ".dwinfo", ".debug_info", true },
4409 { SSUBTYP_DWLINE, ".dwline", ".debug_line", true },
4410 { SSUBTYP_DWPBNMS, ".dwpbnms", ".debug_pubnames", true },
4411 { SSUBTYP_DWPBTYP, ".dwpbtyp", ".debug_pubtypes", true },
4412 { SSUBTYP_DWARNGE, ".dwarnge", ".debug_aranges", true },
4413 { SSUBTYP_DWABREV, ".dwabrev", ".debug_abbrev", false },
4414 { SSUBTYP_DWSTR, ".dwstr", ".debug_str", true },
4415 { SSUBTYP_DWRNGES, ".dwrnges", ".debug_ranges", true },
4416 { SSUBTYP_DWLOC, ".dwloc", ".debug_loc", true },
4417 { SSUBTYP_DWFRAME, ".dwframe", ".debug_frame", true },
4418 { SSUBTYP_DWMAC, ".dwmac", ".debug_macro", true }
4419 };
4420
4421 /* For generic entry points. */
4422 #define _bfd_xcoff_close_and_cleanup _bfd_coff_close_and_cleanup
4423 #define _bfd_xcoff_bfd_free_cached_info _bfd_bool_bfd_true
4424 #define _bfd_xcoff_new_section_hook coff_new_section_hook
4425 #define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
4426 #define _bfd_xcoff_get_section_contents_in_window \
4427 _bfd_generic_get_section_contents_in_window
4428
4429 /* For copy private data entry points. */
4430 #define _bfd_xcoff_bfd_copy_private_bfd_data \
4431 _bfd_xcoff_copy_private_bfd_data
4432 #define _bfd_xcoff_bfd_merge_private_bfd_data \
4433 _bfd_generic_bfd_merge_private_bfd_data
4434 #define _bfd_xcoff_bfd_copy_private_section_data \
4435 _bfd_generic_bfd_copy_private_section_data
4436 #define _bfd_xcoff_bfd_copy_private_symbol_data \
4437 _bfd_generic_bfd_copy_private_symbol_data
4438 #define _bfd_xcoff_bfd_copy_private_header_data \
4439 _bfd_generic_bfd_copy_private_header_data
4440 #define _bfd_xcoff_bfd_set_private_flags \
4441 _bfd_generic_bfd_set_private_flags
4442 #define _bfd_xcoff_bfd_print_private_bfd_data \
4443 _bfd_generic_bfd_print_private_bfd_data
4444
4445 /* For archive entry points. */
4446 #define _bfd_xcoff_slurp_extended_name_table \
4447 _bfd_noarchive_slurp_extended_name_table
4448 #define _bfd_xcoff_construct_extended_name_table \
4449 _bfd_noarchive_construct_extended_name_table
4450 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
4451 #define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
4452 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
4453 #define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
4454 #define _bfd_xcoff_update_armap_timestamp _bfd_bool_bfd_true
4455
4456 /* For symbols entry points. */
4457 #define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
4458 #define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
4459 #define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
4460 #define _bfd_xcoff_print_symbol coff_print_symbol
4461 #define _bfd_xcoff_get_symbol_info coff_get_symbol_info
4462 #define _bfd_xcoff_get_symbol_version_string \
4463 _bfd_nosymbols_get_symbol_version_string
4464 #define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
4465 #define _bfd_xcoff_bfd_is_target_special_symbol \
4466 coff_bfd_is_target_special_symbol
4467 #define _bfd_xcoff_get_lineno coff_get_lineno
4468 #define _bfd_xcoff_find_nearest_line coff_find_nearest_line
4469 #define _bfd_xcoff_find_nearest_line_with_alt \
4470 coff_find_nearest_line_with_alt
4471 #define _bfd_xcoff_find_line coff_find_line
4472 #define _bfd_xcoff_find_inliner_info coff_find_inliner_info
4473 #define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
4474 #define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
4475 #define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
4476
4477 /* For reloc entry points. */
4478 #define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
4479 #define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
4480 #define _bfd_xcoff_set_reloc _bfd_generic_set_reloc
4481 #define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
4482 #define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
4483
4484 /* For link entry points. */
4485 #define _bfd_xcoff_bfd_get_relocated_section_contents \
4486 bfd_generic_get_relocated_section_contents
4487 #define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
4488 #define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
4489 #define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
4490 #define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
4491 _bfd_generic_copy_link_hash_symbol_type
4492 #define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
4493 #define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
4494 #define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
4495 #define _bfd_xcoff_bfd_merge_sections bfd_generic_merge_sections
4496 #define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
4497 #define _bfd_xcoff_bfd_group_name bfd_generic_group_name
4498 #define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
4499 #define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
4500 #define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
4501 #define _bfd_xcoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
4502 #define _bfd_xcoff_bfd_define_start_stop bfd_generic_define_start_stop
4503 #define _bfd_xcoff_bfd_link_check_relocs _bfd_generic_link_check_relocs
4504
4505 /* For dynamic symbols and relocs entry points. */
4506 #define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
4507
4508 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
4509 {
4510 { /* COFF backend, defined in libcoff.h. */
4511 _bfd_xcoff_swap_aux_in,
4512 _bfd_xcoff_swap_sym_in,
4513 coff_swap_lineno_in,
4514 _bfd_xcoff_swap_aux_out,
4515 _bfd_xcoff_swap_sym_out,
4516 coff_swap_lineno_out,
4517 xcoff_swap_reloc_out,
4518 coff_swap_filehdr_out,
4519 coff_swap_aouthdr_out,
4520 coff_swap_scnhdr_out,
4521 FILHSZ,
4522 AOUTSZ,
4523 SCNHSZ,
4524 SYMESZ,
4525 AUXESZ,
4526 RELSZ,
4527 LINESZ,
4528 FILNMLEN,
4529 true, /* _bfd_coff_long_filenames */
4530 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4531 3, /* _bfd_coff_default_section_alignment_power */
4532 false, /* _bfd_coff_force_symnames_in_strings */
4533 2, /* _bfd_coff_debug_string_prefix_length */
4534 32768, /* _bfd_coff_max_nscns */
4535 coff_swap_filehdr_in,
4536 coff_swap_aouthdr_in,
4537 coff_swap_scnhdr_in,
4538 xcoff_swap_reloc_in,
4539 coff_bad_format_hook,
4540 coff_set_arch_mach_hook,
4541 coff_mkobject_hook,
4542 styp_to_sec_flags,
4543 coff_set_alignment_hook,
4544 coff_slurp_symbol_table,
4545 symname_in_debug_hook,
4546 coff_pointerize_aux_hook,
4547 coff_print_aux,
4548 dummy_reloc16_extra_cases,
4549 dummy_reloc16_estimate,
4550 NULL, /* bfd_coff_sym_is_global */
4551 coff_compute_section_file_positions,
4552 NULL, /* _bfd_coff_start_final_link */
4553 xcoff_ppc_relocate_section,
4554 coff_rtype_to_howto,
4555 NULL, /* _bfd_coff_adjust_symndx */
4556 _bfd_generic_link_add_one_symbol,
4557 coff_link_output_has_begun,
4558 coff_final_link_postscript,
4559 NULL /* print_pdata. */
4560 },
4561
4562 0x01DF, /* magic number */
4563 bfd_arch_rs6000,
4564 bfd_mach_rs6k,
4565
4566 /* Function pointers to xcoff specific swap routines. */
4567 xcoff_swap_ldhdr_in,
4568 xcoff_swap_ldhdr_out,
4569 xcoff_swap_ldsym_in,
4570 xcoff_swap_ldsym_out,
4571 xcoff_swap_ldrel_in,
4572 xcoff_swap_ldrel_out,
4573
4574 /* Sizes. */
4575 LDHDRSZ,
4576 LDSYMSZ,
4577 LDRELSZ,
4578 12, /* _xcoff_function_descriptor_size */
4579 SMALL_AOUTSZ,
4580
4581 /* Versions. */
4582 1, /* _xcoff_ldhdr_version */
4583
4584 _bfd_xcoff_put_symbol_name,
4585 _bfd_xcoff_put_ldsymbol_name,
4586 &xcoff_dynamic_reloc,
4587 xcoff_create_csect_from_smclas,
4588
4589 /* Lineno and reloc count overflow. */
4590 xcoff_is_lineno_count_overflow,
4591 xcoff_is_reloc_count_overflow,
4592
4593 xcoff_loader_symbol_offset,
4594 xcoff_loader_reloc_offset,
4595
4596 /* glink. */
4597 &xcoff_glink_code[0],
4598 36, /* _xcoff_glink_size */
4599
4600 /* rtinit */
4601 64, /* _xcoff_rtinit_size */
4602 xcoff_generate_rtinit,
4603
4604 /* Stub indirect call. */
4605 &xcoff_stub_indirect_call_code[0],
4606 16, /* _xcoff_stub_indirect_call_size */
4607
4608 /* Stub shared call. */
4609 &xcoff_stub_shared_call_code[0],
4610 24, /* _xcoff_stub_shared_call_size */
4611 };
4612
4613 /* The transfer vector that leads the outside world to all of the above. */
4614 const bfd_target rs6000_xcoff_vec =
4615 {
4616 "aixcoff-rs6000",
4617 bfd_target_xcoff_flavour,
4618 BFD_ENDIAN_BIG, /* data byte order is big */
4619 BFD_ENDIAN_BIG, /* header byte order is big */
4620
4621 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4622 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4623
4624 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4625 0, /* leading char */
4626 '/', /* ar_pad_char */
4627 15, /* ar_max_namelen */
4628 0, /* match priority. */
4629 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
4630
4631 /* data */
4632 bfd_getb64,
4633 bfd_getb_signed_64,
4634 bfd_putb64,
4635 bfd_getb32,
4636 bfd_getb_signed_32,
4637 bfd_putb32,
4638 bfd_getb16,
4639 bfd_getb_signed_16,
4640 bfd_putb16,
4641
4642 /* hdrs */
4643 bfd_getb64,
4644 bfd_getb_signed_64,
4645 bfd_putb64,
4646 bfd_getb32,
4647 bfd_getb_signed_32,
4648 bfd_putb32,
4649 bfd_getb16,
4650 bfd_getb_signed_16,
4651 bfd_putb16,
4652
4653 { /* bfd_check_format */
4654 _bfd_dummy_target,
4655 coff_object_p,
4656 _bfd_xcoff_archive_p,
4657 CORE_FILE_P
4658 },
4659
4660 { /* bfd_set_format */
4661 _bfd_bool_bfd_false_error,
4662 coff_mkobject,
4663 _bfd_generic_mkarchive,
4664 _bfd_bool_bfd_false_error
4665 },
4666
4667 {/* bfd_write_contents */
4668 _bfd_bool_bfd_false_error,
4669 coff_write_object_contents,
4670 _bfd_xcoff_write_archive_contents,
4671 _bfd_bool_bfd_false_error
4672 },
4673
4674 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4675 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4676 BFD_JUMP_TABLE_CORE (coff),
4677 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4678 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4679 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4680 BFD_JUMP_TABLE_WRITE (coff),
4681 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4682 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4683
4684 /* Opposite endian version, none exists */
4685 NULL,
4686
4687 & bfd_xcoff_backend_data,
4688 };
4689
4690 /* xcoff-powermac target
4691 Old target.
4692 Only difference between this target and the rs6000 target is the
4693 the default architecture and machine type used in coffcode.h
4694
4695 PowerPC Macs use the same magic numbers as RS/6000
4696 (because that's how they were bootstrapped originally),
4697 but they are always PowerPC architecture. */
4698 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
4699 {
4700 { /* COFF backend, defined in libcoff.h. */
4701 _bfd_xcoff_swap_aux_in,
4702 _bfd_xcoff_swap_sym_in,
4703 coff_swap_lineno_in,
4704 _bfd_xcoff_swap_aux_out,
4705 _bfd_xcoff_swap_sym_out,
4706 coff_swap_lineno_out,
4707 xcoff_swap_reloc_out,
4708 coff_swap_filehdr_out,
4709 coff_swap_aouthdr_out,
4710 coff_swap_scnhdr_out,
4711 FILHSZ,
4712 AOUTSZ,
4713 SCNHSZ,
4714 SYMESZ,
4715 AUXESZ,
4716 RELSZ,
4717 LINESZ,
4718 FILNMLEN,
4719 true, /* _bfd_coff_long_filenames */
4720 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4721 3, /* _bfd_coff_default_section_alignment_power */
4722 false, /* _bfd_coff_force_symnames_in_strings */
4723 2, /* _bfd_coff_debug_string_prefix_length */
4724 32768, /* _bfd_coff_max_nscns */
4725 coff_swap_filehdr_in,
4726 coff_swap_aouthdr_in,
4727 coff_swap_scnhdr_in,
4728 xcoff_swap_reloc_in,
4729 coff_bad_format_hook,
4730 coff_set_arch_mach_hook,
4731 coff_mkobject_hook,
4732 styp_to_sec_flags,
4733 coff_set_alignment_hook,
4734 coff_slurp_symbol_table,
4735 symname_in_debug_hook,
4736 coff_pointerize_aux_hook,
4737 coff_print_aux,
4738 dummy_reloc16_extra_cases,
4739 dummy_reloc16_estimate,
4740 NULL, /* bfd_coff_sym_is_global */
4741 coff_compute_section_file_positions,
4742 NULL, /* _bfd_coff_start_final_link */
4743 xcoff_ppc_relocate_section,
4744 coff_rtype_to_howto,
4745 NULL, /* _bfd_coff_adjust_symndx */
4746 _bfd_generic_link_add_one_symbol,
4747 coff_link_output_has_begun,
4748 coff_final_link_postscript,
4749 NULL /* print_pdata. */
4750 },
4751
4752 0x01DF, /* magic number */
4753 bfd_arch_powerpc,
4754 bfd_mach_ppc,
4755
4756 /* Function pointers to xcoff specific swap routines. */
4757 xcoff_swap_ldhdr_in,
4758 xcoff_swap_ldhdr_out,
4759 xcoff_swap_ldsym_in,
4760 xcoff_swap_ldsym_out,
4761 xcoff_swap_ldrel_in,
4762 xcoff_swap_ldrel_out,
4763
4764 /* Sizes. */
4765 LDHDRSZ,
4766 LDSYMSZ,
4767 LDRELSZ,
4768 12, /* _xcoff_function_descriptor_size */
4769 SMALL_AOUTSZ,
4770
4771 /* Versions. */
4772 1, /* _xcoff_ldhdr_version */
4773
4774 _bfd_xcoff_put_symbol_name,
4775 _bfd_xcoff_put_ldsymbol_name,
4776 &xcoff_dynamic_reloc,
4777 xcoff_create_csect_from_smclas,
4778
4779 /* Lineno and reloc count overflow. */
4780 xcoff_is_lineno_count_overflow,
4781 xcoff_is_reloc_count_overflow,
4782
4783 xcoff_loader_symbol_offset,
4784 xcoff_loader_reloc_offset,
4785
4786 /* glink. */
4787 &xcoff_glink_code[0],
4788 36, /* _xcoff_glink_size */
4789
4790 /* rtinit */
4791 0, /* _xcoff_rtinit_size */
4792 xcoff_generate_rtinit,
4793
4794 /* Stub indirect call. */
4795 &xcoff_stub_indirect_call_code[0],
4796 16, /* _xcoff_stub_indirect_call_size */
4797
4798 /* Stub shared call. */
4799 &xcoff_stub_shared_call_code[0],
4800 24, /* _xcoff_stub_shared_call_size */
4801 };
4802
4803 /* The transfer vector that leads the outside world to all of the above. */
4804 const bfd_target powerpc_xcoff_vec =
4805 {
4806 "xcoff-powermac",
4807 bfd_target_xcoff_flavour,
4808 BFD_ENDIAN_BIG, /* data byte order is big */
4809 BFD_ENDIAN_BIG, /* header byte order is big */
4810
4811 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4812 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4813
4814 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4815 0, /* leading char */
4816 '/', /* ar_pad_char */
4817 15, /* ar_max_namelen */
4818 0, /* match priority. */
4819 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
4820
4821 /* data */
4822 bfd_getb64,
4823 bfd_getb_signed_64,
4824 bfd_putb64,
4825 bfd_getb32,
4826 bfd_getb_signed_32,
4827 bfd_putb32,
4828 bfd_getb16,
4829 bfd_getb_signed_16,
4830 bfd_putb16,
4831
4832 /* hdrs */
4833 bfd_getb64,
4834 bfd_getb_signed_64,
4835 bfd_putb64,
4836 bfd_getb32,
4837 bfd_getb_signed_32,
4838 bfd_putb32,
4839 bfd_getb16,
4840 bfd_getb_signed_16,
4841 bfd_putb16,
4842
4843 { /* bfd_check_format */
4844 _bfd_dummy_target,
4845 coff_object_p,
4846 _bfd_xcoff_archive_p,
4847 CORE_FILE_P
4848 },
4849
4850 { /* bfd_set_format */
4851 _bfd_bool_bfd_false_error,
4852 coff_mkobject,
4853 _bfd_generic_mkarchive,
4854 _bfd_bool_bfd_false_error
4855 },
4856
4857 {/* bfd_write_contents */
4858 _bfd_bool_bfd_false_error,
4859 coff_write_object_contents,
4860 _bfd_xcoff_write_archive_contents,
4861 _bfd_bool_bfd_false_error
4862 },
4863
4864 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4865 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4866 BFD_JUMP_TABLE_CORE (coff),
4867 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4868 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4869 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4870 BFD_JUMP_TABLE_WRITE (coff),
4871 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4872 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4873
4874 /* Opposite endian version, none exists */
4875 NULL,
4876
4877 & bfd_pmac_xcoff_backend_data,
4878 };