Bug Summary

File:src/gnu/usr.bin/binutils-2.17/bfd/archive.c
Warning:line 688, column 4
Value stored to 'fail' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name archive.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/binutils-2.17/obj/bfd -resource-dir /usr/local/lib/clang/13.0.0 -D HAVE_CONFIG_H -I . -I /usr/src/gnu/usr.bin/binutils-2.17/bfd -I . -D NETBSD_CORE -I . -I /usr/src/gnu/usr.bin/binutils-2.17/bfd -I /usr/src/gnu/usr.bin/binutils-2.17/bfd/../include -I /usr/src/gnu/usr.bin/binutils-2.17/bfd/../intl -I ../intl -D PIE_DEFAULT=1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/gnu/usr.bin/binutils-2.17/obj/bfd -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/gnu/usr.bin/binutils-2.17/bfd/archive.c
1/* BFD back-end for archive files (libraries).
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23/*
24@setfilename archive-info
25SECTION
26 Archives
27
28DESCRIPTION
29 An archive (or library) is just another BFD. It has a symbol
30 table, although there's not much a user program will do with it.
31
32 The big difference between an archive BFD and an ordinary BFD
33 is that the archive doesn't have sections. Instead it has a
34 chain of BFDs that are considered its contents. These BFDs can
35 be manipulated like any other. The BFDs contained in an
36 archive opened for reading will all be opened for reading. You
37 may put either input or output BFDs into an archive opened for
38 output; they will be handled correctly when the archive is closed.
39
40 Use <<bfd_openr_next_archived_file>> to step through
41 the contents of an archive opened for input. You don't
42 have to read the entire archive if you don't want
43 to! Read it until you find what you want.
44
45 Archive contents of output BFDs are chained through the
46 <<next>> pointer in a BFD. The first one is findable through
47 the <<archive_head>> slot of the archive. Set it with
48 <<bfd_set_archive_head>> (q.v.). A given BFD may be in only one
49 open output archive at a time.
50
51 As expected, the BFD archive code is more general than the
52 archive code of any given environment. BFD archives may
53 contain files of different formats (e.g., a.out and coff) and
54 even different architectures. You may even place archives
55 recursively into archives!
56
57 This can cause unexpected confusion, since some archive
58 formats are more expressive than others. For instance, Intel
59 COFF archives can preserve long filenames; SunOS a.out archives
60 cannot. If you move a file from the first to the second
61 format and back again, the filename may be truncated.
62 Likewise, different a.out environments have different
63 conventions as to how they truncate filenames, whether they
64 preserve directory names in filenames, etc. When
65 interoperating with native tools, be sure your files are
66 homogeneous.
67
68 Beware: most of these formats do not react well to the
69 presence of spaces in filenames. We do the best we can, but
70 can't always handle this case due to restrictions in the format of
71 archives. Many Unix utilities are braindead in regards to
72 spaces and such in filenames anyway, so this shouldn't be much
73 of a restriction.
74
75 Archives are supported in BFD in <<archive.c>>.
76
77SUBSECTION
78 Archive functions
79*/
80
81/* Assumes:
82 o - all archive elements start on an even boundary, newline padded;
83 o - all arch headers are char *;
84 o - all arch headers are the same size (across architectures).
85*/
86
87/* Some formats provide a way to cram a long filename into the short
88 (16 chars) space provided by a BSD archive. The trick is: make a
89 special "file" in the front of the archive, sort of like the SYMDEF
90 entry. If the filename is too long to fit, put it in the extended
91 name table, and use its index as the filename. To prevent
92 confusion prepend the index with a space. This means you can't
93 have filenames that start with a space, but then again, many Unix
94 utilities can't handle that anyway.
95
96 This scheme unfortunately requires that you stand on your head in
97 order to write an archive since you need to put a magic file at the
98 front, and need to touch every entry to do so. C'est la vie.
99
100 We support two variants of this idea:
101 The SVR4 format (extended name table is named "//"),
102 and an extended pseudo-BSD variant (extended name table is named
103 "ARFILENAMES/"). The origin of the latter format is uncertain.
104
105 BSD 4.4 uses a third scheme: It writes a long filename
106 directly after the header. This allows 'ar q' to work.
107 We currently can read BSD 4.4 archives, but not write them.
108*/
109
110/* Summary of archive member names:
111
112 Symbol table (must be first):
113 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
114 "/ " - Symbol table, system 5 style.
115
116 Long name table (must be before regular file members):
117 "// " - Long name table, System 5 R4 style.
118 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
119
120 Regular file members with short names:
121 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
122 "filename.o " - Regular file, Berkeley style (no embedded spaces).
123
124 Regular files with long names (or embedded spaces, for BSD variants):
125 "/18 " - SVR4 style, name at offset 18 in name table.
126 "#1/23 " - Long name (or embedded spaces) 23 characters long,
127 BSD 4.4 style, full name follows header.
128 Implemented for reading, not writing.
129 " 18 " - Long name 18 characters long, extended pseudo-BSD.
130 */
131
132#include "bfd.h"
133#include "sysdep.h"
134#include "libiberty.h"
135#include "libbfd.h"
136#include "aout/ar.h"
137#include "aout/ranlib.h"
138#include "safe-ctype.h"
139#include "hashtab.h"
140
141#ifndef errno(*__errno())
142extern int errno(*__errno());
143#endif
144
145/* We keep a cache of archive filepointers to archive elements to
146 speed up searching the archive by filepos. We only add an entry to
147 the cache when we actually read one. We also don't sort the cache;
148 it's generally short enough to search linearly.
149 Note that the pointers here point to the front of the ar_hdr, not
150 to the front of the contents! */
151struct ar_cache {
152 file_ptr ptr;
153 bfd *arbfd;
154};
155
156#define ar_padchar(abfd)((abfd)->xvec->ar_pad_char) ((abfd)->xvec->ar_pad_char)
157#define ar_maxnamelen(abfd)((abfd)->xvec->ar_max_namelen) ((abfd)->xvec->ar_max_namelen)
158
159#define arch_eltdata(bfd)((struct areltdata *) ((bfd)->arelt_data)) ((struct areltdata *) ((bfd)->arelt_data))
160#define arch_hdr(bfd)((struct ar_hdr *) ((struct areltdata *) ((bfd)->arelt_data
))->arch_header)
((struct ar_hdr *) arch_eltdata(bfd)((struct areltdata *) ((bfd)->arelt_data))->arch_header)
161
162void
163_bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
164{
165 static char buf[20];
166 size_t len;
167 snprintf (buf, sizeof (buf), fmt, val);
168 len = strlen (buf);
169 if (len < n)
170 {
171 memcpy (p, buf, len);
172 memset (p + len, ' ', n - len);
173 }
174 else
175 memcpy (p, buf, n);
176}
177
178void
179_bfd_ar_spacepadll (char *p, size_t n, const char *fmt, long long val)
180{
181 static char buf[20];
182 size_t len;
183 snprintf (buf, sizeof (buf), fmt, val);
184 len = strlen (buf);
185 if (len < n)
186 {
187 memcpy (p, buf, len);
188 memset (p + len, ' ', n - len);
189 }
190 else
191 memcpy (p, buf, n);
192}
193
194bfd_boolean
195_bfd_generic_mkarchive (bfd *abfd)
196{
197 bfd_size_type amt = sizeof (struct artdata);
198
199 abfd->tdata.aout_ar_data = bfd_zalloc (abfd, amt);
200 if (bfd_ardata (abfd)((abfd)->tdata.aout_ar_data) == NULL((void*)0))
201 return FALSE0;
202
203 /* Already cleared by bfd_zalloc above.
204 bfd_ardata (abfd)->cache = NULL;
205 bfd_ardata (abfd)->archive_head = NULL;
206 bfd_ardata (abfd)->symdefs = NULL;
207 bfd_ardata (abfd)->extended_names = NULL;
208 bfd_ardata (abfd)->extended_names_size = 0;
209 bfd_ardata (abfd)->tdata = NULL; */
210
211 return TRUE1;
212}
213
214/*
215FUNCTION
216 bfd_get_next_mapent
217
218SYNOPSIS
219 symindex bfd_get_next_mapent
220 (bfd *abfd, symindex previous, carsym **sym);
221
222DESCRIPTION
223 Step through archive @var{abfd}'s symbol table (if it
224 has one). Successively update @var{sym} with the next symbol's
225 information, returning that symbol's (internal) index into the
226 symbol table.
227
228 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
229 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
230 got the last one.
231
232 A <<carsym>> is a canonical archive symbol. The only
233 user-visible element is its name, a null-terminated string.
234*/
235
236symindex
237bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
238{
239 if (!bfd_has_map (abfd)((abfd)->has_armap))
240 {
241 bfd_set_error (bfd_error_invalid_operation);
242 return BFD_NO_MORE_SYMBOLS((symindex) ~0);
243 }
244
245 if (prev == BFD_NO_MORE_SYMBOLS((symindex) ~0))
246 prev = 0;
247 else
248 ++prev;
249 if (prev >= bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->symdef_count)
250 return BFD_NO_MORE_SYMBOLS((symindex) ~0);
251
252 *entry = (bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->symdefs + prev);
253 return prev;
254}
255
256/* To be called by backends only. */
257
258bfd *
259_bfd_create_empty_archive_element_shell (bfd *obfd)
260{
261 return _bfd_new_bfd_contained_in (obfd);
262}
263
264/*
265FUNCTION
266 bfd_set_archive_head
267
268SYNOPSIS
269 bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
270
271DESCRIPTION
272 Set the head of the chain of
273 BFDs contained in the archive @var{output} to @var{new_head}.
274*/
275
276bfd_boolean
277bfd_set_archive_head (bfd *output_archive, bfd *new_head)
278{
279 output_archive->archive_head = new_head;
280 return TRUE1;
281}
282
283bfd *
284_bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
285{
286 htab_t hash_table = bfd_ardata (arch_bfd)((arch_bfd)->tdata.aout_ar_data)->cache;
287 struct ar_cache m;
288 m.ptr = filepos;
289
290 if (hash_table)
291 {
292 struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
293 if (!entry)
294 return NULL((void*)0);
295 else
296 return entry->arbfd;
297 }
298 else
299 return NULL((void*)0);
300}
301
302static hashval_t
303hash_file_ptr (const PTRvoid * p)
304{
305 return (hashval_t) (((struct ar_cache *) p)->ptr);
306}
307
308/* Returns non-zero if P1 and P2 are equal. */
309
310static int
311eq_file_ptr (const PTRvoid * p1, const PTRvoid * p2)
312{
313 struct ar_cache *arc1 = (struct ar_cache *) p1;
314 struct ar_cache *arc2 = (struct ar_cache *) p2;
315 return arc1->ptr == arc2->ptr;
316}
317
318/* Kind of stupid to call cons for each one, but we don't do too many. */
319
320bfd_boolean
321_bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
322{
323 struct ar_cache *cache;
324 htab_t hash_table = bfd_ardata (arch_bfd)((arch_bfd)->tdata.aout_ar_data)->cache;
325
326 /* If the hash table hasn't been created, create it. */
327 if (hash_table == NULL((void*)0))
328 {
329 hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
330 NULL((void*)0), calloc, free);
331 if (hash_table == NULL((void*)0))
332 return FALSE0;
333 bfd_ardata (arch_bfd)((arch_bfd)->tdata.aout_ar_data)->cache = hash_table;
334 }
335
336 /* Insert new_elt into the hash table by filepos. */
337 cache = bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
338 cache->ptr = filepos;
339 cache->arbfd = new_elt;
340 *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
341
342 return TRUE1;
343}
344
345/* The name begins with space. Hence the rest of the name is an index into
346 the string table. */
347
348static char *
349get_extended_arelt_filename (bfd *arch, const char *name)
350{
351 unsigned long index = 0;
352
353 /* Should extract string so that I can guarantee not to overflow into
354 the next region, but I'm too lazy. */
355 errno(*__errno()) = 0;
356 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
357 index = strtol (name + 1, NULL((void*)0), 10);
358 if (errno(*__errno()) != 0 || index >= bfd_ardata (arch)((arch)->tdata.aout_ar_data)->extended_names_size)
359 {
360 bfd_set_error (bfd_error_malformed_archive);
361 return NULL((void*)0);
362 }
363
364 return bfd_ardata (arch)((arch)->tdata.aout_ar_data)->extended_names + index;
365}
366
367/* This functions reads an arch header and returns an areltdata pointer, or
368 NULL on error.
369
370 Presumes the file pointer is already in the right place (ie pointing
371 to the ar_hdr in the file). Moves the file pointer; on success it
372 should be pointing to the front of the file contents; on failure it
373 could have been moved arbitrarily. */
374
375void *
376_bfd_generic_read_ar_hdr (bfd *abfd)
377{
378 return _bfd_generic_read_ar_hdr_mag (abfd, NULL((void*)0));
379}
380
381/* Alpha ECOFF uses an optional different ARFMAG value, so we have a
382 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */
383
384void *
385_bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag)
386{
387 struct ar_hdr hdr;
388 char *hdrp = (char *) &hdr;
389 size_t parsed_size;
390 struct areltdata *ared;
391 char *filename = NULL((void*)0);
392 bfd_size_type namelen = 0;
393 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
394 char *allocptr = 0;
395
396 if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
397 {
398 if (bfd_get_error () != bfd_error_system_call)
399 bfd_set_error (bfd_error_no_more_archived_files);
400 return NULL((void*)0);
401 }
402 if (strncmp (hdr.ar_fmag, ARFMAG"`\012", 2) != 0
403 && (mag == NULL((void*)0)
404 || strncmp (hdr.ar_fmag, mag, 2) != 0))
405 {
406 bfd_set_error (bfd_error_malformed_archive);
407 return NULL((void*)0);
408 }
409
410 errno(*__errno()) = 0;
411 parsed_size = strtol (hdr.ar_size, NULL((void*)0), 10);
412 if (errno(*__errno()) != 0)
413 {
414 bfd_set_error (bfd_error_malformed_archive);
415 return NULL((void*)0);
416 }
417
418 /* Extract the filename from the archive - there are two ways to
419 specify an extended name table, either the first char of the
420 name is a space, or it's a slash. */
421 if ((hdr.ar_name[0] == '/'
422 || (hdr.ar_name[0] == ' '
423 && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen)) == NULL((void*)0)))
424 && bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names != NULL((void*)0))
425 {
426 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
427 if (filename == NULL((void*)0))
428 return NULL((void*)0);
429 }
430 /* BSD4.4-style long filename.
431 Only implemented for reading, so far! */
432 else if (hdr.ar_name[0] == '#'
433 && hdr.ar_name[1] == '1'
434 && hdr.ar_name[2] == '/'
435 && ISDIGIT (hdr.ar_name[3])(_sch_istable[(hdr.ar_name[3]) & 0xff] & (unsigned short
)(_sch_isdigit))
)
436 {
437 /* BSD-4.4 extended name */
438 namelen = atoi (&hdr.ar_name[3]);
439 allocsize += namelen + 1;
440 parsed_size -= namelen;
441
442 allocptr = bfd_zalloc (abfd, allocsize);
443 if (allocptr == NULL((void*)0))
444 return NULL((void*)0);
445 filename = (allocptr
446 + sizeof (struct areltdata)
447 + sizeof (struct ar_hdr));
448 if (bfd_bread (filename, namelen, abfd) != namelen)
449 {
450 if (bfd_get_error () != bfd_error_system_call)
451 bfd_set_error (bfd_error_no_more_archived_files);
452 return NULL((void*)0);
453 }
454 filename[namelen] = '\0';
455 }
456 else
457 {
458 /* We judge the end of the name by looking for '/' or ' '.
459 Note: The SYSV format (terminated by '/') allows embedded
460 spaces, so only look for ' ' if we don't find '/'. */
461
462 char *e;
463 e = memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen));
464 if (e == NULL((void*)0))
465 {
466 e = memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen));
467 if (e == NULL((void*)0))
468 e = memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen));
469 }
470
471 if (e != NULL((void*)0))
472 namelen = e - hdr.ar_name;
473 else
474 {
475 /* If we didn't find a termination character, then the name
476 must be the entire field. */
477 namelen = ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen);
478 }
479
480 allocsize += namelen + 1;
481 }
482
483 if (!allocptr)
484 {
485 allocptr = bfd_zalloc (abfd, allocsize);
486 if (allocptr == NULL((void*)0))
487 return NULL((void*)0);
488 }
489
490 ared = (struct areltdata *) allocptr;
491
492 ared->arch_header = allocptr + sizeof (struct areltdata);
493 memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
494 ared->parsed_size = parsed_size;
495
496 if (filename != NULL((void*)0))
497 ared->filename = filename;
498 else
499 {
500 ared->filename = allocptr + (sizeof (struct areltdata) +
501 sizeof (struct ar_hdr));
502 if (namelen)
503 memcpy (ared->filename, hdr.ar_name, namelen);
504 ared->filename[namelen] = '\0';
505 }
506
507 return ared;
508}
509
510/* This is an internal function; it's mainly used when indexing
511 through the archive symbol table, but also used to get the next
512 element, since it handles the bookkeeping so nicely for us. */
513
514bfd *
515_bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
516{
517 struct areltdata *new_areldata;
518 bfd *n_nfd;
519
520 if (archive->my_archive)
521 {
522 filepos += archive->origin;
523 archive = archive->my_archive;
524 }
525
526 n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos);
527 if (n_nfd)
528 return n_nfd;
529
530 if (0 > bfd_seek (archive, filepos, SEEK_SET0))
531 return NULL((void*)0);
532
533 if ((new_areldata = _bfd_read_ar_hdr (archive)((*((archive)->xvec->_bfd_read_ar_hdr_fn)) (archive))) == NULL((void*)0))
534 return NULL((void*)0);
535
536 n_nfd = _bfd_create_empty_archive_element_shell (archive);
537 if (n_nfd == NULL((void*)0))
538 {
539 bfd_release (archive, new_areldata);
540 return NULL((void*)0);
541 }
542
543 n_nfd->origin = bfd_tell (archive);
544 n_nfd->arelt_data = new_areldata;
545 n_nfd->filename = new_areldata->filename;
546
547 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
548 return n_nfd;
549
550 /* Huh? */
551 bfd_release (archive, n_nfd);
552 bfd_release (archive, new_areldata);
553 return NULL((void*)0);
554}
555
556/* Return the BFD which is referenced by the symbol in ABFD indexed by
557 INDEX. INDEX should have been returned by bfd_get_next_mapent. */
558
559bfd *
560_bfd_generic_get_elt_at_index (bfd *abfd, symindex index)
561{
562 carsym *entry;
563
564 entry = bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->symdefs + index;
565 return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
566}
567
568/*
569FUNCTION
570 bfd_openr_next_archived_file
571
572SYNOPSIS
573 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
574
575DESCRIPTION
576 Provided a BFD, @var{archive}, containing an archive and NULL, open
577 an input BFD on the first contained element and returns that.
578 Subsequent calls should pass
579 the archive and the previous return value to return a created
580 BFD to the next contained element. NULL is returned when there
581 are no more.
582*/
583
584bfd *
585bfd_openr_next_archived_file (bfd *archive, bfd *last_file)
586{
587 if ((bfd_get_format (archive)((archive)->format) != bfd_archive) ||
588 (archive->direction == write_direction))
589 {
590 bfd_set_error (bfd_error_invalid_operation);
591 return NULL((void*)0);
592 }
593
594 return BFD_SEND (archive,((*((archive)->xvec->openr_next_archived_file)) (archive
, last_file))
595 openr_next_archived_file, (archive, last_file))((*((archive)->xvec->openr_next_archived_file)) (archive
, last_file))
;
596}
597
598bfd *
599bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
600{
601 file_ptr filestart;
602
603 if (!last_file)
604 filestart = bfd_ardata (archive)((archive)->tdata.aout_ar_data)->first_file_filepos;
605 else
606 {
607 unsigned int size = arelt_size (last_file)(((struct areltdata *)((last_file)->arelt_data))->parsed_size
)
;
608 filestart = last_file->origin + size;
609 if (archive->my_archive)
610 filestart -= archive->origin;
611 /* Pad to an even boundary...
612 Note that last_file->origin can be odd in the case of
613 BSD-4.4-style element with a long odd size. */
614 filestart += filestart % 2;
615 }
616
617 return _bfd_get_elt_at_filepos (archive, filestart);
618}
619
620const bfd_target *
621bfd_generic_archive_p (bfd *abfd)
622{
623 struct artdata *tdata_hold;
624 char armag[SARMAG8 + 1];
625 bfd_size_type amt;
626
627 if (bfd_bread (armag, SARMAG8, abfd) != SARMAG8)
628 {
629 if (bfd_get_error () != bfd_error_system_call)
630 bfd_set_error (bfd_error_wrong_format);
631 return NULL((void*)0);
632 }
633
634 if (strncmp (armag, ARMAG"!<arch>\012", SARMAG8) != 0 &&
635 strncmp (armag, ARMAGB"!<bout>\012", SARMAG8) != 0)
636 return 0;
637
638 tdata_hold = bfd_ardata (abfd)((abfd)->tdata.aout_ar_data);
639
640 amt = sizeof (struct artdata);
641 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data) = bfd_zalloc (abfd, amt);
642 if (bfd_ardata (abfd)((abfd)->tdata.aout_ar_data) == NULL((void*)0))
643 {
644 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data) = tdata_hold;
645 return NULL((void*)0);
646 }
647
648 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->first_file_filepos = SARMAG8;
649 /* Cleared by bfd_zalloc above.
650 bfd_ardata (abfd)->cache = NULL;
651 bfd_ardata (abfd)->archive_head = NULL;
652 bfd_ardata (abfd)->symdefs = NULL;
653 bfd_ardata (abfd)->extended_names = NULL;
654 bfd_ardata (abfd)->extended_names_size = 0;
655 bfd_ardata (abfd)->tdata = NULL; */
656
657 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))((*((abfd)->xvec->_bfd_slurp_armap)) (abfd))
658 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))((*((abfd)->xvec->_bfd_slurp_extended_name_table)) (abfd
))
)
659 {
660 if (bfd_get_error () != bfd_error_system_call)
661 bfd_set_error (bfd_error_wrong_format);
662 bfd_release (abfd, bfd_ardata (abfd)((abfd)->tdata.aout_ar_data));
663 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data) = tdata_hold;
664 return NULL((void*)0);
665 }
666
667 if (bfd_has_map (abfd)((abfd)->has_armap))
668 {
669 bfd *first;
670
671 /* This archive has a map, so we may presume that the contents
672 are object files. Make sure that if the first file in the
673 archive can be recognized as an object file, it is for this
674 target. If not, assume that this is the wrong format. If
675 the first file is not an object file, somebody is doing
676 something weird, and we permit it so that ar -t will work.
677
678 This is done because any normal format will recognize any
679 normal archive, regardless of the format of the object files.
680 We do accept an empty archive. */
681
682 first = bfd_openr_next_archived_file (abfd, NULL((void*)0));
683 if (first != NULL((void*)0))
684 {
685 bfd_boolean fail;
686
687 first->target_defaulted = FALSE0;
688 fail = FALSE0;
Value stored to 'fail' is never read
689 if (bfd_check_format (first, bfd_object)
690 && first->xvec != abfd->xvec)
691 {
692 bfd_set_error (bfd_error_wrong_object_format);
693 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data) = tdata_hold;
694 return NULL((void*)0);
695 }
696 /* And we ought to close `first' here too. */
697 }
698 }
699
700 return abfd->xvec;
701}
702
703/* Some constants for a 32 bit BSD archive structure. We do not
704 support 64 bit archives presently; so far as I know, none actually
705 exist. Supporting them would require changing these constants, and
706 changing some H_GET_32 to H_GET_64. */
707
708/* The size of an external symdef structure. */
709#define BSD_SYMDEF_SIZE8 8
710
711/* The offset from the start of a symdef structure to the file offset. */
712#define BSD_SYMDEF_OFFSET_SIZE4 4
713
714/* The size of the symdef count. */
715#define BSD_SYMDEF_COUNT_SIZE4 4
716
717/* The size of the string count. */
718#define BSD_STRING_COUNT_SIZE4 4
719
720/* Returns FALSE on error, TRUE otherwise. */
721
722static bfd_boolean
723do_slurp_bsd_armap (bfd *abfd)
724{
725 struct areltdata *mapdata;
726 unsigned int counter;
727 bfd_byte *raw_armap, *rbase;
728 struct artdata *ardata = bfd_ardata (abfd)((abfd)->tdata.aout_ar_data);
729 char *stringbase;
730 bfd_size_type parsed_size, amt;
731 carsym *set;
732
733 mapdata = _bfd_read_ar_hdr (abfd)((*((abfd)->xvec->_bfd_read_ar_hdr_fn)) (abfd));
734 if (mapdata == NULL((void*)0))
735 return FALSE0;
736 parsed_size = mapdata->parsed_size;
737 bfd_release (abfd, mapdata); /* Don't need it any more. */
738
739 raw_armap = bfd_zalloc (abfd, parsed_size);
740 if (raw_armap == NULL((void*)0))
741 return FALSE0;
742
743 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
744 {
745 if (bfd_get_error () != bfd_error_system_call)
746 bfd_set_error (bfd_error_malformed_archive);
747 byebye:
748 bfd_release (abfd, raw_armap);
749 return FALSE0;
750 }
751
752 ardata->symdef_count = H_GET_32 (abfd, raw_armap)((*((abfd)->xvec->bfd_h_getx32)) (raw_armap)) / BSD_SYMDEF_SIZE8;
753
754 if (ardata->symdef_count * BSD_SYMDEF_SIZE8 >
755 parsed_size - BSD_SYMDEF_COUNT_SIZE4)
756 {
757 /* Probably we're using the wrong byte ordering. */
758 bfd_set_error (bfd_error_wrong_format);
759 goto byebye;
760 }
761
762 ardata->cache = 0;
763 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE4;
764 stringbase = ((char *) rbase
765 + ardata->symdef_count * BSD_SYMDEF_SIZE8
766 + BSD_STRING_COUNT_SIZE4);
767 amt = ardata->symdef_count * sizeof (carsym);
768 ardata->symdefs = bfd_alloc (abfd, amt);
769 if (!ardata->symdefs)
770 return FALSE0;
771
772 for (counter = 0, set = ardata->symdefs;
773 counter < ardata->symdef_count;
774 counter++, set++, rbase += BSD_SYMDEF_SIZE8)
775 {
776 set->name = H_GET_32 (abfd, rbase)((*((abfd)->xvec->bfd_h_getx32)) (rbase)) + stringbase;
777 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE)((*((abfd)->xvec->bfd_h_getx32)) (rbase + 4));
778 }
779
780 ardata->first_file_filepos = bfd_tell (abfd);
781 /* Pad to an even boundary if you have to. */
782 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
783 /* FIXME, we should provide some way to free raw_ardata when
784 we are done using the strings from it. For now, it seems
785 to be allocated on an objalloc anyway... */
786 bfd_has_map (abfd)((abfd)->has_armap) = TRUE1;
787 return TRUE1;
788}
789
790/* Returns FALSE on error, TRUE otherwise. */
791
792static bfd_boolean
793do_slurp_coff_armap (bfd *abfd)
794{
795 struct areltdata *mapdata;
796 int *raw_armap, *rawptr;
797 struct artdata *ardata = bfd_ardata (abfd)((abfd)->tdata.aout_ar_data);
798 char *stringbase;
799 bfd_size_type stringsize;
800 unsigned int parsed_size;
801 carsym *carsyms;
802 bfd_size_type nsymz; /* Number of symbols in armap. */
803 bfd_vma (*swap) (const void *);
804 char int_buf[sizeof (long)];
805 bfd_size_type carsym_size, ptrsize;
806 unsigned int i;
807
808 mapdata = _bfd_read_ar_hdr (abfd)((*((abfd)->xvec->_bfd_read_ar_hdr_fn)) (abfd));
809 if (mapdata == NULL((void*)0))
810 return FALSE0;
811 parsed_size = mapdata->parsed_size;
812 bfd_release (abfd, mapdata); /* Don't need it any more. */
813
814 if (bfd_bread (int_buf, 4, abfd) != 4)
815 {
816 if (bfd_get_error () != bfd_error_system_call)
817 bfd_set_error (bfd_error_malformed_archive);
818 return FALSE0;
819 }
820 /* It seems that all numeric information in a coff archive is always
821 in big endian format, nomatter the host or target. */
822 swap = bfd_getb32;
823 nsymz = bfd_getb32 (int_buf);
824 stringsize = parsed_size - (4 * nsymz) - 4;
825
826 /* ... except that some archive formats are broken, and it may be our
827 fault - the i960 little endian coff sometimes has big and sometimes
828 little, because our tools changed. Here's a horrible hack to clean
829 up the crap. */
830
831 if (stringsize > 0xfffff
832 && bfd_get_arch (abfd) == bfd_arch_i960
833 && bfd_get_flavour (abfd)((abfd)->xvec->flavour) == bfd_target_coff_flavour)
834 {
835 /* This looks dangerous, let's do it the other way around. */
836 nsymz = bfd_getl32 (int_buf);
837 stringsize = parsed_size - (4 * nsymz) - 4;
838 swap = bfd_getl32;
839 }
840
841 /* The coff armap must be read sequentially. So we construct a
842 bsd-style one in core all at once, for simplicity. */
843
844 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
845 return FALSE0;
846
847 carsym_size = (nsymz * sizeof (carsym));
848 ptrsize = (4 * nsymz);
849
850 if (carsym_size + stringsize + 1 <= carsym_size)
851 return FALSE0;
852
853 ardata->symdefs = bfd_zalloc (abfd, carsym_size + stringsize + 1);
854 if (ardata->symdefs == NULL((void*)0))
855 return FALSE0;
856 carsyms = ardata->symdefs;
857 stringbase = ((char *) ardata->symdefs) + carsym_size;
858
859 /* Allocate and read in the raw offsets. */
860 raw_armap = bfd_alloc (abfd, ptrsize);
861 if (raw_armap == NULL((void*)0))
862 goto release_symdefs;
863 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
864 || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
865 {
866 if (bfd_get_error () != bfd_error_system_call)
867 bfd_set_error (bfd_error_malformed_archive);
868 goto release_raw_armap;
869 }
870
871 /* OK, build the carsyms. */
872 for (i = 0; i < nsymz; i++)
873 {
874 rawptr = raw_armap + i;
875 carsyms->file_offset = swap ((bfd_byte *) rawptr);
876 carsyms->name = stringbase;
877 stringbase += strlen (stringbase) + 1;
878 carsyms++;
879 }
880 *stringbase = 0;
881
882 ardata->symdef_count = nsymz;
883 ardata->first_file_filepos = bfd_tell (abfd);
884 /* Pad to an even boundary if you have to. */
885 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
886
887 bfd_has_map (abfd)((abfd)->has_armap) = TRUE1;
888 bfd_release (abfd, raw_armap);
889
890 /* Check for a second archive header (as used by PE). */
891 {
892 struct areltdata *tmp;
893
894 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET0);
895 tmp = _bfd_read_ar_hdr (abfd)((*((abfd)->xvec->_bfd_read_ar_hdr_fn)) (abfd));
896 if (tmp != NULL((void*)0))
897 {
898 if (tmp->arch_header[0] == '/'
899 && tmp->arch_header[1] == ' ')
900 {
901 ardata->first_file_filepos +=
902 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
903 }
904 bfd_release (abfd, tmp);
905 }
906 }
907
908 return TRUE1;
909
910release_raw_armap:
911 bfd_release (abfd, raw_armap);
912release_symdefs:
913 bfd_release (abfd, (ardata)->symdefs);
914 return FALSE0;
915}
916
917/* This routine can handle either coff-style or bsd-style armaps.
918 Returns FALSE on error, TRUE otherwise */
919
920bfd_boolean
921bfd_slurp_armap (bfd *abfd)
922{
923 char nextname[17];
924 int i = bfd_bread (nextname, 16, abfd);
925
926 if (i == 0)
927 return TRUE1;
928 if (i != 16)
929 return FALSE0;
930
931 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR1) != 0)
932 return FALSE0;
933
934 if (!strncmp (nextname, "__.SYMDEF ", 16)
935 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */
936 return do_slurp_bsd_armap (abfd);
937 else if (!strncmp (nextname, "/ ", 16))
938 return do_slurp_coff_armap (abfd);
939 else if (!strncmp (nextname, "/SYM64/ ", 16))
940 {
941 /* 64bit ELF (Irix 6) archive. */
942#ifdef BFD64
943 extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
944 return bfd_elf64_archive_slurp_armap (abfd);
945#else
946 bfd_set_error (bfd_error_wrong_format);
947 return FALSE0;
948#endif
949 }
950
951 bfd_has_map (abfd)((abfd)->has_armap) = FALSE0;
952 return TRUE1;
953}
954
955/* Returns FALSE on error, TRUE otherwise. */
956/* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
957 header is in a slightly different order and the map name is '/'.
958 This flavour is used by hp300hpux. */
959
960#define HPUX_SYMDEF_COUNT_SIZE2 2
961
962bfd_boolean
963bfd_slurp_bsd_armap_f2 (bfd *abfd)
964{
965 struct areltdata *mapdata;
966 char nextname[17];
967 unsigned int counter;
968 bfd_byte *raw_armap, *rbase;
969 struct artdata *ardata = bfd_ardata (abfd)((abfd)->tdata.aout_ar_data);
970 char *stringbase;
971 unsigned int stringsize;
972 bfd_size_type amt;
973 carsym *set;
974 int i = bfd_bread (nextname, 16, abfd);
975
976 if (i == 0)
977 return TRUE1;
978 if (i != 16)
979 return FALSE0;
980
981 /* The archive has at least 16 bytes in it. */
982 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR1) != 0)
983 return FALSE0;
984
985 if (!strncmp (nextname, "__.SYMDEF ", 16)
986 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* Old Linux archives. */
987 return do_slurp_bsd_armap (abfd);
988
989 if (strncmp (nextname, "/ ", 16))
990 {
991 bfd_has_map (abfd)((abfd)->has_armap) = FALSE0;
992 return TRUE1;
993 }
994
995 mapdata = _bfd_read_ar_hdr (abfd)((*((abfd)->xvec->_bfd_read_ar_hdr_fn)) (abfd));
996 if (mapdata == NULL((void*)0))
997 return FALSE0;
998
999 amt = mapdata->parsed_size;
1000 raw_armap = bfd_zalloc (abfd, amt);
1001 if (raw_armap == NULL((void*)0))
1002 {
1003 byebye:
1004 bfd_release (abfd, mapdata);
1005 return FALSE0;
1006 }
1007
1008 if (bfd_bread (raw_armap, amt, abfd) != amt)
1009 {
1010 if (bfd_get_error () != bfd_error_system_call)
1011 bfd_set_error (bfd_error_malformed_archive);
1012 byebyebye:
1013 bfd_release (abfd, raw_armap);
1014 goto byebye;
1015 }
1016
1017 ardata->symdef_count = H_GET_16 (abfd, raw_armap)((*((abfd)->xvec->bfd_h_getx16)) (raw_armap));
1018
1019 if (ardata->symdef_count * BSD_SYMDEF_SIZE8
1020 > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE2)
1021 {
1022 /* Probably we're using the wrong byte ordering. */
1023 bfd_set_error (bfd_error_wrong_format);
1024 goto byebyebye;
1025 }
1026
1027 ardata->cache = 0;
1028
1029 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE)((*((abfd)->xvec->bfd_h_getx32)) (raw_armap + 2));
1030 /* Skip sym count and string sz. */
1031 stringbase = ((char *) raw_armap
1032 + HPUX_SYMDEF_COUNT_SIZE2
1033 + BSD_STRING_COUNT_SIZE4);
1034 rbase = (bfd_byte *) stringbase + stringsize;
1035 amt = ardata->symdef_count * BSD_SYMDEF_SIZE8;
1036 ardata->symdefs = bfd_alloc (abfd, amt);
1037 if (!ardata->symdefs)
1038 return FALSE0;
1039
1040 for (counter = 0, set = ardata->symdefs;
1041 counter < ardata->symdef_count;
1042 counter++, set++, rbase += BSD_SYMDEF_SIZE8)
1043 {
1044 set->name = H_GET_32 (abfd, rbase)((*((abfd)->xvec->bfd_h_getx32)) (rbase)) + stringbase;
1045 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE)((*((abfd)->xvec->bfd_h_getx32)) (rbase + 4));
1046 }
1047
1048 ardata->first_file_filepos = bfd_tell (abfd);
1049 /* Pad to an even boundary if you have to. */
1050 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1051 /* FIXME, we should provide some way to free raw_ardata when
1052 we are done using the strings from it. For now, it seems
1053 to be allocated on an objalloc anyway... */
1054 bfd_has_map (abfd)((abfd)->has_armap) = TRUE1;
1055 return TRUE1;
1056}
1057
1058/** Extended name table.
1059
1060 Normally archives support only 14-character filenames.
1061
1062 Intel has extended the format: longer names are stored in a special
1063 element (the first in the archive, or second if there is an armap);
1064 the name in the ar_hdr is replaced by <space><index into filename
1065 element>. Index is the P.R. of an int (decimal). Data General have
1066 extended the format by using the prefix // for the special element. */
1067
1068/* Returns FALSE on error, TRUE otherwise. */
1069
1070bfd_boolean
1071_bfd_slurp_extended_name_table (bfd *abfd)
1072{
1073 char nextname[17];
1074 struct areltdata *namedata;
1075 bfd_size_type amt;
1076
1077 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
1078 we probably don't want to return TRUE. */
1079 bfd_seek (abfd, bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->first_file_filepos, SEEK_SET0);
1080 if (bfd_bread (nextname, 16, abfd) == 16)
1081 {
1082 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR1) != 0)
1083 return FALSE0;
1084
1085 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
1086 strncmp (nextname, "// ", 16) != 0)
1087 {
1088 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names = NULL((void*)0);
1089 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names_size = 0;
1090 return TRUE1;
1091 }
1092
1093 namedata = _bfd_read_ar_hdr (abfd)((*((abfd)->xvec->_bfd_read_ar_hdr_fn)) (abfd));
1094 if (namedata == NULL((void*)0))
1095 return FALSE0;
1096
1097 amt = namedata->parsed_size;
1098 if (amt + 1 == 0)
1099 goto byebye;
1100
1101 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names_size = amt;
1102 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names = bfd_zalloc (abfd, amt + 1);
1103 if (bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names == NULL((void*)0))
1104 {
1105 byebye:
1106 bfd_release (abfd, namedata);
1107 return FALSE0;
1108 }
1109
1110 if (bfd_bread (bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names, amt, abfd) != amt)
1111 {
1112 if (bfd_get_error () != bfd_error_system_call)
1113 bfd_set_error (bfd_error_malformed_archive);
1114 bfd_release (abfd, (bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names));
1115 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names = NULL((void*)0);
1116 goto byebye;
1117 }
1118
1119 /* Since the archive is supposed to be printable if it contains
1120 text, the entries in the list are newline-padded, not null
1121 padded. In SVR4-style archives, the names also have a
1122 trailing '/'. DOS/NT created archive often have \ in them
1123 We'll fix all problems here.. */
1124 {
1125 char *ext_names = bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->extended_names;
1126 char *temp = ext_names;
1127 char *limit = temp + namedata->parsed_size;
1128 for (; temp < limit; ++temp)
1129 {
1130 if (*temp == '\012')
1131 temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1132 if (*temp == '\\')
1133 *temp = '/';
1134 }
1135 *limit = '\0';
1136 }
1137
1138 /* Pad to an even boundary if you have to. */
1139 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->first_file_filepos = bfd_tell (abfd);
1140 bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->first_file_filepos +=
1141 (bfd_ardata (abfd)((abfd)->tdata.aout_ar_data)->first_file_filepos) % 2;
1142
1143 /* FIXME, we can't release namedata here because it was allocated
1144 below extended_names on the objalloc... */
1145 }
1146 return TRUE1;
1147}
1148
1149#ifdef VMS
1150
1151/* Return a copy of the stuff in the filename between any :]> and a
1152 semicolon. */
1153
1154static const char *
1155normalize (bfd *abfd, const char *file)
1156{
1157 const char *first;
1158 const char *last;
1159 char *copy;
1160
1161 first = file + strlen (file) - 1;
1162 last = first + 1;
1163
1164 while (first != file)
1165 {
1166 if (*first == ';')
1167 last = first;
1168 if (*first == ':' || *first == ']' || *first == '>')
1169 {
1170 first++;
1171 break;
1172 }
1173 first--;
1174 }
1175
1176 copy = bfd_alloc (abfd, last - first + 1);
1177 if (copy == NULL((void*)0))
1178 return NULL((void*)0);
1179
1180 memcpy (copy, first, last - first);
1181 copy[last - first] = 0;
1182
1183 return copy;
1184}
1185
1186#else
1187static const char *
1188normalize (bfd *abfd ATTRIBUTE_UNUSED__attribute__ ((__unused__)), const char *file)
1189{
1190 const char *filename = strrchr (file, '/');
1191
1192#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1193 {
1194 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1195 char *bslash = strrchr (file, '\\');
1196 if (filename == NULL((void*)0) || (bslash != NULL((void*)0) && bslash > filename))
1197 filename = bslash;
1198 if (filename == NULL((void*)0) && file[0] != '\0' && file[1] == ':')
1199 filename = file + 1;
1200 }
1201#endif
1202 if (filename != NULL((void*)0))
1203 filename++;
1204 else
1205 filename = file;
1206 return filename;
1207}
1208#endif
1209
1210/* Build a BFD style extended name table. */
1211
1212bfd_boolean
1213_bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1214 char **tabloc,
1215 bfd_size_type *tablen,
1216 const char **name)
1217{
1218 *name = "ARFILENAMES/";
1219 return _bfd_construct_extended_name_table (abfd, FALSE0, tabloc, tablen);
1220}
1221
1222/* Build an SVR4 style extended name table. */
1223
1224bfd_boolean
1225_bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1226 char **tabloc,
1227 bfd_size_type *tablen,
1228 const char **name)
1229{
1230 *name = "//";
1231 return _bfd_construct_extended_name_table (abfd, TRUE1, tabloc, tablen);
1232}
1233
1234/* Follows archive_head and produces an extended name table if
1235 necessary. Returns (in tabloc) a pointer to an extended name
1236 table, and in tablen the length of the table. If it makes an entry
1237 it clobbers the filename so that the element may be written without
1238 further massage. Returns TRUE if it ran successfully, FALSE if
1239 something went wrong. A successful return may still involve a
1240 zero-length tablen! */
1241
1242bfd_boolean
1243_bfd_construct_extended_name_table (bfd *abfd,
1244 bfd_boolean trailing_slash,
1245 char **tabloc,
1246 bfd_size_type *tablen)
1247{
1248 unsigned int maxname = abfd->xvec->ar_max_namelen;
1249 bfd_size_type total_namelen = 0;
1250 bfd *current;
1251 char *strptr;
1252
1253 *tablen = 0;
1254
1255 /* Figure out how long the table should be. */
1256 for (current = abfd->archive_head; current != NULL((void*)0); current = current->next)
1257 {
1258 const char *normal;
1259 unsigned int thislen;
1260
1261 normal = normalize (current, current->filename);
1262 if (normal == NULL((void*)0))
1263 return FALSE0;
1264
1265 thislen = strlen (normal);
1266
1267 if (thislen > maxname
1268 && (bfd_get_file_flags (abfd)((abfd)->flags) & BFD_TRADITIONAL_FORMAT0x400) != 0)
1269 thislen = maxname;
1270
1271 if (thislen > maxname)
1272 {
1273 /* Add one to leave room for \n. */
1274 total_namelen += thislen + 1;
1275 if (trailing_slash)
1276 {
1277 /* Leave room for trailing slash. */
1278 ++total_namelen;
1279 }
1280 }
1281 else
1282 {
1283 struct ar_hdr *hdr = arch_hdr (current)((struct ar_hdr *) ((struct areltdata *) ((current)->arelt_data
))->arch_header)
;
1284 if (strncmp (normal, hdr->ar_name, thislen) != 0
1285 || (thislen < sizeof hdr->ar_name
1286 && hdr->ar_name[thislen] != ar_padchar (current)((current)->xvec->ar_pad_char)))
1287 {
1288 /* Must have been using extended format even though it
1289 didn't need to. Fix it to use normal format. */
1290 memcpy (hdr->ar_name, normal, thislen);
1291 if (thislen < maxname
1292 || (thislen == maxname && thislen < sizeof hdr->ar_name))
1293 hdr->ar_name[thislen] = ar_padchar (current)((current)->xvec->ar_pad_char);
1294 }
1295 }
1296 }
1297
1298 if (total_namelen == 0)
1299 return TRUE1;
1300
1301 *tabloc = bfd_zalloc (abfd, total_namelen);
1302 if (*tabloc == NULL((void*)0))
1303 return FALSE0;
1304
1305 *tablen = total_namelen;
1306 strptr = *tabloc;
1307
1308 for (current = abfd->archive_head; current != NULL((void*)0); current =
1309 current->next)
1310 {
1311 const char *normal;
1312 unsigned int thislen;
1313
1314 normal = normalize (current, current->filename);
1315 if (normal == NULL((void*)0))
1316 return FALSE0;
1317
1318 thislen = strlen (normal);
1319 if (thislen > maxname)
1320 {
1321 /* Works for now; may need to be re-engineered if we
1322 encounter an oddball archive format and want to
1323 generalise this hack. */
1324 struct ar_hdr *hdr = arch_hdr (current)((struct ar_hdr *) ((struct areltdata *) ((current)->arelt_data
))->arch_header)
;
1325 strcpy (strptr, normal);
1326 if (! trailing_slash)
1327 strptr[thislen] = '\012';
1328 else
1329 {
1330 strptr[thislen] = '/';
1331 strptr[thislen + 1] = '\012';
1332 }
1333 hdr->ar_name[0] = ar_padchar (current)((current)->xvec->ar_pad_char);
1334 _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld",
1335 strptr - *tabloc);
1336 strptr += thislen + 1;
1337 if (trailing_slash)
1338 ++strptr;
1339 }
1340 }
1341
1342 return TRUE1;
1343}
1344
1345/* A couple of functions for creating ar_hdrs. */
1346
1347#ifdef HPUX_LARGE_AR_IDS
1348/* Function to encode large UID/GID values according to HP. */
1349
1350static void
1351hpux_uid_gid_encode (char str[6], long int id)
1352{
1353 int cnt;
1354
1355 str[5] = '@' + (id & 3);
1356 id >>= 2;
1357
1358 for (cnt = 4; cnt >= 0; ++cnt, id >>= 6)
1359 str[cnt] = ' ' + (id & 0x3f);
1360}
1361#endif /* HPUX_LARGE_AR_IDS */
1362
1363#ifndef HAVE_GETUID1
1364#define getuid() 0
1365#endif
1366
1367#ifndef HAVE_GETGID1
1368#define getgid() 0
1369#endif
1370
1371/* Takes a filename, returns an arelt_data for it, or NULL if it can't
1372 make one. The filename must refer to a filename in the filesystem.
1373 The filename field of the ar_hdr will NOT be initialized. If member
1374 is set, and it's an in-memory bfd, we fake it. */
1375
1376static struct areltdata *
1377bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
1378{
1379 struct stat status;
1380 struct areltdata *ared;
1381 struct ar_hdr *hdr;
1382 bfd_size_type amt;
1383
1384 if (member && (member->flags & BFD_IN_MEMORY0x800) != 0)
1385 {
1386 /* Assume we just "made" the member, and fake it. */
1387 struct bfd_in_memory *bim = member->iostream;
1388 time (&status.st_mtimest_mtim.tv_sec);
1389 status.st_uid = getuid ();
1390 status.st_gid = getgid ();
1391 status.st_mode = 0644;
1392 status.st_size = bim->size;
1393 }
1394 else if (stat (filename, &status) != 0)
1395 {
1396 bfd_set_error (bfd_error_system_call);
1397 return NULL((void*)0);
1398 }
1399
1400 if ((abfd->flags & BFD_DETERMINISTIC0x4000) != 0)
1401 {
1402 status.st_mtimest_mtim.tv_sec = 0;
1403 status.st_uid = 0;
1404 status.st_gid = 0;
1405 status.st_mode = 0644;
1406 }
1407
1408 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
1409 ared = bfd_zalloc (abfd, amt);
1410 if (ared == NULL((void*)0))
1411 return NULL((void*)0);
1412 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1413
1414 /* ar headers are space padded, not null padded! */
1415 memset (hdr, ' ', sizeof (struct ar_hdr));
1416
1417 _bfd_ar_spacepadll (hdr->ar_date, sizeof (hdr->ar_date), "%-12lld",
1418 status.st_mtimest_mtim.tv_sec);
1419#ifdef HPUX_LARGE_AR_IDS
1420 /* HP has a very "special" way to handle UID/GID's with numeric values
1421 > 99999. */
1422 if (status.st_uid > 99999)
1423 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid);
1424 else
1425#endif
1426 _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
1427 status.st_uid);
1428#ifdef HPUX_LARGE_AR_IDS
1429 /* HP has a very "special" way to handle UID/GID's with numeric values
1430 > 99999. */
1431 if (status.st_gid > 99999)
1432 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid);
1433 else
1434#endif
1435 _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
1436 status.st_gid);
1437 _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
1438 status.st_mode);
1439 _bfd_ar_spacepadll (hdr->ar_size, sizeof (hdr->ar_size), "%-10lld",
1440 status.st_size);
1441 memcpy (hdr->ar_fmag, ARFMAG"`\012", 2);
1442 ared->parsed_size = status.st_size;
1443 ared->arch_header = (char *) hdr;
1444
1445 return ared;
1446}
1447
1448/* This is magic required by the "ar" program. Since it's
1449 undocumented, it's undocumented. You may think that it would take
1450 a strong stomach to write this, and it does, but it takes even a
1451 stronger stomach to try to code around such a thing! */
1452
1453struct ar_hdr *bfd_special_undocumented_glue (bfd *, const char *);
1454
1455struct ar_hdr *
1456bfd_special_undocumented_glue (bfd *abfd, const char *filename)
1457{
1458 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0);
1459 if (ar_elt == NULL((void*)0))
1460 return NULL((void*)0);
1461 return (struct ar_hdr *) ar_elt->arch_header;
1462}
1463
1464/* Analogous to stat call. */
1465
1466int
1467bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
1468{
1469 struct ar_hdr *hdr;
1470 char *aloser;
1471
1472 if (abfd->arelt_data == NULL((void*)0))
1473 {
1474 bfd_set_error (bfd_error_invalid_operation);
1475 return -1;
1476 }
1477
1478 hdr = arch_hdr (abfd)((struct ar_hdr *) ((struct areltdata *) ((abfd)->arelt_data
))->arch_header)
;
1479
1480#define foo(arelt, stelt, size)buf->stelt = strtol (hdr->arelt, &aloser, size); if
(aloser == hdr->arelt) return -1;
\
1481 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1482 if (aloser == hdr->arelt) \
1483 return -1;
1484
1485#define fooll(arelt, stelt, size)buf->stelt = strtoll (hdr->arelt, &aloser, size); if
(aloser == hdr->arelt) return -1;
\
1486 buf->stelt = strtoll (hdr->arelt, &aloser, size); \
1487 if (aloser == hdr->arelt) \
1488 return -1;
1489
1490 /* Some platforms support special notations for large IDs. */
1491#ifdef HPUX_LARGE_AR_IDS
1492# define foo2(arelt, stelt, size)buf->stelt = strtol (hdr->arelt, &aloser, size); if
(aloser == hdr->arelt) return -1;
\
1493 if (hdr->arelt[5] == ' ') \
1494 { \
1495 foo (arelt, stelt, size)buf->stelt = strtol (hdr->arelt, &aloser, size); if
(aloser == hdr->arelt) return -1;
; \
1496 } \
1497 else \
1498 { \
1499 int cnt; \
1500 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1501 { \
1502 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1503 return -1; \
1504 buf->stelt <<= 6; \
1505 buf->stelt += hdr->arelt[cnt] - ' '; \
1506 } \
1507 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1508 return -1; \
1509 buf->stelt <<= 2; \
1510 buf->stelt += hdr->arelt[5] - '@'; \
1511 }
1512#else
1513# define foo2(arelt, stelt, size)buf->stelt = strtol (hdr->arelt, &aloser, size); if
(aloser == hdr->arelt) return -1;
foo (arelt, stelt, size)buf->stelt = strtol (hdr->arelt, &aloser, size); if
(aloser == hdr->arelt) return -1;
1514#endif
1515
1516 fooll (ar_date, st_mtime, 10)buf->st_mtim.tv_sec = strtoll (hdr->ar_date, &aloser
, 10); if (aloser == hdr->ar_date) return -1;
;
1517 foo2 (ar_uid, st_uid, 10)buf->st_uid = strtol (hdr->ar_uid, &aloser, 10); if
(aloser == hdr->ar_uid) return -1;
;
1518 foo2 (ar_gid, st_gid, 10)buf->st_gid = strtol (hdr->ar_gid, &aloser, 10); if
(aloser == hdr->ar_gid) return -1;
;
1519 foo (ar_mode, st_mode, 8)buf->st_mode = strtol (hdr->ar_mode, &aloser, 8); if
(aloser == hdr->ar_mode) return -1;
;
1520
1521 buf->st_size = arch_eltdata (abfd)((struct areltdata *) ((abfd)->arelt_data))->parsed_size;
1522
1523 return 0;
1524}
1525
1526void
1527bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1528{
1529 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1530 Fortunately ic960 users will never use that option. Fixing this
1531 is very hard; fortunately I know how to do it and will do so once
1532 intel's release is out the door. */
1533
1534 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1535 size_t length;
1536 const char *filename;
1537 size_t maxlen = ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen);
1538
1539 if ((bfd_get_file_flags (abfd)((abfd)->flags) & BFD_TRADITIONAL_FORMAT0x400) != 0)
1540 {
1541 bfd_bsd_truncate_arname (abfd, pathname, arhdr);
1542 return;
1543 }
1544
1545 filename = normalize (abfd, pathname);
1546 if (filename == NULL((void*)0))
1547 {
1548 /* FIXME */
1549 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils-2.17/bfd/archive.c"
, 1549, __PRETTY_FUNCTION__)
;
1550 }
1551
1552 length = strlen (filename);
1553
1554 if (length <= maxlen)
1555 memcpy (hdr->ar_name, filename, length);
1556
1557 /* Add the padding character if there is room for it. */
1558 if (length < maxlen
1559 || (length == maxlen && length < sizeof hdr->ar_name))
1560 (hdr->ar_name)[length] = ar_padchar (abfd)((abfd)->xvec->ar_pad_char);
1561}
1562
1563void
1564bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1565{
1566 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1567 size_t length;
1568 const char *filename = strrchr (pathname, '/');
1569 size_t maxlen = ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen);
1570
1571#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1572 {
1573 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1574 char *bslash = strrchr (pathname, '\\');
1575 if (filename == NULL((void*)0) || (bslash != NULL((void*)0) && bslash > filename))
1576 filename = bslash;
1577 if (filename == NULL((void*)0) && pathname[0] != '\0' && pathname[1] == ':')
1578 filename = pathname + 1;
1579 }
1580#endif
1581
1582 if (filename == NULL((void*)0))
1583 filename = pathname;
1584 else
1585 ++filename;
1586
1587 length = strlen (filename);
1588
1589 if (length <= maxlen)
1590 memcpy (hdr->ar_name, filename, length);
1591 else
1592 {
1593 /* pathname: meet procrustes */
1594 memcpy (hdr->ar_name, filename, maxlen);
1595 length = maxlen;
1596 }
1597
1598 if (length < maxlen)
1599 (hdr->ar_name)[length] = ar_padchar (abfd)((abfd)->xvec->ar_pad_char);
1600}
1601
1602/* Store name into ar header. Truncates the name to fit.
1603 1> strip pathname to be just the basename.
1604 2> if it's short enuf to fit, stuff it in.
1605 3> If it doesn't end with .o, truncate it to fit
1606 4> truncate it before the .o, append .o, stuff THAT in. */
1607
1608/* This is what gnu ar does. It's better but incompatible with the
1609 bsd ar. */
1610
1611void
1612bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1613{
1614 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1615 size_t length;
1616 const char *filename = strrchr (pathname, '/');
1617 size_t maxlen = ar_maxnamelen (abfd)((abfd)->xvec->ar_max_namelen);
1618
1619#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1620 {
1621 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1622 char *bslash = strrchr (pathname, '\\');
1623 if (filename == NULL((void*)0) || (bslash != NULL((void*)0) && bslash > filename))
1624 filename = bslash;
1625 if (filename == NULL((void*)0) && pathname[0] != '\0' && pathname[1] == ':')
1626 filename = pathname + 1;
1627 }
1628#endif
1629
1630 if (filename == NULL((void*)0))
1631 filename = pathname;
1632 else
1633 ++filename;
1634
1635 length = strlen (filename);
1636
1637 if (length <= maxlen)
1638 memcpy (hdr->ar_name, filename, length);
1639 else
1640 { /* pathname: meet procrustes */
1641 memcpy (hdr->ar_name, filename, maxlen);
1642 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
1643 {
1644 hdr->ar_name[maxlen - 2] = '.';
1645 hdr->ar_name[maxlen - 1] = 'o';
1646 }
1647 length = maxlen;
1648 }
1649
1650 if (length < 16)
1651 (hdr->ar_name)[length] = ar_padchar (abfd)((abfd)->xvec->ar_pad_char);
1652}
1653
1654/* The BFD is open for write and has its format set to bfd_archive. */
1655
1656bfd_boolean
1657_bfd_write_archive_contents (bfd *arch)
1658{
1659 bfd *current;
1660 char *etable = NULL((void*)0);
1661 bfd_size_type elength = 0;
1662 const char *ename = NULL((void*)0);
1663 bfd_boolean makemap = bfd_has_map (arch)((arch)->has_armap);
1664 /* If no .o's, don't bother to make a map. */
1665 bfd_boolean hasobjects = FALSE0;
1666 bfd_size_type wrote;
1667 int tries;
1668
1669 /* Verify the viability of all entries; if any of them live in the
1670 filesystem (as opposed to living in an archive open for input)
1671 then construct a fresh ar_hdr for them. */
1672 for (current = arch->archive_head; current; current = current->next)
1673 {
1674 /* This check is checking the bfds for the objects we're reading
1675 from (which are usually either an object file or archive on
1676 disk), not the archive entries we're writing to. We don't
1677 actually create bfds for the archive members, we just copy
1678 them byte-wise when we write out the archive. */
1679 if (bfd_write_p (current)((current)->direction == write_direction || (current)->
direction == both_direction)
)
1680 {
1681 bfd_set_error (bfd_error_invalid_operation);
1682 return FALSE0;
1683 }
1684 if (!current->arelt_data)
1685 {
1686 current->arelt_data =
1687 bfd_ar_hdr_from_filesystem (arch, current->filename, current);
1688 if (!current->arelt_data)
1689 return FALSE0;
1690
1691 /* Put in the file name. */
1692 BFD_SEND (arch, _bfd_truncate_arname,((*((arch)->xvec->_bfd_truncate_arname)) (arch, current
->filename, (char *) ((struct ar_hdr *) ((struct areltdata
*) ((current)->arelt_data))->arch_header)))
1693 (arch, current->filename, (char *) arch_hdr (current)))((*((arch)->xvec->_bfd_truncate_arname)) (arch, current
->filename, (char *) ((struct ar_hdr *) ((struct areltdata
*) ((current)->arelt_data))->arch_header)))
;
1694 }
1695
1696 if (makemap && ! hasobjects)
1697 { /* Don't bother if we won't make a map! */
1698 if ((bfd_check_format (current, bfd_object)))
1699 hasobjects = TRUE1;
1700 }
1701 }
1702
1703 if (!BFD_SEND (arch, _bfd_construct_extended_name_table,((*((arch)->xvec->_bfd_construct_extended_name_table)) (
arch, &etable, &elength, &ename))
1704 (arch, &etable, &elength, &ename))((*((arch)->xvec->_bfd_construct_extended_name_table)) (
arch, &etable, &elength, &ename))
)
1705 return FALSE0;
1706
1707 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET0) != 0)
1708 return FALSE0;
1709 wrote = bfd_bwrite (ARMAG"!<arch>\012", SARMAG8, arch);
1710 if (wrote != SARMAG8)
1711 return FALSE0;
1712
1713 if (makemap && hasobjects)
1714 {
1715 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
1716 return FALSE0;
1717 }
1718
1719 if (elength != 0)
1720 {
1721 struct ar_hdr hdr;
1722
1723 memset (&hdr, ' ', sizeof (struct ar_hdr));
1724 memcpy (hdr.ar_name, ename, strlen (ename));
1725 /* Round size up to even number in archive header. */
1726 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
1727 (elength + 1) & ~(bfd_size_type) 1);
1728 memcpy (hdr.ar_fmag, ARFMAG"`\012", 2);
1729 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
1730 != sizeof (struct ar_hdr))
1731 || bfd_bwrite (etable, elength, arch) != elength)
1732 return FALSE0;
1733 if ((elength % 2) == 1)
1734 {
1735 if (bfd_bwrite ("\012", 1, arch) != 1)
1736 return FALSE0;
1737 }
1738 }
1739
1740 for (current = arch->archive_head; current; current = current->next)
1741 {
1742 char buffer[DEFAULT_BUFFERSIZE8192];
1743 unsigned int remaining = arelt_size (current)(((struct areltdata *)((current)->arelt_data))->parsed_size
)
;
1744 struct ar_hdr *hdr = arch_hdr (current)((struct ar_hdr *) ((struct areltdata *) ((current)->arelt_data
))->arch_header)
;
1745
1746 /* Write ar header. */
1747 if (bfd_bwrite (hdr, sizeof (*hdr), arch)
1748 != sizeof (*hdr))
1749 return FALSE0;
1750 if (bfd_seek (current, (file_ptr) 0, SEEK_SET0) != 0)
1751 return FALSE0;
1752 while (remaining)
1753 {
1754 unsigned int amt = DEFAULT_BUFFERSIZE8192;
1755 if (amt > remaining)
1756 amt = remaining;
1757 errno(*__errno()) = 0;
1758 if (bfd_bread (buffer, amt, current) != amt)
1759 {
1760 if (bfd_get_error () != bfd_error_system_call)
1761 bfd_set_error (bfd_error_malformed_archive);
1762 return FALSE0;
1763 }
1764 if (bfd_bwrite (buffer, amt, arch) != amt)
1765 return FALSE0;
1766 remaining -= amt;
1767 }
1768 if ((arelt_size (current)(((struct areltdata *)((current)->arelt_data))->parsed_size
)
% 2) == 1)
1769 {
1770 if (bfd_bwrite ("\012", 1, arch) != 1)
1771 return FALSE0;
1772 }
1773 }
1774
1775 if (makemap && hasobjects)
1776 {
1777 /* Verify the timestamp in the archive file. If it would not be
1778 accepted by the linker, rewrite it until it would be. If
1779 anything odd happens, break out and just return. (The
1780 Berkeley linker checks the timestamp and refuses to read the
1781 table-of-contents if it is >60 seconds less than the file's
1782 modified-time. That painful hack requires this painful hack. */
1783 tries = 1;
1784 do
1785 {
1786 if (bfd_update_armap_timestamp (arch)((*((arch)->xvec->_bfd_update_armap_timestamp)) (arch)))
1787 break;
1788 (*_bfd_error_handler)
1789 (_("Warning: writing archive was slow: rewriting timestamp\n")("Warning: writing archive was slow: rewriting timestamp\n"));
1790 }
1791 while (++tries < 6);
1792 }
1793
1794 return TRUE1;
1795}
1796
1797/* Note that the namidx for the first symbol is 0. */
1798
1799bfd_boolean
1800_bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
1801{
1802 char *first_name = NULL((void*)0);
1803 bfd *current;
1804 file_ptr elt_no = 0;
1805 struct orl *map = NULL((void*)0);
1806 unsigned int orl_max = 1024; /* Fine initial default. */
1807 unsigned int orl_count = 0;
1808 int stridx = 0;
1809 asymbol **syms = NULL((void*)0);
1810 long syms_max = 0;
1811 bfd_boolean ret;
1812 bfd_size_type amt;
1813
1814 /* Dunno if this is the best place for this info... */
1815 if (elength != 0)
1816 elength += sizeof (struct ar_hdr);
1817 elength += elength % 2;
1818
1819 amt = orl_max * sizeof (struct orl);
1820 map = bfd_malloc (amt);
1821 if (map == NULL((void*)0))
1822 goto error_return;
1823
1824 /* We put the symbol names on the arch objalloc, and then discard
1825 them when done. */
1826 first_name = bfd_alloc (arch, 1);
1827 if (first_name == NULL((void*)0))
1828 goto error_return;
1829
1830 /* Drop all the files called __.SYMDEF, we're going to make our own. */
1831 while (arch->archive_head &&
1832 strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
1833 arch->archive_head = arch->archive_head->next;
1834
1835 /* Map over each element. */
1836 for (current = arch->archive_head;
1837 current != NULL((void*)0);
1838 current = current->next, elt_no++)
1839 {
1840 if (bfd_check_format (current, bfd_object)
1841 && (bfd_get_file_flags (current)((current)->flags) & HAS_SYMS0x10) != 0)
1842 {
1843 long storage;
1844 long symcount;
1845 long src_count;
1846
1847 storage = bfd_get_symtab_upper_bound (current)((*((current)->xvec->_bfd_get_symtab_upper_bound)) (current
))
;
1848 if (storage < 0)
1849 goto error_return;
1850
1851 if (storage != 0)
1852 {
1853 if (storage > syms_max)
1854 {
1855 if (syms_max > 0)
1856 free (syms);
1857 syms_max = storage;
1858 syms = bfd_malloc (syms_max);
1859 if (syms == NULL((void*)0))
1860 goto error_return;
1861 }
1862 symcount = bfd_canonicalize_symtab (current, syms)((*((current)->xvec->_bfd_canonicalize_symtab)) (current
, syms))
;
1863 if (symcount < 0)
1864 goto error_return;
1865
1866 /* Now map over all the symbols, picking out the ones we
1867 want. */
1868 for (src_count = 0; src_count < symcount; src_count++)
1869 {
1870 flagword flags = (syms[src_count])->flags;
1871 asection *sec = syms[src_count]->section;
1872
1873 if ((flags & BSF_GLOBAL0x02 ||
1874 flags & BSF_WEAK0x80 ||
1875 flags & BSF_INDIRECT0x2000 ||
1876 bfd_is_com_section (sec)(((sec)->flags & 0x1000) != 0))
1877 && ! bfd_is_und_section (sec)((sec) == ((asection *) &bfd_und_section)))
1878 {
1879 bfd_size_type namelen;
1880 struct orl *new_map;
1881
1882 /* This symbol will go into the archive header. */
1883 if (orl_count == orl_max)
1884 {
1885 orl_max *= 2;
1886 amt = orl_max * sizeof (struct orl);
1887 new_map = bfd_realloc (map, amt);
1888 if (new_map == NULL((void*)0))
1889 goto error_return;
1890
1891 map = new_map;
1892 }
1893
1894 namelen = strlen (syms[src_count]->name);
1895 amt = sizeof (char *);
1896 map[orl_count].name = bfd_alloc (arch, amt);
1897 if (map[orl_count].name == NULL((void*)0))
1898 goto error_return;
1899 *(map[orl_count].name) = bfd_alloc (arch, namelen + 1);
1900 if (*(map[orl_count].name) == NULL((void*)0))
1901 goto error_return;
1902 strcpy (*(map[orl_count].name), syms[src_count]->name);
1903 map[orl_count].u.abfd = current;
1904 map[orl_count].namidx = stridx;
1905
1906 stridx += namelen + 1;
1907 ++orl_count;
1908 }
1909 }
1910 }
1911
1912 /* Now ask the BFD to free up any cached information, so we
1913 don't fill all of memory with symbol tables. */
1914 if (! bfd_free_cached_info (current)((*((current)->xvec->_bfd_free_cached_info)) (current)))
1915 goto error_return;
1916 }
1917 }
1918
1919 /* OK, now we have collected all the data, let's write them out. */
1920 ret = BFD_SEND (arch, write_armap,((*((arch)->xvec->write_armap)) (arch, elength, map, orl_count
, stridx))
1921 (arch, elength, map, orl_count, stridx))((*((arch)->xvec->write_armap)) (arch, elength, map, orl_count
, stridx))
;
1922
1923 if (syms_max > 0)
1924 free (syms);
1925 if (map != NULL((void*)0))
1926 free (map);
1927 if (first_name != NULL((void*)0))
1928 bfd_release (arch, first_name);
1929
1930 return ret;
1931
1932 error_return:
1933 if (syms_max > 0)
1934 free (syms);
1935 if (map != NULL((void*)0))
1936 free (map);
1937 if (first_name != NULL((void*)0))
1938 bfd_release (arch, first_name);
1939
1940 return FALSE0;
1941}
1942
1943bfd_boolean
1944bsd_write_armap (bfd *arch,
1945 unsigned int elength,
1946 struct orl *map,
1947 unsigned int orl_count,
1948 int stridx)
1949{
1950 int padit = stridx & 1;
1951 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE8;
1952 unsigned int stringsize = stridx + padit;
1953 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1954 unsigned int mapsize = ranlibsize + stringsize + 8;
1955 file_ptr firstreal;
1956 bfd *current = arch->archive_head;
1957 bfd *last_elt = current; /* Last element arch seen. */
1958 bfd_byte temp[4];
1959 unsigned int count;
1960 struct ar_hdr hdr;
1961 struct stat statbuf;
1962 long int uid, gid;
1963
1964 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG8;
1965
1966 stat (arch->filename, &statbuf);
1967 memset (&hdr, ' ', sizeof (struct ar_hdr));
1968 memcpy (hdr.ar_name, RANLIBMAG"__.SYMDEF", strlen (RANLIBMAG"__.SYMDEF"));
1969 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1970 bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_timestamp = statbuf.st_mtimest_mtim.tv_sec + ARMAP_TIME_OFFSET60;
1971 bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_datepos = (SARMAG8
1972 + offsetof (struct ar_hdr, ar_date[0])__builtin_offsetof(struct ar_hdr, ar_date[0]));
1973 _bfd_ar_spacepadll (hdr.ar_date, sizeof (hdr.ar_date), "%lld",
1974 bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_timestamp);
1975 if ((arch->flags & BFD_DETERMINISTIC0x4000) != 0)
1976 uid = gid = 0;
1977 else
1978 {
1979 uid = getuid ();
1980 gid = getgid ();
1981 }
1982 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid);
1983 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid);
1984 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
1985 memcpy (hdr.ar_fmag, ARFMAG"`\012", 2);
1986 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
1987 != sizeof (struct ar_hdr))
1988 return FALSE0;
1989 H_PUT_32 (arch, ranlibsize, temp)((*((arch)->xvec->bfd_h_putx32)) (ranlibsize, temp));
1990 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
1991 return FALSE0;
1992
1993 for (count = 0; count < orl_count; count++)
1994 {
1995 bfd_byte buf[BSD_SYMDEF_SIZE8];
1996
1997 if (map[count].u.abfd != last_elt)
1998 {
1999 do
2000 {
2001 firstreal += arelt_size (current)(((struct areltdata *)((current)->arelt_data))->parsed_size
)
+ sizeof (struct ar_hdr);
2002 firstreal += firstreal % 2;
2003 current = current->next;
2004 }
2005 while (current != map[count].u.abfd);
2006 }
2007
2008 last_elt = current;
2009 H_PUT_32 (arch, map[count].namidx, buf)((*((arch)->xvec->bfd_h_putx32)) (map[count].namidx, buf
))
;
2010 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE)((*((arch)->xvec->bfd_h_putx32)) (firstreal, buf + 4));
2011 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE8, arch)
2012 != BSD_SYMDEF_SIZE8)
2013 return FALSE0;
2014 }
2015
2016 /* Now write the strings themselves. */
2017 H_PUT_32 (arch, stringsize, temp)((*((arch)->xvec->bfd_h_putx32)) (stringsize, temp));
2018 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
2019 return FALSE0;
2020 for (count = 0; count < orl_count; count++)
2021 {
2022 size_t len = strlen (*map[count].name) + 1;
2023
2024 if (bfd_bwrite (*map[count].name, len, arch) != len)
2025 return FALSE0;
2026 }
2027
2028 /* The spec sez this should be a newline. But in order to be
2029 bug-compatible for sun's ar we use a null. */
2030 if (padit)
2031 {
2032 if (bfd_bwrite ("", 1, arch) != 1)
2033 return FALSE0;
2034 }
2035
2036 return TRUE1;
2037}
2038
2039/* At the end of archive file handling, update the timestamp in the
2040 file, so the linker will accept it.
2041
2042 Return TRUE if the timestamp was OK, or an unusual problem happened.
2043 Return FALSE if we updated the timestamp. */
2044
2045bfd_boolean
2046_bfd_archive_bsd_update_armap_timestamp (bfd *arch)
2047{
2048 struct stat archstat;
2049 struct ar_hdr hdr;
2050
2051 /* Flush writes, get last-write timestamp from file, and compare it
2052 to the timestamp IN the file. */
2053 bfd_flush (arch);
2054 if (bfd_stat (arch, &archstat) == -1)
2055 {
2056 bfd_perror (_("Reading archive file mod timestamp")("Reading archive file mod timestamp"));
2057
2058 /* Can't read mod time for some reason. */
2059 return TRUE1;
2060 }
2061 if (archstat.st_mtimest_mtim.tv_sec <= bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_timestamp)
2062 /* OK by the linker's rules. */
2063 return TRUE1;
2064
2065 /* Update the timestamp. */
2066 bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_timestamp = archstat.st_mtimest_mtim.tv_sec + ARMAP_TIME_OFFSET60;
2067
2068 /* Prepare an ASCII version suitable for writing. */
2069 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2070 _bfd_ar_spacepadll (hdr.ar_date, sizeof (hdr.ar_date), "%lld",
2071 bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_timestamp);
2072
2073 /* Write it into the file. */
2074 bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_datepos = (SARMAG8
2075 + offsetof (struct ar_hdr, ar_date[0])__builtin_offsetof(struct ar_hdr, ar_date[0]));
2076 if (bfd_seek (arch, bfd_ardata (arch)((arch)->tdata.aout_ar_data)->armap_datepos, SEEK_SET0) != 0
2077 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
2078 != sizeof (hdr.ar_date)))
2079 {
2080 bfd_perror (_("Writing updated armap timestamp")("Writing updated armap timestamp"));
2081
2082 /* Some error while writing. */
2083 return TRUE1;
2084 }
2085
2086 /* We updated the timestamp successfully. */
2087 return FALSE0;
2088}
2089
2090/* A coff armap looks like :
2091 lARMAG
2092 struct ar_hdr with name = '/'
2093 number of symbols
2094 offset of file for symbol 0
2095 offset of file for symbol 1
2096
2097 offset of file for symbol n-1
2098 symbol name 0
2099 symbol name 1
2100
2101 symbol name n-1 */
2102
2103bfd_boolean
2104coff_write_armap (bfd *arch,
2105 unsigned int elength,
2106 struct orl *map,
2107 unsigned int symbol_count,
2108 int stridx)
2109{
2110 /* The size of the ranlib is the number of exported symbols in the
2111 archive * the number of bytes in an int, + an int for the count. */
2112 unsigned int ranlibsize = (symbol_count * 4) + 4;
2113 unsigned int stringsize = stridx;
2114 unsigned int mapsize = stringsize + ranlibsize;
2115 unsigned int archive_member_file_ptr;
2116 bfd *current = arch->archive_head;
2117 unsigned int count;
2118 struct ar_hdr hdr;
2119 int padit = mapsize & 1;
2120
2121 if (padit)
2122 mapsize++;
2123
2124 /* Work out where the first object file will go in the archive. */
2125 archive_member_file_ptr = (mapsize
2126 + elength
2127 + sizeof (struct ar_hdr)
2128 + SARMAG8);
2129
2130 memset (&hdr, ' ', sizeof (struct ar_hdr));
2131 hdr.ar_name[0] = '/';
2132 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
2133 mapsize);
2134 _bfd_ar_spacepadll (hdr.ar_date, sizeof (hdr.ar_date), "%lld",
2135 time (NULL((void*)0)));
2136 /* This, at least, is what Intel coff sets the values to. */
2137 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2138 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2139 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2140 memcpy (hdr.ar_fmag, ARFMAG"`\012", 2);
2141
2142 /* Write the ar header for this item and the number of symbols. */
2143 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2144 != sizeof (struct ar_hdr))
2145 return FALSE0;
2146
2147 if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
2148 return FALSE0;
2149
2150 /* Two passes, first write the file offsets for each symbol -
2151 remembering that each offset is on a two byte boundary. */
2152
2153 /* Write out the file offset for the file associated with each
2154 symbol, and remember to keep the offsets padded out. */
2155
2156 current = arch->archive_head;
2157 count = 0;
2158 while (current != NULL((void*)0) && count < symbol_count)
2159 {
2160 /* For each symbol which is used defined in this object, write
2161 out the object file's address in the archive. */
2162
2163 while (count < symbol_count && map[count].u.abfd == current)
2164 {
2165 if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr))
2166 return FALSE0;
2167 count++;
2168 }
2169 /* Add size of this archive entry. */
2170 archive_member_file_ptr += arelt_size (current)(((struct areltdata *)((current)->arelt_data))->parsed_size
)
+ sizeof (struct ar_hdr);
2171 /* Remember aboout the even alignment. */
2172 archive_member_file_ptr += archive_member_file_ptr % 2;
2173 current = current->next;
2174 }
2175
2176 /* Now write the strings themselves. */
2177 for (count = 0; count < symbol_count; count++)
2178 {
2179 size_t len = strlen (*map[count].name) + 1;
2180
2181 if (bfd_bwrite (*map[count].name, len, arch) != len)
2182 return FALSE0;
2183 }
2184
2185 /* The spec sez this should be a newline. But in order to be
2186 bug-compatible for arc960 we use a null. */
2187 if (padit)
2188 {
2189 if (bfd_bwrite ("", 1, arch) != 1)
2190 return FALSE0;
2191 }
2192
2193 return TRUE1;
2194}