Merge trunk version 209848 into gupc branch.
[official-gcc.git] / gcc / lto / lto-object.c
blobf43fb3a30d6370d4d607100e7cc6b330deff7205
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 "simple-object.h"
37 /* Segment name for LTO sections. This is only used for Mach-O.
38 FIXME: This needs to be kept in sync with darwin.c. */
40 #define LTO_SEGMENT_NAME "__GNU_LTO"
42 /* An LTO file wrapped around an simple_object. */
44 struct lto_simple_object
46 /* The base information. */
47 lto_file base;
49 /* The system file descriptor. */
50 int fd;
52 /* The simple_object if we are reading the file. */
53 simple_object_read *sobj_r;
55 /* The simple_object if we are writing the file. */
56 simple_object_write *sobj_w;
58 /* The currently active section. */
59 simple_object_write_section *section;
62 /* Saved simple_object attributes. FIXME: Once set, this is never
63 cleared. */
65 static simple_object_attributes *saved_attributes;
67 /* Initialize FILE, an LTO file object for FILENAME. */
69 static void
70 lto_file_init (lto_file *file, const char *filename, off_t offset)
72 file->filename = filename;
73 file->offset = offset;
76 /* Open the file FILENAME. It WRITABLE is true, the file is opened
77 for write and, if necessary, created. Otherwise, the file is
78 opened for reading. Returns the opened file. */
80 lto_file *
81 lto_obj_file_open (const char *filename, bool writable)
83 const char *offset_p;
84 long loffset;
85 int consumed;
86 char *fname;
87 off_t offset;
88 struct lto_simple_object *lo;
89 const char *errmsg;
90 int err;
92 offset_p = strrchr (filename, '@');
93 if (offset_p != NULL
94 && offset_p != filename
95 && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
96 && strlen (offset_p) == (unsigned int) consumed)
98 fname = XNEWVEC (char, offset_p - filename + 1);
99 memcpy (fname, filename, offset_p - filename);
100 fname[offset_p - filename] = '\0';
101 offset = (off_t) loffset;
103 else
105 fname = xstrdup (filename);
106 offset = 0;
109 lo = XCNEW (struct lto_simple_object);
110 lto_file_init ((lto_file *) lo, fname, offset);
112 lo->fd = open (fname,
113 (writable
114 ? O_WRONLY | O_CREAT | O_BINARY
115 : O_RDONLY | O_BINARY),
116 0666);
117 if (lo->fd == -1)
119 error ("open %s failed: %s", fname, xstrerror (errno));
120 goto fail;
123 if (!writable)
125 simple_object_attributes *attrs;
127 lo->sobj_r = simple_object_start_read (lo->fd, offset, LTO_SEGMENT_NAME,
128 &errmsg, &err);
129 if (lo->sobj_r == NULL)
130 goto fail_errmsg;
132 attrs = simple_object_fetch_attributes (lo->sobj_r, &errmsg, &err);
133 if (attrs == NULL)
134 goto fail_errmsg;
136 if (saved_attributes == NULL)
137 saved_attributes = attrs;
138 else
140 errmsg = simple_object_attributes_merge (saved_attributes, attrs,
141 &err);
142 if (errmsg != NULL)
144 free (attrs);
145 goto fail_errmsg;
149 else
151 gcc_assert (saved_attributes != NULL);
152 lo->sobj_w = simple_object_start_write (saved_attributes,
153 LTO_SEGMENT_NAME,
154 &errmsg, &err);
155 if (lo->sobj_w == NULL)
156 goto fail_errmsg;
159 return &lo->base;
161 fail_errmsg:
162 if (err == 0)
163 error ("%s: %s", fname, errmsg);
164 else
165 error ("%s: %s: %s", fname, errmsg, xstrerror (err));
167 fail:
168 if (lo->fd != -1)
169 lto_obj_file_close ((lto_file *) lo);
170 free (lo);
171 return NULL;
175 /* Close FILE. If FILE was opened for writing, it is written out
176 now. */
178 void
179 lto_obj_file_close (lto_file *file)
181 struct lto_simple_object *lo = (struct lto_simple_object *) file;
183 if (lo->sobj_r != NULL)
184 simple_object_release_read (lo->sobj_r);
185 else if (lo->sobj_w != NULL)
187 const char *errmsg;
188 int err;
190 gcc_assert (lo->base.offset == 0);
192 errmsg = simple_object_write_to_file (lo->sobj_w, lo->fd, &err);
193 if (errmsg != NULL)
195 if (err == 0)
196 fatal_error ("%s", errmsg);
197 else
198 fatal_error ("%s: %s", errmsg, xstrerror (err));
201 simple_object_release_write (lo->sobj_w);
204 if (lo->fd != -1)
206 if (close (lo->fd) < 0)
207 fatal_error ("close: %s", xstrerror (errno));
211 /* This is passed to lto_obj_add_section. */
213 struct lto_obj_add_section_data
215 /* The hash table of sections. */
216 htab_t section_hash_table;
217 /* The offset of this file. */
218 off_t base_offset;
219 /* List in linker order */
220 struct lto_section_list *list;
223 /* This is called for each section in the file. */
225 static int
226 lto_obj_add_section (void *data, const char *name, off_t offset,
227 off_t length)
229 struct lto_obj_add_section_data *loasd =
230 (struct lto_obj_add_section_data *) data;
231 htab_t section_hash_table = (htab_t) loasd->section_hash_table;
232 char *new_name;
233 struct lto_section_slot s_slot;
234 void **slot;
235 struct lto_section_list *list = loasd->list;
237 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
238 strlen (LTO_SECTION_NAME_PREFIX)) != 0)
239 return 1;
241 new_name = xstrdup (name);
242 s_slot.name = new_name;
243 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
244 if (*slot == NULL)
246 struct lto_section_slot *new_slot = XCNEW (struct lto_section_slot);
248 new_slot->name = new_name;
249 new_slot->start = loasd->base_offset + offset;
250 new_slot->len = length;
251 *slot = new_slot;
253 if (list != NULL)
255 if (!list->first)
256 list->first = new_slot;
257 if (list->last)
258 list->last->next = new_slot;
259 list->last = new_slot;
262 else
264 error ("two or more sections for %s", new_name);
265 return 0;
268 return 1;
271 /* Build a hash table whose key is the section name and whose data is
272 the start and size of each section in the .o file. */
274 htab_t
275 lto_obj_build_section_table (lto_file *lto_file, struct lto_section_list *list)
277 struct lto_simple_object *lo = (struct lto_simple_object *) lto_file;
278 htab_t section_hash_table;
279 struct lto_obj_add_section_data loasd;
280 const char *errmsg;
281 int err;
283 section_hash_table = lto_obj_create_section_hash_table ();
285 gcc_assert (lo->sobj_r != NULL && lo->sobj_w == NULL);
286 loasd.section_hash_table = section_hash_table;
287 loasd.base_offset = lo->base.offset;
288 loasd.list = list;
289 errmsg = simple_object_find_sections (lo->sobj_r, lto_obj_add_section,
290 &loasd, &err);
291 if (errmsg != NULL)
293 if (err == 0)
294 error ("%s", errmsg);
295 else
296 error ("%s: %s", errmsg, xstrerror (err));
297 htab_delete (section_hash_table);
298 return NULL;
301 return section_hash_table;
304 /* The current output file. */
306 static lto_file *current_out_file;
308 /* Set the current output file. Return the old one. */
310 lto_file *
311 lto_set_current_out_file (lto_file *file)
313 lto_file *old_file;
315 old_file = current_out_file;
316 current_out_file = file;
317 return old_file;
320 /* Return the current output file. */
322 lto_file *
323 lto_get_current_out_file (void)
325 return current_out_file;
328 /* Begin writing a new section named NAME in the current output
329 file. */
331 void
332 lto_obj_begin_section (const char *name)
334 struct lto_simple_object *lo;
335 int align;
336 const char *errmsg;
337 int err;
339 lo = (struct lto_simple_object *) current_out_file;
340 gcc_assert (lo != NULL
341 && lo->sobj_r == NULL
342 && lo->sobj_w != NULL
343 && lo->section == NULL);
345 align = exact_log2 (POINTER_SIZE / BITS_PER_UNIT);
346 lo->section = simple_object_write_create_section (lo->sobj_w, name, align,
347 &errmsg, &err);
348 if (lo->section == NULL)
350 if (err == 0)
351 fatal_error ("%s", errmsg);
352 else
353 fatal_error ("%s: %s", errmsg, xstrerror (errno));
357 /* Add data to a section. BLOCK is a pointer to memory containing
358 DATA. */
360 void
361 lto_obj_append_data (const void *data, size_t len, void *block)
363 struct lto_simple_object *lo;
364 const char *errmsg;
365 int err;
367 lo = (struct lto_simple_object *) current_out_file;
368 gcc_assert (lo != NULL && lo->section != NULL);
370 errmsg = simple_object_write_add_data (lo->sobj_w, lo->section, data, len,
371 1, &err);
372 if (errmsg != NULL)
374 if (err == 0)
375 fatal_error ("%s", errmsg);
376 else
377 fatal_error ("%s: %s", errmsg, xstrerror (errno));
380 free (block);
383 /* Stop writing to the current output section. */
385 void
386 lto_obj_end_section (void)
388 struct lto_simple_object *lo;
390 lo = (struct lto_simple_object *) current_out_file;
391 gcc_assert (lo != NULL && lo->section != NULL);
392 lo->section = NULL;