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