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