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
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
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/>. */
23 #include "coretypes.h"
31 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
40 #include "diagnostic-core.h"
43 #include "plugin-api.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. */
57 /* The system file descriptor. */
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
73 static simple_object_attributes
*saved_attributes
;
75 /* Initialize FILE, an LTO file object for FILENAME. */
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. */
89 lto_obj_file_open (const char *filename
, bool writable
)
96 struct lto_simple_object
*lo
;
100 offset_p
= strrchr (filename
, '@');
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
;
113 fname
= xstrdup (filename
);
117 lo
= XCNEW (struct lto_simple_object
);
118 lto_file_init ((lto_file
*) lo
, fname
, offset
);
120 lo
->fd
= open (fname
,
122 ? O_WRONLY
| O_CREAT
| O_BINARY
123 : O_RDONLY
| O_BINARY
),
127 error ("open %s failed: %s", fname
, xstrerror (errno
));
133 simple_object_attributes
*attrs
;
135 lo
->sobj_r
= simple_object_start_read (lo
->fd
, offset
, LTO_SEGMENT_NAME
,
137 if (lo
->sobj_r
== NULL
)
140 attrs
= simple_object_fetch_attributes (lo
->sobj_r
, &errmsg
, &err
);
144 if (saved_attributes
== NULL
)
145 saved_attributes
= attrs
;
148 errmsg
= simple_object_attributes_merge (saved_attributes
, attrs
,
159 gcc_assert (saved_attributes
!= NULL
);
160 lo
->sobj_w
= simple_object_start_write (saved_attributes
,
163 if (lo
->sobj_w
== NULL
)
171 error ("%s: %s", fname
, errmsg
);
173 error ("%s: %s: %s", fname
, errmsg
, xstrerror (err
));
177 lto_obj_file_close ((lto_file
*) lo
);
183 /* Close FILE. If FILE was opened for writing, it is written out
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
)
198 gcc_assert (lo
->base
.offset
== 0);
200 errmsg
= simple_object_write_to_file (lo
->sobj_w
, lo
->fd
, &err
);
204 fatal_error ("%s", errmsg
);
206 fatal_error ("%s: %s", errmsg
, xstrerror (err
));
209 simple_object_release_write (lo
->sobj_w
);
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. */
227 /* List in linker order */
228 struct lto_section_list
*list
;
231 /* This is called for each section in the file. */
234 lto_obj_add_section (void *data
, const char *name
, off_t offset
,
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
;
241 struct lto_section_slot s_slot
;
243 struct lto_section_list
*list
= loasd
->list
;
245 if (strncmp (name
, LTO_SECTION_NAME_PREFIX
,
246 strlen (LTO_SECTION_NAME_PREFIX
)) != 0)
249 new_name
= xstrdup (name
);
250 s_slot
.name
= new_name
;
251 slot
= htab_find_slot (section_hash_table
, &s_slot
, INSERT
);
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
;
264 list
->first
= new_slot
;
266 list
->last
->next
= new_slot
;
267 list
->last
= new_slot
;
272 error ("two or more sections for %s", new_name
);
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. */
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
;
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
;
297 errmsg
= simple_object_find_sections (lo
->sobj_r
, lto_obj_add_section
,
302 error ("%s", errmsg
);
304 error ("%s: %s", errmsg
, xstrerror (err
));
305 htab_delete (section_hash_table
);
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. */
319 lto_set_current_out_file (lto_file
*file
)
323 old_file
= current_out_file
;
324 current_out_file
= file
;
328 /* Return the current output 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
340 lto_obj_begin_section (const char *name
)
342 struct lto_simple_object
*lo
;
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
,
356 if (lo
->section
== NULL
)
359 fatal_error ("%s", errmsg
);
361 fatal_error ("%s: %s", errmsg
, xstrerror (errno
));
365 /* Add data to a section. BLOCK is a pointer to memory containing
369 lto_obj_append_data (const void *data
, size_t len
, void *)
371 struct lto_simple_object
*lo
;
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
,
383 fatal_error ("%s", errmsg
);
385 fatal_error ("%s: %s", errmsg
, xstrerror (errno
));
389 /* Stop writing to the current output section. */
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
);