OSDN Git Service

gcc:
[pf3gnuchains/gcc-fork.git] / gcc / lto / lto-elf.c
1 /* LTO routines for ELF object files.
2    Copyright 2009, 2010 Free Software Foundation, Inc.
3    Contributed by CodeSourcery, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
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"
32
33 /* Cater to hosts with half-backed <elf.h> file like HP-UX.  */
34 #ifndef EM_SPARC
35 # define EM_SPARC 2
36 #endif
37
38 #ifndef EM_SPARC32PLUS
39 # define EM_SPARC32PLUS 18
40 #endif
41
42 #ifndef ELFOSABI_NONE
43 # define ELFOSABI_NONE 0
44 #endif
45 #ifndef ELFOSABI_LINUX
46 # define ELFOSABI_LINUX 3
47 #endif
48
49
50 /* Handle opening elf files on hosts, such as Windows, that may use 
51    text file handling that will break binary access.  */
52 #ifndef O_BINARY
53 # define O_BINARY 0
54 #endif
55
56
57 /* Initialize FILE, an LTO file object for FILENAME.  */
58 static void
59 lto_file_init (lto_file *file, const char *filename, off_t offset)
60 {
61   file->filename = filename;
62   file->offset = offset;
63 }
64
65 /* An ELF file.  */
66 struct lto_elf_file 
67 {
68   /* The base information.  */
69   lto_file base;
70
71   /* The system file descriptor for the file.  */
72   int fd;
73
74   /* The libelf descriptor for the file.  */
75   Elf *elf;
76
77   /* Section number of string table used for section names.  */
78   size_t sec_strtab;
79
80   /* Writable file members.  */
81
82   /* The currently active section.  */
83   Elf_Scn *scn;
84
85   /* The output stream for section header names.  */
86   struct lto_output_stream *shstrtab_stream;
87
88   /* Linked list of data which must be freed *after* the file has been
89      closed.  This is an annoying limitation of libelf.  */
90   struct lto_char_ptr_base *data;
91 };
92 typedef struct lto_elf_file lto_elf_file;
93
94 /* Stores executable header attributes which must be shared by all ELF files.
95    This is used for validating input files and populating output files.  */
96 static struct {
97   bool initialized;
98   /* 32 or 64 bits?  */
99   size_t bits;
100   unsigned char elf_ident[EI_NIDENT];
101   Elf64_Half elf_machine;
102 } cached_file_attrs;
103
104
105 /* Return the section header for SECTION.  The return value is never
106    NULL.  Call lto_elf_free_shdr to release the memory allocated.  */
107
108 static Elf64_Shdr *
109 lto_elf_get_shdr (Elf_Scn *section)
110 {
111   Elf64_Shdr *shdr;
112
113   switch (cached_file_attrs.bits)
114     {
115     case 32:
116       {
117         Elf32_Shdr *shdr32;
118
119         /* Read the 32-bit section header.  */
120         shdr32 = elf32_getshdr (section);
121         if (!shdr32)
122           fatal_error ("could not read section header: %s", elf_errmsg (0));
123
124         /* Transform it into a 64-bit section header.  */
125         shdr = XNEW (Elf64_Shdr);
126         shdr->sh_name = shdr32->sh_name;
127         shdr->sh_type = shdr32->sh_type;
128         shdr->sh_flags = shdr32->sh_flags;
129         shdr->sh_addr = shdr32->sh_addr;
130         shdr->sh_offset = shdr32->sh_offset;
131         shdr->sh_size = shdr32->sh_size;
132         shdr->sh_link = shdr32->sh_link;
133         shdr->sh_info = shdr32->sh_info;
134         shdr->sh_addralign = shdr32->sh_addralign;
135         shdr->sh_entsize  = shdr32->sh_entsize;
136         break;
137       }
138       break;
139
140     case 64:
141       shdr = elf64_getshdr (section);
142       if (!shdr)
143         fatal_error ("could not read section header: %s", elf_errmsg (0));
144       break;
145
146     default:
147       gcc_unreachable ();
148     }
149
150   return shdr;
151 }
152
153 /* Free SHDR, previously allocated by lto_elf_get_shdr.  */
154 static void
155 lto_elf_free_shdr (Elf64_Shdr *shdr)
156 {
157   if (cached_file_attrs.bits != 64)
158     free (shdr);
159 }
160
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.  */
163
164 htab_t
165 lto_obj_build_section_table (lto_file *lto_file) 
166 {
167   lto_elf_file *elf_file = (lto_elf_file *)lto_file;
168   htab_t section_hash_table;
169   Elf_Scn *section;
170   size_t base_offset;
171
172   section_hash_table = lto_obj_create_section_hash_table ();
173
174   base_offset = elf_getbase (elf_file->elf);
175   /* We are reasonably sure that elf_getbase does not fail at this
176      point.  So assume that we run into the incompatibility with
177      the FreeBSD libelf implementation that has a non-working
178      elf_getbase for non-archive members in which case the offset
179      should be zero.  */
180   if (base_offset == (size_t)-1)
181     base_offset = 0;
182   for (section = elf_getscn (elf_file->elf, 0);
183        section;
184        section = elf_nextscn (elf_file->elf, section)) 
185     {
186       Elf64_Shdr *shdr;
187       const char *name;
188       size_t offset;
189       char *new_name;
190       void **slot;
191       struct lto_section_slot s_slot;
192
193       /* Get the name of this section.  */
194       shdr = lto_elf_get_shdr (section);
195       offset = shdr->sh_name;
196       name = elf_strptr (elf_file->elf, 
197                          elf_file->sec_strtab,
198                          offset);
199
200       /* Only put lto stuff into the symtab.  */
201       if (strncmp (name, LTO_SECTION_NAME_PREFIX, 
202                    strlen (LTO_SECTION_NAME_PREFIX)) != 0)
203         {
204           lto_elf_free_shdr (shdr);
205           continue;
206         }
207
208       new_name = XNEWVEC (char, strlen (name) + 1);
209       strcpy (new_name, name);
210       s_slot.name = new_name;
211       slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
212       if (*slot == NULL)
213         {
214           struct lto_section_slot *new_slot = XNEW (struct lto_section_slot);
215
216           new_slot->name = new_name;
217           /* The offset into the file for this section.  */
218           new_slot->start = base_offset + shdr->sh_offset;
219           new_slot->len = shdr->sh_size;
220           *slot = new_slot;
221         }
222       else
223         {
224           error ("two or more sections for %s:", new_name);
225           return NULL;
226         }
227
228       lto_elf_free_shdr (shdr);
229     }
230
231   return section_hash_table;
232 }
233
234
235 /* Initialize the section header of section SCN.  SH_NAME is the section name
236    as an index into the section header string table.  SH_TYPE is the section
237    type, an SHT_* macro from libelf headers.  */
238
239 #define DEFINE_INIT_SHDR(BITS)                                        \
240 static void                                                           \
241 init_shdr##BITS (Elf_Scn *scn, size_t sh_name, size_t sh_type)        \
242 {                                                                     \
243   Elf##BITS##_Shdr *shdr;                                             \
244                                                                       \
245   shdr = elf##BITS##_getshdr (scn);                                   \
246   if (!shdr)                                                          \
247     {                                                                 \
248       if (BITS == 32)                                                 \
249         fatal_error ("elf32_getshdr() failed: %s", elf_errmsg (-1));  \
250       else                                                            \
251         fatal_error ("elf64_getshdr() failed: %s", elf_errmsg (-1));  \
252     }                                                                 \
253                                                                       \
254   shdr->sh_name = sh_name;                                            \
255   shdr->sh_type = sh_type;                                            \
256   shdr->sh_addralign = POINTER_SIZE / BITS_PER_UNIT;                  \
257   shdr->sh_flags = 0;                                                 \
258   shdr->sh_entsize = 0;                                               \
259 }
260
261 DEFINE_INIT_SHDR (32)
262 DEFINE_INIT_SHDR (64)
263
264 static bool first_data_block;
265
266 /* Begin a new ELF section named NAME with type TYPE in the current output
267    file.  TYPE is an SHT_* macro from the libelf headers.  */
268
269 static void
270 lto_elf_begin_section_with_type (const char *name, size_t type)
271 {
272   lto_elf_file *file;
273   Elf_Scn *scn;
274   size_t sh_name;
275
276   /* Grab the current output file and do some basic assertion checking.  */
277   file = (lto_elf_file *) lto_get_current_out_file (),
278   gcc_assert (file);
279   gcc_assert (file->elf);
280   gcc_assert (!file->scn);
281
282   /* Create a new section.  */
283   scn = elf_newscn (file->elf);
284   if (!scn)
285     fatal_error ("could not create a new ELF section: %s", elf_errmsg (-1));
286   file->scn = scn;
287
288   /* Add a string table entry and record the offset.  */
289   gcc_assert (file->shstrtab_stream);
290   sh_name = file->shstrtab_stream->total_size;
291   lto_output_data_stream (file->shstrtab_stream, name, strlen (name) + 1);
292
293   /* Initialize the section header.  */
294   switch (cached_file_attrs.bits)
295     {
296     case 32:
297       init_shdr32 (scn, sh_name, type);
298       break;
299
300     case 64:
301       init_shdr64 (scn, sh_name, type);
302       break;
303
304     default:
305       gcc_unreachable ();
306     }
307
308   first_data_block = true;
309 }
310
311
312 /* Begin a new ELF section named NAME in the current output file.  */
313
314 void
315 lto_obj_begin_section (const char *name)
316 {
317   lto_elf_begin_section_with_type (name, SHT_PROGBITS);
318 }
319
320
321 /* Append DATA of length LEN to the current output section.  BASE is a pointer
322    to the output page containing DATA.  It is freed once the output file has
323    been written.  */
324
325 void
326 lto_obj_append_data (const void *data, size_t len, void *block)
327 {
328   lto_elf_file *file;
329   Elf_Data *elf_data;
330   struct lto_char_ptr_base *base = (struct lto_char_ptr_base *) block;
331
332   /* Grab the current output file and do some basic assertion checking.  */
333   file = (lto_elf_file *) lto_get_current_out_file ();
334   gcc_assert (file);
335   gcc_assert (file->scn);
336
337   elf_data = elf_newdata (file->scn);
338   if (!elf_data)
339     fatal_error ("could not append data to ELF section: %s", elf_errmsg (-1));
340
341   if (first_data_block)
342     {
343       elf_data->d_align = POINTER_SIZE / BITS_PER_UNIT;
344       first_data_block = false;
345     }
346   else
347     elf_data->d_align = 1;
348   elf_data->d_buf = CONST_CAST (void *, data);
349   elf_data->d_off = 0LL;
350   elf_data->d_size = len;
351   elf_data->d_type = ELF_T_BYTE;
352   elf_data->d_version = EV_CURRENT;
353
354   base->ptr = (char *)file->data;
355   file->data = base;
356 }
357
358
359 /* End the current output section.  This just does some assertion checking
360    and sets the current output file's scn member to NULL.  */
361
362 void
363 lto_obj_end_section (void)
364 {
365   lto_elf_file *file;
366
367   /* Grab the current output file and validate some basic assertions.  */
368   file = (lto_elf_file *) lto_get_current_out_file ();
369   gcc_assert (file);
370   gcc_assert (file->scn);
371
372   file->scn = NULL;
373 }
374
375
376 /* Return true if ELF_MACHINE is compatible with the cached value of the
377    architecture and possibly update the latter.  Return false otherwise.
378
379    Note: if you want to add more EM_* cases, you'll need to provide the
380    corresponding definitions at the beginning of the file.  */
381
382 static bool
383 is_compatible_architecture (Elf64_Half elf_machine)
384 {
385   if (cached_file_attrs.elf_machine == elf_machine)
386     return true;
387
388   switch (cached_file_attrs.elf_machine)
389     {
390     case EM_SPARC:
391       if (elf_machine == EM_SPARC32PLUS)
392         {
393           cached_file_attrs.elf_machine = elf_machine;
394           return true;
395         }
396       break;
397
398     case EM_SPARC32PLUS:
399       if (elf_machine == EM_SPARC)
400         return true;
401       break;
402
403     default:
404       break;
405     }
406
407   return false;
408 }
409
410
411 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
412    uninitialized, caches the architecture.  */
413
414 #define DEFINE_VALIDATE_EHDR(BITS)                              \
415 static bool                                                     \
416 validate_ehdr##BITS (lto_elf_file *elf_file)                    \
417 {                                                               \
418   Elf##BITS##_Ehdr *elf_header;                                 \
419                                                                 \
420   elf_header = elf##BITS##_getehdr (elf_file->elf);             \
421   if (!elf_header)                                              \
422     {                                                           \
423       error ("could not read ELF header: %s", elf_errmsg (0));  \
424       return false;                                             \
425     }                                                           \
426                                                                 \
427   if (elf_header->e_type != ET_REL)                             \
428     {                                                           \
429       error ("not a relocatable ELF object file");              \
430       return false;                                             \
431     }                                                           \
432                                                                 \
433   if (!cached_file_attrs.initialized)                           \
434     cached_file_attrs.elf_machine = elf_header->e_machine;      \
435   else if (!is_compatible_architecture (elf_header->e_machine)) \
436     {                                                           \
437       error ("inconsistent file architecture detected");        \
438       return false;                                             \
439     }                                                           \
440                                                                 \
441   return true;                                                  \
442 }
443
444 DEFINE_VALIDATE_EHDR (32)
445 DEFINE_VALIDATE_EHDR (64)
446
447
448 #ifndef HAVE_ELF_GETSHDRSTRNDX
449 /* elf_getshdrstrndx replacement for systems that lack it, but provide
450    either the gABI conformant or Solaris 2 variant of elf_getshstrndx
451    instead.  */
452
453 static int
454 elf_getshdrstrndx (Elf *elf, size_t *dst)
455 {
456 #ifdef HAVE_ELF_GETSHSTRNDX_GABI
457   return elf_getshstrndx (elf, dst);
458 #else
459   return elf_getshstrndx (elf, dst) ? 0 : -1;
460 #endif
461 }
462 #endif
463
464 /* Validate's ELF_FILE's executable header and, if cached_file_attrs is
465    uninitialized, caches the results.  Also records the section header string
466    table's section index.  Returns true on success or false on failure.  */
467
468 static bool
469 validate_file (lto_elf_file *elf_file)
470 {
471   const char *elf_ident;
472
473   /* Some aspects of the libelf API are dependent on whether the
474      object file is a 32-bit or 64-bit file.  Determine which kind of
475      file this is now.  */
476   elf_ident = elf_getident (elf_file->elf, NULL);
477   if (!elf_ident)
478     {
479       error ("could not read ELF identification information: %s",
480               elf_errmsg (0));
481       return false;
482              
483     }
484
485   if (!cached_file_attrs.initialized)
486     {
487       switch (elf_ident[EI_CLASS])
488         {
489         case ELFCLASS32:
490           cached_file_attrs.bits = 32;
491           break;
492
493         case ELFCLASS64:
494           cached_file_attrs.bits = 64;
495           break;
496
497         default:
498           error ("unsupported ELF file class");
499           return false;
500         }
501
502       memcpy (cached_file_attrs.elf_ident, elf_ident,
503               sizeof cached_file_attrs.elf_ident);
504     }
505   else
506     {
507       char elf_ident_buf[EI_NIDENT];
508
509       memcpy (elf_ident_buf, elf_ident, sizeof elf_ident_buf);
510
511       if (elf_ident_buf[EI_OSABI] != cached_file_attrs.elf_ident[EI_OSABI])
512         {
513           /* Allow mixing ELFOSABI_NONE with ELFOSABI_LINUX, with the result
514              ELFOSABI_LINUX.  */
515           if (elf_ident_buf[EI_OSABI] == ELFOSABI_NONE
516               && cached_file_attrs.elf_ident[EI_OSABI] == ELFOSABI_LINUX)
517             elf_ident_buf[EI_OSABI] = cached_file_attrs.elf_ident[EI_OSABI];
518           else if (elf_ident_buf[EI_OSABI] == ELFOSABI_LINUX
519                    && cached_file_attrs.elf_ident[EI_OSABI] == ELFOSABI_NONE)
520             cached_file_attrs.elf_ident[EI_OSABI] = elf_ident_buf[EI_OSABI];
521         }
522
523       if (memcmp (elf_ident_buf, cached_file_attrs.elf_ident,
524                   sizeof cached_file_attrs.elf_ident))
525         return false;
526     }
527
528   /* Check that the input file is a relocatable object file with the correct
529      architecture.  */
530   switch (cached_file_attrs.bits)
531     {
532     case 32:
533       if (!validate_ehdr32 (elf_file))
534         return false;
535       break;
536
537     case 64:
538       if (!validate_ehdr64 (elf_file))
539         return false;
540       break;
541
542     default:
543       gcc_unreachable ();
544     }
545
546   /* Read the string table used for section header names.  */
547   if (elf_getshdrstrndx (elf_file->elf, &elf_file->sec_strtab) == -1)
548     {
549       error ("could not locate ELF string table: %s", elf_errmsg (0));
550       return false;
551     }
552
553   cached_file_attrs.initialized = true;
554   return true;
555 }
556
557
558 /* Helper functions used by init_ehdr.  Initialize ELF_FILE's executable
559    header using cached data from previously read files.  */
560
561 #define DEFINE_INIT_EHDR(BITS)                                        \
562 static void                                                           \
563 init_ehdr##BITS (lto_elf_file *elf_file)                              \
564 {                                                                     \
565   Elf##BITS##_Ehdr *ehdr;                                             \
566                                                                       \
567   gcc_assert (cached_file_attrs.bits);                                \
568                                                                       \
569   ehdr = elf##BITS##_newehdr (elf_file->elf);                         \
570   if (!ehdr)                                                          \
571     {                                                                 \
572       if (BITS == 32)                                                 \
573         fatal_error ("elf32_newehdr() failed: %s", elf_errmsg (-1));  \
574       else                                                            \
575         fatal_error ("elf64_newehdr() failed: %s", elf_errmsg (-1));  \
576     }                                                                 \
577                                                                       \
578   memcpy (ehdr->e_ident, cached_file_attrs.elf_ident,                 \
579           sizeof cached_file_attrs.elf_ident);                        \
580   ehdr->e_type = ET_REL;                                              \
581   ehdr->e_version = EV_CURRENT;                                       \
582   ehdr->e_machine = cached_file_attrs.elf_machine;                    \
583 }
584
585 DEFINE_INIT_EHDR (32)
586 DEFINE_INIT_EHDR (64)
587
588
589 /* Initialize ELF_FILE's executable header using cached data from previously
590    read files.  */
591
592 static void
593 init_ehdr (lto_elf_file *elf_file)
594 {
595   switch (cached_file_attrs.bits)
596     {
597     case 32:
598       init_ehdr32 (elf_file);
599       break;
600
601     case 64:
602       init_ehdr64 (elf_file);
603       break;
604
605     default:
606       gcc_unreachable ();
607     }
608 }
609
610 /* Open ELF file FILENAME.  If WRITABLE is true, the file is opened for write
611    and, if necessary, created.  Otherwise, the file is opened for reading.
612    Returns the opened file.  */
613
614 lto_file *
615 lto_obj_file_open (const char *filename, bool writable)
616 {
617   lto_elf_file *elf_file;
618   lto_file *result = NULL;
619   off_t offset;
620   long loffset;
621   off_t header_offset;
622   const char *offset_p;
623   char *fname;
624   int consumed;
625
626   offset_p = strrchr (filename, '@');
627   if (offset_p
628       && offset_p != filename
629       && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
630       && strlen (offset_p) == (unsigned int)consumed)
631     {
632       fname = (char *) xmalloc (offset_p - filename + 1);
633       memcpy (fname, filename, offset_p - filename);
634       fname[offset_p - filename] = '\0';
635       offset = (off_t)loffset;
636       /* elf_rand expects the offset to point to the ar header, not the
637          object itself. Subtract the size of the ar header (60 bytes).
638          We don't uses sizeof (struct ar_hd) to avoid including ar.h */
639       header_offset = offset - 60;
640     }
641   else
642     {
643       fname = xstrdup (filename);
644       offset = 0;
645       header_offset = 0;
646     }
647
648   /* Set up.  */
649   elf_file = XCNEW (lto_elf_file);
650   result = (lto_file *) elf_file;
651   lto_file_init (result, fname, offset);
652   elf_file->fd = -1;
653
654   /* Open the file.  */
655   elf_file->fd = open (fname, writable ? O_WRONLY|O_CREAT|O_BINARY 
656                                        : O_RDONLY|O_BINARY, 0666);
657   if (elf_file->fd == -1)
658     {
659       error ("could not open file %s", fname);
660       goto fail;
661     }
662
663   /* Initialize the ELF library.  */
664   if (elf_version (EV_CURRENT) == EV_NONE)
665     {
666       error ("ELF library is older than that used when building GCC");
667       goto fail;
668     }
669
670   /* Open the ELF file descriptor.  */
671   elf_file->elf = elf_begin (elf_file->fd, writable ? ELF_C_WRITE : ELF_C_READ,
672                              NULL);
673   if (!elf_file->elf)
674     {
675       error ("could not open ELF file: %s", elf_errmsg (0));
676       goto fail;
677     }
678
679   if (offset != 0)
680     {
681       Elf *e;
682       off_t t = elf_rand (elf_file->elf, header_offset);
683       if (t != header_offset)
684         {
685           error ("could not seek in archive");
686           goto fail;
687         }
688
689       e = elf_begin (elf_file->fd, ELF_C_READ, elf_file->elf);
690       if (e == NULL)
691         {
692           error("could not find archive member");
693           goto fail;
694         }
695       elf_end (elf_file->elf);
696       elf_file->elf = e;
697     }
698
699   if (writable)
700     {
701       init_ehdr (elf_file);
702       elf_file->shstrtab_stream = XCNEW (struct lto_output_stream);
703       /* Output an empty string to the section header table.  This becomes the
704          name of the initial NULL section.  */
705       lto_output_1_stream (elf_file->shstrtab_stream, '\0');
706     }
707   else
708     if (!validate_file (elf_file))
709       goto fail;
710
711   return result;
712
713  fail:
714   if (result)
715     lto_obj_file_close (result);
716   return NULL;
717 }
718
719
720 /* Close ELF file FILE and clean up any associated data structures.  If FILE
721    was opened for writing, the file's ELF data is written at this time, and
722    any cached data buffers are freed.  */
723
724 void
725 lto_obj_file_close (lto_file *file)
726 {
727   lto_elf_file *elf_file = (lto_elf_file *) file;
728   struct lto_char_ptr_base *cur, *tmp;
729
730   /* Write the ELF section header string table.  */
731   if (elf_file->shstrtab_stream)
732     {
733       size_t strtab;
734       GElf_Ehdr *ehdr_p, ehdr_buf;
735       lto_file *old_file = lto_set_current_out_file (file);
736
737       lto_elf_begin_section_with_type (".shstrtab", SHT_STRTAB);
738       ehdr_p = gelf_getehdr (elf_file->elf, &ehdr_buf);
739       if (ehdr_p == NULL)
740         fatal_error ("gelf_getehdr() failed: %s", elf_errmsg (-1));
741       strtab = elf_ndxscn (elf_file->scn);
742       if (strtab < SHN_LORESERVE)
743         ehdr_p->e_shstrndx = strtab;
744       else
745         {
746           GElf_Shdr *shdr_p, shdr_buf;
747           Elf_Scn *scn_p = elf_getscn (elf_file->elf, 0);
748           if (scn_p == NULL)
749             fatal_error ("elf_getscn() failed: %s", elf_errmsg (-1));
750           shdr_p = gelf_getshdr (scn_p, &shdr_buf);
751           if (shdr_p == NULL)
752             fatal_error ("gelf_getshdr() failed: %s", elf_errmsg (-1));
753           shdr_p->sh_link = strtab;
754           if (gelf_update_shdr (scn_p, shdr_p) == 0)
755             fatal_error ("gelf_update_shdr() failed: %s", elf_errmsg (-1));
756           ehdr_p->e_shstrndx = SHN_XINDEX;
757         }
758       if (gelf_update_ehdr (elf_file->elf, ehdr_p) == 0)
759         fatal_error ("gelf_update_ehdr() failed: %s", elf_errmsg (-1));
760       lto_write_stream (elf_file->shstrtab_stream);
761       lto_obj_end_section ();
762
763       lto_set_current_out_file (old_file);
764       free (elf_file->shstrtab_stream);
765
766       if (elf_update (elf_file->elf, ELF_C_WRITE) < 0)
767         fatal_error ("elf_update() failed: %s", elf_errmsg (-1));
768     }
769
770   if (elf_file->elf)
771     elf_end (elf_file->elf);
772   if (elf_file->fd != -1)
773     close (elf_file->fd);
774
775   /* Free any ELF data buffers.  */
776   cur = elf_file->data;
777   while (cur)
778     {
779       tmp = cur;
780       cur = (struct lto_char_ptr_base *) cur->ptr;
781       free (tmp);
782     }
783
784   free (file);
785 }
786
787
788 /* The current output file.  */
789 static lto_file *current_out_file;
790
791
792 /* Sets the current output file to FILE.  Returns the old output file or
793    NULL.  */
794
795 lto_file *
796 lto_set_current_out_file (lto_file *file)
797 {
798   lto_file *old_file = current_out_file;
799   current_out_file = file;
800   return old_file;
801 }
802
803
804 /* Returns the current output file.  */
805
806 lto_file *
807 lto_get_current_out_file (void)
808 {
809   return current_out_file;
810 }