PR rtl-optimization/57003
[official-gcc.git] / gcc / lto / lto-object.c
blob323f7b2a74eb3c323ee9f3783b21c7cf9ae63df9
1 /* LTO routines to use object files.
2 Copyright (C) 2010-2014 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
10 version.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "basic-block.h"
26 #include "tree-ssa-alias.h"
27 #include "internal-fn.h"
28 #include "gimple-expr.h"
29 #include "is-a.h"
30 #include "gimple.h"
31 #include "diagnostic-core.h"
32 #include "lto.h"
33 #include "tm.h"
34 #include "lto-streamer.h"
35 #include "lto-section-names.h"
36 #include "simple-object.h"
38 /* An LTO file wrapped around an simple_object. */
40 struct lto_simple_object
42 /* The base information. */
43 lto_file base;
45 /* The system file descriptor. */
46 int fd;
48 /* The simple_object if we are reading the file. */
49 simple_object_read *sobj_r;
51 /* The simple_object if we are writing the file. */
52 simple_object_write *sobj_w;
54 /* The currently active section. */
55 simple_object_write_section *section;
58 /* Saved simple_object attributes. FIXME: Once set, this is never
59 cleared. */
61 static simple_object_attributes *saved_attributes;
63 /* Initialize FILE, an LTO file object for FILENAME. */
65 static void
66 lto_file_init (lto_file *file, const char *filename, off_t offset)
68 file->filename = filename;
69 file->offset = offset;
72 /* Open the file FILENAME. It WRITABLE is true, the file is opened
73 for write and, if necessary, created. Otherwise, the file is
74 opened for reading. Returns the opened file. */
76 lto_file *
77 lto_obj_file_open (const char *filename, bool writable)
79 const char *offset_p;
80 long loffset;
81 int consumed;
82 char *fname;
83 off_t offset;
84 struct lto_simple_object *lo;
85 const char *errmsg;
86 int err;
88 offset_p = strrchr (filename, '@');
89 if (offset_p != NULL
90 && offset_p != filename
91 && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
92 && strlen (offset_p) == (unsigned int) consumed)
94 fname = XNEWVEC (char, offset_p - filename + 1);
95 memcpy (fname, filename, offset_p - filename);
96 fname[offset_p - filename] = '\0';
97 offset = (off_t) loffset;
99 else
101 fname = xstrdup (filename);
102 offset = 0;
105 lo = XCNEW (struct lto_simple_object);
106 lto_file_init ((lto_file *) lo, fname, offset);
108 lo->fd = open (fname,
109 (writable
110 ? O_WRONLY | O_CREAT | O_BINARY
111 : O_RDONLY | O_BINARY),
112 0666);
113 if (lo->fd == -1)
115 error ("open %s failed: %s", fname, xstrerror (errno));
116 goto fail;
119 if (!writable)
121 simple_object_attributes *attrs;
123 lo->sobj_r = simple_object_start_read (lo->fd, offset, LTO_SEGMENT_NAME,
124 &errmsg, &err);
125 if (lo->sobj_r == NULL)
126 goto fail_errmsg;
128 attrs = simple_object_fetch_attributes (lo->sobj_r, &errmsg, &err);
129 if (attrs == NULL)
130 goto fail_errmsg;
132 if (saved_attributes == NULL)
133 saved_attributes = attrs;
134 else
136 errmsg = simple_object_attributes_merge (saved_attributes, attrs,
137 &err);
138 if (errmsg != NULL)
140 free (attrs);
141 goto fail_errmsg;
145 else
147 gcc_assert (saved_attributes != NULL);
148 lo->sobj_w = simple_object_start_write (saved_attributes,
149 LTO_SEGMENT_NAME,
150 &errmsg, &err);
151 if (lo->sobj_w == NULL)
152 goto fail_errmsg;
155 return &lo->base;
157 fail_errmsg:
158 if (err == 0)
159 error ("%s: %s", fname, errmsg);
160 else
161 error ("%s: %s: %s", fname, errmsg, xstrerror (err));
163 fail:
164 if (lo->fd != -1)
165 lto_obj_file_close ((lto_file *) lo);
166 free (lo);
167 return NULL;
171 /* Close FILE. If FILE was opened for writing, it is written out
172 now. */
174 void
175 lto_obj_file_close (lto_file *file)
177 struct lto_simple_object *lo = (struct lto_simple_object *) file;
179 if (lo->sobj_r != NULL)
180 simple_object_release_read (lo->sobj_r);
181 else if (lo->sobj_w != NULL)
183 const char *errmsg;
184 int err;
186 gcc_assert (lo->base.offset == 0);
188 errmsg = simple_object_write_to_file (lo->sobj_w, lo->fd, &err);
189 if (errmsg != NULL)
191 if (err == 0)
192 fatal_error ("%s", errmsg);
193 else
194 fatal_error ("%s: %s", errmsg, xstrerror (err));
197 simple_object_release_write (lo->sobj_w);
200 if (lo->fd != -1)
202 if (close (lo->fd) < 0)
203 fatal_error ("close: %s", xstrerror (errno));
207 /* This is passed to lto_obj_add_section. */
209 struct lto_obj_add_section_data
211 /* The hash table of sections. */
212 htab_t section_hash_table;
213 /* The offset of this file. */
214 off_t base_offset;
215 /* List in linker order */
216 struct lto_section_list *list;
219 /* This is called for each section in the file. */
221 static int
222 lto_obj_add_section (void *data, const char *name, off_t offset,
223 off_t length)
225 struct lto_obj_add_section_data *loasd =
226 (struct lto_obj_add_section_data *) data;
227 htab_t section_hash_table = (htab_t) loasd->section_hash_table;
228 char *new_name;
229 struct lto_section_slot s_slot;
230 void **slot;
231 struct lto_section_list *list = loasd->list;
233 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
234 strlen (LTO_SECTION_NAME_PREFIX)) != 0)
235 return 1;
237 new_name = xstrdup (name);
238 s_slot.name = new_name;
239 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
240 if (*slot == NULL)
242 struct lto_section_slot *new_slot = XCNEW (struct lto_section_slot);
244 new_slot->name = new_name;
245 new_slot->start = loasd->base_offset + offset;
246 new_slot->len = length;
247 *slot = new_slot;
249 if (list != NULL)
251 if (!list->first)
252 list->first = new_slot;
253 if (list->last)
254 list->last->next = new_slot;
255 list->last = new_slot;
258 else
260 error ("two or more sections for %s", new_name);
261 return 0;
264 return 1;
267 /* Build a hash table whose key is the section name and whose data is
268 the start and size of each section in the .o file. */
270 htab_t
271 lto_obj_build_section_table (lto_file *lto_file, struct lto_section_list *list)
273 struct lto_simple_object *lo = (struct lto_simple_object *) lto_file;
274 htab_t section_hash_table;
275 struct lto_obj_add_section_data loasd;
276 const char *errmsg;
277 int err;
279 section_hash_table = lto_obj_create_section_hash_table ();
281 gcc_assert (lo->sobj_r != NULL && lo->sobj_w == NULL);
282 loasd.section_hash_table = section_hash_table;
283 loasd.base_offset = lo->base.offset;
284 loasd.list = list;
285 errmsg = simple_object_find_sections (lo->sobj_r, lto_obj_add_section,
286 &loasd, &err);
287 if (errmsg != NULL)
289 if (err == 0)
290 error ("%s", errmsg);
291 else
292 error ("%s: %s", errmsg, xstrerror (err));
293 htab_delete (section_hash_table);
294 return NULL;
297 return section_hash_table;
300 /* The current output file. */
302 static lto_file *current_out_file;
304 /* Set the current output file. Return the old one. */
306 lto_file *
307 lto_set_current_out_file (lto_file *file)
309 lto_file *old_file;
311 old_file = current_out_file;
312 current_out_file = file;
313 return old_file;
316 /* Return the current output file. */
318 lto_file *
319 lto_get_current_out_file (void)
321 return current_out_file;
324 /* Begin writing a new section named NAME in the current output
325 file. */
327 void
328 lto_obj_begin_section (const char *name)
330 struct lto_simple_object *lo;
331 int align;
332 const char *errmsg;
333 int err;
335 lo = (struct lto_simple_object *) current_out_file;
336 gcc_assert (lo != NULL
337 && lo->sobj_r == NULL
338 && lo->sobj_w != NULL
339 && lo->section == NULL);
341 align = exact_log2 (POINTER_SIZE / BITS_PER_UNIT);
342 lo->section = simple_object_write_create_section (lo->sobj_w, name, align,
343 &errmsg, &err);
344 if (lo->section == NULL)
346 if (err == 0)
347 fatal_error ("%s", errmsg);
348 else
349 fatal_error ("%s: %s", errmsg, xstrerror (errno));
353 /* Add data to a section. BLOCK is a pointer to memory containing
354 DATA. */
356 void
357 lto_obj_append_data (const void *data, size_t len, void *)
359 struct lto_simple_object *lo;
360 const char *errmsg;
361 int err;
363 lo = (struct lto_simple_object *) current_out_file;
364 gcc_assert (lo != NULL && lo->section != NULL);
366 errmsg = simple_object_write_add_data (lo->sobj_w, lo->section, data, len,
367 1, &err);
368 if (errmsg != NULL)
370 if (err == 0)
371 fatal_error ("%s", errmsg);
372 else
373 fatal_error ("%s: %s", errmsg, xstrerror (errno));
377 /* Stop writing to the current output section. */
379 void
380 lto_obj_end_section (void)
382 struct lto_simple_object *lo;
384 lo = (struct lto_simple_object *) current_out_file;
385 gcc_assert (lo != NULL && lo->section != NULL);
386 lo->section = NULL;