PR28423, use-after-free in objdump
[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_zeroes = 0;
493 in->x_file.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_fname, ext->x_file.x_n.x_fname, FILNMLEN);
498 break;
499
500 /* RS/6000 "csect" auxents.
501 There is always a CSECT auxiliary entry. But functions can
502 have FCN ones too. In this case, CSECT is always the last
503 one. */
504 case C_EXT:
505 case C_AIX_WEAKEXT:
506 case C_HIDEXT:
507 if (indx + 1 == numaux)
508 {
509 in->x_csect.x_scnlen.l = H_GET_32 (abfd, ext->x_csect.x_scnlen);
510 in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
511 in->x_csect.x_snhash = H_GET_16 (abfd, ext->x_csect.x_snhash);
512 /* We don't have to hack bitfields in x_smtyp because it's
513 defined by shifts-and-ands, which are equivalent on all
514 byte orders. */
515 in->x_csect.x_smtyp = H_GET_8 (abfd, ext->x_csect.x_smtyp);
516 in->x_csect.x_smclas = H_GET_8 (abfd, ext->x_csect.x_smclas);
517 in->x_csect.x_stab = H_GET_32 (abfd, ext->x_csect.x_stab);
518 in->x_csect.x_snstab = H_GET_16 (abfd, ext->x_csect.x_snstab);
519 }
520 else
521 {
522 /* x_exptr isn't supported. */
523 in->x_sym.x_misc.x_fsize
524 = H_GET_32 (abfd, ext->x_fcn.x_fsize);
525 in->x_sym.x_fcnary.x_fcn.x_lnnoptr
526 = H_GET_32 (abfd, ext->x_fcn.x_lnnoptr);
527 in->x_sym.x_fcnary.x_fcn.x_endndx.l
528 = H_GET_32 (abfd, ext->x_fcn.x_endndx);
529 }
530 break;
531
532 case C_STAT:
533 in->x_scn.x_scnlen = H_GET_32 (abfd, ext->x_scn.x_scnlen);
534 in->x_scn.x_nreloc = H_GET_16 (abfd, ext->x_scn.x_nreloc);
535 in->x_scn.x_nlinno = H_GET_16 (abfd, ext->x_scn.x_nlinno);
536 /* PE defines some extra fields; we zero them out for
537 safety. */
538 in->x_scn.x_checksum = 0;
539 in->x_scn.x_associated = 0;
540 in->x_scn.x_comdat = 0;
541 break;
542
543 case C_BLOCK:
544 case C_FCN:
545 in->x_sym.x_misc.x_lnsz.x_lnno
546 = H_GET_32 (abfd, ext->x_sym.x_lnno);
547 break;
548
549 case C_DWARF:
550 in->x_sect.x_scnlen = H_GET_32 (abfd, ext->x_sect.x_scnlen);
551 in->x_sect.x_nreloc = H_GET_32 (abfd, ext->x_sect.x_nreloc);
552 break;
553
554 }
555 }
556
557 unsigned int
558 _bfd_xcoff_swap_aux_out (bfd *abfd, void * inp, int type ATTRIBUTE_UNUSED,
559 int in_class, int indx, int numaux, void * extp)
560 {
561 union internal_auxent *in = (union internal_auxent *)inp;
562 AUXENT *ext = (AUXENT *)extp;
563
564 memset (ext, 0, bfd_coff_auxesz (abfd));
565 switch (in_class)
566 {
567 default:
568 _bfd_error_handler
569 /* xgettext: c-format */
570 (_("%pB: unsupported swap_aux_out for storage class %#x"),
571 abfd, (unsigned int) in_class);
572 bfd_set_error (bfd_error_bad_value);
573 break;
574
575 case C_FILE:
576 if (in->x_file.x_fname[0] == 0)
577 {
578 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_n.x_zeroes);
579 H_PUT_32 (abfd, in->x_file.x_n.x_offset,
580 ext->x_file.x_n.x_n.x_offset);
581 }
582 else
583 memcpy (ext->x_file.x_n.x_fname, in->x_file.x_fname, FILNMLEN);
584 break;
585
586 /* RS/6000 "csect" auxents */
587 case C_EXT:
588 case C_AIX_WEAKEXT:
589 case C_HIDEXT:
590 if (indx + 1 == numaux)
591 {
592 H_PUT_32 (abfd, in->x_csect.x_scnlen.l, ext->x_csect.x_scnlen);
593 H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
594 H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
595 /* We don't have to hack bitfields in x_smtyp because it's
596 defined by shifts-and-ands, which are equivalent on all
597 byte orders. */
598 H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
599 H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
600 H_PUT_32 (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
601 H_PUT_16 (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
602 }
603 else
604 {
605 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_fcn.x_fsize);
606 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
607 ext->x_fcn.x_lnnoptr);
608 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
609 ext->x_fcn.x_endndx);
610 }
611 break;
612
613 case C_STAT:
614 H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen);
615 H_PUT_16 (abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc);
616 H_PUT_16 (abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno);
617 break;
618
619 case C_BLOCK:
620 case C_FCN:
621 H_PUT_32 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext->x_sym.x_lnno);
622 break;
623
624 case C_DWARF:
625 H_PUT_32 (abfd, in->x_sect.x_scnlen, ext->x_sect.x_scnlen);
626 H_PUT_32 (abfd, in->x_sect.x_nreloc, ext->x_sect.x_nreloc);
627 break;
628 }
629
630 return bfd_coff_auxesz (abfd);
631 }
632 \f
633 /* The XCOFF reloc table.
634 XCOFF relocations aren't defined only by the type field r_type.
635 The bitsize and whether they are signed or not, are defined by
636 r_size field. Thus, it's complicated to create a constant
637 table reference every possible relocation.
638 This table contains the "default" relocation and few modified
639 relocations what were already there. It's enough when
640 xcoff_rtype2howto is called.
641 For relocations from an input bfd to an output bfd, the default
642 relocation is retrieved and when manually adapted.
643
644 For now, it seems to be enought. */
645
646 reloc_howto_type xcoff_howto_table[] =
647 {
648 /* 0x00: Standard 32 bit relocation. */
649 HOWTO (R_POS, /* type */
650 0, /* rightshift */
651 2, /* size (0 = byte, 1 = short, 2 = long) */
652 32, /* bitsize */
653 false, /* pc_relative */
654 0, /* bitpos */
655 complain_overflow_bitfield, /* complain_on_overflow */
656 0, /* special_function */
657 "R_POS", /* name */
658 true, /* partial_inplace */
659 0xffffffff, /* src_mask */
660 0xffffffff, /* dst_mask */
661 false), /* pcrel_offset */
662
663 /* 0x01: 32 bit relocation, but store negative value. */
664 HOWTO (R_NEG, /* type */
665 0, /* rightshift */
666 -2, /* size (0 = byte, 1 = short, 2 = long) */
667 32, /* bitsize */
668 false, /* pc_relative */
669 0, /* bitpos */
670 complain_overflow_bitfield, /* complain_on_overflow */
671 0, /* special_function */
672 "R_NEG", /* name */
673 true, /* partial_inplace */
674 0xffffffff, /* src_mask */
675 0xffffffff, /* dst_mask */
676 false), /* pcrel_offset */
677
678 /* 0x02: 32 bit PC relative relocation. */
679 HOWTO (R_REL, /* type */
680 0, /* rightshift */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
682 32, /* bitsize */
683 true, /* pc_relative */
684 0, /* bitpos */
685 complain_overflow_signed, /* complain_on_overflow */
686 0, /* special_function */
687 "R_REL", /* name */
688 true, /* partial_inplace */
689 0xffffffff, /* src_mask */
690 0xffffffff, /* dst_mask */
691 false), /* pcrel_offset */
692
693 /* 0x03: 16 bit TOC relative relocation. */
694 HOWTO (R_TOC, /* type */
695 0, /* rightshift */
696 1, /* size (0 = byte, 1 = short, 2 = long) */
697 16, /* bitsize */
698 false, /* pc_relative */
699 0, /* bitpos */
700 complain_overflow_bitfield, /* complain_on_overflow */
701 0, /* special_function */
702 "R_TOC", /* name */
703 true, /* partial_inplace */
704 0, /* src_mask */
705 0xffff, /* dst_mask */
706 false), /* pcrel_offset */
707
708 /* 0x04: Same as R_TOC */
709 HOWTO (R_TRL, /* type */
710 0, /* rightshift */
711 1, /* size (0 = byte, 1 = short, 2 = long) */
712 16, /* bitsize */
713 false, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_bitfield, /* complain_on_overflow */
716 0, /* special_function */
717 "R_TRL", /* name */
718 true, /* partial_inplace */
719 0, /* src_mask */
720 0xffff, /* dst_mask */
721 false), /* pcrel_offset */
722
723 /* 0x05: External TOC relative symbol. */
724 HOWTO (R_GL, /* type */
725 0, /* rightshift */
726 1, /* size (0 = byte, 1 = short, 2 = long) */
727 16, /* bitsize */
728 false, /* pc_relative */
729 0, /* bitpos */
730 complain_overflow_bitfield, /* complain_on_overflow */
731 0, /* special_function */
732 "R_GL", /* name */
733 true, /* partial_inplace */
734 0, /* src_mask */
735 0xffff, /* dst_mask */
736 false), /* pcrel_offset */
737
738 /* 0x06: Local TOC relative symbol. */
739 HOWTO (R_TCL, /* type */
740 0, /* rightshift */
741 1, /* size (0 = byte, 1 = short, 2 = long) */
742 16, /* bitsize */
743 false, /* pc_relative */
744 0, /* bitpos */
745 complain_overflow_bitfield, /* complain_on_overflow */
746 0, /* special_function */
747 "R_TCL", /* name */
748 true, /* partial_inplace */
749 0, /* src_mask */
750 0xffff, /* dst_mask */
751 false), /* pcrel_offset */
752
753 EMPTY_HOWTO (7),
754
755 /* 0x08: Same as R_RBA. */
756 HOWTO (R_BA, /* type */
757 0, /* rightshift */
758 2, /* size (0 = byte, 1 = short, 2 = long) */
759 26, /* bitsize */
760 false, /* pc_relative */
761 0, /* bitpos */
762 complain_overflow_bitfield, /* complain_on_overflow */
763 0, /* special_function */
764 "R_BA_26", /* name */
765 true, /* partial_inplace */
766 0x03fffffc, /* src_mask */
767 0x03fffffc, /* dst_mask */
768 false), /* pcrel_offset */
769
770 EMPTY_HOWTO (9),
771
772 /* 0x0a: Same as R_RBR. */
773 HOWTO (R_BR, /* type */
774 0, /* rightshift */
775 2, /* size (0 = byte, 1 = short, 2 = long) */
776 26, /* bitsize */
777 true, /* pc_relative */
778 0, /* bitpos */
779 complain_overflow_signed, /* complain_on_overflow */
780 0, /* special_function */
781 "R_BR", /* name */
782 true, /* partial_inplace */
783 0x03fffffc, /* src_mask */
784 0x03fffffc, /* dst_mask */
785 false), /* pcrel_offset */
786
787 EMPTY_HOWTO (0xb),
788
789 /* 0x0c: Same as R_POS. */
790 HOWTO (R_RL, /* type */
791 0, /* rightshift */
792 2, /* size (0 = byte, 1 = short, 2 = long) */
793 32, /* bitsize */
794 false, /* pc_relative */
795 0, /* bitpos */
796 complain_overflow_bitfield, /* complain_on_overflow */
797 0, /* special_function */
798 "R_RL", /* name */
799 true, /* partial_inplace */
800 0xffffffff, /* src_mask */
801 0xffffffff, /* dst_mask */
802 false), /* pcrel_offset */
803
804 /* 0x0d: Same as R_POS. */
805 HOWTO (R_RLA, /* type */
806 0, /* rightshift */
807 2, /* size (0 = byte, 1 = short, 2 = long) */
808 32, /* bitsize */
809 false, /* pc_relative */
810 0, /* bitpos */
811 complain_overflow_bitfield, /* complain_on_overflow */
812 0, /* special_function */
813 "R_RLA", /* name */
814 true, /* partial_inplace */
815 0xffffffff, /* src_mask */
816 0xffffffff, /* dst_mask */
817 false), /* pcrel_offset */
818
819 EMPTY_HOWTO (0xe),
820
821 /* 0x0f: Non-relocating reference. Bitsize is 1 so that r_rsize is 0. */
822 HOWTO (R_REF, /* type */
823 0, /* rightshift */
824 0, /* size (0 = byte, 1 = short, 2 = long) */
825 1, /* bitsize */
826 false, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 0, /* special_function */
830 "R_REF", /* name */
831 false, /* partial_inplace */
832 0, /* src_mask */
833 0, /* dst_mask */
834 false), /* pcrel_offset */
835
836 EMPTY_HOWTO (0x10),
837 EMPTY_HOWTO (0x11),
838 EMPTY_HOWTO (0x12),
839
840 /* 0x13: Same as R_TOC. */
841 HOWTO (R_TRLA, /* type */
842 0, /* rightshift */
843 1, /* size (0 = byte, 1 = short, 2 = long) */
844 16, /* bitsize */
845 false, /* pc_relative */
846 0, /* bitpos */
847 complain_overflow_bitfield, /* complain_on_overflow */
848 0, /* special_function */
849 "R_TRLA", /* name */
850 true, /* partial_inplace */
851 0, /* src_mask */
852 0xffff, /* dst_mask */
853 false), /* pcrel_offset */
854
855 /* 0x14: Modifiable relative branch. */
856 HOWTO (R_RRTBI, /* type */
857 1, /* rightshift */
858 2, /* size (0 = byte, 1 = short, 2 = long) */
859 32, /* bitsize */
860 false, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_bitfield, /* complain_on_overflow */
863 0, /* special_function */
864 "R_RRTBI", /* name */
865 true, /* partial_inplace */
866 0xffffffff, /* src_mask */
867 0xffffffff, /* dst_mask */
868 false), /* pcrel_offset */
869
870 /* 0x15: Modifiable absolute branch. */
871 HOWTO (R_RRTBA, /* type */
872 1, /* rightshift */
873 2, /* size (0 = byte, 1 = short, 2 = long) */
874 32, /* bitsize */
875 false, /* pc_relative */
876 0, /* bitpos */
877 complain_overflow_bitfield, /* complain_on_overflow */
878 0, /* special_function */
879 "R_RRTBA", /* name */
880 true, /* partial_inplace */
881 0xffffffff, /* src_mask */
882 0xffffffff, /* dst_mask */
883 false), /* pcrel_offset */
884
885 /* 0x16: Modifiable call absolute indirect. */
886 HOWTO (R_CAI, /* type */
887 0, /* rightshift */
888 1, /* size (0 = byte, 1 = short, 2 = long) */
889 16, /* bitsize */
890 false, /* pc_relative */
891 0, /* bitpos */
892 complain_overflow_bitfield, /* complain_on_overflow */
893 0, /* special_function */
894 "R_CAI", /* name */
895 true, /* partial_inplace */
896 0xffff, /* src_mask */
897 0xffff, /* dst_mask */
898 false), /* pcrel_offset */
899
900 /* 0x17: Modifiable call relative. */
901 HOWTO (R_CREL, /* type */
902 0, /* rightshift */
903 1, /* size (0 = byte, 1 = short, 2 = long) */
904 16, /* bitsize */
905 false, /* pc_relative */
906 0, /* bitpos */
907 complain_overflow_bitfield, /* complain_on_overflow */
908 0, /* special_function */
909 "R_CREL", /* name */
910 true, /* partial_inplace */
911 0xffff, /* src_mask */
912 0xffff, /* dst_mask */
913 false), /* pcrel_offset */
914
915 /* 0x18: Modifiable branch absolute. */
916 HOWTO (R_RBA, /* type */
917 0, /* rightshift */
918 2, /* size (0 = byte, 1 = short, 2 = long) */
919 26, /* bitsize */
920 false, /* pc_relative */
921 0, /* bitpos */
922 complain_overflow_bitfield, /* complain_on_overflow */
923 0, /* special_function */
924 "R_RBA", /* name */
925 true, /* partial_inplace */
926 0x03fffffc, /* src_mask */
927 0x03fffffc, /* dst_mask */
928 false), /* pcrel_offset */
929
930 /* 0x19: Modifiable branch absolute. */
931 HOWTO (R_RBAC, /* type */
932 0, /* rightshift */
933 2, /* size (0 = byte, 1 = short, 2 = long) */
934 32, /* bitsize */
935 false, /* pc_relative */
936 0, /* bitpos */
937 complain_overflow_bitfield, /* complain_on_overflow */
938 0, /* special_function */
939 "R_RBAC", /* name */
940 true, /* partial_inplace */
941 0xffffffff, /* src_mask */
942 0xffffffff, /* dst_mask */
943 false), /* pcrel_offset */
944
945 /* 0x1a: Modifiable branch relative. */
946 HOWTO (R_RBR, /* type */
947 0, /* rightshift */
948 2, /* size (0 = byte, 1 = short, 2 = long) */
949 26, /* bitsize */
950 false, /* pc_relative */
951 0, /* bitpos */
952 complain_overflow_signed, /* complain_on_overflow */
953 0, /* special_function */
954 "R_RBR_26", /* name */
955 true, /* partial_inplace */
956 0x03fffffc, /* src_mask */
957 0x03fffffc, /* dst_mask */
958 false), /* pcrel_offset */
959
960 /* 0x1b: Modifiable branch absolute. */
961 HOWTO (R_RBRC, /* type */
962 0, /* rightshift */
963 1, /* size (0 = byte, 1 = short, 2 = long) */
964 16, /* bitsize */
965 false, /* pc_relative */
966 0, /* bitpos */
967 complain_overflow_bitfield, /* complain_on_overflow */
968 0, /* special_function */
969 "R_RBRC", /* name */
970 true, /* partial_inplace */
971 0xffff, /* src_mask */
972 0xffff, /* dst_mask */
973 false), /* pcrel_offset */
974
975 /* 0x1c: 16 bit Non modifiable absolute branch. */
976 HOWTO (R_BA, /* type */
977 0, /* rightshift */
978 1, /* size (0 = byte, 1 = short, 2 = long) */
979 16, /* bitsize */
980 false, /* pc_relative */
981 0, /* bitpos */
982 complain_overflow_bitfield, /* complain_on_overflow */
983 0, /* special_function */
984 "R_BA_16", /* name */
985 true, /* partial_inplace */
986 0xfffc, /* src_mask */
987 0xfffc, /* dst_mask */
988 false), /* pcrel_offset */
989
990 /* 0x1d: Modifiable branch relative. */
991 HOWTO (R_RBR, /* type */
992 0, /* rightshift */
993 1, /* size (0 = byte, 1 = short, 2 = long) */
994 16, /* bitsize */
995 true, /* pc_relative */
996 0, /* bitpos */
997 complain_overflow_signed, /* complain_on_overflow */
998 0, /* special_function */
999 "R_RBR_16", /* name */
1000 true, /* partial_inplace */
1001 0xfffc, /* src_mask */
1002 0xfffc, /* dst_mask */
1003 false), /* pcrel_offset */
1004
1005 /* 0x1e: Modifiable branch relative. */
1006 HOWTO (R_RBA, /* type */
1007 0, /* rightshift */
1008 1, /* size (0 = byte, 1 = short, 2 = long) */
1009 16, /* bitsize */
1010 false, /* pc_relative */
1011 0, /* bitpos */
1012 complain_overflow_signed, /* complain_on_overflow */
1013 0, /* special_function */
1014 "R_RBA_16", /* name */
1015 true, /* partial_inplace */
1016 0xffff, /* src_mask */
1017 0xffff, /* dst_mask */
1018 false), /* pcrel_offset */
1019
1020 EMPTY_HOWTO (0x1f),
1021
1022 /* 0x20: General-dynamic TLS relocation. */
1023 HOWTO (R_TLS, /* type */
1024 0, /* rightshift */
1025 2, /* size (0 = byte, 1 = short, 2 = long) */
1026 32, /* bitsize */
1027 false, /* pc_relative */
1028 0, /* bitpos */
1029 complain_overflow_bitfield, /* complain_on_overflow */
1030 0, /* special_function */
1031 "R_TLS", /* name */
1032 true, /* partial_inplace */
1033 0xffffffff, /* src_mask */
1034 0xffffffff, /* dst_mask */
1035 false), /* pcrel_offset */
1036
1037 /* 0x21: Initial-exec TLS relocation. */
1038 HOWTO (R_TLS_IE, /* type */
1039 0, /* rightshift */
1040 2, /* size (0 = byte, 1 = short, 2 = long) */
1041 32, /* bitsize */
1042 false, /* pc_relative */
1043 0, /* bitpos */
1044 complain_overflow_bitfield, /* complain_on_overflow */
1045 0, /* special_function */
1046 "R_TLS_IE", /* name */
1047 true, /* partial_inplace */
1048 0xffffffff, /* src_mask */
1049 0xffffffff, /* dst_mask */
1050 false), /* pcrel_offset */
1051
1052 /* 0x22: Local-dynamic TLS relocation. */
1053 HOWTO (R_TLS_LD, /* type */
1054 0, /* rightshift */
1055 2, /* size (0 = byte, 1 = short, 2 = long) */
1056 32, /* bitsize */
1057 false, /* pc_relative */
1058 0, /* bitpos */
1059 complain_overflow_bitfield, /* complain_on_overflow */
1060 0, /* special_function */
1061 "R_TLS_LD", /* name */
1062 true, /* partial_inplace */
1063 0xffffffff, /* src_mask */
1064 0xffffffff, /* dst_mask */
1065 false), /* pcrel_offset */
1066
1067 /* 0x23: Local-exec TLS relocation. */
1068 HOWTO (R_TLS_LE, /* type */
1069 0, /* rightshift */
1070 2, /* size (0 = byte, 1 = short, 2 = long) */
1071 32, /* bitsize */
1072 false, /* pc_relative */
1073 0, /* bitpos */
1074 complain_overflow_bitfield, /* complain_on_overflow */
1075 0, /* special_function */
1076 "R_TLS_LE", /* name */
1077 true, /* partial_inplace */
1078 0xffffffff, /* src_mask */
1079 0xffffffff, /* dst_mask */
1080 false), /* pcrel_offset */
1081
1082 /* 0x24: TLS relocation. */
1083 HOWTO (R_TLSM, /* type */
1084 0, /* rightshift */
1085 2, /* size (0 = byte, 1 = short, 2 = long) */
1086 32, /* bitsize */
1087 false, /* pc_relative */
1088 0, /* bitpos */
1089 complain_overflow_bitfield, /* complain_on_overflow */
1090 0, /* special_function */
1091 "R_TLSM", /* name */
1092 true, /* partial_inplace */
1093 0xffffffff, /* src_mask */
1094 0xffffffff, /* dst_mask */
1095 false), /* pcrel_offset */
1096
1097
1098 /* 0x25: TLS module relocation. */
1099 HOWTO (R_TLSML, /* type */
1100 0, /* rightshift */
1101 2, /* size (0 = byte, 1 = short, 2 = long) */
1102 32, /* bitsize */
1103 false, /* pc_relative */
1104 0, /* bitpos */
1105 complain_overflow_bitfield, /* complain_on_overflow */
1106 0, /* special_function */
1107 "R_TLSM", /* name */
1108 true, /* partial_inplace */
1109 0xffffffff, /* src_mask */
1110 0xffffffff, /* dst_mask */
1111 false), /* pcrel_offset */
1112
1113 EMPTY_HOWTO(0x26),
1114 EMPTY_HOWTO(0x27),
1115 EMPTY_HOWTO(0x28),
1116 EMPTY_HOWTO(0x29),
1117 EMPTY_HOWTO(0x2a),
1118 EMPTY_HOWTO(0x2b),
1119 EMPTY_HOWTO(0x2c),
1120 EMPTY_HOWTO(0x2d),
1121 EMPTY_HOWTO(0x2e),
1122 EMPTY_HOWTO(0x2f),
1123
1124 /* 0x30: High-order 16 bit TOC relative relocation. */
1125 HOWTO (R_TOCU, /* type */
1126 16, /* rightshift */
1127 1, /* size (0 = byte, 1 = short, 2 = long) */
1128 16, /* bitsize */
1129 false, /* pc_relative */
1130 0, /* bitpos */
1131 complain_overflow_bitfield, /* complain_on_overflow */
1132 0, /* special_function */
1133 "R_TOCU", /* name */
1134 true, /* partial_inplace */
1135 0, /* src_mask */
1136 0xffff, /* dst_mask */
1137 false), /* pcrel_offset */
1138
1139 /* 0x31: Low-order 16 bit TOC relative relocation. */
1140 HOWTO (R_TOCL, /* type */
1141 0, /* rightshift */
1142 1, /* size (0 = byte, 1 = short, 2 = long) */
1143 16, /* bitsize */
1144 false, /* pc_relative */
1145 0, /* bitpos */
1146 complain_overflow_dont, /* complain_on_overflow */
1147 0, /* special_function */
1148 "R_TOCL", /* name */
1149 true, /* partial_inplace */
1150 0, /* src_mask */
1151 0xffff, /* dst_mask */
1152 false), /* pcrel_offset */
1153
1154 };
1155
1156 void
1157 xcoff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1158 {
1159 if (internal->r_type > R_TOCL)
1160 abort ();
1161
1162 /* Default howto layout works most of the time */
1163 relent->howto = &xcoff_howto_table[internal->r_type];
1164
1165 /* Special case some 16 bit reloc */
1166 if (15 == (internal->r_size & 0x1f))
1167 {
1168 if (R_BA == internal->r_type)
1169 relent->howto = &xcoff_howto_table[0x1c];
1170 else if (R_RBR == internal->r_type)
1171 relent->howto = &xcoff_howto_table[0x1d];
1172 else if (R_RBA == internal->r_type)
1173 relent->howto = &xcoff_howto_table[0x1e];
1174 }
1175
1176 /* The r_size field of an XCOFF reloc encodes the bitsize of the
1177 relocation, as well as indicating whether it is signed or not.
1178 Doublecheck that the relocation information gathered from the
1179 type matches this information. The bitsize is not significant
1180 for R_REF relocs. */
1181 if (relent->howto->dst_mask != 0
1182 && (relent->howto->bitsize
1183 != ((unsigned int) internal->r_size & 0x1f) + 1))
1184 abort ();
1185 }
1186
1187 reloc_howto_type *
1188 _bfd_xcoff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1189 bfd_reloc_code_real_type code)
1190 {
1191 switch (code)
1192 {
1193 case BFD_RELOC_PPC_B26:
1194 return &xcoff_howto_table[0xa];
1195 case BFD_RELOC_PPC_BA16:
1196 return &xcoff_howto_table[0x1c];
1197 case BFD_RELOC_PPC_BA26:
1198 return &xcoff_howto_table[8];
1199 case BFD_RELOC_PPC_TOC16:
1200 return &xcoff_howto_table[3];
1201 case BFD_RELOC_PPC_TOC16_HI:
1202 return &xcoff_howto_table[0x30];
1203 case BFD_RELOC_PPC_TOC16_LO:
1204 return &xcoff_howto_table[0x31];
1205 case BFD_RELOC_PPC_B16:
1206 return &xcoff_howto_table[0x1d];
1207 case BFD_RELOC_32:
1208 case BFD_RELOC_CTOR:
1209 return &xcoff_howto_table[0];
1210 case BFD_RELOC_NONE:
1211 return &xcoff_howto_table[0xf];
1212 case BFD_RELOC_PPC_NEG:
1213 return &xcoff_howto_table[0x1];
1214 case BFD_RELOC_PPC_TLSGD:
1215 return &xcoff_howto_table[0x20];
1216 case BFD_RELOC_PPC_TLSIE:
1217 return &xcoff_howto_table[0x21];
1218 case BFD_RELOC_PPC_TLSLD:
1219 return &xcoff_howto_table[0x22];
1220 case BFD_RELOC_PPC_TLSLE:
1221 return &xcoff_howto_table[0x23];
1222 case BFD_RELOC_PPC_TLSM:
1223 return &xcoff_howto_table[0x24];
1224 case BFD_RELOC_PPC_TLSML:
1225 return &xcoff_howto_table[0x25];
1226 default:
1227 return NULL;
1228 }
1229 }
1230
1231 static reloc_howto_type *
1232 _bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1233 const char *r_name)
1234 {
1235 unsigned int i;
1236
1237 for (i = 0;
1238 i < sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]);
1239 i++)
1240 if (xcoff_howto_table[i].name != NULL
1241 && strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
1242 return &xcoff_howto_table[i];
1243
1244 return NULL;
1245 }
1246 \f
1247 /* XCOFF archive support. The original version of this code was by
1248 Damon A. Permezel. It was enhanced to permit cross support, and
1249 writing archive files, by Ian Lance Taylor, Cygnus Support.
1250
1251 XCOFF uses its own archive format. Everything is hooked together
1252 with file offset links, so it is possible to rapidly update an
1253 archive in place. Of course, we don't do that. An XCOFF archive
1254 has a real file header, not just an ARMAG string. The structure of
1255 the file header and of each archive header appear below.
1256
1257 An XCOFF archive also has a member table, which is a list of
1258 elements in the archive (you can get that by looking through the
1259 linked list, but you have to read a lot more of the file). The
1260 member table has a normal archive header with an empty name. It is
1261 normally (and perhaps must be) the second to last entry in the
1262 archive. The member table data is almost printable ASCII. It
1263 starts with a 12 character decimal string which is the number of
1264 entries in the table. For each entry it has a 12 character decimal
1265 string which is the offset in the archive of that member. These
1266 entries are followed by a series of null terminated strings which
1267 are the member names for each entry.
1268
1269 Finally, an XCOFF archive has a global symbol table, which is what
1270 we call the armap. The global symbol table has a normal archive
1271 header with an empty name. It is normally (and perhaps must be)
1272 the last entry in the archive. The contents start with a four byte
1273 binary number which is the number of entries. This is followed by
1274 a that many four byte binary numbers; each is the file offset of an
1275 entry in the archive. These numbers are followed by a series of
1276 null terminated strings, which are symbol names.
1277
1278 AIX 4.3 introduced a new archive format which can handle larger
1279 files and also 32- and 64-bit objects in the same archive. The
1280 things said above remain true except that there is now more than
1281 one global symbol table. The one is used to index 32-bit objects,
1282 the other for 64-bit objects.
1283
1284 The new archives (recognizable by the new ARMAG string) has larger
1285 field lengths so that we cannot really share any code. Also we have
1286 to take care that we are not generating the new form of archives
1287 on AIX 4.2 or earlier systems. */
1288
1289 /* PR 21786: The PE/COFF standard does not require NUL termination for any of
1290 the ASCII fields in the archive headers. So in order to be able to extract
1291 numerical values we provide our own versions of strtol and strtoll which
1292 take a maximum length as an additional parameter. Also - just to save space,
1293 we omit the endptr return parameter, since we know that it is never used. */
1294
1295 static long
1296 _bfd_strntol (const char * nptr, int base, unsigned int maxlen)
1297 {
1298 char buf[24]; /* Should be enough. */
1299
1300 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1301
1302 memcpy (buf, nptr, maxlen);
1303 buf[maxlen] = 0;
1304 return strtol (buf, NULL, base);
1305 }
1306
1307 static long long
1308 _bfd_strntoll (const char * nptr, int base, unsigned int maxlen)
1309 {
1310 char buf[32]; /* Should be enough. */
1311
1312 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1313
1314 memcpy (buf, nptr, maxlen);
1315 buf[maxlen] = 0;
1316 return strtoll (buf, NULL, base);
1317 }
1318
1319 /* Macro to read an ASCII value stored in an archive header field. */
1320 #define GET_VALUE_IN_FIELD(VAR, FIELD, BASE) \
1321 do \
1322 { \
1323 (VAR) = (sizeof (VAR) > sizeof (long) \
1324 ? _bfd_strntoll (FIELD, BASE, sizeof FIELD) \
1325 : _bfd_strntol (FIELD, BASE, sizeof FIELD)); \
1326 } \
1327 while (0)
1328
1329 #define EQ_VALUE_IN_FIELD(VAR, FIELD, BASE) \
1330 (sizeof (VAR) > sizeof (long) \
1331 ? (VAR) == _bfd_strntoll (FIELD, BASE, sizeof FIELD) \
1332 : (VAR) == _bfd_strntol (FIELD, BASE, sizeof FIELD))
1333
1334 /* Read in the armap of an XCOFF archive. */
1335
1336 bool
1337 _bfd_xcoff_slurp_armap (bfd *abfd)
1338 {
1339 file_ptr off;
1340 size_t namlen;
1341 bfd_size_type sz;
1342 bfd_byte *contents, *cend;
1343 bfd_vma c, i;
1344 carsym *arsym;
1345 bfd_byte *p;
1346
1347 if (xcoff_ardata (abfd) == NULL)
1348 {
1349 abfd->has_armap = false;
1350 return true;
1351 }
1352
1353 if (! xcoff_big_format_p (abfd))
1354 {
1355 /* This is for the old format. */
1356 struct xcoff_ar_hdr hdr;
1357
1358 GET_VALUE_IN_FIELD (off, xcoff_ardata (abfd)->symoff, 10);
1359 if (off == 0)
1360 {
1361 abfd->has_armap = false;
1362 return true;
1363 }
1364
1365 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1366 return false;
1367
1368 /* The symbol table starts with a normal archive header. */
1369 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1370 != SIZEOF_AR_HDR)
1371 return false;
1372
1373 /* Skip the name (normally empty). */
1374 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1375 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1376 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1377 return false;
1378
1379 GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1380 if (sz + 1 < 5)
1381 {
1382 bfd_set_error (bfd_error_bad_value);
1383 return false;
1384 }
1385
1386 /* Read in the entire symbol table. */
1387 contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
1388 if (contents == NULL)
1389 return false;
1390
1391 /* Ensure strings are NULL terminated so we don't wander off the
1392 end of the buffer. */
1393 contents[sz] = 0;
1394
1395 /* The symbol table starts with a four byte count. */
1396 c = H_GET_32 (abfd, contents);
1397
1398 if (c >= sz / 4)
1399 {
1400 bfd_set_error (bfd_error_bad_value);
1401 return false;
1402 }
1403
1404 bfd_ardata (abfd)->symdefs =
1405 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1406 if (bfd_ardata (abfd)->symdefs == NULL)
1407 return false;
1408
1409 /* After the count comes a list of four byte file offsets. */
1410 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1411 i < c;
1412 ++i, ++arsym, p += 4)
1413 arsym->file_offset = H_GET_32 (abfd, p);
1414 }
1415 else
1416 {
1417 /* This is for the new format. */
1418 struct xcoff_ar_hdr_big hdr;
1419
1420 GET_VALUE_IN_FIELD (off, xcoff_ardata_big (abfd)->symoff, 10);
1421 if (off == 0)
1422 {
1423 abfd->has_armap = false;
1424 return true;
1425 }
1426
1427 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1428 return false;
1429
1430 /* The symbol table starts with a normal archive header. */
1431 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1432 != SIZEOF_AR_HDR_BIG)
1433 return false;
1434
1435 /* Skip the name (normally empty). */
1436 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1437 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1438 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1439 return false;
1440
1441 GET_VALUE_IN_FIELD (sz, hdr.size, 10);
1442 if (sz + 1 < 9)
1443 {
1444 bfd_set_error (bfd_error_bad_value);
1445 return false;
1446 }
1447
1448 /* Read in the entire symbol table. */
1449 contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
1450 if (contents == NULL)
1451 return false;
1452
1453 /* Ensure strings are NULL terminated so we don't wander off the
1454 end of the buffer. */
1455 contents[sz] = 0;
1456
1457 /* The symbol table starts with an eight byte count. */
1458 c = H_GET_64 (abfd, contents);
1459
1460 if (c >= sz / 8)
1461 {
1462 bfd_set_error (bfd_error_bad_value);
1463 return false;
1464 }
1465
1466 bfd_ardata (abfd)->symdefs =
1467 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1468 if (bfd_ardata (abfd)->symdefs == NULL)
1469 return false;
1470
1471 /* After the count comes a list of eight byte file offsets. */
1472 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1473 i < c;
1474 ++i, ++arsym, p += 8)
1475 arsym->file_offset = H_GET_64 (abfd, p);
1476 }
1477
1478 /* After the file offsets come null terminated symbol names. */
1479 cend = contents + sz;
1480 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1481 i < c;
1482 ++i, ++arsym, p += strlen ((char *) p) + 1)
1483 {
1484 if (p >= cend)
1485 {
1486 bfd_set_error (bfd_error_bad_value);
1487 return false;
1488 }
1489 arsym->name = (char *) p;
1490 }
1491
1492 bfd_ardata (abfd)->symdef_count = c;
1493 abfd->has_armap = true;
1494
1495 return true;
1496 }
1497
1498 /* See if this is an XCOFF archive. */
1499
1500 bfd_cleanup
1501 _bfd_xcoff_archive_p (bfd *abfd)
1502 {
1503 struct artdata *tdata_hold;
1504 char magic[SXCOFFARMAG];
1505 size_t amt = SXCOFFARMAG;
1506
1507 if (bfd_bread (magic, amt, abfd) != amt)
1508 {
1509 if (bfd_get_error () != bfd_error_system_call)
1510 bfd_set_error (bfd_error_wrong_format);
1511 return NULL;
1512 }
1513
1514 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1515 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1516 {
1517 bfd_set_error (bfd_error_wrong_format);
1518 return NULL;
1519 }
1520
1521 tdata_hold = bfd_ardata (abfd);
1522
1523 amt = sizeof (struct artdata);
1524 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
1525 if (bfd_ardata (abfd) == (struct artdata *) NULL)
1526 goto error_ret_restore;
1527
1528 /* Cleared by bfd_zalloc above.
1529 bfd_ardata (abfd)->cache = NULL;
1530 bfd_ardata (abfd)->archive_head = NULL;
1531 bfd_ardata (abfd)->symdefs = NULL;
1532 bfd_ardata (abfd)->extended_names = NULL;
1533 bfd_ardata (abfd)->extended_names_size = 0; */
1534
1535 /* Now handle the two formats. */
1536 if (magic[1] != 'b')
1537 {
1538 /* This is the old format. */
1539 struct xcoff_ar_file_hdr hdr;
1540
1541 /* Copy over the magic string. */
1542 memcpy (hdr.magic, magic, SXCOFFARMAG);
1543
1544 /* Now read the rest of the file header. */
1545 amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
1546 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1547 {
1548 if (bfd_get_error () != bfd_error_system_call)
1549 bfd_set_error (bfd_error_wrong_format);
1550 goto error_ret;
1551 }
1552
1553 GET_VALUE_IN_FIELD (bfd_ardata (abfd)->first_file_filepos,
1554 hdr.firstmemoff, 10);
1555
1556 amt = SIZEOF_AR_FILE_HDR;
1557 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1558 if (bfd_ardata (abfd)->tdata == NULL)
1559 goto error_ret;
1560
1561 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1562 }
1563 else
1564 {
1565 /* This is the new format. */
1566 struct xcoff_ar_file_hdr_big hdr;
1567
1568 /* Copy over the magic string. */
1569 memcpy (hdr.magic, magic, SXCOFFARMAG);
1570
1571 /* Now read the rest of the file header. */
1572 amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
1573 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1574 {
1575 if (bfd_get_error () != bfd_error_system_call)
1576 bfd_set_error (bfd_error_wrong_format);
1577 goto error_ret;
1578 }
1579
1580 bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
1581 (const char **) 0,
1582 10);
1583
1584 amt = SIZEOF_AR_FILE_HDR_BIG;
1585 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1586 if (bfd_ardata (abfd)->tdata == NULL)
1587 goto error_ret;
1588
1589 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1590 }
1591
1592 if (! _bfd_xcoff_slurp_armap (abfd))
1593 {
1594 error_ret:
1595 bfd_release (abfd, bfd_ardata (abfd));
1596 error_ret_restore:
1597 bfd_ardata (abfd) = tdata_hold;
1598 return NULL;
1599 }
1600
1601 return _bfd_no_cleanup;
1602 }
1603
1604 /* Read the archive header in an XCOFF archive. */
1605
1606 void *
1607 _bfd_xcoff_read_ar_hdr (bfd *abfd)
1608 {
1609 bfd_size_type namlen;
1610 struct areltdata *ret;
1611 bfd_size_type amt;
1612
1613 if (! xcoff_big_format_p (abfd))
1614 {
1615 struct xcoff_ar_hdr hdr;
1616 struct xcoff_ar_hdr *hdrp;
1617
1618 if (bfd_bread (&hdr, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR)
1619 return NULL;
1620
1621 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1622 if (namlen > bfd_get_file_size (abfd))
1623 return NULL;
1624 amt = sizeof (struct areltdata) + SIZEOF_AR_HDR + namlen + 1;
1625 ret = (struct areltdata *) bfd_malloc (amt);
1626 if (ret == NULL)
1627 return ret;
1628
1629 hdrp = (struct xcoff_ar_hdr *) (ret + 1);
1630 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1631 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
1632 {
1633 free (ret);
1634 return NULL;
1635 }
1636 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1637
1638 ret->arch_header = (char *) hdrp;
1639 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1640 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1641 }
1642 else
1643 {
1644 struct xcoff_ar_hdr_big hdr;
1645 struct xcoff_ar_hdr_big *hdrp;
1646
1647 if (bfd_bread (&hdr, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG)
1648 return NULL;
1649
1650 GET_VALUE_IN_FIELD (namlen, hdr.namlen, 10);
1651 if (namlen > bfd_get_file_size (abfd))
1652 return NULL;
1653 amt = sizeof (struct areltdata) + SIZEOF_AR_HDR_BIG + namlen + 1;
1654 ret = (struct areltdata *) bfd_malloc (amt);
1655 if (ret == NULL)
1656 return ret;
1657
1658 hdrp = (struct xcoff_ar_hdr_big *) (ret + 1);
1659 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1660 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
1661 {
1662 free (ret);
1663 return NULL;
1664 }
1665 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1666
1667 ret->arch_header = (char *) hdrp;
1668 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size, 10);
1669 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1670 }
1671
1672 /* Size occupied by the header above that covered in the fixed
1673 SIZEOF_AR_HDR or SIZEOF_AR_HDR_BIG. */
1674 ret->extra_size = namlen + (namlen & 1) + SXCOFFARFMAG;
1675
1676 /* Skip over the XCOFFARFMAG at the end of the file name. */
1677 if (bfd_seek (abfd, (file_ptr) ((namlen & 1) + SXCOFFARFMAG), SEEK_CUR) != 0)
1678 return NULL;
1679
1680 return ret;
1681 }
1682
1683 /* Open the next element in an XCOFF archive. */
1684
1685 bfd *
1686 _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
1687 {
1688 file_ptr filestart;
1689 file_ptr laststart, lastend;
1690
1691 if (xcoff_ardata (archive) == NULL)
1692 {
1693 bfd_set_error (bfd_error_invalid_operation);
1694 return NULL;
1695 }
1696
1697 if (! xcoff_big_format_p (archive))
1698 {
1699 if (last_file == NULL)
1700 {
1701 filestart = bfd_ardata (archive)->first_file_filepos;
1702 laststart = 0;
1703 lastend = SIZEOF_AR_FILE_HDR;
1704 }
1705 else
1706 {
1707 struct areltdata *arel = arch_eltdata (last_file);
1708
1709 GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff, 10);
1710 laststart = last_file->proxy_origin;
1711 lastend = laststart + arel->parsed_size;
1712 laststart -= SIZEOF_AR_HDR + arel->extra_size;
1713 }
1714
1715 /* Sanity check that we aren't pointing into the previous element. */
1716 if (filestart != 0 && filestart >= laststart && filestart < lastend)
1717 {
1718 bfd_set_error (bfd_error_malformed_archive);
1719 return NULL;
1720 }
1721
1722 if (filestart == 0
1723 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->memoff, 10)
1724 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->symoff, 10))
1725 {
1726 bfd_set_error (bfd_error_no_more_archived_files);
1727 return NULL;
1728 }
1729 }
1730 else
1731 {
1732 if (last_file == NULL)
1733 {
1734 filestart = bfd_ardata (archive)->first_file_filepos;
1735 laststart = 0;
1736 lastend = SIZEOF_AR_FILE_HDR_BIG;
1737 }
1738 else
1739 {
1740 struct areltdata *arel = arch_eltdata (last_file);
1741
1742 GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff, 10);
1743 laststart = last_file->proxy_origin;
1744 lastend = laststart + arel->parsed_size;
1745 laststart -= SIZEOF_AR_HDR_BIG + arel->extra_size;
1746 }
1747
1748 /* Sanity check that we aren't pointing into the previous element. */
1749 if (filestart != 0 && filestart >= laststart && filestart < lastend)
1750 {
1751 bfd_set_error (bfd_error_malformed_archive);
1752 return NULL;
1753 }
1754
1755 if (filestart == 0
1756 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->memoff, 10)
1757 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->symoff, 10))
1758 {
1759 bfd_set_error (bfd_error_no_more_archived_files);
1760 return NULL;
1761 }
1762 }
1763
1764 return _bfd_get_elt_at_filepos (archive, filestart);
1765 }
1766
1767 /* Stat an element in an XCOFF archive. */
1768
1769 int
1770 _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
1771 {
1772 if (abfd->arelt_data == NULL)
1773 {
1774 bfd_set_error (bfd_error_invalid_operation);
1775 return -1;
1776 }
1777
1778 if (! xcoff_big_format_p (abfd->my_archive))
1779 {
1780 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1781
1782 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1783 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1784 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1785 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1786 s->st_size = arch_eltdata (abfd)->parsed_size;
1787 }
1788 else
1789 {
1790 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1791
1792 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date, 10);
1793 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid, 10);
1794 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid, 10);
1795 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode, 8);
1796 s->st_size = arch_eltdata (abfd)->parsed_size;
1797 }
1798
1799 return 0;
1800 }
1801
1802 /* Normalize a file name for inclusion in an archive. */
1803
1804 static const char *
1805 normalize_filename (bfd *abfd)
1806 {
1807 const char *file;
1808 const char *filename;
1809
1810 file = bfd_get_filename (abfd);
1811 filename = strrchr (file, '/');
1812 if (filename != NULL)
1813 filename++;
1814 else
1815 filename = file;
1816 return filename;
1817 }
1818
1819 /* Write out an XCOFF armap. */
1820
1821 static bool
1822 xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1823 struct orl *map, unsigned int orl_count, int stridx)
1824 {
1825 struct archive_iterator iterator;
1826 struct xcoff_ar_hdr hdr;
1827 char *p;
1828 unsigned char buf[4];
1829 unsigned int i;
1830
1831 memset (&hdr, 0, sizeof hdr);
1832 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1833 sprintf (hdr.nextoff, "%d", 0);
1834 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, XCOFFARMAG_ELEMENT_SIZE);
1835 sprintf (hdr.date, "%d", 0);
1836 sprintf (hdr.uid, "%d", 0);
1837 sprintf (hdr.gid, "%d", 0);
1838 sprintf (hdr.mode, "%d", 0);
1839 sprintf (hdr.namlen, "%d", 0);
1840
1841 /* We need spaces, not null bytes, in the header. */
1842 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1843 if (*p == '\0')
1844 *p = ' ';
1845
1846 if (bfd_bwrite (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1847 != SIZEOF_AR_HDR
1848 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
1849 != SXCOFFARFMAG))
1850 return false;
1851
1852 H_PUT_32 (abfd, orl_count, buf);
1853 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1854 return false;
1855
1856 i = 0;
1857 archive_iterator_begin (&iterator, abfd);
1858 while (i < orl_count && archive_iterator_next (&iterator))
1859 while (map[i].u.abfd == iterator.current.member)
1860 {
1861 H_PUT_32 (abfd, iterator.current.offset, buf);
1862 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1863 return false;
1864 ++i;
1865 }
1866
1867 for (i = 0; i < orl_count; i++)
1868 {
1869 const char *name;
1870 size_t namlen;
1871
1872 name = *map[i].name;
1873 namlen = strlen (name);
1874 if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
1875 return false;
1876 }
1877
1878 if ((stridx & 1) != 0)
1879 {
1880 char b;
1881
1882 b = '\0';
1883 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1884 return false;
1885 }
1886
1887 return true;
1888 }
1889
1890 static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
1891 #if BFD_HOST_64BIT_LONG
1892 #define FMT20 "%-20ld"
1893 #elif defined (__MSVCRT__)
1894 #define FMT20 "%-20I64d"
1895 #else
1896 #define FMT20 "%-20lld"
1897 #endif
1898 #define FMT12 "%-12d"
1899 #define FMT12_OCTAL "%-12o"
1900 #define FMT4 "%-4d"
1901 #define PRINT20(d, v) \
1902 sprintf (buff20, FMT20, (bfd_uint64_t)(v)), \
1903 memcpy ((void *) (d), buff20, 20)
1904
1905 #define PRINT12(d, v) \
1906 sprintf (buff20, FMT12, (int)(v)), \
1907 memcpy ((void *) (d), buff20, 12)
1908
1909 #define PRINT12_OCTAL(d, v) \
1910 sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
1911 memcpy ((void *) (d), buff20, 12)
1912
1913 #define PRINT4(d, v) \
1914 sprintf (buff20, FMT4, (int)(v)), \
1915 memcpy ((void *) (d), buff20, 4)
1916
1917 #define READ20(d, v) \
1918 buff20[20] = 0, \
1919 memcpy (buff20, (d), 20), \
1920 (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
1921
1922 static bool
1923 do_pad (bfd *abfd, unsigned int number)
1924 {
1925 bfd_byte b = 0;
1926
1927 /* Limit pad to <= 4096. */
1928 if (number > 4096)
1929 return false;
1930
1931 while (number--)
1932 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1933 return false;
1934
1935 return true;
1936 }
1937
1938 static bool
1939 do_copy (bfd *out_bfd, bfd *in_bfd)
1940 {
1941 bfd_size_type remaining;
1942 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1943
1944 if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
1945 return false;
1946
1947 remaining = arelt_size (in_bfd);
1948
1949 while (remaining >= DEFAULT_BUFFERSIZE)
1950 {
1951 if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
1952 || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
1953 return false;
1954
1955 remaining -= DEFAULT_BUFFERSIZE;
1956 }
1957
1958 if (remaining)
1959 {
1960 if (bfd_bread (buffer, remaining, in_bfd) != remaining
1961 || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
1962 return false;
1963 }
1964
1965 return true;
1966 }
1967
1968 static bool
1969 xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1970 struct orl *map, unsigned int orl_count, int stridx)
1971 {
1972 struct archive_iterator iterator;
1973 struct xcoff_ar_file_hdr_big *fhdr;
1974 bfd_vma i, sym_32, sym_64, str_32, str_64;
1975 const bfd_arch_info_type *arch_info;
1976 bfd *current_bfd;
1977 size_t string_length;
1978 file_ptr nextoff, prevoff;
1979
1980 /* First, we look through the symbols and work out which are
1981 from 32-bit objects and which from 64-bit ones. */
1982 sym_32 = sym_64 = str_32 = str_64 = 0;
1983
1984 i = 0;
1985 for (current_bfd = abfd->archive_head;
1986 current_bfd != NULL && i < orl_count;
1987 current_bfd = current_bfd->archive_next)
1988 {
1989 arch_info = bfd_get_arch_info (current_bfd);
1990 while (map[i].u.abfd == current_bfd)
1991 {
1992 string_length = strlen (*map[i].name) + 1;
1993 if (arch_info->bits_per_address == 64)
1994 {
1995 sym_64++;
1996 str_64 += string_length;
1997 }
1998 else
1999 {
2000 sym_32++;
2001 str_32 += string_length;
2002 }
2003 i++;
2004 }
2005 }
2006
2007 /* A quick sanity check... */
2008 BFD_ASSERT (sym_64 + sym_32 == orl_count);
2009 /* Explicit cast to int for compiler. */
2010 BFD_ASSERT ((int)(str_64 + str_32) == stridx);
2011
2012 fhdr = xcoff_ardata_big (abfd);
2013
2014 /* xcoff_write_archive_contents_big passes nextoff in symoff. */
2015 READ20 (fhdr->memoff, prevoff);
2016 READ20 (fhdr->symoff, nextoff);
2017
2018 BFD_ASSERT (nextoff == bfd_tell (abfd));
2019
2020 /* Write out the symbol table.
2021 Layout :
2022
2023 standard big archive header
2024 0x0000 ar_size [0x14]
2025 0x0014 ar_nxtmem [0x14]
2026 0x0028 ar_prvmem [0x14]
2027 0x003C ar_date [0x0C]
2028 0x0048 ar_uid [0x0C]
2029 0x0054 ar_gid [0x0C]
2030 0x0060 ar_mod [0x0C]
2031 0x006C ar_namelen[0x04]
2032 0x0070 ar_fmag [SXCOFFARFMAG]
2033
2034 Symbol table
2035 0x0072 num_syms [0x08], binary
2036 0x0078 offsets [0x08 * num_syms], binary
2037 0x0086 + 0x08 * num_syms names [??]
2038 ?? pad to even bytes.
2039 */
2040
2041 if (sym_32)
2042 {
2043 struct xcoff_ar_hdr_big *hdr;
2044 char *symbol_table;
2045 char *st;
2046
2047 bfd_vma symbol_table_size =
2048 SIZEOF_AR_HDR_BIG
2049 + SXCOFFARFMAG
2050 + 8
2051 + 8 * sym_32
2052 + str_32 + (str_32 & 1);
2053
2054 symbol_table = bfd_zmalloc (symbol_table_size);
2055 if (symbol_table == NULL)
2056 return false;
2057
2058 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2059
2060 PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
2061
2062 if (sym_64)
2063 PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
2064 else
2065 PRINT20 (hdr->nextoff, 0);
2066
2067 PRINT20 (hdr->prevoff, prevoff);
2068 PRINT12 (hdr->date, 0);
2069 PRINT12 (hdr->uid, 0);
2070 PRINT12 (hdr->gid, 0);
2071 PRINT12 (hdr->mode, 0);
2072 PRINT4 (hdr->namlen, 0) ;
2073
2074 st = symbol_table + SIZEOF_AR_HDR_BIG;
2075 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2076 st += SXCOFFARFMAG;
2077
2078 bfd_h_put_64 (abfd, sym_32, st);
2079 st += 8;
2080
2081 /* loop over the 32 bit offsets */
2082 i = 0;
2083 archive_iterator_begin (&iterator, abfd);
2084 while (i < orl_count && archive_iterator_next (&iterator))
2085 {
2086 arch_info = bfd_get_arch_info (iterator.current.member);
2087 while (map[i].u.abfd == iterator.current.member)
2088 {
2089 if (arch_info->bits_per_address == 32)
2090 {
2091 bfd_h_put_64 (abfd, iterator.current.offset, st);
2092 st += 8;
2093 }
2094 i++;
2095 }
2096 }
2097
2098 /* loop over the 32 bit symbol names */
2099 i = 0;
2100 for (current_bfd = abfd->archive_head;
2101 current_bfd != NULL && i < orl_count;
2102 current_bfd = current_bfd->archive_next)
2103 {
2104 arch_info = bfd_get_arch_info (current_bfd);
2105 while (map[i].u.abfd == current_bfd)
2106 {
2107 if (arch_info->bits_per_address == 32)
2108 {
2109 string_length = sprintf (st, "%s", *map[i].name);
2110 st += string_length + 1;
2111 }
2112 i++;
2113 }
2114 }
2115
2116 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2117
2118 free (symbol_table);
2119
2120 prevoff = nextoff;
2121 nextoff = nextoff + symbol_table_size;
2122 }
2123 else
2124 PRINT20 (fhdr->symoff, 0);
2125
2126 if (sym_64)
2127 {
2128 struct xcoff_ar_hdr_big *hdr;
2129 char *symbol_table;
2130 char *st;
2131
2132 bfd_vma symbol_table_size =
2133 SIZEOF_AR_HDR_BIG
2134 + SXCOFFARFMAG
2135 + 8
2136 + 8 * sym_64
2137 + str_64 + (str_64 & 1);
2138
2139 symbol_table = bfd_zmalloc (symbol_table_size);
2140 if (symbol_table == NULL)
2141 return false;
2142
2143 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2144
2145 PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
2146 PRINT20 (hdr->nextoff, 0);
2147 PRINT20 (hdr->prevoff, prevoff);
2148 PRINT12 (hdr->date, 0);
2149 PRINT12 (hdr->uid, 0);
2150 PRINT12 (hdr->gid, 0);
2151 PRINT12 (hdr->mode, 0);
2152 PRINT4 (hdr->namlen, 0);
2153
2154 st = symbol_table + SIZEOF_AR_HDR_BIG;
2155 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2156 st += SXCOFFARFMAG;
2157
2158 bfd_h_put_64 (abfd, sym_64, st);
2159 st += 8;
2160
2161 /* loop over the 64 bit offsets */
2162 i = 0;
2163 archive_iterator_begin (&iterator, abfd);
2164 while (i < orl_count && archive_iterator_next (&iterator))
2165 {
2166 arch_info = bfd_get_arch_info (iterator.current.member);
2167 while (map[i].u.abfd == iterator.current.member)
2168 {
2169 if (arch_info->bits_per_address == 64)
2170 {
2171 bfd_h_put_64 (abfd, iterator.current.offset, st);
2172 st += 8;
2173 }
2174 i++;
2175 }
2176 }
2177
2178 /* loop over the 64 bit symbol names */
2179 i = 0;
2180 for (current_bfd = abfd->archive_head;
2181 current_bfd != NULL && i < orl_count;
2182 current_bfd = current_bfd->archive_next)
2183 {
2184 arch_info = bfd_get_arch_info (current_bfd);
2185 while (map[i].u.abfd == current_bfd)
2186 {
2187 if (arch_info->bits_per_address == 64)
2188 {
2189 string_length = sprintf (st, "%s", *map[i].name);
2190 st += string_length + 1;
2191 }
2192 i++;
2193 }
2194 }
2195
2196 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2197
2198 free (symbol_table);
2199
2200 PRINT20 (fhdr->symoff64, nextoff);
2201 }
2202 else
2203 PRINT20 (fhdr->symoff64, 0);
2204
2205 return true;
2206 }
2207
2208 bool
2209 _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2210 struct orl *map, unsigned int orl_count, int stridx)
2211 {
2212 if (! xcoff_big_format_p (abfd))
2213 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
2214 else
2215 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
2216 }
2217
2218 /* Write out an XCOFF archive. We always write an entire archive,
2219 rather than fussing with the freelist and so forth. */
2220
2221 static bool
2222 xcoff_write_archive_contents_old (bfd *abfd)
2223 {
2224 struct archive_iterator iterator;
2225 struct xcoff_ar_file_hdr fhdr;
2226 bfd_size_type count;
2227 bfd_size_type total_namlen;
2228 file_ptr *offsets;
2229 bool makemap;
2230 bool hasobjects;
2231 file_ptr prevoff, nextoff;
2232 bfd *sub;
2233 size_t i;
2234 struct xcoff_ar_hdr ahdr;
2235 bfd_size_type size;
2236 char *p;
2237 char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
2238
2239 memset (&fhdr, 0, sizeof fhdr);
2240 (void) memcpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
2241 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
2242 sprintf (fhdr.freeoff, "%d", 0);
2243
2244 count = 0;
2245 total_namlen = 0;
2246 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2247 {
2248 ++count;
2249 total_namlen += strlen (normalize_filename (sub)) + 1;
2250 if (sub->arelt_data == NULL)
2251 {
2252 sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
2253 if (sub->arelt_data == NULL)
2254 return false;
2255 }
2256 if (arch_xhdr (sub) == NULL)
2257 {
2258 struct xcoff_ar_hdr *ahdrp;
2259 struct stat s;
2260
2261 if ((sub->flags & BFD_IN_MEMORY) != 0)
2262 {
2263 /* Assume we just "made" the member, and fake it. */
2264 struct bfd_in_memory *bim
2265 = (struct bfd_in_memory *) sub->iostream;
2266 time (&s.st_mtime);
2267 s.st_uid = getuid ();
2268 s.st_gid = getgid ();
2269 s.st_mode = 0644;
2270 s.st_size = bim->size;
2271 }
2272 else if (stat (bfd_get_filename (sub), &s) != 0)
2273 {
2274 bfd_set_input_error (sub, bfd_error_system_call);
2275 return false;
2276 }
2277
2278 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2279 {
2280 s.st_mtime = 0;
2281 s.st_uid = 0;
2282 s.st_gid = 0;
2283 s.st_mode = 0644;
2284 }
2285
2286 ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
2287 if (ahdrp == NULL)
2288 return false;
2289
2290 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2291 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2292 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2293 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2294 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2295
2296 arch_eltdata (sub)->arch_header = (char *) ahdrp;
2297 arch_eltdata (sub)->parsed_size = s.st_size;
2298 }
2299 }
2300 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2301 if (offsets == NULL)
2302 return false;
2303
2304 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
2305 return false;
2306
2307 makemap = bfd_has_map (abfd);
2308 hasobjects = false;
2309 prevoff = 0;
2310 for (archive_iterator_begin (&iterator, abfd), i = 0;
2311 archive_iterator_next (&iterator);
2312 i++)
2313 {
2314 bfd_size_type namlen;
2315 struct xcoff_ar_hdr *ahdrp;
2316
2317 if (makemap && ! hasobjects)
2318 {
2319 if (bfd_check_format (iterator.current.member, bfd_object))
2320 hasobjects = true;
2321 }
2322
2323 ahdrp = arch_xhdr (iterator.current.member);
2324 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2325 sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
2326 sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
2327
2328 /* We need spaces, not null bytes, in the header. */
2329 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
2330 if (*p == '\0')
2331 *p = ' ';
2332
2333 if (!do_pad (abfd, iterator.current.leading_padding))
2334 return false;
2335
2336 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2337 namlen = iterator.current.padded_namlen;
2338 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2339 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2340 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2341 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2342 || !do_copy (abfd, iterator.current.member)
2343 || !do_pad (abfd, iterator.current.trailing_padding))
2344 return false;
2345
2346 offsets[i] = iterator.current.offset;
2347 prevoff = iterator.current.offset;
2348 }
2349
2350 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2351
2352 /* Write out the member table. */
2353
2354 nextoff = iterator.next.offset;
2355 BFD_ASSERT (nextoff == bfd_tell (abfd));
2356 sprintf (fhdr.memoff, "%ld", (long) nextoff);
2357
2358 memset (&ahdr, 0, sizeof ahdr);
2359 sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
2360 + count * XCOFFARMAG_ELEMENT_SIZE
2361 + total_namlen));
2362 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2363 sprintf (ahdr.date, "%d", 0);
2364 sprintf (ahdr.uid, "%d", 0);
2365 sprintf (ahdr.gid, "%d", 0);
2366 sprintf (ahdr.mode, "%d", 0);
2367 sprintf (ahdr.namlen, "%d", 0);
2368
2369 size = (SIZEOF_AR_HDR
2370 + XCOFFARMAG_ELEMENT_SIZE
2371 + count * XCOFFARMAG_ELEMENT_SIZE
2372 + total_namlen
2373 + SXCOFFARFMAG);
2374
2375 prevoff = nextoff;
2376 nextoff += size + (size & 1);
2377
2378 if (makemap && hasobjects)
2379 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2380 else
2381 sprintf (ahdr.nextoff, "%d", 0);
2382
2383 /* We need spaces, not null bytes, in the header. */
2384 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2385 if (*p == '\0')
2386 *p = ' ';
2387
2388 if ((bfd_bwrite (&ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
2389 != SIZEOF_AR_HDR)
2390 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
2391 != SXCOFFARFMAG))
2392 return false;
2393
2394 sprintf (decbuf, "%-12ld", (long) count);
2395 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
2396 != XCOFFARMAG_ELEMENT_SIZE)
2397 return false;
2398 for (i = 0; i < (size_t) count; i++)
2399 {
2400 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2401 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
2402 abfd) != XCOFFARMAG_ELEMENT_SIZE)
2403 return false;
2404 }
2405 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2406 {
2407 const char *name;
2408 bfd_size_type namlen;
2409
2410 name = normalize_filename (sub);
2411 namlen = strlen (name);
2412 if (bfd_bwrite (name, namlen + 1, abfd) != namlen + 1)
2413 return false;
2414 }
2415
2416 if (! do_pad (abfd, size & 1))
2417 return false;
2418
2419 /* Write out the armap, if appropriate. */
2420 if (! makemap || ! hasobjects)
2421 sprintf (fhdr.symoff, "%d", 0);
2422 else
2423 {
2424 BFD_ASSERT (nextoff == bfd_tell (abfd));
2425 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2426 bfd_ardata (abfd)->tdata = &fhdr;
2427 if (! _bfd_compute_and_write_armap (abfd, 0))
2428 return false;
2429 }
2430
2431 /* Write out the archive file header. */
2432
2433 /* We need spaces, not null bytes, in the header. */
2434 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2435 if (*p == '\0')
2436 *p = ' ';
2437
2438 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2439 || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR, abfd)
2440 != SIZEOF_AR_FILE_HDR))
2441 return false;
2442
2443 return true;
2444 }
2445
2446 static bool
2447 xcoff_write_archive_contents_big (bfd *abfd)
2448 {
2449 struct xcoff_ar_file_hdr_big fhdr;
2450 bfd_size_type count;
2451 bfd_size_type total_namlen;
2452 file_ptr *offsets;
2453 bool makemap;
2454 bool hasobjects;
2455 file_ptr prevoff, nextoff;
2456 bfd *current_bfd;
2457 size_t i;
2458 struct xcoff_ar_hdr_big *hdr;
2459 bfd_size_type size;
2460 char *member_table, *mt;
2461 bfd_vma member_table_size;
2462 struct archive_iterator iterator;
2463
2464 memset (&fhdr, 0, SIZEOF_AR_FILE_HDR_BIG);
2465 memcpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
2466
2467 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2468 return false;
2469
2470 /* Calculate count and total_namlen. */
2471 makemap = bfd_has_map (abfd);
2472 hasobjects = false;
2473 for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
2474 current_bfd != NULL;
2475 current_bfd = current_bfd->archive_next, count++)
2476 {
2477 total_namlen += strlen (normalize_filename (current_bfd)) + 1;
2478
2479 if (makemap
2480 && ! hasobjects
2481 && bfd_check_format (current_bfd, bfd_object))
2482 hasobjects = true;
2483
2484 if (current_bfd->arelt_data == NULL)
2485 {
2486 size = sizeof (struct areltdata);
2487 current_bfd->arelt_data = bfd_zmalloc (size);
2488 if (current_bfd->arelt_data == NULL)
2489 return false;
2490 }
2491
2492 if (arch_xhdr_big (current_bfd) == NULL)
2493 {
2494 struct xcoff_ar_hdr_big *ahdrp;
2495 struct stat s;
2496
2497 if ((current_bfd->flags & BFD_IN_MEMORY) != 0)
2498 {
2499 /* Assume we just "made" the member, and fake it. */
2500 struct bfd_in_memory *bim
2501 = (struct bfd_in_memory *) current_bfd->iostream;
2502 time (&s.st_mtime);
2503 s.st_uid = getuid ();
2504 s.st_gid = getgid ();
2505 s.st_mode = 0644;
2506 s.st_size = bim->size;
2507 }
2508 else if (stat (bfd_get_filename (current_bfd), &s) != 0)
2509 {
2510 bfd_set_input_error (current_bfd, bfd_error_system_call);
2511 return false;
2512 }
2513
2514 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2515 {
2516 s.st_mtime = 0;
2517 s.st_uid = 0;
2518 s.st_gid = 0;
2519 s.st_mode = 0644;
2520 }
2521
2522 ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
2523 if (ahdrp == NULL)
2524 return false;
2525
2526 PRINT20 (ahdrp->size, s.st_size);
2527 PRINT12 (ahdrp->date, s.st_mtime);
2528 PRINT12 (ahdrp->uid, s.st_uid);
2529 PRINT12 (ahdrp->gid, s.st_gid);
2530 PRINT12_OCTAL (ahdrp->mode, s.st_mode);
2531
2532 arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
2533 arch_eltdata (current_bfd)->parsed_size = s.st_size;
2534 }
2535 }
2536
2537 offsets = NULL;
2538 if (count)
2539 {
2540 offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
2541 if (offsets == NULL)
2542 return false;
2543 }
2544
2545 prevoff = 0;
2546 for (archive_iterator_begin (&iterator, abfd), i = 0;
2547 archive_iterator_next (&iterator);
2548 i++)
2549 {
2550 bfd_size_type namlen;
2551 struct xcoff_ar_hdr_big *ahdrp;
2552
2553 ahdrp = arch_xhdr_big (iterator.current.member);
2554 PRINT20 (ahdrp->prevoff, prevoff);
2555 PRINT4 (ahdrp->namlen, iterator.current.namlen);
2556 PRINT20 (ahdrp->nextoff, iterator.next.offset);
2557
2558 if (!do_pad (abfd, iterator.current.leading_padding))
2559 {
2560 free (offsets);
2561 return false;
2562 }
2563
2564 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2565 namlen = iterator.current.padded_namlen;
2566 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2567 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2568 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2569 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2570 || !do_copy (abfd, iterator.current.member)
2571 || !do_pad (abfd, iterator.current.trailing_padding))
2572 {
2573 free (offsets);
2574 return false;
2575 }
2576
2577 offsets[i] = iterator.current.offset;
2578 prevoff = iterator.current.offset;
2579 }
2580
2581 if (count)
2582 {
2583 PRINT20 (fhdr.firstmemoff, offsets[0]);
2584 PRINT20 (fhdr.lastmemoff, prevoff);
2585 }
2586
2587 /* Write out the member table.
2588 Layout :
2589
2590 standard big archive header
2591 0x0000 ar_size [0x14]
2592 0x0014 ar_nxtmem [0x14]
2593 0x0028 ar_prvmem [0x14]
2594 0x003C ar_date [0x0C]
2595 0x0048 ar_uid [0x0C]
2596 0x0054 ar_gid [0x0C]
2597 0x0060 ar_mod [0x0C]
2598 0x006C ar_namelen[0x04]
2599 0x0070 ar_fmag [0x02]
2600
2601 Member table
2602 0x0072 count [0x14]
2603 0x0086 offsets [0x14 * counts]
2604 0x0086 + 0x14 * counts names [??]
2605 ?? pad to even bytes.
2606 */
2607
2608 nextoff = iterator.next.offset;
2609 BFD_ASSERT (nextoff == bfd_tell (abfd));
2610
2611 member_table_size = (SIZEOF_AR_HDR_BIG
2612 + SXCOFFARFMAG
2613 + XCOFFARMAGBIG_ELEMENT_SIZE
2614 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2615 + total_namlen);
2616
2617 member_table_size += member_table_size & 1;
2618 member_table = bfd_zmalloc (member_table_size);
2619 if (member_table == NULL)
2620 {
2621 free (offsets);
2622 return false;
2623 }
2624
2625 hdr = (struct xcoff_ar_hdr_big *) member_table;
2626
2627 PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
2628 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2629 + total_namlen + (total_namlen & 1)));
2630 if (makemap && hasobjects)
2631 PRINT20 (hdr->nextoff, nextoff + member_table_size);
2632 else
2633 PRINT20 (hdr->nextoff, 0);
2634 PRINT20 (hdr->prevoff, prevoff);
2635 PRINT12 (hdr->date, 0);
2636 PRINT12 (hdr->uid, 0);
2637 PRINT12 (hdr->gid, 0);
2638 PRINT12 (hdr->mode, 0);
2639 PRINT4 (hdr->namlen, 0);
2640
2641 mt = member_table + SIZEOF_AR_HDR_BIG;
2642 memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
2643 mt += SXCOFFARFMAG;
2644
2645 PRINT20 (mt, count);
2646 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2647 for (i = 0; i < (size_t) count; i++)
2648 {
2649 PRINT20 (mt, offsets[i]);
2650 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2651 }
2652
2653 if (count)
2654 {
2655 free (offsets);
2656 offsets = NULL;
2657 }
2658
2659 for (current_bfd = abfd->archive_head;
2660 current_bfd != NULL;
2661 current_bfd = current_bfd->archive_next)
2662 {
2663 const char *name;
2664 size_t namlen;
2665
2666 name = normalize_filename (current_bfd);
2667 namlen = sprintf (mt, "%s", name);
2668 mt += namlen + 1;
2669 }
2670
2671 if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
2672 return false;
2673
2674 free (member_table);
2675
2676 PRINT20 (fhdr.memoff, nextoff);
2677
2678 prevoff = nextoff;
2679 nextoff += member_table_size;
2680
2681 /* Write out the armap, if appropriate. */
2682
2683 if (! makemap || ! hasobjects)
2684 PRINT20 (fhdr.symoff, 0);
2685 else
2686 {
2687 BFD_ASSERT (nextoff == bfd_tell (abfd));
2688
2689 /* Save nextoff in fhdr.symoff so the armap routine can use it. */
2690 PRINT20 (fhdr.symoff, nextoff);
2691
2692 bfd_ardata (abfd)->tdata = &fhdr;
2693 if (! _bfd_compute_and_write_armap (abfd, 0))
2694 return false;
2695 }
2696
2697 /* Write out the archive file header. */
2698
2699 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2700 || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR_BIG,
2701 abfd) != SIZEOF_AR_FILE_HDR_BIG))
2702 return false;
2703
2704 return true;
2705 }
2706
2707 bool
2708 _bfd_xcoff_write_archive_contents (bfd *abfd)
2709 {
2710 if (! xcoff_big_format_p (abfd))
2711 return xcoff_write_archive_contents_old (abfd);
2712 else
2713 return xcoff_write_archive_contents_big (abfd);
2714 }
2715 \f
2716 /* We can't use the usual coff_sizeof_headers routine, because AIX
2717 always uses an a.out header. */
2718
2719 int
2720 _bfd_xcoff_sizeof_headers (bfd *abfd,
2721 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2722 {
2723 int size;
2724
2725 size = FILHSZ;
2726 if (xcoff_data (abfd)->full_aouthdr)
2727 size += AOUTSZ;
2728 else
2729 size += SMALL_AOUTSZ;
2730 size += abfd->section_count * SCNHSZ;
2731
2732 if (info->strip != strip_all)
2733 {
2734 /* There can be additional sections just for dealing with overflow in
2735 reloc and lineno counts. But the numbers of relocs and lineno aren't
2736 known when bfd_sizeof_headers is called, so we compute them by
2737 summing the numbers from input sections. */
2738 struct nbr_reloc_lineno
2739 {
2740 unsigned int reloc_count;
2741 unsigned int lineno_count;
2742 };
2743 struct nbr_reloc_lineno *n_rl;
2744 bfd *sub;
2745 unsigned int max_index;
2746 asection *s;
2747
2748 /* Although the number of sections is known, the maximum value of
2749 section->index isn't (because some sections may have been removed).
2750 Don't try to renumber sections, just compute the upper bound. */
2751 max_index = 0;
2752 for (s = abfd->sections; s != NULL; s = s->next)
2753 if (s->index > max_index)
2754 max_index = s->index;
2755
2756 /* Allocate the per section counters. It could be possible to use a
2757 preallocated array as the number of sections is limited on XCOFF,
2758 but this creates a maintainance issue. */
2759 n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
2760 if (n_rl == NULL)
2761 return -1;
2762
2763 /* Sum. */
2764 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2765 for (s = sub->sections; s != NULL; s = s->next)
2766 if (s->output_section->owner == abfd
2767 && !bfd_section_removed_from_list (abfd, s->output_section))
2768 {
2769 struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
2770 e->reloc_count += s->reloc_count;
2771 e->lineno_count += s->lineno_count;
2772 }
2773
2774 /* Add the size of a section for each section with an overflow. */
2775 for (s = abfd->sections; s != NULL; s = s->next)
2776 {
2777 struct nbr_reloc_lineno *e = &n_rl[s->index];
2778
2779 if (e->reloc_count >= 0xffff
2780 || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
2781 size += SCNHSZ;
2782 }
2783
2784 free (n_rl);
2785 }
2786
2787 return size;
2788 }
2789 \f
2790 /* Routines to swap information in the XCOFF .loader section. If we
2791 ever need to write an XCOFF loader, this stuff will need to be
2792 moved to another file shared by the linker (which XCOFF calls the
2793 ``binder'') and the loader. */
2794
2795 /* Swap in the ldhdr structure. */
2796
2797 static void
2798 xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
2799 {
2800 const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2801
2802 dst->l_version = bfd_get_32 (abfd, src->l_version);
2803 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2804 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2805 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2806 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2807 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2808 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2809 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2810 }
2811
2812 /* Swap out the ldhdr structure. */
2813
2814 static void
2815 xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
2816 {
2817 struct external_ldhdr *dst = (struct external_ldhdr *) d;
2818
2819 bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
2820 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2821 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2822 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2823 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2824 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2825 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2826 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2827 }
2828
2829 /* Swap in the ldsym structure. */
2830
2831 static void
2832 xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
2833 {
2834 const struct external_ldsym *src = (const struct external_ldsym *) s;
2835
2836 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2837 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2838 } else {
2839 dst->_l._l_l._l_zeroes = 0;
2840 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2841 }
2842 dst->l_value = bfd_get_32 (abfd, src->l_value);
2843 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2844 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2845 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2846 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2847 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2848 }
2849
2850 /* Swap out the ldsym structure. */
2851
2852 static void
2853 xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
2854 {
2855 struct external_ldsym *dst = (struct external_ldsym *) d;
2856
2857 if (src->_l._l_l._l_zeroes != 0)
2858 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2859 else
2860 {
2861 bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2862 bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2863 dst->_l._l_l._l_offset);
2864 }
2865 bfd_put_32 (abfd, src->l_value, dst->l_value);
2866 bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
2867 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2868 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2869 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2870 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2871 }
2872
2873 static void
2874 xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
2875 {
2876 struct external_reloc *src = (struct external_reloc *) s;
2877 struct internal_reloc *dst = (struct internal_reloc *) d;
2878
2879 memset (dst, 0, sizeof (struct internal_reloc));
2880
2881 dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2882 dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2883 dst->r_size = bfd_get_8 (abfd, src->r_size);
2884 dst->r_type = bfd_get_8 (abfd, src->r_type);
2885 }
2886
2887 static unsigned int
2888 xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
2889 {
2890 struct internal_reloc *src = (struct internal_reloc *) s;
2891 struct external_reloc *dst = (struct external_reloc *) d;
2892
2893 bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2894 bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2895 bfd_put_8 (abfd, src->r_type, dst->r_type);
2896 bfd_put_8 (abfd, src->r_size, dst->r_size);
2897
2898 return bfd_coff_relsz (abfd);
2899 }
2900
2901 /* Swap in the ldrel structure. */
2902
2903 static void
2904 xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
2905 {
2906 const struct external_ldrel *src = (const struct external_ldrel *) s;
2907
2908 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2909 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2910 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2911 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2912 }
2913
2914 /* Swap out the ldrel structure. */
2915
2916 static void
2917 xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
2918 {
2919 struct external_ldrel *dst = (struct external_ldrel *) d;
2920
2921 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2922 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
2923 bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
2924 bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
2925 }
2926 \f
2927
2928 bool
2929 xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
2930 asection *input_section ATTRIBUTE_UNUSED,
2931 bfd *output_bfd ATTRIBUTE_UNUSED,
2932 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2933 struct internal_syment *sym ATTRIBUTE_UNUSED,
2934 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2935 bfd_vma val ATTRIBUTE_UNUSED,
2936 bfd_vma addend ATTRIBUTE_UNUSED,
2937 bfd_vma *relocation ATTRIBUTE_UNUSED,
2938 bfd_byte *contents ATTRIBUTE_UNUSED)
2939 {
2940 return true;
2941 }
2942
2943 bool
2944 xcoff_reloc_type_fail (bfd *input_bfd,
2945 asection *input_section ATTRIBUTE_UNUSED,
2946 bfd *output_bfd ATTRIBUTE_UNUSED,
2947 struct internal_reloc *rel,
2948 struct internal_syment *sym ATTRIBUTE_UNUSED,
2949 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2950 bfd_vma val ATTRIBUTE_UNUSED,
2951 bfd_vma addend ATTRIBUTE_UNUSED,
2952 bfd_vma *relocation ATTRIBUTE_UNUSED,
2953 bfd_byte *contents ATTRIBUTE_UNUSED)
2954 {
2955 _bfd_error_handler
2956 /* xgettext: c-format */
2957 (_("%pB: unsupported relocation type %#x"),
2958 input_bfd, (unsigned int) rel->r_type);
2959 bfd_set_error (bfd_error_bad_value);
2960 return false;
2961 }
2962
2963 bool
2964 xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
2965 asection *input_section ATTRIBUTE_UNUSED,
2966 bfd *output_bfd ATTRIBUTE_UNUSED,
2967 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2968 struct internal_syment *sym ATTRIBUTE_UNUSED,
2969 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2970 bfd_vma val,
2971 bfd_vma addend,
2972 bfd_vma *relocation,
2973 bfd_byte *contents ATTRIBUTE_UNUSED)
2974 {
2975 *relocation = val + addend;
2976 return true;
2977 }
2978
2979 bool
2980 xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
2981 asection *input_section ATTRIBUTE_UNUSED,
2982 bfd *output_bfd ATTRIBUTE_UNUSED,
2983 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2984 struct internal_syment *sym ATTRIBUTE_UNUSED,
2985 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2986 bfd_vma val,
2987 bfd_vma addend,
2988 bfd_vma *relocation,
2989 bfd_byte *contents ATTRIBUTE_UNUSED)
2990 {
2991 *relocation = - val - addend;
2992 return true;
2993 }
2994
2995 bool
2996 xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
2997 asection *input_section,
2998 bfd *output_bfd ATTRIBUTE_UNUSED,
2999 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3000 struct internal_syment *sym ATTRIBUTE_UNUSED,
3001 struct reloc_howto_struct *howto,
3002 bfd_vma val,
3003 bfd_vma addend,
3004 bfd_vma *relocation,
3005 bfd_byte *contents ATTRIBUTE_UNUSED)
3006 {
3007 howto->pc_relative = true;
3008
3009 /* A PC relative reloc includes the section address. */
3010 addend += input_section->vma;
3011
3012 *relocation = val + addend;
3013 *relocation -= (input_section->output_section->vma
3014 + input_section->output_offset);
3015 return true;
3016 }
3017
3018 bool
3019 xcoff_reloc_type_toc (bfd *input_bfd,
3020 asection *input_section ATTRIBUTE_UNUSED,
3021 bfd *output_bfd,
3022 struct internal_reloc *rel,
3023 struct internal_syment *sym ATTRIBUTE_UNUSED,
3024 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
3025 bfd_vma val,
3026 bfd_vma addend ATTRIBUTE_UNUSED,
3027 bfd_vma *relocation,
3028 bfd_byte *contents ATTRIBUTE_UNUSED)
3029 {
3030 struct xcoff_link_hash_entry *h;
3031
3032 if (0 > rel->r_symndx)
3033 return false;
3034
3035 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3036
3037 if (h != NULL && h->smclas != XMC_TD)
3038 {
3039 if (h->toc_section == NULL)
3040 {
3041 _bfd_error_handler
3042 /* xgettext: c-format */
3043 (_("%pB: TOC reloc at %#" PRIx64 " to symbol `%s' with no TOC entry"),
3044 input_bfd, (uint64_t) rel->r_vaddr, h->root.root.string);
3045 bfd_set_error (bfd_error_bad_value);
3046 return false;
3047 }
3048
3049 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
3050 val = (h->toc_section->output_section->vma
3051 + h->toc_section->output_offset);
3052 }
3053
3054 /* We can't use the preexisting value written down by the
3055 assembly, as R_TOCU needs to be adjusted when the final
3056 R_TOCL value is signed. */
3057 *relocation = val - xcoff_data (output_bfd)->toc;
3058
3059 if (rel->r_type == R_TOCU)
3060 *relocation = ((*relocation + 0x8000) >> 16) & 0xffff;
3061 if (rel->r_type == R_TOCL)
3062 *relocation = *relocation & 0x0000ffff;
3063
3064 return true;
3065 }
3066
3067 bool
3068 xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
3069 asection *input_section ATTRIBUTE_UNUSED,
3070 bfd *output_bfd ATTRIBUTE_UNUSED,
3071 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3072 struct internal_syment *sym ATTRIBUTE_UNUSED,
3073 struct reloc_howto_struct *howto,
3074 bfd_vma val,
3075 bfd_vma addend,
3076 bfd_vma *relocation,
3077 bfd_byte *contents ATTRIBUTE_UNUSED)
3078 {
3079 howto->src_mask &= ~3;
3080 howto->dst_mask = howto->src_mask;
3081
3082 *relocation = val + addend;
3083
3084 return true;
3085 }
3086
3087 static bool
3088 xcoff_reloc_type_br (bfd *input_bfd,
3089 asection *input_section,
3090 bfd *output_bfd ATTRIBUTE_UNUSED,
3091 struct internal_reloc *rel,
3092 struct internal_syment *sym ATTRIBUTE_UNUSED,
3093 struct reloc_howto_struct *howto,
3094 bfd_vma val,
3095 bfd_vma addend,
3096 bfd_vma *relocation,
3097 bfd_byte *contents)
3098 {
3099 struct xcoff_link_hash_entry *h;
3100 bfd_vma section_offset;
3101
3102 if (0 > rel->r_symndx)
3103 return false;
3104
3105 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3106 section_offset = rel->r_vaddr - input_section->vma;
3107
3108 /* If we see an R_BR or R_RBR reloc which is jumping to global
3109 linkage code, and it is followed by an appropriate cror nop
3110 instruction, we replace the cror with lwz r2,20(r1). This
3111 restores the TOC after the glink code. Contrariwise, if the
3112 call is followed by a lwz r2,20(r1), but the call is not
3113 going to global linkage code, we can replace the load with a
3114 cror. */
3115 if (NULL != h
3116 && (bfd_link_hash_defined == h->root.type
3117 || bfd_link_hash_defweak == h->root.type)
3118 && section_offset + 8 <= input_section->size)
3119 {
3120 bfd_byte *pnext;
3121 unsigned long next;
3122
3123 pnext = contents + section_offset + 4;
3124 next = bfd_get_32 (input_bfd, pnext);
3125
3126 /* The _ptrgl function is magic. It is used by the AIX
3127 compiler to call a function through a pointer. */
3128 if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
3129 {
3130 if (next == 0x4def7b82 /* cror 15,15,15 */
3131 || next == 0x4ffffb82 /* cror 31,31,31 */
3132 || next == 0x60000000) /* ori r0,r0,0 */
3133 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r2,20(r1) */
3134
3135 }
3136 else
3137 {
3138 if (next == 0x80410014) /* lwz r2,20(r1) */
3139 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
3140 }
3141 }
3142 else if (NULL != h && bfd_link_hash_undefined == h->root.type)
3143 {
3144 /* Normally, this relocation is against a defined symbol. In the
3145 case where this is a partial link and the output section offset
3146 is greater than 2^25, the linker will return an invalid error
3147 message that the relocation has been truncated. Yes it has been
3148 truncated but no it not important. For this case, disable the
3149 overflow checking. */
3150
3151 howto->complain_on_overflow = complain_overflow_dont;
3152 }
3153
3154 /* The original PC-relative relocation is biased by -r_vaddr, so adding
3155 the value below will give the absolute target address. */
3156 *relocation = val + addend + rel->r_vaddr;
3157
3158 howto->src_mask &= ~3;
3159 howto->dst_mask = howto->src_mask;
3160
3161 if (h != NULL
3162 && (h->root.type == bfd_link_hash_defined
3163 || h->root.type == bfd_link_hash_defweak)
3164 && bfd_is_abs_section (h->root.u.def.section)
3165 && section_offset + 4 <= input_section->size)
3166 {
3167 bfd_byte *ptr;
3168 bfd_vma insn;
3169
3170 /* Turn the relative branch into an absolute one by setting the
3171 AA bit. */
3172 ptr = contents + section_offset;
3173 insn = bfd_get_32 (input_bfd, ptr);
3174 insn |= 2;
3175 bfd_put_32 (input_bfd, insn, ptr);
3176
3177 /* Make the howto absolute too. */
3178 howto->pc_relative = false;
3179 howto->complain_on_overflow = complain_overflow_bitfield;
3180 }
3181 else
3182 {
3183 /* Use a PC-relative howto and subtract the instruction's address
3184 from the target address we calculated above. */
3185 howto->pc_relative = true;
3186 *relocation -= (input_section->output_section->vma
3187 + input_section->output_offset
3188 + section_offset);
3189 }
3190 return true;
3191 }
3192
3193 bool
3194 xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
3195 asection *input_section,
3196 bfd *output_bfd ATTRIBUTE_UNUSED,
3197 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3198 struct internal_syment *sym ATTRIBUTE_UNUSED,
3199 struct reloc_howto_struct *howto,
3200 bfd_vma val ATTRIBUTE_UNUSED,
3201 bfd_vma addend,
3202 bfd_vma *relocation,
3203 bfd_byte *contents ATTRIBUTE_UNUSED)
3204 {
3205 howto->pc_relative = true;
3206 howto->src_mask &= ~3;
3207 howto->dst_mask = howto->src_mask;
3208
3209 /* A PC relative reloc includes the section address. */
3210 addend += input_section->vma;
3211
3212 *relocation = val + addend;
3213 *relocation -= (input_section->output_section->vma
3214 + input_section->output_offset);
3215 return true;
3216 }
3217
3218 bool
3219 xcoff_reloc_type_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
3220 asection *input_section ATTRIBUTE_UNUSED,
3221 bfd *output_bfd ATTRIBUTE_UNUSED,
3222 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3223 struct internal_syment *sym ATTRIBUTE_UNUSED,
3224 struct reloc_howto_struct *howto,
3225 bfd_vma val,
3226 bfd_vma addend,
3227 bfd_vma *relocation,
3228 bfd_byte *contents ATTRIBUTE_UNUSED)
3229 {
3230 struct xcoff_link_hash_entry *h;
3231
3232 if (0 > rel->r_symndx)
3233 return false;
3234
3235 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
3236
3237 /* FIXME: R_TLSML is targeting a internal TOC symbol, which will
3238 make the following checks failing. It should be moved with
3239 R_TLSM bellow once it works. */
3240 if (howto->type == R_TLSML)
3241 {
3242 *relocation = 0;
3243 return true;
3244 }
3245
3246 /* FIXME: h is sometimes null, if the TLS symbol is not exported. */
3247 if (!h)
3248 {
3249 char vaddr_buf[128];
3250
3251 sprintf_vma (vaddr_buf, rel->r_vaddr);
3252 _bfd_error_handler
3253 (_("%pB: TLS relocation at 0x%s over internal symbols (C_HIDEXT) not yet possible\n"),
3254 input_bfd, vaddr_buf);
3255 return false;
3256 }
3257
3258
3259 /* TLS relocations must target a TLS symbol. */
3260 if (h->smclas != XMC_TL && h->smclas != XMC_UL)
3261 {
3262 char vaddr_buf[128];
3263
3264 sprintf_vma (vaddr_buf, rel->r_vaddr);
3265 _bfd_error_handler
3266 (_("%pB: TLS relocation at 0x%s over non-TLS symbol %s (0x%x)\n"),
3267 input_bfd, vaddr_buf, h->root.root.string, h->smclas);
3268 return false;
3269 }
3270
3271 /* Local TLS relocations must target a local symbol, ie
3272 non-imported. */
3273 if ((rel->r_type == R_TLS_LD || rel->r_type == R_TLS_LE)
3274 && (((h->flags & XCOFF_DEF_REGULAR) == 0
3275 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3276 || (h->flags & XCOFF_IMPORT) != 0))
3277 {
3278 char vaddr_buf[128];
3279
3280 sprintf_vma (vaddr_buf, rel->r_vaddr);
3281 _bfd_error_handler
3282 (_("%pB: TLS local relocation at 0x%s over imported symbol %s\n"),
3283 input_bfd, vaddr_buf, h->root.root.string);
3284 return false;
3285 }
3286
3287 /* R_TLSM and R_TLSML are relocations used by the loader.
3288 The value must be 0.
3289 FIXME: move R_TLSML here. */
3290 if (howto->type == R_TLSM)
3291 {
3292 *relocation = 0;
3293 return true;
3294 }
3295
3296 /* Other TLS relocations aims to put offsets from TLS pointers
3297 starting at -0x7c00 (or -0x7800 in XCOFF64). It becomes a
3298 simple R_POS relocation as long as .tdata and .tbss addresses
3299 start at the same value. This is done in aix ld scripts.
3300 TODO: implement optimization when tls size is < 62K. */
3301 *relocation = val + addend;
3302
3303 return true;
3304 }
3305
3306 static bool
3307 xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
3308 bfd_vma val ATTRIBUTE_UNUSED,
3309 bfd_vma relocation ATTRIBUTE_UNUSED,
3310 struct reloc_howto_struct *
3311 howto ATTRIBUTE_UNUSED)
3312 {
3313 return false;
3314 }
3315
3316 static bool
3317 xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
3318 bfd_vma val,
3319 bfd_vma relocation,
3320 struct reloc_howto_struct *howto)
3321 {
3322 bfd_vma fieldmask, signmask, ss;
3323 bfd_vma a, b, sum;
3324
3325 /* Get the values to be added together. For signed and unsigned
3326 relocations, we assume that all values should be truncated to
3327 the size of an address. For bitfields, all the bits matter.
3328 See also bfd_check_overflow. */
3329 fieldmask = N_ONES (howto->bitsize);
3330 a = relocation;
3331 b = val & howto->src_mask;
3332
3333 /* Much like unsigned, except no trimming with addrmask. In
3334 addition, the sum overflows if there is a carry out of
3335 the bfd_vma, i.e., the sum is less than either input
3336 operand. */
3337 a >>= howto->rightshift;
3338 b >>= howto->bitpos;
3339
3340 /* Bitfields are sometimes used for signed numbers; for
3341 example, a 13-bit field sometimes represents values in
3342 0..8191 and sometimes represents values in -4096..4095.
3343 If the field is signed and a is -4095 (0x1001) and b is
3344 -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3345 0x1fff is 0x3000). It's not clear how to handle this
3346 everywhere, since there is not way to know how many bits
3347 are significant in the relocation, but the original code
3348 assumed that it was fully sign extended, and we will keep
3349 that assumption. */
3350 signmask = (fieldmask >> 1) + 1;
3351
3352 if ((a & ~ fieldmask) != 0)
3353 {
3354 /* Some bits out of the field are set. This might not
3355 be a problem: if this is a signed bitfield, it is OK
3356 iff all the high bits are set, including the sign
3357 bit. We'll try setting all but the most significant
3358 bit in the original relocation value: if this is all
3359 ones, we are OK, assuming a signed bitfield. */
3360 ss = (signmask << howto->rightshift) - 1;
3361 if ((ss | relocation) != ~ (bfd_vma) 0)
3362 return true;
3363 a &= fieldmask;
3364 }
3365
3366 /* We just assume (b & ~ fieldmask) == 0. */
3367
3368 /* We explicitly permit wrap around if this relocation
3369 covers the high bit of an address. The Linux kernel
3370 relies on it, and it is the only way to write assembler
3371 code which can run when loaded at a location 0x80000000
3372 away from the location at which it is linked. */
3373 if ((unsigned) howto->bitsize + howto->rightshift
3374 == bfd_arch_bits_per_address (input_bfd))
3375 return false;
3376
3377 sum = a + b;
3378 if (sum < a || (sum & ~ fieldmask) != 0)
3379 {
3380 /* There was a carry out, or the field overflow. Test
3381 for signed operands again. Here is the overflow test
3382 is as for complain_overflow_signed. */
3383 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3384 return true;
3385 }
3386
3387 return false;
3388 }
3389
3390 static bool
3391 xcoff_complain_overflow_signed_func (bfd *input_bfd,
3392 bfd_vma val,
3393 bfd_vma relocation,
3394 struct reloc_howto_struct *howto)
3395 {
3396 bfd_vma addrmask, fieldmask, signmask, ss;
3397 bfd_vma a, b, sum;
3398
3399 /* Get the values to be added together. For signed and unsigned
3400 relocations, we assume that all values should be truncated to
3401 the size of an address. For bitfields, all the bits matter.
3402 See also bfd_check_overflow. */
3403 fieldmask = N_ONES (howto->bitsize);
3404 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3405 a = relocation;
3406 b = val & howto->src_mask;
3407
3408 a = (a & addrmask) >> howto->rightshift;
3409
3410 /* If any sign bits are set, all sign bits must be set.
3411 That is, A must be a valid negative address after
3412 shifting. */
3413 signmask = ~ (fieldmask >> 1);
3414 ss = a & signmask;
3415 if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
3416 return true;
3417
3418 /* We only need this next bit of code if the sign bit of B
3419 is below the sign bit of A. This would only happen if
3420 SRC_MASK had fewer bits than BITSIZE. Note that if
3421 SRC_MASK has more bits than BITSIZE, we can get into
3422 trouble; we would need to verify that B is in range, as
3423 we do for A above. */
3424 signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3425 if ((b & signmask) != 0)
3426 {
3427 /* Set all the bits above the sign bit. */
3428 b -= signmask <<= 1;
3429 }
3430
3431 b = (b & addrmask) >> howto->bitpos;
3432
3433 /* Now we can do the addition. */
3434 sum = a + b;
3435
3436 /* See if the result has the correct sign. Bits above the
3437 sign bit are junk now; ignore them. If the sum is
3438 positive, make sure we did not have all negative inputs;
3439 if the sum is negative, make sure we did not have all
3440 positive inputs. The test below looks only at the sign
3441 bits, and it really just
3442 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3443 */
3444 signmask = (fieldmask >> 1) + 1;
3445 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3446 return true;
3447
3448 return false;
3449 }
3450
3451 static bool
3452 xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
3453 bfd_vma val,
3454 bfd_vma relocation,
3455 struct reloc_howto_struct *howto)
3456 {
3457 bfd_vma addrmask, fieldmask;
3458 bfd_vma a, b, sum;
3459
3460 /* Get the values to be added together. For signed and unsigned
3461 relocations, we assume that all values should be truncated to
3462 the size of an address. For bitfields, all the bits matter.
3463 See also bfd_check_overflow. */
3464 fieldmask = N_ONES (howto->bitsize);
3465 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3466 a = relocation;
3467 b = val & howto->src_mask;
3468
3469 /* Checking for an unsigned overflow is relatively easy:
3470 trim the addresses and add, and trim the result as well.
3471 Overflow is normally indicated when the result does not
3472 fit in the field. However, we also need to consider the
3473 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3474 input is 0x80000000, and bfd_vma is only 32 bits; then we
3475 will get sum == 0, but there is an overflow, since the
3476 inputs did not fit in the field. Instead of doing a
3477 separate test, we can check for this by or-ing in the
3478 operands when testing for the sum overflowing its final
3479 field. */
3480 a = (a & addrmask) >> howto->rightshift;
3481 b = (b & addrmask) >> howto->bitpos;
3482 sum = (a + b) & addrmask;
3483 if ((a | b | sum) & ~ fieldmask)
3484 return true;
3485
3486 return false;
3487 }
3488
3489 /* This is the relocation function for the RS/6000/POWER/PowerPC.
3490 This is currently the only processor which uses XCOFF; I hope that
3491 will never change.
3492
3493 The original version was based on two documents:
3494 the PowerPC AIX Version 4 Application Binary Interface, First
3495 Edition (April 1992), and the PowerOpen ABI, Big-Endian
3496 32-Bit Hardware Implementation (June 30, 1994). Differences
3497 between the documents are noted below.
3498 Now, IBM has released an official documentation about XCOFF
3499 format:
3500 https://www.ibm.com/support/knowledgecenter/ssw_aix_72/filesreference/XCOFF.html
3501
3502 Unsupported r_type's
3503
3504 R_RTB:
3505 R_RRTBI:
3506 R_RRTBA:
3507
3508 These relocs are defined by the PowerPC ABI to be
3509 relative branches which use half of the difference
3510 between the symbol and the program counter. I can't
3511 quite figure out when this is useful. These relocs are
3512 not defined by the PowerOpen ABI.
3513
3514 Supported r_type's
3515
3516 R_POS:
3517 Simple positive relocation.
3518
3519 R_NEG:
3520 Simple negative relocation.
3521
3522 R_REL:
3523 Simple PC relative relocation.
3524
3525 R_TOC:
3526 TOC relative relocation. The value in the instruction in
3527 the input file is the offset from the input file TOC to
3528 the desired location. We want the offset from the final
3529 TOC to the desired location. We have:
3530 isym = iTOC + in
3531 iinsn = in + o
3532 osym = oTOC + on
3533 oinsn = on + o
3534 so we must change insn by on - in.
3535 This relocation allows the linker to perform optimizations
3536 by transforming a load instruction into a add-immediate
3537 when possible. The relocation is, then, changed to R_TRLA
3538 in the output file.
3539 TODO: Currently, the optimisation isn't implemented.
3540
3541 R_TRL:
3542 TOC relative relocation. Same as R_TOC, except that
3543 the optimization isn't allowed
3544
3545 R_TRLA:
3546 TOC relative relocation. This is a TOC relative load
3547 address instruction which have been changed to an add-
3548 immediate instruction.
3549
3550 R_GL:
3551 GL linkage relocation. The value of this relocation
3552 is the address of the external symbol in the TOC
3553 section.
3554
3555 R_TCL:
3556 Local object TOC address. I can't figure out the
3557 difference between this and case R_GL.
3558
3559 R_RL:
3560 The PowerPC AIX ABI describes this as a load which may be
3561 changed to a load address. The PowerOpen ABI says this
3562 is the same as case R_POS.
3563
3564 R_RLA:
3565 The PowerPC AIX ABI describes this as a load address
3566 which may be changed to a load. The PowerOpen ABI says
3567 this is the same as R_POS.
3568
3569 R_REF:
3570 Not a relocation but a way to prevent the garbage
3571 collector of AIX linker to remove symbols.
3572 This is not needed in our case.
3573
3574 R_BA:
3575 The PowerOpen ABI says this is the same as R_RBA.
3576
3577 R_RBA:
3578 Absolute branch which may be modified to become a
3579 relative branch.
3580
3581 R_BR:
3582 The PowerOpen ABI says this is the same as R_RBR.
3583
3584 R_RBR:
3585 A relative branch which may be modified to become an
3586 absolute branch.
3587
3588 R_CAI:
3589 The PowerPC ABI defines this as an absolute call which
3590 may be modified to become a relative call. The PowerOpen
3591 ABI does not define this relocation type.
3592
3593 R_CREL:
3594 The PowerPC ABI defines this as a relative call which may
3595 be modified to become an absolute call. The PowerOpen
3596 ABI does not define this relocation type.
3597
3598 R_RBAC:
3599 The PowerPC ABI defines this as an absolute branch to a
3600 fixed address which may be modified to an absolute branch
3601 to a symbol. The PowerOpen ABI does not define this
3602 relocation type.
3603
3604 R_RBRC:
3605 The PowerPC ABI defines this as an absolute branch to a
3606 fixed address which may be modified to a relative branch.
3607 The PowerOpen ABI does not define this relocation type.
3608
3609 R_TLS:
3610 Thread-local storage relocation using general-dynamic
3611 model.
3612
3613 R_TLS_IE:
3614 Thread-local storage relocation using initial-exec model.
3615
3616 R_TLS_LD:
3617 Thread-local storage relocation using local-dynamic model.
3618
3619 R_TLS_LE:
3620 Thread-local storage relocation using local-exec model.
3621
3622 R_TLS:
3623 Tread-local storage relocation used by the loader.
3624
3625 R_TLSM:
3626 Tread-local storage relocation used by the loader.
3627
3628 R_TOCU:
3629 Upper TOC relative relocation. The value is the
3630 high-order 16 bit of a TOC relative relocation.
3631
3632 R_TOCL:
3633 Lower TOC relative relocation. The value is the
3634 low-order 16 bit of a TOC relative relocation.
3635 */
3636
3637 bool
3638 xcoff_ppc_relocate_section (bfd *output_bfd,
3639 struct bfd_link_info *info,
3640 bfd *input_bfd,
3641 asection *input_section,
3642 bfd_byte *contents,
3643 struct internal_reloc *relocs,
3644 struct internal_syment *syms,
3645 asection **sections)
3646 {
3647 struct internal_reloc *rel;
3648 struct internal_reloc *relend;
3649
3650 rel = relocs;
3651 relend = rel + input_section->reloc_count;
3652 for (; rel < relend; rel++)
3653 {
3654 long symndx;
3655 struct xcoff_link_hash_entry *h;
3656 struct internal_syment *sym;
3657 bfd_vma addend;
3658 bfd_vma val;
3659 struct reloc_howto_struct howto;
3660 bfd_vma relocation;
3661 bfd_vma value_to_relocate;
3662 bfd_vma address;
3663 bfd_byte *location;
3664
3665 /* Relocation type R_REF is a special relocation type which is
3666 merely used to prevent garbage collection from occurring for
3667 the csect including the symbol which it references. */
3668 if (rel->r_type == R_REF)
3669 continue;
3670
3671 /* Retrieve default value in HOWTO table and fix up according
3672 to r_size field, if it can be different.
3673 This should be made during relocation reading but the algorithms
3674 are expecting constant howtos. */
3675 memcpy (&howto, &xcoff_howto_table[rel->r_type], sizeof (howto));
3676 if (howto.bitsize != (rel->r_size & 0x1f) + 1)
3677 {
3678 switch (rel->r_type)
3679 {
3680 case R_POS:
3681 case R_NEG:
3682 howto.bitsize = (rel->r_size & 0x1f) + 1;
3683 howto.size = howto.bitsize > 16 ? 2 : 1;
3684 howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
3685 break;
3686
3687 default:
3688 {
3689 char vaddr_buf[128];
3690
3691 sprintf_vma (vaddr_buf, rel->r_vaddr);
3692 _bfd_error_handler
3693 (_("%pB: relocation (%d) at 0x%s has wrong r_rsize (0x%x)\n"),
3694 input_bfd, rel->r_type, vaddr_buf, rel->r_size);
3695 return false;
3696 }
3697 }
3698 }
3699
3700 howto.complain_on_overflow = (rel->r_size & 0x80
3701 ? complain_overflow_signed
3702 : complain_overflow_bitfield);
3703
3704 /* symbol */
3705 val = 0;
3706 addend = 0;
3707 h = NULL;
3708 sym = NULL;
3709 symndx = rel->r_symndx;
3710
3711 if (-1 != symndx)
3712 {
3713 asection *sec;
3714
3715 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3716 sym = syms + symndx;
3717 addend = - sym->n_value;
3718
3719 if (NULL == h)
3720 {
3721 sec = sections[symndx];
3722 /* Hack to make sure we use the right TOC anchor value
3723 if this reloc is against the TOC anchor. */
3724 if (sec->name[3] == '0'
3725 && strcmp (sec->name, ".tc0") == 0)
3726 val = xcoff_data (output_bfd)->toc;
3727 else
3728 val = (sec->output_section->vma
3729 + sec->output_offset
3730 + sym->n_value
3731 - sec->vma);
3732 }
3733 else
3734 {
3735 if (info->unresolved_syms_in_objects != RM_IGNORE
3736 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3737 (*info->callbacks->undefined_symbol)
3738 (info, h->root.root.string,
3739 input_bfd, input_section,
3740 rel->r_vaddr - input_section->vma,
3741 info->unresolved_syms_in_objects == RM_DIAGNOSE &&
3742 !info->warn_unresolved_syms);
3743
3744 if (h->root.type == bfd_link_hash_defined
3745 || h->root.type == bfd_link_hash_defweak)
3746 {
3747 sec = h->root.u.def.section;
3748 val = (h->root.u.def.value
3749 + sec->output_section->vma
3750 + sec->output_offset);
3751 }
3752 else if (h->root.type == bfd_link_hash_common)
3753 {
3754 sec = h->root.u.c.p->section;
3755 val = (sec->output_section->vma
3756 + sec->output_offset);
3757
3758 }
3759 else
3760 {
3761 BFD_ASSERT (bfd_link_relocatable (info)
3762 || (info->static_link
3763 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3764 || (h->flags & XCOFF_DEF_DYNAMIC) != 0
3765 || (h->flags & XCOFF_IMPORT) != 0);
3766 }
3767 }
3768 }
3769
3770 if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
3771 || !((*xcoff_calculate_relocation[rel->r_type])
3772 (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
3773 addend, &relocation, contents)))
3774 return false;
3775
3776 /* address */
3777 address = rel->r_vaddr - input_section->vma;
3778 location = contents + address;
3779
3780 if (address > input_section->size)
3781 abort ();
3782
3783 /* Get the value we are going to relocate. */
3784 if (1 == howto.size)
3785 value_to_relocate = bfd_get_16 (input_bfd, location);
3786 else
3787 value_to_relocate = bfd_get_32 (input_bfd, location);
3788
3789 /* overflow.
3790
3791 FIXME: We may drop bits during the addition
3792 which we don't check for. We must either check at every single
3793 operation, which would be tedious, or we must do the computations
3794 in a type larger than bfd_vma, which would be inefficient. */
3795
3796 if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3797 (input_bfd, value_to_relocate, relocation, &howto)))
3798 {
3799 const char *name;
3800 char buf[SYMNMLEN + 1];
3801 char reloc_type_name[10];
3802
3803 if (symndx == -1)
3804 {
3805 name = "*ABS*";
3806 }
3807 else if (h != NULL)
3808 {
3809 name = NULL;
3810 }
3811 else
3812 {
3813 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3814 if (name == NULL)
3815 name = "UNKNOWN";
3816 }
3817 sprintf (reloc_type_name, "0x%02x", rel->r_type);
3818
3819 (*info->callbacks->reloc_overflow)
3820 (info, (h ? &h->root : NULL), name, reloc_type_name,
3821 (bfd_vma) 0, input_bfd, input_section,
3822 rel->r_vaddr - input_section->vma);
3823 }
3824
3825 /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE. */
3826 value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3827 | (((value_to_relocate & howto.src_mask)
3828 + relocation) & howto.dst_mask));
3829
3830 /* Put the value back in the object file. */
3831 if (1 == howto.size)
3832 bfd_put_16 (input_bfd, value_to_relocate, location);
3833 else
3834 bfd_put_32 (input_bfd, value_to_relocate, location);
3835 }
3836
3837 return true;
3838 }
3839
3840 /* gcc-8 warns (*) on all the strncpy calls in this function about
3841 possible string truncation. The "truncation" is not a bug. We
3842 have an external representation of structs with fields that are not
3843 necessarily NULL terminated and corresponding internal
3844 representation fields that are one larger so that they can always
3845 be NULL terminated.
3846 gcc versions between 4.2 and 4.6 do not allow pragma control of
3847 diagnostics inside functions, giving a hard error if you try to use
3848 the finer control available with later versions.
3849 gcc prior to 4.2 warns about diagnostic push and pop.
3850 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
3851 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
3852 (*) Depending on your system header files! */
3853 #if GCC_VERSION >= 8000
3854 # pragma GCC diagnostic push
3855 # pragma GCC diagnostic ignored "-Wstringop-truncation"
3856 #endif
3857 static bool
3858 _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
3859 struct xcoff_loader_info *ldinfo,
3860 struct internal_ldsym *ldsym,
3861 const char *name)
3862 {
3863 size_t len;
3864 len = strlen (name);
3865
3866 if (len <= SYMNMLEN)
3867 strncpy (ldsym->_l._l_name, name, SYMNMLEN);
3868 else
3869 {
3870 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3871 {
3872 bfd_size_type newalc;
3873 char *newstrings;
3874
3875 newalc = ldinfo->string_alc * 2;
3876 if (newalc == 0)
3877 newalc = 32;
3878 while (ldinfo->string_size + len + 3 > newalc)
3879 newalc *= 2;
3880
3881 newstrings = bfd_realloc (ldinfo->strings, newalc);
3882 if (newstrings == NULL)
3883 {
3884 ldinfo->failed = true;
3885 return false;
3886 }
3887 ldinfo->string_alc = newalc;
3888 ldinfo->strings = newstrings;
3889 }
3890
3891 bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
3892 ldinfo->strings + ldinfo->string_size);
3893 strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
3894 ldsym->_l._l_l._l_zeroes = 0;
3895 ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3896 ldinfo->string_size += len + 3;
3897 }
3898
3899 return true;
3900 }
3901
3902 static bool
3903 _bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
3904 struct bfd_strtab_hash *strtab,
3905 struct internal_syment *sym,
3906 const char *name)
3907 {
3908 if (strlen (name) <= SYMNMLEN)
3909 {
3910 strncpy (sym->_n._n_name, name, SYMNMLEN);
3911 }
3912 else
3913 {
3914 bool hash;
3915 bfd_size_type indx;
3916
3917 hash = !info->traditional_format;
3918 indx = _bfd_stringtab_add (strtab, name, hash, false);
3919 if (indx == (bfd_size_type) -1)
3920 return false;
3921 sym->_n._n_n._n_zeroes = 0;
3922 sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3923 }
3924 return true;
3925 }
3926 #if GCC_VERSION >= 8000
3927 # pragma GCC diagnostic pop
3928 #endif
3929
3930 static asection *
3931 xcoff_create_csect_from_smclas (bfd *abfd,
3932 union internal_auxent *aux,
3933 const char *symbol_name)
3934 {
3935 asection *return_value = NULL;
3936
3937 /* .sv64 = x_smclas == 17
3938 This is an invalid csect for 32 bit apps. */
3939 static const char * const names[] =
3940 {
3941 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
3942 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
3943 ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
3944 };
3945
3946 if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
3947 && (NULL != names[aux->x_csect.x_smclas]))
3948 {
3949 return_value = bfd_make_section_anyway
3950 (abfd, names[aux->x_csect.x_smclas]);
3951 }
3952 else
3953 {
3954 _bfd_error_handler
3955 /* xgettext: c-format */
3956 (_("%pB: symbol `%s' has unrecognized smclas %d"),
3957 abfd, symbol_name, aux->x_csect.x_smclas);
3958 bfd_set_error (bfd_error_bad_value);
3959 }
3960
3961 return return_value;
3962 }
3963
3964 static bool
3965 xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3966 {
3967 if (0xffff <= value)
3968 return true;
3969
3970 return false;
3971 }
3972
3973 static bool
3974 xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3975 {
3976 if (0xffff <= value)
3977 return true;
3978
3979 return false;
3980 }
3981
3982 static bfd_vma
3983 xcoff_loader_symbol_offset (bfd *abfd,
3984 struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
3985 {
3986 return bfd_xcoff_ldhdrsz (abfd);
3987 }
3988
3989 static bfd_vma
3990 xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
3991 {
3992 return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
3993 }
3994
3995 static bool
3996 xcoff_generate_rtinit (bfd *abfd, const char *init, const char *fini,
3997 bool rtld)
3998 {
3999 bfd_byte filehdr_ext[FILHSZ];
4000 bfd_byte scnhdr_ext[SCNHSZ];
4001 bfd_byte syment_ext[SYMESZ * 10];
4002 bfd_byte reloc_ext[RELSZ * 3];
4003 bfd_byte *data_buffer;
4004 bfd_size_type data_buffer_size;
4005 bfd_byte *string_table = NULL, *st_tmp = NULL;
4006 bfd_size_type string_table_size;
4007 bfd_vma val;
4008 size_t initsz, finisz;
4009 struct internal_filehdr filehdr;
4010 struct internal_scnhdr scnhdr;
4011 struct internal_syment syment;
4012 union internal_auxent auxent;
4013 struct internal_reloc reloc;
4014
4015 char *data_name = ".data";
4016 char *rtinit_name = "__rtinit";
4017 char *rtld_name = "__rtld";
4018
4019 if (! bfd_xcoff_rtinit_size (abfd))
4020 return false;
4021
4022 initsz = (init == NULL ? 0 : 1 + strlen (init));
4023 finisz = (fini == NULL ? 0 : 1 + strlen (fini));
4024
4025 /* file header */
4026 memset (filehdr_ext, 0, FILHSZ);
4027 memset (&filehdr, 0, sizeof (struct internal_filehdr));
4028 filehdr.f_magic = bfd_xcoff_magic_number (abfd);
4029 filehdr.f_nscns = 1;
4030 filehdr.f_timdat = 0;
4031 filehdr.f_nsyms = 0; /* at least 6, no more than 10 */
4032 filehdr.f_symptr = 0; /* set below */
4033 filehdr.f_opthdr = 0;
4034 filehdr.f_flags = 0;
4035
4036 /* section header */
4037 memset (scnhdr_ext, 0, SCNHSZ);
4038 memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
4039 memcpy (scnhdr.s_name, data_name, strlen (data_name));
4040 scnhdr.s_paddr = 0;
4041 scnhdr.s_vaddr = 0;
4042 scnhdr.s_size = 0; /* set below */
4043 scnhdr.s_scnptr = FILHSZ + SCNHSZ;
4044 scnhdr.s_relptr = 0; /* set below */
4045 scnhdr.s_lnnoptr = 0;
4046 scnhdr.s_nreloc = 0; /* either 1 or 2 */
4047 scnhdr.s_nlnno = 0;
4048 scnhdr.s_flags = STYP_DATA;
4049
4050 /* .data
4051 0x0000 0x00000000 : rtl
4052 0x0004 0x00000010 : offset to init, or 0
4053 0x0008 0x00000028 : offset to fini, or 0
4054 0x000C 0x0000000C : size of descriptor
4055 0x0010 0x00000000 : init, needs a reloc
4056 0x0014 0x00000040 : offset to init name
4057 0x0018 0x00000000 : flags, padded to a word
4058 0x001C 0x00000000 : empty init
4059 0x0020 0x00000000 :
4060 0x0024 0x00000000 :
4061 0x0028 0x00000000 : fini, needs a reloc
4062 0x002C 0x00000??? : offset to fini name
4063 0x0030 0x00000000 : flags, padded to a word
4064 0x0034 0x00000000 : empty fini
4065 0x0038 0x00000000 :
4066 0x003C 0x00000000 :
4067 0x0040 init name
4068 0x0040 + initsz fini name */
4069
4070 data_buffer_size = 0x0040 + initsz + finisz;
4071 data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
4072 data_buffer = NULL;
4073 data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
4074 if (data_buffer == NULL)
4075 return false;
4076
4077 if (initsz)
4078 {
4079 val = 0x10;
4080 bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
4081 val = 0x40;
4082 bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
4083 memcpy (&data_buffer[val], init, initsz);
4084 }
4085
4086 if (finisz)
4087 {
4088 val = 0x28;
4089 bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
4090 val = 0x40 + initsz;
4091 bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
4092 memcpy (&data_buffer[val], fini, finisz);
4093 }
4094
4095 val = 0x0C;
4096 bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
4097
4098 scnhdr.s_size = data_buffer_size;
4099
4100 /* string table */
4101 string_table_size = 0;
4102 if (initsz > 9)
4103 string_table_size += initsz;
4104 if (finisz > 9)
4105 string_table_size += finisz;
4106 if (string_table_size)
4107 {
4108 string_table_size += 4;
4109 string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
4110 if (string_table == NULL)
4111 return false;
4112
4113 val = string_table_size;
4114 bfd_h_put_32 (abfd, val, &string_table[0]);
4115 st_tmp = string_table + 4;
4116 }
4117
4118 /* symbols
4119 0. .data csect
4120 2. __rtinit
4121 4. init function
4122 6. fini function
4123 8. __rtld */
4124 memset (syment_ext, 0, 10 * SYMESZ);
4125 memset (reloc_ext, 0, 3 * RELSZ);
4126
4127 /* .data csect */
4128 memset (&syment, 0, sizeof (struct internal_syment));
4129 memset (&auxent, 0, sizeof (union internal_auxent));
4130 memcpy (syment._n._n_name, data_name, strlen (data_name));
4131 syment.n_scnum = 1;
4132 syment.n_sclass = C_HIDEXT;
4133 syment.n_numaux = 1;
4134 auxent.x_csect.x_scnlen.l = data_buffer_size;
4135 auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
4136 auxent.x_csect.x_smclas = XMC_RW;
4137 bfd_coff_swap_sym_out (abfd, &syment,
4138 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4139 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4140 syment.n_numaux,
4141 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4142 filehdr.f_nsyms += 2;
4143
4144 /* __rtinit */
4145 memset (&syment, 0, sizeof (struct internal_syment));
4146 memset (&auxent, 0, sizeof (union internal_auxent));
4147 memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
4148 syment.n_scnum = 1;
4149 syment.n_sclass = C_EXT;
4150 syment.n_numaux = 1;
4151 auxent.x_csect.x_smtyp = XTY_LD;
4152 auxent.x_csect.x_smclas = XMC_RW;
4153 bfd_coff_swap_sym_out (abfd, &syment,
4154 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4155 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4156 syment.n_numaux,
4157 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4158 filehdr.f_nsyms += 2;
4159
4160 /* init */
4161 if (initsz)
4162 {
4163 memset (&syment, 0, sizeof (struct internal_syment));
4164 memset (&auxent, 0, sizeof (union internal_auxent));
4165
4166 if (initsz > 9)
4167 {
4168 syment._n._n_n._n_offset = st_tmp - string_table;
4169 memcpy (st_tmp, init, initsz);
4170 st_tmp += initsz;
4171 }
4172 else
4173 memcpy (syment._n._n_name, init, initsz - 1);
4174
4175 syment.n_sclass = C_EXT;
4176 syment.n_numaux = 1;
4177 bfd_coff_swap_sym_out (abfd, &syment,
4178 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4179 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4180 syment.n_numaux,
4181 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4182
4183 /* reloc */
4184 memset (&reloc, 0, sizeof (struct internal_reloc));
4185 reloc.r_vaddr = 0x0010;
4186 reloc.r_symndx = filehdr.f_nsyms;
4187 reloc.r_type = R_POS;
4188 reloc.r_size = 31;
4189 bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
4190
4191 filehdr.f_nsyms += 2;
4192 scnhdr.s_nreloc += 1;
4193 }
4194
4195 /* fini */
4196 if (finisz)
4197 {
4198 memset (&syment, 0, sizeof (struct internal_syment));
4199 memset (&auxent, 0, sizeof (union internal_auxent));
4200
4201 if (finisz > 9)
4202 {
4203 syment._n._n_n._n_offset = st_tmp - string_table;
4204 memcpy (st_tmp, fini, finisz);
4205 st_tmp += finisz;
4206 }
4207 else
4208 memcpy (syment._n._n_name, fini, finisz - 1);
4209
4210 syment.n_sclass = C_EXT;
4211 syment.n_numaux = 1;
4212 bfd_coff_swap_sym_out (abfd, &syment,
4213 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4214 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4215 syment.n_numaux,
4216 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4217
4218 /* reloc */
4219 memset (&reloc, 0, sizeof (struct internal_reloc));
4220 reloc.r_vaddr = 0x0028;
4221 reloc.r_symndx = filehdr.f_nsyms;
4222 reloc.r_type = R_POS;
4223 reloc.r_size = 31;
4224 bfd_coff_swap_reloc_out (abfd, &reloc,
4225 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4226
4227 filehdr.f_nsyms += 2;
4228 scnhdr.s_nreloc += 1;
4229 }
4230
4231 if (rtld)
4232 {
4233 memset (&syment, 0, sizeof (struct internal_syment));
4234 memset (&auxent, 0, sizeof (union internal_auxent));
4235 memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
4236 syment.n_sclass = C_EXT;
4237 syment.n_numaux = 1;
4238 bfd_coff_swap_sym_out (abfd, &syment,
4239 &syment_ext[filehdr.f_nsyms * SYMESZ]);
4240 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
4241 syment.n_numaux,
4242 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
4243
4244 /* reloc */
4245 memset (&reloc, 0, sizeof (struct internal_reloc));
4246 reloc.r_vaddr = 0x0000;
4247 reloc.r_symndx = filehdr.f_nsyms;
4248 reloc.r_type = R_POS;
4249 reloc.r_size = 31;
4250 bfd_coff_swap_reloc_out (abfd, &reloc,
4251 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
4252
4253 filehdr.f_nsyms += 2;
4254 scnhdr.s_nreloc += 1;
4255 }
4256
4257 scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
4258 filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
4259
4260 bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
4261 bfd_bwrite (filehdr_ext, FILHSZ, abfd);
4262 bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
4263 bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
4264 bfd_bwrite (data_buffer, data_buffer_size, abfd);
4265 bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
4266 bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
4267 bfd_bwrite (string_table, string_table_size, abfd);
4268
4269 free (data_buffer);
4270 data_buffer = NULL;
4271
4272 return true;
4273 }
4274
4275
4276 static reloc_howto_type xcoff_dynamic_reloc =
4277 HOWTO (0, /* type */
4278 0, /* rightshift */
4279 2, /* size (0 = byte, 1 = short, 2 = long) */
4280 32, /* bitsize */
4281 false, /* pc_relative */
4282 0, /* bitpos */
4283 complain_overflow_bitfield, /* complain_on_overflow */
4284 0, /* special_function */
4285 "R_POS", /* name */
4286 true, /* partial_inplace */
4287 0xffffffff, /* src_mask */
4288 0xffffffff, /* dst_mask */
4289 false); /* pcrel_offset */
4290
4291 /* glink
4292
4293 The first word of global linkage code must be modified by filling in
4294 the correct TOC offset. */
4295
4296 static const unsigned long xcoff_glink_code[9] =
4297 {
4298 0x81820000, /* lwz r12,0(r2) */
4299 0x90410014, /* stw r2,20(r1) */
4300 0x800c0000, /* lwz r0,0(r12) */
4301 0x804c0004, /* lwz r2,4(r12) */
4302 0x7c0903a6, /* mtctr r0 */
4303 0x4e800420, /* bctr */
4304 0x00000000, /* start of traceback table */
4305 0x000c8000, /* traceback table */
4306 0x00000000, /* traceback table */
4307 };
4308
4309 /* Table to convert DWARF flags to section names.
4310 Remember to update binutils/dwarf.c:debug_displays
4311 if new DWARF sections are supported by XCOFF. */
4312
4313 const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
4314 { SSUBTYP_DWINFO, ".dwinfo", ".debug_info", true },
4315 { SSUBTYP_DWLINE, ".dwline", ".debug_line", true },
4316 { SSUBTYP_DWPBNMS, ".dwpbnms", ".debug_pubnames", true },
4317 { SSUBTYP_DWPBTYP, ".dwpbtyp", ".debug_pubtypes", true },
4318 { SSUBTYP_DWARNGE, ".dwarnge", ".debug_aranges", true },
4319 { SSUBTYP_DWABREV, ".dwabrev", ".debug_abbrev", false },
4320 { SSUBTYP_DWSTR, ".dwstr", ".debug_str", true },
4321 { SSUBTYP_DWRNGES, ".dwrnges", ".debug_ranges", true },
4322 { SSUBTYP_DWLOC, ".dwloc", ".debug_loc", true },
4323 { SSUBTYP_DWFRAME, ".dwframe", ".debug_frame", true },
4324 { SSUBTYP_DWMAC, ".dwmac", ".debug_macro", true }
4325 };
4326
4327 /* For generic entry points. */
4328 #define _bfd_xcoff_close_and_cleanup _bfd_archive_close_and_cleanup
4329 #define _bfd_xcoff_bfd_free_cached_info _bfd_bool_bfd_true
4330 #define _bfd_xcoff_new_section_hook coff_new_section_hook
4331 #define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
4332 #define _bfd_xcoff_get_section_contents_in_window \
4333 _bfd_generic_get_section_contents_in_window
4334
4335 /* For copy private data entry points. */
4336 #define _bfd_xcoff_bfd_copy_private_bfd_data \
4337 _bfd_xcoff_copy_private_bfd_data
4338 #define _bfd_xcoff_bfd_merge_private_bfd_data \
4339 _bfd_generic_bfd_merge_private_bfd_data
4340 #define _bfd_xcoff_bfd_copy_private_section_data \
4341 _bfd_generic_bfd_copy_private_section_data
4342 #define _bfd_xcoff_bfd_copy_private_symbol_data \
4343 _bfd_generic_bfd_copy_private_symbol_data
4344 #define _bfd_xcoff_bfd_copy_private_header_data \
4345 _bfd_generic_bfd_copy_private_header_data
4346 #define _bfd_xcoff_bfd_set_private_flags \
4347 _bfd_generic_bfd_set_private_flags
4348 #define _bfd_xcoff_bfd_print_private_bfd_data \
4349 _bfd_generic_bfd_print_private_bfd_data
4350
4351 /* For archive entry points. */
4352 #define _bfd_xcoff_slurp_extended_name_table \
4353 _bfd_noarchive_slurp_extended_name_table
4354 #define _bfd_xcoff_construct_extended_name_table \
4355 _bfd_noarchive_construct_extended_name_table
4356 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
4357 #define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
4358 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
4359 #define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
4360 #define _bfd_xcoff_update_armap_timestamp _bfd_bool_bfd_true
4361
4362 /* For symbols entry points. */
4363 #define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
4364 #define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
4365 #define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
4366 #define _bfd_xcoff_print_symbol coff_print_symbol
4367 #define _bfd_xcoff_get_symbol_info coff_get_symbol_info
4368 #define _bfd_xcoff_get_symbol_version_string \
4369 _bfd_nosymbols_get_symbol_version_string
4370 #define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
4371 #define _bfd_xcoff_bfd_is_target_special_symbol \
4372 coff_bfd_is_target_special_symbol
4373 #define _bfd_xcoff_get_lineno coff_get_lineno
4374 #define _bfd_xcoff_find_nearest_line coff_find_nearest_line
4375 #define _bfd_xcoff_find_line coff_find_line
4376 #define _bfd_xcoff_find_inliner_info coff_find_inliner_info
4377 #define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
4378 #define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
4379 #define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
4380
4381 /* For reloc entry points. */
4382 #define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
4383 #define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
4384 #define _bfd_xcoff_set_reloc _bfd_generic_set_reloc
4385 #define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
4386 #define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
4387
4388 /* For link entry points. */
4389 #define _bfd_xcoff_bfd_get_relocated_section_contents \
4390 bfd_generic_get_relocated_section_contents
4391 #define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
4392 #define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
4393 #define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
4394 #define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
4395 _bfd_generic_copy_link_hash_symbol_type
4396 #define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
4397 #define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
4398 #define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
4399 #define _bfd_xcoff_bfd_merge_sections bfd_generic_merge_sections
4400 #define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
4401 #define _bfd_xcoff_bfd_group_name bfd_generic_group_name
4402 #define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
4403 #define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
4404 #define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
4405 #define _bfd_xcoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
4406 #define _bfd_xcoff_bfd_define_start_stop bfd_generic_define_start_stop
4407 #define _bfd_xcoff_bfd_link_check_relocs _bfd_generic_link_check_relocs
4408
4409 /* For dynamic symbols and relocs entry points. */
4410 #define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
4411
4412 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
4413 {
4414 { /* COFF backend, defined in libcoff.h. */
4415 _bfd_xcoff_swap_aux_in,
4416 _bfd_xcoff_swap_sym_in,
4417 coff_swap_lineno_in,
4418 _bfd_xcoff_swap_aux_out,
4419 _bfd_xcoff_swap_sym_out,
4420 coff_swap_lineno_out,
4421 xcoff_swap_reloc_out,
4422 coff_swap_filehdr_out,
4423 coff_swap_aouthdr_out,
4424 coff_swap_scnhdr_out,
4425 FILHSZ,
4426 AOUTSZ,
4427 SCNHSZ,
4428 SYMESZ,
4429 AUXESZ,
4430 RELSZ,
4431 LINESZ,
4432 FILNMLEN,
4433 true, /* _bfd_coff_long_filenames */
4434 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4435 3, /* _bfd_coff_default_section_alignment_power */
4436 false, /* _bfd_coff_force_symnames_in_strings */
4437 2, /* _bfd_coff_debug_string_prefix_length */
4438 32768, /* _bfd_coff_max_nscns */
4439 coff_swap_filehdr_in,
4440 coff_swap_aouthdr_in,
4441 coff_swap_scnhdr_in,
4442 xcoff_swap_reloc_in,
4443 coff_bad_format_hook,
4444 coff_set_arch_mach_hook,
4445 coff_mkobject_hook,
4446 styp_to_sec_flags,
4447 coff_set_alignment_hook,
4448 coff_slurp_symbol_table,
4449 symname_in_debug_hook,
4450 coff_pointerize_aux_hook,
4451 coff_print_aux,
4452 dummy_reloc16_extra_cases,
4453 dummy_reloc16_estimate,
4454 NULL, /* bfd_coff_sym_is_global */
4455 coff_compute_section_file_positions,
4456 NULL, /* _bfd_coff_start_final_link */
4457 xcoff_ppc_relocate_section,
4458 coff_rtype_to_howto,
4459 NULL, /* _bfd_coff_adjust_symndx */
4460 _bfd_generic_link_add_one_symbol,
4461 coff_link_output_has_begun,
4462 coff_final_link_postscript,
4463 NULL /* print_pdata. */
4464 },
4465
4466 0x01DF, /* magic number */
4467 bfd_arch_rs6000,
4468 bfd_mach_rs6k,
4469
4470 /* Function pointers to xcoff specific swap routines. */
4471 xcoff_swap_ldhdr_in,
4472 xcoff_swap_ldhdr_out,
4473 xcoff_swap_ldsym_in,
4474 xcoff_swap_ldsym_out,
4475 xcoff_swap_ldrel_in,
4476 xcoff_swap_ldrel_out,
4477
4478 /* Sizes. */
4479 LDHDRSZ,
4480 LDSYMSZ,
4481 LDRELSZ,
4482 12, /* _xcoff_function_descriptor_size */
4483 SMALL_AOUTSZ,
4484
4485 /* Versions. */
4486 1, /* _xcoff_ldhdr_version */
4487
4488 _bfd_xcoff_put_symbol_name,
4489 _bfd_xcoff_put_ldsymbol_name,
4490 &xcoff_dynamic_reloc,
4491 xcoff_create_csect_from_smclas,
4492
4493 /* Lineno and reloc count overflow. */
4494 xcoff_is_lineno_count_overflow,
4495 xcoff_is_reloc_count_overflow,
4496
4497 xcoff_loader_symbol_offset,
4498 xcoff_loader_reloc_offset,
4499
4500 /* glink. */
4501 &xcoff_glink_code[0],
4502 36, /* _xcoff_glink_size */
4503
4504 /* rtinit */
4505 64, /* _xcoff_rtinit_size */
4506 xcoff_generate_rtinit,
4507 };
4508
4509 /* The transfer vector that leads the outside world to all of the above. */
4510 const bfd_target rs6000_xcoff_vec =
4511 {
4512 "aixcoff-rs6000",
4513 bfd_target_xcoff_flavour,
4514 BFD_ENDIAN_BIG, /* data byte order is big */
4515 BFD_ENDIAN_BIG, /* header byte order is big */
4516
4517 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4518 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4519
4520 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4521 0, /* leading char */
4522 '/', /* ar_pad_char */
4523 15, /* ar_max_namelen */
4524 0, /* match priority. */
4525 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
4526
4527 /* data */
4528 bfd_getb64,
4529 bfd_getb_signed_64,
4530 bfd_putb64,
4531 bfd_getb32,
4532 bfd_getb_signed_32,
4533 bfd_putb32,
4534 bfd_getb16,
4535 bfd_getb_signed_16,
4536 bfd_putb16,
4537
4538 /* hdrs */
4539 bfd_getb64,
4540 bfd_getb_signed_64,
4541 bfd_putb64,
4542 bfd_getb32,
4543 bfd_getb_signed_32,
4544 bfd_putb32,
4545 bfd_getb16,
4546 bfd_getb_signed_16,
4547 bfd_putb16,
4548
4549 { /* bfd_check_format */
4550 _bfd_dummy_target,
4551 coff_object_p,
4552 _bfd_xcoff_archive_p,
4553 CORE_FILE_P
4554 },
4555
4556 { /* bfd_set_format */
4557 _bfd_bool_bfd_false_error,
4558 coff_mkobject,
4559 _bfd_generic_mkarchive,
4560 _bfd_bool_bfd_false_error
4561 },
4562
4563 {/* bfd_write_contents */
4564 _bfd_bool_bfd_false_error,
4565 coff_write_object_contents,
4566 _bfd_xcoff_write_archive_contents,
4567 _bfd_bool_bfd_false_error
4568 },
4569
4570 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4571 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4572 BFD_JUMP_TABLE_CORE (coff),
4573 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4574 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4575 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4576 BFD_JUMP_TABLE_WRITE (coff),
4577 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4578 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4579
4580 /* Opposite endian version, none exists */
4581 NULL,
4582
4583 & bfd_xcoff_backend_data,
4584 };
4585
4586 /* xcoff-powermac target
4587 Old target.
4588 Only difference between this target and the rs6000 target is the
4589 the default architecture and machine type used in coffcode.h
4590
4591 PowerPC Macs use the same magic numbers as RS/6000
4592 (because that's how they were bootstrapped originally),
4593 but they are always PowerPC architecture. */
4594 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
4595 {
4596 { /* COFF backend, defined in libcoff.h. */
4597 _bfd_xcoff_swap_aux_in,
4598 _bfd_xcoff_swap_sym_in,
4599 coff_swap_lineno_in,
4600 _bfd_xcoff_swap_aux_out,
4601 _bfd_xcoff_swap_sym_out,
4602 coff_swap_lineno_out,
4603 xcoff_swap_reloc_out,
4604 coff_swap_filehdr_out,
4605 coff_swap_aouthdr_out,
4606 coff_swap_scnhdr_out,
4607 FILHSZ,
4608 AOUTSZ,
4609 SCNHSZ,
4610 SYMESZ,
4611 AUXESZ,
4612 RELSZ,
4613 LINESZ,
4614 FILNMLEN,
4615 true, /* _bfd_coff_long_filenames */
4616 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4617 3, /* _bfd_coff_default_section_alignment_power */
4618 false, /* _bfd_coff_force_symnames_in_strings */
4619 2, /* _bfd_coff_debug_string_prefix_length */
4620 32768, /* _bfd_coff_max_nscns */
4621 coff_swap_filehdr_in,
4622 coff_swap_aouthdr_in,
4623 coff_swap_scnhdr_in,
4624 xcoff_swap_reloc_in,
4625 coff_bad_format_hook,
4626 coff_set_arch_mach_hook,
4627 coff_mkobject_hook,
4628 styp_to_sec_flags,
4629 coff_set_alignment_hook,
4630 coff_slurp_symbol_table,
4631 symname_in_debug_hook,
4632 coff_pointerize_aux_hook,
4633 coff_print_aux,
4634 dummy_reloc16_extra_cases,
4635 dummy_reloc16_estimate,
4636 NULL, /* bfd_coff_sym_is_global */
4637 coff_compute_section_file_positions,
4638 NULL, /* _bfd_coff_start_final_link */
4639 xcoff_ppc_relocate_section,
4640 coff_rtype_to_howto,
4641 NULL, /* _bfd_coff_adjust_symndx */
4642 _bfd_generic_link_add_one_symbol,
4643 coff_link_output_has_begun,
4644 coff_final_link_postscript,
4645 NULL /* print_pdata. */
4646 },
4647
4648 0x01DF, /* magic number */
4649 bfd_arch_powerpc,
4650 bfd_mach_ppc,
4651
4652 /* Function pointers to xcoff specific swap routines. */
4653 xcoff_swap_ldhdr_in,
4654 xcoff_swap_ldhdr_out,
4655 xcoff_swap_ldsym_in,
4656 xcoff_swap_ldsym_out,
4657 xcoff_swap_ldrel_in,
4658 xcoff_swap_ldrel_out,
4659
4660 /* Sizes. */
4661 LDHDRSZ,
4662 LDSYMSZ,
4663 LDRELSZ,
4664 12, /* _xcoff_function_descriptor_size */
4665 SMALL_AOUTSZ,
4666
4667 /* Versions. */
4668 1, /* _xcoff_ldhdr_version */
4669
4670 _bfd_xcoff_put_symbol_name,
4671 _bfd_xcoff_put_ldsymbol_name,
4672 &xcoff_dynamic_reloc,
4673 xcoff_create_csect_from_smclas,
4674
4675 /* Lineno and reloc count overflow. */
4676 xcoff_is_lineno_count_overflow,
4677 xcoff_is_reloc_count_overflow,
4678
4679 xcoff_loader_symbol_offset,
4680 xcoff_loader_reloc_offset,
4681
4682 /* glink. */
4683 &xcoff_glink_code[0],
4684 36, /* _xcoff_glink_size */
4685
4686 /* rtinit */
4687 0, /* _xcoff_rtinit_size */
4688 xcoff_generate_rtinit,
4689 };
4690
4691 /* The transfer vector that leads the outside world to all of the above. */
4692 const bfd_target powerpc_xcoff_vec =
4693 {
4694 "xcoff-powermac",
4695 bfd_target_xcoff_flavour,
4696 BFD_ENDIAN_BIG, /* data byte order is big */
4697 BFD_ENDIAN_BIG, /* header byte order is big */
4698
4699 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4700 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4701
4702 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4703 0, /* leading char */
4704 '/', /* ar_pad_char */
4705 15, /* ar_max_namelen */
4706 0, /* match priority. */
4707 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
4708
4709 /* data */
4710 bfd_getb64,
4711 bfd_getb_signed_64,
4712 bfd_putb64,
4713 bfd_getb32,
4714 bfd_getb_signed_32,
4715 bfd_putb32,
4716 bfd_getb16,
4717 bfd_getb_signed_16,
4718 bfd_putb16,
4719
4720 /* hdrs */
4721 bfd_getb64,
4722 bfd_getb_signed_64,
4723 bfd_putb64,
4724 bfd_getb32,
4725 bfd_getb_signed_32,
4726 bfd_putb32,
4727 bfd_getb16,
4728 bfd_getb_signed_16,
4729 bfd_putb16,
4730
4731 { /* bfd_check_format */
4732 _bfd_dummy_target,
4733 coff_object_p,
4734 _bfd_xcoff_archive_p,
4735 CORE_FILE_P
4736 },
4737
4738 { /* bfd_set_format */
4739 _bfd_bool_bfd_false_error,
4740 coff_mkobject,
4741 _bfd_generic_mkarchive,
4742 _bfd_bool_bfd_false_error
4743 },
4744
4745 {/* bfd_write_contents */
4746 _bfd_bool_bfd_false_error,
4747 coff_write_object_contents,
4748 _bfd_xcoff_write_archive_contents,
4749 _bfd_bool_bfd_false_error
4750 },
4751
4752 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4753 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4754 BFD_JUMP_TABLE_CORE (coff),
4755 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4756 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4757 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4758 BFD_JUMP_TABLE_WRITE (coff),
4759 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4760 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4761
4762 /* Opposite endian version, none exists */
4763 NULL,
4764
4765 & bfd_pmac_xcoff_backend_data,
4766 };