PR testsuite/52641
[official-gcc.git] / gcc / go / go-backend.c
blobc3ffa3b1deaf59776fc94321bc716142f9554dc0
1 /* go-backend.c -- Go frontend interface to gcc backend.
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "simple-object.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "intl.h"
28 #include "output.h" /* for assemble_string */
29 #include "target.h"
30 #include "common/common-target.h"
32 #include "go-c.h"
34 /* The segment name we pass to simple_object_start_read to find Go
35 export data. */
37 #ifndef GO_EXPORT_SEGMENT_NAME
38 #define GO_EXPORT_SEGMENT_NAME "__GNU_GO"
39 #endif
41 /* The section name we use when reading and writing export data. */
43 #ifndef GO_EXPORT_SECTION_NAME
44 #define GO_EXPORT_SECTION_NAME ".go_export"
45 #endif
47 /* This file holds all the cases where the Go frontend needs
48 information from gcc's backend. */
50 /* Return the alignment in bytes of a struct field of type T. */
52 unsigned int
53 go_field_alignment (tree t)
55 unsigned int v;
57 v = TYPE_ALIGN (t);
59 #ifdef BIGGEST_FIELD_ALIGNMENT
60 if (v > BIGGEST_FIELD_ALIGNMENT)
61 v = BIGGEST_FIELD_ALIGNMENT;
62 #endif
64 #ifdef ADJUST_FIELD_ALIGN
66 tree field ATTRIBUTE_UNUSED;
67 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL, t);
68 v = ADJUST_FIELD_ALIGN (field, v);
70 #endif
72 return v / BITS_PER_UNIT;
75 /* Return the size and alignment of a trampoline. */
77 void
78 go_trampoline_info (unsigned int *size, unsigned int *alignment)
80 *size = TRAMPOLINE_SIZE;
81 *alignment = TRAMPOLINE_ALIGNMENT;
84 /* This is called by the Go frontend proper if the unsafe package was
85 imported. When that happens we can not do type-based alias
86 analysis. */
88 void
89 go_imported_unsafe (void)
91 flag_strict_aliasing = false;
93 /* Let the backend know that the options have changed. */
94 targetm.override_options_after_change ();
97 /* This is called by the Go frontend proper to add data to the
98 section containing Go export data. */
100 void
101 go_write_export_data (const char *bytes, unsigned int size)
103 static section* sec;
105 if (sec == NULL)
107 gcc_assert (targetm_common.have_named_sections);
108 sec = get_section (GO_EXPORT_SECTION_NAME, SECTION_DEBUG, NULL);
111 switch_to_section (sec);
112 assemble_string (bytes, size);
115 /* The go_read_export_data function is called by the Go frontend
116 proper to read Go export data from an object file. FD is a file
117 descriptor open for reading. OFFSET is the offset within the file
118 where the object file starts; this will be 0 except when reading an
119 archive. On success this returns NULL and sets *PBUF to a buffer
120 allocated using malloc, of size *PLEN, holding the export data. If
121 the data is not found, this returns NULL and sets *PBUF to NULL and
122 *PLEN to 0. If some error occurs, this returns an error message
123 and sets *PERR to an errno value or 0 if there is no relevant
124 errno. */
126 const char *
127 go_read_export_data (int fd, off_t offset, char **pbuf, size_t *plen,
128 int *perr)
130 simple_object_read *sobj;
131 const char *errmsg;
132 off_t sec_offset;
133 off_t sec_length;
134 int found;
135 char *buf;
136 ssize_t c;
138 *pbuf = NULL;
139 *plen = 0;
141 sobj = simple_object_start_read (fd, offset, GO_EXPORT_SEGMENT_NAME,
142 &errmsg, perr);
143 if (sobj == NULL)
145 /* If we get an error here, just pretend that we didn't find any
146 export data. This is the right thing to do if the error is
147 that the file was not recognized as an object file. This
148 will ignore file I/O errors, but it's not too big a deal
149 because we will wind up giving some other error later. */
150 return NULL;
153 found = simple_object_find_section (sobj, GO_EXPORT_SECTION_NAME,
154 &sec_offset, &sec_length,
155 &errmsg, perr);
156 simple_object_release_read (sobj);
157 if (!found)
158 return errmsg;
160 if (lseek (fd, offset + sec_offset, SEEK_SET) < 0)
162 *perr = errno;
163 return _("lseek failed while reading export data");
166 buf = XNEWVEC (char, sec_length);
167 if (buf == NULL)
169 *perr = errno;
170 return _("memory allocation failed while reading export data");
173 c = read (fd, buf, sec_length);
174 if (c < 0)
176 *perr = errno;
177 free (buf);
178 return _("read failed while reading export data");
181 if (c < sec_length)
183 free (buf);
184 return _("short read while reading export data");
187 *pbuf = buf;
188 *plen = sec_length;
190 return NULL;