simple-object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* simple-object.c -- 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. #include "config.h"
  17. #include "libiberty.h"
  18. #include "simple-object.h"
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #ifdef HAVE_STDLIB_H
  22. #include <stdlib.h>
  23. #endif
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #ifdef HAVE_STRING_H
  28. #include <string.h>
  29. #endif
  30. #ifdef HAVE_INTTYPES_H
  31. #include <inttypes.h>
  32. #endif
  33. #ifndef SEEK_SET
  34. #define SEEK_SET 0
  35. #endif
  36. #ifndef O_BINARY
  37. #define O_BINARY 0
  38. #endif
  39. #include "simple-object-common.h"
  40. /* The known object file formats. */
  41. static const struct simple_object_functions * const format_functions[] =
  42. {
  43. &simple_object_elf_functions,
  44. &simple_object_mach_o_functions,
  45. &simple_object_coff_functions,
  46. &simple_object_xcoff_functions
  47. };
  48. /* Read data from a file using the simple_object error reporting
  49. conventions. */
  50. int
  51. simple_object_internal_read (int descriptor, off_t offset,
  52. unsigned char *buffer, size_t size,
  53. const char **errmsg, int *err)
  54. {
  55. if (lseek (descriptor, offset, SEEK_SET) < 0)
  56. {
  57. *errmsg = "lseek";
  58. *err = errno;
  59. return 0;
  60. }
  61. do
  62. {
  63. ssize_t got = read (descriptor, buffer, size);
  64. if (got == 0)
  65. break;
  66. else if (got > 0)
  67. {
  68. buffer += got;
  69. size -= got;
  70. }
  71. else if (errno != EINTR)
  72. {
  73. *errmsg = "read";
  74. *err = errno;
  75. return 0;
  76. }
  77. }
  78. while (size > 0);
  79. if (size > 0)
  80. {
  81. *errmsg = "file too short";
  82. *err = 0;
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. /* Write data to a file using the simple_object error reporting
  88. conventions. */
  89. int
  90. simple_object_internal_write (int descriptor, off_t offset,
  91. const unsigned char *buffer, size_t size,
  92. const char **errmsg, int *err)
  93. {
  94. if (lseek (descriptor, offset, SEEK_SET) < 0)
  95. {
  96. *errmsg = "lseek";
  97. *err = errno;
  98. return 0;
  99. }
  100. do
  101. {
  102. ssize_t wrote = write (descriptor, buffer, size);
  103. if (wrote == 0)
  104. break;
  105. else if (wrote > 0)
  106. {
  107. buffer += wrote;
  108. size -= wrote;
  109. }
  110. else if (errno != EINTR)
  111. {
  112. *errmsg = "write";
  113. *err = errno;
  114. return 0;
  115. }
  116. }
  117. while (size > 0);
  118. if (size > 0)
  119. {
  120. *errmsg = "short write";
  121. *err = 0;
  122. return 0;
  123. }
  124. return 1;
  125. }
  126. /* Open for read. */
  127. simple_object_read *
  128. simple_object_start_read (int descriptor, off_t offset,
  129. const char *segment_name, const char **errmsg,
  130. int *err)
  131. {
  132. unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
  133. size_t len, i;
  134. if (!simple_object_internal_read (descriptor, offset, header,
  135. SIMPLE_OBJECT_MATCH_HEADER_LEN,
  136. errmsg, err))
  137. return NULL;
  138. len = sizeof (format_functions) / sizeof (format_functions[0]);
  139. for (i = 0; i < len; ++i)
  140. {
  141. void *data;
  142. data = format_functions[i]->match (header, descriptor, offset,
  143. segment_name, errmsg, err);
  144. if (data != NULL)
  145. {
  146. simple_object_read *ret;
  147. ret = XNEW (simple_object_read);
  148. ret->descriptor = descriptor;
  149. ret->offset = offset;
  150. ret->functions = format_functions[i];
  151. ret->data = data;
  152. return ret;
  153. }
  154. }
  155. *errmsg = "file not recognized";
  156. *err = 0;
  157. return NULL;
  158. }
  159. /* Find all sections. */
  160. const char *
  161. simple_object_find_sections (simple_object_read *sobj,
  162. int (*pfn) (void *, const char *, off_t, off_t),
  163. void *data,
  164. int *err)
  165. {
  166. return sobj->functions->find_sections (sobj, pfn, data, err);
  167. }
  168. /* Internal data passed to find_one_section. */
  169. struct find_one_section_data
  170. {
  171. /* The section we are looking for. */
  172. const char *name;
  173. /* Where to store the section offset. */
  174. off_t *offset;
  175. /* Where to store the section length. */
  176. off_t *length;
  177. /* Set if the name is found. */
  178. int found;
  179. };
  180. /* Internal function passed to find_sections. */
  181. static int
  182. find_one_section (void *data, const char *name, off_t offset, off_t length)
  183. {
  184. struct find_one_section_data *fosd = (struct find_one_section_data *) data;
  185. if (strcmp (name, fosd->name) != 0)
  186. return 1;
  187. *fosd->offset = offset;
  188. *fosd->length = length;
  189. fosd->found = 1;
  190. /* Stop iteration. */
  191. return 0;
  192. }
  193. /* Find a section. */
  194. int
  195. simple_object_find_section (simple_object_read *sobj, const char *name,
  196. off_t *offset, off_t *length,
  197. const char **errmsg, int *err)
  198. {
  199. struct find_one_section_data fosd;
  200. fosd.name = name;
  201. fosd.offset = offset;
  202. fosd.length = length;
  203. fosd.found = 0;
  204. *errmsg = simple_object_find_sections (sobj, find_one_section,
  205. (void *) &fosd, err);
  206. if (*errmsg != NULL)
  207. return 0;
  208. if (!fosd.found)
  209. return 0;
  210. return 1;
  211. }
  212. /* Callback to identify and rename LTO debug sections by name.
  213. Returns non-NULL if NAME is a LTO debug section, NULL if not.
  214. If RENAME is true it will rename LTO debug sections to non-LTO
  215. ones. */
  216. static char *
  217. handle_lto_debug_sections (const char *name, int rename)
  218. {
  219. char *newname = rename ? XCNEWVEC (char, strlen (name) + 1)
  220. : xstrdup (name);
  221. /* ??? So we can't use .gnu.lto_ prefixed sections as the assembler
  222. complains about bogus section flags. Which means we need to arrange
  223. for that to be fixed or .gnu.debuglto_ marked as SHF_EXCLUDE (to make
  224. fat lto object tooling work for the fat part). */
  225. /* Also include corresponding reloc sections. */
  226. if (strncmp (name, ".rela", sizeof (".rela") - 1) == 0)
  227. {
  228. if (rename)
  229. strncpy (newname, name, sizeof (".rela") - 1);
  230. name += sizeof (".rela") - 1;
  231. }
  232. else if (strncmp (name, ".rel", sizeof (".rel") - 1) == 0)
  233. {
  234. if (rename)
  235. strncpy (newname, name, sizeof (".rel") - 1);
  236. name += sizeof (".rel") - 1;
  237. }
  238. /* ??? For now this handles both .gnu.lto_ and .gnu.debuglto_ prefixed
  239. sections. */
  240. /* Copy LTO debug sections and rename them to their non-LTO name. */
  241. if (strncmp (name, ".gnu.debuglto_", sizeof (".gnu.debuglto_") - 1) == 0)
  242. return rename ? strcat (newname, name + sizeof (".gnu.debuglto_") - 1) : newname;
  243. else if (strncmp (name, ".gnu.lto_.debug_",
  244. sizeof (".gnu.lto_.debug_") -1) == 0)
  245. return rename ? strcat (newname, name + sizeof (".gnu.lto_") - 1) : newname;
  246. /* Copy over .note.GNU-stack section under the same name if present. */
  247. else if (strcmp (name, ".note.GNU-stack") == 0)
  248. return strcpy (newname, name);
  249. /* Copy over .note.gnu.property section under the same name if present. */
  250. else if (strcmp (name, ".note.gnu.property") == 0)
  251. return strcpy (newname, name);
  252. /* Copy over .comment section under the same name if present. Solaris
  253. ld uses them to relax its checking of ELF gABI access rules for
  254. COMDAT sections in objects produced by GCC. */
  255. else if (strcmp (name, ".comment") == 0)
  256. return strcpy (newname, name);
  257. /* Copy over .GCC.command.line section under the same name if present. */
  258. else if (strcmp (name, ".GCC.command.line") == 0)
  259. return strcpy (newname, name);
  260. /* Copy over .ctf section under the same name if present. */
  261. else if (strcmp (name, ".ctf") == 0)
  262. return strcpy (newname, name);
  263. /* Copy over .BTF section under the same name if present. */
  264. else if (strcmp (name, ".BTF") == 0)
  265. return strcpy (newname, name);
  266. free (newname);
  267. return NULL;
  268. }
  269. /* Wrapper for handle_lto_debug_sections. */
  270. static char *
  271. handle_lto_debug_sections_rename (const char *name)
  272. {
  273. return handle_lto_debug_sections (name, 1);
  274. }
  275. /* Wrapper for handle_lto_debug_sections. */
  276. static char *
  277. handle_lto_debug_sections_norename (const char *name)
  278. {
  279. return handle_lto_debug_sections (name, 0);
  280. }
  281. /* Copy LTO debug sections. */
  282. const char *
  283. simple_object_copy_lto_debug_sections (simple_object_read *sobj,
  284. const char *dest, int *err, int rename)
  285. {
  286. const char *errmsg;
  287. simple_object_write *dest_sobj;
  288. simple_object_attributes *attrs;
  289. int outfd;
  290. if (! sobj->functions->copy_lto_debug_sections)
  291. {
  292. *err = EINVAL;
  293. return "simple_object_copy_lto_debug_sections not implemented";
  294. }
  295. attrs = simple_object_fetch_attributes (sobj, &errmsg, err);
  296. if (! attrs)
  297. return errmsg;
  298. dest_sobj = simple_object_start_write (attrs, NULL, &errmsg, err);
  299. simple_object_release_attributes (attrs);
  300. if (! dest_sobj)
  301. return errmsg;
  302. errmsg = sobj->functions->copy_lto_debug_sections
  303. (sobj, dest_sobj,
  304. rename ? handle_lto_debug_sections_rename
  305. : handle_lto_debug_sections_norename, err);
  306. if (errmsg)
  307. {
  308. simple_object_release_write (dest_sobj);
  309. return errmsg;
  310. }
  311. outfd = open (dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 00777);
  312. if (outfd == -1)
  313. {
  314. *err = errno;
  315. simple_object_release_write (dest_sobj);
  316. return "open failed";
  317. }
  318. errmsg = simple_object_write_to_file (dest_sobj, outfd, err);
  319. close (outfd);
  320. if (errmsg)
  321. {
  322. simple_object_release_write (dest_sobj);
  323. return errmsg;
  324. }
  325. simple_object_release_write (dest_sobj);
  326. return NULL;
  327. }
  328. /* Fetch attributes. */
  329. simple_object_attributes *
  330. simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
  331. int *err)
  332. {
  333. void *data;
  334. simple_object_attributes *ret;
  335. data = sobj->functions->fetch_attributes (sobj, errmsg, err);
  336. if (data == NULL)
  337. return NULL;
  338. ret = XNEW (simple_object_attributes);
  339. ret->functions = sobj->functions;
  340. ret->data = data;
  341. return ret;
  342. }
  343. /* Release an simple_object_read. */
  344. void
  345. simple_object_release_read (simple_object_read *sobj)
  346. {
  347. sobj->functions->release_read (sobj->data);
  348. XDELETE (sobj);
  349. }
  350. /* Merge attributes. */
  351. const char *
  352. simple_object_attributes_merge (simple_object_attributes *to,
  353. simple_object_attributes *from,
  354. int *err)
  355. {
  356. if (to->functions != from->functions)
  357. {
  358. *err = 0;
  359. return "different object file format";
  360. }
  361. return to->functions->attributes_merge (to->data, from->data, err);
  362. }
  363. /* Release an attributes structure. */
  364. void
  365. simple_object_release_attributes (simple_object_attributes *attrs)
  366. {
  367. attrs->functions->release_attributes (attrs->data);
  368. XDELETE (attrs);
  369. }
  370. /* Start creating an object file. */
  371. simple_object_write *
  372. simple_object_start_write (simple_object_attributes *attrs,
  373. const char *segment_name, const char **errmsg,
  374. int *err)
  375. {
  376. void *data;
  377. simple_object_write *ret;
  378. data = attrs->functions->start_write (attrs->data, errmsg, err);
  379. if (data == NULL)
  380. return NULL;
  381. ret = XNEW (simple_object_write);
  382. ret->functions = attrs->functions;
  383. ret->segment_name = segment_name ? xstrdup (segment_name) : NULL;
  384. ret->sections = NULL;
  385. ret->last_section = NULL;
  386. ret->data = data;
  387. return ret;
  388. }
  389. /* Start creating a section. */
  390. simple_object_write_section *
  391. simple_object_write_create_section (simple_object_write *sobj, const char *name,
  392. unsigned int align,
  393. const char **errmsg ATTRIBUTE_UNUSED,
  394. int *err ATTRIBUTE_UNUSED)
  395. {
  396. simple_object_write_section *ret;
  397. ret = XNEW (simple_object_write_section);
  398. ret->next = NULL;
  399. ret->name = xstrdup (name);
  400. ret->align = align;
  401. ret->buffers = NULL;
  402. ret->last_buffer = NULL;
  403. if (sobj->last_section == NULL)
  404. {
  405. sobj->sections = ret;
  406. sobj->last_section = ret;
  407. }
  408. else
  409. {
  410. sobj->last_section->next = ret;
  411. sobj->last_section = ret;
  412. }
  413. return ret;
  414. }
  415. /* Add data to a section. */
  416. const char *
  417. simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
  418. simple_object_write_section *section,
  419. const void *buffer,
  420. size_t size, int copy,
  421. int *err ATTRIBUTE_UNUSED)
  422. {
  423. struct simple_object_write_section_buffer *wsb;
  424. wsb = XNEW (struct simple_object_write_section_buffer);
  425. wsb->next = NULL;
  426. wsb->size = size;
  427. if (!copy)
  428. {
  429. wsb->buffer = buffer;
  430. wsb->free_buffer = NULL;
  431. }
  432. else
  433. {
  434. wsb->free_buffer = (void *) XNEWVEC (char, size);
  435. memcpy (wsb->free_buffer, buffer, size);
  436. wsb->buffer = wsb->free_buffer;
  437. }
  438. if (section->last_buffer == NULL)
  439. {
  440. section->buffers = wsb;
  441. section->last_buffer = wsb;
  442. }
  443. else
  444. {
  445. section->last_buffer->next = wsb;
  446. section->last_buffer = wsb;
  447. }
  448. return NULL;
  449. }
  450. /* Write the complete object file. */
  451. const char *
  452. simple_object_write_to_file (simple_object_write *sobj, int descriptor,
  453. int *err)
  454. {
  455. return sobj->functions->write_to_file (sobj, descriptor, err);
  456. }
  457. /* Release an simple_object_write. */
  458. void
  459. simple_object_release_write (simple_object_write *sobj)
  460. {
  461. simple_object_write_section *section;
  462. free (sobj->segment_name);
  463. section = sobj->sections;
  464. while (section != NULL)
  465. {
  466. struct simple_object_write_section_buffer *buffer;
  467. simple_object_write_section *next_section;
  468. buffer = section->buffers;
  469. while (buffer != NULL)
  470. {
  471. struct simple_object_write_section_buffer *next_buffer;
  472. if (buffer->free_buffer != NULL)
  473. XDELETEVEC (buffer->free_buffer);
  474. next_buffer = buffer->next;
  475. XDELETE (buffer);
  476. buffer = next_buffer;
  477. }
  478. next_section = section->next;
  479. free (section->name);
  480. XDELETE (section);
  481. section = next_section;
  482. }
  483. sobj->functions->release_write (sobj->data);
  484. XDELETE (sobj);
  485. }