mesa: Fix memory leak in out-of-memory path.
[mesa.git] / src / glsl / linker.cpp
index 255edc6a76f179d2198805c8db0e0b8c780a7958..a7c38a3426a5f914d638476d698112aa9cc113ad 100644 (file)
@@ -164,7 +164,7 @@ private:
 
 
 void
-linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
+linker_error(gl_shader_program *prog, const char *fmt, ...)
 {
    va_list ap;
 
@@ -172,6 +172,21 @@ linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
    va_start(ap, fmt);
    ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
    va_end(ap);
+
+   prog->LinkStatus = false;
+}
+
+
+void
+linker_warning(gl_shader_program *prog, const char *fmt, ...)
+{
+   va_list ap;
+
+   ralloc_strcat(&prog->InfoLog, "error: ");
+   va_start(ap, fmt);
+   ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
+   va_end(ap);
+
 }
 
 
@@ -229,7 +244,9 @@ count_attribute_slots(const glsl_type *t)
 
 
 /**
- * Verify that a vertex shader executable meets all semantic requirements
+ * Verify that a vertex shader executable meets all semantic requirements.
+ *
+ * Also sets prog->Vert.UsesClipDistance as a side effect.
  *
  * \param shader  Vertex shader executable to be verified
  */
@@ -243,11 +260,30 @@ validate_vertex_shader_executable(struct gl_shader_program *prog,
    find_assignment_visitor find("gl_Position");
    find.run(shader->ir);
    if (!find.variable_found()) {
-      linker_error_printf(prog,
-                         "vertex shader does not write to `gl_Position'\n");
+      linker_error(prog, "vertex shader does not write to `gl_Position'\n");
       return false;
    }
 
+   if (prog->Version >= 130) {
+      /* From section 7.1 (Vertex Shader Special Variables) of the
+       * GLSL 1.30 spec:
+       *
+       *   "It is an error for a shader to statically write both
+       *   gl_ClipVertex and gl_ClipDistance."
+       */
+      find_assignment_visitor clip_vertex("gl_ClipVertex");
+      find_assignment_visitor clip_distance("gl_ClipDistance");
+
+      clip_vertex.run(shader->ir);
+      clip_distance.run(shader->ir);
+      if (clip_vertex.variable_found() && clip_distance.variable_found()) {
+         linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
+                      "and `gl_ClipDistance'\n");
+         return false;
+      }
+      prog->Vert.UsesClipDistance = clip_distance.variable_found();
+   }
+
    return true;
 }
 
@@ -271,8 +307,8 @@ validate_fragment_shader_executable(struct gl_shader_program *prog,
    frag_data.run(shader->ir);
 
    if (frag_color.variable_found() && frag_data.variable_found()) {
-      linker_error_printf(prog,  "fragment shader writes to both "
-                         "`gl_FragColor' and `gl_FragData'\n");
+      linker_error(prog,  "fragment shader writes to both "
+                  "`gl_FragColor' and `gl_FragData'\n");
       return false;
    }
 
@@ -357,11 +393,11 @@ cross_validate_globals(struct gl_shader_program *prog,
                     existing->type = var->type;
                  }
               } else {
-                 linker_error_printf(prog, "%s `%s' declared as type "
-                                     "`%s' and type `%s'\n",
-                                     mode_string(var),
-                                     var->name, var->type->name,
-                                     existing->type->name);
+                 linker_error(prog, "%s `%s' declared as type "
+                              "`%s' and type `%s'\n",
+                              mode_string(var),
+                              var->name, var->type->name,
+                              existing->type->name);
                  return false;
               }
            }
@@ -369,9 +405,9 @@ cross_validate_globals(struct gl_shader_program *prog,
            if (var->explicit_location) {
               if (existing->explicit_location
                   && (var->location != existing->location)) {
-                    linker_error_printf(prog, "explicit locations for %s "
-                                        "`%s' have differing values\n",
-                                        mode_string(var), var->name);
+                    linker_error(prog, "explicit locations for %s "
+                                 "`%s' have differing values\n",
+                                 mode_string(var), var->name);
                     return false;
               }
 
@@ -381,7 +417,7 @@ cross_validate_globals(struct gl_shader_program *prog,
 
         /* Validate layout qualifiers for gl_FragDepth.
          *
-         * From the AMD_conservative_depth spec:
+         * From the AMD/ARB_conservative_depth specs:
          *    "If gl_FragDepth is redeclared in any fragment shader in
          *    a program, it must be redeclared in all fragment shaders in that
          *    program that have static assignments to gl_FragDepth. All
@@ -392,12 +428,12 @@ cross_validate_globals(struct gl_shader_program *prog,
            bool layout_declared = var->depth_layout != ir_depth_layout_none;
            bool layout_differs = var->depth_layout != existing->depth_layout;
            if (layout_declared && layout_differs) {
-              linker_error_printf(prog,
+              linker_error(prog,
                  "All redeclarations of gl_FragDepth in all fragment shaders "
                  "in a single program must have the same set of qualifiers.");
            }
            if (var->used && layout_differs) {
-              linker_error_printf(prog,
+              linker_error(prog,
                     "If gl_FragDepth is redeclared with a layout qualifier in"
                     "any fragment shader, it must be redeclared with the same"
                     "layout qualifier in all fragment shaders that have"
@@ -410,9 +446,9 @@ cross_validate_globals(struct gl_shader_program *prog,
            if (var->constant_value != NULL) {
               if (existing->constant_value != NULL) {
                  if (!var->constant_value->has_value(existing->constant_value)) {
-                    linker_error_printf(prog, "initializers for %s "
-                                        "`%s' have differing values\n",
-                                        mode_string(var), var->name);
+                    linker_error(prog, "initializers for %s "
+                                 "`%s' have differing values\n",
+                                 mode_string(var), var->name);
                     return false;
                  }
               } else
@@ -433,15 +469,15 @@ cross_validate_globals(struct gl_shader_program *prog,
            }
 
            if (existing->invariant != var->invariant) {
-              linker_error_printf(prog, "declarations for %s `%s' have "
-                                  "mismatching invariant qualifiers\n",
-                                  mode_string(var), var->name);
+              linker_error(prog, "declarations for %s `%s' have "
+                           "mismatching invariant qualifiers\n",
+                           mode_string(var), var->name);
               return false;
            }
             if (existing->centroid != var->centroid) {
-               linker_error_printf(prog, "declarations for %s `%s' have "
-                                   "mismatching centroid qualifiers\n",
-                                   mode_string(var), var->name);
+               linker_error(prog, "declarations for %s `%s' have "
+                           "mismatching centroid qualifiers\n",
+                           mode_string(var), var->name);
                return false;
             }
         } else
@@ -529,13 +565,12 @@ cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
             */
            if (!output->type->is_array()
                || (strncmp("gl_", output->name, 3) != 0)) {
-              linker_error_printf(prog,
-                                  "%s shader output `%s' declared as "
-                                  "type `%s', but %s shader input declared "
-                                  "as type `%s'\n",
-                                  producer_stage, output->name,
-                                  output->type->name,
-                                  consumer_stage, input->type->name);
+              linker_error(prog,
+                           "%s shader output `%s' declared as type `%s', "
+                           "but %s shader input declared as type `%s'\n",
+                           producer_stage, output->name,
+                           output->type->name,
+                           consumer_stage, input->type->name);
               return false;
            }
         }
@@ -543,40 +578,40 @@ cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
         /* Check that all of the qualifiers match between stages.
          */
         if (input->centroid != output->centroid) {
-           linker_error_printf(prog,
-                               "%s shader output `%s' %s centroid qualifier, "
-                               "but %s shader input %s centroid qualifier\n",
-                               producer_stage,
-                               output->name,
-                               (output->centroid) ? "has" : "lacks",
-                               consumer_stage,
-                               (input->centroid) ? "has" : "lacks");
+           linker_error(prog,
+                        "%s shader output `%s' %s centroid qualifier, "
+                        "but %s shader input %s centroid qualifier\n",
+                        producer_stage,
+                        output->name,
+                        (output->centroid) ? "has" : "lacks",
+                        consumer_stage,
+                        (input->centroid) ? "has" : "lacks");
            return false;
         }
 
         if (input->invariant != output->invariant) {
-           linker_error_printf(prog,
-                               "%s shader output `%s' %s invariant qualifier, "
-                               "but %s shader input %s invariant qualifier\n",
-                               producer_stage,
-                               output->name,
-                               (output->invariant) ? "has" : "lacks",
-                               consumer_stage,
-                               (input->invariant) ? "has" : "lacks");
+           linker_error(prog,
+                        "%s shader output `%s' %s invariant qualifier, "
+                        "but %s shader input %s invariant qualifier\n",
+                        producer_stage,
+                        output->name,
+                        (output->invariant) ? "has" : "lacks",
+                        consumer_stage,
+                        (input->invariant) ? "has" : "lacks");
            return false;
         }
 
         if (input->interpolation != output->interpolation) {
-           linker_error_printf(prog,
-                               "%s shader output `%s' specifies %s "
-                               "interpolation qualifier, "
-                               "but %s shader input specifies %s "
-                               "interpolation qualifier\n",
-                               producer_stage,
-                               output->name,
-                               output->interpolation_string(),
-                               consumer_stage,
-                               input->interpolation_string());
+           linker_error(prog,
+                        "%s shader output `%s' specifies %s "
+                        "interpolation qualifier, "
+                        "but %s shader input specifies %s "
+                        "interpolation qualifier\n",
+                        producer_stage,
+                        output->name,
+                        output->interpolation_string(),
+                        consumer_stage,
+                        input->interpolation_string());
            return false;
         }
       }
@@ -823,9 +858,8 @@ link_intrastage_shaders(void *mem_ctx,
 
               if ((other_sig != NULL) && other_sig->is_defined
                   && !other_sig->is_builtin) {
-                 linker_error_printf(prog,
-                                     "function `%s' is multiply defined",
-                                     f->name);
+                 linker_error(prog, "function `%s' is multiply defined",
+                              f->name);
                  return NULL;
               }
            }
@@ -849,9 +883,9 @@ link_intrastage_shaders(void *mem_ctx,
    }
 
    if (main == NULL) {
-      linker_error_printf(prog, "%s shader lacks `main'\n",
-                         (shader_list[0]->Type == GL_VERTEX_SHADER)
-                         ? "vertex" : "fragment");
+      linker_error(prog, "%s shader lacks `main'\n",
+                  (shader_list[0]->Type == GL_VERTEX_SHADER)
+                  ? "vertex" : "fragment");
       return NULL;
    }
 
@@ -910,6 +944,14 @@ link_intrastage_shaders(void *mem_ctx,
 
    free(linking_shaders);
 
+#ifdef DEBUG
+   /* At this point linked should contain all of the linked IR, so
+    * validate it to make sure nothing went wrong.
+    */
+   if (linked)
+      validate_ir_tree(linked->ir);
+#endif
+
    /* Make a pass over all variable declarations to ensure that arrays with
     * unspecified sizes have a size specified.  The size is inferred from the
     * max_array_access field.
@@ -1194,16 +1236,43 @@ find_available_slots(unsigned used_mask, unsigned needed_count)
 }
 
 
+/**
+ * Assign locations for either VS inputs for FS outputs
+ *
+ * \param prog          Shader program whose variables need locations assigned
+ * \param target_index  Selector for the program target to receive location
+ *                      assignmnets.  Must be either \c MESA_SHADER_VERTEX or
+ *                      \c MESA_SHADER_FRAGMENT.
+ * \param max_index     Maximum number of generic locations.  This corresponds
+ *                      to either the maximum number of draw buffers or the
+ *                      maximum number of generic attributes.
+ *
+ * \return
+ * If locations are successfully assigned, true is returned.  Otherwise an
+ * error is emitted to the shader link log and false is returned.
+ *
+ * \bug
+ * Locations set via \c glBindFragDataLocation are not currently supported.
+ * Only locations assigned automatically by the linker, explicitly set by a
+ * layout qualifier, or explicitly set by a built-in variable (e.g., \c
+ * gl_FragColor) are supported for fragment shaders.
+ */
 bool
-assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index)
+assign_attribute_or_color_locations(gl_shader_program *prog,
+                                   unsigned target_index,
+                                   unsigned max_index)
 {
-   /* Mark invalid attribute locations as being used.
+   /* Mark invalid locations as being used.
     */
-   unsigned used_locations = (max_attribute_index >= 32)
-      ? ~0 : ~((1 << max_attribute_index) - 1);
+   unsigned used_locations = (max_index >= 32)
+      ? ~0 : ~((1 << max_index) - 1);
 
-   gl_shader *const sh = prog->_LinkedShaders[0];
-   assert(sh->Type == GL_VERTEX_SHADER);
+   assert((target_index == MESA_SHADER_VERTEX)
+         || (target_index == MESA_SHADER_FRAGMENT));
+
+   gl_shader *const sh = prog->_LinkedShaders[target_index];
+   if (sh == NULL)
+      return true;
 
    /* Operate in a total of four passes.
     *
@@ -1220,72 +1289,14 @@ assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index
     * 4. Assign locations to any inputs without assigned locations.
     */
 
-   invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0);
+   const int generic_base = (target_index == MESA_SHADER_VERTEX)
+      ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
 
-   if (prog->Attributes != NULL) {
-      for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
-        ir_variable *const var =
-           sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
+   const enum ir_variable_mode direction =
+      (target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
 
-        /* Note: attributes that occupy multiple slots, such as arrays or
-         * matrices, may appear in the attrib array multiple times.
-         */
-        if ((var == NULL) || (var->location != -1))
-           continue;
 
-        /* From page 61 of the OpenGL 4.0 spec:
-         *
-         *     "LinkProgram will fail if the attribute bindings assigned by
-         *     BindAttribLocation do not leave not enough space to assign a
-         *     location for an active matrix attribute or an active attribute
-         *     array, both of which require multiple contiguous generic
-         *     attributes."
-         *
-         * Previous versions of the spec contain similar language but omit the
-         * bit about attribute arrays.
-         *
-         * Page 61 of the OpenGL 4.0 spec also says:
-         *
-         *     "It is possible for an application to bind more than one
-         *     attribute name to the same location. This is referred to as
-         *     aliasing. This will only work if only one of the aliased
-         *     attributes is active in the executable program, or if no path
-         *     through the shader consumes more than one attribute of a set
-         *     of attributes aliased to the same location. A link error can
-         *     occur if the linker determines that every path through the
-         *     shader consumes multiple aliased attributes, but
-         *     implementations are not required to generate an error in this
-         *     case."
-         *
-         * These two paragraphs are either somewhat contradictory, or I don't
-         * fully understand one or both of them.
-         */
-        /* FINISHME: The code as currently written does not support attribute
-         * FINISHME: location aliasing (see comment above).
-         */
-        const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
-        const unsigned slots = count_attribute_slots(var->type);
-
-        /* Mask representing the contiguous slots that will be used by this
-         * attribute.
-         */
-        const unsigned use_mask = (1 << slots) - 1;
-
-        /* Generate a link error if the set of bits requested for this
-         * attribute overlaps any previously allocated bits.
-         */
-        if ((~(use_mask << attr) & used_locations) != used_locations) {
-           linker_error_printf(prog,
-                               "insufficient contiguous attribute locations "
-                               "available for vertex shader input `%s'",
-                               var->name);
-           return false;
-        }
-
-        var->location = VERT_ATTRIB_GENERIC0 + attr;
-        used_locations |= (use_mask << attr);
-      }
-   }
+   invalidate_variable_locations(sh, direction, generic_base);
 
    /* Temporary storage for the set of attributes that need locations assigned.
     */
@@ -1309,33 +1320,90 @@ assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index
    foreach_list(node, sh->ir) {
       ir_variable *const var = ((ir_instruction *) node)->as_variable();
 
-      if ((var == NULL) || (var->mode != ir_var_in))
+      if ((var == NULL) || (var->mode != (unsigned) direction))
         continue;
 
       if (var->explicit_location) {
-        const unsigned slots = count_attribute_slots(var->type);
-        const unsigned use_mask = (1 << slots) - 1;
-        const int attr = var->location - VERT_ATTRIB_GENERIC0;
-
-        if ((var->location >= (int)(max_attribute_index + VERT_ATTRIB_GENERIC0))
+        if ((var->location >= (int)(max_index + generic_base))
             || (var->location < 0)) {
-           linker_error_printf(prog,
-                               "invalid explicit location %d specified for "
-                               "`%s'\n",
-                               (var->location < 0) ? var->location : attr,
-                               var->name);
+           linker_error(prog,
+                        "invalid explicit location %d specified for `%s'\n",
+                        (var->location < 0)
+                        ? var->location : var->location - generic_base,
+                        var->name);
            return false;
-        } else if (var->location >= VERT_ATTRIB_GENERIC0) {
-           used_locations |= (use_mask << attr);
+        }
+      } else if (target_index == MESA_SHADER_VERTEX) {
+        unsigned binding;
+
+        if (prog->AttributeBindings->get(binding, var->name)) {
+           assert(binding >= VERT_ATTRIB_GENERIC0);
+           var->location = binding;
         }
       }
 
-      /* The location was explicitly assigned, nothing to do here.
+      /* If the variable is not a built-in and has a location statically
+       * assigned in the shader (presumably via a layout qualifier), make sure
+       * that it doesn't collide with other assigned locations.  Otherwise,
+       * add it to the list of variables that need linker-assigned locations.
        */
-      if (var->location != -1)
+      const unsigned slots = count_attribute_slots(var->type);
+      if (var->location != -1) {
+        if (var->location >= generic_base) {
+           /* From page 61 of the OpenGL 4.0 spec:
+            *
+            *     "LinkProgram will fail if the attribute bindings assigned
+            *     by BindAttribLocation do not leave not enough space to
+            *     assign a location for an active matrix attribute or an
+            *     active attribute array, both of which require multiple
+            *     contiguous generic attributes."
+            *
+            * Previous versions of the spec contain similar language but omit
+            * the bit about attribute arrays.
+            *
+            * Page 61 of the OpenGL 4.0 spec also says:
+            *
+            *     "It is possible for an application to bind more than one
+            *     attribute name to the same location. This is referred to as
+            *     aliasing. This will only work if only one of the aliased
+            *     attributes is active in the executable program, or if no
+            *     path through the shader consumes more than one attribute of
+            *     a set of attributes aliased to the same location. A link
+            *     error can occur if the linker determines that every path
+            *     through the shader consumes multiple aliased attributes,
+            *     but implementations are not required to generate an error
+            *     in this case."
+            *
+            * These two paragraphs are either somewhat contradictory, or I
+            * don't fully understand one or both of them.
+            */
+           /* FINISHME: The code as currently written does not support
+            * FINISHME: attribute location aliasing (see comment above).
+            */
+           /* Mask representing the contiguous slots that will be used by
+            * this attribute.
+            */
+           const unsigned attr = var->location - generic_base;
+           const unsigned use_mask = (1 << slots) - 1;
+
+           /* Generate a link error if the set of bits requested for this
+            * attribute overlaps any previously allocated bits.
+            */
+           if ((~(use_mask << attr) & used_locations) != used_locations) {
+              linker_error(prog,
+                           "insufficient contiguous attribute locations "
+                           "available for vertex shader input `%s'",
+                           var->name);
+              return false;
+           }
+
+           used_locations |= (use_mask << attr);
+        }
+
         continue;
+      }
 
-      to_assign[num_attr].slots = count_attribute_slots(var->type);
+      to_assign[num_attr].slots = slots;
       to_assign[num_attr].var = var;
       num_attr++;
    }
@@ -1349,14 +1417,16 @@ assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index
 
    qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
 
-   /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS.  It can only
-    * be explicitly assigned by via glBindAttribLocation.  Mark it as reserved
-    * to prevent it from being automatically allocated below.
-    */
-   find_deref_visitor find("gl_Vertex");
-   find.run(sh->ir);
-   if (find.variable_found())
-      used_locations |= (1 << 0);
+   if (target_index == MESA_SHADER_VERTEX) {
+      /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS.  It can
+       * only be explicitly assigned by via glBindAttribLocation.  Mark it as
+       * reserved to prevent it from being automatically allocated below.
+       */
+      find_deref_visitor find("gl_Vertex");
+      find.run(sh->ir);
+      if (find.variable_found())
+        used_locations |= (1 << 0);
+   }
 
    for (unsigned i = 0; i < num_attr; i++) {
       /* Mask representing the contiguous slots that will be used by this
@@ -1367,14 +1437,17 @@ assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index
       int location = find_available_slots(used_locations, to_assign[i].slots);
 
       if (location < 0) {
-        linker_error_printf(prog,
-                            "insufficient contiguous attribute locations "
-                            "available for vertex shader input `%s'",
-                            to_assign[i].var->name);
+        const char *const string = (target_index == MESA_SHADER_VERTEX)
+           ? "vertex shader input" : "fragment shader output";
+
+        linker_error(prog,
+                     "insufficient contiguous attribute locations "
+                     "available for %s `%s'",
+                     string, to_assign[i].var->name);
         return false;
       }
 
-      to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location;
+      to_assign[i].var->location = generic_base + location;
       used_locations |= (use_mask << location);
    }
 
@@ -1405,8 +1478,9 @@ demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
 }
 
 
-void
-assign_varying_locations(struct gl_shader_program *prog,
+bool
+assign_varying_locations(struct gl_context *ctx,
+                        struct gl_shader_program *prog,
                         gl_shader *producer, gl_shader *consumer)
 {
    /* FINISHME: Set dynamically when geometry shader support is added. */
@@ -1462,6 +1536,8 @@ assign_varying_locations(struct gl_shader_program *prog,
       }
    }
 
+   unsigned varying_vectors = 0;
+
    foreach_list(node, consumer->ir) {
       ir_variable *const var = ((ir_instruction *) node)->as_variable();
 
@@ -1483,17 +1559,40 @@ assign_varying_locations(struct gl_shader_program *prog,
             * "glsl1-varying read but not written" in piglit.
             */
 
-           linker_error_printf(prog, "fragment shader varying %s not written "
-                               "by vertex shader\n.", var->name);
-           prog->LinkStatus = false;
+           linker_error(prog, "fragment shader varying %s not written "
+                        "by vertex shader\n.", var->name);
         }
 
         /* An 'in' variable is only really a shader input if its
          * value is written by the previous stage.
          */
         var->mode = ir_var_auto;
+      } else {
+        /* The packing rules are used for vertex shader inputs are also used
+         * for fragment shader inputs.
+         */
+        varying_vectors += count_attribute_slots(var->type);
       }
    }
+
+   if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
+      if (varying_vectors > ctx->Const.MaxVarying) {
+        linker_error(prog, "shader uses too many varying vectors "
+                     "(%u > %u)\n",
+                     varying_vectors, ctx->Const.MaxVarying);
+        return false;
+      }
+   } else {
+      const unsigned float_components = varying_vectors * 4;
+      if (float_components > ctx->Const.MaxVarying * 4) {
+        linker_error(prog, "shader uses too many varying components "
+                     "(%u > %u)\n",
+                     float_components, ctx->Const.MaxVarying * 4);
+        return false;
+      }
+   }
+
+   return true;
 }
 
 
@@ -1552,8 +1651,8 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
    assert(max_version <= 130);
    if ((max_version >= 130 || min_version == 100)
        && min_version != max_version) {
-      linker_error_printf(prog, "all shaders must use same shading "
-                         "language version\n");
+      linker_error(prog, "all shaders must use same shading "
+                  "language version\n");
       goto done;
    }
 
@@ -1636,6 +1735,13 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
       if (prog->_LinkedShaders[i] == NULL)
         continue;
 
+      detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
+      if (!prog->LinkStatus)
+        goto done;
+
+      if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
+         lower_clip_distance(prog->_LinkedShaders[i]->ir);
+
       while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
         ;
    }
@@ -1644,16 +1750,17 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
 
    assign_uniform_locations(prog);
 
-   if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
-      /* FINISHME: The value of the max_attribute_index parameter is
-       * FINISHME: implementation dependent based on the value of
-       * FINISHME: GL_MAX_VERTEX_ATTRIBS.  GL_MAX_VERTEX_ATTRIBS must be
-       * FINISHME: at least 16, so hardcode 16 for now.
-       */
-      if (!assign_attribute_locations(prog, 16)) {
-        prog->LinkStatus = false;
-        goto done;
-      }
+   /* FINISHME: The value of the max_attribute_index parameter is
+    * FINISHME: implementation dependent based on the value of
+    * FINISHME: GL_MAX_VERTEX_ATTRIBS.  GL_MAX_VERTEX_ATTRIBS must be
+    * FINISHME: at least 16, so hardcode 16 for now.
+    */
+   if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
+      goto done;
+   }
+
+   if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, ctx->Const.MaxDrawBuffers)) {
+      goto done;
    }
 
    unsigned prev;
@@ -1666,9 +1773,12 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
       if (prog->_LinkedShaders[i] == NULL)
         continue;
 
-      assign_varying_locations(prog,
-                              prog->_LinkedShaders[prev],
-                              prog->_LinkedShaders[i]);
+      if (!assign_varying_locations(ctx, prog,
+                                   prog->_LinkedShaders[prev],
+                                   prog->_LinkedShaders[i])) {
+        goto done;
+      }
+
       prev = i;
    }
 
@@ -1695,13 +1805,12 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
     * present in a linked program.  By checking for use of shading language
     * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
     */
-   if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
+   if (!prog->InternalSeparateShader &&
+       (ctx->API == API_OPENGLES2 || prog->Version == 100)) {
       if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
-        linker_error_printf(prog, "program lacks a vertex shader\n");
-        prog->LinkStatus = false;
+        linker_error(prog, "program lacks a vertex shader\n");
       } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
-        linker_error_printf(prog, "program lacks a fragment shader\n");
-        prog->LinkStatus = false;
+        linker_error(prog, "program lacks a fragment shader\n");
       }
    }
 
@@ -1716,6 +1825,14 @@ done:
 
       /* Retain any live IR, but trash the rest. */
       reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
+
+      /* The symbol table in the linked shaders may contain references to
+       * variables that were removed (e.g., unused uniforms).  Since it may
+       * contain junk, there is no possible valid use.  Delete it and set the
+       * pointer to NULL.
+       */
+      delete prog->_LinkedShaders[i]->symbols;
+      prog->_LinkedShaders[i]->symbols = NULL;
    }
 
    ralloc_free(mem_ctx);