simple-object-common.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* simple-object-common.h -- common structs for object file manipulation.
  2. Copyright (C) 2010 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. };
  120. /* The known object file formats. */
  121. extern const struct simple_object_functions simple_object_coff_functions;
  122. extern const struct simple_object_functions simple_object_elf_functions;
  123. extern const struct simple_object_functions simple_object_mach_o_functions;
  124. extern const struct simple_object_functions simple_object_xcoff_functions;
  125. /* Read SIZE bytes from DESCRIPTOR at file offset OFFSET into BUFFER.
  126. Return non-zero on success. On failure return 0 and set *ERRMSG
  127. and *ERR. */
  128. extern int
  129. simple_object_internal_read (int descriptor, off_t offset,
  130. unsigned char *buffer, size_t size,
  131. const char **errmsg, int *err);
  132. /* Write SIZE bytes from BUFFER to DESCRIPTOR at file offset OFFSET.
  133. Return non-zero on success. On failure return 0 and set *ERRMSG
  134. and *ERR. */
  135. extern int
  136. simple_object_internal_write (int descriptor, off_t offset,
  137. const unsigned char *buffer, size_t size,
  138. const char **errmsg, int *err);
  139. /* Define ulong_type as an unsigned 64-bit type if available.
  140. Otherwise just make it unsigned long. */
  141. #ifdef UNSIGNED_64BIT_TYPE
  142. __extension__ typedef UNSIGNED_64BIT_TYPE ulong_type;
  143. #else
  144. typedef unsigned long ulong_type;
  145. #endif
  146. /* Fetch a big-endian 16-bit value. */
  147. static inline unsigned short
  148. simple_object_fetch_big_16 (const unsigned char *buf)
  149. {
  150. return ((unsigned short) buf[0] << 8) | (unsigned short) buf[1];
  151. }
  152. /* Fetch a little-endian 16-bit value. */
  153. static inline unsigned short
  154. simple_object_fetch_little_16 (const unsigned char *buf)
  155. {
  156. return ((unsigned short) buf[1] << 8) | (unsigned short) buf[0];
  157. }
  158. /* Fetch a big-endian 32-bit value. */
  159. static inline unsigned int
  160. simple_object_fetch_big_32 (const unsigned char *buf)
  161. {
  162. return (((unsigned int) buf[0] << 24)
  163. | ((unsigned int) buf[1] << 16)
  164. | ((unsigned int) buf[2] << 8)
  165. | (unsigned int) buf[3]);
  166. }
  167. /* Fetch a little-endian 32-bit value. */
  168. static inline unsigned int
  169. simple_object_fetch_little_32 (const unsigned char *buf)
  170. {
  171. return (((unsigned int) buf[3] << 24)
  172. | ((unsigned int) buf[2] << 16)
  173. | ((unsigned int) buf[1] << 8)
  174. | (unsigned int) buf[0]);
  175. }
  176. /* Fetch a big-endian 32-bit value as a ulong_type. */
  177. static inline ulong_type
  178. simple_object_fetch_big_32_ulong (const unsigned char *buf)
  179. {
  180. return (ulong_type) simple_object_fetch_big_32 (buf);
  181. }
  182. /* Fetch a little-endian 32-bit value as a ulong_type. */
  183. static inline ulong_type
  184. simple_object_fetch_little_32_ulong (const unsigned char *buf)
  185. {
  186. return (ulong_type) simple_object_fetch_little_32 (buf);
  187. }
  188. #ifdef UNSIGNED_64BIT_TYPE
  189. /* Fetch a big-endian 64-bit value. */
  190. static inline ulong_type
  191. simple_object_fetch_big_64 (const unsigned char *buf)
  192. {
  193. return (((ulong_type) buf[0] << 56)
  194. | ((ulong_type) buf[1] << 48)
  195. | ((ulong_type) buf[2] << 40)
  196. | ((ulong_type) buf[3] << 32)
  197. | ((ulong_type) buf[4] << 24)
  198. | ((ulong_type) buf[5] << 16)
  199. | ((ulong_type) buf[6] << 8)
  200. | (ulong_type) buf[7]);
  201. }
  202. /* Fetch a little-endian 64-bit value. */
  203. static inline ulong_type
  204. simple_object_fetch_little_64 (const unsigned char *buf)
  205. {
  206. return (((ulong_type) buf[7] << 56)
  207. | ((ulong_type) buf[6] << 48)
  208. | ((ulong_type) buf[5] << 40)
  209. | ((ulong_type) buf[4] << 32)
  210. | ((ulong_type) buf[3] << 24)
  211. | ((ulong_type) buf[2] << 16)
  212. | ((ulong_type) buf[1] << 8)
  213. | (ulong_type) buf[0]);
  214. }
  215. #endif
  216. /* Store a big-endian 16-bit value. */
  217. static inline void
  218. simple_object_set_big_16 (unsigned char *buf, unsigned short val)
  219. {
  220. buf[0] = (val >> 8) & 0xff;
  221. buf[1] = val & 0xff;
  222. }
  223. /* Store a little-endian 16-bit value. */
  224. static inline void
  225. simple_object_set_little_16 (unsigned char *buf, unsigned short val)
  226. {
  227. buf[1] = (val >> 8) & 0xff;
  228. buf[0] = val & 0xff;
  229. }
  230. /* Store a big-endian 32-bit value. */
  231. static inline void
  232. simple_object_set_big_32 (unsigned char *buf, unsigned int val)
  233. {
  234. buf[0] = (val >> 24) & 0xff;
  235. buf[1] = (val >> 16) & 0xff;
  236. buf[2] = (val >> 8) & 0xff;
  237. buf[3] = val & 0xff;
  238. }
  239. /* Store a little-endian 32-bit value. */
  240. static inline void
  241. simple_object_set_little_32 (unsigned char *buf, unsigned int val)
  242. {
  243. buf[3] = (val >> 24) & 0xff;
  244. buf[2] = (val >> 16) & 0xff;
  245. buf[1] = (val >> 8) & 0xff;
  246. buf[0] = val & 0xff;
  247. }
  248. /* Store a big-endian 32-bit value coming in as a ulong_type. */
  249. static inline void
  250. simple_object_set_big_32_ulong (unsigned char *buf, ulong_type val)
  251. {
  252. simple_object_set_big_32 (buf, val);
  253. }
  254. /* Store a little-endian 32-bit value coming in as a ulong_type. */
  255. static inline void
  256. simple_object_set_little_32_ulong (unsigned char *buf, ulong_type val)
  257. {
  258. simple_object_set_little_32 (buf, val);
  259. }
  260. #ifdef UNSIGNED_64BIT_TYPE
  261. /* Store a big-endian 64-bit value. */
  262. static inline void
  263. simple_object_set_big_64 (unsigned char *buf, ulong_type val)
  264. {
  265. buf[0] = (val >> 56) & 0xff;
  266. buf[1] = (val >> 48) & 0xff;
  267. buf[2] = (val >> 40) & 0xff;
  268. buf[3] = (val >> 32) & 0xff;
  269. buf[4] = (val >> 24) & 0xff;
  270. buf[5] = (val >> 16) & 0xff;
  271. buf[6] = (val >> 8) & 0xff;
  272. buf[7] = val & 0xff;
  273. }
  274. /* Store a little-endian 64-bit value. */
  275. static inline void
  276. simple_object_set_little_64 (unsigned char *buf, ulong_type val)
  277. {
  278. buf[7] = (val >> 56) & 0xff;
  279. buf[6] = (val >> 48) & 0xff;
  280. buf[5] = (val >> 40) & 0xff;
  281. buf[4] = (val >> 32) & 0xff;
  282. buf[3] = (val >> 24) & 0xff;
  283. buf[2] = (val >> 16) & 0xff;
  284. buf[1] = (val >> 8) & 0xff;
  285. buf[0] = val & 0xff;
  286. }
  287. #endif