simple-object.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* simple-object.c -- simple routines to read and write object files.
  2. Copyright 2010 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. #include "config.h"
  17. #include "libiberty.h"
  18. #include "simple-object.h"
  19. #include <errno.h>
  20. #ifdef HAVE_STDLIB_H
  21. #include <stdlib.h>
  22. #endif
  23. #ifdef HAVE_STDINT_H
  24. #include <stdint.h>
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #ifdef HAVE_INTTYPES_H
  30. #include <inttypes.h>
  31. #endif
  32. #ifndef SEEK_SET
  33. #define SEEK_SET 0
  34. #endif
  35. #include "simple-object-common.h"
  36. /* The known object file formats. */
  37. static const struct simple_object_functions * const format_functions[] =
  38. {
  39. &simple_object_elf_functions,
  40. &simple_object_mach_o_functions,
  41. &simple_object_coff_functions,
  42. &simple_object_xcoff_functions
  43. };
  44. /* Read data from a file using the simple_object error reporting
  45. conventions. */
  46. int
  47. simple_object_internal_read (int descriptor, off_t offset,
  48. unsigned char *buffer, size_t size,
  49. const char **errmsg, int *err)
  50. {
  51. if (lseek (descriptor, offset, SEEK_SET) < 0)
  52. {
  53. *errmsg = "lseek";
  54. *err = errno;
  55. return 0;
  56. }
  57. do
  58. {
  59. ssize_t got = read (descriptor, buffer, size);
  60. if (got == 0)
  61. break;
  62. else if (got > 0)
  63. {
  64. buffer += got;
  65. size -= got;
  66. }
  67. else if (errno != EINTR)
  68. {
  69. *errmsg = "read";
  70. *err = errno;
  71. return 0;
  72. }
  73. }
  74. while (size > 0);
  75. if (size > 0)
  76. {
  77. *errmsg = "file too short";
  78. *err = 0;
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. /* Write data to a file using the simple_object error reporting
  84. conventions. */
  85. int
  86. simple_object_internal_write (int descriptor, off_t offset,
  87. const unsigned char *buffer, size_t size,
  88. const char **errmsg, int *err)
  89. {
  90. if (lseek (descriptor, offset, SEEK_SET) < 0)
  91. {
  92. *errmsg = "lseek";
  93. *err = errno;
  94. return 0;
  95. }
  96. do
  97. {
  98. ssize_t wrote = write (descriptor, buffer, size);
  99. if (wrote == 0)
  100. break;
  101. else if (wrote > 0)
  102. {
  103. buffer += wrote;
  104. size -= wrote;
  105. }
  106. else if (errno != EINTR)
  107. {
  108. *errmsg = "write";
  109. *err = errno;
  110. return 0;
  111. }
  112. }
  113. while (size > 0);
  114. if (size > 0)
  115. {
  116. *errmsg = "short write";
  117. *err = 0;
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. /* Open for read. */
  123. simple_object_read *
  124. simple_object_start_read (int descriptor, off_t offset,
  125. const char *segment_name, const char **errmsg,
  126. int *err)
  127. {
  128. unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
  129. size_t len, i;
  130. if (!simple_object_internal_read (descriptor, offset, header,
  131. SIMPLE_OBJECT_MATCH_HEADER_LEN,
  132. errmsg, err))
  133. return NULL;
  134. len = sizeof (format_functions) / sizeof (format_functions[0]);
  135. for (i = 0; i < len; ++i)
  136. {
  137. void *data;
  138. data = format_functions[i]->match (header, descriptor, offset,
  139. segment_name, errmsg, err);
  140. if (data != NULL)
  141. {
  142. simple_object_read *ret;
  143. ret = XNEW (simple_object_read);
  144. ret->descriptor = descriptor;
  145. ret->offset = offset;
  146. ret->functions = format_functions[i];
  147. ret->data = data;
  148. return ret;
  149. }
  150. }
  151. *errmsg = "file not recognized";
  152. *err = 0;
  153. return NULL;
  154. }
  155. /* Find all sections. */
  156. const char *
  157. simple_object_find_sections (simple_object_read *sobj,
  158. int (*pfn) (void *, const char *, off_t, off_t),
  159. void *data,
  160. int *err)
  161. {
  162. return sobj->functions->find_sections (sobj, pfn, data, err);
  163. }
  164. /* Internal data passed to find_one_section. */
  165. struct find_one_section_data
  166. {
  167. /* The section we are looking for. */
  168. const char *name;
  169. /* Where to store the section offset. */
  170. off_t *offset;
  171. /* Where to store the section length. */
  172. off_t *length;
  173. /* Set if the name is found. */
  174. int found;
  175. };
  176. /* Internal function passed to find_sections. */
  177. static int
  178. find_one_section (void *data, const char *name, off_t offset, off_t length)
  179. {
  180. struct find_one_section_data *fosd = (struct find_one_section_data *) data;
  181. if (strcmp (name, fosd->name) != 0)
  182. return 1;
  183. *fosd->offset = offset;
  184. *fosd->length = length;
  185. fosd->found = 1;
  186. /* Stop iteration. */
  187. return 0;
  188. }
  189. /* Find a section. */
  190. int
  191. simple_object_find_section (simple_object_read *sobj, const char *name,
  192. off_t *offset, off_t *length,
  193. const char **errmsg, int *err)
  194. {
  195. struct find_one_section_data fosd;
  196. fosd.name = name;
  197. fosd.offset = offset;
  198. fosd.length = length;
  199. fosd.found = 0;
  200. *errmsg = simple_object_find_sections (sobj, find_one_section,
  201. (void *) &fosd, err);
  202. if (*errmsg != NULL)
  203. return 0;
  204. if (!fosd.found)
  205. return 0;
  206. return 1;
  207. }
  208. /* Fetch attributes. */
  209. simple_object_attributes *
  210. simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
  211. int *err)
  212. {
  213. void *data;
  214. simple_object_attributes *ret;
  215. data = sobj->functions->fetch_attributes (sobj, errmsg, err);
  216. if (data == NULL)
  217. return NULL;
  218. ret = XNEW (simple_object_attributes);
  219. ret->functions = sobj->functions;
  220. ret->data = data;
  221. return ret;
  222. }
  223. /* Release an simple_object_read. */
  224. void
  225. simple_object_release_read (simple_object_read *sobj)
  226. {
  227. sobj->functions->release_read (sobj->data);
  228. XDELETE (sobj);
  229. }
  230. /* Merge attributes. */
  231. const char *
  232. simple_object_attributes_merge (simple_object_attributes *to,
  233. simple_object_attributes *from,
  234. int *err)
  235. {
  236. if (to->functions != from->functions)
  237. {
  238. *err = 0;
  239. return "different object file format";
  240. }
  241. return to->functions->attributes_merge (to->data, from->data, err);
  242. }
  243. /* Release an attributes structure. */
  244. void
  245. simple_object_release_attributes (simple_object_attributes *attrs)
  246. {
  247. attrs->functions->release_attributes (attrs->data);
  248. XDELETE (attrs);
  249. }
  250. /* Start creating an object file. */
  251. simple_object_write *
  252. simple_object_start_write (simple_object_attributes *attrs,
  253. const char *segment_name, const char **errmsg,
  254. int *err)
  255. {
  256. void *data;
  257. simple_object_write *ret;
  258. data = attrs->functions->start_write (attrs->data, errmsg, err);
  259. if (data == NULL)
  260. return NULL;
  261. ret = XNEW (simple_object_write);
  262. ret->functions = attrs->functions;
  263. ret->segment_name = xstrdup (segment_name);
  264. ret->sections = NULL;
  265. ret->last_section = NULL;
  266. ret->data = data;
  267. return ret;
  268. }
  269. /* Start creating a section. */
  270. simple_object_write_section *
  271. simple_object_write_create_section (simple_object_write *sobj, const char *name,
  272. unsigned int align,
  273. const char **errmsg ATTRIBUTE_UNUSED,
  274. int *err ATTRIBUTE_UNUSED)
  275. {
  276. simple_object_write_section *ret;
  277. ret = XNEW (simple_object_write_section);
  278. ret->next = NULL;
  279. ret->name = xstrdup (name);
  280. ret->align = align;
  281. ret->buffers = NULL;
  282. ret->last_buffer = NULL;
  283. if (sobj->last_section == NULL)
  284. {
  285. sobj->sections = ret;
  286. sobj->last_section = ret;
  287. }
  288. else
  289. {
  290. sobj->last_section->next = ret;
  291. sobj->last_section = ret;
  292. }
  293. return ret;
  294. }
  295. /* Add data to a section. */
  296. const char *
  297. simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
  298. simple_object_write_section *section,
  299. const void *buffer,
  300. size_t size, int copy,
  301. int *err ATTRIBUTE_UNUSED)
  302. {
  303. struct simple_object_write_section_buffer *wsb;
  304. wsb = XNEW (struct simple_object_write_section_buffer);
  305. wsb->next = NULL;
  306. wsb->size = size;
  307. if (!copy)
  308. {
  309. wsb->buffer = buffer;
  310. wsb->free_buffer = NULL;
  311. }
  312. else
  313. {
  314. wsb->free_buffer = (void *) XNEWVEC (char, size);
  315. memcpy (wsb->free_buffer, buffer, size);
  316. wsb->buffer = wsb->free_buffer;
  317. }
  318. if (section->last_buffer == NULL)
  319. {
  320. section->buffers = wsb;
  321. section->last_buffer = wsb;
  322. }
  323. else
  324. {
  325. section->last_buffer->next = wsb;
  326. section->last_buffer = wsb;
  327. }
  328. return NULL;
  329. }
  330. /* Write the complete object file. */
  331. const char *
  332. simple_object_write_to_file (simple_object_write *sobj, int descriptor,
  333. int *err)
  334. {
  335. return sobj->functions->write_to_file (sobj, descriptor, err);
  336. }
  337. /* Release an simple_object_write. */
  338. void
  339. simple_object_release_write (simple_object_write *sobj)
  340. {
  341. simple_object_write_section *section;
  342. free (sobj->segment_name);
  343. section = sobj->sections;
  344. while (section != NULL)
  345. {
  346. struct simple_object_write_section_buffer *buffer;
  347. simple_object_write_section *next_section;
  348. buffer = section->buffers;
  349. while (buffer != NULL)
  350. {
  351. struct simple_object_write_section_buffer *next_buffer;
  352. if (buffer->free_buffer != NULL)
  353. XDELETEVEC (buffer->free_buffer);
  354. next_buffer = buffer->next;
  355. XDELETE (buffer);
  356. buffer = next_buffer;
  357. }
  358. next_section = section->next;
  359. free (section->name);
  360. XDELETE (section);
  361. section = next_section;
  362. }
  363. sobj->functions->release_write (sobj->data);
  364. XDELETE (sobj);
  365. }