Don't change input_elf_XXX if they are -1
[deliverable/binutils-gdb.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_class = -1;
59
60 #define streq(a,b) (strcmp ((a), (b)) == 0)
61 #define strneq(a,b,n) (strncmp ((a), (b), (n)) == 0)
62 #define const_strneq(a,b) (strncmp ((a), (b), sizeof (b) - 1) == 0)
63
64 void
65 non_fatal (const char *message, ...)
66 {
67 va_list args;
68
69 va_start (args, message);
70 fprintf (stderr, _("%s: Error: "), program_name);
71 vfprintf (stderr, message, args);
72 va_end (args);
73 }
74
75 #define BYTE_GET(field) byte_get (field, sizeof (field))
76 #define BYTE_PUT(field, val) byte_put (field, val, sizeof (field))
77
78 static bfd_vma (*byte_get) (unsigned char *, int);
79 static void (*byte_put) (unsigned char *, bfd_vma, int);
80
81 static bfd_vma
82 byte_get_little_endian (unsigned char *field, int size)
83 {
84 switch (size)
85 {
86 case 1:
87 return *field;
88
89 case 2:
90 return ((unsigned int) (field[0]))
91 | (((unsigned int) (field[1])) << 8);
92
93 case 4:
94 return ((unsigned long) (field[0]))
95 | (((unsigned long) (field[1])) << 8)
96 | (((unsigned long) (field[2])) << 16)
97 | (((unsigned long) (field[3])) << 24);
98
99 case 8:
100 if (sizeof (bfd_vma) == 8)
101 return ((bfd_vma) (field[0]))
102 | (((bfd_vma) (field[1])) << 8)
103 | (((bfd_vma) (field[2])) << 16)
104 | (((bfd_vma) (field[3])) << 24)
105 | (((bfd_vma) (field[4])) << 32)
106 | (((bfd_vma) (field[5])) << 40)
107 | (((bfd_vma) (field[6])) << 48)
108 | (((bfd_vma) (field[7])) << 56);
109 else if (sizeof (bfd_vma) == 4)
110 /* We want to extract data from an 8 byte wide field and
111 place it into a 4 byte wide field. Since this is a little
112 endian source we can just use the 4 byte extraction code. */
113 return ((unsigned long) (field[0]))
114 | (((unsigned long) (field[1])) << 8)
115 | (((unsigned long) (field[2])) << 16)
116 | (((unsigned long) (field[3])) << 24);
117
118 default:
119 non_fatal (_("Unhandled data length: %d\n"), size);
120 abort ();
121 }
122 }
123
124 static bfd_vma
125 byte_get_big_endian (unsigned char *field, int size)
126 {
127 switch (size)
128 {
129 case 1:
130 return *field;
131
132 case 2:
133 return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
134
135 case 4:
136 return ((unsigned long) (field[3]))
137 | (((unsigned long) (field[2])) << 8)
138 | (((unsigned long) (field[1])) << 16)
139 | (((unsigned long) (field[0])) << 24);
140
141 case 8:
142 if (sizeof (bfd_vma) == 8)
143 return ((bfd_vma) (field[7]))
144 | (((bfd_vma) (field[6])) << 8)
145 | (((bfd_vma) (field[5])) << 16)
146 | (((bfd_vma) (field[4])) << 24)
147 | (((bfd_vma) (field[3])) << 32)
148 | (((bfd_vma) (field[2])) << 40)
149 | (((bfd_vma) (field[1])) << 48)
150 | (((bfd_vma) (field[0])) << 56);
151 else if (sizeof (bfd_vma) == 4)
152 {
153 /* Although we are extracing data from an 8 byte wide field,
154 we are returning only 4 bytes of data. */
155 field += 4;
156 return ((unsigned long) (field[3]))
157 | (((unsigned long) (field[2])) << 8)
158 | (((unsigned long) (field[1])) << 16)
159 | (((unsigned long) (field[0])) << 24);
160 }
161
162 default:
163 non_fatal (_("Unhandled data length: %d\n"), size);
164 abort ();
165 }
166 }
167
168 static void
169 byte_put_little_endian (unsigned char * field, bfd_vma value, int size)
170 {
171 switch (size)
172 {
173 case 8:
174 field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
175 field[6] = ((value >> 24) >> 24) & 0xff;
176 field[5] = ((value >> 24) >> 16) & 0xff;
177 field[4] = ((value >> 24) >> 8) & 0xff;
178 /* Fall through. */
179 case 4:
180 field[3] = (value >> 24) & 0xff;
181 field[2] = (value >> 16) & 0xff;
182 /* Fall through. */
183 case 2:
184 field[1] = (value >> 8) & 0xff;
185 /* Fall through. */
186 case 1:
187 field[0] = value & 0xff;
188 break;
189
190 default:
191 non_fatal (_("Unhandled data length: %d\n"), size);
192 abort ();
193 }
194 }
195
196 static void
197 byte_put_big_endian (unsigned char * field, bfd_vma value, int size)
198 {
199 switch (size)
200 {
201 case 8:
202 field[7] = value & 0xff;
203 field[6] = (value >> 8) & 0xff;
204 field[5] = (value >> 16) & 0xff;
205 field[4] = (value >> 24) & 0xff;
206 value >>= 16;
207 value >>= 16;
208 /* Fall through. */
209 case 4:
210 field[3] = value & 0xff;
211 field[2] = (value >> 8) & 0xff;
212 value >>= 16;
213 /* Fall through. */
214 case 2:
215 field[1] = value & 0xff;
216 value >>= 8;
217 /* Fall through. */
218 case 1:
219 field[0] = value & 0xff;
220 break;
221
222 default:
223 non_fatal (_("Unhandled data length: %d\n"), size);
224 abort ();
225 }
226 }
227
228 static int
229 update_elf_header (const char *file_name, FILE *file)
230 {
231 int class, machine, status;
232
233 if (elf_header.e_ident[EI_MAG0] != ELFMAG0
234 || elf_header.e_ident[EI_MAG1] != ELFMAG1
235 || elf_header.e_ident[EI_MAG2] != ELFMAG2
236 || elf_header.e_ident[EI_MAG3] != ELFMAG3)
237 {
238 non_fatal
239 (_("%s: Not an ELF file - wrong magic bytes at the start\n"),
240 file_name);
241 return 0;
242 }
243
244 if (elf_header.e_ident[EI_VERSION] != EV_CURRENT)
245 {
246 non_fatal
247 (_("%s: Unsupported EI_VERSION: %d is not %d\n"),
248 file_name, elf_header.e_ident[EI_VERSION],
249 EV_CURRENT);
250 return 0;
251 }
252
253 /* Return if e_machine is the same as output_elf_machine. */
254 if (output_elf_machine == elf_header.e_machine)
255 return 1;
256
257 class = elf_header.e_ident[EI_CLASS];
258
259 /* Skip if class doesn't match. */
260 if (input_elf_class != -1 && class != input_elf_class)
261 {
262 non_fatal
263 (_("%s: Unmatched EI_CLASS: %d is not %d\n"),
264 file_name, class, input_elf_class);
265 return 0;
266 }
267
268 machine = elf_header.e_machine;
269
270 /* Skip if e_machine doesn't match. */
271 if (input_elf_machine != -1 && machine != input_elf_machine)
272 {
273 non_fatal
274 (_("%s: Unmatched e_machine: %d is not %d\n"),
275 file_name, machine, input_elf_machine);
276 return 0;
277 }
278
279 /* Update e_machine. */
280 switch (class)
281 {
282 default:
283 /* We should never get here. */
284 abort ();
285 break;
286 case ELFCLASS32:
287 BYTE_PUT (ehdr32.e_machine, output_elf_machine);
288 status = fwrite (&ehdr32, sizeof (ehdr32), 1, file) == 1;
289 break;
290 case ELFCLASS64:
291 BYTE_PUT (ehdr64.e_machine, output_elf_machine);
292 status = fwrite (&ehdr64, sizeof (ehdr64), 1, file) == 1;
293 break;
294 }
295
296 if (status != 1)
297 non_fatal (_("%s: Failed to update ELF header: %s\n"),
298 file_name, strerror (errno));
299
300 return status;
301 }
302
303 static int
304 get_file_header (FILE * file)
305 {
306 /* Read in the identity array. */
307 if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
308 return 0;
309
310 /* Determine how to read the rest of the header. */
311 switch (elf_header.e_ident[EI_DATA])
312 {
313 default: /* fall through */
314 case ELFDATANONE: /* fall through */
315 case ELFDATA2LSB:
316 byte_get = byte_get_little_endian;
317 byte_put = byte_put_little_endian;
318 break;
319 case ELFDATA2MSB:
320 byte_get = byte_get_big_endian;
321 byte_put = byte_put_big_endian;
322 break;
323 }
324
325 /* Read in the rest of the header. For now we only support 32 bit
326 and 64 bit ELF files. */
327 switch (elf_header.e_ident[EI_CLASS])
328 {
329 default:
330 non_fatal (_("Unsupported EI_CLASS: %d\n"),
331 elf_header.e_ident[EI_CLASS]);
332 return 0;
333
334 case ELFCLASS32:
335 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT,
336 1, file) != 1)
337 return 0;
338
339 elf_header.e_type = BYTE_GET (ehdr32.e_type);
340 elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
341 elf_header.e_version = BYTE_GET (ehdr32.e_version);
342 elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
343 elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
344 elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
345 elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
346 elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
347 elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
348 elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
349 elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
350 elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
351 elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
352
353 memcpy (&ehdr32, &elf_header, EI_NIDENT);
354 break;
355
356 case ELFCLASS64:
357 /* If we have been compiled with sizeof (bfd_vma) == 4, then
358 we will not be able to cope with the 64bit data found in
359 64 ELF files. Detect this now and abort before we start
360 overwriting things. */
361 if (sizeof (bfd_vma) < 8)
362 {
363 non_fatal (_("This executable has been built without support for a\n\
364 64 bit data type and so it cannot process 64 bit ELF files.\n"));
365 return 0;
366 }
367
368 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT,
369 1, file) != 1)
370 return 0;
371
372 elf_header.e_type = BYTE_GET (ehdr64.e_type);
373 elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
374 elf_header.e_version = BYTE_GET (ehdr64.e_version);
375 elf_header.e_entry = BYTE_GET (ehdr64.e_entry);
376 elf_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
377 elf_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
378 elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
379 elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
380 elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
381 elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
382 elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
383 elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
384 elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
385
386 memcpy (&ehdr64, &elf_header, EI_NIDENT);
387 break;
388 }
389 return 1;
390 }
391
392 /* Process one ELF object file according to the command line options.
393 This file may actually be stored in an archive. The file is
394 positioned at the start of the ELF object. */
395
396 static int
397 process_object (const char *file_name, FILE *file)
398 {
399 /* Rememeber where we are. */
400 long offset = ftell (file);
401
402 if (! get_file_header (file))
403 {
404 non_fatal (_("%s: Failed to read ELF header\n"), file_name);
405 return 1;
406 }
407
408 /* Go to the position of the ELF header. */
409 if (fseek (file, offset, SEEK_SET) != 0)
410 {
411 non_fatal (_("%s: Failed to seek to ELF header\n"), file_name);
412 }
413
414 if (! update_elf_header (file_name, file))
415 return 1;
416
417 return 0;
418 }
419
420 /* Return the path name for a proxy entry in a thin archive, adjusted relative
421 to the path name of the thin archive itself if necessary. Always returns
422 a pointer to malloc'ed memory. */
423
424 static char *
425 adjust_relative_path (const char *file_name, char * name, int name_len)
426 {
427 char * member_file_name;
428 const char * base_name = lbasename (file_name);
429
430 /* This is a proxy entry for a thin archive member.
431 If the extended name table contains an absolute path
432 name, or if the archive is in the current directory,
433 use the path name as given. Otherwise, we need to
434 find the member relative to the directory where the
435 archive is located. */
436 if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
437 {
438 member_file_name = malloc (name_len + 1);
439 if (member_file_name == NULL)
440 {
441 non_fatal (_("Out of memory\n"));
442 return NULL;
443 }
444 memcpy (member_file_name, name, name_len);
445 member_file_name[name_len] = '\0';
446 }
447 else
448 {
449 /* Concatenate the path components of the archive file name
450 to the relative path name from the extended name table. */
451 size_t prefix_len = base_name - file_name;
452 member_file_name = malloc (prefix_len + name_len + 1);
453 if (member_file_name == NULL)
454 {
455 non_fatal (_("Out of memory\n"));
456 return NULL;
457 }
458 memcpy (member_file_name, file_name, prefix_len);
459 memcpy (member_file_name + prefix_len, name, name_len);
460 member_file_name[prefix_len + name_len] = '\0';
461 }
462 return member_file_name;
463 }
464
465 /* Structure to hold information about an archive file. */
466
467 struct archive_info
468 {
469 char * file_name; /* Archive file name. */
470 FILE * file; /* Open file descriptor. */
471 unsigned long index_num; /* Number of symbols in table. */
472 unsigned long * index_array; /* The array of member offsets. */
473 char * sym_table; /* The symbol table. */
474 unsigned long sym_size; /* Size of the symbol table. */
475 char * longnames; /* The long file names table. */
476 unsigned long longnames_size; /* Size of the long file names table. */
477 unsigned long nested_member_origin; /* Origin in the nested archive of the current member. */
478 unsigned long next_arhdr_offset; /* Offset of the next archive header. */
479 bfd_boolean is_thin_archive; /* TRUE if this is a thin archive. */
480 struct ar_hdr arhdr; /* Current archive header. */
481 };
482
483 /* Read the symbol table and long-name table from an archive. */
484
485 static int
486 setup_archive (struct archive_info * arch, const char * file_name,
487 FILE * file, bfd_boolean is_thin_archive)
488 {
489 size_t got;
490 unsigned long size;
491
492 arch->file_name = strdup (file_name);
493 arch->file = file;
494 arch->index_num = 0;
495 arch->index_array = NULL;
496 arch->sym_table = NULL;
497 arch->sym_size = 0;
498 arch->longnames = NULL;
499 arch->longnames_size = 0;
500 arch->nested_member_origin = 0;
501 arch->is_thin_archive = is_thin_archive;
502 arch->next_arhdr_offset = SARMAG;
503
504 /* Read the first archive member header. */
505 if (fseek (file, SARMAG, SEEK_SET) != 0)
506 {
507 non_fatal (_("%s: failed to seek to first archive header\n"),
508 file_name);
509 return 1;
510 }
511 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
512 if (got != sizeof arch->arhdr)
513 {
514 if (got == 0)
515 return 0;
516
517 non_fatal (_("%s: failed to read archive header\n"), file_name);
518 return 1;
519 }
520
521 /* See if this is the archive symbol table. */
522 if (const_strneq (arch->arhdr.ar_name, "/ ")
523 || const_strneq (arch->arhdr.ar_name, "/SYM64/ "))
524 {
525 size = strtoul (arch->arhdr.ar_size, NULL, 10);
526 size = size + (size & 1);
527
528 arch->next_arhdr_offset += sizeof arch->arhdr + size;
529
530 if (fseek (file, size, SEEK_CUR) != 0)
531 {
532 non_fatal (_("%s: failed to skip archive symbol table\n"),
533 file_name);
534 return 1;
535 }
536
537 /* Read the next archive header. */
538 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
539 if (got != sizeof arch->arhdr)
540 {
541 if (got == 0)
542 return 0;
543 non_fatal (_("%s: failed to read archive header following archive index\n"),
544 file_name);
545 return 1;
546 }
547 }
548
549 if (const_strneq (arch->arhdr.ar_name, "// "))
550 {
551 /* This is the archive string table holding long member names. */
552 arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
553 arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
554
555 arch->longnames = malloc (arch->longnames_size);
556 if (arch->longnames == NULL)
557 {
558 non_fatal (_("Out of memory reading long symbol names in archive\n"));
559 return 1;
560 }
561
562 if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
563 {
564 free (arch->longnames);
565 arch->longnames = NULL;
566 non_fatal (_("%s: failed to read long symbol name string table\n")
567 , file_name);
568 return 1;
569 }
570
571 if ((arch->longnames_size & 1) != 0)
572 getc (file);
573 }
574
575 return 0;
576 }
577
578 /* Release the memory used for the archive information. */
579
580 static void
581 release_archive (struct archive_info * arch)
582 {
583 if (arch->file_name != NULL)
584 free (arch->file_name);
585 if (arch->index_array != NULL)
586 free (arch->index_array);
587 if (arch->sym_table != NULL)
588 free (arch->sym_table);
589 if (arch->longnames != NULL)
590 free (arch->longnames);
591 }
592
593 /* Open and setup a nested archive, if not already open. */
594
595 static int
596 setup_nested_archive (struct archive_info * nested_arch, char * member_file_name)
597 {
598 FILE * member_file;
599
600 /* Have we already setup this archive? */
601 if (nested_arch->file_name != NULL
602 && streq (nested_arch->file_name, member_file_name))
603 return 0;
604
605 /* Close previous file and discard cached information. */
606 if (nested_arch->file != NULL)
607 fclose (nested_arch->file);
608 release_archive (nested_arch);
609
610 member_file = fopen (member_file_name, "r+b");
611 if (member_file == NULL)
612 return 1;
613 return setup_archive (nested_arch, member_file_name, member_file,
614 FALSE);
615 }
616
617 static char *
618 get_archive_member_name_at (struct archive_info * arch,
619 unsigned long offset,
620 struct archive_info * nested_arch);
621
622 /* Get the name of an archive member from the current archive header.
623 For simple names, this will modify the ar_name field of the current
624 archive header. For long names, it will return a pointer to the
625 longnames table. For nested archives, it will open the nested archive
626 and get the name recursively. NESTED_ARCH is a single-entry cache so
627 we don't keep rereading the same information from a nested archive. */
628
629 static char *
630 get_archive_member_name (struct archive_info * arch,
631 struct archive_info * nested_arch)
632 {
633 unsigned long j, k;
634
635 if (arch->arhdr.ar_name[0] == '/')
636 {
637 /* We have a long name. */
638 char * endp;
639 char * member_file_name;
640 char * member_name;
641
642 arch->nested_member_origin = 0;
643 k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
644 if (arch->is_thin_archive && endp != NULL && * endp == ':')
645 arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
646
647 while ((j < arch->longnames_size)
648 && (arch->longnames[j] != '\n')
649 && (arch->longnames[j] != '\0'))
650 j++;
651 if (arch->longnames[j-1] == '/')
652 j--;
653 arch->longnames[j] = '\0';
654
655 if (!arch->is_thin_archive || arch->nested_member_origin == 0)
656 return arch->longnames + k;
657
658 /* This is a proxy for a member of a nested archive.
659 Find the name of the member in that archive. */
660 member_file_name = adjust_relative_path (arch->file_name,
661 arch->longnames + k,
662 j - k);
663 if (member_file_name != NULL
664 && setup_nested_archive (nested_arch, member_file_name) == 0
665 && (member_name = get_archive_member_name_at (nested_arch,
666 arch->nested_member_origin,
667 NULL)) != NULL)
668 {
669 free (member_file_name);
670 return member_name;
671 }
672 free (member_file_name);
673
674 /* Last resort: just return the name of the nested archive. */
675 return arch->longnames + k;
676 }
677
678 /* We have a normal (short) name. */
679 j = 0;
680 while ((arch->arhdr.ar_name[j] != '/') && (j < 16))
681 j++;
682 arch->arhdr.ar_name[j] = '\0';
683 return arch->arhdr.ar_name;
684 }
685
686 /* Get the name of an archive member at a given OFFSET within an
687 archive ARCH. */
688
689 static char *
690 get_archive_member_name_at (struct archive_info * arch,
691 unsigned long offset,
692 struct archive_info * nested_arch)
693 {
694 size_t got;
695
696 if (fseek (arch->file, offset, SEEK_SET) != 0)
697 {
698 non_fatal (_("%s: failed to seek to next file name\n"),
699 arch->file_name);
700 return NULL;
701 }
702 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
703 if (got != sizeof arch->arhdr)
704 {
705 non_fatal (_("%s: failed to read archive header\n"),
706 arch->file_name);
707 return NULL;
708 }
709 if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
710 {
711 non_fatal (_("%s: did not find a valid archive header\n"),
712 arch->file_name);
713 return NULL;
714 }
715
716 return get_archive_member_name (arch, nested_arch);
717 }
718
719 /* Construct a string showing the name of the archive member, qualified
720 with the name of the containing archive file. For thin archives, we
721 use square brackets to denote the indirection. For nested archives,
722 we show the qualified name of the external member inside the square
723 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
724
725 static char *
726 make_qualified_name (struct archive_info * arch,
727 struct archive_info * nested_arch,
728 char * member_name)
729 {
730 size_t len;
731 char * name;
732
733 len = strlen (arch->file_name) + strlen (member_name) + 3;
734 if (arch->is_thin_archive && arch->nested_member_origin != 0)
735 len += strlen (nested_arch->file_name) + 2;
736
737 name = malloc (len);
738 if (name == NULL)
739 {
740 non_fatal (_("Out of memory\n"));
741 return NULL;
742 }
743
744 if (arch->is_thin_archive && arch->nested_member_origin != 0)
745 snprintf (name, len, "%s[%s(%s)]", arch->file_name, nested_arch->file_name, member_name);
746 else if (arch->is_thin_archive)
747 snprintf (name, len, "%s[%s]", arch->file_name, member_name);
748 else
749 snprintf (name, len, "%s(%s)", arch->file_name, member_name);
750
751 return name;
752 }
753
754 /* Process an ELF archive.
755 On entry the file is positioned just after the ARMAG string. */
756
757 static int
758 process_archive (const char * file_name, FILE * file,
759 bfd_boolean is_thin_archive)
760 {
761 struct archive_info arch;
762 struct archive_info nested_arch;
763 size_t got;
764 size_t file_name_size;
765 int ret;
766
767 /* The ARCH structure is used to hold information about this archive. */
768 arch.file_name = NULL;
769 arch.file = NULL;
770 arch.index_array = NULL;
771 arch.sym_table = NULL;
772 arch.longnames = NULL;
773
774 /* The NESTED_ARCH structure is used as a single-item cache of information
775 about a nested archive (when members of a thin archive reside within
776 another regular archive file). */
777 nested_arch.file_name = NULL;
778 nested_arch.file = NULL;
779 nested_arch.index_array = NULL;
780 nested_arch.sym_table = NULL;
781 nested_arch.longnames = NULL;
782
783 if (setup_archive (&arch, file_name, file, is_thin_archive) != 0)
784 {
785 ret = 1;
786 goto out;
787 }
788
789 file_name_size = strlen (file_name);
790 ret = 0;
791
792 while (1)
793 {
794 char * name;
795 size_t namelen;
796 char * qualified_name;
797
798 /* Read the next archive header. */
799 if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
800 {
801 non_fatal (_("%s: failed to seek to next archive header\n"),
802 file_name);
803 return 1;
804 }
805 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
806 if (got != sizeof arch.arhdr)
807 {
808 if (got == 0)
809 break;
810 non_fatal (_("%s: failed to read archive header\n"),
811 file_name);
812 ret = 1;
813 break;
814 }
815 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
816 {
817 non_fatal (_("%s: did not find a valid archive header\n"),
818 arch.file_name);
819 ret = 1;
820 break;
821 }
822
823 arch.next_arhdr_offset += sizeof arch.arhdr;
824
825 archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
826 if (archive_file_size & 01)
827 ++archive_file_size;
828
829 name = get_archive_member_name (&arch, &nested_arch);
830 if (name == NULL)
831 {
832 non_fatal (_("%s: bad archive file name\n"), file_name);
833 ret = 1;
834 break;
835 }
836 namelen = strlen (name);
837
838 qualified_name = make_qualified_name (&arch, &nested_arch, name);
839 if (qualified_name == NULL)
840 {
841 non_fatal (_("%s: bad archive file name\n"), file_name);
842 ret = 1;
843 break;
844 }
845
846 if (is_thin_archive && arch.nested_member_origin == 0)
847 {
848 /* This is a proxy for an external member of a thin archive. */
849 FILE *member_file;
850 char *member_file_name = adjust_relative_path (file_name,
851 name, namelen);
852 if (member_file_name == NULL)
853 {
854 ret = 1;
855 break;
856 }
857
858 member_file = fopen (member_file_name, "r+b");
859 if (member_file == NULL)
860 {
861 non_fatal (_("Input file '%s' is not readable\n"),
862 member_file_name);
863 free (member_file_name);
864 ret = 1;
865 break;
866 }
867
868 archive_file_offset = arch.nested_member_origin;
869
870 ret |= process_object (qualified_name, member_file);
871
872 fclose (member_file);
873 free (member_file_name);
874 }
875 else if (is_thin_archive)
876 {
877 /* This is a proxy for a member of a nested archive. */
878 archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
879
880 /* The nested archive file will have been opened and setup by
881 get_archive_member_name. */
882 if (fseek (nested_arch.file, archive_file_offset,
883 SEEK_SET) != 0)
884 {
885 non_fatal (_("%s: failed to seek to archive member\n"),
886 nested_arch.file_name);
887 ret = 1;
888 break;
889 }
890
891 ret |= process_object (qualified_name, nested_arch.file);
892 }
893 else
894 {
895 archive_file_offset = arch.next_arhdr_offset;
896 arch.next_arhdr_offset += archive_file_size;
897
898 ret |= process_object (qualified_name, file);
899 }
900
901 free (qualified_name);
902 }
903
904 out:
905 if (nested_arch.file != NULL)
906 fclose (nested_arch.file);
907 release_archive (&nested_arch);
908 release_archive (&arch);
909
910 return ret;
911 }
912
913 static int
914 check_file (const char *file_name, struct stat *statbuf_p)
915 {
916 struct stat statbuf;
917
918 if (statbuf_p == NULL)
919 statbuf_p = &statbuf;
920
921 if (stat (file_name, statbuf_p) < 0)
922 {
923 if (errno == ENOENT)
924 non_fatal (_("'%s': No such file\n"), file_name);
925 else
926 non_fatal (_("Could not locate '%s'. System error message: %s\n"),
927 file_name, strerror (errno));
928 return 1;
929 }
930
931 if (! S_ISREG (statbuf_p->st_mode))
932 {
933 non_fatal (_("'%s' is not an ordinary file\n"), file_name);
934 return 1;
935 }
936
937 return 0;
938 }
939
940 static int
941 process_file (const char *file_name)
942 {
943 FILE * file;
944 char armag[SARMAG];
945 int ret;
946
947 if (check_file (file_name, NULL))
948 return 1;
949
950 file = fopen (file_name, "r+b");
951 if (file == NULL)
952 {
953 non_fatal (_("Input file '%s' is not readable\n"), file_name);
954 return 1;
955 }
956
957 if (fread (armag, SARMAG, 1, file) != 1)
958 {
959 non_fatal (_("%s: Failed to read file's magic number\n"),
960 file_name);
961 fclose (file);
962 return 1;
963 }
964
965 if (memcmp (armag, ARMAG, SARMAG) == 0)
966 ret = process_archive (file_name, file, FALSE);
967 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
968 ret = process_archive (file_name, file, TRUE);
969 else
970 {
971 rewind (file);
972 archive_file_size = archive_file_offset = 0;
973 ret = process_object (file_name, file);
974 }
975
976 fclose (file);
977
978 return ret;
979 }
980
981 /* Return EM_XXX for a machine string, MACH. */
982
983 static int
984 elf_machine (const char *mach)
985 {
986 if (strcasecmp (mach, "l1om") == 0)
987 return EM_L1OM;
988 if (strcasecmp (mach, "x86_64") == 0)
989 return EM_X86_64;
990 if (strcasecmp (mach, "x86-64") == 0)
991 return EM_X86_64;
992 if (strcasecmp (mach, "none") == 0)
993 return EM_NONE;
994
995 non_fatal (_("Unknown machine type: %s\n"), mach);
996
997 return -1;
998 }
999
1000 /* Return ELF class for a machine type, MACH. */
1001
1002 static int
1003 elf_class (int mach)
1004 {
1005 switch (mach)
1006 {
1007 case EM_L1OM:
1008 case EM_X86_64:
1009 return ELFCLASS64;
1010 case EM_NONE:
1011 return ELFCLASSNONE;
1012 default:
1013 non_fatal (_("Unknown machine type: %d\n"), mach);
1014 return -1;
1015 }
1016 }
1017
1018 enum command_line_switch
1019 {
1020 OPTION_INPUT_MACH = 150,
1021 OPTION_OUTPUT_MACH
1022 };
1023
1024 static struct option options[] =
1025 {
1026 {"input-mach", required_argument, 0, OPTION_INPUT_MACH},
1027 {"output-mach", required_argument, 0, OPTION_OUTPUT_MACH},
1028 {"version", no_argument, 0, 'v'},
1029 {"help", no_argument, 0, 'h'},
1030 {0, no_argument, 0, 0}
1031 };
1032
1033 static void
1034 usage (FILE *stream, int exit_status)
1035 {
1036 fprintf (stream, _("Usage: %s [option(s)] --output-mach <machine> elffile(s)\n"),
1037 program_name);
1038 fprintf (stream, _(" Update the ELF header of ELF files\n"));
1039 fprintf (stream, _(" The options are:\n"));
1040 fprintf (stream, _("\
1041 --input-mach <machine> Set input machine type to <machine>\n\
1042 --output-mach <machine> Set output machine type to <machine>\n\
1043 -h --help Display this information\n\
1044 -v --version Display the version number of %s\n\
1045 "),
1046 program_name);
1047 if (REPORT_BUGS_TO[0] && exit_status == 0)
1048 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1049 exit (exit_status);
1050 }
1051
1052 int
1053 main (int argc, char ** argv)
1054 {
1055 int c, status;
1056
1057 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1058 setlocale (LC_MESSAGES, "");
1059 #endif
1060 #if defined (HAVE_SETLOCALE)
1061 setlocale (LC_CTYPE, "");
1062 #endif
1063 bindtextdomain (PACKAGE, LOCALEDIR);
1064 textdomain (PACKAGE);
1065
1066 expandargv (&argc, &argv);
1067
1068 while ((c = getopt_long (argc, argv, "hv",
1069 options, (int *) 0)) != EOF)
1070 {
1071 switch (c)
1072 {
1073 case OPTION_INPUT_MACH:
1074 input_elf_machine = elf_machine (optarg);
1075 if (input_elf_machine < 0)
1076 return 1;
1077 input_elf_class = elf_class (input_elf_machine);
1078 if (input_elf_class < 0)
1079 return 1;
1080 break;
1081
1082 case OPTION_OUTPUT_MACH:
1083 output_elf_machine = elf_machine (optarg);
1084 if (output_elf_machine < 0)
1085 return 1;
1086 break;
1087
1088 case 'h':
1089 usage (stdout, 0);
1090
1091 case 'v':
1092 print_version (program_name);
1093 break;
1094
1095 default:
1096 usage (stderr, 1);
1097 }
1098 }
1099
1100 if (optind == argc || output_elf_machine == -1)
1101 usage (stderr, 1);
1102
1103 status = 0;
1104 while (optind < argc)
1105 status |= process_file (argv[optind++]);
1106
1107 return status;
1108 }
This page took 0.052415 seconds and 5 git commands to generate.