Require that the module symbol table can't appear after function.

The bitcode parser assumes that the module symbol table, if it exists,
must appear before the first function
block (i.e. defininition). However, the parser did not check this
constraint. This patch fixes that omission.

BUG= https://code.google.com/p/nativeclient/issues/detail?id=4320
R=stichnot@chromium.org

Review URL: https://codereview.chromium.org/1359923002 .
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp
index 3380c9e..812537e 100644
--- a/src/PNaClTranslator.cpp
+++ b/src/PNaClTranslator.cpp
@@ -3158,6 +3158,12 @@
     if (FoundValuesymtab)
       Fatal("Duplicate valuesymtab in module");
 
+    // If we have already processed a function block (i.e. we have already
+    // installed global names and variable initializers) we can no longer accept
+    // the value symbol table. Names have already been generated.
+    if (GlobalDeclarationNamesAndInitializersInstalled)
+      Fatal("Module valuesymtab not allowed after function blocks");
+
     FoundValuesymtab = true;
     ModuleValuesymtabParser Parser(BlockID, this);
     return Parser.ParseThisBlock();
diff --git a/tests_lit/parse_errs/Inputs/symtab-after-fcn.tbc b/tests_lit/parse_errs/Inputs/symtab-after-fcn.tbc
new file mode 100644
index 0000000..4132a5c
--- /dev/null
+++ b/tests_lit/parse_errs/Inputs/symtab-after-fcn.tbc
@@ -0,0 +1,19 @@
+65535,8,2;
+1,1;
+65535,17,2;
+1,2;
+2;
+21,0,0;
+65534;
+8,1,0,0,0;
+65535,19,2;
+5,0;
+65534;
+65535,12,2;
+1,1;
+10;
+65534;
+65535,14,2;
+1,0,102;
+65534;
+65534;
diff --git a/tests_lit/parse_errs/symtab-after-fcn.test b/tests_lit/parse_errs/symtab-after-fcn.test
new file mode 100644
index 0000000..197c87b
--- /dev/null
+++ b/tests_lit/parse_errs/symtab-after-fcn.test
@@ -0,0 +1,19 @@
+; Test if we detect if the value symbol table appears after a function block.
+
+; REQUIRES: no_minimal_build
+
+; RUN: not %pnacl_sz -bitcode-as-text %p/Inputs/symtab-after-fcn.tbc \
+; RUN:     -bitcode-format=pnacl -notranslate -build-on-read 2>&1 \
+; RUN:   | FileCheck %s
+
+; CHECK: Module valuesymtab not allowed after function blocks
+
+; RUN: pnacl-bcfuzz -bitcode-as-text %p/Inputs/symtab-after-fcn.tbc \
+; RUN:   -output - | pnacl-bcdis -no-records | FileCheck -check-prefix=ASM %s
+
+; ASM: module {  // BlockID = 8
+; ASM:   function void @f0() {  // BlockID = 12
+; ASM:   }
+; ASM:   valuesymtab {  // BlockID = 14
+; ASM:   }
+; ASM: }