1 /* LTO routines for ELF object files.
2 Copyright 2009 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
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
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/>. */
23 #include "coretypes.h"
28 #include "libiberty.h"
30 #include "lto-streamer.h"
33 /* Initialize FILE, an LTO file object for FILENAME. */
35 lto_file_init (lto_file *file, const char *filename)
37 file->filename = filename;
43 /* The base information. */
46 /* The system file descriptor for the file. */
49 /* The libelf descriptor for the file. */
52 /* Section number of string table used for section names. */
55 /* Writable file members. */
57 /* The currently active section. */
60 /* The output stream for section header names. */
61 struct lto_output_stream *shstrtab_stream;
63 /* Linked list of data which must be freed *after* the file has been
64 closed. This is an annoying limitation of libelf. */
65 struct lto_char_ptr_base *data;
67 typedef struct lto_elf_file lto_elf_file;
69 /* Stores executable header attributes which must be shared by all ELF files.
70 This is used for validating input files and populating output files. */
75 unsigned char elf_ident[EI_NIDENT];
76 Elf64_Half elf_machine;
80 /* Return the section header for SECTION. The return value is never
81 NULL. Call lto_elf_free_shdr to release the memory allocated. */
84 lto_elf_get_shdr (Elf_Scn *section)
88 switch (cached_file_attrs.bits)
94 /* Read the 32-bit section header. */
95 shdr32 = elf32_getshdr (section);
97 fatal_error ("could not read section header: %s", elf_errmsg (0));
99 /* Transform it into a 64-bit section header. */
100 shdr = XNEW (Elf64_Shdr);
101 shdr->sh_name = shdr32->sh_name;
102 shdr->sh_type = shdr32->sh_type;
103 shdr->sh_flags = shdr32->sh_flags;
104 shdr->sh_addr = shdr32->sh_addr;
105 shdr->sh_offset = shdr32->sh_offset;
106 shdr->sh_size = shdr32->sh_size;
107 shdr->sh_link = shdr32->sh_link;
108 shdr->sh_info = shdr32->sh_info;
109 shdr->sh_addralign = shdr32->sh_addralign;
110 shdr->sh_entsize = shdr32->sh_entsize;
116 shdr = elf64_getshdr (section);
118 fatal_error ("could not read section header: %s", elf_errmsg (0));
128 /* Free SHDR, previously allocated by lto_elf_get_shdr. */
130 lto_elf_free_shdr (Elf64_Shdr *shdr)
132 if (cached_file_attrs.bits != 64)
137 /* Returns a hash code for P. */
140 hash_name (const void *p)
142 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
143 return (hashval_t) htab_hash_string (ds->name);
147 /* Returns nonzero if P1 and P2 are equal. */
150 eq_name (const void *p1, const void *p2)
152 const struct lto_section_slot *s1 =
153 (const struct lto_section_slot *) p1;
154 const struct lto_section_slot *s2 =
155 (const struct lto_section_slot *) p2;
157 return strcmp (s1->name, s2->name) == 0;
161 /* Build a hash table whose key is the section names and whose data is
162 the start and size of each section in the .o file. */
165 lto_elf_build_section_table (lto_file *lto_file)
167 lto_elf_file *elf_file = (lto_elf_file *)lto_file;
168 htab_t section_hash_table;
171 section_hash_table = htab_create (37, hash_name, eq_name, free);
173 for (section = elf_getscn (elf_file->elf, 0);
175 section = elf_nextscn (elf_file->elf, section))
182 struct lto_section_slot s_slot;
184 /* Get the name of this section. */
185 shdr = lto_elf_get_shdr (section);
186 offset = shdr->sh_name;
187 name = elf_strptr (elf_file->elf,
188 elf_file->sec_strtab,
191 /* Only put lto stuff into the symtab. */
192 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
193 strlen (LTO_SECTION_NAME_PREFIX)) != 0)
195 lto_elf_free_shdr (shdr);
199 new_name = XNEWVEC (char, strlen (name) + 1);
200 strcpy (new_name, name);
201 s_slot.name = new_name;
202 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
205 struct lto_section_slot *new_slot = XNEW (struct lto_section_slot);
207 new_slot->name = new_name;
208 /* The offset into the file for this section. */
209 new_slot->start = shdr->sh_offset;
210 new_slot->len = shdr->sh_size;
215 error ("two or more sections for %s:", new_name);
219 lto_elf_free_shdr (shdr);
222 return section_hash_table;
226 /* Initialize the section header of section SCN. SH_NAME is the section name
227 as an index into the section header string table. SH_TYPE is the section
228 type, an SHT_* macro from libelf headers. */
230 #define DEFINE_INIT_SHDR(BITS) \
232 init_shdr##BITS (Elf_Scn *scn, size_t sh_name, size_t sh_type) \
234 Elf##BITS##_Shdr *shdr; \
236 shdr = elf##BITS##_getshdr (scn); \
238 fatal_error ("elf"#BITS"_getshdr() failed: %s", elf_errmsg (-1));\
240 shdr->sh_name = sh_name; \
241 shdr->sh_type = sh_type; \
242 shdr->sh_addralign = POINTER_SIZE / BITS_PER_UNIT; \
243 shdr->sh_flags = 0; \
244 shdr->sh_entsize = 0; \
247 DEFINE_INIT_SHDR (32)
248 DEFINE_INIT_SHDR (64)
250 static bool first_data_block;
252 /* Begin a new ELF section named NAME with type TYPE in the current output
253 file. TYPE is an SHT_* macro from the libelf headers. */
256 lto_elf_begin_section_with_type (const char *name, size_t type)
262 /* Grab the current output file and do some basic assertion checking. */
263 file = (lto_elf_file *) lto_get_current_out_file (),
265 gcc_assert (file->elf);
266 gcc_assert (!file->scn);
268 /* Create a new section. */
269 scn = elf_newscn (file->elf);
271 fatal_error ("could not create a new ELF section: %s", elf_errmsg (-1));
274 /* Add a string table entry and record the offset. */
275 gcc_assert (file->shstrtab_stream);
276 sh_name = file->shstrtab_stream->total_size;
277 lto_output_data_stream (file->shstrtab_stream, name, strlen (name) + 1);
279 /* Initialize the section header. */
280 switch (cached_file_attrs.bits)
283 init_shdr32 (scn, sh_name, type);
287 init_shdr64 (scn, sh_name, type);
294 first_data_block = true;
298 /* Begin a new ELF section named NAME in the current output file. */
301 lto_elf_begin_section (const char *name)
303 lto_elf_begin_section_with_type (name, SHT_PROGBITS);
307 /* Append DATA of length LEN to the current output section. BASE is a pointer
308 to the output page containing DATA. It is freed once the output file has
312 lto_elf_append_data (const void *data, size_t len, void *block)
316 struct lto_char_ptr_base *base = (struct lto_char_ptr_base *) block;
318 /* Grab the current output file and do some basic assertion checking. */
319 file = (lto_elf_file *) lto_get_current_out_file ();
321 gcc_assert (file->scn);
323 elf_data = elf_newdata (file->scn);
325 fatal_error ("could not append data to ELF section: %s", elf_errmsg (-1));
327 if (first_data_block)
329 elf_data->d_align = POINTER_SIZE / BITS_PER_UNIT;
330 first_data_block = false;
333 elf_data->d_align = 1;
334 elf_data->d_buf = CONST_CAST (void *, data);
335 elf_data->d_off = 0LL;
336 elf_data->d_size = len;
337 elf_data->d_type = ELF_T_BYTE;
338 elf_data->d_version = EV_CURRENT;
340 base->ptr = (char *)file->data;
345 /* End the current output section. This just does some assertion checking
346 and sets the current output file's scn member to NULL. */
349 lto_elf_end_section (void)
353 /* Grab the current output file and validate some basic assertions. */
354 file = (lto_elf_file *) lto_get_current_out_file ();
356 gcc_assert (file->scn);
362 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
363 uninitialized, caches the architecture. */
365 #define DEFINE_VALIDATE_EHDR(BITS) \
367 validate_ehdr##BITS (lto_elf_file *elf_file) \
369 Elf##BITS##_Ehdr *elf_header; \
371 elf_header = elf##BITS##_getehdr (elf_file->elf); \
374 error ("could not read ELF header: %s", elf_errmsg (0)); \
378 if (elf_header->e_type != ET_REL) \
380 error ("not a relocatable ELF object file"); \
384 if (!cached_file_attrs.initialized) \
385 cached_file_attrs.elf_machine = elf_header->e_machine; \
387 if (cached_file_attrs.elf_machine != elf_header->e_machine) \
389 error ("inconsistent file architecture detected"); \
396 DEFINE_VALIDATE_EHDR (32)
397 DEFINE_VALIDATE_EHDR (64)
400 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
401 uninitialized, caches the results. Also records the section header string
402 table's section index. Returns true on success or false on failure. */
405 validate_file (lto_elf_file *elf_file)
407 const char *elf_ident;
409 /* Some aspects of the libelf API are dependent on whether the
410 object file is a 32-bit or 64-bit file. Determine which kind of
412 elf_ident = elf_getident (elf_file->elf, NULL);
415 error ("could not read ELF identification information: %s",
421 if (!cached_file_attrs.initialized)
423 switch (elf_ident[EI_CLASS])
426 cached_file_attrs.bits = 32;
430 cached_file_attrs.bits = 64;
434 error ("unsupported ELF file class");
438 memcpy (cached_file_attrs.elf_ident, elf_ident,
439 sizeof cached_file_attrs.elf_ident);
442 if (memcmp (elf_ident, cached_file_attrs.elf_ident,
443 sizeof cached_file_attrs.elf_ident))
446 /* Check that the input file is a relocatable object file with the correct
448 switch (cached_file_attrs.bits)
451 if (!validate_ehdr32 (elf_file))
456 if (!validate_ehdr64 (elf_file))
464 /* Read the string table used for section header names. */
465 if (elf_getshdrstrndx (elf_file->elf, &elf_file->sec_strtab) == -1)
467 error ("could not locate ELF string table: %s", elf_errmsg (0));
471 cached_file_attrs.initialized = true;
476 /* Helper functions used by init_ehdr. Initialize ELF_FILE's executable
477 header using cached data from previously read files. */
479 #define DEFINE_INIT_EHDR(BITS) \
481 init_ehdr##BITS (lto_elf_file *elf_file) \
483 Elf##BITS##_Ehdr *ehdr; \
485 gcc_assert (cached_file_attrs.bits); \
487 ehdr = elf##BITS##_newehdr (elf_file->elf); \
489 fatal_error ("elf"#BITS"_newehdr() failed: %s", elf_errmsg (-1));\
491 memcpy (ehdr->e_ident, cached_file_attrs.elf_ident, \
492 sizeof cached_file_attrs.elf_ident); \
493 ehdr->e_type = ET_REL; \
494 ehdr->e_version = EV_CURRENT; \
495 ehdr->e_machine = cached_file_attrs.elf_machine; \
498 DEFINE_INIT_EHDR (32)
499 DEFINE_INIT_EHDR (64)
502 /* Initialize ELF_FILE's executable header using cached data from previously
506 init_ehdr (lto_elf_file *elf_file)
508 switch (cached_file_attrs.bits)
511 init_ehdr32 (elf_file);
515 init_ehdr64 (elf_file);
524 /* Open ELF file FILENAME. If WRITABLE is true, the file is opened for write
525 and, if necessary, created. Otherwise, the file is opened for reading.
526 Returns the opened file. */
529 lto_elf_file_open (const char *filename, bool writable)
531 lto_elf_file *elf_file;
535 elf_file = XCNEW (lto_elf_file);
536 result = (lto_file *) elf_file;
537 lto_file_init (result, filename);
541 elf_file->fd = open (filename, writable ? O_WRONLY|O_CREAT : O_RDONLY, 0666);
542 if (elf_file->fd == -1)
544 error ("could not open file %s", filename);
548 /* Initialize the ELF library. */
549 if (elf_version (EV_CURRENT) == EV_NONE)
551 error ("ELF library is older than that used when building GCC");
555 /* Open the ELF file descriptor. */
556 elf_file->elf = elf_begin (elf_file->fd, writable ? ELF_C_WRITE : ELF_C_READ,
560 error ("could not open ELF file: %s", elf_errmsg (0));
566 init_ehdr (elf_file);
567 elf_file->shstrtab_stream = XCNEW (struct lto_output_stream);
568 /* Output an empty string to the section header table. This becomes the
569 name of the initial NULL section. */
570 lto_output_1_stream (elf_file->shstrtab_stream, '\0');
573 if (!validate_file (elf_file))
579 lto_elf_file_close (result);
584 /* Close ELF file FILE and clean up any associated data structures. If FILE
585 was opened for writing, the file's ELF data is written at this time, and
586 any cached data buffers are freed. */
589 lto_elf_file_close (lto_file *file)
591 lto_elf_file *elf_file = (lto_elf_file *) file;
592 struct lto_char_ptr_base *cur, *tmp;
594 /* Write the ELF section header string table. */
595 if (elf_file->shstrtab_stream)
598 GElf_Ehdr *ehdr_p, ehdr_buf;
599 lto_file *old_file = lto_set_current_out_file (file);
601 lto_elf_begin_section_with_type (".shstrtab", SHT_STRTAB);
602 ehdr_p = gelf_getehdr (elf_file->elf, &ehdr_buf);
604 fatal_error ("gelf_getehdr() failed: %s", elf_errmsg (-1));
605 strtab = elf_ndxscn (elf_file->scn);
606 if (strtab < SHN_LORESERVE)
607 ehdr_p->e_shstrndx = strtab;
610 GElf_Shdr *shdr_p, shdr_buf;
611 Elf_Scn *scn_p = elf_getscn (elf_file->elf, 0);
613 fatal_error ("elf_getscn() failed: %s", elf_errmsg (-1));
614 shdr_p = gelf_getshdr (scn_p, &shdr_buf);
616 fatal_error ("gelf_getshdr() failed: %s", elf_errmsg (-1));
617 shdr_p->sh_link = strtab;
618 if (gelf_update_shdr (scn_p, shdr_p) == 0)
619 fatal_error ("gelf_update_shdr() failed: %s", elf_errmsg (-1));
620 ehdr_p->e_shstrndx = SHN_XINDEX;
622 if (gelf_update_ehdr (elf_file->elf, ehdr_p) == 0)
623 fatal_error ("gelf_update_ehdr() failed: %s", elf_errmsg (-1));
624 lto_write_stream (elf_file->shstrtab_stream);
625 lto_elf_end_section ();
627 lto_set_current_out_file (old_file);
628 free (elf_file->shstrtab_stream);
630 if (elf_update (elf_file->elf, ELF_C_WRITE) < 0)
631 fatal_error ("elf_update() failed: %s", elf_errmsg (-1));
635 elf_end (elf_file->elf);
636 if (elf_file->fd != -1)
637 close (elf_file->fd);
639 /* Free any ELF data buffers. */
640 cur = elf_file->data;
644 cur = (struct lto_char_ptr_base *) cur->ptr;
652 /* The current output file. */
653 static lto_file *current_out_file;
656 /* Sets the current output file to FILE. Returns the old output file or
660 lto_set_current_out_file (lto_file *file)
662 lto_file *old_file = current_out_file;
663 current_out_file = file;
668 /* Returns the current output file. */
671 lto_get_current_out_file (void)
673 return current_out_file;