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