archive.c revision 1.1.1.3 1 1.1 christos /* BFD back-end for archive files (libraries).
2 1.1.1.3 christos Copyright (C) 1990-2016 Free Software Foundation, Inc.
3 1.1 christos Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
4 1.1 christos
5 1.1 christos This file is part of BFD, the Binary File Descriptor library.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program; if not, write to the Free Software
19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20 1.1 christos
21 1.1 christos /*
22 1.1 christos @setfilename archive-info
23 1.1 christos SECTION
24 1.1 christos Archives
25 1.1 christos
26 1.1 christos DESCRIPTION
27 1.1 christos An archive (or library) is just another BFD. It has a symbol
28 1.1 christos table, although there's not much a user program will do with it.
29 1.1 christos
30 1.1 christos The big difference between an archive BFD and an ordinary BFD
31 1.1 christos is that the archive doesn't have sections. Instead it has a
32 1.1 christos chain of BFDs that are considered its contents. These BFDs can
33 1.1 christos be manipulated like any other. The BFDs contained in an
34 1.1 christos archive opened for reading will all be opened for reading. You
35 1.1 christos may put either input or output BFDs into an archive opened for
36 1.1 christos output; they will be handled correctly when the archive is closed.
37 1.1 christos
38 1.1 christos Use <<bfd_openr_next_archived_file>> to step through
39 1.1 christos the contents of an archive opened for input. You don't
40 1.1 christos have to read the entire archive if you don't want
41 1.1 christos to! Read it until you find what you want.
42 1.1 christos
43 1.1.1.2 christos A BFD returned by <<bfd_openr_next_archived_file>> can be
44 1.1.1.2 christos closed manually with <<bfd_close>>. If you do not close it,
45 1.1.1.2 christos then a second iteration through the members of an archive may
46 1.1.1.2 christos return the same BFD. If you close the archive BFD, then all
47 1.1.1.2 christos the member BFDs will automatically be closed as well.
48 1.1.1.2 christos
49 1.1 christos Archive contents of output BFDs are chained through the
50 1.1.1.2 christos <<archive_next>> pointer in a BFD. The first one is findable
51 1.1.1.2 christos through the <<archive_head>> slot of the archive. Set it with
52 1.1.1.2 christos <<bfd_set_archive_head>> (q.v.). A given BFD may be in only
53 1.1.1.2 christos one open output archive at a time.
54 1.1 christos
55 1.1 christos As expected, the BFD archive code is more general than the
56 1.1 christos archive code of any given environment. BFD archives may
57 1.1 christos contain files of different formats (e.g., a.out and coff) and
58 1.1 christos even different architectures. You may even place archives
59 1.1 christos recursively into archives!
60 1.1 christos
61 1.1 christos This can cause unexpected confusion, since some archive
62 1.1 christos formats are more expressive than others. For instance, Intel
63 1.1 christos COFF archives can preserve long filenames; SunOS a.out archives
64 1.1 christos cannot. If you move a file from the first to the second
65 1.1 christos format and back again, the filename may be truncated.
66 1.1 christos Likewise, different a.out environments have different
67 1.1 christos conventions as to how they truncate filenames, whether they
68 1.1 christos preserve directory names in filenames, etc. When
69 1.1 christos interoperating with native tools, be sure your files are
70 1.1 christos homogeneous.
71 1.1 christos
72 1.1 christos Beware: most of these formats do not react well to the
73 1.1 christos presence of spaces in filenames. We do the best we can, but
74 1.1 christos can't always handle this case due to restrictions in the format of
75 1.1 christos archives. Many Unix utilities are braindead in regards to
76 1.1 christos spaces and such in filenames anyway, so this shouldn't be much
77 1.1 christos of a restriction.
78 1.1 christos
79 1.1 christos Archives are supported in BFD in <<archive.c>>.
80 1.1 christos
81 1.1 christos SUBSECTION
82 1.1 christos Archive functions
83 1.1 christos */
84 1.1 christos
85 1.1 christos /* Assumes:
86 1.1 christos o - all archive elements start on an even boundary, newline padded;
87 1.1 christos o - all arch headers are char *;
88 1.1 christos o - all arch headers are the same size (across architectures).
89 1.1 christos */
90 1.1 christos
91 1.1 christos /* Some formats provide a way to cram a long filename into the short
92 1.1 christos (16 chars) space provided by a BSD archive. The trick is: make a
93 1.1 christos special "file" in the front of the archive, sort of like the SYMDEF
94 1.1 christos entry. If the filename is too long to fit, put it in the extended
95 1.1 christos name table, and use its index as the filename. To prevent
96 1.1 christos confusion prepend the index with a space. This means you can't
97 1.1 christos have filenames that start with a space, but then again, many Unix
98 1.1 christos utilities can't handle that anyway.
99 1.1 christos
100 1.1 christos This scheme unfortunately requires that you stand on your head in
101 1.1 christos order to write an archive since you need to put a magic file at the
102 1.1 christos front, and need to touch every entry to do so. C'est la vie.
103 1.1 christos
104 1.1 christos We support two variants of this idea:
105 1.1 christos The SVR4 format (extended name table is named "//"),
106 1.1 christos and an extended pseudo-BSD variant (extended name table is named
107 1.1 christos "ARFILENAMES/"). The origin of the latter format is uncertain.
108 1.1 christos
109 1.1 christos BSD 4.4 uses a third scheme: It writes a long filename
110 1.1 christos directly after the header. This allows 'ar q' to work.
111 1.1 christos */
112 1.1 christos
113 1.1 christos /* Summary of archive member names:
114 1.1 christos
115 1.1 christos Symbol table (must be first):
116 1.1 christos "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
117 1.1 christos "/ " - Symbol table, system 5 style.
118 1.1 christos
119 1.1 christos Long name table (must be before regular file members):
120 1.1 christos "// " - Long name table, System 5 R4 style.
121 1.1 christos "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
122 1.1 christos
123 1.1 christos Regular file members with short names:
124 1.1 christos "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
125 1.1 christos "filename.o " - Regular file, Berkeley style (no embedded spaces).
126 1.1 christos
127 1.1 christos Regular files with long names (or embedded spaces, for BSD variants):
128 1.1 christos "/18 " - SVR4 style, name at offset 18 in name table.
129 1.1 christos "#1/23 " - Long name (or embedded spaces) 23 characters long,
130 1.1 christos BSD 4.4 style, full name follows header.
131 1.1 christos " 18 " - Long name 18 characters long, extended pseudo-BSD.
132 1.1 christos */
133 1.1 christos
134 1.1 christos #include "sysdep.h"
135 1.1 christos #include "bfd.h"
136 1.1 christos #include "libiberty.h"
137 1.1 christos #include "libbfd.h"
138 1.1 christos #include "aout/ar.h"
139 1.1 christos #include "aout/ranlib.h"
140 1.1 christos #include "safe-ctype.h"
141 1.1 christos #include "hashtab.h"
142 1.1 christos #include "filenames.h"
143 1.1.1.2 christos #include "bfdlink.h"
144 1.1 christos
145 1.1 christos #ifndef errno
146 1.1 christos extern int errno;
147 1.1 christos #endif
148 1.1 christos
149 1.1 christos /* We keep a cache of archive filepointers to archive elements to
150 1.1 christos speed up searching the archive by filepos. We only add an entry to
151 1.1 christos the cache when we actually read one. We also don't sort the cache;
152 1.1 christos it's generally short enough to search linearly.
153 1.1 christos Note that the pointers here point to the front of the ar_hdr, not
154 1.1 christos to the front of the contents! */
155 1.1 christos struct ar_cache
156 1.1 christos {
157 1.1 christos file_ptr ptr;
158 1.1 christos bfd *arbfd;
159 1.1 christos };
160 1.1 christos
161 1.1 christos #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
162 1.1 christos #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
163 1.1 christos
164 1.1 christos #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
165 1.1 christos
166 1.1 christos static const char * normalize (bfd *, const char *);
167 1.1 christos
168 1.1 christos #define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata (bfd)->arch_header)
169 1.1 christos
170 1.1 christos /* True iff NAME designated a BSD 4.4 extended name. */
171 1.1 christos
172 1.1 christos #define is_bsd44_extended_name(NAME) \
173 1.1 christos (NAME[0] == '#' && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3]))
174 1.1 christos
175 1.1 christos void
177 1.1 christos _bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
178 1.1 christos {
179 1.1 christos static char buf[20];
180 1.1 christos size_t len;
181 1.1 christos
182 1.1 christos snprintf (buf, sizeof (buf), fmt, val);
183 1.1 christos len = strlen (buf);
184 1.1 christos if (len < n)
185 1.1 christos {
186 1.1 christos memcpy (p, buf, len);
187 1.1 christos memset (p + len, ' ', n - len);
188 1.1 christos }
189 1.1 christos else
190 1.1 christos memcpy (p, buf, n);
191 1.1 christos }
192 1.1 christos
193 1.1 christos bfd_boolean
194 1.1 christos _bfd_ar_sizepad (char *p, size_t n, bfd_size_type size)
195 1.1 christos {
196 1.1 christos static char buf[21];
197 1.1 christos size_t len;
198 1.1 christos
199 1.1 christos snprintf (buf, sizeof (buf), "%-10" BFD_VMA_FMT "u", size);
200 1.1 christos len = strlen (buf);
201 1.1 christos if (len > n)
202 1.1 christos {
203 1.1 christos bfd_set_error (bfd_error_file_too_big);
204 1.1 christos return FALSE;
205 1.1 christos }
206 1.1 christos if (len < n)
207 1.1 christos {
208 1.1 christos memcpy (p, buf, len);
209 1.1 christos memset (p + len, ' ', n - len);
210 1.1 christos }
211 1.1 christos else
212 1.1 christos memcpy (p, buf, n);
213 1.1 christos return TRUE;
214 1.1 christos }
215 1.1 christos
216 1.1 christos bfd_boolean
218 1.1 christos _bfd_generic_mkarchive (bfd *abfd)
219 1.1 christos {
220 1.1 christos bfd_size_type amt = sizeof (struct artdata);
221 1.1 christos
222 1.1 christos abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
223 1.1 christos if (bfd_ardata (abfd) == NULL)
224 1.1 christos return FALSE;
225 1.1 christos
226 1.1 christos /* Already cleared by bfd_zalloc above.
227 1.1 christos bfd_ardata (abfd)->cache = NULL;
228 1.1 christos bfd_ardata (abfd)->archive_head = NULL;
229 1.1 christos bfd_ardata (abfd)->symdefs = NULL;
230 1.1 christos bfd_ardata (abfd)->extended_names = NULL;
231 1.1 christos bfd_ardata (abfd)->extended_names_size = 0;
232 1.1 christos bfd_ardata (abfd)->tdata = NULL; */
233 1.1 christos
234 1.1 christos return TRUE;
235 1.1 christos }
236 1.1 christos
237 1.1 christos /*
238 1.1 christos FUNCTION
239 1.1 christos bfd_get_next_mapent
240 1.1 christos
241 1.1 christos SYNOPSIS
242 1.1 christos symindex bfd_get_next_mapent
243 1.1 christos (bfd *abfd, symindex previous, carsym **sym);
244 1.1 christos
245 1.1 christos DESCRIPTION
246 1.1 christos Step through archive @var{abfd}'s symbol table (if it
247 1.1 christos has one). Successively update @var{sym} with the next symbol's
248 1.1 christos information, returning that symbol's (internal) index into the
249 1.1 christos symbol table.
250 1.1 christos
251 1.1 christos Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
252 1.1 christos the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
253 1.1 christos got the last one.
254 1.1 christos
255 1.1 christos A <<carsym>> is a canonical archive symbol. The only
256 1.1 christos user-visible element is its name, a null-terminated string.
257 1.1 christos */
258 1.1 christos
259 1.1 christos symindex
260 1.1 christos bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
261 1.1 christos {
262 1.1 christos if (!bfd_has_map (abfd))
263 1.1 christos {
264 1.1 christos bfd_set_error (bfd_error_invalid_operation);
265 1.1 christos return BFD_NO_MORE_SYMBOLS;
266 1.1 christos }
267 1.1 christos
268 1.1 christos if (prev == BFD_NO_MORE_SYMBOLS)
269 1.1 christos prev = 0;
270 1.1 christos else
271 1.1 christos ++prev;
272 1.1 christos if (prev >= bfd_ardata (abfd)->symdef_count)
273 1.1 christos return BFD_NO_MORE_SYMBOLS;
274 1.1 christos
275 1.1 christos *entry = (bfd_ardata (abfd)->symdefs + prev);
276 1.1 christos return prev;
277 1.1 christos }
278 1.1 christos
279 1.1 christos /* To be called by backends only. */
280 1.1 christos
281 1.1 christos bfd *
282 1.1 christos _bfd_create_empty_archive_element_shell (bfd *obfd)
283 1.1 christos {
284 1.1 christos return _bfd_new_bfd_contained_in (obfd);
285 1.1 christos }
286 1.1 christos
287 1.1 christos /*
288 1.1 christos FUNCTION
289 1.1 christos bfd_set_archive_head
290 1.1 christos
291 1.1 christos SYNOPSIS
292 1.1 christos bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
293 1.1 christos
294 1.1 christos DESCRIPTION
295 1.1 christos Set the head of the chain of
296 1.1 christos BFDs contained in the archive @var{output} to @var{new_head}.
297 1.1 christos */
298 1.1 christos
299 1.1 christos bfd_boolean
300 1.1 christos bfd_set_archive_head (bfd *output_archive, bfd *new_head)
301 1.1 christos {
302 1.1 christos output_archive->archive_head = new_head;
303 1.1 christos return TRUE;
304 1.1 christos }
305 1.1 christos
306 1.1 christos bfd *
307 1.1 christos _bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
308 1.1 christos {
309 1.1 christos htab_t hash_table = bfd_ardata (arch_bfd)->cache;
310 1.1 christos struct ar_cache m;
311 1.1 christos
312 1.1 christos m.ptr = filepos;
313 1.1 christos
314 1.1 christos if (hash_table)
315 1.1 christos {
316 1.1 christos struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
317 1.1.1.2 christos if (!entry)
318 1.1.1.2 christos return NULL;
319 1.1.1.2 christos
320 1.1.1.2 christos /* Unfortunately this flag is set after checking that we have
321 1.1.1.2 christos an archive, and checking for an archive means one element has
322 1.1.1.2 christos sneaked into the cache. */
323 1.1 christos entry->arbfd->no_export = arch_bfd->no_export;
324 1.1 christos return entry->arbfd;
325 1.1 christos }
326 1.1 christos else
327 1.1 christos return NULL;
328 1.1 christos }
329 1.1 christos
330 1.1 christos static hashval_t
331 1.1 christos hash_file_ptr (const void * p)
332 1.1 christos {
333 1.1 christos return (hashval_t) (((struct ar_cache *) p)->ptr);
334 1.1 christos }
335 1.1 christos
336 1.1 christos /* Returns non-zero if P1 and P2 are equal. */
337 1.1 christos
338 1.1 christos static int
339 1.1 christos eq_file_ptr (const void * p1, const void * p2)
340 1.1 christos {
341 1.1 christos struct ar_cache *arc1 = (struct ar_cache *) p1;
342 1.1 christos struct ar_cache *arc2 = (struct ar_cache *) p2;
343 1.1 christos return arc1->ptr == arc2->ptr;
344 1.1 christos }
345 1.1 christos
346 1.1 christos /* The calloc function doesn't always take size_t (e.g. on VMS)
347 1.1 christos so wrap it to avoid a compile time warning. */
348 1.1 christos
349 1.1 christos static void *
350 1.1 christos _bfd_calloc_wrapper (size_t a, size_t b)
351 1.1 christos {
352 1.1 christos return calloc (a, b);
353 1.1 christos }
354 1.1 christos
355 1.1 christos /* Kind of stupid to call cons for each one, but we don't do too many. */
356 1.1 christos
357 1.1 christos bfd_boolean
358 1.1 christos _bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
359 1.1 christos {
360 1.1 christos struct ar_cache *cache;
361 1.1 christos htab_t hash_table = bfd_ardata (arch_bfd)->cache;
362 1.1 christos
363 1.1 christos /* If the hash table hasn't been created, create it. */
364 1.1 christos if (hash_table == NULL)
365 1.1 christos {
366 1.1 christos hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
367 1.1 christos NULL, _bfd_calloc_wrapper, free);
368 1.1 christos if (hash_table == NULL)
369 1.1 christos return FALSE;
370 1.1 christos bfd_ardata (arch_bfd)->cache = hash_table;
371 1.1 christos }
372 1.1 christos
373 1.1 christos /* Insert new_elt into the hash table by filepos. */
374 1.1 christos cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
375 1.1 christos cache->ptr = filepos;
376 1.1 christos cache->arbfd = new_elt;
377 1.1.1.2 christos *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
378 1.1.1.2 christos
379 1.1.1.2 christos /* Provide a means of accessing this from child. */
380 1.1.1.2 christos arch_eltdata (new_elt)->parent_cache = hash_table;
381 1.1 christos arch_eltdata (new_elt)->key = filepos;
382 1.1 christos
383 1.1 christos return TRUE;
384 1.1 christos }
385 1.1.1.2 christos
386 1.1 christos static bfd *
388 1.1.1.2 christos open_nested_file (const char *filename, bfd *archive)
389 1.1.1.2 christos {
390 1.1.1.2 christos const char *target;
391 1.1.1.2 christos bfd *n_bfd;
392 1.1.1.2 christos
393 1.1.1.2 christos target = NULL;
394 1.1.1.2 christos if (!archive->target_defaulted)
395 1.1.1.2 christos target = archive->xvec->name;
396 1.1.1.2 christos n_bfd = bfd_openr (filename, target);
397 1.1.1.2 christos if (n_bfd != NULL)
398 1.1.1.3 christos {
399 1.1.1.2 christos n_bfd->lto_output = archive->lto_output;
400 1.1.1.2 christos n_bfd->no_export = archive->no_export;
401 1.1.1.2 christos n_bfd->my_archive = archive;
402 1.1.1.2 christos }
403 1.1.1.2 christos return n_bfd;
404 1.1.1.2 christos }
405 1.1.1.2 christos
406 1.1.1.2 christos static bfd *
407 1.1.1.2 christos find_nested_archive (const char *filename, bfd *arch_bfd)
408 1.1.1.2 christos {
409 1.1.1.2 christos bfd *abfd;
410 1.1.1.2 christos
411 1.1.1.2 christos /* PR 15140: Don't allow a nested archive pointing to itself. */
412 1.1.1.2 christos if (filename_cmp (filename, arch_bfd->filename) == 0)
413 1.1.1.2 christos {
414 1.1 christos bfd_set_error (bfd_error_malformed_archive);
415 1.1 christos return NULL;
416 1.1 christos }
417 1.1 christos
418 1.1 christos for (abfd = arch_bfd->nested_archives;
419 1.1 christos abfd != NULL;
420 1.1 christos abfd = abfd->archive_next)
421 1.1 christos {
422 1.1.1.2 christos if (filename_cmp (filename, abfd->filename) == 0)
423 1.1 christos return abfd;
424 1.1 christos }
425 1.1 christos abfd = open_nested_file (filename, arch_bfd);
426 1.1 christos if (abfd)
427 1.1 christos {
428 1.1 christos abfd->archive_next = arch_bfd->nested_archives;
429 1.1 christos arch_bfd->nested_archives = abfd;
430 1.1 christos }
431 1.1 christos return abfd;
432 1.1 christos }
433 1.1 christos
434 1.1 christos /* The name begins with space. Hence the rest of the name is an index into
435 1.1 christos the string table. */
436 1.1 christos
437 1.1 christos static char *
438 1.1 christos get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp)
439 1.1 christos {
440 1.1 christos unsigned long table_index = 0;
441 1.1 christos const char *endp;
442 1.1 christos
443 1.1 christos /* Should extract string so that I can guarantee not to overflow into
444 1.1 christos the next region, but I'm too lazy. */
445 1.1 christos errno = 0;
446 1.1 christos /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
447 1.1 christos table_index = strtol (name + 1, (char **) &endp, 10);
448 1.1 christos if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
449 1.1 christos {
450 1.1 christos bfd_set_error (bfd_error_malformed_archive);
451 1.1 christos return NULL;
452 1.1 christos }
453 1.1 christos /* In a thin archive, a member of an archive-within-an-archive
454 1.1 christos will have the offset in the inner archive encoded here. */
455 1.1 christos if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':')
456 1.1 christos {
457 1.1 christos file_ptr origin = strtol (endp + 1, NULL, 10);
458 1.1 christos
459 1.1 christos if (errno != 0)
460 1.1 christos {
461 1.1 christos bfd_set_error (bfd_error_malformed_archive);
462 1.1 christos return NULL;
463 1.1 christos }
464 1.1 christos *originp = origin;
465 1.1 christos }
466 1.1 christos else
467 1.1 christos *originp = 0;
468 1.1 christos
469 1.1 christos return bfd_ardata (arch)->extended_names + table_index;
470 1.1 christos }
471 1.1 christos
472 1.1 christos /* This functions reads an arch header and returns an areltdata pointer, or
473 1.1 christos NULL on error.
474 1.1 christos
475 1.1 christos Presumes the file pointer is already in the right place (ie pointing
476 1.1 christos to the ar_hdr in the file). Moves the file pointer; on success it
477 1.1 christos should be pointing to the front of the file contents; on failure it
478 1.1 christos could have been moved arbitrarily. */
479 1.1 christos
480 1.1 christos void *
481 1.1 christos _bfd_generic_read_ar_hdr (bfd *abfd)
482 1.1 christos {
483 1.1 christos return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
484 1.1 christos }
485 1.1 christos
486 1.1 christos /* Alpha ECOFF uses an optional different ARFMAG value, so we have a
487 1.1 christos variant of _bfd_generic_read_ar_hdr which accepts a magic string. */
488 1.1 christos
489 1.1 christos void *
490 1.1 christos _bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag)
491 1.1 christos {
492 1.1 christos struct ar_hdr hdr;
493 1.1 christos char *hdrp = (char *) &hdr;
494 1.1 christos bfd_size_type parsed_size;
495 1.1 christos struct areltdata *ared;
496 1.1 christos char *filename = NULL;
497 1.1 christos bfd_size_type namelen = 0;
498 1.1 christos bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
499 1.1 christos char *allocptr = 0;
500 1.1 christos file_ptr origin = 0;
501 1.1 christos unsigned int extra_size = 0;
502 1.1 christos char fmag_save;
503 1.1 christos int scan;
504 1.1 christos
505 1.1 christos if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
506 1.1 christos {
507 1.1 christos if (bfd_get_error () != bfd_error_system_call)
508 1.1 christos bfd_set_error (bfd_error_no_more_archived_files);
509 1.1 christos return NULL;
510 1.1 christos }
511 1.1 christos if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
512 1.1 christos && (mag == NULL
513 1.1 christos || strncmp (hdr.ar_fmag, mag, 2) != 0))
514 1.1 christos {
515 1.1 christos bfd_set_error (bfd_error_malformed_archive);
516 1.1 christos return NULL;
517 1.1 christos }
518 1.1 christos
519 1.1 christos errno = 0;
520 1.1 christos fmag_save = hdr.ar_fmag[0];
521 1.1 christos hdr.ar_fmag[0] = 0;
522 1.1 christos scan = sscanf (hdr.ar_size, "%" BFD_VMA_FMT "u", &parsed_size);
523 1.1 christos hdr.ar_fmag[0] = fmag_save;
524 1.1 christos if (scan != 1)
525 1.1 christos {
526 1.1 christos bfd_set_error (bfd_error_malformed_archive);
527 1.1 christos return NULL;
528 1.1 christos }
529 1.1 christos
530 1.1 christos /* Extract the filename from the archive - there are two ways to
531 1.1 christos specify an extended name table, either the first char of the
532 1.1 christos name is a space, or it's a slash. */
533 1.1 christos if ((hdr.ar_name[0] == '/'
534 1.1 christos || (hdr.ar_name[0] == ' '
535 1.1 christos && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
536 1.1 christos && bfd_ardata (abfd)->extended_names != NULL)
537 1.1 christos {
538 1.1 christos filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin);
539 1.1 christos if (filename == NULL)
540 1.1 christos return NULL;
541 1.1 christos }
542 1.1 christos /* BSD4.4-style long filename. */
543 1.1 christos else if (is_bsd44_extended_name (hdr.ar_name))
544 1.1 christos {
545 1.1 christos /* BSD-4.4 extended name */
546 1.1 christos namelen = atoi (&hdr.ar_name[3]);
547 1.1 christos allocsize += namelen + 1;
548 1.1.1.2 christos parsed_size -= namelen;
549 1.1 christos extra_size = namelen;
550 1.1 christos
551 1.1 christos allocptr = (char *) bfd_zmalloc (allocsize);
552 1.1 christos if (allocptr == NULL)
553 1.1 christos return NULL;
554 1.1 christos filename = (allocptr
555 1.1 christos + sizeof (struct areltdata)
556 1.1.1.2 christos + sizeof (struct ar_hdr));
557 1.1 christos if (bfd_bread (filename, namelen, abfd) != namelen)
558 1.1 christos {
559 1.1 christos free (allocptr);
560 1.1 christos if (bfd_get_error () != bfd_error_system_call)
561 1.1 christos bfd_set_error (bfd_error_no_more_archived_files);
562 1.1 christos return NULL;
563 1.1 christos }
564 1.1 christos filename[namelen] = '\0';
565 1.1 christos }
566 1.1 christos else
567 1.1 christos {
568 1.1 christos /* We judge the end of the name by looking for '/' or ' '.
569 1.1 christos Note: The SYSV format (terminated by '/') allows embedded
570 1.1 christos spaces, so only look for ' ' if we don't find '/'. */
571 1.1 christos
572 1.1 christos char *e;
573 1.1 christos e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
574 1.1 christos if (e == NULL)
575 1.1 christos {
576 1.1 christos e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
577 1.1 christos if (e == NULL)
578 1.1 christos e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
579 1.1 christos }
580 1.1 christos
581 1.1 christos if (e != NULL)
582 1.1 christos namelen = e - hdr.ar_name;
583 1.1 christos else
584 1.1 christos {
585 1.1 christos /* If we didn't find a termination character, then the name
586 1.1 christos must be the entire field. */
587 1.1 christos namelen = ar_maxnamelen (abfd);
588 1.1 christos }
589 1.1 christos
590 1.1 christos allocsize += namelen + 1;
591 1.1 christos }
592 1.1.1.2 christos
593 1.1 christos if (!allocptr)
594 1.1 christos {
595 1.1 christos allocptr = (char *) bfd_zmalloc (allocsize);
596 1.1 christos if (allocptr == NULL)
597 1.1 christos return NULL;
598 1.1 christos }
599 1.1 christos
600 1.1 christos ared = (struct areltdata *) allocptr;
601 1.1 christos
602 1.1 christos ared->arch_header = allocptr + sizeof (struct areltdata);
603 1.1 christos memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
604 1.1 christos ared->parsed_size = parsed_size;
605 1.1 christos ared->extra_size = extra_size;
606 1.1 christos ared->origin = origin;
607 1.1 christos
608 1.1 christos if (filename != NULL)
609 1.1 christos ared->filename = filename;
610 1.1 christos else
611 1.1 christos {
612 1.1 christos ared->filename = allocptr + (sizeof (struct areltdata) +
613 1.1 christos sizeof (struct ar_hdr));
614 1.1 christos if (namelen)
615 1.1 christos memcpy (ared->filename, hdr.ar_name, namelen);
616 1.1 christos ared->filename[namelen] = '\0';
617 1.1 christos }
618 1.1 christos
619 1.1 christos return ared;
620 1.1 christos }
621 1.1 christos
622 1.1 christos /* Append the relative pathname for a member of the thin archive
624 1.1 christos to the pathname of the directory containing the archive. */
625 1.1 christos
626 1.1 christos char *
627 1.1 christos _bfd_append_relative_path (bfd *arch, char *elt_name)
628 1.1 christos {
629 1.1 christos const char *arch_name = arch->filename;
630 1.1 christos const char *base_name = lbasename (arch_name);
631 1.1 christos size_t prefix_len;
632 1.1 christos char *filename;
633 1.1 christos
634 1.1 christos if (base_name == arch_name)
635 1.1 christos return elt_name;
636 1.1 christos
637 1.1 christos prefix_len = base_name - arch_name;
638 1.1 christos filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1);
639 1.1 christos if (filename == NULL)
640 1.1 christos return NULL;
641 1.1 christos
642 1.1 christos strncpy (filename, arch_name, prefix_len);
643 1.1 christos strcpy (filename + prefix_len, elt_name);
644 1.1 christos return filename;
645 1.1 christos }
646 1.1 christos
647 1.1 christos /* This is an internal function; it's mainly used when indexing
648 1.1 christos through the archive symbol table, but also used to get the next
649 1.1 christos element, since it handles the bookkeeping so nicely for us. */
650 1.1 christos
651 1.1.1.2 christos bfd *
652 1.1 christos _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
653 1.1 christos {
654 1.1.1.2 christos struct areltdata *new_areldata;
655 1.1.1.2 christos bfd *n_bfd;
656 1.1.1.2 christos char *filename;
657 1.1 christos
658 1.1 christos n_bfd = _bfd_look_for_bfd_in_cache (archive, filepos);
659 1.1 christos if (n_bfd)
660 1.1 christos return n_bfd;
661 1.1 christos
662 1.1 christos if (0 > bfd_seek (archive, filepos, SEEK_SET))
663 1.1 christos return NULL;
664 1.1 christos
665 1.1 christos if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
666 1.1 christos return NULL;
667 1.1 christos
668 1.1 christos filename = new_areldata->filename;
669 1.1 christos
670 1.1 christos if (bfd_is_thin_archive (archive))
671 1.1 christos {
672 1.1 christos /* This is a proxy entry for an external file. */
673 1.1.1.2 christos if (! IS_ABSOLUTE_PATH (filename))
674 1.1.1.2 christos {
675 1.1.1.2 christos filename = _bfd_append_relative_path (archive, filename);
676 1.1.1.2 christos if (filename == NULL)
677 1.1 christos {
678 1.1 christos free (new_areldata);
679 1.1 christos return NULL;
680 1.1 christos }
681 1.1 christos }
682 1.1 christos
683 1.1.1.2 christos if (new_areldata->origin > 0)
684 1.1 christos {
685 1.1 christos /* This proxy entry refers to an element of a nested archive.
686 1.1 christos Locate the member of that archive and return a bfd for it. */
687 1.1 christos bfd *ext_arch = find_nested_archive (filename, archive);
688 1.1.1.2 christos
689 1.1 christos if (ext_arch == NULL
690 1.1 christos || ! bfd_check_format (ext_arch, bfd_archive))
691 1.1.1.2 christos {
692 1.1.1.2 christos free (new_areldata);
693 1.1 christos return NULL;
694 1.1.1.2 christos }
695 1.1 christos n_bfd = _bfd_get_elt_at_filepos (ext_arch, new_areldata->origin);
696 1.1 christos if (n_bfd == NULL)
697 1.1.1.2 christos {
698 1.1.1.2 christos free (new_areldata);
699 1.1 christos return NULL;
700 1.1.1.2 christos }
701 1.1 christos n_bfd->proxy_origin = bfd_tell (archive);
702 1.1 christos return n_bfd;
703 1.1.1.2 christos }
704 1.1.1.2 christos
705 1.1 christos /* It's not an element of a nested archive;
706 1.1 christos open the external file as a bfd. */
707 1.1 christos n_bfd = open_nested_file (filename, archive);
708 1.1 christos if (n_bfd == NULL)
709 1.1.1.2 christos bfd_set_error (bfd_error_malformed_archive);
710 1.1 christos }
711 1.1 christos else
712 1.1.1.2 christos {
713 1.1 christos n_bfd = _bfd_create_empty_archive_element_shell (archive);
714 1.1.1.2 christos }
715 1.1 christos
716 1.1 christos if (n_bfd == NULL)
717 1.1 christos {
718 1.1.1.2 christos free (new_areldata);
719 1.1 christos return NULL;
720 1.1 christos }
721 1.1 christos
722 1.1.1.2 christos n_bfd->proxy_origin = bfd_tell (archive);
723 1.1 christos
724 1.1 christos if (bfd_is_thin_archive (archive))
725 1.1 christos {
726 1.1.1.2 christos n_bfd->origin = 0;
727 1.1.1.2 christos }
728 1.1 christos else
729 1.1 christos {
730 1.1.1.2 christos n_bfd->origin = n_bfd->proxy_origin;
731 1.1.1.2 christos n_bfd->filename = xstrdup (filename);
732 1.1.1.2 christos }
733 1.1.1.2 christos
734 1.1.1.2 christos n_bfd->arelt_data = new_areldata;
735 1.1.1.2 christos
736 1.1 christos /* Copy BFD_COMPRESS, BFD_DECOMPRESS and BFD_COMPRESS_GABI flags. */
737 1.1.1.2 christos n_bfd->flags |= archive->flags & (BFD_COMPRESS
738 1.1.1.2 christos | BFD_DECOMPRESS
739 1.1 christos | BFD_COMPRESS_GABI);
740 1.1.1.2 christos
741 1.1.1.2 christos /* Copy is_linker_input. */
742 1.1 christos n_bfd->is_linker_input = archive->is_linker_input;
743 1.1.1.2 christos
744 1.1.1.2 christos if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd))
745 1.1 christos return n_bfd;
746 1.1 christos
747 1.1 christos free (new_areldata);
748 1.1 christos n_bfd->arelt_data = NULL;
749 1.1 christos return NULL;
750 1.1 christos }
751 1.1 christos
752 1.1 christos /* Return the BFD which is referenced by the symbol in ABFD indexed by
753 1.1 christos SYM_INDEX. SYM_INDEX should have been returned by bfd_get_next_mapent. */
754 1.1 christos
755 1.1 christos bfd *
756 1.1 christos _bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index)
757 1.1 christos {
758 1.1 christos carsym *entry;
759 1.1 christos
760 1.1 christos entry = bfd_ardata (abfd)->symdefs + sym_index;
761 1.1 christos return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
762 1.1 christos }
763 1.1 christos
764 1.1 christos /*
765 1.1 christos FUNCTION
766 1.1 christos bfd_openr_next_archived_file
767 1.1 christos
768 1.1 christos SYNOPSIS
769 1.1 christos bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
770 1.1 christos
771 1.1 christos DESCRIPTION
772 1.1 christos Provided a BFD, @var{archive}, containing an archive and NULL, open
773 1.1 christos an input BFD on the first contained element and returns that.
774 1.1 christos Subsequent calls should pass
775 1.1 christos the archive and the previous return value to return a created
776 1.1 christos BFD to the next contained element. NULL is returned when there
777 1.1 christos are no more.
778 1.1 christos */
779 1.1 christos
780 1.1 christos bfd *
781 1.1 christos bfd_openr_next_archived_file (bfd *archive, bfd *last_file)
782 1.1 christos {
783 1.1 christos if ((bfd_get_format (archive) != bfd_archive)
784 1.1 christos || (archive->direction == write_direction))
785 1.1 christos {
786 1.1 christos bfd_set_error (bfd_error_invalid_operation);
787 1.1 christos return NULL;
788 1.1 christos }
789 1.1 christos
790 1.1 christos return BFD_SEND (archive,
791 1.1 christos openr_next_archived_file, (archive, last_file));
792 1.1 christos }
793 1.1.1.2 christos
794 1.1 christos bfd *
795 1.1 christos bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
796 1.1 christos {
797 1.1 christos ufile_ptr filestart;
798 1.1 christos
799 1.1 christos if (!last_file)
800 1.1 christos filestart = bfd_ardata (archive)->first_file_filepos;
801 1.1.1.2 christos else
802 1.1.1.2 christos {
803 1.1 christos filestart = last_file->proxy_origin;
804 1.1 christos if (! bfd_is_thin_archive (archive))
805 1.1 christos #if 0
806 1.1 christos /* OLD CODE */
807 1.1 christos filestart += size;
808 1.1 christos /* Pad to an even boundary...
809 1.1 christos Note that last_file->origin can be odd in the case of
810 1.1.1.2 christos BSD-4.4-style element with a long odd size. */
811 1.1.1.2 christos if (!strncmp(arch_hdr (last_file)->ar_name, "#1/", 3))
812 1.1.1.2 christos size += strlen(normalize(last_file, last_file->filename));
813 1.1.1.2 christos filestart += size % 2;
814 1.1.1.2 christos #endif
815 1.1.1.2 christos {
816 1.1.1.2 christos bfd_size_type size = arelt_size (last_file);
817 1.1.1.2 christos
818 1.1.1.2 christos filestart += size;
819 1.1.1.2 christos /* Pad to an even boundary...
820 1.1.1.2 christos Note that last_file->origin can be odd in the case of
821 1.1.1.2 christos BSD-4.4-style element with a long odd size. */
822 1.1.1.2 christos filestart += filestart % 2;
823 1.1.1.2 christos if (filestart < last_file->proxy_origin)
824 1.1.1.2 christos {
825 1.1.1.2 christos /* Prevent looping. See PR19256. */
826 1.1 christos bfd_set_error (bfd_error_malformed_archive);
827 1.1 christos return NULL;
828 1.1 christos }
829 1.1 christos }
830 1.1 christos }
831 1.1 christos
832 1.1 christos return _bfd_get_elt_at_filepos (archive, filestart);
833 1.1 christos }
834 1.1 christos
835 1.1 christos const bfd_target *
836 1.1 christos bfd_generic_archive_p (bfd *abfd)
837 1.1 christos {
838 1.1 christos struct artdata *tdata_hold;
839 1.1 christos char armag[SARMAG + 1];
840 1.1 christos bfd_size_type amt;
841 1.1 christos
842 1.1 christos if (bfd_bread (armag, SARMAG, abfd) != SARMAG)
843 1.1 christos {
844 1.1 christos if (bfd_get_error () != bfd_error_system_call)
845 1.1 christos bfd_set_error (bfd_error_wrong_format);
846 1.1 christos return NULL;
847 1.1 christos }
848 1.1 christos
849 1.1 christos bfd_is_thin_archive (abfd) = (strncmp (armag, ARMAGT, SARMAG) == 0);
850 1.1 christos
851 1.1 christos if (strncmp (armag, ARMAG, SARMAG) != 0
852 1.1 christos && strncmp (armag, ARMAGB, SARMAG) != 0
853 1.1 christos && ! bfd_is_thin_archive (abfd))
854 1.1 christos return NULL;
855 1.1 christos
856 1.1 christos tdata_hold = bfd_ardata (abfd);
857 1.1 christos
858 1.1 christos amt = sizeof (struct artdata);
859 1.1 christos bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
860 1.1 christos if (bfd_ardata (abfd) == NULL)
861 1.1 christos {
862 1.1 christos bfd_ardata (abfd) = tdata_hold;
863 1.1 christos return NULL;
864 1.1 christos }
865 1.1 christos
866 1.1 christos bfd_ardata (abfd)->first_file_filepos = SARMAG;
867 1.1 christos /* Cleared by bfd_zalloc above.
868 1.1 christos bfd_ardata (abfd)->cache = NULL;
869 1.1 christos bfd_ardata (abfd)->archive_head = NULL;
870 1.1 christos bfd_ardata (abfd)->symdefs = NULL;
871 1.1 christos bfd_ardata (abfd)->extended_names = NULL;
872 1.1 christos bfd_ardata (abfd)->extended_names_size = 0;
873 1.1 christos bfd_ardata (abfd)->tdata = NULL; */
874 1.1 christos
875 1.1 christos if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
876 1.1 christos || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
877 1.1 christos {
878 1.1 christos if (bfd_get_error () != bfd_error_system_call)
879 1.1 christos bfd_set_error (bfd_error_wrong_format);
880 1.1 christos bfd_release (abfd, bfd_ardata (abfd));
881 1.1 christos bfd_ardata (abfd) = tdata_hold;
882 1.1 christos return NULL;
883 1.1 christos }
884 1.1 christos
885 1.1 christos if (abfd->target_defaulted && bfd_has_map (abfd))
886 1.1 christos {
887 1.1 christos bfd *first;
888 1.1 christos
889 1.1 christos /* This archive has a map, so we may presume that the contents
890 1.1 christos are object files. Make sure that if the first file in the
891 1.1 christos archive can be recognized as an object file, it is for this
892 1.1 christos target. If not, assume that this is the wrong format. If
893 1.1 christos the first file is not an object file, somebody is doing
894 1.1 christos something weird, and we permit it so that ar -t will work.
895 1.1 christos
896 1.1 christos This is done because any normal format will recognize any
897 1.1 christos normal archive, regardless of the format of the object files.
898 1.1 christos We do accept an empty archive. */
899 1.1 christos
900 1.1 christos first = bfd_openr_next_archived_file (abfd, NULL);
901 1.1 christos if (first != NULL)
902 1.1.1.2 christos {
903 1.1 christos first->target_defaulted = FALSE;
904 1.1 christos if (bfd_check_format (first, bfd_object)
905 1.1 christos && first->xvec != abfd->xvec)
906 1.1 christos bfd_set_error (bfd_error_wrong_object_format);
907 1.1 christos /* And we ought to close `first' here too. */
908 1.1 christos }
909 1.1 christos }
910 1.1 christos
911 1.1 christos return abfd->xvec;
912 1.1 christos }
913 1.1 christos
914 1.1 christos /* Some constants for a 32 bit BSD archive structure. We do not
915 1.1 christos support 64 bit archives presently; so far as I know, none actually
916 1.1 christos exist. Supporting them would require changing these constants, and
917 1.1 christos changing some H_GET_32 to H_GET_64. */
918 1.1 christos
919 1.1 christos /* The size of an external symdef structure. */
920 1.1 christos #define BSD_SYMDEF_SIZE 8
921 1.1 christos
922 1.1 christos /* The offset from the start of a symdef structure to the file offset. */
923 1.1 christos #define BSD_SYMDEF_OFFSET_SIZE 4
924 1.1 christos
925 1.1 christos /* The size of the symdef count. */
926 1.1 christos #define BSD_SYMDEF_COUNT_SIZE 4
927 1.1 christos
928 1.1 christos /* The size of the string count. */
929 1.1 christos #define BSD_STRING_COUNT_SIZE 4
930 1.1 christos
931 1.1 christos /* Read a BSD-style archive symbol table. Returns FALSE on error,
932 1.1 christos TRUE otherwise. */
933 1.1 christos
934 1.1 christos static bfd_boolean
935 1.1 christos do_slurp_bsd_armap (bfd *abfd)
936 1.1 christos {
937 1.1 christos struct areltdata *mapdata;
938 1.1 christos unsigned int counter;
939 1.1 christos bfd_byte *raw_armap, *rbase;
940 1.1 christos struct artdata *ardata = bfd_ardata (abfd);
941 1.1 christos char *stringbase;
942 1.1 christos bfd_size_type parsed_size, amt;
943 1.1 christos carsym *set;
944 1.1 christos
945 1.1.1.2 christos mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
946 1.1.1.2 christos if (mapdata == NULL)
947 1.1.1.2 christos return FALSE;
948 1.1.1.2 christos parsed_size = mapdata->parsed_size;
949 1.1.1.2 christos free (mapdata);
950 1.1 christos /* PR 17512: file: 883ff754. */
951 1.1 christos /* PR 17512: file: 0458885f. */
952 1.1 christos if (parsed_size < 4)
953 1.1 christos return FALSE;
954 1.1 christos
955 1.1 christos raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size);
956 1.1 christos if (raw_armap == NULL)
957 1.1 christos return FALSE;
958 1.1 christos
959 1.1 christos if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
960 1.1 christos {
961 1.1 christos if (bfd_get_error () != bfd_error_system_call)
962 1.1 christos bfd_set_error (bfd_error_malformed_archive);
963 1.1 christos byebye:
964 1.1 christos bfd_release (abfd, raw_armap);
965 1.1 christos return FALSE;
966 1.1 christos }
967 1.1 christos
968 1.1 christos ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
969 1.1 christos if (ardata->symdef_count * BSD_SYMDEF_SIZE >
970 1.1 christos parsed_size - BSD_SYMDEF_COUNT_SIZE)
971 1.1 christos {
972 1.1 christos /* Probably we're using the wrong byte ordering. */
973 1.1 christos bfd_set_error (bfd_error_wrong_format);
974 1.1 christos goto byebye;
975 1.1 christos }
976 1.1 christos
977 1.1 christos ardata->cache = 0;
978 1.1 christos rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
979 1.1 christos stringbase = ((char *) rbase
980 1.1 christos + ardata->symdef_count * BSD_SYMDEF_SIZE
981 1.1 christos + BSD_STRING_COUNT_SIZE);
982 1.1 christos amt = ardata->symdef_count * sizeof (carsym);
983 1.1 christos ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
984 1.1 christos if (!ardata->symdefs)
985 1.1 christos return FALSE;
986 1.1 christos
987 1.1 christos for (counter = 0, set = ardata->symdefs;
988 1.1 christos counter < ardata->symdef_count;
989 1.1 christos counter++, set++, rbase += BSD_SYMDEF_SIZE)
990 1.1 christos {
991 1.1 christos set->name = H_GET_32 (abfd, rbase) + stringbase;
992 1.1 christos set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
993 1.1 christos }
994 1.1 christos
995 1.1 christos ardata->first_file_filepos = bfd_tell (abfd);
996 1.1 christos /* Pad to an even boundary if you have to. */
997 1.1 christos ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
998 1.1 christos /* FIXME, we should provide some way to free raw_ardata when
999 1.1 christos we are done using the strings from it. For now, it seems
1000 1.1 christos to be allocated on an objalloc anyway... */
1001 1.1 christos bfd_has_map (abfd) = TRUE;
1002 1.1 christos return TRUE;
1003 1.1 christos }
1004 1.1 christos
1005 1.1 christos /* Read a COFF archive symbol table. Returns FALSE on error, TRUE
1006 1.1 christos otherwise. */
1007 1.1 christos
1008 1.1 christos static bfd_boolean
1009 1.1 christos do_slurp_coff_armap (bfd *abfd)
1010 1.1 christos {
1011 1.1 christos struct areltdata *mapdata;
1012 1.1 christos int *raw_armap, *rawptr;
1013 1.1 christos struct artdata *ardata = bfd_ardata (abfd);
1014 1.1 christos char *stringbase;
1015 1.1 christos bfd_size_type stringsize;
1016 1.1 christos bfd_size_type parsed_size;
1017 1.1 christos carsym *carsyms;
1018 1.1 christos bfd_size_type nsymz; /* Number of symbols in armap. */
1019 1.1 christos bfd_vma (*swap) (const void *);
1020 1.1 christos char int_buf[sizeof (long)];
1021 1.1 christos bfd_size_type carsym_size, ptrsize;
1022 1.1 christos unsigned int i;
1023 1.1 christos
1024 1.1.1.2 christos mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1025 1.1 christos if (mapdata == NULL)
1026 1.1 christos return FALSE;
1027 1.1 christos parsed_size = mapdata->parsed_size;
1028 1.1 christos free (mapdata);
1029 1.1 christos
1030 1.1 christos if (bfd_bread (int_buf, 4, abfd) != 4)
1031 1.1 christos {
1032 1.1 christos if (bfd_get_error () != bfd_error_system_call)
1033 1.1 christos bfd_set_error (bfd_error_malformed_archive);
1034 1.1 christos return FALSE;
1035 1.1 christos }
1036 1.1 christos /* It seems that all numeric information in a coff archive is always
1037 1.1 christos in big endian format, nomatter the host or target. */
1038 1.1 christos swap = bfd_getb32;
1039 1.1 christos nsymz = bfd_getb32 (int_buf);
1040 1.1 christos stringsize = parsed_size - (4 * nsymz) - 4;
1041 1.1 christos
1042 1.1 christos /* ... except that some archive formats are broken, and it may be our
1043 1.1 christos fault - the i960 little endian coff sometimes has big and sometimes
1044 1.1 christos little, because our tools changed. Here's a horrible hack to clean
1045 1.1 christos up the crap. */
1046 1.1 christos
1047 1.1 christos if (stringsize > 0xfffff
1048 1.1 christos && bfd_get_arch (abfd) == bfd_arch_i960
1049 1.1 christos && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1050 1.1 christos {
1051 1.1 christos /* This looks dangerous, let's do it the other way around. */
1052 1.1 christos nsymz = bfd_getl32 (int_buf);
1053 1.1 christos stringsize = parsed_size - (4 * nsymz) - 4;
1054 1.1 christos swap = bfd_getl32;
1055 1.1 christos }
1056 1.1 christos
1057 1.1 christos /* The coff armap must be read sequentially. So we construct a
1058 1.1 christos bsd-style one in core all at once, for simplicity. */
1059 1.1 christos
1060 1.1 christos if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
1061 1.1 christos return FALSE;
1062 1.1 christos
1063 1.1 christos carsym_size = (nsymz * sizeof (carsym));
1064 1.1 christos ptrsize = (4 * nsymz);
1065 1.1 christos
1066 1.1 christos if (carsym_size + stringsize + 1 <= carsym_size)
1067 1.1 christos return FALSE;
1068 1.1 christos
1069 1.1 christos ardata->symdefs = (struct carsym *) bfd_zalloc (abfd,
1070 1.1 christos carsym_size + stringsize + 1);
1071 1.1 christos if (ardata->symdefs == NULL)
1072 1.1 christos return FALSE;
1073 1.1 christos carsyms = ardata->symdefs;
1074 1.1 christos stringbase = ((char *) ardata->symdefs) + carsym_size;
1075 1.1 christos
1076 1.1 christos /* Allocate and read in the raw offsets. */
1077 1.1 christos raw_armap = (int *) bfd_alloc (abfd, ptrsize);
1078 1.1 christos if (raw_armap == NULL)
1079 1.1 christos goto release_symdefs;
1080 1.1 christos if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
1081 1.1 christos || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
1082 1.1 christos {
1083 1.1 christos if (bfd_get_error () != bfd_error_system_call)
1084 1.1 christos bfd_set_error (bfd_error_malformed_archive);
1085 1.1.1.2 christos goto release_raw_armap;
1086 1.1 christos }
1087 1.1.1.2 christos
1088 1.1.1.2 christos /* OK, build the carsyms. */
1089 1.1 christos for (i = 0; i < nsymz && stringsize > 0; i++)
1090 1.1 christos {
1091 1.1 christos bfd_size_type len;
1092 1.1.1.2 christos
1093 1.1.1.2 christos rawptr = raw_armap + i;
1094 1.1.1.2 christos carsyms->file_offset = swap ((bfd_byte *) rawptr);
1095 1.1.1.2 christos carsyms->name = stringbase;
1096 1.1.1.2 christos /* PR 17512: file: 4a1d50c1. */
1097 1.1.1.2 christos len = strnlen (stringbase, stringsize);
1098 1.1 christos if (len < stringsize)
1099 1.1 christos len ++;
1100 1.1 christos stringbase += len;
1101 1.1 christos stringsize -= len;
1102 1.1 christos carsyms++;
1103 1.1 christos }
1104 1.1 christos *stringbase = 0;
1105 1.1 christos
1106 1.1 christos ardata->symdef_count = nsymz;
1107 1.1 christos ardata->first_file_filepos = bfd_tell (abfd);
1108 1.1 christos /* Pad to an even boundary if you have to. */
1109 1.1 christos ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1110 1.1 christos
1111 1.1 christos bfd_has_map (abfd) = TRUE;
1112 1.1 christos bfd_release (abfd, raw_armap);
1113 1.1 christos
1114 1.1 christos /* Check for a second archive header (as used by PE). */
1115 1.1 christos {
1116 1.1 christos struct areltdata *tmp;
1117 1.1 christos
1118 1.1 christos bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
1119 1.1 christos tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1120 1.1 christos if (tmp != NULL)
1121 1.1 christos {
1122 1.1 christos if (tmp->arch_header[0] == '/'
1123 1.1 christos && tmp->arch_header[1] == ' ')
1124 1.1.1.2 christos {
1125 1.1 christos ardata->first_file_filepos +=
1126 1.1 christos (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
1127 1.1 christos }
1128 1.1 christos free (tmp);
1129 1.1 christos }
1130 1.1 christos }
1131 1.1 christos
1132 1.1 christos return TRUE;
1133 1.1 christos
1134 1.1 christos release_raw_armap:
1135 1.1 christos bfd_release (abfd, raw_armap);
1136 1.1 christos release_symdefs:
1137 1.1 christos bfd_release (abfd, (ardata)->symdefs);
1138 1.1 christos return FALSE;
1139 1.1 christos }
1140 1.1 christos
1141 1.1 christos /* This routine can handle either coff-style or bsd-style armaps
1142 1.1 christos (archive symbol table). Returns FALSE on error, TRUE otherwise */
1143 1.1 christos
1144 1.1 christos bfd_boolean
1145 1.1 christos bfd_slurp_armap (bfd *abfd)
1146 1.1 christos {
1147 1.1 christos char nextname[17];
1148 1.1 christos int i = bfd_bread (nextname, 16, abfd);
1149 1.1 christos
1150 1.1 christos if (i == 0)
1151 1.1 christos return TRUE;
1152 1.1 christos if (i != 16)
1153 1.1 christos return FALSE;
1154 1.1 christos
1155 1.1 christos if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1156 1.1 christos return FALSE;
1157 1.1 christos
1158 1.1 christos if (CONST_STRNEQ (nextname, "__.SYMDEF ")
1159 1.1 christos || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */
1160 1.1 christos return do_slurp_bsd_armap (abfd);
1161 1.1.1.3 christos else if (CONST_STRNEQ (nextname, "/ "))
1162 1.1 christos return do_slurp_coff_armap (abfd);
1163 1.1.1.3 christos else if (CONST_STRNEQ (nextname, "/SYM64/ "))
1164 1.1 christos {
1165 1.1 christos /* 64bit (Irix 6) archive. */
1166 1.1 christos #ifdef BFD64
1167 1.1 christos return _bfd_archive_64_bit_slurp_armap (abfd);
1168 1.1 christos #else
1169 1.1 christos bfd_set_error (bfd_error_wrong_format);
1170 1.1 christos return FALSE;
1171 1.1 christos #endif
1172 1.1 christos }
1173 1.1 christos else if (CONST_STRNEQ (nextname, "#1/20 "))
1174 1.1 christos {
1175 1.1 christos /* Mach-O has a special name for armap when the map is sorted by name.
1176 1.1 christos However because this name has a space it is slightly more difficult
1177 1.1 christos to check it. */
1178 1.1 christos struct ar_hdr hdr;
1179 1.1 christos char extname[21];
1180 1.1 christos
1181 1.1 christos if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1182 1.1 christos return FALSE;
1183 1.1 christos /* Read the extended name. We know its length. */
1184 1.1.1.2 christos if (bfd_bread (extname, 20, abfd) != 20)
1185 1.1 christos return FALSE;
1186 1.1 christos if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0)
1187 1.1 christos return FALSE;
1188 1.1 christos extname[20] = 0;
1189 1.1 christos if (CONST_STRNEQ (extname, "__.SYMDEF SORTED")
1190 1.1 christos || CONST_STRNEQ (extname, "__.SYMDEF"))
1191 1.1 christos return do_slurp_bsd_armap (abfd);
1192 1.1 christos }
1193 1.1 christos
1194 1.1 christos bfd_has_map (abfd) = FALSE;
1195 1.1 christos return TRUE;
1196 1.1 christos }
1197 1.1 christos
1198 1.1 christos /* Returns FALSE on error, TRUE otherwise. */
1200 1.1 christos /* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
1201 1.1 christos header is in a slightly different order and the map name is '/'.
1202 1.1 christos This flavour is used by hp300hpux. */
1203 1.1 christos
1204 1.1 christos #define HPUX_SYMDEF_COUNT_SIZE 2
1205 1.1 christos
1206 1.1 christos bfd_boolean
1207 1.1 christos bfd_slurp_bsd_armap_f2 (bfd *abfd)
1208 1.1 christos {
1209 1.1 christos struct areltdata *mapdata;
1210 1.1 christos char nextname[17];
1211 1.1 christos unsigned int counter;
1212 1.1 christos bfd_byte *raw_armap, *rbase;
1213 1.1 christos struct artdata *ardata = bfd_ardata (abfd);
1214 1.1 christos char *stringbase;
1215 1.1 christos unsigned int stringsize;
1216 1.1 christos unsigned int left;
1217 1.1 christos bfd_size_type amt;
1218 1.1 christos carsym *set;
1219 1.1 christos int i = bfd_bread (nextname, 16, abfd);
1220 1.1 christos
1221 1.1 christos if (i == 0)
1222 1.1 christos return TRUE;
1223 1.1 christos if (i != 16)
1224 1.1 christos return FALSE;
1225 1.1 christos
1226 1.1 christos /* The archive has at least 16 bytes in it. */
1227 1.1 christos if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1228 1.1 christos return FALSE;
1229 1.1 christos
1230 1.1 christos if (CONST_STRNEQ (nextname, "__.SYMDEF ")
1231 1.1 christos || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */
1232 1.1 christos return do_slurp_bsd_armap (abfd);
1233 1.1 christos
1234 1.1 christos if (! CONST_STRNEQ (nextname, "/ "))
1235 1.1 christos {
1236 1.1 christos bfd_has_map (abfd) = FALSE;
1237 1.1 christos return TRUE;
1238 1.1 christos }
1239 1.1 christos
1240 1.1 christos mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1241 1.1.1.2 christos if (mapdata == NULL)
1242 1.1 christos return FALSE;
1243 1.1 christos
1244 1.1 christos if (mapdata->parsed_size < HPUX_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE)
1245 1.1 christos {
1246 1.1 christos free (mapdata);
1247 1.1 christos wrong_format:
1248 1.1 christos bfd_set_error (bfd_error_wrong_format);
1249 1.1 christos byebye:
1250 1.1.1.2 christos return FALSE;
1251 1.1.1.2 christos }
1252 1.1 christos left = mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE - BSD_STRING_COUNT_SIZE;
1253 1.1 christos
1254 1.1 christos amt = mapdata->parsed_size;
1255 1.1 christos free (mapdata);
1256 1.1 christos
1257 1.1 christos raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt);
1258 1.1 christos if (raw_armap == NULL)
1259 1.1 christos goto byebye;
1260 1.1 christos
1261 1.1 christos if (bfd_bread (raw_armap, amt, abfd) != amt)
1262 1.1 christos {
1263 1.1 christos if (bfd_get_error () != bfd_error_system_call)
1264 1.1 christos bfd_set_error (bfd_error_malformed_archive);
1265 1.1 christos goto byebye;
1266 1.1 christos }
1267 1.1 christos
1268 1.1 christos ardata->symdef_count = H_GET_16 (abfd, raw_armap);
1269 1.1 christos
1270 1.1 christos ardata->cache = 0;
1271 1.1 christos
1272 1.1 christos stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
1273 1.1 christos if (stringsize > left)
1274 1.1 christos goto wrong_format;
1275 1.1 christos left -= stringsize;
1276 1.1 christos
1277 1.1 christos /* Skip sym count and string sz. */
1278 1.1 christos stringbase = ((char *) raw_armap
1279 1.1 christos + HPUX_SYMDEF_COUNT_SIZE
1280 1.1 christos + BSD_STRING_COUNT_SIZE);
1281 1.1 christos rbase = (bfd_byte *) stringbase + stringsize;
1282 1.1 christos amt = ardata->symdef_count * BSD_SYMDEF_SIZE;
1283 1.1 christos if (amt > left)
1284 1.1 christos goto wrong_format;
1285 1.1 christos
1286 1.1 christos ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
1287 1.1 christos if (!ardata->symdefs)
1288 1.1 christos return FALSE;
1289 1.1 christos
1290 1.1 christos for (counter = 0, set = ardata->symdefs;
1291 1.1 christos counter < ardata->symdef_count;
1292 1.1 christos counter++, set++, rbase += BSD_SYMDEF_SIZE)
1293 1.1 christos {
1294 1.1 christos set->name = H_GET_32 (abfd, rbase) + stringbase;
1295 1.1 christos set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
1296 1.1 christos }
1297 1.1 christos
1298 1.1 christos ardata->first_file_filepos = bfd_tell (abfd);
1299 1.1 christos /* Pad to an even boundary if you have to. */
1300 1.1 christos ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1301 1.1 christos /* FIXME, we should provide some way to free raw_ardata when
1302 1.1 christos we are done using the strings from it. For now, it seems
1303 1.1 christos to be allocated on an objalloc anyway... */
1304 1.1 christos bfd_has_map (abfd) = TRUE;
1305 1.1 christos return TRUE;
1306 1.1 christos }
1307 1.1 christos
1308 1.1 christos /** Extended name table.
1310 1.1 christos
1311 1.1 christos Normally archives support only 14-character filenames.
1312 1.1 christos
1313 1.1 christos Intel has extended the format: longer names are stored in a special
1314 1.1 christos element (the first in the archive, or second if there is an armap);
1315 1.1 christos the name in the ar_hdr is replaced by <space><index into filename
1316 1.1 christos element>. Index is the P.R. of an int (decimal). Data General have
1317 1.1 christos extended the format by using the prefix // for the special element. */
1318 1.1 christos
1319 1.1 christos /* Returns FALSE on error, TRUE otherwise. */
1320 1.1 christos
1321 1.1 christos bfd_boolean
1322 1.1 christos _bfd_slurp_extended_name_table (bfd *abfd)
1323 1.1 christos {
1324 1.1 christos char nextname[17];
1325 1.1 christos struct areltdata *namedata;
1326 1.1 christos bfd_size_type amt;
1327 1.1 christos
1328 1.1 christos /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
1329 1.1 christos we probably don't want to return TRUE. */
1330 1.1 christos if (bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET) != 0)
1331 1.1 christos return FALSE;
1332 1.1 christos
1333 1.1 christos if (bfd_bread (nextname, 16, abfd) == 16)
1334 1.1 christos {
1335 1.1 christos if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1336 1.1 christos return FALSE;
1337 1.1 christos
1338 1.1 christos if (! CONST_STRNEQ (nextname, "ARFILENAMES/ ")
1339 1.1 christos && ! CONST_STRNEQ (nextname, "// "))
1340 1.1 christos {
1341 1.1 christos bfd_ardata (abfd)->extended_names = NULL;
1342 1.1 christos bfd_ardata (abfd)->extended_names_size = 0;
1343 1.1 christos return TRUE;
1344 1.1 christos }
1345 1.1 christos
1346 1.1 christos namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1347 1.1 christos if (namedata == NULL)
1348 1.1 christos return FALSE;
1349 1.1 christos
1350 1.1 christos amt = namedata->parsed_size;
1351 1.1 christos if (amt + 1 == 0)
1352 1.1 christos goto byebye;
1353 1.1.1.2 christos
1354 1.1.1.2 christos bfd_ardata (abfd)->extended_names_size = amt;
1355 1.1.1.2 christos bfd_ardata (abfd)->extended_names = (char *) bfd_zalloc (abfd, amt + 1);
1356 1.1 christos if (bfd_ardata (abfd)->extended_names == NULL)
1357 1.1 christos {
1358 1.1 christos byebye:
1359 1.1 christos free (namedata);
1360 1.1 christos bfd_ardata (abfd)->extended_names = NULL;
1361 1.1 christos bfd_ardata (abfd)->extended_names_size = 0;
1362 1.1 christos return FALSE;
1363 1.1 christos }
1364 1.1 christos
1365 1.1 christos if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1366 1.1 christos {
1367 1.1 christos if (bfd_get_error () != bfd_error_system_call)
1368 1.1 christos bfd_set_error (bfd_error_malformed_archive);
1369 1.1 christos bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1370 1.1 christos bfd_ardata (abfd)->extended_names = NULL;
1371 1.1 christos goto byebye;
1372 1.1.1.2 christos }
1373 1.1 christos
1374 1.1 christos /* Since the archive is supposed to be printable if it contains
1375 1.1 christos text, the entries in the list are newline-padded, not null
1376 1.1 christos padded. In SVR4-style archives, the names also have a
1377 1.1.1.2 christos trailing '/'. DOS/NT created archive often have \ in them
1378 1.1 christos We'll fix all problems here. */
1379 1.1 christos {
1380 1.1 christos char *ext_names = bfd_ardata (abfd)->extended_names;
1381 1.1 christos char *temp = ext_names;
1382 1.1 christos char *limit = temp + namedata->parsed_size;
1383 1.1 christos
1384 1.1 christos for (; temp < limit; ++temp)
1385 1.1 christos {
1386 1.1 christos if (*temp == ARFMAG[1])
1387 1.1 christos temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1388 1.1 christos if (*temp == '\\')
1389 1.1 christos *temp = '/';
1390 1.1 christos }
1391 1.1 christos *limit = '\0';
1392 1.1 christos }
1393 1.1.1.2 christos
1394 1.1 christos /* Pad to an even boundary if you have to. */
1395 1.1 christos bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1396 1.1 christos bfd_ardata (abfd)->first_file_filepos +=
1397 1.1 christos (bfd_ardata (abfd)->first_file_filepos) % 2;
1398 1.1 christos
1399 1.1 christos free (namedata);
1400 1.1 christos }
1401 1.1 christos return TRUE;
1402 1.1 christos }
1403 1.1 christos
1404 1.1 christos #ifdef VMS
1405 1.1 christos
1406 1.1 christos /* Return a copy of the stuff in the filename between any :]> and a
1407 1.1 christos semicolon. */
1408 1.1 christos
1409 1.1 christos static const char *
1410 1.1 christos normalize (bfd *abfd, const char *file)
1411 1.1 christos {
1412 1.1 christos const char *first;
1413 1.1 christos const char *last;
1414 1.1 christos char *copy;
1415 1.1 christos
1416 1.1 christos first = file + strlen (file) - 1;
1417 1.1 christos last = first + 1;
1418 1.1 christos
1419 1.1 christos while (first != file)
1420 1.1 christos {
1421 1.1 christos if (*first == ';')
1422 1.1 christos last = first;
1423 1.1 christos if (*first == ':' || *first == ']' || *first == '>')
1424 1.1 christos {
1425 1.1 christos first++;
1426 1.1 christos break;
1427 1.1 christos }
1428 1.1 christos first--;
1429 1.1 christos }
1430 1.1 christos
1431 1.1 christos copy = bfd_alloc (abfd, last - first + 1);
1432 1.1 christos if (copy == NULL)
1433 1.1 christos return NULL;
1434 1.1 christos
1435 1.1 christos memcpy (copy, first, last - first);
1436 1.1 christos copy[last - first] = 0;
1437 1.1 christos
1438 1.1 christos return copy;
1439 1.1 christos }
1440 1.1 christos
1441 1.1 christos #else
1442 1.1 christos static const char *
1443 1.1 christos normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
1444 1.1 christos {
1445 1.1 christos return lbasename (file);
1446 1.1 christos }
1447 1.1 christos #endif
1448 1.1 christos
1449 1.1 christos /* Adjust a relative path name based on the reference path.
1450 1.1 christos For example:
1451 1.1 christos
1452 1.1 christos Relative path Reference path Result
1453 1.1 christos ------------- -------------- ------
1454 1.1 christos bar.o lib.a bar.o
1455 1.1 christos foo/bar.o lib.a foo/bar.o
1456 1.1 christos bar.o foo/lib.a ../bar.o
1457 1.1 christos foo/bar.o baz/lib.a ../foo/bar.o
1458 1.1 christos bar.o ../lib.a <parent of current dir>/bar.o
1459 1.1 christos ; ../bar.o ../lib.a bar.o
1460 1.1 christos ; ../bar.o lib.a ../bar.o
1461 1.1 christos foo/bar.o ../lib.a <parent of current dir>/foo/bar.o
1462 1.1 christos bar.o ../../lib.a <grandparent>/<parent>/bar.o
1463 1.1 christos bar.o foo/baz/lib.a ../../bar.o
1464 1.1 christos
1465 1.1 christos Note - the semicolons above are there to prevent the BFD chew
1466 1.1 christos utility from interpreting those lines as prototypes to put into
1467 1.1 christos the autogenerated bfd.h header...
1468 1.1 christos
1469 1.1 christos Note - the string is returned in a static buffer. */
1470 1.1 christos
1471 1.1 christos static const char *
1472 1.1 christos adjust_relative_path (const char * path, const char * ref_path)
1473 1.1 christos {
1474 1.1 christos static char *pathbuf = NULL;
1475 1.1 christos static unsigned int pathbuf_len = 0;
1476 1.1 christos const char *pathp;
1477 1.1 christos const char *refp;
1478 1.1 christos char * lpath;
1479 1.1 christos char * rpath;
1480 1.1 christos unsigned int len;
1481 1.1 christos unsigned int dir_up = 0;
1482 1.1 christos unsigned int dir_down = 0;
1483 1.1 christos char *newp;
1484 1.1 christos char * pwd = getpwd ();
1485 1.1 christos const char * down;
1486 1.1 christos
1487 1.1 christos /* Remove symlinks, '.' and '..' from the paths, if possible. */
1488 1.1 christos lpath = lrealpath (path);
1489 1.1 christos pathp = lpath == NULL ? path : lpath;
1490 1.1 christos
1491 1.1 christos rpath = lrealpath (ref_path);
1492 1.1 christos refp = rpath == NULL ? ref_path : rpath;
1493 1.1 christos
1494 1.1 christos /* Remove common leading path elements. */
1495 1.1 christos for (;;)
1496 1.1 christos {
1497 1.1 christos const char *e1 = pathp;
1498 1.1 christos const char *e2 = refp;
1499 1.1 christos
1500 1.1 christos while (*e1 && ! IS_DIR_SEPARATOR (*e1))
1501 1.1 christos ++e1;
1502 1.1 christos while (*e2 && ! IS_DIR_SEPARATOR (*e2))
1503 1.1 christos ++e2;
1504 1.1 christos if (*e1 == '\0' || *e2 == '\0' || e1 - pathp != e2 - refp
1505 1.1 christos || filename_ncmp (pathp, refp, e1 - pathp) != 0)
1506 1.1 christos break;
1507 1.1 christos pathp = e1 + 1;
1508 1.1 christos refp = e2 + 1;
1509 1.1 christos }
1510 1.1 christos
1511 1.1 christos len = strlen (pathp) + 1;
1512 1.1 christos /* For each leading path element in the reference path,
1513 1.1 christos insert "../" into the path. */
1514 1.1 christos for (; *refp; ++refp)
1515 1.1 christos if (IS_DIR_SEPARATOR (*refp))
1516 1.1 christos {
1517 1.1 christos /* PR 12710: If the path element is "../" then instead of
1518 1.1 christos inserting "../" we need to insert the name of the directory
1519 1.1 christos at the current level. */
1520 1.1 christos if (refp > ref_path + 1
1521 1.1 christos && refp[-1] == '.'
1522 1.1 christos && refp[-2] == '.')
1523 1.1 christos dir_down ++;
1524 1.1 christos else
1525 1.1 christos dir_up ++;
1526 1.1 christos }
1527 1.1 christos
1528 1.1 christos /* If the lrealpath calls above succeeded then we should never
1529 1.1 christos see dir_up and dir_down both being non-zero. */
1530 1.1 christos
1531 1.1 christos len += 3 * dir_up;
1532 1.1 christos
1533 1.1 christos if (dir_down)
1534 1.1 christos {
1535 1.1 christos down = pwd + strlen (pwd) - 1;
1536 1.1 christos
1537 1.1 christos while (dir_down && down > pwd)
1538 1.1 christos {
1539 1.1 christos if (IS_DIR_SEPARATOR (*down))
1540 1.1 christos --dir_down;
1541 1.1 christos }
1542 1.1 christos BFD_ASSERT (dir_down == 0);
1543 1.1 christos len += strlen (down) + 1;
1544 1.1 christos }
1545 1.1 christos else
1546 1.1 christos down = NULL;
1547 1.1 christos
1548 1.1 christos if (len > pathbuf_len)
1549 1.1 christos {
1550 1.1 christos if (pathbuf != NULL)
1551 1.1 christos free (pathbuf);
1552 1.1 christos pathbuf_len = 0;
1553 1.1 christos pathbuf = (char *) bfd_malloc (len);
1554 1.1 christos if (pathbuf == NULL)
1555 1.1 christos goto out;
1556 1.1 christos pathbuf_len = len;
1557 1.1 christos }
1558 1.1 christos
1559 1.1 christos newp = pathbuf;
1560 1.1 christos while (dir_up-- > 0)
1561 1.1 christos {
1562 1.1 christos /* FIXME: Support Windows style path separators as well. */
1563 1.1 christos strcpy (newp, "../");
1564 1.1 christos newp += 3;
1565 1.1 christos }
1566 1.1 christos
1567 1.1 christos if (down)
1568 1.1 christos sprintf (newp, "%s/%s", down, pathp);
1569 1.1 christos else
1570 1.1 christos strcpy (newp, pathp);
1571 1.1 christos
1572 1.1 christos out:
1573 1.1 christos free (lpath);
1574 1.1 christos free (rpath);
1575 1.1 christos return pathbuf;
1576 1.1 christos }
1577 1.1 christos
1578 1.1 christos /* Build a BFD style extended name table. */
1579 1.1 christos
1580 1.1 christos bfd_boolean
1581 1.1 christos _bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1582 1.1 christos char **tabloc,
1583 1.1 christos bfd_size_type *tablen,
1584 1.1 christos const char **name)
1585 1.1 christos {
1586 1.1 christos *name = "ARFILENAMES/";
1587 1.1 christos return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen);
1588 1.1 christos }
1589 1.1 christos
1590 1.1 christos /* Build an SVR4 style extended name table. */
1591 1.1 christos
1592 1.1 christos bfd_boolean
1593 1.1 christos _bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1594 1.1 christos char **tabloc,
1595 1.1 christos bfd_size_type *tablen,
1596 1.1 christos const char **name)
1597 1.1 christos {
1598 1.1 christos *name = "//";
1599 1.1 christos return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen);
1600 1.1 christos }
1601 1.1 christos
1602 1.1 christos /* Follows archive_head and produces an extended name table if
1603 1.1 christos necessary. Returns (in tabloc) a pointer to an extended name
1604 1.1 christos table, and in tablen the length of the table. If it makes an entry
1605 1.1 christos it clobbers the filename so that the element may be written without
1606 1.1 christos further massage. Returns TRUE if it ran successfully, FALSE if
1607 1.1 christos something went wrong. A successful return may still involve a
1608 1.1 christos zero-length tablen! */
1609 1.1 christos
1610 1.1 christos bfd_boolean
1611 1.1 christos _bfd_construct_extended_name_table (bfd *abfd,
1612 1.1 christos bfd_boolean trailing_slash,
1613 1.1 christos char **tabloc,
1614 1.1 christos bfd_size_type *tablen)
1615 1.1 christos {
1616 1.1 christos unsigned int maxname = ar_maxnamelen (abfd);
1617 1.1 christos bfd_size_type total_namelen = 0;
1618 1.1 christos bfd *current;
1619 1.1 christos char *strptr;
1620 1.1 christos const char *last_filename;
1621 1.1 christos long last_stroff;
1622 1.1 christos
1623 1.1 christos *tablen = 0;
1624 1.1 christos last_filename = NULL;
1625 1.1 christos
1626 1.1 christos /* Figure out how long the table should be. */
1627 1.1 christos for (current = abfd->archive_head;
1628 1.1 christos current != NULL;
1629 1.1 christos current = current->archive_next)
1630 1.1 christos {
1631 1.1 christos const char *normal;
1632 1.1 christos unsigned int thislen;
1633 1.1 christos
1634 1.1 christos if (bfd_is_thin_archive (abfd))
1635 1.1 christos {
1636 1.1 christos const char *filename = current->filename;
1637 1.1 christos
1638 1.1 christos /* If the element being added is a member of another archive
1639 1.1 christos (i.e., we are flattening), use the containing archive's name. */
1640 1.1 christos if (current->my_archive
1641 1.1 christos && ! bfd_is_thin_archive (current->my_archive))
1642 1.1 christos filename = current->my_archive->filename;
1643 1.1 christos
1644 1.1 christos /* If the path is the same as the previous path seen,
1645 1.1 christos reuse it. This can happen when flattening a thin
1646 1.1 christos archive that contains other archives. */
1647 1.1 christos if (last_filename && filename_cmp (last_filename, filename) == 0)
1648 1.1 christos continue;
1649 1.1 christos
1650 1.1 christos last_filename = filename;
1651 1.1 christos
1652 1.1 christos /* If the path is relative, adjust it relative to
1653 1.1 christos the containing archive. */
1654 1.1 christos if (! IS_ABSOLUTE_PATH (filename)
1655 1.1 christos && ! IS_ABSOLUTE_PATH (abfd->filename))
1656 1.1 christos normal = adjust_relative_path (filename, abfd->filename);
1657 1.1 christos else
1658 1.1 christos normal = filename;
1659 1.1 christos
1660 1.1 christos /* In a thin archive, always store the full pathname
1661 1.1 christos in the extended name table. */
1662 1.1 christos total_namelen += strlen (normal) + 1;
1663 1.1 christos if (trailing_slash)
1664 1.1 christos /* Leave room for trailing slash. */
1665 1.1 christos ++total_namelen;
1666 1.1 christos
1667 1.1 christos continue;
1668 1.1 christos }
1669 1.1 christos
1670 1.1 christos normal = normalize (current, current->filename);
1671 1.1 christos if (normal == NULL)
1672 1.1 christos return FALSE;
1673 1.1 christos
1674 1.1 christos thislen = strlen (normal);
1675 1.1 christos
1676 1.1 christos if (thislen > maxname
1677 1.1 christos && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1678 1.1 christos thislen = maxname;
1679 1.1 christos
1680 1.1 christos if (thislen > maxname)
1681 1.1 christos {
1682 1.1 christos /* Add one to leave room for \n. */
1683 1.1 christos total_namelen += thislen + 1;
1684 1.1 christos if (trailing_slash)
1685 1.1 christos {
1686 1.1 christos /* Leave room for trailing slash. */
1687 1.1 christos ++total_namelen;
1688 1.1 christos }
1689 1.1 christos }
1690 1.1 christos else
1691 1.1 christos {
1692 1.1 christos struct ar_hdr *hdr = arch_hdr (current);
1693 1.1 christos if (filename_ncmp (normal, hdr->ar_name, thislen) != 0
1694 1.1 christos || (thislen < sizeof hdr->ar_name
1695 1.1 christos && hdr->ar_name[thislen] != ar_padchar (current)))
1696 1.1 christos {
1697 1.1 christos /* Must have been using extended format even though it
1698 1.1 christos didn't need to. Fix it to use normal format. */
1699 1.1 christos memcpy (hdr->ar_name, normal, thislen);
1700 1.1 christos if (thislen < maxname
1701 1.1 christos || (thislen == maxname && thislen < sizeof hdr->ar_name))
1702 1.1 christos hdr->ar_name[thislen] = ar_padchar (current);
1703 1.1 christos }
1704 1.1 christos }
1705 1.1 christos }
1706 1.1 christos
1707 1.1 christos if (total_namelen == 0)
1708 1.1 christos return TRUE;
1709 1.1 christos
1710 1.1 christos *tabloc = (char *) bfd_zalloc (abfd, total_namelen);
1711 1.1 christos if (*tabloc == NULL)
1712 1.1 christos return FALSE;
1713 1.1 christos
1714 1.1 christos *tablen = total_namelen;
1715 1.1 christos strptr = *tabloc;
1716 1.1 christos
1717 1.1 christos last_filename = NULL;
1718 1.1 christos last_stroff = 0;
1719 1.1 christos
1720 1.1 christos for (current = abfd->archive_head;
1721 1.1 christos current != NULL;
1722 1.1 christos current = current->archive_next)
1723 1.1 christos {
1724 1.1 christos const char *normal;
1725 1.1 christos unsigned int thislen;
1726 1.1 christos long stroff;
1727 1.1 christos const char *filename = current->filename;
1728 1.1 christos
1729 1.1 christos if (bfd_is_thin_archive (abfd))
1730 1.1 christos {
1731 1.1 christos /* If the element being added is a member of another archive
1732 1.1 christos (i.e., we are flattening), use the containing archive's name. */
1733 1.1 christos if (current->my_archive
1734 1.1 christos && ! bfd_is_thin_archive (current->my_archive))
1735 1.1 christos filename = current->my_archive->filename;
1736 1.1 christos /* If the path is the same as the previous path seen,
1737 1.1 christos reuse it. This can happen when flattening a thin
1738 1.1 christos archive that contains other archives.
1739 1.1 christos If the path is relative, adjust it relative to
1740 1.1 christos the containing archive. */
1741 1.1 christos if (last_filename && filename_cmp (last_filename, filename) == 0)
1742 1.1 christos normal = last_filename;
1743 1.1 christos else if (! IS_ABSOLUTE_PATH (filename)
1744 1.1 christos && ! IS_ABSOLUTE_PATH (abfd->filename))
1745 1.1 christos normal = adjust_relative_path (filename, abfd->filename);
1746 1.1 christos else
1747 1.1 christos normal = filename;
1748 1.1 christos }
1749 1.1 christos else
1750 1.1 christos {
1751 1.1 christos normal = normalize (current, filename);
1752 1.1 christos if (normal == NULL)
1753 1.1 christos return FALSE;
1754 1.1 christos }
1755 1.1 christos
1756 1.1 christos thislen = strlen (normal);
1757 1.1 christos if (thislen > maxname || bfd_is_thin_archive (abfd))
1758 1.1 christos {
1759 1.1 christos /* Works for now; may need to be re-engineered if we
1760 1.1 christos encounter an oddball archive format and want to
1761 1.1 christos generalise this hack. */
1762 1.1 christos struct ar_hdr *hdr = arch_hdr (current);
1763 1.1 christos if (normal == last_filename)
1764 1.1 christos stroff = last_stroff;
1765 1.1 christos else
1766 1.1 christos {
1767 1.1 christos strcpy (strptr, normal);
1768 1.1 christos if (! trailing_slash)
1769 1.1 christos strptr[thislen] = ARFMAG[1];
1770 1.1 christos else
1771 1.1 christos {
1772 1.1 christos strptr[thislen] = '/';
1773 1.1 christos strptr[thislen + 1] = ARFMAG[1];
1774 1.1 christos }
1775 1.1 christos stroff = strptr - *tabloc;
1776 1.1 christos last_stroff = stroff;
1777 1.1 christos }
1778 1.1 christos hdr->ar_name[0] = ar_padchar (current);
1779 1.1 christos if (bfd_is_thin_archive (abfd) && current->origin > 0)
1780 1.1 christos {
1781 1.1 christos int len = snprintf (hdr->ar_name + 1, maxname - 1, "%-ld:",
1782 1.1 christos stroff);
1783 1.1 christos _bfd_ar_spacepad (hdr->ar_name + 1 + len, maxname - 1 - len,
1784 1.1 christos "%-ld",
1785 1.1 christos current->origin - sizeof (struct ar_hdr));
1786 1.1 christos }
1787 1.1 christos else
1788 1.1 christos _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
1789 1.1 christos if (normal != last_filename)
1790 1.1 christos {
1791 1.1 christos strptr += thislen + 1;
1792 1.1 christos if (trailing_slash)
1793 1.1 christos ++strptr;
1794 1.1 christos last_filename = filename;
1795 1.1 christos }
1796 1.1 christos }
1797 1.1 christos }
1798 1.1 christos
1799 1.1 christos return TRUE;
1800 1.1 christos }
1801 1.1 christos
1802 1.1 christos /* Do not construct an extended name table but transforms name field into
1803 1.1 christos its extended form. */
1804 1.1 christos
1805 1.1 christos bfd_boolean
1806 1.1 christos _bfd_archive_bsd44_construct_extended_name_table (bfd *abfd,
1807 1.1 christos char **tabloc,
1808 1.1 christos bfd_size_type *tablen,
1809 1.1 christos const char **name)
1810 1.1 christos {
1811 1.1 christos unsigned int maxname = ar_maxnamelen (abfd);
1812 1.1 christos bfd *current;
1813 1.1 christos
1814 1.1 christos *tablen = 0;
1815 1.1 christos *tabloc = NULL;
1816 1.1 christos *name = NULL;
1817 1.1 christos
1818 1.1 christos for (current = abfd->archive_head;
1819 1.1 christos current != NULL;
1820 1.1 christos current = current->archive_next)
1821 1.1 christos {
1822 1.1 christos const char *normal = normalize (current, current->filename);
1823 1.1 christos int has_space = 0;
1824 1.1 christos unsigned int len;
1825 1.1 christos
1826 1.1 christos if (normal == NULL)
1827 1.1 christos return FALSE;
1828 1.1 christos
1829 1.1 christos for (len = 0; normal[len]; len++)
1830 1.1 christos if (normal[len] == ' ')
1831 1.1 christos has_space = 1;
1832 1.1 christos
1833 1.1 christos if (len > maxname || has_space)
1834 1.1 christos {
1835 1.1 christos struct ar_hdr *hdr = arch_hdr (current);
1836 1.1 christos
1837 1.1 christos len = (len + 3) & ~3;
1838 1.1 christos arch_eltdata (current)->extra_size = len;
1839 1.1 christos _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%lu", len);
1840 1.1 christos }
1841 1.1 christos }
1842 1.1 christos
1843 1.1 christos return TRUE;
1844 1.1 christos }
1845 1.1 christos
1846 1.1 christos /* Write an archive header. */
1848 1.1 christos
1849 1.1 christos bfd_boolean
1850 1.1 christos _bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1851 1.1 christos {
1852 1.1 christos struct ar_hdr *hdr = arch_hdr (abfd);
1853 1.1 christos
1854 1.1 christos if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1855 1.1 christos return FALSE;
1856 1.1 christos return TRUE;
1857 1.1 christos }
1858 1.1 christos
1859 1.1 christos /* Write an archive header using BSD4.4 convention. */
1860 1.1 christos
1861 1.1 christos bfd_boolean
1862 1.1 christos _bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
1863 1.1 christos {
1864 1.1 christos struct ar_hdr *hdr = arch_hdr (abfd);
1865 1.1 christos
1866 1.1 christos if (is_bsd44_extended_name (hdr->ar_name))
1867 1.1 christos {
1868 1.1 christos /* This is a BSD 4.4 extended name. */
1869 1.1 christos const char *fullname = normalize (abfd, abfd->filename);
1870 1.1 christos unsigned int len = strlen (fullname);
1871 1.1 christos unsigned int padded_len = (len + 3) & ~3;
1872 1.1 christos
1873 1.1 christos BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size);
1874 1.1 christos
1875 1.1 christos if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size),
1876 1.1 christos arch_eltdata (abfd)->parsed_size + padded_len))
1877 1.1 christos return FALSE;
1878 1.1 christos
1879 1.1 christos if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1880 1.1 christos return FALSE;
1881 1.1 christos
1882 1.1 christos if (bfd_bwrite (fullname, len, archive) != len)
1883 1.1 christos return FALSE;
1884 1.1 christos
1885 1.1 christos if (len & 3)
1886 1.1 christos {
1887 1.1 christos static const char pad[3] = { 0, 0, 0 };
1888 1.1 christos
1889 1.1 christos len = 4 - (len & 3);
1890 1.1 christos if (bfd_bwrite (pad, len, archive) != len)
1891 1.1 christos return FALSE;
1892 1.1 christos }
1893 1.1 christos }
1894 1.1 christos else
1895 1.1 christos {
1896 1.1 christos if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1897 1.1 christos return FALSE;
1898 1.1 christos }
1899 1.1 christos return TRUE;
1900 1.1 christos }
1901 1.1 christos
1902 1.1 christos /* A couple of functions for creating ar_hdrs. */
1904 1.1 christos
1905 1.1 christos #ifdef HPUX_LARGE_AR_IDS
1906 1.1 christos /* Function to encode large UID/GID values according to HP. */
1907 1.1 christos
1908 1.1 christos static void
1909 1.1 christos hpux_uid_gid_encode (char str[6], long int id)
1910 1.1 christos {
1911 1.1 christos int cnt;
1912 1.1 christos
1913 1.1 christos str[5] = '@' + (id & 3);
1914 1.1 christos id >>= 2;
1915 1.1 christos
1916 1.1 christos for (cnt = 4; cnt >= 0; --cnt, id >>= 6)
1917 1.1 christos str[cnt] = ' ' + (id & 0x3f);
1918 1.1 christos }
1919 1.1 christos #endif /* HPUX_LARGE_AR_IDS */
1920 1.1 christos
1921 1.1 christos #ifndef HAVE_GETUID
1922 1.1 christos #define getuid() 0
1923 1.1 christos #endif
1924 1.1 christos
1925 1.1 christos #ifndef HAVE_GETGID
1926 1.1 christos #define getgid() 0
1927 1.1 christos #endif
1928 1.1 christos
1929 1.1 christos /* Takes a filename, returns an arelt_data for it, or NULL if it can't
1930 1.1 christos make one. The filename must refer to a filename in the filesystem.
1931 1.1 christos The filename field of the ar_hdr will NOT be initialized. If member
1932 1.1 christos is set, and it's an in-memory bfd, we fake it. */
1933 1.1 christos
1934 1.1 christos static struct areltdata *
1935 1.1 christos bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
1936 1.1 christos {
1937 1.1 christos struct stat status;
1938 1.1 christos struct areltdata *ared;
1939 1.1 christos struct ar_hdr *hdr;
1940 1.1 christos bfd_size_type amt;
1941 1.1 christos
1942 1.1 christos if (member && (member->flags & BFD_IN_MEMORY) != 0)
1943 1.1 christos {
1944 1.1 christos /* Assume we just "made" the member, and fake it. */
1945 1.1 christos struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream;
1946 1.1 christos time (&status.st_mtime);
1947 1.1 christos status.st_uid = getuid ();
1948 1.1 christos status.st_gid = getgid ();
1949 1.1 christos status.st_mode = 0644;
1950 1.1 christos status.st_size = bim->size;
1951 1.1 christos }
1952 1.1 christos else if (stat (filename, &status) != 0)
1953 1.1 christos {
1954 1.1 christos bfd_set_error (bfd_error_system_call);
1955 1.1 christos return NULL;
1956 1.1 christos }
1957 1.1 christos
1958 1.1 christos /* If the caller requested that the BFD generate deterministic output,
1959 1.1 christos fake values for modification time, UID, GID, and file mode. */
1960 1.1 christos if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
1961 1.1.1.2 christos {
1962 1.1 christos status.st_mtime = 0;
1963 1.1 christos status.st_uid = 0;
1964 1.1 christos status.st_gid = 0;
1965 1.1 christos status.st_mode = 0644;
1966 1.1 christos }
1967 1.1 christos
1968 1.1 christos amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
1969 1.1 christos ared = (struct areltdata *) bfd_zmalloc (amt);
1970 1.1 christos if (ared == NULL)
1971 1.1 christos return NULL;
1972 1.1 christos hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1973 1.1 christos
1974 1.1 christos /* ar headers are space padded, not null padded! */
1975 1.1 christos memset (hdr, ' ', sizeof (struct ar_hdr));
1976 1.1 christos
1977 1.1 christos _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
1978 1.1 christos status.st_mtime);
1979 1.1 christos #ifdef HPUX_LARGE_AR_IDS
1980 1.1 christos /* HP has a very "special" way to handle UID/GID's with numeric values
1981 1.1 christos > 99999. */
1982 1.1 christos if (status.st_uid > 99999)
1983 1.1 christos hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid);
1984 1.1 christos else
1985 1.1 christos #endif
1986 1.1 christos _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
1987 1.1 christos status.st_uid);
1988 1.1 christos #ifdef HPUX_LARGE_AR_IDS
1989 1.1 christos /* HP has a very "special" way to handle UID/GID's with numeric values
1990 1.1 christos > 99999. */
1991 1.1 christos if (status.st_gid > 99999)
1992 1.1 christos hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid);
1993 1.1 christos else
1994 1.1 christos #endif
1995 1.1 christos _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
1996 1.1 christos status.st_gid);
1997 1.1 christos _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
1998 1.1 christos status.st_mode);
1999 1.1 christos if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size), status.st_size))
2000 1.1 christos {
2001 1.1 christos free (ared);
2002 1.1 christos return NULL;
2003 1.1 christos }
2004 1.1 christos memcpy (hdr->ar_fmag, ARFMAG, 2);
2005 1.1 christos ared->parsed_size = status.st_size;
2006 1.1 christos ared->arch_header = (char *) hdr;
2007 1.1 christos
2008 1.1 christos return ared;
2009 1.1 christos }
2010 1.1 christos
2011 1.1 christos /* Analogous to stat call. */
2012 1.1 christos
2013 1.1 christos int
2014 1.1 christos bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
2015 1.1 christos {
2016 1.1 christos struct ar_hdr *hdr;
2017 1.1 christos char *aloser;
2018 1.1.1.2 christos
2019 1.1.1.2 christos if (abfd->arelt_data == NULL)
2020 1.1.1.2 christos {
2021 1.1 christos bfd_set_error (bfd_error_invalid_operation);
2022 1.1 christos return -1;
2023 1.1 christos }
2024 1.1 christos
2025 1.1 christos hdr = arch_hdr (abfd);
2026 1.1 christos /* PR 17512: file: 3d9e9fe9. */
2027 1.1 christos if (hdr == NULL)
2028 1.1 christos return -1;
2029 1.1 christos #define foo(arelt, stelt, size) \
2030 1.1 christos buf->stelt = strtol (hdr->arelt, &aloser, size); \
2031 1.1 christos if (aloser == hdr->arelt) \
2032 1.1 christos return -1;
2033 1.1 christos
2034 1.1 christos /* Some platforms support special notations for large IDs. */
2035 1.1 christos #ifdef HPUX_LARGE_AR_IDS
2036 1.1 christos # define foo2(arelt, stelt, size) \
2037 1.1 christos if (hdr->arelt[5] == ' ') \
2038 1.1 christos { \
2039 1.1 christos foo (arelt, stelt, size); \
2040 1.1 christos } \
2041 1.1 christos else \
2042 1.1 christos { \
2043 1.1 christos int cnt; \
2044 1.1 christos for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
2045 1.1 christos { \
2046 1.1 christos if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
2047 1.1 christos return -1; \
2048 1.1 christos buf->stelt <<= 6; \
2049 1.1 christos buf->stelt += hdr->arelt[cnt] - ' '; \
2050 1.1 christos } \
2051 1.1 christos if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
2052 1.1 christos return -1; \
2053 1.1 christos buf->stelt <<= 2; \
2054 1.1 christos buf->stelt += hdr->arelt[5] - '@'; \
2055 1.1 christos }
2056 1.1 christos #else
2057 1.1 christos # define foo2(arelt, stelt, size) foo (arelt, stelt, size)
2058 1.1 christos #endif
2059 1.1 christos
2060 1.1 christos foo (ar_date, st_mtime, 10);
2061 1.1 christos foo2 (ar_uid, st_uid, 10);
2062 1.1 christos foo2 (ar_gid, st_gid, 10);
2063 1.1 christos foo (ar_mode, st_mode, 8);
2064 1.1 christos
2065 1.1 christos buf->st_size = arch_eltdata (abfd)->parsed_size;
2066 1.1 christos
2067 1.1 christos return 0;
2068 1.1 christos }
2069 1.1 christos
2070 1.1 christos void
2071 1.1 christos bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2072 1.1 christos {
2073 1.1 christos /* FIXME: This interacts unpleasantly with ar's quick-append option.
2074 1.1 christos Fortunately ic960 users will never use that option. Fixing this
2075 1.1 christos is very hard; fortunately I know how to do it and will do so once
2076 1.1 christos intel's release is out the door. */
2077 1.1 christos
2078 1.1 christos struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2079 1.1 christos size_t length;
2080 1.1 christos const char *filename;
2081 1.1 christos size_t maxlen = ar_maxnamelen (abfd);
2082 1.1 christos
2083 1.1 christos if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
2084 1.1 christos {
2085 1.1 christos bfd_bsd_truncate_arname (abfd, pathname, arhdr);
2086 1.1 christos return;
2087 1.1 christos }
2088 1.1 christos
2089 1.1 christos filename = normalize (abfd, pathname);
2090 1.1 christos if (filename == NULL)
2091 1.1 christos {
2092 1.1 christos /* FIXME */
2093 1.1 christos abort ();
2094 1.1 christos }
2095 1.1 christos
2096 1.1 christos length = strlen (filename);
2097 1.1 christos
2098 1.1 christos if (length <= maxlen)
2099 1.1 christos memcpy (hdr->ar_name, filename, length);
2100 1.1 christos
2101 1.1 christos /* Add the padding character if there is room for it. */
2102 1.1 christos if (length < maxlen
2103 1.1 christos || (length == maxlen && length < sizeof hdr->ar_name))
2104 1.1 christos (hdr->ar_name)[length] = ar_padchar (abfd);
2105 1.1 christos }
2106 1.1 christos
2107 1.1 christos void
2108 1.1 christos bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2109 1.1 christos {
2110 1.1 christos struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2111 1.1 christos size_t length;
2112 1.1 christos const char *filename = lbasename (pathname);
2113 1.1 christos size_t maxlen = ar_maxnamelen (abfd);
2114 1.1 christos
2115 1.1 christos length = strlen (filename);
2116 1.1 christos
2117 1.1 christos if (length <= maxlen)
2118 1.1 christos memcpy (hdr->ar_name, filename, length);
2119 1.1 christos else
2120 1.1 christos {
2121 1.1 christos /* pathname: meet procrustes */
2122 1.1 christos memcpy (hdr->ar_name, filename, maxlen);
2123 1.1 christos length = maxlen;
2124 1.1 christos }
2125 1.1 christos
2126 1.1 christos if (length < maxlen)
2127 1.1 christos (hdr->ar_name)[length] = ar_padchar (abfd);
2128 1.1 christos }
2129 1.1 christos
2130 1.1 christos /* Store name into ar header. Truncates the name to fit.
2131 1.1 christos 1> strip pathname to be just the basename.
2132 1.1 christos 2> if it's short enuf to fit, stuff it in.
2133 1.1 christos 3> If it doesn't end with .o, truncate it to fit
2134 1.1 christos 4> truncate it before the .o, append .o, stuff THAT in. */
2135 1.1 christos
2136 1.1 christos /* This is what gnu ar does. It's better but incompatible with the
2137 1.1 christos bsd ar. */
2138 1.1 christos
2139 1.1 christos void
2140 1.1 christos bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2141 1.1 christos {
2142 1.1 christos struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2143 1.1 christos size_t length;
2144 1.1 christos const char *filename = lbasename (pathname);
2145 1.1 christos size_t maxlen = ar_maxnamelen (abfd);
2146 1.1 christos
2147 1.1 christos length = strlen (filename);
2148 1.1 christos
2149 1.1 christos if (length <= maxlen)
2150 1.1 christos memcpy (hdr->ar_name, filename, length);
2151 1.1 christos else
2152 1.1 christos {
2153 1.1 christos /* pathname: meet procrustes. */
2154 1.1 christos memcpy (hdr->ar_name, filename, maxlen);
2155 1.1 christos if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
2156 1.1 christos {
2157 1.1 christos hdr->ar_name[maxlen - 2] = '.';
2158 1.1 christos hdr->ar_name[maxlen - 1] = 'o';
2159 1.1 christos }
2160 1.1 christos length = maxlen;
2161 1.1 christos }
2162 1.1 christos
2163 1.1 christos if (length < 16)
2164 1.1 christos (hdr->ar_name)[length] = ar_padchar (abfd);
2165 1.1 christos }
2166 1.1 christos
2167 1.1 christos /* The BFD is open for write and has its format set to bfd_archive. */
2169 1.1 christos
2170 1.1 christos bfd_boolean
2171 1.1 christos _bfd_write_archive_contents (bfd *arch)
2172 1.1 christos {
2173 1.1 christos bfd *current;
2174 1.1 christos char *etable = NULL;
2175 1.1 christos bfd_size_type elength = 0;
2176 1.1 christos const char *ename = NULL;
2177 1.1 christos bfd_boolean makemap = bfd_has_map (arch);
2178 1.1 christos /* If no .o's, don't bother to make a map. */
2179 1.1 christos bfd_boolean hasobjects = FALSE;
2180 1.1 christos bfd_size_type wrote;
2181 1.1 christos int tries;
2182 1.1 christos char *armag;
2183 1.1 christos
2184 1.1 christos /* Verify the viability of all entries; if any of them live in the
2185 1.1 christos filesystem (as opposed to living in an archive open for input)
2186 1.1 christos then construct a fresh ar_hdr for them. */
2187 1.1 christos for (current = arch->archive_head;
2188 1.1 christos current != NULL;
2189 1.1 christos current = current->archive_next)
2190 1.1 christos {
2191 1.1 christos /* This check is checking the bfds for the objects we're reading
2192 1.1 christos from (which are usually either an object file or archive on
2193 1.1 christos disk), not the archive entries we're writing to. We don't
2194 1.1 christos actually create bfds for the archive members, we just copy
2195 1.1 christos them byte-wise when we write out the archive. */
2196 1.1 christos if (bfd_write_p (current))
2197 1.1 christos {
2198 1.1 christos bfd_set_error (bfd_error_invalid_operation);
2199 1.1 christos goto input_err;
2200 1.1 christos }
2201 1.1 christos if (!current->arelt_data)
2202 1.1 christos {
2203 1.1 christos current->arelt_data =
2204 1.1 christos bfd_ar_hdr_from_filesystem (arch, current->filename, current);
2205 1.1 christos if (!current->arelt_data)
2206 1.1 christos goto input_err;
2207 1.1 christos
2208 1.1 christos /* Put in the file name. */
2209 1.1 christos BFD_SEND (arch, _bfd_truncate_arname,
2210 1.1 christos (arch, current->filename, (char *) arch_hdr (current)));
2211 1.1 christos }
2212 1.1 christos
2213 1.1 christos if (makemap && ! hasobjects)
2214 1.1 christos { /* Don't bother if we won't make a map! */
2215 1.1 christos if ((bfd_check_format (current, bfd_object)))
2216 1.1 christos hasobjects = TRUE;
2217 1.1 christos }
2218 1.1 christos }
2219 1.1 christos
2220 1.1 christos if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
2221 1.1 christos (arch, &etable, &elength, &ename)))
2222 1.1 christos return FALSE;
2223 1.1 christos
2224 1.1 christos if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
2225 1.1 christos return FALSE;
2226 1.1 christos armag = ARMAG;
2227 1.1 christos if (bfd_is_thin_archive (arch))
2228 1.1 christos armag = ARMAGT;
2229 1.1 christos wrote = bfd_bwrite (armag, SARMAG, arch);
2230 1.1 christos if (wrote != SARMAG)
2231 1.1 christos return FALSE;
2232 1.1 christos
2233 1.1 christos if (makemap && hasobjects)
2234 1.1 christos {
2235 1.1 christos if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
2236 1.1 christos return FALSE;
2237 1.1 christos }
2238 1.1 christos
2239 1.1 christos if (elength != 0)
2240 1.1 christos {
2241 1.1 christos struct ar_hdr hdr;
2242 1.1 christos
2243 1.1 christos memset (&hdr, ' ', sizeof (struct ar_hdr));
2244 1.1 christos memcpy (hdr.ar_name, ename, strlen (ename));
2245 1.1 christos /* Round size up to even number in archive header. */
2246 1.1 christos if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size),
2247 1.1 christos (elength + 1) & ~(bfd_size_type) 1))
2248 1.1 christos return FALSE;
2249 1.1 christos memcpy (hdr.ar_fmag, ARFMAG, 2);
2250 1.1 christos if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2251 1.1 christos != sizeof (struct ar_hdr))
2252 1.1 christos || bfd_bwrite (etable, elength, arch) != elength)
2253 1.1 christos return FALSE;
2254 1.1 christos if ((elength % 2) == 1)
2255 1.1 christos {
2256 1.1 christos if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
2257 1.1 christos return FALSE;
2258 1.1 christos }
2259 1.1 christos }
2260 1.1 christos
2261 1.1 christos for (current = arch->archive_head;
2262 1.1 christos current != NULL;
2263 1.1 christos current = current->archive_next)
2264 1.1 christos {
2265 1.1 christos char buffer[DEFAULT_BUFFERSIZE];
2266 1.1 christos bfd_size_type saved_size = arelt_size (current);
2267 1.1 christos bfd_size_type remaining = saved_size;
2268 1.1 christos struct ar_hdr *hdr = arch_hdr (current);
2269 1.1 christos
2270 1.1 christos /* Write ar header. */
2271 1.1 christos if (!_bfd_write_ar_hdr (arch, current))
2272 1.1 christos return FALSE;
2273 1.1 christos /* Write filename if it is a 4.4BSD extended file, and add to size. */
2274 1.1 christos if (!strncmp (hdr->ar_name, "#1/", 3))
2275 1.1 christos {
2276 1.1 christos const char *normal = normalize (current, current->filename);
2277 1.1 christos unsigned int thislen = strlen (normal);
2278 1.1 christos if (bfd_write (normal, 1, thislen, arch) != thislen)
2279 1.1 christos return FALSE;
2280 1.1 christos saved_size += thislen;
2281 1.1 christos }
2282 1.1 christos if (bfd_is_thin_archive (arch))
2283 1.1 christos continue;
2284 1.1 christos if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
2285 1.1 christos goto input_err;
2286 1.1 christos
2287 1.1 christos while (remaining)
2288 1.1 christos {
2289 1.1 christos unsigned int amt = DEFAULT_BUFFERSIZE;
2290 1.1 christos
2291 1.1 christos if (amt > remaining)
2292 1.1 christos amt = remaining;
2293 1.1 christos errno = 0;
2294 1.1 christos if (bfd_bread (buffer, amt, current) != amt)
2295 1.1 christos {
2296 1.1 christos if (bfd_get_error () != bfd_error_system_call)
2297 1.1 christos bfd_set_error (bfd_error_file_truncated);
2298 1.1 christos goto input_err;
2299 1.1 christos }
2300 1.1 christos if (bfd_bwrite (buffer, amt, arch) != amt)
2301 1.1 christos return FALSE;
2302 1.1 christos remaining -= amt;
2303 1.1 christos }
2304 1.1 christos
2305 1.1 christos if ((arelt_size (current) % 2) == 1)
2306 1.1 christos {
2307 1.1 christos if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
2308 1.1 christos return FALSE;
2309 1.1 christos }
2310 1.1 christos }
2311 1.1 christos
2312 1.1 christos if (makemap && hasobjects)
2313 1.1 christos {
2314 1.1 christos /* Verify the timestamp in the archive file. If it would not be
2315 1.1 christos accepted by the linker, rewrite it until it would be. If
2316 1.1 christos anything odd happens, break out and just return. (The
2317 1.1 christos Berkeley linker checks the timestamp and refuses to read the
2318 1.1 christos table-of-contents if it is >60 seconds less than the file's
2319 1.1 christos modified-time. That painful hack requires this painful hack. */
2320 1.1 christos tries = 1;
2321 1.1 christos do
2322 1.1 christos {
2323 1.1 christos if (bfd_update_armap_timestamp (arch))
2324 1.1 christos break;
2325 1.1 christos (*_bfd_error_handler)
2326 1.1 christos (_("Warning: writing archive was slow: rewriting timestamp\n"));
2327 1.1 christos }
2328 1.1 christos while (++tries < 6);
2329 1.1 christos }
2330 1.1 christos
2331 1.1 christos return TRUE;
2332 1.1 christos
2333 1.1 christos input_err:
2334 1.1 christos bfd_set_error (bfd_error_on_input, current, bfd_get_error ());
2335 1.1 christos return FALSE;
2336 1.1 christos }
2337 1.1 christos
2338 1.1 christos /* Note that the namidx for the first symbol is 0. */
2340 1.1 christos
2341 1.1 christos bfd_boolean
2342 1.1 christos _bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
2343 1.1 christos {
2344 1.1 christos char *first_name = NULL;
2345 1.1 christos bfd *current;
2346 1.1 christos file_ptr elt_no = 0;
2347 1.1 christos struct orl *map = NULL;
2348 1.1 christos unsigned int orl_max = 1024; /* Fine initial default. */
2349 1.1 christos unsigned int orl_count = 0;
2350 1.1 christos int stridx = 0;
2351 1.1 christos asymbol **syms = NULL;
2352 1.1 christos long syms_max = 0;
2353 1.1 christos bfd_boolean ret;
2354 1.1 christos bfd_size_type amt;
2355 1.1 christos
2356 1.1 christos /* Dunno if this is the best place for this info... */
2357 1.1 christos if (elength != 0)
2358 1.1 christos elength += sizeof (struct ar_hdr);
2359 1.1 christos elength += elength % 2;
2360 1.1 christos
2361 1.1 christos amt = orl_max * sizeof (struct orl);
2362 1.1 christos map = (struct orl *) bfd_malloc (amt);
2363 1.1 christos if (map == NULL)
2364 1.1 christos goto error_return;
2365 1.1 christos
2366 1.1 christos /* We put the symbol names on the arch objalloc, and then discard
2367 1.1 christos them when done. */
2368 1.1 christos first_name = (char *) bfd_alloc (arch, 1);
2369 1.1 christos if (first_name == NULL)
2370 1.1 christos goto error_return;
2371 1.1 christos
2372 1.1 christos /* Drop all the files called __.SYMDEF, we're going to make our own. */
2373 1.1 christos while (arch->archive_head
2374 1.1 christos && strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
2375 1.1 christos arch->archive_head = arch->archive_head->archive_next;
2376 1.1 christos
2377 1.1 christos /* Map over each element. */
2378 1.1 christos for (current = arch->archive_head;
2379 1.1 christos current != NULL;
2380 1.1 christos current = current->archive_next, elt_no++)
2381 1.1 christos {
2382 1.1 christos if (bfd_check_format (current, bfd_object)
2383 1.1 christos && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
2384 1.1 christos {
2385 1.1 christos long storage;
2386 1.1 christos long symcount;
2387 1.1 christos long src_count;
2388 1.1 christos
2389 1.1 christos storage = bfd_get_symtab_upper_bound (current);
2390 1.1 christos if (storage < 0)
2391 1.1 christos goto error_return;
2392 1.1 christos
2393 1.1 christos if (storage != 0)
2394 1.1 christos {
2395 1.1 christos if (storage > syms_max)
2396 1.1 christos {
2397 1.1 christos if (syms_max > 0)
2398 1.1 christos free (syms);
2399 1.1 christos syms_max = storage;
2400 1.1 christos syms = (asymbol **) bfd_malloc (syms_max);
2401 1.1 christos if (syms == NULL)
2402 1.1 christos goto error_return;
2403 1.1 christos }
2404 1.1 christos symcount = bfd_canonicalize_symtab (current, syms);
2405 1.1 christos if (symcount < 0)
2406 1.1 christos goto error_return;
2407 1.1 christos
2408 1.1 christos /* Now map over all the symbols, picking out the ones we
2409 1.1 christos want. */
2410 1.1 christos for (src_count = 0; src_count < symcount; src_count++)
2411 1.1 christos {
2412 1.1 christos flagword flags = (syms[src_count])->flags;
2413 1.1 christos asection *sec = syms[src_count]->section;
2414 1.1 christos
2415 1.1 christos if (((flags & (BSF_GLOBAL
2416 1.1 christos | BSF_WEAK
2417 1.1 christos | BSF_INDIRECT
2418 1.1 christos | BSF_GNU_UNIQUE)) != 0
2419 1.1 christos || bfd_is_com_section (sec))
2420 1.1 christos && ! bfd_is_und_section (sec))
2421 1.1 christos {
2422 1.1 christos bfd_size_type namelen;
2423 1.1 christos struct orl *new_map;
2424 1.1 christos
2425 1.1 christos /* This symbol will go into the archive header. */
2426 1.1 christos if (orl_count == orl_max)
2427 1.1.1.2 christos {
2428 1.1.1.2 christos orl_max *= 2;
2429 1.1.1.2 christos amt = orl_max * sizeof (struct orl);
2430 1.1.1.2 christos new_map = (struct orl *) bfd_realloc (map, amt);
2431 1.1 christos if (new_map == NULL)
2432 1.1 christos goto error_return;
2433 1.1 christos
2434 1.1 christos map = new_map;
2435 1.1 christos }
2436 1.1 christos
2437 1.1 christos if (strcmp (syms[src_count]->name, "__gnu_lto_slim") == 0)
2438 1.1 christos (*_bfd_error_handler)
2439 1.1 christos (_("%s: plugin needed to handle lto object"),
2440 1.1 christos bfd_get_filename (current));
2441 1.1 christos namelen = strlen (syms[src_count]->name);
2442 1.1 christos amt = sizeof (char *);
2443 1.1 christos map[orl_count].name = (char **) bfd_alloc (arch, amt);
2444 1.1 christos if (map[orl_count].name == NULL)
2445 1.1 christos goto error_return;
2446 1.1 christos *(map[orl_count].name) = (char *) bfd_alloc (arch,
2447 1.1 christos namelen + 1);
2448 1.1 christos if (*(map[orl_count].name) == NULL)
2449 1.1 christos goto error_return;
2450 1.1 christos strcpy (*(map[orl_count].name), syms[src_count]->name);
2451 1.1 christos map[orl_count].u.abfd = current;
2452 1.1 christos map[orl_count].namidx = stridx;
2453 1.1 christos
2454 1.1 christos stridx += namelen + 1;
2455 1.1 christos ++orl_count;
2456 1.1 christos }
2457 1.1 christos }
2458 1.1 christos }
2459 1.1 christos
2460 1.1 christos /* Now ask the BFD to free up any cached information, so we
2461 1.1 christos don't fill all of memory with symbol tables. */
2462 1.1 christos if (! bfd_free_cached_info (current))
2463 1.1 christos goto error_return;
2464 1.1 christos }
2465 1.1 christos }
2466 1.1 christos
2467 1.1 christos /* OK, now we have collected all the data, let's write them out. */
2468 1.1 christos ret = BFD_SEND (arch, write_armap,
2469 1.1 christos (arch, elength, map, orl_count, stridx));
2470 1.1 christos
2471 1.1 christos if (syms_max > 0)
2472 1.1 christos free (syms);
2473 1.1 christos if (map != NULL)
2474 1.1 christos free (map);
2475 1.1 christos if (first_name != NULL)
2476 1.1 christos bfd_release (arch, first_name);
2477 1.1 christos
2478 1.1 christos return ret;
2479 1.1 christos
2480 1.1 christos error_return:
2481 1.1 christos if (syms_max > 0)
2482 1.1 christos free (syms);
2483 1.1 christos if (map != NULL)
2484 1.1 christos free (map);
2485 1.1 christos if (first_name != NULL)
2486 1.1 christos bfd_release (arch, first_name);
2487 1.1 christos
2488 1.1 christos return FALSE;
2489 1.1 christos }
2490 1.1 christos
2491 1.1 christos bfd_boolean
2492 1.1 christos bsd_write_armap (bfd *arch,
2493 1.1.1.3 christos unsigned int elength,
2494 1.1.1.3 christos struct orl *map,
2495 1.1.1.3 christos unsigned int orl_count,
2496 1.1 christos int stridx)
2497 1.1 christos {
2498 1.1 christos int padit = stridx & 1;
2499 1.1 christos unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
2500 1.1 christos unsigned int stringsize = stridx + padit;
2501 1.1.1.3 christos /* Include 8 bytes to store ranlibsize and stringsize in output. */
2502 1.1.1.3 christos unsigned int mapsize = ranlibsize + stringsize + 8;
2503 1.1.1.3 christos file_ptr firstreal, first;
2504 1.1.1.3 christos bfd *current;
2505 1.1.1.3 christos bfd *last_elt;
2506 1.1.1.3 christos bfd_byte temp[4];
2507 1.1.1.3 christos unsigned int count;
2508 1.1.1.3 christos struct ar_hdr hdr;
2509 1.1.1.3 christos long uid, gid;
2510 1.1.1.3 christos
2511 1.1.1.3 christos first = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
2512 1.1.1.3 christos
2513 1.1.1.3 christos #ifdef BFD64
2514 1.1.1.3 christos firstreal = first;
2515 1.1.1.3 christos current = arch->archive_head;
2516 1.1.1.3 christos last_elt = current; /* Last element arch seen. */
2517 1.1.1.3 christos for (count = 0; count < orl_count; count++)
2518 1.1.1.3 christos {
2519 1.1.1.3 christos unsigned int offset;
2520 1.1.1.3 christos
2521 1.1.1.3 christos if (map[count].u.abfd != last_elt)
2522 1.1.1.3 christos {
2523 1.1.1.3 christos do
2524 1.1.1.3 christos {
2525 1.1.1.3 christos struct areltdata *ared = arch_eltdata (current);
2526 1.1.1.3 christos
2527 1.1.1.3 christos firstreal += (ared->parsed_size + ared->extra_size
2528 1.1.1.3 christos + sizeof (struct ar_hdr));
2529 1.1.1.3 christos firstreal += firstreal % 2;
2530 1.1.1.3 christos current = current->archive_next;
2531 1.1.1.3 christos }
2532 1.1.1.3 christos while (current != map[count].u.abfd);
2533 1.1.1.3 christos }
2534 1.1.1.3 christos
2535 1.1.1.3 christos /* The archive file format only has 4 bytes to store the offset
2536 1.1 christos of the member. Generate 64-bit archive if an archive is past
2537 1.1 christos its 4Gb limit. */
2538 1.1 christos offset = (unsigned int) firstreal;
2539 1.1 christos if (firstreal != (file_ptr) offset)
2540 1.1 christos return _bfd_archive_64_bit_write_armap (arch, elength, map,
2541 1.1 christos orl_count, stridx);
2542 1.1 christos
2543 1.1 christos last_elt = current;
2544 1.1 christos }
2545 1.1 christos #endif
2546 1.1 christos
2547 1.1 christos /* If deterministic, we use 0 as the timestamp in the map.
2548 1.1 christos Some linkers may require that the archive filesystem modification
2549 1.1 christos time is less than (or near to) the archive map timestamp. Those
2550 1.1 christos linkers should not be used with deterministic mode. (GNU ld and
2551 1.1 christos Gold do not have this restriction.) */
2552 1.1 christos bfd_ardata (arch)->armap_timestamp = 0;
2553 1.1 christos uid = 0;
2554 1.1 christos gid = 0;
2555 1.1 christos if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0)
2556 1.1 christos {
2557 1.1 christos struct stat statbuf;
2558 1.1 christos
2559 1.1 christos if (stat (arch->filename, &statbuf) == 0)
2560 1.1 christos bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime
2561 1.1 christos + ARMAP_TIME_OFFSET);
2562 1.1 christos uid = getuid();
2563 1.1 christos gid = getgid();
2564 1.1 christos }
2565 1.1 christos
2566 1.1 christos memset (&hdr, ' ', sizeof (struct ar_hdr));
2567 1.1 christos memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
2568 1.1 christos bfd_ardata (arch)->armap_datepos = (SARMAG
2569 1.1 christos + offsetof (struct ar_hdr, ar_date[0]));
2570 1.1 christos _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2571 1.1 christos bfd_ardata (arch)->armap_timestamp);
2572 1.1 christos _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid);
2573 1.1 christos _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid);
2574 1.1.1.3 christos if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2575 1.1.1.3 christos return FALSE;
2576 1.1.1.3 christos memcpy (hdr.ar_fmag, ARFMAG, 2);
2577 1.1 christos if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2578 1.1 christos != sizeof (struct ar_hdr))
2579 1.1 christos return FALSE;
2580 1.1 christos H_PUT_32 (arch, ranlibsize, temp);
2581 1.1 christos if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
2582 1.1 christos return FALSE;
2583 1.1 christos
2584 1.1 christos firstreal = first;
2585 1.1 christos current = arch->archive_head;
2586 1.1 christos last_elt = current; /* Last element arch seen. */
2587 1.1 christos for (count = 0; count < orl_count; count++)
2588 1.1 christos {
2589 1.1 christos unsigned int offset;
2590 1.1 christos bfd_byte buf[BSD_SYMDEF_SIZE];
2591 1.1 christos
2592 1.1 christos if (map[count].u.abfd != last_elt)
2593 1.1 christos {
2594 1.1 christos do
2595 1.1 christos {
2596 1.1 christos #if 1
2597 1.1 christos bfd_size_type size = arelt_size (current);
2598 1.1 christos if (!strncmp(arch_hdr (current)->ar_name, "#1/", 3))
2599 1.1 christos size += strlen(normalize(current, current->filename));
2600 1.1 christos firstreal += size + sizeof (struct ar_hdr);
2601 1.1 christos firstreal += size % 2;
2602 1.1 christos #else
2603 1.1 christos struct areltdata *ared = arch_eltdata (current);
2604 1.1 christos
2605 1.1 christos firstreal += (ared->parsed_size + ared->extra_size
2606 1.1 christos + sizeof (struct ar_hdr));
2607 1.1 christos firstreal += firstreal % 2;
2608 1.1 christos #endif
2609 1.1 christos current = current->archive_next;
2610 1.1 christos }
2611 1.1 christos while (current != map[count].u.abfd);
2612 1.1 christos }
2613 1.1.1.2 christos
2614 1.1 christos /* The archive file format only has 4 bytes to store the offset
2615 1.1 christos of the member. Check to make sure that firstreal has not grown
2616 1.1 christos too big. */
2617 1.1 christos offset = (unsigned int) firstreal;
2618 1.1 christos if (firstreal != (file_ptr) offset)
2619 1.1 christos {
2620 1.1 christos bfd_set_error (bfd_error_file_truncated);
2621 1.1 christos return FALSE;
2622 1.1 christos }
2623 1.1 christos
2624 1.1 christos last_elt = current;
2625 1.1 christos H_PUT_32 (arch, map[count].namidx, buf);
2626 1.1 christos H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE);
2627 1.1 christos if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch)
2628 1.1 christos != BSD_SYMDEF_SIZE)
2629 1.1 christos return FALSE;
2630 1.1 christos }
2631 1.1 christos
2632 1.1 christos /* Now write the strings themselves. */
2633 1.1 christos H_PUT_32 (arch, stringsize, temp);
2634 1.1 christos if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
2635 1.1 christos return FALSE;
2636 1.1 christos for (count = 0; count < orl_count; count++)
2637 1.1 christos {
2638 1.1 christos size_t len = strlen (*map[count].name) + 1;
2639 1.1 christos
2640 1.1 christos if (bfd_bwrite (*map[count].name, len, arch) != len)
2641 1.1 christos return FALSE;
2642 1.1 christos }
2643 1.1 christos
2644 1.1 christos /* The spec sez this should be a newline. But in order to be
2645 1.1 christos bug-compatible for sun's ar we use a null. */
2646 1.1 christos if (padit)
2647 1.1 christos {
2648 1.1 christos if (bfd_bwrite ("", 1, arch) != 1)
2649 1.1 christos return FALSE;
2650 1.1 christos }
2651 1.1 christos
2652 1.1 christos return TRUE;
2653 1.1 christos }
2654 1.1 christos
2655 1.1 christos /* At the end of archive file handling, update the timestamp in the
2656 1.1 christos file, so the linker will accept it.
2657 1.1 christos
2658 1.1 christos Return TRUE if the timestamp was OK, or an unusual problem happened.
2659 1.1 christos Return FALSE if we updated the timestamp. */
2660 1.1 christos
2661 1.1 christos bfd_boolean
2662 1.1 christos _bfd_archive_bsd_update_armap_timestamp (bfd *arch)
2663 1.1 christos {
2664 1.1 christos struct stat archstat;
2665 1.1 christos struct ar_hdr hdr;
2666 1.1 christos
2667 1.1 christos /* If creating deterministic archives, just leave the timestamp as-is. */
2668 1.1 christos if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2669 1.1 christos return TRUE;
2670 1.1 christos
2671 1.1 christos /* Flush writes, get last-write timestamp from file, and compare it
2672 1.1 christos to the timestamp IN the file. */
2673 1.1 christos bfd_flush (arch);
2674 1.1 christos if (bfd_stat (arch, &archstat) == -1)
2675 1.1 christos {
2676 1.1 christos bfd_perror (_("Reading archive file mod timestamp"));
2677 1.1 christos
2678 1.1 christos /* Can't read mod time for some reason. */
2679 1.1 christos return TRUE;
2680 1.1 christos }
2681 1.1 christos if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp)
2682 1.1 christos /* OK by the linker's rules. */
2683 1.1 christos return TRUE;
2684 1.1 christos
2685 1.1 christos /* Update the timestamp. */
2686 1.1 christos bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2687 1.1 christos
2688 1.1 christos /* Prepare an ASCII version suitable for writing. */
2689 1.1 christos memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2690 1.1 christos _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2691 1.1 christos bfd_ardata (arch)->armap_timestamp);
2692 1.1 christos
2693 1.1 christos /* Write it into the file. */
2694 1.1 christos bfd_ardata (arch)->armap_datepos = (SARMAG
2695 1.1 christos + offsetof (struct ar_hdr, ar_date[0]));
2696 1.1 christos if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2697 1.1 christos || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
2698 1.1 christos != sizeof (hdr.ar_date)))
2699 1.1 christos {
2700 1.1 christos bfd_perror (_("Writing updated armap timestamp"));
2701 1.1 christos
2702 1.1 christos /* Some error while writing. */
2703 1.1 christos return TRUE;
2704 1.1 christos }
2705 1.1 christos
2706 1.1 christos /* We updated the timestamp successfully. */
2707 1.1 christos return FALSE;
2708 1.1 christos }
2709 1.1 christos
2710 1.1 christos /* A coff armap looks like :
2712 1.1 christos lARMAG
2713 1.1 christos struct ar_hdr with name = '/'
2714 1.1 christos number of symbols
2715 1.1 christos offset of file for symbol 0
2716 1.1 christos offset of file for symbol 1
2717 1.1 christos
2718 1.1 christos offset of file for symbol n-1
2719 1.1 christos symbol name 0
2720 1.1 christos symbol name 1
2721 1.1 christos
2722 1.1 christos symbol name n-1 */
2723 1.1 christos
2724 1.1 christos bfd_boolean
2725 1.1 christos coff_write_armap (bfd *arch,
2726 1.1.1.3 christos unsigned int elength,
2727 1.1 christos struct orl *map,
2728 1.1 christos unsigned int symbol_count,
2729 1.1 christos int stridx)
2730 1.1 christos {
2731 1.1 christos /* The size of the ranlib is the number of exported symbols in the
2732 1.1 christos archive * the number of bytes in an int, + an int for the count. */
2733 1.1 christos unsigned int ranlibsize = (symbol_count * 4) + 4;
2734 1.1 christos unsigned int stringsize = stridx;
2735 1.1 christos unsigned int mapsize = stringsize + ranlibsize;
2736 1.1.1.3 christos file_ptr archive_member_file_ptr;
2737 1.1.1.3 christos file_ptr first_archive_member_file_ptr;
2738 1.1.1.3 christos bfd *current = arch->archive_head;
2739 1.1.1.3 christos unsigned int count;
2740 1.1.1.3 christos struct ar_hdr hdr;
2741 1.1.1.3 christos int padit = mapsize & 1;
2742 1.1.1.3 christos
2743 1.1.1.3 christos if (padit)
2744 1.1.1.3 christos mapsize++;
2745 1.1.1.3 christos
2746 1.1.1.3 christos /* Work out where the first object file will go in the archive. */
2747 1.1.1.3 christos first_archive_member_file_ptr = (mapsize
2748 1.1.1.3 christos + elength
2749 1.1.1.3 christos + sizeof (struct ar_hdr)
2750 1.1.1.3 christos + SARMAG);
2751 1.1.1.3 christos
2752 1.1.1.3 christos #ifdef BFD64
2753 1.1.1.3 christos current = arch->archive_head;
2754 1.1.1.3 christos count = 0;
2755 1.1.1.3 christos archive_member_file_ptr = first_archive_member_file_ptr;
2756 1.1.1.3 christos while (current != NULL && count < symbol_count)
2757 1.1.1.3 christos {
2758 1.1.1.3 christos /* For each symbol which is used defined in this object, write
2759 1.1.1.3 christos out the object file's address in the archive. */
2760 1.1.1.3 christos
2761 1.1.1.3 christos while (count < symbol_count && map[count].u.abfd == current)
2762 1.1.1.3 christos {
2763 1.1.1.3 christos unsigned int offset = (unsigned int) archive_member_file_ptr;
2764 1.1.1.3 christos
2765 1.1.1.3 christos /* Generate 64-bit archive if an archive is past its 4Gb
2766 1.1.1.3 christos limit. */
2767 1.1.1.3 christos if (archive_member_file_ptr != (file_ptr) offset)
2768 1.1.1.3 christos return _bfd_archive_64_bit_write_armap (arch, elength, map,
2769 1.1.1.3 christos symbol_count, stridx);
2770 1.1.1.3 christos count++;
2771 1.1.1.3 christos }
2772 1.1 christos archive_member_file_ptr += sizeof (struct ar_hdr);
2773 1.1 christos if (! bfd_is_thin_archive (arch))
2774 1.1 christos {
2775 1.1 christos /* Add size of this archive entry. */
2776 1.1 christos archive_member_file_ptr += arelt_size (current);
2777 1.1 christos /* Remember about the even alignment. */
2778 1.1 christos archive_member_file_ptr += archive_member_file_ptr % 2;
2779 1.1 christos }
2780 1.1 christos current = current->archive_next;
2781 1.1 christos }
2782 1.1 christos #endif
2783 1.1 christos
2784 1.1 christos memset (&hdr, ' ', sizeof (struct ar_hdr));
2785 1.1 christos hdr.ar_name[0] = '/';
2786 1.1 christos if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2787 1.1 christos return FALSE;
2788 1.1 christos _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2789 1.1 christos ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0
2790 1.1 christos ? time (NULL) : 0));
2791 1.1 christos /* This, at least, is what Intel coff sets the values to. */
2792 1.1 christos _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2793 1.1 christos _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2794 1.1 christos _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2795 1.1 christos memcpy (hdr.ar_fmag, ARFMAG, 2);
2796 1.1 christos
2797 1.1 christos /* Write the ar header for this item and the number of symbols. */
2798 1.1 christos if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2799 1.1 christos != sizeof (struct ar_hdr))
2800 1.1 christos return FALSE;
2801 1.1 christos
2802 1.1.1.3 christos if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
2803 1.1 christos return FALSE;
2804 1.1 christos
2805 1.1 christos /* Two passes, first write the file offsets for each symbol -
2806 1.1 christos remembering that each offset is on a two byte boundary. */
2807 1.1 christos
2808 1.1 christos /* Write out the file offset for the file associated with each
2809 1.1 christos symbol, and remember to keep the offsets padded out. */
2810 1.1 christos
2811 1.1 christos current = arch->archive_head;
2812 1.1 christos count = 0;
2813 1.1 christos archive_member_file_ptr = first_archive_member_file_ptr;
2814 1.1 christos while (current != NULL && count < symbol_count)
2815 1.1 christos {
2816 1.1 christos /* For each symbol which is used defined in this object, write
2817 1.1 christos out the object file's address in the archive. */
2818 1.1 christos
2819 1.1 christos while (count < symbol_count && map[count].u.abfd == current)
2820 1.1 christos {
2821 1.1 christos unsigned int offset = (unsigned int) archive_member_file_ptr;
2822 1.1 christos
2823 1.1 christos /* Catch an attempt to grow an archive past its 4Gb limit. */
2824 1.1 christos if (archive_member_file_ptr != (file_ptr) offset)
2825 1.1 christos {
2826 1.1 christos bfd_set_error (bfd_error_file_truncated);
2827 1.1 christos return FALSE;
2828 1.1 christos }
2829 1.1 christos if (!bfd_write_bigendian_4byte_int (arch, offset))
2830 1.1 christos return FALSE;
2831 1.1 christos count++;
2832 1.1 christos }
2833 1.1 christos archive_member_file_ptr += sizeof (struct ar_hdr);
2834 1.1 christos if (! bfd_is_thin_archive (arch))
2835 1.1 christos {
2836 1.1 christos /* Add size of this archive entry. */
2837 1.1 christos archive_member_file_ptr += arelt_size (current);
2838 1.1 christos /* Remember about the even alignment. */
2839 1.1 christos archive_member_file_ptr += archive_member_file_ptr % 2;
2840 1.1 christos }
2841 1.1 christos current = current->archive_next;
2842 1.1 christos }
2843 1.1 christos
2844 1.1 christos /* Now write the strings themselves. */
2845 1.1 christos for (count = 0; count < symbol_count; count++)
2846 1.1 christos {
2847 1.1 christos size_t len = strlen (*map[count].name) + 1;
2848 1.1 christos
2849 1.1 christos if (bfd_bwrite (*map[count].name, len, arch) != len)
2850 1.1 christos return FALSE;
2851 1.1 christos }
2852 1.1.1.2 christos
2853 1.1.1.2 christos /* The spec sez this should be a newline. But in order to be
2854 1.1.1.2 christos bug-compatible for arc960 we use a null. */
2855 1.1.1.2 christos if (padit)
2856 1.1.1.2 christos {
2857 1.1.1.2 christos if (bfd_bwrite ("", 1, arch) != 1)
2858 1.1.1.2 christos return FALSE;
2859 1.1.1.2 christos }
2860 1.1.1.2 christos
2861 1.1.1.2 christos return TRUE;
2862 1.1.1.2 christos }
2863 1.1.1.2 christos
2864 1.1.1.2 christos static int
2865 1.1.1.2 christos archive_close_worker (void **slot, void *inf ATTRIBUTE_UNUSED)
2866 1.1.1.2 christos {
2867 1.1.1.2 christos struct ar_cache *ent = (struct ar_cache *) *slot;
2868 1.1.1.2 christos
2869 1.1.1.2 christos bfd_close_all_done (ent->arbfd);
2870 1.1.1.2 christos return 1;
2871 1.1.1.2 christos }
2872 1.1.1.2 christos
2873 1.1.1.2 christos bfd_boolean
2874 1.1.1.2 christos _bfd_archive_close_and_cleanup (bfd *abfd)
2875 1.1.1.2 christos {
2876 1.1.1.2 christos if (bfd_read_p (abfd) && abfd->format == bfd_archive)
2877 1.1.1.2 christos {
2878 1.1.1.2 christos bfd *nbfd;
2879 1.1.1.2 christos bfd *next;
2880 1.1.1.2 christos htab_t htab;
2881 1.1.1.2 christos
2882 1.1.1.2 christos /* Close nested archives (if this bfd is a thin archive). */
2883 1.1.1.2 christos for (nbfd = abfd->nested_archives; nbfd; nbfd = next)
2884 1.1.1.2 christos {
2885 1.1.1.2 christos next = nbfd->archive_next;
2886 1.1.1.2 christos bfd_close (nbfd);
2887 1.1.1.2 christos }
2888 1.1.1.2 christos
2889 1.1.1.2 christos htab = bfd_ardata (abfd)->cache;
2890 1.1.1.2 christos if (htab)
2891 1.1.1.2 christos {
2892 1.1.1.2 christos htab_traverse_noresize (htab, archive_close_worker, NULL);
2893 1.1.1.2 christos htab_delete (htab);
2894 1.1.1.2 christos bfd_ardata (abfd)->cache = NULL;
2895 1.1.1.2 christos }
2896 1.1.1.2 christos }
2897 1.1.1.2 christos if (arch_eltdata (abfd) != NULL)
2898 1.1.1.2 christos {
2899 1.1.1.2 christos struct areltdata *ared = arch_eltdata (abfd);
2900 1.1.1.2 christos htab_t htab = (htab_t) ared->parent_cache;
2901 1.1.1.2 christos
2902 1.1.1.2 christos if (htab)
2903 1.1.1.2 christos {
2904 1.1.1.2 christos struct ar_cache ent;
2905 1.1.1.2 christos void **slot;
2906 1.1.1.2 christos
2907 1.1.1.2 christos ent.ptr = ared->key;
2908 1.1.1.2 christos slot = htab_find_slot (htab, &ent, NO_INSERT);
2909 1.1.1.2 christos if (slot != NULL)
2910 {
2911 BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd);
2912 htab_clear_slot (htab, slot);
2913 }
2914 }
2915 }
2916 if (abfd->is_linker_output)
2917 (*abfd->link.hash->hash_table_free) (abfd);
2918
2919 return TRUE;
2920 }
2921