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
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"
26 #include "diagnostic-core.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. */
44 /* The system file descriptor. */
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
60 static simple_object_attributes
*saved_attributes
;
62 /* Initialize FILE, an LTO file object for FILENAME. */
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. */
76 lto_obj_file_open (const char *filename
, bool writable
)
83 struct lto_simple_object
*lo
;
87 offset_p
= strrchr (filename
, '@');
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
;
100 fname
= xstrdup (filename
);
104 lo
= XCNEW (struct lto_simple_object
);
105 lto_file_init ((lto_file
*) lo
, fname
, offset
);
107 lo
->fd
= open (fname
,
109 ? O_WRONLY
| O_CREAT
| O_BINARY
110 : O_RDONLY
| O_BINARY
),
114 error ("open %s failed: %s", fname
, xstrerror (errno
));
120 simple_object_attributes
*attrs
;
122 lo
->sobj_r
= simple_object_start_read (lo
->fd
, offset
, LTO_SEGMENT_NAME
,
124 if (lo
->sobj_r
== NULL
)
127 attrs
= simple_object_fetch_attributes (lo
->sobj_r
, &errmsg
, &err
);
131 if (saved_attributes
== NULL
)
132 saved_attributes
= attrs
;
135 errmsg
= simple_object_attributes_merge (saved_attributes
, attrs
,
146 gcc_assert (saved_attributes
!= NULL
);
147 lo
->sobj_w
= simple_object_start_write (saved_attributes
,
150 if (lo
->sobj_w
== NULL
)
158 error ("%s: %s", fname
, errmsg
);
160 error ("%s: %s: %s", fname
, errmsg
, xstrerror (err
));
164 lto_obj_file_close ((lto_file
*) lo
);
170 /* Close FILE. If FILE was opened for writing, it is written out
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
)
185 gcc_assert (lo
->base
.offset
== 0);
187 errmsg
= simple_object_write_to_file (lo
->sobj_w
, lo
->fd
, &err
);
191 fatal_error ("%s", errmsg
);
193 fatal_error ("%s: %s", errmsg
, xstrerror (err
));
196 simple_object_release_write (lo
->sobj_w
);
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. */
214 /* List in linker order */
215 struct lto_section_list
*list
;
218 /* This is called for each section in the file. */
221 lto_obj_add_section (void *data
, const char *name
, off_t offset
,
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
;
228 struct lto_section_slot s_slot
;
230 struct lto_section_list
*list
= loasd
->list
;
232 if (strncmp (name
, LTO_SECTION_NAME_PREFIX
,
233 strlen (LTO_SECTION_NAME_PREFIX
)) != 0)
236 new_name
= xstrdup (name
);
237 s_slot
.name
= new_name
;
238 slot
= htab_find_slot (section_hash_table
, &s_slot
, INSERT
);
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
;
251 list
->first
= new_slot
;
253 list
->last
->next
= new_slot
;
254 list
->last
= new_slot
;
259 error ("two or more sections for %s", new_name
);
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. */
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
;
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
;
284 errmsg
= simple_object_find_sections (lo
->sobj_r
, lto_obj_add_section
,
289 error ("%s", errmsg
);
291 error ("%s: %s", errmsg
, xstrerror (err
));
292 htab_delete (section_hash_table
);
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. */
306 lto_set_current_out_file (lto_file
*file
)
310 old_file
= current_out_file
;
311 current_out_file
= file
;
315 /* Return the current output 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
327 lto_obj_begin_section (const char *name
)
329 struct lto_simple_object
*lo
;
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
,
343 if (lo
->section
== NULL
)
346 fatal_error ("%s", errmsg
);
348 fatal_error ("%s: %s", errmsg
, xstrerror (errno
));
352 /* Add data to a section. BLOCK is a pointer to memory containing
356 lto_obj_append_data (const void *data
, size_t len
, void *block
)
358 struct lto_simple_object
*lo
;
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
,
370 fatal_error ("%s", errmsg
);
372 fatal_error ("%s: %s", errmsg
, xstrerror (errno
));
378 /* Stop writing to the current output section. */
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
);