Merge cherrypicks of [1275206, 1275207, 1275375, 1275579, 1275580, 1275581, 1275582, 1275583, 1275584, 1275585, 1275586, 1275587, 1275588, 1275589, 1275398, 1275399, 1275223, 1275540, 1275541, 1275542, 1275435] into ndk-release-r21

Change-Id: I90cdb62fca000fad913ce13717107b0eeb859878
diff --git a/gdb-8.3/gdb/psymtab.c b/gdb-8.3/gdb/psymtab.c
index 0a4502e..682696a 100644
--- a/gdb-8.3/gdb/psymtab.c
+++ b/gdb-8.3/gdb/psymtab.c
@@ -312,14 +312,24 @@
 		      struct obj_section *section,
 		      struct bound_minimal_symbol msymbol)
 {
-  CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
-				 SECT_OFF_TEXT (objfile));
+  /* Try just the PSYMTABS_ADDRMAP mapping first as it has better
+     granularity than the later used TEXTLOW/TEXTHIGH one.  However, we need
+     to take care as the PSYMTABS_ADDRMAP can hold things other than partial
+     symtabs in some cases.
 
-  /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
-     than the later used TEXTLOW/TEXTHIGH one.  */
+     This function should only be called for objfiles that are using partial
+     symtabs, not for objfiles that are using indexes (.gdb_index or
+     .debug_names), however 'maintenance print psymbols' calls this function
+     directly for all objfiles.  If we assume that PSYMTABS_ADDRMAP contains
+     partial symtabs then we will end up returning a pointer to an object
+     that is not a partial_symtab, which doesn't end well.  */
 
-  if (objfile->partial_symtabs->psymtabs_addrmap != NULL)
+  if (objfile->partial_symtabs->psymtabs != NULL
+      && objfile->partial_symtabs->psymtabs_addrmap != NULL)
     {
+      CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
+				     SECT_OFF_TEXT (objfile));
+
       struct partial_symtab *pst
 	= ((struct partial_symtab *)
 	   addrmap_find (objfile->partial_symtabs->psymtabs_addrmap,
diff --git a/gdb-8.3/gdb/psymtab.h b/gdb-8.3/gdb/psymtab.h
index 3ee5eee..24be1d5 100644
--- a/gdb-8.3/gdb/psymtab.h
+++ b/gdb-8.3/gdb/psymtab.h
@@ -112,7 +112,11 @@
   /* Map addresses to the entries of PSYMTABS.  It would be more efficient to
      have a map per the whole process but ADDRMAP cannot selectively remove
      its items during FREE_OBJFILE.  This mapping is already present even for
-     PARTIAL_SYMTABs which still have no corresponding full SYMTABs read.  */
+     PARTIAL_SYMTABs which still have no corresponding full SYMTABs read.
+
+     The DWARF parser reuses this addrmap to store things other than
+     psymtabs in the cases where debug information is being read from, for
+     example, the .debug-names section.  */
 
   struct addrmap *psymtabs_addrmap = nullptr;