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