xref: /dflybsd-src/contrib/gdb-7/include/simple-object.h (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* simple-object.h -- simple routines to read and write object files
286d7f5d3SJohn Marino    Copyright 2010 Free Software Foundation, Inc.
386d7f5d3SJohn Marino    Written by Ian Lance Taylor, Google.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino This program is free software; you can redistribute it and/or modify it
686d7f5d3SJohn Marino under the terms of the GNU General Public License as published by the
786d7f5d3SJohn Marino Free Software Foundation; either version 2, or (at your option) any
886d7f5d3SJohn Marino later version.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino This program is distributed in the hope that it will be useful,
1186d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
1286d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1386d7f5d3SJohn Marino GNU General Public License for more details.
1486d7f5d3SJohn Marino 
1586d7f5d3SJohn Marino You should have received a copy of the GNU General Public License
1686d7f5d3SJohn Marino along with this program; if not, write to the Free Software
1786d7f5d3SJohn Marino Foundation, 51 Franklin Street - Fifth Floor,
1886d7f5d3SJohn Marino Boston, MA 02110-1301, USA.  */
1986d7f5d3SJohn Marino 
2086d7f5d3SJohn Marino #ifndef SIMPLE_OBJECT_H
2186d7f5d3SJohn Marino #define SIMPLE_OBJECT_H
2286d7f5d3SJohn Marino 
2386d7f5d3SJohn Marino #include <stddef.h>
2486d7f5d3SJohn Marino #include <sys/types.h>
2586d7f5d3SJohn Marino 
2686d7f5d3SJohn Marino #ifdef HAVE_UNISTD_H
2786d7f5d3SJohn Marino #include <unistd.h>
2886d7f5d3SJohn Marino #endif
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino #ifdef __cplusplus
3186d7f5d3SJohn Marino extern "C" {
3286d7f5d3SJohn Marino #endif
3386d7f5d3SJohn Marino 
3486d7f5d3SJohn Marino /* This header file provides four types with associated functions.
3586d7f5d3SJohn Marino    They are used to read and write object files.  This is a minimal
3686d7f5d3SJohn Marino    interface, intended to support the needs of gcc without bringing in
3786d7f5d3SJohn Marino    all the power and complexity of BFD.  */
3886d7f5d3SJohn Marino 
3986d7f5d3SJohn Marino /* The type simple_object_read * is used to read an existing object
4086d7f5d3SJohn Marino    file.  */
4186d7f5d3SJohn Marino 
4286d7f5d3SJohn Marino typedef struct simple_object_read_struct simple_object_read;
4386d7f5d3SJohn Marino 
4486d7f5d3SJohn Marino /* Create an simple_object_read given DESCRIPTOR, an open file
4586d7f5d3SJohn Marino    descriptor, and OFFSET, an offset within the file.  The offset is
4686d7f5d3SJohn Marino    for use with archives, and should be 0 for an ordinary object file.
4786d7f5d3SJohn Marino    The descriptor must remain open until done with the returned
4886d7f5d3SJohn Marino    simple_object_read.  SEGMENT_NAME is used on Mach-O and is required
4986d7f5d3SJohn Marino    on that platform: it means to only look at sections within the
5086d7f5d3SJohn Marino    segment with that name.  It is ignored for other object file
5186d7f5d3SJohn Marino    formats.  On error, this function returns NULL, and sets *ERRMSG to
5286d7f5d3SJohn Marino    an error string and sets *ERR to an errno value or 0 if there is no
5386d7f5d3SJohn Marino    relevant errno.  */
5486d7f5d3SJohn Marino 
5586d7f5d3SJohn Marino extern simple_object_read *
5686d7f5d3SJohn Marino simple_object_start_read (int descriptor, off_t offset,
5786d7f5d3SJohn Marino 			  const char *segment_name, const char **errmsg,
5886d7f5d3SJohn Marino 			  int *err);
5986d7f5d3SJohn Marino 
6086d7f5d3SJohn Marino /* Call PFN for each section in SIMPLE_OBJECT, passing it the section
6186d7f5d3SJohn Marino    name, offset within the file of the section contents, and length of
6286d7f5d3SJohn Marino    the section contents.  The offset within the file is relative to
6386d7f5d3SJohn Marino    the offset passed to simple_object_start_read.  The DATA argument
6486d7f5d3SJohn Marino    to simple_object_find_sections is passed on to PFN.  If PFN returns
6586d7f5d3SJohn Marino    0, the loop is stopped and simple_object_find_sections returns.  If
6686d7f5d3SJohn Marino    PFN returns non-zero, the loop continues.  On success this returns
6786d7f5d3SJohn Marino    NULL.  On error it returns an error string, and sets *ERR to an
6886d7f5d3SJohn Marino    errno value or 0 if there is no relevant errno.  */
6986d7f5d3SJohn Marino 
7086d7f5d3SJohn Marino extern const char *
7186d7f5d3SJohn Marino simple_object_find_sections (simple_object_read *simple_object,
7286d7f5d3SJohn Marino 			     int (*pfn) (void *data, const char *,
7386d7f5d3SJohn Marino 					 off_t offset, off_t length),
7486d7f5d3SJohn Marino 			     void *data,
7586d7f5d3SJohn Marino 			     int *err);
7686d7f5d3SJohn Marino 
7786d7f5d3SJohn Marino /* Look for the section NAME in SIMPLE_OBJECT.  This returns
7886d7f5d3SJohn Marino    information for the first section NAME in SIMPLE_OBJECT.  Note that
7986d7f5d3SJohn Marino    calling this multiple times is inefficient; use
8086d7f5d3SJohn Marino    simple_object_find_sections instead.
8186d7f5d3SJohn Marino 
8286d7f5d3SJohn Marino    If found, return 1 and set *OFFSET to the offset in the file of the
8386d7f5d3SJohn Marino    section contents and set *LENGTH to the length of the section
8486d7f5d3SJohn Marino    contents.  *OFFSET will be relative to the offset passed to
8586d7f5d3SJohn Marino    simple_object_start_read.
8686d7f5d3SJohn Marino 
8786d7f5d3SJohn Marino    If the section is not found, and no error occurs, return 0 and set
8886d7f5d3SJohn Marino    *ERRMSG to NULL.
8986d7f5d3SJohn Marino 
9086d7f5d3SJohn Marino    If an error occurs, return 0, set *ERRMSG to an error message, and
9186d7f5d3SJohn Marino    set *ERR to an errno value or 0 if there is no relevant errno.  */
9286d7f5d3SJohn Marino 
9386d7f5d3SJohn Marino extern int
9486d7f5d3SJohn Marino simple_object_find_section (simple_object_read *simple_object,
9586d7f5d3SJohn Marino 			    const char *name, off_t *offset, off_t *length,
9686d7f5d3SJohn Marino 			    const char **errmsg, int *err);
9786d7f5d3SJohn Marino 
9886d7f5d3SJohn Marino /* Release all resources associated with SIMPLE_OBJECT.  This does not
9986d7f5d3SJohn Marino    close the file descriptor.  */
10086d7f5d3SJohn Marino 
10186d7f5d3SJohn Marino extern void
10286d7f5d3SJohn Marino simple_object_release_read (simple_object_read *);
10386d7f5d3SJohn Marino 
10486d7f5d3SJohn Marino /* The type simple_object_attributes holds the attributes of an object
10586d7f5d3SJohn Marino    file that matter for creating a file or ensuring that two files are
10686d7f5d3SJohn Marino    compatible.  This is a set of magic numbers.  */
10786d7f5d3SJohn Marino 
10886d7f5d3SJohn Marino typedef struct simple_object_attributes_struct simple_object_attributes;
10986d7f5d3SJohn Marino 
11086d7f5d3SJohn Marino /* Fetch the attributes of SIMPLE_OBJECT.  This information will
11186d7f5d3SJohn Marino    persist until simple_object_attributes_release is called, even if
11286d7f5d3SJohn Marino    SIMPLE_OBJECT is closed.  On error this returns NULL, sets *ERRMSG
11386d7f5d3SJohn Marino    to an error message, and sets *ERR to an errno value or 0 if there
11486d7f5d3SJohn Marino    isn't one.  */
11586d7f5d3SJohn Marino 
11686d7f5d3SJohn Marino extern simple_object_attributes *
11786d7f5d3SJohn Marino simple_object_fetch_attributes (simple_object_read *simple_object,
11886d7f5d3SJohn Marino 				const char **errmsg, int *err);
11986d7f5d3SJohn Marino 
12086d7f5d3SJohn Marino /* Merge the FROM attributes into TO.  If two objects with these
12186d7f5d3SJohn Marino    attributes could be linked together without error, returns NULL.
12286d7f5d3SJohn Marino    Otherwise, returns an error message, and sets *ERR to an errno
12386d7f5d3SJohn Marino    value or 0 if there isn't one.  */
12486d7f5d3SJohn Marino 
12586d7f5d3SJohn Marino extern const char *
12686d7f5d3SJohn Marino simple_object_attributes_merge (simple_object_attributes *to,
12786d7f5d3SJohn Marino 				simple_object_attributes *from,
12886d7f5d3SJohn Marino 				int *err);
12986d7f5d3SJohn Marino 
13086d7f5d3SJohn Marino /* Release all resources associated with ATTRS.  */
13186d7f5d3SJohn Marino 
13286d7f5d3SJohn Marino extern void
13386d7f5d3SJohn Marino simple_object_release_attributes (simple_object_attributes *attrs);
13486d7f5d3SJohn Marino 
13586d7f5d3SJohn Marino /* The type simple_object_write is used to create a new object file.  */
13686d7f5d3SJohn Marino 
13786d7f5d3SJohn Marino typedef struct simple_object_write_struct simple_object_write;
13886d7f5d3SJohn Marino 
13986d7f5d3SJohn Marino /* Start creating a new object file which is like ATTRS.  You must
14086d7f5d3SJohn Marino    fetch attribute information from an existing object file before you
14186d7f5d3SJohn Marino    can create a new one.  There is currently no support for creating
14286d7f5d3SJohn Marino    an object file de novo.  The segment name is only used on Mach-O,
14386d7f5d3SJohn Marino    where it is required.  It means that all sections are created
14486d7f5d3SJohn Marino    within that segment.  It is ignored for other object file formats.
14586d7f5d3SJohn Marino    On error this function returns NULL, sets *ERRMSG to an error
14686d7f5d3SJohn Marino    message, and sets *ERR to an errno value or 0 if there isn't
14786d7f5d3SJohn Marino    one.  */
14886d7f5d3SJohn Marino 
14986d7f5d3SJohn Marino extern simple_object_write *
15086d7f5d3SJohn Marino simple_object_start_write (simple_object_attributes *attrs,
15186d7f5d3SJohn Marino 			   const char *segment_name,
15286d7f5d3SJohn Marino 			   const char **errmsg, int *err);
15386d7f5d3SJohn Marino 
15486d7f5d3SJohn Marino /* The type simple_object_write_section is a handle for a section
15586d7f5d3SJohn Marino    which is being written.  */
15686d7f5d3SJohn Marino 
15786d7f5d3SJohn Marino typedef struct simple_object_write_section_struct simple_object_write_section;
15886d7f5d3SJohn Marino 
15986d7f5d3SJohn Marino /* Add a section to SIMPLE_OBJECT.  NAME is the name of the new
16086d7f5d3SJohn Marino    section.  ALIGN is the required alignment expressed as the number
16186d7f5d3SJohn Marino    of required low-order 0 bits (e.g., 2 for alignment to a 32-bit
16286d7f5d3SJohn Marino    boundary).  The section is created as containing data, readable,
16386d7f5d3SJohn Marino    not writable, not executable, not loaded at runtime.  On error this
16486d7f5d3SJohn Marino    returns NULL, sets *ERRMSG to an error message, and sets *ERR to an
16586d7f5d3SJohn Marino    errno value or 0 if there isn't one.  */
16686d7f5d3SJohn Marino 
16786d7f5d3SJohn Marino extern simple_object_write_section *
16886d7f5d3SJohn Marino simple_object_write_create_section (simple_object_write *simple_object,
16986d7f5d3SJohn Marino 				    const char *name, unsigned int align,
17086d7f5d3SJohn Marino 				    const char **errmsg, int *err);
17186d7f5d3SJohn Marino 
17286d7f5d3SJohn Marino /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT.  If COPY is
17386d7f5d3SJohn Marino    non-zero, the data will be copied into memory if necessary.  If
17486d7f5d3SJohn Marino    COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released.
17586d7f5d3SJohn Marino    On success this returns NULL.  On error this returns an error
17686d7f5d3SJohn Marino    message, and sets *ERR to an errno value or 0 if there isn't
17786d7f5d3SJohn Marino    one.  */
17886d7f5d3SJohn Marino 
17986d7f5d3SJohn Marino extern const char *
18086d7f5d3SJohn Marino simple_object_write_add_data (simple_object_write *simple_object,
18186d7f5d3SJohn Marino 			      simple_object_write_section *section,
18286d7f5d3SJohn Marino 			      const void *buffer, size_t size,
18386d7f5d3SJohn Marino 			      int copy, int *err);
18486d7f5d3SJohn Marino 
18586d7f5d3SJohn Marino /* Write the complete object file to DESCRIPTOR, an open file
18686d7f5d3SJohn Marino    descriptor.  This returns NULL on success.  On error this returns
18786d7f5d3SJohn Marino    an error message, and sets *ERR to an errno value or 0 if there
18886d7f5d3SJohn Marino    isn't one.  */
18986d7f5d3SJohn Marino 
19086d7f5d3SJohn Marino extern const char *
19186d7f5d3SJohn Marino simple_object_write_to_file (simple_object_write *simple_object,
19286d7f5d3SJohn Marino 			     int descriptor, int *err);
19386d7f5d3SJohn Marino 
19486d7f5d3SJohn Marino /* Release all resources associated with SIMPLE_OBJECT, including any
19586d7f5d3SJohn Marino    simple_object_write_section's that may have been created.  */
19686d7f5d3SJohn Marino 
19786d7f5d3SJohn Marino extern void
19886d7f5d3SJohn Marino simple_object_release_write (simple_object_write *);
19986d7f5d3SJohn Marino 
20086d7f5d3SJohn Marino #ifdef __cplusplus
20186d7f5d3SJohn Marino }
20286d7f5d3SJohn Marino #endif
20386d7f5d3SJohn Marino 
20486d7f5d3SJohn Marino #endif
205