Commit | Line | Data |
---|---|---|
f4bd7a8f | 1 | /* BFD back-end for IBM RS/6000 "XCOFF" files. |
bb2deaf2 | 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. |
f4bd7a8f DM |
3 | FIXME: Can someone provide a transliteration of this name into ASCII? |
4 | Using the following chars caused a compiler warning on HIUX (so I replaced | |
5 | them with octal escapes), and isn't useful without an understanding of what | |
6 | character set it is. | |
7 | Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365, | |
8 | and John Gilmore. | |
ba9137fe JG |
9 | Archive support from Damon A. Permezel. |
10 | Contributed by IBM Corporation and Cygnus Support. | |
11 | ||
12 | This file is part of BFD, the Binary File Descriptor library. | |
13 | ||
14 | This program is free software; you can redistribute it and/or modify | |
15 | it under the terms of the GNU General Public License as published by | |
16 | the Free Software Foundation; either version 2 of the License, or | |
17 | (at your option) any later version. | |
18 | ||
19 | This program is distributed in the hope that it will be useful, | |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | GNU General Public License for more details. | |
23 | ||
24 | You should have received a copy of the GNU General Public License | |
25 | along with this program; if not, write to the Free Software | |
ae115e51 | 26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
ba9137fe | 27 | |
ba9137fe JG |
28 | /* Internalcoff.h and coffcode.h modify themselves based on this flag. */ |
29 | #define RS6000COFF_C 1 | |
30 | ||
ba9137fe | 31 | #include "bfd.h" |
156e3852 | 32 | #include "sysdep.h" |
ba9137fe JG |
33 | #include "libbfd.h" |
34 | #include "obstack.h" | |
294eaca4 SC |
35 | #include "coff/internal.h" |
36 | #include "coff/rs6000.h" | |
ba9137fe JG |
37 | #include "libcoff.h" |
38 | ||
aadf04f7 SS |
39 | /* The main body of code is in coffcode.h. */ |
40 | ||
41 | static boolean xcoff_mkobject PARAMS ((bfd *)); | |
42 | static boolean xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *)); | |
43 | static void xcoff_rtype2howto | |
44 | PARAMS ((arelent *, struct internal_reloc *)); | |
45 | static reloc_howto_type *xcoff_reloc_type_lookup | |
46 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
bb2deaf2 ILT |
47 | static boolean xcoff_slurp_armap PARAMS ((bfd *)); |
48 | static const bfd_target *xcoff_archive_p PARAMS ((bfd *)); | |
49 | static PTR xcoff_read_ar_hdr PARAMS ((bfd *)); | |
50 | static bfd *xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *)); | |
51 | static int xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *)); | |
52 | static const char *normalize_filename PARAMS ((bfd *)); | |
53 | static boolean xcoff_write_armap | |
54 | PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int)); | |
55 | static boolean xcoff_write_archive_contents PARAMS ((bfd *)); | |
aadf04f7 SS |
56 | \f |
57 | /* We use our own tdata type. Its first field is the COFF tdata type, | |
58 | so the COFF routines are compatible. */ | |
bb2deaf2 | 59 | |
aadf04f7 SS |
60 | static boolean |
61 | xcoff_mkobject (abfd) | |
62 | bfd *abfd; | |
63 | { | |
64 | coff_data_type *coff; | |
ba9137fe | 65 | |
aadf04f7 SS |
66 | abfd->tdata.xcoff_obj_data = |
67 | ((struct xcoff_tdata *) | |
68 | bfd_zalloc (abfd, sizeof (struct xcoff_tdata))); | |
69 | if (abfd->tdata.xcoff_obj_data == NULL) | |
a9713b91 | 70 | return false; |
aadf04f7 SS |
71 | coff = coff_data (abfd); |
72 | coff->symbols = (coff_symbol_type *) NULL; | |
73 | coff->conversion_table = (unsigned int *) NULL; | |
74 | coff->raw_syments = (struct coff_ptr_struct *) NULL; | |
75 | coff->relocbase = 0; | |
76 | ||
77 | xcoff_data (abfd)->modtype = ('1' << 8) | 'L'; | |
78 | ||
79 | /* We set cputype to -1 to indicate that it has not been | |
80 | initialized. */ | |
81 | xcoff_data (abfd)->cputype = -1; | |
82 | ||
83 | xcoff_data (abfd)->csects = NULL; | |
84 | xcoff_data (abfd)->debug_indices = NULL; | |
85 | ||
86 | return true; | |
87 | } | |
ae115e51 | 88 | |
aadf04f7 SS |
89 | /* Copy XCOFF data from one BFD to another. */ |
90 | ||
91 | static boolean | |
92 | xcoff_copy_private_bfd_data (ibfd, obfd) | |
93 | bfd *ibfd; | |
94 | bfd *obfd; | |
95 | { | |
96 | struct xcoff_tdata *ix, *ox; | |
97 | ||
98 | if (ibfd->xvec != obfd->xvec) | |
99 | return true; | |
100 | ix = xcoff_data (ibfd); | |
101 | ox = xcoff_data (obfd); | |
867d923d | 102 | ox->full_aouthdr = ix->full_aouthdr; |
aadf04f7 | 103 | ox->toc = ix->toc; |
867d923d ILT |
104 | if (ix->toc_section == NULL) |
105 | ox->toc_section = NULL; | |
106 | else | |
107 | ox->toc_section = ix->toc_section->output_section; | |
108 | if (ix->entry_section == NULL) | |
109 | ox->entry_section = NULL; | |
110 | else | |
111 | ox->entry_section = ix->entry_section->output_section; | |
aadf04f7 SS |
112 | ox->text_align_power = ix->text_align_power; |
113 | ox->data_align_power = ix->data_align_power; | |
114 | ox->modtype = ix->modtype; | |
115 | ox->cputype = ix->cputype; | |
116 | ox->maxdata = ix->maxdata; | |
117 | ox->maxstack = ix->maxstack; | |
118 | return true; | |
119 | } | |
120 | \f | |
8b8ddedb SG |
121 | /* The XCOFF reloc table. Actually, XCOFF relocations specify the |
122 | bitsize and whether they are signed or not, along with a | |
123 | conventional type. This table is for the types, which are used for | |
124 | different algorithms for putting in the reloc. Many of these | |
125 | relocs need special_function entries, which I have not written. */ | |
126 | ||
aadf04f7 | 127 | static reloc_howto_type xcoff_howto_table[] = |
8b8ddedb SG |
128 | { |
129 | /* Standard 32 bit relocation. */ | |
130 | HOWTO (0, /* type */ | |
131 | 0, /* rightshift */ | |
132 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
133 | 32, /* bitsize */ | |
134 | false, /* pc_relative */ | |
135 | 0, /* bitpos */ | |
136 | complain_overflow_bitfield, /* complain_on_overflow */ | |
137 | 0, /* special_function */ | |
138 | "R_POS", /* name */ | |
139 | true, /* partial_inplace */ | |
140 | 0xffffffff, /* src_mask */ | |
141 | 0xffffffff, /* dst_mask */ | |
142 | false), /* pcrel_offset */ | |
143 | ||
144 | /* 32 bit relocation, but store negative value. */ | |
145 | HOWTO (1, /* type */ | |
146 | 0, /* rightshift */ | |
147 | -2, /* size (0 = byte, 1 = short, 2 = long) */ | |
148 | 32, /* bitsize */ | |
149 | false, /* pc_relative */ | |
150 | 0, /* bitpos */ | |
151 | complain_overflow_bitfield, /* complain_on_overflow */ | |
152 | 0, /* special_function */ | |
153 | "R_NEG", /* name */ | |
154 | true, /* partial_inplace */ | |
155 | 0xffffffff, /* src_mask */ | |
156 | 0xffffffff, /* dst_mask */ | |
157 | false), /* pcrel_offset */ | |
158 | ||
159 | /* 32 bit PC relative relocation. */ | |
160 | HOWTO (2, /* type */ | |
161 | 0, /* rightshift */ | |
162 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
163 | 32, /* bitsize */ | |
164 | true, /* pc_relative */ | |
165 | 0, /* bitpos */ | |
166 | complain_overflow_signed, /* complain_on_overflow */ | |
167 | 0, /* special_function */ | |
168 | "R_REL", /* name */ | |
169 | true, /* partial_inplace */ | |
170 | 0xffffffff, /* src_mask */ | |
171 | 0xffffffff, /* dst_mask */ | |
172 | false), /* pcrel_offset */ | |
173 | ||
174 | /* 16 bit TOC relative relocation. */ | |
175 | HOWTO (3, /* type */ | |
176 | 0, /* rightshift */ | |
177 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
178 | 16, /* bitsize */ | |
179 | false, /* pc_relative */ | |
180 | 0, /* bitpos */ | |
aadf04f7 | 181 | complain_overflow_bitfield, /* complain_on_overflow */ |
8b8ddedb SG |
182 | 0, /* special_function */ |
183 | "R_TOC", /* name */ | |
184 | true, /* partial_inplace */ | |
185 | 0xffff, /* src_mask */ | |
186 | 0xffff, /* dst_mask */ | |
187 | false), /* pcrel_offset */ | |
188 | ||
189 | /* I don't really know what this is. */ | |
190 | HOWTO (4, /* type */ | |
191 | 1, /* rightshift */ | |
192 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
193 | 32, /* bitsize */ | |
194 | false, /* pc_relative */ | |
195 | 0, /* bitpos */ | |
196 | complain_overflow_bitfield, /* complain_on_overflow */ | |
197 | 0, /* special_function */ | |
198 | "R_RTB", /* name */ | |
199 | true, /* partial_inplace */ | |
200 | 0xffffffff, /* src_mask */ | |
201 | 0xffffffff, /* dst_mask */ | |
202 | false), /* pcrel_offset */ | |
203 | ||
204 | /* External TOC relative symbol. */ | |
205 | HOWTO (5, /* type */ | |
206 | 0, /* rightshift */ | |
207 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
208 | 16, /* bitsize */ | |
209 | false, /* pc_relative */ | |
210 | 0, /* bitpos */ | |
211 | complain_overflow_bitfield, /* complain_on_overflow */ | |
212 | 0, /* special_function */ | |
213 | "R_GL", /* name */ | |
214 | true, /* partial_inplace */ | |
215 | 0xffff, /* src_mask */ | |
216 | 0xffff, /* dst_mask */ | |
217 | false), /* pcrel_offset */ | |
218 | ||
219 | /* Local TOC relative symbol. */ | |
220 | HOWTO (6, /* type */ | |
221 | 0, /* rightshift */ | |
222 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
223 | 16, /* bitsize */ | |
224 | false, /* pc_relative */ | |
225 | 0, /* bitpos */ | |
226 | complain_overflow_bitfield, /* complain_on_overflow */ | |
227 | 0, /* special_function */ | |
228 | "R_TCL", /* name */ | |
229 | true, /* partial_inplace */ | |
230 | 0xffff, /* src_mask */ | |
231 | 0xffff, /* dst_mask */ | |
232 | false), /* pcrel_offset */ | |
233 | ||
234 | { 7 }, | |
235 | ||
236 | /* Non modifiable absolute branch. */ | |
237 | HOWTO (8, /* type */ | |
238 | 0, /* rightshift */ | |
239 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
240 | 26, /* bitsize */ | |
241 | false, /* pc_relative */ | |
242 | 0, /* bitpos */ | |
243 | complain_overflow_bitfield, /* complain_on_overflow */ | |
244 | 0, /* special_function */ | |
245 | "R_BA", /* name */ | |
246 | true, /* partial_inplace */ | |
247 | 0x3fffffc, /* src_mask */ | |
248 | 0x3fffffc, /* dst_mask */ | |
249 | false), /* pcrel_offset */ | |
250 | ||
251 | { 9 }, | |
252 | ||
253 | /* Non modifiable relative branch. */ | |
254 | HOWTO (0xa, /* type */ | |
255 | 0, /* rightshift */ | |
256 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
257 | 26, /* bitsize */ | |
258 | true, /* pc_relative */ | |
259 | 0, /* bitpos */ | |
260 | complain_overflow_signed, /* complain_on_overflow */ | |
261 | 0, /* special_function */ | |
262 | "R_BR", /* name */ | |
263 | true, /* partial_inplace */ | |
264 | 0x3fffffc, /* src_mask */ | |
265 | 0x3fffffc, /* dst_mask */ | |
266 | false), /* pcrel_offset */ | |
267 | ||
268 | { 0xb }, | |
269 | ||
270 | /* Indirect load. */ | |
271 | HOWTO (0xc, /* type */ | |
272 | 0, /* rightshift */ | |
273 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
274 | 16, /* bitsize */ | |
275 | false, /* pc_relative */ | |
276 | 0, /* bitpos */ | |
277 | complain_overflow_bitfield, /* complain_on_overflow */ | |
278 | 0, /* special_function */ | |
279 | "R_RL", /* name */ | |
280 | true, /* partial_inplace */ | |
281 | 0xffff, /* src_mask */ | |
282 | 0xffff, /* dst_mask */ | |
283 | false), /* pcrel_offset */ | |
284 | ||
285 | /* Load address. */ | |
286 | HOWTO (0xd, /* type */ | |
287 | 0, /* rightshift */ | |
288 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
289 | 16, /* bitsize */ | |
290 | false, /* pc_relative */ | |
291 | 0, /* bitpos */ | |
292 | complain_overflow_bitfield, /* complain_on_overflow */ | |
293 | 0, /* special_function */ | |
294 | "R_RLA", /* name */ | |
295 | true, /* partial_inplace */ | |
296 | 0xffff, /* src_mask */ | |
297 | 0xffff, /* dst_mask */ | |
298 | false), /* pcrel_offset */ | |
299 | ||
300 | { 0xe }, | |
301 | ||
302 | /* Non-relocating reference. */ | |
303 | HOWTO (0xf, /* type */ | |
304 | 0, /* rightshift */ | |
305 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
306 | 32, /* bitsize */ | |
307 | false, /* pc_relative */ | |
308 | 0, /* bitpos */ | |
309 | complain_overflow_bitfield, /* complain_on_overflow */ | |
310 | 0, /* special_function */ | |
311 | "R_REF", /* name */ | |
312 | false, /* partial_inplace */ | |
313 | 0, /* src_mask */ | |
314 | 0, /* dst_mask */ | |
315 | false), /* pcrel_offset */ | |
316 | ||
317 | { 0x10 }, | |
318 | { 0x11 }, | |
319 | ||
320 | /* TOC relative indirect load. */ | |
321 | HOWTO (0x12, /* type */ | |
322 | 0, /* rightshift */ | |
323 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
324 | 16, /* bitsize */ | |
325 | false, /* pc_relative */ | |
326 | 0, /* bitpos */ | |
327 | complain_overflow_bitfield, /* complain_on_overflow */ | |
328 | 0, /* special_function */ | |
329 | "R_TRL", /* name */ | |
330 | true, /* partial_inplace */ | |
331 | 0xffff, /* src_mask */ | |
332 | 0xffff, /* dst_mask */ | |
333 | false), /* pcrel_offset */ | |
334 | ||
335 | /* TOC relative load address. */ | |
336 | HOWTO (0x13, /* type */ | |
337 | 0, /* rightshift */ | |
338 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
339 | 16, /* bitsize */ | |
340 | false, /* pc_relative */ | |
341 | 0, /* bitpos */ | |
342 | complain_overflow_bitfield, /* complain_on_overflow */ | |
343 | 0, /* special_function */ | |
344 | "R_TRLA", /* name */ | |
345 | true, /* partial_inplace */ | |
346 | 0xffff, /* src_mask */ | |
347 | 0xffff, /* dst_mask */ | |
348 | false), /* pcrel_offset */ | |
349 | ||
350 | /* Modifiable relative branch. */ | |
351 | HOWTO (0x14, /* type */ | |
352 | 1, /* rightshift */ | |
353 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
354 | 32, /* bitsize */ | |
355 | false, /* pc_relative */ | |
356 | 0, /* bitpos */ | |
357 | complain_overflow_bitfield, /* complain_on_overflow */ | |
358 | 0, /* special_function */ | |
359 | "R_RRTBI", /* name */ | |
360 | true, /* partial_inplace */ | |
361 | 0xffffffff, /* src_mask */ | |
362 | 0xffffffff, /* dst_mask */ | |
363 | false), /* pcrel_offset */ | |
364 | ||
365 | /* Modifiable absolute branch. */ | |
366 | HOWTO (0x15, /* type */ | |
367 | 1, /* rightshift */ | |
368 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
369 | 32, /* bitsize */ | |
370 | false, /* pc_relative */ | |
371 | 0, /* bitpos */ | |
372 | complain_overflow_bitfield, /* complain_on_overflow */ | |
373 | 0, /* special_function */ | |
374 | "R_RRTBA", /* name */ | |
375 | true, /* partial_inplace */ | |
376 | 0xffffffff, /* src_mask */ | |
377 | 0xffffffff, /* dst_mask */ | |
378 | false), /* pcrel_offset */ | |
379 | ||
380 | /* Modifiable call absolute indirect. */ | |
381 | HOWTO (0x16, /* type */ | |
382 | 0, /* rightshift */ | |
383 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
384 | 16, /* bitsize */ | |
385 | false, /* pc_relative */ | |
386 | 0, /* bitpos */ | |
387 | complain_overflow_bitfield, /* complain_on_overflow */ | |
388 | 0, /* special_function */ | |
389 | "R_CAI", /* name */ | |
390 | true, /* partial_inplace */ | |
391 | 0xffff, /* src_mask */ | |
392 | 0xffff, /* dst_mask */ | |
393 | false), /* pcrel_offset */ | |
394 | ||
395 | /* Modifiable call relative. */ | |
396 | HOWTO (0x17, /* type */ | |
397 | 0, /* rightshift */ | |
398 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
399 | 16, /* bitsize */ | |
400 | false, /* pc_relative */ | |
401 | 0, /* bitpos */ | |
402 | complain_overflow_bitfield, /* complain_on_overflow */ | |
403 | 0, /* special_function */ | |
28a0c103 | 404 | "R_CREL", /* name */ |
8b8ddedb SG |
405 | true, /* partial_inplace */ |
406 | 0xffff, /* src_mask */ | |
407 | 0xffff, /* dst_mask */ | |
408 | false), /* pcrel_offset */ | |
409 | ||
410 | /* Modifiable branch absolute. */ | |
411 | HOWTO (0x18, /* type */ | |
412 | 0, /* rightshift */ | |
413 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
414 | 16, /* bitsize */ | |
415 | false, /* pc_relative */ | |
416 | 0, /* bitpos */ | |
417 | complain_overflow_bitfield, /* complain_on_overflow */ | |
418 | 0, /* special_function */ | |
419 | "R_RBA", /* name */ | |
420 | true, /* partial_inplace */ | |
421 | 0xffff, /* src_mask */ | |
422 | 0xffff, /* dst_mask */ | |
423 | false), /* pcrel_offset */ | |
424 | ||
425 | /* Modifiable branch absolute. */ | |
426 | HOWTO (0x19, /* type */ | |
427 | 0, /* rightshift */ | |
428 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
429 | 16, /* bitsize */ | |
430 | false, /* pc_relative */ | |
431 | 0, /* bitpos */ | |
432 | complain_overflow_bitfield, /* complain_on_overflow */ | |
433 | 0, /* special_function */ | |
434 | "R_RBAC", /* name */ | |
435 | true, /* partial_inplace */ | |
436 | 0xffff, /* src_mask */ | |
437 | 0xffff, /* dst_mask */ | |
438 | false), /* pcrel_offset */ | |
439 | ||
440 | /* Modifiable branch relative. */ | |
441 | HOWTO (0x1a, /* type */ | |
442 | 0, /* rightshift */ | |
443 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
444 | 26, /* bitsize */ | |
445 | false, /* pc_relative */ | |
446 | 0, /* bitpos */ | |
447 | complain_overflow_signed, /* complain_on_overflow */ | |
448 | 0, /* special_function */ | |
28a0c103 | 449 | "R_RBR", /* name */ |
8b8ddedb SG |
450 | true, /* partial_inplace */ |
451 | 0xffff, /* src_mask */ | |
452 | 0xffff, /* dst_mask */ | |
453 | false), /* pcrel_offset */ | |
454 | ||
455 | /* Modifiable branch absolute. */ | |
456 | HOWTO (0x1b, /* type */ | |
457 | 0, /* rightshift */ | |
458 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
459 | 16, /* bitsize */ | |
460 | false, /* pc_relative */ | |
461 | 0, /* bitpos */ | |
462 | complain_overflow_bitfield, /* complain_on_overflow */ | |
463 | 0, /* special_function */ | |
28a0c103 | 464 | "R_RBRC", /* name */ |
8b8ddedb SG |
465 | true, /* partial_inplace */ |
466 | 0xffff, /* src_mask */ | |
467 | 0xffff, /* dst_mask */ | |
468 | false) /* pcrel_offset */ | |
469 | }; | |
470 | ||
8b8ddedb | 471 | static void |
aadf04f7 | 472 | xcoff_rtype2howto (relent, internal) |
8b8ddedb SG |
473 | arelent *relent; |
474 | struct internal_reloc *internal; | |
475 | { | |
aadf04f7 | 476 | relent->howto = xcoff_howto_table + internal->r_type; |
8b8ddedb SG |
477 | |
478 | /* The r_size field of an XCOFF reloc encodes the bitsize of the | |
479 | relocation, as well as indicating whether it is signed or not. | |
480 | Doublecheck that the relocation information gathered from the | |
481 | type matches this information. */ | |
ae115e51 | 482 | if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1) |
8b8ddedb SG |
483 | abort (); |
484 | #if 0 | |
485 | if ((internal->r_size & 0x80) != 0 | |
486 | ? (relent->howto->complain_on_overflow != complain_overflow_signed) | |
487 | : (relent->howto->complain_on_overflow != complain_overflow_bitfield)) | |
488 | abort (); | |
489 | #endif | |
490 | } | |
491 | ||
ae115e51 | 492 | static reloc_howto_type * |
aadf04f7 | 493 | xcoff_reloc_type_lookup (abfd, code) |
8b8ddedb SG |
494 | bfd *abfd; |
495 | bfd_reloc_code_real_type code; | |
496 | { | |
497 | switch (code) | |
498 | { | |
499 | case BFD_RELOC_PPC_B26: | |
aadf04f7 | 500 | return &xcoff_howto_table[0xa]; |
8b8ddedb | 501 | case BFD_RELOC_PPC_BA26: |
aadf04f7 | 502 | return &xcoff_howto_table[8]; |
8b8ddedb | 503 | case BFD_RELOC_PPC_TOC16: |
aadf04f7 | 504 | return &xcoff_howto_table[3]; |
8b8ddedb | 505 | case BFD_RELOC_32: |
aad2c618 | 506 | case BFD_RELOC_CTOR: |
aadf04f7 | 507 | return &xcoff_howto_table[0]; |
8b8ddedb SG |
508 | default: |
509 | return NULL; | |
510 | } | |
511 | } | |
512 | ||
513 | #define SELECT_RELOC(internal, howto) \ | |
514 | { \ | |
515 | internal.r_type = howto->type; \ | |
516 | internal.r_size = \ | |
517 | ((howto->complain_on_overflow == complain_overflow_signed \ | |
518 | ? 0x80 \ | |
519 | : 0) \ | |
520 | | (howto->bitsize - 1)); \ | |
521 | } | |
aadf04f7 SS |
522 | \f |
523 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3) | |
8b8ddedb SG |
524 | |
525 | #define COFF_LONG_FILENAMES | |
f4bd7a8f | 526 | |
aadf04f7 SS |
527 | #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst) |
528 | ||
529 | #define coff_mkobject xcoff_mkobject | |
530 | #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data | |
531 | #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup | |
532 | #define coff_relocate_section _bfd_ppc_xcoff_relocate_section | |
533 | ||
c9301d7b | 534 | #include "coffcode.h" |
bb2deaf2 ILT |
535 | \f |
536 | /* XCOFF archive support. The original version of this code was by | |
537 | Damon A. Permezel. It was enhanced to permit cross support, and | |
538 | writing archive files, by Ian Lance Taylor, Cygnus Support. | |
539 | ||
540 | XCOFF uses its own archive format. Everything is hooked together | |
541 | with file offset links, so it is possible to rapidly update an | |
542 | archive in place. Of course, we don't do that. An XCOFF archive | |
543 | has a real file header, not just an ARMAG string. The structure of | |
544 | the file header and of each archive header appear below. | |
545 | ||
546 | An XCOFF archive also has a member table, which is a list of | |
547 | elements in the archive (you can get that by looking through the | |
548 | linked list, but you have to read a lot more of the file). The | |
549 | member table has a normal archive header with an empty name. It is | |
550 | normally (and perhaps must be) the second to last entry in the | |
551 | archive. The member table data is almost printable ASCII. It | |
552 | starts with a 12 character decimal string which is the number of | |
553 | entries in the table. For each entry it has a 12 character decimal | |
554 | string which is the offset in the archive of that member. These | |
555 | entries are followed by a series of null terminated strings which | |
556 | are the member names for each entry. | |
557 | ||
558 | Finally, an XCOFF archive has a global symbol table, which is what | |
559 | we call the armap. The global symbol table has a normal archive | |
560 | header with an empty name. It is normally (and perhaps must be) | |
561 | the last entry in the archive. The contents start with a four byte | |
562 | binary number which is the number of entries. This is followed by | |
563 | a that many four byte binary numbers; each is the file offset of an | |
564 | entry in the archive. These numbers are followed by a series of | |
565 | null terminated strings, which are symbol names. */ | |
566 | ||
567 | /* XCOFF archives use this as a magic string. */ | |
568 | ||
569 | #define XCOFFARMAG "<aiaff>\012" | |
570 | #define SXCOFFARMAG 8 | |
571 | ||
572 | /* This terminates an XCOFF archive member name. */ | |
573 | ||
574 | #define XCOFFARFMAG "`\012" | |
575 | #define SXCOFFARFMAG 2 | |
576 | ||
577 | /* XCOFF archives start with this (printable) structure. */ | |
578 | ||
579 | struct xcoff_ar_file_hdr | |
580 | { | |
581 | /* Magic string. */ | |
582 | char magic[SXCOFFARMAG]; | |
583 | ||
584 | /* Offset of the member table (decimal ASCII string). */ | |
585 | char memoff[12]; | |
586 | ||
587 | /* Offset of the global symbol table (decimal ASCII string). */ | |
588 | char symoff[12]; | |
589 | ||
590 | /* Offset of the first member in the archive (decimal ASCII string). */ | |
591 | char firstmemoff[12]; | |
592 | ||
593 | /* Offset of the last member in the archive (decimal ASCII string). */ | |
594 | char lastmemoff[12]; | |
595 | ||
596 | /* Offset of the first member on the free list (decimal ASCII | |
597 | string). */ | |
598 | char freeoff[12]; | |
599 | }; | |
ba9137fe | 600 | |
bb2deaf2 | 601 | #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG) |
ba9137fe | 602 | |
bb2deaf2 ILT |
603 | /* Each XCOFF archive member starts with this (printable) structure. */ |
604 | ||
605 | struct xcoff_ar_hdr | |
606 | { | |
607 | /* File size not including the header (decimal ASCII string). */ | |
608 | char size[12]; | |
609 | ||
610 | /* File offset of next archive member (decimal ASCII string). */ | |
611 | char nextoff[12]; | |
612 | ||
613 | /* File offset of previous archive member (decimal ASCII string). */ | |
614 | char prevoff[12]; | |
615 | ||
616 | /* File mtime (decimal ASCII string). */ | |
617 | char date[12]; | |
618 | ||
619 | /* File UID (decimal ASCII string). */ | |
620 | char uid[12]; | |
621 | ||
622 | /* File GID (decimal ASCII string). */ | |
623 | char gid[12]; | |
624 | ||
625 | /* File mode (octal ASCII string). */ | |
626 | char mode[12]; | |
627 | ||
628 | /* Length of file name (decimal ASCII string). */ | |
629 | char namlen[4]; | |
630 | ||
631 | /* This structure is followed by the file name. The length of the | |
632 | name is given in the namlen field. If the length of the name is | |
633 | odd, the name is followed by a null byte. The name and optional | |
634 | null byte are followed by XCOFFARFMAG, which is not included in | |
635 | namlen. The contents of the archive member follow; the number of | |
636 | bytes is given in the size field. */ | |
637 | }; | |
ba9137fe | 638 | |
bb2deaf2 | 639 | #define SIZEOF_AR_HDR (7 * 12 + 4) |
ba9137fe | 640 | |
bb2deaf2 ILT |
641 | /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the |
642 | artdata structure. */ | |
643 | #define xcoff_ardata(abfd) \ | |
644 | ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata) | |
ba9137fe | 645 | |
bb2deaf2 ILT |
646 | /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an |
647 | archive element. */ | |
648 | #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data)) | |
649 | #define arch_xhdr(bfd) \ | |
650 | ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header) | |
ba9137fe | 651 | |
bb2deaf2 ILT |
652 | /* XCOFF archives do not have anything which corresponds to an |
653 | extended name table. */ | |
ba9137fe | 654 | |
bb2deaf2 ILT |
655 | #define xcoff_slurp_extended_name_table bfd_false |
656 | #define xcoff_construct_extended_name_table \ | |
657 | ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \ | |
658 | bfd_false) | |
659 | #define xcoff_truncate_arname bfd_dont_truncate_arname | |
ba9137fe | 660 | |
bb2deaf2 | 661 | /* XCOFF archives do not have a timestamp. */ |
ba9137fe | 662 | |
bb2deaf2 | 663 | #define xcoff_update_armap_timestamp bfd_true |
ba9137fe | 664 | |
bb2deaf2 | 665 | /* Read in the armap of an XCOFF archive. */ |
ba9137fe JG |
666 | |
667 | static boolean | |
bb2deaf2 | 668 | xcoff_slurp_armap (abfd) |
ba9137fe JG |
669 | bfd *abfd; |
670 | { | |
bb2deaf2 ILT |
671 | file_ptr off; |
672 | struct xcoff_ar_hdr hdr; | |
673 | size_t namlen; | |
674 | bfd_size_type sz; | |
675 | bfd_byte *contents, *cend; | |
676 | unsigned int c, i; | |
677 | carsym *arsym; | |
678 | bfd_byte *p; | |
679 | ||
680 | if (xcoff_ardata (abfd) == NULL) | |
681 | { | |
682 | bfd_has_map (abfd) = false; | |
683 | return true; | |
684 | } | |
685 | ||
686 | off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10); | |
687 | if (off == 0) | |
688 | { | |
689 | bfd_has_map (abfd) = false; | |
690 | return true; | |
691 | } | |
692 | ||
693 | if (bfd_seek (abfd, off, SEEK_SET) != 0) | |
694 | return false; | |
695 | ||
696 | /* The symbol table starts with a normal archive header. */ | |
697 | if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR) | |
698 | return false; | |
699 | ||
700 | /* Skip the name (normally empty). */ | |
701 | namlen = strtol (hdr.namlen, (char **) NULL, 10); | |
702 | if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0) | |
703 | return false; | |
704 | ||
705 | /* Read in the entire symbol table. */ | |
706 | sz = strtol (hdr.size, (char **) NULL, 10); | |
707 | contents = (bfd_byte *) bfd_alloc (abfd, sz); | |
708 | if (contents == NULL) | |
a9713b91 | 709 | return false; |
bb2deaf2 ILT |
710 | if (bfd_read ((PTR) contents, 1, sz, abfd) != sz) |
711 | return false; | |
712 | ||
713 | /* The symbol table starts with a four byte count. */ | |
714 | c = bfd_h_get_32 (abfd, contents); | |
715 | ||
716 | if (c * 4 >= sz) | |
717 | { | |
718 | bfd_set_error (bfd_error_bad_value); | |
719 | return false; | |
720 | } | |
721 | ||
722 | bfd_ardata (abfd)->symdefs = ((carsym *) | |
723 | bfd_alloc (abfd, c * sizeof (carsym))); | |
724 | if (bfd_ardata (abfd)->symdefs == NULL) | |
a9713b91 | 725 | return false; |
bb2deaf2 ILT |
726 | |
727 | /* After the count comes a list of four byte file offsets. */ | |
728 | for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4; | |
729 | i < c; | |
730 | ++i, ++arsym, p += 4) | |
731 | arsym->file_offset = bfd_h_get_32 (abfd, p); | |
732 | ||
733 | /* After the file offsets come null terminated symbol names. */ | |
734 | cend = contents + sz; | |
735 | for (i = 0, arsym = bfd_ardata (abfd)->symdefs; | |
736 | i < c; | |
737 | ++i, ++arsym, p += strlen ((char *) p) + 1) | |
738 | { | |
739 | if (p >= cend) | |
740 | { | |
741 | bfd_set_error (bfd_error_bad_value); | |
742 | return false; | |
743 | } | |
744 | arsym->name = (char *) p; | |
745 | } | |
ba9137fe | 746 | |
bb2deaf2 ILT |
747 | bfd_ardata (abfd)->symdef_count = c; |
748 | bfd_has_map (abfd) = true; | |
ba9137fe | 749 | |
bb2deaf2 ILT |
750 | return true; |
751 | } | |
ba9137fe | 752 | |
bb2deaf2 | 753 | /* See if this is an XCOFF archive. */ |
ba9137fe | 754 | |
bb2deaf2 ILT |
755 | static const bfd_target * |
756 | xcoff_archive_p (abfd) | |
ba9137fe JG |
757 | bfd *abfd; |
758 | { | |
bb2deaf2 | 759 | struct xcoff_ar_file_hdr hdr; |
ba9137fe | 760 | |
bb2deaf2 ILT |
761 | if (bfd_read ((PTR) &hdr, SIZEOF_AR_FILE_HDR, 1, abfd) |
762 | != SIZEOF_AR_FILE_HDR) | |
763 | { | |
764 | if (bfd_get_error () != bfd_error_system_call) | |
765 | bfd_set_error (bfd_error_wrong_format); | |
766 | return NULL; | |
767 | } | |
ba9137fe | 768 | |
bb2deaf2 ILT |
769 | if (strncmp (hdr.magic, XCOFFARMAG, SXCOFFARMAG) != 0) |
770 | { | |
771 | bfd_set_error (bfd_error_wrong_format); | |
772 | return NULL; | |
773 | } | |
ba9137fe | 774 | |
bb2deaf2 ILT |
775 | /* We are setting bfd_ardata(abfd) here, but since bfd_ardata |
776 | involves a cast, we can't do it as the left operand of | |
777 | assignment. */ | |
778 | abfd->tdata.aout_ar_data = | |
779 | (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata)); | |
ba9137fe | 780 | |
bb2deaf2 | 781 | if (bfd_ardata (abfd) == (struct artdata *) NULL) |
a9713b91 | 782 | return NULL; |
ba9137fe | 783 | |
bb2deaf2 ILT |
784 | bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff, |
785 | (char **) NULL, 10); | |
786 | bfd_ardata (abfd)->cache = NULL; | |
787 | bfd_ardata (abfd)->archive_head = NULL; | |
788 | bfd_ardata (abfd)->symdefs = NULL; | |
789 | bfd_ardata (abfd)->extended_names = NULL; | |
ba9137fe | 790 | |
bb2deaf2 ILT |
791 | bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR); |
792 | if (bfd_ardata (abfd)->tdata == NULL) | |
a9713b91 | 793 | return NULL; |
ba9137fe | 794 | |
bb2deaf2 | 795 | memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR); |
ba9137fe | 796 | |
bb2deaf2 ILT |
797 | if (! xcoff_slurp_armap (abfd)) |
798 | { | |
799 | bfd_release (abfd, bfd_ardata (abfd)); | |
800 | abfd->tdata.aout_ar_data = (struct artdata *) NULL; | |
801 | return NULL; | |
802 | } | |
ba9137fe | 803 | |
bb2deaf2 | 804 | return abfd->xvec; |
ba9137fe JG |
805 | } |
806 | ||
bb2deaf2 | 807 | /* Read the archive header in an XCOFF archive. */ |
c9301d7b | 808 | |
bb2deaf2 ILT |
809 | static PTR |
810 | xcoff_read_ar_hdr (abfd) | |
811 | bfd *abfd; | |
c9301d7b | 812 | { |
bb2deaf2 ILT |
813 | struct xcoff_ar_hdr hdr; |
814 | size_t namlen; | |
815 | struct xcoff_ar_hdr *hdrp; | |
816 | struct areltdata *ret; | |
c9301d7b | 817 | |
bb2deaf2 | 818 | if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR) |
c9301d7b | 819 | return NULL; |
c9301d7b | 820 | |
bb2deaf2 ILT |
821 | namlen = strtol (hdr.namlen, (char **) NULL, 10); |
822 | hdrp = bfd_alloc (abfd, SIZEOF_AR_HDR + namlen + 1); | |
823 | if (hdrp == NULL) | |
a9713b91 | 824 | return NULL; |
bb2deaf2 ILT |
825 | memcpy (hdrp, &hdr, SIZEOF_AR_HDR); |
826 | if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen) | |
c9301d7b | 827 | return NULL; |
bb2deaf2 | 828 | ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0'; |
c9301d7b | 829 | |
bb2deaf2 ILT |
830 | ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata)); |
831 | if (ret == NULL) | |
a9713b91 | 832 | return NULL; |
bb2deaf2 ILT |
833 | ret->arch_header = (char *) hdrp; |
834 | ret->parsed_size = strtol (hdr.size, (char **) NULL, 10); | |
835 | ret->filename = (char *) hdrp + SIZEOF_AR_HDR; | |
836 | ||
837 | /* Skip over the XCOFFARFMAG at the end of the file name. */ | |
838 | if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0) | |
839 | return NULL; | |
c9301d7b | 840 | |
bb2deaf2 | 841 | return (PTR) ret; |
c9301d7b SC |
842 | } |
843 | ||
bb2deaf2 ILT |
844 | /* Open the next element in an XCOFF archive. */ |
845 | ||
ba9137fe | 846 | static bfd * |
bb2deaf2 ILT |
847 | xcoff_openr_next_archived_file (archive, last_file) |
848 | bfd *archive; | |
849 | bfd *last_file; | |
ba9137fe | 850 | { |
bb2deaf2 ILT |
851 | file_ptr filestart; |
852 | ||
853 | if (xcoff_ardata (archive) == NULL) | |
854 | { | |
855 | bfd_set_error (bfd_error_invalid_operation); | |
856 | return NULL; | |
857 | } | |
858 | ||
859 | if (last_file == NULL) | |
860 | filestart = bfd_ardata (archive)->first_file_filepos; | |
861 | else | |
862 | filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL, 10); | |
ba9137fe | 863 | |
bb2deaf2 ILT |
864 | if (filestart == 0 |
865 | || filestart == strtol (xcoff_ardata (archive)->memoff, | |
866 | (char **) NULL, 10) | |
867 | || filestart == strtol (xcoff_ardata (archive)->symoff, | |
868 | (char **) NULL, 10)) | |
869 | { | |
870 | bfd_set_error (bfd_error_no_more_archived_files); | |
871 | return NULL; | |
872 | } | |
ba9137fe | 873 | |
bb2deaf2 | 874 | return _bfd_get_elt_at_filepos (archive, filestart); |
ba9137fe JG |
875 | } |
876 | ||
bb2deaf2 | 877 | /* Stat an element in an XCOFF archive. */ |
ba9137fe | 878 | |
aadf04f7 | 879 | static int |
bb2deaf2 | 880 | xcoff_generic_stat_arch_elt (abfd, s) |
ba9137fe | 881 | bfd *abfd; |
bb2deaf2 | 882 | struct stat *s; |
ba9137fe | 883 | { |
bb2deaf2 | 884 | struct xcoff_ar_hdr *hdrp; |
ba9137fe | 885 | |
bb2deaf2 ILT |
886 | if (abfd->arelt_data == NULL) |
887 | { | |
888 | bfd_set_error (bfd_error_invalid_operation); | |
889 | return -1; | |
890 | } | |
ba9137fe | 891 | |
bb2deaf2 | 892 | hdrp = arch_xhdr (abfd); |
ba9137fe | 893 | |
bb2deaf2 ILT |
894 | s->st_mtime = strtol (hdrp->date, (char **) NULL, 10); |
895 | s->st_uid = strtol (hdrp->uid, (char **) NULL, 10); | |
896 | s->st_gid = strtol (hdrp->gid, (char **) NULL, 10); | |
897 | s->st_mode = strtol (hdrp->mode, (char **) NULL, 8); | |
898 | s->st_size = arch_eltdata (abfd)->parsed_size; | |
ba9137fe | 899 | |
bb2deaf2 ILT |
900 | return 0; |
901 | } | |
ba9137fe | 902 | |
bb2deaf2 ILT |
903 | /* Normalize a file name for inclusion in an archive. */ |
904 | ||
905 | static const char * | |
906 | normalize_filename (abfd) | |
907 | bfd *abfd; | |
908 | { | |
909 | const char *file; | |
910 | const char *filename; | |
911 | ||
912 | file = bfd_get_filename (abfd); | |
913 | filename = strrchr (file, '/'); | |
914 | if (filename != NULL) | |
915 | filename++; | |
916 | else | |
917 | filename = file; | |
918 | return filename; | |
ba9137fe JG |
919 | } |
920 | ||
bb2deaf2 | 921 | /* Write out an XCOFF armap. */ |
ba9137fe | 922 | |
bb2deaf2 ILT |
923 | /*ARGSUSED*/ |
924 | static boolean | |
925 | xcoff_write_armap (abfd, elength, map, orl_count, stridx) | |
926 | bfd *abfd; | |
927 | unsigned int elength; | |
928 | struct orl *map; | |
929 | unsigned int orl_count; | |
930 | int stridx; | |
ba9137fe | 931 | { |
bb2deaf2 ILT |
932 | struct xcoff_ar_hdr hdr; |
933 | char *p; | |
aadf04f7 | 934 | unsigned char buf[4]; |
bb2deaf2 ILT |
935 | bfd *sub; |
936 | file_ptr fileoff; | |
937 | unsigned int i; | |
938 | ||
939 | memset (&hdr, 0, sizeof hdr); | |
940 | sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx)); | |
941 | sprintf (hdr.nextoff, "%d", 0); | |
942 | memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12); | |
943 | sprintf (hdr.date, "%d", 0); | |
944 | sprintf (hdr.uid, "%d", 0); | |
945 | sprintf (hdr.gid, "%d", 0); | |
946 | sprintf (hdr.mode, "%d", 0); | |
947 | sprintf (hdr.namlen, "%d", 0); | |
948 | ||
949 | /* We need spaces, not null bytes, in the header. */ | |
950 | for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++) | |
951 | if (*p == '\0') | |
952 | *p = ' '; | |
953 | ||
954 | if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR | |
955 | || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG) | |
956 | return false; | |
ba9137fe | 957 | |
bb2deaf2 ILT |
958 | bfd_h_put_32 (abfd, orl_count, buf); |
959 | if (bfd_write (buf, 1, 4, abfd) != 4) | |
960 | return false; | |
961 | ||
962 | sub = abfd->archive_head; | |
963 | fileoff = SIZEOF_AR_FILE_HDR; | |
964 | i = 0; | |
965 | while (sub != NULL && i < orl_count) | |
966 | { | |
967 | size_t namlen; | |
968 | ||
969 | while (((bfd *) (map[i]).pos) == sub) | |
970 | { | |
971 | bfd_h_put_32 (abfd, fileoff, buf); | |
972 | if (bfd_write (buf, 1, 4, abfd) != 4) | |
973 | return false; | |
974 | ++i; | |
ba9137fe | 975 | } |
bb2deaf2 ILT |
976 | namlen = strlen (normalize_filename (sub)); |
977 | namlen = (namlen + 1) &~ 1; | |
978 | fileoff += (SIZEOF_AR_HDR | |
979 | + namlen | |
980 | + SXCOFFARFMAG | |
981 | + arelt_size (sub)); | |
982 | fileoff = (fileoff + 1) &~ 1; | |
983 | sub = sub->next; | |
984 | } | |
ba9137fe | 985 | |
bb2deaf2 ILT |
986 | for (i = 0; i < orl_count; i++) |
987 | { | |
988 | const char *name; | |
989 | size_t namlen; | |
990 | ||
991 | name = *map[i].name; | |
992 | namlen = strlen (name); | |
993 | if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1) | |
994 | return false; | |
995 | } | |
996 | ||
997 | if ((stridx & 1) != 0) | |
998 | { | |
999 | char b; | |
1000 | ||
1001 | b = '\0'; | |
1002 | if (bfd_write (&b, 1, 1, abfd) != 1) | |
1003 | return false; | |
1004 | } | |
1005 | ||
1006 | return true; | |
ba9137fe JG |
1007 | } |
1008 | ||
bb2deaf2 ILT |
1009 | /* Write out an XCOFF archive. We always write an entire archive, |
1010 | rather than fussing with the freelist and so forth. */ | |
1011 | ||
ba9137fe | 1012 | static boolean |
bb2deaf2 ILT |
1013 | xcoff_write_archive_contents (abfd) |
1014 | bfd *abfd; | |
ba9137fe | 1015 | { |
bb2deaf2 ILT |
1016 | struct xcoff_ar_file_hdr fhdr; |
1017 | size_t count; | |
1018 | size_t total_namlen; | |
1019 | file_ptr *offsets; | |
1020 | boolean makemap; | |
1021 | boolean hasobjects; | |
1022 | file_ptr prevoff, nextoff; | |
1023 | bfd *sub; | |
1024 | unsigned int i; | |
1025 | struct xcoff_ar_hdr ahdr; | |
1026 | bfd_size_type size; | |
1027 | char *p; | |
1028 | char decbuf[13]; | |
1029 | ||
1030 | memset (&fhdr, 0, sizeof fhdr); | |
1031 | strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG); | |
1032 | sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR); | |
1033 | sprintf (fhdr.freeoff, "%d", 0); | |
1034 | ||
1035 | count = 0; | |
1036 | total_namlen = 0; | |
1037 | for (sub = abfd->archive_head; sub != NULL; sub = sub->next) | |
1038 | { | |
1039 | ++count; | |
1040 | total_namlen += strlen (normalize_filename (sub)) + 1; | |
1041 | } | |
1042 | offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr)); | |
1043 | if (offsets == NULL) | |
a9713b91 | 1044 | return false; |
bb2deaf2 ILT |
1045 | |
1046 | if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0) | |
1047 | return false; | |
1048 | ||
1049 | makemap = bfd_has_map (abfd); | |
1050 | hasobjects = false; | |
1051 | prevoff = 0; | |
1052 | nextoff = SIZEOF_AR_FILE_HDR; | |
1053 | for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++) | |
1054 | { | |
1055 | const char *name; | |
1056 | size_t namlen; | |
1057 | struct xcoff_ar_hdr *ahdrp; | |
1058 | bfd_size_type remaining; | |
1059 | ||
1060 | if (makemap && ! hasobjects) | |
1061 | { | |
1062 | if (bfd_check_format (sub, bfd_object)) | |
1063 | hasobjects = true; | |
1064 | } | |
1065 | ||
1066 | name = normalize_filename (sub); | |
1067 | namlen = strlen (name); | |
1068 | ||
1069 | if (sub->arelt_data != NULL) | |
1070 | ahdrp = arch_xhdr (sub); | |
1071 | else | |
1072 | ahdrp = NULL; | |
1073 | ||
1074 | if (ahdrp == NULL) | |
1075 | { | |
1076 | struct stat s; | |
1077 | ||
1078 | memset (&ahdr, 0, sizeof ahdr); | |
1079 | ahdrp = &ahdr; | |
1080 | if (stat (bfd_get_filename (sub), &s) != 0) | |
1081 | { | |
1082 | bfd_set_error (bfd_error_system_call); | |
45b5d2f0 | 1083 | return false; |
bb2deaf2 ILT |
1084 | } |
1085 | ||
1086 | sprintf (ahdrp->size, "%ld", (long) s.st_size); | |
1087 | sprintf (ahdrp->date, "%ld", (long) s.st_mtime); | |
1088 | sprintf (ahdrp->uid, "%ld", (long) s.st_uid); | |
1089 | sprintf (ahdrp->gid, "%ld", (long) s.st_gid); | |
1090 | sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode); | |
1091 | ||
1092 | if (sub->arelt_data == NULL) | |
1093 | { | |
1094 | sub->arelt_data = ((struct areltdata *) | |
1095 | bfd_alloc (sub, sizeof (struct areltdata))); | |
1096 | if (sub->arelt_data == NULL) | |
a9713b91 | 1097 | return false; |
bb2deaf2 ILT |
1098 | } |
1099 | ||
1100 | arch_eltdata (sub)->parsed_size = s.st_size; | |
1101 | } | |
1102 | ||
1103 | sprintf (ahdrp->prevoff, "%ld", (long) prevoff); | |
1104 | sprintf (ahdrp->namlen, "%ld", (long) namlen); | |
1105 | ||
1106 | /* If the length of the name is odd, we write out the null byte | |
1107 | after the name as well. */ | |
1108 | namlen = (namlen + 1) &~ 1; | |
1109 | ||
1110 | remaining = arelt_size (sub); | |
1111 | size = (SIZEOF_AR_HDR | |
1112 | + namlen | |
1113 | + SXCOFFARFMAG | |
1114 | + remaining); | |
1115 | ||
1116 | BFD_ASSERT (nextoff == bfd_tell (abfd)); | |
1117 | ||
1118 | offsets[i] = nextoff; | |
1119 | ||
1120 | prevoff = nextoff; | |
1121 | nextoff += size + (size & 1); | |
1122 | ||
1123 | sprintf (ahdrp->nextoff, "%ld", (long) nextoff); | |
1124 | ||
1125 | /* We need spaces, not null bytes, in the header. */ | |
1126 | for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++) | |
1127 | if (*p == '\0') | |
1128 | *p = ' '; | |
1129 | ||
1130 | if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR | |
1131 | || bfd_write ((PTR) name, 1, namlen, abfd) != namlen | |
1132 | || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) | |
1133 | != SXCOFFARFMAG)) | |
ba9137fe | 1134 | return false; |
c9301d7b | 1135 | |
bb2deaf2 ILT |
1136 | if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0) |
1137 | return false; | |
1138 | while (remaining != 0) | |
1139 | { | |
1140 | bfd_size_type amt; | |
1141 | bfd_byte buffer[DEFAULT_BUFFERSIZE]; | |
1142 | ||
1143 | amt = sizeof buffer; | |
1144 | if (amt > remaining) | |
1145 | amt = remaining; | |
1146 | if (bfd_read (buffer, 1, amt, sub) != amt | |
1147 | || bfd_write (buffer, 1, amt, abfd) != amt) | |
1148 | return false; | |
1149 | remaining -= amt; | |
1150 | } | |
1151 | ||
1152 | if ((size & 1) != 0) | |
1153 | { | |
1154 | bfd_byte b; | |
1155 | ||
1156 | b = '\0'; | |
1157 | if (bfd_write (&b, 1, 1, abfd) != 1) | |
1158 | return false; | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | sprintf (fhdr.lastmemoff, "%ld", (long) prevoff); | |
1163 | ||
1164 | /* Write out the member table. */ | |
1165 | ||
1166 | BFD_ASSERT (nextoff == bfd_tell (abfd)); | |
1167 | sprintf (fhdr.memoff, "%ld", (long) nextoff); | |
1168 | ||
1169 | memset (&ahdr, 0, sizeof ahdr); | |
1170 | sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen)); | |
1171 | sprintf (ahdr.prevoff, "%ld", (long) prevoff); | |
1172 | sprintf (ahdr.date, "%d", 0); | |
1173 | sprintf (ahdr.uid, "%d", 0); | |
1174 | sprintf (ahdr.gid, "%d", 0); | |
1175 | sprintf (ahdr.mode, "%d", 0); | |
1176 | sprintf (ahdr.namlen, "%d", 0); | |
1177 | ||
1178 | size = (SIZEOF_AR_HDR | |
1179 | + 12 | |
1180 | + count * 12 | |
1181 | + total_namlen | |
1182 | + SXCOFFARFMAG); | |
1183 | ||
1184 | prevoff = nextoff; | |
1185 | nextoff += size + (size & 1); | |
1186 | ||
1187 | if (makemap && hasobjects) | |
1188 | sprintf (ahdr.nextoff, "%ld", (long) nextoff); | |
1189 | else | |
1190 | sprintf (ahdr.nextoff, "%d", 0); | |
1191 | ||
1192 | /* We need spaces, not null bytes, in the header. */ | |
1193 | for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++) | |
1194 | if (*p == '\0') | |
1195 | *p = ' '; | |
1196 | ||
1197 | if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR | |
1198 | || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) | |
1199 | != SXCOFFARFMAG)) | |
1200 | return false; | |
1201 | ||
1202 | sprintf (decbuf, "%-12ld", (long) count); | |
1203 | if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12) | |
1204 | return false; | |
1205 | for (i = 0; i < count; i++) | |
1206 | { | |
1207 | sprintf (decbuf, "%-12ld", (long) offsets[i]); | |
1208 | if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12) | |
1209 | return false; | |
1210 | } | |
1211 | for (sub = abfd->archive_head; sub != NULL; sub = sub->next) | |
1212 | { | |
1213 | const char *name; | |
1214 | size_t namlen; | |
1215 | ||
1216 | name = normalize_filename (sub); | |
1217 | namlen = strlen (name); | |
1218 | if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1) | |
1219 | return false; | |
1220 | } | |
1221 | if ((size & 1) != 0) | |
1222 | { | |
1223 | bfd_byte b; | |
1224 | ||
1225 | b = '\0'; | |
1226 | if (bfd_write ((PTR) &b, 1, 1, abfd) != 1) | |
1227 | return false; | |
1228 | } | |
1229 | ||
1230 | /* Write out the armap, if appropriate. */ | |
1231 | ||
1232 | if (! makemap || ! hasobjects) | |
1233 | sprintf (fhdr.symoff, "%d", 0); | |
1234 | else | |
1235 | { | |
1236 | BFD_ASSERT (nextoff == bfd_tell (abfd)); | |
1237 | sprintf (fhdr.symoff, "%ld", (long) nextoff); | |
1238 | bfd_ardata (abfd)->tdata = (PTR) &fhdr; | |
1239 | if (! _bfd_compute_and_write_armap (abfd, 0)) | |
1240 | return false; | |
1241 | } | |
1242 | ||
1243 | /* Write out the archive file header. */ | |
1244 | ||
1245 | /* We need spaces, not null bytes, in the header. */ | |
1246 | for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++) | |
1247 | if (*p == '\0') | |
1248 | *p = ' '; | |
1249 | ||
1250 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 | |
1251 | || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) != | |
1252 | SIZEOF_AR_FILE_HDR)) | |
1253 | return false; | |
1254 | ||
1255 | return true; | |
1256 | } | |
294eaca4 | 1257 | \f |
a9713b91 ILT |
1258 | /* We can't use the usual coff_sizeof_headers routine, because AIX |
1259 | always uses an a.out header. */ | |
1260 | ||
1261 | /*ARGSUSED*/ | |
1262 | static int | |
1263 | _bfd_xcoff_sizeof_headers (abfd, reloc) | |
1264 | bfd *abfd; | |
1265 | boolean reloc; | |
1266 | { | |
1267 | int size; | |
1268 | ||
1269 | size = FILHSZ; | |
1270 | if (xcoff_data (abfd)->full_aouthdr) | |
1271 | size += AOUTSZ; | |
1272 | else | |
1273 | size += SMALL_AOUTSZ; | |
1274 | size += abfd->section_count * SCNHSZ; | |
1275 | return size; | |
1276 | } | |
1277 | \f | |
8b8ddedb SG |
1278 | #define CORE_FILE_P _bfd_dummy_target |
1279 | ||
6812b607 ILT |
1280 | #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command |
1281 | #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal | |
1282 | #define coff_core_file_matches_executable_p \ | |
1283 | _bfd_nocore_core_file_matches_executable_p | |
1284 | ||
ae115e51 | 1285 | #ifdef AIX_CORE |
8b8ddedb SG |
1286 | #undef CORE_FILE_P |
1287 | #define CORE_FILE_P rs6000coff_core_p | |
ae115e51 | 1288 | extern const bfd_target * rs6000coff_core_p (); |
294eaca4 SC |
1289 | extern boolean rs6000coff_get_section_contents (); |
1290 | extern boolean rs6000coff_core_file_matches_executable_p (); | |
c9301d7b SC |
1291 | |
1292 | #undef coff_core_file_matches_executable_p | |
1293 | #define coff_core_file_matches_executable_p \ | |
1294 | rs6000coff_core_file_matches_executable_p | |
ba9137fe | 1295 | |
bb2deaf2 ILT |
1296 | extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd)); |
1297 | #undef coff_core_file_failing_command | |
1298 | #define coff_core_file_failing_command rs6000coff_core_file_failing_command | |
1299 | ||
1300 | extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd)); | |
1301 | #undef coff_core_file_failing_signal | |
1302 | #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal | |
1303 | ||
c9301d7b SC |
1304 | #undef coff_get_section_contents |
1305 | #define coff_get_section_contents rs6000coff_get_section_contents | |
ae115e51 | 1306 | #endif /* AIX_CORE */ |
8b8ddedb | 1307 | |
ae115e51 | 1308 | #ifdef LYNX_CORE |
8b8ddedb SG |
1309 | |
1310 | #undef CORE_FILE_P | |
1311 | #define CORE_FILE_P lynx_core_file_p | |
ae115e51 | 1312 | extern const bfd_target *lynx_core_file_p PARAMS ((bfd *abfd)); |
8b8ddedb SG |
1313 | |
1314 | extern boolean lynx_core_file_matches_executable_p PARAMS ((bfd *core_bfd, | |
1315 | bfd *exec_bfd)); | |
1316 | #undef coff_core_file_matches_executable_p | |
1317 | #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p | |
1318 | ||
1319 | extern char *lynx_core_file_failing_command PARAMS ((bfd *abfd)); | |
1320 | #undef coff_core_file_failing_command | |
1321 | #define coff_core_file_failing_command lynx_core_file_failing_command | |
1322 | ||
1323 | extern int lynx_core_file_failing_signal PARAMS ((bfd *abfd)); | |
1324 | #undef coff_core_file_failing_signal | |
1325 | #define coff_core_file_failing_signal lynx_core_file_failing_signal | |
1326 | ||
ae115e51 | 1327 | #endif /* LYNX_CORE */ |
ba9137fe | 1328 | |
aadf04f7 SS |
1329 | #define _bfd_xcoff_bfd_get_relocated_section_contents \ |
1330 | coff_bfd_get_relocated_section_contents | |
1331 | #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section | |
1332 | #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section | |
1333 | ||
c9301d7b SC |
1334 | /* The transfer vector that leads the outside world to all of the above. */ |
1335 | ||
a9713b91 ILT |
1336 | const bfd_target |
1337 | #ifdef TARGET_SYM | |
1338 | TARGET_SYM = | |
1339 | #else | |
1340 | rs6000coff_vec = | |
1341 | #endif | |
c9301d7b | 1342 | { |
a9713b91 ILT |
1343 | #ifdef TARGET_NAME |
1344 | TARGET_NAME, | |
1345 | #else | |
c9301d7b | 1346 | "aixcoff-rs6000", /* name */ |
a9713b91 | 1347 | #endif |
c9301d7b SC |
1348 | bfd_target_coff_flavour, |
1349 | true, /* data byte order is big */ | |
1350 | true, /* header byte order is big */ | |
1351 | ||
1352 | (HAS_RELOC | EXEC_P | /* object flags */ | |
aadf04f7 | 1353 | HAS_LINENO | HAS_DEBUG | DYNAMIC | |
f4bd7a8f | 1354 | HAS_SYMS | HAS_LOCALS | WP_TEXT), |
c9301d7b SC |
1355 | |
1356 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
2d7de17d | 1357 | 0, /* leading char */ |
c9301d7b SC |
1358 | '/', /* ar_pad_char */ |
1359 | 15, /* ar_max_namelen??? FIXMEmgo */ | |
c9301d7b | 1360 | |
f4bd7a8f DM |
1361 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
1362 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1363 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ | |
1364 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
1365 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1366 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ | |
c9301d7b SC |
1367 | |
1368 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */ | |
bb2deaf2 ILT |
1369 | xcoff_archive_p, CORE_FILE_P}, |
1370 | {bfd_false, coff_mkobject, /* bfd_set_format */ | |
1371 | _bfd_generic_mkarchive, bfd_false}, | |
c9301d7b | 1372 | {bfd_false, coff_write_object_contents, /* bfd_write_contents */ |
bb2deaf2 | 1373 | xcoff_write_archive_contents, bfd_false}, |
c9301d7b | 1374 | |
6812b607 ILT |
1375 | BFD_JUMP_TABLE_GENERIC (coff), |
1376 | BFD_JUMP_TABLE_COPY (coff), | |
1377 | BFD_JUMP_TABLE_CORE (coff), | |
bb2deaf2 | 1378 | BFD_JUMP_TABLE_ARCHIVE (xcoff), |
6812b607 ILT |
1379 | BFD_JUMP_TABLE_SYMBOLS (coff), |
1380 | BFD_JUMP_TABLE_RELOCS (coff), | |
1381 | BFD_JUMP_TABLE_WRITE (coff), | |
aadf04f7 | 1382 | BFD_JUMP_TABLE_LINK (_bfd_xcoff), |
ae115e51 | 1383 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
6812b607 | 1384 | |
f4bd7a8f | 1385 | COFF_SWAP_TABLE, |
c9301d7b | 1386 | }; |