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