2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lto / lto-object.c
blob087c6b11d6307d7a9a412ef2e7eef0e0d43e7a79
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
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 "alias.h"
25 #include "tm.h"
26 #include "function.h"
27 #include "predict.h"
28 #include "basic-block.h"
29 #include "tree.h"
30 #include "gimple.h"
31 #include "hard-reg-set.h"
32 #include "options.h"
33 #include "fold-const.h"
34 #include "internal-fn.h"
35 #include "diagnostic-core.h"
36 #include "lto.h"
37 #include "cgraph.h"
38 #include "lto-section-names.h"
39 #include "simple-object.h"
41 /* An LTO file wrapped around an simple_object. */
43 struct lto_simple_object
45 /* The base information. */
46 lto_file base;
48 /* The system file descriptor. */
49 int fd;
51 /* The simple_object if we are reading the file. */
52 simple_object_read *sobj_r;
54 /* The simple_object if we are writing the file. */
55 simple_object_write *sobj_w;
57 /* The currently active section. */
58 simple_object_write_section *section;
61 /* Saved simple_object attributes. FIXME: Once set, this is never
62 cleared. */
64 static simple_object_attributes *saved_attributes;
66 /* Initialize FILE, an LTO file object for FILENAME. */
68 static void
69 lto_file_init (lto_file *file, const char *filename, off_t offset)
71 file->filename = filename;
72 file->offset = offset;
75 /* Open the file FILENAME. It WRITABLE is true, the file is opened
76 for write and, if necessary, created. Otherwise, the file is
77 opened for reading. Returns the opened file. */
79 lto_file *
80 lto_obj_file_open (const char *filename, bool writable)
82 const char *offset_p;
83 long loffset;
84 int consumed;
85 char *fname;
86 off_t offset;
87 struct lto_simple_object *lo;
88 const char *errmsg;
89 int err;
91 offset_p = strrchr (filename, '@');
92 if (offset_p != NULL
93 && offset_p != filename
94 && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
95 && strlen (offset_p) == (unsigned int) consumed)
97 fname = XNEWVEC (char, offset_p - filename + 1);
98 memcpy (fname, filename, offset_p - filename);
99 fname[offset_p - filename] = '\0';
100 offset = (off_t) loffset;
102 else
104 fname = xstrdup (filename);
105 offset = 0;
108 lo = XCNEW (struct lto_simple_object);
109 lto_file_init ((lto_file *) lo, fname, offset);
111 lo->fd = open (fname,
112 (writable
113 ? O_WRONLY | O_CREAT | O_BINARY
114 : O_RDONLY | O_BINARY),
115 0666);
116 if (lo->fd == -1)
118 error ("open %s failed: %s", fname, xstrerror (errno));
119 goto fail;
122 if (!writable)
124 simple_object_attributes *attrs;
126 lo->sobj_r = simple_object_start_read (lo->fd, offset, LTO_SEGMENT_NAME,
127 &errmsg, &err);
128 if (lo->sobj_r == NULL)
129 goto fail_errmsg;
131 attrs = simple_object_fetch_attributes (lo->sobj_r, &errmsg, &err);
132 if (attrs == NULL)
133 goto fail_errmsg;
135 if (saved_attributes == NULL)
136 saved_attributes = attrs;
137 else
139 errmsg = simple_object_attributes_merge (saved_attributes, attrs,
140 &err);
141 if (errmsg != NULL)
143 free (attrs);
144 goto fail_errmsg;
148 else
150 gcc_assert (saved_attributes != NULL);
151 lo->sobj_w = simple_object_start_write (saved_attributes,
152 LTO_SEGMENT_NAME,
153 &errmsg, &err);
154 if (lo->sobj_w == NULL)
155 goto fail_errmsg;
158 return &lo->base;
160 fail_errmsg:
161 if (err == 0)
162 error ("%s: %s", fname, errmsg);
163 else
164 error ("%s: %s: %s", fname, errmsg, xstrerror (err));
166 fail:
167 if (lo->fd != -1)
168 lto_obj_file_close ((lto_file *) lo);
169 free (lo);
170 return NULL;
174 /* Close FILE. If FILE was opened for writing, it is written out
175 now. */
177 void
178 lto_obj_file_close (lto_file *file)
180 struct lto_simple_object *lo = (struct lto_simple_object *) file;
182 if (lo->sobj_r != NULL)
183 simple_object_release_read (lo->sobj_r);
184 else if (lo->sobj_w != NULL)
186 const char *errmsg;
187 int err;
189 gcc_assert (lo->base.offset == 0);
191 errmsg = simple_object_write_to_file (lo->sobj_w, lo->fd, &err);
192 if (errmsg != NULL)
194 if (err == 0)
195 fatal_error (input_location, "%s", errmsg);
196 else
197 fatal_error (input_location, "%s: %s", errmsg, xstrerror (err));
200 simple_object_release_write (lo->sobj_w);
203 if (lo->fd != -1)
205 if (close (lo->fd) < 0)
206 fatal_error (input_location, "close: %s", xstrerror (errno));
210 /* This is passed to lto_obj_add_section. */
212 struct lto_obj_add_section_data
214 /* The hash table of sections. */
215 htab_t section_hash_table;
216 /* The offset of this file. */
217 off_t base_offset;
218 /* List in linker order */
219 struct lto_section_list *list;
222 /* This is called for each section in the file. */
224 static int
225 lto_obj_add_section (void *data, const char *name, off_t offset,
226 off_t length)
228 struct lto_obj_add_section_data *loasd =
229 (struct lto_obj_add_section_data *) data;
230 htab_t section_hash_table = (htab_t) loasd->section_hash_table;
231 char *new_name;
232 struct lto_section_slot s_slot;
233 void **slot;
234 struct lto_section_list *list = loasd->list;
236 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
237 return 1;
239 new_name = xstrdup (name);
240 s_slot.name = new_name;
241 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
242 if (*slot == NULL)
244 struct lto_section_slot *new_slot = XCNEW (struct lto_section_slot);
246 new_slot->name = new_name;
247 new_slot->start = loasd->base_offset + offset;
248 new_slot->len = length;
249 *slot = new_slot;
251 if (list != NULL)
253 if (!list->first)
254 list->first = new_slot;
255 if (list->last)
256 list->last->next = new_slot;
257 list->last = new_slot;
260 else
262 error ("two or more sections for %s", new_name);
263 return 0;
266 return 1;
269 /* Build a hash table whose key is the section name and whose data is
270 the start and size of each section in the .o file. */
272 htab_t
273 lto_obj_build_section_table (lto_file *lto_file, struct lto_section_list *list)
275 struct lto_simple_object *lo = (struct lto_simple_object *) lto_file;
276 htab_t section_hash_table;
277 struct lto_obj_add_section_data loasd;
278 const char *errmsg;
279 int err;
281 section_hash_table = lto_obj_create_section_hash_table ();
283 gcc_assert (lo->sobj_r != NULL && lo->sobj_w == NULL);
284 loasd.section_hash_table = section_hash_table;
285 loasd.base_offset = lo->base.offset;
286 loasd.list = list;
287 errmsg = simple_object_find_sections (lo->sobj_r, lto_obj_add_section,
288 &loasd, &err);
289 if (errmsg != NULL)
291 if (err == 0)
292 error ("%s", errmsg);
293 else
294 error ("%s: %s", errmsg, xstrerror (err));
295 htab_delete (section_hash_table);
296 return NULL;
299 return section_hash_table;
302 /* The current output file. */
304 static lto_file *current_out_file;
306 /* Set the current output file. Return the old one. */
308 lto_file *
309 lto_set_current_out_file (lto_file *file)
311 lto_file *old_file;
313 old_file = current_out_file;
314 current_out_file = file;
315 return old_file;
318 /* Return the current output file. */
320 lto_file *
321 lto_get_current_out_file (void)
323 return current_out_file;
326 /* Begin writing a new section named NAME in the current output
327 file. */
329 void
330 lto_obj_begin_section (const char *name)
332 struct lto_simple_object *lo;
333 int align;
334 const char *errmsg;
335 int err;
337 lo = (struct lto_simple_object *) current_out_file;
338 gcc_assert (lo != NULL
339 && lo->sobj_r == NULL
340 && lo->sobj_w != NULL
341 && lo->section == NULL);
343 align = ceil_log2 (POINTER_SIZE_UNITS);
344 lo->section = simple_object_write_create_section (lo->sobj_w, name, align,
345 &errmsg, &err);
346 if (lo->section == NULL)
348 if (err == 0)
349 fatal_error (input_location, "%s", errmsg);
350 else
351 fatal_error (input_location, "%s: %s", errmsg, xstrerror (errno));
355 /* Add data to a section. BLOCK is a pointer to memory containing
356 DATA. */
358 void
359 lto_obj_append_data (const void *data, size_t len, void *)
361 struct lto_simple_object *lo;
362 const char *errmsg;
363 int err;
365 lo = (struct lto_simple_object *) current_out_file;
366 gcc_assert (lo != NULL && lo->section != NULL);
368 errmsg = simple_object_write_add_data (lo->sobj_w, lo->section, data, len,
369 1, &err);
370 if (errmsg != NULL)
372 if (err == 0)
373 fatal_error (input_location, "%s", errmsg);
374 else
375 fatal_error (input_location, "%s: %s", errmsg, xstrerror (errno));
379 /* Stop writing to the current output section. */
381 void
382 lto_obj_end_section (void)
384 struct lto_simple_object *lo;
386 lo = (struct lto_simple_object *) current_out_file;
387 gcc_assert (lo != NULL && lo->section != NULL);
388 lo->section = NULL;