This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] [WebAssembly] Skeleton support


Here is the second part of the patch, relative to the first part,
adding wasm-module.c and wasm-module.h. I hope I have addressed all of
your comments so far. Again, any further comments would be
appreciated.

I have not been able to completely remove the LEB128 code; reading an
LEB128 integer from a file stream without knowing the length of either
the integer or the stream, while advancing the file position properly
and handling errors and EOF, is quite different from reading the same
integer from a buffer with a known endpoint (also,
_bfd_safe_read_leb128 provides no way of knowing whether the end of
the buffer was reached after a complete LEB128 integer rather than in
the middle of one; I'm still trying to figure out whether that's a
bug).

Thanks,
Pip
-----------
Suggested change log entry:
2017-03-23  Pip Cet  <pipcet@gmail.com>

    * wasm-module.c: New file to support WebAssembly modules.
    * wasm-module.h: New file to support WebAssembly modules.
    * doc/webassembly.texi: Start documenting wasm-module.c.
    * config.bfd: Add wasm_vec.
    * targets.c: Likewise.
    * configure.ac: Likewise.
    * Makefile.am: Add entries for wasm-module.c.

diff --git a/bfd/Makefile.am b/bfd/Makefile.am
index 6fa8302020..49ab092fc3 100644
--- a/bfd/Makefile.am
+++ b/bfd/Makefile.am
@@ -459,6 +459,7 @@ BFD32_BACKENDS = \
     versados.lo \
     vms-lib.lo \
     vms-misc.lo \
+    wasm-module.lo \
     xcofflink.lo \
     xsym.lo \
     xtensa-isa.lo \
@@ -652,6 +653,7 @@ BFD32_BACKENDS_CFILES = \
     versados.c \
     vms-lib.c \
     vms-misc.c \
+    wasm-module.c \
     xcofflink.c \
     xsym.c \
     xtensa-isa.c \
diff --git a/bfd/config.bfd b/bfd/config.bfd
index abcb7ae210..1235c2cb86 100644
--- a/bfd/config.bfd
+++ b/bfd/config.bfd
@@ -1796,6 +1796,7 @@ case "${targ}" in

   wasm32-*-*)
     targ_defvec=wasm32_elf32_vec
+    targ_selvecs="wasm_vec"
     ;;

   we32k-*-*)
diff --git a/bfd/configure.ac b/bfd/configure.ac
index 77961945d1..feb1231c91 100644
--- a/bfd/configure.ac
+++ b/bfd/configure.ac
@@ -700,6 +700,7 @@ do
     ft32_elf32_vec)         tb="$tb elf32-ft32.lo elf32.lo $elf" ;;
     visium_elf32_vec)         tb="$tb elf32-visium.lo elf32.lo $elf" ;;
     w65_coff_vec)         tb="$tb coff-w65.lo reloc16.lo $coffgen" ;;
+    wasm_vec)                    tb="$tb wasm-module.lo" ;;
     wasm32_elf32_vec)            tb="$tb elf32-wasm32.lo elf32.lo $elf" ;;
     we32k_coff_vec)         tb="$tb coff-we32k.lo $coffgen" ;;
     x86_64_coff_vec)         tb="$tb coff-x86_64.lo $coff"; target_size=64 ;;
diff --git a/bfd/doc/webassembly.texi b/bfd/doc/webassembly.texi
new file mode 100644
index 0000000000..ad650943a1
--- /dev/null
+++ b/bfd/doc/webassembly.texi
@@ -0,0 +1,33 @@
+@section WebAssembly backend
+The WebAssembly module file format, at present, is a very simple
+object file format with up to 11 numbered sections plus named
+``custom'' sections. At present, there is no standard for relocations
+or symbols, though a @code{"name"} subsection can assign names to
+function indices and local variables.
+
+As such, it offers no real advantages over ELF, and intermediate ELF
+files can be used to produce WebAssembly modules. The WebAssembly
+backend aims to enable the opposite: reading a WebAssembly module and
+producing an ELF file containing the same information, which can then
+be manipulated and inspected with standard tools.
+
+When writing WebAssembly modules, the WebAssembly backend attempts to
+determine based on the section name whether to use a numeric section
+id, a named section header, or to include the section verbatim,
+assuming it already contains any necessary header.
+
+Function names are supported as symbols; local names and WebAssembly
+relocation sections are currently unsupported.
+
+There are slight differences in the LEB128 integer implementations
+between the WebAssembly specification and the BFD code; these result
+in some malformed WebAssembly modules being treated as valid.
+
+@menu
+* File layout::
+@end menu
+
+@node File layout, WebAssembly
+@subsection File layout
+For a description of the WebAssembly file format, see
+@url{https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md}.
diff --git a/bfd/targets.c b/bfd/targets.c
index 74559ac4c6..5841e8d6e5 100644
--- a/bfd/targets.c
+++ b/bfd/targets.c
@@ -893,6 +893,7 @@ extern const bfd_target vax_aout_nbsd_vec;
 extern const bfd_target vax_elf32_vec;
 extern const bfd_target visium_elf32_vec;
 extern const bfd_target w65_coff_vec;
+extern const bfd_target wasm_vec;
 extern const bfd_target wasm32_elf32_vec;
 extern const bfd_target we32k_coff_vec;
 extern const bfd_target x86_64_coff_vec;
@@ -1422,6 +1423,7 @@ static const bfd_target * const _bfd_target_vector[] =

     &w65_coff_vec,

+        &wasm_vec,
         &wasm32_elf32_vec,

     &we32k_coff_vec,
diff --git a/bfd/wasm-module.c b/bfd/wasm-module.c
new file mode 100644
index 0000000000..7941745894
--- /dev/null
+++ b/bfd/wasm-module.c
@@ -0,0 +1,845 @@
+/* BFD back-end for WebAssembly modules.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+   Based on srec.c, mmo.c, and binary.c
+
+   This file is part of BFD, the Binary File Descriptor library.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
+
+/* The WebAssembly module format is a simple object file format
+   including up to 11 numbered sections, plus any number of named
+   "custom" sections. It is described at
+   https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md. */
+
+#include "sysdep.h"
+#include "alloca-conf.h"
+#include "bfd.h"
+#include "sysdep.h"
+#include <limits.h>
+#include "bfd_stdint.h"
+#include "libiberty.h"
+#include "libbfd.h"
+#include "wasm-module.h"
+
+typedef struct
+{
+  asymbol *symbols;
+  bfd_size_type symcount;
+} tdata_type;
+
+static const char * const wasm_numbered_sections[] = {
+  NULL, /* custom section, different layout */
+  WASM_SECTION( 1, "type"),
+  WASM_SECTION( 2, "import"),
+  WASM_SECTION( 3, "function"),
+  WASM_SECTION( 4, "table"),
+  WASM_SECTION( 5, "memory"),
+  WASM_SECTION( 6, "global"),
+  WASM_SECTION( 7, "export"),
+  WASM_SECTION( 8, "start"),
+  WASM_SECTION( 9, "element"),
+  WASM_SECTION(10, "code"),
+  WASM_SECTION(11, "data"),
+};
+
+#define WASM_NUMBERED_SECTIONS (sizeof (wasm_numbered_sections) /
sizeof (wasm_numbered_sections[0]))
+
+/* Resolve SECTION_CODE to a section name if there is one, NULL
+   otherwise. */
+static const char *
+wasm_section_code_to_name (bfd_byte section_code)
+{
+  if (section_code < WASM_NUMBERED_SECTIONS)
+    {
+      return wasm_numbered_sections[section_code];
+    }
+
+  return NULL;
+}
+
+/* Translate section name NAME to a section code, or 0 if it's a
+   custom name. */
+static int
+wasm_section_name_to_code (const char *name)
+{
+  unsigned i;
+  for (i = 1; i < WASM_NUMBERED_SECTIONS; i++)
+    {
+      if (strcmp (name, wasm_numbered_sections[i]) == 0)
+        return i;
+    }
+
+  return 0;
+}
+
+/* WebAssembly LEB128 integers are sufficiently like DWARF LEB128
+   integers that we use _bfd_safe_read_leb128, but there are two
+   points of difference:
+
+   - WebAssembly requires a 32-bit value to be encoded in at most 5
+     bytes, etc.
+   - _bfd_safe_read_leb128 accepts incomplete LEB128 encodings at the
+     end of the buffer, while these are invalid in WebAssembly.
+
+   Those differences mean that we will accept some files that are
+   invalid WebAssembly. */
+
+/* Read an LEB128-encoded integer from ABFD's I/O stream, reading one
+   byte at a time. Set ERROR_RETURN if no complete integer could be
+   read, LENGTH_RETURN to the number of bytes read (including bytes in
+   incomplete numbers). SIGN means interpret the number as SLEB128. */
+static bfd_vma
+wasm_read_leb128 (bfd *abfd,
+                  bfd_boolean *error_return,
+                  unsigned int *length_return,
+                  bfd_boolean sign)
+{
+  bfd_vma result = 0;
+  unsigned int num_read = 0;
+  unsigned int shift = 0;
+  unsigned char byte = 0;
+  bfd_boolean success = FALSE;
+
+  while (bfd_bread (&byte, 1, abfd) == 1)
+    {
+      num_read++;
+
+      result |= ((bfd_vma) (byte & 0x7f)) << shift;
+
+      shift += 7;
+      if ((byte & 0x80) == 0)
+        {
+          success = TRUE;
+          break;
+        }
+    }
+
+  if (length_return != NULL)
+    *length_return = num_read;
+  if (error_return != NULL)
+    *error_return = ! success;
+
+  if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
+    result |= -((bfd_vma) 1 << shift);
+
+  return result;
+}
+
+/* Encode an integer V as LEB128 and write it to ABFD, return TRUE on
+   success. */
+static bfd_boolean
+wasm_write_uleb128 (bfd *abfd, bfd_vma v)
+{
+  do
+    {
+      bfd_byte c = v & 0x7f;
+      v >>= 7;
+
+      if (v)
+        c |= 0x80;
+
+      if (bfd_bwrite (&c, 1, abfd) != 1)
+        return FALSE;
+    }
+  while (v);
+
+  return TRUE;
+}
+
+/* Read COUNT bytes from ABFD into BUF, jumping to error_return on
+   failure. */
+#define READ_ALL(buf, count, abfd)              \
+  do                                            \
+    {                                           \
+      if (bfd_bread(buf, count, abfd) != count) \
+        goto error_return;                      \
+    }                                           \
+  while (0)
+
+/* Read the LEB128 integer at P, saving it to X; at end of buffer,
+   jump to error_return. */
+#define READ_LEB128(x, p, end)                                          \
+  do                                                                    \
+    {                                                                   \
+      unsigned int length_read;                                         \
+      (x) = _bfd_safe_read_leb128 (abfd, (p), &length_read,             \
+                                   FALSE, (end));                       \
+      (p) += length_read;                                               \
+      if (length_read == 0)                                             \
+        goto error_return;                                              \
+    }                                                                   \
+  while (0)
+
+/* Verify the magic number at the beginning of a WebAssembly module
+   ABFD, setting ERRORPTR if there's a mismatch. */
+static bfd_boolean
+wasm_read_magic (bfd *abfd, bfd_boolean *errorptr)
+{
+  bfd_byte magic_const[] = WASM_MAGIC;
+  bfd_byte magic[4];
+
+  READ_ALL (magic, 4, abfd);
+
+  if (memcmp (magic, magic_const, 4) != 0)
+    goto error_return;
+
+  return TRUE;
+
+ error_return:
+  *errorptr = TRUE;
+  return FALSE;
+}
+
+/* Read the version number from ABFD, returning TRUE if it's a supported
+   version. Set ERRORPTR otherwise. */
+static bfd_boolean
+wasm_read_version (bfd *abfd, bfd_boolean *errorptr)
+{
+  bfd_byte vers[4];
+  bfd_byte vers_const[] = WASM_VERSION;
+
+  READ_ALL (vers, (bfd_size_type) 4, abfd);
+
+  /* Don't attempt to parse newer versions, which are likely to
+     require code changes. */
+  if (memcmp (vers, vers_const, 4) != 0)
+    goto error_return;
+
+  return TRUE;
+
+ error_return:
+  *errorptr = TRUE;
+  return FALSE;
+}
+
+/* Read the WebAssembly header (magic number plus version number) from
+   ABFD, setting ERRORPTR to TRUE if there is a mismatch. */
+static bfd_boolean
+wasm_read_header (bfd *abfd, bfd_boolean *errorptr)
+{
+  if (! wasm_read_magic (abfd, errorptr))
+    return FALSE;
+
+  if (! wasm_read_version (abfd, errorptr))
+    return FALSE;
+
+  return TRUE;
+}
+
+/* Scan the "function" subsection of the "name" section ASECT in the
+   wasm module ABFD. Create symbols. Return TRUE on success. */
+static bfd_boolean
+wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
+{
+  bfd_byte *p;
+  bfd_byte *end;
+  bfd_vma payload_size;
+  bfd_vma symcount = 0;
+  tdata_type *tdata = abfd->tdata.any;
+  asymbol *symbols = NULL;
+  sec_ptr space_function_index;
+
+  if (! asect)
+    return FALSE;
+
+  if (strcmp (asect->name, WASM_NAME_SECTION) != 0)
+    return FALSE;
+
+  p = asect->contents;
+  end = asect->contents + asect->size;
+
+  if (! p)
+    return FALSE;
+
+  while (p < end)
+    {
+      bfd_byte subsection_code = *p++;
+      if (subsection_code == WASM_FUNCTION_SUBSECTION)
+        break;
+
+      /* subsection_code is documented to be a varuint7, meaning that
+         it has to be a single byte in the 0 - 127 range. If it isn't,
+         the spec must have changed underneath us, so give up. */
+      if (subsection_code & 0x80)
+        return FALSE;
+
+      READ_LEB128 (payload_size, p, end);
+
+      if (p > p + payload_size)
+        return FALSE;
+
+      p += payload_size;
+    }
+
+  if (p >= end)
+    return FALSE;
+
+  READ_LEB128 (payload_size, p, end);
+
+  if (p > p + payload_size)
+    return FALSE;
+
+  if (p + payload_size > end)
+    return FALSE;
+
+  end = p + payload_size;
+
+  READ_LEB128 (symcount, p, end);
+
+  /* sanity check: each symbol has at least two bytes. */
+  if (symcount > payload_size/2)
+    return FALSE;
+
+  tdata->symcount = symcount;
+
+  space_function_index = bfd_make_section_with_flags (abfd,
WASM_SECTION_FUNCTION_INDEX, SEC_READONLY | SEC_CODE);
+  if (! space_function_index)
+    space_function_index = bfd_get_section_by_name (abfd,
WASM_SECTION_FUNCTION_INDEX);
+
+  if (! space_function_index)
+    return FALSE;
+
+  symbols = bfd_zalloc (abfd, tdata->symcount * sizeof (asymbol));
+  if (! symbols)
+    return FALSE;
+
+  for (symcount = 0; p < end && symcount < tdata->symcount; symcount++)
+    {
+      bfd_vma index;
+      bfd_vma len;
+      char *name;
+      asymbol *sym;
+
+      READ_LEB128 (index, p, end);
+      READ_LEB128 (len, p, end);
+
+      if (p + len < p || p + len > end)
+        goto error_return;
+
+      name = bfd_zalloc (abfd, len + 1);
+      if (! name)
+        goto error_return;
+
+      memcpy (name, p, len);
+      p += len;
+
+      sym = &symbols[symcount];
+      sym->the_bfd = abfd;
+      sym->name = name;
+      sym->value = index;
+      sym->flags = BSF_GLOBAL | BSF_FUNCTION;
+      sym->section = space_function_index;
+      sym->udata.p = NULL;
+    }
+
+  if (symcount < tdata->symcount)
+    goto error_return;
+
+  tdata->symbols = symbols;
+  abfd->symcount = symcount;
+
+  return TRUE;
+
+ error_return:
+  while (symcount)
+    bfd_release (abfd, (void *)symbols[--symcount].name);
+  bfd_release (abfd, symbols);
+  return FALSE;
+}
+
+/* Read a byte from ABFD and return it, or EOF for EOF or error. Set
+   ERRORPTR on non-EOF error.
+
+   This differs from READ_ALL because we don't know whether there is
+   another section to be read at EOF. */
+static int
+wasm_read_byte (bfd *abfd, bfd_boolean *errorptr)
+{
+  bfd_byte byte;
+  if (bfd_bread (&byte, (bfd_size_type) 1, abfd) != 1)
+    {
+      if (bfd_get_error () != bfd_error_file_truncated)
+        *errorptr = TRUE;
+      return EOF;
+    }
+
+  return byte;
+}
+
+/* Scan the wasm module ABFD, creating sections and symbols. Return
+   TRUE on success. */
+static bfd_boolean
+wasm_scan (bfd *abfd)
+{
+  bfd_boolean error = FALSE;
+  /* fake VMAs for now. Choose 0x80000000 as base to avoid clashes
+     with actual data addresses. */
+  bfd_vma vma = 0x80000000;
+  int section_code;
+  unsigned int bytes_read;
+  char *name = NULL;
+
+  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
+    goto error_return;
+
+  if (! wasm_read_header (abfd, &error))
+    goto error_return;
+
+  while ((section_code = wasm_read_byte (abfd, &error)) != EOF)
+    {
+      asection *bfdsec;
+
+      if (section_code != 0)
+        {
+          const char *sname = wasm_section_code_to_name (section_code);
+
+          if (! sname)
+            goto error_return;
+
+          name = strdup (sname);
+          bfdsec = bfd_make_section_anyway_with_flags (abfd, name,
SEC_HAS_CONTENTS);
+          if (bfdsec == NULL)
+            goto error_return;
+          name = NULL;
+
+          bfdsec->vma = vma;
+          bfdsec->lma = vma;
+          bfdsec->size = wasm_read_leb128 (abfd, &error, &bytes_read, FALSE);
+          if (error)
+            goto error_return;
+          bfdsec->filepos = bfd_tell (abfd);
+          bfdsec->alignment_power = 0;
+        }
+      else
+        {
+          bfd_vma payload_len;
+          file_ptr section_start;
+          bfd_vma namelen;
+          char *prefix = WASM_SECTION_PREFIX;
+          char *p;
+          int ret;
+
+          payload_len = wasm_read_leb128 (abfd, &error, &bytes_read, FALSE);
+          if (error)
+            goto error_return;
+          section_start = bfd_tell (abfd);
+          namelen = wasm_read_leb128 (abfd, &error, &bytes_read, FALSE);
+          if (error || namelen > payload_len)
+            goto error_return;
+          name = bfd_zmalloc (namelen + strlen (prefix) + 1);
+          if (! name)
+            goto error_return;
+          p = name;
+          ret = sprintf (p, "%s", prefix);
+          if (ret < 0 || (bfd_vma) ret != strlen (prefix))
+            goto error_return;
+          p += ret;
+          READ_ALL (p, namelen, abfd);
+
+          bfdsec = bfd_make_section_anyway_with_flags (abfd, name,
SEC_HAS_CONTENTS);
+          if (bfdsec == NULL)
+            goto error_return;
+          name = NULL;
+
+          bfdsec->vma = vma;
+          bfdsec->lma = vma;
+          bfdsec->filepos = bfd_tell (abfd);
+          bfdsec->size = section_start + payload_len - bfdsec->filepos;
+          bfdsec->alignment_power = 0;
+        }
+
+      if (bfdsec->size != 0)
+        {
+          bfdsec->contents = bfd_zalloc (abfd, bfdsec->size);
+          if (! bfdsec->contents)
+            goto error_return;
+
+          READ_ALL (bfdsec->contents, bfdsec->size, abfd);
+        }
+
+      vma += bfdsec->size;
+    }
+
+  /* Make sure we're at actual EOF. There's no indication in the
+     WebAssembly format of how long the file is supposed to be. */
+  if (error)
+    goto error_return;
+
+  return TRUE;
+
+ error_return:
+  if (name)
+    free (name);
+
+  for (asection *bfdsec = abfd->sections; bfdsec; bfdsec = bfdsec->next)
+    free ((void *) bfdsec->name);
+
+  return FALSE;
+}
+
+/* Put a numbered section ASECT of ABFD into the table of numbered
+   sections pointed to by FSARG. */
+static void
+wasm_register_section (bfd *abfd ATTRIBUTE_UNUSED,
+                       asection *asect, void *fsarg)
+{
+  sec_ptr *numbered_sections = fsarg;
+  int index = wasm_section_name_to_code (asect->name);
+
+  if (index == 0)
+    return;
+
+  numbered_sections[index] = asect;
+}
+
+struct compute_section_arg
+{
+  bfd_vma pos;
+  bfd_boolean failed;
+};
+
+/* Compute the file position of ABFD's section ASECT. FSARG is a
+   pointer to the current file position.
+
+   We allow section names of the form .wasm.id to encode the numbered
+   section with ID id, if it exists; otherwise, a custom section with
+   ID "id" is produced. Arbitrary section names are for sections that
+   are assumed already to contain a section header; those are appended
+   to the WebAssembly module verbatim. */
+static void
+wasm_compute_custom_section_file_position (bfd *abfd, sec_ptr asect,
+                                           void *fsarg)
+{
+  struct compute_section_arg *fs = fsarg;
+  int index;
+
+  if (fs->failed)
+    return;
+
+  index = wasm_section_name_to_code (asect->name);
+
+  if (index != 0)
+    return;
+
+  if (CONST_STRNEQ (asect->name, WASM_SECTION_PREFIX))
+    {
+      const char *name = asect->name + strlen (WASM_SECTION_PREFIX);
+      bfd_size_type payload_len = asect->size;
+      bfd_size_type name_len = strlen (name);
+      bfd_size_type nl = name_len;
+
+      payload_len += name_len;
+
+      do
+        {
+          payload_len++;
+          nl >>= 7;
+        }
+      while (nl);
+
+      bfd_seek (abfd, fs->pos, SEEK_SET);
+      if (! wasm_write_uleb128 (abfd, 0)
+          || ! wasm_write_uleb128 (abfd, payload_len)
+          || ! wasm_write_uleb128 (abfd, name_len)
+          || bfd_bwrite (name, name_len, abfd) != name_len)
+        goto error_return;
+      fs->pos = asect->filepos = bfd_tell (abfd);
+    }
+  else
+    {
+      asect->filepos = fs->pos;
+    }
+
+
+  fs->pos += asect->size;
+
+  return;
+
+ error_return:
+  fs->failed = TRUE;
+}
+
+/* Compute the file positions for the sections of ABFD. Currently,
+   this writes all numbered sections first, in order, then all custom
+   sections, in section order.
+
+   The spec says that the numbered sections must appear in order of
+   their ids, but custom sections can appear in any position and any
+   order, and more than once. FIXME: support that. */
+static bfd_boolean
+wasm_compute_section_file_positions (bfd *abfd)
+{
+  bfd_byte magic[] = WASM_MAGIC;
+  bfd_byte vers[] = WASM_VERSION;
+  sec_ptr numbered_sections[WASM_NUMBERED_SECTIONS];
+  struct compute_section_arg fs;
+  unsigned int i;
+
+  bfd_seek (abfd, (bfd_vma) 0, SEEK_SET);
+
+  if (bfd_bwrite (magic, sizeof (magic), abfd) != (sizeof magic)
+      || bfd_bwrite (vers, sizeof (vers), abfd) != sizeof (vers))
+    return FALSE;
+
+  for (i = 0; i < WASM_NUMBERED_SECTIONS; i++)
+    numbered_sections[i] = NULL;
+
+  bfd_map_over_sections (abfd, wasm_register_section, numbered_sections);
+
+  fs.pos = bfd_tell (abfd);
+  for (i = 0; i < WASM_NUMBERED_SECTIONS; i++)
+    {
+      sec_ptr sec = numbered_sections[i];
+      bfd_size_type size;
+
+      if (! sec)
+        continue;
+      size = sec->size;
+      if (bfd_seek (abfd, fs.pos, SEEK_SET) != 0)
+        return FALSE;
+      if (! wasm_write_uleb128 (abfd, i)
+          || ! wasm_write_uleb128 (abfd, size))
+        return FALSE;
+      fs.pos = sec->filepos = bfd_tell (abfd);
+      fs.pos += size;
+    }
+
+  fs.failed = FALSE;
+
+  bfd_map_over_sections (abfd, wasm_compute_custom_section_file_position, &fs);
+
+  if (fs.failed)
+    return FALSE;
+
+  abfd->output_has_begun = TRUE;
+
+  return TRUE;
+}
+
+static bfd_boolean
+wasm_set_section_contents (bfd *abfd,
+                           sec_ptr section,
+                           const void *location,
+                           file_ptr offset,
+                           bfd_size_type count)
+{
+  if (count == 0)
+    return TRUE;
+
+  if (! abfd->output_has_begun
+      && ! wasm_compute_section_file_positions (abfd))
+    return FALSE;
+
+  if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
+      || bfd_bwrite (location, count, abfd) != count)
+    return FALSE;
+
+  return TRUE;
+}
+
+static bfd_boolean
+wasm_write_object_contents (bfd* abfd)
+{
+  bfd_byte magic[] = WASM_MAGIC;
+  bfd_byte vers[] = WASM_VERSION;
+
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
+    return FALSE;
+
+  if (bfd_bwrite (magic, sizeof (magic), abfd) != sizeof (magic)
+      || bfd_bwrite (vers, sizeof (vers), abfd) != sizeof (vers))
+    return FALSE;
+
+  return TRUE;
+}
+
+static bfd_boolean
+wasm_mkobject (bfd *abfd)
+{
+  tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
+
+  if (! tdata)
+    return FALSE;
+
+  tdata->symbols = NULL;
+  tdata->symcount = 0;
+
+  abfd->tdata.any = tdata;
+
+  return TRUE;
+}
+
+static long
+wasm_get_symtab_upper_bound (bfd *abfd)
+{
+  tdata_type *tdata = abfd->tdata.any;
+
+  return (tdata->symcount + 1) * (sizeof (asymbol *));
+}
+
+static long
+wasm_canonicalize_symtab (bfd *abfd, asymbol **alocation)
+{
+  tdata_type *tdata = abfd->tdata.any;
+  size_t i;
+
+  for (i = 0; i < tdata->symcount; i++)
+    alocation[i] = &tdata->symbols[i];
+  alocation[i] = NULL;
+
+  return tdata->symcount;
+}
+
+static asymbol *
+wasm_make_empty_symbol (bfd *abfd)
+{
+  bfd_size_type amt = sizeof (asymbol);
+  asymbol *new_symbol = (asymbol *) bfd_zalloc (abfd, amt);
+
+  if (! new_symbol)
+    return NULL;
+  new_symbol->the_bfd = abfd;
+  return new_symbol;
+}
+
+static void
+wasm_print_symbol (bfd *abfd,
+                   void * filep,
+                   asymbol *symbol,
+                   bfd_print_symbol_type how)
+{
+  FILE *file = (FILE *) filep;
+
+  switch (how)
+    {
+    case bfd_print_symbol_name:
+      fprintf (file, "%s", symbol->name);
+      break;
+
+    default:
+      bfd_print_symbol_vandf (abfd, filep, symbol);
+      fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
+    }
+}
+
+static void
+wasm_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
+                      asymbol *symbol,
+                      symbol_info *ret)
+{
+  bfd_symbol_info (symbol, ret);
+}
+
+/* Check whether ABFD is a WebAssembly module; if so, scan it. */
+static const bfd_target *
+wasm_object_p (bfd *abfd)
+{
+  bfd_boolean error;
+
+  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
+    return NULL;
+
+  if (! wasm_read_header (abfd, &error))
+    {
+      bfd_set_error (bfd_error_wrong_format);
+      return NULL;
+    }
+
+  if (! wasm_mkobject (abfd) || ! wasm_scan (abfd))
+    return NULL;
+
+  if (! bfd_default_set_arch_mach (abfd, bfd_arch_wasm32, 0))
+    return NULL;
+
+  if (wasm_scan_name_function_section (abfd, bfd_get_section_by_name
(abfd, WASM_NAME_SECTION)))
+    {
+      abfd->flags |= HAS_SYMS;
+    }
+
+  return abfd->xvec;
+}
+
+/* BFD_JUMP_TABLE_WRITE */
+#define wasm_set_arch_mach                _bfd_generic_set_arch_mach
+
+/* BFD_JUMP_TABLE_SYMBOLS */
+#define wasm_get_symbol_version_string
_bfd_nosymbols_get_symbol_version_string
+#define wasm_bfd_is_local_label_name       bfd_generic_is_local_label_name
+#define wasm_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *,
asymbol *)) bfd_false)
+#define wasm_get_lineno                   _bfd_nosymbols_get_lineno
+#define wasm_find_nearest_line            _bfd_nosymbols_find_nearest_line
+#define wasm_find_line                    _bfd_nosymbols_find_line
+#define wasm_find_inliner_info            _bfd_nosymbols_find_inliner_info
+#define wasm_bfd_make_debug_symbol        _bfd_nosymbols_bfd_make_debug_symbol
+#define wasm_read_minisymbols             _bfd_generic_read_minisymbols
+#define wasm_minisymbol_to_symbol         _bfd_generic_minisymbol_to_symbol
+
+const bfd_target wasm_vec =
+{
+  "wasm",               /* Name */
+  bfd_target_unknown_flavour,
+  BFD_ENDIAN_LITTLE,
+  BFD_ENDIAN_LITTLE,
+  (HAS_SYMS | WP_TEXT),             /* Object flags. */
+  (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS), /* Section flags */
+  0,                    /* Leading underscore */
+  ' ',                  /* AR_pad_char */
+  255,                  /* AR_max_namelen */
+  0,                /* match priority.  */
+  /* Routines to byte-swap various sized integers from the data sections */
+  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
+  bfd_getl32, bfd_getl_signed_32, bfd_putl32,
+  bfd_getl16, bfd_getl_signed_16, bfd_putl16,
+
+  /* Routines to byte-swap various sized integers from the file headers */
+  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
+  bfd_getl32, bfd_getl_signed_32, bfd_putl32,
+  bfd_getl16, bfd_getl_signed_16, bfd_putl16,
+
+  {
+    _bfd_dummy_target,
+    wasm_object_p,    /* bfd_check_format.  */
+    _bfd_dummy_target,
+    _bfd_dummy_target,
+  },
+  {
+    bfd_false,
+    wasm_mkobject,
+    _bfd_generic_mkarchive,
+    bfd_false,
+  },
+  {                /* bfd_write_contents.  */
+    bfd_false,
+    wasm_write_object_contents,
+    _bfd_write_archive_contents,
+    bfd_false,
+  },
+
+  BFD_JUMP_TABLE_GENERIC (_bfd_generic),
+  BFD_JUMP_TABLE_COPY (_bfd_generic),
+  BFD_JUMP_TABLE_CORE (_bfd_nocore),
+  BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
+  BFD_JUMP_TABLE_SYMBOLS (wasm),
+  BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
+  BFD_JUMP_TABLE_WRITE (wasm),
+  BFD_JUMP_TABLE_LINK (_bfd_nolink),
+  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
+
+  NULL,
+
+  NULL,
+};
diff --git a/bfd/wasm-module.h b/bfd/wasm-module.h
new file mode 100644
index 0000000000..b6cb7d565f
--- /dev/null
+++ b/bfd/wasm-module.h
@@ -0,0 +1,52 @@
+/* BFD back-end for WebAssembly modules.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+   This file is part of BFD, the Binary File Descriptor library.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
+
+#ifndef _WASM_MODULE_H
+#define _WASM_MODULE_H
+
+/* WebAssembly module file header. Note that WASM_VERSION is a 32-bit
+   little-endian integer, not an LEB128-encoded integer. */
+#define WASM_MAGIC    { 0x00, 'a', 's', 'm' }
+#define WASM_VERSION  { 0x01, 0x00, 0x00, 0x00}
+
+/* Prefix to use to form section names. */
+#define WASM_SECTION_PREFIX ".wasm."
+
+/* NUMBER is currently unused, but is included for error checking
+   purposes. */
+#define WASM_SECTION(number, name) (WASM_SECTION_PREFIX name)
+
+/* section names. WASM_NAME_SECTION is the name of the named section
+   named "name". */
+#define WASM_NAME_SECTION         WASM_SECTION(0, "name")
+#define WASM_RELOC_SECTION_PREFIX WASM_SECTION(0, "reloc.")
+#define WASM_LINKING_SECTION      WASM_SECTION(0, "linking")
+#define WASM_DYLINK_SECTION       WASM_SECTION(0, "dylink")
+
+/* subsection indices. Right now, that's subsections of the "name"
+   section only. */
+#define WASM_FUNCTION_SUBSECTION 1 /* function names */
+#define WASM_LOCALS_SUBSECTION   2 /* names of locals by function */
+
+
+/* The section to report wasm symbols in. */
+#define WASM_SECTION_FUNCTION_INDEX ".space.function_index"
+
+#endif /* _WASM_MODULE_H */


On Thu, Mar 23, 2017 at 2:55 AM, Pip Cet <pipcet@gmail.com> wrote:
> Okay, here's the first part of the changes, adding a new architecture,
> wasm32, and minimal ELF support. I've left the date range in the
> copyright notice for elf32-wasm32.c because future patches will add
> substantial parts from other architectures' ELF support files; if that
> needs changing, please let me know.
>
> Any comments would be appreciated.
> Thanks,
> Pip
>
> Suggested change log entries:
> include/:
> 2017-03-22  Pip Cet  <pipcet@gmail.com>
>
>     * elf/wasm32.h: New file to support wasm32 architecture.
>
> bfd/:
> 2017-03-22  Pip Cet  <pipcet@gmail.com>
>     * cpu-wasm32.c: New file to support wasm32 architecture.
>     * elf32-wasm32.c: New file to support wasm32 architecture.
>     * Makefile.am: Add wasm32 architecture.
>     * archures.c: Likewise.
>     * config.bfd: Likewise.
>     * configure.ac: Likewise.
>     * targets.c: Likewise.
> -----
>
> diff --git a/bfd/Makefile.am b/bfd/Makefile.am
> index 0b0226306f..6fa8302020 100644
> --- a/bfd/Makefile.am
> +++ b/bfd/Makefile.am
> @@ -169,6 +169,7 @@ ALL_MACHINES = \
>      cpu-vax.lo \
>      cpu-visium.lo \
>      cpu-w65.lo \
> +    cpu-wasm32.lo \
>      cpu-we32k.lo \
>      cpu-xc16x.lo \
>      cpu-xgate.lo \
> @@ -257,6 +258,7 @@ ALL_MACHINES_CFILES = \
>      cpu-v850_rh850.c \
>      cpu-vax.c \
>      cpu-visium.c \
> +    cpu-wasm32.c \
>      cpu-w65.c \
>      cpu-we32k.c \
>      cpu-xc16x.c \
> @@ -383,6 +385,7 @@ BFD32_BACKENDS = \
>      elf32-v850.lo \
>      elf32-vax.lo \
>      elf32-visium.lo \
> +    elf32-wasm32.lo \
>      elf32-xc16x.lo \
>      elf32-xgate.lo \
>      elf32-xstormy16.lo \
> @@ -576,6 +579,7 @@ BFD32_BACKENDS_CFILES = \
>      elf32-v850.c \
>      elf32-vax.c \
>      elf32-visium.c \
> +    elf32-wasm32.c \
>      elf32-xc16x.c \
>      elf32-xgate.c \
>      elf32-xstormy16.c \
> diff --git a/bfd/archures.c b/bfd/archures.c
> index c909db012d..c6e7152057 100644
> --- a/bfd/archures.c
> +++ b/bfd/archures.c
> @@ -528,6 +528,8 @@ DESCRIPTION
>  .#define bfd_mach_nios2r2    2
>  .  bfd_arch_visium,    {* Visium *}
>  .#define bfd_mach_visium    1
> +.  bfd_arch_wasm32,     {* WebAssembly *}
> +.#define bfd_mach_wasm32        1
>  .  bfd_arch_pru,    {* PRU *}
>  .#define bfd_mach_pru    0
>  .  bfd_arch_last
> @@ -654,6 +656,7 @@ extern const bfd_arch_info_type bfd_v850_arch;
>  extern const bfd_arch_info_type bfd_v850_rh850_arch;
>  extern const bfd_arch_info_type bfd_vax_arch;
>  extern const bfd_arch_info_type bfd_visium_arch;
> +extern const bfd_arch_info_type bfd_wasm32_arch;
>  extern const bfd_arch_info_type bfd_w65_arch;
>  extern const bfd_arch_info_type bfd_we32k_arch;
>  extern const bfd_arch_info_type bfd_xstormy16_arch;
> @@ -746,6 +749,7 @@ static const bfd_arch_info_type * const
> bfd_archures_list[] =
>      &bfd_vax_arch,
>      &bfd_visium_arch,
>      &bfd_w65_arch,
> +    &bfd_wasm32_arch,
>      &bfd_we32k_arch,
>      &bfd_xstormy16_arch,
>      &bfd_xtensa_arch,
> diff --git a/bfd/config.bfd b/bfd/config.bfd
> index 52db9a47f1..abcb7ae210 100644
> --- a/bfd/config.bfd
> +++ b/bfd/config.bfd
> @@ -197,6 +197,7 @@ tilegx*)     targ_archs=bfd_tilegx_arch ;;
>  tilepro*)     targ_archs=bfd_tilepro_arch ;;
>  v850*)         targ_archs="bfd_v850_arch bfd_v850_rh850_arch" ;;
>  visium*)     targ_archs=bfd_visium_arch ;;
> +wasm32)         targ_archs=bfd_wasm32_arch ;;
>  x86_64*)     targ_archs=bfd_i386_arch ;;
>  xtensa*)     targ_archs=bfd_xtensa_arch ;;
>  xgate)         targ_archs=bfd_xgate_arch ;;
> @@ -1793,6 +1794,10 @@ case "${targ}" in
>      targ_defvec=visium_elf32_vec
>      ;;
>
> +  wasm32-*-*)
> +    targ_defvec=wasm32_elf32_vec
> +    ;;
> +
>    we32k-*-*)
>      targ_defvec=we32k_coff_vec
>      ;;
> diff --git a/bfd/configure.ac b/bfd/configure.ac
> index ee0c537ea2..77961945d1 100644
> --- a/bfd/configure.ac
> +++ b/bfd/configure.ac
> @@ -700,6 +700,7 @@ do
>      ft32_elf32_vec)         tb="$tb elf32-ft32.lo elf32.lo $elf" ;;
>      visium_elf32_vec)         tb="$tb elf32-visium.lo elf32.lo $elf" ;;
>      w65_coff_vec)         tb="$tb coff-w65.lo reloc16.lo $coffgen" ;;
> +    wasm32_elf32_vec)            tb="$tb elf32-wasm32.lo elf32.lo $elf" ;;
>      we32k_coff_vec)         tb="$tb coff-we32k.lo $coffgen" ;;
>      x86_64_coff_vec)         tb="$tb coff-x86_64.lo $coff"; target_size=64 ;;
>      x86_64_elf32_vec)         tb="$tb elf64-x86-64.lo elf-ifunc.lo
> elf-nacl.lo elf64.lo elf32.lo $elf"; target_size=64 ;;
> diff --git a/bfd/cpu-wasm32.c b/bfd/cpu-wasm32.c
> new file mode 100644
> index 0000000000..19d4cb9270
> --- /dev/null
> +++ b/bfd/cpu-wasm32.c
> @@ -0,0 +1,36 @@
> +/* BFD support for the WebAssembly target
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +   This file is part of BFD, the Binary File Descriptor library.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program; if not, write to the Free Software
> +   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
> +   MA 02110-1301, USA.  */
> +
> +#include "sysdep.h"
> +#include "bfd.h"
> +#include "libbfd.h"
> +#include "libiberty.h"
> +
> +#define N(number, print, default, next)  \
> +{  32, 32, 8, bfd_arch_wasm32, number, "wasm32", "wasm32", 4,
> default, bfd_default_compatible, \
> +   bfd_default_scan, bfd_arch_default_fill, next }
> +
> +static const bfd_arch_info_type arch_info_struct[] =
> +{
> +  N (bfd_mach_wasm32, "wasm32", TRUE, NULL)
> +};
> +
> +const bfd_arch_info_type bfd_wasm32_arch =
> +  N (bfd_mach_wasm32, "wasm32", TRUE, & arch_info_struct[0]);
> diff --git a/bfd/elf32-wasm32.c b/bfd/elf32-wasm32.c
> new file mode 100644
> index 0000000000..2088146cc4
> --- /dev/null
> +++ b/bfd/elf32-wasm32.c
> @@ -0,0 +1,52 @@
> +/* 32-bit ELF for the WebAssembly target
> +   Copyright (C) 1999-2017 Free Software Foundation, Inc.
> +
> +   This file is part of BFD, the Binary File Descriptor library.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program; if not, write to the Free Software
> +   Foundation, Inc., 51 Franklin Street - Fifth Floor,
> +   Boston, MA 02110-1301, USA.  */
> +
> +#include "sysdep.h"
> +#include "bfd.h"
> +#include "libbfd.h"
> +#include "elf-bfd.h"
> +#include "bfd_stdint.h"
> +#include "elf/wasm32.h"
> +
> +#define ELF_ARCH        bfd_arch_wasm32
> +#define ELF_TARGET_ID        EM_WEBASSEMBLY
> +#define ELF_MACHINE_CODE    EM_WEBASSEMBLY
> +/* FIXME we don't have paged executables, see
> + * https://github.com/pipcet/binutils-gdb/issues/4 */
> +#define ELF_MAXPAGESIZE        4096
> +
> +#define TARGET_LITTLE_SYM       wasm32_elf32_vec
> +#define TARGET_LITTLE_NAME    "elf32-wasm32"
> +
> +#define elf_backend_can_gc_sections          1
> +#define elf_backend_rela_normal              1
> +/* For testing. */
> +#define elf_backend_want_dynrelro            1
> +
> +#define bfd_elf32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
> +#define bfd_elf32_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
> +
> +#define ELF_DYNAMIC_INTERPRETER  "/sbin/elf-dynamic-interpreter.so"
> +
> +#define elf_backend_want_got_plt 1
> +#define elf_backend_plt_readonly 1
> +#define elf_backend_got_header_size 0
> +
> +#include "elf32-target.h"
> diff --git a/bfd/targets.c b/bfd/targets.c
> index 1a7c6b87d6..74559ac4c6 100644
> --- a/bfd/targets.c
> +++ b/bfd/targets.c
> @@ -893,6 +893,7 @@ extern const bfd_target vax_aout_nbsd_vec;
>  extern const bfd_target vax_elf32_vec;
>  extern const bfd_target visium_elf32_vec;
>  extern const bfd_target w65_coff_vec;
> +extern const bfd_target wasm32_elf32_vec;
>  extern const bfd_target we32k_coff_vec;
>  extern const bfd_target x86_64_coff_vec;
>  extern const bfd_target x86_64_elf32_vec;
> @@ -1421,6 +1422,8 @@ static const bfd_target * const _bfd_target_vector[] =
>
>      &w65_coff_vec,
>
> +        &wasm32_elf32_vec,
> +
>      &we32k_coff_vec,
>
>  #ifdef BFD64
> diff --git a/include/elf/wasm32.h b/include/elf/wasm32.h
> new file mode 100644
> index 0000000000..38e6c2e950
> --- /dev/null
> +++ b/include/elf/wasm32.h
> @@ -0,0 +1,28 @@
> +/* ELF support for BFD for the WebAssembly target
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program; if not, write to the Free Software Foundation,
> +   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
> +
> +#ifndef _ELF_WASM32_H
> +#define _ELF_WASM32_H
> +
> +#include "elf/reloc-macros.h"
> +
> +/* Relocation types.  */
> +
> +START_RELOC_NUMBERS (elf_wasm32_reloc_type)
> +END_RELOC_NUMBERS (R_WASM32_max = 1)
> +
> +#endif /* _ELF_WASM32_H */
>
> On Wed, Mar 22, 2017 at 4:11 PM, Pip Cet <pipcet@gmail.com> wrote:
>> Hi Nick,
>> thank you very much for your comments. If it's okay, I shall split the
>> patch into two parts (one for the skeleton code, one for wasm-module.c
>> plus the new header file), address your comments, and try again.
>>
>> On Wed, Mar 22, 2017 at 12:55 PM, Nick Clifton <nickc@redhat.com> wrote:
>>> It is often easier (for us) if changelog entries are not included in
>>> the patch, but instead are just part of the accompanying email.  This
>>> is because they almost never apply when patching the sources, and so
>>> they always have to be added by hand.
>>
>> Thanks, noted.
>>
>>>> diff --git a/bfd/cpu-wasm32.c b/bfd/cpu-wasm32.c
>>>> new file mode 100644
>>>> index 0000000000..929778d531
>>>> --- /dev/null
>>>> +++ b/bfd/cpu-wasm32.c
>>>> @@ -0,0 +1,36 @@
>>>> +/* BFD support for the WebAssembly target
>>>> +   Copyright (C) 1994-2017 Free Software Foundation, Inc.
>>>
>>> Given that this file is brand new, it is hard to see how the FSF can claim
>>> copyright from 1994...
>>
>> You're right; it is based on an older file, but has been replaced entirely.
>>
>>>> +#define ELF_TARGET_ID        0x4157 /* 'WA' */
>>>> +#define ELF_MACHINE_CODE    0x4157 /* 'WA' */
>>>
>>> You could use the value EM_WEBASSEMBLY here.  It is defined in include/elf/common.h.
>>>
>>>> +++ b/bfd/targets.c
>>>> @@ -893,6 +893,8 @@ extern const bfd_target vax_aout_nbsd_vec;
>>>>  extern const bfd_target vax_elf32_vec;
>>>>  extern const bfd_target visium_elf32_vec;
>>>>  extern const bfd_target w65_coff_vec;
>>>> +extern const bfd_target wasm_vec;
>>>> +extern const bfd_target wasm32_elf32_vec;
>>>>  extern const bfd_target we32k_coff_vec;
>>>>  extern const bfd_target x86_64_coff_vec;
>>>>  extern const bfd_target x86_64_elf32_vec;
>>>
>>> You missed out a required patch to this file.  The _bfd_target_vector
>>> array (starting at line 940) needs to have the new wasm vectors added to it.
>>> To see why, try building a set of the binutils sources configured as:
>>>
>>>   --enable-64-bit-bfd --enable-targets=all
>>
>> Thanks!
>>
>>>> diff --git a/bfd/wasm-module.c b/bfd/wasm-module.c
>>>
>>>> +/* From elf-eh-frame.c: */
>>>> +/* If *ITER hasn't reached END yet, read the next byte into *RESULT and
>>>> +   move onto the next byte.  Return true on success.  */
>>>> +
>>>> +static inline bfd_boolean
>>>> +read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
>>>
>>> Given that you are duplicating code that is already in another source file,
>>> it might be better to just change those functions to non-static and use them
>>> directly.  This avoids unnecessary code duplication...
>>
>> Yeah, the situation for the LEB128 code in particular is a bit diffuse
>> at the moment: by my count, there are eleven files (including gdb and
>> gold in the count) defining at least one LEB128 function, with
>> different calling conventions, return types, and error behaviour. I'd
>> like to remedy that in a future patch, but for now the risk of
>> namespace collisions convinced me not to export the ELF functions.
>>
>>>> +static bfd_vma
>>>> +wasm_get_uleb128 (bfd* abfd, bfd_boolean* error)
>>>
>>> It is *very* helpful to have a comment before the start of a function,
>>> explaining what it does.  (Well if it is a non-trivial function).
>>
>> Thanks, I shall add those.
>>
>>>  In
>>> particular I wondered why you need a webasm specific ULEB128 reading
>>> function.  Does webasm use non-standard ULEB128 values, or is there
>>> something else going on ?
>>
>> The problem is that WebAssembly doesn't consider it necessary to tell
>> you in advance how much data there is to be read, so EOF needs to be
>> handled carefully. (The other deviation is that WebAssembly allows
>> overlong LEB128 representations, but only if the number of bytes
>> doesn't exceed the maximum for the integer size (5 bytes for 32 bits,
>> usually)). Now that I write it down this should definitely go into
>> separate functions...
>>
>>> Given that there are already LEB128 reading functions in libbfd.c, maybe
>>> they could be used instead ?
>>
>> I'll give that another try.
>>
>>>> +    {
>>>> +      if (bfd_get_error () != bfd_error_file_truncated)
>>>> +        *errorptr = TRUE;
>>>> +      return FALSE;
>>>> +    }
>>>
>>> Why is it not an error if the read failed for some reason other than file
>>> truncation ?
>>
>> I may be misreading the code; the intention is that *errorptr is set
>> to TRUE, indicating an error, for all reasons except for file
>> truncation, which indicates EOF, which is not an error if we're done
>> with the last section.
>>
>>>> +static int
>>>> +wasm_get_byte (bfd *abfd, bfd_boolean *errorptr)
>>>> +{
>>>> +  bfd_byte byte;
>>>> +  if (bfd_bread (&byte, (bfd_size_type) 1, abfd) != 1)
>>>> +    {
>>>> +      if (bfd_get_error () != bfd_error_file_truncated)
>>>> +        *errorptr = TRUE;
>>>> +      return EOF;
>>>> +    }
>>>
>>> This particular piece of code is repeated several times in the wasm-module.c
>>> source file.  Perhaps this is a suitable case for a macro or inline function ?
>>
>> Yes, I'll make it one.
>>
>>>> +static bfd_boolean
>>>> +wasm_scan_name_function_section (bfd *abfd, sec_ptr asect,
>>>> +                                 void *data ATTRIBUTE_UNUSED)
>>>> +{
>>>> +  if (!asect)
>>>> +    return FALSE;
>>>> +
>>>> +  if (strcmp (asect->name, ".wasm.name") != 0)
>>>> +    return FALSE;
>>>> +
>>>> +  bfd_byte *p = asect->contents;
>>>> +  bfd_byte *end = asect->contents + asect->size;
>>>
>>> Please move variable declarations to the start of the block.  (So
>>> that the sources can be built with any ISO-C compliant C compiler,
>>> not just gcc).
>>
>> I'll add -Wdeclaration-after-statement to my C flags. Perhaps this
>> should be added to warning.m4?
>>
>> (I tried that, and there are a number of places that trigger the
>> warning; I fixed them, mostly to check that there were no actual bugs,
>> and things build cleanly with the warning now. Should I submit a patch
>> for that?)
>>
>>>> +  while (p && p < end)
>>>> +    {
>>>> +      if (*p++ == 1)
>>>> +        break;
>>>> +      bfd_vma payload_size;
>>>> +      if (!read_uleb128 (&p, end, &payload_size))
>>>> +        return FALSE;
>>>> +
>>>> +      p += payload_size;
>>>> +    }
>>>
>>> A very large value for payload_size could result in p wrapping round
>>> to before asect->contents.  Is there a maximum known payload size ?
>>
>> Thanks for spotting that!
>>
>>> If so then you ought to check for it here.  Otherwise check that:
>>> p < p + payload_size < end
>>
>> Will do.
>>
>>>> +  bfd_vma payload_size;
>>>
>>> You are declaring payload_size twice in this function!
>>
>> Oops, sorry about that.
>>
>>> Phew - well that is it for a first pass.  I hope that these comments were helpful.
>>
>> They are, absolutely. Thanks again, and sorry for causing you so much work,
>> Pip

Attachment: binutils-wasm-004.diff
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]