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