1 /* elfcomm.c -- common code for ELF format file.
2 Copyright 2010-2013 Free Software Foundation, Inc.
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
7 This file is part of GNU Binutils.
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.
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.
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
25 #include "libiberty.h"
26 #include "filenames.h"
34 error (const char *message, ...)
38 va_start (args, message);
39 fprintf (stderr, _("%s: Error: "), program_name);
40 vfprintf (stderr, message, args);
45 warn (const char *message, ...)
49 va_start (args, message);
50 fprintf (stderr, _("%s: Warning: "), program_name);
51 vfprintf (stderr, message, args);
55 void (*byte_put) (unsigned char *, elf_vma, int);
58 byte_put_little_endian (unsigned char * field, elf_vma value, int size)
63 field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
64 field[6] = ((value >> 24) >> 24) & 0xff;
65 field[5] = ((value >> 24) >> 16) & 0xff;
66 field[4] = ((value >> 24) >> 8) & 0xff;
69 field[3] = (value >> 24) & 0xff;
72 field[2] = (value >> 16) & 0xff;
75 field[1] = (value >> 8) & 0xff;
78 field[0] = value & 0xff;
82 error (_("Unhandled data length: %d\n"), size);
88 byte_put_big_endian (unsigned char * field, elf_vma value, int size)
93 field[7] = value & 0xff;
94 field[6] = (value >> 8) & 0xff;
95 field[5] = (value >> 16) & 0xff;
96 field[4] = (value >> 24) & 0xff;
101 field[3] = value & 0xff;
105 field[2] = value & 0xff;
109 field[1] = value & 0xff;
113 field[0] = value & 0xff;
117 error (_("Unhandled data length: %d\n"), size);
122 elf_vma (*byte_get) (unsigned char *, int);
125 byte_get_little_endian (unsigned char *field, int size)
133 return ((unsigned int) (field[0]))
134 | (((unsigned int) (field[1])) << 8);
137 return ((unsigned long) (field[0]))
138 | (((unsigned long) (field[1])) << 8)
139 | (((unsigned long) (field[2])) << 16);
142 return ((unsigned long) (field[0]))
143 | (((unsigned long) (field[1])) << 8)
144 | (((unsigned long) (field[2])) << 16)
145 | (((unsigned long) (field[3])) << 24);
148 if (sizeof (elf_vma) == 8)
149 return ((elf_vma) (field[0]))
150 | (((elf_vma) (field[1])) << 8)
151 | (((elf_vma) (field[2])) << 16)
152 | (((elf_vma) (field[3])) << 24)
153 | (((elf_vma) (field[4])) << 32)
154 | (((elf_vma) (field[5])) << 40)
155 | (((elf_vma) (field[6])) << 48)
156 | (((elf_vma) (field[7])) << 56);
157 else if (sizeof (elf_vma) == 4)
158 /* We want to extract data from an 8 byte wide field and
159 place it into a 4 byte wide field. Since this is a little
160 endian source we can just use the 4 byte extraction code. */
161 return ((unsigned long) (field[0]))
162 | (((unsigned long) (field[1])) << 8)
163 | (((unsigned long) (field[2])) << 16)
164 | (((unsigned long) (field[3])) << 24);
167 error (_("Unhandled data length: %d\n"), size);
173 byte_get_big_endian (unsigned char *field, int size)
181 return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
184 return ((unsigned long) (field[2]))
185 | (((unsigned long) (field[1])) << 8)
186 | (((unsigned long) (field[0])) << 16);
189 return ((unsigned long) (field[3]))
190 | (((unsigned long) (field[2])) << 8)
191 | (((unsigned long) (field[1])) << 16)
192 | (((unsigned long) (field[0])) << 24);
195 if (sizeof (elf_vma) == 8)
196 return ((elf_vma) (field[7]))
197 | (((elf_vma) (field[6])) << 8)
198 | (((elf_vma) (field[5])) << 16)
199 | (((elf_vma) (field[4])) << 24)
200 | (((elf_vma) (field[3])) << 32)
201 | (((elf_vma) (field[2])) << 40)
202 | (((elf_vma) (field[1])) << 48)
203 | (((elf_vma) (field[0])) << 56);
204 else if (sizeof (elf_vma) == 4)
206 /* Although we are extracing data from an 8 byte wide field,
207 we are returning only 4 bytes of data. */
209 return ((unsigned long) (field[3]))
210 | (((unsigned long) (field[2])) << 8)
211 | (((unsigned long) (field[1])) << 16)
212 | (((unsigned long) (field[0])) << 24);
216 error (_("Unhandled data length: %d\n"), size);
222 byte_get_signed (unsigned char *field, int size)
224 elf_vma x = byte_get (field, size);
229 return (x ^ 0x80) - 0x80;
231 return (x ^ 0x8000) - 0x8000;
233 return (x ^ 0x80000000) - 0x80000000;
241 /* Return the high-order 32-bits and the low-order 32-bits
242 of an 8-byte value separately. */
245 byte_get_64 (unsigned char *field, elf_vma *high, elf_vma *low)
247 if (byte_get == byte_get_big_endian)
249 *high = byte_get_big_endian (field, 4);
250 *low = byte_get_big_endian (field + 4, 4);
254 *high = byte_get_little_endian (field + 4, 4);
255 *low = byte_get_little_endian (field, 4);
260 /* Return the path name for a proxy entry in a thin archive, adjusted
261 relative to the path name of the thin archive itself if necessary.
262 Always returns a pointer to malloc'ed memory. */
265 adjust_relative_path (const char *file_name, const char *name,
268 char * member_file_name;
269 const char * base_name = lbasename (file_name);
271 /* This is a proxy entry for a thin archive member.
272 If the extended name table contains an absolute path
273 name, or if the archive is in the current directory,
274 use the path name as given. Otherwise, we need to
275 find the member relative to the directory where the
276 archive is located. */
277 if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
279 member_file_name = (char *) malloc (name_len + 1);
280 if (member_file_name == NULL)
282 error (_("Out of memory\n"));
285 memcpy (member_file_name, name, name_len);
286 member_file_name[name_len] = '\0';
290 /* Concatenate the path components of the archive file name
291 to the relative path name from the extended name table. */
292 size_t prefix_len = base_name - file_name;
293 member_file_name = (char *) malloc (prefix_len + name_len + 1);
294 if (member_file_name == NULL)
296 error (_("Out of memory\n"));
299 memcpy (member_file_name, file_name, prefix_len);
300 memcpy (member_file_name + prefix_len, name, name_len);
301 member_file_name[prefix_len + name_len] = '\0';
303 return member_file_name;
306 /* Processes the archive index table and symbol table in ARCH.
307 Entries in the index table are SIZEOF_AR_INDEX bytes long.
308 Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
309 If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
310 ARCH->sym_size and ARCH->sym_table.
311 It is the caller's responsibility to free ARCH->index_array and
313 Returns TRUE upon success, FALSE otherwise.
314 If failure occurs an error message is printed. */
317 process_archive_index_and_symbols (struct archive_info * arch,
318 unsigned int sizeof_ar_index,
319 bfd_boolean read_symbols)
324 size = strtoul (arch->arhdr.ar_size, NULL, 10);
325 size = size + (size & 1);
327 arch->next_arhdr_offset += sizeof arch->arhdr + size;
331 if (fseek (arch->file, size, SEEK_CUR) != 0)
333 error (_("%s: failed to skip archive symbol table\n"),
341 /* A buffer used to hold numbers read in from an archive index.
342 These are always SIZEOF_AR_INDEX bytes long and stored in
343 big-endian format. */
344 unsigned char integer_buffer[sizeof arch->index_num];
345 unsigned char * index_buffer;
347 assert (sizeof_ar_index <= sizeof integer_buffer);
349 /* Check the size of the archive index. */
350 if (size < sizeof_ar_index)
352 error (_("%s: the archive index is empty\n"), arch->file_name);
356 /* Read the number of entries in the archive index. */
357 got = fread (integer_buffer, 1, sizeof_ar_index, arch->file);
358 if (got != sizeof_ar_index)
360 error (_("%s: failed to read archive index\n"), arch->file_name);
364 arch->index_num = byte_get_big_endian (integer_buffer, sizeof_ar_index);
365 size -= sizeof_ar_index;
367 if (size < arch->index_num * sizeof_ar_index)
369 error (_("%s: the archive index is supposed to have %ld entries of %d bytes, but the size is only %ld\n"),
370 arch->file_name, (long) arch->index_num, sizeof_ar_index, size);
374 /* Read in the archive index. */
375 index_buffer = (unsigned char *)
376 malloc (arch->index_num * sizeof_ar_index);
377 if (index_buffer == NULL)
379 error (_("Out of memory whilst trying to read archive symbol index\n"));
383 got = fread (index_buffer, sizeof_ar_index, arch->index_num, arch->file);
384 if (got != arch->index_num)
387 error (_("%s: failed to read archive index\n"), arch->file_name);
391 size -= arch->index_num * sizeof_ar_index;
393 /* Convert the index numbers into the host's numeric format. */
394 arch->index_array = (elf_vma *)
395 malloc (arch->index_num * sizeof (* arch->index_array));
396 if (arch->index_array == NULL)
399 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
403 for (i = 0; i < arch->index_num; i++)
404 arch->index_array[i] =
405 byte_get_big_endian ((unsigned char *) (index_buffer + (i * sizeof_ar_index)),
409 /* The remaining space in the header is taken up by the symbol table. */
412 error (_("%s: the archive has an index but no symbols\n"),
417 arch->sym_table = (char *) malloc (size);
418 if (arch->sym_table == NULL)
420 error (_("Out of memory whilst trying to read archive index symbol table\n"));
424 arch->sym_size = size;
425 got = fread (arch->sym_table, 1, size, arch->file);
428 error (_("%s: failed to read archive index symbol table\n"),
434 /* Read the next archive header. */
435 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
436 if (got != sizeof arch->arhdr && got != 0)
438 error (_("%s: failed to read archive header following archive index\n"),
446 /* Read the symbol table and long-name table from an archive. */
449 setup_archive (struct archive_info *arch, const char *file_name,
450 FILE *file, bfd_boolean is_thin_archive,
451 bfd_boolean read_symbols)
455 arch->file_name = strdup (file_name);
458 arch->index_array = NULL;
459 arch->sym_table = NULL;
461 arch->longnames = NULL;
462 arch->longnames_size = 0;
463 arch->nested_member_origin = 0;
464 arch->is_thin_archive = is_thin_archive;
465 arch->uses_64bit_indicies = FALSE;
466 arch->next_arhdr_offset = SARMAG;
468 /* Read the first archive member header. */
469 if (fseek (file, SARMAG, SEEK_SET) != 0)
471 error (_("%s: failed to seek to first archive header\n"), file_name);
474 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
475 if (got != sizeof arch->arhdr)
480 error (_("%s: failed to read archive header\n"), file_name);
484 /* See if this is the archive symbol table. */
485 if (const_strneq (arch->arhdr.ar_name, "/ "))
487 if (! process_archive_index_and_symbols (arch, 4, read_symbols))
490 else if (const_strneq (arch->arhdr.ar_name, "/SYM64/ "))
492 arch->uses_64bit_indicies = TRUE;
493 if (! process_archive_index_and_symbols (arch, 8, read_symbols))
496 else if (read_symbols)
497 printf (_("%s has no archive index\n"), file_name);
499 if (const_strneq (arch->arhdr.ar_name, "// "))
501 /* This is the archive string table holding long member names. */
502 arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
503 arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
505 arch->longnames = (char *) malloc (arch->longnames_size);
506 if (arch->longnames == NULL)
508 error (_("Out of memory reading long symbol names in archive\n"));
512 if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
514 free (arch->longnames);
515 arch->longnames = NULL;
516 error (_("%s: failed to read long symbol name string table\n"),
521 if ((arch->longnames_size & 1) != 0)
528 /* Open and setup a nested archive, if not already open. */
531 setup_nested_archive (struct archive_info *nested_arch,
532 const char *member_file_name)
536 /* Have we already setup this archive? */
537 if (nested_arch->file_name != NULL
538 && streq (nested_arch->file_name, member_file_name))
541 /* Close previous file and discard cached information. */
542 if (nested_arch->file != NULL)
543 fclose (nested_arch->file);
544 release_archive (nested_arch);
546 member_file = fopen (member_file_name, "rb");
547 if (member_file == NULL)
549 return setup_archive (nested_arch, member_file_name, member_file,
553 /* Release the memory used for the archive information. */
556 release_archive (struct archive_info * arch)
558 if (arch->file_name != NULL)
559 free (arch->file_name);
560 if (arch->index_array != NULL)
561 free (arch->index_array);
562 if (arch->sym_table != NULL)
563 free (arch->sym_table);
564 if (arch->longnames != NULL)
565 free (arch->longnames);
568 /* Get the name of an archive member from the current archive header.
569 For simple names, this will modify the ar_name field of the current
570 archive header. For long names, it will return a pointer to the
571 longnames table. For nested archives, it will open the nested archive
572 and get the name recursively. NESTED_ARCH is a single-entry cache so
573 we don't keep rereading the same information from a nested archive. */
576 get_archive_member_name (struct archive_info *arch,
577 struct archive_info *nested_arch)
581 if (arch->arhdr.ar_name[0] == '/')
583 /* We have a long name. */
585 char *member_file_name;
588 if (arch->longnames == NULL || arch->longnames_size == 0)
590 error (_("Archive member uses long names, but no longname table found\n"));
594 arch->nested_member_origin = 0;
595 k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
596 if (arch->is_thin_archive && endp != NULL && * endp == ':')
597 arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
599 while ((j < arch->longnames_size)
600 && (arch->longnames[j] != '\n')
601 && (arch->longnames[j] != '\0'))
603 if (arch->longnames[j-1] == '/')
605 arch->longnames[j] = '\0';
607 if (!arch->is_thin_archive || arch->nested_member_origin == 0)
608 return arch->longnames + k;
610 /* This is a proxy for a member of a nested archive.
611 Find the name of the member in that archive. */
612 member_file_name = adjust_relative_path (arch->file_name,
613 arch->longnames + k, j - k);
614 if (member_file_name != NULL
615 && setup_nested_archive (nested_arch, member_file_name) == 0)
617 member_name = get_archive_member_name_at (nested_arch,
618 arch->nested_member_origin,
620 if (member_name != NULL)
622 free (member_file_name);
626 free (member_file_name);
628 /* Last resort: just return the name of the nested archive. */
629 return arch->longnames + k;
632 /* We have a normal (short) name. */
633 for (j = 0; j < sizeof (arch->arhdr.ar_name); j++)
634 if (arch->arhdr.ar_name[j] == '/')
636 arch->arhdr.ar_name[j] = '\0';
637 return arch->arhdr.ar_name;
640 /* The full ar_name field is used. Don't rely on ar_date starting
643 char *name = xmalloc (sizeof (arch->arhdr.ar_name) + 1);
644 memcpy (name, arch->arhdr.ar_name, sizeof (arch->arhdr.ar_name));
645 name[sizeof (arch->arhdr.ar_name)] = '\0';
650 /* Get the name of an archive member at a given OFFSET within an archive
654 get_archive_member_name_at (struct archive_info *arch,
655 unsigned long offset,
656 struct archive_info *nested_arch)
660 if (fseek (arch->file, offset, SEEK_SET) != 0)
662 error (_("%s: failed to seek to next file name\n"), arch->file_name);
665 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
666 if (got != sizeof arch->arhdr)
668 error (_("%s: failed to read archive header\n"), arch->file_name);
671 if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
673 error (_("%s: did not find a valid archive header\n"),
678 return get_archive_member_name (arch, nested_arch);
681 /* Construct a string showing the name of the archive member, qualified
682 with the name of the containing archive file. For thin archives, we
683 use square brackets to denote the indirection. For nested archives,
684 we show the qualified name of the external member inside the square
685 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
688 make_qualified_name (struct archive_info * arch,
689 struct archive_info * nested_arch,
690 const char *member_name)
692 const char * error_name = _("<corrupt>");
696 len = strlen (arch->file_name) + strlen (member_name) + 3;
697 if (arch->is_thin_archive
698 && arch->nested_member_origin != 0)
700 /* PR 15140: Allow for corrupt thin archives. */
701 if (nested_arch->file_name)
702 len += strlen (nested_arch->file_name) + 2;
704 len += strlen (error_name) + 2;
707 name = (char *) malloc (len);
710 error (_("Out of memory\n"));
714 if (arch->is_thin_archive
715 && arch->nested_member_origin != 0)
717 if (nested_arch->file_name)
718 snprintf (name, len, "%s[%s(%s)]", arch->file_name,
719 nested_arch->file_name, member_name);
721 snprintf (name, len, "%s[%s(%s)]", arch->file_name,
722 error_name, member_name);
724 else if (arch->is_thin_archive)
725 snprintf (name, len, "%s[%s]", arch->file_name, member_name);
727 snprintf (name, len, "%s(%s)", arch->file_name, member_name);