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