aboutsummaryrefslogtreecommitdiff
path: root/gcc/lto/lto-elf.c
blob: ad496217edea753394475d33ea1c35768f86192d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* LTO routines for ELF object files.
   Copyright 2009, 2010 Free Software Foundation, Inc.
   Contributed by CodeSourcery, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "diagnostic-core.h"
#include "toplev.h"
#include <gelf.h>
#include "lto.h"
#include "tm.h"
#include "libiberty.h"
#include "ggc.h"
#include "lto-streamer.h"

/* Cater to hosts with half-backed <elf.h> file like HP-UX.  */
#ifndef EM_SPARC
# define EM_SPARC 2
#endif

#ifndef EM_SPARC32PLUS
# define EM_SPARC32PLUS 18
#endif

#ifndef ELFOSABI_NONE
# define ELFOSABI_NONE 0
#endif
#ifndef ELFOSABI_LINUX
# define ELFOSABI_LINUX 3
#endif


/* Handle opening elf files on hosts, such as Windows, that may use 
   text file handling that will break binary access.  */
#ifndef O_BINARY
# define O_BINARY 0
#endif


/* Initialize FILE, an LTO file object for FILENAME.  */
static void
lto_file_init (lto_file *file, const char *filename, off_t offset)
{
  file->filename = filename;
  file->offset = offset;
}

/* An ELF file.  */
struct lto_elf_file 
{
  /* The base information.  */
  lto_file base;

  /* The system file descriptor for the file.  */
  int fd;

  /* The libelf descriptor for the file.  */
  Elf *elf;

  /* Section number of string table used for section names.  */
  size_t sec_strtab;

  /* Writable file members.  */

  /* The currently active section.  */
  Elf_Scn *scn;

  /* The output stream for section header names.  */
  struct lto_output_stream *shstrtab_stream;

  /* Linked list of data which must be freed *after* the file has been
     closed.  This is an annoying limitation of libelf.  */
  struct lto_char_ptr_base *data;
};
typedef struct lto_elf_file lto_elf_file;

/* Stores executable header attributes which must be shared by all ELF files.
   This is used for validating input files and populating output files.  */
static struct {
  bool initialized;
  /* 32 or 64 bits?  */
  size_t bits;
  unsigned char elf_ident[EI_NIDENT];
  Elf64_Half elf_machine;
} cached_file_attrs;


/* Return the section header for SECTION.  The return value is never
   NULL.  Call lto_elf_free_shdr to release the memory allocated.  */

static Elf64_Shdr *
lto_elf_get_shdr (Elf_Scn *section)
{
  Elf64_Shdr *shdr;

  switch (cached_file_attrs.bits)
    {
    case 32:
      {
	Elf32_Shdr *shdr32;

	/* Read the 32-bit section header.  */
	shdr32 = elf32_getshdr (section);
	if (!shdr32)
	  fatal_error ("could not read section header: %s", elf_errmsg (0));

	/* Transform it into a 64-bit section header.  */
	shdr = XNEW (Elf64_Shdr);
	shdr->sh_name = shdr32->sh_name;
	shdr->sh_type = shdr32->sh_type;
	shdr->sh_flags = shdr32->sh_flags;
	shdr->sh_addr = shdr32->sh_addr;
	shdr->sh_offset = shdr32->sh_offset;
	shdr->sh_size = shdr32->sh_size;
	shdr->sh_link = shdr32->sh_link;
	shdr->sh_info = shdr32->sh_info;
	shdr->sh_addralign = shdr32->sh_addralign;
	shdr->sh_entsize  = shdr32->sh_entsize;
	break;
      }
      break;

    case 64:
      shdr = elf64_getshdr (section);
      if (!shdr)
	fatal_error ("could not read section header: %s", elf_errmsg (0));
      break;

    default:
      gcc_unreachable ();
    }

  return shdr;
}

/* Free SHDR, previously allocated by lto_elf_get_shdr.  */
static void
lto_elf_free_shdr (Elf64_Shdr *shdr)
{
  if (cached_file_attrs.bits != 64)
    free (shdr);
}

/* Build a hash table whose key is the section names and whose data is
   the start and size of each section in the .o file.  */

htab_t
lto_obj_build_section_table (lto_file *lto_file) 
{
  lto_elf_file *elf_file = (lto_elf_file *)lto_file;
  htab_t section_hash_table;
  Elf_Scn *section;
  size_t base_offset;

  section_hash_table = lto_obj_create_section_hash_table ();

  base_offset = elf_getbase (elf_file->elf);
  /* We are reasonably sure that elf_getbase does not fail at this
     point.  So assume that we run into the incompatibility with
     the FreeBSD libelf implementation that has a non-working
     elf_getbase for non-archive members in which case the offset
     should be zero.  */
  if (base_offset == (size_t)-1)
    base_offset = 0;
  for (section = elf_getscn (elf_file->elf, 0);
       section;
       section = elf_nextscn (elf_file->elf, section)) 
    {
      Elf64_Shdr *shdr;
      const char *name;
      size_t offset;
      char *new_name;
      void **slot;
      struct lto_section_slot s_slot;

      /* Get the name of this section.  */
      shdr = lto_elf_get_shdr (section);
      offset = shdr->sh_name;
      name = elf_strptr (elf_file->elf, 
			 elf_file->sec_strtab,
			 offset);

      /* Only put lto stuff into the symtab.  */
      if (strncmp (name, LTO_SECTION_NAME_PREFIX, 
		   strlen (LTO_SECTION_NAME_PREFIX)) != 0)
	{
	  lto_elf_free_shdr (shdr);
	  continue;
	}

      new_name = XNEWVEC (char, strlen (name) + 1);
      strcpy (new_name, name);
      s_slot.name = new_name;
      slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
      if (*slot == NULL)
	{
	  struct lto_section_slot *new_slot = XNEW (struct lto_section_slot);

	  new_slot->name = new_name;
	  /* The offset into the file for this section.  */
	  new_slot->start = base_offset + shdr->sh_offset;
	  new_slot->len = shdr->sh_size;
	  *slot = new_slot;
	}
      else
	{
	  error ("two or more sections for %s:", new_name);
	  return NULL;
	}

      lto_elf_free_shdr (shdr);
    }

  return section_hash_table;
}


/* Initialize the section header of section SCN.  SH_NAME is the section name
   as an index into the section header string table.  SH_TYPE is the section
   type, an SHT_* macro from libelf headers.  */

#define DEFINE_INIT_SHDR(BITS)					      \
static void							      \
init_shdr##BITS (Elf_Scn *scn, size_t sh_name, size_t sh_type)	      \
{								      \
  Elf##BITS##_Shdr *shdr;					      \
								      \
  shdr = elf##BITS##_getshdr (scn);				      \
  if (!shdr)							      \
    {								      \
      if (BITS == 32)						      \
	fatal_error ("elf32_getshdr() failed: %s", elf_errmsg (-1));  \
      else							      \
	fatal_error ("elf64_getshdr() failed: %s", elf_errmsg (-1));  \
    }								      \
								      \
  shdr->sh_name = sh_name;					      \
  shdr->sh_type = sh_type;					      \
  shdr->sh_addralign = POINTER_SIZE / BITS_PER_UNIT;		      \
  shdr->sh_flags = 0;						      \
  shdr->sh_entsize = 0;						      \
}

DEFINE_INIT_SHDR (32)
DEFINE_INIT_SHDR (64)

static bool first_data_block;

/* Begin a new ELF section named NAME with type TYPE in the current output
   file.  TYPE is an SHT_* macro from the libelf headers.  */

static void
lto_elf_begin_section_with_type (const char *name, size_t type)
{
  lto_elf_file *file;
  Elf_Scn *scn;
  size_t sh_name;

  /* Grab the current output file and do some basic assertion checking.  */
  file = (lto_elf_file *) lto_get_current_out_file (),
  gcc_assert (file);
  gcc_assert (file->elf);
  gcc_assert (!file->scn);

  /* Create a new section.  */
  scn = elf_newscn (file->elf);
  if (!scn)
    fatal_error ("could not create a new ELF section: %s", elf_errmsg (-1));
  file->scn = scn;

  /* Add a string table entry and record the offset.  */
  gcc_assert (file->shstrtab_stream);
  sh_name = file->shstrtab_stream->total_size;
  lto_output_data_stream (file->shstrtab_stream, name, strlen (name) + 1);

  /* Initialize the section header.  */
  switch (cached_file_attrs.bits)
    {
    case 32:
      init_shdr32 (scn, sh_name, type);
      break;

    case 64:
      init_shdr64 (scn, sh_name, type);
      break;

    default:
      gcc_unreachable ();
    }

  first_data_block = true;
}


/* Begin a new ELF section named NAME in the current output file.  */

void
lto_obj_begin_section (const char *name)
{
  lto_elf_begin_section_with_type (name, SHT_PROGBITS);
}


/* Append DATA of length LEN to the current output section.  BASE is a pointer
   to the output page containing DATA.  It is freed once the output file has
   been written.  */

void
lto_obj_append_data (const void *data, size_t len, void *block)
{
  lto_elf_file *file;
  Elf_Data *elf_data;
  struct lto_char_ptr_base *base = (struct lto_char_ptr_base *) block;

  /* Grab the current output file and do some basic assertion checking.  */
  file = (lto_elf_file *) lto_get_current_out_file ();
  gcc_assert (file);
  gcc_assert (file->scn);

  elf_data = elf_newdata (file->scn);
  if (!elf_data)
    fatal_error ("could not append data to ELF section: %s", elf_errmsg (-1));

  if (first_data_block)
    {
      elf_data->d_align = POINTER_SIZE / BITS_PER_UNIT;
      first_data_block = false;
    }
  else
    elf_data->d_align = 1;
  elf_data->d_buf = CONST_CAST (void *, data);
  elf_data->d_off = 0LL;
  elf_data->d_size = len;
  elf_data->d_type = ELF_T_BYTE;
  elf_data->d_version = EV_CURRENT;

  base->ptr = (char *)file->data;
  file->data = base;
}


/* End the current output section.  This just does some assertion checking
   and sets the current output file's scn member to NULL.  */

void
lto_obj_end_section (void)
{
  lto_elf_file *file;

  /* Grab the current output file and validate some basic assertions.  */
  file = (lto_elf_file *) lto_get_current_out_file ();
  gcc_assert (file);
  gcc_assert (file->scn);

  file->scn = NULL;
}


/* Return true if ELF_MACHINE is compatible with the cached value of the
   architecture and possibly update the latter.  Return false otherwise.

   Note: if you want to add more EM_* cases, you'll need to provide the
   corresponding definitions at the beginning of the file.  */

static bool
is_compatible_architecture (Elf64_Half elf_machine)
{
  if (cached_file_attrs.elf_machine == elf_machine)
    return true;

  switch (cached_file_attrs.elf_machine)
    {
    case EM_SPARC:
      if (elf_machine == EM_SPARC32PLUS)
	{
	  cached_file_attrs.elf_machine = elf_machine;
	  return true;
	}
      break;

    case EM_SPARC32PLUS:
      if (elf_machine == EM_SPARC)
	return true;
      break;

    default:
      break;
    }

  return false;
}


/* Validate's ELF_FILE's executable header and, if cached_file_attrs is
   uninitialized, caches the architecture.  */

#define DEFINE_VALIDATE_EHDR(BITS)				\
static bool							\
validate_ehdr##BITS (lto_elf_file *elf_file)			\
{								\
  Elf##BITS##_Ehdr *elf_header;					\
								\
  elf_header = elf##BITS##_getehdr (elf_file->elf);		\
  if (!elf_header)						\
    {								\
      error ("could not read ELF header: %s", elf_errmsg (0));	\
      return false;						\
    }								\
								\
  if (elf_header->e_type != ET_REL)				\
    {								\
      error ("not a relocatable ELF object file");		\
      return false;						\
    }								\
								\
  if (!cached_file_attrs.initialized)				\
    cached_file_attrs.elf_machine = elf_header->e_machine;	\
  else if (!is_compatible_architecture (elf_header->e_machine))	\
    {								\
      error ("inconsistent file architecture detected");	\
      return false;						\
    }								\
								\
  return true;							\
}

DEFINE_VALIDATE_EHDR (32)
DEFINE_VALIDATE_EHDR (64)


#ifndef HAVE_ELF_GETSHDRSTRNDX
/* elf_getshdrstrndx replacement for systems that lack it, but provide
   either the gABI conformant or Solaris 2 variant of elf_getshstrndx
   instead.  */

static int
elf_getshdrstrndx (Elf *elf, size_t *dst)
{
#ifdef HAVE_ELF_GETSHSTRNDX_GABI
  return elf_getshstrndx (elf, dst);
#else
  return elf_getshstrndx (elf, dst) ? 0 : -1;
#endif
}
#endif

/* Validate's ELF_FILE's executable header and, if cached_file_attrs is
   uninitialized, caches the results.  Also records the section header string
   table's section index.  Returns true on success or false on failure.  */

static bool
validate_file (lto_elf_file *elf_file)
{
  const char *elf_ident;

  /* Some aspects of the libelf API are dependent on whether the
     object file is a 32-bit or 64-bit file.  Determine which kind of
     file this is now.  */
  elf_ident = elf_getident (elf_file->elf, NULL);
  if (!elf_ident)
    {
      error ("could not read ELF identification information: %s",
	      elf_errmsg (0));
      return false;
	     
    }

  if (!cached_file_attrs.initialized)
    {
      switch (elf_ident[EI_CLASS])
	{
	case ELFCLASS32:
	  cached_file_attrs.bits = 32;
	  break;

	case ELFCLASS64:
	  cached_file_attrs.bits = 64;
	  break;

	default:
	  error ("unsupported ELF file class");
	  return false;
	}

      memcpy (cached_file_attrs.elf_ident, elf_ident,
	      sizeof cached_file_attrs.elf_ident);
    }
  else
    {
      char elf_ident_buf[EI_NIDENT];

      memcpy (elf_ident_buf, elf_ident, sizeof elf_ident_buf);

      if (elf_ident_buf[EI_OSABI] != cached_file_attrs.elf_ident[EI_OSABI])
	{
	  /* Allow mixing ELFOSABI_NONE with ELFOSABI_LINUX, with the result
	     ELFOSABI_LINUX.  */
	  if (elf_ident_buf[EI_OSABI] == ELFOSABI_NONE
	      && cached_file_attrs.elf_ident[EI_OSABI] == ELFOSABI_LINUX)
	    elf_ident_buf[EI_OSABI] = cached_file_attrs.elf_ident[EI_OSABI];
	  else if (elf_ident_buf[EI_OSABI] == ELFOSABI_LINUX
		   && cached_file_attrs.elf_ident[EI_OSABI] == ELFOSABI_NONE)
	    cached_file_attrs.elf_ident[EI_OSABI] = elf_ident_buf[EI_OSABI];
	}

      if (memcmp (elf_ident_buf, cached_file_attrs.elf_ident,
		  sizeof cached_file_attrs.elf_ident))
	return false;
    }

  /* Check that the input file is a relocatable object file with the correct
     architecture.  */
  switch (cached_file_attrs.bits)
    {
    case 32:
      if (!validate_ehdr32 (elf_file))
	return false;
      break;

    case 64:
      if (!validate_ehdr64 (elf_file))
	return false;
      break;

    default:
      gcc_unreachable ();
    }

  /* Read the string table used for section header names.  */
  if (elf_getshdrstrndx (elf_file->elf, &elf_file->sec_strtab) == -1)
    {
      error ("could not locate ELF string table: %s", elf_errmsg (0));
      return false;
    }

  cached_file_attrs.initialized = true;
  return true;
}


/* Helper functions used by init_ehdr.  Initialize ELF_FILE's executable
   header using cached data from previously read files.  */

#define DEFINE_INIT_EHDR(BITS)					      \
static void							      \
init_ehdr##BITS (lto_elf_file *elf_file)			      \
{								      \
  Elf##BITS##_Ehdr *ehdr;					      \
								      \
  gcc_assert (cached_file_attrs.bits);				      \
								      \
  ehdr = elf##BITS##_newehdr (elf_file->elf);			      \
  if (!ehdr)							      \
    {								      \
      if (BITS == 32)						      \
	fatal_error ("elf32_newehdr() failed: %s", elf_errmsg (-1));  \
      else							      \
	fatal_error ("elf64_newehdr() failed: %s", elf_errmsg (-1));  \
    }								      \
								      \
  memcpy (ehdr->e_ident, cached_file_attrs.elf_ident,		      \
	  sizeof cached_file_attrs.elf_ident);			      \
  ehdr->e_type = ET_REL;					      \
  ehdr->e_version = EV_CURRENT;					      \
  ehdr->e_machine = cached_file_attrs.elf_machine;		      \
}

DEFINE_INIT_EHDR (32)
DEFINE_INIT_EHDR (64)


/* Initialize ELF_FILE's executable header using cached data from previously
   read files.  */

static void
init_ehdr (lto_elf_file *elf_file)
{
  switch (cached_file_attrs.bits)
    {
    case 32:
      init_ehdr32 (elf_file);
      break;

    case 64:
      init_ehdr64 (elf_file);
      break;

    default:
      gcc_unreachable ();
    }
}

/* Open ELF file FILENAME.  If WRITABLE is true, the file is opened for write
   and, if necessary, created.  Otherwise, the file is opened for reading.
   Returns the opened file.  */

lto_file *
lto_obj_file_open (const char *filename, bool writable)
{
  lto_elf_file *elf_file;
  lto_file *result = NULL;
  off_t offset;
  long loffset;
  off_t header_offset;
  const char *offset_p;
  char *fname;
  int consumed;

  offset_p = strrchr (filename, '@');
  if (offset_p
      && offset_p != filename
      && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
      && strlen (offset_p) == (unsigned int)consumed)
    {
      fname = (char *) xmalloc (offset_p - filename + 1);
      memcpy (fname, filename, offset_p - filename);
      fname[offset_p - filename] = '\0';
      offset = (off_t)loffset;
      /* elf_rand expects the offset to point to the ar header, not the
         object itself. Subtract the size of the ar header (60 bytes).
         We don't uses sizeof (struct ar_hd) to avoid including ar.h */
      header_offset = offset - 60;
    }
  else
    {
      fname = xstrdup (filename);
      offset = 0;
      header_offset = 0;
    }

  /* Set up.  */
  elf_file = XCNEW (lto_elf_file);
  result = (lto_file *) elf_file;
  lto_file_init (result, fname, offset);
  elf_file->fd = -1;

  /* Open the file.  */
  elf_file->fd = open (fname, writable ? O_WRONLY|O_CREAT|O_BINARY 
				       : O_RDONLY|O_BINARY, 0666);
  if (elf_file->fd == -1)
    {
      error ("could not open file %s", fname);
      goto fail;
    }

  /* Initialize the ELF library.  */
  if (elf_version (EV_CURRENT) == EV_NONE)
    {
      error ("ELF library is older than that used when building GCC");
      goto fail;
    }

  /* Open the ELF file descriptor.  */
  elf_file->elf = elf_begin (elf_file->fd, writable ? ELF_C_WRITE : ELF_C_READ,
			     NULL);
  if (!elf_file->elf)
    {
      error ("could not open ELF file: %s", elf_errmsg (0));
      goto fail;
    }

  if (offset != 0)
    {
      Elf *e;
      off_t t = elf_rand (elf_file->elf, header_offset);
      if (t != header_offset)
        {
          error ("could not seek in archive");
          goto fail;
        }

      e = elf_begin (elf_file->fd, ELF_C_READ, elf_file->elf);
      if (e == NULL)
        {
          error("could not find archive member");
          goto fail;
        }
      elf_end (elf_file->elf);
      elf_file->elf = e;
    }

  if (writable)
    {
      init_ehdr (elf_file);
      elf_file->shstrtab_stream = XCNEW (struct lto_output_stream);
      /* Output an empty string to the section header table.  This becomes the
	 name of the initial NULL section.  */
      lto_output_1_stream (elf_file->shstrtab_stream, '\0');
    }
  else
    if (!validate_file (elf_file))
      goto fail;

  return result;

 fail:
  if (result)
    lto_obj_file_close (result);
  return NULL;
}


/* Close ELF file FILE and clean up any associated data structures.  If FILE
   was opened for writing, the file's ELF data is written at this time, and
   any cached data buffers are freed.  */

void
lto_obj_file_close (lto_file *file)
{
  lto_elf_file *elf_file = (lto_elf_file *) file;
  struct lto_char_ptr_base *cur, *tmp;

  /* Write the ELF section header string table.  */
  if (elf_file->shstrtab_stream)
    {
      size_t strtab;
      GElf_Ehdr *ehdr_p, ehdr_buf;
      lto_file *old_file = lto_set_current_out_file (file);

      lto_elf_begin_section_with_type (".shstrtab", SHT_STRTAB);
      ehdr_p = gelf_getehdr (elf_file->elf, &ehdr_buf);
      if (ehdr_p == NULL)
	fatal_error ("gelf_getehdr() failed: %s", elf_errmsg (-1));
      strtab = elf_ndxscn (elf_file->scn);
      if (strtab < SHN_LORESERVE)
	ehdr_p->e_shstrndx = strtab;
      else
	{
	  GElf_Shdr *shdr_p, shdr_buf;
	  Elf_Scn *scn_p = elf_getscn (elf_file->elf, 0);
	  if (scn_p == NULL)
	    fatal_error ("elf_getscn() failed: %s", elf_errmsg (-1));
	  shdr_p = gelf_getshdr (scn_p, &shdr_buf);
	  if (shdr_p == NULL)
	    fatal_error ("gelf_getshdr() failed: %s", elf_errmsg (-1));
	  shdr_p->sh_link = strtab;
	  if (gelf_update_shdr (scn_p, shdr_p) == 0)
	    fatal_error ("gelf_update_shdr() failed: %s", elf_errmsg (-1));
	  ehdr_p->e_shstrndx = SHN_XINDEX;
	}
      if (gelf_update_ehdr (elf_file->elf, ehdr_p) == 0)
	fatal_error ("gelf_update_ehdr() failed: %s", elf_errmsg (-1));
      lto_write_stream (elf_file->shstrtab_stream);
      lto_obj_end_section ();

      lto_set_current_out_file (old_file);
      free (elf_file->shstrtab_stream);

      if (elf_update (elf_file->elf, ELF_C_WRITE) < 0)
	fatal_error ("elf_update() failed: %s", elf_errmsg (-1));
    }

  if (elf_file->elf)
    elf_end (elf_file->elf);
  if (elf_file->fd != -1)
    close (elf_file->fd);

  /* Free any ELF data buffers.  */
  cur = elf_file->data;
  while (cur)
    {
      tmp = cur;
      cur = (struct lto_char_ptr_base *) cur->ptr;
      free (tmp);
    }

  free (file);
}


/* The current output file.  */
static lto_file *current_out_file;


/* Sets the current output file to FILE.  Returns the old output file or
   NULL.  */

lto_file *
lto_set_current_out_file (lto_file *file)
{
  lto_file *old_file = current_out_file;
  current_out_file = file;
  return old_file;
}


/* Returns the current output file.  */

lto_file *
lto_get_current_out_file (void)
{
  return current_out_file;
}