Force __pnacl_pso_root to be an external declaration.

This CL updates "isPNaClABIExternalName" -- Subzero checked for the symbol
"__pnacl_pso_root" as a function, but it is a declaration, and should be
checked as one. Additionally, when the PNaClTranslator is verifying the linkage
of declarations, allow "__pnacl_pso_root" to be flipped to external as a special
case.

TEST=pnacl-translate -pso --use-sz -arch x86-32-nonsfi test_pll.final.pso
BUG=https://bugs.chromium.org/p/nativeclient/issues/detail?id=4351
R=jpp@chromium.org

Review URL: https://codereview.chromium.org/1740033002 .
diff --git a/src/IceGlobalInits.h b/src/IceGlobalInits.h
index a111536..72f2663 100644
--- a/src/IceGlobalInits.h
+++ b/src/IceGlobalInits.h
@@ -102,6 +102,10 @@
     return isInternal() ? "internal" : "external";
   }
 
+  /// Returns true if the name of this GlobalDeclaration indicates that it
+  /// should have ExternalLinkage (as a special case).
+  virtual bool isPNaClABIExternalName() const = 0;
+
 protected:
   GlobalDeclaration(GlobalDeclarationKind Kind,
                     llvm::GlobalValue::LinkageTypes Linkage)
@@ -154,9 +158,12 @@
 
   /// Returns true if linkage is correct for the function declaration.
   bool verifyLinkageCorrect(const GlobalContext *Ctx) const {
-    if (isPNaClABIExternalName() || isIntrinsicName(Ctx))
+    if (isIntrinsicName(Ctx))
       return Linkage == llvm::GlobalValue::ExternalLinkage;
-    return verifyLinkageDefault(Ctx);
+    else if (Linkage == llvm::GlobalValue::ExternalLinkage)
+      return isPNaClABIExternalName();
+    else
+      return verifyLinkageDefault(Ctx);
   }
 
   /// Validates that the type signature of the function is correct. Returns true
@@ -196,9 +203,9 @@
       : GlobalDeclaration(FunctionDeclarationKind, Linkage),
         Signature(Signature), CallingConv(CallingConv), IsProto(IsProto) {}
 
-  bool isPNaClABIExternalName() const {
-    const char *Name = getName().c_str();
-    return strcmp(Name, "_start") == 0 || strcmp(Name, "__pnacl_pso_root") == 0;
+  bool isPNaClABIExternalName() const override {
+    static constexpr char Start[] = "_start";
+    return getName() == Start;
   }
 
   bool isIntrinsicName(const GlobalContext *Ctx) const {
@@ -416,6 +423,9 @@
 
   /// Returns true if linkage is correct for the variable declaration.
   bool verifyLinkageCorrect(const GlobalContext *Ctx) const {
+    if (isPNaClABIExternalName()) {
+      return Linkage == llvm::GlobalValue::ExternalLinkage;
+    }
     return verifyLinkageDefault(Ctx);
   }
 
@@ -433,6 +443,11 @@
 
   void discardInitializers() { Initializers = nullptr; }
 
+  bool isPNaClABIExternalName() const override {
+    static constexpr char PnaclPsoRoot[] = "__pnacl_pso_root";
+    return getName() == PnaclPsoRoot;
+  }
+
 private:
   /// List of initializers for the declared variable.
   std::unique_ptr<InitializerListType> Initializers;
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp
index 1fb84c1..edee9e5 100644
--- a/src/PNaClTranslator.cpp
+++ b/src/PNaClTranslator.cpp
@@ -476,6 +476,12 @@
                               const char *DeclType,
                               NaClBcIndexSize_t &NameIndex) {
     if (Decl->hasName()) {
+      if (Decl->isPNaClABIExternalName()) {
+        // Force linkage to be external for the PNaCl ABI. PNaCl bitcode has a
+        // linkage field for Functions, but not for GlobalVariables (because the
+        // latter is not needed for pexes, so it has been removed).
+        Decl->setLinkage(llvm::GlobalValue::ExternalLinkage);
+      }
       Translator.checkIfUnnamedNameSafe(Decl->getName(), DeclType, Prefix);
     } else {
       Decl->setName(Translator.createUnnamedName(Prefix, NameIndex));