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