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