11debfc3dSmrg /* simple-object.h -- simple routines to read and write object files 2*8feb0f0bSmrg Copyright (C) 2010-2020 Free Software Foundation, Inc. 31debfc3dSmrg Written by Ian Lance Taylor, Google. 41debfc3dSmrg 51debfc3dSmrg This program is free software; you can redistribute it and/or modify it 61debfc3dSmrg under the terms of the GNU General Public License as published by the 71debfc3dSmrg Free Software Foundation; either version 2, or (at your option) any 81debfc3dSmrg later version. 91debfc3dSmrg 101debfc3dSmrg This program is distributed in the hope that it will be useful, 111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of 121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131debfc3dSmrg GNU General Public License for more details. 141debfc3dSmrg 151debfc3dSmrg You should have received a copy of the GNU General Public License 161debfc3dSmrg along with this program; if not, write to the Free Software 171debfc3dSmrg Foundation, 51 Franklin Street - Fifth Floor, 181debfc3dSmrg Boston, MA 02110-1301, USA. */ 191debfc3dSmrg 201debfc3dSmrg #ifndef SIMPLE_OBJECT_H 211debfc3dSmrg #define SIMPLE_OBJECT_H 221debfc3dSmrg 231debfc3dSmrg #include <stddef.h> 241debfc3dSmrg #include <sys/types.h> 251debfc3dSmrg 261debfc3dSmrg #ifdef HAVE_UNISTD_H 271debfc3dSmrg #include <unistd.h> 281debfc3dSmrg #endif 291debfc3dSmrg 301debfc3dSmrg #ifdef __cplusplus 311debfc3dSmrg extern "C" { 321debfc3dSmrg #endif 331debfc3dSmrg 341debfc3dSmrg /* This header file provides four types with associated functions. 351debfc3dSmrg They are used to read and write object files. This is a minimal 361debfc3dSmrg interface, intended to support the needs of gcc without bringing in 371debfc3dSmrg all the power and complexity of BFD. */ 381debfc3dSmrg 391debfc3dSmrg /* The type simple_object_read * is used to read an existing object 401debfc3dSmrg file. */ 411debfc3dSmrg 421debfc3dSmrg typedef struct simple_object_read_struct simple_object_read; 431debfc3dSmrg 441debfc3dSmrg /* Create an simple_object_read given DESCRIPTOR, an open file 451debfc3dSmrg descriptor, and OFFSET, an offset within the file. The offset is 461debfc3dSmrg for use with archives, and should be 0 for an ordinary object file. 471debfc3dSmrg The descriptor must remain open until done with the returned 481debfc3dSmrg simple_object_read. SEGMENT_NAME is used on Mach-O and is required 491debfc3dSmrg on that platform: it means to only look at sections within the 501debfc3dSmrg segment with that name. It is ignored for other object file 511debfc3dSmrg formats. On error, this function returns NULL, and sets *ERRMSG to 521debfc3dSmrg an error string and sets *ERR to an errno value or 0 if there is no 531debfc3dSmrg relevant errno. */ 541debfc3dSmrg 551debfc3dSmrg extern simple_object_read * 561debfc3dSmrg simple_object_start_read (int descriptor, off_t offset, 571debfc3dSmrg const char *segment_name, const char **errmsg, 581debfc3dSmrg int *err); 591debfc3dSmrg 601debfc3dSmrg /* Call PFN for each section in SIMPLE_OBJECT, passing it the section 611debfc3dSmrg name, offset within the file of the section contents, and length of 621debfc3dSmrg the section contents. The offset within the file is relative to 631debfc3dSmrg the offset passed to simple_object_start_read. The DATA argument 641debfc3dSmrg to simple_object_find_sections is passed on to PFN. If PFN returns 651debfc3dSmrg 0, the loop is stopped and simple_object_find_sections returns. If 661debfc3dSmrg PFN returns non-zero, the loop continues. On success this returns 671debfc3dSmrg NULL. On error it returns an error string, and sets *ERR to an 681debfc3dSmrg errno value or 0 if there is no relevant errno. */ 691debfc3dSmrg 701debfc3dSmrg extern const char * 711debfc3dSmrg simple_object_find_sections (simple_object_read *simple_object, 721debfc3dSmrg int (*pfn) (void *data, const char *, 731debfc3dSmrg off_t offset, off_t length), 741debfc3dSmrg void *data, 751debfc3dSmrg int *err); 761debfc3dSmrg 771debfc3dSmrg /* Look for the section NAME in SIMPLE_OBJECT. This returns 781debfc3dSmrg information for the first section NAME in SIMPLE_OBJECT. Note that 791debfc3dSmrg calling this multiple times is inefficient; use 801debfc3dSmrg simple_object_find_sections instead. 811debfc3dSmrg 821debfc3dSmrg If found, return 1 and set *OFFSET to the offset in the file of the 831debfc3dSmrg section contents and set *LENGTH to the length of the section 841debfc3dSmrg contents. *OFFSET will be relative to the offset passed to 851debfc3dSmrg simple_object_start_read. 861debfc3dSmrg 871debfc3dSmrg If the section is not found, and no error occurs, return 0 and set 881debfc3dSmrg *ERRMSG to NULL. 891debfc3dSmrg 901debfc3dSmrg If an error occurs, return 0, set *ERRMSG to an error message, and 911debfc3dSmrg set *ERR to an errno value or 0 if there is no relevant errno. */ 921debfc3dSmrg 931debfc3dSmrg extern int 941debfc3dSmrg simple_object_find_section (simple_object_read *simple_object, 951debfc3dSmrg const char *name, off_t *offset, off_t *length, 961debfc3dSmrg const char **errmsg, int *err); 971debfc3dSmrg 981debfc3dSmrg /* Release all resources associated with SIMPLE_OBJECT. This does not 991debfc3dSmrg close the file descriptor. */ 1001debfc3dSmrg 1011debfc3dSmrg extern void 1021debfc3dSmrg simple_object_release_read (simple_object_read *); 1031debfc3dSmrg 1041debfc3dSmrg /* The type simple_object_attributes holds the attributes of an object 1051debfc3dSmrg file that matter for creating a file or ensuring that two files are 1061debfc3dSmrg compatible. This is a set of magic numbers. */ 1071debfc3dSmrg 1081debfc3dSmrg typedef struct simple_object_attributes_struct simple_object_attributes; 1091debfc3dSmrg 1101debfc3dSmrg /* Fetch the attributes of SIMPLE_OBJECT. This information will 1111debfc3dSmrg persist until simple_object_attributes_release is called, even if 1121debfc3dSmrg SIMPLE_OBJECT is closed. On error this returns NULL, sets *ERRMSG 1131debfc3dSmrg to an error message, and sets *ERR to an errno value or 0 if there 1141debfc3dSmrg isn't one. */ 1151debfc3dSmrg 1161debfc3dSmrg extern simple_object_attributes * 1171debfc3dSmrg simple_object_fetch_attributes (simple_object_read *simple_object, 1181debfc3dSmrg const char **errmsg, int *err); 1191debfc3dSmrg 1201debfc3dSmrg /* Merge the FROM attributes into TO. If two objects with these 1211debfc3dSmrg attributes could be linked together without error, returns NULL. 1221debfc3dSmrg Otherwise, returns an error message, and sets *ERR to an errno 1231debfc3dSmrg value or 0 if there isn't one. */ 1241debfc3dSmrg 1251debfc3dSmrg extern const char * 1261debfc3dSmrg simple_object_attributes_merge (simple_object_attributes *to, 1271debfc3dSmrg simple_object_attributes *from, 1281debfc3dSmrg int *err); 1291debfc3dSmrg 1301debfc3dSmrg /* Release all resources associated with ATTRS. */ 1311debfc3dSmrg 1321debfc3dSmrg extern void 1331debfc3dSmrg simple_object_release_attributes (simple_object_attributes *attrs); 1341debfc3dSmrg 1351debfc3dSmrg /* The type simple_object_write is used to create a new object file. */ 1361debfc3dSmrg 1371debfc3dSmrg typedef struct simple_object_write_struct simple_object_write; 1381debfc3dSmrg 1391debfc3dSmrg /* Start creating a new object file which is like ATTRS. You must 1401debfc3dSmrg fetch attribute information from an existing object file before you 1411debfc3dSmrg can create a new one. There is currently no support for creating 1421debfc3dSmrg an object file de novo. The segment name is only used on Mach-O, 1431debfc3dSmrg where it is required. It means that all sections are created 1441debfc3dSmrg within that segment. It is ignored for other object file formats. 1451debfc3dSmrg On error this function returns NULL, sets *ERRMSG to an error 1461debfc3dSmrg message, and sets *ERR to an errno value or 0 if there isn't 1471debfc3dSmrg one. */ 1481debfc3dSmrg 1491debfc3dSmrg extern simple_object_write * 1501debfc3dSmrg simple_object_start_write (simple_object_attributes *attrs, 1511debfc3dSmrg const char *segment_name, 1521debfc3dSmrg const char **errmsg, int *err); 1531debfc3dSmrg 1541debfc3dSmrg /* The type simple_object_write_section is a handle for a section 1551debfc3dSmrg which is being written. */ 1561debfc3dSmrg 1571debfc3dSmrg typedef struct simple_object_write_section_struct simple_object_write_section; 1581debfc3dSmrg 1591debfc3dSmrg /* Add a section to SIMPLE_OBJECT. NAME is the name of the new 1601debfc3dSmrg section. ALIGN is the required alignment expressed as the number 1611debfc3dSmrg of required low-order 0 bits (e.g., 2 for alignment to a 32-bit 1621debfc3dSmrg boundary). The section is created as containing data, readable, 1631debfc3dSmrg not writable, not executable, not loaded at runtime. On error this 1641debfc3dSmrg returns NULL, sets *ERRMSG to an error message, and sets *ERR to an 1651debfc3dSmrg errno value or 0 if there isn't one. */ 1661debfc3dSmrg 1671debfc3dSmrg extern simple_object_write_section * 1681debfc3dSmrg simple_object_write_create_section (simple_object_write *simple_object, 1691debfc3dSmrg const char *name, unsigned int align, 1701debfc3dSmrg const char **errmsg, int *err); 1711debfc3dSmrg 1721debfc3dSmrg /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT. If COPY is 1731debfc3dSmrg non-zero, the data will be copied into memory if necessary. If 1741debfc3dSmrg COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released. 1751debfc3dSmrg On success this returns NULL. On error this returns an error 1761debfc3dSmrg message, and sets *ERR to an errno value or 0 if there isn't 1771debfc3dSmrg one. */ 1781debfc3dSmrg 1791debfc3dSmrg extern const char * 1801debfc3dSmrg simple_object_write_add_data (simple_object_write *simple_object, 1811debfc3dSmrg simple_object_write_section *section, 1821debfc3dSmrg const void *buffer, size_t size, 1831debfc3dSmrg int copy, int *err); 1841debfc3dSmrg 1851debfc3dSmrg /* Write the complete object file to DESCRIPTOR, an open file 1861debfc3dSmrg descriptor. This returns NULL on success. On error this returns 1871debfc3dSmrg an error message, and sets *ERR to an errno value or 0 if there 1881debfc3dSmrg isn't one. */ 1891debfc3dSmrg 1901debfc3dSmrg extern const char * 1911debfc3dSmrg simple_object_write_to_file (simple_object_write *simple_object, 1921debfc3dSmrg int descriptor, int *err); 1931debfc3dSmrg 1941debfc3dSmrg /* Release all resources associated with SIMPLE_OBJECT, including any 1951debfc3dSmrg simple_object_write_section's that may have been created. */ 1961debfc3dSmrg 1971debfc3dSmrg extern void 1981debfc3dSmrg simple_object_release_write (simple_object_write *); 1991debfc3dSmrg 200a2dc1f3fSmrg /* Copy LTO debug sections from SRC_OBJECT to DEST. 201c0a68be4Smrg If RENAME is true, rename LTO debug section into debug section (i.e. 202c0a68be4Smrg when producing final binary) and if it is false, keep the sections with 203c0a68be4Smrg original names (when incrementally linking). 204a2dc1f3fSmrg If an error occurs, return the errno value in ERR and an error string. */ 205a2dc1f3fSmrg 206a2dc1f3fSmrg extern const char * 207a2dc1f3fSmrg simple_object_copy_lto_debug_sections (simple_object_read *src_object, 208a2dc1f3fSmrg const char *dest, 209c0a68be4Smrg int *err, int rename); 210a2dc1f3fSmrg 2111debfc3dSmrg #ifdef __cplusplus 2121debfc3dSmrg } 2131debfc3dSmrg #endif 2141debfc3dSmrg 2151debfc3dSmrg #endif 216