xref: /netbsd-src/external/gpl3/gdb/dist/include/simple-object.h (revision e663ba6e3a60083e70de702e9d54bf486a57b6a7)
198b9484cSchristos /* simple-object.h -- simple routines to read and write object files
2*e663ba6eSchristos    Copyright (C) 2010-2024 Free Software Foundation, Inc.
398b9484cSchristos    Written by Ian Lance Taylor, Google.
498b9484cSchristos 
598b9484cSchristos This program is free software; you can redistribute it and/or modify it
698b9484cSchristos under the terms of the GNU General Public License as published by the
798b9484cSchristos Free Software Foundation; either version 2, or (at your option) any
898b9484cSchristos later version.
998b9484cSchristos 
1098b9484cSchristos This program is distributed in the hope that it will be useful,
1198b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1298b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1398b9484cSchristos GNU General Public License for more details.
1498b9484cSchristos 
1598b9484cSchristos You should have received a copy of the GNU General Public License
1698b9484cSchristos along with this program; if not, write to the Free Software
1798b9484cSchristos Foundation, 51 Franklin Street - Fifth Floor,
1898b9484cSchristos Boston, MA 02110-1301, USA.  */
1998b9484cSchristos 
2098b9484cSchristos #ifndef SIMPLE_OBJECT_H
2198b9484cSchristos #define SIMPLE_OBJECT_H
2298b9484cSchristos 
2398b9484cSchristos #include <stddef.h>
2498b9484cSchristos #include <sys/types.h>
2598b9484cSchristos 
2698b9484cSchristos #ifdef HAVE_UNISTD_H
2798b9484cSchristos #include <unistd.h>
2898b9484cSchristos #endif
2998b9484cSchristos 
3098b9484cSchristos #ifdef __cplusplus
3198b9484cSchristos extern "C" {
3298b9484cSchristos #endif
3398b9484cSchristos 
3498b9484cSchristos /* This header file provides four types with associated functions.
3598b9484cSchristos    They are used to read and write object files.  This is a minimal
3698b9484cSchristos    interface, intended to support the needs of gcc without bringing in
3798b9484cSchristos    all the power and complexity of BFD.  */
3898b9484cSchristos 
3998b9484cSchristos /* The type simple_object_read * is used to read an existing object
4098b9484cSchristos    file.  */
4198b9484cSchristos 
4298b9484cSchristos typedef struct simple_object_read_struct simple_object_read;
4398b9484cSchristos 
4498b9484cSchristos /* Create an simple_object_read given DESCRIPTOR, an open file
4598b9484cSchristos    descriptor, and OFFSET, an offset within the file.  The offset is
4698b9484cSchristos    for use with archives, and should be 0 for an ordinary object file.
4798b9484cSchristos    The descriptor must remain open until done with the returned
4898b9484cSchristos    simple_object_read.  SEGMENT_NAME is used on Mach-O and is required
4998b9484cSchristos    on that platform: it means to only look at sections within the
5098b9484cSchristos    segment with that name.  It is ignored for other object file
5198b9484cSchristos    formats.  On error, this function returns NULL, and sets *ERRMSG to
5298b9484cSchristos    an error string and sets *ERR to an errno value or 0 if there is no
5398b9484cSchristos    relevant errno.  */
5498b9484cSchristos 
5598b9484cSchristos extern simple_object_read *
5698b9484cSchristos simple_object_start_read (int descriptor, off_t offset,
5798b9484cSchristos 			  const char *segment_name, const char **errmsg,
5898b9484cSchristos 			  int *err);
5998b9484cSchristos 
6098b9484cSchristos /* Call PFN for each section in SIMPLE_OBJECT, passing it the section
6198b9484cSchristos    name, offset within the file of the section contents, and length of
6298b9484cSchristos    the section contents.  The offset within the file is relative to
6398b9484cSchristos    the offset passed to simple_object_start_read.  The DATA argument
6498b9484cSchristos    to simple_object_find_sections is passed on to PFN.  If PFN returns
6598b9484cSchristos    0, the loop is stopped and simple_object_find_sections returns.  If
6698b9484cSchristos    PFN returns non-zero, the loop continues.  On success this returns
6798b9484cSchristos    NULL.  On error it returns an error string, and sets *ERR to an
6898b9484cSchristos    errno value or 0 if there is no relevant errno.  */
6998b9484cSchristos 
7098b9484cSchristos extern const char *
7198b9484cSchristos simple_object_find_sections (simple_object_read *simple_object,
7298b9484cSchristos 			     int (*pfn) (void *data, const char *,
7398b9484cSchristos 					 off_t offset, off_t length),
7498b9484cSchristos 			     void *data,
7598b9484cSchristos 			     int *err);
7698b9484cSchristos 
7798b9484cSchristos /* Look for the section NAME in SIMPLE_OBJECT.  This returns
7898b9484cSchristos    information for the first section NAME in SIMPLE_OBJECT.  Note that
7998b9484cSchristos    calling this multiple times is inefficient; use
8098b9484cSchristos    simple_object_find_sections instead.
8198b9484cSchristos 
8298b9484cSchristos    If found, return 1 and set *OFFSET to the offset in the file of the
8398b9484cSchristos    section contents and set *LENGTH to the length of the section
8498b9484cSchristos    contents.  *OFFSET will be relative to the offset passed to
8598b9484cSchristos    simple_object_start_read.
8698b9484cSchristos 
8798b9484cSchristos    If the section is not found, and no error occurs, return 0 and set
8898b9484cSchristos    *ERRMSG to NULL.
8998b9484cSchristos 
9098b9484cSchristos    If an error occurs, return 0, set *ERRMSG to an error message, and
9198b9484cSchristos    set *ERR to an errno value or 0 if there is no relevant errno.  */
9298b9484cSchristos 
9398b9484cSchristos extern int
9498b9484cSchristos simple_object_find_section (simple_object_read *simple_object,
9598b9484cSchristos 			    const char *name, off_t *offset, off_t *length,
9698b9484cSchristos 			    const char **errmsg, int *err);
9798b9484cSchristos 
9898b9484cSchristos /* Release all resources associated with SIMPLE_OBJECT.  This does not
9998b9484cSchristos    close the file descriptor.  */
10098b9484cSchristos 
10198b9484cSchristos extern void
10298b9484cSchristos simple_object_release_read (simple_object_read *);
10398b9484cSchristos 
10498b9484cSchristos /* The type simple_object_attributes holds the attributes of an object
10598b9484cSchristos    file that matter for creating a file or ensuring that two files are
10698b9484cSchristos    compatible.  This is a set of magic numbers.  */
10798b9484cSchristos 
10898b9484cSchristos typedef struct simple_object_attributes_struct simple_object_attributes;
10998b9484cSchristos 
11098b9484cSchristos /* Fetch the attributes of SIMPLE_OBJECT.  This information will
11198b9484cSchristos    persist until simple_object_attributes_release is called, even if
11298b9484cSchristos    SIMPLE_OBJECT is closed.  On error this returns NULL, sets *ERRMSG
11398b9484cSchristos    to an error message, and sets *ERR to an errno value or 0 if there
11498b9484cSchristos    isn't one.  */
11598b9484cSchristos 
11698b9484cSchristos extern simple_object_attributes *
11798b9484cSchristos simple_object_fetch_attributes (simple_object_read *simple_object,
11898b9484cSchristos 				const char **errmsg, int *err);
11998b9484cSchristos 
12098b9484cSchristos /* Merge the FROM attributes into TO.  If two objects with these
12198b9484cSchristos    attributes could be linked together without error, returns NULL.
12298b9484cSchristos    Otherwise, returns an error message, and sets *ERR to an errno
12398b9484cSchristos    value or 0 if there isn't one.  */
12498b9484cSchristos 
12598b9484cSchristos extern const char *
12698b9484cSchristos simple_object_attributes_merge (simple_object_attributes *to,
12798b9484cSchristos 				simple_object_attributes *from,
12898b9484cSchristos 				int *err);
12998b9484cSchristos 
13098b9484cSchristos /* Release all resources associated with ATTRS.  */
13198b9484cSchristos 
13298b9484cSchristos extern void
13398b9484cSchristos simple_object_release_attributes (simple_object_attributes *attrs);
13498b9484cSchristos 
13598b9484cSchristos /* The type simple_object_write is used to create a new object file.  */
13698b9484cSchristos 
13798b9484cSchristos typedef struct simple_object_write_struct simple_object_write;
13898b9484cSchristos 
13998b9484cSchristos /* Start creating a new object file which is like ATTRS.  You must
14098b9484cSchristos    fetch attribute information from an existing object file before you
14198b9484cSchristos    can create a new one.  There is currently no support for creating
14298b9484cSchristos    an object file de novo.  The segment name is only used on Mach-O,
14398b9484cSchristos    where it is required.  It means that all sections are created
14498b9484cSchristos    within that segment.  It is ignored for other object file formats.
14598b9484cSchristos    On error this function returns NULL, sets *ERRMSG to an error
14698b9484cSchristos    message, and sets *ERR to an errno value or 0 if there isn't
14798b9484cSchristos    one.  */
14898b9484cSchristos 
14998b9484cSchristos extern simple_object_write *
15098b9484cSchristos simple_object_start_write (simple_object_attributes *attrs,
15198b9484cSchristos 			   const char *segment_name,
15298b9484cSchristos 			   const char **errmsg, int *err);
15398b9484cSchristos 
15498b9484cSchristos /* The type simple_object_write_section is a handle for a section
15598b9484cSchristos    which is being written.  */
15698b9484cSchristos 
15798b9484cSchristos typedef struct simple_object_write_section_struct simple_object_write_section;
15898b9484cSchristos 
15998b9484cSchristos /* Add a section to SIMPLE_OBJECT.  NAME is the name of the new
16098b9484cSchristos    section.  ALIGN is the required alignment expressed as the number
16198b9484cSchristos    of required low-order 0 bits (e.g., 2 for alignment to a 32-bit
16298b9484cSchristos    boundary).  The section is created as containing data, readable,
16398b9484cSchristos    not writable, not executable, not loaded at runtime.  On error this
16498b9484cSchristos    returns NULL, sets *ERRMSG to an error message, and sets *ERR to an
16598b9484cSchristos    errno value or 0 if there isn't one.  */
16698b9484cSchristos 
16798b9484cSchristos extern simple_object_write_section *
16898b9484cSchristos simple_object_write_create_section (simple_object_write *simple_object,
16998b9484cSchristos 				    const char *name, unsigned int align,
17098b9484cSchristos 				    const char **errmsg, int *err);
17198b9484cSchristos 
17298b9484cSchristos /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT.  If COPY is
17398b9484cSchristos    non-zero, the data will be copied into memory if necessary.  If
17498b9484cSchristos    COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released.
17598b9484cSchristos    On success this returns NULL.  On error this returns an error
17698b9484cSchristos    message, and sets *ERR to an errno value or 0 if there isn't
17798b9484cSchristos    one.  */
17898b9484cSchristos 
17998b9484cSchristos extern const char *
18098b9484cSchristos simple_object_write_add_data (simple_object_write *simple_object,
18198b9484cSchristos 			      simple_object_write_section *section,
18298b9484cSchristos 			      const void *buffer, size_t size,
18398b9484cSchristos 			      int copy, int *err);
18498b9484cSchristos 
18598b9484cSchristos /* Write the complete object file to DESCRIPTOR, an open file
18698b9484cSchristos    descriptor.  This returns NULL on success.  On error this returns
18798b9484cSchristos    an error message, and sets *ERR to an errno value or 0 if there
18898b9484cSchristos    isn't one.  */
18998b9484cSchristos 
19098b9484cSchristos extern const char *
19198b9484cSchristos simple_object_write_to_file (simple_object_write *simple_object,
19298b9484cSchristos 			     int descriptor, int *err);
19398b9484cSchristos 
19498b9484cSchristos /* Release all resources associated with SIMPLE_OBJECT, including any
19598b9484cSchristos    simple_object_write_section's that may have been created.  */
19698b9484cSchristos 
19798b9484cSchristos extern void
19898b9484cSchristos simple_object_release_write (simple_object_write *);
19998b9484cSchristos 
2004559860eSchristos /* Copy LTO debug sections from SRC_OBJECT to DEST.
2014559860eSchristos    If RENAME is true, rename LTO debug section into debug section (i.e.
2024559860eSchristos    when producing final binary) and if it is false, keep the sections with
2034559860eSchristos    original names (when incrementally linking).
2044559860eSchristos    If an error occurs, return the errno value in ERR and an error string.  */
2054559860eSchristos 
2064559860eSchristos extern const char *
2074559860eSchristos simple_object_copy_lto_debug_sections (simple_object_read *src_object,
2084559860eSchristos 				       const char *dest,
2094559860eSchristos 				       int *err, int rename);
2104559860eSchristos 
21198b9484cSchristos #ifdef __cplusplus
21298b9484cSchristos }
21398b9484cSchristos #endif
21498b9484cSchristos 
21598b9484cSchristos #endif
216