OSDN Git Service

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