Commit | Line | Data |
---|---|---|
3284fe0c | 1 | /* elfcomm.c -- common code for ELF format file. |
250d07de | 2 | Copyright (C) 2010-2021 Free Software Foundation, Inc. |
3284fe0c L |
3 | |
4 | Originally developed by Eric Youngdale <eric@andante.jic.com> | |
5 | Modifications by Nick Clifton <nickc@redhat.com> | |
6 | ||
7 | This file is part of GNU Binutils. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA | |
22 | 02110-1301, USA. */ | |
23 | ||
81a65eb3 AM |
24 | /* Do not include bfd.h in this file. Functions in this file are used |
25 | by readelf.c and elfedit.c which define BFD64, and by objdump.c | |
26 | which doesn't. */ | |
645ba681 | 27 | |
3284fe0c L |
28 | #include "sysdep.h" |
29 | #include "libiberty.h" | |
24d127aa | 30 | #include "bfd.h" |
3284fe0c | 31 | #include "filenames.h" |
3284fe0c | 32 | #include "aout/ar.h" |
3284fe0c | 33 | #include "elfcomm.h" |
c2a7d3f5 | 34 | #include <assert.h> |
3284fe0c | 35 | |
81a65eb3 AM |
36 | extern char *program_name; |
37 | ||
3284fe0c L |
38 | void |
39 | error (const char *message, ...) | |
40 | { | |
41 | va_list args; | |
42 | ||
fafd911d NC |
43 | /* Try to keep error messages in sync with the program's normal output. */ |
44 | fflush (stdout); | |
45 | ||
3284fe0c L |
46 | va_start (args, message); |
47 | fprintf (stderr, _("%s: Error: "), program_name); | |
48 | vfprintf (stderr, message, args); | |
49 | va_end (args); | |
50 | } | |
51 | ||
52 | void | |
53 | warn (const char *message, ...) | |
54 | { | |
55 | va_list args; | |
56 | ||
fafd911d NC |
57 | /* Try to keep warning messages in sync with the program's normal output. */ |
58 | fflush (stdout); | |
3aade688 | 59 | |
3284fe0c L |
60 | va_start (args, message); |
61 | fprintf (stderr, _("%s: Warning: "), program_name); | |
62 | vfprintf (stderr, message, args); | |
63 | va_end (args); | |
64 | } | |
65 | ||
66 | void (*byte_put) (unsigned char *, elf_vma, int); | |
67 | ||
68 | void | |
69 | byte_put_little_endian (unsigned char * field, elf_vma value, int size) | |
70 | { | |
5a805384 | 71 | if (size <= 0 || size > 8) |
3284fe0c | 72 | { |
3284fe0c L |
73 | error (_("Unhandled data length: %d\n"), size); |
74 | abort (); | |
75 | } | |
5a805384 AM |
76 | while (size--) |
77 | { | |
78 | *field++ = value & 0xff; | |
79 | value >>= 8; | |
80 | } | |
3284fe0c L |
81 | } |
82 | ||
83 | void | |
84 | byte_put_big_endian (unsigned char * field, elf_vma value, int size) | |
85 | { | |
5a805384 | 86 | if (size <= 0 || size > 8) |
3284fe0c | 87 | { |
3284fe0c L |
88 | error (_("Unhandled data length: %d\n"), size); |
89 | abort (); | |
90 | } | |
5a805384 AM |
91 | while (size--) |
92 | { | |
93 | field[size] = value & 0xff; | |
94 | value >>= 8; | |
95 | } | |
3284fe0c L |
96 | } |
97 | ||
dda8d76d | 98 | elf_vma (*byte_get) (const unsigned char *, int); |
3284fe0c L |
99 | |
100 | elf_vma | |
dda8d76d | 101 | byte_get_little_endian (const unsigned char *field, int size) |
3284fe0c L |
102 | { |
103 | switch (size) | |
104 | { | |
105 | case 1: | |
106 | return *field; | |
107 | ||
108 | case 2: | |
109 | return ((unsigned int) (field[0])) | |
110 | | (((unsigned int) (field[1])) << 8); | |
111 | ||
112 | case 3: | |
113 | return ((unsigned long) (field[0])) | |
114 | | (((unsigned long) (field[1])) << 8) | |
115 | | (((unsigned long) (field[2])) << 16); | |
116 | ||
117 | case 4: | |
118 | return ((unsigned long) (field[0])) | |
119 | | (((unsigned long) (field[1])) << 8) | |
120 | | (((unsigned long) (field[2])) << 16) | |
121 | | (((unsigned long) (field[3])) << 24); | |
122 | ||
87bc83b3 CC |
123 | case 5: |
124 | if (sizeof (elf_vma) == 8) | |
125 | return ((elf_vma) (field[0])) | |
126 | | (((elf_vma) (field[1])) << 8) | |
127 | | (((elf_vma) (field[2])) << 16) | |
128 | | (((elf_vma) (field[3])) << 24) | |
129 | | (((elf_vma) (field[4])) << 32); | |
130 | else if (sizeof (elf_vma) == 4) | |
131 | /* We want to extract data from an 8 byte wide field and | |
132 | place it into a 4 byte wide field. Since this is a little | |
133 | endian source we can just use the 4 byte extraction code. */ | |
134 | return ((unsigned long) (field[0])) | |
135 | | (((unsigned long) (field[1])) << 8) | |
136 | | (((unsigned long) (field[2])) << 16) | |
137 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 138 | /* Fall through. */ |
87bc83b3 CC |
139 | |
140 | case 6: | |
141 | if (sizeof (elf_vma) == 8) | |
142 | return ((elf_vma) (field[0])) | |
143 | | (((elf_vma) (field[1])) << 8) | |
144 | | (((elf_vma) (field[2])) << 16) | |
145 | | (((elf_vma) (field[3])) << 24) | |
146 | | (((elf_vma) (field[4])) << 32) | |
147 | | (((elf_vma) (field[5])) << 40); | |
148 | else if (sizeof (elf_vma) == 4) | |
149 | /* We want to extract data from an 8 byte wide field and | |
150 | place it into a 4 byte wide field. Since this is a little | |
151 | endian source we can just use the 4 byte extraction code. */ | |
152 | return ((unsigned long) (field[0])) | |
153 | | (((unsigned long) (field[1])) << 8) | |
154 | | (((unsigned long) (field[2])) << 16) | |
155 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 156 | /* Fall through. */ |
87bc83b3 CC |
157 | |
158 | case 7: | |
159 | if (sizeof (elf_vma) == 8) | |
160 | return ((elf_vma) (field[0])) | |
161 | | (((elf_vma) (field[1])) << 8) | |
162 | | (((elf_vma) (field[2])) << 16) | |
163 | | (((elf_vma) (field[3])) << 24) | |
164 | | (((elf_vma) (field[4])) << 32) | |
165 | | (((elf_vma) (field[5])) << 40) | |
166 | | (((elf_vma) (field[6])) << 48); | |
167 | else if (sizeof (elf_vma) == 4) | |
168 | /* We want to extract data from an 8 byte wide field and | |
169 | place it into a 4 byte wide field. Since this is a little | |
170 | endian source we can just use the 4 byte extraction code. */ | |
171 | return ((unsigned long) (field[0])) | |
172 | | (((unsigned long) (field[1])) << 8) | |
173 | | (((unsigned long) (field[2])) << 16) | |
174 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 175 | /* Fall through. */ |
87bc83b3 | 176 | |
3284fe0c L |
177 | case 8: |
178 | if (sizeof (elf_vma) == 8) | |
179 | return ((elf_vma) (field[0])) | |
180 | | (((elf_vma) (field[1])) << 8) | |
181 | | (((elf_vma) (field[2])) << 16) | |
182 | | (((elf_vma) (field[3])) << 24) | |
183 | | (((elf_vma) (field[4])) << 32) | |
184 | | (((elf_vma) (field[5])) << 40) | |
185 | | (((elf_vma) (field[6])) << 48) | |
186 | | (((elf_vma) (field[7])) << 56); | |
187 | else if (sizeof (elf_vma) == 4) | |
188 | /* We want to extract data from an 8 byte wide field and | |
189 | place it into a 4 byte wide field. Since this is a little | |
190 | endian source we can just use the 4 byte extraction code. */ | |
191 | return ((unsigned long) (field[0])) | |
192 | | (((unsigned long) (field[1])) << 8) | |
193 | | (((unsigned long) (field[2])) << 16) | |
194 | | (((unsigned long) (field[3])) << 24); | |
1a0670f3 | 195 | /* Fall through. */ |
3284fe0c L |
196 | |
197 | default: | |
198 | error (_("Unhandled data length: %d\n"), size); | |
199 | abort (); | |
200 | } | |
201 | } | |
202 | ||
203 | elf_vma | |
dda8d76d | 204 | byte_get_big_endian (const unsigned char *field, int size) |
3284fe0c L |
205 | { |
206 | switch (size) | |
207 | { | |
208 | case 1: | |
209 | return *field; | |
210 | ||
211 | case 2: | |
212 | return ((unsigned int) (field[1])) | (((int) (field[0])) << 8); | |
213 | ||
214 | case 3: | |
215 | return ((unsigned long) (field[2])) | |
216 | | (((unsigned long) (field[1])) << 8) | |
217 | | (((unsigned long) (field[0])) << 16); | |
218 | ||
219 | case 4: | |
220 | return ((unsigned long) (field[3])) | |
221 | | (((unsigned long) (field[2])) << 8) | |
222 | | (((unsigned long) (field[1])) << 16) | |
223 | | (((unsigned long) (field[0])) << 24); | |
224 | ||
87bc83b3 CC |
225 | case 5: |
226 | if (sizeof (elf_vma) == 8) | |
227 | return ((elf_vma) (field[4])) | |
228 | | (((elf_vma) (field[3])) << 8) | |
229 | | (((elf_vma) (field[2])) << 16) | |
230 | | (((elf_vma) (field[1])) << 24) | |
231 | | (((elf_vma) (field[0])) << 32); | |
232 | else if (sizeof (elf_vma) == 4) | |
233 | { | |
234 | /* Although we are extracting data from an 8 byte wide field, | |
235 | we are returning only 4 bytes of data. */ | |
236 | field += 1; | |
237 | return ((unsigned long) (field[3])) | |
238 | | (((unsigned long) (field[2])) << 8) | |
239 | | (((unsigned long) (field[1])) << 16) | |
240 | | (((unsigned long) (field[0])) << 24); | |
241 | } | |
1a0670f3 | 242 | /* Fall through. */ |
87bc83b3 CC |
243 | |
244 | case 6: | |
245 | if (sizeof (elf_vma) == 8) | |
246 | return ((elf_vma) (field[5])) | |
247 | | (((elf_vma) (field[4])) << 8) | |
248 | | (((elf_vma) (field[3])) << 16) | |
249 | | (((elf_vma) (field[2])) << 24) | |
250 | | (((elf_vma) (field[1])) << 32) | |
251 | | (((elf_vma) (field[0])) << 40); | |
252 | else if (sizeof (elf_vma) == 4) | |
253 | { | |
254 | /* Although we are extracting data from an 8 byte wide field, | |
255 | we are returning only 4 bytes of data. */ | |
256 | field += 2; | |
257 | return ((unsigned long) (field[3])) | |
258 | | (((unsigned long) (field[2])) << 8) | |
259 | | (((unsigned long) (field[1])) << 16) | |
260 | | (((unsigned long) (field[0])) << 24); | |
261 | } | |
1a0670f3 | 262 | /* Fall through. */ |
87bc83b3 CC |
263 | |
264 | case 7: | |
265 | if (sizeof (elf_vma) == 8) | |
266 | return ((elf_vma) (field[6])) | |
267 | | (((elf_vma) (field[5])) << 8) | |
268 | | (((elf_vma) (field[4])) << 16) | |
269 | | (((elf_vma) (field[3])) << 24) | |
270 | | (((elf_vma) (field[2])) << 32) | |
271 | | (((elf_vma) (field[1])) << 40) | |
272 | | (((elf_vma) (field[0])) << 48); | |
273 | else if (sizeof (elf_vma) == 4) | |
274 | { | |
275 | /* Although we are extracting data from an 8 byte wide field, | |
276 | we are returning only 4 bytes of data. */ | |
277 | field += 3; | |
278 | return ((unsigned long) (field[3])) | |
279 | | (((unsigned long) (field[2])) << 8) | |
280 | | (((unsigned long) (field[1])) << 16) | |
281 | | (((unsigned long) (field[0])) << 24); | |
282 | } | |
1a0670f3 | 283 | /* Fall through. */ |
87bc83b3 | 284 | |
3284fe0c L |
285 | case 8: |
286 | if (sizeof (elf_vma) == 8) | |
287 | return ((elf_vma) (field[7])) | |
288 | | (((elf_vma) (field[6])) << 8) | |
289 | | (((elf_vma) (field[5])) << 16) | |
290 | | (((elf_vma) (field[4])) << 24) | |
291 | | (((elf_vma) (field[3])) << 32) | |
292 | | (((elf_vma) (field[2])) << 40) | |
293 | | (((elf_vma) (field[1])) << 48) | |
294 | | (((elf_vma) (field[0])) << 56); | |
295 | else if (sizeof (elf_vma) == 4) | |
296 | { | |
87bc83b3 | 297 | /* Although we are extracting data from an 8 byte wide field, |
3284fe0c L |
298 | we are returning only 4 bytes of data. */ |
299 | field += 4; | |
300 | return ((unsigned long) (field[3])) | |
301 | | (((unsigned long) (field[2])) << 8) | |
302 | | (((unsigned long) (field[1])) << 16) | |
303 | | (((unsigned long) (field[0])) << 24); | |
304 | } | |
1a0670f3 | 305 | /* Fall through. */ |
3284fe0c L |
306 | |
307 | default: | |
308 | error (_("Unhandled data length: %d\n"), size); | |
309 | abort (); | |
310 | } | |
311 | } | |
312 | ||
313 | elf_vma | |
dda8d76d | 314 | byte_get_signed (const unsigned char *field, int size) |
3284fe0c L |
315 | { |
316 | elf_vma x = byte_get (field, size); | |
317 | ||
318 | switch (size) | |
319 | { | |
320 | case 1: | |
321 | return (x ^ 0x80) - 0x80; | |
322 | case 2: | |
323 | return (x ^ 0x8000) - 0x8000; | |
87bc83b3 CC |
324 | case 3: |
325 | return (x ^ 0x800000) - 0x800000; | |
3284fe0c L |
326 | case 4: |
327 | return (x ^ 0x80000000) - 0x80000000; | |
87bc83b3 CC |
328 | case 5: |
329 | case 6: | |
330 | case 7: | |
3284fe0c | 331 | case 8: |
87bc83b3 CC |
332 | /* Reads of 5-, 6-, and 7-byte numbers are the result of |
333 | trying to read past the end of a buffer, and will therefore | |
334 | not have meaningful values, so we don't try to deal with | |
335 | the sign in these cases. */ | |
3284fe0c L |
336 | return x; |
337 | default: | |
338 | abort (); | |
339 | } | |
340 | } | |
341 | ||
74bc6052 CC |
342 | /* Return the high-order 32-bits and the low-order 32-bits |
343 | of an 8-byte value separately. */ | |
344 | ||
345 | void | |
dda8d76d | 346 | byte_get_64 (const unsigned char *field, elf_vma *high, elf_vma *low) |
74bc6052 CC |
347 | { |
348 | if (byte_get == byte_get_big_endian) | |
349 | { | |
350 | *high = byte_get_big_endian (field, 4); | |
351 | *low = byte_get_big_endian (field + 4, 4); | |
352 | } | |
353 | else | |
354 | { | |
355 | *high = byte_get_little_endian (field + 4, 4); | |
356 | *low = byte_get_little_endian (field, 4); | |
357 | } | |
358 | return; | |
359 | } | |
360 | ||
3284fe0c L |
361 | /* Return the path name for a proxy entry in a thin archive, adjusted |
362 | relative to the path name of the thin archive itself if necessary. | |
363 | Always returns a pointer to malloc'ed memory. */ | |
364 | ||
365 | char * | |
366 | adjust_relative_path (const char *file_name, const char *name, | |
591f7597 | 367 | unsigned long name_len) |
3284fe0c L |
368 | { |
369 | char * member_file_name; | |
370 | const char * base_name = lbasename (file_name); | |
591f7597 | 371 | size_t amt; |
3284fe0c L |
372 | |
373 | /* This is a proxy entry for a thin archive member. | |
374 | If the extended name table contains an absolute path | |
375 | name, or if the archive is in the current directory, | |
376 | use the path name as given. Otherwise, we need to | |
377 | find the member relative to the directory where the | |
378 | archive is located. */ | |
379 | if (IS_ABSOLUTE_PATH (name) || base_name == file_name) | |
380 | { | |
591f7597 NC |
381 | amt = name_len + 1; |
382 | if (amt == 0) | |
383 | return NULL; | |
384 | member_file_name = (char *) malloc (amt); | |
3284fe0c L |
385 | if (member_file_name == NULL) |
386 | { | |
387 | error (_("Out of memory\n")); | |
388 | return NULL; | |
389 | } | |
390 | memcpy (member_file_name, name, name_len); | |
391 | member_file_name[name_len] = '\0'; | |
392 | } | |
393 | else | |
394 | { | |
395 | /* Concatenate the path components of the archive file name | |
396 | to the relative path name from the extended name table. */ | |
397 | size_t prefix_len = base_name - file_name; | |
591f7597 NC |
398 | |
399 | amt = prefix_len + name_len + 1; | |
400 | /* PR 17531: file: 2896dc8b | |
401 | Catch wraparound. */ | |
402 | if (amt < prefix_len || amt < name_len) | |
403 | { | |
404 | error (_("Abnormal length of thin archive member name: %lx\n"), | |
405 | name_len); | |
406 | return NULL; | |
407 | } | |
3aade688 | 408 | |
591f7597 | 409 | member_file_name = (char *) malloc (amt); |
3284fe0c L |
410 | if (member_file_name == NULL) |
411 | { | |
412 | error (_("Out of memory\n")); | |
413 | return NULL; | |
414 | } | |
415 | memcpy (member_file_name, file_name, prefix_len); | |
416 | memcpy (member_file_name + prefix_len, name, name_len); | |
417 | member_file_name[prefix_len + name_len] = '\0'; | |
418 | } | |
419 | return member_file_name; | |
420 | } | |
421 | ||
c2a7d3f5 NC |
422 | /* Processes the archive index table and symbol table in ARCH. |
423 | Entries in the index table are SIZEOF_AR_INDEX bytes long. | |
424 | Fills in ARCH->next_arhdr_offset and ARCH->arhdr. | |
425 | If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array, | |
426 | ARCH->sym_size and ARCH->sym_table. | |
427 | It is the caller's responsibility to free ARCH->index_array and | |
428 | ARCH->sym_table. | |
81a65eb3 | 429 | Returns 1 upon success, 0 otherwise. |
c2a7d3f5 NC |
430 | If failure occurs an error message is printed. */ |
431 | ||
81a65eb3 AM |
432 | static int |
433 | process_archive_index_and_symbols (struct archive_info *arch, | |
434 | unsigned int sizeof_ar_index, | |
435 | int read_symbols) | |
c2a7d3f5 NC |
436 | { |
437 | size_t got; | |
438 | unsigned long size; | |
d91f0b20 | 439 | char fmag_save; |
c2a7d3f5 | 440 | |
d91f0b20 AM |
441 | fmag_save = arch->arhdr.ar_fmag[0]; |
442 | arch->arhdr.ar_fmag[0] = 0; | |
c2a7d3f5 | 443 | size = strtoul (arch->arhdr.ar_size, NULL, 10); |
d91f0b20 | 444 | arch->arhdr.ar_fmag[0] = fmag_save; |
591f7597 NC |
445 | /* PR 17531: file: 912bd7de. */ |
446 | if ((signed long) size < 0) | |
447 | { | |
448 | error (_("%s: invalid archive header size: %ld\n"), | |
449 | arch->file_name, size); | |
81a65eb3 | 450 | return 0; |
591f7597 NC |
451 | } |
452 | ||
c2a7d3f5 NC |
453 | size = size + (size & 1); |
454 | ||
455 | arch->next_arhdr_offset += sizeof arch->arhdr + size; | |
456 | ||
457 | if (! read_symbols) | |
458 | { | |
459 | if (fseek (arch->file, size, SEEK_CUR) != 0) | |
460 | { | |
461 | error (_("%s: failed to skip archive symbol table\n"), | |
462 | arch->file_name); | |
81a65eb3 | 463 | return 0; |
c2a7d3f5 NC |
464 | } |
465 | } | |
466 | else | |
467 | { | |
468 | unsigned long i; | |
469 | /* A buffer used to hold numbers read in from an archive index. | |
470 | These are always SIZEOF_AR_INDEX bytes long and stored in | |
471 | big-endian format. */ | |
472 | unsigned char integer_buffer[sizeof arch->index_num]; | |
473 | unsigned char * index_buffer; | |
474 | ||
475 | assert (sizeof_ar_index <= sizeof integer_buffer); | |
3aade688 | 476 | |
c2a7d3f5 NC |
477 | /* Check the size of the archive index. */ |
478 | if (size < sizeof_ar_index) | |
479 | { | |
480 | error (_("%s: the archive index is empty\n"), arch->file_name); | |
81a65eb3 | 481 | return 0; |
c2a7d3f5 NC |
482 | } |
483 | ||
484 | /* Read the number of entries in the archive index. */ | |
485 | got = fread (integer_buffer, 1, sizeof_ar_index, arch->file); | |
486 | if (got != sizeof_ar_index) | |
487 | { | |
488 | error (_("%s: failed to read archive index\n"), arch->file_name); | |
81a65eb3 | 489 | return 0; |
c2a7d3f5 NC |
490 | } |
491 | ||
492 | arch->index_num = byte_get_big_endian (integer_buffer, sizeof_ar_index); | |
493 | size -= sizeof_ar_index; | |
494 | ||
53774b7e NC |
495 | if (size < arch->index_num * sizeof_ar_index |
496 | /* PR 17531: file: 585515d1. */ | |
497 | || size < arch->index_num) | |
c2a7d3f5 | 498 | { |
53774b7e | 499 | error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"), |
c2a7d3f5 | 500 | arch->file_name, (long) arch->index_num, sizeof_ar_index, size); |
81a65eb3 | 501 | return 0; |
c2a7d3f5 NC |
502 | } |
503 | ||
504 | /* Read in the archive index. */ | |
505 | index_buffer = (unsigned char *) | |
506 | malloc (arch->index_num * sizeof_ar_index); | |
507 | if (index_buffer == NULL) | |
508 | { | |
509 | error (_("Out of memory whilst trying to read archive symbol index\n")); | |
81a65eb3 | 510 | return 0; |
c2a7d3f5 NC |
511 | } |
512 | ||
513 | got = fread (index_buffer, sizeof_ar_index, arch->index_num, arch->file); | |
514 | if (got != arch->index_num) | |
515 | { | |
516 | free (index_buffer); | |
517 | error (_("%s: failed to read archive index\n"), arch->file_name); | |
81a65eb3 | 518 | return 0; |
c2a7d3f5 NC |
519 | } |
520 | ||
521 | size -= arch->index_num * sizeof_ar_index; | |
522 | ||
523 | /* Convert the index numbers into the host's numeric format. */ | |
524 | arch->index_array = (elf_vma *) | |
525 | malloc (arch->index_num * sizeof (* arch->index_array)); | |
526 | if (arch->index_array == NULL) | |
527 | { | |
528 | free (index_buffer); | |
529 | error (_("Out of memory whilst trying to convert the archive symbol index\n")); | |
81a65eb3 | 530 | return 0; |
c2a7d3f5 NC |
531 | } |
532 | ||
533 | for (i = 0; i < arch->index_num; i++) | |
534 | arch->index_array[i] = | |
535 | byte_get_big_endian ((unsigned char *) (index_buffer + (i * sizeof_ar_index)), | |
536 | sizeof_ar_index); | |
537 | free (index_buffer); | |
538 | ||
539 | /* The remaining space in the header is taken up by the symbol table. */ | |
540 | if (size < 1) | |
541 | { | |
542 | error (_("%s: the archive has an index but no symbols\n"), | |
543 | arch->file_name); | |
81a65eb3 | 544 | return 0; |
c2a7d3f5 NC |
545 | } |
546 | ||
547 | arch->sym_table = (char *) malloc (size); | |
548 | if (arch->sym_table == NULL) | |
549 | { | |
550 | error (_("Out of memory whilst trying to read archive index symbol table\n")); | |
81a65eb3 | 551 | return 0; |
c2a7d3f5 NC |
552 | } |
553 | ||
554 | arch->sym_size = size; | |
555 | got = fread (arch->sym_table, 1, size, arch->file); | |
556 | if (got != size) | |
557 | { | |
558 | error (_("%s: failed to read archive index symbol table\n"), | |
559 | arch->file_name); | |
81a65eb3 | 560 | return 0; |
c2a7d3f5 NC |
561 | } |
562 | } | |
563 | ||
564 | /* Read the next archive header. */ | |
565 | got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file); | |
566 | if (got != sizeof arch->arhdr && got != 0) | |
567 | { | |
568 | error (_("%s: failed to read archive header following archive index\n"), | |
569 | arch->file_name); | |
81a65eb3 | 570 | return 0; |
c2a7d3f5 NC |
571 | } |
572 | ||
81a65eb3 | 573 | return 1; |
c2a7d3f5 NC |
574 | } |
575 | ||
3284fe0c L |
576 | /* Read the symbol table and long-name table from an archive. */ |
577 | ||
578 | int | |
579 | setup_archive (struct archive_info *arch, const char *file_name, | |
645ba681 | 580 | FILE *file, off_t file_size, |
81a65eb3 | 581 | int is_thin_archive, int read_symbols) |
3284fe0c L |
582 | { |
583 | size_t got; | |
3284fe0c L |
584 | |
585 | arch->file_name = strdup (file_name); | |
586 | arch->file = file; | |
587 | arch->index_num = 0; | |
588 | arch->index_array = NULL; | |
589 | arch->sym_table = NULL; | |
590 | arch->sym_size = 0; | |
591 | arch->longnames = NULL; | |
592 | arch->longnames_size = 0; | |
593 | arch->nested_member_origin = 0; | |
594 | arch->is_thin_archive = is_thin_archive; | |
81a65eb3 | 595 | arch->uses_64bit_indices = 0; |
3284fe0c L |
596 | arch->next_arhdr_offset = SARMAG; |
597 | ||
598 | /* Read the first archive member header. */ | |
599 | if (fseek (file, SARMAG, SEEK_SET) != 0) | |
600 | { | |
601 | error (_("%s: failed to seek to first archive header\n"), file_name); | |
602 | return 1; | |
603 | } | |
604 | got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file); | |
605 | if (got != sizeof arch->arhdr) | |
606 | { | |
607 | if (got == 0) | |
608 | return 0; | |
609 | ||
610 | error (_("%s: failed to read archive header\n"), file_name); | |
611 | return 1; | |
612 | } | |
613 | ||
614 | /* See if this is the archive symbol table. */ | |
24d127aa | 615 | if (startswith (arch->arhdr.ar_name, "/ ")) |
3284fe0c | 616 | { |
c2a7d3f5 NC |
617 | if (! process_archive_index_and_symbols (arch, 4, read_symbols)) |
618 | return 1; | |
619 | } | |
24d127aa | 620 | else if (startswith (arch->arhdr.ar_name, "/SYM64/ ")) |
c2a7d3f5 | 621 | { |
81a65eb3 | 622 | arch->uses_64bit_indices = 1; |
c2a7d3f5 NC |
623 | if (! process_archive_index_and_symbols (arch, 8, read_symbols)) |
624 | return 1; | |
3284fe0c L |
625 | } |
626 | else if (read_symbols) | |
627 | printf (_("%s has no archive index\n"), file_name); | |
628 | ||
24d127aa | 629 | if (startswith (arch->arhdr.ar_name, "// ")) |
3284fe0c L |
630 | { |
631 | /* This is the archive string table holding long member names. */ | |
d91f0b20 AM |
632 | char fmag_save = arch->arhdr.ar_fmag[0]; |
633 | arch->arhdr.ar_fmag[0] = 0; | |
3284fe0c | 634 | arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10); |
d91f0b20 | 635 | arch->arhdr.ar_fmag[0] = fmag_save; |
591f7597 NC |
636 | /* PR 17531: file: 01068045. */ |
637 | if (arch->longnames_size < 8) | |
638 | { | |
639 | error (_("%s: long name table is too small, (size = %ld)\n"), | |
640 | file_name, arch->longnames_size); | |
641 | return 1; | |
642 | } | |
058037d3 | 643 | /* PR 17531: file: 639d6a26. */ |
645ba681 | 644 | if ((off_t) arch->longnames_size > file_size |
780f96ae | 645 | || (signed long) arch->longnames_size < 0) |
058037d3 NC |
646 | { |
647 | error (_("%s: long name table is too big, (size = 0x%lx)\n"), | |
648 | file_name, arch->longnames_size); | |
649 | return 1; | |
650 | } | |
651 | ||
3284fe0c L |
652 | arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size; |
653 | ||
591f7597 NC |
654 | /* Plus one to allow for a string terminator. */ |
655 | arch->longnames = (char *) malloc (arch->longnames_size + 1); | |
3284fe0c L |
656 | if (arch->longnames == NULL) |
657 | { | |
658 | error (_("Out of memory reading long symbol names in archive\n")); | |
659 | return 1; | |
660 | } | |
661 | ||
662 | if (fread (arch->longnames, arch->longnames_size, 1, file) != 1) | |
663 | { | |
664 | free (arch->longnames); | |
665 | arch->longnames = NULL; | |
666 | error (_("%s: failed to read long symbol name string table\n"), | |
667 | file_name); | |
668 | return 1; | |
669 | } | |
670 | ||
671 | if ((arch->longnames_size & 1) != 0) | |
672 | getc (file); | |
058037d3 NC |
673 | |
674 | arch->longnames[arch->longnames_size] = 0; | |
3284fe0c L |
675 | } |
676 | ||
677 | return 0; | |
678 | } | |
679 | ||
680 | /* Open and setup a nested archive, if not already open. */ | |
681 | ||
682 | int | |
683 | setup_nested_archive (struct archive_info *nested_arch, | |
684 | const char *member_file_name) | |
685 | { | |
686 | FILE * member_file; | |
780f96ae | 687 | struct stat statbuf; |
3284fe0c L |
688 | |
689 | /* Have we already setup this archive? */ | |
690 | if (nested_arch->file_name != NULL | |
691 | && streq (nested_arch->file_name, member_file_name)) | |
692 | return 0; | |
693 | ||
694 | /* Close previous file and discard cached information. */ | |
695 | if (nested_arch->file != NULL) | |
cfc16775 AM |
696 | { |
697 | fclose (nested_arch->file); | |
698 | nested_arch->file = NULL; | |
699 | } | |
3284fe0c L |
700 | release_archive (nested_arch); |
701 | ||
702 | member_file = fopen (member_file_name, "rb"); | |
703 | if (member_file == NULL) | |
704 | return 1; | |
780f96ae AM |
705 | if (fstat (fileno (member_file), &statbuf) < 0) |
706 | return 1; | |
3284fe0c | 707 | return setup_archive (nested_arch, member_file_name, member_file, |
81a65eb3 | 708 | statbuf.st_size, 0, 0); |
3284fe0c L |
709 | } |
710 | ||
711 | /* Release the memory used for the archive information. */ | |
712 | ||
713 | void | |
714 | release_archive (struct archive_info * arch) | |
715 | { | |
9db70fc3 AM |
716 | free (arch->file_name); |
717 | free (arch->index_array); | |
718 | free (arch->sym_table); | |
719 | free (arch->longnames); | |
cfc16775 AM |
720 | arch->file_name = NULL; |
721 | arch->index_array = NULL; | |
722 | arch->sym_table = NULL; | |
723 | arch->longnames = NULL; | |
3284fe0c L |
724 | } |
725 | ||
726 | /* Get the name of an archive member from the current archive header. | |
727 | For simple names, this will modify the ar_name field of the current | |
728 | archive header. For long names, it will return a pointer to the | |
729 | longnames table. For nested archives, it will open the nested archive | |
730 | and get the name recursively. NESTED_ARCH is a single-entry cache so | |
731 | we don't keep rereading the same information from a nested archive. */ | |
732 | ||
733 | char * | |
734 | get_archive_member_name (struct archive_info *arch, | |
735 | struct archive_info *nested_arch) | |
736 | { | |
737 | unsigned long j, k; | |
738 | ||
739 | if (arch->arhdr.ar_name[0] == '/') | |
740 | { | |
741 | /* We have a long name. */ | |
742 | char *endp; | |
743 | char *member_file_name; | |
744 | char *member_name; | |
d91f0b20 | 745 | char fmag_save; |
3284fe0c | 746 | |
907b01b7 NC |
747 | if (arch->longnames == NULL || arch->longnames_size == 0) |
748 | { | |
749 | error (_("Archive member uses long names, but no longname table found\n")); | |
750 | return NULL; | |
751 | } | |
3aade688 | 752 | |
3284fe0c | 753 | arch->nested_member_origin = 0; |
d91f0b20 AM |
754 | fmag_save = arch->arhdr.ar_fmag[0]; |
755 | arch->arhdr.ar_fmag[0] = 0; | |
3284fe0c L |
756 | k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10); |
757 | if (arch->is_thin_archive && endp != NULL && * endp == ':') | |
758 | arch->nested_member_origin = strtoul (endp + 1, NULL, 10); | |
d91f0b20 | 759 | arch->arhdr.ar_fmag[0] = fmag_save; |
3284fe0c | 760 | |
591f7597 NC |
761 | if (j > arch->longnames_size) |
762 | { | |
763 | error (_("Found long name index (%ld) beyond end of long name table\n"),j); | |
764 | return NULL; | |
765 | } | |
3284fe0c L |
766 | while ((j < arch->longnames_size) |
767 | && (arch->longnames[j] != '\n') | |
768 | && (arch->longnames[j] != '\0')) | |
769 | j++; | |
591f7597 | 770 | if (j > 0 && arch->longnames[j-1] == '/') |
3284fe0c | 771 | j--; |
591f7597 NC |
772 | if (j > arch->longnames_size) |
773 | j = arch->longnames_size; | |
3284fe0c L |
774 | arch->longnames[j] = '\0'; |
775 | ||
776 | if (!arch->is_thin_archive || arch->nested_member_origin == 0) | |
fd486f32 | 777 | return xstrdup (arch->longnames + k); |
3284fe0c | 778 | |
591f7597 NC |
779 | /* PR 17531: file: 2896dc8b. */ |
780 | if (k >= j) | |
781 | { | |
782 | error (_("Invalid Thin archive member name\n")); | |
783 | return NULL; | |
784 | } | |
3aade688 | 785 | |
3284fe0c L |
786 | /* This is a proxy for a member of a nested archive. |
787 | Find the name of the member in that archive. */ | |
788 | member_file_name = adjust_relative_path (arch->file_name, | |
789 | arch->longnames + k, j - k); | |
790 | if (member_file_name != NULL | |
791 | && setup_nested_archive (nested_arch, member_file_name) == 0) | |
792 | { | |
fd486f32 | 793 | member_name = get_archive_member_name_at (nested_arch, |
3284fe0c L |
794 | arch->nested_member_origin, |
795 | NULL); | |
796 | if (member_name != NULL) | |
797 | { | |
798 | free (member_file_name); | |
799 | return member_name; | |
800 | } | |
801 | } | |
802 | free (member_file_name); | |
803 | ||
804 | /* Last resort: just return the name of the nested archive. */ | |
fd486f32 | 805 | return xstrdup (arch->longnames + k); |
3284fe0c L |
806 | } |
807 | ||
808 | /* We have a normal (short) name. */ | |
809 | for (j = 0; j < sizeof (arch->arhdr.ar_name); j++) | |
810 | if (arch->arhdr.ar_name[j] == '/') | |
811 | { | |
812 | arch->arhdr.ar_name[j] = '\0'; | |
fd486f32 | 813 | return xstrdup (arch->arhdr.ar_name); |
3284fe0c L |
814 | } |
815 | ||
816 | /* The full ar_name field is used. Don't rely on ar_date starting | |
817 | with a zero byte. */ | |
818 | { | |
819 | char *name = xmalloc (sizeof (arch->arhdr.ar_name) + 1); | |
820 | memcpy (name, arch->arhdr.ar_name, sizeof (arch->arhdr.ar_name)); | |
821 | name[sizeof (arch->arhdr.ar_name)] = '\0'; | |
822 | return name; | |
823 | } | |
824 | } | |
825 | ||
826 | /* Get the name of an archive member at a given OFFSET within an archive | |
827 | ARCH. */ | |
828 | ||
829 | char * | |
830 | get_archive_member_name_at (struct archive_info *arch, | |
831 | unsigned long offset, | |
832 | struct archive_info *nested_arch) | |
833 | { | |
834 | size_t got; | |
835 | ||
836 | if (fseek (arch->file, offset, SEEK_SET) != 0) | |
837 | { | |
838 | error (_("%s: failed to seek to next file name\n"), arch->file_name); | |
839 | return NULL; | |
840 | } | |
841 | got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file); | |
842 | if (got != sizeof arch->arhdr) | |
843 | { | |
844 | error (_("%s: failed to read archive header\n"), arch->file_name); | |
845 | return NULL; | |
846 | } | |
847 | if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0) | |
848 | { | |
849 | error (_("%s: did not find a valid archive header\n"), | |
850 | arch->file_name); | |
851 | return NULL; | |
852 | } | |
853 | ||
854 | return get_archive_member_name (arch, nested_arch); | |
855 | } | |
856 | ||
857 | /* Construct a string showing the name of the archive member, qualified | |
858 | with the name of the containing archive file. For thin archives, we | |
859 | use square brackets to denote the indirection. For nested archives, | |
860 | we show the qualified name of the external member inside the square | |
861 | brackets (e.g., "thin.a[normal.a(foo.o)]"). */ | |
862 | ||
863 | char * | |
864 | make_qualified_name (struct archive_info * arch, | |
865 | struct archive_info * nested_arch, | |
866 | const char *member_name) | |
867 | { | |
a043396b | 868 | const char * error_name = _("<corrupt>"); |
3284fe0c L |
869 | size_t len; |
870 | char * name; | |
871 | ||
872 | len = strlen (arch->file_name) + strlen (member_name) + 3; | |
a043396b NC |
873 | if (arch->is_thin_archive |
874 | && arch->nested_member_origin != 0) | |
875 | { | |
876 | /* PR 15140: Allow for corrupt thin archives. */ | |
877 | if (nested_arch->file_name) | |
878 | len += strlen (nested_arch->file_name) + 2; | |
879 | else | |
880 | len += strlen (error_name) + 2; | |
881 | } | |
3284fe0c L |
882 | |
883 | name = (char *) malloc (len); | |
884 | if (name == NULL) | |
885 | { | |
886 | error (_("Out of memory\n")); | |
887 | return NULL; | |
888 | } | |
889 | ||
a043396b NC |
890 | if (arch->is_thin_archive |
891 | && arch->nested_member_origin != 0) | |
892 | { | |
893 | if (nested_arch->file_name) | |
894 | snprintf (name, len, "%s[%s(%s)]", arch->file_name, | |
895 | nested_arch->file_name, member_name); | |
896 | else | |
897 | snprintf (name, len, "%s[%s(%s)]", arch->file_name, | |
3aade688 | 898 | error_name, member_name); |
a043396b | 899 | } |
3284fe0c L |
900 | else if (arch->is_thin_archive) |
901 | snprintf (name, len, "%s[%s]", arch->file_name, member_name); | |
902 | else | |
903 | snprintf (name, len, "%s(%s)", arch->file_name, member_name); | |
904 | ||
905 | return name; | |
906 | } |