* Add TARGET_CANNOT_SUBSTITUTE_MEM_EQUIV target macro.
[official-gcc.git] / gcc / lto / lto-object.c
blobb4124f65b97c49ee3b3d12aef695ae4944c12ed7
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 "predict.h"
26 #include "vec.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "tm.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "diagnostic-core.h"
41 #include "lto.h"
42 #include "hash-map.h"
43 #include "plugin-api.h"
44 #include "ipa-ref.h"
45 #include "cgraph.h"
46 #include "lto-streamer.h"
47 #include "lto-section-names.h"
48 #include "simple-object.h"
50 /* An LTO file wrapped around an simple_object. */
52 struct lto_simple_object
54 /* The base information. */
55 lto_file base;
57 /* The system file descriptor. */
58 int fd;
60 /* The simple_object if we are reading the file. */
61 simple_object_read *sobj_r;
63 /* The simple_object if we are writing the file. */
64 simple_object_write *sobj_w;
66 /* The currently active section. */
67 simple_object_write_section *section;
70 /* Saved simple_object attributes. FIXME: Once set, this is never
71 cleared. */
73 static simple_object_attributes *saved_attributes;
75 /* Initialize FILE, an LTO file object for FILENAME. */
77 static void
78 lto_file_init (lto_file *file, const char *filename, off_t offset)
80 file->filename = filename;
81 file->offset = offset;
84 /* Open the file FILENAME. It WRITABLE is true, the file is opened
85 for write and, if necessary, created. Otherwise, the file is
86 opened for reading. Returns the opened file. */
88 lto_file *
89 lto_obj_file_open (const char *filename, bool writable)
91 const char *offset_p;
92 long loffset;
93 int consumed;
94 char *fname;
95 off_t offset;
96 struct lto_simple_object *lo;
97 const char *errmsg;
98 int err;
100 offset_p = strrchr (filename, '@');
101 if (offset_p != NULL
102 && offset_p != filename
103 && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
104 && strlen (offset_p) == (unsigned int) consumed)
106 fname = XNEWVEC (char, offset_p - filename + 1);
107 memcpy (fname, filename, offset_p - filename);
108 fname[offset_p - filename] = '\0';
109 offset = (off_t) loffset;
111 else
113 fname = xstrdup (filename);
114 offset = 0;
117 lo = XCNEW (struct lto_simple_object);
118 lto_file_init ((lto_file *) lo, fname, offset);
120 lo->fd = open (fname,
121 (writable
122 ? O_WRONLY | O_CREAT | O_BINARY
123 : O_RDONLY | O_BINARY),
124 0666);
125 if (lo->fd == -1)
127 error ("open %s failed: %s", fname, xstrerror (errno));
128 goto fail;
131 if (!writable)
133 simple_object_attributes *attrs;
135 lo->sobj_r = simple_object_start_read (lo->fd, offset, LTO_SEGMENT_NAME,
136 &errmsg, &err);
137 if (lo->sobj_r == NULL)
138 goto fail_errmsg;
140 attrs = simple_object_fetch_attributes (lo->sobj_r, &errmsg, &err);
141 if (attrs == NULL)
142 goto fail_errmsg;
144 if (saved_attributes == NULL)
145 saved_attributes = attrs;
146 else
148 errmsg = simple_object_attributes_merge (saved_attributes, attrs,
149 &err);
150 if (errmsg != NULL)
152 free (attrs);
153 goto fail_errmsg;
157 else
159 gcc_assert (saved_attributes != NULL);
160 lo->sobj_w = simple_object_start_write (saved_attributes,
161 LTO_SEGMENT_NAME,
162 &errmsg, &err);
163 if (lo->sobj_w == NULL)
164 goto fail_errmsg;
167 return &lo->base;
169 fail_errmsg:
170 if (err == 0)
171 error ("%s: %s", fname, errmsg);
172 else
173 error ("%s: %s: %s", fname, errmsg, xstrerror (err));
175 fail:
176 if (lo->fd != -1)
177 lto_obj_file_close ((lto_file *) lo);
178 free (lo);
179 return NULL;
183 /* Close FILE. If FILE was opened for writing, it is written out
184 now. */
186 void
187 lto_obj_file_close (lto_file *file)
189 struct lto_simple_object *lo = (struct lto_simple_object *) file;
191 if (lo->sobj_r != NULL)
192 simple_object_release_read (lo->sobj_r);
193 else if (lo->sobj_w != NULL)
195 const char *errmsg;
196 int err;
198 gcc_assert (lo->base.offset == 0);
200 errmsg = simple_object_write_to_file (lo->sobj_w, lo->fd, &err);
201 if (errmsg != NULL)
203 if (err == 0)
204 fatal_error ("%s", errmsg);
205 else
206 fatal_error ("%s: %s", errmsg, xstrerror (err));
209 simple_object_release_write (lo->sobj_w);
212 if (lo->fd != -1)
214 if (close (lo->fd) < 0)
215 fatal_error ("close: %s", xstrerror (errno));
219 /* This is passed to lto_obj_add_section. */
221 struct lto_obj_add_section_data
223 /* The hash table of sections. */
224 htab_t section_hash_table;
225 /* The offset of this file. */
226 off_t base_offset;
227 /* List in linker order */
228 struct lto_section_list *list;
231 /* This is called for each section in the file. */
233 static int
234 lto_obj_add_section (void *data, const char *name, off_t offset,
235 off_t length)
237 struct lto_obj_add_section_data *loasd =
238 (struct lto_obj_add_section_data *) data;
239 htab_t section_hash_table = (htab_t) loasd->section_hash_table;
240 char *new_name;
241 struct lto_section_slot s_slot;
242 void **slot;
243 struct lto_section_list *list = loasd->list;
245 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
246 return 1;
248 new_name = xstrdup (name);
249 s_slot.name = new_name;
250 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
251 if (*slot == NULL)
253 struct lto_section_slot *new_slot = XCNEW (struct lto_section_slot);
255 new_slot->name = new_name;
256 new_slot->start = loasd->base_offset + offset;
257 new_slot->len = length;
258 *slot = new_slot;
260 if (list != NULL)
262 if (!list->first)
263 list->first = new_slot;
264 if (list->last)
265 list->last->next = new_slot;
266 list->last = new_slot;
269 else
271 error ("two or more sections for %s", new_name);
272 return 0;
275 return 1;
278 /* Build a hash table whose key is the section name and whose data is
279 the start and size of each section in the .o file. */
281 htab_t
282 lto_obj_build_section_table (lto_file *lto_file, struct lto_section_list *list)
284 struct lto_simple_object *lo = (struct lto_simple_object *) lto_file;
285 htab_t section_hash_table;
286 struct lto_obj_add_section_data loasd;
287 const char *errmsg;
288 int err;
290 section_hash_table = lto_obj_create_section_hash_table ();
292 gcc_assert (lo->sobj_r != NULL && lo->sobj_w == NULL);
293 loasd.section_hash_table = section_hash_table;
294 loasd.base_offset = lo->base.offset;
295 loasd.list = list;
296 errmsg = simple_object_find_sections (lo->sobj_r, lto_obj_add_section,
297 &loasd, &err);
298 if (errmsg != NULL)
300 if (err == 0)
301 error ("%s", errmsg);
302 else
303 error ("%s: %s", errmsg, xstrerror (err));
304 htab_delete (section_hash_table);
305 return NULL;
308 return section_hash_table;
311 /* The current output file. */
313 static lto_file *current_out_file;
315 /* Set the current output file. Return the old one. */
317 lto_file *
318 lto_set_current_out_file (lto_file *file)
320 lto_file *old_file;
322 old_file = current_out_file;
323 current_out_file = file;
324 return old_file;
327 /* Return the current output file. */
329 lto_file *
330 lto_get_current_out_file (void)
332 return current_out_file;
335 /* Begin writing a new section named NAME in the current output
336 file. */
338 void
339 lto_obj_begin_section (const char *name)
341 struct lto_simple_object *lo;
342 int align;
343 const char *errmsg;
344 int err;
346 lo = (struct lto_simple_object *) current_out_file;
347 gcc_assert (lo != NULL
348 && lo->sobj_r == NULL
349 && lo->sobj_w != NULL
350 && lo->section == NULL);
352 align = ceil_log2 (POINTER_SIZE_UNITS);
353 lo->section = simple_object_write_create_section (lo->sobj_w, name, align,
354 &errmsg, &err);
355 if (lo->section == NULL)
357 if (err == 0)
358 fatal_error ("%s", errmsg);
359 else
360 fatal_error ("%s: %s", errmsg, xstrerror (errno));
364 /* Add data to a section. BLOCK is a pointer to memory containing
365 DATA. */
367 void
368 lto_obj_append_data (const void *data, size_t len, void *)
370 struct lto_simple_object *lo;
371 const char *errmsg;
372 int err;
374 lo = (struct lto_simple_object *) current_out_file;
375 gcc_assert (lo != NULL && lo->section != NULL);
377 errmsg = simple_object_write_add_data (lo->sobj_w, lo->section, data, len,
378 1, &err);
379 if (errmsg != NULL)
381 if (err == 0)
382 fatal_error ("%s", errmsg);
383 else
384 fatal_error ("%s: %s", errmsg, xstrerror (errno));
388 /* Stop writing to the current output section. */
390 void
391 lto_obj_end_section (void)
393 struct lto_simple_object *lo;
395 lo = (struct lto_simple_object *) current_out_file;
396 gcc_assert (lo != NULL && lo->section != NULL);
397 lo->section = NULL;