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