wasm-module.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* BFD back-end for WebAssembly modules.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. Based on srec.c, mmo.c, and binary.c
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* The WebAssembly module format is a simple object file format
  18. including up to 11 numbered sections, plus any number of named
  19. "custom" sections. It is described at:
  20. https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md. */
  21. #include "sysdep.h"
  22. #include "bfd.h"
  23. #include "libiberty.h"
  24. #include "libbfd.h"
  25. #include "wasm-module.h"
  26. #include <limits.h>
  27. #ifndef CHAR_BIT
  28. #define CHAR_BIT 8
  29. #endif
  30. typedef struct
  31. {
  32. asymbol * symbols;
  33. bfd_size_type symcount;
  34. } tdata_type;
  35. static const char * const wasm_numbered_sections[] =
  36. {
  37. NULL, /* Custom section, different layout. */
  38. WASM_SECTION ( 1, "type"),
  39. WASM_SECTION ( 2, "import"),
  40. WASM_SECTION ( 3, "function"),
  41. WASM_SECTION ( 4, "table"),
  42. WASM_SECTION ( 5, "memory"),
  43. WASM_SECTION ( 6, "global"),
  44. WASM_SECTION ( 7, "export"),
  45. WASM_SECTION ( 8, "start"),
  46. WASM_SECTION ( 9, "element"),
  47. WASM_SECTION (10, "code"),
  48. WASM_SECTION (11, "data"),
  49. };
  50. #define WASM_NUMBERED_SECTIONS ARRAY_SIZE (wasm_numbered_sections)
  51. /* Resolve SECTION_CODE to a section name if there is one, NULL
  52. otherwise. */
  53. static const char *
  54. wasm_section_code_to_name (bfd_byte section_code)
  55. {
  56. if (section_code < WASM_NUMBERED_SECTIONS)
  57. return wasm_numbered_sections[section_code];
  58. return NULL;
  59. }
  60. /* Translate section name NAME to a section code, or 0 if it's a
  61. custom name. */
  62. static unsigned int
  63. wasm_section_name_to_code (const char *name)
  64. {
  65. unsigned i;
  66. for (i = 1; i < WASM_NUMBERED_SECTIONS; i++)
  67. if (strcmp (name, wasm_numbered_sections[i]) == 0)
  68. return i;
  69. return 0;
  70. }
  71. /* WebAssembly LEB128 integers are sufficiently like DWARF LEB128
  72. integers that we use _bfd_safe_read_leb128, but there are two
  73. points of difference:
  74. - WebAssembly requires a 32-bit value to be encoded in at most 5
  75. bytes, etc.
  76. - _bfd_safe_read_leb128 accepts incomplete LEB128 encodings at the
  77. end of the buffer, while these are invalid in WebAssembly.
  78. Those differences mean that we will accept some files that are
  79. invalid WebAssembly. */
  80. /* Read an LEB128-encoded integer from ABFD's I/O stream, reading one
  81. byte at a time. Set ERROR_RETURN if no complete integer could be
  82. read, LENGTH_RETURN to the number of bytes read (including bytes in
  83. incomplete numbers). SIGN means interpret the number as SLEB128. */
  84. static bfd_vma
  85. wasm_read_leb128 (bfd *abfd,
  86. bool *error_return,
  87. unsigned int *length_return,
  88. bool sign)
  89. {
  90. bfd_vma result = 0;
  91. unsigned int num_read = 0;
  92. unsigned int shift = 0;
  93. unsigned char byte = 0;
  94. unsigned char lost, mask;
  95. int status = 1;
  96. while (bfd_bread (&byte, 1, abfd) == 1)
  97. {
  98. num_read++;
  99. if (shift < CHAR_BIT * sizeof (result))
  100. {
  101. result |= ((bfd_vma) (byte & 0x7f)) << shift;
  102. /* These bits overflowed. */
  103. lost = byte ^ (result >> shift);
  104. /* And this is the mask of possible overflow bits. */
  105. mask = 0x7f ^ ((bfd_vma) 0x7f << shift >> shift);
  106. shift += 7;
  107. }
  108. else
  109. {
  110. lost = byte;
  111. mask = 0x7f;
  112. }
  113. if ((lost & mask) != (sign && (bfd_signed_vma) result < 0 ? mask : 0))
  114. status |= 2;
  115. if ((byte & 0x80) == 0)
  116. {
  117. status &= ~1;
  118. if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
  119. result |= -((bfd_vma) 1 << shift);
  120. break;
  121. }
  122. }
  123. if (length_return != NULL)
  124. *length_return = num_read;
  125. if (error_return != NULL)
  126. *error_return = status != 0;
  127. return result;
  128. }
  129. /* Encode an integer V as LEB128 and write it to ABFD, return TRUE on
  130. success. */
  131. static bool
  132. wasm_write_uleb128 (bfd *abfd, bfd_vma v)
  133. {
  134. do
  135. {
  136. bfd_byte c = v & 0x7f;
  137. v >>= 7;
  138. if (v)
  139. c |= 0x80;
  140. if (bfd_bwrite (&c, 1, abfd) != 1)
  141. return false;
  142. }
  143. while (v);
  144. return true;
  145. }
  146. /* Read the LEB128 integer at P, saving it to X; at end of buffer,
  147. jump to error_return. */
  148. #define READ_LEB128(x, p, end) \
  149. do \
  150. { \
  151. if ((p) >= (end)) \
  152. goto error_return; \
  153. (x) = _bfd_safe_read_leb128 (abfd, &(p), false, (end)); \
  154. } \
  155. while (0)
  156. /* Verify the magic number at the beginning of a WebAssembly module
  157. ABFD, setting ERRORPTR if there's a mismatch. */
  158. static bool
  159. wasm_read_magic (bfd *abfd, bool *errorptr)
  160. {
  161. bfd_byte magic_const[SIZEOF_WASM_MAGIC] = WASM_MAGIC;
  162. bfd_byte magic[SIZEOF_WASM_MAGIC];
  163. if (bfd_bread (magic, sizeof (magic), abfd) == sizeof (magic)
  164. && memcmp (magic, magic_const, sizeof (magic)) == 0)
  165. return true;
  166. *errorptr = true;
  167. return false;
  168. }
  169. /* Read the version number from ABFD, returning TRUE if it's a supported
  170. version. Set ERRORPTR otherwise. */
  171. static bool
  172. wasm_read_version (bfd *abfd, bool *errorptr)
  173. {
  174. bfd_byte vers_const[SIZEOF_WASM_VERSION] = WASM_VERSION;
  175. bfd_byte vers[SIZEOF_WASM_VERSION];
  176. if (bfd_bread (vers, sizeof (vers), abfd) == sizeof (vers)
  177. /* Don't attempt to parse newer versions, which are likely to
  178. require code changes. */
  179. && memcmp (vers, vers_const, sizeof (vers)) == 0)
  180. return true;
  181. *errorptr = true;
  182. return false;
  183. }
  184. /* Read the WebAssembly header (magic number plus version number) from
  185. ABFD, setting ERRORPTR to TRUE if there is a mismatch. */
  186. static bool
  187. wasm_read_header (bfd *abfd, bool *errorptr)
  188. {
  189. if (! wasm_read_magic (abfd, errorptr))
  190. return false;
  191. if (! wasm_read_version (abfd, errorptr))
  192. return false;
  193. return true;
  194. }
  195. /* Scan the "function" subsection of the "name" section ASECT in the
  196. wasm module ABFD. Create symbols. Return TRUE on success. */
  197. static bool
  198. wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
  199. {
  200. bfd_byte *p;
  201. bfd_byte *end;
  202. bfd_vma payload_size;
  203. bfd_vma symcount = 0;
  204. tdata_type *tdata = abfd->tdata.any;
  205. asymbol *symbols = NULL;
  206. sec_ptr space_function_index;
  207. size_t amt;
  208. p = asect->contents;
  209. end = asect->contents + asect->size;
  210. if (!p)
  211. return false;
  212. while (p < end)
  213. {
  214. bfd_byte subsection_code = *p++;
  215. if (subsection_code == WASM_FUNCTION_SUBSECTION)
  216. break;
  217. /* subsection_code is documented to be a varuint7, meaning that
  218. it has to be a single byte in the 0 - 127 range. If it isn't,
  219. the spec must have changed underneath us, so give up. */
  220. if (subsection_code & 0x80)
  221. return false;
  222. READ_LEB128 (payload_size, p, end);
  223. if (payload_size > (size_t) (end - p))
  224. return false;
  225. p += payload_size;
  226. }
  227. if (p >= end)
  228. return false;
  229. READ_LEB128 (payload_size, p, end);
  230. if (payload_size > (size_t) (end - p))
  231. return false;
  232. end = p + payload_size;
  233. READ_LEB128 (symcount, p, end);
  234. /* Sanity check: each symbol has at least two bytes. */
  235. if (symcount > payload_size / 2)
  236. return false;
  237. tdata->symcount = symcount;
  238. space_function_index
  239. = bfd_make_section_with_flags (abfd, WASM_SECTION_FUNCTION_INDEX,
  240. SEC_READONLY | SEC_CODE);
  241. if (!space_function_index)
  242. space_function_index
  243. = bfd_get_section_by_name (abfd, WASM_SECTION_FUNCTION_INDEX);
  244. if (!space_function_index)
  245. return false;
  246. if (_bfd_mul_overflow (tdata->symcount, sizeof (asymbol), &amt))
  247. {
  248. bfd_set_error (bfd_error_file_too_big);
  249. return false;
  250. }
  251. symbols = bfd_alloc (abfd, amt);
  252. if (!symbols)
  253. return false;
  254. for (symcount = 0; p < end && symcount < tdata->symcount; symcount++)
  255. {
  256. bfd_vma idx;
  257. bfd_vma len;
  258. char *name;
  259. asymbol *sym;
  260. READ_LEB128 (idx, p, end);
  261. READ_LEB128 (len, p, end);
  262. if (len > (size_t) (end - p))
  263. goto error_return;
  264. name = bfd_alloc (abfd, len + 1);
  265. if (!name)
  266. goto error_return;
  267. memcpy (name, p, len);
  268. name[len] = 0;
  269. p += len;
  270. sym = &symbols[symcount];
  271. sym->the_bfd = abfd;
  272. sym->name = name;
  273. sym->value = idx;
  274. sym->flags = BSF_GLOBAL | BSF_FUNCTION;
  275. sym->section = space_function_index;
  276. sym->udata.p = NULL;
  277. }
  278. if (symcount < tdata->symcount)
  279. goto error_return;
  280. tdata->symbols = symbols;
  281. abfd->symcount = symcount;
  282. return true;
  283. error_return:
  284. if (symbols)
  285. bfd_release (abfd, symbols);
  286. return false;
  287. }
  288. /* Read a byte from ABFD and return it, or EOF for EOF or error.
  289. Set ERRORPTR on non-EOF error. */
  290. static int
  291. wasm_read_byte (bfd *abfd, bool *errorptr)
  292. {
  293. bfd_byte byte;
  294. if (bfd_bread (&byte, (bfd_size_type) 1, abfd) != 1)
  295. {
  296. if (bfd_get_error () != bfd_error_file_truncated)
  297. *errorptr = true;
  298. return EOF;
  299. }
  300. return byte;
  301. }
  302. /* Scan the wasm module ABFD, creating sections and symbols.
  303. Return TRUE on success. */
  304. static bool
  305. wasm_scan (bfd *abfd)
  306. {
  307. bool error = false;
  308. /* Fake VMAs for now. Choose 0x80000000 as base to avoid clashes
  309. with actual data addresses. */
  310. bfd_vma vma = 0x80000000;
  311. int section_code;
  312. unsigned int bytes_read;
  313. asection *bfdsec;
  314. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  315. goto error_return;
  316. if (!wasm_read_header (abfd, &error))
  317. goto error_return;
  318. while ((section_code = wasm_read_byte (abfd, &error)) != EOF)
  319. {
  320. if (section_code != 0)
  321. {
  322. const char *sname = wasm_section_code_to_name (section_code);
  323. if (!sname)
  324. goto error_return;
  325. bfdsec = bfd_make_section_anyway_with_flags (abfd, sname,
  326. SEC_HAS_CONTENTS);
  327. if (bfdsec == NULL)
  328. goto error_return;
  329. bfdsec->size = wasm_read_leb128 (abfd, &error, &bytes_read, false);
  330. if (error)
  331. goto error_return;
  332. }
  333. else
  334. {
  335. bfd_vma payload_len;
  336. bfd_vma namelen;
  337. char *name;
  338. char *prefix = WASM_SECTION_PREFIX;
  339. size_t prefixlen = strlen (prefix);
  340. ufile_ptr filesize;
  341. payload_len = wasm_read_leb128 (abfd, &error, &bytes_read, false);
  342. if (error)
  343. goto error_return;
  344. namelen = wasm_read_leb128 (abfd, &error, &bytes_read, false);
  345. if (error || bytes_read > payload_len
  346. || namelen > payload_len - bytes_read)
  347. goto error_return;
  348. payload_len -= namelen + bytes_read;
  349. filesize = bfd_get_file_size (abfd);
  350. if (filesize != 0 && namelen > filesize)
  351. {
  352. bfd_set_error (bfd_error_file_truncated);
  353. return false;
  354. }
  355. name = bfd_alloc (abfd, namelen + prefixlen + 1);
  356. if (!name)
  357. goto error_return;
  358. memcpy (name, prefix, prefixlen);
  359. if (bfd_bread (name + prefixlen, namelen, abfd) != namelen)
  360. goto error_return;
  361. name[prefixlen + namelen] = 0;
  362. bfdsec = bfd_make_section_anyway_with_flags (abfd, name,
  363. SEC_HAS_CONTENTS);
  364. if (bfdsec == NULL)
  365. goto error_return;
  366. bfdsec->size = payload_len;
  367. }
  368. bfdsec->vma = vma;
  369. bfdsec->lma = vma;
  370. bfdsec->alignment_power = 0;
  371. bfdsec->filepos = bfd_tell (abfd);
  372. if (bfdsec->size != 0)
  373. {
  374. bfdsec->contents = _bfd_alloc_and_read (abfd, bfdsec->size,
  375. bfdsec->size);
  376. if (!bfdsec->contents)
  377. goto error_return;
  378. }
  379. vma += bfdsec->size;
  380. }
  381. /* Make sure we're at actual EOF. There's no indication in the
  382. WebAssembly format of how long the file is supposed to be. */
  383. if (error)
  384. goto error_return;
  385. return true;
  386. error_return:
  387. return false;
  388. }
  389. /* Put a numbered section ASECT of ABFD into the table of numbered
  390. sections pointed to by FSARG. */
  391. static void
  392. wasm_register_section (bfd *abfd ATTRIBUTE_UNUSED,
  393. asection *asect,
  394. void *fsarg)
  395. {
  396. sec_ptr *numbered_sections = fsarg;
  397. int idx = wasm_section_name_to_code (asect->name);
  398. if (idx == 0)
  399. return;
  400. numbered_sections[idx] = asect;
  401. }
  402. struct compute_section_arg
  403. {
  404. bfd_vma pos;
  405. bool failed;
  406. };
  407. /* Compute the file position of ABFD's section ASECT. FSARG is a
  408. pointer to the current file position.
  409. We allow section names of the form .wasm.id to encode the numbered
  410. section with ID id, if it exists; otherwise, a custom section with
  411. ID "id" is produced. Arbitrary section names are for sections that
  412. are assumed already to contain a section header; those are appended
  413. to the WebAssembly module verbatim. */
  414. static void
  415. wasm_compute_custom_section_file_position (bfd *abfd,
  416. sec_ptr asect,
  417. void *fsarg)
  418. {
  419. struct compute_section_arg *fs = fsarg;
  420. int idx;
  421. if (fs->failed)
  422. return;
  423. idx = wasm_section_name_to_code (asect->name);
  424. if (idx != 0)
  425. return;
  426. if (startswith (asect->name, WASM_SECTION_PREFIX))
  427. {
  428. const char *name = asect->name + strlen (WASM_SECTION_PREFIX);
  429. bfd_size_type payload_len = asect->size;
  430. bfd_size_type name_len = strlen (name);
  431. bfd_size_type nl = name_len;
  432. payload_len += name_len;
  433. do
  434. {
  435. payload_len++;
  436. nl >>= 7;
  437. }
  438. while (nl);
  439. bfd_seek (abfd, fs->pos, SEEK_SET);
  440. if (! wasm_write_uleb128 (abfd, 0)
  441. || ! wasm_write_uleb128 (abfd, payload_len)
  442. || ! wasm_write_uleb128 (abfd, name_len)
  443. || bfd_bwrite (name, name_len, abfd) != name_len)
  444. goto error_return;
  445. fs->pos = asect->filepos = bfd_tell (abfd);
  446. }
  447. else
  448. {
  449. asect->filepos = fs->pos;
  450. }
  451. fs->pos += asect->size;
  452. return;
  453. error_return:
  454. fs->failed = true;
  455. }
  456. /* Compute the file positions for the sections of ABFD. Currently,
  457. this writes all numbered sections first, in order, then all custom
  458. sections, in section order.
  459. The spec says that the numbered sections must appear in order of
  460. their ids, but custom sections can appear in any position and any
  461. order, and more than once. FIXME: support that. */
  462. static bool
  463. wasm_compute_section_file_positions (bfd *abfd)
  464. {
  465. bfd_byte magic[SIZEOF_WASM_MAGIC] = WASM_MAGIC;
  466. bfd_byte vers[SIZEOF_WASM_VERSION] = WASM_VERSION;
  467. sec_ptr numbered_sections[WASM_NUMBERED_SECTIONS];
  468. struct compute_section_arg fs;
  469. unsigned int i;
  470. bfd_seek (abfd, (bfd_vma) 0, SEEK_SET);
  471. if (bfd_bwrite (magic, sizeof (magic), abfd) != (sizeof magic)
  472. || bfd_bwrite (vers, sizeof (vers), abfd) != sizeof (vers))
  473. return false;
  474. for (i = 0; i < WASM_NUMBERED_SECTIONS; i++)
  475. numbered_sections[i] = NULL;
  476. bfd_map_over_sections (abfd, wasm_register_section, numbered_sections);
  477. fs.pos = bfd_tell (abfd);
  478. for (i = 0; i < WASM_NUMBERED_SECTIONS; i++)
  479. {
  480. sec_ptr sec = numbered_sections[i];
  481. bfd_size_type size;
  482. if (! sec)
  483. continue;
  484. size = sec->size;
  485. if (bfd_seek (abfd, fs.pos, SEEK_SET) != 0)
  486. return false;
  487. if (! wasm_write_uleb128 (abfd, i)
  488. || ! wasm_write_uleb128 (abfd, size))
  489. return false;
  490. fs.pos = sec->filepos = bfd_tell (abfd);
  491. fs.pos += size;
  492. }
  493. fs.failed = false;
  494. bfd_map_over_sections (abfd, wasm_compute_custom_section_file_position, &fs);
  495. if (fs.failed)
  496. return false;
  497. abfd->output_has_begun = true;
  498. return true;
  499. }
  500. static bool
  501. wasm_set_section_contents (bfd *abfd,
  502. sec_ptr section,
  503. const void *location,
  504. file_ptr offset,
  505. bfd_size_type count)
  506. {
  507. if (count == 0)
  508. return true;
  509. if (! abfd->output_has_begun
  510. && ! wasm_compute_section_file_positions (abfd))
  511. return false;
  512. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
  513. || bfd_bwrite (location, count, abfd) != count)
  514. return false;
  515. return true;
  516. }
  517. static bool
  518. wasm_write_object_contents (bfd* abfd)
  519. {
  520. bfd_byte magic[] = WASM_MAGIC;
  521. bfd_byte vers[] = WASM_VERSION;
  522. if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  523. return false;
  524. if (bfd_bwrite (magic, sizeof (magic), abfd) != sizeof (magic)
  525. || bfd_bwrite (vers, sizeof (vers), abfd) != sizeof (vers))
  526. return false;
  527. return true;
  528. }
  529. static bool
  530. wasm_mkobject (bfd *abfd)
  531. {
  532. tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
  533. if (! tdata)
  534. return false;
  535. tdata->symbols = NULL;
  536. tdata->symcount = 0;
  537. abfd->tdata.any = tdata;
  538. return true;
  539. }
  540. static long
  541. wasm_get_symtab_upper_bound (bfd *abfd)
  542. {
  543. tdata_type *tdata = abfd->tdata.any;
  544. return (tdata->symcount + 1) * (sizeof (asymbol *));
  545. }
  546. static long
  547. wasm_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  548. {
  549. tdata_type *tdata = abfd->tdata.any;
  550. size_t i;
  551. for (i = 0; i < tdata->symcount; i++)
  552. alocation[i] = &tdata->symbols[i];
  553. alocation[i] = NULL;
  554. return tdata->symcount;
  555. }
  556. static asymbol *
  557. wasm_make_empty_symbol (bfd *abfd)
  558. {
  559. size_t amt = sizeof (asymbol);
  560. asymbol *new_symbol = (asymbol *) bfd_zalloc (abfd, amt);
  561. if (! new_symbol)
  562. return NULL;
  563. new_symbol->the_bfd = abfd;
  564. return new_symbol;
  565. }
  566. static void
  567. wasm_print_symbol (bfd *abfd,
  568. void * filep,
  569. asymbol *symbol,
  570. bfd_print_symbol_type how)
  571. {
  572. FILE *file = (FILE *) filep;
  573. switch (how)
  574. {
  575. case bfd_print_symbol_name:
  576. fprintf (file, "%s", symbol->name);
  577. break;
  578. default:
  579. bfd_print_symbol_vandf (abfd, filep, symbol);
  580. fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
  581. }
  582. }
  583. static void
  584. wasm_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
  585. asymbol *symbol,
  586. symbol_info *ret)
  587. {
  588. bfd_symbol_info (symbol, ret);
  589. }
  590. /* Check whether ABFD is a WebAssembly module; if so, scan it. */
  591. static bfd_cleanup
  592. wasm_object_p (bfd *abfd)
  593. {
  594. bool error;
  595. asection *s;
  596. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  597. return NULL;
  598. if (!wasm_read_header (abfd, &error))
  599. {
  600. bfd_set_error (bfd_error_wrong_format);
  601. return NULL;
  602. }
  603. if (!wasm_mkobject (abfd))
  604. return NULL;
  605. if (!wasm_scan (abfd)
  606. || !bfd_default_set_arch_mach (abfd, bfd_arch_wasm32, 0))
  607. {
  608. bfd_release (abfd, abfd->tdata.any);
  609. abfd->tdata.any = NULL;
  610. return NULL;
  611. }
  612. s = bfd_get_section_by_name (abfd, WASM_NAME_SECTION);
  613. if (s != NULL && wasm_scan_name_function_section (abfd, s))
  614. abfd->flags |= HAS_SYMS;
  615. return _bfd_no_cleanup;
  616. }
  617. /* BFD_JUMP_TABLE_WRITE */
  618. #define wasm_set_arch_mach _bfd_generic_set_arch_mach
  619. /* BFD_JUMP_TABLE_SYMBOLS */
  620. #define wasm_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  621. #define wasm_bfd_is_local_label_name bfd_generic_is_local_label_name
  622. #define wasm_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  623. #define wasm_get_lineno _bfd_nosymbols_get_lineno
  624. #define wasm_find_nearest_line _bfd_nosymbols_find_nearest_line
  625. #define wasm_find_line _bfd_nosymbols_find_line
  626. #define wasm_find_inliner_info _bfd_nosymbols_find_inliner_info
  627. #define wasm_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  628. #define wasm_read_minisymbols _bfd_generic_read_minisymbols
  629. #define wasm_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  630. const bfd_target wasm_vec =
  631. {
  632. "wasm", /* Name. */
  633. bfd_target_unknown_flavour,
  634. BFD_ENDIAN_LITTLE,
  635. BFD_ENDIAN_LITTLE,
  636. (HAS_SYMS | WP_TEXT), /* Object flags. */
  637. (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS), /* Section flags. */
  638. 0, /* Leading underscore. */
  639. ' ', /* AR_pad_char. */
  640. 255, /* AR_max_namelen. */
  641. 0, /* Match priority. */
  642. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  643. /* Routines to byte-swap various sized integers from the data sections. */
  644. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  645. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  646. bfd_getl16, bfd_getl_signed_16, bfd_putl16,
  647. /* Routines to byte-swap various sized integers from the file headers. */
  648. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  649. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  650. bfd_getl16, bfd_getl_signed_16, bfd_putl16,
  651. {
  652. _bfd_dummy_target,
  653. wasm_object_p, /* bfd_check_format. */
  654. _bfd_dummy_target,
  655. _bfd_dummy_target,
  656. },
  657. {
  658. _bfd_bool_bfd_false_error,
  659. wasm_mkobject,
  660. _bfd_generic_mkarchive,
  661. _bfd_bool_bfd_false_error,
  662. },
  663. { /* bfd_write_contents. */
  664. _bfd_bool_bfd_false_error,
  665. wasm_write_object_contents,
  666. _bfd_write_archive_contents,
  667. _bfd_bool_bfd_false_error,
  668. },
  669. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  670. BFD_JUMP_TABLE_COPY (_bfd_generic),
  671. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  672. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  673. BFD_JUMP_TABLE_SYMBOLS (wasm),
  674. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  675. BFD_JUMP_TABLE_WRITE (wasm),
  676. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  677. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  678. NULL,
  679. NULL,
  680. };