1 /* rust-backend.c -- Rust frontend interface to gcc backend.
2 Copyright (C) 2010-2024 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
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
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 "rust-system.h"
21 #include "coretypes.h"
26 #include "diagnostic.h"
27 #include "simple-object.h"
28 #include "stor-layout.h"
30 #include "output.h" /* for assemble_string */
31 #include "common/common-target.h"
33 // satisfy intellisense
36 /* The segment name we pass to simple_object_start_read to find Rust
39 #ifndef RUST_EXPORT_SEGMENT_NAME
40 #define RUST_EXPORT_SEGMENT_NAME "__GNU_RUST"
43 /* The section name we use when reading and writing export data. */
45 #ifndef RUST_EXPORT_SECTION_NAME
46 #define RUST_EXPORT_SECTION_NAME ".rust_export"
50 #define TARGET_AIX_OS 0
53 /* Return whether or not GCC has reported any errors. */
58 return errorcount
!= 0 || sorrycount
!= 0;
61 /* Return the alignment in bytes of a struct field of type T. */
64 rust_field_alignment (tree t
)
70 #ifdef BIGGEST_FIELD_ALIGNMENT
71 if (v
> BIGGEST_FIELD_ALIGNMENT
)
72 v
= BIGGEST_FIELD_ALIGNMENT
;
75 #ifdef ADJUST_FIELD_ALIGN
76 v
= ADJUST_FIELD_ALIGN (NULL_TREE
, t
, v
);
79 return v
/ BITS_PER_UNIT
;
82 /* This is called by the Rust frontend proper to add data to the
83 section containing Rust export data. */
86 rust_write_export_data (const char *bytes
, unsigned int size
)
92 gcc_assert (targetm_common
.have_named_sections
);
93 sec
= get_section (RUST_EXPORT_SECTION_NAME
,
94 TARGET_AIX_OS
? SECTION_EXCLUDE
: SECTION_DEBUG
, NULL
);
97 switch_to_section (sec
);
98 assemble_string (bytes
, size
);
101 /* The rust_read_export_data function is called by the Rust frontend
102 proper to read Rust export data from an object file. FD is a file
103 descriptor open for reading. OFFSET is the offset within the file
104 where the object file starts; this will be 0 except when reading an
105 archive. On success this returns NULL and sets *PBUF to a buffer
106 allocated using malloc, of size *PLEN, holding the export data. If
107 the data is not found, this returns NULL and sets *PBUF to NULL and
108 *PLEN to 0. If some error occurs, this returns an error message
109 and sets *PERR to an errno value or 0 if there is no relevant
113 rust_read_export_data (int fd
, off_t offset
, char **pbuf
, size_t *plen
,
116 simple_object_read
*sobj
;
127 sobj
= simple_object_start_read (fd
, offset
, RUST_EXPORT_SEGMENT_NAME
,
131 /* If we get an error here, just pretend that we didn't find any
132 export data. This is the right thing to do if the error is
133 that the file was not recognized as an object file. This
134 will ignore file I/O errors, but it's not too big a deal
135 because we will wind up giving some other error later. */
139 found
= simple_object_find_section (sobj
, RUST_EXPORT_SECTION_NAME
,
140 &sec_offset
, &sec_length
, &errmsg
, perr
);
141 simple_object_release_read (sobj
);
145 if (lseek (fd
, offset
+ sec_offset
, SEEK_SET
) < 0)
148 return _ ("lseek failed while reading export data");
151 buf
= XNEWVEC (char, sec_length
);
155 return _ ("memory allocation failed while reading export data");
158 c
= read (fd
, buf
, sec_length
);
163 return _ ("read failed while reading export data");
169 return _ ("short read while reading export data");