PR sanitizer/59009
[official-gcc.git] / gcc / lto / lto-object.c
blob19f10ccb978317f90639e0c0e31fa78ea8a32c41
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 "gimple.h"
26 #include "diagnostic-core.h"
27 #include "lto.h"
28 #include "tm.h"
29 #include "lto-streamer.h"
30 #include "simple-object.h"
32 /* Segment name for LTO sections. This is only used for Mach-O.
33 FIXME: This needs to be kept in sync with darwin.c. */
35 #define LTO_SEGMENT_NAME "__GNU_LTO"
37 /* An LTO file wrapped around an simple_object. */
39 struct lto_simple_object
41 /* The base information. */
42 lto_file base;
44 /* The system file descriptor. */
45 int fd;
47 /* The simple_object if we are reading the file. */
48 simple_object_read *sobj_r;
50 /* The simple_object if we are writing the file. */
51 simple_object_write *sobj_w;
53 /* The currently active section. */
54 simple_object_write_section *section;
57 /* Saved simple_object attributes. FIXME: Once set, this is never
58 cleared. */
60 static simple_object_attributes *saved_attributes;
62 /* Initialize FILE, an LTO file object for FILENAME. */
64 static void
65 lto_file_init (lto_file *file, const char *filename, off_t offset)
67 file->filename = filename;
68 file->offset = offset;
71 /* Open the file FILENAME. It WRITABLE is true, the file is opened
72 for write and, if necessary, created. Otherwise, the file is
73 opened for reading. Returns the opened file. */
75 lto_file *
76 lto_obj_file_open (const char *filename, bool writable)
78 const char *offset_p;
79 long loffset;
80 int consumed;
81 char *fname;
82 off_t offset;
83 struct lto_simple_object *lo;
84 const char *errmsg;
85 int err;
87 offset_p = strrchr (filename, '@');
88 if (offset_p != NULL
89 && offset_p != filename
90 && sscanf (offset_p, "@%li%n", &loffset, &consumed) >= 1
91 && strlen (offset_p) == (unsigned int) consumed)
93 fname = XNEWVEC (char, offset_p - filename + 1);
94 memcpy (fname, filename, offset_p - filename);
95 fname[offset_p - filename] = '\0';
96 offset = (off_t) loffset;
98 else
100 fname = xstrdup (filename);
101 offset = 0;
104 lo = XCNEW (struct lto_simple_object);
105 lto_file_init ((lto_file *) lo, fname, offset);
107 lo->fd = open (fname,
108 (writable
109 ? O_WRONLY | O_CREAT | O_BINARY
110 : O_RDONLY | O_BINARY),
111 0666);
112 if (lo->fd == -1)
114 error ("open %s failed: %s", fname, xstrerror (errno));
115 goto fail;
118 if (!writable)
120 simple_object_attributes *attrs;
122 lo->sobj_r = simple_object_start_read (lo->fd, offset, LTO_SEGMENT_NAME,
123 &errmsg, &err);
124 if (lo->sobj_r == NULL)
125 goto fail_errmsg;
127 attrs = simple_object_fetch_attributes (lo->sobj_r, &errmsg, &err);
128 if (attrs == NULL)
129 goto fail_errmsg;
131 if (saved_attributes == NULL)
132 saved_attributes = attrs;
133 else
135 errmsg = simple_object_attributes_merge (saved_attributes, attrs,
136 &err);
137 if (errmsg != NULL)
139 free (attrs);
140 goto fail_errmsg;
144 else
146 gcc_assert (saved_attributes != NULL);
147 lo->sobj_w = simple_object_start_write (saved_attributes,
148 LTO_SEGMENT_NAME,
149 &errmsg, &err);
150 if (lo->sobj_w == NULL)
151 goto fail_errmsg;
154 return &lo->base;
156 fail_errmsg:
157 if (err == 0)
158 error ("%s: %s", fname, errmsg);
159 else
160 error ("%s: %s: %s", fname, errmsg, xstrerror (err));
162 fail:
163 if (lo->fd != -1)
164 lto_obj_file_close ((lto_file *) lo);
165 free (lo);
166 return NULL;
170 /* Close FILE. If FILE was opened for writing, it is written out
171 now. */
173 void
174 lto_obj_file_close (lto_file *file)
176 struct lto_simple_object *lo = (struct lto_simple_object *) file;
178 if (lo->sobj_r != NULL)
179 simple_object_release_read (lo->sobj_r);
180 else if (lo->sobj_w != NULL)
182 const char *errmsg;
183 int err;
185 gcc_assert (lo->base.offset == 0);
187 errmsg = simple_object_write_to_file (lo->sobj_w, lo->fd, &err);
188 if (errmsg != NULL)
190 if (err == 0)
191 fatal_error ("%s", errmsg);
192 else
193 fatal_error ("%s: %s", errmsg, xstrerror (err));
196 simple_object_release_write (lo->sobj_w);
199 if (lo->fd != -1)
201 if (close (lo->fd) < 0)
202 fatal_error ("close: %s", xstrerror (errno));
206 /* This is passed to lto_obj_add_section. */
208 struct lto_obj_add_section_data
210 /* The hash table of sections. */
211 htab_t section_hash_table;
212 /* The offset of this file. */
213 off_t base_offset;
214 /* List in linker order */
215 struct lto_section_list *list;
218 /* This is called for each section in the file. */
220 static int
221 lto_obj_add_section (void *data, const char *name, off_t offset,
222 off_t length)
224 struct lto_obj_add_section_data *loasd =
225 (struct lto_obj_add_section_data *) data;
226 htab_t section_hash_table = (htab_t) loasd->section_hash_table;
227 char *new_name;
228 struct lto_section_slot s_slot;
229 void **slot;
230 struct lto_section_list *list = loasd->list;
232 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
233 strlen (LTO_SECTION_NAME_PREFIX)) != 0)
234 return 1;
236 new_name = xstrdup (name);
237 s_slot.name = new_name;
238 slot = htab_find_slot (section_hash_table, &s_slot, INSERT);
239 if (*slot == NULL)
241 struct lto_section_slot *new_slot = XCNEW (struct lto_section_slot);
243 new_slot->name = new_name;
244 new_slot->start = loasd->base_offset + offset;
245 new_slot->len = length;
246 *slot = new_slot;
248 if (list != NULL)
250 if (!list->first)
251 list->first = new_slot;
252 if (list->last)
253 list->last->next = new_slot;
254 list->last = new_slot;
257 else
259 error ("two or more sections for %s", new_name);
260 return 0;
263 return 1;
266 /* Build a hash table whose key is the section name and whose data is
267 the start and size of each section in the .o file. */
269 htab_t
270 lto_obj_build_section_table (lto_file *lto_file, struct lto_section_list *list)
272 struct lto_simple_object *lo = (struct lto_simple_object *) lto_file;
273 htab_t section_hash_table;
274 struct lto_obj_add_section_data loasd;
275 const char *errmsg;
276 int err;
278 section_hash_table = lto_obj_create_section_hash_table ();
280 gcc_assert (lo->sobj_r != NULL && lo->sobj_w == NULL);
281 loasd.section_hash_table = section_hash_table;
282 loasd.base_offset = lo->base.offset;
283 loasd.list = list;
284 errmsg = simple_object_find_sections (lo->sobj_r, lto_obj_add_section,
285 &loasd, &err);
286 if (errmsg != NULL)
288 if (err == 0)
289 error ("%s", errmsg);
290 else
291 error ("%s: %s", errmsg, xstrerror (err));
292 htab_delete (section_hash_table);
293 return NULL;
296 return section_hash_table;
299 /* The current output file. */
301 static lto_file *current_out_file;
303 /* Set the current output file. Return the old one. */
305 lto_file *
306 lto_set_current_out_file (lto_file *file)
308 lto_file *old_file;
310 old_file = current_out_file;
311 current_out_file = file;
312 return old_file;
315 /* Return the current output file. */
317 lto_file *
318 lto_get_current_out_file (void)
320 return current_out_file;
323 /* Begin writing a new section named NAME in the current output
324 file. */
326 void
327 lto_obj_begin_section (const char *name)
329 struct lto_simple_object *lo;
330 int align;
331 const char *errmsg;
332 int err;
334 lo = (struct lto_simple_object *) current_out_file;
335 gcc_assert (lo != NULL
336 && lo->sobj_r == NULL
337 && lo->sobj_w != NULL
338 && lo->section == NULL);
340 align = exact_log2 (POINTER_SIZE / BITS_PER_UNIT);
341 lo->section = simple_object_write_create_section (lo->sobj_w, name, align,
342 &errmsg, &err);
343 if (lo->section == NULL)
345 if (err == 0)
346 fatal_error ("%s", errmsg);
347 else
348 fatal_error ("%s: %s", errmsg, xstrerror (errno));
352 /* Add data to a section. BLOCK is a pointer to memory containing
353 DATA. */
355 void
356 lto_obj_append_data (const void *data, size_t len, void *block)
358 struct lto_simple_object *lo;
359 const char *errmsg;
360 int err;
362 lo = (struct lto_simple_object *) current_out_file;
363 gcc_assert (lo != NULL && lo->section != NULL);
365 errmsg = simple_object_write_add_data (lo->sobj_w, lo->section, data, len,
366 1, &err);
367 if (errmsg != NULL)
369 if (err == 0)
370 fatal_error ("%s", errmsg);
371 else
372 fatal_error ("%s: %s", errmsg, xstrerror (errno));
375 free (block);
378 /* Stop writing to the current output section. */
380 void
381 lto_obj_end_section (void)
383 struct lto_simple_object *lo;
385 lo = (struct lto_simple_object *) current_out_file;
386 gcc_assert (lo != NULL && lo->section != NULL);
387 lo->section = NULL;