148fb7bfaSmrg /* simple-object.h -- simple routines to read and write object files 2*b1e83836Smrg Copyright (C) 2010-2022 Free Software Foundation, Inc. 348fb7bfaSmrg Written by Ian Lance Taylor, Google. 448fb7bfaSmrg 548fb7bfaSmrg This program is free software; you can redistribute it and/or modify it 648fb7bfaSmrg under the terms of the GNU General Public License as published by the 748fb7bfaSmrg Free Software Foundation; either version 2, or (at your option) any 848fb7bfaSmrg later version. 948fb7bfaSmrg 1048fb7bfaSmrg This program is distributed in the hope that it will be useful, 1148fb7bfaSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of 1248fb7bfaSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1348fb7bfaSmrg GNU General Public License for more details. 1448fb7bfaSmrg 1548fb7bfaSmrg You should have received a copy of the GNU General Public License 1648fb7bfaSmrg along with this program; if not, write to the Free Software 1748fb7bfaSmrg Foundation, 51 Franklin Street - Fifth Floor, 1848fb7bfaSmrg Boston, MA 02110-1301, USA. */ 1948fb7bfaSmrg 2048fb7bfaSmrg #ifndef SIMPLE_OBJECT_H 2148fb7bfaSmrg #define SIMPLE_OBJECT_H 2248fb7bfaSmrg 2348fb7bfaSmrg #include <stddef.h> 2448fb7bfaSmrg #include <sys/types.h> 2548fb7bfaSmrg 2648fb7bfaSmrg #ifdef HAVE_UNISTD_H 2748fb7bfaSmrg #include <unistd.h> 2848fb7bfaSmrg #endif 2948fb7bfaSmrg 3048fb7bfaSmrg #ifdef __cplusplus 3148fb7bfaSmrg extern "C" { 3248fb7bfaSmrg #endif 3348fb7bfaSmrg 3448fb7bfaSmrg /* This header file provides four types with associated functions. 3548fb7bfaSmrg They are used to read and write object files. This is a minimal 3648fb7bfaSmrg interface, intended to support the needs of gcc without bringing in 3748fb7bfaSmrg all the power and complexity of BFD. */ 3848fb7bfaSmrg 3948fb7bfaSmrg /* The type simple_object_read * is used to read an existing object 4048fb7bfaSmrg file. */ 4148fb7bfaSmrg 4248fb7bfaSmrg typedef struct simple_object_read_struct simple_object_read; 4348fb7bfaSmrg 4448fb7bfaSmrg /* Create an simple_object_read given DESCRIPTOR, an open file 4548fb7bfaSmrg descriptor, and OFFSET, an offset within the file. The offset is 4648fb7bfaSmrg for use with archives, and should be 0 for an ordinary object file. 4748fb7bfaSmrg The descriptor must remain open until done with the returned 4848fb7bfaSmrg simple_object_read. SEGMENT_NAME is used on Mach-O and is required 4948fb7bfaSmrg on that platform: it means to only look at sections within the 5048fb7bfaSmrg segment with that name. It is ignored for other object file 5148fb7bfaSmrg formats. On error, this function returns NULL, and sets *ERRMSG to 5248fb7bfaSmrg an error string and sets *ERR to an errno value or 0 if there is no 5348fb7bfaSmrg relevant errno. */ 5448fb7bfaSmrg 5548fb7bfaSmrg extern simple_object_read * 5648fb7bfaSmrg simple_object_start_read (int descriptor, off_t offset, 5748fb7bfaSmrg const char *segment_name, const char **errmsg, 5848fb7bfaSmrg int *err); 5948fb7bfaSmrg 6048fb7bfaSmrg /* Call PFN for each section in SIMPLE_OBJECT, passing it the section 6148fb7bfaSmrg name, offset within the file of the section contents, and length of 6248fb7bfaSmrg the section contents. The offset within the file is relative to 6348fb7bfaSmrg the offset passed to simple_object_start_read. The DATA argument 6448fb7bfaSmrg to simple_object_find_sections is passed on to PFN. If PFN returns 6548fb7bfaSmrg 0, the loop is stopped and simple_object_find_sections returns. If 6648fb7bfaSmrg PFN returns non-zero, the loop continues. On success this returns 6748fb7bfaSmrg NULL. On error it returns an error string, and sets *ERR to an 6848fb7bfaSmrg errno value or 0 if there is no relevant errno. */ 6948fb7bfaSmrg 7048fb7bfaSmrg extern const char * 7148fb7bfaSmrg simple_object_find_sections (simple_object_read *simple_object, 7248fb7bfaSmrg int (*pfn) (void *data, const char *, 7348fb7bfaSmrg off_t offset, off_t length), 7448fb7bfaSmrg void *data, 7548fb7bfaSmrg int *err); 7648fb7bfaSmrg 7748fb7bfaSmrg /* Look for the section NAME in SIMPLE_OBJECT. This returns 7848fb7bfaSmrg information for the first section NAME in SIMPLE_OBJECT. Note that 7948fb7bfaSmrg calling this multiple times is inefficient; use 8048fb7bfaSmrg simple_object_find_sections instead. 8148fb7bfaSmrg 8248fb7bfaSmrg If found, return 1 and set *OFFSET to the offset in the file of the 8348fb7bfaSmrg section contents and set *LENGTH to the length of the section 8448fb7bfaSmrg contents. *OFFSET will be relative to the offset passed to 8548fb7bfaSmrg simple_object_start_read. 8648fb7bfaSmrg 8748fb7bfaSmrg If the section is not found, and no error occurs, return 0 and set 8848fb7bfaSmrg *ERRMSG to NULL. 8948fb7bfaSmrg 9048fb7bfaSmrg If an error occurs, return 0, set *ERRMSG to an error message, and 9148fb7bfaSmrg set *ERR to an errno value or 0 if there is no relevant errno. */ 9248fb7bfaSmrg 9348fb7bfaSmrg extern int 9448fb7bfaSmrg simple_object_find_section (simple_object_read *simple_object, 9548fb7bfaSmrg const char *name, off_t *offset, off_t *length, 9648fb7bfaSmrg const char **errmsg, int *err); 9748fb7bfaSmrg 9848fb7bfaSmrg /* Release all resources associated with SIMPLE_OBJECT. This does not 9948fb7bfaSmrg close the file descriptor. */ 10048fb7bfaSmrg 10148fb7bfaSmrg extern void 10248fb7bfaSmrg simple_object_release_read (simple_object_read *); 10348fb7bfaSmrg 10448fb7bfaSmrg /* The type simple_object_attributes holds the attributes of an object 10548fb7bfaSmrg file that matter for creating a file or ensuring that two files are 10648fb7bfaSmrg compatible. This is a set of magic numbers. */ 10748fb7bfaSmrg 10848fb7bfaSmrg typedef struct simple_object_attributes_struct simple_object_attributes; 10948fb7bfaSmrg 11048fb7bfaSmrg /* Fetch the attributes of SIMPLE_OBJECT. This information will 11148fb7bfaSmrg persist until simple_object_attributes_release is called, even if 11248fb7bfaSmrg SIMPLE_OBJECT is closed. On error this returns NULL, sets *ERRMSG 11348fb7bfaSmrg to an error message, and sets *ERR to an errno value or 0 if there 11448fb7bfaSmrg isn't one. */ 11548fb7bfaSmrg 11648fb7bfaSmrg extern simple_object_attributes * 11748fb7bfaSmrg simple_object_fetch_attributes (simple_object_read *simple_object, 11848fb7bfaSmrg const char **errmsg, int *err); 11948fb7bfaSmrg 12048fb7bfaSmrg /* Merge the FROM attributes into TO. If two objects with these 12148fb7bfaSmrg attributes could be linked together without error, returns NULL. 12248fb7bfaSmrg Otherwise, returns an error message, and sets *ERR to an errno 12348fb7bfaSmrg value or 0 if there isn't one. */ 12448fb7bfaSmrg 12548fb7bfaSmrg extern const char * 12648fb7bfaSmrg simple_object_attributes_merge (simple_object_attributes *to, 12748fb7bfaSmrg simple_object_attributes *from, 12848fb7bfaSmrg int *err); 12948fb7bfaSmrg 13048fb7bfaSmrg /* Release all resources associated with ATTRS. */ 13148fb7bfaSmrg 13248fb7bfaSmrg extern void 13348fb7bfaSmrg simple_object_release_attributes (simple_object_attributes *attrs); 13448fb7bfaSmrg 13548fb7bfaSmrg /* The type simple_object_write is used to create a new object file. */ 13648fb7bfaSmrg 13748fb7bfaSmrg typedef struct simple_object_write_struct simple_object_write; 13848fb7bfaSmrg 13948fb7bfaSmrg /* Start creating a new object file which is like ATTRS. You must 14048fb7bfaSmrg fetch attribute information from an existing object file before you 14148fb7bfaSmrg can create a new one. There is currently no support for creating 14248fb7bfaSmrg an object file de novo. The segment name is only used on Mach-O, 14348fb7bfaSmrg where it is required. It means that all sections are created 14448fb7bfaSmrg within that segment. It is ignored for other object file formats. 14548fb7bfaSmrg On error this function returns NULL, sets *ERRMSG to an error 14648fb7bfaSmrg message, and sets *ERR to an errno value or 0 if there isn't 14748fb7bfaSmrg one. */ 14848fb7bfaSmrg 14948fb7bfaSmrg extern simple_object_write * 15048fb7bfaSmrg simple_object_start_write (simple_object_attributes *attrs, 15148fb7bfaSmrg const char *segment_name, 15248fb7bfaSmrg const char **errmsg, int *err); 15348fb7bfaSmrg 15448fb7bfaSmrg /* The type simple_object_write_section is a handle for a section 15548fb7bfaSmrg which is being written. */ 15648fb7bfaSmrg 15748fb7bfaSmrg typedef struct simple_object_write_section_struct simple_object_write_section; 15848fb7bfaSmrg 15948fb7bfaSmrg /* Add a section to SIMPLE_OBJECT. NAME is the name of the new 16048fb7bfaSmrg section. ALIGN is the required alignment expressed as the number 16148fb7bfaSmrg of required low-order 0 bits (e.g., 2 for alignment to a 32-bit 16248fb7bfaSmrg boundary). The section is created as containing data, readable, 16348fb7bfaSmrg not writable, not executable, not loaded at runtime. On error this 16448fb7bfaSmrg returns NULL, sets *ERRMSG to an error message, and sets *ERR to an 16548fb7bfaSmrg errno value or 0 if there isn't one. */ 16648fb7bfaSmrg 16748fb7bfaSmrg extern simple_object_write_section * 16848fb7bfaSmrg simple_object_write_create_section (simple_object_write *simple_object, 16948fb7bfaSmrg const char *name, unsigned int align, 17048fb7bfaSmrg const char **errmsg, int *err); 17148fb7bfaSmrg 17248fb7bfaSmrg /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT. If COPY is 17348fb7bfaSmrg non-zero, the data will be copied into memory if necessary. If 17448fb7bfaSmrg COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released. 17548fb7bfaSmrg On success this returns NULL. On error this returns an error 17648fb7bfaSmrg message, and sets *ERR to an errno value or 0 if there isn't 17748fb7bfaSmrg one. */ 17848fb7bfaSmrg 17948fb7bfaSmrg extern const char * 18048fb7bfaSmrg simple_object_write_add_data (simple_object_write *simple_object, 18148fb7bfaSmrg simple_object_write_section *section, 18248fb7bfaSmrg const void *buffer, size_t size, 18348fb7bfaSmrg int copy, int *err); 18448fb7bfaSmrg 18548fb7bfaSmrg /* Write the complete object file to DESCRIPTOR, an open file 18648fb7bfaSmrg descriptor. This returns NULL on success. On error this returns 18748fb7bfaSmrg an error message, and sets *ERR to an errno value or 0 if there 18848fb7bfaSmrg isn't one. */ 18948fb7bfaSmrg 19048fb7bfaSmrg extern const char * 19148fb7bfaSmrg simple_object_write_to_file (simple_object_write *simple_object, 19248fb7bfaSmrg int descriptor, int *err); 19348fb7bfaSmrg 19448fb7bfaSmrg /* Release all resources associated with SIMPLE_OBJECT, including any 19548fb7bfaSmrg simple_object_write_section's that may have been created. */ 19648fb7bfaSmrg 19748fb7bfaSmrg extern void 19848fb7bfaSmrg simple_object_release_write (simple_object_write *); 19948fb7bfaSmrg 200a3e9eb18Smrg /* Copy LTO debug sections from SRC_OBJECT to DEST. 201181254a7Smrg If RENAME is true, rename LTO debug section into debug section (i.e. 202181254a7Smrg when producing final binary) and if it is false, keep the sections with 203181254a7Smrg original names (when incrementally linking). 204a3e9eb18Smrg If an error occurs, return the errno value in ERR and an error string. */ 205a3e9eb18Smrg 206a3e9eb18Smrg extern const char * 207a3e9eb18Smrg simple_object_copy_lto_debug_sections (simple_object_read *src_object, 208a3e9eb18Smrg const char *dest, 209181254a7Smrg int *err, int rename); 210a3e9eb18Smrg 21148fb7bfaSmrg #ifdef __cplusplus 21248fb7bfaSmrg } 21348fb7bfaSmrg #endif 21448fb7bfaSmrg 21548fb7bfaSmrg #endif 216