Mon Aug 19 13:48:22 1991 Roland H. Pesch (pesch at cygint.cygnus.com)
[deliverable/binutils-gdb.git] / bfd / archive.c
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.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /*doc*
22 @setfilename archive-info
23 @section Archives
24
25 Gumby, you promised to write this bit...
26
27 Archives are supported in BFD in @code{archive.c}.
28
29 An archive is represented internally just like another BFD, with a
30 pointer to a chain of contained BFDs. Archives can be created by
31 opening BFDs, linking them together and attatching them as children to
32 another BFD and then closing the parent BFD.
33
34 *-*/
35
36 /* Assumes:
37 o - all archive elements start on an even boundary, newline padded;
38 o - all arch headers are char *;
39 o - all arch headers are the same size (across architectures).
40 */
41
42 /* $Id$ */
43
44 #include <sysdep.h>
45 #include "bfd.h"
46 #include "libbfd.h"
47 #include "ar.h"
48 #include "ranlib.h"
49
50 #ifdef GNU960
51 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
52 #endif
53
54 /* We keep a cache of archive filepointers to archive elements to
55 speed up searching the archive by filepos. We only add an entry to
56 the cache when we actually read one. We also don't sort the cache;
57 it's short enough to search linearly.
58 Note that the pointers here point to the front of the ar_hdr, not
59 to the front of the contents!
60 */
61 struct ar_cache {
62 file_ptr ptr;
63 bfd* arelt;
64 struct ar_cache *next;
65 };
66
67 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
68 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
69
70 #define arch_hdr(bfd) ((struct ar_hdr *) \
71 (((struct areltdata *)((bfd)->arelt_data))->arch_header))
72 \f
73 boolean
74 _bfd_generic_mkarchive (abfd)
75 bfd *abfd;
76 {
77 set_tdata (abfd, bfd_zalloc(abfd, sizeof (struct artdata)));
78
79 if (bfd_ardata (abfd) == NULL) {
80 bfd_error = no_memory;
81 return false;
82 }
83 bfd_ardata(abfd)->cache = 0;
84 return true;
85 }
86
87 /*proto* bfd_get_next_mapent
88 What this does
89 *; PROTO(symindex, bfd_get_next_mapent, (bfd *, symindex, carsym **));
90 */
91 symindex
92 bfd_get_next_mapent (abfd, prev, entry)
93 bfd *abfd;
94 symindex prev;
95 carsym **entry;
96 {
97 if (!bfd_has_map (abfd)) {
98 bfd_error = invalid_operation;
99 return BFD_NO_MORE_SYMBOLS;
100 }
101
102 if (prev == BFD_NO_MORE_SYMBOLS) prev = 0;
103 else if (++prev >= bfd_ardata (abfd)->symdef_count)
104 return BFD_NO_MORE_SYMBOLS;
105
106 *entry = (bfd_ardata (abfd)->symdefs + prev);
107 return prev;
108 }
109
110
111 /* To be called by backends only */
112 bfd *
113 _bfd_create_empty_archive_element_shell (obfd)
114 bfd *obfd;
115 {
116 bfd *nbfd;
117
118 nbfd = new_bfd_contained_in(obfd);
119 if (nbfd == NULL) {
120 bfd_error = no_memory;
121 return NULL;
122 }
123 return nbfd;
124 }
125
126 /*proto* bfd_set_archive_head
127 Used whilst processing archives. Sets the head of the chain of BFDs
128 contained in an archive to @var{new_head}. (see chapter on archives)
129 *; PROTO(boolean, bfd_set_archive_head, (bfd *output, bfd *new_head));
130 */
131
132 boolean
133 DEFUN(bfd_set_archive_head,(output_archive, new_head),
134 bfd *output_archive AND
135 bfd *new_head)
136 {
137
138 output_archive->archive_head = new_head;
139 return true;
140 }
141
142 bfd *
143 look_for_bfd_in_cache (arch_bfd, filepos)
144 bfd *arch_bfd;
145 file_ptr filepos;
146 {
147 struct ar_cache *current;
148
149 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
150 current = current->next)
151 if (current->ptr == filepos) return current->arelt;
152
153 return NULL;
154 }
155
156 /* Kind of stupid to call cons for each one, but we don't do too many */
157 boolean
158 add_bfd_to_cache (arch_bfd, filepos, new_elt)
159 bfd *arch_bfd, *new_elt;
160 file_ptr filepos;
161 {
162 struct ar_cache *new_cache = (struct ar_cache *)
163 bfd_zalloc(arch_bfd, sizeof (struct ar_cache));
164
165 if (new_cache == NULL) {
166 bfd_error = no_memory;
167 return false;
168 }
169
170 new_cache->ptr = filepos;
171 new_cache->arelt = new_elt;
172 new_cache->next = (struct ar_cache *)NULL;
173 if (bfd_ardata (arch_bfd)->cache == NULL)
174 bfd_ardata (arch_bfd)->cache = new_cache;
175 else {
176 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
177
178 for (; current->next != NULL; current = current->next);
179 current->next = new_cache;
180 }
181
182 return true;
183 }
184
185 \f
186
187 /* The name begins with space. Hence the rest of the name is an index into
188 the string table. */
189
190 char *
191 get_extended_arelt_filename (arch, name)
192 bfd *arch;
193 char *name;
194 {
195 #ifndef errno
196 extern int errno;
197 #endif
198 unsigned long index = 0;
199
200 /* Should extract string so that I can guarantee not to overflow into
201 the next region, but I"m too lazy. */
202 errno = 0;
203 index = strtol (name, NULL, 10);
204 if (errno != 0) {
205 bfd_error = malformed_archive;
206 return NULL;
207 }
208
209 return bfd_ardata (arch)->extended_names + index;
210 }
211
212 /* This functions reads an arch header and returns an areltdata pointer, or
213 NULL on error.
214
215 Presumes the file pointer is already in the right place (ie pointing
216 to the ar_hdr in the file). Moves the file pointer; on success it
217 should be pointing to the front of the file contents; on failure it
218 could have been moved arbitrarily.
219 */
220
221 struct areltdata *
222 snarf_ar_hdr (abfd)
223 bfd *abfd;
224 {
225 #ifndef errno
226 extern int errno;
227 #endif
228
229 struct ar_hdr hdr;
230 char *hdrp = (char *) &hdr;
231 unsigned int parsed_size;
232 struct areltdata *ared;
233 char *filename = NULL;
234 unsigned int namelen = 0;
235 unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
236 char *allocptr;
237
238 if (bfd_read ((PTR)hdrp, 1, sizeof (struct ar_hdr), abfd)
239 != sizeof (struct ar_hdr)) {
240 bfd_error = no_more_archived_files;
241 return NULL;
242 }
243 if (strncmp ((hdr.ar_fmag), ARFMAG, 2)) {
244 bfd_error = malformed_archive;
245 return NULL;
246 }
247
248 errno = 0;
249 parsed_size = strtol (hdr.ar_size, NULL, 10);
250 if (errno != 0) {
251 bfd_error = malformed_archive;
252 return NULL;
253 }
254
255 /* extract the filename from the archive - there are two ways to
256 specify an extendend name table, either the first char of the
257 name is a space, or it's a slash */
258 if ((hdr.ar_name[0] == '/' || hdr.ar_name[0] == ' ') && bfd_ardata (abfd)->extended_names != NULL) {
259 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
260 if (filename == NULL) {
261 bfd_error = malformed_archive;
262 return NULL;
263 }
264 }
265 else
266 {
267 /* We judge the end of the name by looking for a space or a
268 padchar */
269
270 namelen = 0;
271
272 while (namelen < (unsigned)ar_maxnamelen(abfd) &&
273 ( hdr.ar_name[namelen] != 0 &&
274 hdr.ar_name[namelen] != ' ' &&
275 hdr.ar_name[namelen] != ar_padchar(abfd))) {
276 namelen++;
277 }
278
279 allocsize += namelen + 1;
280 }
281
282 allocptr = bfd_zalloc(abfd, allocsize);
283 if (allocptr == NULL) {
284 bfd_error = no_memory;
285 return NULL;
286 }
287
288 ared = (struct areltdata *) allocptr;
289
290 ared->arch_header = allocptr + sizeof (struct areltdata);
291 memcpy ((char *) ared->arch_header, (char *) &hdr, sizeof (struct ar_hdr));
292 ared->parsed_size = parsed_size;
293
294 if (filename != NULL) ared->filename = filename;
295 else {
296 ared->filename = allocptr + (sizeof (struct areltdata) +
297 sizeof (struct ar_hdr));
298 if (namelen)
299 memcpy (ared->filename, hdr.ar_name, namelen);
300 ared->filename[namelen] = '\0';
301 }
302
303 return ared;
304 }
305 \f
306 bfd *
307 get_elt_at_filepos (archive, filepos)
308 bfd *archive;
309 file_ptr filepos;
310 {
311 struct areltdata *new_areldata;
312 bfd *n_nfd;
313
314 n_nfd = look_for_bfd_in_cache (archive, filepos);
315 if (n_nfd) return n_nfd;
316
317 if (0 > bfd_seek (archive, filepos, SEEK_SET)) {
318 bfd_error = system_call_error;
319 return NULL;
320 }
321
322 if ((new_areldata = snarf_ar_hdr (archive)) == NULL) return NULL;
323
324 n_nfd = _bfd_create_empty_archive_element_shell (archive);
325 if (n_nfd == NULL) {
326 bfd_release (archive, (PTR)new_areldata);
327 return NULL;
328 }
329 n_nfd->origin = bfd_tell (archive);
330 n_nfd->arelt_data = (PTR) new_areldata;
331 n_nfd->filename = new_areldata->filename;
332
333 if (add_bfd_to_cache (archive, filepos, n_nfd))
334 return n_nfd;
335
336 /* huh? */
337 bfd_release (archive, (PTR)n_nfd);
338 bfd_release (archive, (PTR)new_areldata);
339 return NULL;
340 }
341
342 /*proto* bfd_get_elt_at_index
343
344 *; PROTO(bfd *, bfd_get_elt_at_index, (bfd *, int));
345
346 */
347 bfd *
348 bfd_get_elt_at_index (abfd, index)
349 bfd *abfd;
350 int index;
351 {
352 bfd *result =
353 get_elt_at_filepos
354 (abfd, (bfd_ardata (abfd)->symdefs + index)->file_offset);
355 return result;
356 }
357
358 /*proto* bfd_openr_next_archived_file
359 Initially provided a BFD containing an archive and NULL, opens a BFD
360 on the first contained element and returns that. Subsequent calls to
361 bfd_openr_next_archived_file should pass the archive and the previous
362 return value to return a created BFD to the next contained element.
363 NULL is returned when there are no more.
364
365 *; PROTO(bfd*, bfd_openr_next_archived_file,
366 (bfd *archive, bfd *previous));
367
368 */
369
370 bfd *
371 DEFUN(bfd_openr_next_archived_file,(archive, last_file),
372 bfd *archive AND
373 bfd*last_file)
374 {
375
376 if ((bfd_get_format (archive) != bfd_archive) ||
377 (archive->direction == write_direction)) {
378 bfd_error = invalid_operation;
379 return NULL;
380 }
381
382
383 return BFD_SEND (archive,
384 openr_next_archived_file,
385 (archive,
386 last_file));
387
388 }
389
390 bfd *bfd_generic_openr_next_archived_file(archive, last_file)
391 bfd *archive;
392 bfd *last_file;
393 {
394 file_ptr filestart;
395
396 if (!last_file)
397 filestart = bfd_ardata (archive)->first_file_filepos;
398 else {
399 unsigned int size = arelt_size(last_file);
400 /* Pad to an even boundary... */
401 filestart = last_file->origin + size + size%2;
402 }
403
404 return get_elt_at_filepos (archive, filestart);
405 }
406 \f
407
408 bfd_target *
409 bfd_generic_archive_p (abfd)
410 bfd *abfd;
411 {
412 char armag[SARMAG+1];
413
414 if (bfd_read ((PTR)armag, 1, SARMAG, abfd) != SARMAG) {
415 bfd_error = wrong_format;
416 return 0;
417 }
418
419 #ifdef GNU960
420 if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG)) return 0;
421 #else
422 if (strncmp (armag, ARMAG, SARMAG)) return 0;
423 #endif
424
425 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
426 involves a cast, we can't do it as the left operand of assignment. */
427 set_tdata (abfd, bfd_zalloc(abfd,sizeof (struct artdata)));
428
429 if (bfd_ardata (abfd) == NULL) {
430 bfd_error = no_memory;
431 return 0;
432 }
433
434 bfd_ardata (abfd)->first_file_filepos = SARMAG;
435
436 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))) {
437 bfd_release(abfd, bfd_ardata (abfd));
438 abfd->tdata = NULL;
439 return 0;
440 }
441
442 if (!BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) {
443 bfd_release(abfd, bfd_ardata (abfd));
444 abfd->tdata = NULL;
445 return 0;
446 }
447
448 return abfd->xvec;
449 }
450
451 /* Returns false on error, true otherwise */
452 boolean
453 bfd_slurp_bsd_armap (abfd)
454 bfd *abfd;
455 {
456
457 struct areltdata *mapdata;
458 char nextname[17];
459 unsigned int counter = 0;
460 int *raw_armap, *rbase;
461 struct artdata *ardata = bfd_ardata (abfd);
462 char *stringbase;
463
464 /* FIXME, if the read fails, this routine quietly returns "true"!!
465 It should probably do that if the read gives 0 bytes (empty archive),
466 but fail for any other size... */
467 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
468 /* The archive has at least 16 bytes in it */
469 bfd_seek (abfd, -16L, SEEK_CUR);
470
471 /* This should be using RANLIBMAG, but at least it can be grepped for
472 in this comment. */
473 if (strncmp (nextname, "__.SYMDEF ", 16)) {
474 bfd_has_map (abfd) = false;
475 return true;
476 }
477
478 mapdata = snarf_ar_hdr (abfd);
479 if (mapdata == NULL) return false;
480
481 raw_armap = (int *) bfd_zalloc(abfd,mapdata->parsed_size);
482 if (raw_armap == NULL) {
483 bfd_error = no_memory;
484 byebye:
485 bfd_release (abfd, (PTR)mapdata);
486 return false;
487 }
488
489 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
490 mapdata->parsed_size) {
491 bfd_error = malformed_archive;
492 bfd_release (abfd, (PTR)raw_armap);
493 goto byebye;
494 }
495
496 ardata->symdef_count = bfd_h_get_32(abfd, (PTR)raw_armap) / sizeof (struct symdef);
497 ardata->cache = 0;
498 rbase = raw_armap+1;
499 ardata->symdefs = (carsym *) rbase;
500 stringbase = ((char *) (ardata->symdefs + ardata->symdef_count)) + 4;
501
502 for (;counter < ardata->symdef_count; counter++) {
503 struct symdef *sym = ((struct symdef *) rbase) + counter;
504 sym->s.name = bfd_h_get_32(abfd, (PTR)(&(sym->s.string_offset))) + stringbase;
505 sym->file_offset = bfd_h_get_32(abfd, (PTR)( &(sym->file_offset)));
506 }
507
508 ardata->first_file_filepos = bfd_tell (abfd);
509 /* Pad to an even boundary if you have to */
510 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
511 /* FIXME, we should provide some way to free raw_ardata when
512 we are done using the strings from it. For now, it seems
513 to be allocated on an obstack anyway... */
514 bfd_has_map (abfd) = true;
515 }
516 return true;
517 }
518
519 /* Returns false on error, true otherwise */
520 boolean
521 bfd_slurp_coff_armap (abfd)
522 bfd *abfd;
523 {
524 struct areltdata *mapdata;
525 char nextname;
526 int *raw_armap, *rawptr;
527 struct artdata *ardata = bfd_ardata (abfd);
528 char *stringbase;
529 unsigned int stringsize;
530 carsym *carsyms;
531 int result;
532
533 result = bfd_read ((PTR)&nextname, 1, 1, abfd);
534 bfd_seek (abfd, -1L, SEEK_CUR);
535
536 if (result != 1 || nextname != '/') {
537 /* Actually I think this is an error for a COFF archive */
538 bfd_has_map (abfd) = false;
539 return true;
540 }
541
542 mapdata = snarf_ar_hdr (abfd);
543 if (mapdata == NULL) return false;
544
545 raw_armap = (int *) bfd_alloc(abfd,mapdata->parsed_size);
546
547 if (raw_armap == NULL)
548 {
549 bfd_error = no_memory;
550 byebye:
551 bfd_release (abfd, (PTR)mapdata);
552 return false;
553 }
554
555 /* read in the raw map */
556 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
557 mapdata->parsed_size) {
558 bfd_error = malformed_archive;
559 oops:
560 bfd_release (abfd, (PTR)raw_armap);
561 goto byebye;
562 }
563
564 /* The coff armap must be read sequentially. So we construct a bsd-style
565 one in core all at once, for simplicity. */
566
567 stringsize = mapdata->parsed_size - (4 * (*raw_armap)) - 4;
568
569 {
570 unsigned int nsymz = *raw_armap;
571 unsigned int carsym_size = (nsymz * sizeof (carsym));
572 unsigned int ptrsize = (4 * nsymz);
573 unsigned int i;
574 ardata->symdefs = (carsym *) bfd_zalloc(abfd,carsym_size + stringsize + 1);
575 if (ardata->symdefs == NULL) {
576 bfd_error = no_memory;
577 goto oops;
578 }
579 carsyms = ardata->symdefs;
580
581 stringbase = ((char *) ardata->symdefs) + carsym_size;
582 memcpy (stringbase, (char*)raw_armap + ptrsize + 4, stringsize);
583
584
585 /* OK, build the carsyms */
586 for (i = 0; i < nsymz; i++)
587 {
588 rawptr = raw_armap + i + 1;
589 carsyms->file_offset = *rawptr;
590 carsyms->name = stringbase;
591 for (; *(stringbase++););
592 carsyms++;
593 }
594 *stringbase = 0;
595 }
596 ardata->symdef_count = *raw_armap;
597 ardata->first_file_filepos = bfd_tell (abfd);
598 /* Pad to an even boundary if you have to */
599 ardata->first_file_filepos += (ardata->first_file_filepos) %2;
600
601 /* We'd like to release these allocations, but we have allocated stuff
602 since then (using the same obstack, if bfd_release is obstack based).
603 So they will stick around until the BFD is closed. */
604 /* bfd_release (abfd, (PTR)raw_armap);
605 bfd_release (abfd, (PTR)mapdata); */
606 bfd_has_map (abfd) = true;
607 return true;
608 }
609 \f
610 /** Extended name table.
611
612 Normally archives support only 14-character filenames.
613
614 Intel has extended the format: longer names are stored in a special
615 element (the first in the archive, or second if there is an armap);
616 the name in the ar_hdr is replaced by <space><index into filename
617 element>. Index is the P.R. of an int (radix: 8). Data General have
618 extended the format by using the prefix // for the special element */
619
620 /* Returns false on error, true otherwise */
621 boolean
622 _bfd_slurp_extended_name_table (abfd)
623 bfd *abfd;
624 {
625 char nextname[17];
626 struct areltdata *namedata;
627
628 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
629 we probably don't want to return true. */
630 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
631
632 bfd_seek (abfd, -16L, SEEK_CUR);
633
634 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
635 strncmp (nextname, "// ", 16) != 0)
636 {
637 bfd_ardata (abfd)->extended_names = NULL;
638 return true;
639 }
640
641 namedata = snarf_ar_hdr (abfd);
642 if (namedata == NULL) return false;
643
644 bfd_ardata (abfd)->extended_names = bfd_zalloc(abfd,namedata->parsed_size);
645 if (bfd_ardata (abfd)->extended_names == NULL) {
646 bfd_error = no_memory;
647 byebye:
648 bfd_release (abfd, (PTR)namedata);
649 return false;
650 }
651
652 if (bfd_read ((PTR)bfd_ardata (abfd)->extended_names, 1,
653 namedata->parsed_size, abfd) != namedata->parsed_size) {
654 bfd_error = malformed_archive;
655 bfd_release (abfd, (PTR)(bfd_ardata (abfd)->extended_names));
656 bfd_ardata (abfd)->extended_names = NULL;
657 goto byebye;
658 }
659
660 /* Since the archive is supposed to be printable if it contains
661 text, the entries in the list are newline-padded, not null
662 padded. We'll fix that there.. */
663 {
664 char *temp = bfd_ardata (abfd)->extended_names;
665 for (; *temp != '\0'; ++temp)
666 if (*temp == '\n') *temp = '\0';
667 }
668
669 /* Pad to an even boundary if you have to */
670 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
671 bfd_ardata (abfd)->first_file_filepos +=
672 (bfd_ardata (abfd)->first_file_filepos) %2;
673
674 /* FIXME, we can't release namedata here because it was allocated
675 below extended_names on the obstack... */
676 /* bfd_release (abfd, namedata); */
677 }
678 return true;
679 }
680
681 static
682 char *normalize(file)
683 char *file;
684 {
685 char * filename = strrchr(file, '/');
686 if (filename != (char *)NULL) {
687 filename ++;
688 }
689 else {
690 filename = file;
691 }
692 return filename;
693 }
694
695 /* Follows archive_head and produces an extended name table if necessary.
696 Returns (in tabloc) a pointer to an extended name table, and in tablen
697 the length of the table. If it makes an entry it clobbers the filename
698 so that the element may be written without further massage.
699 Returns true if it ran successfully, false if something went wrong.
700 A successful return may still involve a zero-length tablen!
701 */
702 boolean
703 bfd_construct_extended_name_table (abfd, tabloc, tablen)
704 bfd *abfd;
705 char **tabloc;
706 unsigned int *tablen;
707 {
708 unsigned int maxname = abfd->xvec->ar_max_namelen;
709 unsigned int total_namelen = 0;
710 bfd *current;
711 char *strptr;
712
713 *tablen = 0;
714
715 /* Figure out how long the table should be */
716 for (current = abfd->archive_head; current != NULL; current = current->next){
717 unsigned int thislen = strlen (normalize(current->filename));
718 if (thislen > maxname) total_namelen += thislen + 1; /* leave room for \n */
719 }
720
721 if (total_namelen == 0) return true;
722
723 *tabloc = bfd_zalloc (abfd,total_namelen);
724 if (*tabloc == NULL) {
725 bfd_error = no_memory;
726 return false;
727 }
728
729 *tablen = total_namelen;
730 strptr = *tabloc;
731
732 for (current = abfd->archive_head; current != NULL; current =
733 current->next) {
734 char *normal =normalize( current->filename);
735 unsigned int thislen = strlen (normal);
736 if (thislen > maxname) {
737 /* Works for now; may need to be re-engineered if we encounter an oddball
738 archive format and want to generalise this hack. */
739 struct ar_hdr *hdr = arch_hdr(current);
740 strcpy (strptr, normal);
741 strptr[thislen] = '\n';
742 hdr->ar_name[0] = ' ';
743 /* We know there will always be enough room (one of the few cases
744 where you may safely use sprintf). */
745 sprintf ((hdr->ar_name) + 1, "%-o", (unsigned) (strptr - *tabloc));
746 /* Kinda Kludgy. We should just use the returned value of sprintf
747 but not all implementations get this right */
748 {
749 char *temp = hdr->ar_name +2;
750 for (; temp < hdr->ar_name + maxname; temp++)
751 if (*temp == '\0') *temp = ' ';
752 }
753 strptr += thislen + 1;
754 }
755 }
756
757 return true;
758 }
759 \f
760 /** A couple of functions for creating ar_hdrs */
761
762 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
763 The filename must refer to a filename in the filesystem.
764 The filename field of the ar_hdr will NOT be initialized
765 */
766
767 struct areltdata *
768 DEFUN(bfd_ar_hdr_from_filesystem, (abfd,filename),
769 bfd* abfd AND
770 CONST char *filename)
771 {
772 struct stat status;
773 struct areltdata *ared;
774 struct ar_hdr *hdr;
775 char *temp, *temp1;
776
777
778 if (stat (filename, &status) != 0) {
779 bfd_error = system_call_error;
780 return NULL;
781 }
782
783 ared = (struct areltdata *) bfd_zalloc(abfd, sizeof (struct ar_hdr) +
784 sizeof (struct areltdata));
785 if (ared == NULL) {
786 bfd_error = no_memory;
787 return NULL;
788 }
789 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
790
791 /* ar headers are space padded, not null padded! */
792 temp = (char *) hdr;
793 temp1 = temp + sizeof (struct ar_hdr) - 2;
794 for (; temp < temp1; *(temp++) = ' ');
795 strncpy (hdr->ar_fmag, ARFMAG, 2);
796
797 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
798 sprintf ((hdr->ar_date), "%-12ld", status.st_mtime);
799 sprintf ((hdr->ar_uid), "%d", status.st_uid);
800 sprintf ((hdr->ar_gid), "%d", status.st_gid);
801 sprintf ((hdr->ar_mode), "%-8o", (unsigned) status.st_mode);
802 sprintf ((hdr->ar_size), "%-10ld", status.st_size);
803 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
804 understand how these C losers could design such a ramshackle bunch of
805 IO operations */
806 temp = (char *) hdr;
807 temp1 = temp + sizeof (struct ar_hdr) - 2;
808 for (; temp < temp1; temp++) {
809 if (*temp == '\0') *temp = ' ';
810 }
811 strncpy (hdr->ar_fmag, ARFMAG, 2);
812 ared->parsed_size = status.st_size;
813 ared->arch_header = (char *) hdr;
814
815 return ared;
816 }
817
818 struct ar_hdr *
819 DEFUN(bfd_special_undocumented_glue, (abfd, filename),
820 bfd *abfd AND
821 char *filename)
822 {
823
824 return (struct ar_hdr *) bfd_ar_hdr_from_filesystem (abfd, filename) -> arch_header;
825 }
826
827
828 /* Analogous to stat call */
829 int
830 bfd_generic_stat_arch_elt (abfd, buf)
831 bfd *abfd;
832 struct stat *buf;
833 {
834 struct ar_hdr *hdr;
835 char *aloser;
836
837 if (abfd->arelt_data == NULL) {
838 bfd_error = invalid_operation;
839 return -1;
840 }
841
842 hdr = arch_hdr (abfd);
843
844 #define foo(arelt, stelt, size) \
845 buf->stelt = strtol (hdr->arelt, &aloser, size); \
846 if (aloser == hdr->arelt) return -1;
847
848 foo (ar_date, st_mtime, 10);
849 foo (ar_uid, st_uid, 10);
850 foo (ar_gid, st_gid, 10);
851 foo (ar_mode, st_mode, 8);
852 foo (ar_size, st_size, 10);
853
854 return 0;
855 }
856
857 void
858 bfd_dont_truncate_arname (abfd, pathname, arhdr)
859 bfd *abfd;
860 CONST char *pathname;
861 char *arhdr;
862 {
863 /* FIXME: This interacts unpleasantly with ar's quick-append option.
864 Fortunately ic960 users will never use that option. Fixing this
865 is very hard; fortunately I know how to do it and will do so once
866 intel's release is out the door. */
867
868 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
869 int length;
870 CONST char *filename = strrchr (pathname, '/');
871 int maxlen = ar_maxnamelen (abfd);
872
873 if (filename == NULL)
874 filename = pathname;
875 else
876 ++filename;
877
878 length = strlen (filename);
879
880 if (length <= maxlen)
881 memcpy (hdr->ar_name, filename, length);
882
883 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
884 return;
885
886 }
887
888 void
889 bfd_bsd_truncate_arname (abfd, pathname, arhdr)
890 bfd *abfd;
891 CONST char *pathname;
892 char *arhdr;
893 {
894 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
895 int length;
896 CONST char *filename = strrchr (pathname, '/');
897 int maxlen = ar_maxnamelen (abfd);
898
899
900 if (filename == NULL)
901 filename = pathname;
902 else
903 ++filename;
904
905 length = strlen (filename);
906
907 if (length <= maxlen)
908 memcpy (hdr->ar_name, filename, length);
909 else {
910 /* pathname: meet procrustes */
911 memcpy (hdr->ar_name, filename, maxlen);
912 length = maxlen;
913 }
914
915 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
916 }
917
918 /* Store name into ar header. Truncates the name to fit.
919 1> strip pathname to be just the basename.
920 2> if it's short enuf to fit, stuff it in.
921 3> If it doesn't end with .o, truncate it to fit
922 4> truncate it before the .o, append .o, stuff THAT in.
923 */
924
925 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
926 void
927 bfd_gnu_truncate_arname (abfd, pathname, arhdr)
928 bfd *abfd;
929 CONST char *pathname;
930 char *arhdr;
931 {
932 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
933 int length;
934 CONST char *filename = strrchr (pathname, '/');
935 int maxlen = ar_maxnamelen (abfd);
936
937 if (filename == NULL)
938 filename = pathname;
939 else
940 ++filename;
941
942 length = strlen (filename);
943
944 if (length <= maxlen)
945 memcpy (hdr->ar_name, filename, length);
946 else { /* pathname: meet procrustes */
947 memcpy (hdr->ar_name, filename, maxlen);
948 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) {
949 hdr->ar_name[maxlen - 2] = '.';
950 hdr->ar_name[maxlen - 1] = 'o';
951 }
952 length = maxlen;
953 }
954
955 if (length < 16) (hdr->ar_name)[length] = ar_padchar (abfd);
956 }
957 \f
958
959 PROTO (boolean, compute_and_write_armap, (bfd *arch, unsigned int elength));
960
961 /* The BFD is open for write and has its format set to bfd_archive */
962 boolean
963 _bfd_write_archive_contents (arch)
964 bfd *arch;
965 {
966 bfd *current;
967 char *etable = NULL;
968 unsigned int elength = 0;
969 boolean makemap = bfd_has_map (arch);
970 boolean hasobjects = false; /* if no .o's, don't bother to make a map */
971 unsigned int i;
972
973 /* Verify the viability of all entries; if any of them live in the
974 filesystem (as opposed to living in an archive open for input)
975 then construct a fresh ar_hdr for them.
976 */
977 for (current = arch->archive_head; current; current = current->next) {
978 if (bfd_write_p (current)) {
979 bfd_error = invalid_operation;
980 return false;
981 }
982 if (!current->arelt_data) {
983 current->arelt_data =
984 (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename);
985 if (!current->arelt_data) return false;
986
987 /* Put in the file name */
988
989 BFD_SEND (arch, _bfd_truncate_arname,(arch,
990 current->filename,
991 (char *) arch_hdr(current)));
992
993
994 }
995
996 if (makemap) { /* don't bother if we won't make a map! */
997 if ((bfd_check_format (current, bfd_object))
998 #if 0 /* FIXME -- these are not set correctly */
999 && ((bfd_get_file_flags (current) & HAS_SYMS))
1000 #endif
1001 )
1002 hasobjects = true;
1003 }
1004 }
1005
1006 if (!bfd_construct_extended_name_table (arch, &etable, &elength))
1007 return false;
1008
1009 bfd_seek (arch, 0, SEEK_SET);
1010 #ifdef GNU960
1011 bfd_write (BFD_GNU960_ARMAG(arch), 1, SARMAG, arch);
1012 #else
1013 bfd_write (ARMAG, 1, SARMAG, arch);
1014 #endif
1015
1016 if (makemap && hasobjects) {
1017
1018 if (compute_and_write_armap (arch, elength) != true) {
1019 return false;
1020 }
1021 }
1022
1023 if (elength != 0) {
1024 struct ar_hdr hdr;
1025
1026 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1027 sprintf (&(hdr.ar_name[0]), "ARFILENAMES/");
1028 sprintf (&(hdr.ar_size[0]), "%-10d", (int) elength);
1029 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1030 for (i = 0; i < sizeof (struct ar_hdr); i++)
1031 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1032 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1033 bfd_write (etable, 1, elength, arch);
1034 if ((elength % 2) == 1) bfd_write ("\n", 1, 1, arch);
1035
1036 }
1037
1038 for (current = arch->archive_head; current; current = current->next) {
1039 char buffer[DEFAULT_BUFFERSIZE];
1040 unsigned int remaining = arelt_size (current);
1041 struct ar_hdr *hdr = arch_hdr(current);
1042 /* write ar header */
1043
1044 if (bfd_write ((char *)hdr, 1, sizeof(*hdr), arch) != sizeof(*hdr)) {
1045 syserr:
1046 bfd_error = system_call_error;
1047 return false;
1048 }
1049 if (bfd_seek (current, 0L, SEEK_SET) != 0L) goto syserr;
1050 while (remaining)
1051 {
1052 unsigned int amt = DEFAULT_BUFFERSIZE;
1053 if (amt > remaining) {
1054 amt = remaining;
1055 }
1056 if (bfd_read (buffer, amt, 1, current) != amt) goto syserr;
1057 if (bfd_write (buffer, amt, 1, arch) != amt) goto syserr;
1058 remaining -= amt;
1059 }
1060 if ((arelt_size (current) % 2) == 1) bfd_write ("\n", 1, 1, arch);
1061 }
1062 return true;
1063 }
1064 \f
1065 /* Note that the namidx for the first symbol is 0 */
1066
1067 boolean
1068 compute_and_write_armap (arch, elength)
1069 bfd *arch;
1070 unsigned int elength;
1071 {
1072 bfd *current;
1073 file_ptr elt_no = 0;
1074 struct orl *map;
1075 int orl_max = 15000; /* fine initial default */
1076 int orl_count = 0;
1077 int stridx = 0; /* string index */
1078
1079 /* Dunno if this is the best place for this info... */
1080 if (elength != 0) elength += sizeof (struct ar_hdr);
1081 elength += elength %2 ;
1082
1083 map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
1084 if (map == NULL) {
1085 bfd_error = no_memory;
1086 return false;
1087 }
1088
1089 /* Map over each element */
1090 for (current = arch->archive_head;
1091 current != (bfd *)NULL;
1092 current = current->next, elt_no++)
1093 {
1094 if ((bfd_check_format (current, bfd_object) == true)
1095 && ((bfd_get_file_flags (current) & HAS_SYMS))) {
1096 asymbol **syms;
1097 unsigned int storage;
1098 unsigned int symcount;
1099 unsigned int src_count;
1100
1101 storage = get_symtab_upper_bound (current);
1102 if (storage != 0) {
1103
1104 syms = (asymbol **) bfd_zalloc (arch,storage);
1105 if (syms == NULL) {
1106 bfd_error = no_memory; /* FIXME -- memory leak */
1107 return false;
1108 }
1109 symcount = bfd_canonicalize_symtab (current, syms);
1110
1111
1112 /* Now map over all the symbols, picking out the ones we want */
1113 for (src_count = 0; src_count <symcount; src_count++) {
1114 flagword flags = (syms[src_count])->flags;
1115 if ((flags & BSF_GLOBAL) ||
1116 (flags & BSF_FORT_COMM)) {
1117
1118 /* This symbol will go into the archive header */
1119 if (orl_count == orl_max)
1120 {
1121 orl_max *= 2;
1122 map = (struct orl *) bfd_realloc (arch, (char *) map,
1123 orl_max * sizeof (struct orl));
1124 }
1125
1126 (map[orl_count]).name = (char **) &((syms[src_count])->name);
1127 (map[orl_count]).pos = (file_ptr) current;
1128 (map[orl_count]).namidx = stridx;
1129
1130 stridx += strlen ((syms[src_count])->name) + 1;
1131 ++orl_count;
1132 }
1133 }
1134 }
1135 }
1136 }
1137 /* OK, now we have collected all the data, let's write them out */
1138 if (!BFD_SEND (arch, write_armap,
1139 (arch, elength, map, orl_count, stridx))) {
1140
1141 return false;
1142 }
1143
1144
1145 return true;
1146 }
1147
1148 boolean
1149 bsd_write_armap (arch, elength, map, orl_count, stridx)
1150 bfd *arch;
1151 unsigned int elength;
1152 struct orl *map;
1153 int orl_count;
1154 int stridx;
1155 {
1156 unsigned int ranlibsize = (orl_count * sizeof (struct ranlib)) + 4;
1157 unsigned int stringsize = stridx + 4;
1158 unsigned int mapsize = stringsize + ranlibsize;
1159 file_ptr firstreal;
1160 bfd *current = arch->archive_head;
1161 bfd *last_elt = current; /* last element arch seen */
1162 int temp;
1163 int count;
1164 struct ar_hdr hdr;
1165 struct stat statbuf;
1166 unsigned int i;
1167 int padit = mapsize & 1;
1168
1169 if (padit) mapsize ++;
1170
1171 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1172
1173 stat (arch->filename, &statbuf);
1174 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1175 sprintf (hdr.ar_name, RANLIBMAG);
1176 sprintf (hdr.ar_date, "%ld", statbuf.st_mtime);
1177 sprintf (hdr.ar_uid, "%d", getuid());
1178 sprintf (hdr.ar_gid, "%d", getgid());
1179 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1180 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1181 for (i = 0; i < sizeof (struct ar_hdr); i++)
1182 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1183 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1184 bfd_h_put_32(arch, ranlibsize, (PTR)&temp);
1185 bfd_write (&temp, 1, sizeof (temp), arch);
1186
1187 for (count = 0; count < orl_count; count++) {
1188 struct symdef outs;
1189 struct symdef *outp = &outs;
1190
1191 if (((bfd *)(map[count]).pos) != last_elt) {
1192 do {
1193 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1194 firstreal += firstreal % 2;
1195 current = current->next;
1196 } while (current != (bfd *)(map[count]).pos);
1197 } /* if new archive element */
1198
1199 last_elt = current;
1200 bfd_h_put_32(arch, ((map[count]).namidx),(PTR) &outs.s.string_offset);
1201 bfd_h_put_32(arch, firstreal,(PTR) &outs.file_offset);
1202 bfd_write ((char *)outp, 1, sizeof (outs), arch);
1203 }
1204
1205 /* now write the strings themselves */
1206 bfd_h_put_32(arch, stridx, (PTR)&temp);
1207 bfd_write ((PTR)&temp, 1, sizeof (temp), arch);
1208 for (count = 0; count < orl_count; count++)
1209 bfd_write (*((map[count]).name), 1, strlen (*((map[count]).name))+1, arch);
1210
1211 /* The spec sez this should be a newline. But in order to be
1212 bug-compatible for sun's ar we use a null. */
1213 if (padit)
1214 bfd_write("\0",1,1,arch);
1215
1216 return true;
1217 }
1218 \f
1219
1220 /* A coff armap looks like :
1221 ARMAG
1222 struct ar_hdr with name = '/'
1223 number of symbols
1224 offset of file for symbol 0
1225 offset of file for symbol 1
1226 ..
1227 offset of file for symbol n-1
1228 symbol name 0
1229 symbol name 1
1230 ..
1231 symbol name n-1
1232
1233 */
1234
1235 boolean
1236 coff_write_armap (arch, elength, map, orl_count, stridx)
1237 bfd *arch;
1238 unsigned int elength;
1239 struct orl *map;
1240 int orl_count;
1241 int stridx;
1242 {
1243 unsigned int ranlibsize = (orl_count * 4) + 4;
1244 unsigned int stringsize = stridx;
1245 unsigned int mapsize = stringsize + ranlibsize;
1246 file_ptr archive_member_file_ptr;
1247 bfd *current = arch->archive_head;
1248 int last_eltno = 0; /* last element arch seen */
1249 int count;
1250 struct ar_hdr hdr;
1251 unsigned int i;
1252 int padit = mapsize & 1;
1253
1254 if (padit) mapsize ++;
1255
1256 archive_member_file_ptr =
1257 mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1258
1259 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1260 hdr.ar_name[0] = '/';
1261 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1262 sprintf (hdr.ar_date, "%ld", (long)time (NULL));
1263 /* This, at least, is what Intel coff sets the values to.: */
1264 sprintf ((hdr.ar_uid), "%d", 0);
1265 sprintf ((hdr.ar_gid), "%d", 0);
1266 sprintf ((hdr.ar_mode), "%-7o",(unsigned ) 0);
1267 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1268
1269 for (i = 0; i < sizeof (struct ar_hdr); i++)
1270 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1271
1272 /* Write the ar header for this item and the number of symbols */
1273
1274 bfd_write ((PTR)&hdr, 1, sizeof (struct ar_hdr), arch);
1275 /* FIXME, this needs to be byte-swapped */
1276 bfd_write ((PTR)&orl_count, 1, sizeof (orl_count), arch);
1277
1278 /* Two passes, first write the file offsets for each symbol -
1279 remembering that each offset is on a two byte boundary
1280 */
1281
1282 for (count = 0; count < orl_count; count++) {
1283 while ((map[count]).pos != last_eltno) {
1284 /* If this is the first time we've seen a ref to this archive
1285 then remember it's size */
1286 archive_member_file_ptr +=
1287 arelt_size (current) + sizeof (struct ar_hdr);
1288 archive_member_file_ptr += archive_member_file_ptr % 2;
1289 current = current->next;
1290 last_eltno++;
1291 }
1292 /* FIXME, this needs to be byte-swapped */
1293 bfd_write ((PTR)&archive_member_file_ptr,
1294 1,
1295 sizeof (archive_member_file_ptr),
1296 arch);
1297 }
1298
1299 /* now write the strings themselves */
1300 for (count = 0; count < orl_count; count++) {
1301 bfd_write ((PTR)*((map[count]).name),
1302 1,
1303 strlen (*((map[count]).name))+1, arch);
1304
1305 }
1306 /* The spec sez this should be a newline. But in order to be
1307 bug-compatible for arc960 we use a null. */
1308 if (padit)
1309 bfd_write("\0",1,1,arch);
1310
1311 return true;
1312 }
This page took 0.058311 seconds and 4 git commands to generate.