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