simple-object-common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* simple-object-common.h -- common structs for object file manipulation.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. This file is part of the libiberty library.
  4. Libiberty is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. Libiberty 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 GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with libiberty; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /* Forward reference. */
  17. struct simple_object_functions;
  18. /* An object file opened for reading. */
  19. struct simple_object_read_struct
  20. {
  21. /* The file descriptor. */
  22. int descriptor;
  23. /* The offset within the file. */
  24. off_t offset;
  25. /* The functions which do the actual work. */
  26. const struct simple_object_functions *functions;
  27. /* Private data for the object file format. */
  28. void *data;
  29. };
  30. /* Object file attributes. */
  31. struct simple_object_attributes_struct
  32. {
  33. /* The functions which do the actual work. */
  34. const struct simple_object_functions *functions;
  35. /* Private data for the object file format. */
  36. void *data;
  37. };
  38. /* An object file being created. */
  39. struct simple_object_write_struct
  40. {
  41. /* The functions which do the actual work. */
  42. const struct simple_object_functions *functions;
  43. /* The segment_name argument from the user. */
  44. char *segment_name;
  45. /* The start of the list of sections. */
  46. simple_object_write_section *sections;
  47. /* The last entry in the list of sections. */
  48. simple_object_write_section *last_section;
  49. /* Private data for the object file format. */
  50. void *data;
  51. };
  52. /* A section in an object file being created. */
  53. struct simple_object_write_section_struct
  54. {
  55. /* Next in the list of sections attached to an
  56. simple_object_write. */
  57. simple_object_write_section *next;
  58. /* The name of this section. */
  59. char *name;
  60. /* The required alignment. */
  61. unsigned int align;
  62. /* The first data attached to this section. */
  63. struct simple_object_write_section_buffer *buffers;
  64. /* The last data attached to this section. */
  65. struct simple_object_write_section_buffer *last_buffer;
  66. };
  67. /* Data attached to a section. */
  68. struct simple_object_write_section_buffer
  69. {
  70. /* The next data for this section. */
  71. struct simple_object_write_section_buffer *next;
  72. /* The size of the buffer. */
  73. size_t size;
  74. /* The actual bytes. */
  75. const void *buffer;
  76. /* A buffer to free, or NULL. */
  77. void *free_buffer;
  78. };
  79. /* The number of bytes we read from the start of the file to pass to
  80. the match function. */
  81. #define SIMPLE_OBJECT_MATCH_HEADER_LEN (16)
  82. /* Format-specific object file functions. */
  83. struct simple_object_functions
  84. {
  85. /* If this file matches these functions, return a new value for the
  86. private data for an simple_object_read. HEADER is the first 16
  87. bytes of the file. DESCRIPTOR, OFFSET, SEGMENT_NAME, ERRMSG, and
  88. ERR are as for simple_object_open_read. If this file does not
  89. match, this function should return NULL with *ERRMSG set to
  90. NULL. */
  91. void *(*match) (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
  92. int descriptor, off_t offset, const char *segment_name,
  93. const char **errmsg, int *err);
  94. /* Implement simple_object_find_sections. */
  95. const char *(*find_sections) (simple_object_read *,
  96. int (*pfn) (void *, const char *,
  97. off_t offset, off_t length),
  98. void *data,
  99. int *err);
  100. /* Return the private data for the attributes for SOBJ. */
  101. void *(*fetch_attributes) (simple_object_read *sobj, const char **errmsg,
  102. int *err);
  103. /* Release the private data for an simple_object_read. */
  104. void (*release_read) (void *);
  105. /* Merge the private data for the attributes of two files. If they
  106. could be linked together, return NULL. Otherwise return an error
  107. message. */
  108. const char *(*attributes_merge) (void *, void *, int *err);
  109. /* Release the private data for an simple_object_attributes. */
  110. void (*release_attributes) (void *);
  111. /* Start creating an object file. */
  112. void *(*start_write) (void *attributes_data, const char **errmsg,
  113. int *err);
  114. /* Write the complete object file. */
  115. const char *(*write_to_file) (simple_object_write *sobj, int descriptor,
  116. int *err);
  117. /* Release the private data for an simple_object_write. */
  118. void (*release_write) (void *);
  119. /* Copy LTO debug sections. */
  120. const char *(*copy_lto_debug_sections) (simple_object_read *sobj,
  121. simple_object_write *dobj,
  122. char *(*pfn) (const char *),
  123. int *err);
  124. };
  125. /* The known object file formats. */
  126. extern const struct simple_object_functions simple_object_coff_functions;
  127. extern const struct simple_object_functions simple_object_elf_functions;
  128. extern const struct simple_object_functions simple_object_mach_o_functions;
  129. extern const struct simple_object_functions simple_object_xcoff_functions;
  130. /* Read SIZE bytes from DESCRIPTOR at file offset OFFSET into BUFFER.
  131. Return non-zero on success. On failure return 0 and set *ERRMSG
  132. and *ERR. */
  133. extern int
  134. simple_object_internal_read (int descriptor, off_t offset,
  135. unsigned char *buffer, size_t size,
  136. const char **errmsg, int *err);
  137. /* Write SIZE bytes from BUFFER to DESCRIPTOR at file offset OFFSET.
  138. Return non-zero on success. On failure return 0 and set *ERRMSG
  139. and *ERR. */
  140. extern int
  141. simple_object_internal_write (int descriptor, off_t offset,
  142. const unsigned char *buffer, size_t size,
  143. const char **errmsg, int *err);
  144. /* Define ulong_type as an unsigned 64-bit type if available.
  145. Otherwise just make it unsigned long. */
  146. #ifdef UNSIGNED_64BIT_TYPE
  147. __extension__ typedef UNSIGNED_64BIT_TYPE ulong_type;
  148. #else
  149. typedef unsigned long ulong_type;
  150. #endif
  151. /* Fetch a big-endian 16-bit value. */
  152. static inline unsigned short
  153. simple_object_fetch_big_16 (const unsigned char *buf)
  154. {
  155. return ((unsigned short) buf[0] << 8) | (unsigned short) buf[1];
  156. }
  157. /* Fetch a little-endian 16-bit value. */
  158. static inline unsigned short
  159. simple_object_fetch_little_16 (const unsigned char *buf)
  160. {
  161. return ((unsigned short) buf[1] << 8) | (unsigned short) buf[0];
  162. }
  163. /* Fetch a big-endian 32-bit value. */
  164. static inline unsigned int
  165. simple_object_fetch_big_32 (const unsigned char *buf)
  166. {
  167. return (((unsigned int) buf[0] << 24)
  168. | ((unsigned int) buf[1] << 16)
  169. | ((unsigned int) buf[2] << 8)
  170. | (unsigned int) buf[3]);
  171. }
  172. /* Fetch a little-endian 32-bit value. */
  173. static inline unsigned int
  174. simple_object_fetch_little_32 (const unsigned char *buf)
  175. {
  176. return (((unsigned int) buf[3] << 24)
  177. | ((unsigned int) buf[2] << 16)
  178. | ((unsigned int) buf[1] << 8)
  179. | (unsigned int) buf[0]);
  180. }
  181. /* Fetch a big-endian 32-bit value as a ulong_type. */
  182. static inline ulong_type
  183. simple_object_fetch_big_32_ulong (const unsigned char *buf)
  184. {
  185. return (ulong_type) simple_object_fetch_big_32 (buf);
  186. }
  187. /* Fetch a little-endian 32-bit value as a ulong_type. */
  188. static inline ulong_type
  189. simple_object_fetch_little_32_ulong (const unsigned char *buf)
  190. {
  191. return (ulong_type) simple_object_fetch_little_32 (buf);
  192. }
  193. #ifdef UNSIGNED_64BIT_TYPE
  194. /* Fetch a big-endian 64-bit value. */
  195. static inline ulong_type
  196. simple_object_fetch_big_64 (const unsigned char *buf)
  197. {
  198. return (((ulong_type) buf[0] << 56)
  199. | ((ulong_type) buf[1] << 48)
  200. | ((ulong_type) buf[2] << 40)
  201. | ((ulong_type) buf[3] << 32)
  202. | ((ulong_type) buf[4] << 24)
  203. | ((ulong_type) buf[5] << 16)
  204. | ((ulong_type) buf[6] << 8)
  205. | (ulong_type) buf[7]);
  206. }
  207. /* Fetch a little-endian 64-bit value. */
  208. static inline ulong_type
  209. simple_object_fetch_little_64 (const unsigned char *buf)
  210. {
  211. return (((ulong_type) buf[7] << 56)
  212. | ((ulong_type) buf[6] << 48)
  213. | ((ulong_type) buf[5] << 40)
  214. | ((ulong_type) buf[4] << 32)
  215. | ((ulong_type) buf[3] << 24)
  216. | ((ulong_type) buf[2] << 16)
  217. | ((ulong_type) buf[1] << 8)
  218. | (ulong_type) buf[0]);
  219. }
  220. #endif
  221. /* Store a big-endian 16-bit value. */
  222. static inline void
  223. simple_object_set_big_16 (unsigned char *buf, unsigned short val)
  224. {
  225. buf[0] = (val >> 8) & 0xff;
  226. buf[1] = val & 0xff;
  227. }
  228. /* Store a little-endian 16-bit value. */
  229. static inline void
  230. simple_object_set_little_16 (unsigned char *buf, unsigned short val)
  231. {
  232. buf[1] = (val >> 8) & 0xff;
  233. buf[0] = val & 0xff;
  234. }
  235. /* Store a big-endian 32-bit value. */
  236. static inline void
  237. simple_object_set_big_32 (unsigned char *buf, unsigned int val)
  238. {
  239. buf[0] = (val >> 24) & 0xff;
  240. buf[1] = (val >> 16) & 0xff;
  241. buf[2] = (val >> 8) & 0xff;
  242. buf[3] = val & 0xff;
  243. }
  244. /* Store a little-endian 32-bit value. */
  245. static inline void
  246. simple_object_set_little_32 (unsigned char *buf, unsigned int val)
  247. {
  248. buf[3] = (val >> 24) & 0xff;
  249. buf[2] = (val >> 16) & 0xff;
  250. buf[1] = (val >> 8) & 0xff;
  251. buf[0] = val & 0xff;
  252. }
  253. /* Store a big-endian 32-bit value coming in as a ulong_type. */
  254. static inline void
  255. simple_object_set_big_32_ulong (unsigned char *buf, ulong_type val)
  256. {
  257. simple_object_set_big_32 (buf, val);
  258. }
  259. /* Store a little-endian 32-bit value coming in as a ulong_type. */
  260. static inline void
  261. simple_object_set_little_32_ulong (unsigned char *buf, ulong_type val)
  262. {
  263. simple_object_set_little_32 (buf, val);
  264. }
  265. #ifdef UNSIGNED_64BIT_TYPE
  266. /* Store a big-endian 64-bit value. */
  267. static inline void
  268. simple_object_set_big_64 (unsigned char *buf, ulong_type val)
  269. {
  270. buf[0] = (val >> 56) & 0xff;
  271. buf[1] = (val >> 48) & 0xff;
  272. buf[2] = (val >> 40) & 0xff;
  273. buf[3] = (val >> 32) & 0xff;
  274. buf[4] = (val >> 24) & 0xff;
  275. buf[5] = (val >> 16) & 0xff;
  276. buf[6] = (val >> 8) & 0xff;
  277. buf[7] = val & 0xff;
  278. }
  279. /* Store a little-endian 64-bit value. */
  280. static inline void
  281. simple_object_set_little_64 (unsigned char *buf, ulong_type val)
  282. {
  283. buf[7] = (val >> 56) & 0xff;
  284. buf[6] = (val >> 48) & 0xff;
  285. buf[5] = (val >> 40) & 0xff;
  286. buf[4] = (val >> 32) & 0xff;
  287. buf[3] = (val >> 24) & 0xff;
  288. buf[2] = (val >> 16) & 0xff;
  289. buf[1] = (val >> 8) & 0xff;
  290. buf[0] = val & 0xff;
  291. }
  292. #endif