Automatic date update in version.in
[binutils-gdb.git] / bfd / wasm-module.c
index 1fde4a57ab0c55cf33b1060fc1aaf5b0c037d8cb..d97da1519a7fc55b1ed64a9e28744c5f9f8fc49f 100644 (file)
@@ -1,5 +1,5 @@
 /* BFD back-end for WebAssembly modules.
-   Copyright (C) 2017-2021 Free Software Foundation, Inc.
+   Copyright (C) 2017-2023 Free Software Foundation, Inc.
 
    Based on srec.c, mmo.c, and binary.c
 
    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)
+  while (bfd_read (&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
@@ -159,12 +169,12 @@ wasm_write_uleb128 (bfd *abfd, bfd_vma v)
       if (v)
        c |= 0x80;
 
-      if (bfd_bwrite (&c, 1, abfd) != 1)
-       return FALSE;
+      if (bfd_write (&c, 1, abfd) != 1)
+       return false;
     }
   while (v);
 
-  return TRUE;
+  return true;
 }
 
 /* Read the LEB128 integer at P, saving it to X; at end of buffer,
@@ -172,70 +182,67 @@ 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)
+  if (bfd_read (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];
 
-  if (bfd_bread (vers, sizeof (vers), abfd) == sizeof (vers)
+  if (bfd_read (vers, sizeof (vers), abfd) == sizeof (vers)
       /* 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,27 @@ 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;
+  if (symbols)
+    bfd_release (abfd, symbols);
+  tdata->symcount = 0;
+  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_read (&byte, 1, abfd) != 1)
     {
       if (bfd_get_error () != bfd_error_file_truncated)
-       *errorptr = TRUE;
+       *errorptr = true;
       return EOF;
     }
 
@@ -375,10 +384,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;
@@ -386,7 +395,7 @@ wasm_scan (bfd *abfd)
   unsigned int bytes_read;
   asection *bfdsec;
 
-  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
     goto error_return;
 
   if (!wasm_read_header (abfd, &error))
@@ -406,7 +415,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 +428,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,13 +440,13 @@ 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)
            goto error_return;
          memcpy (name, prefix, prefixlen);
-         if (bfd_bread (name + prefixlen, namelen, abfd) != namelen)
+         if (bfd_read (name + prefixlen, namelen, abfd) != namelen)
            goto error_return;
          name[prefixlen + namelen] = 0;
 
@@ -469,10 +478,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 +504,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 +532,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;
@@ -539,11 +548,11 @@ wasm_compute_custom_section_file_position (bfd *abfd,
        }
       while (nl);
 
-      bfd_seek (abfd, fs->pos, SEEK_SET);
-      if (! wasm_write_uleb128 (abfd, 0)
+      if (bfd_seek (abfd, fs->pos, SEEK_SET) != 0
+         || ! 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)
+         || bfd_write (name, name_len, abfd) != name_len)
        goto error_return;
       fs->pos = asect->filepos = bfd_tell (abfd);
     }
@@ -557,7 +566,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 +577,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;
@@ -577,11 +586,10 @@ wasm_compute_section_file_positions (bfd *abfd)
   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;
+  if (bfd_seek (abfd, (bfd_vma) 0, SEEK_SET) != 0
+      || bfd_write (magic, sizeof (magic), abfd) != (sizeof magic)
+      || bfd_write (vers, sizeof (vers), abfd) != sizeof (vers))
+    return false;
 
   for (i = 0; i < WASM_NUMBERED_SECTIONS; i++)
     numbered_sections[i] = NULL;
@@ -598,27 +606,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 +634,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;
+      || bfd_write (location, count, abfd) != count)
+    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;
+  if (bfd_write (magic, sizeof (magic), abfd) != sizeof (magic)
+      || bfd_write (vers, sizeof (vers), abfd) != sizeof (vers))
+    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,10 +745,10 @@ 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)
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
     return NULL;
 
   if (!wasm_read_header (abfd, &error))
@@ -776,6 +784,7 @@ wasm_object_p (bfd *abfd)
 #define wasm_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
 #define wasm_get_lineno                          _bfd_nosymbols_get_lineno
 #define wasm_find_nearest_line           _bfd_nosymbols_find_nearest_line
+#define wasm_find_nearest_line_with_alt          _bfd_nosymbols_find_nearest_line_with_alt
 #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
@@ -794,6 +803,7 @@ const bfd_target wasm_vec =
   ' ',                         /* AR_pad_char.  */
   255,                         /* AR_max_namelen.  */
   0,                           /* Match priority.  */
+  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
   /* 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,