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