xref: /dflybsd-src/contrib/gcc-8.0/include/simple-object.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* simple-object.h -- simple routines to read and write object files
2*38fd1498Szrj    Copyright (C) 2010-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Written by Ian Lance Taylor, Google.
4*38fd1498Szrj 
5*38fd1498Szrj This program is free software; you can redistribute it and/or modify it
6*38fd1498Szrj under the terms of the GNU General Public License as published by the
7*38fd1498Szrj Free Software Foundation; either version 2, or (at your option) any
8*38fd1498Szrj later version.
9*38fd1498Szrj 
10*38fd1498Szrj This program is distributed in the hope that it will be useful,
11*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
12*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*38fd1498Szrj GNU General Public License for more details.
14*38fd1498Szrj 
15*38fd1498Szrj You should have received a copy of the GNU General Public License
16*38fd1498Szrj along with this program; if not, write to the Free Software
17*38fd1498Szrj Foundation, 51 Franklin Street - Fifth Floor,
18*38fd1498Szrj Boston, MA 02110-1301, USA.  */
19*38fd1498Szrj 
20*38fd1498Szrj #ifndef SIMPLE_OBJECT_H
21*38fd1498Szrj #define SIMPLE_OBJECT_H
22*38fd1498Szrj 
23*38fd1498Szrj #include <stddef.h>
24*38fd1498Szrj #include <sys/types.h>
25*38fd1498Szrj 
26*38fd1498Szrj #ifdef HAVE_UNISTD_H
27*38fd1498Szrj #include <unistd.h>
28*38fd1498Szrj #endif
29*38fd1498Szrj 
30*38fd1498Szrj #ifdef __cplusplus
31*38fd1498Szrj extern "C" {
32*38fd1498Szrj #endif
33*38fd1498Szrj 
34*38fd1498Szrj /* This header file provides four types with associated functions.
35*38fd1498Szrj    They are used to read and write object files.  This is a minimal
36*38fd1498Szrj    interface, intended to support the needs of gcc without bringing in
37*38fd1498Szrj    all the power and complexity of BFD.  */
38*38fd1498Szrj 
39*38fd1498Szrj /* The type simple_object_read * is used to read an existing object
40*38fd1498Szrj    file.  */
41*38fd1498Szrj 
42*38fd1498Szrj typedef struct simple_object_read_struct simple_object_read;
43*38fd1498Szrj 
44*38fd1498Szrj /* Create an simple_object_read given DESCRIPTOR, an open file
45*38fd1498Szrj    descriptor, and OFFSET, an offset within the file.  The offset is
46*38fd1498Szrj    for use with archives, and should be 0 for an ordinary object file.
47*38fd1498Szrj    The descriptor must remain open until done with the returned
48*38fd1498Szrj    simple_object_read.  SEGMENT_NAME is used on Mach-O and is required
49*38fd1498Szrj    on that platform: it means to only look at sections within the
50*38fd1498Szrj    segment with that name.  It is ignored for other object file
51*38fd1498Szrj    formats.  On error, this function returns NULL, and sets *ERRMSG to
52*38fd1498Szrj    an error string and sets *ERR to an errno value or 0 if there is no
53*38fd1498Szrj    relevant errno.  */
54*38fd1498Szrj 
55*38fd1498Szrj extern simple_object_read *
56*38fd1498Szrj simple_object_start_read (int descriptor, off_t offset,
57*38fd1498Szrj 			  const char *segment_name, const char **errmsg,
58*38fd1498Szrj 			  int *err);
59*38fd1498Szrj 
60*38fd1498Szrj /* Call PFN for each section in SIMPLE_OBJECT, passing it the section
61*38fd1498Szrj    name, offset within the file of the section contents, and length of
62*38fd1498Szrj    the section contents.  The offset within the file is relative to
63*38fd1498Szrj    the offset passed to simple_object_start_read.  The DATA argument
64*38fd1498Szrj    to simple_object_find_sections is passed on to PFN.  If PFN returns
65*38fd1498Szrj    0, the loop is stopped and simple_object_find_sections returns.  If
66*38fd1498Szrj    PFN returns non-zero, the loop continues.  On success this returns
67*38fd1498Szrj    NULL.  On error it returns an error string, and sets *ERR to an
68*38fd1498Szrj    errno value or 0 if there is no relevant errno.  */
69*38fd1498Szrj 
70*38fd1498Szrj extern const char *
71*38fd1498Szrj simple_object_find_sections (simple_object_read *simple_object,
72*38fd1498Szrj 			     int (*pfn) (void *data, const char *,
73*38fd1498Szrj 					 off_t offset, off_t length),
74*38fd1498Szrj 			     void *data,
75*38fd1498Szrj 			     int *err);
76*38fd1498Szrj 
77*38fd1498Szrj /* Look for the section NAME in SIMPLE_OBJECT.  This returns
78*38fd1498Szrj    information for the first section NAME in SIMPLE_OBJECT.  Note that
79*38fd1498Szrj    calling this multiple times is inefficient; use
80*38fd1498Szrj    simple_object_find_sections instead.
81*38fd1498Szrj 
82*38fd1498Szrj    If found, return 1 and set *OFFSET to the offset in the file of the
83*38fd1498Szrj    section contents and set *LENGTH to the length of the section
84*38fd1498Szrj    contents.  *OFFSET will be relative to the offset passed to
85*38fd1498Szrj    simple_object_start_read.
86*38fd1498Szrj 
87*38fd1498Szrj    If the section is not found, and no error occurs, return 0 and set
88*38fd1498Szrj    *ERRMSG to NULL.
89*38fd1498Szrj 
90*38fd1498Szrj    If an error occurs, return 0, set *ERRMSG to an error message, and
91*38fd1498Szrj    set *ERR to an errno value or 0 if there is no relevant errno.  */
92*38fd1498Szrj 
93*38fd1498Szrj extern int
94*38fd1498Szrj simple_object_find_section (simple_object_read *simple_object,
95*38fd1498Szrj 			    const char *name, off_t *offset, off_t *length,
96*38fd1498Szrj 			    const char **errmsg, int *err);
97*38fd1498Szrj 
98*38fd1498Szrj /* Release all resources associated with SIMPLE_OBJECT.  This does not
99*38fd1498Szrj    close the file descriptor.  */
100*38fd1498Szrj 
101*38fd1498Szrj extern void
102*38fd1498Szrj simple_object_release_read (simple_object_read *);
103*38fd1498Szrj 
104*38fd1498Szrj /* The type simple_object_attributes holds the attributes of an object
105*38fd1498Szrj    file that matter for creating a file or ensuring that two files are
106*38fd1498Szrj    compatible.  This is a set of magic numbers.  */
107*38fd1498Szrj 
108*38fd1498Szrj typedef struct simple_object_attributes_struct simple_object_attributes;
109*38fd1498Szrj 
110*38fd1498Szrj /* Fetch the attributes of SIMPLE_OBJECT.  This information will
111*38fd1498Szrj    persist until simple_object_attributes_release is called, even if
112*38fd1498Szrj    SIMPLE_OBJECT is closed.  On error this returns NULL, sets *ERRMSG
113*38fd1498Szrj    to an error message, and sets *ERR to an errno value or 0 if there
114*38fd1498Szrj    isn't one.  */
115*38fd1498Szrj 
116*38fd1498Szrj extern simple_object_attributes *
117*38fd1498Szrj simple_object_fetch_attributes (simple_object_read *simple_object,
118*38fd1498Szrj 				const char **errmsg, int *err);
119*38fd1498Szrj 
120*38fd1498Szrj /* Merge the FROM attributes into TO.  If two objects with these
121*38fd1498Szrj    attributes could be linked together without error, returns NULL.
122*38fd1498Szrj    Otherwise, returns an error message, and sets *ERR to an errno
123*38fd1498Szrj    value or 0 if there isn't one.  */
124*38fd1498Szrj 
125*38fd1498Szrj extern const char *
126*38fd1498Szrj simple_object_attributes_merge (simple_object_attributes *to,
127*38fd1498Szrj 				simple_object_attributes *from,
128*38fd1498Szrj 				int *err);
129*38fd1498Szrj 
130*38fd1498Szrj /* Release all resources associated with ATTRS.  */
131*38fd1498Szrj 
132*38fd1498Szrj extern void
133*38fd1498Szrj simple_object_release_attributes (simple_object_attributes *attrs);
134*38fd1498Szrj 
135*38fd1498Szrj /* The type simple_object_write is used to create a new object file.  */
136*38fd1498Szrj 
137*38fd1498Szrj typedef struct simple_object_write_struct simple_object_write;
138*38fd1498Szrj 
139*38fd1498Szrj /* Start creating a new object file which is like ATTRS.  You must
140*38fd1498Szrj    fetch attribute information from an existing object file before you
141*38fd1498Szrj    can create a new one.  There is currently no support for creating
142*38fd1498Szrj    an object file de novo.  The segment name is only used on Mach-O,
143*38fd1498Szrj    where it is required.  It means that all sections are created
144*38fd1498Szrj    within that segment.  It is ignored for other object file formats.
145*38fd1498Szrj    On error this function returns NULL, sets *ERRMSG to an error
146*38fd1498Szrj    message, and sets *ERR to an errno value or 0 if there isn't
147*38fd1498Szrj    one.  */
148*38fd1498Szrj 
149*38fd1498Szrj extern simple_object_write *
150*38fd1498Szrj simple_object_start_write (simple_object_attributes *attrs,
151*38fd1498Szrj 			   const char *segment_name,
152*38fd1498Szrj 			   const char **errmsg, int *err);
153*38fd1498Szrj 
154*38fd1498Szrj /* The type simple_object_write_section is a handle for a section
155*38fd1498Szrj    which is being written.  */
156*38fd1498Szrj 
157*38fd1498Szrj typedef struct simple_object_write_section_struct simple_object_write_section;
158*38fd1498Szrj 
159*38fd1498Szrj /* Add a section to SIMPLE_OBJECT.  NAME is the name of the new
160*38fd1498Szrj    section.  ALIGN is the required alignment expressed as the number
161*38fd1498Szrj    of required low-order 0 bits (e.g., 2 for alignment to a 32-bit
162*38fd1498Szrj    boundary).  The section is created as containing data, readable,
163*38fd1498Szrj    not writable, not executable, not loaded at runtime.  On error this
164*38fd1498Szrj    returns NULL, sets *ERRMSG to an error message, and sets *ERR to an
165*38fd1498Szrj    errno value or 0 if there isn't one.  */
166*38fd1498Szrj 
167*38fd1498Szrj extern simple_object_write_section *
168*38fd1498Szrj simple_object_write_create_section (simple_object_write *simple_object,
169*38fd1498Szrj 				    const char *name, unsigned int align,
170*38fd1498Szrj 				    const char **errmsg, int *err);
171*38fd1498Szrj 
172*38fd1498Szrj /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT.  If COPY is
173*38fd1498Szrj    non-zero, the data will be copied into memory if necessary.  If
174*38fd1498Szrj    COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released.
175*38fd1498Szrj    On success this returns NULL.  On error this returns an error
176*38fd1498Szrj    message, and sets *ERR to an errno value or 0 if there isn't
177*38fd1498Szrj    one.  */
178*38fd1498Szrj 
179*38fd1498Szrj extern const char *
180*38fd1498Szrj simple_object_write_add_data (simple_object_write *simple_object,
181*38fd1498Szrj 			      simple_object_write_section *section,
182*38fd1498Szrj 			      const void *buffer, size_t size,
183*38fd1498Szrj 			      int copy, int *err);
184*38fd1498Szrj 
185*38fd1498Szrj /* Write the complete object file to DESCRIPTOR, an open file
186*38fd1498Szrj    descriptor.  This returns NULL on success.  On error this returns
187*38fd1498Szrj    an error message, and sets *ERR to an errno value or 0 if there
188*38fd1498Szrj    isn't one.  */
189*38fd1498Szrj 
190*38fd1498Szrj extern const char *
191*38fd1498Szrj simple_object_write_to_file (simple_object_write *simple_object,
192*38fd1498Szrj 			     int descriptor, int *err);
193*38fd1498Szrj 
194*38fd1498Szrj /* Release all resources associated with SIMPLE_OBJECT, including any
195*38fd1498Szrj    simple_object_write_section's that may have been created.  */
196*38fd1498Szrj 
197*38fd1498Szrj extern void
198*38fd1498Szrj simple_object_release_write (simple_object_write *);
199*38fd1498Szrj 
200*38fd1498Szrj /* Copy LTO debug sections from SRC_OBJECT to DEST.
201*38fd1498Szrj    If an error occurs, return the errno value in ERR and an error string.  */
202*38fd1498Szrj 
203*38fd1498Szrj extern const char *
204*38fd1498Szrj simple_object_copy_lto_debug_sections (simple_object_read *src_object,
205*38fd1498Szrj 				       const char *dest,
206*38fd1498Szrj 				       int *err);
207*38fd1498Szrj 
208*38fd1498Szrj #ifdef __cplusplus
209*38fd1498Szrj }
210*38fd1498Szrj #endif
211*38fd1498Szrj 
212*38fd1498Szrj #endif
213