go-backend.c 5.3 KB

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