1 /* LTO routines to use object files.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "double-int.h"
35 #include "fold-const.h"
38 #include "hard-reg-set.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-expr.h"
47 #include "diagnostic-core.h"
50 #include "plugin-api.h"
53 #include "lto-streamer.h"
54 #include "lto-section-names.h"
55 #include "simple-object.h"
57 /* An LTO file wrapped around an simple_object. */
59 struct lto_simple_object
61 /* The base information. */
64 /* The system file descriptor. */
67 /* The simple_object if we are reading the file. */
68 simple_object_read
*sobj_r
;
70 /* The simple_object if we are writing the file. */
71 simple_object_write
*sobj_w
;
73 /* The currently active section. */
74 simple_object_write_section
*section
;
77 /* Saved simple_object attributes. FIXME: Once set, this is never
80 static simple_object_attributes
*saved_attributes
;
82 /* Initialize FILE, an LTO file object for FILENAME. */
85 lto_file_init (lto_file
*file
, const char *filename
, off_t offset
)
87 file
->filename
= filename
;
88 file
->offset
= offset
;
91 /* Open the file FILENAME. It WRITABLE is true, the file is opened
92 for write and, if necessary, created. Otherwise, the file is
93 opened for reading. Returns the opened file. */
96 lto_obj_file_open (const char *filename
, bool writable
)
103 struct lto_simple_object
*lo
;
107 offset_p
= strrchr (filename
, '@');
109 && offset_p
!= filename
110 && sscanf (offset_p
, "@%li%n", &loffset
, &consumed
) >= 1
111 && strlen (offset_p
) == (unsigned int) consumed
)
113 fname
= XNEWVEC (char, offset_p
- filename
+ 1);
114 memcpy (fname
, filename
, offset_p
- filename
);
115 fname
[offset_p
- filename
] = '\0';
116 offset
= (off_t
) loffset
;
120 fname
= xstrdup (filename
);
124 lo
= XCNEW (struct lto_simple_object
);
125 lto_file_init ((lto_file
*) lo
, fname
, offset
);
127 lo
->fd
= open (fname
,
129 ? O_WRONLY
| O_CREAT
| O_BINARY
130 : O_RDONLY
| O_BINARY
),
134 error ("open %s failed: %s", fname
, xstrerror (errno
));
140 simple_object_attributes
*attrs
;
142 lo
->sobj_r
= simple_object_start_read (lo
->fd
, offset
, LTO_SEGMENT_NAME
,
144 if (lo
->sobj_r
== NULL
)
147 attrs
= simple_object_fetch_attributes (lo
->sobj_r
, &errmsg
, &err
);
151 if (saved_attributes
== NULL
)
152 saved_attributes
= attrs
;
155 errmsg
= simple_object_attributes_merge (saved_attributes
, attrs
,
166 gcc_assert (saved_attributes
!= NULL
);
167 lo
->sobj_w
= simple_object_start_write (saved_attributes
,
170 if (lo
->sobj_w
== NULL
)
178 error ("%s: %s", fname
, errmsg
);
180 error ("%s: %s: %s", fname
, errmsg
, xstrerror (err
));
184 lto_obj_file_close ((lto_file
*) lo
);
190 /* Close FILE. If FILE was opened for writing, it is written out
194 lto_obj_file_close (lto_file
*file
)
196 struct lto_simple_object
*lo
= (struct lto_simple_object
*) file
;
198 if (lo
->sobj_r
!= NULL
)
199 simple_object_release_read (lo
->sobj_r
);
200 else if (lo
->sobj_w
!= NULL
)
205 gcc_assert (lo
->base
.offset
== 0);
207 errmsg
= simple_object_write_to_file (lo
->sobj_w
, lo
->fd
, &err
);
211 fatal_error (input_location
, "%s", errmsg
);
213 fatal_error (input_location
, "%s: %s", errmsg
, xstrerror (err
));
216 simple_object_release_write (lo
->sobj_w
);
221 if (close (lo
->fd
) < 0)
222 fatal_error (input_location
, "close: %s", xstrerror (errno
));
226 /* This is passed to lto_obj_add_section. */
228 struct lto_obj_add_section_data
230 /* The hash table of sections. */
231 htab_t section_hash_table
;
232 /* The offset of this file. */
234 /* List in linker order */
235 struct lto_section_list
*list
;
238 /* This is called for each section in the file. */
241 lto_obj_add_section (void *data
, const char *name
, off_t offset
,
244 struct lto_obj_add_section_data
*loasd
=
245 (struct lto_obj_add_section_data
*) data
;
246 htab_t section_hash_table
= (htab_t
) loasd
->section_hash_table
;
248 struct lto_section_slot s_slot
;
250 struct lto_section_list
*list
= loasd
->list
;
252 if (strncmp (name
, section_name_prefix
, strlen (section_name_prefix
)))
255 new_name
= xstrdup (name
);
256 s_slot
.name
= new_name
;
257 slot
= htab_find_slot (section_hash_table
, &s_slot
, INSERT
);
260 struct lto_section_slot
*new_slot
= XCNEW (struct lto_section_slot
);
262 new_slot
->name
= new_name
;
263 new_slot
->start
= loasd
->base_offset
+ offset
;
264 new_slot
->len
= length
;
270 list
->first
= new_slot
;
272 list
->last
->next
= new_slot
;
273 list
->last
= new_slot
;
278 error ("two or more sections for %s", new_name
);
285 /* Build a hash table whose key is the section name and whose data is
286 the start and size of each section in the .o file. */
289 lto_obj_build_section_table (lto_file
*lto_file
, struct lto_section_list
*list
)
291 struct lto_simple_object
*lo
= (struct lto_simple_object
*) lto_file
;
292 htab_t section_hash_table
;
293 struct lto_obj_add_section_data loasd
;
297 section_hash_table
= lto_obj_create_section_hash_table ();
299 gcc_assert (lo
->sobj_r
!= NULL
&& lo
->sobj_w
== NULL
);
300 loasd
.section_hash_table
= section_hash_table
;
301 loasd
.base_offset
= lo
->base
.offset
;
303 errmsg
= simple_object_find_sections (lo
->sobj_r
, lto_obj_add_section
,
308 error ("%s", errmsg
);
310 error ("%s: %s", errmsg
, xstrerror (err
));
311 htab_delete (section_hash_table
);
315 return section_hash_table
;
318 /* The current output file. */
320 static lto_file
*current_out_file
;
322 /* Set the current output file. Return the old one. */
325 lto_set_current_out_file (lto_file
*file
)
329 old_file
= current_out_file
;
330 current_out_file
= file
;
334 /* Return the current output file. */
337 lto_get_current_out_file (void)
339 return current_out_file
;
342 /* Begin writing a new section named NAME in the current output
346 lto_obj_begin_section (const char *name
)
348 struct lto_simple_object
*lo
;
353 lo
= (struct lto_simple_object
*) current_out_file
;
354 gcc_assert (lo
!= NULL
355 && lo
->sobj_r
== NULL
356 && lo
->sobj_w
!= NULL
357 && lo
->section
== NULL
);
359 align
= ceil_log2 (POINTER_SIZE_UNITS
);
360 lo
->section
= simple_object_write_create_section (lo
->sobj_w
, name
, align
,
362 if (lo
->section
== NULL
)
365 fatal_error (input_location
, "%s", errmsg
);
367 fatal_error (input_location
, "%s: %s", errmsg
, xstrerror (errno
));
371 /* Add data to a section. BLOCK is a pointer to memory containing
375 lto_obj_append_data (const void *data
, size_t len
, void *)
377 struct lto_simple_object
*lo
;
381 lo
= (struct lto_simple_object
*) current_out_file
;
382 gcc_assert (lo
!= NULL
&& lo
->section
!= NULL
);
384 errmsg
= simple_object_write_add_data (lo
->sobj_w
, lo
->section
, data
, len
,
389 fatal_error (input_location
, "%s", errmsg
);
391 fatal_error (input_location
, "%s: %s", errmsg
, xstrerror (errno
));
395 /* Stop writing to the current output section. */
398 lto_obj_end_section (void)
400 struct lto_simple_object
*lo
;
402 lo
= (struct lto_simple_object
*) current_out_file
;
403 gcc_assert (lo
!= NULL
&& lo
->section
!= NULL
);