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