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