Re-factor inclusion of tree.h.
[official-gcc.git] / gcc / lto / lto-object.c
blob2f51952ef760e5addd1c3323825754ecfaa34a4b
1 /* LTO routines to use object files.
2 Copyright (C) 2010-2013 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 "diagnostic-core.h"
26 #include "lto.h"
27 #include "tm.h"
28 #include "lto-streamer.h"
29 #include "simple-object.h"
31 /* Segment name for LTO sections. This is only used for Mach-O.
32 FIXME: This needs to be kept in sync with darwin.c. */
34 #define LTO_SEGMENT_NAME "__GNU_LTO"
36 /* An LTO file wrapped around an simple_object. */
38 struct lto_simple_object
40 /* The base information. */
41 lto_file base;
43 /* The system file descriptor. */
44 int fd;
46 /* The simple_object if we are reading the file. */
47 simple_object_read *sobj_r;
49 /* The simple_object if we are writing the file. */
50 simple_object_write *sobj_w;
52 /* The currently active section. */
53 simple_object_write_section *section;
56 /* Saved simple_object attributes. FIXME: Once set, this is never
57 cleared. */
59 static simple_object_attributes *saved_attributes;
61 /* Initialize FILE, an LTO file object for FILENAME. */
63 static void
64 lto_file_init (lto_file *file, const char *filename, off_t offset)
66 file->filename = filename;
67 file->offset = offset;
70 /* Open the file FILENAME. It WRITABLE is true, the file is opened
71 for write and, if necessary, created. Otherwise, the file is
72 opened for reading. Returns the opened file. */
74 lto_file *
75 lto_obj_file_open (const char *filename, bool writable)
77 const char *offset_p;
78 long loffset;
79 int consumed;
80 char *fname;
81 off_t offset;
82 struct lto_simple_object *lo;
83 const char *errmsg;
84 int err;
86 offset_p = strrchr (filename, '@');
87 if (offset_p != NULL
88 && offset_p != filename
89 && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
90 && strlen (offset_p) == (unsigned int) consumed)
92 fname = XNEWVEC (char, offset_p - filename + 1);
93 memcpy (fname, filename, offset_p - filename);
94 fname[offset_p - filename] = '\0';
95 offset = (off_t) loffset;
97 else
99 fname = xstrdup (filename);
100 offset = 0;
103 lo = XCNEW (struct lto_simple_object);
104 lto_file_init ((lto_file *) lo, fname, offset);
106 lo->fd = open (fname,
107 (writable
108 ? O_WRONLY | O_CREAT | O_BINARY
109 : O_RDONLY | O_BINARY),
110 0666);
111 if (lo->fd == -1)
113 error ("open %s failed: %s", fname, xstrerror (errno));
114 goto fail;
117 if (!writable)
119 simple_object_attributes *attrs;
121 lo->sobj_r = simple_object_start_read (lo->fd, offset, LTO_SEGMENT_NAME,
122 &errmsg, &err);
123 if (lo->sobj_r == NULL)
124 goto fail_errmsg;
126 attrs = simple_object_fetch_attributes (lo->sobj_r, &errmsg, &err);
127 if (attrs == NULL)
128 goto fail_errmsg;
130 if (saved_attributes == NULL)
131 saved_attributes = attrs;
132 else
134 errmsg = simple_object_attributes_merge (saved_attributes, attrs,
135 &err);
136 if (errmsg != NULL)
138 free (attrs);
139 goto fail_errmsg;
143 else
145 gcc_assert (saved_attributes != NULL);
146 lo->sobj_w = simple_object_start_write (saved_attributes,
147 LTO_SEGMENT_NAME,
148 &errmsg, &err);
149 if (lo->sobj_w == NULL)
150 goto fail_errmsg;
153 return &lo->base;
155 fail_errmsg:
156 if (err == 0)
157 error ("%s: %s", fname, errmsg);
158 else
159 error ("%s: %s: %s", fname, errmsg, xstrerror (err));
161 fail:
162 if (lo->fd != -1)
163 lto_obj_file_close ((lto_file *) lo);
164 free (lo);
165 return NULL;
169 /* Close FILE. If FILE was opened for writing, it is written out
170 now. */
172 void
173 lto_obj_file_close (lto_file *file)
175 struct lto_simple_object *lo = (struct lto_simple_object *) file;
177 if (lo->sobj_r != NULL)
178 simple_object_release_read (lo->sobj_r);
179 else if (lo->sobj_w != NULL)
181 const char *errmsg;
182 int err;
184 gcc_assert (lo->base.offset == 0);
186 errmsg = simple_object_write_to_file (lo->sobj_w, lo->fd, &err);
187 if (errmsg != NULL)
189 if (err == 0)
190 fatal_error ("%s", errmsg);
191 else
192 fatal_error ("%s: %s", errmsg, xstrerror (err));
195 simple_object_release_write (lo->sobj_w);
198 if (lo->fd != -1)
200 if (close (lo->fd) < 0)
201 fatal_error ("close: %s", xstrerror (errno));
205 /* This is passed to lto_obj_add_section. */
207 struct lto_obj_add_section_data
209 /* The hash table of sections. */
210 htab_t section_hash_table;
211 /* The offset of this file. */
212 off_t base_offset;
213 /* List in linker order */
214 struct lto_section_list *list;
217 /* This is called for each section in the file. */
219 static int
220 lto_obj_add_section (void *data, const char *name, off_t offset,
221 off_t length)
223 struct lto_obj_add_section_data *loasd =
224 (struct lto_obj_add_section_data *) data;
225 htab_t section_hash_table = (htab_t) loasd->section_hash_table;
226 char *new_name;
227 struct lto_section_slot s_slot;
228 void **slot;
229 struct lto_section_list *list = loasd->list;
231 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
232 strlen (LTO_SECTION_NAME_PREFIX)) != 0)
233 return 1;
235 new_name = xstrdup (name);
236 s_slot.name = new_name;
237 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
238 if (*slot == NULL)
240 struct lto_section_slot *new_slot = XCNEW (struct lto_section_slot);
242 new_slot->name = new_name;
243 new_slot->start = loasd->base_offset + offset;
244 new_slot->len = length;
245 *slot = new_slot;
247 if (list != NULL)
249 if (!list->first)
250 list->first = new_slot;
251 if (list->last)
252 list->last->next = new_slot;
253 list->last = new_slot;
256 else
258 error ("two or more sections for %s", new_name);
259 return 0;
262 return 1;
265 /* Build a hash table whose key is the section name and whose data is
266 the start and size of each section in the .o file. */
268 htab_t
269 lto_obj_build_section_table (lto_file *lto_file, struct lto_section_list *list)
271 struct lto_simple_object *lo = (struct lto_simple_object *) lto_file;
272 htab_t section_hash_table;
273 struct lto_obj_add_section_data loasd;
274 const char *errmsg;
275 int err;
277 section_hash_table = lto_obj_create_section_hash_table ();
279 gcc_assert (lo->sobj_r != NULL && lo->sobj_w == NULL);
280 loasd.section_hash_table = section_hash_table;
281 loasd.base_offset = lo->base.offset;
282 loasd.list = list;
283 errmsg = simple_object_find_sections (lo->sobj_r, lto_obj_add_section,
284 &loasd, &err);
285 if (errmsg != NULL)
287 if (err == 0)
288 error ("%s", errmsg);
289 else
290 error ("%s: %s", errmsg, xstrerror (err));
291 htab_delete (section_hash_table);
292 return NULL;
295 return section_hash_table;
298 /* The current output file. */
300 static lto_file *current_out_file;
302 /* Set the current output file. Return the old one. */
304 lto_file *
305 lto_set_current_out_file (lto_file *file)
307 lto_file *old_file;
309 old_file = current_out_file;
310 current_out_file = file;
311 return old_file;
314 /* Return the current output file. */
316 lto_file *
317 lto_get_current_out_file (void)
319 return current_out_file;
322 /* Begin writing a new section named NAME in the current output
323 file. */
325 void
326 lto_obj_begin_section (const char *name)
328 struct lto_simple_object *lo;
329 int align;
330 const char *errmsg;
331 int err;
333 lo = (struct lto_simple_object *) current_out_file;
334 gcc_assert (lo != NULL
335 && lo->sobj_r == NULL
336 && lo->sobj_w != NULL
337 && lo->section == NULL);
339 align = exact_log2 (POINTER_SIZE / BITS_PER_UNIT);
340 lo->section = simple_object_write_create_section (lo->sobj_w, name, align,
341 &errmsg, &err);
342 if (lo->section == NULL)
344 if (err == 0)
345 fatal_error ("%s", errmsg);
346 else
347 fatal_error ("%s: %s", errmsg, xstrerror (errno));
351 /* Add data to a section. BLOCK is a pointer to memory containing
352 DATA. */
354 void
355 lto_obj_append_data (const void *data, size_t len, void *block)
357 struct lto_simple_object *lo;
358 const char *errmsg;
359 int err;
361 lo = (struct lto_simple_object *) current_out_file;
362 gcc_assert (lo != NULL && lo->section != NULL);
364 errmsg = simple_object_write_add_data (lo->sobj_w, lo->section, data, len,
365 1, &err);
366 if (errmsg != NULL)
368 if (err == 0)
369 fatal_error ("%s", errmsg);
370 else
371 fatal_error ("%s: %s", errmsg, xstrerror (errno));
374 free (block);
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;