Subject: [PATCH] Debug support for global variable alias

When clang emits the dwarf information of global variable alias
as DW_TAG_imported_declaration, gdb does not handle it. GDB reads
this tag as C++/fortran imported declaration. Added support to
handle this tag as variable alias.

This change fixes the failure in testcase:
           gdb.base/symbol-alias.exp
This testcase is also updated to test nested (recursive) alias,
static aliasee and deferred aliasee.

---
 gdb/dwarf2/read.c                       | 113 ++++++++++++++++--------
 gdb/testsuite/gdb.base/symbol-alias.exp |  23 ++++-
 gdb/testsuite/gdb.base/symbol-alias2.c  |  10 +++
 3 files changed, 107 insertions(+), 39 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 10550336063..0f709d9ccc1 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1132,6 +1132,10 @@ static void add_partial_subprogram (struct partial_die_info *pdi,
 				    CORE_ADDR *lowpc, CORE_ADDR *highpc,
 				    int need_pc, struct dwarf2_cu *cu);
 
+static void var_decode_location (struct attribute *attr,
+                                 struct symbol *sym,
+                                 struct dwarf2_cu *cu);
+
 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
 
 static struct partial_die_info *load_partial_dies
@@ -1332,7 +1336,7 @@ static struct using_direct **using_directives (struct dwarf2_cu *cu);
 
 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
 
-static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
+static int read_alias (struct die_info *die, struct dwarf2_cu *cu);
 
 static struct type *read_module_type (struct die_info *die,
 				      struct dwarf2_cu *cu);
@@ -9742,9 +9746,11 @@ process_die (struct die_info *die, struct dwarf2_cu *cu)
       break;
     case DW_TAG_imported_declaration:
       cu->processing_has_namespace_info = true;
-      if (read_namespace_alias (die, cu))
+      if (((cu->per_cu->lang == language_c)
+            || (cu->per_cu->lang == language_cplus))
+	                     && read_alias (die, cu))
 	break;
-      /* The declaration is not a global namespace alias.  */
+      /* The declaration is not a global namespace or variable alias. */
       /* Fall through.  */
     case DW_TAG_imported_module:
       cu->processing_has_namespace_info = true;
@@ -10212,18 +10218,20 @@ dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
   return retval;
 }
 
-/* Inspect DIE in CU for a namespace alias.  If one exists, record
-   a new symbol for it.
+#define MAX_NESTED_IMPORTED_DECLARATIONS 100
+
+/* Inspect DIE in CU for a namespace alias or a variable
+   with alias attribute. If one exists, record a new symbol
+   for it.
 
-   Returns 1 if a namespace alias was recorded, 0 otherwise.  */
+   Returns 1 if an alias was recorded, 0 otherwise. */
 
 static int
-read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
+read_alias (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct attribute *attr;
 
-  /* If the die does not have a name, this is not a namespace
-     alias.  */
+  /* The die must have a name for namespace or variable alias */
   attr = dwarf2_attr (die, DW_AT_name, cu);
   if (attr != NULL)
     {
@@ -10232,42 +10240,71 @@ read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
       struct dwarf2_cu *imported_cu = cu;
 
       /* If the compiler has nested DW_AT_imported_declaration DIEs,
-	 keep inspecting DIEs until we hit the underlying import.  */
-#define MAX_NESTED_IMPORTED_DECLARATIONS 100
+         keep inspecting DIEs until we hit the underlying import. */
       for (num = 0; num  < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
-	{
-	  attr = dwarf2_attr (d, DW_AT_import, cu);
-	  if (attr == NULL)
-	    break;
+        {
+          attr = dwarf2_attr (d, DW_AT_import, cu);
+          if (attr == NULL)
+            return 0;
 
-	  d = follow_die_ref (d, attr, &imported_cu);
-	  if (d->tag != DW_TAG_imported_declaration)
-	    break;
-	}
+          d = follow_die_ref (d, attr, &imported_cu);
+          if (d->tag != DW_TAG_imported_declaration)
+            break;
+        }
 
       if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
-	{
-	  complaint (_("DIE at %s has too many recursively imported "
-		       "declarations"), sect_offset_str (d->sect_off));
-	  return 0;
-	}
+        {
+          complaint (_("DIE at %s has too many recursively imported "
+                       "declarations"), sect_offset_str (d->sect_off));
+          return 0;
+        }
 
       if (attr != NULL)
-	{
-	  struct type *type;
-	  sect_offset sect_off = attr->get_ref_die_offset ();
-
-	  type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
-	  if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
-	    {
-	      /* This declaration is a global namespace alias.  Add
-		 a symbol for it whose type is the aliased namespace.  */
-	      new_symbol (die, type, cu);
-	      return 1;
-	    }
-	}
+        {
+          struct type *type;
+          if (d->tag == DW_TAG_variable)
+            {
+	      /* This declaration is a global variable alias. Add
+	         a symbol for it whose type is same as aliased variable. */
+              type = die_type (d, imported_cu);
+              struct symbol *sym = new_symbol(die, type, cu);
+              attr = dwarf2_attr (d, DW_AT_location, imported_cu);
+              if (attr != nullptr)
+                {
+                  var_decode_location (attr, sym, cu);
+                  return 1;
+                }
+              else
+                {
+                  /* We do not know the address of this symbol.
+                     If it is an external symbol and we have type information
+                     for it, enter the symbol as a LOC_UNRESOLVED symbol.
+                     The address of the variable will then be determined from
+                     the minimal symbol table whenever the variable is
+                     referenced.  */
+                  attr = dwarf2_attr (d, DW_AT_external, imported_cu);
+                  if (attr != nullptr && attr->as_boolean ()
+                      && dwarf2_attr (d, DW_AT_type, imported_cu) != NULL)
+                    {
+                      sym->set_aclass_index (LOC_UNRESOLVED);
+                      return 1;
+                    }
+                }
+            }
+          else
+            {
+              sect_offset sect_off = attr->get_ref_die_offset ();
+              type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
+              if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
+                {
+                  /* This declaration is a global namespace alias. Add
+                     a symbol for it whose type is the aliased namespace. */
+                  new_symbol (die, type, cu);
+                  return 1;
+                }
+            }
+        }
     }
-
   return 0;
 }
 
diff --git a/gdb/testsuite/gdb.base/symbol-alias.exp b/gdb/testsuite/gdb.base/symbol-alias.exp
index 2b53cc31053..1259c1daf1d 100644
--- a/gdb/testsuite/gdb.base/symbol-alias.exp
+++ b/gdb/testsuite/gdb.base/symbol-alias.exp
@@ -15,6 +15,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 standard_testfile symbol-alias.c symbol-alias2.c
+set using_clang [test_compiler_info clang*]
 
 if { [prepare_for_testing "failed to prepare" ${testfile} [list $srcfile $srcfile2]] } {
     return -1
@@ -31,6 +32,26 @@ foreach f {"func" "func_alias"} {
 }
 
 # Variables.
-foreach v {"g_var_s" "g_var_s_alias"} {
+foreach v {"g_var_s" "g_var_s_alias" "g_var_s_alias2"} {
     gdb_test "p $v" "= {field1 = 1, field2 = 2}"
 }
+
+# Static Variable.
+foreach v {"g_var" "g_var_alias"} {
+    gdb_test "p $v" " = 1"
+}
+
+# Deferred Variable.
+gdb_test "p g_def_var" " = 2"
+gdb_test_multiple "p g_def_var_alias" "p g_def_var_alias" {
+    -re  " = 2.*$gdb_prompt $" {
+        pass "print alias of deferred variable"
+    }
+    -re  ".*has unknown type; cast it to its declared type.*$gdb_prompt $" {
+        if { $using_clang } {
+          xfail "print alias of deferred variable"
+        } else {
+          fail "print alias of deferred variable"
+        }
+    }
+}
diff --git a/gdb/testsuite/gdb.base/symbol-alias2.c b/gdb/testsuite/gdb.base/symbol-alias2.c
index 34f4e121e25..79b3195caf5 100644
--- a/gdb/testsuite/gdb.base/symbol-alias2.c
+++ b/gdb/testsuite/gdb.base/symbol-alias2.c
@@ -23,6 +23,8 @@ struct S
 
 struct S g_var_s = { 1, 2 };
 
+static int g_var = 1;
+
 #ifdef __cplusplus
 /* So that the alias attribute below work without having to figure out
    this function's mangled name.  */
@@ -38,3 +40,11 @@ func (void)
 struct S *func_alias (void) __attribute__ ((alias ("func")));
 
 extern struct S g_var_s_alias __attribute__ ((alias ("g_var_s")));
+
+extern struct S g_var_s_alias2 __attribute__ ((alias ("g_var_s_alias")));
+
+extern int g_var_alias __attribute__ ((alias ("g_var")));
+
+extern int g_def_var_alias __attribute__ ((alias ("g_def_var")));
+
+int g_def_var = 2;
-- 
2.25.1

