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