Fix unused warnings.
[official-gcc/graphite-test-results.git] / gcc / lto / lto-elf.c
blob6268a9c0bf9a4d696b2506196ebd8b87bc1b3917
1 /* LTO routines for ELF object files.
2 Copyright 2009, 2010 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "diagnostic-core.h"
25 #include "toplev.h"
26 #include <gelf.h>
27 #include "lto.h"
28 #include "tm.h"
29 #include "libiberty.h"
30 #include "ggc.h"
31 #include "lto-streamer.h"
33 /* Cater to hosts with half-backed <elf.h> file like HP-UX. */
34 #ifndef EM_SPARC
35 # define EM_SPARC 2
36 #endif
38 #ifndef EM_SPARC32PLUS
39 # define EM_SPARC32PLUS 18
40 #endif
42 #ifndef ELFOSABI_NONE
43 # define ELFOSABI_NONE 0
44 #endif
46 #ifndef ELFOSABI_LINUX
47 # define ELFOSABI_LINUX 3
48 #endif
50 #ifndef SHN_XINDEX
51 # define SHN_XINDEX 0xffff
52 #endif
55 /* Handle opening elf files on hosts, such as Windows, that may use
56 text file handling that will break binary access. */
57 #ifndef O_BINARY
58 # define O_BINARY 0
59 #endif
62 /* Initialize FILE, an LTO file object for FILENAME. */
63 static void
64 lto_file_init (lto_file *file, const char *filename, off_t offset)
66 file->filename = filename;
67 file->offset = offset;
70 /* An ELF file. */
71 struct lto_elf_file
73 /* The base information. */
74 lto_file base;
76 /* The system file descriptor for the file. */
77 int fd;
79 /* The libelf descriptor for the file. */
80 Elf *elf;
82 /* Section number of string table used for section names. */
83 size_t sec_strtab;
85 /* Writable file members. */
87 /* The currently active section. */
88 Elf_Scn *scn;
90 /* The output stream for section header names. */
91 struct lto_output_stream *shstrtab_stream;
93 /* Linked list of data which must be freed *after* the file has been
94 closed. This is an annoying limitation of libelf. */
95 struct lto_char_ptr_base *data;
97 typedef struct lto_elf_file lto_elf_file;
99 /* Stores executable header attributes which must be shared by all ELF files.
100 This is used for validating input files and populating output files. */
101 static struct {
102 bool initialized;
103 /* 32 or 64 bits? */
104 size_t bits;
105 unsigned char elf_ident[EI_NIDENT];
106 Elf64_Half elf_machine;
107 } cached_file_attrs;
110 /* Return the section header for SECTION. The return value is never
111 NULL. Call lto_elf_free_shdr to release the memory allocated. */
113 static Elf64_Shdr *
114 lto_elf_get_shdr (Elf_Scn *section)
116 Elf64_Shdr *shdr;
118 switch (cached_file_attrs.bits)
120 case 32:
122 Elf32_Shdr *shdr32;
124 /* Read the 32-bit section header. */
125 shdr32 = elf32_getshdr (section);
126 if (!shdr32)
127 fatal_error ("could not read section header: %s", elf_errmsg (0));
129 /* Transform it into a 64-bit section header. */
130 shdr = XNEW (Elf64_Shdr);
131 shdr->sh_name = shdr32->sh_name;
132 shdr->sh_type = shdr32->sh_type;
133 shdr->sh_flags = shdr32->sh_flags;
134 shdr->sh_addr = shdr32->sh_addr;
135 shdr->sh_offset = shdr32->sh_offset;
136 shdr->sh_size = shdr32->sh_size;
137 shdr->sh_link = shdr32->sh_link;
138 shdr->sh_info = shdr32->sh_info;
139 shdr->sh_addralign = shdr32->sh_addralign;
140 shdr->sh_entsize = shdr32->sh_entsize;
141 break;
143 break;
145 case 64:
146 shdr = elf64_getshdr (section);
147 if (!shdr)
148 fatal_error ("could not read section header: %s", elf_errmsg (0));
149 break;
151 default:
152 gcc_unreachable ();
155 return shdr;
158 /* Free SHDR, previously allocated by lto_elf_get_shdr. */
159 static void
160 lto_elf_free_shdr (Elf64_Shdr *shdr)
162 if (cached_file_attrs.bits != 64)
163 free (shdr);
166 /* Build a hash table whose key is the section names and whose data is
167 the start and size of each section in the .o file. */
169 htab_t
170 lto_obj_build_section_table (lto_file *lto_file)
172 lto_elf_file *elf_file = (lto_elf_file *)lto_file;
173 htab_t section_hash_table;
174 Elf_Scn *section;
175 size_t base_offset;
177 section_hash_table = lto_obj_create_section_hash_table ();
179 base_offset = elf_getbase (elf_file->elf);
180 /* We are reasonably sure that elf_getbase does not fail at this
181 point. So assume that we run into the incompatibility with
182 the FreeBSD libelf implementation that has a non-working
183 elf_getbase for non-archive members in which case the offset
184 should be zero. */
185 if (base_offset == (size_t)-1)
186 base_offset = 0;
187 for (section = elf_getscn (elf_file->elf, 0);
188 section;
189 section = elf_nextscn (elf_file->elf, section))
191 Elf64_Shdr *shdr;
192 const char *name;
193 size_t offset;
194 char *new_name;
195 void **slot;
196 struct lto_section_slot s_slot;
198 /* Get the name of this section. */
199 shdr = lto_elf_get_shdr (section);
200 offset = shdr->sh_name;
201 name = elf_strptr (elf_file->elf,
202 elf_file->sec_strtab,
203 offset);
205 /* Only put lto stuff into the symtab. */
206 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
207 strlen (LTO_SECTION_NAME_PREFIX)) != 0)
209 lto_elf_free_shdr (shdr);
210 continue;
213 new_name = XNEWVEC (char, strlen (name) + 1);
214 strcpy (new_name, name);
215 s_slot.name = new_name;
216 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
217 if (*slot == NULL)
219 struct lto_section_slot *new_slot = XNEW (struct lto_section_slot);
221 new_slot->name = new_name;
222 /* The offset into the file for this section. */
223 new_slot->start = base_offset + shdr->sh_offset;
224 new_slot->len = shdr->sh_size;
225 *slot = new_slot;
227 else
229 error ("two or more sections for %s:", new_name);
230 return NULL;
233 lto_elf_free_shdr (shdr);
236 return section_hash_table;
240 /* Initialize the section header of section SCN. SH_NAME is the section name
241 as an index into the section header string table. SH_TYPE is the section
242 type, an SHT_* macro from libelf headers. */
244 #define DEFINE_INIT_SHDR(BITS) \
245 static void \
246 init_shdr##BITS (Elf_Scn *scn, size_t sh_name, size_t sh_type) \
248 Elf##BITS##_Shdr *shdr; \
250 shdr = elf##BITS##_getshdr (scn); \
251 if (!shdr) \
253 if (BITS == 32) \
254 fatal_error ("elf32_getshdr() failed: %s", elf_errmsg (-1)); \
255 else \
256 fatal_error ("elf64_getshdr() failed: %s", elf_errmsg (-1)); \
259 shdr->sh_name = sh_name; \
260 shdr->sh_type = sh_type; \
261 shdr->sh_addralign = POINTER_SIZE / BITS_PER_UNIT; \
262 shdr->sh_flags = 0; \
263 shdr->sh_entsize = 0; \
266 DEFINE_INIT_SHDR (32)
267 DEFINE_INIT_SHDR (64)
269 static bool first_data_block;
271 /* Begin a new ELF section named NAME with type TYPE in the current output
272 file. TYPE is an SHT_* macro from the libelf headers. */
274 static void
275 lto_elf_begin_section_with_type (const char *name, size_t type)
277 lto_elf_file *file;
278 Elf_Scn *scn;
279 size_t sh_name;
281 /* Grab the current output file and do some basic assertion checking. */
282 file = (lto_elf_file *) lto_get_current_out_file (),
283 gcc_assert (file);
284 gcc_assert (file->elf);
285 gcc_assert (!file->scn);
287 /* Create a new section. */
288 scn = elf_newscn (file->elf);
289 if (!scn)
290 fatal_error ("could not create a new ELF section: %s", elf_errmsg (-1));
291 file->scn = scn;
293 /* Add a string table entry and record the offset. */
294 gcc_assert (file->shstrtab_stream);
295 sh_name = file->shstrtab_stream->total_size;
296 lto_output_data_stream (file->shstrtab_stream, name, strlen (name) + 1);
298 /* Initialize the section header. */
299 switch (cached_file_attrs.bits)
301 case 32:
302 init_shdr32 (scn, sh_name, type);
303 break;
305 case 64:
306 init_shdr64 (scn, sh_name, type);
307 break;
309 default:
310 gcc_unreachable ();
313 first_data_block = true;
317 /* Begin a new ELF section named NAME in the current output file. */
319 void
320 lto_obj_begin_section (const char *name)
322 lto_elf_begin_section_with_type (name, SHT_PROGBITS);
326 /* Append DATA of length LEN to the current output section. BASE is a pointer
327 to the output page containing DATA. It is freed once the output file has
328 been written. */
330 void
331 lto_obj_append_data (const void *data, size_t len, void *block)
333 lto_elf_file *file;
334 Elf_Data *elf_data;
335 struct lto_char_ptr_base *base = (struct lto_char_ptr_base *) block;
337 /* Grab the current output file and do some basic assertion checking. */
338 file = (lto_elf_file *) lto_get_current_out_file ();
339 gcc_assert (file);
340 gcc_assert (file->scn);
342 elf_data = elf_newdata (file->scn);
343 if (!elf_data)
344 fatal_error ("could not append data to ELF section: %s", elf_errmsg (-1));
346 if (first_data_block)
348 elf_data->d_align = POINTER_SIZE / BITS_PER_UNIT;
349 first_data_block = false;
351 else
352 elf_data->d_align = 1;
353 elf_data->d_buf = CONST_CAST (void *, data);
354 elf_data->d_off = 0LL;
355 elf_data->d_size = len;
356 elf_data->d_type = ELF_T_BYTE;
357 elf_data->d_version = EV_CURRENT;
359 base->ptr = (char *)file->data;
360 file->data = base;
364 /* End the current output section. This just does some assertion checking
365 and sets the current output file's scn member to NULL. */
367 void
368 lto_obj_end_section (void)
370 lto_elf_file *file;
372 /* Grab the current output file and validate some basic assertions. */
373 file = (lto_elf_file *) lto_get_current_out_file ();
374 gcc_assert (file);
375 gcc_assert (file->scn);
377 file->scn = NULL;
381 /* Return true if ELF_MACHINE is compatible with the cached value of the
382 architecture and possibly update the latter. Return false otherwise.
384 Note: if you want to add more EM_* cases, you'll need to provide the
385 corresponding definitions at the beginning of the file. */
387 static bool
388 is_compatible_architecture (Elf64_Half elf_machine)
390 if (cached_file_attrs.elf_machine == elf_machine)
391 return true;
393 switch (cached_file_attrs.elf_machine)
395 case EM_SPARC:
396 if (elf_machine == EM_SPARC32PLUS)
398 cached_file_attrs.elf_machine = elf_machine;
399 return true;
401 break;
403 case EM_SPARC32PLUS:
404 if (elf_machine == EM_SPARC)
405 return true;
406 break;
408 default:
409 break;
412 return false;
416 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
417 uninitialized, caches the architecture. */
419 #define DEFINE_VALIDATE_EHDR(BITS) \
420 static bool \
421 validate_ehdr##BITS (lto_elf_file *elf_file) \
423 Elf##BITS##_Ehdr *elf_header; \
425 elf_header = elf##BITS##_getehdr (elf_file->elf); \
426 if (!elf_header) \
428 error ("could not read ELF header: %s", elf_errmsg (0)); \
429 return false; \
432 if (elf_header->e_type != ET_REL) \
434 error ("not a relocatable ELF object file"); \
435 return false; \
438 if (!cached_file_attrs.initialized) \
439 cached_file_attrs.elf_machine = elf_header->e_machine; \
440 else if (!is_compatible_architecture (elf_header->e_machine)) \
442 error ("inconsistent file architecture detected"); \
443 return false; \
446 return true; \
449 DEFINE_VALIDATE_EHDR (32)
450 DEFINE_VALIDATE_EHDR (64)
453 #ifndef HAVE_ELF_GETSHDRSTRNDX
454 /* elf_getshdrstrndx replacement for systems that lack it, but provide
455 either the gABI conformant or Solaris 2 variant of elf_getshstrndx
456 instead. */
458 static int
459 elf_getshdrstrndx (Elf *elf, size_t *dst)
461 #ifdef HAVE_ELF_GETSHSTRNDX_GABI
462 return elf_getshstrndx (elf, dst);
463 #else
464 return elf_getshstrndx (elf, dst) ? 0 : -1;
465 #endif
467 #endif
469 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
470 uninitialized, caches the results. Also records the section header string
471 table's section index. Returns true on success or false on failure. */
473 static bool
474 validate_file (lto_elf_file *elf_file)
476 const char *elf_ident;
478 /* Some aspects of the libelf API are dependent on whether the
479 object file is a 32-bit or 64-bit file. Determine which kind of
480 file this is now. */
481 elf_ident = elf_getident (elf_file->elf, NULL);
482 if (!elf_ident)
484 error ("could not read ELF identification information: %s",
485 elf_errmsg (0));
486 return false;
489 if (!cached_file_attrs.initialized)
491 switch (elf_ident[EI_CLASS])
493 case ELFCLASS32:
494 cached_file_attrs.bits = 32;
495 break;
497 case ELFCLASS64:
498 cached_file_attrs.bits = 64;
499 break;
501 default:
502 error ("unsupported ELF file class");
503 return false;
506 memcpy (cached_file_attrs.elf_ident, elf_ident,
507 sizeof cached_file_attrs.elf_ident);
509 else
511 char elf_ident_buf[EI_NIDENT];
513 memcpy (elf_ident_buf, elf_ident, sizeof elf_ident_buf);
515 if (elf_ident_buf[EI_OSABI] != cached_file_attrs.elf_ident[EI_OSABI])
517 /* Allow mixing ELFOSABI_NONE with ELFOSABI_LINUX, with the result
518 ELFOSABI_LINUX. */
519 if (elf_ident_buf[EI_OSABI] == ELFOSABI_NONE
520 && cached_file_attrs.elf_ident[EI_OSABI] == ELFOSABI_LINUX)
521 elf_ident_buf[EI_OSABI] = cached_file_attrs.elf_ident[EI_OSABI];
522 else if (elf_ident_buf[EI_OSABI] == ELFOSABI_LINUX
523 && cached_file_attrs.elf_ident[EI_OSABI] == ELFOSABI_NONE)
524 cached_file_attrs.elf_ident[EI_OSABI] = elf_ident_buf[EI_OSABI];
527 if (memcmp (elf_ident_buf, cached_file_attrs.elf_ident,
528 sizeof cached_file_attrs.elf_ident))
530 error ("incompatible ELF identification");
531 return false;
535 /* Check that the input file is a relocatable object file with the correct
536 architecture. */
537 switch (cached_file_attrs.bits)
539 case 32:
540 if (!validate_ehdr32 (elf_file))
541 return false;
542 break;
544 case 64:
545 if (!validate_ehdr64 (elf_file))
546 return false;
547 break;
549 default:
550 gcc_unreachable ();
553 /* Read the string table used for section header names. */
554 if (elf_getshdrstrndx (elf_file->elf, &elf_file->sec_strtab) == -1)
556 error ("could not locate ELF string table: %s", elf_errmsg (0));
557 return false;
560 cached_file_attrs.initialized = true;
561 return true;
565 /* Helper functions used by init_ehdr. Initialize ELF_FILE's executable
566 header using cached data from previously read files. */
568 #define DEFINE_INIT_EHDR(BITS) \
569 static void \
570 init_ehdr##BITS (lto_elf_file *elf_file) \
572 Elf##BITS##_Ehdr *ehdr; \
574 gcc_assert (cached_file_attrs.bits); \
576 ehdr = elf##BITS##_newehdr (elf_file->elf); \
577 if (!ehdr) \
579 if (BITS == 32) \
580 fatal_error ("elf32_newehdr() failed: %s", elf_errmsg (-1)); \
581 else \
582 fatal_error ("elf64_newehdr() failed: %s", elf_errmsg (-1)); \
585 memcpy (ehdr->e_ident, cached_file_attrs.elf_ident, \
586 sizeof cached_file_attrs.elf_ident); \
587 ehdr->e_type = ET_REL; \
588 ehdr->e_version = EV_CURRENT; \
589 ehdr->e_machine = cached_file_attrs.elf_machine; \
592 DEFINE_INIT_EHDR (32)
593 DEFINE_INIT_EHDR (64)
596 /* Initialize ELF_FILE's executable header using cached data from previously
597 read files. */
599 static void
600 init_ehdr (lto_elf_file *elf_file)
602 switch (cached_file_attrs.bits)
604 case 32:
605 init_ehdr32 (elf_file);
606 break;
608 case 64:
609 init_ehdr64 (elf_file);
610 break;
612 default:
613 gcc_unreachable ();
617 /* Open ELF file FILENAME. If WRITABLE is true, the file is opened for write
618 and, if necessary, created. Otherwise, the file is opened for reading.
619 Returns the opened file. */
621 lto_file *
622 lto_obj_file_open (const char *filename, bool writable)
624 lto_elf_file *elf_file;
625 lto_file *result = NULL;
626 off_t offset;
627 long loffset;
628 off_t header_offset;
629 const char *offset_p;
630 char *fname;
631 int consumed;
633 offset_p = strrchr (filename, '@');
634 if (offset_p
635 && offset_p != filename
636 && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
637 && strlen (offset_p) == (unsigned int)consumed)
639 fname = (char *) xmalloc (offset_p - filename + 1);
640 memcpy (fname, filename, offset_p - filename);
641 fname[offset_p - filename] = '\0';
642 offset = (off_t)loffset;
643 /* elf_rand expects the offset to point to the ar header, not the
644 object itself. Subtract the size of the ar header (60 bytes).
645 We don't uses sizeof (struct ar_hd) to avoid including ar.h */
646 header_offset = offset - 60;
648 else
650 fname = xstrdup (filename);
651 offset = 0;
652 header_offset = 0;
655 /* Set up. */
656 elf_file = XCNEW (lto_elf_file);
657 result = (lto_file *) elf_file;
658 lto_file_init (result, fname, offset);
659 elf_file->fd = -1;
661 /* Open the file. */
662 elf_file->fd = open (fname, writable ? O_WRONLY|O_CREAT|O_BINARY
663 : O_RDONLY|O_BINARY, 0666);
664 if (elf_file->fd == -1)
666 error ("could not open file %s", fname);
667 goto fail;
670 /* Initialize the ELF library. */
671 if (elf_version (EV_CURRENT) == EV_NONE)
673 error ("ELF library is older than that used when building GCC");
674 goto fail;
677 /* Open the ELF file descriptor. */
678 elf_file->elf = elf_begin (elf_file->fd, writable ? ELF_C_WRITE : ELF_C_READ,
679 NULL);
680 if (!elf_file->elf)
682 error ("could not open %s as an ELF file: %s", fname, elf_errmsg (0));
683 goto fail;
686 if (offset != 0)
688 Elf *e;
689 off_t t = elf_rand (elf_file->elf, header_offset);
690 if (t != header_offset)
692 error ("could not seek in archive");
693 goto fail;
696 e = elf_begin (elf_file->fd, ELF_C_READ, elf_file->elf);
697 if (e == NULL)
699 error("could not find archive member");
700 goto fail;
702 elf_end (elf_file->elf);
703 elf_file->elf = e;
706 if (writable)
708 init_ehdr (elf_file);
709 elf_file->shstrtab_stream = XCNEW (struct lto_output_stream);
710 /* Output an empty string to the section header table. This becomes the
711 name of the initial NULL section. */
712 lto_output_1_stream (elf_file->shstrtab_stream, '\0');
714 else
715 if (!validate_file (elf_file))
716 goto fail;
718 return result;
720 fail:
721 if (result)
722 lto_obj_file_close (result);
723 return NULL;
727 /* Close ELF file FILE and clean up any associated data structures. If FILE
728 was opened for writing, the file's ELF data is written at this time, and
729 any cached data buffers are freed. */
731 void
732 lto_obj_file_close (lto_file *file)
734 lto_elf_file *elf_file = (lto_elf_file *) file;
735 struct lto_char_ptr_base *cur, *tmp;
737 /* Write the ELF section header string table. */
738 if (elf_file->shstrtab_stream)
740 size_t strtab;
741 GElf_Ehdr *ehdr_p, ehdr_buf;
742 lto_file *old_file = lto_set_current_out_file (file);
744 lto_elf_begin_section_with_type (".shstrtab", SHT_STRTAB);
745 ehdr_p = gelf_getehdr (elf_file->elf, &ehdr_buf);
746 if (ehdr_p == NULL)
747 fatal_error ("gelf_getehdr() failed: %s", elf_errmsg (-1));
748 strtab = elf_ndxscn (elf_file->scn);
749 if (strtab < SHN_LORESERVE)
750 ehdr_p->e_shstrndx = strtab;
751 else
753 GElf_Shdr *shdr_p, shdr_buf;
754 Elf_Scn *scn_p = elf_getscn (elf_file->elf, 0);
755 if (scn_p == NULL)
756 fatal_error ("elf_getscn() failed: %s", elf_errmsg (-1));
757 shdr_p = gelf_getshdr (scn_p, &shdr_buf);
758 if (shdr_p == NULL)
759 fatal_error ("gelf_getshdr() failed: %s", elf_errmsg (-1));
760 shdr_p->sh_link = strtab;
761 if (gelf_update_shdr (scn_p, shdr_p) == 0)
762 fatal_error ("gelf_update_shdr() failed: %s", elf_errmsg (-1));
763 ehdr_p->e_shstrndx = SHN_XINDEX;
765 if (gelf_update_ehdr (elf_file->elf, ehdr_p) == 0)
766 fatal_error ("gelf_update_ehdr() failed: %s", elf_errmsg (-1));
767 lto_write_stream (elf_file->shstrtab_stream);
768 lto_obj_end_section ();
770 lto_set_current_out_file (old_file);
771 free (elf_file->shstrtab_stream);
773 if (elf_update (elf_file->elf, ELF_C_WRITE) < 0)
774 fatal_error ("elf_update() failed: %s", elf_errmsg (-1));
777 if (elf_file->elf)
778 elf_end (elf_file->elf);
779 if (elf_file->fd != -1)
780 close (elf_file->fd);
782 /* Free any ELF data buffers. */
783 cur = elf_file->data;
784 while (cur)
786 tmp = cur;
787 cur = (struct lto_char_ptr_base *) cur->ptr;
788 free (tmp);
791 free (file);
795 /* The current output file. */
796 static lto_file *current_out_file;
799 /* Sets the current output file to FILE. Returns the old output file or
800 NULL. */
802 lto_file *
803 lto_set_current_out_file (lto_file *file)
805 lto_file *old_file = current_out_file;
806 current_out_file = file;
807 return old_file;
811 /* Returns the current output file. */
813 lto_file *
814 lto_get_current_out_file (void)
816 return current_out_file;