]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back-end for archive files (libraries). |
7898deda | 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
54327882 | 3 | 2000, 2001, 2002 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault. | |
6 | ||
7 | This file is part of BFD, the Binary File Descriptor library. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | /* | |
24 | @setfilename archive-info | |
25 | SECTION | |
26 | Archives | |
27 | ||
28 | DESCRIPTION | |
29 | An archive (or library) is just another BFD. It has a symbol | |
30 | table, although there's not much a user program will do with it. | |
31 | ||
32 | The big difference between an archive BFD and an ordinary BFD | |
33 | is that the archive doesn't have sections. Instead it has a | |
34 | chain of BFDs that are considered its contents. These BFDs can | |
35 | be manipulated like any other. The BFDs contained in an | |
36 | archive opened for reading will all be opened for reading. You | |
37 | may put either input or output BFDs into an archive opened for | |
38 | output; they will be handled correctly when the archive is closed. | |
39 | ||
40 | Use <<bfd_openr_next_archived_file>> to step through | |
41 | the contents of an archive opened for input. You don't | |
42 | have to read the entire archive if you don't want | |
43 | to! Read it until you find what you want. | |
44 | ||
45 | Archive contents of output BFDs are chained through the | |
46 | <<next>> pointer in a BFD. The first one is findable through | |
47 | the <<archive_head>> slot of the archive. Set it with | |
48 | <<bfd_set_archive_head>> (q.v.). A given BFD may be in only one | |
49 | open output archive at a time. | |
50 | ||
51 | As expected, the BFD archive code is more general than the | |
52 | archive code of any given environment. BFD archives may | |
53 | contain files of different formats (e.g., a.out and coff) and | |
54 | even different architectures. You may even place archives | |
55 | recursively into archives! | |
56 | ||
57 | This can cause unexpected confusion, since some archive | |
58 | formats are more expressive than others. For instance, Intel | |
59 | COFF archives can preserve long filenames; SunOS a.out archives | |
60 | cannot. If you move a file from the first to the second | |
61 | format and back again, the filename may be truncated. | |
62 | Likewise, different a.out environments have different | |
63 | conventions as to how they truncate filenames, whether they | |
64 | preserve directory names in filenames, etc. When | |
65 | interoperating with native tools, be sure your files are | |
66 | homogeneous. | |
67 | ||
68 | Beware: most of these formats do not react well to the | |
69 | presence of spaces in filenames. We do the best we can, but | |
70 | can't always handle this case due to restrictions in the format of | |
71 | archives. Many Unix utilities are braindead in regards to | |
72 | spaces and such in filenames anyway, so this shouldn't be much | |
73 | of a restriction. | |
74 | ||
75 | Archives are supported in BFD in <<archive.c>>. | |
76 | ||
77 | */ | |
78 | ||
79 | /* Assumes: | |
80 | o - all archive elements start on an even boundary, newline padded; | |
81 | o - all arch headers are char *; | |
82 | o - all arch headers are the same size (across architectures). | |
83 | */ | |
84 | ||
85 | /* Some formats provide a way to cram a long filename into the short | |
86 | (16 chars) space provided by a BSD archive. The trick is: make a | |
87 | special "file" in the front of the archive, sort of like the SYMDEF | |
88 | entry. If the filename is too long to fit, put it in the extended | |
89 | name table, and use its index as the filename. To prevent | |
90 | confusion prepend the index with a space. This means you can't | |
91 | have filenames that start with a space, but then again, many Unix | |
92 | utilities can't handle that anyway. | |
93 | ||
94 | This scheme unfortunately requires that you stand on your head in | |
95 | order to write an archive since you need to put a magic file at the | |
96 | front, and need to touch every entry to do so. C'est la vie. | |
97 | ||
98 | We support two variants of this idea: | |
99 | The SVR4 format (extended name table is named "//"), | |
100 | and an extended pseudo-BSD variant (extended name table is named | |
101 | "ARFILENAMES/"). The origin of the latter format is uncertain. | |
102 | ||
103 | BSD 4.4 uses a third scheme: It writes a long filename | |
104 | directly after the header. This allows 'ar q' to work. | |
105 | We currently can read BSD 4.4 archives, but not write them. | |
106 | */ | |
107 | ||
108 | /* Summary of archive member names: | |
109 | ||
110 | Symbol table (must be first): | |
111 | "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib. | |
112 | "/ " - Symbol table, system 5 style. | |
113 | ||
114 | Long name table (must be before regular file members): | |
115 | "// " - Long name table, System 5 R4 style. | |
116 | "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4). | |
117 | ||
118 | Regular file members with short names: | |
119 | "filename.o/ " - Regular file, System 5 style (embedded spaces ok). | |
120 | "filename.o " - Regular file, Berkeley style (no embedded spaces). | |
121 | ||
122 | Regular files with long names (or embedded spaces, for BSD variants): | |
123 | "/18 " - SVR4 style, name at offset 18 in name table. | |
124 | "#1/23 " - Long name (or embedded paces) 23 characters long, | |
125 | BSD 4.4 style, full name follows header. | |
126 | Implemented for reading, not writing. | |
127 | " 18 " - Long name 18 characters long, extended pseudo-BSD. | |
128 | */ | |
129 | ||
130 | #include "bfd.h" | |
131 | #include "sysdep.h" | |
132 | #include "libbfd.h" | |
133 | #include "aout/ar.h" | |
134 | #include "aout/ranlib.h" | |
3882b010 | 135 | #include "safe-ctype.h" |
252b5132 RH |
136 | |
137 | #ifndef errno | |
138 | extern int errno; | |
139 | #endif | |
140 | ||
141 | #ifdef GNU960 | |
142 | #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB) | |
143 | #endif | |
144 | ||
252b5132 RH |
145 | /* We keep a cache of archive filepointers to archive elements to |
146 | speed up searching the archive by filepos. We only add an entry to | |
147 | the cache when we actually read one. We also don't sort the cache; | |
148 | it's generally short enough to search linearly. | |
149 | Note that the pointers here point to the front of the ar_hdr, not | |
047066e1 KH |
150 | to the front of the contents! */ |
151 | struct ar_cache { | |
252b5132 RH |
152 | file_ptr ptr; |
153 | bfd *arelt; | |
154 | struct ar_cache *next; | |
155 | }; | |
156 | ||
157 | #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char) | |
158 | #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen) | |
159 | ||
beb0d161 | 160 | #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data)) |
252b5132 RH |
161 | #define arch_hdr(bfd) ((struct ar_hdr *)arch_eltdata(bfd)->arch_header) |
162 | ||
b34976b6 AM |
163 | static char *get_extended_arelt_filename |
164 | PARAMS ((bfd *arch, const char *name)); | |
165 | static bfd_boolean do_slurp_bsd_armap | |
166 | PARAMS ((bfd *abfd)); | |
167 | static bfd_boolean do_slurp_coff_armap | |
168 | PARAMS ((bfd *abfd)); | |
169 | bfd_boolean bfd_elf64_archive_slurp_armap | |
170 | PARAMS ((bfd *abfd)); | |
171 | static const char *normalize | |
172 | PARAMS ((bfd *, const char *file)); | |
173 | static struct areltdata *bfd_ar_hdr_from_filesystem | |
174 | PARAMS ((bfd *abfd, const char *, bfd *member)); | |
252b5132 | 175 | \f |
b34976b6 | 176 | bfd_boolean |
252b5132 RH |
177 | _bfd_generic_mkarchive (abfd) |
178 | bfd *abfd; | |
179 | { | |
dc810e39 | 180 | bfd_size_type amt = sizeof (struct artdata); |
252b5132 | 181 | |
dc810e39 | 182 | abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt); |
252b5132 | 183 | if (bfd_ardata (abfd) == NULL) |
b34976b6 | 184 | return FALSE; |
252b5132 RH |
185 | |
186 | bfd_ardata (abfd)->cache = NULL; | |
187 | bfd_ardata (abfd)->archive_head = NULL; | |
188 | bfd_ardata (abfd)->symdefs = NULL; | |
189 | bfd_ardata (abfd)->extended_names = NULL; | |
190 | bfd_ardata (abfd)->tdata = NULL; | |
191 | ||
b34976b6 | 192 | return TRUE; |
252b5132 RH |
193 | } |
194 | ||
195 | /* | |
196 | FUNCTION | |
197 | bfd_get_next_mapent | |
198 | ||
199 | SYNOPSIS | |
200 | symindex bfd_get_next_mapent(bfd *abfd, symindex previous, carsym **sym); | |
201 | ||
202 | DESCRIPTION | |
203 | Step through archive @var{abfd}'s symbol table (if it | |
204 | has one). Successively update @var{sym} with the next symbol's | |
205 | information, returning that symbol's (internal) index into the | |
206 | symbol table. | |
207 | ||
208 | Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get | |
209 | the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already | |
210 | got the last one. | |
211 | ||
212 | A <<carsym>> is a canonical archive symbol. The only | |
213 | user-visible element is its name, a null-terminated string. | |
214 | */ | |
215 | ||
216 | symindex | |
217 | bfd_get_next_mapent (abfd, prev, entry) | |
218 | bfd *abfd; | |
219 | symindex prev; | |
220 | carsym **entry; | |
221 | { | |
222 | if (!bfd_has_map (abfd)) | |
223 | { | |
224 | bfd_set_error (bfd_error_invalid_operation); | |
225 | return BFD_NO_MORE_SYMBOLS; | |
226 | } | |
227 | ||
228 | if (prev == BFD_NO_MORE_SYMBOLS) | |
229 | prev = 0; | |
230 | else | |
231 | ++prev; | |
232 | if (prev >= bfd_ardata (abfd)->symdef_count) | |
233 | return BFD_NO_MORE_SYMBOLS; | |
234 | ||
235 | *entry = (bfd_ardata (abfd)->symdefs + prev); | |
236 | return prev; | |
237 | } | |
238 | ||
239 | /* To be called by backends only */ | |
240 | ||
241 | bfd * | |
242 | _bfd_create_empty_archive_element_shell (obfd) | |
243 | bfd *obfd; | |
244 | { | |
245 | return _bfd_new_bfd_contained_in (obfd); | |
246 | } | |
247 | ||
248 | /* | |
249 | FUNCTION | |
250 | bfd_set_archive_head | |
251 | ||
252 | SYNOPSIS | |
b34976b6 | 253 | bfd_boolean bfd_set_archive_head(bfd *output, bfd *new_head); |
252b5132 RH |
254 | |
255 | DESCRIPTION | |
256 | Set the head of the chain of | |
257 | BFDs contained in the archive @var{output} to @var{new_head}. | |
258 | */ | |
259 | ||
b34976b6 | 260 | bfd_boolean |
252b5132 RH |
261 | bfd_set_archive_head (output_archive, new_head) |
262 | bfd *output_archive; | |
263 | bfd *new_head; | |
264 | { | |
265 | ||
266 | output_archive->archive_head = new_head; | |
b34976b6 | 267 | return TRUE; |
252b5132 RH |
268 | } |
269 | ||
270 | bfd * | |
271 | _bfd_look_for_bfd_in_cache (arch_bfd, filepos) | |
272 | bfd *arch_bfd; | |
273 | file_ptr filepos; | |
274 | { | |
275 | struct ar_cache *current; | |
276 | ||
277 | for (current = bfd_ardata (arch_bfd)->cache; current != NULL; | |
278 | current = current->next) | |
279 | if (current->ptr == filepos) | |
280 | return current->arelt; | |
281 | ||
282 | return NULL; | |
283 | } | |
284 | ||
285 | /* Kind of stupid to call cons for each one, but we don't do too many */ | |
b34976b6 | 286 | bfd_boolean |
252b5132 RH |
287 | _bfd_add_bfd_to_archive_cache (arch_bfd, filepos, new_elt) |
288 | bfd *arch_bfd, *new_elt; | |
289 | file_ptr filepos; | |
290 | { | |
dc810e39 | 291 | bfd_size_type amt = sizeof (struct ar_cache); |
252b5132 | 292 | |
dc810e39 | 293 | struct ar_cache *new_cache = (struct ar_cache *) bfd_zalloc (arch_bfd, amt); |
252b5132 | 294 | if (new_cache == NULL) |
b34976b6 | 295 | return FALSE; |
252b5132 RH |
296 | |
297 | new_cache->ptr = filepos; | |
298 | new_cache->arelt = new_elt; | |
299 | new_cache->next = (struct ar_cache *) NULL; | |
300 | if (bfd_ardata (arch_bfd)->cache == NULL) | |
301 | bfd_ardata (arch_bfd)->cache = new_cache; | |
302 | else | |
303 | { | |
304 | struct ar_cache *current = bfd_ardata (arch_bfd)->cache; | |
305 | ||
306 | while (current->next != NULL) | |
307 | current = current->next; | |
308 | current->next = new_cache; | |
309 | } | |
310 | ||
b34976b6 | 311 | return TRUE; |
252b5132 RH |
312 | } |
313 | \f | |
314 | /* The name begins with space. Hence the rest of the name is an index into | |
d70910e8 | 315 | the string table. */ |
252b5132 RH |
316 | |
317 | static char * | |
318 | get_extended_arelt_filename (arch, name) | |
319 | bfd *arch; | |
320 | const char *name; | |
321 | { | |
322 | unsigned long index = 0; | |
323 | ||
324 | /* Should extract string so that I can guarantee not to overflow into | |
d70910e8 | 325 | the next region, but I'm too lazy. */ |
252b5132 | 326 | errno = 0; |
d70910e8 | 327 | /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */ |
252b5132 RH |
328 | index = strtol (name + 1, NULL, 10); |
329 | if (errno != 0) | |
330 | { | |
331 | bfd_set_error (bfd_error_malformed_archive); | |
332 | return NULL; | |
333 | } | |
334 | ||
335 | return bfd_ardata (arch)->extended_names + index; | |
336 | } | |
337 | ||
338 | /* This functions reads an arch header and returns an areltdata pointer, or | |
339 | NULL on error. | |
340 | ||
341 | Presumes the file pointer is already in the right place (ie pointing | |
342 | to the ar_hdr in the file). Moves the file pointer; on success it | |
343 | should be pointing to the front of the file contents; on failure it | |
344 | could have been moved arbitrarily. | |
345 | */ | |
346 | ||
347 | PTR | |
348 | _bfd_generic_read_ar_hdr (abfd) | |
349 | bfd *abfd; | |
350 | { | |
351 | return _bfd_generic_read_ar_hdr_mag (abfd, (const char *) NULL); | |
352 | } | |
353 | ||
354 | /* Alpha ECOFF uses an optional different ARFMAG value, so we have a | |
355 | variant of _bfd_generic_read_ar_hdr which accepts a magic string. */ | |
356 | ||
357 | PTR | |
358 | _bfd_generic_read_ar_hdr_mag (abfd, mag) | |
359 | bfd *abfd; | |
360 | const char *mag; | |
361 | { | |
362 | struct ar_hdr hdr; | |
363 | char *hdrp = (char *) &hdr; | |
dc810e39 | 364 | size_t parsed_size; |
252b5132 RH |
365 | struct areltdata *ared; |
366 | char *filename = NULL; | |
dc810e39 AM |
367 | bfd_size_type namelen = 0; |
368 | bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr); | |
252b5132 RH |
369 | char *allocptr = 0; |
370 | ||
dc810e39 | 371 | if (bfd_bread ((PTR) hdrp, (bfd_size_type) sizeof (struct ar_hdr), abfd) |
252b5132 RH |
372 | != sizeof (struct ar_hdr)) |
373 | { | |
374 | if (bfd_get_error () != bfd_error_system_call) | |
375 | bfd_set_error (bfd_error_no_more_archived_files); | |
376 | return NULL; | |
377 | } | |
378 | if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0 | |
379 | && (mag == NULL | |
380 | || strncmp (hdr.ar_fmag, mag, 2) != 0)) | |
381 | { | |
382 | bfd_set_error (bfd_error_malformed_archive); | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | errno = 0; | |
387 | parsed_size = strtol (hdr.ar_size, NULL, 10); | |
388 | if (errno != 0) | |
389 | { | |
390 | bfd_set_error (bfd_error_malformed_archive); | |
391 | return NULL; | |
392 | } | |
393 | ||
394 | /* Extract the filename from the archive - there are two ways to | |
395 | specify an extended name table, either the first char of the | |
396 | name is a space, or it's a slash. */ | |
397 | if ((hdr.ar_name[0] == '/' | |
398 | || (hdr.ar_name[0] == ' ' | |
399 | && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL)) | |
400 | && bfd_ardata (abfd)->extended_names != NULL) | |
401 | { | |
402 | filename = get_extended_arelt_filename (abfd, hdr.ar_name); | |
403 | if (filename == NULL) | |
404 | { | |
405 | bfd_set_error (bfd_error_malformed_archive); | |
406 | return NULL; | |
407 | } | |
408 | } | |
409 | /* BSD4.4-style long filename. | |
047066e1 | 410 | Only implemented for reading, so far! */ |
252b5132 RH |
411 | else if (hdr.ar_name[0] == '#' |
412 | && hdr.ar_name[1] == '1' | |
413 | && hdr.ar_name[2] == '/' | |
3882b010 | 414 | && ISDIGIT (hdr.ar_name[3])) |
252b5132 RH |
415 | { |
416 | /* BSD-4.4 extended name */ | |
417 | namelen = atoi (&hdr.ar_name[3]); | |
418 | allocsize += namelen + 1; | |
419 | parsed_size -= namelen; | |
420 | ||
421 | allocptr = bfd_zalloc (abfd, allocsize); | |
422 | if (allocptr == NULL) | |
423 | return NULL; | |
424 | filename = (allocptr | |
425 | + sizeof (struct areltdata) | |
426 | + sizeof (struct ar_hdr)); | |
dc810e39 | 427 | if (bfd_bread (filename, namelen, abfd) != namelen) |
252b5132 RH |
428 | { |
429 | if (bfd_get_error () != bfd_error_system_call) | |
430 | bfd_set_error (bfd_error_no_more_archived_files); | |
431 | return NULL; | |
432 | } | |
433 | filename[namelen] = '\0'; | |
434 | } | |
435 | else | |
436 | { | |
437 | /* We judge the end of the name by looking for '/' or ' '. | |
438 | Note: The SYSV format (terminated by '/') allows embedded | |
d70910e8 | 439 | spaces, so only look for ' ' if we don't find '/'. */ |
252b5132 RH |
440 | |
441 | char *e; | |
d45913a0 | 442 | e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd)); |
252b5132 RH |
443 | if (e == NULL) |
444 | { | |
d45913a0 | 445 | e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)); |
252b5132 | 446 | if (e == NULL) |
d45913a0 | 447 | e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd)); |
252b5132 RH |
448 | } |
449 | ||
450 | if (e != NULL) | |
451 | namelen = e - hdr.ar_name; | |
452 | else | |
453 | { | |
454 | /* If we didn't find a termination character, then the name | |
455 | must be the entire field. */ | |
456 | namelen = ar_maxnamelen (abfd); | |
457 | } | |
458 | ||
459 | allocsize += namelen + 1; | |
460 | } | |
461 | ||
462 | if (!allocptr) | |
463 | { | |
464 | allocptr = bfd_zalloc (abfd, allocsize); | |
465 | if (allocptr == NULL) | |
466 | return NULL; | |
467 | } | |
468 | ||
469 | ared = (struct areltdata *) allocptr; | |
470 | ||
471 | ared->arch_header = allocptr + sizeof (struct areltdata); | |
472 | memcpy ((char *) ared->arch_header, (char *) &hdr, sizeof (struct ar_hdr)); | |
473 | ared->parsed_size = parsed_size; | |
474 | ||
475 | if (filename != NULL) | |
476 | ared->filename = filename; | |
477 | else | |
478 | { | |
479 | ared->filename = allocptr + (sizeof (struct areltdata) + | |
480 | sizeof (struct ar_hdr)); | |
481 | if (namelen) | |
dc810e39 | 482 | memcpy (ared->filename, hdr.ar_name, (size_t) namelen); |
252b5132 RH |
483 | ared->filename[namelen] = '\0'; |
484 | } | |
485 | ||
486 | return (PTR) ared; | |
487 | } | |
488 | \f | |
489 | /* This is an internal function; it's mainly used when indexing | |
490 | through the archive symbol table, but also used to get the next | |
491 | element, since it handles the bookkeeping so nicely for us. */ | |
492 | ||
493 | bfd * | |
494 | _bfd_get_elt_at_filepos (archive, filepos) | |
495 | bfd *archive; | |
496 | file_ptr filepos; | |
497 | { | |
498 | struct areltdata *new_areldata; | |
499 | bfd *n_nfd; | |
500 | ||
501 | n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos); | |
502 | if (n_nfd) | |
503 | return n_nfd; | |
504 | ||
505 | if (0 > bfd_seek (archive, filepos, SEEK_SET)) | |
506 | return NULL; | |
507 | ||
508 | if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL) | |
509 | return NULL; | |
510 | ||
511 | n_nfd = _bfd_create_empty_archive_element_shell (archive); | |
512 | if (n_nfd == NULL) | |
513 | { | |
514 | bfd_release (archive, (PTR) new_areldata); | |
515 | return NULL; | |
516 | } | |
517 | ||
518 | n_nfd->origin = bfd_tell (archive); | |
519 | n_nfd->arelt_data = (PTR) new_areldata; | |
520 | n_nfd->filename = new_areldata->filename; | |
521 | ||
522 | if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd)) | |
523 | return n_nfd; | |
524 | ||
047066e1 | 525 | /* Huh? */ |
252b5132 RH |
526 | bfd_release (archive, (PTR) n_nfd); |
527 | bfd_release (archive, (PTR) new_areldata); | |
528 | return NULL; | |
529 | } | |
530 | ||
531 | /* Return the BFD which is referenced by the symbol in ABFD indexed by | |
532 | INDEX. INDEX should have been returned by bfd_get_next_mapent. */ | |
533 | ||
534 | bfd * | |
535 | _bfd_generic_get_elt_at_index (abfd, index) | |
536 | bfd *abfd; | |
537 | symindex index; | |
538 | { | |
539 | carsym *entry; | |
540 | ||
541 | entry = bfd_ardata (abfd)->symdefs + index; | |
542 | return _bfd_get_elt_at_filepos (abfd, entry->file_offset); | |
543 | } | |
544 | ||
545 | /* | |
546 | FUNCTION | |
547 | bfd_openr_next_archived_file | |
548 | ||
549 | SYNOPSIS | |
550 | bfd *bfd_openr_next_archived_file(bfd *archive, bfd *previous); | |
551 | ||
552 | DESCRIPTION | |
553 | Provided a BFD, @var{archive}, containing an archive and NULL, open | |
554 | an input BFD on the first contained element and returns that. | |
555 | Subsequent calls should pass | |
556 | the archive and the previous return value to return a created | |
557 | BFD to the next contained element. NULL is returned when there | |
558 | are no more. | |
252b5132 RH |
559 | */ |
560 | ||
561 | bfd * | |
562 | bfd_openr_next_archived_file (archive, last_file) | |
563 | bfd *archive; | |
564 | bfd *last_file; | |
565 | { | |
566 | if ((bfd_get_format (archive) != bfd_archive) || | |
567 | (archive->direction == write_direction)) | |
568 | { | |
569 | bfd_set_error (bfd_error_invalid_operation); | |
570 | return NULL; | |
571 | } | |
572 | ||
573 | return BFD_SEND (archive, | |
574 | openr_next_archived_file, | |
575 | (archive, | |
576 | last_file)); | |
577 | } | |
578 | ||
579 | bfd * | |
580 | bfd_generic_openr_next_archived_file (archive, last_file) | |
581 | bfd *archive; | |
582 | bfd *last_file; | |
583 | { | |
584 | file_ptr filestart; | |
585 | ||
586 | if (!last_file) | |
587 | filestart = bfd_ardata (archive)->first_file_filepos; | |
588 | else | |
589 | { | |
590 | unsigned int size = arelt_size (last_file); | |
591 | /* Pad to an even boundary... | |
592 | Note that last_file->origin can be odd in the case of | |
d70910e8 | 593 | BSD-4.4-style element with a long odd size. */ |
252b5132 RH |
594 | filestart = last_file->origin + size; |
595 | filestart += filestart % 2; | |
596 | } | |
597 | ||
598 | return _bfd_get_elt_at_filepos (archive, filestart); | |
599 | } | |
600 | ||
252b5132 RH |
601 | const bfd_target * |
602 | bfd_generic_archive_p (abfd) | |
603 | bfd *abfd; | |
604 | { | |
605 | struct artdata *tdata_hold; | |
606 | char armag[SARMAG + 1]; | |
dc810e39 | 607 | bfd_size_type amt; |
252b5132 | 608 | |
dc810e39 | 609 | if (bfd_bread ((PTR) armag, (bfd_size_type) SARMAG, abfd) != SARMAG) |
252b5132 RH |
610 | { |
611 | if (bfd_get_error () != bfd_error_system_call) | |
612 | bfd_set_error (bfd_error_wrong_format); | |
613 | return NULL; | |
614 | } | |
615 | ||
616 | #ifdef GNU960 | |
617 | if (strncmp (armag, BFD_GNU960_ARMAG (abfd), SARMAG) != 0) | |
618 | return 0; | |
619 | #else | |
620 | if (strncmp (armag, ARMAG, SARMAG) != 0 && | |
621 | strncmp (armag, ARMAGB, SARMAG) != 0) | |
622 | return 0; | |
623 | #endif | |
624 | ||
487e54f2 | 625 | tdata_hold = bfd_ardata (abfd); |
252b5132 | 626 | |
487e54f2 AM |
627 | amt = sizeof (struct artdata); |
628 | bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt); | |
252b5132 | 629 | if (bfd_ardata (abfd) == NULL) |
487e54f2 AM |
630 | { |
631 | bfd_ardata (abfd) = tdata_hold; | |
632 | return NULL; | |
633 | } | |
252b5132 RH |
634 | |
635 | bfd_ardata (abfd)->first_file_filepos = SARMAG; | |
636 | bfd_ardata (abfd)->cache = NULL; | |
637 | bfd_ardata (abfd)->archive_head = NULL; | |
638 | bfd_ardata (abfd)->symdefs = NULL; | |
639 | bfd_ardata (abfd)->extended_names = NULL; | |
640 | bfd_ardata (abfd)->tdata = NULL; | |
641 | ||
487e54f2 AM |
642 | if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd)) |
643 | || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) | |
252b5132 | 644 | { |
252b5132 RH |
645 | if (bfd_get_error () != bfd_error_system_call) |
646 | bfd_set_error (bfd_error_wrong_format); | |
252b5132 | 647 | bfd_release (abfd, bfd_ardata (abfd)); |
487e54f2 | 648 | bfd_ardata (abfd) = tdata_hold; |
252b5132 RH |
649 | return NULL; |
650 | } | |
651 | ||
652 | if (bfd_has_map (abfd)) | |
653 | { | |
654 | bfd *first; | |
655 | ||
656 | /* This archive has a map, so we may presume that the contents | |
657 | are object files. Make sure that if the first file in the | |
658 | archive can be recognized as an object file, it is for this | |
659 | target. If not, assume that this is the wrong format. If | |
660 | the first file is not an object file, somebody is doing | |
661 | something weird, and we permit it so that ar -t will work. | |
662 | ||
663 | This is done because any normal format will recognize any | |
664 | normal archive, regardless of the format of the object files. | |
665 | We do accept an empty archive. */ | |
666 | ||
667 | first = bfd_openr_next_archived_file (abfd, (bfd *) NULL); | |
668 | if (first != NULL) | |
669 | { | |
b34976b6 | 670 | bfd_boolean fail; |
252b5132 | 671 | |
b34976b6 AM |
672 | first->target_defaulted = FALSE; |
673 | fail = FALSE; | |
252b5132 RH |
674 | if (bfd_check_format (first, bfd_object) |
675 | && first->xvec != abfd->xvec) | |
676 | { | |
3619ad04 AM |
677 | #if 0 |
678 | /* We ought to close `first' here, but we can't, because | |
679 | we have no way to remove it from the archive cache. | |
680 | It's close to impossible to figure out when we can | |
681 | release bfd_ardata. FIXME. */ | |
252b5132 RH |
682 | (void) bfd_close (first); |
683 | bfd_release (abfd, bfd_ardata (abfd)); | |
3619ad04 AM |
684 | #endif |
685 | bfd_set_error (bfd_error_wrong_object_format); | |
487e54f2 | 686 | bfd_ardata (abfd) = tdata_hold; |
252b5132 RH |
687 | return NULL; |
688 | } | |
3619ad04 | 689 | /* And we ought to close `first' here too. */ |
252b5132 RH |
690 | } |
691 | } | |
692 | ||
693 | return abfd->xvec; | |
694 | } | |
695 | ||
696 | /* Some constants for a 32 bit BSD archive structure. We do not | |
697 | support 64 bit archives presently; so far as I know, none actually | |
698 | exist. Supporting them would require changing these constants, and | |
dc810e39 | 699 | changing some H_GET_32 to H_GET_64. */ |
252b5132 RH |
700 | |
701 | /* The size of an external symdef structure. */ | |
702 | #define BSD_SYMDEF_SIZE 8 | |
703 | ||
704 | /* The offset from the start of a symdef structure to the file offset. */ | |
705 | #define BSD_SYMDEF_OFFSET_SIZE 4 | |
706 | ||
707 | /* The size of the symdef count. */ | |
708 | #define BSD_SYMDEF_COUNT_SIZE 4 | |
709 | ||
710 | /* The size of the string count. */ | |
711 | #define BSD_STRING_COUNT_SIZE 4 | |
712 | ||
b34976b6 | 713 | /* Returns FALSE on error, TRUE otherwise */ |
252b5132 | 714 | |
b34976b6 | 715 | static bfd_boolean |
252b5132 RH |
716 | do_slurp_bsd_armap (abfd) |
717 | bfd *abfd; | |
718 | { | |
719 | struct areltdata *mapdata; | |
720 | unsigned int counter; | |
721 | bfd_byte *raw_armap, *rbase; | |
722 | struct artdata *ardata = bfd_ardata (abfd); | |
723 | char *stringbase; | |
dc810e39 | 724 | bfd_size_type parsed_size, amt; |
252b5132 RH |
725 | carsym *set; |
726 | ||
727 | mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); | |
728 | if (mapdata == NULL) | |
b34976b6 | 729 | return FALSE; |
252b5132 | 730 | parsed_size = mapdata->parsed_size; |
d70910e8 | 731 | bfd_release (abfd, (PTR) mapdata); /* Don't need it any more. */ |
252b5132 RH |
732 | |
733 | raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size); | |
734 | if (raw_armap == (bfd_byte *) NULL) | |
b34976b6 | 735 | return FALSE; |
252b5132 | 736 | |
dc810e39 | 737 | if (bfd_bread ((PTR) raw_armap, parsed_size, abfd) != parsed_size) |
252b5132 RH |
738 | { |
739 | if (bfd_get_error () != bfd_error_system_call) | |
740 | bfd_set_error (bfd_error_malformed_archive); | |
741 | byebye: | |
742 | bfd_release (abfd, (PTR) raw_armap); | |
b34976b6 | 743 | return FALSE; |
252b5132 RH |
744 | } |
745 | ||
dc810e39 | 746 | ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE; |
252b5132 RH |
747 | |
748 | if (ardata->symdef_count * BSD_SYMDEF_SIZE > | |
749 | parsed_size - BSD_SYMDEF_COUNT_SIZE) | |
750 | { | |
751 | /* Probably we're using the wrong byte ordering. */ | |
752 | bfd_set_error (bfd_error_wrong_format); | |
753 | goto byebye; | |
754 | } | |
755 | ||
756 | ardata->cache = 0; | |
757 | rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; | |
758 | stringbase = ((char *) rbase | |
759 | + ardata->symdef_count * BSD_SYMDEF_SIZE | |
760 | + BSD_STRING_COUNT_SIZE); | |
dc810e39 AM |
761 | amt = (bfd_size_type) ardata->symdef_count * sizeof (carsym); |
762 | ardata->symdefs = (carsym *) bfd_alloc (abfd, amt); | |
252b5132 | 763 | if (!ardata->symdefs) |
b34976b6 | 764 | return FALSE; |
252b5132 RH |
765 | |
766 | for (counter = 0, set = ardata->symdefs; | |
767 | counter < ardata->symdef_count; | |
768 | counter++, set++, rbase += BSD_SYMDEF_SIZE) | |
769 | { | |
dc810e39 AM |
770 | set->name = H_GET_32 (abfd, rbase) + stringbase; |
771 | set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); | |
252b5132 RH |
772 | } |
773 | ||
774 | ardata->first_file_filepos = bfd_tell (abfd); | |
047066e1 | 775 | /* Pad to an even boundary if you have to. */ |
252b5132 RH |
776 | ardata->first_file_filepos += (ardata->first_file_filepos) % 2; |
777 | /* FIXME, we should provide some way to free raw_ardata when | |
778 | we are done using the strings from it. For now, it seems | |
d70910e8 | 779 | to be allocated on an objalloc anyway... */ |
b34976b6 AM |
780 | bfd_has_map (abfd) = TRUE; |
781 | return TRUE; | |
252b5132 RH |
782 | } |
783 | ||
b34976b6 | 784 | /* Returns FALSE on error, TRUE otherwise. */ |
047066e1 | 785 | |
b34976b6 | 786 | static bfd_boolean |
252b5132 RH |
787 | do_slurp_coff_armap (abfd) |
788 | bfd *abfd; | |
789 | { | |
790 | struct areltdata *mapdata; | |
791 | int *raw_armap, *rawptr; | |
792 | struct artdata *ardata = bfd_ardata (abfd); | |
793 | char *stringbase; | |
dc810e39 | 794 | bfd_size_type stringsize; |
252b5132 RH |
795 | unsigned int parsed_size; |
796 | carsym *carsyms; | |
dc810e39 | 797 | bfd_size_type nsymz; /* Number of symbols in armap. */ |
252b5132 RH |
798 | bfd_vma (*swap) PARAMS ((const bfd_byte *)); |
799 | char int_buf[sizeof (long)]; | |
dc810e39 AM |
800 | bfd_size_type carsym_size, ptrsize; |
801 | unsigned int i; | |
252b5132 RH |
802 | |
803 | mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); | |
804 | if (mapdata == NULL) | |
b34976b6 | 805 | return FALSE; |
252b5132 | 806 | parsed_size = mapdata->parsed_size; |
d70910e8 | 807 | bfd_release (abfd, (PTR) mapdata); /* Don't need it any more. */ |
252b5132 | 808 | |
dc810e39 | 809 | if (bfd_bread ((PTR) int_buf, (bfd_size_type) 4, abfd) != 4) |
252b5132 RH |
810 | { |
811 | if (bfd_get_error () != bfd_error_system_call) | |
812 | bfd_set_error (bfd_error_malformed_archive); | |
b34976b6 | 813 | return FALSE; |
252b5132 RH |
814 | } |
815 | /* It seems that all numeric information in a coff archive is always | |
d70910e8 | 816 | in big endian format, nomatter the host or target. */ |
252b5132 RH |
817 | swap = bfd_getb32; |
818 | nsymz = bfd_getb32 ((PTR) int_buf); | |
819 | stringsize = parsed_size - (4 * nsymz) - 4; | |
820 | ||
821 | #if 1 | |
822 | /* ... except that some archive formats are broken, and it may be our | |
823 | fault - the i960 little endian coff sometimes has big and sometimes | |
824 | little, because our tools changed. Here's a horrible hack to clean | |
825 | up the crap. */ | |
826 | ||
827 | if (stringsize > 0xfffff | |
828 | && bfd_get_arch (abfd) == bfd_arch_i960 | |
829 | && bfd_get_flavour (abfd) == bfd_target_coff_flavour) | |
830 | { | |
047066e1 | 831 | /* This looks dangerous, let's do it the other way around. */ |
252b5132 RH |
832 | nsymz = bfd_getl32 ((PTR) int_buf); |
833 | stringsize = parsed_size - (4 * nsymz) - 4; | |
834 | swap = bfd_getl32; | |
835 | } | |
836 | #endif | |
837 | ||
838 | /* The coff armap must be read sequentially. So we construct a | |
d70910e8 | 839 | bsd-style one in core all at once, for simplicity. */ |
252b5132 RH |
840 | |
841 | carsym_size = (nsymz * sizeof (carsym)); | |
842 | ptrsize = (4 * nsymz); | |
843 | ||
844 | ardata->symdefs = (carsym *) bfd_zalloc (abfd, carsym_size + stringsize + 1); | |
845 | if (ardata->symdefs == NULL) | |
b34976b6 | 846 | return FALSE; |
252b5132 RH |
847 | carsyms = ardata->symdefs; |
848 | stringbase = ((char *) ardata->symdefs) + carsym_size; | |
849 | ||
d70910e8 | 850 | /* Allocate and read in the raw offsets. */ |
252b5132 RH |
851 | raw_armap = (int *) bfd_alloc (abfd, ptrsize); |
852 | if (raw_armap == NULL) | |
853 | goto release_symdefs; | |
dc810e39 AM |
854 | if (bfd_bread ((PTR) raw_armap, ptrsize, abfd) != ptrsize |
855 | || (bfd_bread ((PTR) stringbase, stringsize, abfd) != stringsize)) | |
252b5132 RH |
856 | { |
857 | if (bfd_get_error () != bfd_error_system_call) | |
858 | bfd_set_error (bfd_error_malformed_archive); | |
859 | goto release_raw_armap; | |
860 | } | |
861 | ||
047066e1 | 862 | /* OK, build the carsyms. */ |
252b5132 RH |
863 | for (i = 0; i < nsymz; i++) |
864 | { | |
865 | rawptr = raw_armap + i; | |
866 | carsyms->file_offset = swap ((PTR) rawptr); | |
867 | carsyms->name = stringbase; | |
868 | stringbase += strlen (stringbase) + 1; | |
869 | carsyms++; | |
870 | } | |
871 | *stringbase = 0; | |
872 | ||
873 | ardata->symdef_count = nsymz; | |
874 | ardata->first_file_filepos = bfd_tell (abfd); | |
047066e1 | 875 | /* Pad to an even boundary if you have to. */ |
252b5132 RH |
876 | ardata->first_file_filepos += (ardata->first_file_filepos) % 2; |
877 | ||
b34976b6 | 878 | bfd_has_map (abfd) = TRUE; |
252b5132 RH |
879 | bfd_release (abfd, (PTR) raw_armap); |
880 | ||
047066e1 | 881 | /* Check for a second archive header (as used by PE). */ |
252b5132 RH |
882 | { |
883 | struct areltdata *tmp; | |
884 | ||
dc810e39 | 885 | bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET); |
252b5132 | 886 | tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd); |
23afc6f6 | 887 | if (tmp != NULL) |
252b5132 RH |
888 | { |
889 | if (tmp->arch_header[0] == '/' | |
23afc6f6 | 890 | && tmp->arch_header[1] == ' ') |
252b5132 RH |
891 | { |
892 | ardata->first_file_filepos += | |
dc810e39 | 893 | (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1; |
252b5132 RH |
894 | } |
895 | bfd_release (abfd, tmp); | |
896 | } | |
897 | } | |
898 | ||
b34976b6 | 899 | return TRUE; |
252b5132 RH |
900 | |
901 | release_raw_armap: | |
902 | bfd_release (abfd, (PTR) raw_armap); | |
903 | release_symdefs: | |
904 | bfd_release (abfd, (PTR) (ardata)->symdefs); | |
b34976b6 | 905 | return FALSE; |
252b5132 RH |
906 | } |
907 | ||
908 | /* This routine can handle either coff-style or bsd-style armaps. | |
b34976b6 | 909 | Returns FALSE on error, TRUE otherwise */ |
252b5132 | 910 | |
b34976b6 | 911 | bfd_boolean |
252b5132 RH |
912 | bfd_slurp_armap (abfd) |
913 | bfd *abfd; | |
914 | { | |
915 | char nextname[17]; | |
dc810e39 | 916 | int i = bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd); |
252b5132 RH |
917 | |
918 | if (i == 0) | |
b34976b6 | 919 | return TRUE; |
252b5132 | 920 | if (i != 16) |
b34976b6 | 921 | return FALSE; |
252b5132 | 922 | |
dc810e39 | 923 | if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) |
b34976b6 | 924 | return FALSE; |
252b5132 RH |
925 | |
926 | if (!strncmp (nextname, "__.SYMDEF ", 16) | |
927 | || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */ | |
928 | return do_slurp_bsd_armap (abfd); | |
929 | else if (!strncmp (nextname, "/ ", 16)) | |
930 | return do_slurp_coff_armap (abfd); | |
931 | else if (!strncmp (nextname, "/SYM64/ ", 16)) | |
932 | { | |
36b45482 TS |
933 | /* 64bit ELF (Irix 6) archive. */ |
934 | #ifdef BFD64 | |
935 | return bfd_elf64_archive_slurp_armap (abfd); | |
936 | #else | |
252b5132 | 937 | bfd_set_error (bfd_error_wrong_format); |
b34976b6 | 938 | return FALSE; |
36b45482 | 939 | #endif |
252b5132 RH |
940 | } |
941 | ||
b34976b6 AM |
942 | bfd_has_map (abfd) = FALSE; |
943 | return TRUE; | |
252b5132 RH |
944 | } |
945 | \f | |
b34976b6 | 946 | /* Returns FALSE on error, TRUE otherwise */ |
252b5132 RH |
947 | /* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the |
948 | header is in a slightly different order and the map name is '/'. | |
d70910e8 | 949 | This flavour is used by hp300hpux. */ |
252b5132 RH |
950 | |
951 | #define HPUX_SYMDEF_COUNT_SIZE 2 | |
952 | ||
b34976b6 | 953 | bfd_boolean |
252b5132 RH |
954 | bfd_slurp_bsd_armap_f2 (abfd) |
955 | bfd *abfd; | |
956 | { | |
957 | struct areltdata *mapdata; | |
958 | char nextname[17]; | |
959 | unsigned int counter; | |
960 | bfd_byte *raw_armap, *rbase; | |
961 | struct artdata *ardata = bfd_ardata (abfd); | |
962 | char *stringbase; | |
963 | unsigned int stringsize; | |
dc810e39 | 964 | bfd_size_type amt; |
252b5132 | 965 | carsym *set; |
dc810e39 | 966 | int i = bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd); |
252b5132 RH |
967 | |
968 | if (i == 0) | |
b34976b6 | 969 | return TRUE; |
252b5132 | 970 | if (i != 16) |
b34976b6 | 971 | return FALSE; |
252b5132 | 972 | |
047066e1 | 973 | /* The archive has at least 16 bytes in it. */ |
dc810e39 | 974 | if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) |
b34976b6 | 975 | return FALSE; |
252b5132 RH |
976 | |
977 | if (!strncmp (nextname, "__.SYMDEF ", 16) | |
978 | || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */ | |
979 | return do_slurp_bsd_armap (abfd); | |
980 | ||
981 | if (strncmp (nextname, "/ ", 16)) | |
982 | { | |
b34976b6 AM |
983 | bfd_has_map (abfd) = FALSE; |
984 | return TRUE; | |
252b5132 RH |
985 | } |
986 | ||
987 | mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); | |
988 | if (mapdata == NULL) | |
b34976b6 | 989 | return FALSE; |
252b5132 | 990 | |
dc810e39 AM |
991 | amt = mapdata->parsed_size; |
992 | raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
993 | if (raw_armap == NULL) |
994 | { | |
995 | byebye: | |
996 | bfd_release (abfd, (PTR) mapdata); | |
b34976b6 | 997 | return FALSE; |
252b5132 RH |
998 | } |
999 | ||
dc810e39 | 1000 | if (bfd_bread ((PTR) raw_armap, amt, abfd) != amt) |
252b5132 RH |
1001 | { |
1002 | if (bfd_get_error () != bfd_error_system_call) | |
1003 | bfd_set_error (bfd_error_malformed_archive); | |
1004 | byebyebye: | |
1005 | bfd_release (abfd, (PTR) raw_armap); | |
1006 | goto byebye; | |
1007 | } | |
1008 | ||
dc810e39 | 1009 | ardata->symdef_count = H_GET_16 (abfd, (PTR) raw_armap); |
252b5132 RH |
1010 | |
1011 | if (ardata->symdef_count * BSD_SYMDEF_SIZE | |
1012 | > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE) | |
1013 | { | |
1014 | /* Probably we're using the wrong byte ordering. */ | |
1015 | bfd_set_error (bfd_error_wrong_format); | |
1016 | goto byebyebye; | |
1017 | } | |
1018 | ||
1019 | ardata->cache = 0; | |
1020 | ||
dc810e39 | 1021 | stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE); |
047066e1 | 1022 | /* Skip sym count and string sz. */ |
252b5132 RH |
1023 | stringbase = ((char *) raw_armap |
1024 | + HPUX_SYMDEF_COUNT_SIZE | |
1025 | + BSD_STRING_COUNT_SIZE); | |
1026 | rbase = (bfd_byte *) stringbase + stringsize; | |
dc810e39 AM |
1027 | amt = (bfd_size_type) ardata->symdef_count * BSD_SYMDEF_SIZE; |
1028 | ardata->symdefs = (carsym *) bfd_alloc (abfd, amt); | |
252b5132 | 1029 | if (!ardata->symdefs) |
b34976b6 | 1030 | return FALSE; |
252b5132 RH |
1031 | |
1032 | for (counter = 0, set = ardata->symdefs; | |
1033 | counter < ardata->symdef_count; | |
1034 | counter++, set++, rbase += BSD_SYMDEF_SIZE) | |
1035 | { | |
dc810e39 AM |
1036 | set->name = H_GET_32 (abfd, rbase) + stringbase; |
1037 | set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); | |
252b5132 RH |
1038 | } |
1039 | ||
1040 | ardata->first_file_filepos = bfd_tell (abfd); | |
047066e1 | 1041 | /* Pad to an even boundary if you have to. */ |
252b5132 RH |
1042 | ardata->first_file_filepos += (ardata->first_file_filepos) % 2; |
1043 | /* FIXME, we should provide some way to free raw_ardata when | |
1044 | we are done using the strings from it. For now, it seems | |
d70910e8 | 1045 | to be allocated on an objalloc anyway... */ |
b34976b6 AM |
1046 | bfd_has_map (abfd) = TRUE; |
1047 | return TRUE; | |
252b5132 RH |
1048 | } |
1049 | \f | |
1050 | /** Extended name table. | |
1051 | ||
1052 | Normally archives support only 14-character filenames. | |
1053 | ||
1054 | Intel has extended the format: longer names are stored in a special | |
1055 | element (the first in the archive, or second if there is an armap); | |
1056 | the name in the ar_hdr is replaced by <space><index into filename | |
1057 | element>. Index is the P.R. of an int (decimal). Data General have | |
047066e1 KH |
1058 | extended the format by using the prefix // for the special element. */ |
1059 | ||
b34976b6 | 1060 | /* Returns FALSE on error, TRUE otherwise. */ |
252b5132 | 1061 | |
b34976b6 | 1062 | bfd_boolean |
252b5132 RH |
1063 | _bfd_slurp_extended_name_table (abfd) |
1064 | bfd *abfd; | |
1065 | { | |
1066 | char nextname[17]; | |
1067 | struct areltdata *namedata; | |
dc810e39 | 1068 | bfd_size_type amt; |
252b5132 RH |
1069 | |
1070 | /* FIXME: Formatting sucks here, and in case of failure of BFD_READ, | |
b34976b6 | 1071 | we probably don't want to return TRUE. */ |
252b5132 | 1072 | bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET); |
dc810e39 | 1073 | if (bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd) == 16) |
252b5132 | 1074 | { |
dc810e39 | 1075 | if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) |
b34976b6 | 1076 | return FALSE; |
252b5132 RH |
1077 | |
1078 | if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 && | |
1079 | strncmp (nextname, "// ", 16) != 0) | |
1080 | { | |
1081 | bfd_ardata (abfd)->extended_names = NULL; | |
b34976b6 | 1082 | return TRUE; |
252b5132 RH |
1083 | } |
1084 | ||
1085 | namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd); | |
1086 | if (namedata == NULL) | |
b34976b6 | 1087 | return FALSE; |
252b5132 | 1088 | |
dc810e39 AM |
1089 | amt = namedata->parsed_size; |
1090 | bfd_ardata (abfd)->extended_names = bfd_zalloc (abfd, amt); | |
252b5132 RH |
1091 | if (bfd_ardata (abfd)->extended_names == NULL) |
1092 | { | |
1093 | byebye: | |
1094 | bfd_release (abfd, (PTR) namedata); | |
b34976b6 | 1095 | return FALSE; |
252b5132 RH |
1096 | } |
1097 | ||
dc810e39 | 1098 | if (bfd_bread ((PTR) bfd_ardata (abfd)->extended_names, amt, abfd) != amt) |
252b5132 RH |
1099 | { |
1100 | if (bfd_get_error () != bfd_error_system_call) | |
1101 | bfd_set_error (bfd_error_malformed_archive); | |
1102 | bfd_release (abfd, (PTR) (bfd_ardata (abfd)->extended_names)); | |
1103 | bfd_ardata (abfd)->extended_names = NULL; | |
1104 | goto byebye; | |
1105 | } | |
1106 | ||
1107 | /* Since the archive is supposed to be printable if it contains | |
1108 | text, the entries in the list are newline-padded, not null | |
1109 | padded. In SVR4-style archives, the names also have a | |
1110 | trailing '/'. DOS/NT created archive often have \ in them | |
1111 | We'll fix all problems here.. */ | |
1112 | { | |
1113 | char *temp = bfd_ardata (abfd)->extended_names; | |
1114 | char *limit = temp + namedata->parsed_size; | |
047066e1 KH |
1115 | for (; temp < limit; ++temp) |
1116 | { | |
1117 | if (*temp == '\012') | |
1118 | temp[temp[-1] == '/' ? -1 : 0] = '\0'; | |
1119 | if (*temp == '\\') | |
1120 | *temp = '/'; | |
1121 | } | |
252b5132 RH |
1122 | } |
1123 | ||
047066e1 | 1124 | /* Pad to an even boundary if you have to. */ |
252b5132 RH |
1125 | bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd); |
1126 | bfd_ardata (abfd)->first_file_filepos += | |
1127 | (bfd_ardata (abfd)->first_file_filepos) % 2; | |
1128 | ||
1129 | /* FIXME, we can't release namedata here because it was allocated | |
d70910e8 | 1130 | below extended_names on the objalloc... */ |
047066e1 KH |
1131 | #if 0 |
1132 | bfd_release (abfd, namedata); | |
1133 | #endif | |
252b5132 | 1134 | } |
b34976b6 | 1135 | return TRUE; |
252b5132 RH |
1136 | } |
1137 | ||
1138 | #ifdef VMS | |
1139 | ||
1140 | /* Return a copy of the stuff in the filename between any :]> and a | |
047066e1 KH |
1141 | semicolon. */ |
1142 | ||
252b5132 RH |
1143 | static const char * |
1144 | normalize (abfd, file) | |
1145 | bfd *abfd; | |
1146 | const char *file; | |
1147 | { | |
dc810e39 AM |
1148 | const char *first; |
1149 | const char *last; | |
252b5132 RH |
1150 | char *copy; |
1151 | ||
1152 | first = file + strlen (file) - 1; | |
1153 | last = first + 1; | |
1154 | ||
1155 | while (first != file) | |
1156 | { | |
1157 | if (*first == ';') | |
1158 | last = first; | |
1159 | if (*first == ':' || *first == ']' || *first == '>') | |
1160 | { | |
1161 | first++; | |
1162 | break; | |
1163 | } | |
1164 | first--; | |
1165 | } | |
1166 | ||
dc810e39 | 1167 | copy = (char *) bfd_alloc (abfd, (bfd_size_type) (last - first + 1)); |
252b5132 RH |
1168 | if (copy == NULL) |
1169 | return NULL; | |
1170 | ||
1171 | memcpy (copy, first, last - first); | |
1172 | copy[last - first] = 0; | |
1173 | ||
1174 | return copy; | |
1175 | } | |
1176 | ||
1177 | #else | |
1178 | static const char * | |
1179 | normalize (abfd, file) | |
7442e600 | 1180 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
1181 | const char *file; |
1182 | { | |
1183 | const char *filename = strrchr (file, '/'); | |
1184 | ||
5af11cab AM |
1185 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
1186 | { | |
1187 | /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ | |
1188 | char *bslash = strrchr (file, '\\'); | |
2ab47eed | 1189 | if (filename == NULL || (bslash != NULL && bslash > filename)) |
5af11cab AM |
1190 | filename = bslash; |
1191 | if (filename == NULL && file[0] != '\0' && file[1] == ':') | |
1192 | filename = file + 1; | |
1193 | } | |
1194 | #endif | |
252b5132 RH |
1195 | if (filename != (char *) NULL) |
1196 | filename++; | |
1197 | else | |
1198 | filename = file; | |
1199 | return filename; | |
1200 | } | |
1201 | #endif | |
1202 | ||
1203 | /* Build a BFD style extended name table. */ | |
1204 | ||
b34976b6 | 1205 | bfd_boolean |
252b5132 RH |
1206 | _bfd_archive_bsd_construct_extended_name_table (abfd, tabloc, tablen, name) |
1207 | bfd *abfd; | |
1208 | char **tabloc; | |
1209 | bfd_size_type *tablen; | |
1210 | const char **name; | |
1211 | { | |
1212 | *name = "ARFILENAMES/"; | |
b34976b6 | 1213 | return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen); |
252b5132 RH |
1214 | } |
1215 | ||
1216 | /* Build an SVR4 style extended name table. */ | |
1217 | ||
b34976b6 | 1218 | bfd_boolean |
252b5132 RH |
1219 | _bfd_archive_coff_construct_extended_name_table (abfd, tabloc, tablen, name) |
1220 | bfd *abfd; | |
1221 | char **tabloc; | |
1222 | bfd_size_type *tablen; | |
1223 | const char **name; | |
1224 | { | |
1225 | *name = "//"; | |
b34976b6 | 1226 | return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen); |
252b5132 RH |
1227 | } |
1228 | ||
1229 | /* Follows archive_head and produces an extended name table if | |
1230 | necessary. Returns (in tabloc) a pointer to an extended name | |
1231 | table, and in tablen the length of the table. If it makes an entry | |
1232 | it clobbers the filename so that the element may be written without | |
b34976b6 | 1233 | further massage. Returns TRUE if it ran successfully, FALSE if |
252b5132 RH |
1234 | something went wrong. A successful return may still involve a |
1235 | zero-length tablen! */ | |
1236 | ||
b34976b6 | 1237 | bfd_boolean |
252b5132 RH |
1238 | _bfd_construct_extended_name_table (abfd, trailing_slash, tabloc, tablen) |
1239 | bfd *abfd; | |
b34976b6 | 1240 | bfd_boolean trailing_slash; |
252b5132 RH |
1241 | char **tabloc; |
1242 | bfd_size_type *tablen; | |
1243 | { | |
1244 | unsigned int maxname = abfd->xvec->ar_max_namelen; | |
dc810e39 | 1245 | bfd_size_type total_namelen = 0; |
252b5132 RH |
1246 | bfd *current; |
1247 | char *strptr; | |
1248 | ||
1249 | *tablen = 0; | |
1250 | ||
047066e1 | 1251 | /* Figure out how long the table should be. */ |
252b5132 RH |
1252 | for (current = abfd->archive_head; current != NULL; current = current->next) |
1253 | { | |
1254 | const char *normal; | |
1255 | unsigned int thislen; | |
1256 | ||
1257 | normal = normalize (current, current->filename); | |
1258 | if (normal == NULL) | |
b34976b6 | 1259 | return FALSE; |
252b5132 RH |
1260 | |
1261 | thislen = strlen (normal); | |
1262 | ||
1263 | if (thislen > maxname | |
1264 | && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) | |
1265 | thislen = maxname; | |
1266 | ||
1267 | if (thislen > maxname) | |
1268 | { | |
1269 | /* Add one to leave room for \n. */ | |
1270 | total_namelen += thislen + 1; | |
1271 | if (trailing_slash) | |
1272 | { | |
1273 | /* Leave room for trailing slash. */ | |
1274 | ++total_namelen; | |
1275 | } | |
1276 | } | |
1277 | else | |
1278 | { | |
1279 | struct ar_hdr *hdr = arch_hdr (current); | |
1280 | if (strncmp (normal, hdr->ar_name, thislen) != 0 | |
1281 | || (thislen < sizeof hdr->ar_name | |
1282 | && hdr->ar_name[thislen] != ar_padchar (current))) | |
1283 | { | |
1284 | /* Must have been using extended format even though it | |
1285 | didn't need to. Fix it to use normal format. */ | |
1286 | memcpy (hdr->ar_name, normal, thislen); | |
1287 | if (thislen < maxname | |
1288 | || (thislen == maxname && thislen < sizeof hdr->ar_name)) | |
1289 | hdr->ar_name[thislen] = ar_padchar (current); | |
1290 | } | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | if (total_namelen == 0) | |
b34976b6 | 1295 | return TRUE; |
252b5132 RH |
1296 | |
1297 | *tabloc = bfd_zalloc (abfd, total_namelen); | |
1298 | if (*tabloc == NULL) | |
b34976b6 | 1299 | return FALSE; |
252b5132 RH |
1300 | |
1301 | *tablen = total_namelen; | |
1302 | strptr = *tabloc; | |
1303 | ||
1304 | for (current = abfd->archive_head; current != NULL; current = | |
1305 | current->next) | |
1306 | { | |
1307 | const char *normal; | |
1308 | unsigned int thislen; | |
1309 | ||
1310 | normal = normalize (current, current->filename); | |
1311 | if (normal == NULL) | |
b34976b6 | 1312 | return FALSE; |
252b5132 RH |
1313 | |
1314 | thislen = strlen (normal); | |
1315 | if (thislen > maxname) | |
1316 | { | |
1317 | /* Works for now; may need to be re-engineered if we | |
1318 | encounter an oddball archive format and want to | |
d70910e8 | 1319 | generalise this hack. */ |
252b5132 RH |
1320 | struct ar_hdr *hdr = arch_hdr (current); |
1321 | strcpy (strptr, normal); | |
1322 | if (! trailing_slash) | |
1323 | strptr[thislen] = '\012'; | |
1324 | else | |
1325 | { | |
1326 | strptr[thislen] = '/'; | |
1327 | strptr[thislen + 1] = '\012'; | |
1328 | } | |
1329 | hdr->ar_name[0] = ar_padchar (current); | |
1330 | /* We know there will always be enough room (one of the few | |
d70910e8 | 1331 | cases where you may safely use sprintf). */ |
252b5132 RH |
1332 | sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc)); |
1333 | /* Kinda Kludgy. We should just use the returned value of | |
047066e1 | 1334 | sprintf but not all implementations get this right. */ |
252b5132 RH |
1335 | { |
1336 | char *temp = hdr->ar_name + 2; | |
1337 | for (; temp < hdr->ar_name + maxname; temp++) | |
1338 | if (*temp == '\0') | |
1339 | *temp = ' '; | |
1340 | } | |
1341 | strptr += thislen + 1; | |
1342 | if (trailing_slash) | |
1343 | ++strptr; | |
1344 | } | |
1345 | } | |
1346 | ||
b34976b6 | 1347 | return TRUE; |
252b5132 RH |
1348 | } |
1349 | \f | |
1350 | /** A couple of functions for creating ar_hdrs */ | |
1351 | ||
23afc6f6 JL |
1352 | #ifdef HPUX_LARGE_AR_IDS |
1353 | /* Function to encode large UID/GID values according to HP. */ | |
047066e1 | 1354 | |
23afc6f6 JL |
1355 | static void |
1356 | hpux_uid_gid_encode (str, id) | |
1357 | char str[6]; | |
1358 | long int id; | |
1359 | { | |
1360 | int cnt; | |
1361 | ||
1362 | str[5] = '@' + (id & 3); | |
1363 | id >>= 2; | |
1364 | ||
1365 | for (cnt = 4; cnt >= 0; ++cnt, id >>= 6) | |
1366 | str[cnt] = ' ' + (id & 0x3f); | |
1367 | } | |
1368 | #endif /* HPUX_LARGE_AR_IDS */ | |
1369 | ||
633fd09f ILT |
1370 | #ifndef HAVE_GETUID |
1371 | #define getuid() 0 | |
1372 | #endif | |
1373 | ||
1374 | #ifndef HAVE_GETGID | |
1375 | #define getgid() 0 | |
1376 | #endif | |
1377 | ||
252b5132 RH |
1378 | /* Takes a filename, returns an arelt_data for it, or NULL if it can't |
1379 | make one. The filename must refer to a filename in the filesystem. | |
1380 | The filename field of the ar_hdr will NOT be initialized. If member | |
d70910e8 | 1381 | is set, and it's an in-memory bfd, we fake it. */ |
252b5132 RH |
1382 | |
1383 | static struct areltdata * | |
1384 | bfd_ar_hdr_from_filesystem (abfd, filename, member) | |
1385 | bfd *abfd; | |
1386 | const char *filename; | |
1387 | bfd *member; | |
1388 | { | |
1389 | struct stat status; | |
1390 | struct areltdata *ared; | |
1391 | struct ar_hdr *hdr; | |
1392 | char *temp, *temp1; | |
dc810e39 | 1393 | bfd_size_type amt; |
252b5132 RH |
1394 | |
1395 | if (member && (member->flags & BFD_IN_MEMORY) != 0) | |
1396 | { | |
047066e1 | 1397 | /* Assume we just "made" the member, and fake it. */ |
252b5132 | 1398 | struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream; |
047066e1 KH |
1399 | time (&status.st_mtime); |
1400 | status.st_uid = getuid (); | |
1401 | status.st_gid = getgid (); | |
252b5132 RH |
1402 | status.st_mode = 0644; |
1403 | status.st_size = bim->size; | |
1404 | } | |
1405 | else if (stat (filename, &status) != 0) | |
1406 | { | |
1407 | bfd_set_error (bfd_error_system_call); | |
1408 | return NULL; | |
1409 | } | |
1410 | ||
dc810e39 AM |
1411 | amt = sizeof (struct ar_hdr) + sizeof (struct areltdata); |
1412 | ared = (struct areltdata *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
1413 | if (ared == NULL) |
1414 | return NULL; | |
1415 | hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata)); | |
1416 | ||
047066e1 | 1417 | /* ar headers are space padded, not null padded! */ |
252b5132 RH |
1418 | memset ((PTR) hdr, ' ', sizeof (struct ar_hdr)); |
1419 | ||
1420 | strncpy (hdr->ar_fmag, ARFMAG, 2); | |
1421 | ||
047066e1 | 1422 | /* Goddamned sprintf doesn't permit MAXIMUM field lengths. */ |
252b5132 | 1423 | sprintf ((hdr->ar_date), "%-12ld", (long) status.st_mtime); |
23afc6f6 JL |
1424 | #ifdef HPUX_LARGE_AR_IDS |
1425 | /* HP has a very "special" way to handle UID/GID's with numeric values | |
1426 | > 99999. */ | |
1427 | if (status.st_uid > 99999) | |
1428 | hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_uid); | |
1429 | else | |
1430 | #endif | |
1431 | sprintf ((hdr->ar_uid), "%ld", (long) status.st_uid); | |
1432 | #ifdef HPUX_LARGE_AR_IDS | |
1433 | /* HP has a very "special" way to handle UID/GID's with numeric values | |
1434 | > 99999. */ | |
1435 | if (status.st_gid > 99999) | |
1436 | hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_gid); | |
1437 | else | |
1438 | #endif | |
252b5132 RH |
1439 | sprintf ((hdr->ar_gid), "%ld", (long) status.st_gid); |
1440 | sprintf ((hdr->ar_mode), "%-8o", (unsigned int) status.st_mode); | |
1441 | sprintf ((hdr->ar_size), "%-10ld", (long) status.st_size); | |
1442 | /* Correct for a lossage in sprintf whereby it null-terminates. I cannot | |
1443 | understand how these C losers could design such a ramshackle bunch of | |
047066e1 | 1444 | IO operations. */ |
252b5132 RH |
1445 | temp = (char *) hdr; |
1446 | temp1 = temp + sizeof (struct ar_hdr) - 2; | |
1447 | for (; temp < temp1; temp++) | |
1448 | { | |
1449 | if (*temp == '\0') | |
1450 | *temp = ' '; | |
1451 | } | |
1452 | strncpy (hdr->ar_fmag, ARFMAG, 2); | |
1453 | ared->parsed_size = status.st_size; | |
1454 | ared->arch_header = (char *) hdr; | |
1455 | ||
1456 | return ared; | |
1457 | } | |
1458 | ||
1459 | /* This is magic required by the "ar" program. Since it's | |
047066e1 KH |
1460 | undocumented, it's undocumented. You may think that it would take |
1461 | a strong stomach to write this, and it does, but it takes even a | |
1462 | stronger stomach to try to code around such a thing! */ | |
252b5132 | 1463 | |
b34976b6 AM |
1464 | struct ar_hdr *bfd_special_undocumented_glue |
1465 | PARAMS ((bfd *, const char *)); | |
252b5132 RH |
1466 | |
1467 | struct ar_hdr * | |
1468 | bfd_special_undocumented_glue (abfd, filename) | |
1469 | bfd *abfd; | |
1470 | const char *filename; | |
1471 | { | |
1472 | struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0); | |
1473 | if (ar_elt == NULL) | |
1474 | return NULL; | |
1475 | return (struct ar_hdr *) ar_elt->arch_header; | |
1476 | } | |
1477 | ||
047066e1 KH |
1478 | /* Analogous to stat call. */ |
1479 | ||
252b5132 RH |
1480 | int |
1481 | bfd_generic_stat_arch_elt (abfd, buf) | |
1482 | bfd *abfd; | |
1483 | struct stat *buf; | |
1484 | { | |
1485 | struct ar_hdr *hdr; | |
1486 | char *aloser; | |
1487 | ||
1488 | if (abfd->arelt_data == NULL) | |
1489 | { | |
1490 | bfd_set_error (bfd_error_invalid_operation); | |
1491 | return -1; | |
1492 | } | |
1493 | ||
1494 | hdr = arch_hdr (abfd); | |
1495 | ||
047066e1 KH |
1496 | #define foo(arelt, stelt, size) \ |
1497 | buf->stelt = strtol (hdr->arelt, &aloser, size); \ | |
1498 | if (aloser == hdr->arelt) \ | |
1499 | return -1; | |
1500 | ||
23afc6f6 JL |
1501 | /* Some platforms support special notations for large IDs. */ |
1502 | #ifdef HPUX_LARGE_AR_IDS | |
047066e1 KH |
1503 | # define foo2(arelt, stelt, size) \ |
1504 | if (hdr->arelt[5] == ' ') \ | |
1505 | { \ | |
1506 | foo (arelt, stelt, size); \ | |
1507 | } \ | |
1508 | else \ | |
1509 | { \ | |
1510 | int cnt; \ | |
1511 | for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \ | |
1512 | { \ | |
1513 | if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \ | |
1514 | return -1; \ | |
1515 | buf->stelt <<= 6; \ | |
1516 | buf->stelt += hdr->arelt[cnt] - ' '; \ | |
1517 | } \ | |
1518 | if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \ | |
1519 | return -1; \ | |
1520 | buf->stelt <<= 2; \ | |
1521 | buf->stelt += hdr->arelt[5] - '@'; \ | |
1522 | } | |
23afc6f6 JL |
1523 | #else |
1524 | # define foo2(arelt, stelt, size) foo (arelt, stelt, size) | |
1525 | #endif | |
252b5132 RH |
1526 | |
1527 | foo (ar_date, st_mtime, 10); | |
23afc6f6 JL |
1528 | foo2 (ar_uid, st_uid, 10); |
1529 | foo2 (ar_gid, st_gid, 10); | |
252b5132 RH |
1530 | foo (ar_mode, st_mode, 8); |
1531 | ||
1532 | buf->st_size = arch_eltdata (abfd)->parsed_size; | |
1533 | ||
1534 | return 0; | |
1535 | } | |
1536 | ||
1537 | void | |
1538 | bfd_dont_truncate_arname (abfd, pathname, arhdr) | |
1539 | bfd *abfd; | |
dc810e39 | 1540 | const char *pathname; |
252b5132 RH |
1541 | char *arhdr; |
1542 | { | |
1543 | /* FIXME: This interacts unpleasantly with ar's quick-append option. | |
1544 | Fortunately ic960 users will never use that option. Fixing this | |
1545 | is very hard; fortunately I know how to do it and will do so once | |
d70910e8 | 1546 | intel's release is out the door. */ |
252b5132 RH |
1547 | |
1548 | struct ar_hdr *hdr = (struct ar_hdr *) arhdr; | |
1549 | size_t length; | |
1550 | const char *filename; | |
1551 | size_t maxlen = ar_maxnamelen (abfd); | |
1552 | ||
1553 | if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) | |
1554 | { | |
1555 | bfd_bsd_truncate_arname (abfd, pathname, arhdr); | |
1556 | return; | |
1557 | } | |
1558 | ||
1559 | filename = normalize (abfd, pathname); | |
1560 | if (filename == NULL) | |
1561 | { | |
1562 | /* FIXME */ | |
1563 | abort (); | |
1564 | } | |
1565 | ||
1566 | length = strlen (filename); | |
1567 | ||
1568 | if (length <= maxlen) | |
1569 | memcpy (hdr->ar_name, filename, length); | |
1570 | ||
1571 | /* Add the padding character if there is room for it. */ | |
1572 | if (length < maxlen | |
1573 | || (length == maxlen && length < sizeof hdr->ar_name)) | |
1574 | (hdr->ar_name)[length] = ar_padchar (abfd); | |
1575 | } | |
1576 | ||
1577 | void | |
1578 | bfd_bsd_truncate_arname (abfd, pathname, arhdr) | |
1579 | bfd *abfd; | |
dc810e39 | 1580 | const char *pathname; |
252b5132 RH |
1581 | char *arhdr; |
1582 | { | |
1583 | struct ar_hdr *hdr = (struct ar_hdr *) arhdr; | |
dc810e39 AM |
1584 | size_t length; |
1585 | const char *filename = strrchr (pathname, '/'); | |
1586 | size_t maxlen = ar_maxnamelen (abfd); | |
252b5132 | 1587 | |
5af11cab AM |
1588 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
1589 | { | |
1590 | /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ | |
1591 | char *bslash = strrchr (pathname, '\\'); | |
2ab47eed | 1592 | if (filename == NULL || (bslash != NULL && bslash > filename)) |
5af11cab AM |
1593 | filename = bslash; |
1594 | if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':') | |
1595 | filename = pathname + 1; | |
1596 | } | |
1597 | #endif | |
1598 | ||
252b5132 RH |
1599 | if (filename == NULL) |
1600 | filename = pathname; | |
1601 | else | |
1602 | ++filename; | |
1603 | ||
1604 | length = strlen (filename); | |
1605 | ||
1606 | if (length <= maxlen) | |
1607 | memcpy (hdr->ar_name, filename, length); | |
1608 | else | |
1609 | { | |
1610 | /* pathname: meet procrustes */ | |
1611 | memcpy (hdr->ar_name, filename, maxlen); | |
1612 | length = maxlen; | |
1613 | } | |
1614 | ||
1615 | if (length < maxlen) | |
1616 | (hdr->ar_name)[length] = ar_padchar (abfd); | |
1617 | } | |
1618 | ||
1619 | /* Store name into ar header. Truncates the name to fit. | |
1620 | 1> strip pathname to be just the basename. | |
1621 | 2> if it's short enuf to fit, stuff it in. | |
1622 | 3> If it doesn't end with .o, truncate it to fit | |
1623 | 4> truncate it before the .o, append .o, stuff THAT in. */ | |
1624 | ||
1625 | /* This is what gnu ar does. It's better but incompatible with the | |
d70910e8 | 1626 | bsd ar. */ |
252b5132 RH |
1627 | |
1628 | void | |
1629 | bfd_gnu_truncate_arname (abfd, pathname, arhdr) | |
1630 | bfd *abfd; | |
dc810e39 | 1631 | const char *pathname; |
252b5132 RH |
1632 | char *arhdr; |
1633 | { | |
1634 | struct ar_hdr *hdr = (struct ar_hdr *) arhdr; | |
dc810e39 AM |
1635 | size_t length; |
1636 | const char *filename = strrchr (pathname, '/'); | |
1637 | size_t maxlen = ar_maxnamelen (abfd); | |
252b5132 | 1638 | |
5af11cab AM |
1639 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
1640 | { | |
1641 | /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ | |
1642 | char *bslash = strrchr (pathname, '\\'); | |
2ab47eed | 1643 | if (filename == NULL || (bslash != NULL && bslash > filename)) |
5af11cab AM |
1644 | filename = bslash; |
1645 | if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':') | |
1646 | filename = pathname + 1; | |
1647 | } | |
1648 | #endif | |
1649 | ||
252b5132 RH |
1650 | if (filename == NULL) |
1651 | filename = pathname; | |
1652 | else | |
1653 | ++filename; | |
1654 | ||
1655 | length = strlen (filename); | |
1656 | ||
1657 | if (length <= maxlen) | |
1658 | memcpy (hdr->ar_name, filename, length); | |
1659 | else | |
1660 | { /* pathname: meet procrustes */ | |
1661 | memcpy (hdr->ar_name, filename, maxlen); | |
1662 | if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) | |
1663 | { | |
1664 | hdr->ar_name[maxlen - 2] = '.'; | |
1665 | hdr->ar_name[maxlen - 1] = 'o'; | |
1666 | } | |
1667 | length = maxlen; | |
1668 | } | |
1669 | ||
1670 | if (length < 16) | |
1671 | (hdr->ar_name)[length] = ar_padchar (abfd); | |
1672 | } | |
1673 | \f | |
047066e1 | 1674 | /* The BFD is open for write and has its format set to bfd_archive. */ |
252b5132 | 1675 | |
b34976b6 | 1676 | bfd_boolean |
252b5132 RH |
1677 | _bfd_write_archive_contents (arch) |
1678 | bfd *arch; | |
1679 | { | |
1680 | bfd *current; | |
1681 | char *etable = NULL; | |
1682 | bfd_size_type elength = 0; | |
1683 | const char *ename = NULL; | |
b34976b6 AM |
1684 | bfd_boolean makemap = bfd_has_map (arch); |
1685 | /* If no .o's, don't bother to make a map. */ | |
1686 | bfd_boolean hasobjects = FALSE; | |
252b5132 RH |
1687 | bfd_size_type wrote; |
1688 | unsigned int i; | |
1689 | int tries; | |
1690 | ||
1691 | /* Verify the viability of all entries; if any of them live in the | |
1692 | filesystem (as opposed to living in an archive open for input) | |
1693 | then construct a fresh ar_hdr for them. */ | |
1694 | for (current = arch->archive_head; current; current = current->next) | |
1695 | { | |
8000a618 DD |
1696 | /* This check is checking the bfds for the objects we're reading |
1697 | from (which are usually either an object file or archive on | |
1698 | disk), not the archive entries we're writing to. We don't | |
1699 | actually create bfds for the archive members, we just copy | |
1700 | them byte-wise when we write out the archive. */ | |
252b5132 RH |
1701 | if (bfd_write_p (current)) |
1702 | { | |
1703 | bfd_set_error (bfd_error_invalid_operation); | |
b34976b6 | 1704 | return FALSE; |
252b5132 RH |
1705 | } |
1706 | if (!current->arelt_data) | |
1707 | { | |
1708 | current->arelt_data = | |
1709 | (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename, current); | |
1710 | if (!current->arelt_data) | |
b34976b6 | 1711 | return FALSE; |
252b5132 | 1712 | |
047066e1 | 1713 | /* Put in the file name. */ |
252b5132 RH |
1714 | BFD_SEND (arch, _bfd_truncate_arname, (arch, |
1715 | current->filename, | |
1716 | (char *) arch_hdr (current))); | |
1717 | } | |
1718 | ||
1719 | if (makemap && ! hasobjects) | |
047066e1 | 1720 | { /* Don't bother if we won't make a map! */ |
252b5132 RH |
1721 | if ((bfd_check_format (current, bfd_object)) |
1722 | #if 0 /* FIXME -- these are not set correctly */ | |
1723 | && ((bfd_get_file_flags (current) & HAS_SYMS)) | |
1724 | #endif | |
1725 | ) | |
b34976b6 | 1726 | hasobjects = TRUE; |
252b5132 RH |
1727 | } |
1728 | } | |
1729 | ||
1730 | if (!BFD_SEND (arch, _bfd_construct_extended_name_table, | |
1731 | (arch, &etable, &elength, &ename))) | |
b34976b6 | 1732 | return FALSE; |
252b5132 RH |
1733 | |
1734 | if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0) | |
b34976b6 | 1735 | return FALSE; |
252b5132 | 1736 | #ifdef GNU960 |
dc810e39 | 1737 | wrote = bfd_bwrite (BFD_GNU960_ARMAG (arch), (bfd_size_type) SARMAG, arch); |
252b5132 | 1738 | #else |
dc810e39 | 1739 | wrote = bfd_bwrite (ARMAG, (bfd_size_type) SARMAG, arch); |
252b5132 RH |
1740 | #endif |
1741 | if (wrote != SARMAG) | |
b34976b6 | 1742 | return FALSE; |
252b5132 RH |
1743 | |
1744 | if (makemap && hasobjects) | |
1745 | { | |
82e51918 | 1746 | if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength)) |
b34976b6 | 1747 | return FALSE; |
252b5132 RH |
1748 | } |
1749 | ||
1750 | if (elength != 0) | |
1751 | { | |
1752 | struct ar_hdr hdr; | |
1753 | ||
1754 | memset ((char *) (&hdr), 0, sizeof (struct ar_hdr)); | |
1755 | strcpy (hdr.ar_name, ename); | |
1756 | /* Round size up to even number in archive header. */ | |
1757 | sprintf (&(hdr.ar_size[0]), "%-10d", | |
dc810e39 | 1758 | (int) ((elength + 1) & ~(bfd_size_type) 1)); |
252b5132 RH |
1759 | strncpy (hdr.ar_fmag, ARFMAG, 2); |
1760 | for (i = 0; i < sizeof (struct ar_hdr); i++) | |
1761 | if (((char *) (&hdr))[i] == '\0') | |
1762 | (((char *) (&hdr))[i]) = ' '; | |
dc810e39 | 1763 | if ((bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), arch) |
252b5132 | 1764 | != sizeof (struct ar_hdr)) |
dc810e39 | 1765 | || bfd_bwrite (etable, elength, arch) != elength) |
b34976b6 | 1766 | return FALSE; |
252b5132 RH |
1767 | if ((elength % 2) == 1) |
1768 | { | |
dc810e39 | 1769 | if (bfd_bwrite ("\012", (bfd_size_type) 1, arch) != 1) |
b34976b6 | 1770 | return FALSE; |
252b5132 RH |
1771 | } |
1772 | } | |
1773 | ||
1774 | for (current = arch->archive_head; current; current = current->next) | |
1775 | { | |
1776 | char buffer[DEFAULT_BUFFERSIZE]; | |
1777 | unsigned int remaining = arelt_size (current); | |
1778 | struct ar_hdr *hdr = arch_hdr (current); | |
1779 | ||
047066e1 | 1780 | /* Write ar header. */ |
dc810e39 AM |
1781 | if (bfd_bwrite ((PTR) hdr, (bfd_size_type) sizeof (*hdr), arch) |
1782 | != sizeof (*hdr)) | |
b34976b6 | 1783 | return FALSE; |
252b5132 | 1784 | if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) |
b34976b6 | 1785 | return FALSE; |
252b5132 RH |
1786 | while (remaining) |
1787 | { | |
1788 | unsigned int amt = DEFAULT_BUFFERSIZE; | |
1789 | if (amt > remaining) | |
1790 | amt = remaining; | |
1791 | errno = 0; | |
dc810e39 | 1792 | if (bfd_bread (buffer, (bfd_size_type) amt, current) != amt) |
252b5132 RH |
1793 | { |
1794 | if (bfd_get_error () != bfd_error_system_call) | |
1795 | bfd_set_error (bfd_error_malformed_archive); | |
b34976b6 | 1796 | return FALSE; |
252b5132 | 1797 | } |
dc810e39 | 1798 | if (bfd_bwrite (buffer, (bfd_size_type) amt, arch) != amt) |
b34976b6 | 1799 | return FALSE; |
252b5132 RH |
1800 | remaining -= amt; |
1801 | } | |
1802 | if ((arelt_size (current) % 2) == 1) | |
1803 | { | |
dc810e39 | 1804 | if (bfd_bwrite ("\012", (bfd_size_type) 1, arch) != 1) |
b34976b6 | 1805 | return FALSE; |
252b5132 RH |
1806 | } |
1807 | } | |
1808 | ||
1809 | if (makemap && hasobjects) | |
1810 | { | |
1811 | /* Verify the timestamp in the archive file. If it would not be | |
1812 | accepted by the linker, rewrite it until it would be. If | |
1813 | anything odd happens, break out and just return. (The | |
1814 | Berkeley linker checks the timestamp and refuses to read the | |
1815 | table-of-contents if it is >60 seconds less than the file's | |
1816 | modified-time. That painful hack requires this painful hack. */ | |
1817 | tries = 1; | |
1818 | do | |
1819 | { | |
1820 | if (bfd_update_armap_timestamp (arch)) | |
1821 | break; | |
1822 | (*_bfd_error_handler) | |
1823 | (_("Warning: writing archive was slow: rewriting timestamp\n")); | |
1824 | } | |
1825 | while (++tries < 6); | |
1826 | } | |
1827 | ||
b34976b6 | 1828 | return TRUE; |
252b5132 RH |
1829 | } |
1830 | \f | |
047066e1 | 1831 | /* Note that the namidx for the first symbol is 0. */ |
252b5132 | 1832 | |
b34976b6 | 1833 | bfd_boolean |
252b5132 RH |
1834 | _bfd_compute_and_write_armap (arch, elength) |
1835 | bfd *arch; | |
1836 | unsigned int elength; | |
1837 | { | |
1838 | char *first_name = NULL; | |
1839 | bfd *current; | |
1840 | file_ptr elt_no = 0; | |
1841 | struct orl *map = NULL; | |
dc810e39 AM |
1842 | unsigned int orl_max = 1024; /* fine initial default */ |
1843 | unsigned int orl_count = 0; | |
252b5132 RH |
1844 | int stridx = 0; /* string index */ |
1845 | asymbol **syms = NULL; | |
1846 | long syms_max = 0; | |
b34976b6 | 1847 | bfd_boolean ret; |
dc810e39 | 1848 | bfd_size_type amt; |
252b5132 | 1849 | |
d70910e8 | 1850 | /* Dunno if this is the best place for this info... */ |
252b5132 RH |
1851 | if (elength != 0) |
1852 | elength += sizeof (struct ar_hdr); | |
1853 | elength += elength % 2; | |
1854 | ||
dc810e39 AM |
1855 | amt = (bfd_size_type) orl_max * sizeof (struct orl); |
1856 | map = (struct orl *) bfd_malloc (amt); | |
252b5132 RH |
1857 | if (map == NULL) |
1858 | goto error_return; | |
1859 | ||
1860 | /* We put the symbol names on the arch objalloc, and then discard | |
1861 | them when done. */ | |
dc810e39 | 1862 | first_name = bfd_alloc (arch, (bfd_size_type) 1); |
252b5132 RH |
1863 | if (first_name == NULL) |
1864 | goto error_return; | |
1865 | ||
047066e1 | 1866 | /* Drop all the files called __.SYMDEF, we're going to make our own. */ |
252b5132 RH |
1867 | while (arch->archive_head && |
1868 | strcmp (arch->archive_head->filename, "__.SYMDEF") == 0) | |
1869 | arch->archive_head = arch->archive_head->next; | |
1870 | ||
047066e1 | 1871 | /* Map over each element. */ |
252b5132 RH |
1872 | for (current = arch->archive_head; |
1873 | current != (bfd *) NULL; | |
1874 | current = current->next, elt_no++) | |
1875 | { | |
82e51918 AM |
1876 | if (bfd_check_format (current, bfd_object) |
1877 | && (bfd_get_file_flags (current) & HAS_SYMS) != 0) | |
252b5132 RH |
1878 | { |
1879 | long storage; | |
1880 | long symcount; | |
1881 | long src_count; | |
1882 | ||
1883 | storage = bfd_get_symtab_upper_bound (current); | |
1884 | if (storage < 0) | |
1885 | goto error_return; | |
1886 | ||
1887 | if (storage != 0) | |
1888 | { | |
1889 | if (storage > syms_max) | |
1890 | { | |
1891 | if (syms_max > 0) | |
1892 | free (syms); | |
1893 | syms_max = storage; | |
dc810e39 | 1894 | syms = (asymbol **) bfd_malloc ((bfd_size_type) syms_max); |
252b5132 RH |
1895 | if (syms == NULL) |
1896 | goto error_return; | |
1897 | } | |
1898 | symcount = bfd_canonicalize_symtab (current, syms); | |
1899 | if (symcount < 0) | |
1900 | goto error_return; | |
1901 | ||
047066e1 KH |
1902 | /* Now map over all the symbols, picking out the ones we |
1903 | want. */ | |
252b5132 RH |
1904 | for (src_count = 0; src_count < symcount; src_count++) |
1905 | { | |
1906 | flagword flags = (syms[src_count])->flags; | |
1907 | asection *sec = syms[src_count]->section; | |
1908 | ||
77fb9c28 NC |
1909 | if ((flags & BSF_GLOBAL || |
1910 | flags & BSF_WEAK || | |
1911 | flags & BSF_INDIRECT || | |
1912 | bfd_is_com_section (sec)) | |
1913 | && ! bfd_is_und_section (sec)) | |
252b5132 | 1914 | { |
dc810e39 | 1915 | bfd_size_type namelen; |
77fb9c28 NC |
1916 | struct orl *new_map; |
1917 | ||
047066e1 | 1918 | /* This symbol will go into the archive header. */ |
252b5132 RH |
1919 | if (orl_count == orl_max) |
1920 | { | |
1921 | orl_max *= 2; | |
dc810e39 AM |
1922 | amt = (bfd_size_type) orl_max * sizeof (struct orl); |
1923 | new_map = (struct orl *) bfd_realloc (map, amt); | |
252b5132 RH |
1924 | if (new_map == (struct orl *) NULL) |
1925 | goto error_return; | |
1926 | ||
1927 | map = new_map; | |
1928 | } | |
1929 | ||
1930 | namelen = strlen (syms[src_count]->name); | |
dc810e39 AM |
1931 | amt = sizeof (char *); |
1932 | map[orl_count].name = (char **) bfd_alloc (arch, amt); | |
252b5132 RH |
1933 | if (map[orl_count].name == NULL) |
1934 | goto error_return; | |
1935 | *(map[orl_count].name) = bfd_alloc (arch, namelen + 1); | |
1936 | if (*(map[orl_count].name) == NULL) | |
1937 | goto error_return; | |
1938 | strcpy (*(map[orl_count].name), syms[src_count]->name); | |
dc810e39 AM |
1939 | map[orl_count].u.abfd = current; |
1940 | map[orl_count].namidx = stridx; | |
252b5132 RH |
1941 | |
1942 | stridx += namelen + 1; | |
1943 | ++orl_count; | |
77fb9c28 | 1944 | } |
252b5132 RH |
1945 | } |
1946 | } | |
1947 | ||
1948 | /* Now ask the BFD to free up any cached information, so we | |
1949 | don't fill all of memory with symbol tables. */ | |
1950 | if (! bfd_free_cached_info (current)) | |
1951 | goto error_return; | |
1952 | } | |
1953 | } | |
1954 | ||
047066e1 | 1955 | /* OK, now we have collected all the data, let's write them out. */ |
252b5132 RH |
1956 | ret = BFD_SEND (arch, write_armap, |
1957 | (arch, elength, map, orl_count, stridx)); | |
1958 | ||
1959 | if (syms_max > 0) | |
1960 | free (syms); | |
1961 | if (map != NULL) | |
1962 | free (map); | |
1963 | if (first_name != NULL) | |
1964 | bfd_release (arch, first_name); | |
1965 | ||
1966 | return ret; | |
1967 | ||
1968 | error_return: | |
1969 | if (syms_max > 0) | |
1970 | free (syms); | |
1971 | if (map != NULL) | |
1972 | free (map); | |
1973 | if (first_name != NULL) | |
1974 | bfd_release (arch, first_name); | |
1975 | ||
b34976b6 | 1976 | return FALSE; |
252b5132 RH |
1977 | } |
1978 | ||
b34976b6 | 1979 | bfd_boolean |
252b5132 RH |
1980 | bsd_write_armap (arch, elength, map, orl_count, stridx) |
1981 | bfd *arch; | |
1982 | unsigned int elength; | |
1983 | struct orl *map; | |
1984 | unsigned int orl_count; | |
1985 | int stridx; | |
1986 | { | |
1987 | int padit = stridx & 1; | |
1988 | unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE; | |
1989 | unsigned int stringsize = stridx + padit; | |
d70910e8 | 1990 | /* Include 8 bytes to store ranlibsize and stringsize in output. */ |
252b5132 RH |
1991 | unsigned int mapsize = ranlibsize + stringsize + 8; |
1992 | file_ptr firstreal; | |
1993 | bfd *current = arch->archive_head; | |
1994 | bfd *last_elt = current; /* last element arch seen */ | |
1995 | bfd_byte temp[4]; | |
1996 | unsigned int count; | |
1997 | struct ar_hdr hdr; | |
1998 | struct stat statbuf; | |
1999 | unsigned int i; | |
2000 | ||
2001 | firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG; | |
2002 | ||
2003 | stat (arch->filename, &statbuf); | |
2004 | memset ((char *) (&hdr), 0, sizeof (struct ar_hdr)); | |
2005 | sprintf (hdr.ar_name, RANLIBMAG); | |
2006 | /* Remember the timestamp, to keep it holy. But fudge it a little. */ | |
2007 | bfd_ardata (arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET; | |
2008 | bfd_ardata (arch)->armap_datepos = (SARMAG | |
2009 | + offsetof (struct ar_hdr, ar_date[0])); | |
2010 | sprintf (hdr.ar_date, "%ld", bfd_ardata (arch)->armap_timestamp); | |
252b5132 RH |
2011 | sprintf (hdr.ar_uid, "%ld", (long) getuid ()); |
2012 | sprintf (hdr.ar_gid, "%ld", (long) getgid ()); | |
252b5132 RH |
2013 | sprintf (hdr.ar_size, "%-10d", (int) mapsize); |
2014 | strncpy (hdr.ar_fmag, ARFMAG, 2); | |
2015 | for (i = 0; i < sizeof (struct ar_hdr); i++) | |
2016 | if (((char *) (&hdr))[i] == '\0') | |
2017 | (((char *) (&hdr))[i]) = ' '; | |
dc810e39 | 2018 | if (bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), arch) |
252b5132 | 2019 | != sizeof (struct ar_hdr)) |
b34976b6 | 2020 | return FALSE; |
dc810e39 AM |
2021 | H_PUT_32 (arch, ranlibsize, temp); |
2022 | if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), arch) != sizeof (temp)) | |
b34976b6 | 2023 | return FALSE; |
252b5132 RH |
2024 | |
2025 | for (count = 0; count < orl_count; count++) | |
2026 | { | |
2027 | bfd_byte buf[BSD_SYMDEF_SIZE]; | |
2028 | ||
dc810e39 | 2029 | if (map[count].u.abfd != last_elt) |
252b5132 RH |
2030 | { |
2031 | do | |
2032 | { | |
2033 | firstreal += arelt_size (current) + sizeof (struct ar_hdr); | |
2034 | firstreal += firstreal % 2; | |
2035 | current = current->next; | |
2036 | } | |
dc810e39 | 2037 | while (current != map[count].u.abfd); |
252b5132 RH |
2038 | } /* if new archive element */ |
2039 | ||
2040 | last_elt = current; | |
dc810e39 AM |
2041 | H_PUT_32 (arch, map[count].namidx, buf); |
2042 | H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE); | |
2043 | if (bfd_bwrite (buf, (bfd_size_type) BSD_SYMDEF_SIZE, arch) | |
2044 | != BSD_SYMDEF_SIZE) | |
b34976b6 | 2045 | return FALSE; |
252b5132 RH |
2046 | } |
2047 | ||
047066e1 | 2048 | /* Now write the strings themselves. */ |
dc810e39 AM |
2049 | H_PUT_32 (arch, stringsize, temp); |
2050 | if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), arch) != sizeof (temp)) | |
b34976b6 | 2051 | return FALSE; |
252b5132 RH |
2052 | for (count = 0; count < orl_count; count++) |
2053 | { | |
2054 | size_t len = strlen (*map[count].name) + 1; | |
2055 | ||
dc810e39 | 2056 | if (bfd_bwrite (*map[count].name, (bfd_size_type) len, arch) != len) |
b34976b6 | 2057 | return FALSE; |
252b5132 RH |
2058 | } |
2059 | ||
2060 | /* The spec sez this should be a newline. But in order to be | |
d70910e8 | 2061 | bug-compatible for sun's ar we use a null. */ |
252b5132 RH |
2062 | if (padit) |
2063 | { | |
dc810e39 | 2064 | if (bfd_bwrite ("", (bfd_size_type) 1, arch) != 1) |
b34976b6 | 2065 | return FALSE; |
252b5132 RH |
2066 | } |
2067 | ||
b34976b6 | 2068 | return TRUE; |
252b5132 RH |
2069 | } |
2070 | ||
2071 | /* At the end of archive file handling, update the timestamp in the | |
2072 | file, so the linker will accept it. | |
2073 | ||
b34976b6 AM |
2074 | Return TRUE if the timestamp was OK, or an unusual problem happened. |
2075 | Return FALSE if we updated the timestamp. */ | |
252b5132 | 2076 | |
b34976b6 | 2077 | bfd_boolean |
252b5132 RH |
2078 | _bfd_archive_bsd_update_armap_timestamp (arch) |
2079 | bfd *arch; | |
2080 | { | |
2081 | struct stat archstat; | |
2082 | struct ar_hdr hdr; | |
2083 | unsigned int i; | |
2084 | ||
2085 | /* Flush writes, get last-write timestamp from file, and compare it | |
2086 | to the timestamp IN the file. */ | |
2087 | bfd_flush (arch); | |
2088 | if (bfd_stat (arch, &archstat) == -1) | |
2089 | { | |
5fe39cae | 2090 | bfd_perror (_("Reading archive file mod timestamp")); |
047066e1 KH |
2091 | |
2092 | /* Can't read mod time for some reason. */ | |
b34976b6 | 2093 | return TRUE; |
252b5132 RH |
2094 | } |
2095 | if (archstat.st_mtime <= bfd_ardata (arch)->armap_timestamp) | |
047066e1 | 2096 | /* OK by the linker's rules. */ |
b34976b6 | 2097 | return TRUE; |
252b5132 RH |
2098 | |
2099 | /* Update the timestamp. */ | |
2100 | bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET; | |
2101 | ||
2102 | /* Prepare an ASCII version suitable for writing. */ | |
2103 | memset (hdr.ar_date, 0, sizeof (hdr.ar_date)); | |
2104 | sprintf (hdr.ar_date, "%ld", bfd_ardata (arch)->armap_timestamp); | |
2105 | for (i = 0; i < sizeof (hdr.ar_date); i++) | |
2106 | if (hdr.ar_date[i] == '\0') | |
2107 | (hdr.ar_date)[i] = ' '; | |
2108 | ||
2109 | /* Write it into the file. */ | |
2110 | bfd_ardata (arch)->armap_datepos = (SARMAG | |
2111 | + offsetof (struct ar_hdr, ar_date[0])); | |
2112 | if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0 | |
dc810e39 | 2113 | || (bfd_bwrite (hdr.ar_date, (bfd_size_type) sizeof (hdr.ar_date), arch) |
252b5132 RH |
2114 | != sizeof (hdr.ar_date))) |
2115 | { | |
5fe39cae | 2116 | bfd_perror (_("Writing updated armap timestamp")); |
047066e1 KH |
2117 | |
2118 | /* Some error while writing. */ | |
b34976b6 | 2119 | return TRUE; |
252b5132 RH |
2120 | } |
2121 | ||
047066e1 | 2122 | /* We updated the timestamp successfully. */ |
b34976b6 | 2123 | return FALSE; |
252b5132 RH |
2124 | } |
2125 | \f | |
2126 | /* A coff armap looks like : | |
2127 | lARMAG | |
2128 | struct ar_hdr with name = '/' | |
2129 | number of symbols | |
2130 | offset of file for symbol 0 | |
2131 | offset of file for symbol 1 | |
2132 | ||
2133 | offset of file for symbol n-1 | |
2134 | symbol name 0 | |
2135 | symbol name 1 | |
2136 | ||
2137 | symbol name n-1 | |
2138 | */ | |
2139 | ||
b34976b6 | 2140 | bfd_boolean |
252b5132 RH |
2141 | coff_write_armap (arch, elength, map, symbol_count, stridx) |
2142 | bfd *arch; | |
2143 | unsigned int elength; | |
2144 | struct orl *map; | |
2145 | unsigned int symbol_count; | |
2146 | int stridx; | |
2147 | { | |
2148 | /* The size of the ranlib is the number of exported symbols in the | |
08da05b0 | 2149 | archive * the number of bytes in an int, + an int for the count. */ |
252b5132 RH |
2150 | unsigned int ranlibsize = (symbol_count * 4) + 4; |
2151 | unsigned int stringsize = stridx; | |
2152 | unsigned int mapsize = stringsize + ranlibsize; | |
dc810e39 | 2153 | unsigned int archive_member_file_ptr; |
252b5132 RH |
2154 | bfd *current = arch->archive_head; |
2155 | unsigned int count; | |
2156 | struct ar_hdr hdr; | |
2157 | unsigned int i; | |
2158 | int padit = mapsize & 1; | |
2159 | ||
2160 | if (padit) | |
2161 | mapsize++; | |
2162 | ||
047066e1 | 2163 | /* Work out where the first object file will go in the archive. */ |
252b5132 RH |
2164 | archive_member_file_ptr = (mapsize |
2165 | + elength | |
2166 | + sizeof (struct ar_hdr) | |
2167 | + SARMAG); | |
2168 | ||
2169 | memset ((char *) (&hdr), 0, sizeof (struct ar_hdr)); | |
2170 | hdr.ar_name[0] = '/'; | |
2171 | sprintf (hdr.ar_size, "%-10d", (int) mapsize); | |
2172 | sprintf (hdr.ar_date, "%ld", (long) time (NULL)); | |
047066e1 | 2173 | /* This, at least, is what Intel coff sets the values to. */ |
252b5132 RH |
2174 | sprintf ((hdr.ar_uid), "%d", 0); |
2175 | sprintf ((hdr.ar_gid), "%d", 0); | |
2176 | sprintf ((hdr.ar_mode), "%-7o", (unsigned) 0); | |
2177 | strncpy (hdr.ar_fmag, ARFMAG, 2); | |
2178 | ||
2179 | for (i = 0; i < sizeof (struct ar_hdr); i++) | |
2180 | if (((char *) (&hdr))[i] == '\0') | |
2181 | (((char *) (&hdr))[i]) = ' '; | |
2182 | ||
047066e1 | 2183 | /* Write the ar header for this item and the number of symbols. */ |
252b5132 | 2184 | |
dc810e39 | 2185 | if (bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), arch) |
252b5132 | 2186 | != sizeof (struct ar_hdr)) |
b34976b6 | 2187 | return FALSE; |
252b5132 | 2188 | |
4dae1ae7 | 2189 | if (!bfd_write_bigendian_4byte_int (arch, symbol_count)) |
b34976b6 | 2190 | return FALSE; |
252b5132 RH |
2191 | |
2192 | /* Two passes, first write the file offsets for each symbol - | |
2193 | remembering that each offset is on a two byte boundary. */ | |
2194 | ||
2195 | /* Write out the file offset for the file associated with each | |
2196 | symbol, and remember to keep the offsets padded out. */ | |
2197 | ||
2198 | current = arch->archive_head; | |
2199 | count = 0; | |
2200 | while (current != (bfd *) NULL && count < symbol_count) | |
2201 | { | |
047066e1 KH |
2202 | /* For each symbol which is used defined in this object, write |
2203 | out the object file's address in the archive. */ | |
252b5132 | 2204 | |
dc810e39 | 2205 | while (count < symbol_count && map[count].u.abfd == current) |
252b5132 | 2206 | { |
4dae1ae7 | 2207 | if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr)) |
b34976b6 | 2208 | return FALSE; |
252b5132 RH |
2209 | count++; |
2210 | } | |
047066e1 | 2211 | /* Add size of this archive entry. */ |
252b5132 RH |
2212 | archive_member_file_ptr += (arelt_size (current) |
2213 | + sizeof (struct ar_hdr)); | |
047066e1 | 2214 | /* Remember aboout the even alignment. */ |
252b5132 RH |
2215 | archive_member_file_ptr += archive_member_file_ptr % 2; |
2216 | current = current->next; | |
2217 | } | |
2218 | ||
047066e1 | 2219 | /* Now write the strings themselves. */ |
252b5132 RH |
2220 | for (count = 0; count < symbol_count; count++) |
2221 | { | |
2222 | size_t len = strlen (*map[count].name) + 1; | |
2223 | ||
dc810e39 | 2224 | if (bfd_bwrite (*map[count].name, (bfd_size_type) len, arch) != len) |
b34976b6 | 2225 | return FALSE; |
252b5132 RH |
2226 | } |
2227 | ||
2228 | /* The spec sez this should be a newline. But in order to be | |
d70910e8 | 2229 | bug-compatible for arc960 we use a null. */ |
252b5132 RH |
2230 | if (padit) |
2231 | { | |
dc810e39 | 2232 | if (bfd_bwrite ("", (bfd_size_type) 1, arch) != 1) |
b34976b6 | 2233 | return FALSE; |
252b5132 RH |
2234 | } |
2235 | ||
b34976b6 | 2236 | return TRUE; |
252b5132 | 2237 | } |