xref: /netbsd-src/external/gpl3/binutils.old/dist/include/simple-object.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* simple-object.h -- simple routines to read and write object files
2*e992f068Schristos    Copyright (C) 2010-2022 Free Software Foundation, Inc.
375fd0b74Schristos    Written by Ian Lance Taylor, Google.
475fd0b74Schristos 
575fd0b74Schristos This program is free software; you can redistribute it and/or modify it
675fd0b74Schristos under the terms of the GNU General Public License as published by the
775fd0b74Schristos Free Software Foundation; either version 2, or (at your option) any
875fd0b74Schristos later version.
975fd0b74Schristos 
1075fd0b74Schristos This program is distributed in the hope that it will be useful,
1175fd0b74Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1275fd0b74Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1375fd0b74Schristos GNU General Public License for more details.
1475fd0b74Schristos 
1575fd0b74Schristos You should have received a copy of the GNU General Public License
1675fd0b74Schristos along with this program; if not, write to the Free Software
1775fd0b74Schristos Foundation, 51 Franklin Street - Fifth Floor,
1875fd0b74Schristos Boston, MA 02110-1301, USA.  */
1975fd0b74Schristos 
2075fd0b74Schristos #ifndef SIMPLE_OBJECT_H
2175fd0b74Schristos #define SIMPLE_OBJECT_H
2275fd0b74Schristos 
2375fd0b74Schristos #include <stddef.h>
2475fd0b74Schristos #include <sys/types.h>
2575fd0b74Schristos 
2675fd0b74Schristos #ifdef HAVE_UNISTD_H
2775fd0b74Schristos #include <unistd.h>
2875fd0b74Schristos #endif
2975fd0b74Schristos 
3075fd0b74Schristos #ifdef __cplusplus
3175fd0b74Schristos extern "C" {
3275fd0b74Schristos #endif
3375fd0b74Schristos 
3475fd0b74Schristos /* This header file provides four types with associated functions.
3575fd0b74Schristos    They are used to read and write object files.  This is a minimal
3675fd0b74Schristos    interface, intended to support the needs of gcc without bringing in
3775fd0b74Schristos    all the power and complexity of BFD.  */
3875fd0b74Schristos 
3975fd0b74Schristos /* The type simple_object_read * is used to read an existing object
4075fd0b74Schristos    file.  */
4175fd0b74Schristos 
4275fd0b74Schristos typedef struct simple_object_read_struct simple_object_read;
4375fd0b74Schristos 
4475fd0b74Schristos /* Create an simple_object_read given DESCRIPTOR, an open file
4575fd0b74Schristos    descriptor, and OFFSET, an offset within the file.  The offset is
4675fd0b74Schristos    for use with archives, and should be 0 for an ordinary object file.
4775fd0b74Schristos    The descriptor must remain open until done with the returned
4875fd0b74Schristos    simple_object_read.  SEGMENT_NAME is used on Mach-O and is required
4975fd0b74Schristos    on that platform: it means to only look at sections within the
5075fd0b74Schristos    segment with that name.  It is ignored for other object file
5175fd0b74Schristos    formats.  On error, this function returns NULL, and sets *ERRMSG to
5275fd0b74Schristos    an error string and sets *ERR to an errno value or 0 if there is no
5375fd0b74Schristos    relevant errno.  */
5475fd0b74Schristos 
5575fd0b74Schristos extern simple_object_read *
5675fd0b74Schristos simple_object_start_read (int descriptor, off_t offset,
5775fd0b74Schristos 			  const char *segment_name, const char **errmsg,
5875fd0b74Schristos 			  int *err);
5975fd0b74Schristos 
6075fd0b74Schristos /* Call PFN for each section in SIMPLE_OBJECT, passing it the section
6175fd0b74Schristos    name, offset within the file of the section contents, and length of
6275fd0b74Schristos    the section contents.  The offset within the file is relative to
6375fd0b74Schristos    the offset passed to simple_object_start_read.  The DATA argument
6475fd0b74Schristos    to simple_object_find_sections is passed on to PFN.  If PFN returns
6575fd0b74Schristos    0, the loop is stopped and simple_object_find_sections returns.  If
6675fd0b74Schristos    PFN returns non-zero, the loop continues.  On success this returns
6775fd0b74Schristos    NULL.  On error it returns an error string, and sets *ERR to an
6875fd0b74Schristos    errno value or 0 if there is no relevant errno.  */
6975fd0b74Schristos 
7075fd0b74Schristos extern const char *
7175fd0b74Schristos simple_object_find_sections (simple_object_read *simple_object,
7275fd0b74Schristos 			     int (*pfn) (void *data, const char *,
7375fd0b74Schristos 					 off_t offset, off_t length),
7475fd0b74Schristos 			     void *data,
7575fd0b74Schristos 			     int *err);
7675fd0b74Schristos 
7775fd0b74Schristos /* Look for the section NAME in SIMPLE_OBJECT.  This returns
7875fd0b74Schristos    information for the first section NAME in SIMPLE_OBJECT.  Note that
7975fd0b74Schristos    calling this multiple times is inefficient; use
8075fd0b74Schristos    simple_object_find_sections instead.
8175fd0b74Schristos 
8275fd0b74Schristos    If found, return 1 and set *OFFSET to the offset in the file of the
8375fd0b74Schristos    section contents and set *LENGTH to the length of the section
8475fd0b74Schristos    contents.  *OFFSET will be relative to the offset passed to
8575fd0b74Schristos    simple_object_start_read.
8675fd0b74Schristos 
8775fd0b74Schristos    If the section is not found, and no error occurs, return 0 and set
8875fd0b74Schristos    *ERRMSG to NULL.
8975fd0b74Schristos 
9075fd0b74Schristos    If an error occurs, return 0, set *ERRMSG to an error message, and
9175fd0b74Schristos    set *ERR to an errno value or 0 if there is no relevant errno.  */
9275fd0b74Schristos 
9375fd0b74Schristos extern int
9475fd0b74Schristos simple_object_find_section (simple_object_read *simple_object,
9575fd0b74Schristos 			    const char *name, off_t *offset, off_t *length,
9675fd0b74Schristos 			    const char **errmsg, int *err);
9775fd0b74Schristos 
9875fd0b74Schristos /* Release all resources associated with SIMPLE_OBJECT.  This does not
9975fd0b74Schristos    close the file descriptor.  */
10075fd0b74Schristos 
10175fd0b74Schristos extern void
10275fd0b74Schristos simple_object_release_read (simple_object_read *);
10375fd0b74Schristos 
10475fd0b74Schristos /* The type simple_object_attributes holds the attributes of an object
10575fd0b74Schristos    file that matter for creating a file or ensuring that two files are
10675fd0b74Schristos    compatible.  This is a set of magic numbers.  */
10775fd0b74Schristos 
10875fd0b74Schristos typedef struct simple_object_attributes_struct simple_object_attributes;
10975fd0b74Schristos 
11075fd0b74Schristos /* Fetch the attributes of SIMPLE_OBJECT.  This information will
11175fd0b74Schristos    persist until simple_object_attributes_release is called, even if
11275fd0b74Schristos    SIMPLE_OBJECT is closed.  On error this returns NULL, sets *ERRMSG
11375fd0b74Schristos    to an error message, and sets *ERR to an errno value or 0 if there
11475fd0b74Schristos    isn't one.  */
11575fd0b74Schristos 
11675fd0b74Schristos extern simple_object_attributes *
11775fd0b74Schristos simple_object_fetch_attributes (simple_object_read *simple_object,
11875fd0b74Schristos 				const char **errmsg, int *err);
11975fd0b74Schristos 
12075fd0b74Schristos /* Merge the FROM attributes into TO.  If two objects with these
12175fd0b74Schristos    attributes could be linked together without error, returns NULL.
12275fd0b74Schristos    Otherwise, returns an error message, and sets *ERR to an errno
12375fd0b74Schristos    value or 0 if there isn't one.  */
12475fd0b74Schristos 
12575fd0b74Schristos extern const char *
12675fd0b74Schristos simple_object_attributes_merge (simple_object_attributes *to,
12775fd0b74Schristos 				simple_object_attributes *from,
12875fd0b74Schristos 				int *err);
12975fd0b74Schristos 
13075fd0b74Schristos /* Release all resources associated with ATTRS.  */
13175fd0b74Schristos 
13275fd0b74Schristos extern void
13375fd0b74Schristos simple_object_release_attributes (simple_object_attributes *attrs);
13475fd0b74Schristos 
13575fd0b74Schristos /* The type simple_object_write is used to create a new object file.  */
13675fd0b74Schristos 
13775fd0b74Schristos typedef struct simple_object_write_struct simple_object_write;
13875fd0b74Schristos 
13975fd0b74Schristos /* Start creating a new object file which is like ATTRS.  You must
14075fd0b74Schristos    fetch attribute information from an existing object file before you
14175fd0b74Schristos    can create a new one.  There is currently no support for creating
14275fd0b74Schristos    an object file de novo.  The segment name is only used on Mach-O,
14375fd0b74Schristos    where it is required.  It means that all sections are created
14475fd0b74Schristos    within that segment.  It is ignored for other object file formats.
14575fd0b74Schristos    On error this function returns NULL, sets *ERRMSG to an error
14675fd0b74Schristos    message, and sets *ERR to an errno value or 0 if there isn't
14775fd0b74Schristos    one.  */
14875fd0b74Schristos 
14975fd0b74Schristos extern simple_object_write *
15075fd0b74Schristos simple_object_start_write (simple_object_attributes *attrs,
15175fd0b74Schristos 			   const char *segment_name,
15275fd0b74Schristos 			   const char **errmsg, int *err);
15375fd0b74Schristos 
15475fd0b74Schristos /* The type simple_object_write_section is a handle for a section
15575fd0b74Schristos    which is being written.  */
15675fd0b74Schristos 
15775fd0b74Schristos typedef struct simple_object_write_section_struct simple_object_write_section;
15875fd0b74Schristos 
15975fd0b74Schristos /* Add a section to SIMPLE_OBJECT.  NAME is the name of the new
16075fd0b74Schristos    section.  ALIGN is the required alignment expressed as the number
16175fd0b74Schristos    of required low-order 0 bits (e.g., 2 for alignment to a 32-bit
16275fd0b74Schristos    boundary).  The section is created as containing data, readable,
16375fd0b74Schristos    not writable, not executable, not loaded at runtime.  On error this
16475fd0b74Schristos    returns NULL, sets *ERRMSG to an error message, and sets *ERR to an
16575fd0b74Schristos    errno value or 0 if there isn't one.  */
16675fd0b74Schristos 
16775fd0b74Schristos extern simple_object_write_section *
16875fd0b74Schristos simple_object_write_create_section (simple_object_write *simple_object,
16975fd0b74Schristos 				    const char *name, unsigned int align,
17075fd0b74Schristos 				    const char **errmsg, int *err);
17175fd0b74Schristos 
17275fd0b74Schristos /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT.  If COPY is
17375fd0b74Schristos    non-zero, the data will be copied into memory if necessary.  If
17475fd0b74Schristos    COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released.
17575fd0b74Schristos    On success this returns NULL.  On error this returns an error
17675fd0b74Schristos    message, and sets *ERR to an errno value or 0 if there isn't
17775fd0b74Schristos    one.  */
17875fd0b74Schristos 
17975fd0b74Schristos extern const char *
18075fd0b74Schristos simple_object_write_add_data (simple_object_write *simple_object,
18175fd0b74Schristos 			      simple_object_write_section *section,
18275fd0b74Schristos 			      const void *buffer, size_t size,
18375fd0b74Schristos 			      int copy, int *err);
18475fd0b74Schristos 
18575fd0b74Schristos /* Write the complete object file to DESCRIPTOR, an open file
18675fd0b74Schristos    descriptor.  This returns NULL on success.  On error this returns
18775fd0b74Schristos    an error message, and sets *ERR to an errno value or 0 if there
18875fd0b74Schristos    isn't one.  */
18975fd0b74Schristos 
19075fd0b74Schristos extern const char *
19175fd0b74Schristos simple_object_write_to_file (simple_object_write *simple_object,
19275fd0b74Schristos 			     int descriptor, int *err);
19375fd0b74Schristos 
19475fd0b74Schristos /* Release all resources associated with SIMPLE_OBJECT, including any
19575fd0b74Schristos    simple_object_write_section's that may have been created.  */
19675fd0b74Schristos 
19775fd0b74Schristos extern void
19875fd0b74Schristos simple_object_release_write (simple_object_write *);
19975fd0b74Schristos 
200ede78133Schristos /* Copy LTO debug sections from SRC_OBJECT to DEST.
201ede78133Schristos    If RENAME is true, rename LTO debug section into debug section (i.e.
202ede78133Schristos    when producing final binary) and if it is false, keep the sections with
203ede78133Schristos    original names (when incrementally linking).
204ede78133Schristos    If an error occurs, return the errno value in ERR and an error string.  */
205ede78133Schristos 
206ede78133Schristos extern const char *
207ede78133Schristos simple_object_copy_lto_debug_sections (simple_object_read *src_object,
208ede78133Schristos 				       const char *dest,
209ede78133Schristos 				       int *err, int rename);
210ede78133Schristos 
21175fd0b74Schristos #ifdef __cplusplus
21275fd0b74Schristos }
21375fd0b74Schristos #endif
21475fd0b74Schristos 
21575fd0b74Schristos #endif
216