compiler: mangle dots in pkgpath
authorIan Lance Taylor <ian@gcc.gnu.org>
Sat, 12 Oct 2019 01:06:43 +0000 (01:06 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Sat, 12 Oct 2019 01:06:43 +0000 (01:06 +0000)
    We need to mangle dots to avoid problems with -fgo-pkgpath=a.0.
    That will confuse the name mangling, which assumes that names
    entering the mangling cannot contain arbitrary dot characters.
    We don't need to mangle other characters; go_encode_id will handle them.

    Fixes golang/go#33871

    Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/200838

From-SVN: r276913

gcc/go/gofrontend/MERGE
gcc/go/gofrontend/go-encode-id.cc
gcc/go/gofrontend/go-encode-id.h
gcc/go/gofrontend/gogo.cc

index 1508eb166504666e548d5e177f4d31a664798323..1c90e55e9b198d57da99d2307bd1c11bb585d379 100644 (file)
@@ -1,4 +1,4 @@
-ddfb845fad1f2e8b84383f262ed5ea5be7b3e35a
+f174fdad69cad42309984dfa108d80f2ae8a9f78
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
index 7c7aa13ea1c9505e306026027e7722ad38677f33..550da9759f637f46bdcbcd772f326340d587eebc 100644 (file)
@@ -253,3 +253,16 @@ go_mangle_struct_tag(const std::string& tag)
     }
   return ret;
 }
+
+// Encode a package path.
+
+std::string
+go_mangle_pkgpath(const std::string& pkgpath)
+{
+  std::string s = pkgpath;
+  for (size_t i = s.find('.');
+       i != std::string::npos;
+       i = s.find('.', i + 1))
+    s.replace(i, 1, ".x2e"); // 0x2e is the ASCII encoding for '.'
+  return s;
+}
index 70126bae9d171b8f252943d8e0a0cc12c1cf94b9..2d09b0c545d630b63b896cf7594e8d60c318c04e 100644 (file)
@@ -34,4 +34,12 @@ go_selectively_encode_id(const std::string &id);
 extern std::string
 go_mangle_struct_tag(const std::string& tag);
 
+// Encode a package path.  A package path can contain any arbitrary
+// character, including '.'.  go_encode_id expects that any '.' will
+// be inserted by name mangling in a controlled manner.  So first
+// translate any '.' using the same .x encoding as used by
+// go_mangle_struct_tag.
+extern std::string
+go_mangle_pkgpath(const std::string& pkgpath);
+
 #endif // !defined(GO_ENCODE_ID_H)
index a79cfc3a9a76e9d8487dc629fedfb26b8fd867ef..1ee49dcfa9c47cd9f1b1cb21d1600881764ecd4b 100644 (file)
@@ -298,7 +298,7 @@ void
 Gogo::set_pkgpath(const std::string& arg)
 {
   go_assert(!this->pkgpath_set_);
-  this->pkgpath_ = arg;
+  this->pkgpath_ = go_mangle_pkgpath(arg);
   this->pkgpath_set_ = true;
   this->pkgpath_from_option_ = true;
 }
@@ -396,7 +396,8 @@ Gogo::set_package_name(const std::string& package_name,
        {
          if (!this->prefix_from_option_)
            this->prefix_ = "go";
-         this->pkgpath_ = this->prefix_ + '.' + package_name;
+         this->pkgpath_ = (go_mangle_pkgpath(this->prefix_) + '.'
+                           + package_name);
          this->pkgpath_symbol_ = (Gogo::pkgpath_for_symbol(this->prefix_) + '.'
                                   + Gogo::pkgpath_for_symbol(package_name));
        }