simple-object.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* simple-object.h -- simple routines to read and write object files
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #ifndef SIMPLE_OBJECT_H
  17. #define SIMPLE_OBJECT_H
  18. #include <stddef.h>
  19. #include <sys/types.h>
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* This header file provides four types with associated functions.
  27. They are used to read and write object files. This is a minimal
  28. interface, intended to support the needs of gcc without bringing in
  29. all the power and complexity of BFD. */
  30. /* The type simple_object_read * is used to read an existing object
  31. file. */
  32. typedef struct simple_object_read_struct simple_object_read;
  33. /* Create an simple_object_read given DESCRIPTOR, an open file
  34. descriptor, and OFFSET, an offset within the file. The offset is
  35. for use with archives, and should be 0 for an ordinary object file.
  36. The descriptor must remain open until done with the returned
  37. simple_object_read. SEGMENT_NAME is used on Mach-O and is required
  38. on that platform: it means to only look at sections within the
  39. segment with that name. It is ignored for other object file
  40. formats. On error, this function returns NULL, and sets *ERRMSG to
  41. an error string and sets *ERR to an errno value or 0 if there is no
  42. relevant errno. */
  43. extern simple_object_read *
  44. simple_object_start_read (int descriptor, off_t offset,
  45. const char *segment_name, const char **errmsg,
  46. int *err);
  47. /* Call PFN for each section in SIMPLE_OBJECT, passing it the section
  48. name, offset within the file of the section contents, and length of
  49. the section contents. The offset within the file is relative to
  50. the offset passed to simple_object_start_read. The DATA argument
  51. to simple_object_find_sections is passed on to PFN. If PFN returns
  52. 0, the loop is stopped and simple_object_find_sections returns. If
  53. PFN returns non-zero, the loop continues. On success this returns
  54. NULL. On error it returns an error string, and sets *ERR to an
  55. errno value or 0 if there is no relevant errno. */
  56. extern const char *
  57. simple_object_find_sections (simple_object_read *simple_object,
  58. int (*pfn) (void *data, const char *,
  59. off_t offset, off_t length),
  60. void *data,
  61. int *err);
  62. /* Look for the section NAME in SIMPLE_OBJECT. This returns
  63. information for the first section NAME in SIMPLE_OBJECT. Note that
  64. calling this multiple times is inefficient; use
  65. simple_object_find_sections instead.
  66. If found, return 1 and set *OFFSET to the offset in the file of the
  67. section contents and set *LENGTH to the length of the section
  68. contents. *OFFSET will be relative to the offset passed to
  69. simple_object_start_read.
  70. If the section is not found, and no error occurs, return 0 and set
  71. *ERRMSG to NULL.
  72. If an error occurs, return 0, set *ERRMSG to an error message, and
  73. set *ERR to an errno value or 0 if there is no relevant errno. */
  74. extern int
  75. simple_object_find_section (simple_object_read *simple_object,
  76. const char *name, off_t *offset, off_t *length,
  77. const char **errmsg, int *err);
  78. /* Release all resources associated with SIMPLE_OBJECT. This does not
  79. close the file descriptor. */
  80. extern void
  81. simple_object_release_read (simple_object_read *);
  82. /* The type simple_object_attributes holds the attributes of an object
  83. file that matter for creating a file or ensuring that two files are
  84. compatible. This is a set of magic numbers. */
  85. typedef struct simple_object_attributes_struct simple_object_attributes;
  86. /* Fetch the attributes of SIMPLE_OBJECT. This information will
  87. persist until simple_object_attributes_release is called, even if
  88. SIMPLE_OBJECT is closed. On error this returns NULL, sets *ERRMSG
  89. to an error message, and sets *ERR to an errno value or 0 if there
  90. isn't one. */
  91. extern simple_object_attributes *
  92. simple_object_fetch_attributes (simple_object_read *simple_object,
  93. const char **errmsg, int *err);
  94. /* Merge the FROM attributes into TO. If two objects with these
  95. attributes could be linked together without error, returns NULL.
  96. Otherwise, returns an error message, and sets *ERR to an errno
  97. value or 0 if there isn't one. */
  98. extern const char *
  99. simple_object_attributes_merge (simple_object_attributes *to,
  100. simple_object_attributes *from,
  101. int *err);
  102. /* Release all resources associated with ATTRS. */
  103. extern void
  104. simple_object_release_attributes (simple_object_attributes *attrs);
  105. /* The type simple_object_write is used to create a new object file. */
  106. typedef struct simple_object_write_struct simple_object_write;
  107. /* Start creating a new object file which is like ATTRS. You must
  108. fetch attribute information from an existing object file before you
  109. can create a new one. There is currently no support for creating
  110. an object file de novo. The segment name is only used on Mach-O,
  111. where it is required. It means that all sections are created
  112. within that segment. It is ignored for other object file formats.
  113. On error this function returns NULL, sets *ERRMSG to an error
  114. message, and sets *ERR to an errno value or 0 if there isn't
  115. one. */
  116. extern simple_object_write *
  117. simple_object_start_write (simple_object_attributes *attrs,
  118. const char *segment_name,
  119. const char **errmsg, int *err);
  120. /* The type simple_object_write_section is a handle for a section
  121. which is being written. */
  122. typedef struct simple_object_write_section_struct simple_object_write_section;
  123. /* Add a section to SIMPLE_OBJECT. NAME is the name of the new
  124. section. ALIGN is the required alignment expressed as the number
  125. of required low-order 0 bits (e.g., 2 for alignment to a 32-bit
  126. boundary). The section is created as containing data, readable,
  127. not writable, not executable, not loaded at runtime. On error this
  128. returns NULL, sets *ERRMSG to an error message, and sets *ERR to an
  129. errno value or 0 if there isn't one. */
  130. extern simple_object_write_section *
  131. simple_object_write_create_section (simple_object_write *simple_object,
  132. const char *name, unsigned int align,
  133. const char **errmsg, int *err);
  134. /* Add data BUFFER/SIZE to SECTION in SIMPLE_OBJECT. If COPY is
  135. non-zero, the data will be copied into memory if necessary. If
  136. COPY is zero, BUFFER must persist until SIMPLE_OBJECT is released.
  137. On success this returns NULL. On error this returns an error
  138. message, and sets *ERR to an errno value or 0 if there isn't
  139. one. */
  140. extern const char *
  141. simple_object_write_add_data (simple_object_write *simple_object,
  142. simple_object_write_section *section,
  143. const void *buffer, size_t size,
  144. int copy, int *err);
  145. /* Write the complete object file to DESCRIPTOR, an open file
  146. descriptor. This returns NULL on success. On error this returns
  147. an error message, and sets *ERR to an errno value or 0 if there
  148. isn't one. */
  149. extern const char *
  150. simple_object_write_to_file (simple_object_write *simple_object,
  151. int descriptor, int *err);
  152. /* Release all resources associated with SIMPLE_OBJECT, including any
  153. simple_object_write_section's that may have been created. */
  154. extern void
  155. simple_object_release_write (simple_object_write *);
  156. /* Copy LTO debug sections from SRC_OBJECT to DEST.
  157. If RENAME is true, rename LTO debug section into debug section (i.e.
  158. when producing final binary) and if it is false, keep the sections with
  159. original names (when incrementally linking).
  160. If an error occurs, return the errno value in ERR and an error string. */
  161. extern const char *
  162. simple_object_copy_lto_debug_sections (simple_object_read *src_object,
  163. const char *dest,
  164. int *err, int rename);
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif