OSDN Git Service

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