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