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