gas: blackfin: add missing register move insns
[binutils.git] / binutils / elfedit.c
blobc9a4b5ab689827b4f379563e69f302def0f0b49b
1 /* elfedit.c -- Update the ELF header of an ELF format file
2 Copyright 2010
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "config.h"
23 #include "sysdep.h"
24 #include <assert.h>
25 #include <sys/stat.h>
27 #if __GNUC__ >= 2
28 /* Define BFD64 here, even if our default architecture is 32 bit ELF
29 as this will allow us to read in and parse 64bit and 32bit ELF files.
30 Only do this if we believe that the compiler can support a 64 bit
31 data type. For now we only rely on GCC being able to do this. */
32 #define BFD64
33 #endif
35 #include "bfd.h"
36 #include "bucomm.h"
38 #include "elf/common.h"
39 #include "elf/external.h"
40 #include "elf/internal.h"
43 #include "aout/ar.h"
45 #include "getopt.h"
46 #include "libiberty.h"
47 #include "safe-ctype.h"
48 #include "filenames.h"
50 char * program_name = "elfedit";
51 static long archive_file_offset;
52 static unsigned long archive_file_size;
53 static Elf_Internal_Ehdr elf_header;
54 static Elf32_External_Ehdr ehdr32;
55 static Elf64_External_Ehdr ehdr64;
56 static int input_elf_machine = -1;
57 static int output_elf_machine = -1;
58 static int input_elf_type = -1;
59 static int output_elf_type = -1;
60 static int input_elf_osabi = -1;
61 static int output_elf_osabi = -1;
62 static int input_elf_class = -1;
64 #define streq(a,b) (strcmp ((a), (b)) == 0)
65 #define strneq(a,b,n) (strncmp ((a), (b), (n)) == 0)
66 #define const_strneq(a,b) (strncmp ((a), (b), sizeof (b) - 1) == 0)
68 void
69 non_fatal (const char *message, ...)
71 va_list args;
73 va_start (args, message);
74 fprintf (stderr, _("%s: Error: "), program_name);
75 vfprintf (stderr, message, args);
76 va_end (args);
79 #define BYTE_GET(field) byte_get (field, sizeof (field))
80 #define BYTE_PUT(field, val) byte_put (field, val, sizeof (field))
82 static bfd_vma (*byte_get) (unsigned char *, int);
83 static void (*byte_put) (unsigned char *, bfd_vma, int);
85 static bfd_vma
86 byte_get_little_endian (unsigned char *field, int size)
88 switch (size)
90 case 1:
91 return *field;
93 case 2:
94 return ((unsigned int) (field[0]))
95 | (((unsigned int) (field[1])) << 8);
97 case 4:
98 return ((unsigned long) (field[0]))
99 | (((unsigned long) (field[1])) << 8)
100 | (((unsigned long) (field[2])) << 16)
101 | (((unsigned long) (field[3])) << 24);
103 case 8:
104 if (sizeof (bfd_vma) == 8)
105 return ((bfd_vma) (field[0]))
106 | (((bfd_vma) (field[1])) << 8)
107 | (((bfd_vma) (field[2])) << 16)
108 | (((bfd_vma) (field[3])) << 24)
109 | (((bfd_vma) (field[4])) << 32)
110 | (((bfd_vma) (field[5])) << 40)
111 | (((bfd_vma) (field[6])) << 48)
112 | (((bfd_vma) (field[7])) << 56);
113 else if (sizeof (bfd_vma) == 4)
114 /* We want to extract data from an 8 byte wide field and
115 place it into a 4 byte wide field. Since this is a little
116 endian source we can just use the 4 byte extraction code. */
117 return ((unsigned long) (field[0]))
118 | (((unsigned long) (field[1])) << 8)
119 | (((unsigned long) (field[2])) << 16)
120 | (((unsigned long) (field[3])) << 24);
122 default:
123 non_fatal (_("Unhandled data length: %d\n"), size);
124 abort ();
128 static bfd_vma
129 byte_get_big_endian (unsigned char *field, int size)
131 switch (size)
133 case 1:
134 return *field;
136 case 2:
137 return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
139 case 4:
140 return ((unsigned long) (field[3]))
141 | (((unsigned long) (field[2])) << 8)
142 | (((unsigned long) (field[1])) << 16)
143 | (((unsigned long) (field[0])) << 24);
145 case 8:
146 if (sizeof (bfd_vma) == 8)
147 return ((bfd_vma) (field[7]))
148 | (((bfd_vma) (field[6])) << 8)
149 | (((bfd_vma) (field[5])) << 16)
150 | (((bfd_vma) (field[4])) << 24)
151 | (((bfd_vma) (field[3])) << 32)
152 | (((bfd_vma) (field[2])) << 40)
153 | (((bfd_vma) (field[1])) << 48)
154 | (((bfd_vma) (field[0])) << 56);
155 else if (sizeof (bfd_vma) == 4)
157 /* Although we are extracing data from an 8 byte wide field,
158 we are returning only 4 bytes of data. */
159 field += 4;
160 return ((unsigned long) (field[3]))
161 | (((unsigned long) (field[2])) << 8)
162 | (((unsigned long) (field[1])) << 16)
163 | (((unsigned long) (field[0])) << 24);
166 default:
167 non_fatal (_("Unhandled data length: %d\n"), size);
168 abort ();
172 static void
173 byte_put_little_endian (unsigned char * field, bfd_vma value, int size)
175 switch (size)
177 case 8:
178 field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
179 field[6] = ((value >> 24) >> 24) & 0xff;
180 field[5] = ((value >> 24) >> 16) & 0xff;
181 field[4] = ((value >> 24) >> 8) & 0xff;
182 /* Fall through. */
183 case 4:
184 field[3] = (value >> 24) & 0xff;
185 field[2] = (value >> 16) & 0xff;
186 /* Fall through. */
187 case 2:
188 field[1] = (value >> 8) & 0xff;
189 /* Fall through. */
190 case 1:
191 field[0] = value & 0xff;
192 break;
194 default:
195 non_fatal (_("Unhandled data length: %d\n"), size);
196 abort ();
200 static void
201 byte_put_big_endian (unsigned char * field, bfd_vma value, int size)
203 switch (size)
205 case 8:
206 field[7] = value & 0xff;
207 field[6] = (value >> 8) & 0xff;
208 field[5] = (value >> 16) & 0xff;
209 field[4] = (value >> 24) & 0xff;
210 value >>= 16;
211 value >>= 16;
212 /* Fall through. */
213 case 4:
214 field[3] = value & 0xff;
215 field[2] = (value >> 8) & 0xff;
216 value >>= 16;
217 /* Fall through. */
218 case 2:
219 field[1] = value & 0xff;
220 value >>= 8;
221 /* Fall through. */
222 case 1:
223 field[0] = value & 0xff;
224 break;
226 default:
227 non_fatal (_("Unhandled data length: %d\n"), size);
228 abort ();
232 static int
233 update_elf_header (const char *file_name, FILE *file)
235 int class, machine, type, status, osabi;
237 if (elf_header.e_ident[EI_MAG0] != ELFMAG0
238 || elf_header.e_ident[EI_MAG1] != ELFMAG1
239 || elf_header.e_ident[EI_MAG2] != ELFMAG2
240 || elf_header.e_ident[EI_MAG3] != ELFMAG3)
242 non_fatal
243 (_("%s: Not an ELF file - wrong magic bytes at the start\n"),
244 file_name);
245 return 0;
248 if (elf_header.e_ident[EI_VERSION] != EV_CURRENT)
250 non_fatal
251 (_("%s: Unsupported EI_VERSION: %d is not %d\n"),
252 file_name, elf_header.e_ident[EI_VERSION],
253 EV_CURRENT);
254 return 0;
257 /* Return if e_machine is the same as output_elf_machine. */
258 if (output_elf_machine == elf_header.e_machine)
259 return 1;
261 class = elf_header.e_ident[EI_CLASS];
263 /* Skip if class doesn't match. */
264 if (input_elf_class != -1 && class != input_elf_class)
266 non_fatal
267 (_("%s: Unmatched EI_CLASS: %d is not %d\n"),
268 file_name, class, input_elf_class);
269 return 0;
272 machine = elf_header.e_machine;
274 /* Skip if e_machine doesn't match. */
275 if (input_elf_machine != -1 && machine != input_elf_machine)
277 non_fatal
278 (_("%s: Unmatched e_machine: %d is not %d\n"),
279 file_name, machine, input_elf_machine);
280 return 0;
283 type = elf_header.e_type;
285 /* Skip if e_type doesn't match. */
286 if (input_elf_type != -1 && type != input_elf_type)
288 non_fatal
289 (_("%s: Unmatched e_type: %d is not %d\n"),
290 file_name, type, input_elf_type);
291 return 0;
294 osabi = elf_header.e_ident[EI_OSABI];
296 /* Skip if OSABI doesn't match. */
297 if (input_elf_osabi != -1 && osabi != input_elf_osabi)
299 non_fatal
300 (_("%s: Unmatched EI_OSABI: %d is not %d\n"),
301 file_name, osabi, input_elf_osabi);
302 return 0;
305 /* Update e_machine, e_type and EI_OSABI. */
306 switch (class)
308 default:
309 /* We should never get here. */
310 abort ();
311 break;
312 case ELFCLASS32:
313 if (output_elf_machine != -1)
314 BYTE_PUT (ehdr32.e_machine, output_elf_machine);
315 if (output_elf_type != -1)
316 BYTE_PUT (ehdr32.e_type, output_elf_type);
317 if (output_elf_osabi != -1)
318 ehdr32.e_ident[EI_OSABI] = output_elf_osabi;
319 status = fwrite (&ehdr32, sizeof (ehdr32), 1, file) == 1;
320 break;
321 case ELFCLASS64:
322 if (output_elf_machine != -1)
323 BYTE_PUT (ehdr64.e_machine, output_elf_machine);
324 if (output_elf_type != -1)
325 BYTE_PUT (ehdr64.e_type, output_elf_type);
326 if (output_elf_osabi != -1)
327 ehdr64.e_ident[EI_OSABI] = output_elf_osabi;
328 status = fwrite (&ehdr64, sizeof (ehdr64), 1, file) == 1;
329 break;
332 if (status != 1)
333 non_fatal (_("%s: Failed to update ELF header: %s\n"),
334 file_name, strerror (errno));
336 return status;
339 static int
340 get_file_header (FILE * file)
342 /* Read in the identity array. */
343 if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
344 return 0;
346 /* Determine how to read the rest of the header. */
347 switch (elf_header.e_ident[EI_DATA])
349 default: /* fall through */
350 case ELFDATANONE: /* fall through */
351 case ELFDATA2LSB:
352 byte_get = byte_get_little_endian;
353 byte_put = byte_put_little_endian;
354 break;
355 case ELFDATA2MSB:
356 byte_get = byte_get_big_endian;
357 byte_put = byte_put_big_endian;
358 break;
361 /* Read in the rest of the header. For now we only support 32 bit
362 and 64 bit ELF files. */
363 switch (elf_header.e_ident[EI_CLASS])
365 default:
366 non_fatal (_("Unsupported EI_CLASS: %d\n"),
367 elf_header.e_ident[EI_CLASS]);
368 return 0;
370 case ELFCLASS32:
371 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT,
372 1, file) != 1)
373 return 0;
375 elf_header.e_type = BYTE_GET (ehdr32.e_type);
376 elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
377 elf_header.e_version = BYTE_GET (ehdr32.e_version);
378 elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
379 elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
380 elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
381 elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
382 elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
383 elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
384 elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
385 elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
386 elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
387 elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
389 memcpy (&ehdr32, &elf_header, EI_NIDENT);
390 break;
392 case ELFCLASS64:
393 /* If we have been compiled with sizeof (bfd_vma) == 4, then
394 we will not be able to cope with the 64bit data found in
395 64 ELF files. Detect this now and abort before we start
396 overwriting things. */
397 if (sizeof (bfd_vma) < 8)
399 non_fatal (_("This executable has been built without support for a\n\
400 64 bit data type and so it cannot process 64 bit ELF files.\n"));
401 return 0;
404 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT,
405 1, file) != 1)
406 return 0;
408 elf_header.e_type = BYTE_GET (ehdr64.e_type);
409 elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
410 elf_header.e_version = BYTE_GET (ehdr64.e_version);
411 elf_header.e_entry = BYTE_GET (ehdr64.e_entry);
412 elf_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
413 elf_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
414 elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
415 elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
416 elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
417 elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
418 elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
419 elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
420 elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
422 memcpy (&ehdr64, &elf_header, EI_NIDENT);
423 break;
425 return 1;
428 /* Process one ELF object file according to the command line options.
429 This file may actually be stored in an archive. The file is
430 positioned at the start of the ELF object. */
432 static int
433 process_object (const char *file_name, FILE *file)
435 /* Rememeber where we are. */
436 long offset = ftell (file);
438 if (! get_file_header (file))
440 non_fatal (_("%s: Failed to read ELF header\n"), file_name);
441 return 1;
444 /* Go to the position of the ELF header. */
445 if (fseek (file, offset, SEEK_SET) != 0)
447 non_fatal (_("%s: Failed to seek to ELF header\n"), file_name);
450 if (! update_elf_header (file_name, file))
451 return 1;
453 return 0;
456 /* Return the path name for a proxy entry in a thin archive, adjusted relative
457 to the path name of the thin archive itself if necessary. Always returns
458 a pointer to malloc'ed memory. */
460 static char *
461 adjust_relative_path (const char *file_name, char * name, int name_len)
463 char * member_file_name;
464 const char * base_name = lbasename (file_name);
466 /* This is a proxy entry for a thin archive member.
467 If the extended name table contains an absolute path
468 name, or if the archive is in the current directory,
469 use the path name as given. Otherwise, we need to
470 find the member relative to the directory where the
471 archive is located. */
472 if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
474 member_file_name = malloc (name_len + 1);
475 if (member_file_name == NULL)
477 non_fatal (_("Out of memory\n"));
478 return NULL;
480 memcpy (member_file_name, name, name_len);
481 member_file_name[name_len] = '\0';
483 else
485 /* Concatenate the path components of the archive file name
486 to the relative path name from the extended name table. */
487 size_t prefix_len = base_name - file_name;
488 member_file_name = malloc (prefix_len + name_len + 1);
489 if (member_file_name == NULL)
491 non_fatal (_("Out of memory\n"));
492 return NULL;
494 memcpy (member_file_name, file_name, prefix_len);
495 memcpy (member_file_name + prefix_len, name, name_len);
496 member_file_name[prefix_len + name_len] = '\0';
498 return member_file_name;
501 /* Structure to hold information about an archive file. */
503 struct archive_info
505 char * file_name; /* Archive file name. */
506 FILE * file; /* Open file descriptor. */
507 unsigned long index_num; /* Number of symbols in table. */
508 unsigned long * index_array; /* The array of member offsets. */
509 char * sym_table; /* The symbol table. */
510 unsigned long sym_size; /* Size of the symbol table. */
511 char * longnames; /* The long file names table. */
512 unsigned long longnames_size; /* Size of the long file names table. */
513 unsigned long nested_member_origin; /* Origin in the nested archive of the current member. */
514 unsigned long next_arhdr_offset; /* Offset of the next archive header. */
515 bfd_boolean is_thin_archive; /* TRUE if this is a thin archive. */
516 struct ar_hdr arhdr; /* Current archive header. */
519 /* Read the symbol table and long-name table from an archive. */
521 static int
522 setup_archive (struct archive_info * arch, const char * file_name,
523 FILE * file, bfd_boolean is_thin_archive)
525 size_t got;
526 unsigned long size;
528 arch->file_name = strdup (file_name);
529 arch->file = file;
530 arch->index_num = 0;
531 arch->index_array = NULL;
532 arch->sym_table = NULL;
533 arch->sym_size = 0;
534 arch->longnames = NULL;
535 arch->longnames_size = 0;
536 arch->nested_member_origin = 0;
537 arch->is_thin_archive = is_thin_archive;
538 arch->next_arhdr_offset = SARMAG;
540 /* Read the first archive member header. */
541 if (fseek (file, SARMAG, SEEK_SET) != 0)
543 non_fatal (_("%s: failed to seek to first archive header\n"),
544 file_name);
545 return 1;
547 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
548 if (got != sizeof arch->arhdr)
550 if (got == 0)
551 return 0;
553 non_fatal (_("%s: failed to read archive header\n"), file_name);
554 return 1;
557 /* See if this is the archive symbol table. */
558 if (const_strneq (arch->arhdr.ar_name, "/ ")
559 || const_strneq (arch->arhdr.ar_name, "/SYM64/ "))
561 size = strtoul (arch->arhdr.ar_size, NULL, 10);
562 size = size + (size & 1);
564 arch->next_arhdr_offset += sizeof arch->arhdr + size;
566 if (fseek (file, size, SEEK_CUR) != 0)
568 non_fatal (_("%s: failed to skip archive symbol table\n"),
569 file_name);
570 return 1;
573 /* Read the next archive header. */
574 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
575 if (got != sizeof arch->arhdr)
577 if (got == 0)
578 return 0;
579 non_fatal (_("%s: failed to read archive header following archive index\n"),
580 file_name);
581 return 1;
585 if (const_strneq (arch->arhdr.ar_name, "// "))
587 /* This is the archive string table holding long member names. */
588 arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
589 arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
591 arch->longnames = malloc (arch->longnames_size);
592 if (arch->longnames == NULL)
594 non_fatal (_("Out of memory reading long symbol names in archive\n"));
595 return 1;
598 if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
600 free (arch->longnames);
601 arch->longnames = NULL;
602 non_fatal (_("%s: failed to read long symbol name string table\n")
603 , file_name);
604 return 1;
607 if ((arch->longnames_size & 1) != 0)
608 getc (file);
611 return 0;
614 /* Release the memory used for the archive information. */
616 static void
617 release_archive (struct archive_info * arch)
619 if (arch->file_name != NULL)
620 free (arch->file_name);
621 if (arch->index_array != NULL)
622 free (arch->index_array);
623 if (arch->sym_table != NULL)
624 free (arch->sym_table);
625 if (arch->longnames != NULL)
626 free (arch->longnames);
629 /* Open and setup a nested archive, if not already open. */
631 static int
632 setup_nested_archive (struct archive_info * nested_arch, char * member_file_name)
634 FILE * member_file;
636 /* Have we already setup this archive? */
637 if (nested_arch->file_name != NULL
638 && streq (nested_arch->file_name, member_file_name))
639 return 0;
641 /* Close previous file and discard cached information. */
642 if (nested_arch->file != NULL)
643 fclose (nested_arch->file);
644 release_archive (nested_arch);
646 member_file = fopen (member_file_name, "r+b");
647 if (member_file == NULL)
648 return 1;
649 return setup_archive (nested_arch, member_file_name, member_file,
650 FALSE);
653 static char *
654 get_archive_member_name_at (struct archive_info * arch,
655 unsigned long offset,
656 struct archive_info * nested_arch);
658 /* Get the name of an archive member from the current archive header.
659 For simple names, this will modify the ar_name field of the current
660 archive header. For long names, it will return a pointer to the
661 longnames table. For nested archives, it will open the nested archive
662 and get the name recursively. NESTED_ARCH is a single-entry cache so
663 we don't keep rereading the same information from a nested archive. */
665 static char *
666 get_archive_member_name (struct archive_info * arch,
667 struct archive_info * nested_arch)
669 unsigned long j, k;
671 if (arch->arhdr.ar_name[0] == '/')
673 /* We have a long name. */
674 char * endp;
675 char * member_file_name;
676 char * member_name;
678 arch->nested_member_origin = 0;
679 k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
680 if (arch->is_thin_archive && endp != NULL && * endp == ':')
681 arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
683 while ((j < arch->longnames_size)
684 && (arch->longnames[j] != '\n')
685 && (arch->longnames[j] != '\0'))
686 j++;
687 if (arch->longnames[j-1] == '/')
688 j--;
689 arch->longnames[j] = '\0';
691 if (!arch->is_thin_archive || arch->nested_member_origin == 0)
692 return arch->longnames + k;
694 /* This is a proxy for a member of a nested archive.
695 Find the name of the member in that archive. */
696 member_file_name = adjust_relative_path (arch->file_name,
697 arch->longnames + k,
698 j - k);
699 if (member_file_name != NULL
700 && setup_nested_archive (nested_arch, member_file_name) == 0
701 && (member_name = get_archive_member_name_at (nested_arch,
702 arch->nested_member_origin,
703 NULL)) != NULL)
705 free (member_file_name);
706 return member_name;
708 free (member_file_name);
710 /* Last resort: just return the name of the nested archive. */
711 return arch->longnames + k;
714 /* We have a normal (short) name. */
715 j = 0;
716 while ((arch->arhdr.ar_name[j] != '/') && (j < 16))
717 j++;
718 arch->arhdr.ar_name[j] = '\0';
719 return arch->arhdr.ar_name;
722 /* Get the name of an archive member at a given OFFSET within an
723 archive ARCH. */
725 static char *
726 get_archive_member_name_at (struct archive_info * arch,
727 unsigned long offset,
728 struct archive_info * nested_arch)
730 size_t got;
732 if (fseek (arch->file, offset, SEEK_SET) != 0)
734 non_fatal (_("%s: failed to seek to next file name\n"),
735 arch->file_name);
736 return NULL;
738 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
739 if (got != sizeof arch->arhdr)
741 non_fatal (_("%s: failed to read archive header\n"),
742 arch->file_name);
743 return NULL;
745 if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
747 non_fatal (_("%s: did not find a valid archive header\n"),
748 arch->file_name);
749 return NULL;
752 return get_archive_member_name (arch, nested_arch);
755 /* Construct a string showing the name of the archive member, qualified
756 with the name of the containing archive file. For thin archives, we
757 use square brackets to denote the indirection. For nested archives,
758 we show the qualified name of the external member inside the square
759 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
761 static char *
762 make_qualified_name (struct archive_info * arch,
763 struct archive_info * nested_arch,
764 char * member_name)
766 size_t len;
767 char * name;
769 len = strlen (arch->file_name) + strlen (member_name) + 3;
770 if (arch->is_thin_archive && arch->nested_member_origin != 0)
771 len += strlen (nested_arch->file_name) + 2;
773 name = malloc (len);
774 if (name == NULL)
776 non_fatal (_("Out of memory\n"));
777 return NULL;
780 if (arch->is_thin_archive && arch->nested_member_origin != 0)
781 snprintf (name, len, "%s[%s(%s)]", arch->file_name,
782 nested_arch->file_name, member_name);
783 else if (arch->is_thin_archive)
784 snprintf (name, len, "%s[%s]", arch->file_name, member_name);
785 else
786 snprintf (name, len, "%s(%s)", arch->file_name, member_name);
788 return name;
791 /* Process an ELF archive.
792 On entry the file is positioned just after the ARMAG string. */
794 static int
795 process_archive (const char * file_name, FILE * file,
796 bfd_boolean is_thin_archive)
798 struct archive_info arch;
799 struct archive_info nested_arch;
800 size_t got;
801 int ret;
803 /* The ARCH structure is used to hold information about this archive. */
804 arch.file_name = NULL;
805 arch.file = NULL;
806 arch.index_array = NULL;
807 arch.sym_table = NULL;
808 arch.longnames = NULL;
810 /* The NESTED_ARCH structure is used as a single-item cache of information
811 about a nested archive (when members of a thin archive reside within
812 another regular archive file). */
813 nested_arch.file_name = NULL;
814 nested_arch.file = NULL;
815 nested_arch.index_array = NULL;
816 nested_arch.sym_table = NULL;
817 nested_arch.longnames = NULL;
819 if (setup_archive (&arch, file_name, file, is_thin_archive) != 0)
821 ret = 1;
822 goto out;
825 ret = 0;
827 while (1)
829 char * name;
830 size_t namelen;
831 char * qualified_name;
833 /* Read the next archive header. */
834 if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
836 non_fatal (_("%s: failed to seek to next archive header\n"),
837 file_name);
838 return 1;
840 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
841 if (got != sizeof arch.arhdr)
843 if (got == 0)
844 break;
845 non_fatal (_("%s: failed to read archive header\n"),
846 file_name);
847 ret = 1;
848 break;
850 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
852 non_fatal (_("%s: did not find a valid archive header\n"),
853 arch.file_name);
854 ret = 1;
855 break;
858 arch.next_arhdr_offset += sizeof arch.arhdr;
860 archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
861 if (archive_file_size & 01)
862 ++archive_file_size;
864 name = get_archive_member_name (&arch, &nested_arch);
865 if (name == NULL)
867 non_fatal (_("%s: bad archive file name\n"), file_name);
868 ret = 1;
869 break;
871 namelen = strlen (name);
873 qualified_name = make_qualified_name (&arch, &nested_arch, name);
874 if (qualified_name == NULL)
876 non_fatal (_("%s: bad archive file name\n"), file_name);
877 ret = 1;
878 break;
881 if (is_thin_archive && arch.nested_member_origin == 0)
883 /* This is a proxy for an external member of a thin archive. */
884 FILE *member_file;
885 char *member_file_name = adjust_relative_path (file_name,
886 name, namelen);
887 if (member_file_name == NULL)
889 ret = 1;
890 break;
893 member_file = fopen (member_file_name, "r+b");
894 if (member_file == NULL)
896 non_fatal (_("Input file '%s' is not readable\n"),
897 member_file_name);
898 free (member_file_name);
899 ret = 1;
900 break;
903 archive_file_offset = arch.nested_member_origin;
905 ret |= process_object (qualified_name, member_file);
907 fclose (member_file);
908 free (member_file_name);
910 else if (is_thin_archive)
912 /* This is a proxy for a member of a nested archive. */
913 archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
915 /* The nested archive file will have been opened and setup by
916 get_archive_member_name. */
917 if (fseek (nested_arch.file, archive_file_offset,
918 SEEK_SET) != 0)
920 non_fatal (_("%s: failed to seek to archive member\n"),
921 nested_arch.file_name);
922 ret = 1;
923 break;
926 ret |= process_object (qualified_name, nested_arch.file);
928 else
930 archive_file_offset = arch.next_arhdr_offset;
931 arch.next_arhdr_offset += archive_file_size;
933 ret |= process_object (qualified_name, file);
936 free (qualified_name);
939 out:
940 if (nested_arch.file != NULL)
941 fclose (nested_arch.file);
942 release_archive (&nested_arch);
943 release_archive (&arch);
945 return ret;
948 static int
949 check_file (const char *file_name, struct stat *statbuf_p)
951 struct stat statbuf;
953 if (statbuf_p == NULL)
954 statbuf_p = &statbuf;
956 if (stat (file_name, statbuf_p) < 0)
958 if (errno == ENOENT)
959 non_fatal (_("'%s': No such file\n"), file_name);
960 else
961 non_fatal (_("Could not locate '%s'. System error message: %s\n"),
962 file_name, strerror (errno));
963 return 1;
966 if (! S_ISREG (statbuf_p->st_mode))
968 non_fatal (_("'%s' is not an ordinary file\n"), file_name);
969 return 1;
972 return 0;
975 static int
976 process_file (const char *file_name)
978 FILE * file;
979 char armag[SARMAG];
980 int ret;
982 if (check_file (file_name, NULL))
983 return 1;
985 file = fopen (file_name, "r+b");
986 if (file == NULL)
988 non_fatal (_("Input file '%s' is not readable\n"), file_name);
989 return 1;
992 if (fread (armag, SARMAG, 1, file) != 1)
994 non_fatal (_("%s: Failed to read file's magic number\n"),
995 file_name);
996 fclose (file);
997 return 1;
1000 if (memcmp (armag, ARMAG, SARMAG) == 0)
1001 ret = process_archive (file_name, file, FALSE);
1002 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
1003 ret = process_archive (file_name, file, TRUE);
1004 else
1006 rewind (file);
1007 archive_file_size = archive_file_offset = 0;
1008 ret = process_object (file_name, file);
1011 fclose (file);
1013 return ret;
1016 static const struct
1018 int osabi;
1019 const char *name;
1021 osabis[] =
1023 { ELFOSABI_NONE, "none" },
1024 { ELFOSABI_HPUX, "HPUX" },
1025 { ELFOSABI_NETBSD, "NetBSD" },
1026 { ELFOSABI_LINUX, "Linux" },
1027 { ELFOSABI_HURD, "Hurd" },
1028 { ELFOSABI_SOLARIS, "Solaris" },
1029 { ELFOSABI_AIX, "AIX" },
1030 { ELFOSABI_IRIX, "Irix" },
1031 { ELFOSABI_FREEBSD, "FreeBSD" },
1032 { ELFOSABI_TRU64, "TRU64" },
1033 { ELFOSABI_MODESTO, "Modesto" },
1034 { ELFOSABI_OPENBSD, "OpenBSD" },
1035 { ELFOSABI_OPENVMS, "OpenVMS" },
1036 { ELFOSABI_NSK, "NSK" },
1037 { ELFOSABI_AROS, "AROS" },
1038 { ELFOSABI_FENIXOS, "FenixOS" }
1041 /* Return ELFOSABI_XXX for an OSABI string, OSABI. */
1043 static int
1044 elf_osabi (const char *osabi)
1046 unsigned int i;
1048 for (i = 0; i < ARRAY_SIZE (osabis); i++)
1049 if (strcasecmp (osabi, osabis[i].name) == 0)
1050 return osabis[i].osabi;
1052 non_fatal (_("Unknown OSABI: %s\n"), osabi);
1054 return -1;
1057 /* Return EM_XXX for a machine string, MACH. */
1059 static int
1060 elf_machine (const char *mach)
1062 if (strcasecmp (mach, "l1om") == 0)
1063 return EM_L1OM;
1064 if (strcasecmp (mach, "x86_64") == 0)
1065 return EM_X86_64;
1066 if (strcasecmp (mach, "x86-64") == 0)
1067 return EM_X86_64;
1068 if (strcasecmp (mach, "none") == 0)
1069 return EM_NONE;
1071 non_fatal (_("Unknown machine type: %s\n"), mach);
1073 return -1;
1076 /* Return ELF class for a machine type, MACH. */
1078 static int
1079 elf_class (int mach)
1081 switch (mach)
1083 case EM_L1OM:
1084 case EM_X86_64:
1085 return ELFCLASS64;
1086 case EM_NONE:
1087 return ELFCLASSNONE;
1088 default:
1089 non_fatal (_("Unknown machine type: %d\n"), mach);
1090 return -1;
1094 /* Return ET_XXX for a type string, TYPE. */
1096 static int
1097 elf_type (const char *type)
1099 if (strcasecmp (type, "rel") == 0)
1100 return ET_REL;
1101 if (strcasecmp (type, "exec") == 0)
1102 return ET_EXEC;
1103 if (strcasecmp (type, "dyn") == 0)
1104 return ET_DYN;
1105 if (strcasecmp (type, "none") == 0)
1106 return ET_NONE;
1108 non_fatal (_("Unknown type: %s\n"), type);
1110 return -1;
1113 enum command_line_switch
1115 OPTION_INPUT_MACH = 150,
1116 OPTION_OUTPUT_MACH,
1117 OPTION_INPUT_TYPE,
1118 OPTION_OUTPUT_TYPE,
1119 OPTION_INPUT_OSABI,
1120 OPTION_OUTPUT_OSABI
1123 static struct option options[] =
1125 {"input-mach", required_argument, 0, OPTION_INPUT_MACH},
1126 {"output-mach", required_argument, 0, OPTION_OUTPUT_MACH},
1127 {"input-type", required_argument, 0, OPTION_INPUT_TYPE},
1128 {"output-type", required_argument, 0, OPTION_OUTPUT_TYPE},
1129 {"input-osabi", required_argument, 0, OPTION_INPUT_OSABI},
1130 {"output-osabi", required_argument, 0, OPTION_OUTPUT_OSABI},
1131 {"version", no_argument, 0, 'v'},
1132 {"help", no_argument, 0, 'h'},
1133 {0, no_argument, 0, 0}
1136 static void
1137 usage (FILE *stream, int exit_status)
1139 fprintf (stream, _("Usage: %s <option(s)> elffile(s)\n"),
1140 program_name);
1141 fprintf (stream, _(" Update the ELF header of ELF files\n"));
1142 fprintf (stream, _(" The options are:\n"));
1143 fprintf (stream, _("\
1144 --input-mach <machine> Set input machine type to <machine>\n\
1145 --output-mach <machine> Set output machine type to <machine>\n\
1146 --input-type <type> Set input file type to <type>\n\
1147 --output-type <type> Set output file type to <type>\n\
1148 --input-osabi <osabi> Set input OSABI to <osabi>\n\
1149 --output-osabi <osabi> Set output OSABI to <osabi>\n\
1150 -h --help Display this information\n\
1151 -v --version Display the version number of %s\n\
1153 program_name);
1154 if (REPORT_BUGS_TO[0] && exit_status == 0)
1155 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1156 exit (exit_status);
1160 main (int argc, char ** argv)
1162 int c, status;
1164 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1165 setlocale (LC_MESSAGES, "");
1166 #endif
1167 #if defined (HAVE_SETLOCALE)
1168 setlocale (LC_CTYPE, "");
1169 #endif
1170 bindtextdomain (PACKAGE, LOCALEDIR);
1171 textdomain (PACKAGE);
1173 expandargv (&argc, &argv);
1175 while ((c = getopt_long (argc, argv, "hv",
1176 options, (int *) 0)) != EOF)
1178 switch (c)
1180 case OPTION_INPUT_MACH:
1181 input_elf_machine = elf_machine (optarg);
1182 if (input_elf_machine < 0)
1183 return 1;
1184 input_elf_class = elf_class (input_elf_machine);
1185 if (input_elf_class < 0)
1186 return 1;
1187 break;
1189 case OPTION_OUTPUT_MACH:
1190 output_elf_machine = elf_machine (optarg);
1191 if (output_elf_machine < 0)
1192 return 1;
1193 break;
1195 case OPTION_INPUT_TYPE:
1196 input_elf_type = elf_type (optarg);
1197 if (input_elf_type < 0)
1198 return 1;
1199 break;
1201 case OPTION_OUTPUT_TYPE:
1202 output_elf_type = elf_type (optarg);
1203 if (output_elf_type < 0)
1204 return 1;
1205 break;
1207 case OPTION_INPUT_OSABI:
1208 input_elf_osabi = elf_osabi (optarg);
1209 if (input_elf_osabi < 0)
1210 return 1;
1211 break;
1213 case OPTION_OUTPUT_OSABI:
1214 output_elf_osabi = elf_osabi (optarg);
1215 if (output_elf_osabi < 0)
1216 return 1;
1217 break;
1219 case 'h':
1220 usage (stdout, 0);
1222 case 'v':
1223 print_version (program_name);
1224 break;
1226 default:
1227 usage (stderr, 1);
1231 if (optind == argc
1232 || (output_elf_machine == -1
1233 && output_elf_type == -1
1234 && output_elf_osabi == -1))
1235 usage (stderr, 1);
1237 status = 0;
1238 while (optind < argc)
1239 status |= process_file (argv[optind++]);
1241 return status;