PR ld/12549
[binutils-gdb.git] / gdb / dwarf2read.c
index 0a3a9772f27d5d1b8efacae326c1df8b5ad970cc..d26e7c8f6f8a7399813ff17b9417af2e1e9e5008 100644 (file)
@@ -3072,6 +3072,7 @@ dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
       for (j = 0; j < file_data->num_file_names; ++j)
        {
          const char *this_name = file_data->file_names[j];
+         const char *this_real_name;
 
          if (compare_filenames_for_search (this_name, name))
            {
@@ -3086,11 +3087,16 @@ dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
              && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
            continue;
 
-         if (real_path != NULL)
+         this_real_name = dw2_get_real_path (objfile, file_data, j);
+         if (compare_filenames_for_search (this_real_name, name))
            {
-             const char *this_real_name = dw2_get_real_path (objfile,
-                                                             file_data, j);
+             if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
+                                       callback, data))
+               return 1;
+           }
 
+         if (real_path != NULL)
+           {
              gdb_assert (IS_ABSOLUTE_PATH (real_path));
              gdb_assert (IS_ABSOLUTE_PATH (name));
              if (this_real_name != NULL
@@ -3473,7 +3479,7 @@ dw2_map_matching_symbols (const char * name, domain_enum namespace,
 static void
 dw2_expand_symtabs_matching
   (struct objfile *objfile,
-   int (*file_matcher) (const char *, void *),
+   int (*file_matcher) (const char *, void *, int basenames),
    int (*name_matcher) (const char *, void *),
    enum search_domain kind,
    void *data)
@@ -3533,7 +3539,23 @@ dw2_expand_symtabs_matching
 
          for (j = 0; j < file_data->num_file_names; ++j)
            {
-             if (file_matcher (file_data->file_names[j], data))
+             const char *this_real_name;
+
+             if (file_matcher (file_data->file_names[j], data, 0))
+               {
+                 per_cu->v.quick->mark = 1;
+                 break;
+               }
+
+             /* Before we invoke realpath, which can get expensive when many
+                files are involved, do a quick comparison of the basenames.  */
+             if (!basenames_may_differ
+                 && !file_matcher (lbasename (file_data->file_names[j]),
+                                   data, 1))
+               continue;
+
+             this_real_name = dw2_get_real_path (objfile, file_data, j);
+             if (file_matcher (this_real_name, data, 0))
                {
                  per_cu->v.quick->mark = 1;
                  break;
@@ -4054,6 +4076,12 @@ dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
 {
   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
 
+  if (!IS_ABSOLUTE_PATH (subpst->filename))
+    {
+      /* It shares objfile->objfile_obstack.  */
+      subpst->dirname = pst->dirname;
+    }
+
   subpst->section_offsets = pst->section_offsets;
   subpst->textlow = 0;
   subpst->texthigh = 0;
@@ -18006,12 +18034,12 @@ dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
 \f
 /* Macro support.  */
 
-/* Return the full name of file number I in *LH's file name table.
-   Use COMP_DIR as the name of the current directory of the
-   compilation.  The result is allocated using xmalloc; the caller is
+/* Return file name relative to the compilation directory of file number I in
+   *LH's file name table.  The result is allocated using xmalloc; the caller is
    responsible for freeing it.  */
+
 static char *
-file_full_name (int file, struct line_header *lh, const char *comp_dir)
+file_file_name (int file, struct line_header *lh)
 {
   /* Is the file number a valid index into the line header's file name
      table?  Remember that file numbers start with one, not zero.  */
@@ -18019,31 +18047,10 @@ file_full_name (int file, struct line_header *lh, const char *comp_dir)
     {
       struct file_entry *fe = &lh->file_names[file - 1];
 
-      if (IS_ABSOLUTE_PATH (fe->name))
+      if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
         return xstrdup (fe->name);
-      else
-        {
-          const char *dir;
-          int dir_len;
-          char *full_name;
-
-          if (fe->dir_index)
-            dir = lh->include_dirs[fe->dir_index - 1];
-          else
-            dir = comp_dir;
-
-          if (dir)
-            {
-              dir_len = strlen (dir);
-              full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
-              strcpy (full_name, dir);
-              full_name[dir_len] = '/';
-              strcpy (full_name + dir_len + 1, fe->name);
-              return full_name;
-            }
-          else
-            return xstrdup (fe->name);
-        }
+      return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
+                    fe->name, NULL);
     }
   else
     {
@@ -18063,6 +18070,27 @@ file_full_name (int file, struct line_header *lh, const char *comp_dir)
     }
 }
 
+/* Return the full name of file number I in *LH's file name table.
+   Use COMP_DIR as the name of the current directory of the
+   compilation.  The result is allocated using xmalloc; the caller is
+   responsible for freeing it.  */
+static char *
+file_full_name (int file, struct line_header *lh, const char *comp_dir)
+{
+  /* Is the file number a valid index into the line header's file name
+     table?  Remember that file numbers start with one, not zero.  */
+  if (1 <= file && file <= lh->num_file_names)
+    {
+      char *relative = file_file_name (file, lh);
+
+      if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
+       return relative;
+      return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
+    }
+  else
+    return file_file_name (file, lh);
+}
+
 
 static struct macro_source_file *
 macro_start_file (int file, int line,
@@ -18070,26 +18098,27 @@ macro_start_file (int file, int line,
                   const char *comp_dir,
                   struct line_header *lh, struct objfile *objfile)
 {
-  /* The full name of this source file.  */
-  char *full_name = file_full_name (file, lh, comp_dir);
+  /* File name relative to the compilation directory of this source file.  */
+  char *file_name = file_file_name (file, lh);
 
   /* We don't create a macro table for this compilation unit
      at all until we actually get a filename.  */
   if (! pending_macros)
     pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
-                                      objfile->per_bfd->macro_cache);
+                                     objfile->per_bfd->macro_cache,
+                                     comp_dir);
 
   if (! current_file)
     {
       /* If we have no current file, then this must be the start_file
         directive for the compilation unit's main source file.  */
-      current_file = macro_set_main (pending_macros, full_name);
+      current_file = macro_set_main (pending_macros, file_name);
       macro_define_special (pending_macros);
     }
   else
-    current_file = macro_include (current_file, line, full_name);
+    current_file = macro_include (current_file, line, file_name);
 
-  xfree (full_name);
+  xfree (file_name);
 
   return current_file;
 }