Add --input-osabi and --output-osabi to elfedit.
[external/binutils.git] / binutils / elfedit.c
1 /* elfedit.c -- Update the ELF header of an ELF format file
2    Copyright 2010
3    Free Software Foundation, Inc.
4
5    This file is part of GNU Binutils.
6
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.
11
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.
16
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.  */
21 \f
22 #include "config.h"
23 #include "sysdep.h"
24 #include <assert.h>
25 #include <sys/stat.h>
26
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
34
35 #include "bfd.h"
36 #include "bucomm.h"
37
38 #include "elf/common.h"
39 #include "elf/external.h"
40 #include "elf/internal.h"
41
42
43 #include "aout/ar.h"
44
45 #include "getopt.h"
46 #include "libiberty.h"
47 #include "safe-ctype.h"
48 #include "filenames.h"
49
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;
63
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)
67
68 void
69 non_fatal (const char *message, ...)
70 {
71   va_list args;
72
73   va_start (args, message);
74   fprintf (stderr, _("%s: Error: "), program_name);
75   vfprintf (stderr, message, args);
76   va_end (args);
77 }
78
79 #define BYTE_GET(field)         byte_get (field, sizeof (field))
80 #define BYTE_PUT(field, val)    byte_put (field, val, sizeof (field))
81
82 static bfd_vma (*byte_get) (unsigned char *, int);
83 static void (*byte_put) (unsigned char *, bfd_vma, int);
84
85 static bfd_vma
86 byte_get_little_endian (unsigned char *field, int size)
87 {
88   switch (size)
89     {
90     case 1:
91       return *field;
92
93     case 2:
94       return  ((unsigned int) (field[0]))
95         |    (((unsigned int) (field[1])) << 8);
96
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);
102
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);
121
122     default:
123       non_fatal (_("Unhandled data length: %d\n"), size);
124       abort ();
125     }
126 }
127
128 static bfd_vma
129 byte_get_big_endian (unsigned char *field, int size)
130 {
131   switch (size)
132     {
133     case 1:
134       return *field;
135
136     case 2:
137       return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
138
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);
144
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)
156         {
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);
164         }
165
166     default:
167       non_fatal (_("Unhandled data length: %d\n"), size);
168       abort ();
169     }
170 }
171
172 static void
173 byte_put_little_endian (unsigned char * field, bfd_vma value, int size)
174 {
175   switch (size)
176     {
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;
193
194     default:
195       non_fatal (_("Unhandled data length: %d\n"), size);
196       abort ();
197     }
198 }
199
200 static void
201 byte_put_big_endian (unsigned char * field, bfd_vma value, int size)
202 {
203   switch (size)
204     {
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;
225
226     default:
227       non_fatal (_("Unhandled data length: %d\n"), size);
228       abort ();
229     }
230 }
231
232 static int
233 update_elf_header (const char *file_name, FILE *file)
234 {
235   int class, machine, type, status, osabi;
236
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)
241     {
242       non_fatal
243         (_("%s: Not an ELF file - wrong magic bytes at the start\n"),
244          file_name);
245       return 0;
246     }
247
248   if (elf_header.e_ident[EI_VERSION] != EV_CURRENT)
249     {
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;
255     }
256
257   /* Return if e_machine is the same as output_elf_machine.  */
258   if (output_elf_machine == elf_header.e_machine)
259     return 1;
260
261   class = elf_header.e_ident[EI_CLASS];
262
263   /* Skip if class doesn't match. */
264   if (input_elf_class != -1 && class != input_elf_class)
265     {
266       non_fatal
267         (_("%s: Unmatched EI_CLASS: %d is not %d\n"),
268          file_name, class, input_elf_class);
269       return 0;
270     }
271
272   machine = elf_header.e_machine;
273
274   /* Skip if e_machine doesn't match. */
275   if (input_elf_machine != -1 && machine != input_elf_machine)
276     {
277       non_fatal
278         (_("%s: Unmatched e_machine: %d is not %d\n"),
279          file_name, machine, input_elf_machine);
280       return 0;
281     }
282
283   type = elf_header.e_type;
284
285   /* Skip if e_type doesn't match. */
286   if (input_elf_type != -1 && type != input_elf_type)
287     {
288       non_fatal
289         (_("%s: Unmatched e_type: %d is not %d\n"),
290          file_name, type, input_elf_type);
291       return 0;
292     }
293
294   osabi = elf_header.e_ident[EI_OSABI];
295
296   /* Skip if OSABI doesn't match. */
297   if (input_elf_osabi != -1 && osabi != input_elf_osabi)
298     {
299       non_fatal
300         (_("%s: Unmatched EI_OSABI: %d is not %d\n"),
301          file_name, osabi, input_elf_osabi);
302       return 0;
303     }
304
305   /* Update e_machine, e_type and EI_OSABI.  */
306   switch (class)
307     {
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;
330     }
331
332   if (status != 1)
333     non_fatal (_("%s: Failed to update ELF header: %s\n"),
334                file_name, strerror (errno));
335
336   return status;
337 }
338
339 static int
340 get_file_header (FILE * file)
341 {
342   /* Read in the identity array.  */
343   if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
344     return 0;
345
346   /* Determine how to read the rest of the header.  */
347   switch (elf_header.e_ident[EI_DATA])
348     {
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;
359     }
360
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])
364     {
365     default:
366       non_fatal (_("Unsupported EI_CLASS: %d\n"),
367                  elf_header.e_ident[EI_CLASS]);
368       return 0;
369
370     case ELFCLASS32:
371       if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT,
372                  1, file) != 1)
373         return 0;
374
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);
388
389       memcpy (&ehdr32, &elf_header, EI_NIDENT);
390       break;
391
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)
398         {
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;
402         }
403
404       if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT,
405                  1, file) != 1)
406         return 0;
407
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);
421
422       memcpy (&ehdr64, &elf_header, EI_NIDENT);
423       break;
424     }
425   return 1;
426 }
427
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.  */
431
432 static int
433 process_object (const char *file_name, FILE *file)
434 {
435   /* Rememeber where we are.  */
436   long offset = ftell (file);
437
438   if (! get_file_header (file))
439     {
440       non_fatal (_("%s: Failed to read ELF header\n"), file_name);
441       return 1;
442     }
443
444   /* Go to the position of the ELF header.  */
445   if (fseek (file, offset, SEEK_SET) != 0)
446     {
447       non_fatal (_("%s: Failed to seek to ELF header\n"), file_name);
448     }
449
450   if (! update_elf_header (file_name, file))
451     return 1;
452
453   return 0;
454 }
455
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.  */
459
460 static char *
461 adjust_relative_path (const char *file_name, char * name, int name_len)
462 {
463   char * member_file_name;
464   const char * base_name = lbasename (file_name);
465
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)
473     {
474       member_file_name = malloc (name_len + 1);
475       if (member_file_name == NULL)
476         {
477           non_fatal (_("Out of memory\n"));
478           return NULL;
479         }
480       memcpy (member_file_name, name, name_len);
481       member_file_name[name_len] = '\0';
482     }
483   else
484     {
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)
490         {
491           non_fatal (_("Out of memory\n"));
492           return NULL;
493         }
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';
497     }
498   return member_file_name;
499 }
500
501 /* Structure to hold information about an archive file.  */
502
503 struct archive_info
504 {
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.  */
517 };
518
519 /* Read the symbol table and long-name table from an archive.  */
520
521 static int
522 setup_archive (struct archive_info * arch, const char * file_name,
523                FILE * file, bfd_boolean is_thin_archive)
524 {
525   size_t got;
526   unsigned long size;
527
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;
539
540   /* Read the first archive member header.  */
541   if (fseek (file, SARMAG, SEEK_SET) != 0)
542     {
543       non_fatal (_("%s: failed to seek to first archive header\n"),
544                  file_name);
545       return 1;
546     }
547   got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
548   if (got != sizeof arch->arhdr)
549     {
550       if (got == 0)
551         return 0;
552
553       non_fatal (_("%s: failed to read archive header\n"), file_name);
554       return 1;
555     }
556
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/         "))
560     {
561       size = strtoul (arch->arhdr.ar_size, NULL, 10);
562       size = size + (size & 1);
563
564       arch->next_arhdr_offset += sizeof arch->arhdr + size;
565
566       if (fseek (file, size, SEEK_CUR) != 0)
567         {
568           non_fatal (_("%s: failed to skip archive symbol table\n"),
569                      file_name);
570           return 1;
571         }
572
573       /* Read the next archive header.  */
574       got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
575       if (got != sizeof arch->arhdr)
576         {
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;
582         }
583     }
584
585   if (const_strneq (arch->arhdr.ar_name, "//              "))
586     {
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;
590
591       arch->longnames = malloc (arch->longnames_size);
592       if (arch->longnames == NULL)
593         {
594           non_fatal (_("Out of memory reading long symbol names in archive\n"));
595           return 1;
596         }
597
598       if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
599         {
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;
605         }
606
607       if ((arch->longnames_size & 1) != 0)
608         getc (file);
609     }
610
611   return 0;
612 }
613
614 /* Release the memory used for the archive information.  */
615
616 static void
617 release_archive (struct archive_info * arch)
618 {
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);
627 }
628
629 /* Open and setup a nested archive, if not already open.  */
630
631 static int
632 setup_nested_archive (struct archive_info * nested_arch, char * member_file_name)
633 {
634   FILE * member_file;
635
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;
640
641   /* Close previous file and discard cached information.  */
642   if (nested_arch->file != NULL)
643     fclose (nested_arch->file);
644   release_archive (nested_arch);
645
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);
651 }
652
653 static char *
654 get_archive_member_name_at (struct archive_info *  arch,
655                             unsigned long          offset,
656                             struct archive_info *  nested_arch);
657
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.  */
664
665 static char *
666 get_archive_member_name (struct archive_info *  arch,
667                          struct archive_info *  nested_arch)
668 {
669   unsigned long j, k;
670
671   if (arch->arhdr.ar_name[0] == '/')
672     {
673       /* We have a long name.  */
674       char * endp;
675       char * member_file_name;
676       char * member_name;
677
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);
682
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';
690
691       if (!arch->is_thin_archive || arch->nested_member_origin == 0)
692         return arch->longnames + k;
693
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)
704         {
705           free (member_file_name);
706           return member_name;
707         }
708       free (member_file_name);
709
710       /* Last resort: just return the name of the nested archive.  */
711       return arch->longnames + k;
712     }
713
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;
720 }
721
722 /* Get the name of an archive member at a given OFFSET within an
723    archive ARCH.  */
724
725 static char *
726 get_archive_member_name_at (struct archive_info * arch,
727                             unsigned long         offset,
728                             struct archive_info * nested_arch)
729 {
730   size_t got;
731
732   if (fseek (arch->file, offset, SEEK_SET) != 0)
733     {
734       non_fatal (_("%s: failed to seek to next file name\n"),
735                  arch->file_name);
736       return NULL;
737     }
738   got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
739   if (got != sizeof arch->arhdr)
740     {
741       non_fatal (_("%s: failed to read archive header\n"),
742                  arch->file_name);
743       return NULL;
744     }
745   if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
746     {
747       non_fatal (_("%s: did not find a valid archive header\n"),
748                  arch->file_name);
749       return NULL;
750     }
751
752   return get_archive_member_name (arch, nested_arch);
753 }
754
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)]").  */
760
761 static char *
762 make_qualified_name (struct archive_info * arch,
763                      struct archive_info * nested_arch,
764                      char * member_name)
765 {
766   size_t len;
767   char * name;
768
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;
772
773   name = malloc (len);
774   if (name == NULL)
775     {
776       non_fatal (_("Out of memory\n"));
777       return NULL;
778     }
779
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);
787
788   return name;
789 }
790
791 /* Process an ELF archive.
792    On entry the file is positioned just after the ARMAG string.  */
793
794 static int
795 process_archive (const char * file_name, FILE * file,
796                  bfd_boolean is_thin_archive)
797 {
798   struct archive_info arch;
799   struct archive_info nested_arch;
800   size_t got;
801   int ret;
802
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;
809
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;
818
819   if (setup_archive (&arch, file_name, file, is_thin_archive) != 0)
820     {
821       ret = 1;
822       goto out;
823     }
824
825   ret = 0;
826
827   while (1)
828     {
829       char * name;
830       size_t namelen;
831       char * qualified_name;
832
833       /* Read the next archive header.  */
834       if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
835         {
836           non_fatal (_("%s: failed to seek to next archive header\n"),
837                      file_name);
838           return 1;
839         }
840       got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
841       if (got != sizeof arch.arhdr)
842         {
843           if (got == 0)
844             break;
845           non_fatal (_("%s: failed to read archive header\n"),
846                      file_name);
847           ret = 1;
848           break;
849         }
850       if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
851         {
852           non_fatal (_("%s: did not find a valid archive header\n"),
853                      arch.file_name);
854           ret = 1;
855           break;
856         }
857
858       arch.next_arhdr_offset += sizeof arch.arhdr;
859
860       archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
861       if (archive_file_size & 01)
862         ++archive_file_size;
863
864       name = get_archive_member_name (&arch, &nested_arch);
865       if (name == NULL)
866         {
867           non_fatal (_("%s: bad archive file name\n"), file_name);
868           ret = 1;
869           break;
870         }
871       namelen = strlen (name);
872
873       qualified_name = make_qualified_name (&arch, &nested_arch, name);
874       if (qualified_name == NULL)
875         {
876           non_fatal (_("%s: bad archive file name\n"), file_name);
877           ret = 1;
878           break;
879         }
880
881       if (is_thin_archive && arch.nested_member_origin == 0)
882         {
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)
888             {
889               ret = 1;
890               break;
891             }
892
893           member_file = fopen (member_file_name, "r+b");
894           if (member_file == NULL)
895             {
896               non_fatal (_("Input file '%s' is not readable\n"),
897                          member_file_name);
898               free (member_file_name);
899               ret = 1;
900               break;
901             }
902
903           archive_file_offset = arch.nested_member_origin;
904
905           ret |= process_object (qualified_name, member_file);
906
907           fclose (member_file);
908           free (member_file_name);
909         }
910       else if (is_thin_archive)
911         {
912           /* This is a proxy for a member of a nested archive.  */
913           archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
914
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)
919             {
920               non_fatal (_("%s: failed to seek to archive member\n"),
921                          nested_arch.file_name);
922               ret = 1;
923               break;
924             }
925
926           ret |= process_object (qualified_name, nested_arch.file);
927         }
928       else
929         {
930           archive_file_offset = arch.next_arhdr_offset;
931           arch.next_arhdr_offset += archive_file_size;
932
933           ret |= process_object (qualified_name, file);
934         }
935
936       free (qualified_name);
937     }
938
939  out:
940   if (nested_arch.file != NULL)
941     fclose (nested_arch.file);
942   release_archive (&nested_arch);
943   release_archive (&arch);
944
945   return ret;
946 }
947
948 static int
949 check_file (const char *file_name, struct stat *statbuf_p)
950 {
951   struct stat statbuf;
952
953   if (statbuf_p == NULL)
954     statbuf_p = &statbuf;
955
956   if (stat (file_name, statbuf_p) < 0)
957     {
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;
964     }
965
966   if (! S_ISREG (statbuf_p->st_mode))
967     {
968       non_fatal (_("'%s' is not an ordinary file\n"), file_name);
969       return 1;
970     }
971
972   return 0;
973 }
974
975 static int
976 process_file (const char *file_name)
977 {
978   FILE * file;
979   char armag[SARMAG];
980   int ret;
981
982   if (check_file (file_name, NULL))
983     return 1;
984
985   file = fopen (file_name, "r+b");
986   if (file == NULL)
987     {
988       non_fatal (_("Input file '%s' is not readable\n"), file_name);
989       return 1;
990     }
991
992   if (fread (armag, SARMAG, 1, file) != 1)
993     {
994       non_fatal (_("%s: Failed to read file's magic number\n"),
995                  file_name);
996       fclose (file);
997       return 1;
998     }
999
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
1005     {
1006       rewind (file);
1007       archive_file_size = archive_file_offset = 0;
1008       ret = process_object (file_name, file);
1009     }
1010
1011   fclose (file);
1012
1013   return ret;
1014 }
1015
1016 static const struct
1017 {
1018   int osabi;
1019   const char *name;
1020 }
1021 osabis[] =
1022 {
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" }
1039 };
1040
1041 /* Return ELFOSABI_XXX for an OSABI string, OSABI.  */
1042
1043 static int
1044 elf_osabi (const char *osabi)
1045 {
1046   unsigned int i;
1047
1048   for (i = 0; i < ARRAY_SIZE (osabis); i++)
1049     if (strcasecmp (osabi, osabis[i].name) == 0)
1050       return osabis[i].osabi;
1051
1052   non_fatal (_("Unknown OSABI: %s\n"), osabi);
1053
1054   return -1;
1055 }
1056
1057 /* Return EM_XXX for a machine string, MACH.  */
1058
1059 static int
1060 elf_machine (const char *mach)
1061 {
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;
1070
1071   non_fatal (_("Unknown machine type: %s\n"), mach);
1072
1073   return -1;
1074 }
1075
1076 /* Return ELF class for a machine type, MACH.  */
1077
1078 static int
1079 elf_class (int mach)
1080 {
1081   switch (mach)
1082     {
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;
1091     }
1092 }
1093
1094 /* Return ET_XXX for a type string, TYPE.  */
1095
1096 static int
1097 elf_type (const char *type)
1098 {
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;
1107
1108   non_fatal (_("Unknown type: %s\n"), type);
1109
1110   return -1;
1111 }
1112
1113 enum command_line_switch
1114   {
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
1121   };
1122
1123 static struct option options[] =
1124 {
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}
1134 };
1135
1136 static void
1137 usage (FILE *stream, int exit_status)
1138 {
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\
1152 "),
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);
1157 }
1158
1159 int
1160 main (int argc, char ** argv)
1161 {
1162   int c, status;
1163
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);
1172
1173   expandargv (&argc, &argv);
1174
1175   while ((c = getopt_long (argc, argv, "hv",
1176                            options, (int *) 0)) != EOF)
1177     {
1178       switch (c)
1179         {
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;
1188
1189         case OPTION_OUTPUT_MACH:
1190           output_elf_machine = elf_machine (optarg);
1191           if (output_elf_machine < 0)
1192             return 1;
1193           break;
1194
1195         case OPTION_INPUT_TYPE:
1196           input_elf_type = elf_type (optarg);
1197           if (input_elf_type < 0)
1198             return 1;
1199           break;
1200
1201         case OPTION_OUTPUT_TYPE:
1202           output_elf_type = elf_type (optarg);
1203           if (output_elf_type < 0)
1204             return 1;
1205           break;
1206
1207         case OPTION_INPUT_OSABI:
1208           input_elf_osabi = elf_osabi (optarg);
1209           if (input_elf_osabi < 0)
1210             return 1;
1211           break;
1212
1213         case OPTION_OUTPUT_OSABI:
1214           output_elf_osabi = elf_osabi (optarg);
1215           if (output_elf_osabi < 0)
1216             return 1;
1217           break;
1218
1219         case 'h':
1220           usage (stdout, 0);
1221
1222         case 'v':
1223           print_version (program_name);
1224           break;
1225
1226         default:
1227           usage (stderr, 1);
1228         }
1229     }
1230
1231   if (optind == argc
1232       || (output_elf_machine == -1
1233           && output_elf_type == -1
1234           && output_elf_osabi == -1))
1235     usage (stderr, 1);
1236
1237   status = 0;
1238   while (optind < argc)
1239     status |= process_file (argv[optind++]);
1240
1241   return status;
1242 }