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