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