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); \
240 fatal_error ("elf32_getshdr() failed: %s", elf_errmsg (-1)); \
242 fatal_error ("elf64_getshdr() failed: %s", elf_errmsg (-1)); \
245 shdr->sh_name = sh_name; \
246 shdr->sh_type = sh_type; \
247 shdr->sh_addralign = POINTER_SIZE / BITS_PER_UNIT; \
248 shdr->sh_flags = 0; \
249 shdr->sh_entsize = 0; \
252 DEFINE_INIT_SHDR (32)
253 DEFINE_INIT_SHDR (64)
255 static bool first_data_block;
257 /* Begin a new ELF section named NAME with type TYPE in the current output
258 file. TYPE is an SHT_* macro from the libelf headers. */
261 lto_elf_begin_section_with_type (const char *name, size_t type)
267 /* Grab the current output file and do some basic assertion checking. */
268 file = (lto_elf_file *) lto_get_current_out_file (),
270 gcc_assert (file->elf);
271 gcc_assert (!file->scn);
273 /* Create a new section. */
274 scn = elf_newscn (file->elf);
276 fatal_error ("could not create a new ELF section: %s", elf_errmsg (-1));
279 /* Add a string table entry and record the offset. */
280 gcc_assert (file->shstrtab_stream);
281 sh_name = file->shstrtab_stream->total_size;
282 lto_output_data_stream (file->shstrtab_stream, name, strlen (name) + 1);
284 /* Initialize the section header. */
285 switch (cached_file_attrs.bits)
288 init_shdr32 (scn, sh_name, type);
292 init_shdr64 (scn, sh_name, type);
299 first_data_block = true;
303 /* Begin a new ELF section named NAME in the current output file. */
306 lto_elf_begin_section (const char *name)
308 lto_elf_begin_section_with_type (name, SHT_PROGBITS);
312 /* Append DATA of length LEN to the current output section. BASE is a pointer
313 to the output page containing DATA. It is freed once the output file has
317 lto_elf_append_data (const void *data, size_t len, void *block)
321 struct lto_char_ptr_base *base = (struct lto_char_ptr_base *) block;
323 /* Grab the current output file and do some basic assertion checking. */
324 file = (lto_elf_file *) lto_get_current_out_file ();
326 gcc_assert (file->scn);
328 elf_data = elf_newdata (file->scn);
330 fatal_error ("could not append data to ELF section: %s", elf_errmsg (-1));
332 if (first_data_block)
334 elf_data->d_align = POINTER_SIZE / BITS_PER_UNIT;
335 first_data_block = false;
338 elf_data->d_align = 1;
339 elf_data->d_buf = CONST_CAST (void *, data);
340 elf_data->d_off = 0LL;
341 elf_data->d_size = len;
342 elf_data->d_type = ELF_T_BYTE;
343 elf_data->d_version = EV_CURRENT;
345 base->ptr = (char *)file->data;
350 /* End the current output section. This just does some assertion checking
351 and sets the current output file's scn member to NULL. */
354 lto_elf_end_section (void)
358 /* Grab the current output file and validate some basic assertions. */
359 file = (lto_elf_file *) lto_get_current_out_file ();
361 gcc_assert (file->scn);
367 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
368 uninitialized, caches the architecture. */
370 #define DEFINE_VALIDATE_EHDR(BITS) \
372 validate_ehdr##BITS (lto_elf_file *elf_file) \
374 Elf##BITS##_Ehdr *elf_header; \
376 elf_header = elf##BITS##_getehdr (elf_file->elf); \
379 error ("could not read ELF header: %s", elf_errmsg (0)); \
383 if (elf_header->e_type != ET_REL) \
385 error ("not a relocatable ELF object file"); \
389 if (!cached_file_attrs.initialized) \
390 cached_file_attrs.elf_machine = elf_header->e_machine; \
392 if (cached_file_attrs.elf_machine != elf_header->e_machine) \
394 error ("inconsistent file architecture detected"); \
401 DEFINE_VALIDATE_EHDR (32)
402 DEFINE_VALIDATE_EHDR (64)
405 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
406 uninitialized, caches the results. Also records the section header string
407 table's section index. Returns true on success or false on failure. */
410 validate_file (lto_elf_file *elf_file)
412 const char *elf_ident;
414 /* Some aspects of the libelf API are dependent on whether the
415 object file is a 32-bit or 64-bit file. Determine which kind of
417 elf_ident = elf_getident (elf_file->elf, NULL);
420 error ("could not read ELF identification information: %s",
426 if (!cached_file_attrs.initialized)
428 switch (elf_ident[EI_CLASS])
431 cached_file_attrs.bits = 32;
435 cached_file_attrs.bits = 64;
439 error ("unsupported ELF file class");
443 memcpy (cached_file_attrs.elf_ident, elf_ident,
444 sizeof cached_file_attrs.elf_ident);
447 if (memcmp (elf_ident, cached_file_attrs.elf_ident,
448 sizeof cached_file_attrs.elf_ident))
451 /* Check that the input file is a relocatable object file with the correct
453 switch (cached_file_attrs.bits)
456 if (!validate_ehdr32 (elf_file))
461 if (!validate_ehdr64 (elf_file))
469 /* Read the string table used for section header names. */
470 if (elf_getshdrstrndx (elf_file->elf, &elf_file->sec_strtab) == -1)
472 error ("could not locate ELF string table: %s", elf_errmsg (0));
476 cached_file_attrs.initialized = true;
481 /* Helper functions used by init_ehdr. Initialize ELF_FILE's executable
482 header using cached data from previously read files. */
484 #define DEFINE_INIT_EHDR(BITS) \
486 init_ehdr##BITS (lto_elf_file *elf_file) \
488 Elf##BITS##_Ehdr *ehdr; \
490 gcc_assert (cached_file_attrs.bits); \
492 ehdr = elf##BITS##_newehdr (elf_file->elf); \
496 fatal_error ("elf32_newehdr() failed: %s", elf_errmsg (-1)); \
498 fatal_error ("elf64_newehdr() failed: %s", elf_errmsg (-1)); \
501 memcpy (ehdr->e_ident, cached_file_attrs.elf_ident, \
502 sizeof cached_file_attrs.elf_ident); \
503 ehdr->e_type = ET_REL; \
504 ehdr->e_version = EV_CURRENT; \
505 ehdr->e_machine = cached_file_attrs.elf_machine; \
508 DEFINE_INIT_EHDR (32)
509 DEFINE_INIT_EHDR (64)
512 /* Initialize ELF_FILE's executable header using cached data from previously
516 init_ehdr (lto_elf_file *elf_file)
518 switch (cached_file_attrs.bits)
521 init_ehdr32 (elf_file);
525 init_ehdr64 (elf_file);
534 /* Open ELF file FILENAME. If WRITABLE is true, the file is opened for write
535 and, if necessary, created. Otherwise, the file is opened for reading.
536 Returns the opened file. */
539 lto_elf_file_open (const char *filename, bool writable)
541 lto_elf_file *elf_file;
545 elf_file = XCNEW (lto_elf_file);
546 result = (lto_file *) elf_file;
547 lto_file_init (result, filename);
551 elf_file->fd = open (filename, writable ? O_WRONLY|O_CREAT : O_RDONLY, 0666);
552 if (elf_file->fd == -1)
554 error ("could not open file %s", filename);
558 /* Initialize the ELF library. */
559 if (elf_version (EV_CURRENT) == EV_NONE)
561 error ("ELF library is older than that used when building GCC");
565 /* Open the ELF file descriptor. */
566 elf_file->elf = elf_begin (elf_file->fd, writable ? ELF_C_WRITE : ELF_C_READ,
570 error ("could not open ELF file: %s", elf_errmsg (0));
576 init_ehdr (elf_file);
577 elf_file->shstrtab_stream = XCNEW (struct lto_output_stream);
578 /* Output an empty string to the section header table. This becomes the
579 name of the initial NULL section. */
580 lto_output_1_stream (elf_file->shstrtab_stream, '\0');
583 if (!validate_file (elf_file))
589 lto_elf_file_close (result);
594 /* Close ELF file FILE and clean up any associated data structures. If FILE
595 was opened for writing, the file's ELF data is written at this time, and
596 any cached data buffers are freed. */
599 lto_elf_file_close (lto_file *file)
601 lto_elf_file *elf_file = (lto_elf_file *) file;
602 struct lto_char_ptr_base *cur, *tmp;
604 /* Write the ELF section header string table. */
605 if (elf_file->shstrtab_stream)
608 GElf_Ehdr *ehdr_p, ehdr_buf;
609 lto_file *old_file = lto_set_current_out_file (file);
611 lto_elf_begin_section_with_type (".shstrtab", SHT_STRTAB);
612 ehdr_p = gelf_getehdr (elf_file->elf, &ehdr_buf);
614 fatal_error ("gelf_getehdr() failed: %s", elf_errmsg (-1));
615 strtab = elf_ndxscn (elf_file->scn);
616 if (strtab < SHN_LORESERVE)
617 ehdr_p->e_shstrndx = strtab;
620 GElf_Shdr *shdr_p, shdr_buf;
621 Elf_Scn *scn_p = elf_getscn (elf_file->elf, 0);
623 fatal_error ("elf_getscn() failed: %s", elf_errmsg (-1));
624 shdr_p = gelf_getshdr (scn_p, &shdr_buf);
626 fatal_error ("gelf_getshdr() failed: %s", elf_errmsg (-1));
627 shdr_p->sh_link = strtab;
628 if (gelf_update_shdr (scn_p, shdr_p) == 0)
629 fatal_error ("gelf_update_shdr() failed: %s", elf_errmsg (-1));
630 ehdr_p->e_shstrndx = SHN_XINDEX;
632 if (gelf_update_ehdr (elf_file->elf, ehdr_p) == 0)
633 fatal_error ("gelf_update_ehdr() failed: %s", elf_errmsg (-1));
634 lto_write_stream (elf_file->shstrtab_stream);
635 lto_elf_end_section ();
637 lto_set_current_out_file (old_file);
638 free (elf_file->shstrtab_stream);
640 if (elf_update (elf_file->elf, ELF_C_WRITE) < 0)
641 fatal_error ("elf_update() failed: %s", elf_errmsg (-1));
645 elf_end (elf_file->elf);
646 if (elf_file->fd != -1)
647 close (elf_file->fd);
649 /* Free any ELF data buffers. */
650 cur = elf_file->data;
654 cur = (struct lto_char_ptr_base *) cur->ptr;
662 /* The current output file. */
663 static lto_file *current_out_file;
666 /* Sets the current output file to FILE. Returns the old output file or
670 lto_set_current_out_file (lto_file *file)
672 lto_file *old_file = current_out_file;
673 current_out_file = file;
678 /* Returns the current output file. */
681 lto_get_current_out_file (void)
683 return current_out_file;