1 /* go-backend.c -- Go frontend interface to gcc backend.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "tree.h"
25 #include "tm_p.h"
26 #include "diagnostic.h"
27 #include "simple-object.h"
28 #include "stor-layout.h"
29 #include "intl.h"
30 #include "output.h" /* for assemble_string */
31 #include "common/common-target.h"
32
33
34 /* The segment name we pass to simple_object_start_read to find Go
35 export data. */
36
37 #ifndef GO_EXPORT_SEGMENT_NAME
38 #define GO_EXPORT_SEGMENT_NAME "__GNU_GO"
39 #endif
40
41 /* The section name we use when reading and writing export data. */
42
43 #ifndef GO_EXPORT_SECTION_NAME
44 #define GO_EXPORT_SECTION_NAME ".go_export"
45 #endif
46
47 /* This file holds all the cases where the Go frontend needs
48 information from gcc's backend. */
49
50 /* Return whether or not GCC has reported any errors. */
51
52 bool
saw_errors(void)53 saw_errors (void)
54 {
55 return errorcount != 0 || sorrycount != 0;
56 }
57
58 /* Return the alignment in bytes of a struct field of type T. */
59
60 unsigned int
go_field_alignment(tree t)61 go_field_alignment (tree t)
62 {
63 unsigned int v;
64
65 v = TYPE_ALIGN (t);
66
67 #ifdef BIGGEST_FIELD_ALIGNMENT
68 if (v > BIGGEST_FIELD_ALIGNMENT)
69 v = BIGGEST_FIELD_ALIGNMENT;
70 #endif
71
72 #ifdef ADJUST_FIELD_ALIGN
73 {
74 tree field ATTRIBUTE_UNUSED;
75 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL, t);
76 v = ADJUST_FIELD_ALIGN (field, v);
77 }
78 #endif
79
80 return v / BITS_PER_UNIT;
81 }
82
83 /* Return the size and alignment of a trampoline. */
84
85 void
go_trampoline_info(unsigned int * size,unsigned int * alignment)86 go_trampoline_info (unsigned int *size, unsigned int *alignment)
87 {
88 *size = TRAMPOLINE_SIZE;
89 *alignment = TRAMPOLINE_ALIGNMENT;
90 }
91
92 /* This is called by the Go frontend proper if the unsafe package was
93 imported. When that happens we can not do type-based alias
94 analysis. */
95
96 void
go_imported_unsafe(void)97 go_imported_unsafe (void)
98 {
99 flag_strict_aliasing = false;
100
101 /* Let the backend know that the options have changed. */
102 targetm.override_options_after_change ();
103 }
104
105 /* This is called by the Go frontend proper to add data to the
106 section containing Go export data. */
107
108 void
go_write_export_data(const char * bytes,unsigned int size)109 go_write_export_data (const char *bytes, unsigned int size)
110 {
111 static section* sec;
112
113 if (sec == NULL)
114 {
115 gcc_assert (targetm_common.have_named_sections);
116 sec = get_section (GO_EXPORT_SECTION_NAME, SECTION_DEBUG, NULL);
117 }
118
119 switch_to_section (sec);
120 assemble_string (bytes, size);
121 }
122
123 /* The go_read_export_data function is called by the Go frontend
124 proper to read Go export data from an object file. FD is a file
125 descriptor open for reading. OFFSET is the offset within the file
126 where the object file starts; this will be 0 except when reading an
127 archive. On success this returns NULL and sets *PBUF to a buffer
128 allocated using malloc, of size *PLEN, holding the export data. If
129 the data is not found, this returns NULL and sets *PBUF to NULL and
130 *PLEN to 0. If some error occurs, this returns an error message
131 and sets *PERR to an errno value or 0 if there is no relevant
132 errno. */
133
134 const char *
go_read_export_data(int fd,off_t offset,char ** pbuf,size_t * plen,int * perr)135 go_read_export_data (int fd, off_t offset, char **pbuf, size_t *plen,
136 int *perr)
137 {
138 simple_object_read *sobj;
139 const char *errmsg;
140 off_t sec_offset;
141 off_t sec_length;
142 int found;
143 char *buf;
144 ssize_t c;
145
146 *pbuf = NULL;
147 *plen = 0;
148
149 sobj = simple_object_start_read (fd, offset, GO_EXPORT_SEGMENT_NAME,
150 &errmsg, perr);
151 if (sobj == NULL)
152 {
153 /* If we get an error here, just pretend that we didn't find any
154 export data. This is the right thing to do if the error is
155 that the file was not recognized as an object file. This
156 will ignore file I/O errors, but it's not too big a deal
157 because we will wind up giving some other error later. */
158 return NULL;
159 }
160
161 found = simple_object_find_section (sobj, GO_EXPORT_SECTION_NAME,
162 &sec_offset, &sec_length,
163 &errmsg, perr);
164 simple_object_release_read (sobj);
165 if (!found)
166 return errmsg;
167
168 if (lseek (fd, offset + sec_offset, SEEK_SET) < 0)
169 {
170 *perr = errno;
171 return _("lseek failed while reading export data");
172 }
173
174 buf = XNEWVEC (char, sec_length);
175 if (buf == NULL)
176 {
177 *perr = errno;
178 return _("memory allocation failed while reading export data");
179 }
180
181 c = read (fd, buf, sec_length);
182 if (c < 0)
183 {
184 *perr = errno;
185 free (buf);
186 return _("read failed while reading export data");
187 }
188
189 if (c < sec_length)
190 {
191 free (buf);
192 return _("short read while reading export data");
193 }
194
195 *pbuf = buf;
196 *plen = sec_length;
197
198 return NULL;
199 }
200