--- ./gdb/xcoffread.c_orig	2018-08-25 16:31:31 +0000
+++ ./gdb/xcoffread.c	2018-08-25 16:37:49 +0000
@@ -52,6 +52,9 @@
 /* For interface with stabsread.c.  */
 #include "aout/stab_gnu.h"
 
+/*Previously dependent pst.  */
+struct partial_symtab *pst_dependent_prev;
+
 
 /* Key for XCOFF-associated data.  */
 
@@ -1853,6 +1856,14 @@
 
   /* Read in all partial symtabs on which this one is dependent.  */
   for (i = 0; i < pst->number_of_dependencies; i++)
+	  {
+    /* If the types defined in the dependent pst have never been
+       saved before, read the pst again.  */
+    if (pst->dependencies[i]->readin)
+    {
+     if (pst->dependencies[i] != pst_dependent_prev) /* && !all_type_vector)*/
+      pst->dependencies[i]->readin = 0;
+    }
     if (!pst->dependencies[i]->readin)
       {
 	/* Inform about additional files that need to be read in.  */
@@ -1866,7 +1877,18 @@
 	    wrap_here ("");	/* Flush output */
 	    gdb_flush (gdb_stdout);
 	  }
-	xcoff_psymtab_to_symtab_1 (objfile, pst->dependencies[i]);
+	/* Save the types defined in the pst as it may be referenced in
+            the other partial symtabs.  */
+
+          save_type_vector = 1;
+          pst_dependent_prev = pst->dependencies[i];  
+	  xcoff_psymtab_to_symtab_1 (objfile, pst->dependencies[i]);
+          
+          /* Reinitialize save_type_vector back to 0 to avoid saving of
+            unnecessary types declared in following psymtabs.  */
+
+          save_type_vector = 0;
+        }
       }
 
   if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
@@ -2214,6 +2236,9 @@
 
   /* Current partial symtab */
   struct partial_symtab *pst;
+  
+  /* Pst of any file which contains type declarations.  */
+  struct partial_symtab *pst_file;
 
   /* List of current psymtab's include files.  */
   const char **psymtab_include_list;
@@ -2236,6 +2261,7 @@
   int textlow_not_set = 1;
 
   pst = (struct partial_symtab *) 0;
+  pst_file = (struct partial_symtab *) 0;
 
   includes_allocated = 30;
   includes_used = 0;
@@ -2336,8 +2362,18 @@
 			       symnum_before,
 			       objfile->global_psymbols,
 			       objfile->static_psymbols);
+                            /* Mark pst dependent unless it is compiler generated. */
+                             if (strncmp (namestring, "@FIX", 4) != 0)
+                             {
+                               if (!strcmp(pst->filename, pst_file->filename))
+                               {
+                                 dependency_list[dependencies_used] = pst_file;
+                                 dependencies_used ++;
+                               }
+                             }
 			  }
 		      }
+
 		    /* Activate the misc_func_recorded mechanism for
 		       compiler- and linker-generated CSECTs like ".strcmp"
 		       and "@FIX1".  */ 
@@ -2520,6 +2556,10 @@
 				       objfile->global_psymbols,
 				       objfile->static_psymbols);
 	    last_csect_name = NULL;
+		/* Initialize pst_file to NULL as we have encountered a new
+             file declaration.  */
+
+           pst_file = (struct partial_symtab *) 0;
 	  }
 	  break;
 
@@ -2640,6 +2680,19 @@
 	      }
 	    continue;
 	  }
+
+	  case C_DECL:
+         /* We have encountered a new type declaration. The types
+            defined in this pst may be referenced by other psymtabs
+            in the same file itself. (This occurs when we use xlc
+            -qfuncsect or gcc --ffunction-section options.
+            So, if this is not marked as pst_file before or if pst_file
+            has not been defined for this particular file, then mark
+           this pst as pst_file.  */
+
+          if (!pst_file)
+           pst_file = pst;
+
 	case C_FUN:
 	  /* The value of the C_FUN is not the address of the function (it
 	     appears to be the address before linking), but as long as it
@@ -2648,7 +2701,6 @@
 
 	case C_GSYM:
 	case C_ECOML:
-	case C_DECL:
 	case C_STSYM:
 	  {
 	    const char *p;
--- ./gdb/buildsym.h_orig	2018-08-25 16:42:15 +0000
+++ ./gdb/buildsym.h	2018-08-25 16:30:08 +0000
@@ -193,6 +193,26 @@
 
 #define next_symbol_text(objfile) (*next_symbol_text_func)(objfile)
 
+/* Vector of all the types which were defined in the pst of a file
+   which had the type declarations. This is useful when the debugee
+   is compiled with the xlc -qfuncsect or gcc --function-sections
+   options. These linker options split every function in a file into
+   different PSTs. SO type declarations happen in one PST while
+   references to it happen in another pst. */
+
+EXTERN struct type **all_type_vector;
+
+/* This is set when ever we need to save contents of type_vector
+   in all_type_vector.  */
+
+EXTERN int save_type_vector;
+
+EXTERN int all_type_index[50000];
+
+EXTERN int all_type_count;
+
+
+
 /* Function to invoke get the next symbol.  Return the symbol name.  */
 
 EXTERN const char *(*next_symbol_text_func) (struct objfile *);
--- ./gdb/stabsread.c_orig	2018-08-25 16:38:16 +0000
+++ ./gdb/stabsread.c	2018-08-25 16:40:29 +0000
@@ -52,6 +52,9 @@
 #include "stabsread.h"		/* Our own declarations */
 #undef	EXTERN
 
+/* Check if all the types of the particluar pst must be saved. */
+int check_all_types;
+
 struct nextfield
 {
   struct nextfield *next;
@@ -76,6 +79,7 @@
    This is part of some reorganization of low level C++ support and is
    expected to eventually go away...  (FIXME) */
 
+
 struct field_info
   {
     struct nextfield *list;
@@ -280,6 +284,11 @@
 	    {
 	      type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
 	      type_vector = XNEWVEC (struct type *, type_vector_length);
+		  if (save_type_vector) {
+                all_type_vector = (struct type **)
+                  xmalloc (type_vector_length * sizeof (struct type *));
+              }
+
 	    }
 	  while (index >= type_vector_length)
 	    {
@@ -290,8 +299,27 @@
 		      (type_vector_length * sizeof (struct type *)));
 	  memset (&type_vector[old_len], 0,
 		  (type_vector_length - old_len) * sizeof (struct type *));
-	}
-      return (&type_vector[index]);
+	
+        if (save_type_vector)
+        {
+          all_type_vector = (struct type **)
+                xrealloc ((char *) all_type_vector,
+                          (type_vector_length * sizeof (struct type *)));
+          memset (&all_type_vector[old_len], 0,
+                  (type_vector_length - old_len) * sizeof (struct type *));
+         }
+      }
+
+     /* If the type has been declared in another pst which was
+       stored in all_type_vector, then assign the type to that
+       stored value. The check_all_types variable is set by
+       the cleanup_undefined_types_noname().  */
+
+     if (check_all_types && all_type_vector[index])
+     {
+        type_vector[index] = all_type_vector[index];
+     }
+     return (&type_vector[index]);
     }
   else
     {
@@ -2050,6 +2078,25 @@
       if (typenums[0] != -1)
 	*dbx_lookup_type (typenums, objfile) = type;
       break;
+	  
+	case 'm':
+      /* type = dbx_alloc_type (typenums, objfile);
+      all_type_vector[typenums[1]] = type; */
+
+      all_type_index[all_type_count] = typenums[1];
+      all_type_count += 1;
+      break;
+
+    case 'M':
+     all_type_index[all_type_count] = typenums[1];
+     all_type_count += 1;
+     break;
+
+    case 'V':
+      type = read_type (pp, objfile);
+      type = make_cv_type (TYPE_CONST (type), 1, type,
+                           dbx_lookup_type (typenums, objfile));
+      break;
 
     default:
       --*pp;			/* Go back to the symbol in error.  */
@@ -2059,7 +2106,7 @@
 
   if (type == 0)
     {
-      warning (_("GDB internal error, type is NULL in stabsread.c."));
+      /* warning (_("GDB internal error, type is NULL in stabsread.c.")); */
       return error_type (pp, objfile);
     }
 
@@ -2067,6 +2114,14 @@
   if (type_size != -1)
     TYPE_LENGTH (type) = (type_size + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
 
+  /* Save all the types into all_type_vector if the current pst
+     has C_DECL variable declarations and is a dependent pst to
+     another pst.  */
+
+  if (save_type_vector && (typenums[1] > 0))
+   all_type_vector[typenums[1]] = type_vector[typenums[1]];
+
+
   return type;
 }
 
@@ -4487,7 +4542,7 @@
 static void
 cleanup_undefined_types_noname (struct objfile *objfile)
 {
-  int i;
+  int i, j, is_index = 0;;
 
   for (i = 0; i < noname_undefs_length; i++)
     {
@@ -4495,6 +4550,43 @@
       struct type **type;
 
       type = dbx_lookup_type (nat.typenums, objfile);
+
+      /* If a type is undefined, it may be because of the use of xlc
+      -qfuncsect or gcc --function-sections option which causes types
+      to be defined in one pst while references to it may happen in
+      another pst.
+
+      So, if we find an undefined type, check it against the
+      all_type_vector and assign it to the previously defined type.  */
+
+      if (nat.type == *type && TYPE_CODE (*type) == TYPE_CODE_UNDEF)
+      {
+       /* if (all_type_vector)
+          check_all_types = 1;
+        type = dbx_lookup_type(nat.typenums, objfile);
+        check_all_types = 0;
+       */
+       
+         if (all_type_vector) { 
+            for (j = 0; j < all_type_count; j++) {
+              if (nat.typenums[1] == all_type_index[j])
+                {
+                    is_index =1 ;
+                    break;               
+                }
+              }
+            if( is_index != 1)
+            {
+               check_all_types = 1;
+               type = dbx_lookup_type(nat.typenums, objfile);
+               check_all_types = 0;
+            }    
+         }
+       }
+
+
+   if (is_index !=1)
+   {
       if (nat.type != *type && TYPE_CODE (*type) != TYPE_CODE_UNDEF)
         {
           /* The instance flags of the undefined type are still unset,
@@ -4505,6 +4597,7 @@
           replace_type (nat.type, *type);
         }
     }
+	}
 
   noname_undefs_length = 0;
 }
