OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [bfd/] [archive.c] - Blame information for rev 1776

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.