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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [bfd/] [archive.c] - Blame information for rev 24

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

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

powered by: WebSVN 2.1.0

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