Go patch commited: Mangle dots in pkgpath

Message ID CAOyqgcVJMW9P2WhOCjK9mVpcxdT9mpKwBC2aSJDOFjVuu_LaYw@mail.gmail.com
State New
Headers show
Series
  • Go patch commited: Mangle dots in pkgpath
Related show

Commit Message

Ian Lance Taylor Oct. 12, 2019, 1:06 a.m.
The Go frontend needs 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.  This patch fixes the problem.  It fixes
https://golang.org/issue/33871.  Bootstrapped and ran Go testsuite on
x86_64-pc-linux-gnu.  Committed to mainline.

Ian

Patch

Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE	(revision 276594)
+++ gcc/go/gofrontend/MERGE	(working copy)
@@ -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: gcc/go/gofrontend/go-encode-id.cc
===================================================================
--- gcc/go/gofrontend/go-encode-id.cc	(revision 276382)
+++ gcc/go/gofrontend/go-encode-id.cc	(working copy)
@@ -253,3 +253,16 @@  go_mangle_struct_tag(const std::string&
     }
   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: gcc/go/gofrontend/go-encode-id.h
===================================================================
--- gcc/go/gofrontend/go-encode-id.h	(revision 276382)
+++ gcc/go/gofrontend/go-encode-id.h	(working copy)
@@ -34,4 +34,12 @@  go_selectively_encode_id(const std::stri
 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: gcc/go/gofrontend/gogo.cc
===================================================================
--- gcc/go/gofrontend/gogo.cc	(revision 276579)
+++ gcc/go/gofrontend/gogo.cc	(working copy)
@@ -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
 	{
 	  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));
 	}