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