* gcc.c-torture/execute/20010129-1.c: Compile with -mtune=i686
[official-gcc.git] / gcc / lto / lto-object.c
blob1178326b99199f4588ed2e1e3de82b4c5e75d8bb
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, LTO_SECTION_NAME_PREFIX,
246 strlen (LTO_SECTION_NAME_PREFIX)) != 0)
247 return 1;
249 new_name = xstrdup (name);
250 s_slot.name = new_name;
251 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
252 if (*slot == NULL)
254 struct lto_section_slot *new_slot = XCNEW (struct lto_section_slot);
256 new_slot->name = new_name;
257 new_slot->start = loasd->base_offset + offset;
258 new_slot->len = length;
259 *slot = new_slot;
261 if (list != NULL)
263 if (!list->first)
264 list->first = new_slot;
265 if (list->last)
266 list->last->next = new_slot;
267 list->last = new_slot;
270 else
272 error ("two or more sections for %s", new_name);
273 return 0;
276 return 1;
279 /* Build a hash table whose key is the section name and whose data is
280 the start and size of each section in the .o file. */
282 htab_t
283 lto_obj_build_section_table (lto_file *lto_file, struct lto_section_list *list)
285 struct lto_simple_object *lo = (struct lto_simple_object *) lto_file;
286 htab_t section_hash_table;
287 struct lto_obj_add_section_data loasd;
288 const char *errmsg;
289 int err;
291 section_hash_table = lto_obj_create_section_hash_table ();
293 gcc_assert (lo->sobj_r != NULL && lo->sobj_w == NULL);
294 loasd.section_hash_table = section_hash_table;
295 loasd.base_offset = lo->base.offset;
296 loasd.list = list;
297 errmsg = simple_object_find_sections (lo->sobj_r, lto_obj_add_section,
298 &loasd, &err);
299 if (errmsg != NULL)
301 if (err == 0)
302 error ("%s", errmsg);
303 else
304 error ("%s: %s", errmsg, xstrerror (err));
305 htab_delete (section_hash_table);
306 return NULL;
309 return section_hash_table;
312 /* The current output file. */
314 static lto_file *current_out_file;
316 /* Set the current output file. Return the old one. */
318 lto_file *
319 lto_set_current_out_file (lto_file *file)
321 lto_file *old_file;
323 old_file = current_out_file;
324 current_out_file = file;
325 return old_file;
328 /* Return the current output file. */
330 lto_file *
331 lto_get_current_out_file (void)
333 return current_out_file;
336 /* Begin writing a new section named NAME in the current output
337 file. */
339 void
340 lto_obj_begin_section (const char *name)
342 struct lto_simple_object *lo;
343 int align;
344 const char *errmsg;
345 int err;
347 lo = (struct lto_simple_object *) current_out_file;
348 gcc_assert (lo != NULL
349 && lo->sobj_r == NULL
350 && lo->sobj_w != NULL
351 && lo->section == NULL);
353 align = ceil_log2 (POINTER_SIZE_UNITS);
354 lo->section = simple_object_write_create_section (lo->sobj_w, name, align,
355 &errmsg, &err);
356 if (lo->section == NULL)
358 if (err == 0)
359 fatal_error ("%s", errmsg);
360 else
361 fatal_error ("%s: %s", errmsg, xstrerror (errno));
365 /* Add data to a section. BLOCK is a pointer to memory containing
366 DATA. */
368 void
369 lto_obj_append_data (const void *data, size_t len, void *)
371 struct lto_simple_object *lo;
372 const char *errmsg;
373 int err;
375 lo = (struct lto_simple_object *) current_out_file;
376 gcc_assert (lo != NULL && lo->section != NULL);
378 errmsg = simple_object_write_add_data (lo->sobj_w, lo->section, data, len,
379 1, &err);
380 if (errmsg != NULL)
382 if (err == 0)
383 fatal_error ("%s", errmsg);
384 else
385 fatal_error ("%s: %s", errmsg, xstrerror (errno));
389 /* Stop writing to the current output section. */
391 void
392 lto_obj_end_section (void)
394 struct lto_simple_object *lo;
396 lo = (struct lto_simple_object *) current_out_file;
397 gcc_assert (lo != NULL && lo->section != NULL);
398 lo->section = NULL;