Revert the value change of DT_ENCODING from 2003-01-17. Luckily (and
[deliverable/binutils-gdb.git] / bfd / coff-rs6000.c
CommitLineData
252b5132 1/* BFD back-end for IBM RS/6000 "XCOFF" files.
157090f7 2 Copyright 1990-1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5f771d47 3 Free Software Foundation, Inc.
252b5132
RH
4 FIXME: Can someone provide a transliteration of this name into ASCII?
5 Using the following chars caused a compiler warning on HIUX (so I replaced
6 them with octal escapes), and isn't useful without an understanding of what
7 character set it is.
c5930ee6 8 Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365,
252b5132
RH
9 and John Gilmore.
10 Archive support from Damon A. Permezel.
11 Contributed by IBM Corporation and Cygnus Support.
12
13This file is part of BFD, the Binary File Descriptor library.
14
15This program is free software; you can redistribute it and/or modify
16it under the terms of the GNU General Public License as published by
17the Free Software Foundation; either version 2 of the License, or
18(at your option) any later version.
19
20This program is distributed in the hope that it will be useful,
21but WITHOUT ANY WARRANTY; without even the implied warranty of
22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23GNU General Public License for more details.
24
25You should have received a copy of the GNU General Public License
26along with this program; if not, write to the Free Software
3e110533 27Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132 28
252b5132
RH
29#include "bfd.h"
30#include "sysdep.h"
beb1bf64 31#include "bfdlink.h"
252b5132
RH
32#include "libbfd.h"
33#include "coff/internal.h"
beb1bf64 34#include "coff/xcoff.h"
252b5132
RH
35#include "coff/rs6000.h"
36#include "libcoff.h"
beb1bf64
TR
37#include "libxcoff.h"
38
b34976b6
AM
39extern bfd_boolean _bfd_xcoff_mkobject
40 PARAMS ((bfd *));
41extern bfd_boolean _bfd_xcoff_copy_private_bfd_data
42 PARAMS ((bfd *, bfd *));
43extern bfd_boolean _bfd_xcoff_is_local_label_name
44 PARAMS ((bfd *, const char *));
beb1bf64
TR
45extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
46 PARAMS ((bfd *, bfd_reloc_code_real_type));
b34976b6
AM
47extern bfd_boolean _bfd_xcoff_slurp_armap
48 PARAMS ((bfd *));
49extern const bfd_target *_bfd_xcoff_archive_p
50 PARAMS ((bfd *));
51extern PTR _bfd_xcoff_read_ar_hdr
52 PARAMS ((bfd *));
53extern bfd *_bfd_xcoff_openr_next_archived_file
54 PARAMS ((bfd *, bfd *));
55extern int _bfd_xcoff_stat_arch_elt
56 PARAMS ((bfd *, struct stat *));
57extern bfd_boolean _bfd_xcoff_write_armap
beb1bf64 58 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
b34976b6
AM
59extern bfd_boolean _bfd_xcoff_write_archive_contents
60 PARAMS ((bfd *));
61extern int _bfd_xcoff_sizeof_headers
a6b96beb 62 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6
AM
63extern void _bfd_xcoff_swap_sym_in
64 PARAMS ((bfd *, PTR, PTR));
65extern unsigned int _bfd_xcoff_swap_sym_out
66 PARAMS ((bfd *, PTR, PTR));
67extern void _bfd_xcoff_swap_aux_in
68 PARAMS ((bfd *, PTR, int, int, int, int, PTR));
69extern unsigned int _bfd_xcoff_swap_aux_out
70 PARAMS ((bfd *, PTR, int, int, int, int, PTR));
71static void xcoff_swap_reloc_in
72 PARAMS ((bfd *, PTR, PTR));
73static unsigned int xcoff_swap_reloc_out
74 PARAMS ((bfd *, PTR, PTR));
beb1bf64 75
59862849 76/* Forward declare xcoff_rtype2howto for coffcode.h macro. */
b34976b6
AM
77void xcoff_rtype2howto
78 PARAMS ((arelent *, struct internal_reloc *));
beb1bf64 79
f4ffd778 80/* coffcode.h needs these to be defined. */
beb1bf64
TR
81#define RS6000COFF_C 1
82
83#define SELECT_RELOC(internal, howto) \
84 { \
85 internal.r_type = howto->type; \
86 internal.r_size = \
87 ((howto->complain_on_overflow == complain_overflow_signed \
88 ? 0x80 \
89 : 0) \
90 | (howto->bitsize - 1)); \
91 }
92
93#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
94#define COFF_LONG_FILENAMES
95#define NO_COFF_SYMBOLS
59862849 96#define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
dc810e39
AM
97#define coff_mkobject _bfd_xcoff_mkobject
98#define coff_bfd_copy_private_bfd_data _bfd_xcoff_copy_private_bfd_data
99#define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
100#define coff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
157090f7 101#define coff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
b55039f4 102#ifdef AIX_CORE
b34976b6
AM
103extern const bfd_target * rs6000coff_core_p
104 PARAMS ((bfd *abfd));
105extern bfd_boolean rs6000coff_core_file_matches_executable_p
69f284c7 106 PARAMS ((bfd *cbfd, bfd *ebfd));
b34976b6
AM
107extern char *rs6000coff_core_file_failing_command
108 PARAMS ((bfd *abfd));
109extern int rs6000coff_core_file_failing_signal
110 PARAMS ((bfd *abfd));
beb1bf64 111#define CORE_FILE_P rs6000coff_core_p
b55039f4
L
112#define coff_core_file_failing_command \
113 rs6000coff_core_file_failing_command
114#define coff_core_file_failing_signal \
115 rs6000coff_core_file_failing_signal
116#define coff_core_file_matches_executable_p \
117 rs6000coff_core_file_matches_executable_p
118#else
119#define CORE_FILE_P _bfd_dummy_target
120#define coff_core_file_failing_command \
121 _bfd_nocore_core_file_failing_command
122#define coff_core_file_failing_signal \
123 _bfd_nocore_core_file_failing_signal
124#define coff_core_file_matches_executable_p \
125 _bfd_nocore_core_file_matches_executable_p
126#endif
beb1bf64
TR
127#define coff_SWAP_sym_in _bfd_xcoff_swap_sym_in
128#define coff_SWAP_sym_out _bfd_xcoff_swap_sym_out
129#define coff_SWAP_aux_in _bfd_xcoff_swap_aux_in
130#define coff_SWAP_aux_out _bfd_xcoff_swap_aux_out
59862849
TR
131#define coff_swap_reloc_in xcoff_swap_reloc_in
132#define coff_swap_reloc_out xcoff_swap_reloc_out
133#define NO_COFF_RELOCS
beb1bf64
TR
134
135#include "coffcode.h"
14958a43 136
252b5132
RH
137/* The main body of code is in coffcode.h. */
138
b34976b6
AM
139static const char *normalize_filename
140 PARAMS ((bfd *));
141static bfd_boolean xcoff_write_armap_old
a7b97311 142 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
b34976b6 143static bfd_boolean xcoff_write_armap_big
a7b97311 144 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
b34976b6
AM
145static bfd_boolean xcoff_write_archive_contents_old
146 PARAMS ((bfd *));
147static bfd_boolean xcoff_write_archive_contents_big
148 PARAMS ((bfd *));
a7b97311 149static void xcoff_swap_ldhdr_in
814fa6ab 150 PARAMS ((bfd *, const PTR, struct internal_ldhdr *));
a7b97311 151static void xcoff_swap_ldhdr_out
814fa6ab 152 PARAMS ((bfd *, const struct internal_ldhdr *, PTR));
a7b97311 153static void xcoff_swap_ldsym_in
814fa6ab 154 PARAMS ((bfd *, const PTR, struct internal_ldsym *));
a7b97311 155static void xcoff_swap_ldsym_out
814fa6ab 156 PARAMS ((bfd *, const struct internal_ldsym *, PTR));
a7b97311 157static void xcoff_swap_ldrel_in
814fa6ab 158 PARAMS ((bfd *, const PTR, struct internal_ldrel *));
a7b97311 159static void xcoff_swap_ldrel_out
814fa6ab 160 PARAMS ((bfd *, const struct internal_ldrel *, PTR));
b34976b6 161static bfd_boolean xcoff_ppc_relocate_section
a7b97311
AM
162 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
163 struct internal_reloc *, struct internal_syment *, asection **));
b34976b6 164static bfd_boolean _bfd_xcoff_put_ldsymbol_name
a7b97311
AM
165 PARAMS ((bfd *, struct xcoff_loader_info *, struct internal_ldsym *,
166 const char *));
167static asection *xcoff_create_csect_from_smclas
814fa6ab 168 PARAMS ((bfd *, union internal_auxent *, const char *));
b34976b6
AM
169static bfd_boolean xcoff_is_lineno_count_overflow
170 PARAMS ((bfd *, bfd_vma));
171static bfd_boolean xcoff_is_reloc_count_overflow
172 PARAMS ((bfd *, bfd_vma));
a7b97311
AM
173static bfd_vma xcoff_loader_symbol_offset
174 PARAMS ((bfd *, struct internal_ldhdr *));
175static bfd_vma xcoff_loader_reloc_offset
176 PARAMS ((bfd *, struct internal_ldhdr *));
b34976b6
AM
177static bfd_boolean xcoff_generate_rtinit
178 PARAMS ((bfd *, const char *, const char *, bfd_boolean));
179static bfd_boolean do_pad
180 PARAMS ((bfd *, unsigned int));
181static bfd_boolean do_copy
182 PARAMS ((bfd *, bfd *));
183static bfd_boolean do_shared_object_padding
9dadfa79 184 PARAMS ((bfd *, bfd *, file_ptr *, int));
14958a43 185
dbe341c6 186/* Relocation functions */
b34976b6
AM
187static bfd_boolean xcoff_reloc_type_br
188 PARAMS ((XCOFF_RELOC_FUNCTION_ARGS));
dbe341c6 189
b34976b6 190static bfd_boolean xcoff_complain_overflow_dont_func
dbe341c6 191 PARAMS ((XCOFF_COMPLAIN_FUNCTION_ARGS));
b34976b6 192static bfd_boolean xcoff_complain_overflow_bitfield_func
dbe341c6 193 PARAMS ((XCOFF_COMPLAIN_FUNCTION_ARGS));
b34976b6 194static bfd_boolean xcoff_complain_overflow_signed_func
dbe341c6 195 PARAMS ((XCOFF_COMPLAIN_FUNCTION_ARGS));
b34976b6 196static bfd_boolean xcoff_complain_overflow_unsigned_func
dbe341c6
TR
197 PARAMS ((XCOFF_COMPLAIN_FUNCTION_ARGS));
198
b34976b6 199bfd_boolean (*xcoff_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION])
cf9ab45b 200 PARAMS ((XCOFF_RELOC_FUNCTION_ARGS)) =
dbe341c6 201{
cf9ab45b
AM
202 xcoff_reloc_type_pos, /* R_POS (0x00) */
203 xcoff_reloc_type_neg, /* R_NEG (0x01) */
204 xcoff_reloc_type_rel, /* R_REL (0x02) */
205 xcoff_reloc_type_toc, /* R_TOC (0x03) */
dbe341c6 206 xcoff_reloc_type_fail, /* R_RTB (0x04) */
cf9ab45b
AM
207 xcoff_reloc_type_toc, /* R_GL (0x05) */
208 xcoff_reloc_type_toc, /* R_TCL (0x06) */
209 xcoff_reloc_type_fail, /* (0x07) */
210 xcoff_reloc_type_ba, /* R_BA (0x08) */
211 xcoff_reloc_type_fail, /* (0x09) */
212 xcoff_reloc_type_br, /* R_BR (0x0a) */
213 xcoff_reloc_type_fail, /* (0x0b) */
214 xcoff_reloc_type_pos, /* R_RL (0x0c) */
215 xcoff_reloc_type_pos, /* R_RLA (0x0d) */
216 xcoff_reloc_type_fail, /* (0x0e) */
dbe341c6 217 xcoff_reloc_type_noop, /* R_REF (0x0f) */
cf9ab45b
AM
218 xcoff_reloc_type_fail, /* (0x10) */
219 xcoff_reloc_type_fail, /* (0x11) */
220 xcoff_reloc_type_toc, /* R_TRL (0x12) */
221 xcoff_reloc_type_toc, /* R_TRLA (0x13) */
dbe341c6
TR
222 xcoff_reloc_type_fail, /* R_RRTBI (0x14) */
223 xcoff_reloc_type_fail, /* R_RRTBA (0x15) */
cf9ab45b 224 xcoff_reloc_type_ba, /* R_CAI (0x16) */
dbe341c6 225 xcoff_reloc_type_crel, /* R_CREL (0x17) */
cf9ab45b
AM
226 xcoff_reloc_type_ba, /* R_RBA (0x18) */
227 xcoff_reloc_type_ba, /* R_RBAC (0x19) */
228 xcoff_reloc_type_br, /* R_RBR (0x1a) */
229 xcoff_reloc_type_ba, /* R_RBRC (0x1b) */
dbe341c6
TR
230};
231
b34976b6 232bfd_boolean (*xcoff_complain_overflow[XCOFF_MAX_COMPLAIN_OVERFLOW])
cf9ab45b 233 PARAMS ((XCOFF_COMPLAIN_FUNCTION_ARGS)) =
dbe341c6
TR
234{
235 xcoff_complain_overflow_dont_func,
236 xcoff_complain_overflow_bitfield_func,
237 xcoff_complain_overflow_signed_func,
238 xcoff_complain_overflow_unsigned_func,
239};
240
252b5132
RH
241/* We use our own tdata type. Its first field is the COFF tdata type,
242 so the COFF routines are compatible. */
243
b34976b6 244bfd_boolean
7f6d05e8 245_bfd_xcoff_mkobject (abfd)
252b5132
RH
246 bfd *abfd;
247{
248 coff_data_type *coff;
dc810e39 249 bfd_size_type amt = sizeof (struct xcoff_tdata);
252b5132 250
dc810e39 251 abfd->tdata.xcoff_obj_data = (struct xcoff_tdata *) bfd_zalloc (abfd, amt);
252b5132 252 if (abfd->tdata.xcoff_obj_data == NULL)
b34976b6 253 return FALSE;
252b5132
RH
254 coff = coff_data (abfd);
255 coff->symbols = (coff_symbol_type *) NULL;
256 coff->conversion_table = (unsigned int *) NULL;
257 coff->raw_syments = (struct coff_ptr_struct *) NULL;
258 coff->relocbase = 0;
259
260 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
261
262 /* We set cputype to -1 to indicate that it has not been
263 initialized. */
264 xcoff_data (abfd)->cputype = -1;
265
266 xcoff_data (abfd)->csects = NULL;
267 xcoff_data (abfd)->debug_indices = NULL;
268
beb1bf64 269 /* text section alignment is different than the default */
f3813499 270 bfd_xcoff_text_align_power (abfd) = 2;
beb1bf64 271
b34976b6 272 return TRUE;
252b5132
RH
273}
274
275/* Copy XCOFF data from one BFD to another. */
276
b34976b6 277bfd_boolean
7f6d05e8 278_bfd_xcoff_copy_private_bfd_data (ibfd, obfd)
252b5132
RH
279 bfd *ibfd;
280 bfd *obfd;
281{
282 struct xcoff_tdata *ix, *ox;
283 asection *sec;
284
285 if (ibfd->xvec != obfd->xvec)
b34976b6 286 return TRUE;
252b5132
RH
287 ix = xcoff_data (ibfd);
288 ox = xcoff_data (obfd);
289 ox->full_aouthdr = ix->full_aouthdr;
290 ox->toc = ix->toc;
291 if (ix->sntoc == 0)
292 ox->sntoc = 0;
293 else
294 {
295 sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
296 if (sec == NULL)
297 ox->sntoc = 0;
298 else
299 ox->sntoc = sec->output_section->target_index;
300 }
301 if (ix->snentry == 0)
302 ox->snentry = 0;
303 else
304 {
305 sec = coff_section_from_bfd_index (ibfd, ix->snentry);
306 if (sec == NULL)
307 ox->snentry = 0;
308 else
309 ox->snentry = sec->output_section->target_index;
310 }
f3813499
TR
311 bfd_xcoff_text_align_power (obfd) = bfd_xcoff_text_align_power (ibfd);
312 bfd_xcoff_data_align_power (obfd) = bfd_xcoff_data_align_power (ibfd);
252b5132
RH
313 ox->modtype = ix->modtype;
314 ox->cputype = ix->cputype;
315 ox->maxdata = ix->maxdata;
316 ox->maxstack = ix->maxstack;
b34976b6 317 return TRUE;
252b5132
RH
318}
319
320/* I don't think XCOFF really has a notion of local labels based on
321 name. This will mean that ld -X doesn't actually strip anything.
322 The AIX native linker does not have a -X option, and it ignores the
323 -x option. */
324
b34976b6 325bfd_boolean
7f6d05e8 326_bfd_xcoff_is_local_label_name (abfd, name)
5f771d47
ILT
327 bfd *abfd ATTRIBUTE_UNUSED;
328 const char *name ATTRIBUTE_UNUSED;
252b5132 329{
b34976b6 330 return FALSE;
252b5132 331}
7f6d05e8 332\f
14958a43
CP
333void
334_bfd_xcoff_swap_sym_in (abfd, ext1, in1)
cf9ab45b 335 bfd *abfd;
7f6d05e8
CP
336 PTR ext1;
337 PTR in1;
338{
339 SYMENT *ext = (SYMENT *)ext1;
f4ffd778 340 struct internal_syment * in = (struct internal_syment *)in1;
7f6d05e8 341
f4ffd778
NC
342 if (ext->e.e_name[0] != 0)
343 {
cf9ab45b 344 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
f4ffd778
NC
345 }
346 else
347 {
348 in->_n._n_n._n_zeroes = 0;
dc810e39 349 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
f4ffd778 350 }
7f6d05e8 351
dc810e39
AM
352 in->n_value = H_GET_32 (abfd, ext->e_value);
353 in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
354 in->n_type = H_GET_16 (abfd, ext->e_type);
355 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
356 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
7f6d05e8
CP
357}
358
14958a43
CP
359unsigned int
360_bfd_xcoff_swap_sym_out (abfd, inp, extp)
cf9ab45b
AM
361 bfd *abfd;
362 PTR inp;
363 PTR extp;
7f6d05e8
CP
364{
365 struct internal_syment *in = (struct internal_syment *)inp;
366 SYMENT *ext =(SYMENT *)extp;
367
f4ffd778
NC
368 if (in->_n._n_name[0] != 0)
369 {
cf9ab45b 370 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
f4ffd778
NC
371 }
372 else
373 {
dc810e39
AM
374 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
375 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
f4ffd778 376 }
7f6d05e8 377
dc810e39
AM
378 H_PUT_32 (abfd, in->n_value, ext->e_value);
379 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
380 H_PUT_16 (abfd, in->n_type, ext->e_type);
381 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
382 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
7f6d05e8
CP
383 return bfd_coff_symesz (abfd);
384}
385
14958a43
CP
386void
387_bfd_xcoff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
cf9ab45b
AM
388 bfd *abfd;
389 PTR ext1;
390 int type;
391 int class;
392 int indx;
393 int numaux;
394 PTR in1;
7f6d05e8 395{
f4ffd778 396 AUXENT * ext = (AUXENT *)ext1;
7f6d05e8
CP
397 union internal_auxent *in = (union internal_auxent *)in1;
398
f4ffd778
NC
399 switch (class)
400 {
7f6d05e8 401 case C_FILE:
f4ffd778
NC
402 if (ext->x_file.x_fname[0] == 0)
403 {
7f6d05e8 404 in->x_file.x_n.x_zeroes = 0;
dc810e39
AM
405 in->x_file.x_n.x_offset =
406 H_GET_32 (abfd, ext->x_file.x_n.x_offset);
f4ffd778
NC
407 }
408 else
409 {
410 if (numaux > 1)
411 {
412 if (indx == 0)
413 memcpy (in->x_file.x_fname, ext->x_file.x_fname,
414 numaux * sizeof (AUXENT));
415 }
416 else
417 {
418 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
419 }
420 }
7f6d05e8
CP
421 goto end;
422
423 /* RS/6000 "csect" auxents */
424 case C_EXT:
425 case C_HIDEXT:
426 if (indx + 1 == numaux)
427 {
dc810e39
AM
428 in->x_csect.x_scnlen.l = H_GET_32 (abfd, ext->x_csect.x_scnlen);
429 in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
430 in->x_csect.x_snhash = H_GET_16 (abfd, ext->x_csect.x_snhash);
7f6d05e8
CP
431 /* We don't have to hack bitfields in x_smtyp because it's
432 defined by shifts-and-ands, which are equivalent on all
433 byte orders. */
dc810e39
AM
434 in->x_csect.x_smtyp = H_GET_8 (abfd, ext->x_csect.x_smtyp);
435 in->x_csect.x_smclas = H_GET_8 (abfd, ext->x_csect.x_smclas);
436 in->x_csect.x_stab = H_GET_32 (abfd, ext->x_csect.x_stab);
437 in->x_csect.x_snstab = H_GET_16 (abfd, ext->x_csect.x_snstab);
7f6d05e8
CP
438 goto end;
439 }
440 break;
441
442 case C_STAT:
443 case C_LEAFSTAT:
444 case C_HIDDEN:
f4ffd778
NC
445 if (type == T_NULL)
446 {
dc810e39
AM
447 in->x_scn.x_scnlen = H_GET_32 (abfd, ext->x_scn.x_scnlen);
448 in->x_scn.x_nreloc = H_GET_16 (abfd, ext->x_scn.x_nreloc);
449 in->x_scn.x_nlinno = H_GET_16 (abfd, ext->x_scn.x_nlinno);
7f6d05e8 450 /* PE defines some extra fields; we zero them out for
cf9ab45b 451 safety. */
7f6d05e8
CP
452 in->x_scn.x_checksum = 0;
453 in->x_scn.x_associated = 0;
454 in->x_scn.x_comdat = 0;
455
456 goto end;
457 }
458 break;
459 }
460
dc810e39
AM
461 in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
462 in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
7f6d05e8
CP
463
464 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
465 {
dc810e39
AM
466 in->x_sym.x_fcnary.x_fcn.x_lnnoptr =
467 H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
468 in->x_sym.x_fcnary.x_fcn.x_endndx.l =
469 H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx);
7f6d05e8
CP
470 }
471 else
472 {
473 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
dc810e39 474 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
7f6d05e8 475 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
dc810e39 476 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
7f6d05e8 477 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
dc810e39 478 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
7f6d05e8 479 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
dc810e39 480 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
7f6d05e8 481 }
7f6d05e8 482
f4ffd778
NC
483 if (ISFCN (type))
484 {
dc810e39 485 in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
f4ffd778
NC
486 }
487 else
488 {
dc810e39
AM
489 in->x_sym.x_misc.x_lnsz.x_lnno =
490 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno);
491 in->x_sym.x_misc.x_lnsz.x_size =
492 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size);
f4ffd778 493 }
7f6d05e8 494
f4ffd778
NC
495 end: ;
496 /* The semicolon is because MSVC doesn't like labels at
497 end of block. */
7f6d05e8
CP
498}
499
beb1bf64 500
b34976b6
AM
501unsigned int _bfd_xcoff_swap_aux_out
502 PARAMS ((bfd *, PTR, int, int, int, int, PTR));
beb1bf64 503
14958a43
CP
504unsigned int
505_bfd_xcoff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
917583ad
NC
506 bfd * abfd;
507 PTR inp;
7f6d05e8
CP
508 int type;
509 int class;
510 int indx ATTRIBUTE_UNUSED;
511 int numaux ATTRIBUTE_UNUSED;
917583ad 512 PTR extp;
7f6d05e8
CP
513{
514 union internal_auxent *in = (union internal_auxent *)inp;
515 AUXENT *ext = (AUXENT *)extp;
516
cf9ab45b 517 memset ((PTR)ext, 0, bfd_coff_auxesz (abfd));
7f6d05e8
CP
518 switch (class)
519 {
f4ffd778
NC
520 case C_FILE:
521 if (in->x_file.x_fname[0] == 0)
522 {
dc810e39
AM
523 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
524 H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
f4ffd778
NC
525 }
526 else
527 {
528 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
529 }
7f6d05e8 530 goto end;
f4ffd778
NC
531
532 /* RS/6000 "csect" auxents */
533 case C_EXT:
534 case C_HIDEXT:
535 if (indx + 1 == numaux)
536 {
dc810e39
AM
537 H_PUT_32 (abfd, in->x_csect.x_scnlen.l, ext->x_csect.x_scnlen);
538 H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
539 H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
f4ffd778
NC
540 /* We don't have to hack bitfields in x_smtyp because it's
541 defined by shifts-and-ands, which are equivalent on all
542 byte orders. */
dc810e39
AM
543 H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
544 H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
545 H_PUT_32 (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
546 H_PUT_16 (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
f4ffd778
NC
547 goto end;
548 }
549 break;
550
551 case C_STAT:
552 case C_LEAFSTAT:
553 case C_HIDDEN:
554 if (type == T_NULL)
555 {
dc810e39
AM
556 H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen);
557 H_PUT_16 (abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc);
558 H_PUT_16 (abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno);
f4ffd778
NC
559 goto end;
560 }
561 break;
7f6d05e8 562 }
7f6d05e8 563
dc810e39
AM
564 H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
565 H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
7f6d05e8
CP
566
567 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
568 {
dc810e39
AM
569 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
570 ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
571 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
572 ext->x_sym.x_fcnary.x_fcn.x_endndx);
7f6d05e8
CP
573 }
574 else
575 {
dc810e39
AM
576 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
577 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
578 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
579 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
580 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
581 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
582 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
583 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
7f6d05e8
CP
584 }
585
586 if (ISFCN (type))
dc810e39 587 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
7f6d05e8
CP
588 else
589 {
dc810e39
AM
590 H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
591 ext->x_sym.x_misc.x_lnsz.x_lnno);
592 H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_size,
593 ext->x_sym.x_misc.x_lnsz.x_size);
7f6d05e8
CP
594 }
595
596end:
597 return bfd_coff_auxesz (abfd);
598}
beb1bf64
TR
599
600
252b5132
RH
601\f
602/* The XCOFF reloc table. Actually, XCOFF relocations specify the
603 bitsize and whether they are signed or not, along with a
604 conventional type. This table is for the types, which are used for
605 different algorithms for putting in the reloc. Many of these
606 relocs need special_function entries, which I have not written. */
607
7f6d05e8
CP
608
609reloc_howto_type xcoff_howto_table[] =
252b5132
RH
610{
611 /* Standard 32 bit relocation. */
cf9ab45b
AM
612 HOWTO (R_POS, /* type */
613 0, /* rightshift */
614 2, /* size (0 = byte, 1 = short, 2 = long) */
615 32, /* bitsize */
b34976b6 616 FALSE, /* pc_relative */
cf9ab45b 617 0, /* bitpos */
252b5132 618 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
619 0, /* special_function */
620 "R_POS", /* name */
b34976b6 621 TRUE, /* partial_inplace */
cf9ab45b
AM
622 0xffffffff, /* src_mask */
623 0xffffffff, /* dst_mask */
b34976b6 624 FALSE), /* pcrel_offset */
252b5132
RH
625
626 /* 32 bit relocation, but store negative value. */
cf9ab45b
AM
627 HOWTO (R_NEG, /* type */
628 0, /* rightshift */
629 -2, /* size (0 = byte, 1 = short, 2 = long) */
630 32, /* bitsize */
b34976b6 631 FALSE, /* pc_relative */
cf9ab45b 632 0, /* bitpos */
252b5132 633 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
634 0, /* special_function */
635 "R_NEG", /* name */
b34976b6 636 TRUE, /* partial_inplace */
cf9ab45b
AM
637 0xffffffff, /* src_mask */
638 0xffffffff, /* dst_mask */
b34976b6 639 FALSE), /* pcrel_offset */
252b5132
RH
640
641 /* 32 bit PC relative relocation. */
cf9ab45b
AM
642 HOWTO (R_REL, /* type */
643 0, /* rightshift */
644 2, /* size (0 = byte, 1 = short, 2 = long) */
645 32, /* bitsize */
b34976b6 646 TRUE, /* pc_relative */
cf9ab45b 647 0, /* bitpos */
252b5132 648 complain_overflow_signed, /* complain_on_overflow */
cf9ab45b
AM
649 0, /* special_function */
650 "R_REL", /* name */
b34976b6 651 TRUE, /* partial_inplace */
cf9ab45b
AM
652 0xffffffff, /* src_mask */
653 0xffffffff, /* dst_mask */
b34976b6 654 FALSE), /* pcrel_offset */
c5930ee6 655
252b5132 656 /* 16 bit TOC relative relocation. */
cf9ab45b
AM
657 HOWTO (R_TOC, /* type */
658 0, /* rightshift */
659 1, /* size (0 = byte, 1 = short, 2 = long) */
660 16, /* bitsize */
b34976b6 661 FALSE, /* pc_relative */
cf9ab45b 662 0, /* bitpos */
252b5132 663 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
664 0, /* special_function */
665 "R_TOC", /* name */
b34976b6 666 TRUE, /* partial_inplace */
cf9ab45b
AM
667 0xffff, /* src_mask */
668 0xffff, /* dst_mask */
b34976b6 669 FALSE), /* pcrel_offset */
c5930ee6 670
252b5132 671 /* I don't really know what this is. */
cf9ab45b
AM
672 HOWTO (R_RTB, /* type */
673 1, /* rightshift */
674 2, /* size (0 = byte, 1 = short, 2 = long) */
675 32, /* bitsize */
b34976b6 676 FALSE, /* pc_relative */
cf9ab45b 677 0, /* bitpos */
252b5132 678 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
679 0, /* special_function */
680 "R_RTB", /* name */
b34976b6 681 TRUE, /* partial_inplace */
cf9ab45b
AM
682 0xffffffff, /* src_mask */
683 0xffffffff, /* dst_mask */
b34976b6 684 FALSE), /* pcrel_offset */
c5930ee6 685
252b5132 686 /* External TOC relative symbol. */
cf9ab45b
AM
687 HOWTO (R_GL, /* type */
688 0, /* rightshift */
48bfecdd 689 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 690 16, /* bitsize */
b34976b6 691 FALSE, /* pc_relative */
cf9ab45b 692 0, /* bitpos */
252b5132 693 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
694 0, /* special_function */
695 "R_GL", /* name */
b34976b6 696 TRUE, /* partial_inplace */
cf9ab45b
AM
697 0xffff, /* src_mask */
698 0xffff, /* dst_mask */
b34976b6 699 FALSE), /* pcrel_offset */
cf9ab45b
AM
700
701 /* Local TOC relative symbol. */
702 HOWTO (R_TCL, /* type */
703 0, /* rightshift */
48bfecdd 704 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 705 16, /* bitsize */
b34976b6 706 FALSE, /* pc_relative */
cf9ab45b 707 0, /* bitpos */
252b5132 708 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
709 0, /* special_function */
710 "R_TCL", /* name */
b34976b6 711 TRUE, /* partial_inplace */
cf9ab45b
AM
712 0xffff, /* src_mask */
713 0xffff, /* dst_mask */
b34976b6 714 FALSE), /* pcrel_offset */
c5930ee6 715
5f771d47 716 EMPTY_HOWTO (7),
c5930ee6 717
252b5132 718 /* Non modifiable absolute branch. */
cf9ab45b
AM
719 HOWTO (R_BA, /* type */
720 0, /* rightshift */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
722 26, /* bitsize */
b34976b6 723 FALSE, /* pc_relative */
cf9ab45b 724 0, /* bitpos */
252b5132 725 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
726 0, /* special_function */
727 "R_BA_26", /* name */
b34976b6 728 TRUE, /* partial_inplace */
a78eab4e 729 0x03fffffc, /* src_mask */
48bfecdd 730 0x03fffffc, /* dst_mask */
b34976b6 731 FALSE), /* pcrel_offset */
c5930ee6 732
5f771d47 733 EMPTY_HOWTO (9),
252b5132
RH
734
735 /* Non modifiable relative branch. */
cf9ab45b
AM
736 HOWTO (R_BR, /* type */
737 0, /* rightshift */
738 2, /* size (0 = byte, 1 = short, 2 = long) */
739 26, /* bitsize */
b34976b6 740 TRUE, /* pc_relative */
cf9ab45b 741 0, /* bitpos */
252b5132 742 complain_overflow_signed, /* complain_on_overflow */
cf9ab45b
AM
743 0, /* special_function */
744 "R_BR", /* name */
b34976b6 745 TRUE, /* partial_inplace */
a78eab4e 746 0x03fffffc, /* src_mask */
48bfecdd 747 0x03fffffc, /* dst_mask */
b34976b6 748 FALSE), /* pcrel_offset */
c5930ee6 749
5f771d47 750 EMPTY_HOWTO (0xb),
252b5132
RH
751
752 /* Indirect load. */
cf9ab45b
AM
753 HOWTO (R_RL, /* type */
754 0, /* rightshift */
48bfecdd 755 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 756 16, /* bitsize */
b34976b6 757 FALSE, /* pc_relative */
cf9ab45b 758 0, /* bitpos */
252b5132 759 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
760 0, /* special_function */
761 "R_RL", /* name */
b34976b6 762 TRUE, /* partial_inplace */
cf9ab45b
AM
763 0xffff, /* src_mask */
764 0xffff, /* dst_mask */
b34976b6 765 FALSE), /* pcrel_offset */
c5930ee6 766
252b5132 767 /* Load address. */
cf9ab45b
AM
768 HOWTO (R_RLA, /* type */
769 0, /* rightshift */
48bfecdd 770 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 771 16, /* bitsize */
b34976b6 772 FALSE, /* pc_relative */
cf9ab45b 773 0, /* bitpos */
252b5132 774 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
775 0, /* special_function */
776 "R_RLA", /* name */
b34976b6 777 TRUE, /* partial_inplace */
cf9ab45b
AM
778 0xffff, /* src_mask */
779 0xffff, /* dst_mask */
b34976b6 780 FALSE), /* pcrel_offset */
c5930ee6 781
5f771d47 782 EMPTY_HOWTO (0xe),
c5930ee6 783
252b5132 784 /* Non-relocating reference. */
cf9ab45b
AM
785 HOWTO (R_REF, /* type */
786 0, /* rightshift */
787 2, /* size (0 = byte, 1 = short, 2 = long) */
788 32, /* bitsize */
b34976b6 789 FALSE, /* pc_relative */
cf9ab45b 790 0, /* bitpos */
48bfecdd 791 complain_overflow_dont, /* complain_on_overflow */
cf9ab45b
AM
792 0, /* special_function */
793 "R_REF", /* name */
b34976b6 794 FALSE, /* partial_inplace */
cf9ab45b
AM
795 0, /* src_mask */
796 0, /* dst_mask */
b34976b6 797 FALSE), /* pcrel_offset */
c5930ee6 798
5f771d47
ILT
799 EMPTY_HOWTO (0x10),
800 EMPTY_HOWTO (0x11),
c5930ee6 801
252b5132 802 /* TOC relative indirect load. */
cf9ab45b
AM
803 HOWTO (R_TRL, /* type */
804 0, /* rightshift */
48bfecdd 805 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 806 16, /* bitsize */
b34976b6 807 FALSE, /* pc_relative */
cf9ab45b 808 0, /* bitpos */
252b5132 809 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
810 0, /* special_function */
811 "R_TRL", /* name */
b34976b6 812 TRUE, /* partial_inplace */
cf9ab45b
AM
813 0xffff, /* src_mask */
814 0xffff, /* dst_mask */
b34976b6 815 FALSE), /* pcrel_offset */
c5930ee6 816
252b5132 817 /* TOC relative load address. */
cf9ab45b
AM
818 HOWTO (R_TRLA, /* type */
819 0, /* rightshift */
48bfecdd 820 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 821 16, /* bitsize */
b34976b6 822 FALSE, /* pc_relative */
cf9ab45b 823 0, /* bitpos */
252b5132 824 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
825 0, /* special_function */
826 "R_TRLA", /* name */
b34976b6 827 TRUE, /* partial_inplace */
cf9ab45b
AM
828 0xffff, /* src_mask */
829 0xffff, /* dst_mask */
b34976b6 830 FALSE), /* pcrel_offset */
c5930ee6 831
252b5132 832 /* Modifiable relative branch. */
cf9ab45b
AM
833 HOWTO (R_RRTBI, /* type */
834 1, /* rightshift */
835 2, /* size (0 = byte, 1 = short, 2 = long) */
836 32, /* bitsize */
b34976b6 837 FALSE, /* pc_relative */
cf9ab45b 838 0, /* bitpos */
252b5132 839 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
840 0, /* special_function */
841 "R_RRTBI", /* name */
b34976b6 842 TRUE, /* partial_inplace */
cf9ab45b
AM
843 0xffffffff, /* src_mask */
844 0xffffffff, /* dst_mask */
b34976b6 845 FALSE), /* pcrel_offset */
c5930ee6 846
252b5132 847 /* Modifiable absolute branch. */
cf9ab45b
AM
848 HOWTO (R_RRTBA, /* type */
849 1, /* rightshift */
850 2, /* size (0 = byte, 1 = short, 2 = long) */
851 32, /* bitsize */
b34976b6 852 FALSE, /* pc_relative */
cf9ab45b 853 0, /* bitpos */
252b5132 854 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
855 0, /* special_function */
856 "R_RRTBA", /* name */
b34976b6 857 TRUE, /* partial_inplace */
cf9ab45b
AM
858 0xffffffff, /* src_mask */
859 0xffffffff, /* dst_mask */
b34976b6 860 FALSE), /* pcrel_offset */
c5930ee6 861
252b5132 862 /* Modifiable call absolute indirect. */
cf9ab45b
AM
863 HOWTO (R_CAI, /* type */
864 0, /* rightshift */
48bfecdd 865 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 866 16, /* bitsize */
b34976b6 867 FALSE, /* pc_relative */
cf9ab45b 868 0, /* bitpos */
252b5132 869 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
870 0, /* special_function */
871 "R_CAI", /* name */
b34976b6 872 TRUE, /* partial_inplace */
cf9ab45b
AM
873 0xffff, /* src_mask */
874 0xffff, /* dst_mask */
b34976b6 875 FALSE), /* pcrel_offset */
c5930ee6 876
252b5132 877 /* Modifiable call relative. */
cf9ab45b
AM
878 HOWTO (R_CREL, /* type */
879 0, /* rightshift */
48bfecdd 880 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 881 16, /* bitsize */
b34976b6 882 FALSE, /* pc_relative */
cf9ab45b 883 0, /* bitpos */
252b5132 884 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
885 0, /* special_function */
886 "R_CREL", /* name */
b34976b6 887 TRUE, /* partial_inplace */
cf9ab45b
AM
888 0xffff, /* src_mask */
889 0xffff, /* dst_mask */
b34976b6 890 FALSE), /* pcrel_offset */
c5930ee6 891
252b5132 892 /* Modifiable branch absolute. */
cf9ab45b
AM
893 HOWTO (R_RBA, /* type */
894 0, /* rightshift */
895 2, /* size (0 = byte, 1 = short, 2 = long) */
896 26, /* bitsize */
b34976b6 897 FALSE, /* pc_relative */
cf9ab45b 898 0, /* bitpos */
252b5132 899 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
900 0, /* special_function */
901 "R_RBA", /* name */
b34976b6 902 TRUE, /* partial_inplace */
a78eab4e 903 0x03fffffc, /* src_mask */
48bfecdd 904 0x03fffffc, /* dst_mask */
b34976b6 905 FALSE), /* pcrel_offset */
c5930ee6 906
252b5132 907 /* Modifiable branch absolute. */
cf9ab45b
AM
908 HOWTO (R_RBAC, /* type */
909 0, /* rightshift */
910 2, /* size (0 = byte, 1 = short, 2 = long) */
911 32, /* bitsize */
b34976b6 912 FALSE, /* pc_relative */
cf9ab45b 913 0, /* bitpos */
252b5132 914 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
915 0, /* special_function */
916 "R_RBAC", /* name */
b34976b6 917 TRUE, /* partial_inplace */
a78eab4e 918 0xffffffff, /* src_mask */
48bfecdd 919 0xffffffff, /* dst_mask */
b34976b6 920 FALSE), /* pcrel_offset */
c5930ee6 921
252b5132 922 /* Modifiable branch relative. */
cf9ab45b
AM
923 HOWTO (R_RBR, /* type */
924 0, /* rightshift */
925 2, /* size (0 = byte, 1 = short, 2 = long) */
926 26, /* bitsize */
b34976b6 927 FALSE, /* pc_relative */
cf9ab45b 928 0, /* bitpos */
252b5132 929 complain_overflow_signed, /* complain_on_overflow */
cf9ab45b
AM
930 0, /* special_function */
931 "R_RBR_26", /* name */
b34976b6 932 TRUE, /* partial_inplace */
a78eab4e 933 0x03fffffc, /* src_mask */
48bfecdd 934 0x03fffffc, /* dst_mask */
b34976b6 935 FALSE), /* pcrel_offset */
c5930ee6 936
252b5132 937 /* Modifiable branch absolute. */
cf9ab45b
AM
938 HOWTO (R_RBRC, /* type */
939 0, /* rightshift */
48bfecdd 940 1, /* size (0 = byte, 1 = short, 2 = long) */
cf9ab45b 941 16, /* bitsize */
b34976b6 942 FALSE, /* pc_relative */
cf9ab45b 943 0, /* bitpos */
252b5132 944 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
945 0, /* special_function */
946 "R_RBRC", /* name */
b34976b6 947 TRUE, /* partial_inplace */
cf9ab45b
AM
948 0xffff, /* src_mask */
949 0xffff, /* dst_mask */
b34976b6 950 FALSE), /* pcrel_offset */
beb1bf64 951
ff3a6ee3 952 /* 16 bit Non modifiable absolute branch. */
cf9ab45b
AM
953 HOWTO (R_BA, /* type */
954 0, /* rightshift */
955 1, /* size (0 = byte, 1 = short, 2 = long) */
956 16, /* bitsize */
b34976b6 957 FALSE, /* pc_relative */
cf9ab45b 958 0, /* bitpos */
ff3a6ee3 959 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
960 0, /* special_function */
961 "R_BA_16", /* name */
b34976b6 962 TRUE, /* partial_inplace */
cf9ab45b
AM
963 0xfffc, /* src_mask */
964 0xfffc, /* dst_mask */
b34976b6 965 FALSE), /* pcrel_offset */
59862849
TR
966
967 /* Modifiable branch relative. */
cf9ab45b
AM
968 HOWTO (R_RBR, /* type */
969 0, /* rightshift */
970 1, /* size (0 = byte, 1 = short, 2 = long) */
971 16, /* bitsize */
b34976b6 972 FALSE, /* pc_relative */
cf9ab45b 973 0, /* bitpos */
59862849 974 complain_overflow_signed, /* complain_on_overflow */
cf9ab45b
AM
975 0, /* special_function */
976 "R_RBR_16", /* name */
b34976b6 977 TRUE, /* partial_inplace */
cf9ab45b
AM
978 0xffff, /* src_mask */
979 0xffff, /* dst_mask */
b34976b6 980 FALSE), /* pcrel_offset */
59862849 981
1b164155 982 /* Modifiable branch relative. */
cf9ab45b
AM
983 HOWTO (R_RBA, /* type */
984 0, /* rightshift */
985 1, /* size (0 = byte, 1 = short, 2 = long) */
986 16, /* bitsize */
b34976b6 987 FALSE, /* pc_relative */
cf9ab45b 988 0, /* bitpos */
1b164155 989 complain_overflow_signed, /* complain_on_overflow */
cf9ab45b
AM
990 0, /* special_function */
991 "R_RBA_16", /* name */
b34976b6 992 TRUE, /* partial_inplace */
cf9ab45b
AM
993 0xffff, /* src_mask */
994 0xffff, /* dst_mask */
b34976b6 995 FALSE), /* pcrel_offset */
1b164155 996
252b5132
RH
997};
998
7f6d05e8 999void
59862849 1000xcoff_rtype2howto (relent, internal)
252b5132
RH
1001 arelent *relent;
1002 struct internal_reloc *internal;
1003{
59862849 1004 if (internal->r_type > R_RBRC)
beb1bf64 1005 abort ();
5ea1af0d 1006
59862849
TR
1007 /* Default howto layout works most of the time */
1008 relent->howto = &xcoff_howto_table[internal->r_type];
cf9ab45b 1009
5c4491d3 1010 /* Special case some 16 bit reloc */
59862849
TR
1011 if (15 == (internal->r_size & 0x1f))
1012 {
cf9ab45b 1013 if (R_BA == internal->r_type)
59862849 1014 relent->howto = &xcoff_howto_table[0x1c];
cf9ab45b 1015 else if (R_RBR == internal->r_type)
59862849 1016 relent->howto = &xcoff_howto_table[0x1d];
cf9ab45b 1017 else if (R_RBA == internal->r_type)
1b164155 1018 relent->howto = &xcoff_howto_table[0x1e];
59862849 1019 }
cf9ab45b 1020
252b5132
RH
1021 /* The r_size field of an XCOFF reloc encodes the bitsize of the
1022 relocation, as well as indicating whether it is signed or not.
1023 Doublecheck that the relocation information gathered from the
c5930ee6
KH
1024 type matches this information. The bitsize is not significant
1025 for R_REF relocs. */
1026 if (relent->howto->dst_mask != 0
dc810e39 1027 && (relent->howto->bitsize
59862849 1028 != ((unsigned int) internal->r_size & 0x1f) + 1))
252b5132 1029 abort ();
252b5132
RH
1030}
1031
7f6d05e8
CP
1032reloc_howto_type *
1033_bfd_xcoff_reloc_type_lookup (abfd, code)
5f771d47 1034 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1035 bfd_reloc_code_real_type code;
1036{
1037 switch (code)
1038 {
1039 case BFD_RELOC_PPC_B26:
1040 return &xcoff_howto_table[0xa];
ff3a6ee3 1041 case BFD_RELOC_PPC_BA16:
59862849 1042 return &xcoff_howto_table[0x1c];
252b5132
RH
1043 case BFD_RELOC_PPC_BA26:
1044 return &xcoff_howto_table[8];
1045 case BFD_RELOC_PPC_TOC16:
1046 return &xcoff_howto_table[3];
1047 case BFD_RELOC_32:
1048 case BFD_RELOC_CTOR:
1049 return &xcoff_howto_table[0];
1050 default:
1051 return NULL;
1052 }
1053}
beb1bf64 1054
157090f7
AM
1055static reloc_howto_type *
1056_bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1057 const char *r_name)
1058{
1059 unsigned int i;
1060
1061 for (i = 0;
1062 i < sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]);
1063 i++)
1064 if (xcoff_howto_table[i].name != NULL
1065 && strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
1066 return &xcoff_howto_table[i];
1067
1068 return NULL;
1069}
252b5132
RH
1070\f
1071/* XCOFF archive support. The original version of this code was by
1072 Damon A. Permezel. It was enhanced to permit cross support, and
1073 writing archive files, by Ian Lance Taylor, Cygnus Support.
1074
1075 XCOFF uses its own archive format. Everything is hooked together
1076 with file offset links, so it is possible to rapidly update an
1077 archive in place. Of course, we don't do that. An XCOFF archive
1078 has a real file header, not just an ARMAG string. The structure of
1079 the file header and of each archive header appear below.
1080
1081 An XCOFF archive also has a member table, which is a list of
1082 elements in the archive (you can get that by looking through the
1083 linked list, but you have to read a lot more of the file). The
1084 member table has a normal archive header with an empty name. It is
1085 normally (and perhaps must be) the second to last entry in the
1086 archive. The member table data is almost printable ASCII. It
1087 starts with a 12 character decimal string which is the number of
1088 entries in the table. For each entry it has a 12 character decimal
1089 string which is the offset in the archive of that member. These
1090 entries are followed by a series of null terminated strings which
1091 are the member names for each entry.
1092
1093 Finally, an XCOFF archive has a global symbol table, which is what
1094 we call the armap. The global symbol table has a normal archive
1095 header with an empty name. It is normally (and perhaps must be)
1096 the last entry in the archive. The contents start with a four byte
1097 binary number which is the number of entries. This is followed by
1098 a that many four byte binary numbers; each is the file offset of an
1099 entry in the archive. These numbers are followed by a series of
5ea1af0d
GK
1100 null terminated strings, which are symbol names.
1101
1102 AIX 4.3 introduced a new archive format which can handle larger
1103 files and also 32- and 64-bit objects in the same archive. The
1104 things said above remain true except that there is now more than
1105 one global symbol table. The one is used to index 32-bit objects,
1106 the other for 64-bit objects.
1107
1108 The new archives (recognizable by the new ARMAG string) has larger
1109 field lengths so that we cannot really share any code. Also we have
1110 to take care that we are not generating the new form of archives
1111 on AIX 4.2 or earlier systems. */
252b5132 1112
5ea1af0d
GK
1113/* XCOFF archives use this as a magic string. Note that both strings
1114 have the same length. */
252b5132 1115
eb1e0e80 1116/* Set the magic for archive. */
252b5132 1117
b34976b6 1118bfd_boolean
eb1e0e80
NC
1119bfd_xcoff_ar_archive_set_magic (abfd, magic)
1120 bfd *abfd ATTRIBUTE_UNUSED;
1121 char *magic ATTRIBUTE_UNUSED;
1122{
1123 /* Not supported yet. */
b34976b6 1124 return FALSE;
eb1e0e80
NC
1125 /* bfd_xcoff_archive_set_magic (abfd, magic); */
1126}
252b5132 1127
252b5132
RH
1128/* Read in the armap of an XCOFF archive. */
1129
b34976b6 1130bfd_boolean
7f6d05e8 1131_bfd_xcoff_slurp_armap (abfd)
252b5132
RH
1132 bfd *abfd;
1133{
1134 file_ptr off;
252b5132
RH
1135 size_t namlen;
1136 bfd_size_type sz;
1137 bfd_byte *contents, *cend;
31612ca6 1138 bfd_vma c, i;
252b5132
RH
1139 carsym *arsym;
1140 bfd_byte *p;
1141
1142 if (xcoff_ardata (abfd) == NULL)
1143 {
b34976b6
AM
1144 bfd_has_map (abfd) = FALSE;
1145 return TRUE;
252b5132
RH
1146 }
1147
5ea1af0d 1148 if (! xcoff_big_format_p (abfd))
252b5132 1149 {
5ea1af0d
GK
1150 /* This is for the old format. */
1151 struct xcoff_ar_hdr hdr;
1152
1153 off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
1154 if (off == 0)
1155 {
b34976b6
AM
1156 bfd_has_map (abfd) = FALSE;
1157 return TRUE;
5ea1af0d
GK
1158 }
1159
1160 if (bfd_seek (abfd, off, SEEK_SET) != 0)
b34976b6 1161 return FALSE;
5ea1af0d
GK
1162
1163 /* The symbol table starts with a normal archive header. */
dc810e39
AM
1164 if (bfd_bread ((PTR) &hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1165 != SIZEOF_AR_HDR)
b34976b6 1166 return FALSE;
5ea1af0d
GK
1167
1168 /* Skip the name (normally empty). */
1169 namlen = strtol (hdr.namlen, (char **) NULL, 10);
dc810e39
AM
1170 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1171 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
b34976b6 1172 return FALSE;
5ea1af0d
GK
1173
1174 sz = strtol (hdr.size, (char **) NULL, 10);
31612ca6
GK
1175
1176 /* Read in the entire symbol table. */
1177 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1178 if (contents == NULL)
b34976b6 1179 return FALSE;
dc810e39 1180 if (bfd_bread ((PTR) contents, sz, abfd) != sz)
b34976b6 1181 return FALSE;
31612ca6
GK
1182
1183 /* The symbol table starts with a four byte count. */
dc810e39
AM
1184 c = H_GET_32 (abfd, contents);
1185
31612ca6
GK
1186 if (c * 4 >= sz)
1187 {
1188 bfd_set_error (bfd_error_bad_value);
b34976b6 1189 return FALSE;
31612ca6 1190 }
dc810e39
AM
1191
1192 bfd_ardata (abfd)->symdefs =
1193 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
31612ca6 1194 if (bfd_ardata (abfd)->symdefs == NULL)
b34976b6 1195 return FALSE;
dc810e39 1196
31612ca6
GK
1197 /* After the count comes a list of four byte file offsets. */
1198 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1199 i < c;
1200 ++i, ++arsym, p += 4)
dc810e39 1201 arsym->file_offset = H_GET_32 (abfd, p);
252b5132 1202 }
5ea1af0d
GK
1203 else
1204 {
1205 /* This is for the new format. */
1206 struct xcoff_ar_hdr_big hdr;
252b5132 1207
5ea1af0d
GK
1208 off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
1209 if (off == 0)
1210 {
b34976b6
AM
1211 bfd_has_map (abfd) = FALSE;
1212 return TRUE;
5ea1af0d 1213 }
252b5132 1214
5ea1af0d 1215 if (bfd_seek (abfd, off, SEEK_SET) != 0)
b34976b6 1216 return FALSE;
252b5132 1217
5ea1af0d 1218 /* The symbol table starts with a normal archive header. */
dc810e39 1219 if (bfd_bread ((PTR) &hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
5ea1af0d 1220 != SIZEOF_AR_HDR_BIG)
b34976b6 1221 return FALSE;
5ea1af0d
GK
1222
1223 /* Skip the name (normally empty). */
1224 namlen = strtol (hdr.namlen, (char **) NULL, 10);
dc810e39
AM
1225 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1226 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
b34976b6 1227 return FALSE;
5ea1af0d
GK
1228
1229 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1230 machines) since the field width is 20 and there numbers with more
1231 than 32 bits can be represented. */
1232 sz = strtol (hdr.size, (char **) NULL, 10);
252b5132 1233
31612ca6
GK
1234 /* Read in the entire symbol table. */
1235 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1236 if (contents == NULL)
b34976b6 1237 return FALSE;
dc810e39 1238 if (bfd_bread ((PTR) contents, sz, abfd) != sz)
b34976b6 1239 return FALSE;
252b5132 1240
31612ca6 1241 /* The symbol table starts with an eight byte count. */
dc810e39 1242 c = H_GET_64 (abfd, contents);
252b5132 1243
31612ca6
GK
1244 if (c * 8 >= sz)
1245 {
1246 bfd_set_error (bfd_error_bad_value);
b34976b6 1247 return FALSE;
31612ca6 1248 }
dc810e39
AM
1249
1250 bfd_ardata (abfd)->symdefs =
1251 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
31612ca6 1252 if (bfd_ardata (abfd)->symdefs == NULL)
b34976b6 1253 return FALSE;
dc810e39 1254
31612ca6
GK
1255 /* After the count comes a list of eight byte file offsets. */
1256 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1257 i < c;
1258 ++i, ++arsym, p += 8)
dc810e39 1259 arsym->file_offset = H_GET_64 (abfd, p);
252b5132
RH
1260 }
1261
252b5132
RH
1262 /* After the file offsets come null terminated symbol names. */
1263 cend = contents + sz;
1264 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1265 i < c;
1266 ++i, ++arsym, p += strlen ((char *) p) + 1)
1267 {
1268 if (p >= cend)
1269 {
1270 bfd_set_error (bfd_error_bad_value);
b34976b6 1271 return FALSE;
252b5132
RH
1272 }
1273 arsym->name = (char *) p;
1274 }
1275
1276 bfd_ardata (abfd)->symdef_count = c;
b34976b6 1277 bfd_has_map (abfd) = TRUE;
252b5132 1278
b34976b6 1279 return TRUE;
252b5132
RH
1280}
1281
1282/* See if this is an XCOFF archive. */
1283
7f6d05e8
CP
1284const bfd_target *
1285_bfd_xcoff_archive_p (abfd)
252b5132
RH
1286 bfd *abfd;
1287{
487e54f2 1288 struct artdata *tdata_hold;
5ea1af0d 1289 char magic[SXCOFFARMAG];
487e54f2 1290 bfd_size_type amt = SXCOFFARMAG;
252b5132 1291
487e54f2 1292 if (bfd_bread ((PTR) magic, amt, abfd) != amt)
252b5132
RH
1293 {
1294 if (bfd_get_error () != bfd_error_system_call)
1295 bfd_set_error (bfd_error_wrong_format);
1296 return NULL;
1297 }
1298
5ea1af0d
GK
1299 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1300 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
252b5132
RH
1301 {
1302 bfd_set_error (bfd_error_wrong_format);
1303 return NULL;
1304 }
1305
487e54f2
AM
1306 tdata_hold = bfd_ardata (abfd);
1307
dc810e39 1308 amt = sizeof (struct artdata);
487e54f2 1309 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
252b5132 1310 if (bfd_ardata (abfd) == (struct artdata *) NULL)
487e54f2 1311 goto error_ret_restore;
252b5132 1312
9e492e05
JJ
1313 /* Cleared by bfd_zalloc above.
1314 bfd_ardata (abfd)->cache = NULL;
1315 bfd_ardata (abfd)->archive_head = NULL;
1316 bfd_ardata (abfd)->symdefs = NULL;
1317 bfd_ardata (abfd)->extended_names = NULL;
1318 bfd_ardata (abfd)->extended_names_size = 0; */
252b5132 1319
5ea1af0d
GK
1320 /* Now handle the two formats. */
1321 if (magic[1] != 'b')
1322 {
1323 /* This is the old format. */
1324 struct xcoff_ar_file_hdr hdr;
252b5132 1325
5ea1af0d
GK
1326 /* Copy over the magic string. */
1327 memcpy (hdr.magic, magic, SXCOFFARMAG);
1328
1329 /* Now read the rest of the file header. */
487e54f2
AM
1330 amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
1331 if (bfd_bread ((PTR) &hdr.memoff, amt, abfd) != amt)
5ea1af0d
GK
1332 {
1333 if (bfd_get_error () != bfd_error_system_call)
1334 bfd_set_error (bfd_error_wrong_format);
487e54f2 1335 goto error_ret;
5ea1af0d
GK
1336 }
1337
1338 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1339 (char **) NULL, 10);
1340
dc810e39
AM
1341 amt = SIZEOF_AR_FILE_HDR;
1342 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
5ea1af0d 1343 if (bfd_ardata (abfd)->tdata == NULL)
487e54f2 1344 goto error_ret;
5ea1af0d
GK
1345
1346 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1347 }
1348 else
1349 {
1350 /* This is the new format. */
1351 struct xcoff_ar_file_hdr_big hdr;
1352
1353 /* Copy over the magic string. */
1354 memcpy (hdr.magic, magic, SXCOFFARMAG);
1355
1356 /* Now read the rest of the file header. */
487e54f2
AM
1357 amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
1358 if (bfd_bread ((PTR) &hdr.memoff, amt, abfd) != amt)
5ea1af0d
GK
1359 {
1360 if (bfd_get_error () != bfd_error_system_call)
1361 bfd_set_error (bfd_error_wrong_format);
487e54f2 1362 goto error_ret;
5ea1af0d
GK
1363 }
1364
487e54f2
AM
1365 bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
1366 (const char **) 0,
1367 10);
5ea1af0d 1368
dc810e39
AM
1369 amt = SIZEOF_AR_FILE_HDR_BIG;
1370 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
5ea1af0d 1371 if (bfd_ardata (abfd)->tdata == NULL)
487e54f2 1372 goto error_ret;
5ea1af0d
GK
1373
1374 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1375 }
252b5132 1376
7f6d05e8 1377 if (! _bfd_xcoff_slurp_armap (abfd))
252b5132 1378 {
487e54f2 1379 error_ret:
252b5132 1380 bfd_release (abfd, bfd_ardata (abfd));
487e54f2
AM
1381 error_ret_restore:
1382 bfd_ardata (abfd) = tdata_hold;
252b5132
RH
1383 return NULL;
1384 }
1385
1386 return abfd->xvec;
1387}
1388
1389/* Read the archive header in an XCOFF archive. */
1390
7f6d05e8
CP
1391PTR
1392_bfd_xcoff_read_ar_hdr (abfd)
252b5132
RH
1393 bfd *abfd;
1394{
dc810e39 1395 bfd_size_type namlen;
252b5132 1396 struct areltdata *ret;
dc810e39 1397 bfd_size_type amt = sizeof (struct areltdata);
252b5132 1398
dc810e39 1399 ret = (struct areltdata *) bfd_alloc (abfd, amt);
252b5132
RH
1400 if (ret == NULL)
1401 return NULL;
5ea1af0d
GK
1402
1403 if (! xcoff_big_format_p (abfd))
1404 {
1405 struct xcoff_ar_hdr hdr;
1406 struct xcoff_ar_hdr *hdrp;
1407
dc810e39
AM
1408 if (bfd_bread ((PTR) &hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1409 != SIZEOF_AR_HDR)
5ea1af0d
GK
1410 {
1411 free (ret);
1412 return NULL;
1413 }
1414
1415 namlen = strtol (hdr.namlen, (char **) NULL, 10);
dc810e39
AM
1416 amt = SIZEOF_AR_HDR + namlen + 1;
1417 hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd, amt);
5ea1af0d
GK
1418 if (hdrp == NULL)
1419 {
1420 free (ret);
1421 return NULL;
1422 }
1423 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
dc810e39 1424 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
5ea1af0d
GK
1425 {
1426 free (ret);
1427 return NULL;
1428 }
1429 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1430
1431 ret->arch_header = (char *) hdrp;
1432 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1433 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1434 }
1435 else
1436 {
1437 struct xcoff_ar_hdr_big hdr;
1438 struct xcoff_ar_hdr_big *hdrp;
1439
dc810e39 1440 if (bfd_bread ((PTR) &hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
5ea1af0d
GK
1441 != SIZEOF_AR_HDR_BIG)
1442 {
1443 free (ret);
1444 return NULL;
1445 }
1446
1447 namlen = strtol (hdr.namlen, (char **) NULL, 10);
dc810e39
AM
1448 amt = SIZEOF_AR_HDR_BIG + namlen + 1;
1449 hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd, amt);
5ea1af0d
GK
1450 if (hdrp == NULL)
1451 {
1452 free (ret);
1453 return NULL;
1454 }
1455 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
dc810e39 1456 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
5ea1af0d
GK
1457 {
1458 free (ret);
1459 return NULL;
1460 }
1461 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1462
1463 ret->arch_header = (char *) hdrp;
1464 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1465 machines) since the field width is 20 and there numbers with more
1466 than 32 bits can be represented. */
1467 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1468 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1469 }
252b5132
RH
1470
1471 /* Skip over the XCOFFARFMAG at the end of the file name. */
dc810e39 1472 if (bfd_seek (abfd, (file_ptr) ((namlen & 1) + SXCOFFARFMAG), SEEK_CUR) != 0)
252b5132
RH
1473 return NULL;
1474
1475 return (PTR) ret;
1476}
1477
1478/* Open the next element in an XCOFF archive. */
1479
7f6d05e8
CP
1480bfd *
1481_bfd_xcoff_openr_next_archived_file (archive, last_file)
252b5132
RH
1482 bfd *archive;
1483 bfd *last_file;
1484{
1485 file_ptr filestart;
1486
1487 if (xcoff_ardata (archive) == NULL)
1488 {
1489 bfd_set_error (bfd_error_invalid_operation);
1490 return NULL;
1491 }
1492
5ea1af0d
GK
1493 if (! xcoff_big_format_p (archive))
1494 {
1495 if (last_file == NULL)
1496 filestart = bfd_ardata (archive)->first_file_filepos;
1497 else
1498 filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
1499 10);
1500
1501 if (filestart == 0
1502 || filestart == strtol (xcoff_ardata (archive)->memoff,
1503 (char **) NULL, 10)
1504 || filestart == strtol (xcoff_ardata (archive)->symoff,
1505 (char **) NULL, 10))
1506 {
1507 bfd_set_error (bfd_error_no_more_archived_files);
1508 return NULL;
1509 }
1510 }
252b5132 1511 else
252b5132 1512 {
5ea1af0d
GK
1513 if (last_file == NULL)
1514 filestart = bfd_ardata (archive)->first_file_filepos;
1515 else
1516 /* XXX These actually have to be a calls to strtoll (at least
1517 on 32-bit machines) since the fields's width is 20 and
1518 there numbers with more than 32 bits can be represented. */
1519 filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1520 10);
1521
1522 /* XXX These actually have to be calls to strtoll (at least on 32-bit
1523 machines) since the fields's width is 20 and there numbers with more
1524 than 32 bits can be represented. */
1525 if (filestart == 0
1526 || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1527 (char **) NULL, 10)
1528 || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1529 (char **) NULL, 10))
1530 {
1531 bfd_set_error (bfd_error_no_more_archived_files);
1532 return NULL;
1533 }
252b5132
RH
1534 }
1535
1536 return _bfd_get_elt_at_filepos (archive, filestart);
1537}
1538
1539/* Stat an element in an XCOFF archive. */
1540
7f6d05e8 1541int
51b9608c 1542_bfd_xcoff_stat_arch_elt (abfd, s)
252b5132
RH
1543 bfd *abfd;
1544 struct stat *s;
1545{
252b5132
RH
1546 if (abfd->arelt_data == NULL)
1547 {
1548 bfd_set_error (bfd_error_invalid_operation);
1549 return -1;
1550 }
1551
51b9608c 1552 if (! xcoff_big_format_p (abfd->my_archive))
5ea1af0d
GK
1553 {
1554 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1555
1556 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1557 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1558 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1559 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1560 s->st_size = arch_eltdata (abfd)->parsed_size;
1561 }
1562 else
1563 {
1564 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
252b5132 1565
5ea1af0d
GK
1566 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1567 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1568 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1569 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1570 s->st_size = arch_eltdata (abfd)->parsed_size;
1571 }
252b5132
RH
1572
1573 return 0;
1574}
1575
1576/* Normalize a file name for inclusion in an archive. */
1577
1578static const char *
1579normalize_filename (abfd)
1580 bfd *abfd;
1581{
1582 const char *file;
1583 const char *filename;
1584
1585 file = bfd_get_filename (abfd);
1586 filename = strrchr (file, '/');
1587 if (filename != NULL)
1588 filename++;
1589 else
1590 filename = file;
1591 return filename;
1592}
1593
1594/* Write out an XCOFF armap. */
1595
b34976b6 1596static bfd_boolean
5ea1af0d 1597xcoff_write_armap_old (abfd, elength, map, orl_count, stridx)
252b5132 1598 bfd *abfd;
5f771d47 1599 unsigned int elength ATTRIBUTE_UNUSED;
252b5132
RH
1600 struct orl *map;
1601 unsigned int orl_count;
1602 int stridx;
1603{
1604 struct xcoff_ar_hdr hdr;
1605 char *p;
1606 unsigned char buf[4];
1607 bfd *sub;
1608 file_ptr fileoff;
1609 unsigned int i;
1610
1611 memset (&hdr, 0, sizeof hdr);
1612 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1613 sprintf (hdr.nextoff, "%d", 0);
330693f5 1614 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, XCOFFARMAG_ELEMENT_SIZE);
252b5132
RH
1615 sprintf (hdr.date, "%d", 0);
1616 sprintf (hdr.uid, "%d", 0);
1617 sprintf (hdr.gid, "%d", 0);
1618 sprintf (hdr.mode, "%d", 0);
1619 sprintf (hdr.namlen, "%d", 0);
1620
1621 /* We need spaces, not null bytes, in the header. */
1622 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1623 if (*p == '\0')
1624 *p = ' ';
1625
dc810e39
AM
1626 if (bfd_bwrite ((PTR) &hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1627 != SIZEOF_AR_HDR
1628 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
1629 != SXCOFFARFMAG))
b34976b6 1630 return FALSE;
5ea1af0d 1631
dc810e39
AM
1632 H_PUT_32 (abfd, orl_count, buf);
1633 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
b34976b6 1634 return FALSE;
252b5132
RH
1635
1636 sub = abfd->archive_head;
1637 fileoff = SIZEOF_AR_FILE_HDR;
1638 i = 0;
1639 while (sub != NULL && i < orl_count)
1640 {
1641 size_t namlen;
1642
dc810e39 1643 while (map[i].u.abfd == sub)
252b5132 1644 {
dc810e39
AM
1645 H_PUT_32 (abfd, fileoff, buf);
1646 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
b34976b6 1647 return FALSE;
252b5132
RH
1648 ++i;
1649 }
1650 namlen = strlen (normalize_filename (sub));
dc810e39 1651 namlen = (namlen + 1) &~ (size_t) 1;
252b5132
RH
1652 fileoff += (SIZEOF_AR_HDR
1653 + namlen
1654 + SXCOFFARFMAG
1655 + arelt_size (sub));
1656 fileoff = (fileoff + 1) &~ 1;
1657 sub = sub->next;
1658 }
1659
1660 for (i = 0; i < orl_count; i++)
1661 {
1662 const char *name;
1663 size_t namlen;
1664
1665 name = *map[i].name;
1666 namlen = strlen (name);
dc810e39 1667 if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
b34976b6 1668 return FALSE;
252b5132
RH
1669 }
1670
1671 if ((stridx & 1) != 0)
1672 {
1673 char b;
1674
1675 b = '\0';
dc810e39 1676 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
b34976b6 1677 return FALSE;
252b5132
RH
1678 }
1679
b34976b6 1680 return TRUE;
252b5132
RH
1681}
1682
330693f5
TR
1683static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
1684#define FMT20 "%-20lld"
1685#define FMT12 "%-12d"
1686#define FMT12_OCTAL "%-12o"
1687#define FMT4 "%-4d"
1688#define PRINT20(d, v) \
1689 sprintf (buff20, FMT20, (long long)(v)), \
1690 memcpy ((void *) (d), buff20, 20)
1691
1692#define PRINT12(d, v) \
1693 sprintf (buff20, FMT12, (int)(v)), \
cf9ab45b 1694 memcpy ((void *) (d), buff20, 12)
330693f5
TR
1695
1696#define PRINT12_OCTAL(d, v) \
1697 sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
1698 memcpy ((void *) (d), buff20, 12)
1699
1700#define PRINT4(d, v) \
1701 sprintf (buff20, FMT4, (int)(v)), \
cf9ab45b 1702 memcpy ((void *) (d), buff20, 4)
330693f5
TR
1703
1704#define READ20(d, v) \
1705 buff20[20] = 0, \
1706 memcpy (buff20, (d), 20), \
1dba4cb4 1707 (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
f4ffd778 1708
b34976b6 1709static bfd_boolean
eb1e0e80
NC
1710do_pad (abfd, number)
1711 bfd *abfd;
1712 unsigned int number;
1713{
1714 bfd_byte b = 0;
1715
1716 /* Limit pad to <= 4096. */
1717 if (number > 4096)
b34976b6 1718 return FALSE;
eb1e0e80
NC
1719
1720 while (number--)
1721 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
b34976b6 1722 return FALSE;
eb1e0e80 1723
b34976b6 1724 return TRUE;
eb1e0e80
NC
1725}
1726
b34976b6 1727static bfd_boolean
eb1e0e80
NC
1728do_copy (out_bfd, in_bfd)
1729 bfd *out_bfd;
1730 bfd *in_bfd;
1731{
1732 bfd_size_type remaining;
1733 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1734
1735 if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
b34976b6 1736 return FALSE;
eb1e0e80
NC
1737
1738 remaining = arelt_size (in_bfd);
1739
1740 while (remaining >= DEFAULT_BUFFERSIZE)
1741 {
1742 if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
1743 || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
b34976b6 1744 return FALSE;
eb1e0e80
NC
1745
1746 remaining -= DEFAULT_BUFFERSIZE;
1747 }
1748
1749 if (remaining)
1750 {
cf9ab45b 1751 if (bfd_bread (buffer, remaining, in_bfd) != remaining
eb1e0e80 1752 || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
b34976b6 1753 return FALSE;
eb1e0e80
NC
1754 }
1755
b34976b6 1756 return TRUE;
eb1e0e80
NC
1757}
1758
b34976b6 1759static bfd_boolean
eb1e0e80
NC
1760do_shared_object_padding (out_bfd, in_bfd, offset, ar_header_size)
1761 bfd *out_bfd;
1762 bfd *in_bfd;
9dadfa79 1763 file_ptr *offset;
eb1e0e80
NC
1764 int ar_header_size;
1765{
1766 if (bfd_check_format (in_bfd, bfd_object)
1767 && bfd_get_flavour (in_bfd) == bfd_target_xcoff_flavour
1768 && (in_bfd->flags & DYNAMIC) != 0)
1769 {
1770 bfd_size_type pad = 0;
1771 int text_align_power;
1772
1773 text_align_power = bfd_xcoff_text_align_power (in_bfd);
eb1e0e80
NC
1774
1775 pad = 1 << text_align_power;
1776 pad -= (*offset + ar_header_size) & (pad - 1);
1777
1778 if (! do_pad (out_bfd, pad))
b34976b6 1779 return FALSE;
eb1e0e80
NC
1780
1781 *offset += pad;
1782 }
1783
b34976b6 1784 return TRUE;
eb1e0e80
NC
1785}
1786
b34976b6 1787static bfd_boolean
330693f5 1788xcoff_write_armap_big (abfd, elength, map, orl_count, stridx)
252b5132 1789 bfd *abfd;
330693f5 1790 unsigned int elength ATTRIBUTE_UNUSED;
5ea1af0d
GK
1791 struct orl *map;
1792 unsigned int orl_count;
330693f5 1793 int stridx;
252b5132 1794{
330693f5
TR
1795 struct xcoff_ar_file_hdr_big *fhdr;
1796 bfd_vma i, sym_32, sym_64, str_32, str_64;
f4ffd778 1797 const bfd_arch_info_type *arch_info = NULL;
330693f5
TR
1798 bfd *current_bfd;
1799 size_t string_length;
9dadfa79 1800 file_ptr nextoff, prevoff;
cf9ab45b 1801
330693f5
TR
1802 /* First, we look through the symbols and work out which are
1803 from 32-bit objects and which from 64-bit ones. */
1804 sym_32 = sym_64 = str_32 = str_64 = 0;
252b5132 1805
330693f5
TR
1806 current_bfd = abfd->archive_head;
1807 if (current_bfd != NULL)
1808 arch_info = bfd_get_arch_info (current_bfd);
1809 i = 0;
1810 while (current_bfd != NULL && i < orl_count)
f4ffd778 1811 {
330693f5
TR
1812 while (map[i].u.abfd == current_bfd)
1813 {
1814 string_length = strlen (*map[i].name) + 1;
252b5132 1815
330693f5
TR
1816 if (arch_info->bits_per_address == 64)
1817 {
1818 sym_64++;
1819 str_64 += string_length;
1820 }
1821 else
1822 {
1823 sym_32++;
1824 str_32 += string_length;
1825 }
1826 i++;
1827 }
1828 current_bfd = current_bfd->next;
1829 if (current_bfd != NULL)
1830 arch_info = bfd_get_arch_info (current_bfd);
1831 }
5ea1af0d 1832
330693f5
TR
1833 /* A quick sanity check... */
1834 BFD_ASSERT (sym_64 + sym_32 == orl_count);
1835 /* Explicit cast to int for compiler. */
1836 BFD_ASSERT ((int)(str_64 + str_32) == stridx);
1a6df346 1837
330693f5 1838 fhdr = xcoff_ardata_big (abfd);
252b5132 1839
330693f5
TR
1840 /* xcoff_write_archive_contents_big passes nextoff in symoff. */
1841 READ20 (fhdr->memoff, prevoff);
1842 READ20 (fhdr->symoff, nextoff);
252b5132 1843
330693f5 1844 BFD_ASSERT (nextoff == bfd_tell (abfd));
5ea1af0d 1845
cf9ab45b
AM
1846 /* Write out the symbol table.
1847 Layout :
1848
330693f5 1849 standard big archive header
cf9ab45b
AM
1850 0x0000 ar_size [0x14]
1851 0x0014 ar_nxtmem [0x14]
1852 0x0028 ar_prvmem [0x14]
1853 0x003C ar_date [0x0C]
1854 0x0048 ar_uid [0x0C]
1855 0x0054 ar_gid [0x0C]
1856 0x0060 ar_mod [0x0C]
1857 0x006C ar_namelen[0x04]
1858 0x0070 ar_fmag [SXCOFFARFMAG]
1859
1860 Symbol table
1861 0x0072 num_syms [0x08], binary
1862 0x0078 offsets [0x08 * num_syms], binary
1863 0x0086 + 0x08 * num_syms names [??]
1864 ?? pad to even bytes.
330693f5
TR
1865 */
1866
cf9ab45b 1867 if (sym_32)
330693f5
TR
1868 {
1869 struct xcoff_ar_hdr_big *hdr;
f075ee0c
AM
1870 char *symbol_table;
1871 char *st;
330693f5
TR
1872 file_ptr fileoff;
1873
cf9ab45b 1874 bfd_vma symbol_table_size =
330693f5
TR
1875 SIZEOF_AR_HDR_BIG
1876 + SXCOFFARFMAG
cf9ab45b
AM
1877 + 8
1878 + 8 * sym_32
330693f5
TR
1879 + str_32 + (str_32 & 1);
1880
f075ee0c 1881 symbol_table = bfd_zmalloc (symbol_table_size);
330693f5 1882 if (symbol_table == NULL)
b34976b6 1883 return FALSE;
330693f5
TR
1884
1885 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
cf9ab45b 1886
330693f5 1887 PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
cf9ab45b 1888
330693f5
TR
1889 if (sym_64)
1890 PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
1a6df346 1891 else
330693f5
TR
1892 PRINT20 (hdr->nextoff, 0);
1893
1894 PRINT20 (hdr->prevoff, prevoff);
1895 PRINT12 (hdr->date, 0);
1896 PRINT12 (hdr->uid, 0);
1897 PRINT12 (hdr->gid, 0);
1898 PRINT12 (hdr->mode, 0);
1899 PRINT4 (hdr->namlen, 0) ;
1900
1901 st = symbol_table + SIZEOF_AR_HDR_BIG;
1902 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
1903 st += SXCOFFARFMAG;
1904
1905 bfd_h_put_64 (abfd, sym_32, st);
1906 st += 8;
cf9ab45b 1907
330693f5
TR
1908 /* loop over the 32 bit offsets */
1909 current_bfd = abfd->archive_head;
1910 if (current_bfd != NULL)
1911 arch_info = bfd_get_arch_info (current_bfd);
1912 fileoff = SIZEOF_AR_FILE_HDR_BIG;
1913 i = 0;
1914 while (current_bfd != NULL && i < orl_count)
1915 {
1916 while (map[i].u.abfd == current_bfd)
1917 {
1918 if (arch_info->bits_per_address == 32)
1919 {
1920 bfd_h_put_64 (abfd, fileoff, st);
1921 st += 8;
1922 }
1923 i++;
1924 }
1925 string_length = strlen (normalize_filename (current_bfd));
1926 string_length += string_length & 1;
1927 fileoff += (SIZEOF_AR_HDR_BIG
1928 + string_length
1929 + SXCOFFARFMAG
1930 + arelt_size (current_bfd));
1931 fileoff += fileoff & 1;
1932 current_bfd = current_bfd->next;
1933 if (current_bfd != NULL)
1934 arch_info = bfd_get_arch_info (current_bfd);
1935 }
1a6df346 1936
330693f5
TR
1937 /* loop over the 32 bit symbol names */
1938 current_bfd = abfd->archive_head;
1939 if (current_bfd != NULL)
1940 arch_info = bfd_get_arch_info (current_bfd);
1941 i = 0;
1942 while (current_bfd != NULL && i < orl_count)
1943 {
1944 while (map[i].u.abfd == current_bfd)
1945 {
1946 if (arch_info->bits_per_address == 32)
1947 {
1948 string_length = sprintf (st, "%s", *map[i].name);
1949 st += string_length + 1;
1950 }
1951 i++;
1952 }
1953 current_bfd = current_bfd->next;
1954 if (current_bfd != NULL)
1955 arch_info = bfd_get_arch_info (current_bfd);
1956 }
5ea1af0d 1957
330693f5 1958 bfd_bwrite (symbol_table, symbol_table_size, abfd);
1a6df346 1959
330693f5 1960 free (symbol_table);
5ea1af0d 1961
330693f5
TR
1962 prevoff = nextoff;
1963 nextoff = nextoff + symbol_table_size;
5ea1af0d 1964 }
cf9ab45b 1965 else
330693f5 1966 PRINT20 (fhdr->symoff, 0);
cf9ab45b
AM
1967
1968 if (sym_64)
330693f5
TR
1969 {
1970 struct xcoff_ar_hdr_big *hdr;
f075ee0c
AM
1971 char *symbol_table;
1972 char *st;
330693f5
TR
1973 file_ptr fileoff;
1974
cf9ab45b 1975 bfd_vma symbol_table_size =
330693f5
TR
1976 SIZEOF_AR_HDR_BIG
1977 + SXCOFFARFMAG
cf9ab45b
AM
1978 + 8
1979 + 8 * sym_64
330693f5
TR
1980 + str_64 + (str_64 & 1);
1981
f075ee0c 1982 symbol_table = bfd_zmalloc (symbol_table_size);
330693f5 1983 if (symbol_table == NULL)
b34976b6 1984 return FALSE;
330693f5
TR
1985
1986 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
1987
1988 PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
1989 PRINT20 (hdr->nextoff, 0);
1990 PRINT20 (hdr->prevoff, prevoff);
1991 PRINT12 (hdr->date, 0);
1992 PRINT12 (hdr->uid, 0);
1993 PRINT12 (hdr->gid, 0);
1994 PRINT12 (hdr->mode, 0);
1995 PRINT4 (hdr->namlen, 0);
1996
1997 st = symbol_table + SIZEOF_AR_HDR_BIG;
1998 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
1999 st += SXCOFFARFMAG;
2000
2001 bfd_h_put_64 (abfd, sym_64, st);
2002 st += 8;
cf9ab45b 2003
330693f5
TR
2004 /* loop over the 64 bit offsets */
2005 current_bfd = abfd->archive_head;
2006 if (current_bfd != NULL)
2007 arch_info = bfd_get_arch_info (current_bfd);
2008 fileoff = SIZEOF_AR_FILE_HDR_BIG;
2009 i = 0;
2010 while (current_bfd != NULL && i < orl_count)
1a6df346 2011 {
330693f5
TR
2012 while (map[i].u.abfd == current_bfd)
2013 {
2014 if (arch_info->bits_per_address == 64)
2015 {
2016 bfd_h_put_64 (abfd, fileoff, st);
2017 st += 8;
2018 }
2019 i++;
2020 }
2021 string_length = strlen (normalize_filename (current_bfd));
2022 string_length += string_length & 1;
2023 fileoff += (SIZEOF_AR_HDR_BIG
2024 + string_length
2025 + SXCOFFARFMAG
2026 + arelt_size (current_bfd));
2027 fileoff += fileoff & 1;
2028 current_bfd = current_bfd->next;
2029 if (current_bfd != NULL)
2030 arch_info = bfd_get_arch_info (current_bfd);
1a6df346 2031 }
330693f5
TR
2032
2033 /* loop over the 64 bit symbol names */
2034 current_bfd = abfd->archive_head;
2035 if (current_bfd != NULL)
2036 arch_info = bfd_get_arch_info (current_bfd);
2037 i = 0;
2038 while (current_bfd != NULL && i < orl_count)
1a6df346 2039 {
330693f5
TR
2040 while (map[i].u.abfd == current_bfd)
2041 {
2042 if (arch_info->bits_per_address == 64)
2043 {
2044 string_length = sprintf (st, "%s", *map[i].name);
2045 st += string_length + 1;
2046 }
2047 i++;
2048 }
2049 current_bfd = current_bfd->next;
2050 if (current_bfd != NULL)
2051 arch_info = bfd_get_arch_info (current_bfd);
1a6df346 2052 }
1a6df346 2053
330693f5
TR
2054 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2055
2056 free (symbol_table);
dc810e39 2057
330693f5
TR
2058 PRINT20 (fhdr->symoff64, nextoff);
2059 }
cf9ab45b 2060 else
330693f5 2061 PRINT20 (fhdr->symoff64, 0);
cf9ab45b 2062
b34976b6 2063 return TRUE;
1a6df346
GK
2064}
2065
b34976b6 2066bfd_boolean
7f6d05e8 2067_bfd_xcoff_write_armap (abfd, elength, map, orl_count, stridx)
5ea1af0d
GK
2068 bfd *abfd;
2069 unsigned int elength ATTRIBUTE_UNUSED;
2070 struct orl *map;
2071 unsigned int orl_count;
2072 int stridx;
2073{
2074 if (! xcoff_big_format_p (abfd))
2075 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
2076 else
2077 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
2078}
2079
2080/* Write out an XCOFF archive. We always write an entire archive,
2081 rather than fussing with the freelist and so forth. */
2082
b34976b6 2083static bfd_boolean
5ea1af0d
GK
2084xcoff_write_archive_contents_old (abfd)
2085 bfd *abfd;
2086{
2087 struct xcoff_ar_file_hdr fhdr;
dc810e39
AM
2088 bfd_size_type count;
2089 bfd_size_type total_namlen;
5ea1af0d 2090 file_ptr *offsets;
b34976b6
AM
2091 bfd_boolean makemap;
2092 bfd_boolean hasobjects;
9dadfa79 2093 file_ptr prevoff, nextoff;
5ea1af0d 2094 bfd *sub;
dc810e39 2095 size_t i;
5ea1af0d
GK
2096 struct xcoff_ar_hdr ahdr;
2097 bfd_size_type size;
2098 char *p;
330693f5 2099 char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
5ea1af0d
GK
2100
2101 memset (&fhdr, 0, sizeof fhdr);
f05742e6 2102 (void) strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
5ea1af0d
GK
2103 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
2104 sprintf (fhdr.freeoff, "%d", 0);
2105
2106 count = 0;
2107 total_namlen = 0;
2108 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2109 {
2110 ++count;
2111 total_namlen += strlen (normalize_filename (sub)) + 1;
2112 }
2113 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2114 if (offsets == NULL)
b34976b6 2115 return FALSE;
5ea1af0d 2116
dc810e39 2117 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
b34976b6 2118 return FALSE;
5ea1af0d
GK
2119
2120 makemap = bfd_has_map (abfd);
b34976b6 2121 hasobjects = FALSE;
5ea1af0d
GK
2122 prevoff = 0;
2123 nextoff = SIZEOF_AR_FILE_HDR;
2124 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
2125 {
2126 const char *name;
dc810e39 2127 bfd_size_type namlen;
252b5132
RH
2128 struct xcoff_ar_hdr *ahdrp;
2129 bfd_size_type remaining;
2130
2131 if (makemap && ! hasobjects)
2132 {
2133 if (bfd_check_format (sub, bfd_object))
b34976b6 2134 hasobjects = TRUE;
252b5132
RH
2135 }
2136
2137 name = normalize_filename (sub);
2138 namlen = strlen (name);
2139
2140 if (sub->arelt_data != NULL)
2141 ahdrp = arch_xhdr (sub);
2142 else
2143 ahdrp = NULL;
2144
2145 if (ahdrp == NULL)
2146 {
2147 struct stat s;
2148
2149 memset (&ahdr, 0, sizeof ahdr);
2150 ahdrp = &ahdr;
2151 if (stat (bfd_get_filename (sub), &s) != 0)
2152 {
2153 bfd_set_error (bfd_error_system_call);
b34976b6 2154 return FALSE;
252b5132
RH
2155 }
2156
2157 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2158 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2159 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2160 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2161 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2162
2163 if (sub->arelt_data == NULL)
2164 {
dc810e39
AM
2165 size = sizeof (struct areltdata);
2166 sub->arelt_data = bfd_alloc (sub, size);
252b5132 2167 if (sub->arelt_data == NULL)
b34976b6 2168 return FALSE;
252b5132
RH
2169 }
2170
2171 arch_eltdata (sub)->parsed_size = s.st_size;
2172 }
2173
2174 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2175 sprintf (ahdrp->namlen, "%ld", (long) namlen);
2176
2177 /* If the length of the name is odd, we write out the null byte
cf9ab45b 2178 after the name as well. */
dc810e39 2179 namlen = (namlen + 1) &~ (bfd_size_type) 1;
252b5132
RH
2180
2181 remaining = arelt_size (sub);
2182 size = (SIZEOF_AR_HDR
2183 + namlen
2184 + SXCOFFARFMAG
2185 + remaining);
2186
2187 BFD_ASSERT (nextoff == bfd_tell (abfd));
2188
2189 offsets[i] = nextoff;
2190
2191 prevoff = nextoff;
2192 nextoff += size + (size & 1);
2193
2194 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
2195
2196 /* We need spaces, not null bytes, in the header. */
2197 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
2198 if (*p == '\0')
2199 *p = ' ';
2200
dc810e39
AM
2201 if ((bfd_bwrite ((PTR) ahdrp, (bfd_size_type) SIZEOF_AR_HDR, abfd)
2202 != SIZEOF_AR_HDR)
b34976b6
AM
2203 || bfd_bwrite ((PTR) name, namlen, abfd) != namlen
2204 || bfd_bwrite ((PTR) XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG,
2205 abfd) != SXCOFFARFMAG)
2206 return FALSE;
252b5132
RH
2207
2208 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
b34976b6 2209 return FALSE;
252b5132 2210
eb1e0e80 2211 if (! do_copy (abfd, sub))
b34976b6 2212 return FALSE;
cf9ab45b 2213
eb1e0e80 2214 if (! do_pad (abfd, size & 1))
b34976b6 2215 return FALSE;
252b5132
RH
2216 }
2217
2218 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2219
2220 /* Write out the member table. */
2221
2222 BFD_ASSERT (nextoff == bfd_tell (abfd));
2223 sprintf (fhdr.memoff, "%ld", (long) nextoff);
2224
2225 memset (&ahdr, 0, sizeof ahdr);
cf9ab45b
AM
2226 sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
2227 + count * XCOFFARMAG_ELEMENT_SIZE
2228 + total_namlen));
252b5132
RH
2229 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2230 sprintf (ahdr.date, "%d", 0);
2231 sprintf (ahdr.uid, "%d", 0);
2232 sprintf (ahdr.gid, "%d", 0);
2233 sprintf (ahdr.mode, "%d", 0);
2234 sprintf (ahdr.namlen, "%d", 0);
2235
2236 size = (SIZEOF_AR_HDR
330693f5
TR
2237 + XCOFFARMAG_ELEMENT_SIZE
2238 + count * XCOFFARMAG_ELEMENT_SIZE
252b5132
RH
2239 + total_namlen
2240 + SXCOFFARFMAG);
2241
2242 prevoff = nextoff;
2243 nextoff += size + (size & 1);
2244
2245 if (makemap && hasobjects)
2246 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2247 else
2248 sprintf (ahdr.nextoff, "%d", 0);
2249
2250 /* We need spaces, not null bytes, in the header. */
2251 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2252 if (*p == '\0')
2253 *p = ' ';
2254
dc810e39
AM
2255 if ((bfd_bwrite ((PTR) &ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
2256 != SIZEOF_AR_HDR)
2257 || (bfd_bwrite ((PTR) XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
252b5132 2258 != SXCOFFARFMAG))
b34976b6 2259 return FALSE;
252b5132
RH
2260
2261 sprintf (decbuf, "%-12ld", (long) count);
330693f5
TR
2262 if (bfd_bwrite ((PTR) decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
2263 != XCOFFARMAG_ELEMENT_SIZE)
b34976b6 2264 return FALSE;
dc810e39 2265 for (i = 0; i < (size_t) count; i++)
252b5132
RH
2266 {
2267 sprintf (decbuf, "%-12ld", (long) offsets[i]);
cf9ab45b 2268 if (bfd_bwrite ((PTR) decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
330693f5 2269 abfd) != XCOFFARMAG_ELEMENT_SIZE)
b34976b6 2270 return FALSE;
252b5132
RH
2271 }
2272 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2273 {
2274 const char *name;
dc810e39 2275 bfd_size_type namlen;
252b5132
RH
2276
2277 name = normalize_filename (sub);
2278 namlen = strlen (name);
dc810e39 2279 if (bfd_bwrite ((PTR) name, namlen + 1, abfd) != namlen + 1)
b34976b6 2280 return FALSE;
252b5132 2281 }
252b5132 2282
eb1e0e80 2283 if (! do_pad (abfd, size & 1))
b34976b6 2284 return FALSE;
252b5132
RH
2285
2286 /* Write out the armap, if appropriate. */
252b5132
RH
2287 if (! makemap || ! hasobjects)
2288 sprintf (fhdr.symoff, "%d", 0);
2289 else
2290 {
2291 BFD_ASSERT (nextoff == bfd_tell (abfd));
2292 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2293 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2294 if (! _bfd_compute_and_write_armap (abfd, 0))
b34976b6 2295 return FALSE;
252b5132
RH
2296 }
2297
2298 /* Write out the archive file header. */
2299
2300 /* We need spaces, not null bytes, in the header. */
2301 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2302 if (*p == '\0')
2303 *p = ' ';
2304
2305 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
dc810e39
AM
2306 || (bfd_bwrite ((PTR) &fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR, abfd)
2307 != SIZEOF_AR_FILE_HDR))
b34976b6 2308 return FALSE;
252b5132 2309
b34976b6 2310 return TRUE;
252b5132 2311}
5ea1af0d 2312
b34976b6 2313static bfd_boolean
5ea1af0d
GK
2314xcoff_write_archive_contents_big (abfd)
2315 bfd *abfd;
2316{
2317 struct xcoff_ar_file_hdr_big fhdr;
dc810e39
AM
2318 bfd_size_type count;
2319 bfd_size_type total_namlen;
5ea1af0d 2320 file_ptr *offsets;
b34976b6
AM
2321 bfd_boolean makemap;
2322 bfd_boolean hasobjects;
9dadfa79 2323 file_ptr prevoff, nextoff;
330693f5 2324 bfd *current_bfd;
dc810e39 2325 size_t i;
330693f5 2326 struct xcoff_ar_hdr_big *hdr, ahdr;
5ea1af0d 2327 bfd_size_type size;
f075ee0c 2328 char *member_table, *mt;
330693f5 2329 bfd_vma member_table_size;
5ea1af0d 2330
eb1e0e80 2331 memset (&fhdr, 0, SIZEOF_AR_FILE_HDR_BIG);
330693f5 2332 memcpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
5ea1af0d 2333
eb1e0e80 2334 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
b34976b6 2335 return FALSE;
cf9ab45b 2336
eb1e0e80
NC
2337 /* Calculate count and total_namlen. */
2338 makemap = bfd_has_map (abfd);
b34976b6 2339 hasobjects = FALSE;
cf9ab45b
AM
2340 for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
2341 current_bfd != NULL;
330693f5 2342 current_bfd = current_bfd->next, count++)
eb1e0e80
NC
2343 {
2344 total_namlen += strlen (normalize_filename (current_bfd)) + 1;
2345
2346 if (makemap
2347 && ! hasobjects
2348 && bfd_check_format (current_bfd, bfd_object))
b34976b6 2349 hasobjects = TRUE;
eb1e0e80 2350 }
330693f5
TR
2351
2352 offsets = NULL;
2353 if (count)
5ea1af0d 2354 {
330693f5
TR
2355 offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
2356 if (offsets == NULL)
b34976b6 2357 return FALSE;
5ea1af0d 2358 }
5ea1af0d 2359
5ea1af0d
GK
2360 prevoff = 0;
2361 nextoff = SIZEOF_AR_FILE_HDR_BIG;
cf9ab45b
AM
2362 for (current_bfd = abfd->archive_head, i = 0;
2363 current_bfd != NULL;
330693f5 2364 current_bfd = current_bfd->next, i++)
5ea1af0d
GK
2365 {
2366 const char *name;
dc810e39 2367 bfd_size_type namlen;
5ea1af0d
GK
2368 struct xcoff_ar_hdr_big *ahdrp;
2369 bfd_size_type remaining;
2370
330693f5 2371 name = normalize_filename (current_bfd);
5ea1af0d
GK
2372 namlen = strlen (name);
2373
330693f5
TR
2374 if (current_bfd->arelt_data != NULL)
2375 ahdrp = arch_xhdr_big (current_bfd);
5ea1af0d
GK
2376 else
2377 ahdrp = NULL;
2378
2379 if (ahdrp == NULL)
2380 {
2381 struct stat s;
2382
5ea1af0d
GK
2383 ahdrp = &ahdr;
2384 /* XXX This should actually be a call to stat64 (at least on
cf9ab45b 2385 32-bit machines).
330693f5
TR
2386 XXX This call will fail if the original object is not found. */
2387 if (stat (bfd_get_filename (current_bfd), &s) != 0)
5ea1af0d
GK
2388 {
2389 bfd_set_error (bfd_error_system_call);
b34976b6 2390 return FALSE;
5ea1af0d
GK
2391 }
2392
330693f5
TR
2393 PRINT20 (ahdrp->size, s.st_size);
2394 PRINT12 (ahdrp->date, s.st_mtime);
2395 PRINT12 (ahdrp->uid, s.st_uid);
2396 PRINT12 (ahdrp->gid, s.st_gid);
2397 PRINT12_OCTAL (ahdrp->mode, s.st_mode);
5ea1af0d 2398
330693f5 2399 if (current_bfd->arelt_data == NULL)
5ea1af0d 2400 {
dc810e39 2401 size = sizeof (struct areltdata);
330693f5
TR
2402 current_bfd->arelt_data = bfd_alloc (current_bfd, size);
2403 if (current_bfd->arelt_data == NULL)
b34976b6 2404 return FALSE;
5ea1af0d
GK
2405 }
2406
330693f5 2407 arch_eltdata (current_bfd)->parsed_size = s.st_size;
5ea1af0d
GK
2408 }
2409
330693f5
TR
2410 PRINT20 (ahdrp->prevoff, prevoff);
2411 PRINT4 (ahdrp->namlen, namlen);
5ea1af0d
GK
2412
2413 /* If the length of the name is odd, we write out the null byte
cf9ab45b 2414 after the name as well. */
dc810e39 2415 namlen = (namlen + 1) &~ (bfd_size_type) 1;
5ea1af0d 2416
330693f5 2417 remaining = arelt_size (current_bfd);
5ea1af0d
GK
2418 size = (SIZEOF_AR_HDR_BIG
2419 + namlen
2420 + SXCOFFARFMAG
2421 + remaining);
2422
2423 BFD_ASSERT (nextoff == bfd_tell (abfd));
2424
eb1e0e80
NC
2425 /* Check for xcoff shared objects.
2426 Their text section needs to be aligned wrt the archive file position.
2427 This requires extra padding before the archive header. */
2428 if (! do_shared_object_padding (abfd, current_bfd, & nextoff,
cf9ab45b 2429 SIZEOF_AR_HDR_BIG + namlen
eb1e0e80 2430 + SXCOFFARFMAG))
b34976b6 2431 return FALSE;
eb1e0e80 2432
5ea1af0d
GK
2433 offsets[i] = nextoff;
2434
2435 prevoff = nextoff;
2436 nextoff += size + (size & 1);
2437
330693f5 2438 PRINT20 (ahdrp->nextoff, nextoff);
5ea1af0d 2439
dc810e39
AM
2440 if ((bfd_bwrite ((PTR) ahdrp, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
2441 != SIZEOF_AR_HDR_BIG)
2442 || bfd_bwrite ((PTR) name, (bfd_size_type) namlen, abfd) != namlen
cf9ab45b 2443 || (bfd_bwrite ((PTR) XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG,
330693f5 2444 abfd) != SXCOFFARFMAG))
b34976b6 2445 return FALSE;
5ea1af0d 2446
330693f5 2447 if (bfd_seek (current_bfd, (file_ptr) 0, SEEK_SET) != 0)
b34976b6 2448 return FALSE;
5ea1af0d 2449
eb1e0e80 2450 if (! do_copy (abfd, current_bfd))
b34976b6 2451 return FALSE;
cf9ab45b 2452
eb1e0e80 2453 if (! do_pad (abfd, size & 1))
b34976b6 2454 return FALSE;
5ea1af0d
GK
2455 }
2456
eb1e0e80
NC
2457 if (count)
2458 {
2459 PRINT20 (fhdr.firstmemoff, offsets[0]);
2460 PRINT20 (fhdr.lastmemoff, prevoff);
2461 }
5ea1af0d 2462
cf9ab45b
AM
2463 /* Write out the member table.
2464 Layout :
5ea1af0d 2465
330693f5 2466 standard big archive header
cf9ab45b
AM
2467 0x0000 ar_size [0x14]
2468 0x0014 ar_nxtmem [0x14]
2469 0x0028 ar_prvmem [0x14]
2470 0x003C ar_date [0x0C]
2471 0x0048 ar_uid [0x0C]
2472 0x0054 ar_gid [0x0C]
2473 0x0060 ar_mod [0x0C]
2474 0x006C ar_namelen[0x04]
2475 0x0070 ar_fmag [0x02]
2476
2477 Member table
2478 0x0072 count [0x14]
2479 0x0086 offsets [0x14 * counts]
2480 0x0086 + 0x14 * counts names [??]
2481 ?? pad to even bytes.
330693f5 2482 */
5ea1af0d 2483
330693f5 2484 BFD_ASSERT (nextoff == bfd_tell (abfd));
5ea1af0d 2485
330693f5
TR
2486 member_table_size = (SIZEOF_AR_HDR_BIG
2487 + SXCOFFARFMAG
2488 + XCOFFARMAGBIG_ELEMENT_SIZE
2489 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2490 + total_namlen);
5ea1af0d 2491
330693f5 2492 member_table_size += member_table_size & 1;
f075ee0c 2493 member_table = bfd_zmalloc (member_table_size);
330693f5 2494 if (member_table == NULL)
b34976b6 2495 return FALSE;
5ea1af0d 2496
330693f5 2497 hdr = (struct xcoff_ar_hdr_big *) member_table;
5ea1af0d 2498
cf9ab45b
AM
2499 PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
2500 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2501 + total_namlen + (total_namlen & 1)));
2502 if (makemap && hasobjects)
330693f5
TR
2503 PRINT20 (hdr->nextoff, nextoff + member_table_size);
2504 else
2505 PRINT20 (hdr->nextoff, 0);
2506 PRINT20 (hdr->prevoff, prevoff);
2507 PRINT12 (hdr->date, 0);
2508 PRINT12 (hdr->uid, 0);
2509 PRINT12 (hdr->gid, 0);
2510 PRINT12 (hdr->mode, 0);
2511 PRINT4 (hdr->namlen, 0);
cf9ab45b 2512
330693f5
TR
2513 mt = member_table + SIZEOF_AR_HDR_BIG;
2514 memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
2515 mt += SXCOFFARFMAG;
5ea1af0d 2516
330693f5
TR
2517 PRINT20 (mt, count);
2518 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
dc810e39 2519 for (i = 0; i < (size_t) count; i++)
5ea1af0d 2520 {
330693f5
TR
2521 PRINT20 (mt, offsets[i]);
2522 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
5ea1af0d 2523 }
330693f5 2524
cf9ab45b 2525 if (count)
330693f5
TR
2526 {
2527 free (offsets);
2528 offsets = NULL;
2529 }
2530
cf9ab45b 2531 for (current_bfd = abfd->archive_head; current_bfd != NULL;
330693f5 2532 current_bfd = current_bfd->next)
5ea1af0d
GK
2533 {
2534 const char *name;
2535 size_t namlen;
2536
330693f5 2537 name = normalize_filename (current_bfd);
cf9ab45b 2538 namlen = sprintf (mt, "%s", name);
330693f5 2539 mt += namlen + 1;
5ea1af0d 2540 }
cf9ab45b 2541
330693f5 2542 if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
b34976b6 2543 return FALSE;
5ea1af0d 2544
330693f5 2545 free (member_table);
330693f5
TR
2546
2547 PRINT20 (fhdr.memoff, nextoff);
2548
2549 prevoff = nextoff;
2550 nextoff += member_table_size;
5ea1af0d
GK
2551
2552 /* Write out the armap, if appropriate. */
2553
cf9ab45b 2554 if (! makemap || ! hasobjects)
330693f5 2555 PRINT20 (fhdr.symoff, 0);
5ea1af0d
GK
2556 else
2557 {
2558 BFD_ASSERT (nextoff == bfd_tell (abfd));
330693f5
TR
2559
2560 /* Save nextoff in fhdr.symoff so the armap routine can use it. */
2561 PRINT20 (fhdr.symoff, nextoff);
cf9ab45b 2562
5ea1af0d
GK
2563 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2564 if (! _bfd_compute_and_write_armap (abfd, 0))
b34976b6 2565 return FALSE;
5ea1af0d
GK
2566 }
2567
2568 /* Write out the archive file header. */
2569
5ea1af0d 2570 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
cf9ab45b 2571 || (bfd_bwrite ((PTR) &fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR_BIG,
330693f5 2572 abfd) != SIZEOF_AR_FILE_HDR_BIG))
b34976b6 2573 return FALSE;
cf9ab45b 2574
b34976b6 2575 return TRUE;
5ea1af0d
GK
2576}
2577
b34976b6 2578bfd_boolean
7f6d05e8 2579_bfd_xcoff_write_archive_contents (abfd)
5ea1af0d
GK
2580 bfd *abfd;
2581{
2582 if (! xcoff_big_format_p (abfd))
2583 return xcoff_write_archive_contents_old (abfd);
2584 else
2585 return xcoff_write_archive_contents_big (abfd);
2586}
252b5132
RH
2587\f
2588/* We can't use the usual coff_sizeof_headers routine, because AIX
2589 always uses an a.out header. */
2590
7f6d05e8 2591int
a6b96beb
AM
2592_bfd_xcoff_sizeof_headers (bfd *abfd,
2593 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132
RH
2594{
2595 int size;
2596
2597 size = FILHSZ;
2598 if (xcoff_data (abfd)->full_aouthdr)
2599 size += AOUTSZ;
2600 else
2601 size += SMALL_AOUTSZ;
2602 size += abfd->section_count * SCNHSZ;
2603 return size;
2604}
beb1bf64
TR
2605\f
2606/* Routines to swap information in the XCOFF .loader section. If we
2607 ever need to write an XCOFF loader, this stuff will need to be
2608 moved to another file shared by the linker (which XCOFF calls the
2609 ``binder'') and the loader. */
2610
2611/* Swap in the ldhdr structure. */
2612
2613static void
814fa6ab 2614xcoff_swap_ldhdr_in (abfd, s, dst)
beb1bf64 2615 bfd *abfd;
814fa6ab 2616 const PTR s;
beb1bf64
TR
2617 struct internal_ldhdr *dst;
2618{
814fa6ab
AM
2619 const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2620
beb1bf64
TR
2621 dst->l_version = bfd_get_32 (abfd, src->l_version);
2622 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2623 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2624 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2625 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2626 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2627 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2628 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2629}
2630
2631/* Swap out the ldhdr structure. */
2632
2633static void
814fa6ab 2634xcoff_swap_ldhdr_out (abfd, src, d)
beb1bf64
TR
2635 bfd *abfd;
2636 const struct internal_ldhdr *src;
814fa6ab 2637 PTR d;
beb1bf64 2638{
814fa6ab
AM
2639 struct external_ldhdr *dst = (struct external_ldhdr *) d;
2640
dc810e39 2641 bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
beb1bf64
TR
2642 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2643 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2644 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2645 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2646 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2647 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2648 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2649}
2650
2651/* Swap in the ldsym structure. */
2652
2653static void
814fa6ab 2654xcoff_swap_ldsym_in (abfd, s, dst)
beb1bf64 2655 bfd *abfd;
814fa6ab 2656 const PTR s;
beb1bf64
TR
2657 struct internal_ldsym *dst;
2658{
814fa6ab
AM
2659 const struct external_ldsym *src = (const struct external_ldsym *) s;
2660
beb1bf64
TR
2661 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2662 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2663 } else {
2664 dst->_l._l_l._l_zeroes = 0;
2665 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2666 }
2667 dst->l_value = bfd_get_32 (abfd, src->l_value);
2668 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2669 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2670 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2671 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2672 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2673}
2674
2675/* Swap out the ldsym structure. */
2676
2677static void
814fa6ab 2678xcoff_swap_ldsym_out (abfd, src, d)
beb1bf64
TR
2679 bfd *abfd;
2680 const struct internal_ldsym *src;
814fa6ab 2681 PTR d;
beb1bf64 2682{
814fa6ab 2683 struct external_ldsym *dst = (struct external_ldsym *) d;
beb1bf64
TR
2684
2685 if (src->_l._l_l._l_zeroes != 0)
2686 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2687 else
2688 {
dc810e39
AM
2689 bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2690 bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2691 dst->_l._l_l._l_offset);
beb1bf64
TR
2692 }
2693 bfd_put_32 (abfd, src->l_value, dst->l_value);
dc810e39 2694 bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
beb1bf64
TR
2695 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2696 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2697 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2698 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2699}
2700
59862849
TR
2701static void
2702xcoff_swap_reloc_in (abfd, s, d)
2703 bfd *abfd;
2704 PTR s;
2705 PTR d;
2706{
2707 struct external_reloc *src = (struct external_reloc *) s;
2708 struct internal_reloc *dst = (struct internal_reloc *) d;
2709
2710 memset (dst, 0, sizeof (struct internal_reloc));
2711
2712 dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2713 dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2714 dst->r_size = bfd_get_8 (abfd, src->r_size);
2715 dst->r_type = bfd_get_8 (abfd, src->r_type);
2716}
2717
2718static unsigned int
2719xcoff_swap_reloc_out (abfd, s, d)
2720 bfd *abfd;
2721 PTR s;
2722 PTR d;
2723{
2724 struct internal_reloc *src = (struct internal_reloc *) s;
2725 struct external_reloc *dst = (struct external_reloc *) d;
2726
2727 bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2728 bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2729 bfd_put_8 (abfd, src->r_type, dst->r_type);
2730 bfd_put_8 (abfd, src->r_size, dst->r_size);
2731
2732 return bfd_coff_relsz (abfd);
2733}
2734
beb1bf64
TR
2735/* Swap in the ldrel structure. */
2736
2737static void
814fa6ab 2738xcoff_swap_ldrel_in (abfd, s, dst)
beb1bf64 2739 bfd *abfd;
814fa6ab 2740 const PTR s;
beb1bf64
TR
2741 struct internal_ldrel *dst;
2742{
814fa6ab
AM
2743 const struct external_ldrel *src = (const struct external_ldrel *) s;
2744
beb1bf64
TR
2745 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2746 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2747 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2748 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2749}
2750
2751/* Swap out the ldrel structure. */
2752
2753static void
814fa6ab 2754xcoff_swap_ldrel_out (abfd, src, d)
beb1bf64
TR
2755 bfd *abfd;
2756 const struct internal_ldrel *src;
814fa6ab 2757 PTR d;
beb1bf64 2758{
814fa6ab
AM
2759 struct external_ldrel *dst = (struct external_ldrel *) d;
2760
beb1bf64
TR
2761 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2762 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
dc810e39
AM
2763 bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
2764 bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
beb1bf64
TR
2765}
2766\f
2767
b34976b6 2768bfd_boolean
cf9ab45b 2769xcoff_reloc_type_noop (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2770 val, addend, relocation, contents)
2771 bfd *input_bfd ATTRIBUTE_UNUSED;
2772 asection *input_section ATTRIBUTE_UNUSED;
2773 bfd *output_bfd ATTRIBUTE_UNUSED;
2774 struct internal_reloc *rel ATTRIBUTE_UNUSED;
2775 struct internal_syment *sym ATTRIBUTE_UNUSED;
2776 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED;
2777 bfd_vma val ATTRIBUTE_UNUSED;
2778 bfd_vma addend ATTRIBUTE_UNUSED;
2779 bfd_vma *relocation ATTRIBUTE_UNUSED;
2780 bfd_byte *contents ATTRIBUTE_UNUSED;
2781{
b34976b6 2782 return TRUE;
dbe341c6
TR
2783}
2784
b34976b6 2785bfd_boolean
cf9ab45b 2786xcoff_reloc_type_fail (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2787 val, addend, relocation, contents)
2788 bfd *input_bfd;
2789 asection *input_section ATTRIBUTE_UNUSED;
2790 bfd *output_bfd ATTRIBUTE_UNUSED;
2791 struct internal_reloc *rel;
2792 struct internal_syment *sym ATTRIBUTE_UNUSED;
2793 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED;
2794 bfd_vma val ATTRIBUTE_UNUSED;
2795 bfd_vma addend ATTRIBUTE_UNUSED;
2796 bfd_vma *relocation ATTRIBUTE_UNUSED;
2797 bfd_byte *contents ATTRIBUTE_UNUSED;
2798{
2799 (*_bfd_error_handler)
2800 (_("%s: unsupported relocation type 0x%02x"),
2801 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
2802 bfd_set_error (bfd_error_bad_value);
b34976b6 2803 return FALSE;
dbe341c6
TR
2804}
2805
b34976b6 2806bfd_boolean
cf9ab45b 2807xcoff_reloc_type_pos (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2808 val, addend, relocation, contents)
2809 bfd *input_bfd ATTRIBUTE_UNUSED;
2810 asection *input_section ATTRIBUTE_UNUSED;
2811 bfd *output_bfd ATTRIBUTE_UNUSED;
2812 struct internal_reloc *rel ATTRIBUTE_UNUSED;
2813 struct internal_syment *sym ATTRIBUTE_UNUSED;
2814 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED;
2815 bfd_vma val;
2816 bfd_vma addend;
2817 bfd_vma *relocation;
2818 bfd_byte *contents ATTRIBUTE_UNUSED;
2819{
2820 *relocation = val + addend;
b34976b6 2821 return TRUE;
dbe341c6
TR
2822}
2823
b34976b6 2824bfd_boolean
cf9ab45b 2825xcoff_reloc_type_neg (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2826 val, addend, relocation, contents)
2827 bfd *input_bfd ATTRIBUTE_UNUSED;
2828 asection *input_section ATTRIBUTE_UNUSED;
2829 bfd *output_bfd ATTRIBUTE_UNUSED;
2830 struct internal_reloc *rel ATTRIBUTE_UNUSED;
2831 struct internal_syment *sym ATTRIBUTE_UNUSED;
2832 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED;
2833 bfd_vma val;
2834 bfd_vma addend;
2835 bfd_vma *relocation;
2836 bfd_byte *contents ATTRIBUTE_UNUSED;
2837{
2838 *relocation = addend - val;
b34976b6 2839 return TRUE;
dbe341c6
TR
2840}
2841
b34976b6 2842bfd_boolean
cf9ab45b 2843xcoff_reloc_type_rel (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2844 val, addend, relocation, contents)
2845 bfd *input_bfd ATTRIBUTE_UNUSED;
2846 asection *input_section;
2847 bfd *output_bfd ATTRIBUTE_UNUSED;
2848 struct internal_reloc *rel ATTRIBUTE_UNUSED;
2849 struct internal_syment *sym ATTRIBUTE_UNUSED;
2850 struct reloc_howto_struct *howto;
2851 bfd_vma val;
2852 bfd_vma addend;
2853 bfd_vma *relocation;
2854 bfd_byte *contents ATTRIBUTE_UNUSED;
2855{
b34976b6 2856 howto->pc_relative = TRUE;
dbe341c6
TR
2857
2858 /* A PC relative reloc includes the section address. */
2859 addend += input_section->vma;
2860
2861 *relocation = val + addend;
cf9ab45b
AM
2862 *relocation -= (input_section->output_section->vma
2863 + input_section->output_offset);
b34976b6 2864 return TRUE;
dbe341c6 2865}
f1f0d9ab 2866
b34976b6 2867bfd_boolean
cf9ab45b 2868xcoff_reloc_type_toc (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2869 val, addend, relocation, contents)
2870 bfd *input_bfd;
2871 asection *input_section ATTRIBUTE_UNUSED;
2872 bfd *output_bfd;
2873 struct internal_reloc *rel;
2874 struct internal_syment *sym;
2875 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED;
2876 bfd_vma val;
2877 bfd_vma addend ATTRIBUTE_UNUSED;
2878 bfd_vma *relocation;
2879 bfd_byte *contents ATTRIBUTE_UNUSED;
2880{
2881 struct xcoff_link_hash_entry *h;
2882
cf9ab45b 2883 if (0 > rel->r_symndx)
b34976b6 2884 return FALSE;
dbe341c6
TR
2885
2886 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
2887
2888 if (h != NULL && h->smclas != XMC_TD)
2889 {
2890 if (h->toc_section == NULL)
2891 {
2892 (*_bfd_error_handler)
2893 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
2894 bfd_get_filename (input_bfd), rel->r_vaddr,
2895 h->root.root.string);
2896 bfd_set_error (bfd_error_bad_value);
b34976b6 2897 return FALSE;
dbe341c6 2898 }
cf9ab45b 2899
dbe341c6
TR
2900 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
2901 val = (h->toc_section->output_section->vma
2902 + h->toc_section->output_offset);
2903 }
cf9ab45b
AM
2904
2905 *relocation = ((val - xcoff_data (output_bfd)->toc)
2906 - (sym->n_value - xcoff_data (input_bfd)->toc));
b34976b6 2907 return TRUE;
dbe341c6 2908}
f1f0d9ab 2909
b34976b6 2910bfd_boolean
cf9ab45b 2911xcoff_reloc_type_ba (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2912 val, addend, relocation, contents)
2913 bfd *input_bfd ATTRIBUTE_UNUSED;
2914 asection *input_section ATTRIBUTE_UNUSED;
2915 bfd *output_bfd ATTRIBUTE_UNUSED;
2916 struct internal_reloc *rel ATTRIBUTE_UNUSED;
2917 struct internal_syment *sym ATTRIBUTE_UNUSED;
2918 struct reloc_howto_struct *howto;
2919 bfd_vma val;
2920 bfd_vma addend;
2921 bfd_vma *relocation;
2922 bfd_byte *contents ATTRIBUTE_UNUSED;
2923{
a78eab4e
AM
2924 howto->src_mask &= ~3;
2925 howto->dst_mask = howto->src_mask;
dbe341c6
TR
2926
2927 *relocation = val + addend;
2928
b34976b6 2929 return TRUE;
dbe341c6
TR
2930}
2931
b34976b6 2932static bfd_boolean
cf9ab45b 2933xcoff_reloc_type_br (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
2934 val, addend, relocation, contents)
2935 bfd *input_bfd;
2936 asection *input_section;
2937 bfd *output_bfd ATTRIBUTE_UNUSED;
2938 struct internal_reloc *rel;
2939 struct internal_syment *sym ATTRIBUTE_UNUSED;
2940 struct reloc_howto_struct *howto;
2941 bfd_vma val;
2942 bfd_vma addend;
2943 bfd_vma *relocation;
2944 bfd_byte *contents;
2945{
2946 struct xcoff_link_hash_entry *h;
2947
cf9ab45b 2948 if (0 > rel->r_symndx)
b34976b6 2949 return FALSE;
dbe341c6
TR
2950
2951 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
2952
2953 /* If we see an R_BR or R_RBR reloc which is jumping to global
2954 linkage code, and it is followed by an appropriate cror nop
2955 instruction, we replace the cror with lwz r2,20(r1). This
2956 restores the TOC after the glink code. Contrariwise, if the
2957 call is followed by a lwz r2,20(r1), but the call is not
2958 going to global linkage code, we can replace the load with a
2959 cror. */
cf9ab45b
AM
2960 if (NULL != h
2961 && bfd_link_hash_defined == h->root.type
eea6121a 2962 && rel->r_vaddr - input_section->vma + 8 <= input_section->size)
dbe341c6
TR
2963 {
2964 bfd_byte *pnext;
2965 unsigned long next;
cf9ab45b 2966
dbe341c6
TR
2967 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
2968 next = bfd_get_32 (input_bfd, pnext);
cf9ab45b 2969
dbe341c6
TR
2970 /* The _ptrgl function is magic. It is used by the AIX
2971 compiler to call a function through a pointer. */
2972 if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
2973 {
cf9ab45b
AM
2974 if (next == 0x4def7b82 /* cror 15,15,15 */
2975 || next == 0x4ffffb82 /* cror 31,31,31 */
2976 || next == 0x60000000) /* ori r0,r0,0 */
2977 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
2978
2979 }
2980 else
2981 {
2982 if (next == 0x80410014) /* lwz r1,20(r1) */
2983 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
2984 }
2985 }
2986 else if (NULL != h && bfd_link_hash_undefined == h->root.type)
dbe341c6
TR
2987 {
2988 /* Normally, this relocation is against a defined symbol. In the
2989 case where this is a partial link and the output section offset
cf9ab45b 2990 is greater than 2^25, the linker will return an invalid error
dbe341c6 2991 message that the relocation has been truncated. Yes it has been
cf9ab45b 2992 truncated but no it not important. For this case, disable the
dbe341c6 2993 overflow checking. */
cf9ab45b 2994
dbe341c6
TR
2995 howto->complain_on_overflow = complain_overflow_dont;
2996 }
cf9ab45b 2997
b34976b6 2998 howto->pc_relative = TRUE;
a78eab4e
AM
2999 howto->src_mask &= ~3;
3000 howto->dst_mask = howto->src_mask;
dbe341c6
TR
3001
3002 /* A PC relative reloc includes the section address. */
3003 addend += input_section->vma;
cf9ab45b 3004
dbe341c6 3005 *relocation = val + addend;
cf9ab45b
AM
3006 *relocation -= (input_section->output_section->vma
3007 + input_section->output_offset);
b34976b6 3008 return TRUE;
dbe341c6
TR
3009}
3010
b34976b6 3011bfd_boolean
cf9ab45b 3012xcoff_reloc_type_crel (input_bfd, input_section, output_bfd, rel, sym, howto,
dbe341c6
TR
3013 val, addend, relocation, contents)
3014 bfd *input_bfd ATTRIBUTE_UNUSED;
3015 asection *input_section;
3016 bfd *output_bfd ATTRIBUTE_UNUSED;
3017 struct internal_reloc *rel ATTRIBUTE_UNUSED;
3018 struct internal_syment *sym ATTRIBUTE_UNUSED;
3019 struct reloc_howto_struct *howto;
3020 bfd_vma val ATTRIBUTE_UNUSED;
3021 bfd_vma addend;
3022 bfd_vma *relocation;
3023 bfd_byte *contents ATTRIBUTE_UNUSED;
3024{
b34976b6 3025 howto->pc_relative = TRUE;
a78eab4e
AM
3026 howto->src_mask &= ~3;
3027 howto->dst_mask = howto->src_mask;
dbe341c6
TR
3028
3029 /* A PC relative reloc includes the section address. */
3030 addend += input_section->vma;
3031
3032 *relocation = val + addend;
cf9ab45b
AM
3033 *relocation -= (input_section->output_section->vma
3034 + input_section->output_offset);
b34976b6 3035 return TRUE;
dbe341c6
TR
3036}
3037
b34976b6 3038static bfd_boolean
cf9ab45b 3039xcoff_complain_overflow_dont_func (input_bfd, val, relocation, howto)
dbe341c6
TR
3040 bfd *input_bfd ATTRIBUTE_UNUSED;
3041 bfd_vma val ATTRIBUTE_UNUSED;
3042 bfd_vma relocation ATTRIBUTE_UNUSED;
3043 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED;
3044{
b34976b6 3045 return FALSE;
dbe341c6
TR
3046}
3047
b34976b6 3048static bfd_boolean
cf9ab45b 3049xcoff_complain_overflow_bitfield_func (input_bfd, val, relocation, howto)
dbe341c6
TR
3050 bfd *input_bfd;
3051 bfd_vma val;
3052 bfd_vma relocation;
cf9ab45b 3053 struct reloc_howto_struct *howto;
dbe341c6
TR
3054{
3055 bfd_vma addrmask, fieldmask, signmask, ss;
3056 bfd_vma a, b, sum;
cf9ab45b 3057
dbe341c6
TR
3058 /* Get the values to be added together. For signed and unsigned
3059 relocations, we assume that all values should be truncated to
3060 the size of an address. For bitfields, all the bits matter.
3061 See also bfd_check_overflow. */
3062 fieldmask = N_ONES (howto->bitsize);
3063 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3064 a = relocation;
3065 b = val & howto->src_mask;
3066
3067 /* Much like unsigned, except no trimming with addrmask. In
3068 addition, the sum overflows if there is a carry out of
3069 the bfd_vma, i.e., the sum is less than either input
3070 operand. */
3071 a >>= howto->rightshift;
3072 b >>= howto->bitpos;
cf9ab45b 3073
dbe341c6
TR
3074 /* Bitfields are sometimes used for signed numbers; for
3075 example, a 13-bit field sometimes represents values in
3076 0..8191 and sometimes represents values in -4096..4095.
3077 If the field is signed and a is -4095 (0x1001) and b is
3078 -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3079 0x1fff is 0x3000). It's not clear how to handle this
3080 everywhere, since there is not way to know how many bits
3081 are significant in the relocation, but the original code
3082 assumed that it was fully sign extended, and we will keep
3083 that assumption. */
3084 signmask = (fieldmask >> 1) + 1;
cf9ab45b 3085
dbe341c6
TR
3086 if ((a & ~ fieldmask) != 0)
3087 {
3088 /* Some bits out of the field are set. This might not
3089 be a problem: if this is a signed bitfield, it is OK
3090 iff all the high bits are set, including the sign
3091 bit. We'll try setting all but the most significant
3092 bit in the original relocation value: if this is all
3093 ones, we are OK, assuming a signed bitfield. */
3094 ss = (signmask << howto->rightshift) - 1;
3095 if ((ss | relocation) != ~ (bfd_vma) 0)
b34976b6 3096 return TRUE;
dbe341c6
TR
3097 a &= fieldmask;
3098 }
cf9ab45b 3099
dbe341c6 3100 /* We just assume (b & ~ fieldmask) == 0. */
cf9ab45b 3101
dbe341c6
TR
3102 /* We explicitly permit wrap around if this relocation
3103 covers the high bit of an address. The Linux kernel
3104 relies on it, and it is the only way to write assembler
3105 code which can run when loaded at a location 0x80000000
3106 away from the location at which it is linked. */
3107 if (howto->bitsize + howto->rightshift
3108 == bfd_arch_bits_per_address (input_bfd))
b34976b6 3109 return FALSE;
cf9ab45b 3110
dbe341c6
TR
3111 sum = a + b;
3112 if (sum < a || (sum & ~ fieldmask) != 0)
3113 {
3114 /* There was a carry out, or the field overflow. Test
3115 for signed operands again. Here is the overflow test
3116 is as for complain_overflow_signed. */
3117 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
b34976b6 3118 return TRUE;
dbe341c6 3119 }
cf9ab45b 3120
b34976b6 3121 return FALSE;
dbe341c6
TR
3122}
3123
b34976b6 3124static bfd_boolean
cf9ab45b 3125xcoff_complain_overflow_signed_func (input_bfd, val, relocation, howto)
dbe341c6
TR
3126 bfd *input_bfd;
3127 bfd_vma val;
3128 bfd_vma relocation;
3129 struct reloc_howto_struct *howto;
3130{
3131 bfd_vma addrmask, fieldmask, signmask, ss;
3132 bfd_vma a, b, sum;
cf9ab45b 3133
dbe341c6
TR
3134 /* Get the values to be added together. For signed and unsigned
3135 relocations, we assume that all values should be truncated to
3136 the size of an address. For bitfields, all the bits matter.
3137 See also bfd_check_overflow. */
3138 fieldmask = N_ONES (howto->bitsize);
3139 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3140 a = relocation;
3141 b = val & howto->src_mask;
3142
3143 a = (a & addrmask) >> howto->rightshift;
cf9ab45b 3144
dbe341c6
TR
3145 /* If any sign bits are set, all sign bits must be set.
3146 That is, A must be a valid negative address after
3147 shifting. */
3148 signmask = ~ (fieldmask >> 1);
3149 ss = a & signmask;
3150 if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
b34976b6 3151 return TRUE;
cf9ab45b 3152
dbe341c6
TR
3153 /* We only need this next bit of code if the sign bit of B
3154 is below the sign bit of A. This would only happen if
3155 SRC_MASK had fewer bits than BITSIZE. Note that if
3156 SRC_MASK has more bits than BITSIZE, we can get into
3157 trouble; we would need to verify that B is in range, as
3158 we do for A above. */
3159 signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3160 if ((b & signmask) != 0)
3161 {
3162 /* Set all the bits above the sign bit. */
3163 b -= signmask <<= 1;
3164 }
cf9ab45b 3165
dbe341c6 3166 b = (b & addrmask) >> howto->bitpos;
cf9ab45b 3167
dbe341c6
TR
3168 /* Now we can do the addition. */
3169 sum = a + b;
cf9ab45b 3170
dbe341c6
TR
3171 /* See if the result has the correct sign. Bits above the
3172 sign bit are junk now; ignore them. If the sum is
3173 positive, make sure we did not have all negative inputs;
3174 if the sum is negative, make sure we did not have all
3175 positive inputs. The test below looks only at the sign
3176 bits, and it really just
3177 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3178 */
3179 signmask = (fieldmask >> 1) + 1;
3180 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
b34976b6 3181 return TRUE;
cf9ab45b 3182
b34976b6 3183 return FALSE;
dbe341c6
TR
3184}
3185
b34976b6 3186static bfd_boolean
cf9ab45b 3187xcoff_complain_overflow_unsigned_func (input_bfd, val, relocation, howto)
dbe341c6
TR
3188 bfd *input_bfd;
3189 bfd_vma val;
3190 bfd_vma relocation;
cf9ab45b 3191 struct reloc_howto_struct *howto;
dbe341c6
TR
3192{
3193 bfd_vma addrmask, fieldmask;
3194 bfd_vma a, b, sum;
cf9ab45b 3195
dbe341c6
TR
3196 /* Get the values to be added together. For signed and unsigned
3197 relocations, we assume that all values should be truncated to
3198 the size of an address. For bitfields, all the bits matter.
3199 See also bfd_check_overflow. */
3200 fieldmask = N_ONES (howto->bitsize);
3201 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3202 a = relocation;
3203 b = val & howto->src_mask;
3204
3205 /* Checking for an unsigned overflow is relatively easy:
3206 trim the addresses and add, and trim the result as well.
3207 Overflow is normally indicated when the result does not
3208 fit in the field. However, we also need to consider the
3209 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3210 input is 0x80000000, and bfd_vma is only 32 bits; then we
3211 will get sum == 0, but there is an overflow, since the
3212 inputs did not fit in the field. Instead of doing a
3213 separate test, we can check for this by or-ing in the
3214 operands when testing for the sum overflowing its final
3215 field. */
3216 a = (a & addrmask) >> howto->rightshift;
3217 b = (b & addrmask) >> howto->bitpos;
3218 sum = (a + b) & addrmask;
3219 if ((a | b | sum) & ~ fieldmask)
b34976b6 3220 return TRUE;
cf9ab45b 3221
b34976b6 3222 return FALSE;
dbe341c6 3223}
beb1bf64
TR
3224
3225/* This is the relocation function for the RS/6000/POWER/PowerPC.
3226 This is currently the only processor which uses XCOFF; I hope that
cf9ab45b 3227 will never change.
beb1bf64 3228
dbe341c6
TR
3229 I took the relocation type definitions from two documents:
3230 the PowerPC AIX Version 4 Application Binary Interface, First
3231 Edition (April 1992), and the PowerOpen ABI, Big-Endian
3232 32-Bit Hardware Implementation (June 30, 1994). Differences
cf9ab45b 3233 between the documents are noted below.
dbe341c6 3234
cf9ab45b 3235 Unsupported r_type's
dbe341c6
TR
3236
3237 R_RTB:
3238 R_RRTBI:
3239 R_RRTBA:
cf9ab45b 3240
dbe341c6
TR
3241 These relocs are defined by the PowerPC ABI to be
3242 relative branches which use half of the difference
3243 between the symbol and the program counter. I can't
3244 quite figure out when this is useful. These relocs are
cf9ab45b 3245 not defined by the PowerOpen ABI.
dbe341c6
TR
3246
3247 Supported r_type's
3248
3249 R_POS:
3250 Simple positive relocation.
3251
3252 R_NEG:
cf9ab45b 3253 Simple negative relocation.
dbe341c6
TR
3254
3255 R_REL:
3256 Simple PC relative relocation.
3257
3258 R_TOC:
3259 TOC relative relocation. The value in the instruction in
3260 the input file is the offset from the input file TOC to
3261 the desired location. We want the offset from the final
3262 TOC to the desired location. We have:
3263 isym = iTOC + in
3264 iinsn = in + o
3265 osym = oTOC + on
3266 oinsn = on + o
3267 so we must change insn by on - in.
3268
3269 R_GL:
3270 GL linkage relocation. The value of this relocation
cf9ab45b 3271 is the address of the entry in the TOC section.
dbe341c6
TR
3272
3273 R_TCL:
3274 Local object TOC address. I can't figure out the
cf9ab45b 3275 difference between this and case R_GL.
dbe341c6
TR
3276
3277 R_TRL:
3278 TOC relative relocation. A TOC relative load instruction
3279 which may be changed to a load address instruction.
cf9ab45b 3280 FIXME: We don't currently implement this optimization.
dbe341c6
TR
3281
3282 R_TRLA:
3283 TOC relative relocation. This is a TOC relative load
3284 address instruction which may be changed to a load
3285 instruction. FIXME: I don't know if this is the correct
3286 implementation.
3287
3288 R_BA:
3289 Absolute branch. We don't want to mess with the lower
cf9ab45b 3290 two bits of the instruction.
dbe341c6
TR
3291
3292 R_CAI:
3293 The PowerPC ABI defines this as an absolute call which
3294 may be modified to become a relative call. The PowerOpen
cf9ab45b
AM
3295 ABI does not define this relocation type.
3296
dbe341c6
TR
3297 R_RBA:
3298 Absolute branch which may be modified to become a
cf9ab45b 3299 relative branch.
dbe341c6
TR
3300
3301 R_RBAC:
3302 The PowerPC ABI defines this as an absolute branch to a
3303 fixed address which may be modified to an absolute branch
3304 to a symbol. The PowerOpen ABI does not define this
cf9ab45b 3305 relocation type.
dbe341c6
TR
3306
3307 R_RBRC:
3308 The PowerPC ABI defines this as an absolute branch to a
3309 fixed address which may be modified to a relative branch.
cf9ab45b 3310 The PowerOpen ABI does not define this relocation type.
dbe341c6
TR
3311
3312 R_BR:
3313 Relative branch. We don't want to mess with the lower
cf9ab45b 3314 two bits of the instruction.
dbe341c6
TR
3315
3316 R_CREL:
3317 The PowerPC ABI defines this as a relative call which may
3318 be modified to become an absolute call. The PowerOpen
cf9ab45b 3319 ABI does not define this relocation type.
dbe341c6
TR
3320
3321 R_RBR:
3322 A relative branch which may be modified to become an
3323 absolute branch. FIXME: We don't implement this,
3324 although we should for symbols of storage mapping class
cf9ab45b 3325 XMC_XO.
dbe341c6
TR
3326
3327 R_RL:
3328 The PowerPC AIX ABI describes this as a load which may be
3329 changed to a load address. The PowerOpen ABI says this
cf9ab45b 3330 is the same as case R_POS.
dbe341c6
TR
3331
3332 R_RLA:
3333 The PowerPC AIX ABI describes this as a load address
3334 which may be changed to a load. The PowerOpen ABI says
cf9ab45b 3335 this is the same as R_POS.
dbe341c6
TR
3336*/
3337
b34976b6 3338bfd_boolean
beb1bf64
TR
3339xcoff_ppc_relocate_section (output_bfd, info, input_bfd,
3340 input_section, contents, relocs, syms,
3341 sections)
3342 bfd *output_bfd;
3343 struct bfd_link_info *info;
3344 bfd *input_bfd;
3345 asection *input_section;
3346 bfd_byte *contents;
3347 struct internal_reloc *relocs;
3348 struct internal_syment *syms;
3349 asection **sections;
3350{
3351 struct internal_reloc *rel;
3352 struct internal_reloc *relend;
3353
3354 rel = relocs;
3355 relend = rel + input_section->reloc_count;
beb1bf64
TR
3356 for (; rel < relend; rel++)
3357 {
3358 long symndx;
3359 struct xcoff_link_hash_entry *h;
3360 struct internal_syment *sym;
3361 bfd_vma addend;
3362 bfd_vma val;
3363 struct reloc_howto_struct howto;
dbe341c6
TR
3364 bfd_vma relocation;
3365 bfd_vma value_to_relocate;
3366 bfd_vma address;
3367 bfd_byte *location;
beb1bf64
TR
3368
3369 /* Relocation type R_REF is a special relocation type which is
cf9ab45b
AM
3370 merely used to prevent garbage collection from occurring for
3371 the csect including the symbol which it references. */
beb1bf64
TR
3372 if (rel->r_type == R_REF)
3373 continue;
3374
dbe341c6 3375 /* howto */
beb1bf64
TR
3376 howto.type = rel->r_type;
3377 howto.rightshift = 0;
beb1bf64 3378 howto.bitsize = (rel->r_size & 0x1f) + 1;
dbe341c6 3379 howto.size = howto.bitsize > 16 ? 2 : 1;
b34976b6 3380 howto.pc_relative = FALSE;
beb1bf64 3381 howto.bitpos = 0;
cf9ab45b
AM
3382 howto.complain_on_overflow = (rel->r_size & 0x80
3383 ? complain_overflow_signed
3384 : complain_overflow_bitfield);
beb1bf64
TR
3385 howto.special_function = NULL;
3386 howto.name = "internal";
b34976b6 3387 howto.partial_inplace = TRUE;
cf9ab45b 3388 howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
b34976b6 3389 howto.pcrel_offset = FALSE;
beb1bf64 3390
dbe341c6 3391 /* symbol */
beb1bf64 3392 val = 0;
dbe341c6
TR
3393 addend = 0;
3394 h = NULL;
3395 sym = NULL;
cf9ab45b 3396 symndx = rel->r_symndx;
beb1bf64 3397
cf9ab45b 3398 if (-1 != symndx)
beb1bf64
TR
3399 {
3400 asection *sec;
cf9ab45b 3401
dbe341c6
TR
3402 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3403 sym = syms + symndx;
3404 addend = - sym->n_value;
cf9ab45b
AM
3405
3406 if (NULL == h)
beb1bf64
TR
3407 {
3408 sec = sections[symndx];
3409 /* Hack to make sure we use the right TOC anchor value
dbe341c6 3410 if this reloc is against the TOC anchor. */
beb1bf64 3411 if (sec->name[3] == '0'
dbe341c6
TR
3412 && strcmp (sec->name, ".tc0") == 0)
3413 val = xcoff_data (output_bfd)->toc;
f4ffd778 3414 else
dbe341c6
TR
3415 val = (sec->output_section->vma
3416 + sec->output_offset
3417 + sym->n_value
3418 - sec->vma);
cf9ab45b
AM
3419 }
3420 else
dbe341c6 3421 {
cf9ab45b
AM
3422 if (h->root.type == bfd_link_hash_defined
3423 || h->root.type == bfd_link_hash_defweak)
dbe341c6
TR
3424 {
3425 sec = h->root.u.def.section;
3426 val = (h->root.u.def.value
3427 + sec->output_section->vma
3428 + sec->output_offset);
cf9ab45b
AM
3429 }
3430 else if (h->root.type == bfd_link_hash_common)
f4ffd778 3431 {
dbe341c6 3432 sec = h->root.u.c.p->section;
f4ffd778 3433 val = (sec->output_section->vma
dbe341c6 3434 + sec->output_offset);
cf9ab45b
AM
3435
3436 }
3437 else if ((0 == (h->flags & (XCOFF_DEF_DYNAMIC | XCOFF_IMPORT)))
1049f94e 3438 && ! info->relocatable)
dbe341c6
TR
3439 {
3440 if (! ((*info->callbacks->undefined_symbol)
3441 (info, h->root.root.string, input_bfd, input_section,
b34976b6
AM
3442 rel->r_vaddr - input_section->vma, TRUE)))
3443 return FALSE;
cf9ab45b 3444
dbe341c6
TR
3445 /* Don't try to process the reloc. It can't help, and
3446 it may generate another error. */
3447 continue;
f4ffd778 3448 }
beb1bf64
TR
3449 }
3450 }
beb1bf64 3451
cf9ab45b
AM
3452 if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
3453 || !((*xcoff_calculate_relocation[rel->r_type])
3454 (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
3455 addend, &relocation, contents)))
b34976b6 3456 return FALSE;
cf9ab45b 3457
dbe341c6
TR
3458 /* address */
3459 address = rel->r_vaddr - input_section->vma;
3460 location = contents + address;
cf9ab45b 3461
eea6121a 3462 if (address > input_section->size)
cf9ab45b 3463 abort ();
dbe341c6
TR
3464
3465 /* Get the value we are going to relocate. */
3466 if (1 == howto.size)
3467 value_to_relocate = bfd_get_16 (input_bfd, location);
cf9ab45b 3468 else
dbe341c6 3469 value_to_relocate = bfd_get_32 (input_bfd, location);
cf9ab45b
AM
3470
3471 /* overflow.
3472
dbe341c6
TR
3473 FIXME: We may drop bits during the addition
3474 which we don't check for. We must either check at every single
3475 operation, which would be tedious, or we must do the computations
3476 in a type larger than bfd_vma, which would be inefficient. */
beb1bf64 3477
cf9ab45b
AM
3478 if ((unsigned int) howto.complain_on_overflow
3479 >= XCOFF_MAX_COMPLAIN_OVERFLOW)
3480 abort ();
3481
3482 if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3483 (input_bfd, value_to_relocate, relocation, &howto)))
beb1bf64 3484 {
dbe341c6
TR
3485 const char *name;
3486 char buf[SYMNMLEN + 1];
3487 char reloc_type_name[10];
cf9ab45b
AM
3488
3489 if (symndx == -1)
beb1bf64 3490 {
dbe341c6 3491 name = "*ABS*";
cf9ab45b
AM
3492 }
3493 else if (h != NULL)
beb1bf64 3494 {
dfeffb9f 3495 name = NULL;
cf9ab45b
AM
3496 }
3497 else
beb1bf64 3498 {
dbe341c6
TR
3499 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3500 if (name == NULL)
3501 name = "UNKNOWN";
beb1bf64 3502 }
dbe341c6 3503 sprintf (reloc_type_name, "0x%02x", rel->r_type);
cf9ab45b 3504
dbe341c6 3505 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3506 (info, (h ? &h->root : NULL), name, reloc_type_name,
3507 (bfd_vma) 0, input_bfd, input_section,
3508 rel->r_vaddr - input_section->vma)))
b34976b6 3509 return FALSE;
beb1bf64 3510 }
cf9ab45b 3511
dbe341c6 3512 /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE. */
cf9ab45b
AM
3513 value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3514 | (((value_to_relocate & howto.src_mask)
3515 + relocation) & howto.dst_mask));
3516
dbe341c6
TR
3517 /* Put the value back in the object file. */
3518 if (1 == howto.size)
3519 bfd_put_16 (input_bfd, value_to_relocate, location);
cf9ab45b 3520 else
dbe341c6 3521 bfd_put_32 (input_bfd, value_to_relocate, location);
beb1bf64
TR
3522 }
3523
b34976b6 3524 return TRUE;
beb1bf64
TR
3525}
3526
b34976b6 3527static bfd_boolean
beb1bf64
TR
3528_bfd_xcoff_put_ldsymbol_name (abfd, ldinfo, ldsym, name)
3529 bfd *abfd ATTRIBUTE_UNUSED;
3530 struct xcoff_loader_info *ldinfo;
3531 struct internal_ldsym *ldsym;
3532 const char *name;
3533{
3534 size_t len;
3535 len = strlen (name);
3536
3537 if (len <= SYMNMLEN)
3538 strncpy (ldsym->_l._l_name, name, SYMNMLEN);
3539 else
3540 {
3541 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3542 {
dc810e39 3543 bfd_size_type newalc;
f075ee0c 3544 char *newstrings;
beb1bf64
TR
3545
3546 newalc = ldinfo->string_alc * 2;
3547 if (newalc == 0)
3548 newalc = 32;
3549 while (ldinfo->string_size + len + 3 > newalc)
3550 newalc *= 2;
3551
f075ee0c 3552 newstrings = bfd_realloc (ldinfo->strings, newalc);
beb1bf64
TR
3553 if (newstrings == NULL)
3554 {
b34976b6
AM
3555 ldinfo->failed = TRUE;
3556 return FALSE;
beb1bf64
TR
3557 }
3558 ldinfo->string_alc = newalc;
3559 ldinfo->strings = newstrings;
3560 }
3561
dc810e39
AM
3562 bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
3563 ldinfo->strings + ldinfo->string_size);
beb1bf64
TR
3564 strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
3565 ldsym->_l._l_l._l_zeroes = 0;
3566 ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3567 ldinfo->string_size += len + 3;
3568 }
3569
b34976b6 3570 return TRUE;
beb1bf64
TR
3571}
3572
b34976b6 3573static bfd_boolean
dc810e39 3574_bfd_xcoff_put_symbol_name (bfd *abfd, struct bfd_strtab_hash *strtab,
beb1bf64 3575 struct internal_syment *sym,
f4ffd778
NC
3576 const char *name)
3577{
3578 if (strlen (name) <= SYMNMLEN)
3579 {
3580 strncpy (sym->_n._n_name, name, SYMNMLEN);
3581 }
3582 else
3583 {
b34976b6 3584 bfd_boolean hash;
f4ffd778
NC
3585 bfd_size_type indx;
3586
b34976b6 3587 hash = TRUE;
f4ffd778 3588 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
b34976b6
AM
3589 hash = FALSE;
3590 indx = _bfd_stringtab_add (strtab, name, hash, FALSE);
f4ffd778 3591 if (indx == (bfd_size_type) -1)
b34976b6 3592 return FALSE;
f4ffd778
NC
3593 sym->_n._n_n._n_zeroes = 0;
3594 sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3595 }
b34976b6 3596 return TRUE;
beb1bf64
TR
3597}
3598
3599static asection *
dc810e39 3600xcoff_create_csect_from_smclas (abfd, aux, symbol_name)
beb1bf64
TR
3601 bfd *abfd;
3602 union internal_auxent *aux;
814fa6ab 3603 const char *symbol_name;
beb1bf64 3604{
beb1bf64
TR
3605 asection *return_value = NULL;
3606
f4ffd778
NC
3607 /* .sv64 = x_smclas == 17
3608 This is an invalid csect for 32 bit apps. */
3609 static const char *names[19] =
3610 {
beb1bf64
TR
3611 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
3612 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
dc810e39 3613 ".td", NULL, ".sv3264"
beb1bf64
TR
3614 };
3615
cf9ab45b
AM
3616 if ((19 >= aux->x_csect.x_smclas)
3617 && (NULL != names[aux->x_csect.x_smclas]))
f4ffd778 3618 {
dc810e39 3619 return_value = bfd_make_section_anyway
f4ffd778
NC
3620 (abfd, names[aux->x_csect.x_smclas]);
3621 }
3622 else
3623 {
3624 (*_bfd_error_handler)
d003868e
AM
3625 (_("%B: symbol `%s' has unrecognized smclas %d"),
3626 abfd, symbol_name, aux->x_csect.x_smclas);
f4ffd778
NC
3627 bfd_set_error (bfd_error_bad_value);
3628 }
beb1bf64
TR
3629
3630 return return_value;
3631}
3632
b34976b6 3633static bfd_boolean
beb1bf64
TR
3634xcoff_is_lineno_count_overflow (abfd, value)
3635 bfd *abfd ATTRIBUTE_UNUSED;
3636 bfd_vma value;
3637{
f4ffd778 3638 if (0xffff <= value)
b34976b6 3639 return TRUE;
f4ffd778 3640
b34976b6 3641 return FALSE;
beb1bf64
TR
3642}
3643
b34976b6 3644static bfd_boolean
beb1bf64
TR
3645xcoff_is_reloc_count_overflow (abfd, value)
3646 bfd *abfd ATTRIBUTE_UNUSED;
3647 bfd_vma value;
3648{
f4ffd778 3649 if (0xffff <= value)
b34976b6 3650 return TRUE;
f4ffd778 3651
b34976b6 3652 return FALSE;
beb1bf64
TR
3653}
3654
a7b97311 3655static bfd_vma
beb1bf64
TR
3656xcoff_loader_symbol_offset (abfd, ldhdr)
3657 bfd *abfd;
f4ffd778 3658 struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED;
beb1bf64 3659{
cf9ab45b 3660 return bfd_xcoff_ldhdrsz (abfd);
beb1bf64
TR
3661}
3662
a7b97311 3663static bfd_vma
beb1bf64
TR
3664xcoff_loader_reloc_offset (abfd, ldhdr)
3665 bfd *abfd;
f4ffd778 3666 struct internal_ldhdr *ldhdr;
beb1bf64 3667{
cf9ab45b 3668 return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
beb1bf64
TR
3669}
3670
b34976b6 3671static bfd_boolean
69f284c7 3672xcoff_generate_rtinit (abfd, init, fini, rtld)
9a4c7f16
TR
3673 bfd *abfd;
3674 const char *init;
3675 const char *fini;
b34976b6 3676 bfd_boolean rtld;
9a4c7f16
TR
3677{
3678 bfd_byte filehdr_ext[FILHSZ];
3679 bfd_byte scnhdr_ext[SCNHSZ];
69f284c7
TR
3680 bfd_byte syment_ext[SYMESZ * 10];
3681 bfd_byte reloc_ext[RELSZ * 3];
9a4c7f16
TR
3682 bfd_byte *data_buffer;
3683 bfd_size_type data_buffer_size;
d426c6b0 3684 bfd_byte *string_table = NULL, *st_tmp = NULL;
9a4c7f16
TR
3685 bfd_size_type string_table_size;
3686 bfd_vma val;
3687 size_t initsz, finisz;
3688 struct internal_filehdr filehdr;
3689 struct internal_scnhdr scnhdr;
3690 struct internal_syment syment;
3691 union internal_auxent auxent;
3692 struct internal_reloc reloc;
cf9ab45b 3693
9a4c7f16
TR
3694 char *data_name = ".data";
3695 char *rtinit_name = "__rtinit";
69f284c7 3696 char *rtld_name = "__rtld";
cf9ab45b 3697
69f284c7 3698 if (! bfd_xcoff_rtinit_size (abfd))
b34976b6 3699 return FALSE;
9a4c7f16
TR
3700
3701 initsz = (init == NULL ? 0 : 1 + strlen (init));
3702 finisz = (fini == NULL ? 0 : 1 + strlen (fini));
3703
3704 /* file header */
3705 memset (filehdr_ext, 0, FILHSZ);
3706 memset (&filehdr, 0, sizeof (struct internal_filehdr));
3707 filehdr.f_magic = bfd_xcoff_magic_number (abfd);
cf9ab45b 3708 filehdr.f_nscns = 1;
9a4c7f16 3709 filehdr.f_timdat = 0;
69f284c7 3710 filehdr.f_nsyms = 0; /* at least 6, no more than 10 */
9a4c7f16
TR
3711 filehdr.f_symptr = 0; /* set below */
3712 filehdr.f_opthdr = 0;
3713 filehdr.f_flags = 0;
3714
3715 /* section header */
3716 memset (scnhdr_ext, 0, SCNHSZ);
3717 memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
3718 memcpy (scnhdr.s_name, data_name, strlen (data_name));
3719 scnhdr.s_paddr = 0;
3720 scnhdr.s_vaddr = 0;
3721 scnhdr.s_size = 0; /* set below */
3722 scnhdr.s_scnptr = FILHSZ + SCNHSZ;
3723 scnhdr.s_relptr = 0; /* set below */
3724 scnhdr.s_lnnoptr = 0;
3725 scnhdr.s_nreloc = 0; /* either 1 or 2 */
3726 scnhdr.s_nlnno = 0;
3727 scnhdr.s_flags = STYP_DATA;
3728
cf9ab45b
AM
3729 /* .data
3730 0x0000 0x00000000 : rtl
3731 0x0004 0x00000010 : offset to init, or 0
3732 0x0008 0x00000028 : offset to fini, or 0
3733 0x000C 0x0000000C : size of descriptor
3734 0x0010 0x00000000 : init, needs a reloc
3735 0x0014 0x00000040 : offset to init name
3736 0x0018 0x00000000 : flags, padded to a word
3737 0x001C 0x00000000 : empty init
3738 0x0020 0x00000000 :
3739 0x0024 0x00000000 :
3740 0x0028 0x00000000 : fini, needs a reloc
3741 0x002C 0x00000??? : offset to fini name
3742 0x0030 0x00000000 : flags, padded to a word
3743 0x0034 0x00000000 : empty fini
3744 0x0038 0x00000000 :
3745 0x003C 0x00000000 :
3746 0x0040 init name
9a4c7f16
TR
3747 0x0040 + initsz fini name */
3748
3749 data_buffer_size = 0x0040 + initsz + finisz;
2a52da53 3750 data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
330693f5 3751 data_buffer = NULL;
9bab7074 3752 data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
330693f5 3753 if (data_buffer == NULL)
b34976b6 3754 return FALSE;
9a4c7f16 3755
cf9ab45b 3756 if (initsz)
9a4c7f16
TR
3757 {
3758 val = 0x10;
3759 bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
3760 val = 0x40;
3761 bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
3762 memcpy (&data_buffer[val], init, initsz);
3763 }
3764
cf9ab45b 3765 if (finisz)
9a4c7f16
TR
3766 {
3767 val = 0x28;
3768 bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
3769 val = 0x40 + initsz;
3770 bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
3771 memcpy (&data_buffer[val], fini, finisz);
3772 }
3773
3774 val = 0x0C;
3775 bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
3776
3777 scnhdr.s_size = data_buffer_size;
3778
3779 /* string table */
3780 string_table_size = 0;
cf9ab45b 3781 if (initsz > 9)
9a4c7f16
TR
3782 string_table_size += initsz;
3783 if (finisz > 9)
3784 string_table_size += finisz;
3785 if (string_table_size)
3786 {
3787 string_table_size += 4;
9bab7074 3788 string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
021d6096 3789 if (string_table == NULL)
b34976b6 3790 return FALSE;
9bab7074 3791
9a4c7f16
TR
3792 val = string_table_size;
3793 bfd_h_put_32 (abfd, val, &string_table[0]);
3794 st_tmp = string_table + 4;
3795 }
cf9ab45b
AM
3796
3797 /* symbols
9a4c7f16
TR
3798 0. .data csect
3799 2. __rtinit
cf9ab45b
AM
3800 4. init function
3801 6. fini function
69f284c7
TR
3802 8. __rtld */
3803 memset (syment_ext, 0, 10 * SYMESZ);
3804 memset (reloc_ext, 0, 3 * RELSZ);
9a4c7f16
TR
3805
3806 /* .data csect */
3807 memset (&syment, 0, sizeof (struct internal_syment));
3808 memset (&auxent, 0, sizeof (union internal_auxent));
3809 memcpy (syment._n._n_name, data_name, strlen (data_name));
3810 syment.n_scnum = 1;
3811 syment.n_sclass = C_HIDEXT;
3812 syment.n_numaux = 1;
3813 auxent.x_csect.x_scnlen.l = data_buffer_size;
3814 auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
3815 auxent.x_csect.x_smclas = XMC_RW;
cf9ab45b 3816 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3817 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3818 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3819 syment.n_numaux,
9a4c7f16
TR
3820 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3821 filehdr.f_nsyms += 2;
3822
3823 /* __rtinit */
3824 memset (&syment, 0, sizeof (struct internal_syment));
3825 memset (&auxent, 0, sizeof (union internal_auxent));
3826 memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
3827 syment.n_scnum = 1;
3828 syment.n_sclass = C_EXT;
3829 syment.n_numaux = 1;
3830 auxent.x_csect.x_smtyp = XTY_LD;
3831 auxent.x_csect.x_smclas = XMC_RW;
cf9ab45b 3832 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3833 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3834 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3835 syment.n_numaux,
9a4c7f16
TR
3836 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3837 filehdr.f_nsyms += 2;
3838
3839 /* init */
cf9ab45b 3840 if (initsz)
9a4c7f16
TR
3841 {
3842 memset (&syment, 0, sizeof (struct internal_syment));
3843 memset (&auxent, 0, sizeof (union internal_auxent));
3844
cf9ab45b 3845 if (initsz > 9)
9a4c7f16
TR
3846 {
3847 syment._n._n_n._n_offset = st_tmp - string_table;
3848 memcpy (st_tmp, init, initsz);
3849 st_tmp += initsz;
3850 }
3851 else
3852 memcpy (syment._n._n_name, init, initsz - 1);
3853
3854 syment.n_sclass = C_EXT;
3855 syment.n_numaux = 1;
cf9ab45b 3856 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3857 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3858 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3859 syment.n_numaux,
9a4c7f16
TR
3860 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3861
3862 /* reloc */
3863 memset (&reloc, 0, sizeof (struct internal_reloc));
3864 reloc.r_vaddr = 0x0010;
3865 reloc.r_symndx = filehdr.f_nsyms;
3866 reloc.r_type = R_POS;
3867 reloc.r_size = 31;
3868 bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
3869
3870 filehdr.f_nsyms += 2;
3871 scnhdr.s_nreloc += 1;
3872 }
cf9ab45b 3873
9a4c7f16 3874 /* fini */
cf9ab45b 3875 if (finisz)
9a4c7f16
TR
3876 {
3877 memset (&syment, 0, sizeof (struct internal_syment));
3878 memset (&auxent, 0, sizeof (union internal_auxent));
3879
cf9ab45b 3880 if (finisz > 9)
9a4c7f16
TR
3881 {
3882 syment._n._n_n._n_offset = st_tmp - string_table;
3883 memcpy (st_tmp, fini, finisz);
3884 st_tmp += finisz;
3885 }
3886 else
3887 memcpy (syment._n._n_name, fini, finisz - 1);
3888
3889 syment.n_sclass = C_EXT;
3890 syment.n_numaux = 1;
cf9ab45b 3891 bfd_coff_swap_sym_out (abfd, &syment,
9a4c7f16 3892 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3893 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3894 syment.n_numaux,
9a4c7f16
TR
3895 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3896
3897 /* reloc */
3898 memset (&reloc, 0, sizeof (struct internal_reloc));
3899 reloc.r_vaddr = 0x0028;
3900 reloc.r_symndx = filehdr.f_nsyms;
3901 reloc.r_type = R_POS;
3902 reloc.r_size = 31;
cf9ab45b 3903 bfd_coff_swap_reloc_out (abfd, &reloc,
9a4c7f16
TR
3904 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3905
3906 filehdr.f_nsyms += 2;
3907 scnhdr.s_nreloc += 1;
3908 }
3909
69f284c7
TR
3910 if (rtld)
3911 {
3912 memset (&syment, 0, sizeof (struct internal_syment));
3913 memset (&auxent, 0, sizeof (union internal_auxent));
3914 memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
3915 syment.n_sclass = C_EXT;
3916 syment.n_numaux = 1;
cf9ab45b 3917 bfd_coff_swap_sym_out (abfd, &syment,
69f284c7 3918 &syment_ext[filehdr.f_nsyms * SYMESZ]);
cf9ab45b
AM
3919 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3920 syment.n_numaux,
69f284c7
TR
3921 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3922
3923 /* reloc */
3924 memset (&reloc, 0, sizeof (struct internal_reloc));
3925 reloc.r_vaddr = 0x0000;
3926 reloc.r_symndx = filehdr.f_nsyms;
3927 reloc.r_type = R_POS;
3928 reloc.r_size = 31;
cf9ab45b 3929 bfd_coff_swap_reloc_out (abfd, &reloc,
69f284c7
TR
3930 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3931
3932 filehdr.f_nsyms += 2;
3933 scnhdr.s_nreloc += 1;
3934 }
3935
9a4c7f16
TR
3936 scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
3937 filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
3938
3939 bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
3940 bfd_bwrite (filehdr_ext, FILHSZ, abfd);
3941 bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
3942 bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
3943 bfd_bwrite (data_buffer, data_buffer_size, abfd);
3944 bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
3945 bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
3946 bfd_bwrite (string_table, string_table_size, abfd);
3947
330693f5
TR
3948 free (data_buffer);
3949 data_buffer = NULL;
3950
b34976b6 3951 return TRUE;
9a4c7f16
TR
3952}
3953
beb1bf64
TR
3954
3955static reloc_howto_type xcoff_dynamic_reloc =
cf9ab45b
AM
3956HOWTO (0, /* type */
3957 0, /* rightshift */
3958 2, /* size (0 = byte, 1 = short, 2 = long) */
3959 32, /* bitsize */
b34976b6 3960 FALSE, /* pc_relative */
cf9ab45b 3961 0, /* bitpos */
beb1bf64 3962 complain_overflow_bitfield, /* complain_on_overflow */
cf9ab45b
AM
3963 0, /* special_function */
3964 "R_POS", /* name */
b34976b6 3965 TRUE, /* partial_inplace */
cf9ab45b
AM
3966 0xffffffff, /* src_mask */
3967 0xffffffff, /* dst_mask */
b34976b6 3968 FALSE); /* pcrel_offset */
beb1bf64 3969
dc810e39
AM
3970/* glink
3971
3972 The first word of global linkage code must be modified by filling in
f4ffd778
NC
3973 the correct TOC offset. */
3974
beb1bf64 3975static unsigned long xcoff_glink_code[9] =
f4ffd778
NC
3976 {
3977 0x81820000, /* lwz r12,0(r2) */
3978 0x90410014, /* stw r2,20(r1) */
3979 0x800c0000, /* lwz r0,0(r12) */
3980 0x804c0004, /* lwz r2,4(r12) */
3981 0x7c0903a6, /* mtctr r0 */
3982 0x4e800420, /* bctr */
3983 0x00000000, /* start of traceback table */
3984 0x000c8000, /* traceback table */
3985 0x00000000, /* traceback table */
3986 };
beb1bf64
TR
3987
3988
dc810e39 3989static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
f4ffd778
NC
3990 {
3991 { /* COFF backend, defined in libcoff.h. */
cf9ab45b
AM
3992 _bfd_xcoff_swap_aux_in,
3993 _bfd_xcoff_swap_sym_in,
3994 coff_swap_lineno_in,
3995 _bfd_xcoff_swap_aux_out,
3996 _bfd_xcoff_swap_sym_out,
3997 coff_swap_lineno_out,
3998 xcoff_swap_reloc_out,
3999 coff_swap_filehdr_out,
4000 coff_swap_aouthdr_out,
4001 coff_swap_scnhdr_out,
4002 FILHSZ,
4003 AOUTSZ,
4004 SCNHSZ,
4005 SYMESZ,
4006 AUXESZ,
4007 RELSZ,
4008 LINESZ,
4009 FILNMLEN,
b34976b6
AM
4010 TRUE, /* _bfd_coff_long_filenames */
4011 FALSE, /* _bfd_coff_long_section_names */
cf9ab45b 4012 3, /* _bfd_coff_default_section_alignment_power */
b34976b6 4013 FALSE, /* _bfd_coff_force_symnames_in_strings */
cf9ab45b
AM
4014 2, /* _bfd_coff_debug_string_prefix_length */
4015 coff_swap_filehdr_in,
4016 coff_swap_aouthdr_in,
4017 coff_swap_scnhdr_in,
4018 xcoff_swap_reloc_in,
4019 coff_bad_format_hook,
4020 coff_set_arch_mach_hook,
4021 coff_mkobject_hook,
4022 styp_to_sec_flags,
4023 coff_set_alignment_hook,
4024 coff_slurp_symbol_table,
4025 symname_in_debug_hook,
4026 coff_pointerize_aux_hook,
4027 coff_print_aux,
4028 dummy_reloc16_extra_cases,
4029 dummy_reloc16_estimate,
4030 NULL, /* bfd_coff_sym_is_global */
4031 coff_compute_section_file_positions,
4032 NULL, /* _bfd_coff_start_final_link */
4033 xcoff_ppc_relocate_section,
4034 coff_rtype_to_howto,
4035 NULL, /* _bfd_coff_adjust_symndx */
4036 _bfd_generic_link_add_one_symbol,
4037 coff_link_output_has_begun,
4038 coff_final_link_postscript
f4ffd778
NC
4039 },
4040
cf9ab45b
AM
4041 0x01DF, /* magic number */
4042 bfd_arch_rs6000,
4043 bfd_mach_rs6k,
dc810e39 4044
f4ffd778 4045 /* Function pointers to xcoff specific swap routines. */
cf9ab45b
AM
4046 xcoff_swap_ldhdr_in,
4047 xcoff_swap_ldhdr_out,
4048 xcoff_swap_ldsym_in,
4049 xcoff_swap_ldsym_out,
4050 xcoff_swap_ldrel_in,
4051 xcoff_swap_ldrel_out,
f4ffd778
NC
4052
4053 /* Sizes. */
cf9ab45b
AM
4054 LDHDRSZ,
4055 LDSYMSZ,
4056 LDRELSZ,
4057 12, /* _xcoff_function_descriptor_size */
4058 SMALL_AOUTSZ,
f4ffd778 4059
cf9ab45b
AM
4060 /* Versions. */
4061 1, /* _xcoff_ldhdr_version */
f4ffd778 4062
cf9ab45b
AM
4063 _bfd_xcoff_put_symbol_name,
4064 _bfd_xcoff_put_ldsymbol_name,
4065 &xcoff_dynamic_reloc,
4066 xcoff_create_csect_from_smclas,
f4ffd778
NC
4067
4068 /* Lineno and reloc count overflow. */
4069 xcoff_is_lineno_count_overflow,
4070 xcoff_is_reloc_count_overflow,
4071
4072 xcoff_loader_symbol_offset,
4073 xcoff_loader_reloc_offset,
4074
4075 /* glink. */
cf9ab45b
AM
4076 &xcoff_glink_code[0],
4077 36, /* _xcoff_glink_size */
9a4c7f16
TR
4078
4079 /* rtinit */
cf9ab45b
AM
4080 64, /* _xcoff_rtinit_size */
4081 xcoff_generate_rtinit,
4082 };
beb1bf64 4083
eb1e0e80 4084/* The transfer vector that leads the outside world to all of the above. */
beb1bf64 4085const bfd_target rs6000coff_vec =
cf9ab45b
AM
4086 {
4087 "aixcoff-rs6000",
4088 bfd_target_xcoff_flavour,
4089 BFD_ENDIAN_BIG, /* data byte order is big */
4090 BFD_ENDIAN_BIG, /* header byte order is big */
4091
4092 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4093 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4094
a7c71b0c 4095 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
cf9ab45b
AM
4096 0, /* leading char */
4097 '/', /* ar_pad_char */
4098 15, /* ar_max_namelen */
4099
4100 /* data */
4101 bfd_getb64,
4102 bfd_getb_signed_64,
4103 bfd_putb64,
4104 bfd_getb32,
4105 bfd_getb_signed_32,
4106 bfd_putb32,
4107 bfd_getb16,
4108 bfd_getb_signed_16,
4109 bfd_putb16,
4110
4111 /* hdrs */
4112 bfd_getb64,
4113 bfd_getb_signed_64,
4114 bfd_putb64,
4115 bfd_getb32,
4116 bfd_getb_signed_32,
4117 bfd_putb32,
4118 bfd_getb16,
4119 bfd_getb_signed_16,
4120 bfd_putb16,
4121
4122 { /* bfd_check_format */
4123 _bfd_dummy_target,
4124 coff_object_p,
4125 _bfd_xcoff_archive_p,
4126 CORE_FILE_P
4127 },
dc810e39 4128
cf9ab45b
AM
4129 { /* bfd_set_format */
4130 bfd_false,
4131 coff_mkobject,
4132 _bfd_generic_mkarchive,
4133 bfd_false
4134 },
4135
4136 {/* bfd_write_contents */
4137 bfd_false,
4138 coff_write_object_contents,
4139 _bfd_xcoff_write_archive_contents,
4140 bfd_false
4141 },
4142
4143 /* Generic */
4144 bfd_true,
4145 bfd_true,
4146 coff_new_section_hook,
4147 _bfd_generic_get_section_contents,
4148 _bfd_generic_get_section_contents_in_window,
4149
4150 /* Copy */
4151 _bfd_xcoff_copy_private_bfd_data,
b34976b6 4152 ((bfd_boolean (*) (bfd *, bfd *)) bfd_true),
60b48850 4153 _bfd_generic_init_private_section_data,
b34976b6
AM
4154 ((bfd_boolean (*) (bfd *, asection *, bfd *, asection *)) bfd_true),
4155 ((bfd_boolean (*) (bfd *, asymbol *, bfd *, asymbol *)) bfd_true),
80fccad2 4156 ((bfd_boolean (*) (bfd *, bfd *)) bfd_true),
b34976b6
AM
4157 ((bfd_boolean (*) (bfd *, flagword)) bfd_true),
4158 ((bfd_boolean (*) (bfd *, void * )) bfd_true),
cf9ab45b
AM
4159
4160 /* Core */
4161 coff_core_file_failing_command,
4162 coff_core_file_failing_signal,
4163 coff_core_file_matches_executable_p,
4164
4165 /* Archive */
4166 _bfd_xcoff_slurp_armap,
dc810e39 4167 bfd_false,
b34976b6 4168 ((bfd_boolean (*) (bfd *, char **, bfd_size_type *, const char **)) bfd_false),
cf9ab45b
AM
4169 bfd_dont_truncate_arname,
4170 _bfd_xcoff_write_armap,
4171 _bfd_xcoff_read_ar_hdr,
4172 _bfd_xcoff_openr_next_archived_file,
4173 _bfd_generic_get_elt_at_index,
4174 _bfd_xcoff_stat_arch_elt,
4175 bfd_true,
4176
4177 /* Symbols */
4178 coff_get_symtab_upper_bound,
6cee3f79 4179 coff_canonicalize_symtab,
cf9ab45b
AM
4180 coff_make_empty_symbol,
4181 coff_print_symbol,
4182 coff_get_symbol_info,
4183 _bfd_xcoff_is_local_label_name,
7db6994f 4184 coff_bfd_is_target_special_symbol,
cf9ab45b
AM
4185 coff_get_lineno,
4186 coff_find_nearest_line,
4575b1b5 4187 _bfd_generic_find_line,
4ab527b0 4188 coff_find_inliner_info,
cf9ab45b
AM
4189 coff_bfd_make_debug_symbol,
4190 _bfd_generic_read_minisymbols,
4191 _bfd_generic_minisymbol_to_symbol,
4192
4193 /* Reloc */
4194 coff_get_reloc_upper_bound,
4195 coff_canonicalize_reloc,
4196 _bfd_xcoff_reloc_type_lookup,
157090f7 4197 _bfd_xcoff_reloc_name_lookup,
cf9ab45b
AM
4198
4199 /* Write */
4200 coff_set_arch_mach,
4201 coff_set_section_contents,
4202
4203 /* Link */
4204 _bfd_xcoff_sizeof_headers,
4205 bfd_generic_get_relocated_section_contents,
4206 bfd_generic_relax_section,
4207 _bfd_xcoff_bfd_link_hash_table_create,
4208 _bfd_generic_link_hash_table_free,
4209 _bfd_xcoff_bfd_link_add_symbols,
4210 _bfd_generic_link_just_syms,
4211 _bfd_xcoff_bfd_final_link,
4212 _bfd_generic_link_split_section,
4213 bfd_generic_gc_sections,
4214 bfd_generic_merge_sections,
72adc230 4215 bfd_generic_is_group_section,
cf9ab45b 4216 bfd_generic_discard_group,
082b7297 4217 _bfd_generic_section_already_linked,
cf9ab45b
AM
4218
4219 /* Dynamic */
4220 _bfd_xcoff_get_dynamic_symtab_upper_bound,
4221 _bfd_xcoff_canonicalize_dynamic_symtab,
4c45e5c9 4222 _bfd_nodynamic_get_synthetic_symtab,
cf9ab45b
AM
4223 _bfd_xcoff_get_dynamic_reloc_upper_bound,
4224 _bfd_xcoff_canonicalize_dynamic_reloc,
4225
4226 /* Opposite endian version, none exists */
4227 NULL,
4228
4229 (void *) &bfd_xcoff_backend_data,
4230 };
beb1bf64 4231
cf9ab45b
AM
4232/* xcoff-powermac target
4233 Old target.
4234 Only difference between this target and the rs6000 target is the
4235 the default architecture and machine type used in coffcode.h
4236
4237 PowerPC Macs use the same magic numbers as RS/6000
4238 (because that's how they were bootstrapped originally),
4239 but they are always PowerPC architecture. */
dc810e39 4240static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
cf9ab45b
AM
4241 {
4242 { /* COFF backend, defined in libcoff.h. */
4243 _bfd_xcoff_swap_aux_in,
4244 _bfd_xcoff_swap_sym_in,
4245 coff_swap_lineno_in,
4246 _bfd_xcoff_swap_aux_out,
4247 _bfd_xcoff_swap_sym_out,
4248 coff_swap_lineno_out,
4249 xcoff_swap_reloc_out,
4250 coff_swap_filehdr_out,
4251 coff_swap_aouthdr_out,
4252 coff_swap_scnhdr_out,
4253 FILHSZ,
4254 AOUTSZ,
4255 SCNHSZ,
4256 SYMESZ,
4257 AUXESZ,
4258 RELSZ,
4259 LINESZ,
4260 FILNMLEN,
b34976b6
AM
4261 TRUE, /* _bfd_coff_long_filenames */
4262 FALSE, /* _bfd_coff_long_section_names */
cf9ab45b 4263 3, /* _bfd_coff_default_section_alignment_power */
b34976b6 4264 FALSE, /* _bfd_coff_force_symnames_in_strings */
cf9ab45b
AM
4265 2, /* _bfd_coff_debug_string_prefix_length */
4266 coff_swap_filehdr_in,
4267 coff_swap_aouthdr_in,
4268 coff_swap_scnhdr_in,
4269 xcoff_swap_reloc_in,
4270 coff_bad_format_hook,
4271 coff_set_arch_mach_hook,
4272 coff_mkobject_hook,
4273 styp_to_sec_flags,
4274 coff_set_alignment_hook,
4275 coff_slurp_symbol_table,
4276 symname_in_debug_hook,
4277 coff_pointerize_aux_hook,
4278 coff_print_aux,
4279 dummy_reloc16_extra_cases,
4280 dummy_reloc16_estimate,
4281 NULL, /* bfd_coff_sym_is_global */
4282 coff_compute_section_file_positions,
4283 NULL, /* _bfd_coff_start_final_link */
4284 xcoff_ppc_relocate_section,
4285 coff_rtype_to_howto,
4286 NULL, /* _bfd_coff_adjust_symndx */
4287 _bfd_generic_link_add_one_symbol,
4288 coff_link_output_has_begun,
4289 coff_final_link_postscript
4290 },
4291
4292 0x01DF, /* magic number */
4293 bfd_arch_powerpc,
4294 bfd_mach_ppc,
4295
4296 /* Function pointers to xcoff specific swap routines. */
4297 xcoff_swap_ldhdr_in,
4298 xcoff_swap_ldhdr_out,
4299 xcoff_swap_ldsym_in,
4300 xcoff_swap_ldsym_out,
4301 xcoff_swap_ldrel_in,
4302 xcoff_swap_ldrel_out,
4303
4304 /* Sizes. */
4305 LDHDRSZ,
4306 LDSYMSZ,
4307 LDRELSZ,
4308 12, /* _xcoff_function_descriptor_size */
4309 SMALL_AOUTSZ,
4310
4311 /* Versions. */
4312 1, /* _xcoff_ldhdr_version */
4313
4314 _bfd_xcoff_put_symbol_name,
4315 _bfd_xcoff_put_ldsymbol_name,
4316 &xcoff_dynamic_reloc,
4317 xcoff_create_csect_from_smclas,
4318
4319 /* Lineno and reloc count overflow. */
4320 xcoff_is_lineno_count_overflow,
4321 xcoff_is_reloc_count_overflow,
4322
4323 xcoff_loader_symbol_offset,
4324 xcoff_loader_reloc_offset,
beb1bf64 4325
cf9ab45b
AM
4326 /* glink. */
4327 &xcoff_glink_code[0],
4328 36, /* _xcoff_glink_size */
4329
4330 /* rtinit */
4331 0, /* _xcoff_rtinit_size */
4332 xcoff_generate_rtinit,
4333 };
4334
4335/* The transfer vector that leads the outside world to all of the above. */
beb1bf64 4336const bfd_target pmac_xcoff_vec =
cf9ab45b
AM
4337 {
4338 "xcoff-powermac",
4339 bfd_target_xcoff_flavour,
4340 BFD_ENDIAN_BIG, /* data byte order is big */
4341 BFD_ENDIAN_BIG, /* header byte order is big */
4342
4343 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4344 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4345
a7c71b0c 4346 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
cf9ab45b
AM
4347 0, /* leading char */
4348 '/', /* ar_pad_char */
4349 15, /* ar_max_namelen */
4350
4351 /* data */
4352 bfd_getb64,
4353 bfd_getb_signed_64,
4354 bfd_putb64,
4355 bfd_getb32,
4356 bfd_getb_signed_32,
4357 bfd_putb32,
4358 bfd_getb16,
4359 bfd_getb_signed_16,
4360 bfd_putb16,
4361
4362 /* hdrs */
4363 bfd_getb64,
4364 bfd_getb_signed_64,
4365 bfd_putb64,
4366 bfd_getb32,
4367 bfd_getb_signed_32,
4368 bfd_putb32,
4369 bfd_getb16,
4370 bfd_getb_signed_16,
4371 bfd_putb16,
4372
4373 { /* bfd_check_format */
4374 _bfd_dummy_target,
4375 coff_object_p,
4376 _bfd_xcoff_archive_p,
4377 CORE_FILE_P
4378 },
4379
4380 { /* bfd_set_format */
4381 bfd_false,
4382 coff_mkobject,
4383 _bfd_generic_mkarchive,
4384 bfd_false
4385 },
4386
4387 {/* bfd_write_contents */
4388 bfd_false,
4389 coff_write_object_contents,
4390 _bfd_xcoff_write_archive_contents,
4391 bfd_false
4392 },
dc810e39 4393
cf9ab45b
AM
4394 /* Generic */
4395 bfd_true,
4396 bfd_true,
4397 coff_new_section_hook,
4398 _bfd_generic_get_section_contents,
4399 _bfd_generic_get_section_contents_in_window,
4400
4401 /* Copy */
4402 _bfd_xcoff_copy_private_bfd_data,
b34976b6 4403 ((bfd_boolean (*) (bfd *, bfd *)) bfd_true),
60b48850 4404 _bfd_generic_init_private_section_data,
b34976b6
AM
4405 ((bfd_boolean (*) (bfd *, asection *, bfd *, asection *)) bfd_true),
4406 ((bfd_boolean (*) (bfd *, asymbol *, bfd *, asymbol *)) bfd_true),
80fccad2 4407 ((bfd_boolean (*) (bfd *, bfd *)) bfd_true),
b34976b6
AM
4408 ((bfd_boolean (*) (bfd *, flagword)) bfd_true),
4409 ((bfd_boolean (*) (bfd *, void * )) bfd_true),
cf9ab45b
AM
4410
4411 /* Core */
4412 coff_core_file_failing_command,
4413 coff_core_file_failing_signal,
4414 coff_core_file_matches_executable_p,
4415
4416 /* Archive */
4417 _bfd_xcoff_slurp_armap,
dc810e39 4418 bfd_false,
b34976b6 4419 ((bfd_boolean (*) (bfd *, char **, bfd_size_type *, const char **)) bfd_false),
cf9ab45b
AM
4420 bfd_dont_truncate_arname,
4421 _bfd_xcoff_write_armap,
4422 _bfd_xcoff_read_ar_hdr,
4423 _bfd_xcoff_openr_next_archived_file,
4424 _bfd_generic_get_elt_at_index,
4425 _bfd_xcoff_stat_arch_elt,
4426 bfd_true,
4427
4428 /* Symbols */
4429 coff_get_symtab_upper_bound,
6cee3f79 4430 coff_canonicalize_symtab,
cf9ab45b
AM
4431 coff_make_empty_symbol,
4432 coff_print_symbol,
4433 coff_get_symbol_info,
4434 _bfd_xcoff_is_local_label_name,
7db6994f 4435 coff_bfd_is_target_special_symbol,
cf9ab45b
AM
4436 coff_get_lineno,
4437 coff_find_nearest_line,
4575b1b5 4438 _bfd_generic_find_line,
4ab527b0 4439 coff_find_inliner_info,
cf9ab45b
AM
4440 coff_bfd_make_debug_symbol,
4441 _bfd_generic_read_minisymbols,
4442 _bfd_generic_minisymbol_to_symbol,
4443
4444 /* Reloc */
4445 coff_get_reloc_upper_bound,
4446 coff_canonicalize_reloc,
4447 _bfd_xcoff_reloc_type_lookup,
157090f7 4448 _bfd_xcoff_reloc_name_lookup,
cf9ab45b
AM
4449
4450 /* Write */
4451 coff_set_arch_mach,
4452 coff_set_section_contents,
4453
4454 /* Link */
4455 _bfd_xcoff_sizeof_headers,
4456 bfd_generic_get_relocated_section_contents,
4457 bfd_generic_relax_section,
4458 _bfd_xcoff_bfd_link_hash_table_create,
4459 _bfd_generic_link_hash_table_free,
4460 _bfd_xcoff_bfd_link_add_symbols,
4461 _bfd_generic_link_just_syms,
4462 _bfd_xcoff_bfd_final_link,
4463 _bfd_generic_link_split_section,
4464 bfd_generic_gc_sections,
4465 bfd_generic_merge_sections,
72adc230 4466 bfd_generic_is_group_section,
cf9ab45b 4467 bfd_generic_discard_group,
082b7297 4468 _bfd_generic_section_already_linked,
cf9ab45b
AM
4469
4470 /* Dynamic */
4471 _bfd_xcoff_get_dynamic_symtab_upper_bound,
4472 _bfd_xcoff_canonicalize_dynamic_symtab,
4c45e5c9 4473 _bfd_nodynamic_get_synthetic_symtab,
cf9ab45b
AM
4474 _bfd_xcoff_get_dynamic_reloc_upper_bound,
4475 _bfd_xcoff_canonicalize_dynamic_reloc,
4476
4477 /* Opposite endian version, none exists */
4478 NULL,
4479
4480 (void *) &bfd_pmac_xcoff_backend_data,
4481 };
This page took 0.902458 seconds and 4 git commands to generate.