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