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