* Makefile.in (MAKEOVERRIDES): Define to be empty.
[deliverable/binutils-gdb.git] / bfd / archive.c
CommitLineData
6724ff46 1/* BFD back-end for archive files (libraries).
287c221d 2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
6724ff46 3 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
9872a49c 4
6724ff46 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
6724ff46 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
6724ff46
RP
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
6724ff46 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
6724ff46
RP
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
4a81b561 20
286fd2f9 21/*
6f715d66 22@setfilename archive-info
0cda46cf
SC
23SECTION
24 Archives
6f715d66 25
0cda46cf 26DESCRIPTION
0cda46cf 27 Archives are supported in BFD in <<archive.c>>.
6f715d66 28
4ee249da
DHW
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 considered its contents. These BFDs can be
35 manipulated just 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; it will be handled correctly when the archive is closed.
39
40 Use <<bfd_openr_next_archived_file>> to step through all
41 the contents of an archive opened for input. It's not
42 required that you 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 <<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
287c221d 53 contain files of different formats (e.g., a.out and coff) and
4ee249da
DHW
54 even different architectures. You may even place archives
55 recursively into archives!
56
57 This can cause unexpected confusion, since some archive
287c221d 58 formats are more expressive than others. For instance, Intel
4ee249da
DHW
59 COFF archives can preserve long filenames; Sun 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 due to restrctions 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.
0cda46cf 74*/
6f715d66 75
4a81b561
DHW
76/* Assumes:
77 o - all archive elements start on an even boundary, newline padded;
78 o - all arch headers are char *;
79 o - all arch headers are the same size (across architectures).
80*/
81
4ee249da
DHW
82/* Some formats provide a way to cram a long filename into the short
83 (16 chars) space provided by a bsd archive. The trick is: make a
84 special "file" in the front of the archive, sort of like the SYMDEF
85 entry. If the filename is too long to fit, put it in the extended
86 name table, and use its index as the filename. To prevent
87 confusion prepend the index with a space. This means you can't
88 have filenames that start with a space, but then again, many unix
89 utilities can't handle that anyway.
90
91 This scheme unfortunately requires that you stand on your head in
92 order to write an archive since you need to put a magic file at the
93 front, and need to touch every entry to do so. C'est la vie.
94*/
95
4a81b561 96#include "bfd.h"
f58809fd 97#include "sysdep.h"
4a81b561 98#include "libbfd.h"
c3eb25fc
SC
99#include "aout/ar.h"
100#include "aout/ranlib.h"
446c5af7
PB
101#include <errno.h>
102
103#ifndef errno
104extern int errno;
105#endif
4a81b561 106
9846338e
SC
107#ifdef GNU960
108#define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
109#endif
110
4a81b561
DHW
111/* We keep a cache of archive filepointers to archive elements to
112 speed up searching the archive by filepos. We only add an entry to
113 the cache when we actually read one. We also don't sort the cache;
4ee249da 114 it's generally short enough to search linearly.
4a81b561
DHW
115 Note that the pointers here point to the front of the ar_hdr, not
116 to the front of the contents!
117*/
118struct ar_cache {
119 file_ptr ptr;
120 bfd* arelt;
121 struct ar_cache *next;
122};
123
124#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
125#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
126
127#define arch_hdr(bfd) ((struct ar_hdr *) \
128 (((struct areltdata *)((bfd)->arelt_data))->arch_header))
4a81b561 129\f
4a81b561
DHW
130boolean
131_bfd_generic_mkarchive (abfd)
132 bfd *abfd;
133{
286fd2f9 134 abfd->tdata.aout_ar_data = (struct artdata *)bfd_zalloc(abfd, sizeof (struct artdata));
4a81b561 135
fc723380 136 if (bfd_ardata (abfd) == NULL) {
286fd2f9
PB
137 bfd_error = no_memory;
138 return false;
139 }
a37cc0c0 140 bfd_ardata(abfd)->cache = 0;
4a81b561
DHW
141 return true;
142}
143
0cda46cf
SC
144/*
145FUNCTION
146 bfd_get_next_mapent
147
4ee249da
DHW
148SYNOPSIS
149 symindex bfd_get_next_mapent(bfd *, symindex previous, carsym ** sym);
150
0cda46cf 151DESCRIPTION
4ee249da
DHW
152 This function steps through an archive's symbol table (if it
153 has one). Successively updates <<sym>> with the next symbol's
154 information, returning that symbol's (internal) index into the
155 symbol table.
0cda46cf 156
4ee249da
DHW
157 Supply BFD_NO_MORE_SYMBOLS as the <<previous>> entry to get
158 the first one; returns BFD_NO_MORE_SYMBOLS when you're already
159 got the last one.
160
161 A <<carsym>> is a canonical archive symbol. The only
162 user-visible element is its name, a null-terminated string.
6f715d66 163*/
4ee249da 164
4a81b561 165symindex
286fd2f9 166DEFUN(bfd_get_next_mapent,(abfd, prev, entry),
4ee249da
DHW
167 bfd *abfd AND
168 symindex prev AND
169 carsym **entry)
4a81b561
DHW
170{
171 if (!bfd_has_map (abfd)) {
172 bfd_error = invalid_operation;
173 return BFD_NO_MORE_SYMBOLS;
174 }
175
176 if (prev == BFD_NO_MORE_SYMBOLS) prev = 0;
fc723380 177 else if (++prev >= bfd_ardata (abfd)->symdef_count)
4a81b561
DHW
178 return BFD_NO_MORE_SYMBOLS;
179
180 *entry = (bfd_ardata (abfd)->symdefs + prev);
181 return prev;
182}
183
4a81b561
DHW
184/* To be called by backends only */
185bfd *
186_bfd_create_empty_archive_element_shell (obfd)
187 bfd *obfd;
188{
189 bfd *nbfd;
190
191 nbfd = new_bfd_contained_in(obfd);
192 if (nbfd == NULL) {
193 bfd_error = no_memory;
194 return NULL;
195 }
196 return nbfd;
197}
198
0cda46cf
SC
199/*
200FUNCTION
201 bfd_set_archive_head
f58809fd 202
0cda46cf
SC
203SYNOPSIS
204 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
f58809fd 205
4ee249da
DHW
206DESCRIPTION
207 Used whilst processing archives. Sets the head of the chain of
208 BFDs contained in an archive to @var{new_head}.
6f715d66
SC
209*/
210
4a81b561 211boolean
6f715d66
SC
212DEFUN(bfd_set_archive_head,(output_archive, new_head),
213 bfd *output_archive AND
214 bfd *new_head)
4a81b561 215{
fc723380 216
4a81b561
DHW
217 output_archive->archive_head = new_head;
218 return true;
219}
220
221bfd *
222look_for_bfd_in_cache (arch_bfd, filepos)
223 bfd *arch_bfd;
224 file_ptr filepos;
225{
226 struct ar_cache *current;
227
228 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
229 current = current->next)
230 if (current->ptr == filepos) return current->arelt;
231
232 return NULL;
233}
234
235/* Kind of stupid to call cons for each one, but we don't do too many */
236boolean
237add_bfd_to_cache (arch_bfd, filepos, new_elt)
238 bfd *arch_bfd, *new_elt;
239 file_ptr filepos;
240{
fc723380
JG
241 struct ar_cache *new_cache = (struct ar_cache *)
242 bfd_zalloc(arch_bfd, sizeof (struct ar_cache));
4a81b561
DHW
243
244 if (new_cache == NULL) {
245 bfd_error = no_memory;
246 return false;
247 }
248
249 new_cache->ptr = filepos;
250 new_cache->arelt = new_elt;
251 new_cache->next = (struct ar_cache *)NULL;
252 if (bfd_ardata (arch_bfd)->cache == NULL)
253 bfd_ardata (arch_bfd)->cache = new_cache;
254 else {
255 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
256
257 for (; current->next != NULL; current = current->next);
258 current->next = new_cache;
259 }
260
261 return true;
262}
263
264\f
265
266/* The name begins with space. Hence the rest of the name is an index into
267 the string table. */
4a81b561
DHW
268char *
269get_extended_arelt_filename (arch, name)
270 bfd *arch;
271 char *name;
272{
286fd2f9 273 unsigned long index = 0;
4a81b561 274
286fd2f9 275 /* Should extract string so that I can guarantee not to overflow into
287c221d 276 the next region, but I'm too lazy. */
286fd2f9 277 errno = 0;
287c221d
PB
278 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
279 index = strtol (name+1, NULL, 10);
286fd2f9
PB
280 if (errno != 0) {
281 bfd_error = malformed_archive;
282 return NULL;
4a81b561
DHW
283 }
284
286fd2f9 285 return bfd_ardata (arch)->extended_names + index;
4a81b561
DHW
286}
287
288/* This functions reads an arch header and returns an areltdata pointer, or
289 NULL on error.
290
291 Presumes the file pointer is already in the right place (ie pointing
292 to the ar_hdr in the file). Moves the file pointer; on success it
293 should be pointing to the front of the file contents; on failure it
294 could have been moved arbitrarily.
295*/
296
297struct areltdata *
298snarf_ar_hdr (abfd)
299 bfd *abfd;
300{
7ed4093a
SC
301#ifndef errno
302 extern int errno;
303#endif
304
4a81b561
DHW
305 struct ar_hdr hdr;
306 char *hdrp = (char *) &hdr;
307 unsigned int parsed_size;
308 struct areltdata *ared;
309 char *filename = NULL;
310 unsigned int namelen = 0;
311 unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
312 char *allocptr;
313
9846338e 314 if (bfd_read ((PTR)hdrp, 1, sizeof (struct ar_hdr), abfd)
4a81b561
DHW
315 != sizeof (struct ar_hdr)) {
316 bfd_error = no_more_archived_files;
317 return NULL;
318 }
319 if (strncmp ((hdr.ar_fmag), ARFMAG, 2)) {
320 bfd_error = malformed_archive;
321 return NULL;
322 }
323
324 errno = 0;
325 parsed_size = strtol (hdr.ar_size, NULL, 10);
326 if (errno != 0) {
327 bfd_error = malformed_archive;
328 return NULL;
329 }
330
6f715d66
SC
331 /* extract the filename from the archive - there are two ways to
332 specify an extendend name table, either the first char of the
287c221d
PB
333 name is a space, or it's a slash. */
334 if ((hdr.ar_name[0] == '/'
335 || (hdr.ar_name[0] == ' '
336 && memchr (hdr.ar_name, '/', ar_maxnamelen(abfd)) == NULL))
286fd2f9 337 && bfd_ardata (abfd)->extended_names != NULL) {
4a81b561
DHW
338 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
339 if (filename == NULL) {
340 bfd_error = malformed_archive;
341 return NULL;
342 }
343 }
344 else
345 {
287c221d
PB
346 /* We judge the end of the name by looking for '/' or ' '.
347 Note: The SYSV format (terminated by '/') allows embedded
348 spaces, so only look for ' ' if we don't find '/'. */
4a81b561
DHW
349
350 namelen = 0;
287c221d
PB
351 while (hdr.ar_name[namelen] != '\0' &&
352 hdr.ar_name[namelen] != '/') {
4a81b561 353 namelen++;
287c221d
PB
354 if (namelen == (unsigned)ar_maxnamelen(abfd)) {
355 namelen = 0;
356 while (hdr.ar_name[namelen] != ' '
357 && namelen < (unsigned)ar_maxnamelen(abfd)) {
358 namelen++;
359 }
360 break;
361 }
4a81b561
DHW
362 }
363
364 allocsize += namelen + 1;
365 }
366
a37cc0c0 367 allocptr = bfd_zalloc(abfd, allocsize);
4a81b561
DHW
368 if (allocptr == NULL) {
369 bfd_error = no_memory;
370 return NULL;
371 }
372
373 ared = (struct areltdata *) allocptr;
374
375 ared->arch_header = allocptr + sizeof (struct areltdata);
6724ff46 376 memcpy ((char *) ared->arch_header, (char *) &hdr, sizeof (struct ar_hdr));
4a81b561
DHW
377 ared->parsed_size = parsed_size;
378
379 if (filename != NULL) ared->filename = filename;
380 else {
381 ared->filename = allocptr + (sizeof (struct areltdata) +
382 sizeof (struct ar_hdr));
383 if (namelen)
384 memcpy (ared->filename, hdr.ar_name, namelen);
385 ared->filename[namelen] = '\0';
386 }
387
388 return ared;
389}
390\f
4ee249da
DHW
391/* This is an internal function; it's mainly used when indexing
392 through the archive symbol table, but also used to get the next
393 element, since it handles the bookkeeping so nicely for us.
394*/
395
4a81b561
DHW
396bfd *
397get_elt_at_filepos (archive, filepos)
398 bfd *archive;
399 file_ptr filepos;
400{
401 struct areltdata *new_areldata;
402 bfd *n_nfd;
403
404 n_nfd = look_for_bfd_in_cache (archive, filepos);
405 if (n_nfd) return n_nfd;
406
407 if (0 > bfd_seek (archive, filepos, SEEK_SET)) {
408 bfd_error = system_call_error;
409 return NULL;
410 }
411
412 if ((new_areldata = snarf_ar_hdr (archive)) == NULL) return NULL;
413
414 n_nfd = _bfd_create_empty_archive_element_shell (archive);
415 if (n_nfd == NULL) {
fc723380 416 bfd_release (archive, (PTR)new_areldata);
4a81b561
DHW
417 return NULL;
418 }
419 n_nfd->origin = bfd_tell (archive);
9846338e 420 n_nfd->arelt_data = (PTR) new_areldata;
4a81b561
DHW
421 n_nfd->filename = new_areldata->filename;
422
423 if (add_bfd_to_cache (archive, filepos, n_nfd))
424 return n_nfd;
425
426 /* huh? */
fc723380
JG
427 bfd_release (archive, (PTR)n_nfd);
428 bfd_release (archive, (PTR)new_areldata);
4a81b561
DHW
429 return NULL;
430}
431
0cda46cf
SC
432/*
433FUNCTION
434 bfd_get_elt_at_index
6724ff46 435
0cda46cf 436SYNOPSIS
4ee249da
DHW
437 bfd *bfd_get_elt_at_index(bfd * archive, int index);
438
439DESCRIPTION
440 Return the bfd which is referenced by the symbol indexed by <<index>>.
441 <<index>> should have been returned by <<bfd_get_next_mapent>> (q.v.).
6724ff46
RP
442
443*/
4a81b561 444bfd *
286fd2f9 445DEFUN(bfd_get_elt_at_index,(abfd, index),
4ee249da
DHW
446 bfd *abfd AND
447 int index)
4a81b561
DHW
448{
449 bfd *result =
450 get_elt_at_filepos
451 (abfd, (bfd_ardata (abfd)->symdefs + index)->file_offset);
452 return result;
453}
454
0cda46cf
SC
455/*
456FUNCTION
457 bfd_openr_next_archived_file
458
4ee249da
DHW
459SYNOPSIS
460 bfd* bfd_openr_next_archived_file(bfd *archive, bfd *previous);
461
0cda46cf
SC
462DESCRIPTION
463 Initially provided a BFD containing an archive and NULL, opens
4ee249da 464 an inpout BFD on the first contained element and returns that.
0cda46cf
SC
465 Subsequent calls to bfd_openr_next_archived_file should pass
466 the archive and the previous return value to return a created
467 BFD to the next contained element. NULL is returned when there
468 are no more.
6f715d66 469
6f715d66
SC
470*/
471
4a81b561 472bfd *
6f715d66
SC
473DEFUN(bfd_openr_next_archived_file,(archive, last_file),
474 bfd *archive AND
475 bfd*last_file)
4a81b561
DHW
476{
477
4ee249da
DHW
478 if ((bfd_get_format (archive) != bfd_archive) ||
479 (archive->direction == write_direction)) {
480 bfd_error = invalid_operation;
481 return NULL;
482 }
4a81b561
DHW
483
484
4ee249da
DHW
485 return BFD_SEND (archive,
486 openr_next_archived_file,
487 (archive,
488 last_file));
4a81b561
DHW
489
490}
491
492bfd *bfd_generic_openr_next_archived_file(archive, last_file)
493 bfd *archive;
494 bfd *last_file;
495{
496 file_ptr filestart;
497
498 if (!last_file)
499 filestart = bfd_ardata (archive)->first_file_filepos;
500 else {
fc723380
JG
501 unsigned int size = arelt_size(last_file);
502 /* Pad to an even boundary... */
503 filestart = last_file->origin + size + size%2;
504 }
4a81b561
DHW
505
506 return get_elt_at_filepos (archive, filestart);
507}
4ee249da 508
4a81b561
DHW
509
510bfd_target *
511bfd_generic_archive_p (abfd)
512 bfd *abfd;
513{
514 char armag[SARMAG+1];
515
9846338e 516 if (bfd_read ((PTR)armag, 1, SARMAG, abfd) != SARMAG) {
4a81b561
DHW
517 bfd_error = wrong_format;
518 return 0;
519 }
520
9846338e
SC
521#ifdef GNU960
522 if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG)) return 0;
523#else
286fd2f9
PB
524 if (strncmp (armag, ARMAG, SARMAG) &&
525 strncmp (armag, ARMAGB, SARMAG)) return 0;
9846338e 526#endif
4a81b561 527
286fd2f9
PB
528
529
fc723380
JG
530 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
531 involves a cast, we can't do it as the left operand of assignment. */
286fd2f9 532 abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc(abfd,sizeof (struct artdata));
4a81b561
DHW
533
534 if (bfd_ardata (abfd) == NULL) {
535 bfd_error = no_memory;
536 return 0;
537 }
538
539 bfd_ardata (abfd)->first_file_filepos = SARMAG;
540
287c221d 541 if (!bfd_slurp_armap (abfd)) {
a37cc0c0 542 bfd_release(abfd, bfd_ardata (abfd));
286fd2f9 543 abfd->tdata.aout_ar_data = NULL;
4a81b561
DHW
544 return 0;
545 }
546
4a81b561 547 if (!BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) {
a37cc0c0 548 bfd_release(abfd, bfd_ardata (abfd));
286fd2f9 549 abfd->tdata.aout_ar_data = NULL;
4a81b561
DHW
550 return 0;
551 }
552
553 return abfd->xvec;
554}
555
556/* Returns false on error, true otherwise */
287c221d
PB
557static boolean
558do_slurp_bsd_armap (abfd)
4a81b561
DHW
559 bfd *abfd;
560{
6f715d66 561
287c221d
PB
562 struct areltdata *mapdata;
563 char nextname[17];
564 unsigned int counter = 0;
565 int *raw_armap, *rbase;
566 struct artdata *ardata = bfd_ardata (abfd);
567 char *stringbase;
568 unsigned int parsed_size;
4ee249da 569
287c221d
PB
570 mapdata = snarf_ar_hdr (abfd);
571 if (mapdata == NULL) return false;
572 parsed_size = mapdata->parsed_size;
573 bfd_release (abfd, (PTR)mapdata); /* Don't need it any more. */
574
575 raw_armap = (int *) bfd_zalloc(abfd, parsed_size);
576 if (raw_armap == NULL) {
577 bfd_error = no_memory;
578 return false;
579 }
580
581 if (bfd_read ((PTR)raw_armap, 1, parsed_size, abfd) != parsed_size) {
582 bfd_error = malformed_archive;
583 byebye:
584 bfd_release (abfd, (PTR)raw_armap);
585 return false;
586 }
587
588 ardata->symdef_count =
589 bfd_h_get_32(abfd, (PTR)raw_armap) / sizeof (struct symdef);
590
591 if (ardata->symdef_count * sizeof (struct symdef)
592 > parsed_size - sizeof (*raw_armap)) {
593 /* Probably we're using the wrong byte ordering. */
594 bfd_error = wrong_format;
595 goto byebye;
596 }
4a81b561 597
287c221d
PB
598 ardata->cache = 0;
599 rbase = raw_armap+1;
600 ardata->symdefs = (carsym *) rbase;
601 stringbase = ((char *) (ardata->symdefs + ardata->symdef_count)) + 4;
602
603 for (;counter < ardata->symdef_count; counter++) {
604 struct symdef *sym = ((struct symdef *) rbase) + counter;
605 sym->s.name = bfd_h_get_32(abfd, (PTR)(&(sym->s.string_offset))) + stringbase;
606 sym->file_offset = bfd_h_get_32(abfd, (PTR)( &(sym->file_offset)));
607 }
608
609 ardata->first_file_filepos = bfd_tell (abfd);
610 /* Pad to an even boundary if you have to */
611 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
612 /* FIXME, we should provide some way to free raw_ardata when
613 we are done using the strings from it. For now, it seems
614 to be allocated on an obstack anyway... */
615 bfd_has_map (abfd) = true;
616 return true;
4a81b561
DHW
617}
618
619/* Returns false on error, true otherwise */
287c221d
PB
620static boolean
621do_slurp_coff_armap (abfd)
4a81b561
DHW
622 bfd *abfd;
623{
624 struct areltdata *mapdata;
625 char nextname;
626 int *raw_armap, *rawptr;
627 struct artdata *ardata = bfd_ardata (abfd);
628 char *stringbase;
629 unsigned int stringsize;
287c221d 630 unsigned int parsed_size;
4a81b561 631 carsym *carsyms;
fc723380 632 int result;
287c221d
PB
633 unsigned int nsymz; /* Number of symbols in armap. */
634
286fd2f9 635 bfd_vma (*swap)();
287c221d
PB
636 char int_buf[sizeof(long)];
637 unsigned int carsym_size, ptrsize, i;
286fd2f9 638
4a81b561
DHW
639 mapdata = snarf_ar_hdr (abfd);
640 if (mapdata == NULL) return false;
287c221d
PB
641 parsed_size = mapdata->parsed_size;
642 bfd_release (abfd, (PTR)mapdata); /* Don't need it any more. */
4a81b561 643
287c221d 644 if (bfd_read ((PTR)int_buf, 1, 4, abfd) != 4) {
4a81b561 645 bfd_error = malformed_archive;
287c221d 646 return false;
4a81b561 647 }
287c221d 648 /* It seems that all numeric information in a coff archive is always
f58809fd 649 in big endian format, nomatter the host or target. */
287c221d
PB
650 swap = _do_getb32;
651 nsymz = _do_getb32((PTR)int_buf);
652 stringsize = parsed_size - (4 * nsymz) - 4;
4a81b561 653
287c221d
PB
654#if 1
655 /* ... except that some archive formats are broken, and it may be our
286fd2f9
PB
656 fault - the i960 little endian coff sometimes has big and sometimes
657 little, because our tools changed. Here's a horrible hack to clean
287c221d 658 up the crap. */
286fd2f9 659
287c221d
PB
660 if (stringsize > 0xfffff) {
661 /* This looks dangerous, let's do it the other way around */
662 nsymz = _do_getl32((PTR)int_buf);
663 stringsize = parsed_size - (4 * nsymz) - 4;
664 swap = _do_getl32;
4a81b561 665 }
287c221d
PB
666#endif
667
668 /* The coff armap must be read sequentially. So we construct a bsd-style
669 one in core all at once, for simplicity. */
286fd2f9 670
287c221d
PB
671 carsym_size = (nsymz * sizeof (carsym));
672 ptrsize = (4 * nsymz);
673
674 ardata->symdefs = (carsym *) bfd_zalloc(abfd, carsym_size + stringsize + 1);
675 if (ardata->symdefs == NULL) {
676 bfd_error = no_memory;
677 return false;
678 }
679 carsyms = ardata->symdefs;
680 stringbase = ((char *) ardata->symdefs) + carsym_size;
681
682 /* Allocate and read in the raw offsets. */
683 raw_armap = (int *) bfd_alloc(abfd, ptrsize);
684 if (raw_armap == NULL) {
685 bfd_error = no_memory;
686 goto release_symdefs;
687 }
688 if (bfd_read ((PTR)raw_armap, 1, ptrsize, abfd) != ptrsize
689 || bfd_read ((PTR)stringbase, 1, stringsize, abfd) != stringsize) {
690 bfd_error = malformed_archive;
691 goto release_raw_armap;
692 }
693
694 /* OK, build the carsyms */
695 for (i = 0; i < nsymz; i++) {
696 rawptr = raw_armap + i;
697 carsyms->file_offset = swap((PTR)rawptr);
698 carsyms->name = stringbase;
699 while (*stringbase++) ;
700 carsyms++;
701 }
702 *stringbase = 0;
286fd2f9 703
7b4eaa0e 704 ardata->symdef_count = nsymz;
4a81b561
DHW
705 ardata->first_file_filepos = bfd_tell (abfd);
706 /* Pad to an even boundary if you have to */
707 ardata->first_file_filepos += (ardata->first_file_filepos) %2;
6f715d66 708
4a81b561 709 bfd_has_map (abfd) = true;
287c221d
PB
710 bfd_release (abfd, (PTR)raw_armap);
711 return true;
712
713 release_raw_armap:
714 bfd_release (abfd, (PTR)raw_armap);
715 release_symdefs:
716 bfd_release (abfd, (PTR)(ardata)->symdefs);
717 return false;
718}
719
720/* This routine can handle either coff-style or bsd-style armaps.
721 Returns false on error, true otherwise */
722
723boolean
724bfd_slurp_armap (abfd)
725 bfd *abfd;
726{
727 char nextname[17];
728 int i = bfd_read ((PTR)nextname, 1, 16, abfd);
729
730 if (i == 0)
731 return true;
732 if (i != 16)
733 return false;
734
735 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
736
737 if (!strncmp (nextname, "__.SYMDEF ", 16))
738 return do_slurp_bsd_armap (abfd);
739 else if (!strncmp (nextname, "/ ", 16))
740 return do_slurp_coff_armap (abfd);
741
742 bfd_has_map (abfd) = false;
4a81b561
DHW
743 return true;
744}
4a81b561
DHW
745\f
746/** Extended name table.
747
6f715d66
SC
748 Normally archives support only 14-character filenames.
749
750 Intel has extended the format: longer names are stored in a special
751 element (the first in the archive, or second if there is an armap);
752 the name in the ar_hdr is replaced by <space><index into filename
286fd2f9 753 element>. Index is the P.R. of an int (decimal). Data General have
6f715d66 754 extended the format by using the prefix // for the special element */
4a81b561
DHW
755
756/* Returns false on error, true otherwise */
757boolean
758_bfd_slurp_extended_name_table (abfd)
759 bfd *abfd;
760{
761 char nextname[17];
762 struct areltdata *namedata;
763
fc723380
JG
764 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
765 we probably don't want to return true. */
9846338e 766 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
4a81b561 767
f8e01940 768 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
4a81b561 769
6f715d66
SC
770 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
771 strncmp (nextname, "// ", 16) != 0)
772 {
773 bfd_ardata (abfd)->extended_names = NULL;
774 return true;
775 }
4a81b561 776
6f715d66
SC
777 namedata = snarf_ar_hdr (abfd);
778 if (namedata == NULL) return false;
4a81b561 779
6f715d66
SC
780 bfd_ardata (abfd)->extended_names = bfd_zalloc(abfd,namedata->parsed_size);
781 if (bfd_ardata (abfd)->extended_names == NULL) {
782 bfd_error = no_memory;
783 byebye:
784 bfd_release (abfd, (PTR)namedata);
785 return false;
786 }
4a81b561 787
6f715d66
SC
788 if (bfd_read ((PTR)bfd_ardata (abfd)->extended_names, 1,
789 namedata->parsed_size, abfd) != namedata->parsed_size) {
790 bfd_error = malformed_archive;
791 bfd_release (abfd, (PTR)(bfd_ardata (abfd)->extended_names));
792 bfd_ardata (abfd)->extended_names = NULL;
793 goto byebye;
794 }
4a81b561 795
6f715d66
SC
796 /* Since the archive is supposed to be printable if it contains
797 text, the entries in the list are newline-padded, not null
287c221d
PB
798 padded. In SVR4-style archives, the names also have a
799 trailing '/'. We'll fix both problems here.. */
6f715d66
SC
800 {
801 char *temp = bfd_ardata (abfd)->extended_names;
287c221d
PB
802 char *limit = temp + namedata->parsed_size;
803 for (; temp < limit; ++temp)
804 if (*temp == '\n')
805 temp[temp[-1] == '/' ? -1 : 0] = '\0';
6f715d66 806 }
9846338e 807
6f715d66
SC
808 /* Pad to an even boundary if you have to */
809 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
810 bfd_ardata (abfd)->first_file_filepos +=
811 (bfd_ardata (abfd)->first_file_filepos) %2;
812
813 /* FIXME, we can't release namedata here because it was allocated
814 below extended_names on the obstack... */
815 /* bfd_release (abfd, namedata); */
816 }
4a81b561
DHW
817 return true;
818}
819
286fd2f9
PB
820#ifdef VMS
821
822/* Return a copy of the stuff in the filename between any :]> and a
823 semicolon */
824static CONST char *
825DEFUN(normalize,(file),
826 CONST char *file)
827{
828 CONST char *first;
829 CONST char *last;
830 char *copy;
831
832 first = file + strlen(file)-1;
833 last = first+1;
834
835 while (first != file)
836 {
837 if (*first == ';')
838 last = first;
839 if (*first == ':' || *first == ']' ||*first == '>')
840 {
841 first++;
842 break;
843 }
844 first --;
845 }
846
847
848 copy = malloc(last - first + 1);
849 memcpy(copy, first, last-first);
850 copy[last-first] = 0;
851
852 return copy;
853}
854
855#else
4a81b561 856static
286fd2f9
PB
857CONST char *normalize(file)
858CONST char *file;
4a81b561 859{
286fd2f9
PB
860 CONST char * filename = strrchr(file, '/');
861
862 if (filename != (char *)NULL) {
863 filename ++;
4a81b561 864 }
286fd2f9
PB
865 else {
866 filename = file;
4a81b561 867 }
286fd2f9 868 return filename;
4a81b561 869}
286fd2f9 870#endif
4a81b561
DHW
871/* Follows archive_head and produces an extended name table if necessary.
872 Returns (in tabloc) a pointer to an extended name table, and in tablen
873 the length of the table. If it makes an entry it clobbers the filename
874 so that the element may be written without further massage.
875 Returns true if it ran successfully, false if something went wrong.
876 A successful return may still involve a zero-length tablen!
877 */
878boolean
879bfd_construct_extended_name_table (abfd, tabloc, tablen)
880 bfd *abfd;
881 char **tabloc;
882 unsigned int *tablen;
883{
9846338e
SC
884 unsigned int maxname = abfd->xvec->ar_max_namelen;
885 unsigned int total_namelen = 0;
886 bfd *current;
887 char *strptr;
4a81b561 888
9846338e 889 *tablen = 0;
4a81b561 890
9846338e
SC
891 /* Figure out how long the table should be */
892 for (current = abfd->archive_head; current != NULL; current = current->next){
893 unsigned int thislen = strlen (normalize(current->filename));
894 if (thislen > maxname) total_namelen += thislen + 1; /* leave room for \n */
895 }
4a81b561 896
9846338e 897 if (total_namelen == 0) return true;
4a81b561 898
a37cc0c0 899 *tabloc = bfd_zalloc (abfd,total_namelen);
9846338e
SC
900 if (*tabloc == NULL) {
901 bfd_error = no_memory;
902 return false;
903 }
4a81b561 904
9846338e
SC
905 *tablen = total_namelen;
906 strptr = *tabloc;
907
908 for (current = abfd->archive_head; current != NULL; current =
909 current->next) {
286fd2f9 910 CONST char *normal =normalize( current->filename);
9846338e
SC
911 unsigned int thislen = strlen (normal);
912 if (thislen > maxname) {
913 /* Works for now; may need to be re-engineered if we encounter an oddball
914 archive format and want to generalise this hack. */
915 struct ar_hdr *hdr = arch_hdr(current);
916 strcpy (strptr, normal);
917 strptr[thislen] = '\n';
918 hdr->ar_name[0] = ' ';
919 /* We know there will always be enough room (one of the few cases
920 where you may safely use sprintf). */
286fd2f9 921 sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
9846338e
SC
922 /* Kinda Kludgy. We should just use the returned value of sprintf
923 but not all implementations get this right */
924 {
925 char *temp = hdr->ar_name +2;
926 for (; temp < hdr->ar_name + maxname; temp++)
927 if (*temp == '\0') *temp = ' ';
4a81b561 928 }
9846338e 929 strptr += thislen + 1;
4a81b561 930 }
9846338e 931 }
4a81b561 932
9846338e 933 return true;
4a81b561
DHW
934}
935\f
936/** A couple of functions for creating ar_hdrs */
937
938/* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
939 The filename must refer to a filename in the filesystem.
940 The filename field of the ar_hdr will NOT be initialized
941*/
942
943struct areltdata *
a37cc0c0
SC
944DEFUN(bfd_ar_hdr_from_filesystem, (abfd,filename),
945 bfd* abfd AND
946 CONST char *filename)
4a81b561
DHW
947{
948 struct stat status;
949 struct areltdata *ared;
950 struct ar_hdr *hdr;
951 char *temp, *temp1;
952
953
954 if (stat (filename, &status) != 0) {
955 bfd_error = system_call_error;
956 return NULL;
957 }
958
a37cc0c0 959 ared = (struct areltdata *) bfd_zalloc(abfd, sizeof (struct ar_hdr) +
4a81b561
DHW
960 sizeof (struct areltdata));
961 if (ared == NULL) {
962 bfd_error = no_memory;
963 return NULL;
964 }
965 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
966
967 /* ar headers are space padded, not null padded! */
968 temp = (char *) hdr;
969 temp1 = temp + sizeof (struct ar_hdr) - 2;
970 for (; temp < temp1; *(temp++) = ' ');
971 strncpy (hdr->ar_fmag, ARFMAG, 2);
972
973 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
974 sprintf ((hdr->ar_date), "%-12ld", status.st_mtime);
975 sprintf ((hdr->ar_uid), "%d", status.st_uid);
976 sprintf ((hdr->ar_gid), "%d", status.st_gid);
977 sprintf ((hdr->ar_mode), "%-8o", (unsigned) status.st_mode);
978 sprintf ((hdr->ar_size), "%-10ld", status.st_size);
979 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
980 understand how these C losers could design such a ramshackle bunch of
981 IO operations */
982 temp = (char *) hdr;
983 temp1 = temp + sizeof (struct ar_hdr) - 2;
984 for (; temp < temp1; temp++) {
985 if (*temp == '\0') *temp = ' ';
986 }
987 strncpy (hdr->ar_fmag, ARFMAG, 2);
988 ared->parsed_size = status.st_size;
989 ared->arch_header = (char *) hdr;
990
991 return ared;
992}
993
4ee249da
DHW
994/* This is magic required by the "ar" program. Since it's
995 undocumented, it's undocumented. You may think that it would
996 take a strong stomach to write this, and it does, but it takes
997 even a stronger stomach to try to code around such a thing!
998*/
999
4a81b561 1000struct ar_hdr *
a37cc0c0
SC
1001DEFUN(bfd_special_undocumented_glue, (abfd, filename),
1002 bfd *abfd AND
1003 char *filename)
4a81b561 1004{
37217060
PB
1005 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename);
1006 if (ar_elt == NULL)
1007 return NULL;
1008 return (struct ar_hdr *) ar_elt->arch_header;
4a81b561
DHW
1009}
1010
1011
1012/* Analogous to stat call */
1013int
1014bfd_generic_stat_arch_elt (abfd, buf)
1015 bfd *abfd;
1016 struct stat *buf;
1017{
1018 struct ar_hdr *hdr;
1019 char *aloser;
1020
1021 if (abfd->arelt_data == NULL) {
1022 bfd_error = invalid_operation;
1023 return -1;
1024 }
1025
1026 hdr = arch_hdr (abfd);
1027
1028#define foo(arelt, stelt, size) \
1029 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1030 if (aloser == hdr->arelt) return -1;
1031
1032 foo (ar_date, st_mtime, 10);
1033 foo (ar_uid, st_uid, 10);
1034 foo (ar_gid, st_gid, 10);
1035 foo (ar_mode, st_mode, 8);
1036 foo (ar_size, st_size, 10);
1037
1038 return 0;
1039}
1040
4a81b561 1041void
9846338e
SC
1042bfd_dont_truncate_arname (abfd, pathname, arhdr)
1043 bfd *abfd;
8b0328db 1044 CONST char *pathname;
9846338e 1045 char *arhdr;
4a81b561 1046{
fc723380 1047 /* FIXME: This interacts unpleasantly with ar's quick-append option.
9846338e
SC
1048 Fortunately ic960 users will never use that option. Fixing this
1049 is very hard; fortunately I know how to do it and will do so once
1050 intel's release is out the door. */
1051
1052 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1053 int length;
286fd2f9 1054 CONST char *filename = normalize(pathname);
9846338e 1055 int maxlen = ar_maxnamelen (abfd);
4a81b561 1056
9846338e 1057 length = strlen (filename);
4a81b561 1058
9846338e
SC
1059 if (length <= maxlen)
1060 memcpy (hdr->ar_name, filename, length);
1061
1062 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
4a81b561 1063 return;
9846338e 1064
4a81b561
DHW
1065}
1066
1067void
1068bfd_bsd_truncate_arname (abfd, pathname, arhdr)
1069 bfd *abfd;
8b0328db 1070 CONST char *pathname;
4a81b561
DHW
1071 char *arhdr;
1072{
1073 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1074 int length;
8b0328db 1075 CONST char *filename = strrchr (pathname, '/');
4a81b561
DHW
1076 int maxlen = ar_maxnamelen (abfd);
1077
1078
1079 if (filename == NULL)
1080 filename = pathname;
1081 else
1082 ++filename;
1083
1084 length = strlen (filename);
1085
1086 if (length <= maxlen)
1087 memcpy (hdr->ar_name, filename, length);
1088 else {
1089 /* pathname: meet procrustes */
1090 memcpy (hdr->ar_name, filename, maxlen);
1091 length = maxlen;
1092 }
1093
9846338e 1094 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
4a81b561
DHW
1095}
1096
1097/* Store name into ar header. Truncates the name to fit.
1098 1> strip pathname to be just the basename.
1099 2> if it's short enuf to fit, stuff it in.
1100 3> If it doesn't end with .o, truncate it to fit
1101 4> truncate it before the .o, append .o, stuff THAT in.
1102*/
1103
1104/* This is what gnu ar does. It's better but incompatible with the bsd ar. */
1105void
1106bfd_gnu_truncate_arname (abfd, pathname, arhdr)
1107 bfd *abfd;
8b0328db 1108 CONST char *pathname;
4a81b561
DHW
1109 char *arhdr;
1110{
1111 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1112 int length;
8b0328db 1113 CONST char *filename = strrchr (pathname, '/');
4a81b561
DHW
1114 int maxlen = ar_maxnamelen (abfd);
1115
1116 if (filename == NULL)
1117 filename = pathname;
1118 else
1119 ++filename;
1120
1121 length = strlen (filename);
1122
1123 if (length <= maxlen)
1124 memcpy (hdr->ar_name, filename, length);
1125 else { /* pathname: meet procrustes */
1126 memcpy (hdr->ar_name, filename, maxlen);
1127 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) {
1128 hdr->ar_name[maxlen - 2] = '.';
1129 hdr->ar_name[maxlen - 1] = 'o';
1130 }
1131 length = maxlen;
1132 }
1133
1134 if (length < 16) (hdr->ar_name)[length] = ar_padchar (abfd);
1135}
1136\f
1137
1138PROTO (boolean, compute_and_write_armap, (bfd *arch, unsigned int elength));
1139
6724ff46 1140/* The BFD is open for write and has its format set to bfd_archive */
4a81b561
DHW
1141boolean
1142_bfd_write_archive_contents (arch)
1143 bfd *arch;
1144{
1145 bfd *current;
1146 char *etable = NULL;
1147 unsigned int elength = 0;
287c221d 1148
91c9d029 1149 boolean makemap = bfd_has_map (arch);
4a81b561
DHW
1150 boolean hasobjects = false; /* if no .o's, don't bother to make a map */
1151 unsigned int i;
1152
4a81b561
DHW
1153 /* Verify the viability of all entries; if any of them live in the
1154 filesystem (as opposed to living in an archive open for input)
1155 then construct a fresh ar_hdr for them.
1156 */
1157 for (current = arch->archive_head; current; current = current->next) {
1158 if (bfd_write_p (current)) {
1159 bfd_error = invalid_operation;
1160 return false;
1161 }
1162 if (!current->arelt_data) {
1163 current->arelt_data =
a37cc0c0 1164 (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename);
4a81b561
DHW
1165 if (!current->arelt_data) return false;
1166
1167 /* Put in the file name */
1168
1169 BFD_SEND (arch, _bfd_truncate_arname,(arch,
1170 current->filename,
0452b5aa 1171 (char *) arch_hdr(current)));
4a81b561
DHW
1172
1173
1174 }
1175
1176 if (makemap) { /* don't bother if we won't make a map! */
1177 if ((bfd_check_format (current, bfd_object))
1178#if 0 /* FIXME -- these are not set correctly */
1179 && ((bfd_get_file_flags (current) & HAS_SYMS))
1180#endif
1181 )
1182 hasobjects = true;
1183 }
1184 }
1185
1186 if (!bfd_construct_extended_name_table (arch, &etable, &elength))
1187 return false;
1188
f8e01940 1189 bfd_seek (arch, (file_ptr) 0, SEEK_SET);
9846338e
SC
1190#ifdef GNU960
1191 bfd_write (BFD_GNU960_ARMAG(arch), 1, SARMAG, arch);
1192#else
4a81b561 1193 bfd_write (ARMAG, 1, SARMAG, arch);
9846338e 1194#endif
4a81b561
DHW
1195
1196 if (makemap && hasobjects) {
1197
1198 if (compute_and_write_armap (arch, elength) != true) {
4a81b561
DHW
1199 return false;
1200 }
1201 }
1202
1203 if (elength != 0) {
1204 struct ar_hdr hdr;
1205
1206 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1207 sprintf (&(hdr.ar_name[0]), "ARFILENAMES/");
1208 sprintf (&(hdr.ar_size[0]), "%-10d", (int) elength);
1209 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1210 for (i = 0; i < sizeof (struct ar_hdr); i++)
1211 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
b1847ba9 1212 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
4a81b561
DHW
1213 bfd_write (etable, 1, elength, arch);
1214 if ((elength % 2) == 1) bfd_write ("\n", 1, 1, arch);
a37cc0c0 1215
4a81b561
DHW
1216 }
1217
1218 for (current = arch->archive_head; current; current = current->next) {
1219 char buffer[DEFAULT_BUFFERSIZE];
1220 unsigned int remaining = arelt_size (current);
1221 struct ar_hdr *hdr = arch_hdr(current);
1222 /* write ar header */
1223
b1847ba9 1224 if (bfd_write ((char *)hdr, 1, sizeof(*hdr), arch) != sizeof(*hdr)) {
4a81b561
DHW
1225 syserr:
1226 bfd_error = system_call_error;
1227 return false;
1228 }
f8e01940 1229 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) goto syserr;
0452b5aa
SC
1230 while (remaining)
1231 {
1232 unsigned int amt = DEFAULT_BUFFERSIZE;
1233 if (amt > remaining) {
1234 amt = remaining;
1235 }
446c5af7 1236 errno = 0;
286fd2f9 1237 if (bfd_read (buffer, amt, 1, current) != amt) {
446c5af7 1238 if (errno) goto syserr;
286fd2f9
PB
1239 /* Looks like a truncated archive. */
1240 bfd_error = malformed_archive;
1241 return false;
1242 }
0452b5aa
SC
1243 if (bfd_write (buffer, amt, 1, arch) != amt) goto syserr;
1244 remaining -= amt;
1245 }
4a81b561
DHW
1246 if ((arelt_size (current) % 2) == 1) bfd_write ("\n", 1, 1, arch);
1247 }
1248return true;
1249}
1250\f
1251/* Note that the namidx for the first symbol is 0 */
1252
4a81b561
DHW
1253boolean
1254compute_and_write_armap (arch, elength)
1255 bfd *arch;
1256 unsigned int elength;
1257{
37217060
PB
1258 bfd *current;
1259 file_ptr elt_no = 0;
1260 struct orl *map;
1261 int orl_max = 15000; /* fine initial default */
1262 int orl_count = 0;
1263 int stridx = 0; /* string index */
1264
1265 /* Dunno if this is the best place for this info... */
1266 if (elength != 0) elength += sizeof (struct ar_hdr);
1267 elength += elength %2 ;
1268
1269 map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
1270 if (map == NULL) {
1271 bfd_error = no_memory;
1272 return false;
1273 }
4a81b561 1274
37217060
PB
1275 /* Drop all the files called __.SYMDEF, we're going to make our
1276 own */
1277 while (arch->archive_head &&
1278 strcmp(arch->archive_head->filename,"__.SYMDEF") == 0)
1279 {
1280 arch->archive_head = arch->archive_head->next;
1281 }
1282 /* Map over each element */
1283 for (current = arch->archive_head;
1284 current != (bfd *)NULL;
1285 current = current->next, elt_no++)
1286 {
0452b5aa
SC
1287 if ((bfd_check_format (current, bfd_object) == true)
1288 && ((bfd_get_file_flags (current) & HAS_SYMS))) {
37217060
PB
1289 asymbol **syms;
1290 unsigned int storage;
1291 unsigned int symcount;
1292 unsigned int src_count;
1293
1294 storage = get_symtab_upper_bound (current);
1295 if (storage != 0) {
1296
1297 syms = (asymbol **) bfd_zalloc (arch,storage);
1298 if (syms == NULL) {
1299 bfd_error = no_memory; /* FIXME -- memory leak */
1300 return false;
1301 }
1302 symcount = bfd_canonicalize_symtab (current, syms);
1303
1304
1305 /* Now map over all the symbols, picking out the ones we want */
1306 for (src_count = 0; src_count <symcount; src_count++) {
286fd2f9
PB
1307 flagword flags =
1308 (syms[src_count])->flags;
1309 asection *sec =
1310 syms[src_count]->section;
1311
287c221d
PB
1312 if ((flags & BSF_GLOBAL ||
1313 flags & BSF_INDIRECT ||
1314 sec == &bfd_com_section)
1315 && (sec != &bfd_und_section)) {
37217060
PB
1316
1317 /* This symbol will go into the archive header */
1318 if (orl_count == orl_max)
1319 {
1320 orl_max *= 2;
1321 map = (struct orl *) bfd_realloc (arch, (char *) map,
1322 orl_max * sizeof (struct orl));
1323 }
1324
1325 (map[orl_count]).name = (char **) &((syms[src_count])->name);
1326 (map[orl_count]).pos = (file_ptr) current;
1327 (map[orl_count]).namidx = stridx;
1328
1329 stridx += strlen ((syms[src_count])->name) + 1;
1330 ++orl_count;
1331 }
1332 }
0452b5aa 1333 }
0452b5aa 1334 }
37217060
PB
1335 }
1336 /* OK, now we have collected all the data, let's write them out */
1337 if (!BFD_SEND (arch, write_armap,
1338 (arch, elength, map, orl_count, stridx))) {
a37cc0c0 1339
37217060
PB
1340 return false;
1341 }
4a81b561 1342
a37cc0c0 1343
37217060 1344 return true;
4a81b561
DHW
1345}
1346
4a81b561
DHW
1347boolean
1348bsd_write_armap (arch, elength, map, orl_count, stridx)
1349 bfd *arch;
1350 unsigned int elength;
1351 struct orl *map;
37217060 1352 unsigned int orl_count;
4a81b561
DHW
1353 int stridx;
1354{
4ee249da 1355 int padit = stridx & 1;
f58809fd 1356 unsigned int ranlibsize = orl_count * sizeof (struct ranlib);
4ee249da
DHW
1357 unsigned int stringsize = stridx + padit;
1358 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1359 unsigned int mapsize = ranlibsize + stringsize + 8;
4a81b561
DHW
1360 file_ptr firstreal;
1361 bfd *current = arch->archive_head;
286fd2f9 1362 bfd *last_elt = current; /* last element arch seen */
4a81b561
DHW
1363 int temp;
1364 int count;
1365 struct ar_hdr hdr;
1366 struct stat statbuf;
1367 unsigned int i;
4a81b561
DHW
1368
1369 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1370
2a525d0c 1371 stat (arch->filename, &statbuf);
4a81b561 1372 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
d6a554ae 1373 sprintf (hdr.ar_name, RANLIBMAG);
286fd2f9
PB
1374
1375 /* write the timestamp of the archive header to be just a little bit
1376 later than the timestamp of the file, otherwise the linker will
1377 complain that the index is out of date.
1378 */
1379
1380 sprintf (hdr.ar_date, "%ld", statbuf.st_mtime + 60);
45021fee
RP
1381 sprintf (hdr.ar_uid, "%d", getuid());
1382 sprintf (hdr.ar_gid, "%d", getgid());
1383 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
4a81b561
DHW
1384 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1385 for (i = 0; i < sizeof (struct ar_hdr); i++)
286fd2f9 1386 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
b1847ba9 1387 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
6f715d66 1388 bfd_h_put_32(arch, ranlibsize, (PTR)&temp);
4a81b561
DHW
1389 bfd_write (&temp, 1, sizeof (temp), arch);
1390
1391 for (count = 0; count < orl_count; count++) {
1392 struct symdef outs;
1393 struct symdef *outp = &outs;
1394
45021fee 1395 if (((bfd *)(map[count]).pos) != last_elt) {
286fd2f9
PB
1396 do {
1397 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1398 firstreal += firstreal % 2;
1399 current = current->next;
1400 } while (current != (bfd *)(map[count]).pos);
1401 } /* if new archive element */
45021fee
RP
1402
1403 last_elt = current;
6f715d66
SC
1404 bfd_h_put_32(arch, ((map[count]).namidx),(PTR) &outs.s.string_offset);
1405 bfd_h_put_32(arch, firstreal,(PTR) &outs.file_offset);
4a81b561
DHW
1406 bfd_write ((char *)outp, 1, sizeof (outs), arch);
1407 }
1408
1409 /* now write the strings themselves */
4ee249da 1410 bfd_h_put_32(arch, stringsize, (PTR)&temp);
d0ec7a8e 1411 bfd_write ((PTR)&temp, 1, sizeof (temp), arch);
4a81b561 1412 for (count = 0; count < orl_count; count++)
286fd2f9 1413 bfd_write (*((map[count]).name), 1, strlen (*((map[count]).name))+1, arch);
4a81b561
DHW
1414
1415 /* The spec sez this should be a newline. But in order to be
1416 bug-compatible for sun's ar we use a null. */
1417 if (padit)
286fd2f9 1418 bfd_write("\0",1,1,arch);
4a81b561
DHW
1419
1420 return true;
1421}
1422\f
1423
1424/* A coff armap looks like :
0cda46cf 1425 lARMAG
4a81b561
DHW
1426 struct ar_hdr with name = '/'
1427 number of symbols
1428 offset of file for symbol 0
1429 offset of file for symbol 1
4ee249da 1430
4a81b561
DHW
1431 offset of file for symbol n-1
1432 symbol name 0
1433 symbol name 1
4ee249da 1434
4a81b561
DHW
1435 symbol name n-1
1436
1437*/
4ee249da 1438
4a81b561 1439boolean
f58809fd 1440coff_write_armap (arch, elength, map, symbol_count, stridx)
4a81b561
DHW
1441 bfd *arch;
1442 unsigned int elength;
1443 struct orl *map;
f58809fd 1444 unsigned int symbol_count;
4a81b561
DHW
1445 int stridx;
1446{
f58809fd
SC
1447 /* The size of the ranlib is the number of exported symbols in the
1448 archive * the number of bytes in a int, + an int for the count */
1449
1450 unsigned int ranlibsize = (symbol_count * 4) + 4;
4a81b561
DHW
1451 unsigned int stringsize = stridx;
1452 unsigned int mapsize = stringsize + ranlibsize;
1453 file_ptr archive_member_file_ptr;
1454 bfd *current = arch->archive_head;
4a81b561
DHW
1455 int count;
1456 struct ar_hdr hdr;
4a81b561
DHW
1457 unsigned int i;
1458 int padit = mapsize & 1;
1459
1460 if (padit) mapsize ++;
1461
f58809fd
SC
1462 /* work out where the first object file will go in the archive */
1463 archive_member_file_ptr = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
4a81b561 1464
4a81b561
DHW
1465 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1466 hdr.ar_name[0] = '/';
1467 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
9846338e 1468 sprintf (hdr.ar_date, "%ld", (long)time (NULL));
37a1fd96
DHW
1469 /* This, at least, is what Intel coff sets the values to.: */
1470 sprintf ((hdr.ar_uid), "%d", 0);
1471 sprintf ((hdr.ar_gid), "%d", 0);
9846338e 1472 sprintf ((hdr.ar_mode), "%-7o",(unsigned ) 0);
4a81b561
DHW
1473 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1474
1475 for (i = 0; i < sizeof (struct ar_hdr); i++)
f58809fd 1476 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
4a81b561
DHW
1477
1478 /* Write the ar header for this item and the number of symbols */
1479
f58809fd 1480
d0ec7a8e 1481 bfd_write ((PTR)&hdr, 1, sizeof (struct ar_hdr), arch);
f58809fd
SC
1482
1483 bfd_write_bigendian_4byte_int(arch, symbol_count);
4a81b561
DHW
1484
1485 /* Two passes, first write the file offsets for each symbol -
286fd2f9 1486 remembering that each offset is on a two byte boundary. */
4a81b561 1487
f58809fd 1488 /* Write out the file offset for the file associated with each
286fd2f9 1489 symbol, and remember to keep the offsets padded out. */
f58809fd
SC
1490
1491 current = arch->archive_head;
1492 count = 0;
1493 while (current != (bfd *)NULL && count < symbol_count) {
1494 /* For each symbol which is used defined in this object, write out
1495 the object file's address in the archive */
1496
1497 while (((bfd *)(map[count]).pos) == current) {
1498 bfd_write_bigendian_4byte_int(arch, archive_member_file_ptr);
1499 count++;
4a81b561 1500 }
f58809fd
SC
1501 /* Add size of this archive entry */
1502 archive_member_file_ptr += arelt_size (current) + sizeof (struct
1503 ar_hdr);
1504 /* remember aboout the even alignment */
1505 archive_member_file_ptr += archive_member_file_ptr % 2;
1506 current = current->next;
1507 }
1508
1509
4a81b561
DHW
1510
1511 /* now write the strings themselves */
f58809fd 1512 for (count = 0; count < symbol_count; count++) {
d0ec7a8e 1513 bfd_write ((PTR)*((map[count]).name),
4a81b561
DHW
1514 1,
1515 strlen (*((map[count]).name))+1, arch);
1516
1517 }
1518 /* The spec sez this should be a newline. But in order to be
1519 bug-compatible for arc960 we use a null. */
1520 if (padit)
f58809fd 1521 bfd_write("\0",1,1,arch);
4a81b561
DHW
1522
1523 return true;
1524}
This page took 0.136453 seconds and 4 git commands to generate.