Subversion Repositories Kolibri OS

Rev

Rev 5197 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5197 Rev 6324
1
/* BFD back-end for archive files (libraries).
1
/* BFD back-end for archive files (libraries).
2
   Copyright 1990-2013 Free Software Foundation, Inc.
2
   Copyright (C) 1990-2015 Free Software Foundation, Inc.
3
   Written by Cygnus Support.  Mostly Gumby Henkel-Wallace's fault.
3
   Written by Cygnus Support.  Mostly Gumby Henkel-Wallace's fault.
4
 
4
 
5
   This file is part of BFD, the Binary File Descriptor library.
5
   This file is part of BFD, the Binary File Descriptor library.
6
 
6
 
7
   This program is free software; you can redistribute it and/or modify
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
10
   (at your option) any later version.
11
 
11
 
12
   This program is distributed in the hope that it will be useful,
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
15
   GNU General Public License for more details.
16
 
16
 
17
   You should have received a copy of the GNU General Public License
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
 
20
 
21
/*
21
/*
22
@setfilename archive-info
22
@setfilename archive-info
23
SECTION
23
SECTION
24
	Archives
24
	Archives
25
 
25
 
26
DESCRIPTION
26
DESCRIPTION
27
	An archive (or library) is just another BFD.  It has a symbol
27
	An archive (or library) is just another BFD.  It has a symbol
28
	table, although there's not much a user program will do with it.
28
	table, although there's not much a user program will do with it.
29
 
29
 
30
	The big difference between an archive BFD and an ordinary BFD
30
	The big difference between an archive BFD and an ordinary BFD
31
	is that the archive doesn't have sections.  Instead it has a
31
	is that the archive doesn't have sections.  Instead it has a
32
	chain of BFDs that are considered its contents.  These BFDs can
32
	chain of BFDs that are considered its contents.  These BFDs can
33
	be manipulated like any other.  The BFDs contained in an
33
	be manipulated like any other.  The BFDs contained in an
34
	archive opened for reading will all be opened for reading.  You
34
	archive opened for reading will all be opened for reading.  You
35
	may put either input or output BFDs into an archive opened for
35
	may put either input or output BFDs into an archive opened for
36
	output; they will be handled correctly when the archive is closed.
36
	output; they will be handled correctly when the archive is closed.
37
 
37
 
38
	Use <> to step through
38
	Use <> to step through
39
	the contents of an archive opened for input.  You don't
39
	the contents of an archive opened for input.  You don't
40
	have to read the entire archive if you don't want
40
	have to read the entire archive if you don't want
41
	to!  Read it until you find what you want.
41
	to!  Read it until you find what you want.
42
 
42
 
43
	A BFD returned by <> can be
43
	A BFD returned by <> can be
44
	closed manually with <>.  If you do not close it,
44
	closed manually with <>.  If you do not close it,
45
	then a second iteration through the members of an archive may
45
	then a second iteration through the members of an archive may
46
	return the same BFD.  If you close the archive BFD, then all
46
	return the same BFD.  If you close the archive BFD, then all
47
	the member BFDs will automatically be closed as well.
47
	the member BFDs will automatically be closed as well.
48
 
48
 
49
	Archive contents of output BFDs are chained through the
49
	Archive contents of output BFDs are chained through the
50
	<> pointer in a BFD.  The first one is findable
50
	<> pointer in a BFD.  The first one is findable
51
	through the <> slot of the archive.  Set it with
51
	through the <> slot of the archive.  Set it with
52
	<> (q.v.).  A given BFD may be in only
52
	<> (q.v.).  A given BFD may be in only
53
	one open output archive at a time.
53
	one open output archive at a time.
54
 
54
 
55
	As expected, the BFD archive code is more general than the
55
	As expected, the BFD archive code is more general than the
56
	archive code of any given environment.  BFD archives may
56
	archive code of any given environment.  BFD archives may
57
	contain files of different formats (e.g., a.out and coff) and
57
	contain files of different formats (e.g., a.out and coff) and
58
	even different architectures.  You may even place archives
58
	even different architectures.  You may even place archives
59
	recursively into archives!
59
	recursively into archives!
60
 
60
 
61
	This can cause unexpected confusion, since some archive
61
	This can cause unexpected confusion, since some archive
62
	formats are more expressive than others.  For instance, Intel
62
	formats are more expressive than others.  For instance, Intel
63
	COFF archives can preserve long filenames; SunOS a.out archives
63
	COFF archives can preserve long filenames; SunOS a.out archives
64
	cannot.  If you move a file from the first to the second
64
	cannot.  If you move a file from the first to the second
65
	format and back again, the filename may be truncated.
65
	format and back again, the filename may be truncated.
66
	Likewise, different a.out environments have different
66
	Likewise, different a.out environments have different
67
	conventions as to how they truncate filenames, whether they
67
	conventions as to how they truncate filenames, whether they
68
	preserve directory names in filenames, etc.  When
68
	preserve directory names in filenames, etc.  When
69
	interoperating with native tools, be sure your files are
69
	interoperating with native tools, be sure your files are
70
	homogeneous.
70
	homogeneous.
71
 
71
 
72
	Beware: most of these formats do not react well to the
72
	Beware: most of these formats do not react well to the
73
	presence of spaces in filenames.  We do the best we can, but
73
	presence of spaces in filenames.  We do the best we can, but
74
	can't always handle this case due to restrictions in the format of
74
	can't always handle this case due to restrictions in the format of
75
	archives.  Many Unix utilities are braindead in regards to
75
	archives.  Many Unix utilities are braindead in regards to
76
	spaces and such in filenames anyway, so this shouldn't be much
76
	spaces and such in filenames anyway, so this shouldn't be much
77
	of a restriction.
77
	of a restriction.
78
 
78
 
79
	Archives are supported in BFD in <>.
79
	Archives are supported in BFD in <>.
80
 
80
 
81
SUBSECTION
81
SUBSECTION
82
	Archive functions
82
	Archive functions
83
*/
83
*/
84
 
84
 
85
/* Assumes:
85
/* Assumes:
86
   o - all archive elements start on an even boundary, newline padded;
86
   o - all archive elements start on an even boundary, newline padded;
87
   o - all arch headers are char *;
87
   o - all arch headers are char *;
88
   o - all arch headers are the same size (across architectures).
88
   o - all arch headers are the same size (across architectures).
89
*/
89
*/
90
 
90
 
91
/* Some formats provide a way to cram a long filename into the short
91
/* Some formats provide a way to cram a long filename into the short
92
   (16 chars) space provided by a BSD archive.  The trick is: make a
92
   (16 chars) space provided by a BSD archive.  The trick is: make a
93
   special "file" in the front of the archive, sort of like the SYMDEF
93
   special "file" in the front of the archive, sort of like the SYMDEF
94
   entry.  If the filename is too long to fit, put it in the extended
94
   entry.  If the filename is too long to fit, put it in the extended
95
   name table, and use its index as the filename.  To prevent
95
   name table, and use its index as the filename.  To prevent
96
   confusion prepend the index with a space.  This means you can't
96
   confusion prepend the index with a space.  This means you can't
97
   have filenames that start with a space, but then again, many Unix
97
   have filenames that start with a space, but then again, many Unix
98
   utilities can't handle that anyway.
98
   utilities can't handle that anyway.
99
 
99
 
100
   This scheme unfortunately requires that you stand on your head in
100
   This scheme unfortunately requires that you stand on your head in
101
   order to write an archive since you need to put a magic file at the
101
   order to write an archive since you need to put a magic file at the
102
   front, and need to touch every entry to do so.  C'est la vie.
102
   front, and need to touch every entry to do so.  C'est la vie.
103
 
103
 
104
   We support two variants of this idea:
104
   We support two variants of this idea:
105
   The SVR4 format (extended name table is named "//"),
105
   The SVR4 format (extended name table is named "//"),
106
   and an extended pseudo-BSD variant (extended name table is named
106
   and an extended pseudo-BSD variant (extended name table is named
107
   "ARFILENAMES/").  The origin of the latter format is uncertain.
107
   "ARFILENAMES/").  The origin of the latter format is uncertain.
108
 
108
 
109
   BSD 4.4 uses a third scheme:  It writes a long filename
109
   BSD 4.4 uses a third scheme:  It writes a long filename
110
   directly after the header.  This allows 'ar q' to work.
110
   directly after the header.  This allows 'ar q' to work.
111
*/
111
*/
112
 
112
 
113
/* Summary of archive member names:
113
/* Summary of archive member names:
114
 
114
 
115
 Symbol table (must be first):
115
 Symbol table (must be first):
116
 "__.SYMDEF       " - Symbol table, Berkeley style, produced by ranlib.
116
 "__.SYMDEF       " - Symbol table, Berkeley style, produced by ranlib.
117
 "/               " - Symbol table, system 5 style.
117
 "/               " - Symbol table, system 5 style.
118
 
118
 
119
 Long name table (must be before regular file members):
119
 Long name table (must be before regular file members):
120
 "//              " - Long name table, System 5 R4 style.
120
 "//              " - Long name table, System 5 R4 style.
121
 "ARFILENAMES/    " - Long name table, non-standard extended BSD (not BSD 4.4).
121
 "ARFILENAMES/    " - Long name table, non-standard extended BSD (not BSD 4.4).
122
 
122
 
123
 Regular file members with short names:
123
 Regular file members with short names:
124
 "filename.o/     " - Regular file, System 5 style (embedded spaces ok).
124
 "filename.o/     " - Regular file, System 5 style (embedded spaces ok).
125
 "filename.o      " - Regular file, Berkeley style (no embedded spaces).
125
 "filename.o      " - Regular file, Berkeley style (no embedded spaces).
126
 
126
 
127
 Regular files with long names (or embedded spaces, for BSD variants):
127
 Regular files with long names (or embedded spaces, for BSD variants):
128
 "/18             " - SVR4 style, name at offset 18 in name table.
128
 "/18             " - SVR4 style, name at offset 18 in name table.
129
 "#1/23           " - Long name (or embedded spaces) 23 characters long,
129
 "#1/23           " - Long name (or embedded spaces) 23 characters long,
130
		      BSD 4.4 style, full name follows header.
130
		      BSD 4.4 style, full name follows header.
131
 " 18             " - Long name 18 characters long, extended pseudo-BSD.
131
 " 18             " - Long name 18 characters long, extended pseudo-BSD.
132
 */
132
 */
133
 
133
 
134
#include "sysdep.h"
134
#include "sysdep.h"
135
#include "bfd.h"
135
#include "bfd.h"
136
#include "libiberty.h"
136
#include "libiberty.h"
137
#include "libbfd.h"
137
#include "libbfd.h"
138
#include "aout/ar.h"
138
#include "aout/ar.h"
139
#include "aout/ranlib.h"
139
#include "aout/ranlib.h"
140
#include "safe-ctype.h"
140
#include "safe-ctype.h"
141
#include "hashtab.h"
141
#include "hashtab.h"
142
#include "filenames.h"
142
#include "filenames.h"
-
 
143
#include "bfdlink.h"
143
 
144
 
144
#ifndef errno
145
#ifndef errno
145
extern int errno;
146
extern int errno;
146
#endif
147
#endif
147
 
148
 
148
/* We keep a cache of archive filepointers to archive elements to
149
/* We keep a cache of archive filepointers to archive elements to
149
   speed up searching the archive by filepos.  We only add an entry to
150
   speed up searching the archive by filepos.  We only add an entry to
150
   the cache when we actually read one.  We also don't sort the cache;
151
   the cache when we actually read one.  We also don't sort the cache;
151
   it's generally short enough to search linearly.
152
   it's generally short enough to search linearly.
152
   Note that the pointers here point to the front of the ar_hdr, not
153
   Note that the pointers here point to the front of the ar_hdr, not
153
   to the front of the contents!  */
154
   to the front of the contents!  */
154
struct ar_cache
155
struct ar_cache
155
{
156
{
156
  file_ptr ptr;
157
  file_ptr ptr;
157
  bfd *arbfd;
158
  bfd *arbfd;
158
};
159
};
159
 
160
 
160
#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
161
#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
161
#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
162
#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
162
 
163
 
163
#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
164
#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
164
#define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata (bfd)->arch_header)
165
#define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata (bfd)->arch_header)
165
 
166
 
166
/* True iff NAME designated a BSD 4.4 extended name.  */
167
/* True iff NAME designated a BSD 4.4 extended name.  */
167
 
168
 
168
#define is_bsd44_extended_name(NAME) \
169
#define is_bsd44_extended_name(NAME) \
169
  (NAME[0] == '#'  && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3]))
170
  (NAME[0] == '#'  && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3]))
170

171

171
void
172
void
172
_bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
173
_bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
173
{
174
{
174
  static char buf[20];
175
  static char buf[20];
175
  size_t len;
176
  size_t len;
176
 
177
 
177
  snprintf (buf, sizeof (buf), fmt, val);
178
  snprintf (buf, sizeof (buf), fmt, val);
178
  len = strlen (buf);
179
  len = strlen (buf);
179
  if (len < n)
180
  if (len < n)
180
    {
181
    {
181
      memcpy (p, buf, len);
182
      memcpy (p, buf, len);
182
      memset (p + len, ' ', n - len);
183
      memset (p + len, ' ', n - len);
183
    }
184
    }
184
  else
185
  else
185
    memcpy (p, buf, n);
186
    memcpy (p, buf, n);
186
}
187
}
187
 
188
 
188
bfd_boolean
189
bfd_boolean
189
_bfd_ar_sizepad (char *p, size_t n, bfd_size_type size)
190
_bfd_ar_sizepad (char *p, size_t n, bfd_size_type size)
190
{
191
{
191
  static char buf[21];
192
  static char buf[21];
192
  size_t len;
193
  size_t len;
193
 
194
 
194
  snprintf (buf, sizeof (buf), "%-10" BFD_VMA_FMT "u", size);
195
  snprintf (buf, sizeof (buf), "%-10" BFD_VMA_FMT "u", size);
195
  len = strlen (buf);
196
  len = strlen (buf);
196
  if (len > n)
197
  if (len > n)
197
    {
198
    {
198
      bfd_set_error (bfd_error_file_too_big);
199
      bfd_set_error (bfd_error_file_too_big);
199
      return FALSE;
200
      return FALSE;
200
    }
201
    }
201
  if (len < n)
202
  if (len < n)
202
    {
203
    {
203
      memcpy (p, buf, len);
204
      memcpy (p, buf, len);
204
      memset (p + len, ' ', n - len);
205
      memset (p + len, ' ', n - len);
205
    }
206
    }
206
  else
207
  else
207
    memcpy (p, buf, n);
208
    memcpy (p, buf, n);
208
  return TRUE;
209
  return TRUE;
209
}
210
}
210

211

211
bfd_boolean
212
bfd_boolean
212
_bfd_generic_mkarchive (bfd *abfd)
213
_bfd_generic_mkarchive (bfd *abfd)
213
{
214
{
214
  bfd_size_type amt = sizeof (struct artdata);
215
  bfd_size_type amt = sizeof (struct artdata);
215
 
216
 
216
  abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
217
  abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
217
  if (bfd_ardata (abfd) == NULL)
218
  if (bfd_ardata (abfd) == NULL)
218
    return FALSE;
219
    return FALSE;
219
 
220
 
220
  /* Already cleared by bfd_zalloc above.
221
  /* Already cleared by bfd_zalloc above.
221
     bfd_ardata (abfd)->cache = NULL;
222
     bfd_ardata (abfd)->cache = NULL;
222
     bfd_ardata (abfd)->archive_head = NULL;
223
     bfd_ardata (abfd)->archive_head = NULL;
223
     bfd_ardata (abfd)->symdefs = NULL;
224
     bfd_ardata (abfd)->symdefs = NULL;
224
     bfd_ardata (abfd)->extended_names = NULL;
225
     bfd_ardata (abfd)->extended_names = NULL;
225
     bfd_ardata (abfd)->extended_names_size = 0;
226
     bfd_ardata (abfd)->extended_names_size = 0;
226
     bfd_ardata (abfd)->tdata = NULL;  */
227
     bfd_ardata (abfd)->tdata = NULL;  */
227
 
228
 
228
  return TRUE;
229
  return TRUE;
229
}
230
}
230
 
231
 
231
/*
232
/*
232
FUNCTION
233
FUNCTION
233
	bfd_get_next_mapent
234
	bfd_get_next_mapent
234
 
235
 
235
SYNOPSIS
236
SYNOPSIS
236
	symindex bfd_get_next_mapent
237
	symindex bfd_get_next_mapent
237
	  (bfd *abfd, symindex previous, carsym **sym);
238
	  (bfd *abfd, symindex previous, carsym **sym);
238
 
239
 
239
DESCRIPTION
240
DESCRIPTION
240
	Step through archive @var{abfd}'s symbol table (if it
241
	Step through archive @var{abfd}'s symbol table (if it
241
	has one).  Successively update @var{sym} with the next symbol's
242
	has one).  Successively update @var{sym} with the next symbol's
242
	information, returning that symbol's (internal) index into the
243
	information, returning that symbol's (internal) index into the
243
	symbol table.
244
	symbol table.
244
 
245
 
245
	Supply <> as the @var{previous} entry to get
246
	Supply <> as the @var{previous} entry to get
246
	the first one; returns <> when you've already
247
	the first one; returns <> when you've already
247
	got the last one.
248
	got the last one.
248
 
249
 
249
	A <> is a canonical archive symbol.  The only
250
	A <> is a canonical archive symbol.  The only
250
	user-visible element is its name, a null-terminated string.
251
	user-visible element is its name, a null-terminated string.
251
*/
252
*/
252
 
253
 
253
symindex
254
symindex
254
bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
255
bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
255
{
256
{
256
  if (!bfd_has_map (abfd))
257
  if (!bfd_has_map (abfd))
257
    {
258
    {
258
      bfd_set_error (bfd_error_invalid_operation);
259
      bfd_set_error (bfd_error_invalid_operation);
259
      return BFD_NO_MORE_SYMBOLS;
260
      return BFD_NO_MORE_SYMBOLS;
260
    }
261
    }
261
 
262
 
262
  if (prev == BFD_NO_MORE_SYMBOLS)
263
  if (prev == BFD_NO_MORE_SYMBOLS)
263
    prev = 0;
264
    prev = 0;
264
  else
265
  else
265
    ++prev;
266
    ++prev;
266
  if (prev >= bfd_ardata (abfd)->symdef_count)
267
  if (prev >= bfd_ardata (abfd)->symdef_count)
267
    return BFD_NO_MORE_SYMBOLS;
268
    return BFD_NO_MORE_SYMBOLS;
268
 
269
 
269
  *entry = (bfd_ardata (abfd)->symdefs + prev);
270
  *entry = (bfd_ardata (abfd)->symdefs + prev);
270
  return prev;
271
  return prev;
271
}
272
}
272
 
273
 
273
/* To be called by backends only.  */
274
/* To be called by backends only.  */
274
 
275
 
275
bfd *
276
bfd *
276
_bfd_create_empty_archive_element_shell (bfd *obfd)
277
_bfd_create_empty_archive_element_shell (bfd *obfd)
277
{
278
{
278
  return _bfd_new_bfd_contained_in (obfd);
279
  return _bfd_new_bfd_contained_in (obfd);
279
}
280
}
280
 
281
 
281
/*
282
/*
282
FUNCTION
283
FUNCTION
283
	bfd_set_archive_head
284
	bfd_set_archive_head
284
 
285
 
285
SYNOPSIS
286
SYNOPSIS
286
	bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
287
	bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
287
 
288
 
288
DESCRIPTION
289
DESCRIPTION
289
	Set the head of the chain of
290
	Set the head of the chain of
290
	BFDs contained in the archive @var{output} to @var{new_head}.
291
	BFDs contained in the archive @var{output} to @var{new_head}.
291
*/
292
*/
292
 
293
 
293
bfd_boolean
294
bfd_boolean
294
bfd_set_archive_head (bfd *output_archive, bfd *new_head)
295
bfd_set_archive_head (bfd *output_archive, bfd *new_head)
295
{
296
{
296
  output_archive->archive_head = new_head;
297
  output_archive->archive_head = new_head;
297
  return TRUE;
298
  return TRUE;
298
}
299
}
299
 
300
 
300
bfd *
301
bfd *
301
_bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
302
_bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
302
{
303
{
303
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
304
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
304
  struct ar_cache m;
305
  struct ar_cache m;
305
 
306
 
306
  m.ptr = filepos;
307
  m.ptr = filepos;
307
 
308
 
308
  if (hash_table)
309
  if (hash_table)
309
    {
310
    {
310
      struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
311
      struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
311
      if (!entry)
312
      if (!entry)
312
	return NULL;
313
	return NULL;
-
 
314
 
-
 
315
      /* Unfortunately this flag is set after checking that we have
-
 
316
	 an archive, and checking for an archive means one element has
313
      else
317
	 sneaked into the cache.  */
-
 
318
      entry->arbfd->no_export = arch_bfd->no_export;
314
	return entry->arbfd;
319
	return entry->arbfd;
315
    }
320
    }
316
  else
321
  else
317
    return NULL;
322
    return NULL;
318
}
323
}
319
 
324
 
320
static hashval_t
325
static hashval_t
321
hash_file_ptr (const void * p)
326
hash_file_ptr (const void * p)
322
{
327
{
323
  return (hashval_t) (((struct ar_cache *) p)->ptr);
328
  return (hashval_t) (((struct ar_cache *) p)->ptr);
324
}
329
}
325
 
330
 
326
/* Returns non-zero if P1 and P2 are equal.  */
331
/* Returns non-zero if P1 and P2 are equal.  */
327
 
332
 
328
static int
333
static int
329
eq_file_ptr (const void * p1, const void * p2)
334
eq_file_ptr (const void * p1, const void * p2)
330
{
335
{
331
  struct ar_cache *arc1 = (struct ar_cache *) p1;
336
  struct ar_cache *arc1 = (struct ar_cache *) p1;
332
  struct ar_cache *arc2 = (struct ar_cache *) p2;
337
  struct ar_cache *arc2 = (struct ar_cache *) p2;
333
  return arc1->ptr == arc2->ptr;
338
  return arc1->ptr == arc2->ptr;
334
}
339
}
335
 
340
 
336
/* The calloc function doesn't always take size_t (e.g. on VMS)
341
/* The calloc function doesn't always take size_t (e.g. on VMS)
337
   so wrap it to avoid a compile time warning.   */
342
   so wrap it to avoid a compile time warning.   */
338
 
343
 
339
static void *
344
static void *
340
_bfd_calloc_wrapper (size_t a, size_t b)
345
_bfd_calloc_wrapper (size_t a, size_t b)
341
{
346
{
342
  return calloc (a, b);
347
  return calloc (a, b);
343
}
348
}
344
 
349
 
345
/* Kind of stupid to call cons for each one, but we don't do too many.  */
350
/* Kind of stupid to call cons for each one, but we don't do too many.  */
346
 
351
 
347
bfd_boolean
352
bfd_boolean
348
_bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
353
_bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
349
{
354
{
350
  struct ar_cache *cache;
355
  struct ar_cache *cache;
351
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
356
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
352
 
357
 
353
  /* If the hash table hasn't been created, create it.  */
358
  /* If the hash table hasn't been created, create it.  */
354
  if (hash_table == NULL)
359
  if (hash_table == NULL)
355
    {
360
    {
356
      hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
361
      hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
357
				      NULL, _bfd_calloc_wrapper, free);
362
				      NULL, _bfd_calloc_wrapper, free);
358
      if (hash_table == NULL)
363
      if (hash_table == NULL)
359
	return FALSE;
364
	return FALSE;
360
      bfd_ardata (arch_bfd)->cache = hash_table;
365
      bfd_ardata (arch_bfd)->cache = hash_table;
361
    }
366
    }
362
 
367
 
363
  /* Insert new_elt into the hash table by filepos.  */
368
  /* Insert new_elt into the hash table by filepos.  */
364
  cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
369
  cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
365
  cache->ptr = filepos;
370
  cache->ptr = filepos;
366
  cache->arbfd = new_elt;
371
  cache->arbfd = new_elt;
367
  *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
372
  *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
368
 
373
 
369
  /* Provide a means of accessing this from child.  */
374
  /* Provide a means of accessing this from child.  */
370
  arch_eltdata (new_elt)->parent_cache = hash_table;
375
  arch_eltdata (new_elt)->parent_cache = hash_table;
371
  arch_eltdata (new_elt)->key = filepos;
376
  arch_eltdata (new_elt)->key = filepos;
372
 
377
 
373
  return TRUE;
378
  return TRUE;
374
}
379
}
375

380

376
static bfd *
381
static bfd *
377
_bfd_find_nested_archive (bfd *arch_bfd, const char *filename)
382
open_nested_file (const char *filename, bfd *archive)
378
{
383
{
379
  bfd *abfd;
-
 
380
  const char *target;
384
  const char *target;
-
 
385
  bfd *n_bfd;
-
 
386
 
-
 
387
  target = NULL;
-
 
388
  if (!archive->target_defaulted)
-
 
389
    target = archive->xvec->name;
-
 
390
  n_bfd = bfd_openr (filename, target);
-
 
391
  if (n_bfd != NULL)
-
 
392
    {
-
 
393
      n_bfd->lto_output = archive->lto_output;
-
 
394
      n_bfd->no_export = archive->no_export;
-
 
395
    }
-
 
396
  return n_bfd;
-
 
397
}
-
 
398
 
-
 
399
static bfd *
-
 
400
find_nested_archive (const char *filename, bfd *arch_bfd)
-
 
401
{
-
 
402
  bfd *abfd;
381
 
403
 
382
  /* PR 15140: Don't allow a nested archive pointing to itself.  */
404
  /* PR 15140: Don't allow a nested archive pointing to itself.  */
383
  if (filename_cmp (filename, arch_bfd->filename) == 0)
405
  if (filename_cmp (filename, arch_bfd->filename) == 0)
384
    {
406
    {
385
      bfd_set_error (bfd_error_malformed_archive);
407
      bfd_set_error (bfd_error_malformed_archive);
386
      return NULL;
408
      return NULL;
387
    }
409
    }
388
 
410
 
389
  for (abfd = arch_bfd->nested_archives;
411
  for (abfd = arch_bfd->nested_archives;
390
       abfd != NULL;
412
       abfd != NULL;
391
       abfd = abfd->archive_next)
413
       abfd = abfd->archive_next)
392
    {
414
    {
393
      if (filename_cmp (filename, abfd->filename) == 0)
415
      if (filename_cmp (filename, abfd->filename) == 0)
394
	return abfd;
416
	return abfd;
395
    }
417
    }
396
  target = NULL;
-
 
397
  if (!arch_bfd->target_defaulted)
-
 
398
    target = arch_bfd->xvec->name;
-
 
399
  abfd = bfd_openr (filename, target);
418
  abfd = open_nested_file (filename, arch_bfd);
400
  if (abfd)
419
  if (abfd)
401
    {
420
    {
402
      abfd->archive_next = arch_bfd->nested_archives;
421
      abfd->archive_next = arch_bfd->nested_archives;
403
      arch_bfd->nested_archives = abfd;
422
      arch_bfd->nested_archives = abfd;
404
    }
423
    }
405
  return abfd;
424
  return abfd;
406
}
425
}
407
 
426
 
408
/* The name begins with space.  Hence the rest of the name is an index into
427
/* The name begins with space.  Hence the rest of the name is an index into
409
   the string table.  */
428
   the string table.  */
410
 
429
 
411
static char *
430
static char *
412
get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp)
431
get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp)
413
{
432
{
414
  unsigned long table_index = 0;
433
  unsigned long table_index = 0;
415
  const char *endp;
434
  const char *endp;
416
 
435
 
417
  /* Should extract string so that I can guarantee not to overflow into
436
  /* Should extract string so that I can guarantee not to overflow into
418
     the next region, but I'm too lazy.  */
437
     the next region, but I'm too lazy.  */
419
  errno = 0;
438
  errno = 0;
420
  /* Skip first char, which is '/' in SVR4 or ' ' in some other variants.  */
439
  /* Skip first char, which is '/' in SVR4 or ' ' in some other variants.  */
421
  table_index = strtol (name + 1, (char **) &endp, 10);
440
  table_index = strtol (name + 1, (char **) &endp, 10);
422
  if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
441
  if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
423
    {
442
    {
424
      bfd_set_error (bfd_error_malformed_archive);
443
      bfd_set_error (bfd_error_malformed_archive);
425
      return NULL;
444
      return NULL;
426
    }
445
    }
427
  /* In a thin archive, a member of an archive-within-an-archive
446
  /* In a thin archive, a member of an archive-within-an-archive
428
     will have the offset in the inner archive encoded here.  */
447
     will have the offset in the inner archive encoded here.  */
429
  if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':')
448
  if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':')
430
    {
449
    {
431
      file_ptr origin = strtol (endp + 1, NULL, 10);
450
      file_ptr origin = strtol (endp + 1, NULL, 10);
432
 
451
 
433
      if (errno != 0)
452
      if (errno != 0)
434
	{
453
	{
435
	  bfd_set_error (bfd_error_malformed_archive);
454
	  bfd_set_error (bfd_error_malformed_archive);
436
	  return NULL;
455
	  return NULL;
437
	}
456
	}
438
      *originp = origin;
457
      *originp = origin;
439
    }
458
    }
440
  else
459
  else
441
    *originp = 0;
460
    *originp = 0;
442
 
461
 
443
  return bfd_ardata (arch)->extended_names + table_index;
462
  return bfd_ardata (arch)->extended_names + table_index;
444
}
463
}
445
 
464
 
446
/* This functions reads an arch header and returns an areltdata pointer, or
465
/* This functions reads an arch header and returns an areltdata pointer, or
447
   NULL on error.
466
   NULL on error.
448
 
467
 
449
   Presumes the file pointer is already in the right place (ie pointing
468
   Presumes the file pointer is already in the right place (ie pointing
450
   to the ar_hdr in the file).   Moves the file pointer; on success it
469
   to the ar_hdr in the file).   Moves the file pointer; on success it
451
   should be pointing to the front of the file contents; on failure it
470
   should be pointing to the front of the file contents; on failure it
452
   could have been moved arbitrarily.  */
471
   could have been moved arbitrarily.  */
453
 
472
 
454
void *
473
void *
455
_bfd_generic_read_ar_hdr (bfd *abfd)
474
_bfd_generic_read_ar_hdr (bfd *abfd)
456
{
475
{
457
  return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
476
  return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
458
}
477
}
459
 
478
 
460
/* Alpha ECOFF uses an optional different ARFMAG value, so we have a
479
/* Alpha ECOFF uses an optional different ARFMAG value, so we have a
461
   variant of _bfd_generic_read_ar_hdr which accepts a magic string.  */
480
   variant of _bfd_generic_read_ar_hdr which accepts a magic string.  */
462
 
481
 
463
void *
482
void *
464
_bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag)
483
_bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag)
465
{
484
{
466
  struct ar_hdr hdr;
485
  struct ar_hdr hdr;
467
  char *hdrp = (char *) &hdr;
486
  char *hdrp = (char *) &hdr;
468
  bfd_size_type parsed_size;
487
  bfd_size_type parsed_size;
469
  struct areltdata *ared;
488
  struct areltdata *ared;
470
  char *filename = NULL;
489
  char *filename = NULL;
471
  bfd_size_type namelen = 0;
490
  bfd_size_type namelen = 0;
472
  bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
491
  bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
473
  char *allocptr = 0;
492
  char *allocptr = 0;
474
  file_ptr origin = 0;
493
  file_ptr origin = 0;
475
  unsigned int extra_size = 0;
494
  unsigned int extra_size = 0;
476
  char fmag_save;
495
  char fmag_save;
477
  int scan;
496
  int scan;
478
 
497
 
479
  if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
498
  if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
480
    {
499
    {
481
      if (bfd_get_error () != bfd_error_system_call)
500
      if (bfd_get_error () != bfd_error_system_call)
482
	bfd_set_error (bfd_error_no_more_archived_files);
501
	bfd_set_error (bfd_error_no_more_archived_files);
483
      return NULL;
502
      return NULL;
484
    }
503
    }
485
  if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
504
  if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
486
      && (mag == NULL
505
      && (mag == NULL
487
	  || strncmp (hdr.ar_fmag, mag, 2) != 0))
506
	  || strncmp (hdr.ar_fmag, mag, 2) != 0))
488
    {
507
    {
489
      bfd_set_error (bfd_error_malformed_archive);
508
      bfd_set_error (bfd_error_malformed_archive);
490
      return NULL;
509
      return NULL;
491
    }
510
    }
492
 
511
 
493
  errno = 0;
512
  errno = 0;
494
  fmag_save = hdr.ar_fmag[0];
513
  fmag_save = hdr.ar_fmag[0];
495
  hdr.ar_fmag[0] = 0;
514
  hdr.ar_fmag[0] = 0;
496
  scan = sscanf (hdr.ar_size, "%" BFD_VMA_FMT "u", &parsed_size);
515
  scan = sscanf (hdr.ar_size, "%" BFD_VMA_FMT "u", &parsed_size);
497
  hdr.ar_fmag[0] = fmag_save;
516
  hdr.ar_fmag[0] = fmag_save;
498
  if (scan != 1)
517
  if (scan != 1)
499
    {
518
    {
500
      bfd_set_error (bfd_error_malformed_archive);
519
      bfd_set_error (bfd_error_malformed_archive);
501
      return NULL;
520
      return NULL;
502
    }
521
    }
503
 
522
 
504
  /* Extract the filename from the archive - there are two ways to
523
  /* Extract the filename from the archive - there are two ways to
505
     specify an extended name table, either the first char of the
524
     specify an extended name table, either the first char of the
506
     name is a space, or it's a slash.  */
525
     name is a space, or it's a slash.  */
507
  if ((hdr.ar_name[0] == '/'
526
  if ((hdr.ar_name[0] == '/'
508
       || (hdr.ar_name[0] == ' '
527
       || (hdr.ar_name[0] == ' '
509
	   && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
528
	   && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
510
      && bfd_ardata (abfd)->extended_names != NULL)
529
      && bfd_ardata (abfd)->extended_names != NULL)
511
    {
530
    {
512
      filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin);
531
      filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin);
513
      if (filename == NULL)
532
      if (filename == NULL)
514
	return NULL;
533
	return NULL;
515
    }
534
    }
516
  /* BSD4.4-style long filename.  */
535
  /* BSD4.4-style long filename.  */
517
  else if (is_bsd44_extended_name (hdr.ar_name))
536
  else if (is_bsd44_extended_name (hdr.ar_name))
518
    {
537
    {
519
      /* BSD-4.4 extended name */
538
      /* BSD-4.4 extended name */
520
      namelen = atoi (&hdr.ar_name[3]);
539
      namelen = atoi (&hdr.ar_name[3]);
521
      allocsize += namelen + 1;
540
      allocsize += namelen + 1;
522
      parsed_size -= namelen;
541
      parsed_size -= namelen;
523
      extra_size = namelen;
542
      extra_size = namelen;
524
 
543
 
525
      allocptr = (char *) bfd_zmalloc (allocsize);
544
      allocptr = (char *) bfd_zmalloc (allocsize);
526
      if (allocptr == NULL)
545
      if (allocptr == NULL)
527
	return NULL;
546
	return NULL;
528
      filename = (allocptr
547
      filename = (allocptr
529
		  + sizeof (struct areltdata)
548
		  + sizeof (struct areltdata)
530
		  + sizeof (struct ar_hdr));
549
		  + sizeof (struct ar_hdr));
531
      if (bfd_bread (filename, namelen, abfd) != namelen)
550
      if (bfd_bread (filename, namelen, abfd) != namelen)
532
	{
551
	{
533
	  free (allocptr);
552
	  free (allocptr);
534
	  if (bfd_get_error () != bfd_error_system_call)
553
	  if (bfd_get_error () != bfd_error_system_call)
535
	    bfd_set_error (bfd_error_no_more_archived_files);
554
	    bfd_set_error (bfd_error_no_more_archived_files);
536
	  return NULL;
555
	  return NULL;
537
	}
556
	}
538
      filename[namelen] = '\0';
557
      filename[namelen] = '\0';
539
    }
558
    }
540
  else
559
  else
541
    {
560
    {
542
      /* We judge the end of the name by looking for '/' or ' '.
561
      /* We judge the end of the name by looking for '/' or ' '.
543
	 Note:  The SYSV format (terminated by '/') allows embedded
562
	 Note:  The SYSV format (terminated by '/') allows embedded
544
	 spaces, so only look for ' ' if we don't find '/'.  */
563
	 spaces, so only look for ' ' if we don't find '/'.  */
545
 
564
 
546
      char *e;
565
      char *e;
547
      e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
566
      e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
548
      if (e == NULL)
567
      if (e == NULL)
549
	{
568
	{
550
	  e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
569
	  e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
551
	  if (e == NULL)
570
	  if (e == NULL)
552
	    e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
571
	    e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
553
	}
572
	}
554
 
573
 
555
      if (e != NULL)
574
      if (e != NULL)
556
	namelen = e - hdr.ar_name;
575
	namelen = e - hdr.ar_name;
557
      else
576
      else
558
	{
577
	{
559
	  /* If we didn't find a termination character, then the name
578
	  /* If we didn't find a termination character, then the name
560
	     must be the entire field.  */
579
	     must be the entire field.  */
561
	  namelen = ar_maxnamelen (abfd);
580
	  namelen = ar_maxnamelen (abfd);
562
	}
581
	}
563
 
582
 
564
      allocsize += namelen + 1;
583
      allocsize += namelen + 1;
565
    }
584
    }
566
 
585
 
567
  if (!allocptr)
586
  if (!allocptr)
568
    {
587
    {
569
      allocptr = (char *) bfd_zmalloc (allocsize);
588
      allocptr = (char *) bfd_zmalloc (allocsize);
570
      if (allocptr == NULL)
589
      if (allocptr == NULL)
571
	return NULL;
590
	return NULL;
572
    }
591
    }
573
 
592
 
574
  ared = (struct areltdata *) allocptr;
593
  ared = (struct areltdata *) allocptr;
575
 
594
 
576
  ared->arch_header = allocptr + sizeof (struct areltdata);
595
  ared->arch_header = allocptr + sizeof (struct areltdata);
577
  memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
596
  memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
578
  ared->parsed_size = parsed_size;
597
  ared->parsed_size = parsed_size;
579
  ared->extra_size = extra_size;
598
  ared->extra_size = extra_size;
580
  ared->origin = origin;
599
  ared->origin = origin;
581
 
600
 
582
  if (filename != NULL)
601
  if (filename != NULL)
583
    ared->filename = filename;
602
    ared->filename = filename;
584
  else
603
  else
585
    {
604
    {
586
      ared->filename = allocptr + (sizeof (struct areltdata) +
605
      ared->filename = allocptr + (sizeof (struct areltdata) +
587
				   sizeof (struct ar_hdr));
606
				   sizeof (struct ar_hdr));
588
      if (namelen)
607
      if (namelen)
589
	memcpy (ared->filename, hdr.ar_name, namelen);
608
	memcpy (ared->filename, hdr.ar_name, namelen);
590
      ared->filename[namelen] = '\0';
609
      ared->filename[namelen] = '\0';
591
    }
610
    }
592
 
611
 
593
  return ared;
612
  return ared;
594
}
613
}
595

614

596
/* Append the relative pathname for a member of the thin archive
615
/* Append the relative pathname for a member of the thin archive
597
   to the pathname of the directory containing the archive.  */
616
   to the pathname of the directory containing the archive.  */
598
 
617
 
599
char *
618
char *
600
_bfd_append_relative_path (bfd *arch, char *elt_name)
619
_bfd_append_relative_path (bfd *arch, char *elt_name)
601
{
620
{
602
  const char *arch_name = arch->filename;
621
  const char *arch_name = arch->filename;
603
  const char *base_name = lbasename (arch_name);
622
  const char *base_name = lbasename (arch_name);
604
  size_t prefix_len;
623
  size_t prefix_len;
605
  char *filename;
624
  char *filename;
606
 
625
 
607
  if (base_name == arch_name)
626
  if (base_name == arch_name)
608
    return elt_name;
627
    return elt_name;
609
 
628
 
610
  prefix_len = base_name - arch_name;
629
  prefix_len = base_name - arch_name;
611
  filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1);
630
  filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1);
612
  if (filename == NULL)
631
  if (filename == NULL)
613
    return NULL;
632
    return NULL;
614
 
633
 
615
  strncpy (filename, arch_name, prefix_len);
634
  strncpy (filename, arch_name, prefix_len);
616
  strcpy (filename + prefix_len, elt_name);
635
  strcpy (filename + prefix_len, elt_name);
617
  return filename;
636
  return filename;
618
}
637
}
619
 
638
 
620
/* This is an internal function; it's mainly used when indexing
639
/* This is an internal function; it's mainly used when indexing
621
   through the archive symbol table, but also used to get the next
640
   through the archive symbol table, but also used to get the next
622
   element, since it handles the bookkeeping so nicely for us.  */
641
   element, since it handles the bookkeeping so nicely for us.  */
623
 
642
 
624
bfd *
643
bfd *
625
_bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
644
_bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
626
{
645
{
627
  struct areltdata *new_areldata;
646
  struct areltdata *new_areldata;
628
  bfd *n_nfd;
647
  bfd *n_bfd;
629
  char *filename;
648
  char *filename;
630
 
649
 
631
  n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos);
650
  n_bfd = _bfd_look_for_bfd_in_cache (archive, filepos);
632
  if (n_nfd)
651
  if (n_bfd)
633
    return n_nfd;
652
    return n_bfd;
634
 
653
 
635
  if (0 > bfd_seek (archive, filepos, SEEK_SET))
654
  if (0 > bfd_seek (archive, filepos, SEEK_SET))
636
    return NULL;
655
    return NULL;
637
 
656
 
638
  if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
657
  if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
639
    return NULL;
658
    return NULL;
640
 
659
 
641
  filename = new_areldata->filename;
660
  filename = new_areldata->filename;
642
 
661
 
643
  if (bfd_is_thin_archive (archive))
662
  if (bfd_is_thin_archive (archive))
644
    {
663
    {
645
      const char *target;
-
 
646
 
-
 
647
      /* This is a proxy entry for an external file.  */
664
      /* This is a proxy entry for an external file.  */
648
      if (! IS_ABSOLUTE_PATH (filename))
665
      if (! IS_ABSOLUTE_PATH (filename))
649
	{
666
	{
650
	  filename = _bfd_append_relative_path (archive, filename);
667
	  filename = _bfd_append_relative_path (archive, filename);
651
	  if (filename == NULL)
668
	  if (filename == NULL)
652
	    {
669
	    {
653
	      free (new_areldata);
670
	      free (new_areldata);
654
	      return NULL;
671
	      return NULL;
655
	    }
672
	    }
656
	}
673
	}
657
 
674
 
658
      if (new_areldata->origin > 0)
675
      if (new_areldata->origin > 0)
659
	{
676
	{
660
	  /* This proxy entry refers to an element of a nested archive.
677
	  /* This proxy entry refers to an element of a nested archive.
661
	     Locate the member of that archive and return a bfd for it.  */
678
	     Locate the member of that archive and return a bfd for it.  */
662
	  bfd *ext_arch = _bfd_find_nested_archive (archive, filename);
679
	  bfd *ext_arch = find_nested_archive (filename, archive);
663
 
680
 
664
	  if (ext_arch == NULL
681
	  if (ext_arch == NULL
665
	      || ! bfd_check_format (ext_arch, bfd_archive))
682
	      || ! bfd_check_format (ext_arch, bfd_archive))
666
	    {
683
	    {
667
	      free (new_areldata);
684
	      free (new_areldata);
668
	      return NULL;
685
	      return NULL;
669
	    }
686
	    }
670
	  n_nfd = _bfd_get_elt_at_filepos (ext_arch, new_areldata->origin);
687
	  n_bfd = _bfd_get_elt_at_filepos (ext_arch, new_areldata->origin);
671
	  if (n_nfd == NULL)
688
	  if (n_bfd == NULL)
672
	    {
689
	    {
673
	      free (new_areldata);
690
	      free (new_areldata);
674
	      return NULL;
691
	      return NULL;
675
	    }
692
	    }
676
	  n_nfd->proxy_origin = bfd_tell (archive);
693
	  n_bfd->proxy_origin = bfd_tell (archive);
677
	  return n_nfd;
694
	  return n_bfd;
678
	}
695
	}
-
 
696
 
679
      /* It's not an element of a nested archive;
697
      /* It's not an element of a nested archive;
680
	 open the external file as a bfd.  */
698
	 open the external file as a bfd.  */
681
      target = NULL;
-
 
682
      if (!archive->target_defaulted)
-
 
683
	target = archive->xvec->name;
-
 
684
      n_nfd = bfd_openr (filename, target);
699
      n_bfd = open_nested_file (filename, archive);
685
      if (n_nfd == NULL)
700
      if (n_bfd == NULL)
686
	bfd_set_error (bfd_error_malformed_archive);
701
	bfd_set_error (bfd_error_malformed_archive);
687
    }
702
    }
688
  else
703
  else
689
    {
704
    {
690
      n_nfd = _bfd_create_empty_archive_element_shell (archive);
705
      n_bfd = _bfd_create_empty_archive_element_shell (archive);
691
    }
706
    }
692
 
707
 
693
  if (n_nfd == NULL)
708
  if (n_bfd == NULL)
694
    {
709
    {
695
      free (new_areldata);
710
      free (new_areldata);
696
      return NULL;
711
      return NULL;
697
    }
712
    }
698
 
713
 
699
  n_nfd->proxy_origin = bfd_tell (archive);
714
  n_bfd->proxy_origin = bfd_tell (archive);
700
 
715
 
701
  if (bfd_is_thin_archive (archive))
716
  if (bfd_is_thin_archive (archive))
702
    {
717
    {
703
      n_nfd->origin = 0;
718
      n_bfd->origin = 0;
704
    }
719
    }
705
  else
720
  else
706
    {
721
    {
707
      n_nfd->origin = n_nfd->proxy_origin;
722
      n_bfd->origin = n_bfd->proxy_origin;
708
      n_nfd->filename = filename;
723
      n_bfd->filename = xstrdup (filename);
709
    }
724
    }
710
 
725
 
711
  n_nfd->arelt_data = new_areldata;
726
  n_bfd->arelt_data = new_areldata;
712
 
727
 
-
 
728
  /* Copy BFD_COMPRESS, BFD_DECOMPRESS and BFD_COMPRESS_GABI flags.  */
-
 
729
  n_bfd->flags |= archive->flags & (BFD_COMPRESS
-
 
730
				    | BFD_DECOMPRESS
-
 
731
				    | BFD_COMPRESS_GABI);
-
 
732
 
713
  /* Copy BFD_COMPRESS and BFD_DECOMPRESS flags.  */
733
  /* Copy is_linker_input.  */
714
  n_nfd->flags |= archive->flags & (BFD_COMPRESS | BFD_DECOMPRESS);
734
  n_bfd->is_linker_input = archive->is_linker_input;
715
 
735
 
716
  if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
736
  if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd))
717
    return n_nfd;
737
    return n_bfd;
718
 
738
 
719
  free (new_areldata);
739
  free (new_areldata);
720
  n_nfd->arelt_data = NULL;
740
  n_bfd->arelt_data = NULL;
721
  return NULL;
741
  return NULL;
722
}
742
}
723
 
743
 
724
/* Return the BFD which is referenced by the symbol in ABFD indexed by
744
/* Return the BFD which is referenced by the symbol in ABFD indexed by
725
   SYM_INDEX.  SYM_INDEX should have been returned by bfd_get_next_mapent.  */
745
   SYM_INDEX.  SYM_INDEX should have been returned by bfd_get_next_mapent.  */
726
 
746
 
727
bfd *
747
bfd *
728
_bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index)
748
_bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index)
729
{
749
{
730
  carsym *entry;
750
  carsym *entry;
731
 
751
 
732
  entry = bfd_ardata (abfd)->symdefs + sym_index;
752
  entry = bfd_ardata (abfd)->symdefs + sym_index;
733
  return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
753
  return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
734
}
754
}
735
 
755
 
736
/*
756
/*
737
FUNCTION
757
FUNCTION
738
	bfd_openr_next_archived_file
758
	bfd_openr_next_archived_file
739
 
759
 
740
SYNOPSIS
760
SYNOPSIS
741
	bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
761
	bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
742
 
762
 
743
DESCRIPTION
763
DESCRIPTION
744
	Provided a BFD, @var{archive}, containing an archive and NULL, open
764
	Provided a BFD, @var{archive}, containing an archive and NULL, open
745
	an input BFD on the first contained element and returns that.
765
	an input BFD on the first contained element and returns that.
746
	Subsequent calls should pass
766
	Subsequent calls should pass
747
	the archive and the previous return value to return a created
767
	the archive and the previous return value to return a created
748
	BFD to the next contained element. NULL is returned when there
768
	BFD to the next contained element. NULL is returned when there
749
	are no more.
769
	are no more.
750
*/
770
*/
751
 
771
 
752
bfd *
772
bfd *
753
bfd_openr_next_archived_file (bfd *archive, bfd *last_file)
773
bfd_openr_next_archived_file (bfd *archive, bfd *last_file)
754
{
774
{
755
  if ((bfd_get_format (archive) != bfd_archive)
775
  if ((bfd_get_format (archive) != bfd_archive)
756
      || (archive->direction == write_direction))
776
      || (archive->direction == write_direction))
757
    {
777
    {
758
      bfd_set_error (bfd_error_invalid_operation);
778
      bfd_set_error (bfd_error_invalid_operation);
759
      return NULL;
779
      return NULL;
760
    }
780
    }
761
 
781
 
762
  return BFD_SEND (archive,
782
  return BFD_SEND (archive,
763
		   openr_next_archived_file, (archive, last_file));
783
		   openr_next_archived_file, (archive, last_file));
764
}
784
}
765
 
785
 
766
bfd *
786
bfd *
767
bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
787
bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
768
{
788
{
769
  file_ptr filestart;
789
  ufile_ptr filestart;
770
 
790
 
771
  if (!last_file)
791
  if (!last_file)
772
    filestart = bfd_ardata (archive)->first_file_filepos;
792
    filestart = bfd_ardata (archive)->first_file_filepos;
773
  else
793
  else
774
    {
794
    {
775
      bfd_size_type size = arelt_size (last_file);
-
 
776
 
-
 
777
      filestart = last_file->proxy_origin;
795
      filestart = last_file->proxy_origin;
778
      if (! bfd_is_thin_archive (archive))
796
      if (! bfd_is_thin_archive (archive))
-
 
797
	{
-
 
798
      bfd_size_type size = arelt_size (last_file);
-
 
799
 
779
	filestart += size;
800
	filestart += size;
780
      /* Pad to an even boundary...
801
      /* Pad to an even boundary...
781
	 Note that last_file->origin can be odd in the case of
802
	 Note that last_file->origin can be odd in the case of
782
	 BSD-4.4-style element with a long odd size.  */
803
	 BSD-4.4-style element with a long odd size.  */
783
      filestart += filestart % 2;
804
      filestart += filestart % 2;
-
 
805
	  if (filestart <= last_file->proxy_origin)
-
 
806
	    {
-
 
807
	      /* Prevent looping.  See PR19256.  */
-
 
808
	      bfd_set_error (bfd_error_malformed_archive);
-
 
809
	      return NULL;
-
 
810
	    }
-
 
811
	}
784
    }
812
    }
785
 
813
 
786
  return _bfd_get_elt_at_filepos (archive, filestart);
814
  return _bfd_get_elt_at_filepos (archive, filestart);
787
}
815
}
788
 
816
 
789
const bfd_target *
817
const bfd_target *
790
bfd_generic_archive_p (bfd *abfd)
818
bfd_generic_archive_p (bfd *abfd)
791
{
819
{
792
  struct artdata *tdata_hold;
820
  struct artdata *tdata_hold;
793
  char armag[SARMAG + 1];
821
  char armag[SARMAG + 1];
794
  bfd_size_type amt;
822
  bfd_size_type amt;
795
 
823
 
796
  if (bfd_bread (armag, SARMAG, abfd) != SARMAG)
824
  if (bfd_bread (armag, SARMAG, abfd) != SARMAG)
797
    {
825
    {
798
      if (bfd_get_error () != bfd_error_system_call)
826
      if (bfd_get_error () != bfd_error_system_call)
799
	bfd_set_error (bfd_error_wrong_format);
827
	bfd_set_error (bfd_error_wrong_format);
800
      return NULL;
828
      return NULL;
801
    }
829
    }
802
 
830
 
803
  bfd_is_thin_archive (abfd) = (strncmp (armag, ARMAGT, SARMAG) == 0);
831
  bfd_is_thin_archive (abfd) = (strncmp (armag, ARMAGT, SARMAG) == 0);
804
 
832
 
805
  if (strncmp (armag, ARMAG, SARMAG) != 0
833
  if (strncmp (armag, ARMAG, SARMAG) != 0
806
      && strncmp (armag, ARMAGB, SARMAG) != 0
834
      && strncmp (armag, ARMAGB, SARMAG) != 0
807
      && ! bfd_is_thin_archive (abfd))
835
      && ! bfd_is_thin_archive (abfd))
808
    return NULL;
836
    return NULL;
809
 
837
 
810
  tdata_hold = bfd_ardata (abfd);
838
  tdata_hold = bfd_ardata (abfd);
811
 
839
 
812
  amt = sizeof (struct artdata);
840
  amt = sizeof (struct artdata);
813
  bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
841
  bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
814
  if (bfd_ardata (abfd) == NULL)
842
  if (bfd_ardata (abfd) == NULL)
815
    {
843
    {
816
      bfd_ardata (abfd) = tdata_hold;
844
      bfd_ardata (abfd) = tdata_hold;
817
      return NULL;
845
      return NULL;
818
    }
846
    }
819
 
847
 
820
  bfd_ardata (abfd)->first_file_filepos = SARMAG;
848
  bfd_ardata (abfd)->first_file_filepos = SARMAG;
821
  /* Cleared by bfd_zalloc above.
849
  /* Cleared by bfd_zalloc above.
822
     bfd_ardata (abfd)->cache = NULL;
850
     bfd_ardata (abfd)->cache = NULL;
823
     bfd_ardata (abfd)->archive_head = NULL;
851
     bfd_ardata (abfd)->archive_head = NULL;
824
     bfd_ardata (abfd)->symdefs = NULL;
852
     bfd_ardata (abfd)->symdefs = NULL;
825
     bfd_ardata (abfd)->extended_names = NULL;
853
     bfd_ardata (abfd)->extended_names = NULL;
826
     bfd_ardata (abfd)->extended_names_size = 0;
854
     bfd_ardata (abfd)->extended_names_size = 0;
827
     bfd_ardata (abfd)->tdata = NULL;  */
855
     bfd_ardata (abfd)->tdata = NULL;  */
828
 
856
 
829
  if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
857
  if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
830
      || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
858
      || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
831
    {
859
    {
832
      if (bfd_get_error () != bfd_error_system_call)
860
      if (bfd_get_error () != bfd_error_system_call)
833
	bfd_set_error (bfd_error_wrong_format);
861
	bfd_set_error (bfd_error_wrong_format);
834
      bfd_release (abfd, bfd_ardata (abfd));
862
      bfd_release (abfd, bfd_ardata (abfd));
835
      bfd_ardata (abfd) = tdata_hold;
863
      bfd_ardata (abfd) = tdata_hold;
836
      return NULL;
864
      return NULL;
837
    }
865
    }
838
 
866
 
839
  if (abfd->target_defaulted && bfd_has_map (abfd))
867
  if (abfd->target_defaulted && bfd_has_map (abfd))
840
    {
868
    {
841
      bfd *first;
869
      bfd *first;
842
 
870
 
843
      /* This archive has a map, so we may presume that the contents
871
      /* This archive has a map, so we may presume that the contents
844
	 are object files.  Make sure that if the first file in the
872
	 are object files.  Make sure that if the first file in the
845
	 archive can be recognized as an object file, it is for this
873
	 archive can be recognized as an object file, it is for this
846
	 target.  If not, assume that this is the wrong format.  If
874
	 target.  If not, assume that this is the wrong format.  If
847
	 the first file is not an object file, somebody is doing
875
	 the first file is not an object file, somebody is doing
848
	 something weird, and we permit it so that ar -t will work.
876
	 something weird, and we permit it so that ar -t will work.
849
 
877
 
850
	 This is done because any normal format will recognize any
878
	 This is done because any normal format will recognize any
851
	 normal archive, regardless of the format of the object files.
879
	 normal archive, regardless of the format of the object files.
852
	 We do accept an empty archive.  */
880
	 We do accept an empty archive.  */
853
 
881
 
854
      first = bfd_openr_next_archived_file (abfd, NULL);
882
      first = bfd_openr_next_archived_file (abfd, NULL);
855
      if (first != NULL)
883
      if (first != NULL)
856
	{
884
	{
857
	  first->target_defaulted = FALSE;
885
	  first->target_defaulted = FALSE;
858
	  if (bfd_check_format (first, bfd_object)
886
	  if (bfd_check_format (first, bfd_object)
859
	      && first->xvec != abfd->xvec)
887
	      && first->xvec != abfd->xvec)
860
	    bfd_set_error (bfd_error_wrong_object_format);
888
	    bfd_set_error (bfd_error_wrong_object_format);
861
	  /* And we ought to close `first' here too.  */
889
	  /* And we ought to close `first' here too.  */
862
	}
890
	}
863
    }
891
    }
864
 
892
 
865
  return abfd->xvec;
893
  return abfd->xvec;
866
}
894
}
867
 
895
 
868
/* Some constants for a 32 bit BSD archive structure.  We do not
896
/* Some constants for a 32 bit BSD archive structure.  We do not
869
   support 64 bit archives presently; so far as I know, none actually
897
   support 64 bit archives presently; so far as I know, none actually
870
   exist.  Supporting them would require changing these constants, and
898
   exist.  Supporting them would require changing these constants, and
871
   changing some H_GET_32 to H_GET_64.  */
899
   changing some H_GET_32 to H_GET_64.  */
872
 
900
 
873
/* The size of an external symdef structure.  */
901
/* The size of an external symdef structure.  */
874
#define BSD_SYMDEF_SIZE 8
902
#define BSD_SYMDEF_SIZE 8
875
 
903
 
876
/* The offset from the start of a symdef structure to the file offset.  */
904
/* The offset from the start of a symdef structure to the file offset.  */
877
#define BSD_SYMDEF_OFFSET_SIZE 4
905
#define BSD_SYMDEF_OFFSET_SIZE 4
878
 
906
 
879
/* The size of the symdef count.  */
907
/* The size of the symdef count.  */
880
#define BSD_SYMDEF_COUNT_SIZE 4
908
#define BSD_SYMDEF_COUNT_SIZE 4
881
 
909
 
882
/* The size of the string count.  */
910
/* The size of the string count.  */
883
#define BSD_STRING_COUNT_SIZE 4
911
#define BSD_STRING_COUNT_SIZE 4
884
 
912
 
885
/* Read a BSD-style archive symbol table.  Returns FALSE on error,
913
/* Read a BSD-style archive symbol table.  Returns FALSE on error,
886
   TRUE otherwise.  */
914
   TRUE otherwise.  */
887
 
915
 
888
static bfd_boolean
916
static bfd_boolean
889
do_slurp_bsd_armap (bfd *abfd)
917
do_slurp_bsd_armap (bfd *abfd)
890
{
918
{
891
  struct areltdata *mapdata;
919
  struct areltdata *mapdata;
892
  unsigned int counter;
920
  unsigned int counter;
893
  bfd_byte *raw_armap, *rbase;
921
  bfd_byte *raw_armap, *rbase;
894
  struct artdata *ardata = bfd_ardata (abfd);
922
  struct artdata *ardata = bfd_ardata (abfd);
895
  char *stringbase;
923
  char *stringbase;
896
  bfd_size_type parsed_size, amt;
924
  bfd_size_type parsed_size, amt;
897
  carsym *set;
925
  carsym *set;
898
 
926
 
899
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
927
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
900
  if (mapdata == NULL)
928
  if (mapdata == NULL)
901
    return FALSE;
929
    return FALSE;
902
  parsed_size = mapdata->parsed_size;
930
  parsed_size = mapdata->parsed_size;
903
  free (mapdata);
931
  free (mapdata);
-
 
932
  /* PR 17512: file: 883ff754.  */
-
 
933
  /* PR 17512: file: 0458885f.  */
-
 
934
  if (parsed_size < 4)
-
 
935
    return FALSE;
904
 
936
 
905
  raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size);
937
  raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size);
906
  if (raw_armap == NULL)
938
  if (raw_armap == NULL)
907
    return FALSE;
939
    return FALSE;
908
 
940
 
909
  if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
941
  if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
910
    {
942
    {
911
      if (bfd_get_error () != bfd_error_system_call)
943
      if (bfd_get_error () != bfd_error_system_call)
912
	bfd_set_error (bfd_error_malformed_archive);
944
	bfd_set_error (bfd_error_malformed_archive);
913
    byebye:
945
    byebye:
914
      bfd_release (abfd, raw_armap);
946
      bfd_release (abfd, raw_armap);
915
      return FALSE;
947
      return FALSE;
916
    }
948
    }
917
 
949
 
918
  ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
950
  ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
919
 
-
 
920
  if (ardata->symdef_count * BSD_SYMDEF_SIZE >
951
  if (ardata->symdef_count * BSD_SYMDEF_SIZE >
921
      parsed_size - BSD_SYMDEF_COUNT_SIZE)
952
      parsed_size - BSD_SYMDEF_COUNT_SIZE)
922
    {
953
    {
923
      /* Probably we're using the wrong byte ordering.  */
954
      /* Probably we're using the wrong byte ordering.  */
924
      bfd_set_error (bfd_error_wrong_format);
955
      bfd_set_error (bfd_error_wrong_format);
925
      goto byebye;
956
      goto byebye;
926
    }
957
    }
927
 
958
 
928
  ardata->cache = 0;
959
  ardata->cache = 0;
929
  rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
960
  rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
930
  stringbase = ((char *) rbase
961
  stringbase = ((char *) rbase
931
		+ ardata->symdef_count * BSD_SYMDEF_SIZE
962
		+ ardata->symdef_count * BSD_SYMDEF_SIZE
932
		+ BSD_STRING_COUNT_SIZE);
963
		+ BSD_STRING_COUNT_SIZE);
933
  amt = ardata->symdef_count * sizeof (carsym);
964
  amt = ardata->symdef_count * sizeof (carsym);
934
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
965
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
935
  if (!ardata->symdefs)
966
  if (!ardata->symdefs)
936
    return FALSE;
967
    return FALSE;
937
 
968
 
938
  for (counter = 0, set = ardata->symdefs;
969
  for (counter = 0, set = ardata->symdefs;
939
       counter < ardata->symdef_count;
970
       counter < ardata->symdef_count;
940
       counter++, set++, rbase += BSD_SYMDEF_SIZE)
971
       counter++, set++, rbase += BSD_SYMDEF_SIZE)
941
    {
972
    {
942
      set->name = H_GET_32 (abfd, rbase) + stringbase;
973
      set->name = H_GET_32 (abfd, rbase) + stringbase;
943
      set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
974
      set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
944
    }
975
    }
945
 
976
 
946
  ardata->first_file_filepos = bfd_tell (abfd);
977
  ardata->first_file_filepos = bfd_tell (abfd);
947
  /* Pad to an even boundary if you have to.  */
978
  /* Pad to an even boundary if you have to.  */
948
  ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
979
  ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
949
  /* FIXME, we should provide some way to free raw_ardata when
980
  /* FIXME, we should provide some way to free raw_ardata when
950
     we are done using the strings from it.  For now, it seems
981
     we are done using the strings from it.  For now, it seems
951
     to be allocated on an objalloc anyway...  */
982
     to be allocated on an objalloc anyway...  */
952
  bfd_has_map (abfd) = TRUE;
983
  bfd_has_map (abfd) = TRUE;
953
  return TRUE;
984
  return TRUE;
954
}
985
}
955
 
986
 
956
/* Read a COFF archive symbol table.  Returns FALSE on error, TRUE
987
/* Read a COFF archive symbol table.  Returns FALSE on error, TRUE
957
   otherwise.  */
988
   otherwise.  */
958
 
989
 
959
static bfd_boolean
990
static bfd_boolean
960
do_slurp_coff_armap (bfd *abfd)
991
do_slurp_coff_armap (bfd *abfd)
961
{
992
{
962
  struct areltdata *mapdata;
993
  struct areltdata *mapdata;
963
  int *raw_armap, *rawptr;
994
  int *raw_armap, *rawptr;
964
  struct artdata *ardata = bfd_ardata (abfd);
995
  struct artdata *ardata = bfd_ardata (abfd);
965
  char *stringbase;
996
  char *stringbase;
966
  bfd_size_type stringsize;
997
  bfd_size_type stringsize;
967
  bfd_size_type parsed_size;
998
  bfd_size_type parsed_size;
968
  carsym *carsyms;
999
  carsym *carsyms;
969
  bfd_size_type nsymz;		/* Number of symbols in armap.  */
1000
  bfd_size_type nsymz;		/* Number of symbols in armap.  */
970
  bfd_vma (*swap) (const void *);
1001
  bfd_vma (*swap) (const void *);
971
  char int_buf[sizeof (long)];
1002
  char int_buf[sizeof (long)];
972
  bfd_size_type carsym_size, ptrsize;
1003
  bfd_size_type carsym_size, ptrsize;
973
  unsigned int i;
1004
  unsigned int i;
974
 
1005
 
975
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1006
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
976
  if (mapdata == NULL)
1007
  if (mapdata == NULL)
977
    return FALSE;
1008
    return FALSE;
978
  parsed_size = mapdata->parsed_size;
1009
  parsed_size = mapdata->parsed_size;
979
  free (mapdata);
1010
  free (mapdata);
980
 
1011
 
981
  if (bfd_bread (int_buf, 4, abfd) != 4)
1012
  if (bfd_bread (int_buf, 4, abfd) != 4)
982
    {
1013
    {
983
      if (bfd_get_error () != bfd_error_system_call)
1014
      if (bfd_get_error () != bfd_error_system_call)
984
	bfd_set_error (bfd_error_malformed_archive);
1015
	bfd_set_error (bfd_error_malformed_archive);
985
      return FALSE;
1016
      return FALSE;
986
    }
1017
    }
987
  /* It seems that all numeric information in a coff archive is always
1018
  /* It seems that all numeric information in a coff archive is always
988
     in big endian format, nomatter the host or target.  */
1019
     in big endian format, nomatter the host or target.  */
989
  swap = bfd_getb32;
1020
  swap = bfd_getb32;
990
  nsymz = bfd_getb32 (int_buf);
1021
  nsymz = bfd_getb32 (int_buf);
991
  stringsize = parsed_size - (4 * nsymz) - 4;
1022
  stringsize = parsed_size - (4 * nsymz) - 4;
992
 
1023
 
993
  /* ... except that some archive formats are broken, and it may be our
1024
  /* ... except that some archive formats are broken, and it may be our
994
     fault - the i960 little endian coff sometimes has big and sometimes
1025
     fault - the i960 little endian coff sometimes has big and sometimes
995
     little, because our tools changed.  Here's a horrible hack to clean
1026
     little, because our tools changed.  Here's a horrible hack to clean
996
     up the crap.  */
1027
     up the crap.  */
997
 
1028
 
998
  if (stringsize > 0xfffff
1029
  if (stringsize > 0xfffff
999
      && bfd_get_arch (abfd) == bfd_arch_i960
1030
      && bfd_get_arch (abfd) == bfd_arch_i960
1000
      && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1031
      && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1001
    {
1032
    {
1002
      /* This looks dangerous, let's do it the other way around.  */
1033
      /* This looks dangerous, let's do it the other way around.  */
1003
      nsymz = bfd_getl32 (int_buf);
1034
      nsymz = bfd_getl32 (int_buf);
1004
      stringsize = parsed_size - (4 * nsymz) - 4;
1035
      stringsize = parsed_size - (4 * nsymz) - 4;
1005
      swap = bfd_getl32;
1036
      swap = bfd_getl32;
1006
    }
1037
    }
1007
 
1038
 
1008
  /* The coff armap must be read sequentially.  So we construct a
1039
  /* The coff armap must be read sequentially.  So we construct a
1009
     bsd-style one in core all at once, for simplicity.  */
1040
     bsd-style one in core all at once, for simplicity.  */
1010
 
1041
 
1011
  if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
1042
  if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
1012
    return FALSE;
1043
    return FALSE;
1013
 
1044
 
1014
  carsym_size = (nsymz * sizeof (carsym));
1045
  carsym_size = (nsymz * sizeof (carsym));
1015
  ptrsize = (4 * nsymz);
1046
  ptrsize = (4 * nsymz);
1016
 
1047
 
1017
  if (carsym_size + stringsize + 1 <= carsym_size)
1048
  if (carsym_size + stringsize + 1 <= carsym_size)
1018
    return FALSE;
1049
    return FALSE;
1019
 
1050
 
1020
  ardata->symdefs = (struct carsym *) bfd_zalloc (abfd,
1051
  ardata->symdefs = (struct carsym *) bfd_zalloc (abfd,
1021
						  carsym_size + stringsize + 1);
1052
						  carsym_size + stringsize + 1);
1022
  if (ardata->symdefs == NULL)
1053
  if (ardata->symdefs == NULL)
1023
    return FALSE;
1054
    return FALSE;
1024
  carsyms = ardata->symdefs;
1055
  carsyms = ardata->symdefs;
1025
  stringbase = ((char *) ardata->symdefs) + carsym_size;
1056
  stringbase = ((char *) ardata->symdefs) + carsym_size;
1026
 
1057
 
1027
  /* Allocate and read in the raw offsets.  */
1058
  /* Allocate and read in the raw offsets.  */
1028
  raw_armap = (int *) bfd_alloc (abfd, ptrsize);
1059
  raw_armap = (int *) bfd_alloc (abfd, ptrsize);
1029
  if (raw_armap == NULL)
1060
  if (raw_armap == NULL)
1030
    goto release_symdefs;
1061
    goto release_symdefs;
1031
  if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
1062
  if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
1032
      || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
1063
      || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
1033
    {
1064
    {
1034
      if (bfd_get_error () != bfd_error_system_call)
1065
      if (bfd_get_error () != bfd_error_system_call)
1035
	bfd_set_error (bfd_error_malformed_archive);
1066
	bfd_set_error (bfd_error_malformed_archive);
1036
      goto release_raw_armap;
1067
      goto release_raw_armap;
1037
    }
1068
    }
1038
 
1069
 
1039
  /* OK, build the carsyms.  */
1070
  /* OK, build the carsyms.  */
1040
  for (i = 0; i < nsymz; i++)
1071
  for (i = 0; i < nsymz && stringsize > 0; i++)
1041
    {
1072
    {
-
 
1073
      bfd_size_type len;
-
 
1074
 
1042
      rawptr = raw_armap + i;
1075
      rawptr = raw_armap + i;
1043
      carsyms->file_offset = swap ((bfd_byte *) rawptr);
1076
      carsyms->file_offset = swap ((bfd_byte *) rawptr);
1044
      carsyms->name = stringbase;
1077
      carsyms->name = stringbase;
-
 
1078
      /* PR 17512: file: 4a1d50c1.  */
1045
      stringbase += strlen (stringbase) + 1;
1079
      len = strnlen (stringbase, stringsize);
-
 
1080
      if (len < stringsize)
-
 
1081
	len ++;
-
 
1082
      stringbase += len;
-
 
1083
      stringsize -= len;
1046
      carsyms++;
1084
      carsyms++;
1047
    }
1085
    }
1048
  *stringbase = 0;
1086
  *stringbase = 0;
1049
 
1087
 
1050
  ardata->symdef_count = nsymz;
1088
  ardata->symdef_count = nsymz;
1051
  ardata->first_file_filepos = bfd_tell (abfd);
1089
  ardata->first_file_filepos = bfd_tell (abfd);
1052
  /* Pad to an even boundary if you have to.  */
1090
  /* Pad to an even boundary if you have to.  */
1053
  ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1091
  ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1054
 
1092
 
1055
  bfd_has_map (abfd) = TRUE;
1093
  bfd_has_map (abfd) = TRUE;
1056
  bfd_release (abfd, raw_armap);
1094
  bfd_release (abfd, raw_armap);
1057
 
1095
 
1058
  /* Check for a second archive header (as used by PE).  */
1096
  /* Check for a second archive header (as used by PE).  */
1059
  {
1097
  {
1060
    struct areltdata *tmp;
1098
    struct areltdata *tmp;
1061
 
1099
 
1062
    bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
1100
    bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
1063
    tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1101
    tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1064
    if (tmp != NULL)
1102
    if (tmp != NULL)
1065
      {
1103
      {
1066
	if (tmp->arch_header[0] == '/'
1104
	if (tmp->arch_header[0] == '/'
1067
	    && tmp->arch_header[1] == ' ')
1105
	    && tmp->arch_header[1] == ' ')
1068
	  {
1106
	  {
1069
	    ardata->first_file_filepos +=
1107
	    ardata->first_file_filepos +=
1070
	      (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
1108
	      (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
1071
	  }
1109
	  }
1072
	free (tmp);
1110
	free (tmp);
1073
      }
1111
      }
1074
  }
1112
  }
1075
 
1113
 
1076
  return TRUE;
1114
  return TRUE;
1077
 
1115
 
1078
release_raw_armap:
1116
release_raw_armap:
1079
  bfd_release (abfd, raw_armap);
1117
  bfd_release (abfd, raw_armap);
1080
release_symdefs:
1118
release_symdefs:
1081
  bfd_release (abfd, (ardata)->symdefs);
1119
  bfd_release (abfd, (ardata)->symdefs);
1082
  return FALSE;
1120
  return FALSE;
1083
}
1121
}
1084
 
1122
 
1085
/* This routine can handle either coff-style or bsd-style armaps
1123
/* This routine can handle either coff-style or bsd-style armaps
1086
   (archive symbol table).  Returns FALSE on error, TRUE otherwise */
1124
   (archive symbol table).  Returns FALSE on error, TRUE otherwise */
1087
 
1125
 
1088
bfd_boolean
1126
bfd_boolean
1089
bfd_slurp_armap (bfd *abfd)
1127
bfd_slurp_armap (bfd *abfd)
1090
{
1128
{
1091
  char nextname[17];
1129
  char nextname[17];
1092
  int i = bfd_bread (nextname, 16, abfd);
1130
  int i = bfd_bread (nextname, 16, abfd);
1093
 
1131
 
1094
  if (i == 0)
1132
  if (i == 0)
1095
    return TRUE;
1133
    return TRUE;
1096
  if (i != 16)
1134
  if (i != 16)
1097
    return FALSE;
1135
    return FALSE;
1098
 
1136
 
1099
  if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1137
  if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1100
    return FALSE;
1138
    return FALSE;
1101
 
1139
 
1102
  if (CONST_STRNEQ (nextname, "__.SYMDEF       ")
1140
  if (CONST_STRNEQ (nextname, "__.SYMDEF       ")
1103
      || CONST_STRNEQ (nextname, "__.SYMDEF/      ")) /* Old Linux archives.  */
1141
      || CONST_STRNEQ (nextname, "__.SYMDEF/      ")) /* Old Linux archives.  */
1104
    return do_slurp_bsd_armap (abfd);
1142
    return do_slurp_bsd_armap (abfd);
1105
  else if (CONST_STRNEQ (nextname, "/               "))
1143
  else if (CONST_STRNEQ (nextname, "/               "))
1106
    return do_slurp_coff_armap (abfd);
1144
    return do_slurp_coff_armap (abfd);
1107
  else if (CONST_STRNEQ (nextname, "/SYM64/         "))
1145
  else if (CONST_STRNEQ (nextname, "/SYM64/         "))
1108
    {
1146
    {
1109
      /* 64bit ELF (Irix 6) archive.  */
1147
      /* 64bit ELF (Irix 6) archive.  */
1110
#ifdef BFD64
1148
#ifdef BFD64
1111
      extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
1149
      extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
1112
      return bfd_elf64_archive_slurp_armap (abfd);
1150
      return bfd_elf64_archive_slurp_armap (abfd);
1113
#else
1151
#else
1114
      bfd_set_error (bfd_error_wrong_format);
1152
      bfd_set_error (bfd_error_wrong_format);
1115
      return FALSE;
1153
      return FALSE;
1116
#endif
1154
#endif
1117
    }
1155
    }
1118
  else if (CONST_STRNEQ (nextname, "#1/20           "))
1156
  else if (CONST_STRNEQ (nextname, "#1/20           "))
1119
    {
1157
    {
1120
      /* Mach-O has a special name for armap when the map is sorted by name.
1158
      /* Mach-O has a special name for armap when the map is sorted by name.
1121
	 However because this name has a space it is slightly more difficult
1159
	 However because this name has a space it is slightly more difficult
1122
	 to check it.  */
1160
	 to check it.  */
1123
      struct ar_hdr hdr;
1161
      struct ar_hdr hdr;
1124
      char extname[21];
1162
      char extname[21];
1125
 
1163
 
1126
      if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1164
      if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1127
	return FALSE;
1165
	return FALSE;
1128
      /* Read the extended name.  We know its length.  */
1166
      /* Read the extended name.  We know its length.  */
1129
      if (bfd_bread (extname, 20, abfd) != 20)
1167
      if (bfd_bread (extname, 20, abfd) != 20)
1130
	return FALSE;
1168
	return FALSE;
1131
      if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0)
1169
      if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0)
1132
	return FALSE;
1170
	return FALSE;
-
 
1171
      extname[20] = 0;
1133
      if (CONST_STRNEQ (extname, "__.SYMDEF SORTED")
1172
      if (CONST_STRNEQ (extname, "__.SYMDEF SORTED")
1134
	  || CONST_STRNEQ (extname, "__.SYMDEF"))
1173
	  || CONST_STRNEQ (extname, "__.SYMDEF"))
1135
	return do_slurp_bsd_armap (abfd);
1174
	return do_slurp_bsd_armap (abfd);
1136
    }
1175
    }
1137
 
1176
 
1138
  bfd_has_map (abfd) = FALSE;
1177
  bfd_has_map (abfd) = FALSE;
1139
  return TRUE;
1178
  return TRUE;
1140
}
1179
}
1141

1180

1142
/* Returns FALSE on error, TRUE otherwise.  */
1181
/* Returns FALSE on error, TRUE otherwise.  */
1143
/* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
1182
/* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
1144
   header is in a slightly different order and the map name is '/'.
1183
   header is in a slightly different order and the map name is '/'.
1145
   This flavour is used by hp300hpux.  */
1184
   This flavour is used by hp300hpux.  */
1146
 
1185
 
1147
#define HPUX_SYMDEF_COUNT_SIZE 2
1186
#define HPUX_SYMDEF_COUNT_SIZE 2
1148
 
1187
 
1149
bfd_boolean
1188
bfd_boolean
1150
bfd_slurp_bsd_armap_f2 (bfd *abfd)
1189
bfd_slurp_bsd_armap_f2 (bfd *abfd)
1151
{
1190
{
1152
  struct areltdata *mapdata;
1191
  struct areltdata *mapdata;
1153
  char nextname[17];
1192
  char nextname[17];
1154
  unsigned int counter;
1193
  unsigned int counter;
1155
  bfd_byte *raw_armap, *rbase;
1194
  bfd_byte *raw_armap, *rbase;
1156
  struct artdata *ardata = bfd_ardata (abfd);
1195
  struct artdata *ardata = bfd_ardata (abfd);
1157
  char *stringbase;
1196
  char *stringbase;
1158
  unsigned int stringsize;
1197
  unsigned int stringsize;
1159
  unsigned int left;
1198
  unsigned int left;
1160
  bfd_size_type amt;
1199
  bfd_size_type amt;
1161
  carsym *set;
1200
  carsym *set;
1162
  int i = bfd_bread (nextname, 16, abfd);
1201
  int i = bfd_bread (nextname, 16, abfd);
1163
 
1202
 
1164
  if (i == 0)
1203
  if (i == 0)
1165
    return TRUE;
1204
    return TRUE;
1166
  if (i != 16)
1205
  if (i != 16)
1167
    return FALSE;
1206
    return FALSE;
1168
 
1207
 
1169
  /* The archive has at least 16 bytes in it.  */
1208
  /* The archive has at least 16 bytes in it.  */
1170
  if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1209
  if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1171
    return FALSE;
1210
    return FALSE;
1172
 
1211
 
1173
  if (CONST_STRNEQ (nextname, "__.SYMDEF       ")
1212
  if (CONST_STRNEQ (nextname, "__.SYMDEF       ")
1174
      || CONST_STRNEQ (nextname, "__.SYMDEF/      ")) /* Old Linux archives.  */
1213
      || CONST_STRNEQ (nextname, "__.SYMDEF/      ")) /* Old Linux archives.  */
1175
    return do_slurp_bsd_armap (abfd);
1214
    return do_slurp_bsd_armap (abfd);
1176
 
1215
 
1177
  if (! CONST_STRNEQ (nextname, "/               "))
1216
  if (! CONST_STRNEQ (nextname, "/               "))
1178
    {
1217
    {
1179
      bfd_has_map (abfd) = FALSE;
1218
      bfd_has_map (abfd) = FALSE;
1180
      return TRUE;
1219
      return TRUE;
1181
    }
1220
    }
1182
 
1221
 
1183
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1222
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1184
  if (mapdata == NULL)
1223
  if (mapdata == NULL)
1185
    return FALSE;
1224
    return FALSE;
1186
 
1225
 
1187
  if (mapdata->parsed_size < HPUX_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE)
1226
  if (mapdata->parsed_size < HPUX_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE)
1188
    {
1227
    {
1189
      free (mapdata);
1228
      free (mapdata);
1190
    wrong_format:
1229
    wrong_format:
1191
      bfd_set_error (bfd_error_wrong_format);
1230
      bfd_set_error (bfd_error_wrong_format);
1192
    byebye:
1231
    byebye:
1193
      return FALSE;
1232
      return FALSE;
1194
    }
1233
    }
1195
  left = mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE - BSD_STRING_COUNT_SIZE;
1234
  left = mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE - BSD_STRING_COUNT_SIZE;
1196
 
1235
 
1197
  amt = mapdata->parsed_size;
1236
  amt = mapdata->parsed_size;
1198
  free (mapdata);
1237
  free (mapdata);
1199
 
1238
 
1200
  raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt);
1239
  raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt);
1201
  if (raw_armap == NULL)
1240
  if (raw_armap == NULL)
1202
    goto byebye;
1241
    goto byebye;
1203
 
1242
 
1204
  if (bfd_bread (raw_armap, amt, abfd) != amt)
1243
  if (bfd_bread (raw_armap, amt, abfd) != amt)
1205
    {
1244
    {
1206
      if (bfd_get_error () != bfd_error_system_call)
1245
      if (bfd_get_error () != bfd_error_system_call)
1207
	bfd_set_error (bfd_error_malformed_archive);
1246
	bfd_set_error (bfd_error_malformed_archive);
1208
      goto byebye;
1247
      goto byebye;
1209
    }
1248
    }
1210
 
1249
 
1211
  ardata->symdef_count = H_GET_16 (abfd, raw_armap);
1250
  ardata->symdef_count = H_GET_16 (abfd, raw_armap);
1212
 
1251
 
1213
  ardata->cache = 0;
1252
  ardata->cache = 0;
1214
 
1253
 
1215
  stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
1254
  stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
1216
  if (stringsize > left)
1255
  if (stringsize > left)
1217
    goto wrong_format;
1256
    goto wrong_format;
1218
  left -= stringsize;
1257
  left -= stringsize;
1219
 
1258
 
1220
  /* Skip sym count and string sz.  */
1259
  /* Skip sym count and string sz.  */
1221
  stringbase = ((char *) raw_armap
1260
  stringbase = ((char *) raw_armap
1222
		+ HPUX_SYMDEF_COUNT_SIZE
1261
		+ HPUX_SYMDEF_COUNT_SIZE
1223
		+ BSD_STRING_COUNT_SIZE);
1262
		+ BSD_STRING_COUNT_SIZE);
1224
  rbase = (bfd_byte *) stringbase + stringsize;
1263
  rbase = (bfd_byte *) stringbase + stringsize;
1225
  amt = ardata->symdef_count * BSD_SYMDEF_SIZE;
1264
  amt = ardata->symdef_count * BSD_SYMDEF_SIZE;
1226
  if (amt > left)
1265
  if (amt > left)
1227
    goto wrong_format;
1266
    goto wrong_format;
1228
 
1267
 
1229
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
1268
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
1230
  if (!ardata->symdefs)
1269
  if (!ardata->symdefs)
1231
    return FALSE;
1270
    return FALSE;
1232
 
1271
 
1233
  for (counter = 0, set = ardata->symdefs;
1272
  for (counter = 0, set = ardata->symdefs;
1234
       counter < ardata->symdef_count;
1273
       counter < ardata->symdef_count;
1235
       counter++, set++, rbase += BSD_SYMDEF_SIZE)
1274
       counter++, set++, rbase += BSD_SYMDEF_SIZE)
1236
    {
1275
    {
1237
      set->name = H_GET_32 (abfd, rbase) + stringbase;
1276
      set->name = H_GET_32 (abfd, rbase) + stringbase;
1238
      set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
1277
      set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
1239
    }
1278
    }
1240
 
1279
 
1241
  ardata->first_file_filepos = bfd_tell (abfd);
1280
  ardata->first_file_filepos = bfd_tell (abfd);
1242
  /* Pad to an even boundary if you have to.  */
1281
  /* Pad to an even boundary if you have to.  */
1243
  ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1282
  ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1244
  /* FIXME, we should provide some way to free raw_ardata when
1283
  /* FIXME, we should provide some way to free raw_ardata when
1245
     we are done using the strings from it.  For now, it seems
1284
     we are done using the strings from it.  For now, it seems
1246
     to be allocated on an objalloc anyway...  */
1285
     to be allocated on an objalloc anyway...  */
1247
  bfd_has_map (abfd) = TRUE;
1286
  bfd_has_map (abfd) = TRUE;
1248
  return TRUE;
1287
  return TRUE;
1249
}
1288
}
1250

1289

1251
/** Extended name table.
1290
/** Extended name table.
1252
 
1291
 
1253
  Normally archives support only 14-character filenames.
1292
  Normally archives support only 14-character filenames.
1254
 
1293
 
1255
  Intel has extended the format: longer names are stored in a special
1294
  Intel has extended the format: longer names are stored in a special
1256
  element (the first in the archive, or second if there is an armap);
1295
  element (the first in the archive, or second if there is an armap);
1257
  the name in the ar_hdr is replaced by 
1296
  the name in the ar_hdr is replaced by 
1258
  element>.  Index is the P.R. of an int (decimal).  Data General have
1297
  element>.  Index is the P.R. of an int (decimal).  Data General have
1259
  extended the format by using the prefix // for the special element.  */
1298
  extended the format by using the prefix // for the special element.  */
1260
 
1299
 
1261
/* Returns FALSE on error, TRUE otherwise.  */
1300
/* Returns FALSE on error, TRUE otherwise.  */
1262
 
1301
 
1263
bfd_boolean
1302
bfd_boolean
1264
_bfd_slurp_extended_name_table (bfd *abfd)
1303
_bfd_slurp_extended_name_table (bfd *abfd)
1265
{
1304
{
1266
  char nextname[17];
1305
  char nextname[17];
1267
  struct areltdata *namedata;
1306
  struct areltdata *namedata;
1268
  bfd_size_type amt;
1307
  bfd_size_type amt;
1269
 
1308
 
1270
  /* FIXME:  Formatting sucks here, and in case of failure of BFD_READ,
1309
  /* FIXME:  Formatting sucks here, and in case of failure of BFD_READ,
1271
     we probably don't want to return TRUE.  */
1310
     we probably don't want to return TRUE.  */
1272
  if (bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET) != 0)
1311
  if (bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET) != 0)
1273
    return FALSE;
1312
    return FALSE;
1274
 
1313
 
1275
  if (bfd_bread (nextname, 16, abfd) == 16)
1314
  if (bfd_bread (nextname, 16, abfd) == 16)
1276
    {
1315
    {
1277
      if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1316
      if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1278
	return FALSE;
1317
	return FALSE;
1279
 
1318
 
1280
      if (! CONST_STRNEQ (nextname, "ARFILENAMES/    ")
1319
      if (! CONST_STRNEQ (nextname, "ARFILENAMES/    ")
1281
	  && ! CONST_STRNEQ (nextname, "//              "))
1320
	  && ! CONST_STRNEQ (nextname, "//              "))
1282
	{
1321
	{
1283
	  bfd_ardata (abfd)->extended_names = NULL;
1322
	  bfd_ardata (abfd)->extended_names = NULL;
1284
	  bfd_ardata (abfd)->extended_names_size = 0;
1323
	  bfd_ardata (abfd)->extended_names_size = 0;
1285
	  return TRUE;
1324
	  return TRUE;
1286
	}
1325
	}
1287
 
1326
 
1288
      namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1327
      namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1289
      if (namedata == NULL)
1328
      if (namedata == NULL)
1290
	return FALSE;
1329
	return FALSE;
1291
 
1330
 
1292
      amt = namedata->parsed_size;
1331
      amt = namedata->parsed_size;
1293
      if (amt + 1 == 0)
1332
      if (amt + 1 == 0)
1294
	goto byebye;
1333
	goto byebye;
1295
 
1334
 
1296
      bfd_ardata (abfd)->extended_names_size = amt;
1335
      bfd_ardata (abfd)->extended_names_size = amt;
1297
      bfd_ardata (abfd)->extended_names = (char *) bfd_zalloc (abfd, amt + 1);
1336
      bfd_ardata (abfd)->extended_names = (char *) bfd_zalloc (abfd, amt + 1);
1298
      if (bfd_ardata (abfd)->extended_names == NULL)
1337
      if (bfd_ardata (abfd)->extended_names == NULL)
1299
	{
1338
	{
1300
	byebye:
1339
	byebye:
1301
	  free (namedata);
1340
	  free (namedata);
-
 
1341
	  bfd_ardata (abfd)->extended_names = NULL;
-
 
1342
	  bfd_ardata (abfd)->extended_names_size = 0;
1302
	  return FALSE;
1343
	  return FALSE;
1303
	}
1344
	}
1304
 
1345
 
1305
      if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1346
      if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1306
	{
1347
	{
1307
	  if (bfd_get_error () != bfd_error_system_call)
1348
	  if (bfd_get_error () != bfd_error_system_call)
1308
	    bfd_set_error (bfd_error_malformed_archive);
1349
	    bfd_set_error (bfd_error_malformed_archive);
1309
	  bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1350
	  bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1310
	  bfd_ardata (abfd)->extended_names = NULL;
1351
	  bfd_ardata (abfd)->extended_names = NULL;
1311
	  goto byebye;
1352
	  goto byebye;
1312
	}
1353
	}
1313
 
1354
 
1314
      /* Since the archive is supposed to be printable if it contains
1355
      /* Since the archive is supposed to be printable if it contains
1315
	 text, the entries in the list are newline-padded, not null
1356
	 text, the entries in the list are newline-padded, not null
1316
	 padded. In SVR4-style archives, the names also have a
1357
	 padded. In SVR4-style archives, the names also have a
1317
	 trailing '/'.  DOS/NT created archive often have \ in them
1358
	 trailing '/'.  DOS/NT created archive often have \ in them
1318
	 We'll fix all problems here..  */
1359
	 We'll fix all problems here.  */
1319
      {
1360
      {
1320
	char *ext_names = bfd_ardata (abfd)->extended_names;
1361
	char *ext_names = bfd_ardata (abfd)->extended_names;
1321
	char *temp = ext_names;
1362
	char *temp = ext_names;
1322
	char *limit = temp + namedata->parsed_size;
1363
	char *limit = temp + namedata->parsed_size;
-
 
1364
 
1323
	for (; temp < limit; ++temp)
1365
	for (; temp < limit; ++temp)
1324
	  {
1366
	  {
1325
	    if (*temp == ARFMAG[1])
1367
	    if (*temp == ARFMAG[1])
1326
	      temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1368
	      temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1327
	    if (*temp == '\\')
1369
	    if (*temp == '\\')
1328
	      *temp = '/';
1370
	      *temp = '/';
1329
	  }
1371
	  }
1330
	*limit = '\0';
1372
	*limit = '\0';
1331
      }
1373
      }
1332
 
1374
 
1333
      /* Pad to an even boundary if you have to.  */
1375
      /* Pad to an even boundary if you have to.  */
1334
      bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1376
      bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1335
      bfd_ardata (abfd)->first_file_filepos +=
1377
      bfd_ardata (abfd)->first_file_filepos +=
1336
	(bfd_ardata (abfd)->first_file_filepos) % 2;
1378
	(bfd_ardata (abfd)->first_file_filepos) % 2;
1337
 
1379
 
1338
      free (namedata);
1380
      free (namedata);
1339
    }
1381
    }
1340
  return TRUE;
1382
  return TRUE;
1341
}
1383
}
1342
 
1384
 
1343
#ifdef VMS
1385
#ifdef VMS
1344
 
1386
 
1345
/* Return a copy of the stuff in the filename between any :]> and a
1387
/* Return a copy of the stuff in the filename between any :]> and a
1346
   semicolon.  */
1388
   semicolon.  */
1347
 
1389
 
1348
static const char *
1390
static const char *
1349
normalize (bfd *abfd, const char *file)
1391
normalize (bfd *abfd, const char *file)
1350
{
1392
{
1351
  const char *first;
1393
  const char *first;
1352
  const char *last;
1394
  const char *last;
1353
  char *copy;
1395
  char *copy;
1354
 
1396
 
1355
  first = file + strlen (file) - 1;
1397
  first = file + strlen (file) - 1;
1356
  last = first + 1;
1398
  last = first + 1;
1357
 
1399
 
1358
  while (first != file)
1400
  while (first != file)
1359
    {
1401
    {
1360
      if (*first == ';')
1402
      if (*first == ';')
1361
	last = first;
1403
	last = first;
1362
      if (*first == ':' || *first == ']' || *first == '>')
1404
      if (*first == ':' || *first == ']' || *first == '>')
1363
	{
1405
	{
1364
	  first++;
1406
	  first++;
1365
	  break;
1407
	  break;
1366
	}
1408
	}
1367
      first--;
1409
      first--;
1368
    }
1410
    }
1369
 
1411
 
1370
  copy = bfd_alloc (abfd, last - first + 1);
1412
  copy = bfd_alloc (abfd, last - first + 1);
1371
  if (copy == NULL)
1413
  if (copy == NULL)
1372
    return NULL;
1414
    return NULL;
1373
 
1415
 
1374
  memcpy (copy, first, last - first);
1416
  memcpy (copy, first, last - first);
1375
  copy[last - first] = 0;
1417
  copy[last - first] = 0;
1376
 
1418
 
1377
  return copy;
1419
  return copy;
1378
}
1420
}
1379
 
1421
 
1380
#else
1422
#else
1381
static const char *
1423
static const char *
1382
normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
1424
normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
1383
{
1425
{
1384
  return lbasename (file);
1426
  return lbasename (file);
1385
}
1427
}
1386
#endif
1428
#endif
1387
 
1429
 
1388
/* Adjust a relative path name based on the reference path.
1430
/* Adjust a relative path name based on the reference path.
1389
   For example:
1431
   For example:
1390
 
1432
 
1391
     Relative path  Reference path  Result
1433
     Relative path  Reference path  Result
1392
     -------------  --------------  ------
1434
     -------------  --------------  ------
1393
     bar.o          lib.a           bar.o
1435
     bar.o          lib.a           bar.o
1394
     foo/bar.o      lib.a           foo/bar.o
1436
     foo/bar.o      lib.a           foo/bar.o
1395
     bar.o          foo/lib.a       ../bar.o
1437
     bar.o          foo/lib.a       ../bar.o
1396
     foo/bar.o      baz/lib.a       ../foo/bar.o
1438
     foo/bar.o      baz/lib.a       ../foo/bar.o
1397
     bar.o          ../lib.a        /bar.o
1439
     bar.o          ../lib.a        /bar.o
1398
   ; ../bar.o       ../lib.a        bar.o
1440
   ; ../bar.o       ../lib.a        bar.o
1399
   ; ../bar.o       lib.a           ../bar.o
1441
   ; ../bar.o       lib.a           ../bar.o
1400
     foo/bar.o      ../lib.a        /foo/bar.o
1442
     foo/bar.o      ../lib.a        /foo/bar.o
1401
     bar.o          ../../lib.a     //bar.o
1443
     bar.o          ../../lib.a     //bar.o
1402
     bar.o          foo/baz/lib.a   ../../bar.o
1444
     bar.o          foo/baz/lib.a   ../../bar.o
1403
 
1445
 
1404
   Note - the semicolons above are there to prevent the BFD chew
1446
   Note - the semicolons above are there to prevent the BFD chew
1405
   utility from interpreting those lines as prototypes to put into
1447
   utility from interpreting those lines as prototypes to put into
1406
   the autogenerated bfd.h header...
1448
   the autogenerated bfd.h header...
1407
 
1449
 
1408
   Note - the string is returned in a static buffer.  */
1450
   Note - the string is returned in a static buffer.  */
1409
 
1451
 
1410
static const char *
1452
static const char *
1411
adjust_relative_path (const char * path, const char * ref_path)
1453
adjust_relative_path (const char * path, const char * ref_path)
1412
{
1454
{
1413
  static char *pathbuf = NULL;
1455
  static char *pathbuf = NULL;
1414
  static unsigned int pathbuf_len = 0;
1456
  static unsigned int pathbuf_len = 0;
1415
  const char *pathp;
1457
  const char *pathp;
1416
  const char *refp;
1458
  const char *refp;
1417
  char * lpath;
1459
  char * lpath;
1418
  char * rpath;
1460
  char * rpath;
1419
  unsigned int len;
1461
  unsigned int len;
1420
  unsigned int dir_up = 0;
1462
  unsigned int dir_up = 0;
1421
  unsigned int dir_down = 0;
1463
  unsigned int dir_down = 0;
1422
  char *newp;
1464
  char *newp;
1423
  char * pwd = getpwd ();
1465
  char * pwd = getpwd ();
1424
  const char * down;
1466
  const char * down;
1425
 
1467
 
1426
  /* Remove symlinks, '.' and '..' from the paths, if possible.  */
1468
  /* Remove symlinks, '.' and '..' from the paths, if possible.  */
1427
  lpath = lrealpath (path);
1469
  lpath = lrealpath (path);
1428
  pathp = lpath == NULL ? path : lpath;
1470
  pathp = lpath == NULL ? path : lpath;
1429
 
1471
 
1430
  rpath = lrealpath (ref_path);
1472
  rpath = lrealpath (ref_path);
1431
  refp = rpath == NULL ? ref_path : rpath;
1473
  refp = rpath == NULL ? ref_path : rpath;
1432
 
1474
 
1433
  /* Remove common leading path elements.  */
1475
  /* Remove common leading path elements.  */
1434
  for (;;)
1476
  for (;;)
1435
    {
1477
    {
1436
      const char *e1 = pathp;
1478
      const char *e1 = pathp;
1437
      const char *e2 = refp;
1479
      const char *e2 = refp;
1438
 
1480
 
1439
      while (*e1 && ! IS_DIR_SEPARATOR (*e1))
1481
      while (*e1 && ! IS_DIR_SEPARATOR (*e1))
1440
	++e1;
1482
	++e1;
1441
      while (*e2 && ! IS_DIR_SEPARATOR (*e2))
1483
      while (*e2 && ! IS_DIR_SEPARATOR (*e2))
1442
	++e2;
1484
	++e2;
1443
      if (*e1 == '\0' || *e2 == '\0' || e1 - pathp != e2 - refp
1485
      if (*e1 == '\0' || *e2 == '\0' || e1 - pathp != e2 - refp
1444
	  || filename_ncmp (pathp, refp, e1 - pathp) != 0)
1486
	  || filename_ncmp (pathp, refp, e1 - pathp) != 0)
1445
	break;
1487
	break;
1446
      pathp = e1 + 1;
1488
      pathp = e1 + 1;
1447
      refp = e2 + 1;
1489
      refp = e2 + 1;
1448
    }
1490
    }
1449
 
1491
 
1450
  len = strlen (pathp) + 1;
1492
  len = strlen (pathp) + 1;
1451
  /* For each leading path element in the reference path,
1493
  /* For each leading path element in the reference path,
1452
     insert "../" into the path.  */
1494
     insert "../" into the path.  */
1453
  for (; *refp; ++refp)
1495
  for (; *refp; ++refp)
1454
    if (IS_DIR_SEPARATOR (*refp))
1496
    if (IS_DIR_SEPARATOR (*refp))
1455
      {
1497
      {
1456
	/* PR 12710:  If the path element is "../" then instead of
1498
	/* PR 12710:  If the path element is "../" then instead of
1457
	   inserting "../" we need to insert the name of the directory
1499
	   inserting "../" we need to insert the name of the directory
1458
	   at the current level.  */
1500
	   at the current level.  */
1459
	if (refp > ref_path + 1
1501
	if (refp > ref_path + 1
1460
	    && refp[-1] == '.'
1502
	    && refp[-1] == '.'
1461
	    && refp[-2] == '.')
1503
	    && refp[-2] == '.')
1462
	  dir_down ++;
1504
	  dir_down ++;
1463
	else
1505
	else
1464
	  dir_up ++;
1506
	  dir_up ++;
1465
      }
1507
      }
1466
 
1508
 
1467
  /* If the lrealpath calls above succeeded then we should never
1509
  /* If the lrealpath calls above succeeded then we should never
1468
     see dir_up and dir_down both being non-zero.  */
1510
     see dir_up and dir_down both being non-zero.  */
1469
 
1511
 
1470
  len += 3 * dir_up;
1512
  len += 3 * dir_up;
1471
 
1513
 
1472
  if (dir_down)
1514
  if (dir_down)
1473
    {
1515
    {
1474
      down = pwd + strlen (pwd) - 1;
1516
      down = pwd + strlen (pwd) - 1;
1475
 
1517
 
1476
      while (dir_down && down > pwd)
1518
      while (dir_down && down > pwd)
1477
	{
1519
	{
1478
	  if (IS_DIR_SEPARATOR (*down))
1520
	  if (IS_DIR_SEPARATOR (*down))
1479
	    --dir_down;
1521
	    --dir_down;
1480
	}
1522
	}
1481
      BFD_ASSERT (dir_down == 0);
1523
      BFD_ASSERT (dir_down == 0);
1482
      len += strlen (down) + 1;
1524
      len += strlen (down) + 1;
1483
    }
1525
    }
1484
  else
1526
  else
1485
    down = NULL;
1527
    down = NULL;
1486
 
1528
 
1487
  if (len > pathbuf_len)
1529
  if (len > pathbuf_len)
1488
    {
1530
    {
1489
      if (pathbuf != NULL)
1531
      if (pathbuf != NULL)
1490
	free (pathbuf);
1532
	free (pathbuf);
1491
      pathbuf_len = 0;
1533
      pathbuf_len = 0;
1492
      pathbuf = (char *) bfd_malloc (len);
1534
      pathbuf = (char *) bfd_malloc (len);
1493
      if (pathbuf == NULL)
1535
      if (pathbuf == NULL)
1494
	goto out;
1536
	goto out;
1495
      pathbuf_len = len;
1537
      pathbuf_len = len;
1496
    }
1538
    }
1497
 
1539
 
1498
  newp = pathbuf;
1540
  newp = pathbuf;
1499
  while (dir_up-- > 0)
1541
  while (dir_up-- > 0)
1500
    {
1542
    {
1501
      /* FIXME: Support Windows style path separators as well.  */
1543
      /* FIXME: Support Windows style path separators as well.  */
1502
      strcpy (newp, "../");
1544
      strcpy (newp, "../");
1503
      newp += 3;
1545
      newp += 3;
1504
    }
1546
    }
1505
 
1547
 
1506
  if (down)
1548
  if (down)
1507
    sprintf (newp, "%s/%s", down, pathp);
1549
    sprintf (newp, "%s/%s", down, pathp);
1508
  else
1550
  else
1509
    strcpy (newp, pathp);
1551
    strcpy (newp, pathp);
1510
 
1552
 
1511
 out:
1553
 out:
1512
  free (lpath);
1554
  free (lpath);
1513
  free (rpath);
1555
  free (rpath);
1514
  return pathbuf;
1556
  return pathbuf;
1515
}
1557
}
1516
 
1558
 
1517
/* Build a BFD style extended name table.  */
1559
/* Build a BFD style extended name table.  */
1518
 
1560
 
1519
bfd_boolean
1561
bfd_boolean
1520
_bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1562
_bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1521
						char **tabloc,
1563
						char **tabloc,
1522
						bfd_size_type *tablen,
1564
						bfd_size_type *tablen,
1523
						const char **name)
1565
						const char **name)
1524
{
1566
{
1525
  *name = "ARFILENAMES/";
1567
  *name = "ARFILENAMES/";
1526
  return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen);
1568
  return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen);
1527
}
1569
}
1528
 
1570
 
1529
/* Build an SVR4 style extended name table.  */
1571
/* Build an SVR4 style extended name table.  */
1530
 
1572
 
1531
bfd_boolean
1573
bfd_boolean
1532
_bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1574
_bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1533
						 char **tabloc,
1575
						 char **tabloc,
1534
						 bfd_size_type *tablen,
1576
						 bfd_size_type *tablen,
1535
						 const char **name)
1577
						 const char **name)
1536
{
1578
{
1537
  *name = "//";
1579
  *name = "//";
1538
  return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen);
1580
  return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen);
1539
}
1581
}
1540
 
1582
 
1541
/* Follows archive_head and produces an extended name table if
1583
/* Follows archive_head and produces an extended name table if
1542
   necessary.  Returns (in tabloc) a pointer to an extended name
1584
   necessary.  Returns (in tabloc) a pointer to an extended name
1543
   table, and in tablen the length of the table.  If it makes an entry
1585
   table, and in tablen the length of the table.  If it makes an entry
1544
   it clobbers the filename so that the element may be written without
1586
   it clobbers the filename so that the element may be written without
1545
   further massage.  Returns TRUE if it ran successfully, FALSE if
1587
   further massage.  Returns TRUE if it ran successfully, FALSE if
1546
   something went wrong.  A successful return may still involve a
1588
   something went wrong.  A successful return may still involve a
1547
   zero-length tablen!  */
1589
   zero-length tablen!  */
1548
 
1590
 
1549
bfd_boolean
1591
bfd_boolean
1550
_bfd_construct_extended_name_table (bfd *abfd,
1592
_bfd_construct_extended_name_table (bfd *abfd,
1551
				    bfd_boolean trailing_slash,
1593
				    bfd_boolean trailing_slash,
1552
				    char **tabloc,
1594
				    char **tabloc,
1553
				    bfd_size_type *tablen)
1595
				    bfd_size_type *tablen)
1554
{
1596
{
1555
  unsigned int maxname = ar_maxnamelen (abfd);
1597
  unsigned int maxname = ar_maxnamelen (abfd);
1556
  bfd_size_type total_namelen = 0;
1598
  bfd_size_type total_namelen = 0;
1557
  bfd *current;
1599
  bfd *current;
1558
  char *strptr;
1600
  char *strptr;
1559
  const char *last_filename;
1601
  const char *last_filename;
1560
  long last_stroff;
1602
  long last_stroff;
1561
 
1603
 
1562
  *tablen = 0;
1604
  *tablen = 0;
1563
  last_filename = NULL;
1605
  last_filename = NULL;
1564
 
1606
 
1565
  /* Figure out how long the table should be.  */
1607
  /* Figure out how long the table should be.  */
1566
  for (current = abfd->archive_head;
1608
  for (current = abfd->archive_head;
1567
       current != NULL;
1609
       current != NULL;
1568
       current = current->archive_next)
1610
       current = current->archive_next)
1569
    {
1611
    {
1570
      const char *normal;
1612
      const char *normal;
1571
      unsigned int thislen;
1613
      unsigned int thislen;
1572
 
1614
 
1573
      if (bfd_is_thin_archive (abfd))
1615
      if (bfd_is_thin_archive (abfd))
1574
	{
1616
	{
1575
	  const char *filename = current->filename;
1617
	  const char *filename = current->filename;
1576
 
1618
 
1577
	  /* If the element being added is a member of another archive
1619
	  /* If the element being added is a member of another archive
1578
	     (i.e., we are flattening), use the containing archive's name.  */
1620
	     (i.e., we are flattening), use the containing archive's name.  */
1579
	  if (current->my_archive
1621
	  if (current->my_archive
1580
	      && ! bfd_is_thin_archive (current->my_archive))
1622
	      && ! bfd_is_thin_archive (current->my_archive))
1581
	    filename = current->my_archive->filename;
1623
	    filename = current->my_archive->filename;
1582
 
1624
 
1583
	  /* If the path is the same as the previous path seen,
1625
	  /* If the path is the same as the previous path seen,
1584
	     reuse it.  This can happen when flattening a thin
1626
	     reuse it.  This can happen when flattening a thin
1585
	     archive that contains other archives.  */
1627
	     archive that contains other archives.  */
1586
	  if (last_filename && filename_cmp (last_filename, filename) == 0)
1628
	  if (last_filename && filename_cmp (last_filename, filename) == 0)
1587
	    continue;
1629
	    continue;
1588
 
1630
 
1589
	  last_filename = filename;
1631
	  last_filename = filename;
1590
 
1632
 
1591
	  /* If the path is relative, adjust it relative to
1633
	  /* If the path is relative, adjust it relative to
1592
	     the containing archive. */
1634
	     the containing archive. */
1593
	  if (! IS_ABSOLUTE_PATH (filename)
1635
	  if (! IS_ABSOLUTE_PATH (filename)
1594
	      && ! IS_ABSOLUTE_PATH (abfd->filename))
1636
	      && ! IS_ABSOLUTE_PATH (abfd->filename))
1595
	    normal = adjust_relative_path (filename, abfd->filename);
1637
	    normal = adjust_relative_path (filename, abfd->filename);
1596
	  else
1638
	  else
1597
	    normal = filename;
1639
	    normal = filename;
1598
 
1640
 
1599
	  /* In a thin archive, always store the full pathname
1641
	  /* In a thin archive, always store the full pathname
1600
	     in the extended name table.  */
1642
	     in the extended name table.  */
1601
	  total_namelen += strlen (normal) + 1;
1643
	  total_namelen += strlen (normal) + 1;
1602
	  if (trailing_slash)
1644
	  if (trailing_slash)
1603
	    /* Leave room for trailing slash.  */
1645
	    /* Leave room for trailing slash.  */
1604
	    ++total_namelen;
1646
	    ++total_namelen;
1605
 
1647
 
1606
	  continue;
1648
	  continue;
1607
	}
1649
	}
1608
 
1650
 
1609
      normal = normalize (current, current->filename);
1651
      normal = normalize (current, current->filename);
1610
      if (normal == NULL)
1652
      if (normal == NULL)
1611
	return FALSE;
1653
	return FALSE;
1612
 
1654
 
1613
      thislen = strlen (normal);
1655
      thislen = strlen (normal);
1614
 
1656
 
1615
      if (thislen > maxname
1657
      if (thislen > maxname
1616
	  && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1658
	  && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1617
	thislen = maxname;
1659
	thislen = maxname;
1618
 
1660
 
1619
      if (thislen > maxname)
1661
      if (thislen > maxname)
1620
	{
1662
	{
1621
	  /* Add one to leave room for \n.  */
1663
	  /* Add one to leave room for \n.  */
1622
	  total_namelen += thislen + 1;
1664
	  total_namelen += thislen + 1;
1623
	  if (trailing_slash)
1665
	  if (trailing_slash)
1624
	    {
1666
	    {
1625
	      /* Leave room for trailing slash.  */
1667
	      /* Leave room for trailing slash.  */
1626
	      ++total_namelen;
1668
	      ++total_namelen;
1627
	    }
1669
	    }
1628
	}
1670
	}
1629
      else
1671
      else
1630
	{
1672
	{
1631
	  struct ar_hdr *hdr = arch_hdr (current);
1673
	  struct ar_hdr *hdr = arch_hdr (current);
1632
	  if (filename_ncmp (normal, hdr->ar_name, thislen) != 0
1674
	  if (filename_ncmp (normal, hdr->ar_name, thislen) != 0
1633
	      || (thislen < sizeof hdr->ar_name
1675
	      || (thislen < sizeof hdr->ar_name
1634
		  && hdr->ar_name[thislen] != ar_padchar (current)))
1676
		  && hdr->ar_name[thislen] != ar_padchar (current)))
1635
	    {
1677
	    {
1636
	      /* Must have been using extended format even though it
1678
	      /* Must have been using extended format even though it
1637
		 didn't need to.  Fix it to use normal format.  */
1679
		 didn't need to.  Fix it to use normal format.  */
1638
	      memcpy (hdr->ar_name, normal, thislen);
1680
	      memcpy (hdr->ar_name, normal, thislen);
1639
	      if (thislen < maxname
1681
	      if (thislen < maxname
1640
		  || (thislen == maxname && thislen < sizeof hdr->ar_name))
1682
		  || (thislen == maxname && thislen < sizeof hdr->ar_name))
1641
		hdr->ar_name[thislen] = ar_padchar (current);
1683
		hdr->ar_name[thislen] = ar_padchar (current);
1642
	    }
1684
	    }
1643
	}
1685
	}
1644
    }
1686
    }
1645
 
1687
 
1646
  if (total_namelen == 0)
1688
  if (total_namelen == 0)
1647
    return TRUE;
1689
    return TRUE;
1648
 
1690
 
1649
  *tabloc = (char *) bfd_zalloc (abfd, total_namelen);
1691
  *tabloc = (char *) bfd_zalloc (abfd, total_namelen);
1650
  if (*tabloc == NULL)
1692
  if (*tabloc == NULL)
1651
    return FALSE;
1693
    return FALSE;
1652
 
1694
 
1653
  *tablen = total_namelen;
1695
  *tablen = total_namelen;
1654
  strptr = *tabloc;
1696
  strptr = *tabloc;
1655
 
1697
 
1656
  last_filename = NULL;
1698
  last_filename = NULL;
1657
  last_stroff = 0;
1699
  last_stroff = 0;
1658
 
1700
 
1659
  for (current = abfd->archive_head;
1701
  for (current = abfd->archive_head;
1660
       current != NULL;
1702
       current != NULL;
1661
       current = current->archive_next)
1703
       current = current->archive_next)
1662
    {
1704
    {
1663
      const char *normal;
1705
      const char *normal;
1664
      unsigned int thislen;
1706
      unsigned int thislen;
1665
      long stroff;
1707
      long stroff;
1666
      const char *filename = current->filename;
1708
      const char *filename = current->filename;
1667
 
1709
 
1668
      if (bfd_is_thin_archive (abfd))
1710
      if (bfd_is_thin_archive (abfd))
1669
	{
1711
	{
1670
	  /* If the element being added is a member of another archive
1712
	  /* If the element being added is a member of another archive
1671
	     (i.e., we are flattening), use the containing archive's name.  */
1713
	     (i.e., we are flattening), use the containing archive's name.  */
1672
	  if (current->my_archive
1714
	  if (current->my_archive
1673
	      && ! bfd_is_thin_archive (current->my_archive))
1715
	      && ! bfd_is_thin_archive (current->my_archive))
1674
	    filename = current->my_archive->filename;
1716
	    filename = current->my_archive->filename;
1675
	  /* If the path is the same as the previous path seen,
1717
	  /* If the path is the same as the previous path seen,
1676
	     reuse it.  This can happen when flattening a thin
1718
	     reuse it.  This can happen when flattening a thin
1677
	     archive that contains other archives.
1719
	     archive that contains other archives.
1678
	     If the path is relative, adjust it relative to
1720
	     If the path is relative, adjust it relative to
1679
	     the containing archive.  */
1721
	     the containing archive.  */
1680
	  if (last_filename && filename_cmp (last_filename, filename) == 0)
1722
	  if (last_filename && filename_cmp (last_filename, filename) == 0)
1681
	    normal = last_filename;
1723
	    normal = last_filename;
1682
	  else if (! IS_ABSOLUTE_PATH (filename)
1724
	  else if (! IS_ABSOLUTE_PATH (filename)
1683
		   && ! IS_ABSOLUTE_PATH (abfd->filename))
1725
		   && ! IS_ABSOLUTE_PATH (abfd->filename))
1684
	    normal = adjust_relative_path (filename, abfd->filename);
1726
	    normal = adjust_relative_path (filename, abfd->filename);
1685
	  else
1727
	  else
1686
	    normal = filename;
1728
	    normal = filename;
1687
	}
1729
	}
1688
      else
1730
      else
1689
	{
1731
	{
1690
	  normal = normalize (current, filename);
1732
	  normal = normalize (current, filename);
1691
	  if (normal == NULL)
1733
	  if (normal == NULL)
1692
	    return FALSE;
1734
	    return FALSE;
1693
	}
1735
	}
1694
 
1736
 
1695
      thislen = strlen (normal);
1737
      thislen = strlen (normal);
1696
      if (thislen > maxname || bfd_is_thin_archive (abfd))
1738
      if (thislen > maxname || bfd_is_thin_archive (abfd))
1697
	{
1739
	{
1698
	  /* Works for now; may need to be re-engineered if we
1740
	  /* Works for now; may need to be re-engineered if we
1699
	     encounter an oddball archive format and want to
1741
	     encounter an oddball archive format and want to
1700
	     generalise this hack.  */
1742
	     generalise this hack.  */
1701
	  struct ar_hdr *hdr = arch_hdr (current);
1743
	  struct ar_hdr *hdr = arch_hdr (current);
1702
	  if (normal == last_filename)
1744
	  if (normal == last_filename)
1703
	    stroff = last_stroff;
1745
	    stroff = last_stroff;
1704
	  else
1746
	  else
1705
	    {
1747
	    {
1706
	      strcpy (strptr, normal);
1748
	      strcpy (strptr, normal);
1707
	      if (! trailing_slash)
1749
	      if (! trailing_slash)
1708
		strptr[thislen] = ARFMAG[1];
1750
		strptr[thislen] = ARFMAG[1];
1709
	      else
1751
	      else
1710
		{
1752
		{
1711
		  strptr[thislen] = '/';
1753
		  strptr[thislen] = '/';
1712
		  strptr[thislen + 1] = ARFMAG[1];
1754
		  strptr[thislen + 1] = ARFMAG[1];
1713
		}
1755
		}
1714
	      stroff = strptr - *tabloc;
1756
	      stroff = strptr - *tabloc;
1715
	      last_stroff = stroff;
1757
	      last_stroff = stroff;
1716
	    }
1758
	    }
1717
	  hdr->ar_name[0] = ar_padchar (current);
1759
	  hdr->ar_name[0] = ar_padchar (current);
1718
	  if (bfd_is_thin_archive (abfd) && current->origin > 0)
1760
	  if (bfd_is_thin_archive (abfd) && current->origin > 0)
1719
	    {
1761
	    {
1720
	      int len = snprintf (hdr->ar_name + 1, maxname - 1, "%-ld:",
1762
	      int len = snprintf (hdr->ar_name + 1, maxname - 1, "%-ld:",
1721
				  stroff);
1763
				  stroff);
1722
	      _bfd_ar_spacepad (hdr->ar_name + 1 + len, maxname - 1 - len,
1764
	      _bfd_ar_spacepad (hdr->ar_name + 1 + len, maxname - 1 - len,
1723
				"%-ld",
1765
				"%-ld",
1724
				current->origin - sizeof (struct ar_hdr));
1766
				current->origin - sizeof (struct ar_hdr));
1725
	    }
1767
	    }
1726
	  else
1768
	  else
1727
	    _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
1769
	    _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
1728
	  if (normal != last_filename)
1770
	  if (normal != last_filename)
1729
	    {
1771
	    {
1730
	      strptr += thislen + 1;
1772
	      strptr += thislen + 1;
1731
	      if (trailing_slash)
1773
	      if (trailing_slash)
1732
		++strptr;
1774
		++strptr;
1733
	      last_filename = filename;
1775
	      last_filename = filename;
1734
	    }
1776
	    }
1735
	}
1777
	}
1736
    }
1778
    }
1737
 
1779
 
1738
  return TRUE;
1780
  return TRUE;
1739
}
1781
}
1740
 
1782
 
1741
/* Do not construct an extended name table but transforms name field into
1783
/* Do not construct an extended name table but transforms name field into
1742
   its extended form.  */
1784
   its extended form.  */
1743
 
1785
 
1744
bfd_boolean
1786
bfd_boolean
1745
_bfd_archive_bsd44_construct_extended_name_table (bfd *abfd,
1787
_bfd_archive_bsd44_construct_extended_name_table (bfd *abfd,
1746
						  char **tabloc,
1788
						  char **tabloc,
1747
						  bfd_size_type *tablen,
1789
						  bfd_size_type *tablen,
1748
						  const char **name)
1790
						  const char **name)
1749
{
1791
{
1750
  unsigned int maxname = ar_maxnamelen (abfd);
1792
  unsigned int maxname = ar_maxnamelen (abfd);
1751
  bfd *current;
1793
  bfd *current;
1752
 
1794
 
1753
  *tablen = 0;
1795
  *tablen = 0;
1754
  *tabloc = NULL;
1796
  *tabloc = NULL;
1755
  *name = NULL;
1797
  *name = NULL;
1756
 
1798
 
1757
  for (current = abfd->archive_head;
1799
  for (current = abfd->archive_head;
1758
       current != NULL;
1800
       current != NULL;
1759
       current = current->archive_next)
1801
       current = current->archive_next)
1760
    {
1802
    {
1761
      const char *normal = normalize (current, current->filename);
1803
      const char *normal = normalize (current, current->filename);
1762
      int has_space = 0;
1804
      int has_space = 0;
1763
      unsigned int len;
1805
      unsigned int len;
1764
 
1806
 
1765
      if (normal == NULL)
1807
      if (normal == NULL)
1766
	return FALSE;
1808
	return FALSE;
1767
 
1809
 
1768
      for (len = 0; normal[len]; len++)
1810
      for (len = 0; normal[len]; len++)
1769
	if (normal[len] == ' ')
1811
	if (normal[len] == ' ')
1770
	  has_space = 1;
1812
	  has_space = 1;
1771
 
1813
 
1772
      if (len > maxname || has_space)
1814
      if (len > maxname || has_space)
1773
	{
1815
	{
1774
	  struct ar_hdr *hdr = arch_hdr (current);
1816
	  struct ar_hdr *hdr = arch_hdr (current);
1775
 
1817
 
1776
	  len = (len + 3) & ~3;
1818
	  len = (len + 3) & ~3;
1777
	  arch_eltdata (current)->extra_size = len;
1819
	  arch_eltdata (current)->extra_size = len;
1778
	  _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%lu", len);
1820
	  _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%lu", len);
1779
	}
1821
	}
1780
    }
1822
    }
1781
 
1823
 
1782
  return TRUE;
1824
  return TRUE;
1783
}
1825
}
1784

1826

1785
/* Write an archive header.  */
1827
/* Write an archive header.  */
1786
 
1828
 
1787
bfd_boolean
1829
bfd_boolean
1788
_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1830
_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1789
{
1831
{
1790
  struct ar_hdr *hdr = arch_hdr (abfd);
1832
  struct ar_hdr *hdr = arch_hdr (abfd);
1791
 
1833
 
1792
  if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1834
  if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1793
    return FALSE;
1835
    return FALSE;
1794
  return TRUE;
1836
  return TRUE;
1795
}
1837
}
1796
 
1838
 
1797
/* Write an archive header using BSD4.4 convention.  */
1839
/* Write an archive header using BSD4.4 convention.  */
1798
 
1840
 
1799
bfd_boolean
1841
bfd_boolean
1800
_bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
1842
_bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
1801
{
1843
{
1802
  struct ar_hdr *hdr = arch_hdr (abfd);
1844
  struct ar_hdr *hdr = arch_hdr (abfd);
1803
 
1845
 
1804
  if (is_bsd44_extended_name (hdr->ar_name))
1846
  if (is_bsd44_extended_name (hdr->ar_name))
1805
    {
1847
    {
1806
      /* This is a BSD 4.4 extended name.  */
1848
      /* This is a BSD 4.4 extended name.  */
1807
      const char *fullname = normalize (abfd, abfd->filename);
1849
      const char *fullname = normalize (abfd, abfd->filename);
1808
      unsigned int len = strlen (fullname);
1850
      unsigned int len = strlen (fullname);
1809
      unsigned int padded_len = (len + 3) & ~3;
1851
      unsigned int padded_len = (len + 3) & ~3;
1810
 
1852
 
1811
      BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size);
1853
      BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size);
1812
 
1854
 
1813
      if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size),
1855
      if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size),
1814
			    arch_eltdata (abfd)->parsed_size + padded_len))
1856
			    arch_eltdata (abfd)->parsed_size + padded_len))
1815
	return FALSE;
1857
	return FALSE;
1816
 
1858
 
1817
      if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1859
      if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1818
	return FALSE;
1860
	return FALSE;
1819
 
1861
 
1820
      if (bfd_bwrite (fullname, len, archive) != len)
1862
      if (bfd_bwrite (fullname, len, archive) != len)
1821
	return FALSE;
1863
	return FALSE;
1822
 
1864
 
1823
      if (len & 3)
1865
      if (len & 3)
1824
	{
1866
	{
1825
	  static const char pad[3] = { 0, 0, 0 };
1867
	  static const char pad[3] = { 0, 0, 0 };
1826
 
1868
 
1827
	  len = 4 - (len & 3);
1869
	  len = 4 - (len & 3);
1828
	  if (bfd_bwrite (pad, len, archive) != len)
1870
	  if (bfd_bwrite (pad, len, archive) != len)
1829
	    return FALSE;
1871
	    return FALSE;
1830
	}
1872
	}
1831
    }
1873
    }
1832
  else
1874
  else
1833
    {
1875
    {
1834
      if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1876
      if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1835
	return FALSE;
1877
	return FALSE;
1836
    }
1878
    }
1837
  return TRUE;
1879
  return TRUE;
1838
}
1880
}
1839

1881

1840
/* A couple of functions for creating ar_hdrs.  */
1882
/* A couple of functions for creating ar_hdrs.  */
1841
 
1883
 
1842
#ifdef HPUX_LARGE_AR_IDS
1884
#ifdef HPUX_LARGE_AR_IDS
1843
/* Function to encode large UID/GID values according to HP.  */
1885
/* Function to encode large UID/GID values according to HP.  */
1844
 
1886
 
1845
static void
1887
static void
1846
hpux_uid_gid_encode (char str[6], long int id)
1888
hpux_uid_gid_encode (char str[6], long int id)
1847
{
1889
{
1848
  int cnt;
1890
  int cnt;
1849
 
1891
 
1850
  str[5] = '@' + (id & 3);
1892
  str[5] = '@' + (id & 3);
1851
  id >>= 2;
1893
  id >>= 2;
1852
 
1894
 
1853
  for (cnt = 4; cnt >= 0; --cnt, id >>= 6)
1895
  for (cnt = 4; cnt >= 0; --cnt, id >>= 6)
1854
    str[cnt] = ' ' + (id & 0x3f);
1896
    str[cnt] = ' ' + (id & 0x3f);
1855
}
1897
}
1856
#endif	/* HPUX_LARGE_AR_IDS */
1898
#endif	/* HPUX_LARGE_AR_IDS */
1857
 
1899
 
1858
#ifndef HAVE_GETUID
1900
#ifndef HAVE_GETUID
1859
#define getuid() 0
1901
#define getuid() 0
1860
#endif
1902
#endif
1861
 
1903
 
1862
#ifndef HAVE_GETGID
1904
#ifndef HAVE_GETGID
1863
#define getgid() 0
1905
#define getgid() 0
1864
#endif
1906
#endif
1865
 
1907
 
1866
/* Takes a filename, returns an arelt_data for it, or NULL if it can't
1908
/* Takes a filename, returns an arelt_data for it, or NULL if it can't
1867
   make one.  The filename must refer to a filename in the filesystem.
1909
   make one.  The filename must refer to a filename in the filesystem.
1868
   The filename field of the ar_hdr will NOT be initialized.  If member
1910
   The filename field of the ar_hdr will NOT be initialized.  If member
1869
   is set, and it's an in-memory bfd, we fake it.  */
1911
   is set, and it's an in-memory bfd, we fake it.  */
1870
 
1912
 
1871
static struct areltdata *
1913
static struct areltdata *
1872
bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
1914
bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
1873
{
1915
{
1874
  struct stat status;
1916
  struct stat status;
1875
  struct areltdata *ared;
1917
  struct areltdata *ared;
1876
  struct ar_hdr *hdr;
1918
  struct ar_hdr *hdr;
1877
  bfd_size_type amt;
1919
  bfd_size_type amt;
1878
 
1920
 
1879
  if (member && (member->flags & BFD_IN_MEMORY) != 0)
1921
  if (member && (member->flags & BFD_IN_MEMORY) != 0)
1880
    {
1922
    {
1881
      /* Assume we just "made" the member, and fake it.  */
1923
      /* Assume we just "made" the member, and fake it.  */
1882
      struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream;
1924
      struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream;
1883
      time (&status.st_mtime);
1925
      time (&status.st_mtime);
1884
      status.st_uid = getuid ();
1926
      status.st_uid = getuid ();
1885
      status.st_gid = getgid ();
1927
      status.st_gid = getgid ();
1886
      status.st_mode = 0644;
1928
      status.st_mode = 0644;
1887
      status.st_size = bim->size;
1929
      status.st_size = bim->size;
1888
    }
1930
    }
1889
  else if (stat (filename, &status) != 0)
1931
  else if (stat (filename, &status) != 0)
1890
    {
1932
    {
1891
      bfd_set_error (bfd_error_system_call);
1933
      bfd_set_error (bfd_error_system_call);
1892
      return NULL;
1934
      return NULL;
1893
    }
1935
    }
1894
 
1936
 
1895
  /* If the caller requested that the BFD generate deterministic output,
1937
  /* If the caller requested that the BFD generate deterministic output,
1896
     fake values for modification time, UID, GID, and file mode.  */
1938
     fake values for modification time, UID, GID, and file mode.  */
1897
  if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
1939
  if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
1898
    {
1940
    {
1899
      status.st_mtime = 0;
1941
      status.st_mtime = 0;
1900
      status.st_uid = 0;
1942
      status.st_uid = 0;
1901
      status.st_gid = 0;
1943
      status.st_gid = 0;
1902
      status.st_mode = 0644;
1944
      status.st_mode = 0644;
1903
    }
1945
    }
1904
 
1946
 
1905
  amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
1947
  amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
1906
  ared = (struct areltdata *) bfd_zmalloc (amt);
1948
  ared = (struct areltdata *) bfd_zmalloc (amt);
1907
  if (ared == NULL)
1949
  if (ared == NULL)
1908
    return NULL;
1950
    return NULL;
1909
  hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1951
  hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1910
 
1952
 
1911
  /* ar headers are space padded, not null padded!  */
1953
  /* ar headers are space padded, not null padded!  */
1912
  memset (hdr, ' ', sizeof (struct ar_hdr));
1954
  memset (hdr, ' ', sizeof (struct ar_hdr));
1913
 
1955
 
1914
  _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
1956
  _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
1915
		    status.st_mtime);
1957
		    status.st_mtime);
1916
#ifdef HPUX_LARGE_AR_IDS
1958
#ifdef HPUX_LARGE_AR_IDS
1917
  /* HP has a very "special" way to handle UID/GID's with numeric values
1959
  /* HP has a very "special" way to handle UID/GID's with numeric values
1918
     > 99999.  */
1960
     > 99999.  */
1919
  if (status.st_uid > 99999)
1961
  if (status.st_uid > 99999)
1920
    hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid);
1962
    hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid);
1921
  else
1963
  else
1922
#endif
1964
#endif
1923
    _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
1965
    _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
1924
		      status.st_uid);
1966
		      status.st_uid);
1925
#ifdef HPUX_LARGE_AR_IDS
1967
#ifdef HPUX_LARGE_AR_IDS
1926
  /* HP has a very "special" way to handle UID/GID's with numeric values
1968
  /* HP has a very "special" way to handle UID/GID's with numeric values
1927
     > 99999.  */
1969
     > 99999.  */
1928
  if (status.st_gid > 99999)
1970
  if (status.st_gid > 99999)
1929
    hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid);
1971
    hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid);
1930
  else
1972
  else
1931
#endif
1973
#endif
1932
    _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
1974
    _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
1933
		      status.st_gid);
1975
		      status.st_gid);
1934
  _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
1976
  _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
1935
		    status.st_mode);
1977
		    status.st_mode);
1936
  if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size), status.st_size))
1978
  if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size), status.st_size))
1937
    {
1979
    {
1938
      free (ared);
1980
      free (ared);
1939
      return NULL;
1981
      return NULL;
1940
    }
1982
    }
1941
  memcpy (hdr->ar_fmag, ARFMAG, 2);
1983
  memcpy (hdr->ar_fmag, ARFMAG, 2);
1942
  ared->parsed_size = status.st_size;
1984
  ared->parsed_size = status.st_size;
1943
  ared->arch_header = (char *) hdr;
1985
  ared->arch_header = (char *) hdr;
1944
 
1986
 
1945
  return ared;
1987
  return ared;
1946
}
1988
}
1947
 
1989
 
1948
/* Analogous to stat call.  */
1990
/* Analogous to stat call.  */
1949
 
1991
 
1950
int
1992
int
1951
bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
1993
bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
1952
{
1994
{
1953
  struct ar_hdr *hdr;
1995
  struct ar_hdr *hdr;
1954
  char *aloser;
1996
  char *aloser;
1955
 
1997
 
1956
  if (abfd->arelt_data == NULL)
1998
  if (abfd->arelt_data == NULL)
1957
    {
1999
    {
1958
      bfd_set_error (bfd_error_invalid_operation);
2000
      bfd_set_error (bfd_error_invalid_operation);
1959
      return -1;
2001
      return -1;
1960
    }
2002
    }
1961
 
2003
 
1962
  hdr = arch_hdr (abfd);
2004
  hdr = arch_hdr (abfd);
-
 
2005
  /* PR 17512: file: 3d9e9fe9.  */
-
 
2006
  if (hdr == NULL)
1963
 
2007
    return -1;
1964
#define foo(arelt, stelt, size)				\
2008
#define foo(arelt, stelt, size)				\
1965
  buf->stelt = strtol (hdr->arelt, &aloser, size);	\
2009
  buf->stelt = strtol (hdr->arelt, &aloser, size);	\
1966
  if (aloser == hdr->arelt)	      			\
2010
  if (aloser == hdr->arelt)	      			\
1967
    return -1;
2011
    return -1;
1968
 
2012
 
1969
  /* Some platforms support special notations for large IDs.  */
2013
  /* Some platforms support special notations for large IDs.  */
1970
#ifdef HPUX_LARGE_AR_IDS
2014
#ifdef HPUX_LARGE_AR_IDS
1971
# define foo2(arelt, stelt, size)					\
2015
# define foo2(arelt, stelt, size)					\
1972
  if (hdr->arelt[5] == ' ')						\
2016
  if (hdr->arelt[5] == ' ')						\
1973
    {									\
2017
    {									\
1974
      foo (arelt, stelt, size);						\
2018
      foo (arelt, stelt, size);						\
1975
    }									\
2019
    }									\
1976
  else									\
2020
  else									\
1977
    {									\
2021
    {									\
1978
      int cnt;								\
2022
      int cnt;								\
1979
      for (buf->stelt = cnt = 0; cnt < 5; ++cnt)			\
2023
      for (buf->stelt = cnt = 0; cnt < 5; ++cnt)			\
1980
	{								\
2024
	{								\
1981
	  if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f)	\
2025
	  if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f)	\
1982
	    return -1;							\
2026
	    return -1;							\
1983
	  buf->stelt <<= 6;						\
2027
	  buf->stelt <<= 6;						\
1984
	  buf->stelt += hdr->arelt[cnt] - ' ';				\
2028
	  buf->stelt += hdr->arelt[cnt] - ' ';				\
1985
	}								\
2029
	}								\
1986
      if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3)		\
2030
      if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3)		\
1987
	return -1;							\
2031
	return -1;							\
1988
      buf->stelt <<= 2;							\
2032
      buf->stelt <<= 2;							\
1989
      buf->stelt += hdr->arelt[5] - '@';				\
2033
      buf->stelt += hdr->arelt[5] - '@';				\
1990
    }
2034
    }
1991
#else
2035
#else
1992
# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
2036
# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1993
#endif
2037
#endif
1994
 
2038
 
1995
  foo (ar_date, st_mtime, 10);
2039
  foo (ar_date, st_mtime, 10);
1996
  foo2 (ar_uid, st_uid, 10);
2040
  foo2 (ar_uid, st_uid, 10);
1997
  foo2 (ar_gid, st_gid, 10);
2041
  foo2 (ar_gid, st_gid, 10);
1998
  foo (ar_mode, st_mode, 8);
2042
  foo (ar_mode, st_mode, 8);
1999
 
2043
 
2000
  buf->st_size = arch_eltdata (abfd)->parsed_size;
2044
  buf->st_size = arch_eltdata (abfd)->parsed_size;
2001
 
2045
 
2002
  return 0;
2046
  return 0;
2003
}
2047
}
2004
 
2048
 
2005
void
2049
void
2006
bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2050
bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2007
{
2051
{
2008
  /* FIXME: This interacts unpleasantly with ar's quick-append option.
2052
  /* FIXME: This interacts unpleasantly with ar's quick-append option.
2009
     Fortunately ic960 users will never use that option.  Fixing this
2053
     Fortunately ic960 users will never use that option.  Fixing this
2010
     is very hard; fortunately I know how to do it and will do so once
2054
     is very hard; fortunately I know how to do it and will do so once
2011
     intel's release is out the door.  */
2055
     intel's release is out the door.  */
2012
 
2056
 
2013
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2057
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2014
  size_t length;
2058
  size_t length;
2015
  const char *filename;
2059
  const char *filename;
2016
  size_t maxlen = ar_maxnamelen (abfd);
2060
  size_t maxlen = ar_maxnamelen (abfd);
2017
 
2061
 
2018
  if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
2062
  if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
2019
    {
2063
    {
2020
      bfd_bsd_truncate_arname (abfd, pathname, arhdr);
2064
      bfd_bsd_truncate_arname (abfd, pathname, arhdr);
2021
      return;
2065
      return;
2022
    }
2066
    }
2023
 
2067
 
2024
  filename = normalize (abfd, pathname);
2068
  filename = normalize (abfd, pathname);
2025
  if (filename == NULL)
2069
  if (filename == NULL)
2026
    {
2070
    {
2027
      /* FIXME */
2071
      /* FIXME */
2028
      abort ();
2072
      abort ();
2029
    }
2073
    }
2030
 
2074
 
2031
  length = strlen (filename);
2075
  length = strlen (filename);
2032
 
2076
 
2033
  if (length <= maxlen)
2077
  if (length <= maxlen)
2034
    memcpy (hdr->ar_name, filename, length);
2078
    memcpy (hdr->ar_name, filename, length);
2035
 
2079
 
2036
  /* Add the padding character if there is room for it.  */
2080
  /* Add the padding character if there is room for it.  */
2037
  if (length < maxlen
2081
  if (length < maxlen
2038
      || (length == maxlen && length < sizeof hdr->ar_name))
2082
      || (length == maxlen && length < sizeof hdr->ar_name))
2039
    (hdr->ar_name)[length] = ar_padchar (abfd);
2083
    (hdr->ar_name)[length] = ar_padchar (abfd);
2040
}
2084
}
2041
 
2085
 
2042
void
2086
void
2043
bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2087
bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2044
{
2088
{
2045
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2089
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2046
  size_t length;
2090
  size_t length;
2047
  const char *filename = lbasename (pathname);
2091
  const char *filename = lbasename (pathname);
2048
  size_t maxlen = ar_maxnamelen (abfd);
2092
  size_t maxlen = ar_maxnamelen (abfd);
2049
 
2093
 
2050
  length = strlen (filename);
2094
  length = strlen (filename);
2051
 
2095
 
2052
  if (length <= maxlen)
2096
  if (length <= maxlen)
2053
    memcpy (hdr->ar_name, filename, length);
2097
    memcpy (hdr->ar_name, filename, length);
2054
  else
2098
  else
2055
    {
2099
    {
2056
      /* pathname: meet procrustes */
2100
      /* pathname: meet procrustes */
2057
      memcpy (hdr->ar_name, filename, maxlen);
2101
      memcpy (hdr->ar_name, filename, maxlen);
2058
      length = maxlen;
2102
      length = maxlen;
2059
    }
2103
    }
2060
 
2104
 
2061
  if (length < maxlen)
2105
  if (length < maxlen)
2062
    (hdr->ar_name)[length] = ar_padchar (abfd);
2106
    (hdr->ar_name)[length] = ar_padchar (abfd);
2063
}
2107
}
2064
 
2108
 
2065
/* Store name into ar header.  Truncates the name to fit.
2109
/* Store name into ar header.  Truncates the name to fit.
2066
   1> strip pathname to be just the basename.
2110
   1> strip pathname to be just the basename.
2067
   2> if it's short enuf to fit, stuff it in.
2111
   2> if it's short enuf to fit, stuff it in.
2068
   3> If it doesn't end with .o, truncate it to fit
2112
   3> If it doesn't end with .o, truncate it to fit
2069
   4> truncate it before the .o, append .o, stuff THAT in.  */
2113
   4> truncate it before the .o, append .o, stuff THAT in.  */
2070
 
2114
 
2071
/* This is what gnu ar does.  It's better but incompatible with the
2115
/* This is what gnu ar does.  It's better but incompatible with the
2072
   bsd ar.  */
2116
   bsd ar.  */
2073
 
2117
 
2074
void
2118
void
2075
bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2119
bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2076
{
2120
{
2077
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2121
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2078
  size_t length;
2122
  size_t length;
2079
  const char *filename = lbasename (pathname);
2123
  const char *filename = lbasename (pathname);
2080
  size_t maxlen = ar_maxnamelen (abfd);
2124
  size_t maxlen = ar_maxnamelen (abfd);
2081
 
2125
 
2082
  length = strlen (filename);
2126
  length = strlen (filename);
2083
 
2127
 
2084
  if (length <= maxlen)
2128
  if (length <= maxlen)
2085
    memcpy (hdr->ar_name, filename, length);
2129
    memcpy (hdr->ar_name, filename, length);
2086
  else
2130
  else
2087
    {
2131
    {
2088
      /* pathname: meet procrustes.  */
2132
      /* pathname: meet procrustes.  */
2089
      memcpy (hdr->ar_name, filename, maxlen);
2133
      memcpy (hdr->ar_name, filename, maxlen);
2090
      if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
2134
      if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
2091
	{
2135
	{
2092
	  hdr->ar_name[maxlen - 2] = '.';
2136
	  hdr->ar_name[maxlen - 2] = '.';
2093
	  hdr->ar_name[maxlen - 1] = 'o';
2137
	  hdr->ar_name[maxlen - 1] = 'o';
2094
	}
2138
	}
2095
      length = maxlen;
2139
      length = maxlen;
2096
    }
2140
    }
2097
 
2141
 
2098
  if (length < 16)
2142
  if (length < 16)
2099
    (hdr->ar_name)[length] = ar_padchar (abfd);
2143
    (hdr->ar_name)[length] = ar_padchar (abfd);
2100
}
2144
}
2101

2145

2102
/* The BFD is open for write and has its format set to bfd_archive.  */
2146
/* The BFD is open for write and has its format set to bfd_archive.  */
2103
 
2147
 
2104
bfd_boolean
2148
bfd_boolean
2105
_bfd_write_archive_contents (bfd *arch)
2149
_bfd_write_archive_contents (bfd *arch)
2106
{
2150
{
2107
  bfd *current;
2151
  bfd *current;
2108
  char *etable = NULL;
2152
  char *etable = NULL;
2109
  bfd_size_type elength = 0;
2153
  bfd_size_type elength = 0;
2110
  const char *ename = NULL;
2154
  const char *ename = NULL;
2111
  bfd_boolean makemap = bfd_has_map (arch);
2155
  bfd_boolean makemap = bfd_has_map (arch);
2112
  /* If no .o's, don't bother to make a map.  */
2156
  /* If no .o's, don't bother to make a map.  */
2113
  bfd_boolean hasobjects = FALSE;
2157
  bfd_boolean hasobjects = FALSE;
2114
  bfd_size_type wrote;
2158
  bfd_size_type wrote;
2115
  int tries;
2159
  int tries;
2116
  char *armag;
2160
  char *armag;
2117
 
2161
 
2118
  /* Verify the viability of all entries; if any of them live in the
2162
  /* Verify the viability of all entries; if any of them live in the
2119
     filesystem (as opposed to living in an archive open for input)
2163
     filesystem (as opposed to living in an archive open for input)
2120
     then construct a fresh ar_hdr for them.  */
2164
     then construct a fresh ar_hdr for them.  */
2121
  for (current = arch->archive_head;
2165
  for (current = arch->archive_head;
2122
       current != NULL;
2166
       current != NULL;
2123
       current = current->archive_next)
2167
       current = current->archive_next)
2124
    {
2168
    {
2125
      /* This check is checking the bfds for the objects we're reading
2169
      /* This check is checking the bfds for the objects we're reading
2126
	 from (which are usually either an object file or archive on
2170
	 from (which are usually either an object file or archive on
2127
	 disk), not the archive entries we're writing to.  We don't
2171
	 disk), not the archive entries we're writing to.  We don't
2128
	 actually create bfds for the archive members, we just copy
2172
	 actually create bfds for the archive members, we just copy
2129
	 them byte-wise when we write out the archive.  */
2173
	 them byte-wise when we write out the archive.  */
2130
      if (bfd_write_p (current))
2174
      if (bfd_write_p (current))
2131
	{
2175
	{
2132
	  bfd_set_error (bfd_error_invalid_operation);
2176
	  bfd_set_error (bfd_error_invalid_operation);
2133
	  goto input_err;
2177
	  goto input_err;
2134
	}
2178
	}
2135
      if (!current->arelt_data)
2179
      if (!current->arelt_data)
2136
	{
2180
	{
2137
	  current->arelt_data =
2181
	  current->arelt_data =
2138
	    bfd_ar_hdr_from_filesystem (arch, current->filename, current);
2182
	    bfd_ar_hdr_from_filesystem (arch, current->filename, current);
2139
	  if (!current->arelt_data)
2183
	  if (!current->arelt_data)
2140
	    goto input_err;
2184
	    goto input_err;
2141
 
2185
 
2142
	  /* Put in the file name.  */
2186
	  /* Put in the file name.  */
2143
	  BFD_SEND (arch, _bfd_truncate_arname,
2187
	  BFD_SEND (arch, _bfd_truncate_arname,
2144
		    (arch, current->filename, (char *) arch_hdr (current)));
2188
		    (arch, current->filename, (char *) arch_hdr (current)));
2145
	}
2189
	}
2146
 
2190
 
2147
      if (makemap && ! hasobjects)
2191
      if (makemap && ! hasobjects)
2148
	{			/* Don't bother if we won't make a map!  */
2192
	{			/* Don't bother if we won't make a map!  */
2149
	  if ((bfd_check_format (current, bfd_object)))
2193
	  if ((bfd_check_format (current, bfd_object)))
2150
	    hasobjects = TRUE;
2194
	    hasobjects = TRUE;
2151
	}
2195
	}
2152
    }
2196
    }
2153
 
2197
 
2154
  if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
2198
  if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
2155
		 (arch, &etable, &elength, &ename)))
2199
		 (arch, &etable, &elength, &ename)))
2156
    return FALSE;
2200
    return FALSE;
2157
 
2201
 
2158
  if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
2202
  if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
2159
    return FALSE;
2203
    return FALSE;
2160
  armag = ARMAG;
2204
  armag = ARMAG;
2161
  if (bfd_is_thin_archive (arch))
2205
  if (bfd_is_thin_archive (arch))
2162
    armag = ARMAGT;
2206
    armag = ARMAGT;
2163
  wrote = bfd_bwrite (armag, SARMAG, arch);
2207
  wrote = bfd_bwrite (armag, SARMAG, arch);
2164
  if (wrote != SARMAG)
2208
  if (wrote != SARMAG)
2165
    return FALSE;
2209
    return FALSE;
2166
 
2210
 
2167
  if (makemap && hasobjects)
2211
  if (makemap && hasobjects)
2168
    {
2212
    {
2169
      if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
2213
      if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
2170
	return FALSE;
2214
	return FALSE;
2171
    }
2215
    }
2172
 
2216
 
2173
  if (elength != 0)
2217
  if (elength != 0)
2174
    {
2218
    {
2175
      struct ar_hdr hdr;
2219
      struct ar_hdr hdr;
2176
 
2220
 
2177
      memset (&hdr, ' ', sizeof (struct ar_hdr));
2221
      memset (&hdr, ' ', sizeof (struct ar_hdr));
2178
      memcpy (hdr.ar_name, ename, strlen (ename));
2222
      memcpy (hdr.ar_name, ename, strlen (ename));
2179
      /* Round size up to even number in archive header.  */
2223
      /* Round size up to even number in archive header.  */
2180
      if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size),
2224
      if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size),
2181
			    (elength + 1) & ~(bfd_size_type) 1))
2225
			    (elength + 1) & ~(bfd_size_type) 1))
2182
	return FALSE;
2226
	return FALSE;
2183
      memcpy (hdr.ar_fmag, ARFMAG, 2);
2227
      memcpy (hdr.ar_fmag, ARFMAG, 2);
2184
      if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2228
      if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2185
	   != sizeof (struct ar_hdr))
2229
	   != sizeof (struct ar_hdr))
2186
	  || bfd_bwrite (etable, elength, arch) != elength)
2230
	  || bfd_bwrite (etable, elength, arch) != elength)
2187
	return FALSE;
2231
	return FALSE;
2188
      if ((elength % 2) == 1)
2232
      if ((elength % 2) == 1)
2189
	{
2233
	{
2190
	  if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
2234
	  if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
2191
	    return FALSE;
2235
	    return FALSE;
2192
	}
2236
	}
2193
    }
2237
    }
2194
 
2238
 
2195
  for (current = arch->archive_head;
2239
  for (current = arch->archive_head;
2196
       current != NULL;
2240
       current != NULL;
2197
       current = current->archive_next)
2241
       current = current->archive_next)
2198
    {
2242
    {
2199
      char buffer[DEFAULT_BUFFERSIZE];
2243
      char buffer[DEFAULT_BUFFERSIZE];
2200
      bfd_size_type remaining = arelt_size (current);
2244
      bfd_size_type remaining = arelt_size (current);
2201
 
2245
 
2202
      /* Write ar header.  */
2246
      /* Write ar header.  */
2203
      if (!_bfd_write_ar_hdr (arch, current))
2247
      if (!_bfd_write_ar_hdr (arch, current))
2204
	return FALSE;
2248
	return FALSE;
2205
      if (bfd_is_thin_archive (arch))
2249
      if (bfd_is_thin_archive (arch))
2206
	continue;
2250
	continue;
2207
      if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
2251
      if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
2208
	goto input_err;
2252
	goto input_err;
2209
 
2253
 
2210
      while (remaining)
2254
      while (remaining)
2211
	{
2255
	{
2212
	  unsigned int amt = DEFAULT_BUFFERSIZE;
2256
	  unsigned int amt = DEFAULT_BUFFERSIZE;
2213
 
2257
 
2214
	  if (amt > remaining)
2258
	  if (amt > remaining)
2215
	    amt = remaining;
2259
	    amt = remaining;
2216
	  errno = 0;
2260
	  errno = 0;
2217
	  if (bfd_bread (buffer, amt, current) != amt)
2261
	  if (bfd_bread (buffer, amt, current) != amt)
2218
	    {
2262
	    {
2219
	      if (bfd_get_error () != bfd_error_system_call)
2263
	      if (bfd_get_error () != bfd_error_system_call)
2220
		bfd_set_error (bfd_error_file_truncated);
2264
		bfd_set_error (bfd_error_file_truncated);
2221
	      goto input_err;
2265
	      goto input_err;
2222
	    }
2266
	    }
2223
	  if (bfd_bwrite (buffer, amt, arch) != amt)
2267
	  if (bfd_bwrite (buffer, amt, arch) != amt)
2224
	    return FALSE;
2268
	    return FALSE;
2225
	  remaining -= amt;
2269
	  remaining -= amt;
2226
	}
2270
	}
2227
 
2271
 
2228
      if ((arelt_size (current) % 2) == 1)
2272
      if ((arelt_size (current) % 2) == 1)
2229
	{
2273
	{
2230
	  if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
2274
	  if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
2231
	    return FALSE;
2275
	    return FALSE;
2232
	}
2276
	}
2233
    }
2277
    }
2234
 
2278
 
2235
  if (makemap && hasobjects)
2279
  if (makemap && hasobjects)
2236
    {
2280
    {
2237
      /* Verify the timestamp in the archive file.  If it would not be
2281
      /* Verify the timestamp in the archive file.  If it would not be
2238
	 accepted by the linker, rewrite it until it would be.  If
2282
	 accepted by the linker, rewrite it until it would be.  If
2239
	 anything odd happens, break out and just return.  (The
2283
	 anything odd happens, break out and just return.  (The
2240
	 Berkeley linker checks the timestamp and refuses to read the
2284
	 Berkeley linker checks the timestamp and refuses to read the
2241
	 table-of-contents if it is >60 seconds less than the file's
2285
	 table-of-contents if it is >60 seconds less than the file's
2242
	 modified-time.  That painful hack requires this painful hack.  */
2286
	 modified-time.  That painful hack requires this painful hack.  */
2243
      tries = 1;
2287
      tries = 1;
2244
      do
2288
      do
2245
	{
2289
	{
2246
	  if (bfd_update_armap_timestamp (arch))
2290
	  if (bfd_update_armap_timestamp (arch))
2247
	    break;
2291
	    break;
2248
	  (*_bfd_error_handler)
2292
	  (*_bfd_error_handler)
2249
	    (_("Warning: writing archive was slow: rewriting timestamp\n"));
2293
	    (_("Warning: writing archive was slow: rewriting timestamp\n"));
2250
	}
2294
	}
2251
      while (++tries < 6);
2295
      while (++tries < 6);
2252
    }
2296
    }
2253
 
2297
 
2254
  return TRUE;
2298
  return TRUE;
2255
 
2299
 
2256
 input_err:
2300
 input_err:
2257
  bfd_set_error (bfd_error_on_input, current, bfd_get_error ());
2301
  bfd_set_error (bfd_error_on_input, current, bfd_get_error ());
2258
  return FALSE;
2302
  return FALSE;
2259
}
2303
}
2260

2304

2261
/* Note that the namidx for the first symbol is 0.  */
2305
/* Note that the namidx for the first symbol is 0.  */
2262
 
2306
 
2263
bfd_boolean
2307
bfd_boolean
2264
_bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
2308
_bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
2265
{
2309
{
2266
  char *first_name = NULL;
2310
  char *first_name = NULL;
2267
  bfd *current;
2311
  bfd *current;
2268
  file_ptr elt_no = 0;
2312
  file_ptr elt_no = 0;
2269
  struct orl *map = NULL;
2313
  struct orl *map = NULL;
2270
  unsigned int orl_max = 1024;		/* Fine initial default.  */
2314
  unsigned int orl_max = 1024;		/* Fine initial default.  */
2271
  unsigned int orl_count = 0;
2315
  unsigned int orl_count = 0;
2272
  int stridx = 0;
2316
  int stridx = 0;
2273
  asymbol **syms = NULL;
2317
  asymbol **syms = NULL;
2274
  long syms_max = 0;
2318
  long syms_max = 0;
2275
  bfd_boolean ret;
2319
  bfd_boolean ret;
2276
  bfd_size_type amt;
2320
  bfd_size_type amt;
2277
 
2321
 
2278
  /* Dunno if this is the best place for this info...  */
2322
  /* Dunno if this is the best place for this info...  */
2279
  if (elength != 0)
2323
  if (elength != 0)
2280
    elength += sizeof (struct ar_hdr);
2324
    elength += sizeof (struct ar_hdr);
2281
  elength += elength % 2;
2325
  elength += elength % 2;
2282
 
2326
 
2283
  amt = orl_max * sizeof (struct orl);
2327
  amt = orl_max * sizeof (struct orl);
2284
  map = (struct orl *) bfd_malloc (amt);
2328
  map = (struct orl *) bfd_malloc (amt);
2285
  if (map == NULL)
2329
  if (map == NULL)
2286
    goto error_return;
2330
    goto error_return;
2287
 
2331
 
2288
  /* We put the symbol names on the arch objalloc, and then discard
2332
  /* We put the symbol names on the arch objalloc, and then discard
2289
     them when done.  */
2333
     them when done.  */
2290
  first_name = (char *) bfd_alloc (arch, 1);
2334
  first_name = (char *) bfd_alloc (arch, 1);
2291
  if (first_name == NULL)
2335
  if (first_name == NULL)
2292
    goto error_return;
2336
    goto error_return;
2293
 
2337
 
2294
  /* Drop all the files called __.SYMDEF, we're going to make our own.  */
2338
  /* Drop all the files called __.SYMDEF, we're going to make our own.  */
2295
  while (arch->archive_head
2339
  while (arch->archive_head
2296
	 && strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
2340
	 && strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
2297
    arch->archive_head = arch->archive_head->archive_next;
2341
    arch->archive_head = arch->archive_head->archive_next;
2298
 
2342
 
2299
  /* Map over each element.  */
2343
  /* Map over each element.  */
2300
  for (current = arch->archive_head;
2344
  for (current = arch->archive_head;
2301
       current != NULL;
2345
       current != NULL;
2302
       current = current->archive_next, elt_no++)
2346
       current = current->archive_next, elt_no++)
2303
    {
2347
    {
2304
      if (bfd_check_format (current, bfd_object)
2348
      if (bfd_check_format (current, bfd_object)
2305
	  && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
2349
	  && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
2306
	{
2350
	{
2307
	  long storage;
2351
	  long storage;
2308
	  long symcount;
2352
	  long symcount;
2309
	  long src_count;
2353
	  long src_count;
2310
 
2354
 
2311
	  storage = bfd_get_symtab_upper_bound (current);
2355
	  storage = bfd_get_symtab_upper_bound (current);
2312
	  if (storage < 0)
2356
	  if (storage < 0)
2313
	    goto error_return;
2357
	    goto error_return;
2314
 
2358
 
2315
	  if (storage != 0)
2359
	  if (storage != 0)
2316
	    {
2360
	    {
2317
	      if (storage > syms_max)
2361
	      if (storage > syms_max)
2318
		{
2362
		{
2319
		  if (syms_max > 0)
2363
		  if (syms_max > 0)
2320
		    free (syms);
2364
		    free (syms);
2321
		  syms_max = storage;
2365
		  syms_max = storage;
2322
		  syms = (asymbol **) bfd_malloc (syms_max);
2366
		  syms = (asymbol **) bfd_malloc (syms_max);
2323
		  if (syms == NULL)
2367
		  if (syms == NULL)
2324
		    goto error_return;
2368
		    goto error_return;
2325
		}
2369
		}
2326
	      symcount = bfd_canonicalize_symtab (current, syms);
2370
	      symcount = bfd_canonicalize_symtab (current, syms);
2327
	      if (symcount < 0)
2371
	      if (symcount < 0)
2328
		goto error_return;
2372
		goto error_return;
2329
 
2373
 
2330
	      /* Now map over all the symbols, picking out the ones we
2374
	      /* Now map over all the symbols, picking out the ones we
2331
		 want.  */
2375
		 want.  */
2332
	      for (src_count = 0; src_count < symcount; src_count++)
2376
	      for (src_count = 0; src_count < symcount; src_count++)
2333
		{
2377
		{
2334
		  flagword flags = (syms[src_count])->flags;
2378
		  flagword flags = (syms[src_count])->flags;
2335
		  asection *sec = syms[src_count]->section;
2379
		  asection *sec = syms[src_count]->section;
2336
 
2380
 
2337
		  if (((flags & (BSF_GLOBAL
2381
		  if (((flags & (BSF_GLOBAL
2338
				 | BSF_WEAK
2382
				 | BSF_WEAK
2339
				 | BSF_INDIRECT
2383
				 | BSF_INDIRECT
2340
				 | BSF_GNU_UNIQUE)) != 0
2384
				 | BSF_GNU_UNIQUE)) != 0
2341
		       || bfd_is_com_section (sec))
2385
		       || bfd_is_com_section (sec))
2342
		      && ! bfd_is_und_section (sec))
2386
		      && ! bfd_is_und_section (sec))
2343
		    {
2387
		    {
2344
		      bfd_size_type namelen;
2388
		      bfd_size_type namelen;
2345
		      struct orl *new_map;
2389
		      struct orl *new_map;
2346
 
2390
 
2347
		      /* This symbol will go into the archive header.  */
2391
		      /* This symbol will go into the archive header.  */
2348
		      if (orl_count == orl_max)
2392
		      if (orl_count == orl_max)
2349
			{
2393
			{
2350
			  orl_max *= 2;
2394
			  orl_max *= 2;
2351
			  amt = orl_max * sizeof (struct orl);
2395
			  amt = orl_max * sizeof (struct orl);
2352
			  new_map = (struct orl *) bfd_realloc (map, amt);
2396
			  new_map = (struct orl *) bfd_realloc (map, amt);
2353
			  if (new_map == NULL)
2397
			  if (new_map == NULL)
2354
			    goto error_return;
2398
			    goto error_return;
2355
 
2399
 
2356
			  map = new_map;
2400
			  map = new_map;
2357
			}
2401
			}
-
 
2402
 
-
 
2403
		      if (strcmp (syms[src_count]->name, "__gnu_lto_slim") == 0)
-
 
2404
			(*_bfd_error_handler)
-
 
2405
			  (_("%s: plugin needed to handle lto object"),
2358
 
2406
			   bfd_get_filename (current));
2359
		      namelen = strlen (syms[src_count]->name);
2407
		      namelen = strlen (syms[src_count]->name);
2360
		      amt = sizeof (char *);
2408
		      amt = sizeof (char *);
2361
		      map[orl_count].name = (char **) bfd_alloc (arch, amt);
2409
		      map[orl_count].name = (char **) bfd_alloc (arch, amt);
2362
		      if (map[orl_count].name == NULL)
2410
		      if (map[orl_count].name == NULL)
2363
			goto error_return;
2411
			goto error_return;
2364
		      *(map[orl_count].name) = (char *) bfd_alloc (arch,
2412
		      *(map[orl_count].name) = (char *) bfd_alloc (arch,
2365
								   namelen + 1);
2413
								   namelen + 1);
2366
		      if (*(map[orl_count].name) == NULL)
2414
		      if (*(map[orl_count].name) == NULL)
2367
			goto error_return;
2415
			goto error_return;
2368
		      strcpy (*(map[orl_count].name), syms[src_count]->name);
2416
		      strcpy (*(map[orl_count].name), syms[src_count]->name);
2369
		      map[orl_count].u.abfd = current;
2417
		      map[orl_count].u.abfd = current;
2370
		      map[orl_count].namidx = stridx;
2418
		      map[orl_count].namidx = stridx;
2371
 
2419
 
2372
		      stridx += namelen + 1;
2420
		      stridx += namelen + 1;
2373
		      ++orl_count;
2421
		      ++orl_count;
2374
		    }
2422
		    }
2375
		}
2423
		}
2376
	    }
2424
	    }
2377
 
2425
 
2378
	  /* Now ask the BFD to free up any cached information, so we
2426
	  /* Now ask the BFD to free up any cached information, so we
2379
	     don't fill all of memory with symbol tables.  */
2427
	     don't fill all of memory with symbol tables.  */
2380
	  if (! bfd_free_cached_info (current))
2428
	  if (! bfd_free_cached_info (current))
2381
	    goto error_return;
2429
	    goto error_return;
2382
	}
2430
	}
2383
    }
2431
    }
2384
 
2432
 
2385
  /* OK, now we have collected all the data, let's write them out.  */
2433
  /* OK, now we have collected all the data, let's write them out.  */
2386
  ret = BFD_SEND (arch, write_armap,
2434
  ret = BFD_SEND (arch, write_armap,
2387
		  (arch, elength, map, orl_count, stridx));
2435
		  (arch, elength, map, orl_count, stridx));
2388
 
2436
 
2389
  if (syms_max > 0)
2437
  if (syms_max > 0)
2390
    free (syms);
2438
    free (syms);
2391
  if (map != NULL)
2439
  if (map != NULL)
2392
    free (map);
2440
    free (map);
2393
  if (first_name != NULL)
2441
  if (first_name != NULL)
2394
    bfd_release (arch, first_name);
2442
    bfd_release (arch, first_name);
2395
 
2443
 
2396
  return ret;
2444
  return ret;
2397
 
2445
 
2398
 error_return:
2446
 error_return:
2399
  if (syms_max > 0)
2447
  if (syms_max > 0)
2400
    free (syms);
2448
    free (syms);
2401
  if (map != NULL)
2449
  if (map != NULL)
2402
    free (map);
2450
    free (map);
2403
  if (first_name != NULL)
2451
  if (first_name != NULL)
2404
    bfd_release (arch, first_name);
2452
    bfd_release (arch, first_name);
2405
 
2453
 
2406
  return FALSE;
2454
  return FALSE;
2407
}
2455
}
2408
 
2456
 
2409
bfd_boolean
2457
bfd_boolean
2410
bsd_write_armap (bfd *arch,
2458
bsd_write_armap (bfd *arch,
2411
		 unsigned int elength,
2459
		 unsigned int elength,
2412
		 struct orl *map,
2460
		 struct orl *map,
2413
		 unsigned int orl_count,
2461
		 unsigned int orl_count,
2414
		 int stridx)
2462
		 int stridx)
2415
{
2463
{
2416
  int padit = stridx & 1;
2464
  int padit = stridx & 1;
2417
  unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
2465
  unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
2418
  unsigned int stringsize = stridx + padit;
2466
  unsigned int stringsize = stridx + padit;
2419
  /* Include 8 bytes to store ranlibsize and stringsize in output.  */
2467
  /* Include 8 bytes to store ranlibsize and stringsize in output.  */
2420
  unsigned int mapsize = ranlibsize + stringsize + 8;
2468
  unsigned int mapsize = ranlibsize + stringsize + 8;
2421
  file_ptr firstreal;
2469
  file_ptr firstreal;
2422
  bfd *current = arch->archive_head;
2470
  bfd *current = arch->archive_head;
2423
  bfd *last_elt = current;	/* Last element arch seen.  */
2471
  bfd *last_elt = current;	/* Last element arch seen.  */
2424
  bfd_byte temp[4];
2472
  bfd_byte temp[4];
2425
  unsigned int count;
2473
  unsigned int count;
2426
  struct ar_hdr hdr;
2474
  struct ar_hdr hdr;
2427
  long uid, gid;
2475
  long uid, gid;
2428
 
2476
 
2429
  firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
2477
  firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
2430
 
2478
 
2431
  /* If deterministic, we use 0 as the timestamp in the map.
2479
  /* If deterministic, we use 0 as the timestamp in the map.
2432
     Some linkers may require that the archive filesystem modification
2480
     Some linkers may require that the archive filesystem modification
2433
     time is less than (or near to) the archive map timestamp.  Those
2481
     time is less than (or near to) the archive map timestamp.  Those
2434
     linkers should not be used with deterministic mode.  (GNU ld and
2482
     linkers should not be used with deterministic mode.  (GNU ld and
2435
     Gold do not have this restriction.)  */
2483
     Gold do not have this restriction.)  */
2436
  bfd_ardata (arch)->armap_timestamp = 0;
2484
  bfd_ardata (arch)->armap_timestamp = 0;
2437
  uid = 0;
2485
  uid = 0;
2438
  gid = 0;
2486
  gid = 0;
2439
  if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0)
2487
  if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0)
2440
    {
2488
    {
2441
      struct stat statbuf;
2489
      struct stat statbuf;
2442
 
2490
 
2443
      if (stat (arch->filename, &statbuf) == 0)
2491
      if (stat (arch->filename, &statbuf) == 0)
2444
	bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime
2492
	bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime
2445
					      + ARMAP_TIME_OFFSET);
2493
					      + ARMAP_TIME_OFFSET);
2446
      uid = getuid();
2494
      uid = getuid();
2447
      gid = getgid();
2495
      gid = getgid();
2448
    }
2496
    }
2449
 
2497
 
2450
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2498
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2451
  memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
2499
  memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
2452
  bfd_ardata (arch)->armap_datepos = (SARMAG
2500
  bfd_ardata (arch)->armap_datepos = (SARMAG
2453
				      + offsetof (struct ar_hdr, ar_date[0]));
2501
				      + offsetof (struct ar_hdr, ar_date[0]));
2454
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2502
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2455
		    bfd_ardata (arch)->armap_timestamp);
2503
		    bfd_ardata (arch)->armap_timestamp);
2456
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid);
2504
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid);
2457
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid);
2505
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid);
2458
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2506
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2459
    return FALSE;
2507
    return FALSE;
2460
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2508
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2461
  if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2509
  if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2462
      != sizeof (struct ar_hdr))
2510
      != sizeof (struct ar_hdr))
2463
    return FALSE;
2511
    return FALSE;
2464
  H_PUT_32 (arch, ranlibsize, temp);
2512
  H_PUT_32 (arch, ranlibsize, temp);
2465
  if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
2513
  if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
2466
    return FALSE;
2514
    return FALSE;
2467
 
2515
 
2468
  for (count = 0; count < orl_count; count++)
2516
  for (count = 0; count < orl_count; count++)
2469
    {
2517
    {
2470
      unsigned int offset;
2518
      unsigned int offset;
2471
      bfd_byte buf[BSD_SYMDEF_SIZE];
2519
      bfd_byte buf[BSD_SYMDEF_SIZE];
2472
 
2520
 
2473
      if (map[count].u.abfd != last_elt)
2521
      if (map[count].u.abfd != last_elt)
2474
	{
2522
	{
2475
	  do
2523
	  do
2476
	    {
2524
	    {
2477
	      struct areltdata *ared = arch_eltdata (current);
2525
	      struct areltdata *ared = arch_eltdata (current);
2478
 
2526
 
2479
	      firstreal += (ared->parsed_size + ared->extra_size
2527
	      firstreal += (ared->parsed_size + ared->extra_size
2480
			    + sizeof (struct ar_hdr));
2528
			    + sizeof (struct ar_hdr));
2481
	      firstreal += firstreal % 2;
2529
	      firstreal += firstreal % 2;
2482
	      current = current->archive_next;
2530
	      current = current->archive_next;
2483
	    }
2531
	    }
2484
	  while (current != map[count].u.abfd);
2532
	  while (current != map[count].u.abfd);
2485
	}
2533
	}
2486
 
2534
 
2487
      /* The archive file format only has 4 bytes to store the offset
2535
      /* The archive file format only has 4 bytes to store the offset
2488
	 of the member.  Check to make sure that firstreal has not grown
2536
	 of the member.  Check to make sure that firstreal has not grown
2489
	 too big.  */
2537
	 too big.  */
2490
      offset = (unsigned int) firstreal;
2538
      offset = (unsigned int) firstreal;
2491
      if (firstreal != (file_ptr) offset)
2539
      if (firstreal != (file_ptr) offset)
2492
	{
2540
	{
2493
	  bfd_set_error (bfd_error_file_truncated);
2541
	  bfd_set_error (bfd_error_file_truncated);
2494
	  return FALSE;
2542
	  return FALSE;
2495
	}
2543
	}
2496
 
2544
 
2497
      last_elt = current;
2545
      last_elt = current;
2498
      H_PUT_32 (arch, map[count].namidx, buf);
2546
      H_PUT_32 (arch, map[count].namidx, buf);
2499
      H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE);
2547
      H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE);
2500
      if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch)
2548
      if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch)
2501
	  != BSD_SYMDEF_SIZE)
2549
	  != BSD_SYMDEF_SIZE)
2502
	return FALSE;
2550
	return FALSE;
2503
    }
2551
    }
2504
 
2552
 
2505
  /* Now write the strings themselves.  */
2553
  /* Now write the strings themselves.  */
2506
  H_PUT_32 (arch, stringsize, temp);
2554
  H_PUT_32 (arch, stringsize, temp);
2507
  if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
2555
  if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
2508
    return FALSE;
2556
    return FALSE;
2509
  for (count = 0; count < orl_count; count++)
2557
  for (count = 0; count < orl_count; count++)
2510
    {
2558
    {
2511
      size_t len = strlen (*map[count].name) + 1;
2559
      size_t len = strlen (*map[count].name) + 1;
2512
 
2560
 
2513
      if (bfd_bwrite (*map[count].name, len, arch) != len)
2561
      if (bfd_bwrite (*map[count].name, len, arch) != len)
2514
	return FALSE;
2562
	return FALSE;
2515
    }
2563
    }
2516
 
2564
 
2517
  /* The spec sez this should be a newline.  But in order to be
2565
  /* The spec sez this should be a newline.  But in order to be
2518
     bug-compatible for sun's ar we use a null.  */
2566
     bug-compatible for sun's ar we use a null.  */
2519
  if (padit)
2567
  if (padit)
2520
    {
2568
    {
2521
      if (bfd_bwrite ("", 1, arch) != 1)
2569
      if (bfd_bwrite ("", 1, arch) != 1)
2522
	return FALSE;
2570
	return FALSE;
2523
    }
2571
    }
2524
 
2572
 
2525
  return TRUE;
2573
  return TRUE;
2526
}
2574
}
2527
 
2575
 
2528
/* At the end of archive file handling, update the timestamp in the
2576
/* At the end of archive file handling, update the timestamp in the
2529
   file, so the linker will accept it.
2577
   file, so the linker will accept it.
2530
 
2578
 
2531
   Return TRUE if the timestamp was OK, or an unusual problem happened.
2579
   Return TRUE if the timestamp was OK, or an unusual problem happened.
2532
   Return FALSE if we updated the timestamp.  */
2580
   Return FALSE if we updated the timestamp.  */
2533
 
2581
 
2534
bfd_boolean
2582
bfd_boolean
2535
_bfd_archive_bsd_update_armap_timestamp (bfd *arch)
2583
_bfd_archive_bsd_update_armap_timestamp (bfd *arch)
2536
{
2584
{
2537
  struct stat archstat;
2585
  struct stat archstat;
2538
  struct ar_hdr hdr;
2586
  struct ar_hdr hdr;
2539
 
2587
 
2540
  /* If creating deterministic archives, just leave the timestamp as-is.  */
2588
  /* If creating deterministic archives, just leave the timestamp as-is.  */
2541
  if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2589
  if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2542
    return TRUE;
2590
    return TRUE;
2543
 
2591
 
2544
  /* Flush writes, get last-write timestamp from file, and compare it
2592
  /* Flush writes, get last-write timestamp from file, and compare it
2545
     to the timestamp IN the file.  */
2593
     to the timestamp IN the file.  */
2546
  bfd_flush (arch);
2594
  bfd_flush (arch);
2547
  if (bfd_stat (arch, &archstat) == -1)
2595
  if (bfd_stat (arch, &archstat) == -1)
2548
    {
2596
    {
2549
      bfd_perror (_("Reading archive file mod timestamp"));
2597
      bfd_perror (_("Reading archive file mod timestamp"));
2550
 
2598
 
2551
      /* Can't read mod time for some reason.  */
2599
      /* Can't read mod time for some reason.  */
2552
      return TRUE;
2600
      return TRUE;
2553
    }
2601
    }
2554
  if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp)
2602
  if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp)
2555
    /* OK by the linker's rules.  */
2603
    /* OK by the linker's rules.  */
2556
    return TRUE;
2604
    return TRUE;
2557
 
2605
 
2558
  /* Update the timestamp.  */
2606
  /* Update the timestamp.  */
2559
  bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2607
  bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2560
 
2608
 
2561
  /* Prepare an ASCII version suitable for writing.  */
2609
  /* Prepare an ASCII version suitable for writing.  */
2562
  memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2610
  memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2563
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2611
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2564
		    bfd_ardata (arch)->armap_timestamp);
2612
		    bfd_ardata (arch)->armap_timestamp);
2565
 
2613
 
2566
  /* Write it into the file.  */
2614
  /* Write it into the file.  */
2567
  bfd_ardata (arch)->armap_datepos = (SARMAG
2615
  bfd_ardata (arch)->armap_datepos = (SARMAG
2568
				      + offsetof (struct ar_hdr, ar_date[0]));
2616
				      + offsetof (struct ar_hdr, ar_date[0]));
2569
  if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2617
  if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2570
      || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
2618
      || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
2571
	  != sizeof (hdr.ar_date)))
2619
	  != sizeof (hdr.ar_date)))
2572
    {
2620
    {
2573
      bfd_perror (_("Writing updated armap timestamp"));
2621
      bfd_perror (_("Writing updated armap timestamp"));
2574
 
2622
 
2575
      /* Some error while writing.  */
2623
      /* Some error while writing.  */
2576
      return TRUE;
2624
      return TRUE;
2577
    }
2625
    }
2578
 
2626
 
2579
  /* We updated the timestamp successfully.  */
2627
  /* We updated the timestamp successfully.  */
2580
  return FALSE;
2628
  return FALSE;
2581
}
2629
}
2582

2630

2583
/* A coff armap looks like :
2631
/* A coff armap looks like :
2584
   lARMAG
2632
   lARMAG
2585
   struct ar_hdr with name = '/'
2633
   struct ar_hdr with name = '/'
2586
   number of symbols
2634
   number of symbols
2587
   offset of file for symbol 0
2635
   offset of file for symbol 0
2588
   offset of file for symbol 1
2636
   offset of file for symbol 1
2589
 
2637
 
2590
   offset of file for symbol n-1
2638
   offset of file for symbol n-1
2591
   symbol name 0
2639
   symbol name 0
2592
   symbol name 1
2640
   symbol name 1
2593
 
2641
 
2594
   symbol name n-1  */
2642
   symbol name n-1  */
2595
 
2643
 
2596
bfd_boolean
2644
bfd_boolean
2597
coff_write_armap (bfd *arch,
2645
coff_write_armap (bfd *arch,
2598
		  unsigned int elength,
2646
		  unsigned int elength,
2599
		  struct orl *map,
2647
		  struct orl *map,
2600
		  unsigned int symbol_count,
2648
		  unsigned int symbol_count,
2601
		  int stridx)
2649
		  int stridx)
2602
{
2650
{
2603
  /* The size of the ranlib is the number of exported symbols in the
2651
  /* The size of the ranlib is the number of exported symbols in the
2604
     archive * the number of bytes in an int, + an int for the count.  */
2652
     archive * the number of bytes in an int, + an int for the count.  */
2605
  unsigned int ranlibsize = (symbol_count * 4) + 4;
2653
  unsigned int ranlibsize = (symbol_count * 4) + 4;
2606
  unsigned int stringsize = stridx;
2654
  unsigned int stringsize = stridx;
2607
  unsigned int mapsize = stringsize + ranlibsize;
2655
  unsigned int mapsize = stringsize + ranlibsize;
2608
  file_ptr archive_member_file_ptr;
2656
  file_ptr archive_member_file_ptr;
2609
  bfd *current = arch->archive_head;
2657
  bfd *current = arch->archive_head;
2610
  unsigned int count;
2658
  unsigned int count;
2611
  struct ar_hdr hdr;
2659
  struct ar_hdr hdr;
2612
  int padit = mapsize & 1;
2660
  int padit = mapsize & 1;
2613
 
2661
 
2614
  if (padit)
2662
  if (padit)
2615
    mapsize++;
2663
    mapsize++;
2616
 
2664
 
2617
  /* Work out where the first object file will go in the archive.  */
2665
  /* Work out where the first object file will go in the archive.  */
2618
  archive_member_file_ptr = (mapsize
2666
  archive_member_file_ptr = (mapsize
2619
			     + elength
2667
			     + elength
2620
			     + sizeof (struct ar_hdr)
2668
			     + sizeof (struct ar_hdr)
2621
			     + SARMAG);
2669
			     + SARMAG);
2622
 
2670
 
2623
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2671
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2624
  hdr.ar_name[0] = '/';
2672
  hdr.ar_name[0] = '/';
2625
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2673
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2626
    return FALSE;
2674
    return FALSE;
2627
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2675
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2628
		    ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0
2676
		    ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0
2629
		     ? time (NULL) : 0));
2677
		     ? time (NULL) : 0));
2630
  /* This, at least, is what Intel coff sets the values to.  */
2678
  /* This, at least, is what Intel coff sets the values to.  */
2631
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2679
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2632
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2680
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2633
  _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2681
  _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2634
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2682
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2635
 
2683
 
2636
  /* Write the ar header for this item and the number of symbols.  */
2684
  /* Write the ar header for this item and the number of symbols.  */
2637
  if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2685
  if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2638
      != sizeof (struct ar_hdr))
2686
      != sizeof (struct ar_hdr))
2639
    return FALSE;
2687
    return FALSE;
2640
 
2688
 
2641
  if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
2689
  if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
2642
    return FALSE;
2690
    return FALSE;
2643
 
2691
 
2644
  /* Two passes, first write the file offsets for each symbol -
2692
  /* Two passes, first write the file offsets for each symbol -
2645
     remembering that each offset is on a two byte boundary.  */
2693
     remembering that each offset is on a two byte boundary.  */
2646
 
2694
 
2647
  /* Write out the file offset for the file associated with each
2695
  /* Write out the file offset for the file associated with each
2648
     symbol, and remember to keep the offsets padded out.  */
2696
     symbol, and remember to keep the offsets padded out.  */
2649
 
2697
 
2650
  current = arch->archive_head;
2698
  current = arch->archive_head;
2651
  count = 0;
2699
  count = 0;
2652
  while (current != NULL && count < symbol_count)
2700
  while (current != NULL && count < symbol_count)
2653
    {
2701
    {
2654
      /* For each symbol which is used defined in this object, write
2702
      /* For each symbol which is used defined in this object, write
2655
	 out the object file's address in the archive.  */
2703
	 out the object file's address in the archive.  */
2656
 
2704
 
2657
      while (count < symbol_count && map[count].u.abfd == current)
2705
      while (count < symbol_count && map[count].u.abfd == current)
2658
	{
2706
	{
2659
	  unsigned int offset = (unsigned int) archive_member_file_ptr;
2707
	  unsigned int offset = (unsigned int) archive_member_file_ptr;
2660
 
2708
 
2661
	  /* Catch an attempt to grow an archive past its 4Gb limit.  */
2709
	  /* Catch an attempt to grow an archive past its 4Gb limit.  */
2662
	  if (archive_member_file_ptr != (file_ptr) offset)
2710
	  if (archive_member_file_ptr != (file_ptr) offset)
2663
	    {
2711
	    {
2664
	      bfd_set_error (bfd_error_file_truncated);
2712
	      bfd_set_error (bfd_error_file_truncated);
2665
	      return FALSE;
2713
	      return FALSE;
2666
	    }
2714
	    }
2667
	  if (!bfd_write_bigendian_4byte_int (arch, offset))
2715
	  if (!bfd_write_bigendian_4byte_int (arch, offset))
2668
	    return FALSE;
2716
	    return FALSE;
2669
	  count++;
2717
	  count++;
2670
	}
2718
	}
2671
      archive_member_file_ptr += sizeof (struct ar_hdr);
2719
      archive_member_file_ptr += sizeof (struct ar_hdr);
2672
      if (! bfd_is_thin_archive (arch))
2720
      if (! bfd_is_thin_archive (arch))
2673
	{
2721
	{
2674
	  /* Add size of this archive entry.  */
2722
	  /* Add size of this archive entry.  */
2675
	  archive_member_file_ptr += arelt_size (current);
2723
	  archive_member_file_ptr += arelt_size (current);
2676
	  /* Remember about the even alignment.  */
2724
	  /* Remember about the even alignment.  */
2677
	  archive_member_file_ptr += archive_member_file_ptr % 2;
2725
	  archive_member_file_ptr += archive_member_file_ptr % 2;
2678
	}
2726
	}
2679
      current = current->archive_next;
2727
      current = current->archive_next;
2680
    }
2728
    }
2681
 
2729
 
2682
  /* Now write the strings themselves.  */
2730
  /* Now write the strings themselves.  */
2683
  for (count = 0; count < symbol_count; count++)
2731
  for (count = 0; count < symbol_count; count++)
2684
    {
2732
    {
2685
      size_t len = strlen (*map[count].name) + 1;
2733
      size_t len = strlen (*map[count].name) + 1;
2686
 
2734
 
2687
      if (bfd_bwrite (*map[count].name, len, arch) != len)
2735
      if (bfd_bwrite (*map[count].name, len, arch) != len)
2688
	return FALSE;
2736
	return FALSE;
2689
    }
2737
    }
2690
 
2738
 
2691
  /* The spec sez this should be a newline.  But in order to be
2739
  /* The spec sez this should be a newline.  But in order to be
2692
     bug-compatible for arc960 we use a null.  */
2740
     bug-compatible for arc960 we use a null.  */
2693
  if (padit)
2741
  if (padit)
2694
    {
2742
    {
2695
      if (bfd_bwrite ("", 1, arch) != 1)
2743
      if (bfd_bwrite ("", 1, arch) != 1)
2696
	return FALSE;
2744
	return FALSE;
2697
    }
2745
    }
2698
 
2746
 
2699
  return TRUE;
2747
  return TRUE;
2700
}
2748
}
2701
 
2749
 
2702
static int
2750
static int
2703
archive_close_worker (void **slot, void *inf ATTRIBUTE_UNUSED)
2751
archive_close_worker (void **slot, void *inf ATTRIBUTE_UNUSED)
2704
{
2752
{
2705
  struct ar_cache *ent = (struct ar_cache *) *slot;
2753
  struct ar_cache *ent = (struct ar_cache *) *slot;
2706
 
2754
 
2707
  bfd_close_all_done (ent->arbfd);
2755
  bfd_close_all_done (ent->arbfd);
2708
  return 1;
2756
  return 1;
2709
}
2757
}
2710
 
2758
 
2711
bfd_boolean
2759
bfd_boolean
2712
_bfd_archive_close_and_cleanup (bfd *abfd)
2760
_bfd_archive_close_and_cleanup (bfd *abfd)
2713
{
2761
{
2714
  if (bfd_read_p (abfd) && abfd->format == bfd_archive)
2762
  if (bfd_read_p (abfd) && abfd->format == bfd_archive)
2715
    {
2763
    {
2716
      bfd *nbfd;
2764
      bfd *nbfd;
2717
      bfd *next;
2765
      bfd *next;
2718
      htab_t htab;
2766
      htab_t htab;
2719
 
2767
 
2720
      /* Close nested archives (if this bfd is a thin archive).  */
2768
      /* Close nested archives (if this bfd is a thin archive).  */
2721
      for (nbfd = abfd->nested_archives; nbfd; nbfd = next)
2769
      for (nbfd = abfd->nested_archives; nbfd; nbfd = next)
2722
	{
2770
	{
2723
	  next = nbfd->archive_next;
2771
	  next = nbfd->archive_next;
2724
	  bfd_close (nbfd);
2772
	  bfd_close (nbfd);
2725
	}
2773
	}
2726
 
2774
 
2727
      htab = bfd_ardata (abfd)->cache;
2775
      htab = bfd_ardata (abfd)->cache;
2728
      if (htab)
2776
      if (htab)
2729
	{
2777
	{
2730
	  htab_traverse_noresize (htab, archive_close_worker, NULL);
2778
	  htab_traverse_noresize (htab, archive_close_worker, NULL);
2731
	  htab_delete (htab);
2779
	  htab_delete (htab);
2732
	  bfd_ardata (abfd)->cache = NULL;
2780
	  bfd_ardata (abfd)->cache = NULL;
2733
	}
2781
	}
2734
    }
2782
    }
2735
  if (arch_eltdata (abfd) != NULL)
2783
  if (arch_eltdata (abfd) != NULL)
2736
    {
2784
    {
2737
      struct areltdata *ared = arch_eltdata (abfd);
2785
      struct areltdata *ared = arch_eltdata (abfd);
2738
      htab_t htab = (htab_t) ared->parent_cache;
2786
      htab_t htab = (htab_t) ared->parent_cache;
2739
 
2787
 
2740
      if (htab)
2788
      if (htab)
2741
	{
2789
	{
2742
	  struct ar_cache ent;
2790
	  struct ar_cache ent;
2743
	  void **slot;
2791
	  void **slot;
2744
 
2792
 
2745
	  ent.ptr = ared->key;
2793
	  ent.ptr = ared->key;
2746
	  slot = htab_find_slot (htab, &ent, NO_INSERT);
2794
	  slot = htab_find_slot (htab, &ent, NO_INSERT);
2747
	  if (slot != NULL)
2795
	  if (slot != NULL)
2748
	    {
2796
	    {
2749
	      BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd);
2797
	      BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd);
2750
	      htab_clear_slot (htab, slot);
2798
	      htab_clear_slot (htab, slot);
2751
	    }
2799
	    }
2752
	}
2800
	}
2753
    }
2801
    }
-
 
2802
  if (abfd->is_linker_output)
-
 
2803
    (*abfd->link.hash->hash_table_free) (abfd);
-
 
2804
 
2754
  return TRUE;
2805
  return TRUE;
2755
}
2806
}