]>
Commit | Line | Data |
---|---|---|
6724ff46 | 1 | /* BFD back-end for archive files (libraries). |
bf9884d4 | 2 | Copyright 1990, 91, 92, 93, 94, 95, 1996 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 | |
bf9884d4 ILT |
664 | are object files. Make sure that if the first file in the |
665 | archive can be recognized as an object file, it is for this | |
666 | target. If not, assume that this is the wrong format. If | |
667 | the first file is not an object file, somebody is doing | |
668 | something weird, and we permit it so that ar -t will work. | |
ae115e51 ILT |
669 | |
670 | This is done because any normal format will recognize any | |
671 | normal archive, regardless of the format of the object files. | |
672 | We do accept an empty archive. */ | |
673 | ||
674 | first = bfd_openr_next_archived_file (abfd, (bfd *) NULL); | |
675 | if (first != NULL) | |
676 | { | |
c33a0e41 ILT |
677 | boolean fail; |
678 | ||
ae115e51 | 679 | first->target_defaulted = false; |
c33a0e41 | 680 | fail = false; |
bf9884d4 ILT |
681 | if (bfd_check_format (first, bfd_object) |
682 | && first->xvec != abfd->xvec) | |
c33a0e41 | 683 | { |
ae115e51 ILT |
684 | (void) bfd_close (first); |
685 | bfd_release (abfd, bfd_ardata (abfd)); | |
a9713b91 | 686 | abfd->tdata.aout_ar_data = tdata_hold; |
bf9884d4 | 687 | bfd_set_error (bfd_error_wrong_format); |
ae115e51 ILT |
688 | return NULL; |
689 | } | |
690 | ||
691 | /* We ought to close first here, but we can't, because we | |
692 | have no way to remove it from the archive cache. FIXME. */ | |
693 | } | |
694 | } | |
695 | ||
4a81b561 DHW |
696 | return abfd->xvec; |
697 | } | |
698 | ||
9a793780 SS |
699 | /* Some constants for a 32 bit BSD archive structure. We do not |
700 | support 64 bit archives presently; so far as I know, none actually | |
701 | exist. Supporting them would require changing these constants, and | |
702 | changing some bfd_h_get_32 to bfd_h_get_64. */ | |
703 | ||
704 | /* The size of an external symdef structure. */ | |
705 | #define BSD_SYMDEF_SIZE 8 | |
706 | ||
707 | /* The offset from the start of a symdef structure to the file offset. */ | |
708 | #define BSD_SYMDEF_OFFSET_SIZE 4 | |
709 | ||
710 | /* The size of the symdef count. */ | |
711 | #define BSD_SYMDEF_COUNT_SIZE 4 | |
712 | ||
713 | /* The size of the string count. */ | |
714 | #define BSD_STRING_COUNT_SIZE 4 | |
715 | ||
4a81b561 | 716 | /* Returns false on error, true otherwise */ |
9a793780 | 717 | |
287c221d | 718 | static boolean |
b59f0276 ILT |
719 | do_slurp_bsd_armap (abfd) |
720 | bfd *abfd; | |
4a81b561 | 721 | { |
287c221d | 722 | struct areltdata *mapdata; |
9a793780 SS |
723 | unsigned int counter; |
724 | bfd_byte *raw_armap, *rbase; | |
287c221d PB |
725 | struct artdata *ardata = bfd_ardata (abfd); |
726 | char *stringbase; | |
727 | unsigned int parsed_size; | |
9a793780 | 728 | carsym *set; |
4ee249da | 729 | |
c33a0e41 | 730 | mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); |
b59f0276 ILT |
731 | if (mapdata == NULL) |
732 | return false; | |
287c221d | 733 | parsed_size = mapdata->parsed_size; |
a927c32d ILT |
734 | bfd_release (abfd, (PTR) mapdata); /* Don't need it any more. */ |
735 | ||
9a793780 SS |
736 | raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size); |
737 | if (raw_armap == (bfd_byte *) NULL) | |
a9713b91 | 738 | return false; |
a927c32d ILT |
739 | |
740 | if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size) | |
741 | { | |
4002f18a ILT |
742 | if (bfd_get_error () != bfd_error_system_call) |
743 | bfd_set_error (bfd_error_malformed_archive); | |
287c221d | 744 | byebye: |
a927c32d | 745 | bfd_release (abfd, (PTR) raw_armap); |
287c221d | 746 | return false; |
a927c32d ILT |
747 | } |
748 | ||
9a793780 | 749 | ardata->symdef_count = bfd_h_get_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE; |
a927c32d | 750 | |
9a793780 SS |
751 | if (ardata->symdef_count * BSD_SYMDEF_SIZE > |
752 | parsed_size - BSD_SYMDEF_COUNT_SIZE) | |
a927c32d | 753 | { |
287c221d | 754 | /* Probably we're using the wrong byte ordering. */ |
25057836 | 755 | bfd_set_error (bfd_error_wrong_format); |
287c221d | 756 | goto byebye; |
a927c32d ILT |
757 | } |
758 | ||
287c221d | 759 | ardata->cache = 0; |
9a793780 SS |
760 | rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; |
761 | stringbase = ((char *) rbase | |
762 | + ardata->symdef_count * BSD_SYMDEF_SIZE | |
763 | + BSD_STRING_COUNT_SIZE); | |
764 | ardata->symdefs = (carsym *) bfd_alloc (abfd, | |
765 | (ardata->symdef_count | |
766 | * sizeof (carsym))); | |
9783e04a | 767 | if (!ardata->symdefs) |
a9713b91 | 768 | return false; |
9a793780 SS |
769 | |
770 | for (counter = 0, set = ardata->symdefs; | |
771 | counter < ardata->symdef_count; | |
772 | counter++, set++, rbase += BSD_SYMDEF_SIZE) | |
a927c32d | 773 | { |
9a793780 SS |
774 | set->name = bfd_h_get_32 (abfd, rbase) + stringbase; |
775 | set->file_offset = bfd_h_get_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); | |
a927c32d ILT |
776 | } |
777 | ||
287c221d PB |
778 | ardata->first_file_filepos = bfd_tell (abfd); |
779 | /* Pad to an even boundary if you have to */ | |
a927c32d | 780 | ardata->first_file_filepos += (ardata->first_file_filepos) % 2; |
287c221d PB |
781 | /* FIXME, we should provide some way to free raw_ardata when |
782 | we are done using the strings from it. For now, it seems | |
783 | to be allocated on an obstack anyway... */ | |
784 | bfd_has_map (abfd) = true; | |
785 | return true; | |
4a81b561 DHW |
786 | } |
787 | ||
788 | /* Returns false on error, true otherwise */ | |
287c221d | 789 | static boolean |
b59f0276 ILT |
790 | do_slurp_coff_armap (abfd) |
791 | bfd *abfd; | |
4a81b561 DHW |
792 | { |
793 | struct areltdata *mapdata; | |
4a81b561 DHW |
794 | int *raw_armap, *rawptr; |
795 | struct artdata *ardata = bfd_ardata (abfd); | |
796 | char *stringbase; | |
797 | unsigned int stringsize; | |
287c221d | 798 | unsigned int parsed_size; |
4a81b561 | 799 | carsym *carsyms; |
a927c32d ILT |
800 | unsigned int nsymz; /* Number of symbols in armap. */ |
801 | bfd_vma (*swap) PARAMS ((const bfd_byte *)); | |
802 | char int_buf[sizeof (long)]; | |
287c221d | 803 | unsigned int carsym_size, ptrsize, i; |
a927c32d | 804 | |
c33a0e41 | 805 | mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); |
b59f0276 ILT |
806 | if (mapdata == NULL) |
807 | return false; | |
287c221d | 808 | parsed_size = mapdata->parsed_size; |
a927c32d | 809 | bfd_release (abfd, (PTR) mapdata); /* Don't need it any more. */ |
4a81b561 | 810 | |
a927c32d ILT |
811 | if (bfd_read ((PTR) int_buf, 1, 4, abfd) != 4) |
812 | { | |
4002f18a ILT |
813 | if (bfd_get_error () != bfd_error_system_call) |
814 | bfd_set_error (bfd_error_malformed_archive); | |
a927c32d ILT |
815 | return false; |
816 | } | |
287c221d | 817 | /* It seems that all numeric information in a coff archive is always |
f58809fd | 818 | in big endian format, nomatter the host or target. */ |
b5b4294e | 819 | swap = bfd_getb32; |
a927c32d | 820 | nsymz = bfd_getb32 ((PTR) int_buf); |
287c221d | 821 | stringsize = parsed_size - (4 * nsymz) - 4; |
4a81b561 | 822 | |
287c221d PB |
823 | #if 1 |
824 | /* ... except that some archive formats are broken, and it may be our | |
286fd2f9 PB |
825 | fault - the i960 little endian coff sometimes has big and sometimes |
826 | little, because our tools changed. Here's a horrible hack to clean | |
287c221d | 827 | up the crap. */ |
a927c32d ILT |
828 | |
829 | if (stringsize > 0xfffff) | |
830 | { | |
287c221d | 831 | /* This looks dangerous, let's do it the other way around */ |
a927c32d | 832 | nsymz = bfd_getl32 ((PTR) int_buf); |
287c221d | 833 | stringsize = parsed_size - (4 * nsymz) - 4; |
b5b4294e | 834 | swap = bfd_getl32; |
a927c32d | 835 | } |
287c221d PB |
836 | #endif |
837 | ||
a927c32d ILT |
838 | /* The coff armap must be read sequentially. So we construct a |
839 | bsd-style one in core all at once, for simplicity. */ | |
840 | ||
287c221d PB |
841 | carsym_size = (nsymz * sizeof (carsym)); |
842 | ptrsize = (4 * nsymz); | |
843 | ||
a927c32d ILT |
844 | ardata->symdefs = (carsym *) bfd_zalloc (abfd, carsym_size + stringsize + 1); |
845 | if (ardata->symdefs == NULL) | |
a9713b91 | 846 | return false; |
287c221d PB |
847 | carsyms = ardata->symdefs; |
848 | stringbase = ((char *) ardata->symdefs) + carsym_size; | |
849 | ||
850 | /* Allocate and read in the raw offsets. */ | |
a927c32d ILT |
851 | raw_armap = (int *) bfd_alloc (abfd, ptrsize); |
852 | if (raw_armap == NULL) | |
a9713b91 | 853 | goto release_symdefs; |
a927c32d ILT |
854 | if (bfd_read ((PTR) raw_armap, 1, ptrsize, abfd) != ptrsize |
855 | || bfd_read ((PTR) stringbase, 1, stringsize, abfd) != stringsize) | |
856 | { | |
4002f18a ILT |
857 | if (bfd_get_error () != bfd_error_system_call) |
858 | bfd_set_error (bfd_error_malformed_archive); | |
a927c32d ILT |
859 | goto release_raw_armap; |
860 | } | |
287c221d PB |
861 | |
862 | /* OK, build the carsyms */ | |
a927c32d ILT |
863 | for (i = 0; i < nsymz; i++) |
864 | { | |
287c221d | 865 | rawptr = raw_armap + i; |
a927c32d | 866 | carsyms->file_offset = swap ((PTR) rawptr); |
287c221d | 867 | carsyms->name = stringbase; |
728472f1 | 868 | stringbase += strlen (stringbase) + 1; |
287c221d | 869 | carsyms++; |
a927c32d | 870 | } |
287c221d | 871 | *stringbase = 0; |
286fd2f9 | 872 | |
7b4eaa0e | 873 | ardata->symdef_count = nsymz; |
4a81b561 DHW |
874 | ardata->first_file_filepos = bfd_tell (abfd); |
875 | /* Pad to an even boundary if you have to */ | |
a927c32d | 876 | ardata->first_file_filepos += (ardata->first_file_filepos) % 2; |
6f715d66 | 877 | |
e5100743 | 878 | |
4a81b561 | 879 | bfd_has_map (abfd) = true; |
a927c32d | 880 | bfd_release (abfd, (PTR) raw_armap); |
e5100743 ILT |
881 | |
882 | ||
883 | /* Check for a second archive header (as used by PE) */ | |
884 | { | |
885 | struct areltdata *tmp; | |
886 | ||
887 | bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET); | |
c33a0e41 | 888 | tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd); |
e5100743 ILT |
889 | if (tmp != NULL) |
890 | { | |
891 | if (tmp->arch_header[0] == '/' | |
892 | && tmp->arch_header[1] == ' ') | |
893 | { | |
894 | ardata->first_file_filepos += | |
895 | (tmp->parsed_size + sizeof(struct ar_hdr) + 1) & ~1; | |
896 | } | |
897 | bfd_release (abfd, tmp); | |
898 | } | |
899 | } | |
900 | ||
287c221d PB |
901 | return true; |
902 | ||
a927c32d ILT |
903 | release_raw_armap: |
904 | bfd_release (abfd, (PTR) raw_armap); | |
905 | release_symdefs: | |
906 | bfd_release (abfd, (PTR) (ardata)->symdefs); | |
287c221d PB |
907 | return false; |
908 | } | |
909 | ||
910 | /* This routine can handle either coff-style or bsd-style armaps. | |
911 | Returns false on error, true otherwise */ | |
912 | ||
913 | boolean | |
914 | bfd_slurp_armap (abfd) | |
915 | bfd *abfd; | |
916 | { | |
917 | char nextname[17]; | |
a927c32d ILT |
918 | int i = bfd_read ((PTR) nextname, 1, 16, abfd); |
919 | ||
287c221d | 920 | if (i == 0) |
a927c32d | 921 | return true; |
287c221d | 922 | if (i != 16) |
a927c32d | 923 | return false; |
287c221d | 924 | |
4002f18a ILT |
925 | if (bfd_seek (abfd, (file_ptr) - 16, SEEK_CUR) != 0) |
926 | return false; | |
287c221d | 927 | |
aeef32f0 ILT |
928 | if (!strncmp (nextname, "__.SYMDEF ", 16) |
929 | || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */ | |
287c221d PB |
930 | return do_slurp_bsd_armap (abfd); |
931 | else if (!strncmp (nextname, "/ ", 16)) | |
932 | return do_slurp_coff_armap (abfd); | |
bf9884d4 ILT |
933 | else if (!strncmp (nextname, "/SYM64/ ", 16)) |
934 | { | |
935 | /* Irix 6 archive--must be recognized by code in elf64-mips.c. */ | |
936 | bfd_set_error (bfd_error_wrong_format); | |
937 | return false; | |
938 | } | |
287c221d PB |
939 | |
940 | bfd_has_map (abfd) = false; | |
4a81b561 DHW |
941 | return true; |
942 | } | |
4a81b561 | 943 | \f |
b5b4294e JG |
944 | /* Returns false on error, true otherwise */ |
945 | /* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the | |
a927c32d | 946 | header is in a slightly different order and the map name is '/'. |
b5b4294e | 947 | This flavour is used by hp300hpux. */ |
9a793780 SS |
948 | |
949 | #define HPUX_SYMDEF_COUNT_SIZE 2 | |
950 | ||
b5b4294e | 951 | boolean |
a927c32d | 952 | bfd_slurp_bsd_armap_f2 (abfd) |
b5b4294e JG |
953 | bfd *abfd; |
954 | { | |
955 | struct areltdata *mapdata; | |
956 | char nextname[17]; | |
9a793780 SS |
957 | unsigned int counter; |
958 | bfd_byte *raw_armap, *rbase; | |
b5b4294e JG |
959 | struct artdata *ardata = bfd_ardata (abfd); |
960 | char *stringbase; | |
961 | unsigned int stringsize; | |
9a793780 | 962 | carsym *set; |
a927c32d | 963 | int i = bfd_read ((PTR) nextname, 1, 16, abfd); |
b5b4294e JG |
964 | |
965 | if (i == 0) | |
966 | return true; | |
967 | if (i != 16) | |
968 | return false; | |
969 | ||
970 | /* The archive has at least 16 bytes in it */ | |
4002f18a ILT |
971 | if (bfd_seek (abfd, -16L, SEEK_CUR) != 0) |
972 | return false; | |
b5b4294e | 973 | |
aeef32f0 ILT |
974 | if (!strncmp (nextname, "__.SYMDEF ", 16) |
975 | || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */ | |
b5b4294e JG |
976 | return do_slurp_bsd_armap (abfd); |
977 | ||
978 | if (strncmp (nextname, "/ ", 16)) | |
979 | { | |
980 | bfd_has_map (abfd) = false; | |
981 | return true; | |
982 | } | |
983 | ||
c33a0e41 | 984 | mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); |
b59f0276 ILT |
985 | if (mapdata == NULL) |
986 | return false; | |
b5b4294e | 987 | |
9a793780 | 988 | raw_armap = (bfd_byte *) bfd_zalloc (abfd, mapdata->parsed_size); |
b5b4294e JG |
989 | if (raw_armap == NULL) |
990 | { | |
b5b4294e | 991 | byebye: |
a927c32d | 992 | bfd_release (abfd, (PTR) mapdata); |
b5b4294e JG |
993 | return false; |
994 | } | |
995 | ||
a927c32d | 996 | if (bfd_read ((PTR) raw_armap, 1, mapdata->parsed_size, abfd) != |
b5b4294e JG |
997 | mapdata->parsed_size) |
998 | { | |
4002f18a ILT |
999 | if (bfd_get_error () != bfd_error_system_call) |
1000 | bfd_set_error (bfd_error_malformed_archive); | |
b5b4294e | 1001 | byebyebye: |
a927c32d | 1002 | bfd_release (abfd, (PTR) raw_armap); |
b5b4294e JG |
1003 | goto byebye; |
1004 | } | |
1005 | ||
a927c32d | 1006 | ardata->symdef_count = bfd_h_get_16 (abfd, (PTR) raw_armap); |
b5b4294e | 1007 | |
9a793780 SS |
1008 | if (ardata->symdef_count * BSD_SYMDEF_SIZE |
1009 | > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE) | |
b5b4294e JG |
1010 | { |
1011 | /* Probably we're using the wrong byte ordering. */ | |
25057836 | 1012 | bfd_set_error (bfd_error_wrong_format); |
b5b4294e JG |
1013 | goto byebyebye; |
1014 | } | |
1015 | ||
1016 | ardata->cache = 0; | |
1017 | ||
9a793780 | 1018 | stringsize = bfd_h_get_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE); |
b5b4294e | 1019 | /* skip sym count and string sz */ |
9a793780 SS |
1020 | stringbase = ((char *) raw_armap |
1021 | + HPUX_SYMDEF_COUNT_SIZE | |
1022 | + BSD_STRING_COUNT_SIZE); | |
1023 | rbase = (bfd_byte *) stringbase + stringsize; | |
1024 | ardata->symdefs = (carsym *) bfd_alloc (abfd, | |
1025 | (ardata->symdef_count | |
1026 | * BSD_SYMDEF_SIZE)); | |
9783e04a | 1027 | if (!ardata->symdefs) |
a9713b91 | 1028 | return false; |
9a793780 SS |
1029 | |
1030 | for (counter = 0, set = ardata->symdefs; | |
1031 | counter < ardata->symdef_count; | |
1032 | counter++, set++, rbase += BSD_SYMDEF_SIZE) | |
b5b4294e | 1033 | { |
9a793780 SS |
1034 | set->name = bfd_h_get_32 (abfd, rbase) + stringbase; |
1035 | set->file_offset = bfd_h_get_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE); | |
b5b4294e JG |
1036 | } |
1037 | ||
1038 | ardata->first_file_filepos = bfd_tell (abfd); | |
1039 | /* Pad to an even boundary if you have to */ | |
a927c32d | 1040 | ardata->first_file_filepos += (ardata->first_file_filepos) % 2; |
b5b4294e JG |
1041 | /* FIXME, we should provide some way to free raw_ardata when |
1042 | we are done using the strings from it. For now, it seems | |
1043 | to be allocated on an obstack anyway... */ | |
1044 | bfd_has_map (abfd) = true; | |
1045 | return true; | |
1046 | } | |
1047 | \f | |
4a81b561 DHW |
1048 | /** Extended name table. |
1049 | ||
6f715d66 SC |
1050 | Normally archives support only 14-character filenames. |
1051 | ||
1052 | Intel has extended the format: longer names are stored in a special | |
1053 | element (the first in the archive, or second if there is an armap); | |
1054 | the name in the ar_hdr is replaced by <space><index into filename | |
286fd2f9 | 1055 | element>. Index is the P.R. of an int (decimal). Data General have |
6f715d66 | 1056 | extended the format by using the prefix // for the special element */ |
4a81b561 DHW |
1057 | |
1058 | /* Returns false on error, true otherwise */ | |
1059 | boolean | |
1060 | _bfd_slurp_extended_name_table (abfd) | |
1061 | bfd *abfd; | |
1062 | { | |
1063 | char nextname[17]; | |
1064 | struct areltdata *namedata; | |
1065 | ||
fc723380 JG |
1066 | /* FIXME: Formatting sucks here, and in case of failure of BFD_READ, |
1067 | we probably don't want to return true. */ | |
e5100743 | 1068 | bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET); |
a927c32d ILT |
1069 | if (bfd_read ((PTR) nextname, 1, 16, abfd) == 16) |
1070 | { | |
4002f18a ILT |
1071 | if (bfd_seek (abfd, (file_ptr) - 16, SEEK_CUR) != 0) |
1072 | return false; | |
4a81b561 | 1073 | |
a927c32d ILT |
1074 | if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 && |
1075 | strncmp (nextname, "// ", 16) != 0) | |
6f715d66 | 1076 | { |
a927c32d ILT |
1077 | bfd_ardata (abfd)->extended_names = NULL; |
1078 | return true; | |
1079 | } | |
4a81b561 | 1080 | |
c33a0e41 | 1081 | namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd); |
a927c32d ILT |
1082 | if (namedata == NULL) |
1083 | return false; | |
4a81b561 | 1084 | |
a927c32d ILT |
1085 | bfd_ardata (abfd)->extended_names = |
1086 | bfd_zalloc (abfd, namedata->parsed_size); | |
1087 | if (bfd_ardata (abfd)->extended_names == NULL) | |
1088 | { | |
a927c32d ILT |
1089 | byebye: |
1090 | bfd_release (abfd, (PTR) namedata); | |
1091 | return false; | |
1092 | } | |
4a81b561 | 1093 | |
a927c32d ILT |
1094 | if (bfd_read ((PTR) bfd_ardata (abfd)->extended_names, 1, |
1095 | namedata->parsed_size, abfd) != namedata->parsed_size) | |
1096 | { | |
4002f18a ILT |
1097 | if (bfd_get_error () != bfd_error_system_call) |
1098 | bfd_set_error (bfd_error_malformed_archive); | |
a927c32d ILT |
1099 | bfd_release (abfd, (PTR) (bfd_ardata (abfd)->extended_names)); |
1100 | bfd_ardata (abfd)->extended_names = NULL; | |
1101 | goto byebye; | |
1102 | } | |
1103 | ||
1104 | /* Since the archive is supposed to be printable if it contains | |
1105 | text, the entries in the list are newline-padded, not null | |
1106 | padded. In SVR4-style archives, the names also have a | |
e5100743 ILT |
1107 | trailing '/'. DOS/NT created archive often have \ in them |
1108 | We'll fix all problems here.. */ | |
6f715d66 SC |
1109 | { |
1110 | char *temp = bfd_ardata (abfd)->extended_names; | |
287c221d | 1111 | char *limit = temp + namedata->parsed_size; |
e5100743 | 1112 | for (; temp < limit; ++temp) { |
9a793780 | 1113 | if (*temp == '\012') |
287c221d | 1114 | temp[temp[-1] == '/' ? -1 : 0] = '\0'; |
e5100743 ILT |
1115 | if (*temp == '\\') |
1116 | *temp = '/'; | |
1117 | } | |
6f715d66 | 1118 | } |
a927c32d ILT |
1119 | |
1120 | /* Pad to an even boundary if you have to */ | |
1121 | bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd); | |
1122 | bfd_ardata (abfd)->first_file_filepos += | |
1123 | (bfd_ardata (abfd)->first_file_filepos) % 2; | |
1124 | ||
1125 | /* FIXME, we can't release namedata here because it was allocated | |
1126 | below extended_names on the obstack... */ | |
1127 | /* bfd_release (abfd, namedata); */ | |
1128 | } | |
4a81b561 DHW |
1129 | return true; |
1130 | } | |
1131 | ||
286fd2f9 PB |
1132 | #ifdef VMS |
1133 | ||
1134 | /* Return a copy of the stuff in the filename between any :]> and a | |
1135 | semicolon */ | |
b59f0276 | 1136 | static const char * |
e5100743 ILT |
1137 | normalize (abfd, file) |
1138 | bfd *abfd; | |
b59f0276 | 1139 | const char *file; |
286fd2f9 PB |
1140 | { |
1141 | CONST char *first; | |
1142 | CONST char *last; | |
1143 | char *copy; | |
1144 | ||
a927c32d ILT |
1145 | first = file + strlen (file) - 1; |
1146 | last = first + 1; | |
1147 | ||
1148 | while (first != file) | |
1149 | { | |
1150 | if (*first == ';') | |
1151 | last = first; | |
1152 | if (*first == ':' || *first == ']' || *first == '>') | |
1153 | { | |
1154 | first++; | |
1155 | break; | |
1156 | } | |
1157 | first--; | |
286fd2f9 | 1158 | } |
286fd2f9 | 1159 | |
e5100743 ILT |
1160 | copy = (char *) bfd_alloc (abfd, last - first + 1); |
1161 | if (copy == NULL) | |
a9713b91 | 1162 | return NULL; |
a15691a5 | 1163 | |
a927c32d ILT |
1164 | memcpy (copy, first, last - first); |
1165 | copy[last - first] = 0; | |
286fd2f9 PB |
1166 | |
1167 | return copy; | |
1168 | } | |
1169 | ||
1170 | #else | |
b59f0276 | 1171 | static const char * |
e5100743 ILT |
1172 | normalize (abfd, file) |
1173 | bfd *abfd; | |
b59f0276 | 1174 | const char *file; |
4a81b561 | 1175 | { |
e5100743 | 1176 | const char *filename = strrchr (file, '/'); |
286fd2f9 | 1177 | |
a927c32d ILT |
1178 | if (filename != (char *) NULL) |
1179 | filename++; | |
1180 | else | |
1181 | filename = file; | |
286fd2f9 | 1182 | return filename; |
4a81b561 | 1183 | } |
286fd2f9 | 1184 | #endif |
a927c32d | 1185 | |
cd9782e8 ILT |
1186 | /* Build a BFD style extended name table. */ |
1187 | ||
1188 | boolean | |
1189 | _bfd_archive_bsd_construct_extended_name_table (abfd, tabloc, tablen, name) | |
1190 | bfd *abfd; | |
1191 | char **tabloc; | |
1192 | bfd_size_type *tablen; | |
1193 | const char **name; | |
1194 | { | |
1195 | *name = "ARFILENAMES/"; | |
1196 | return _bfd_construct_extended_name_table (abfd, false, tabloc, tablen); | |
1197 | } | |
1198 | ||
1199 | /* Build an SVR4 style extended name table. */ | |
1200 | ||
1201 | boolean | |
1202 | _bfd_archive_coff_construct_extended_name_table (abfd, tabloc, tablen, name) | |
1203 | bfd *abfd; | |
1204 | char **tabloc; | |
1205 | bfd_size_type *tablen; | |
1206 | const char **name; | |
1207 | { | |
1208 | *name = "//"; | |
1209 | return _bfd_construct_extended_name_table (abfd, true, tabloc, tablen); | |
1210 | } | |
1211 | ||
a927c32d ILT |
1212 | /* Follows archive_head and produces an extended name table if |
1213 | necessary. Returns (in tabloc) a pointer to an extended name | |
1214 | table, and in tablen the length of the table. If it makes an entry | |
1215 | it clobbers the filename so that the element may be written without | |
1216 | further massage. Returns true if it ran successfully, false if | |
1217 | something went wrong. A successful return may still involve a | |
1218 | zero-length tablen! */ | |
1219 | ||
cd9782e8 ILT |
1220 | boolean |
1221 | _bfd_construct_extended_name_table (abfd, trailing_slash, tabloc, tablen) | |
b59f0276 | 1222 | bfd *abfd; |
cd9782e8 | 1223 | boolean trailing_slash; |
b59f0276 | 1224 | char **tabloc; |
cd9782e8 | 1225 | bfd_size_type *tablen; |
4a81b561 | 1226 | { |
9846338e SC |
1227 | unsigned int maxname = abfd->xvec->ar_max_namelen; |
1228 | unsigned int total_namelen = 0; | |
1229 | bfd *current; | |
1230 | char *strptr; | |
4a81b561 | 1231 | |
9846338e | 1232 | *tablen = 0; |
a927c32d | 1233 | |
9846338e | 1234 | /* Figure out how long the table should be */ |
a927c32d ILT |
1235 | for (current = abfd->archive_head; current != NULL; current = current->next) |
1236 | { | |
e5100743 | 1237 | const char *normal; |
a15691a5 DM |
1238 | unsigned int thislen; |
1239 | ||
e5100743 ILT |
1240 | normal = normalize (current, current->filename); |
1241 | if (normal == NULL) | |
1242 | return false; | |
1243 | ||
a15691a5 | 1244 | thislen = strlen (normal); |
27b1ec94 ILT |
1245 | |
1246 | if (thislen > maxname | |
1247 | && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) | |
1248 | thislen = maxname; | |
1249 | ||
a927c32d | 1250 | if (thislen > maxname) |
cd9782e8 ILT |
1251 | { |
1252 | /* Add one to leave room for \n. */ | |
1253 | total_namelen += thislen + 1; | |
1254 | if (trailing_slash) | |
1255 | { | |
1256 | /* Leave room for trailing slash. */ | |
1257 | ++total_namelen; | |
1258 | } | |
1259 | } | |
e5100743 ILT |
1260 | else |
1261 | { | |
1262 | struct ar_hdr *hdr = arch_hdr (current); | |
1263 | if (strncmp (normal, hdr->ar_name, thislen) != 0 | |
1264 | || (thislen < sizeof hdr->ar_name | |
1265 | && hdr->ar_name[thislen] != ar_padchar (current))) | |
1266 | { | |
1267 | /* Must have been using extended format even though it | |
1268 | didn't need to. Fix it to use normal format. */ | |
1269 | memcpy (hdr->ar_name, normal, thislen); | |
1270 | if (thislen < maxname | |
1271 | || (thislen == maxname && thislen < sizeof hdr->ar_name)) | |
1272 | hdr->ar_name[thislen] = ar_padchar (current); | |
1273 | } | |
1274 | } | |
a927c32d | 1275 | } |
4a81b561 | 1276 | |
a927c32d ILT |
1277 | if (total_namelen == 0) |
1278 | return true; | |
4a81b561 | 1279 | |
a927c32d ILT |
1280 | *tabloc = bfd_zalloc (abfd, total_namelen); |
1281 | if (*tabloc == NULL) | |
a9713b91 | 1282 | return false; |
4a81b561 | 1283 | |
9846338e SC |
1284 | *tablen = total_namelen; |
1285 | strptr = *tabloc; | |
1286 | ||
1287 | for (current = abfd->archive_head; current != NULL; current = | |
a927c32d ILT |
1288 | current->next) |
1289 | { | |
e5100743 | 1290 | const char *normal; |
a15691a5 DM |
1291 | unsigned int thislen; |
1292 | ||
e5100743 ILT |
1293 | normal = normalize (current, current->filename); |
1294 | if (normal == NULL) | |
1295 | return false; | |
1296 | ||
a15691a5 | 1297 | thislen = strlen (normal); |
a927c32d | 1298 | if (thislen > maxname) |
9846338e | 1299 | { |
a927c32d ILT |
1300 | /* Works for now; may need to be re-engineered if we |
1301 | encounter an oddball archive format and want to | |
1302 | generalise this hack. */ | |
1303 | struct ar_hdr *hdr = arch_hdr (current); | |
1304 | strcpy (strptr, normal); | |
cd9782e8 ILT |
1305 | if (! trailing_slash) |
1306 | strptr[thislen] = '\012'; | |
1307 | else | |
1308 | { | |
1309 | strptr[thislen] = '/'; | |
1310 | strptr[thislen + 1] = '\012'; | |
1311 | } | |
25057836 | 1312 | hdr->ar_name[0] = ar_padchar (current); |
a927c32d ILT |
1313 | /* We know there will always be enough room (one of the few |
1314 | cases where you may safely use sprintf). */ | |
1315 | sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc)); | |
1316 | /* Kinda Kludgy. We should just use the returned value of | |
1317 | sprintf but not all implementations get this right */ | |
1318 | { | |
1319 | char *temp = hdr->ar_name + 2; | |
1320 | for (; temp < hdr->ar_name + maxname; temp++) | |
1321 | if (*temp == '\0') | |
1322 | *temp = ' '; | |
1323 | } | |
1324 | strptr += thislen + 1; | |
cd9782e8 ILT |
1325 | if (trailing_slash) |
1326 | ++strptr; | |
4a81b561 DHW |
1327 | } |
1328 | } | |
1329 | ||
9846338e | 1330 | return true; |
4a81b561 DHW |
1331 | } |
1332 | \f | |
1333 | /** A couple of functions for creating ar_hdrs */ | |
1334 | ||
a927c32d ILT |
1335 | /* Takes a filename, returns an arelt_data for it, or NULL if it can't |
1336 | make one. The filename must refer to a filename in the filesystem. | |
1337 | The filename field of the ar_hdr will NOT be initialized */ | |
4a81b561 | 1338 | |
b59f0276 | 1339 | static struct areltdata * |
a927c32d ILT |
1340 | bfd_ar_hdr_from_filesystem (abfd, filename) |
1341 | bfd *abfd; | |
b59f0276 | 1342 | const char *filename; |
4a81b561 DHW |
1343 | { |
1344 | struct stat status; | |
1345 | struct areltdata *ared; | |
1346 | struct ar_hdr *hdr; | |
1347 | char *temp, *temp1; | |
1348 | ||
a927c32d ILT |
1349 | if (stat (filename, &status) != 0) |
1350 | { | |
25057836 | 1351 | bfd_set_error (bfd_error_system_call); |
a927c32d ILT |
1352 | return NULL; |
1353 | } | |
4a81b561 | 1354 | |
a927c32d ILT |
1355 | ared = (struct areltdata *) bfd_zalloc (abfd, sizeof (struct ar_hdr) + |
1356 | sizeof (struct areltdata)); | |
1357 | if (ared == NULL) | |
a9713b91 | 1358 | return NULL; |
4a81b561 DHW |
1359 | hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata)); |
1360 | ||
1361 | /* ar headers are space padded, not null padded! */ | |
25057836 | 1362 | memset ((PTR) hdr, ' ', sizeof (struct ar_hdr)); |
728472f1 | 1363 | |
4a81b561 | 1364 | strncpy (hdr->ar_fmag, ARFMAG, 2); |
a927c32d | 1365 | |
4a81b561 | 1366 | /* Goddamned sprintf doesn't permit MAXIMUM field lengths */ |
9a793780 SS |
1367 | sprintf ((hdr->ar_date), "%-12ld", (long) status.st_mtime); |
1368 | sprintf ((hdr->ar_uid), "%ld", (long) status.st_uid); | |
1369 | sprintf ((hdr->ar_gid), "%ld", (long) status.st_gid); | |
1370 | sprintf ((hdr->ar_mode), "%-8o", (unsigned int) status.st_mode); | |
1371 | sprintf ((hdr->ar_size), "%-10ld", (long) status.st_size); | |
4a81b561 DHW |
1372 | /* Correct for a lossage in sprintf whereby it null-terminates. I cannot |
1373 | understand how these C losers could design such a ramshackle bunch of | |
1374 | IO operations */ | |
1375 | temp = (char *) hdr; | |
1376 | temp1 = temp + sizeof (struct ar_hdr) - 2; | |
a927c32d ILT |
1377 | for (; temp < temp1; temp++) |
1378 | { | |
1379 | if (*temp == '\0') | |
1380 | *temp = ' '; | |
1381 | } | |
4a81b561 DHW |
1382 | strncpy (hdr->ar_fmag, ARFMAG, 2); |
1383 | ared->parsed_size = status.st_size; | |
1384 | ared->arch_header = (char *) hdr; | |
1385 | ||
1386 | return ared; | |
1387 | } | |
1388 | ||
4ee249da | 1389 | /* This is magic required by the "ar" program. Since it's |
a927c32d ILT |
1390 | undocumented, it's undocumented. You may think that it would take |
1391 | a strong stomach to write this, and it does, but it takes even a | |
1392 | stronger stomach to try to code around such a thing! */ | |
4ee249da | 1393 | |
4a81b561 | 1394 | struct ar_hdr * |
b59f0276 ILT |
1395 | bfd_special_undocumented_glue (abfd, filename) |
1396 | bfd *abfd; | |
1397 | char *filename; | |
4a81b561 | 1398 | { |
37217060 PB |
1399 | struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename); |
1400 | if (ar_elt == NULL) | |
a927c32d | 1401 | return NULL; |
37217060 | 1402 | return (struct ar_hdr *) ar_elt->arch_header; |
4a81b561 DHW |
1403 | } |
1404 | ||
1405 | ||
1406 | /* Analogous to stat call */ | |
1407 | int | |
1408 | bfd_generic_stat_arch_elt (abfd, buf) | |
1409 | bfd *abfd; | |
1410 | struct stat *buf; | |
1411 | { | |
1412 | struct ar_hdr *hdr; | |
1413 | char *aloser; | |
a927c32d ILT |
1414 | |
1415 | if (abfd->arelt_data == NULL) | |
1416 | { | |
25057836 | 1417 | bfd_set_error (bfd_error_invalid_operation); |
a927c32d ILT |
1418 | return -1; |
1419 | } | |
1420 | ||
4a81b561 DHW |
1421 | hdr = arch_hdr (abfd); |
1422 | ||
1423 | #define foo(arelt, stelt, size) \ | |
1424 | buf->stelt = strtol (hdr->arelt, &aloser, size); \ | |
1425 | if (aloser == hdr->arelt) return -1; | |
a927c32d | 1426 | |
4a81b561 DHW |
1427 | foo (ar_date, st_mtime, 10); |
1428 | foo (ar_uid, st_uid, 10); | |
1429 | foo (ar_gid, st_gid, 10); | |
1430 | foo (ar_mode, st_mode, 8); | |
b5b4294e JG |
1431 | |
1432 | buf->st_size = arch_eltdata (abfd)->parsed_size; | |
4a81b561 DHW |
1433 | |
1434 | return 0; | |
1435 | } | |
1436 | ||
4a81b561 | 1437 | void |
9846338e SC |
1438 | bfd_dont_truncate_arname (abfd, pathname, arhdr) |
1439 | bfd *abfd; | |
8b0328db | 1440 | CONST char *pathname; |
9846338e | 1441 | char *arhdr; |
4a81b561 | 1442 | { |
fc723380 | 1443 | /* FIXME: This interacts unpleasantly with ar's quick-append option. |
9846338e SC |
1444 | Fortunately ic960 users will never use that option. Fixing this |
1445 | is very hard; fortunately I know how to do it and will do so once | |
1446 | intel's release is out the door. */ | |
a927c32d | 1447 | |
9846338e | 1448 | struct ar_hdr *hdr = (struct ar_hdr *) arhdr; |
ae115e51 | 1449 | size_t length; |
e5100743 | 1450 | const char *filename; |
ae115e51 | 1451 | size_t maxlen = ar_maxnamelen (abfd); |
4a81b561 | 1452 | |
27b1ec94 ILT |
1453 | if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0) |
1454 | { | |
1455 | bfd_bsd_truncate_arname (abfd, pathname, arhdr); | |
1456 | return; | |
1457 | } | |
1458 | ||
e5100743 ILT |
1459 | filename = normalize (abfd, pathname); |
1460 | if (filename == NULL) | |
1461 | { | |
1462 | /* FIXME */ | |
1463 | abort (); | |
1464 | } | |
1465 | ||
9846338e | 1466 | length = strlen (filename); |
4a81b561 | 1467 | |
9846338e SC |
1468 | if (length <= maxlen) |
1469 | memcpy (hdr->ar_name, filename, length); | |
1470 | ||
da6c4a8b ILT |
1471 | /* Add the padding character if there is room for it. */ |
1472 | if (length < maxlen | |
1473 | || (length == maxlen && length < sizeof hdr->ar_name)) | |
a927c32d | 1474 | (hdr->ar_name)[length] = ar_padchar (abfd); |
4a81b561 DHW |
1475 | } |
1476 | ||
1477 | void | |
1478 | bfd_bsd_truncate_arname (abfd, pathname, arhdr) | |
1479 | bfd *abfd; | |
8b0328db | 1480 | CONST char *pathname; |
4a81b561 DHW |
1481 | char *arhdr; |
1482 | { | |
1483 | struct ar_hdr *hdr = (struct ar_hdr *) arhdr; | |
1484 | int length; | |
8b0328db | 1485 | CONST char *filename = strrchr (pathname, '/'); |
4a81b561 DHW |
1486 | int maxlen = ar_maxnamelen (abfd); |
1487 | ||
4a81b561 DHW |
1488 | if (filename == NULL) |
1489 | filename = pathname; | |
1490 | else | |
1491 | ++filename; | |
1492 | ||
1493 | length = strlen (filename); | |
1494 | ||
1495 | if (length <= maxlen) | |
1496 | memcpy (hdr->ar_name, filename, length); | |
a927c32d ILT |
1497 | else |
1498 | { | |
1499 | /* pathname: meet procrustes */ | |
1500 | memcpy (hdr->ar_name, filename, maxlen); | |
1501 | length = maxlen; | |
1502 | } | |
4a81b561 | 1503 | |
b59f0276 ILT |
1504 | if (length < maxlen) |
1505 | (hdr->ar_name)[length] = ar_padchar (abfd); | |
4a81b561 DHW |
1506 | } |
1507 | ||
1508 | /* Store name into ar header. Truncates the name to fit. | |
1509 | 1> strip pathname to be just the basename. | |
1510 | 2> if it's short enuf to fit, stuff it in. | |
1511 | 3> If it doesn't end with .o, truncate it to fit | |
a927c32d ILT |
1512 | 4> truncate it before the .o, append .o, stuff THAT in. */ |
1513 | ||
1514 | /* This is what gnu ar does. It's better but incompatible with the | |
1515 | bsd ar. */ | |
4a81b561 | 1516 | |
4a81b561 DHW |
1517 | void |
1518 | bfd_gnu_truncate_arname (abfd, pathname, arhdr) | |
1519 | bfd *abfd; | |
8b0328db | 1520 | CONST char *pathname; |
4a81b561 DHW |
1521 | char *arhdr; |
1522 | { | |
1523 | struct ar_hdr *hdr = (struct ar_hdr *) arhdr; | |
1524 | int length; | |
8b0328db | 1525 | CONST char *filename = strrchr (pathname, '/'); |
4a81b561 | 1526 | int maxlen = ar_maxnamelen (abfd); |
a927c32d | 1527 | |
4a81b561 DHW |
1528 | if (filename == NULL) |
1529 | filename = pathname; | |
1530 | else | |
1531 | ++filename; | |
1532 | ||
1533 | length = strlen (filename); | |
1534 | ||
1535 | if (length <= maxlen) | |
1536 | memcpy (hdr->ar_name, filename, length); | |
a927c32d ILT |
1537 | else |
1538 | { /* pathname: meet procrustes */ | |
1539 | memcpy (hdr->ar_name, filename, maxlen); | |
1540 | if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) | |
1541 | { | |
1542 | hdr->ar_name[maxlen - 2] = '.'; | |
1543 | hdr->ar_name[maxlen - 1] = 'o'; | |
1544 | } | |
1545 | length = maxlen; | |
4a81b561 | 1546 | } |
4a81b561 | 1547 | |
b59f0276 ILT |
1548 | if (length < 16) |
1549 | (hdr->ar_name)[length] = ar_padchar (abfd); | |
4a81b561 DHW |
1550 | } |
1551 | \f | |
6724ff46 | 1552 | /* The BFD is open for write and has its format set to bfd_archive */ |
a927c32d | 1553 | |
4a81b561 DHW |
1554 | boolean |
1555 | _bfd_write_archive_contents (arch) | |
1556 | bfd *arch; | |
1557 | { | |
1558 | bfd *current; | |
1559 | char *etable = NULL; | |
cd9782e8 ILT |
1560 | bfd_size_type elength = 0; |
1561 | const char *ename = NULL; | |
91c9d029 | 1562 | boolean makemap = bfd_has_map (arch); |
4a81b561 | 1563 | boolean hasobjects = false; /* if no .o's, don't bother to make a map */ |
4002f18a | 1564 | bfd_size_type wrote; |
4a81b561 | 1565 | unsigned int i; |
b5b4294e | 1566 | int tries; |
4a81b561 | 1567 | |
4a81b561 DHW |
1568 | /* Verify the viability of all entries; if any of them live in the |
1569 | filesystem (as opposed to living in an archive open for input) | |
a927c32d ILT |
1570 | then construct a fresh ar_hdr for them. */ |
1571 | for (current = arch->archive_head; current; current = current->next) | |
1572 | { | |
1573 | if (bfd_write_p (current)) | |
1574 | { | |
25057836 | 1575 | bfd_set_error (bfd_error_invalid_operation); |
a927c32d ILT |
1576 | return false; |
1577 | } | |
1578 | if (!current->arelt_data) | |
1579 | { | |
1580 | current->arelt_data = | |
1581 | (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename); | |
1582 | if (!current->arelt_data) | |
1583 | return false; | |
1584 | ||
1585 | /* Put in the file name */ | |
1586 | BFD_SEND (arch, _bfd_truncate_arname, (arch, | |
1587 | current->filename, | |
1588 | (char *) arch_hdr (current))); | |
1589 | } | |
4a81b561 | 1590 | |
dfe09c49 | 1591 | if (makemap && ! hasobjects) |
a927c32d ILT |
1592 | { /* don't bother if we won't make a map! */ |
1593 | if ((bfd_check_format (current, bfd_object)) | |
4a81b561 | 1594 | #if 0 /* FIXME -- these are not set correctly */ |
a927c32d | 1595 | && ((bfd_get_file_flags (current) & HAS_SYMS)) |
4a81b561 | 1596 | #endif |
a927c32d ILT |
1597 | ) |
1598 | hasobjects = true; | |
1599 | } | |
4a81b561 | 1600 | } |
4a81b561 | 1601 | |
cd9782e8 ILT |
1602 | if (!BFD_SEND (arch, _bfd_construct_extended_name_table, |
1603 | (arch, &etable, &elength, &ename))) | |
4a81b561 DHW |
1604 | return false; |
1605 | ||
4002f18a ILT |
1606 | if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0) |
1607 | return false; | |
9846338e | 1608 | #ifdef GNU960 |
4002f18a | 1609 | wrote = bfd_write (BFD_GNU960_ARMAG (arch), 1, SARMAG, arch); |
9846338e | 1610 | #else |
4002f18a | 1611 | wrote = bfd_write (ARMAG, 1, SARMAG, arch); |
9846338e | 1612 | #endif |
4002f18a ILT |
1613 | if (wrote != SARMAG) |
1614 | return false; | |
4a81b561 | 1615 | |
a927c32d ILT |
1616 | if (makemap && hasobjects) |
1617 | { | |
c53fac12 | 1618 | if (_bfd_compute_and_write_armap (arch, elength) != true) |
a927c32d ILT |
1619 | return false; |
1620 | } | |
4a81b561 | 1621 | |
a927c32d ILT |
1622 | if (elength != 0) |
1623 | { | |
1624 | struct ar_hdr hdr; | |
1625 | ||
1626 | memset ((char *) (&hdr), 0, sizeof (struct ar_hdr)); | |
cd9782e8 | 1627 | strcpy (hdr.ar_name, ename); |
e5100743 ILT |
1628 | /* Round size up to even number in archive header. */ |
1629 | sprintf (&(hdr.ar_size[0]), "%-10d", | |
1630 | (int) ((elength + 1) & ~1)); | |
aeef32f0 | 1631 | strncpy (hdr.ar_fmag, ARFMAG, 2); |
a927c32d ILT |
1632 | for (i = 0; i < sizeof (struct ar_hdr); i++) |
1633 | if (((char *) (&hdr))[i] == '\0') | |
1634 | (((char *) (&hdr))[i]) = ' '; | |
4002f18a ILT |
1635 | if ((bfd_write ((char *) &hdr, 1, sizeof (struct ar_hdr), arch) |
1636 | != sizeof (struct ar_hdr)) | |
1637 | || bfd_write (etable, 1, elength, arch) != elength) | |
1638 | return false; | |
a927c32d | 1639 | if ((elength % 2) == 1) |
4002f18a ILT |
1640 | { |
1641 | if (bfd_write ("\012", 1, 1, arch) != 1) | |
1642 | return false; | |
1643 | } | |
4a81b561 | 1644 | } |
a927c32d ILT |
1645 | |
1646 | for (current = arch->archive_head; current; current = current->next) | |
1647 | { | |
1648 | char buffer[DEFAULT_BUFFERSIZE]; | |
1649 | unsigned int remaining = arelt_size (current); | |
1650 | struct ar_hdr *hdr = arch_hdr (current); | |
1651 | ||
1652 | /* write ar header */ | |
1653 | if (bfd_write ((char *) hdr, 1, sizeof (*hdr), arch) != sizeof (*hdr)) | |
25057836 | 1654 | return false; |
a927c32d | 1655 | if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) |
25057836 | 1656 | return false; |
a927c32d | 1657 | while (remaining) |
0452b5aa SC |
1658 | { |
1659 | unsigned int amt = DEFAULT_BUFFERSIZE; | |
a927c32d | 1660 | if (amt > remaining) |
0452b5aa | 1661 | amt = remaining; |
446c5af7 | 1662 | errno = 0; |
a927c32d ILT |
1663 | if (bfd_read (buffer, amt, 1, current) != amt) |
1664 | { | |
25057836 JL |
1665 | if (bfd_get_error () != bfd_error_system_call) |
1666 | bfd_set_error (bfd_error_malformed_archive); | |
286fd2f9 | 1667 | return false; |
a927c32d ILT |
1668 | } |
1669 | if (bfd_write (buffer, amt, 1, arch) != amt) | |
25057836 | 1670 | return false; |
0452b5aa SC |
1671 | remaining -= amt; |
1672 | } | |
a927c32d | 1673 | if ((arelt_size (current) % 2) == 1) |
4002f18a ILT |
1674 | { |
1675 | if (bfd_write ("\012", 1, 1, arch) != 1) | |
1676 | return false; | |
1677 | } | |
a927c32d | 1678 | } |
b5b4294e | 1679 | |
cd9782e8 | 1680 | if (makemap && hasobjects) |
a927c32d | 1681 | { |
cd9782e8 ILT |
1682 | /* Verify the timestamp in the archive file. If it would not be |
1683 | accepted by the linker, rewrite it until it would be. If | |
1684 | anything odd happens, break out and just return. (The | |
1685 | Berkeley linker checks the timestamp and refuses to read the | |
1686 | table-of-contents if it is >60 seconds less than the file's | |
1687 | modified-time. That painful hack requires this painful hack. */ | |
1688 | tries = 1; | |
1689 | do | |
1690 | { | |
1691 | if (bfd_update_armap_timestamp (arch)) | |
1692 | break; | |
c53fac12 ILT |
1693 | (*_bfd_error_handler) |
1694 | ("Warning: writing archive was slow: rewriting timestamp\n"); | |
cd9782e8 ILT |
1695 | } |
1696 | while (++tries < 6); | |
a927c32d | 1697 | } |
b5b4294e JG |
1698 | |
1699 | return true; | |
4a81b561 DHW |
1700 | } |
1701 | \f | |
1702 | /* Note that the namidx for the first symbol is 0 */ | |
1703 | ||
c53fac12 ILT |
1704 | boolean |
1705 | _bfd_compute_and_write_armap (arch, elength) | |
4a81b561 DHW |
1706 | bfd *arch; |
1707 | unsigned int elength; | |
1708 | { | |
326e32d7 | 1709 | char *first_name = NULL; |
a927c32d ILT |
1710 | bfd *current; |
1711 | file_ptr elt_no = 0; | |
326e32d7 | 1712 | struct orl *map = NULL; |
5ee3886b | 1713 | int orl_max = 1024; /* fine initial default */ |
a927c32d ILT |
1714 | int orl_count = 0; |
1715 | int stridx = 0; /* string index */ | |
5ee3886b | 1716 | asymbol **syms = NULL; |
326e32d7 | 1717 | long syms_max = 0; |
5ee3886b | 1718 | boolean ret; |
a927c32d ILT |
1719 | |
1720 | /* Dunno if this is the best place for this info... */ | |
1721 | if (elength != 0) | |
1722 | elength += sizeof (struct ar_hdr); | |
1723 | elength += elength % 2; | |
1724 | ||
64d5f5d0 | 1725 | map = (struct orl *) bfd_malloc (orl_max * sizeof (struct orl)); |
a927c32d | 1726 | if (map == NULL) |
64d5f5d0 | 1727 | goto error_return; |
a927c32d | 1728 | |
5ee3886b ILT |
1729 | /* We put the symbol names on the arch obstack, and then discard |
1730 | them when done. */ | |
1731 | first_name = bfd_alloc (arch, 1); | |
1732 | if (first_name == NULL) | |
a9713b91 | 1733 | goto error_return; |
5ee3886b | 1734 | |
a927c32d ILT |
1735 | /* Drop all the files called __.SYMDEF, we're going to make our |
1736 | own */ | |
1737 | while (arch->archive_head && | |
1738 | strcmp (arch->archive_head->filename, "__.SYMDEF") == 0) | |
1739 | arch->archive_head = arch->archive_head->next; | |
1740 | ||
1741 | /* Map over each element */ | |
1742 | for (current = arch->archive_head; | |
1743 | current != (bfd *) NULL; | |
1744 | current = current->next, elt_no++) | |
37217060 | 1745 | { |
a927c32d ILT |
1746 | if ((bfd_check_format (current, bfd_object) == true) |
1747 | && ((bfd_get_file_flags (current) & HAS_SYMS))) | |
1748 | { | |
326e32d7 ILT |
1749 | long storage; |
1750 | long symcount; | |
1751 | long src_count; | |
1752 | ||
1753 | storage = bfd_get_symtab_upper_bound (current); | |
1754 | if (storage < 0) | |
1755 | goto error_return; | |
a927c32d | 1756 | |
a927c32d ILT |
1757 | if (storage != 0) |
1758 | { | |
5ee3886b | 1759 | if (storage > syms_max) |
a927c32d | 1760 | { |
5ee3886b ILT |
1761 | if (syms_max > 0) |
1762 | free (syms); | |
1763 | syms_max = storage; | |
64d5f5d0 | 1764 | syms = (asymbol **) bfd_malloc ((size_t) syms_max); |
5ee3886b | 1765 | if (syms == NULL) |
64d5f5d0 | 1766 | goto error_return; |
a927c32d ILT |
1767 | } |
1768 | symcount = bfd_canonicalize_symtab (current, syms); | |
326e32d7 ILT |
1769 | if (symcount < 0) |
1770 | goto error_return; | |
a927c32d | 1771 | |
a927c32d ILT |
1772 | /* Now map over all the symbols, picking out the ones we want */ |
1773 | for (src_count = 0; src_count < symcount; src_count++) | |
1774 | { | |
13944a3e ILT |
1775 | flagword flags = (syms[src_count])->flags; |
1776 | asection *sec = syms[src_count]->section; | |
a927c32d ILT |
1777 | |
1778 | if ((flags & BSF_GLOBAL || | |
1779 | flags & BSF_WEAK || | |
1780 | flags & BSF_INDIRECT || | |
1781 | bfd_is_com_section (sec)) | |
81eb52b3 | 1782 | && ! bfd_is_und_section (sec)) |
a927c32d | 1783 | { |
5ee3886b ILT |
1784 | size_t namelen; |
1785 | struct orl *new_map; | |
1786 | ||
a927c32d ILT |
1787 | /* This symbol will go into the archive header */ |
1788 | if (orl_count == orl_max) | |
1789 | { | |
1790 | orl_max *= 2; | |
64d5f5d0 ILT |
1791 | new_map = |
1792 | ((struct orl *) | |
1793 | bfd_realloc (map, orl_max * sizeof (struct orl))); | |
5ee3886b | 1794 | if (new_map == (struct orl *) NULL) |
64d5f5d0 | 1795 | goto error_return; |
5ee3886b ILT |
1796 | |
1797 | map = new_map; | |
a927c32d ILT |
1798 | } |
1799 | ||
5ee3886b ILT |
1800 | namelen = strlen (syms[src_count]->name); |
1801 | map[orl_count].name = ((char **) | |
1802 | bfd_alloc (arch, | |
1803 | sizeof (char *))); | |
1804 | if (map[orl_count].name == NULL) | |
a9713b91 | 1805 | goto error_return; |
5ee3886b ILT |
1806 | *(map[orl_count].name) = bfd_alloc (arch, namelen + 1); |
1807 | if (*(map[orl_count].name) == NULL) | |
a9713b91 | 1808 | goto error_return; |
5ee3886b | 1809 | strcpy (*(map[orl_count].name), syms[src_count]->name); |
a927c32d ILT |
1810 | (map[orl_count]).pos = (file_ptr) current; |
1811 | (map[orl_count]).namidx = stridx; | |
1812 | ||
5ee3886b | 1813 | stridx += namelen + 1; |
a927c32d | 1814 | ++orl_count; |
0452b5aa | 1815 | } |
a927c32d | 1816 | } |
0452b5aa | 1817 | } |
dfe09c49 ILT |
1818 | |
1819 | /* Now ask the BFD to free up any cached information, so we | |
1820 | don't fill all of memory with symbol tables. */ | |
1821 | if (! bfd_free_cached_info (current)) | |
1822 | goto error_return; | |
37217060 | 1823 | } |
a927c32d | 1824 | } |
4a81b561 | 1825 | |
a927c32d | 1826 | /* OK, now we have collected all the data, let's write them out */ |
5ee3886b ILT |
1827 | ret = BFD_SEND (arch, write_armap, |
1828 | (arch, elength, map, orl_count, stridx)); | |
a37cc0c0 | 1829 | |
5ee3886b ILT |
1830 | if (syms_max > 0) |
1831 | free (syms); | |
326e32d7 ILT |
1832 | if (map != NULL) |
1833 | free (map); | |
1834 | if (first_name != NULL) | |
1835 | bfd_release (arch, first_name); | |
5ee3886b ILT |
1836 | |
1837 | return ret; | |
326e32d7 | 1838 | |
326e32d7 ILT |
1839 | error_return: |
1840 | if (syms_max > 0) | |
1841 | free (syms); | |
1842 | if (map != NULL) | |
1843 | free (map); | |
1844 | if (first_name != NULL) | |
1845 | bfd_release (arch, first_name); | |
1846 | ||
1847 | return false; | |
4a81b561 DHW |
1848 | } |
1849 | ||
4a81b561 DHW |
1850 | boolean |
1851 | bsd_write_armap (arch, elength, map, orl_count, stridx) | |
1852 | bfd *arch; | |
1853 | unsigned int elength; | |
1854 | struct orl *map; | |
37217060 | 1855 | unsigned int orl_count; |
4a81b561 DHW |
1856 | int stridx; |
1857 | { | |
4ee249da | 1858 | int padit = stridx & 1; |
cd9782e8 | 1859 | unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE; |
4ee249da DHW |
1860 | unsigned int stringsize = stridx + padit; |
1861 | /* Include 8 bytes to store ranlibsize and stringsize in output. */ | |
1862 | unsigned int mapsize = ranlibsize + stringsize + 8; | |
4a81b561 DHW |
1863 | file_ptr firstreal; |
1864 | bfd *current = arch->archive_head; | |
286fd2f9 | 1865 | bfd *last_elt = current; /* last element arch seen */ |
cd9782e8 | 1866 | bfd_byte temp[4]; |
ae115e51 | 1867 | unsigned int count; |
4a81b561 DHW |
1868 | struct ar_hdr hdr; |
1869 | struct stat statbuf; | |
1870 | unsigned int i; | |
4a81b561 DHW |
1871 | |
1872 | firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG; | |
1873 | ||
2a525d0c | 1874 | stat (arch->filename, &statbuf); |
a927c32d | 1875 | memset ((char *) (&hdr), 0, sizeof (struct ar_hdr)); |
d6a554ae | 1876 | sprintf (hdr.ar_name, RANLIBMAG); |
b5b4294e | 1877 | /* Remember the timestamp, to keep it holy. But fudge it a little. */ |
a927c32d ILT |
1878 | bfd_ardata (arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET; |
1879 | bfd_ardata (arch)->armap_datepos = (SARMAG | |
1880 | + offsetof (struct ar_hdr, ar_date[0])); | |
1881 | sprintf (hdr.ar_date, "%ld", bfd_ardata (arch)->armap_timestamp); | |
e5100743 ILT |
1882 | sprintf (hdr.ar_uid, "%ld", (long) getuid ()); |
1883 | sprintf (hdr.ar_gid, "%ld", (long) getgid ()); | |
45021fee | 1884 | sprintf (hdr.ar_size, "%-10d", (int) mapsize); |
aeef32f0 | 1885 | strncpy (hdr.ar_fmag, ARFMAG, 2); |
4a81b561 | 1886 | for (i = 0; i < sizeof (struct ar_hdr); i++) |
a927c32d ILT |
1887 | if (((char *) (&hdr))[i] == '\0') |
1888 | (((char *) (&hdr))[i]) = ' '; | |
4002f18a ILT |
1889 | if (bfd_write ((char *) &hdr, 1, sizeof (struct ar_hdr), arch) |
1890 | != sizeof (struct ar_hdr)) | |
1891 | return false; | |
cd9782e8 ILT |
1892 | bfd_h_put_32 (arch, (bfd_vma) ranlibsize, temp); |
1893 | if (bfd_write (temp, 1, sizeof (temp), arch) != sizeof (temp)) | |
4002f18a | 1894 | return false; |
45021fee | 1895 | |
a927c32d ILT |
1896 | for (count = 0; count < orl_count; count++) |
1897 | { | |
cd9782e8 | 1898 | bfd_byte buf[BSD_SYMDEF_SIZE]; |
a927c32d ILT |
1899 | |
1900 | if (((bfd *) (map[count]).pos) != last_elt) | |
1901 | { | |
1902 | do | |
1903 | { | |
1904 | firstreal += arelt_size (current) + sizeof (struct ar_hdr); | |
1905 | firstreal += firstreal % 2; | |
1906 | current = current->next; | |
1907 | } | |
1908 | while (current != (bfd *) (map[count]).pos); | |
1909 | } /* if new archive element */ | |
1910 | ||
1911 | last_elt = current; | |
cd9782e8 ILT |
1912 | bfd_h_put_32 (arch, map[count].namidx, buf); |
1913 | bfd_h_put_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE); | |
1914 | if (bfd_write (buf, BSD_SYMDEF_SIZE, 1, arch) != BSD_SYMDEF_SIZE) | |
4002f18a | 1915 | return false; |
a927c32d | 1916 | } |
4a81b561 DHW |
1917 | |
1918 | /* now write the strings themselves */ | |
cd9782e8 ILT |
1919 | bfd_h_put_32 (arch, stringsize, temp); |
1920 | if (bfd_write (temp, 1, sizeof (temp), arch) != sizeof (temp)) | |
4002f18a | 1921 | return false; |
4a81b561 | 1922 | for (count = 0; count < orl_count; count++) |
4002f18a ILT |
1923 | { |
1924 | size_t len = strlen (*map[count].name) + 1; | |
1925 | ||
1926 | if (bfd_write (*map[count].name, 1, len, arch) != len) | |
1927 | return false; | |
1928 | } | |
4a81b561 DHW |
1929 | |
1930 | /* The spec sez this should be a newline. But in order to be | |
1931 | bug-compatible for sun's ar we use a null. */ | |
1932 | if (padit) | |
4002f18a ILT |
1933 | { |
1934 | if (bfd_write ("", 1, 1, arch) != 1) | |
1935 | return false; | |
1936 | } | |
4a81b561 DHW |
1937 | |
1938 | return true; | |
1939 | } | |
b5b4294e | 1940 | |
b5b4294e | 1941 | /* At the end of archive file handling, update the timestamp in the |
a927c32d | 1942 | file, so the linker will accept it. |
b5b4294e JG |
1943 | |
1944 | Return true if the timestamp was OK, or an unusual problem happened. | |
1945 | Return false if we updated the timestamp. */ | |
1946 | ||
81eb52b3 ILT |
1947 | boolean |
1948 | _bfd_archive_bsd_update_armap_timestamp (arch) | |
b5b4294e JG |
1949 | bfd *arch; |
1950 | { | |
1951 | struct stat archstat; | |
1952 | struct ar_hdr hdr; | |
ae115e51 | 1953 | unsigned int i; |
b5b4294e JG |
1954 | |
1955 | /* Flush writes, get last-write timestamp from file, and compare it | |
1956 | to the timestamp IN the file. */ | |
1957 | bfd_flush (arch); | |
a927c32d ILT |
1958 | if (bfd_stat (arch, &archstat) == -1) |
1959 | { | |
1960 | perror ("Reading archive file mod timestamp"); | |
1961 | return true; /* Can't read mod time for some reason */ | |
1962 | } | |
1963 | if (archstat.st_mtime <= bfd_ardata (arch)->armap_timestamp) | |
b5b4294e JG |
1964 | return true; /* OK by the linker's rules */ |
1965 | ||
1966 | /* Update the timestamp. */ | |
a927c32d | 1967 | bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET; |
b5b4294e JG |
1968 | |
1969 | /* Prepare an ASCII version suitable for writing. */ | |
1970 | memset (hdr.ar_date, 0, sizeof (hdr.ar_date)); | |
a927c32d | 1971 | sprintf (hdr.ar_date, "%ld", bfd_ardata (arch)->armap_timestamp); |
b5b4294e | 1972 | for (i = 0; i < sizeof (hdr.ar_date); i++) |
a927c32d ILT |
1973 | if (hdr.ar_date[i] == '\0') |
1974 | (hdr.ar_date)[i] = ' '; | |
b5b4294e JG |
1975 | |
1976 | /* Write it into the file. */ | |
81eb52b3 ILT |
1977 | bfd_ardata (arch)->armap_datepos = (SARMAG |
1978 | + offsetof (struct ar_hdr, ar_date[0])); | |
4002f18a ILT |
1979 | if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0 |
1980 | || (bfd_write (hdr.ar_date, sizeof (hdr.ar_date), 1, arch) | |
1981 | != sizeof (hdr.ar_date))) | |
a927c32d | 1982 | { |
4002f18a | 1983 | /* FIXME: bfd can't call perror. */ |
a927c32d ILT |
1984 | perror ("Writing updated armap timestamp"); |
1985 | return true; /* Some error while writing */ | |
1986 | } | |
b5b4294e JG |
1987 | |
1988 | return false; /* We updated the timestamp successfully. */ | |
1989 | } | |
4a81b561 | 1990 | \f |
4a81b561 | 1991 | /* A coff armap looks like : |
a927c32d ILT |
1992 | lARMAG |
1993 | struct ar_hdr with name = '/' | |
1994 | number of symbols | |
1995 | offset of file for symbol 0 | |
1996 | offset of file for symbol 1 | |
4a81b561 | 1997 | |
a927c32d ILT |
1998 | offset of file for symbol n-1 |
1999 | symbol name 0 | |
2000 | symbol name 1 | |
2001 | ||
2002 | symbol name n-1 | |
4a81b561 | 2003 | */ |
4ee249da | 2004 | |
4a81b561 | 2005 | boolean |
f58809fd | 2006 | coff_write_armap (arch, elength, map, symbol_count, stridx) |
4a81b561 DHW |
2007 | bfd *arch; |
2008 | unsigned int elength; | |
2009 | struct orl *map; | |
f58809fd | 2010 | unsigned int symbol_count; |
4a81b561 DHW |
2011 | int stridx; |
2012 | { | |
a927c32d ILT |
2013 | /* The size of the ranlib is the number of exported symbols in the |
2014 | archive * the number of bytes in a int, + an int for the count */ | |
2015 | unsigned int ranlibsize = (symbol_count * 4) + 4; | |
2016 | unsigned int stringsize = stridx; | |
2017 | unsigned int mapsize = stringsize + ranlibsize; | |
2018 | file_ptr archive_member_file_ptr; | |
2019 | bfd *current = arch->archive_head; | |
ae115e51 | 2020 | unsigned int count; |
a927c32d ILT |
2021 | struct ar_hdr hdr; |
2022 | unsigned int i; | |
2023 | int padit = mapsize & 1; | |
f58809fd | 2024 | |
a927c32d ILT |
2025 | if (padit) |
2026 | mapsize++; | |
2027 | ||
2028 | /* work out where the first object file will go in the archive */ | |
2029 | archive_member_file_ptr = (mapsize | |
2030 | + elength | |
2031 | + sizeof (struct ar_hdr) | |
2032 | + SARMAG); | |
f58809fd | 2033 | |
a927c32d ILT |
2034 | memset ((char *) (&hdr), 0, sizeof (struct ar_hdr)); |
2035 | hdr.ar_name[0] = '/'; | |
2036 | sprintf (hdr.ar_size, "%-10d", (int) mapsize); | |
2037 | sprintf (hdr.ar_date, "%ld", (long) time (NULL)); | |
2038 | /* This, at least, is what Intel coff sets the values to.: */ | |
2039 | sprintf ((hdr.ar_uid), "%d", 0); | |
2040 | sprintf ((hdr.ar_gid), "%d", 0); | |
2041 | sprintf ((hdr.ar_mode), "%-7o", (unsigned) 0); | |
aeef32f0 | 2042 | strncpy (hdr.ar_fmag, ARFMAG, 2); |
4a81b561 | 2043 | |
a927c32d ILT |
2044 | for (i = 0; i < sizeof (struct ar_hdr); i++) |
2045 | if (((char *) (&hdr))[i] == '\0') | |
2046 | (((char *) (&hdr))[i]) = ' '; | |
4a81b561 | 2047 | |
a927c32d ILT |
2048 | /* Write the ar header for this item and the number of symbols */ |
2049 | ||
4002f18a ILT |
2050 | if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), arch) |
2051 | != sizeof (struct ar_hdr)) | |
2052 | return false; | |
a927c32d ILT |
2053 | |
2054 | bfd_write_bigendian_4byte_int (arch, symbol_count); | |
2055 | ||
2056 | /* Two passes, first write the file offsets for each symbol - | |
2057 | remembering that each offset is on a two byte boundary. */ | |
2058 | ||
2059 | /* Write out the file offset for the file associated with each | |
2060 | symbol, and remember to keep the offsets padded out. */ | |
2061 | ||
2062 | current = arch->archive_head; | |
2063 | count = 0; | |
2064 | while (current != (bfd *) NULL && count < symbol_count) | |
2065 | { | |
2066 | /* For each symbol which is used defined in this object, write out | |
2067 | the object file's address in the archive */ | |
2068 | ||
2069 | while (((bfd *) (map[count]).pos) == current) | |
2070 | { | |
2071 | bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr); | |
2072 | count++; | |
2073 | } | |
2074 | /* Add size of this archive entry */ | |
13944a3e ILT |
2075 | archive_member_file_ptr += (arelt_size (current) |
2076 | + sizeof (struct ar_hdr)); | |
a927c32d ILT |
2077 | /* remember aboout the even alignment */ |
2078 | archive_member_file_ptr += archive_member_file_ptr % 2; | |
2079 | current = current->next; | |
4a81b561 | 2080 | } |
4a81b561 | 2081 | |
a927c32d ILT |
2082 | /* now write the strings themselves */ |
2083 | for (count = 0; count < symbol_count; count++) | |
4002f18a ILT |
2084 | { |
2085 | size_t len = strlen (*map[count].name) + 1; | |
2086 | ||
2087 | if (bfd_write (*map[count].name, 1, len, arch) != len) | |
2088 | return false; | |
2089 | } | |
a927c32d ILT |
2090 | |
2091 | /* The spec sez this should be a newline. But in order to be | |
2092 | bug-compatible for arc960 we use a null. */ | |
2093 | if (padit) | |
4002f18a ILT |
2094 | { |
2095 | if (bfd_write ("", 1, 1, arch) != 1) | |
2096 | return false; | |
2097 | } | |
a927c32d ILT |
2098 | |
2099 | return true; | |
4a81b561 | 2100 | } |