Change how .debug_aranges padding is skipped
[deliverable/binutils-gdb.git] / bfd / wasm-module.c
index dc135645f4acc51ee8ed5e0d68c208394ecddfb8..9735ebeabca8be02e5ad10ee8aafbc815f46dbba 100644 (file)
    https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md. */
 
 #include "sysdep.h"
-#include "alloca-conf.h"
 #include "bfd.h"
-#include <limits.h>
 #include "libiberty.h"
 #include "libbfd.h"
 #include "wasm-module.h"
 
+#include <limits.h>
+#ifndef CHAR_BIT
+#define CHAR_BIT 8
+#endif
+
 typedef struct
 {
   asymbol *      symbols;
@@ -102,36 +105,43 @@ wasm_section_name_to_code (const char *name)
    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)
+wasm_read_leb128 (bfd *abfd,
+                 bool *error_return,
+                 unsigned int *length_return,
+                 bool sign)
 {
   bfd_vma result = 0;
   unsigned int num_read = 0;
   unsigned int shift = 0;
   unsigned char byte = 0;
+  unsigned char lost, mask;
   int status = 1;
 
   while (bfd_bread (&byte, 1, abfd) == 1)
     {
       num_read++;
 
-      if (shift < sizeof (result) * 8)
+      if (shift < CHAR_BIT * sizeof (result))
        {
          result |= ((bfd_vma) (byte & 0x7f)) << shift;
-         if ((result >> shift) != (byte & 0x7f))
-           /* Overflow.  */
-           status |= 2;
+         /* These bits overflowed.  */
+         lost = byte ^ (result >> shift);
+         /* And this is the mask of possible overflow bits.  */
+         mask = 0x7f ^ ((bfd_vma) 0x7f << shift >> shift);
          shift += 7;
        }
-      else if ((byte & 0x7f) != 0)
+      else
+       {
+         lost = byte;
+         mask = 0x7f;
+       }
+      if ((lost & mask) != (sign && (bfd_signed_vma) result < 0 ? mask : 0))
        status |= 2;
 
       if ((byte & 0x80) == 0)
        {
          status &= ~1;
-         if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
+         if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
            result |= -((bfd_vma) 1 << shift);
          break;
        }
@@ -148,7 +158,7 @@ wasm_read_leb128 (bfd *               abfd,
 /* Encode an integer V as LEB128 and write it to ABFD, return TRUE on
    success.  */
 
-static bfd_boolean
+static bool
 wasm_write_uleb128 (bfd *abfd, bfd_vma v)
 {
   do
@@ -160,11 +170,11 @@ wasm_write_uleb128 (bfd *abfd, bfd_vma v)
        c |= 0x80;
 
       if (bfd_bwrite (&c, 1, abfd) != 1)
-       return FALSE;
+       return false;
     }
   while (v);
 
-  return TRUE;
+  return true;
 }
 
 /* Read the LEB128 integer at P, saving it to X; at end of buffer,
@@ -172,37 +182,34 @@ wasm_write_uleb128 (bfd *abfd, bfd_vma v)
 #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)                                            \
+      if ((p) >= (end))                                                        \
        goto error_return;                                              \
+      (x) = _bfd_safe_read_leb128 (abfd, &(p), false, (end));          \
     }                                                                  \
   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)
+static bool
+wasm_read_magic (bfd *abfd, bool *errorptr)
 {
   bfd_byte magic_const[SIZEOF_WASM_MAGIC] = WASM_MAGIC;
   bfd_byte magic[SIZEOF_WASM_MAGIC];
 
   if (bfd_bread (magic, sizeof (magic), abfd) == sizeof (magic)
       && memcmp (magic, magic_const, sizeof (magic)) == 0)
-    return TRUE;
+    return true;
 
-  *errorptr = TRUE;
-  return FALSE;
+  *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)
+static bool
+wasm_read_version (bfd *abfd, bool *errorptr)
 {
   bfd_byte vers_const[SIZEOF_WASM_VERSION] = WASM_VERSION;
   bfd_byte vers[SIZEOF_WASM_VERSION];
@@ -211,31 +218,31 @@ wasm_read_version (bfd *abfd, bfd_boolean *errorptr)
       /* Don't attempt to parse newer versions, which are likely to
         require code changes.  */
       && memcmp (vers, vers_const, sizeof (vers)) == 0)
-    return TRUE;
+    return true;
 
-  *errorptr = TRUE;
-  return FALSE;
+  *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)
+static bool
+wasm_read_header (bfd *abfd, bool *errorptr)
 {
   if (! wasm_read_magic (abfd, errorptr))
-    return FALSE;
+    return false;
 
   if (! wasm_read_version (abfd, errorptr))
-    return FALSE;
+    return false;
 
-  return TRUE;
+  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
+static bool
 wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
 {
   bfd_byte *p;
@@ -251,7 +258,7 @@ wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
   end = asect->contents + asect->size;
 
   if (!p)
-    return FALSE;
+    return false;
 
   while (p < end)
     {
@@ -263,23 +270,23 @@ wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
         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;
+       return false;
 
       READ_LEB128 (payload_size, p, end);
 
       if (payload_size > (size_t) (end - p))
-       return FALSE;
+       return false;
 
       p += payload_size;
     }
 
   if (p >= end)
-    return FALSE;
+    return false;
 
   READ_LEB128 (payload_size, p, end);
 
   if (payload_size > (size_t) (end - p))
-    return FALSE;
+    return false;
 
   end = p + payload_size;
 
@@ -287,7 +294,7 @@ wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
 
   /* Sanity check: each symbol has at least two bytes.  */
   if (symcount > payload_size / 2)
-    return FALSE;
+    return false;
 
   tdata->symcount = symcount;
 
@@ -300,16 +307,16 @@ wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
       = bfd_get_section_by_name (abfd, WASM_SECTION_FUNCTION_INDEX);
 
   if (!space_function_index)
-    return FALSE;
+    return false;
 
   if (_bfd_mul_overflow (tdata->symcount, sizeof (asymbol), &amt))
     {
       bfd_set_error (bfd_error_file_too_big);
-      return FALSE;
+      return false;
     }
   symbols = bfd_alloc (abfd, amt);
   if (!symbols)
-    return FALSE;
+    return false;
 
   for (symcount = 0; p < end && symcount < tdata->symcount; symcount++)
     {
@@ -347,25 +354,25 @@ wasm_scan_name_function_section (bfd *abfd, sec_ptr asect)
   tdata->symbols = symbols;
   abfd->symcount = symcount;
 
-  return TRUE;
+  return true;
 
  error_return:
   bfd_release (abfd, symbols);
-  return FALSE;
+  return false;
 }
 
 /* Read a byte from ABFD and return it, or EOF for EOF or error.
    Set ERRORPTR on non-EOF error.  */
 
 static int
-wasm_read_byte (bfd *abfd, bfd_boolean *errorptr)
+wasm_read_byte (bfd *abfd, bool *errorptr)
 {
   bfd_byte byte;
 
   if (bfd_bread (&byte, (bfd_size_type) 1, abfd) != 1)
     {
       if (bfd_get_error () != bfd_error_file_truncated)
-       *errorptr = TRUE;
+       *errorptr = true;
       return EOF;
     }
 
@@ -375,10 +382,10 @@ wasm_read_byte (bfd *abfd, bfd_boolean *errorptr)
 /* Scan the wasm module ABFD, creating sections and symbols.
    Return TRUE on success.  */
 
-static bfd_boolean
+static bool
 wasm_scan (bfd *abfd)
 {
-  bfd_boolean error = FALSE;
+  bool error = false;
   /* Fake VMAs for now. Choose 0x80000000 as base to avoid clashes
      with actual data addresses.  */
   bfd_vma vma = 0x80000000;
@@ -406,7 +413,7 @@ wasm_scan (bfd *abfd)
          if (bfdsec == NULL)
            goto error_return;
 
-         bfdsec->size = wasm_read_leb128 (abfd, &error, &bytes_read, FALSE);
+         bfdsec->size = wasm_read_leb128 (abfd, &error, &bytes_read, false);
          if (error)
            goto error_return;
        }
@@ -419,10 +426,10 @@ wasm_scan (bfd *abfd)
          size_t prefixlen = strlen (prefix);
          ufile_ptr filesize;
 
-         payload_len = wasm_read_leb128 (abfd, &error, &bytes_read, FALSE);
+         payload_len = wasm_read_leb128 (abfd, &error, &bytes_read, false);
          if (error)
            goto error_return;
-         namelen = wasm_read_leb128 (abfd, &error, &bytes_read, FALSE);
+         namelen = wasm_read_leb128 (abfd, &error, &bytes_read, false);
          if (error || bytes_read > payload_len
              || namelen > payload_len - bytes_read)
            goto error_return;
@@ -431,7 +438,7 @@ wasm_scan (bfd *abfd)
          if (filesize != 0 && namelen > filesize)
            {
              bfd_set_error (bfd_error_file_truncated);
-             return FALSE;
+             return false;
            }
          name = bfd_alloc (abfd, namelen + prefixlen + 1);
          if (!name)
@@ -469,10 +476,10 @@ wasm_scan (bfd *abfd)
   if (error)
     goto error_return;
 
-  return TRUE;
+  return true;
 
  error_return:
-  return FALSE;
+  return false;
 }
 
 /* Put a numbered section ASECT of ABFD into the table of numbered
@@ -495,7 +502,7 @@ wasm_register_section (bfd *abfd ATTRIBUTE_UNUSED,
 struct compute_section_arg
 {
   bfd_vma pos;
-  bfd_boolean failed;
+  bool failed;
 };
 
 /* Compute the file position of ABFD's section ASECT.  FSARG is a
@@ -523,7 +530,7 @@ wasm_compute_custom_section_file_position (bfd *abfd,
   if (idx != 0)
     return;
 
-  if (CONST_STRNEQ (asect->name, WASM_SECTION_PREFIX))
+  if (startswith (asect->name, WASM_SECTION_PREFIX))
     {
       const char *name = asect->name + strlen (WASM_SECTION_PREFIX);
       bfd_size_type payload_len = asect->size;
@@ -557,7 +564,7 @@ wasm_compute_custom_section_file_position (bfd *abfd,
   return;
 
  error_return:
-  fs->failed = TRUE;
+  fs->failed = true;
 }
 
 /* Compute the file positions for the sections of ABFD.  Currently,
@@ -568,7 +575,7 @@ wasm_compute_custom_section_file_position (bfd *abfd,
    their ids, but custom sections can appear in any position and any
    order, and more than once. FIXME: support that.  */
 
-static bfd_boolean
+static bool
 wasm_compute_section_file_positions (bfd *abfd)
 {
   bfd_byte magic[SIZEOF_WASM_MAGIC] = WASM_MAGIC;
@@ -581,7 +588,7 @@ wasm_compute_section_file_positions (bfd *abfd)
 
   if (bfd_bwrite (magic, sizeof (magic), abfd) != (sizeof magic)
       || bfd_bwrite (vers, sizeof (vers), abfd) != sizeof (vers))
-    return FALSE;
+    return false;
 
   for (i = 0; i < WASM_NUMBERED_SECTIONS; i++)
     numbered_sections[i] = NULL;
@@ -598,27 +605,27 @@ wasm_compute_section_file_positions (bfd *abfd)
        continue;
       size = sec->size;
       if (bfd_seek (abfd, fs.pos, SEEK_SET) != 0)
-       return FALSE;
+       return false;
       if (! wasm_write_uleb128 (abfd, i)
          || ! wasm_write_uleb128 (abfd, size))
-       return FALSE;
+       return false;
       fs.pos = sec->filepos = bfd_tell (abfd);
       fs.pos += size;
     }
 
-  fs.failed = FALSE;
+  fs.failed = false;
 
   bfd_map_over_sections (abfd, wasm_compute_custom_section_file_position, &fs);
 
   if (fs.failed)
-    return FALSE;
+    return false;
 
-  abfd->output_has_begun = TRUE;
+  abfd->output_has_begun = true;
 
-  return TRUE;
+  return true;
 }
 
-static bfd_boolean
+static bool
 wasm_set_section_contents (bfd *abfd,
                           sec_ptr section,
                           const void *location,
@@ -626,49 +633,49 @@ wasm_set_section_contents (bfd *abfd,
                           bfd_size_type count)
 {
   if (count == 0)
-    return TRUE;
+    return true;
 
   if (! abfd->output_has_begun
       && ! wasm_compute_section_file_positions (abfd))
-    return FALSE;
+    return false;
 
   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
       || bfd_bwrite (location, count, abfd) != count)
-    return FALSE;
+    return false;
 
-  return TRUE;
+  return true;
 }
 
-static bfd_boolean
+static bool
 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;
+    return false;
 
   if (bfd_bwrite (magic, sizeof (magic), abfd) != sizeof (magic)
       || bfd_bwrite (vers, sizeof (vers), abfd) != sizeof (vers))
-    return FALSE;
+    return false;
 
-  return TRUE;
+  return true;
 }
 
-static bfd_boolean
+static bool
 wasm_mkobject (bfd *abfd)
 {
   tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
 
   if (! tdata)
-    return FALSE;
+    return false;
 
   tdata->symbols = NULL;
   tdata->symcount = 0;
 
   abfd->tdata.any = tdata;
 
-  return TRUE;
+  return true;
 }
 
 static long
@@ -737,7 +744,7 @@ wasm_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
 static bfd_cleanup
 wasm_object_p (bfd *abfd)
 {
-  bfd_boolean error;
+  bool error;
   asection *s;
 
   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
This page took 0.030693 seconds and 4 git commands to generate.