1 /* Copyright (C) 2010-2024 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License
16 along with GCC; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
19 #include "rust-system.h"
20 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "simple-object.h"
27 #include "stor-layout.h"
29 #include "output.h" /* for assemble_string */
30 #include "common/common-target.h"
32 // satisfy intellisense
35 /* The segment name we pass to simple_object_start_read to find Rust
38 #ifndef RUST_EXPORT_SEGMENT_NAME
39 #define RUST_EXPORT_SEGMENT_NAME "__GNU_RUST"
42 /* The section name we use when reading and writing export data. */
44 #ifndef RUST_EXPORT_SECTION_NAME
45 #define RUST_EXPORT_SECTION_NAME ".rust_export"
49 #define TARGET_AIX_OS 0
52 /* Return whether or not GCC has reported any errors. */
57 return errorcount
!= 0 || sorrycount
!= 0;
60 /* Return the alignment in bytes of a struct field of type T. */
63 rust_field_alignment (tree t
)
69 #ifdef BIGGEST_FIELD_ALIGNMENT
70 if (v
> BIGGEST_FIELD_ALIGNMENT
)
71 v
= BIGGEST_FIELD_ALIGNMENT
;
74 #ifdef ADJUST_FIELD_ALIGN
75 v
= ADJUST_FIELD_ALIGN (NULL_TREE
, t
, v
);
78 return v
/ BITS_PER_UNIT
;
81 /* This is called by the Rust frontend proper to add data to the
82 section containing Rust export data. */
85 rust_write_export_data (const char *bytes
, unsigned int size
)
91 gcc_assert (targetm_common
.have_named_sections
);
92 sec
= get_section (RUST_EXPORT_SECTION_NAME
,
93 TARGET_AIX_OS
? SECTION_EXCLUDE
: SECTION_DEBUG
, NULL
);
96 switch_to_section (sec
);
97 assemble_string (bytes
, size
);
100 /* The rust_read_export_data function is called by the Rust frontend
101 proper to read Rust export data from an object file. FD is a file
102 descriptor open for reading. OFFSET is the offset within the file
103 where the object file starts; this will be 0 except when reading an
104 archive. On success this returns NULL and sets *PBUF to a buffer
105 allocated using malloc, of size *PLEN, holding the export data. If
106 the data is not found, this returns NULL and sets *PBUF to NULL and
107 *PLEN to 0. If some error occurs, this returns an error message
108 and sets *PERR to an errno value or 0 if there is no relevant
112 rust_read_export_data (int fd
, off_t offset
, char **pbuf
, size_t *plen
,
115 simple_object_read
*sobj
;
126 sobj
= simple_object_start_read (fd
, offset
, RUST_EXPORT_SEGMENT_NAME
,
130 /* If we get an error here, just pretend that we didn't find any
131 export data. This is the right thing to do if the error is
132 that the file was not recognized as an object file. This
133 will ignore file I/O errors, but it's not too big a deal
134 because we will wind up giving some other error later. */
138 found
= simple_object_find_section (sobj
, RUST_EXPORT_SECTION_NAME
,
139 &sec_offset
, &sec_length
, &errmsg
, perr
);
140 simple_object_release_read (sobj
);
144 if (lseek (fd
, offset
+ sec_offset
, SEEK_SET
) < 0)
147 return _ ("lseek failed while reading export data");
150 buf
= XNEWVEC (char, sec_length
);
154 return _ ("memory allocation failed while reading export data");
157 c
= read (fd
, buf
, sec_length
);
162 return _ ("read failed while reading export data");
168 return _ ("short read while reading export data");