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