145548106Schristos /* simple-object.h -- simple routines to read and write object files 2*cb63e24eSchristos Copyright (C) 2010-2024 Free Software Foundation, Inc. 345548106Schristos Written by Ian Lance Taylor, Google. 445548106Schristos 545548106Schristos This program is free software; you can redistribute it and/or modify it 645548106Schristos under the terms of the GNU General Public License as published by the 745548106Schristos Free Software Foundation; either version 2, or (at your option) any 845548106Schristos later version. 945548106Schristos 1045548106Schristos This program is distributed in the hope that it will be useful, 1145548106Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1245548106Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1345548106Schristos GNU General Public License for more details. 1445548106Schristos 1545548106Schristos You should have received a copy of the GNU General Public License 1645548106Schristos along with this program; if not, write to the Free Software 1745548106Schristos Foundation, 51 Franklin Street - Fifth Floor, 1845548106Schristos Boston, MA 02110-1301, USA. */ 1945548106Schristos 2045548106Schristos #ifndef SIMPLE_OBJECT_H 2145548106Schristos #define SIMPLE_OBJECT_H 2245548106Schristos 2345548106Schristos #include <stddef.h> 2445548106Schristos #include <sys/types.h> 2545548106Schristos 2645548106Schristos #ifdef HAVE_UNISTD_H 2745548106Schristos #include <unistd.h> 2845548106Schristos #endif 2945548106Schristos 3045548106Schristos #ifdef __cplusplus 3145548106Schristos extern "C" { 3245548106Schristos #endif 3345548106Schristos 3445548106Schristos /* This header file provides four types with associated functions. 3545548106Schristos They are used to read and write object files. This is a minimal 3645548106Schristos interface, intended to support the needs of gcc without bringing in 3745548106Schristos all the power and complexity of BFD. */ 3845548106Schristos 3945548106Schristos /* The type simple_object_read * is used to read an existing object 4045548106Schristos file. */ 4145548106Schristos 4245548106Schristos typedef struct simple_object_read_struct simple_object_read; 4345548106Schristos 4445548106Schristos /* Create an simple_object_read given DESCRIPTOR, an open file 4545548106Schristos descriptor, and OFFSET, an offset within the file. The offset is 4645548106Schristos for use with archives, and should be 0 for an ordinary object file. 4745548106Schristos The descriptor must remain open until done with the returned 4845548106Schristos simple_object_read. SEGMENT_NAME is used on Mach-O and is required 4945548106Schristos on that platform: it means to only look at sections within the 5045548106Schristos segment with that name. It is ignored for other object file 5145548106Schristos formats. On error, this function returns NULL, and sets *ERRMSG to 5245548106Schristos an error string and sets *ERR to an errno value or 0 if there is no 5345548106Schristos relevant errno. */ 5445548106Schristos 5545548106Schristos extern simple_object_read * 5645548106Schristos simple_object_start_read (int descriptor, off_t offset, 5745548106Schristos const char *segment_name, const char **errmsg, 5845548106Schristos int *err); 5945548106Schristos 6045548106Schristos /* Call PFN for each section in SIMPLE_OBJECT, passing it the section 6145548106Schristos name, offset within the file of the section contents, and length of 6245548106Schristos the section contents. The offset within the file is relative to 6345548106Schristos the offset passed to simple_object_start_read. The DATA argument 6445548106Schristos to simple_object_find_sections is passed on to PFN. If PFN returns 6545548106Schristos 0, the loop is stopped and simple_object_find_sections returns. If 6645548106Schristos PFN returns non-zero, the loop continues. On success this returns 6745548106Schristos NULL. On error it returns an error string, and sets *ERR to an 6845548106Schristos errno value or 0 if there is no relevant errno. */ 6945548106Schristos 7045548106Schristos extern const char * 7145548106Schristos simple_object_find_sections (simple_object_read *simple_object, 7245548106Schristos int (*pfn) (void *data, const char *, 7345548106Schristos off_t offset, off_t length), 7445548106Schristos void *data, 7545548106Schristos int *err); 7645548106Schristos 7745548106Schristos /* Look for the section NAME in SIMPLE_OBJECT. This returns 7845548106Schristos information for the first section NAME in SIMPLE_OBJECT. Note that 7945548106Schristos calling this multiple times is inefficient; use 8045548106Schristos simple_object_find_sections instead. 8145548106Schristos 8245548106Schristos If found, return 1 and set *OFFSET to the offset in the file of the 8345548106Schristos section contents and set *LENGTH to the length of the section 8445548106Schristos contents. *OFFSET will be relative to the offset passed to 8545548106Schristos simple_object_start_read. 8645548106Schristos 8745548106Schristos If the section is not found, and no error occurs, return 0 and set 8845548106Schristos *ERRMSG to NULL. 8945548106Schristos 9045548106Schristos If an error occurs, return 0, set *ERRMSG to an error message, and 9145548106Schristos set *ERR to an errno value or 0 if there is no relevant errno. */ 9245548106Schristos 9345548106Schristos extern int 9445548106Schristos simple_object_find_section (simple_object_read *simple_object, 9545548106Schristos const char *name, off_t *offset, off_t *length, 9645548106Schristos const char **errmsg, int *err); 9745548106Schristos 9845548106Schristos /* Release all resources associated with SIMPLE_OBJECT. This does not 9945548106Schristos close the file descriptor. */ 10045548106Schristos 10145548106Schristos extern void 10245548106Schristos simple_object_release_read (simple_object_read *); 10345548106Schristos 10445548106Schristos /* The type simple_object_attributes holds the attributes of an object 10545548106Schristos file that matter for creating a file or ensuring that two files are 10645548106Schristos compatible. This is a set of magic numbers. */ 10745548106Schristos 10845548106Schristos typedef struct simple_object_attributes_struct simple_object_attributes; 10945548106Schristos 11045548106Schristos /* Fetch the attributes of SIMPLE_OBJECT. This information will 11145548106Schristos persist until simple_object_attributes_release is called, even if 11245548106Schristos SIMPLE_OBJECT is closed. On error this returns NULL, sets *ERRMSG 11345548106Schristos to an error message, and sets *ERR to an errno value or 0 if there 11445548106Schristos isn't one. */ 11545548106Schristos 11645548106Schristos extern simple_object_attributes * 11745548106Schristos simple_object_fetch_attributes (simple_object_read *simple_object, 11845548106Schristos const char **errmsg, int *err); 11945548106Schristos 120883529b6Schristos /* Merge the FROM attributes into TO. If two objects with these 121883529b6Schristos attributes could be linked together without error, returns NULL. 122883529b6Schristos Otherwise, returns an error message, and sets *ERR to an errno 123883529b6Schristos value or 0 if there isn't one. */ 12445548106Schristos 12545548106Schristos extern const char * 126883529b6Schristos simple_object_attributes_merge (simple_object_attributes *to, 127883529b6Schristos simple_object_attributes *from, 12845548106Schristos int *err); 12945548106Schristos 13045548106Schristos /* Release all resources associated with ATTRS. */ 13145548106Schristos 13245548106Schristos extern void 13345548106Schristos simple_object_release_attributes (simple_object_attributes *attrs); 13445548106Schristos 13545548106Schristos /* The type simple_object_write is used to create a new object file. */ 13645548106Schristos 13745548106Schristos typedef struct simple_object_write_struct simple_object_write; 13845548106Schristos 13945548106Schristos /* Start creating a new object file which is like ATTRS. You must 14045548106Schristos fetch attribute information from an existing object file before you 14145548106Schristos can create a new one. There is currently no support for creating 14245548106Schristos an object file de novo. The segment name is only used on Mach-O, 14345548106Schristos where it is required. It means that all sections are created 14445548106Schristos within that segment. It is ignored for other object file formats. 14545548106Schristos On error this function returns NULL, sets *ERRMSG to an error 14645548106Schristos message, and sets *ERR to an errno value or 0 if there isn't 14745548106Schristos one. */ 14845548106Schristos 14945548106Schristos extern simple_object_write * 15045548106Schristos simple_object_start_write (simple_object_attributes *attrs, 15145548106Schristos const char *segment_name, 15245548106Schristos const char **errmsg, int *err); 15345548106Schristos 15445548106Schristos /* The type simple_object_write_section is a handle for a section 15545548106Schristos which is being written. */ 15645548106Schristos 15745548106Schristos typedef struct simple_object_write_section_struct simple_object_write_section; 15845548106Schristos 15945548106Schristos /* Add a section to SIMPLE_OBJECT. NAME is the name of the new 16045548106Schristos section. ALIGN is the required alignment expressed as the number 16145548106Schristos of required low-order 0 bits (e.g., 2 for alignment to a 32-bit 16245548106Schristos boundary). The section is created as containing data, readable, 16345548106Schristos not writable, not executable, not loaded at runtime. On error this 16445548106Schristos returns NULL, sets *ERRMSG to an error message, and sets *ERR to an 16545548106Schristos errno value or 0 if there isn't one. */ 16645548106Schristos 16745548106Schristos extern simple_object_write_section * 16845548106Schristos simple_object_write_create_section (simple_object_write *simple_object, 16945548106Schristos const char *name, unsigned int align, 17045548106Schristos const char **errmsg, int *err); 17145548106Schristos 17245548106Schristos /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT. If COPY is 17345548106Schristos non-zero, the data will be copied into memory if necessary. If 17445548106Schristos COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released. 17545548106Schristos On success this returns NULL. On error this returns an error 17645548106Schristos message, and sets *ERR to an errno value or 0 if there isn't 17745548106Schristos one. */ 17845548106Schristos 17945548106Schristos extern const char * 18045548106Schristos simple_object_write_add_data (simple_object_write *simple_object, 18145548106Schristos simple_object_write_section *section, 18245548106Schristos const void *buffer, size_t size, 18345548106Schristos int copy, int *err); 18445548106Schristos 18545548106Schristos /* Write the complete object file to DESCRIPTOR, an open file 18645548106Schristos descriptor. This returns NULL on success. On error this returns 18745548106Schristos an error message, and sets *ERR to an errno value or 0 if there 18845548106Schristos isn't one. */ 18945548106Schristos 19045548106Schristos extern const char * 19145548106Schristos simple_object_write_to_file (simple_object_write *simple_object, 19245548106Schristos int descriptor, int *err); 19345548106Schristos 19445548106Schristos /* Release all resources associated with SIMPLE_OBJECT, including any 19545548106Schristos simple_object_write_section's that may have been created. */ 19645548106Schristos 19745548106Schristos extern void 19845548106Schristos simple_object_release_write (simple_object_write *); 19945548106Schristos 200fc4f4269Schristos /* Copy LTO debug sections from SRC_OBJECT to DEST. 201c1a20988Schristos If RENAME is true, rename LTO debug section into debug section (i.e. 202c1a20988Schristos when producing final binary) and if it is false, keep the sections with 203c1a20988Schristos original names (when incrementally linking). 204fc4f4269Schristos If an error occurs, return the errno value in ERR and an error string. */ 205fc4f4269Schristos 206fc4f4269Schristos extern const char * 207fc4f4269Schristos simple_object_copy_lto_debug_sections (simple_object_read *src_object, 208fc4f4269Schristos const char *dest, 209c1a20988Schristos int *err, int rename); 210fc4f4269Schristos 21145548106Schristos #ifdef __cplusplus 21245548106Schristos } 21345548106Schristos #endif 21445548106Schristos 21545548106Schristos #endif 216