diff --git a/gdb/rs6000-aix-tdep.c b/gdb/rs6000-aix-tdep.c
index d4a7b22bc4f5..0567481d96ce 100644
--- a/gdb/rs6000-aix-tdep.c
+++ b/gdb/rs6000-aix-tdep.c
@@ -1222,44 +1222,41 @@ rs6000_aix_extract_ld_info (struct gdbarch *gdbarch,
   return info;
 }
 
-/* Append to OBJSTACK an XML string description of the shared library
+/* Append to XML an XML string description of the shared library
    corresponding to LDI, following the TARGET_OBJECT_LIBRARIES_AIX
    format.  */
 
 static void
-rs6000_aix_shared_library_to_xml (struct ld_info *ldi,
-				  struct obstack *obstack)
+rs6000_aix_shared_library_to_xml (struct ld_info *ldi, std::string &xml)
 {
-  obstack_grow_str (obstack, "<library name=\"");
-  std::string p = xml_escape_text (ldi->filename);
-  obstack_grow_str (obstack, p.c_str ());
-  obstack_grow_str (obstack, "\"");
+  xml += "<library name=\"";
+  xml_escape_text_append (xml, ldi->filename);
+  xml += '"';
 
   if (ldi->member_name[0] != '\0')
     {
-      obstack_grow_str (obstack, " member=\"");
-      p = xml_escape_text (ldi->member_name);
-      obstack_grow_str (obstack, p.c_str ());
-      obstack_grow_str (obstack, "\"");
+      xml += " member=\"";
+      xml_escape_text_append (xml, ldi->member_name);
+      xml += '"';
     }
 
-  obstack_grow_str (obstack, " text_addr=\"");
-  obstack_grow_str (obstack, core_addr_to_string (ldi->textorg));
-  obstack_grow_str (obstack, "\"");
+  xml += " text_addr=\"";
+  xml += core_addr_to_string (ldi->textorg);
+  xml += '"';
 
-  obstack_grow_str (obstack, " text_size=\"");
-  obstack_grow_str (obstack, pulongest (ldi->textsize));
-  obstack_grow_str (obstack, "\"");
+  xml += " text_size=\"";
+  xml += pulongest (ldi->textsize);
+  xml += '"';
 
-  obstack_grow_str (obstack, " data_addr=\"");
-  obstack_grow_str (obstack, core_addr_to_string (ldi->dataorg));
-  obstack_grow_str (obstack, "\"");
+  xml += " data_addr=\"";
+  xml += core_addr_to_string (ldi->dataorg);
+  xml += '"';
 
-  obstack_grow_str (obstack, " data_size=\"");
-  obstack_grow_str (obstack, pulongest (ldi->datasize));
-  obstack_grow_str (obstack, "\"");
+  xml += " data_size=\"";
+  xml += pulongest (ldi->datasize);
+  xml += '"';
 
-  obstack_grow_str (obstack, "></library>");
+  xml += "></library>";
 }
 
 /* Convert the ld_info binary data provided by the AIX loader into
@@ -1282,18 +1279,13 @@ rs6000_aix_ld_info_to_xml (struct gdbarch *gdbarch, const gdb_byte *ldi_buf,
 			   gdb_byte *readbuf, ULONGEST offset, ULONGEST len,
 			   int close_ldinfo_fd)
 {
-  struct obstack obstack;
-  const char *buf;
-  ULONGEST len_avail;
-
-  obstack_init (&obstack);
-  obstack_grow_str (&obstack, "<library-list-aix version=\"1.0\">\n");
+  std::string xml = "<library-list-aix version=\"1.0\">\n";
 
   while (1)
     {
       struct ld_info ldi = rs6000_aix_extract_ld_info (gdbarch, ldi_buf);
 
-      rs6000_aix_shared_library_to_xml (&ldi, &obstack);
+      rs6000_aix_shared_library_to_xml (&ldi, xml);
       if (close_ldinfo_fd)
 	close (ldi.fd);
 
@@ -1302,20 +1294,18 @@ rs6000_aix_ld_info_to_xml (struct gdbarch *gdbarch, const gdb_byte *ldi_buf,
       ldi_buf = ldi_buf + ldi.next;
     }
 
-  obstack_grow_str0 (&obstack, "</library-list-aix>\n");
+  xml += "</library-list-aix>\n";
 
-  buf = (const char *) obstack_finish (&obstack);
-  len_avail = strlen (buf);
+  ULONGEST len_avail = xml.length ();
   if (offset >= len_avail)
     len= 0;
   else
     {
       if (len > len_avail - offset)
 	len = len_avail - offset;
-      memcpy (readbuf, buf + offset, len);
+      memcpy (readbuf, xml.data () + offset, len);
     }
 
-  obstack_free (&obstack, NULL);
   return len;
 }
 
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index c09e459a8019..e8a04896e054 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2916,30 +2916,25 @@ windows_xfer_shared_libraries (struct target_ops *ops,
 			       ULONGEST offset, ULONGEST len,
 			       ULONGEST *xfered_len)
 {
-  auto_obstack obstack;
-  const char *buf;
-  LONGEST len_avail;
-
   if (writebuf)
     return TARGET_XFER_E_IO;
 
-  obstack_grow_str (&obstack, "<library-list>\n");
+  std::string xml = "<library-list>\n";
   for (windows_solib &so : windows_process.solibs)
     windows_xfer_shared_library (so.name.c_str (),
 				 (CORE_ADDR) (uintptr_t) so.load_addr,
 				 &so.text_offset,
-				 target_gdbarch (), &obstack);
-  obstack_grow_str0 (&obstack, "</library-list>\n");
+				 target_gdbarch (), xml);
+  xml += "</library-list>\n";
 
-  buf = (const char *) obstack_finish (&obstack);
-  len_avail = strlen (buf);
+  ULONGEST len_avail = xml.size ();
   if (offset >= len_avail)
-    len= 0;
+    len = 0;
   else
     {
       if (len > len_avail - offset)
 	len = len_avail - offset;
-      memcpy (readbuf, buf + offset, len);
+      memcpy (readbuf, xml.data () + offset, len);
     }
 
   *xfered_len = (ULONGEST) len;
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index bc1927049d81..bf9748bdb879 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -527,14 +527,13 @@ display_tib (const char * args, int from_tty)
 void
 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
 			     CORE_ADDR *text_offset_cached,
-			     struct gdbarch *gdbarch, struct obstack *obstack)
+			     struct gdbarch *gdbarch, std::string &xml)
 {
   CORE_ADDR text_offset = text_offset_cached ? *text_offset_cached : 0;
 
-  obstack_grow_str (obstack, "<library name=\"");
-  std::string p = xml_escape_text (so_name);
-  obstack_grow_str (obstack, p.c_str ());
-  obstack_grow_str (obstack, "\"><segment address=\"");
+  xml += "<library name=\"";
+  xml_escape_text_append (xml, so_name);
+  xml += "\"><segment address=\"";
 
   if (!text_offset)
     {
@@ -547,8 +546,8 @@ windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
 	*text_offset_cached = text_offset;
     }
 
-  obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
-  obstack_grow_str (obstack, "\"/></library>");
+  xml += paddress (gdbarch, load_addr + text_offset);
+  xml += "\"/></library>";
 }
 
 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
@@ -1089,7 +1088,7 @@ range [%s, %s]."),
 struct cpms_data
 {
   struct gdbarch *gdbarch;
-  struct obstack *obstack;
+  std::string xml;
   int module_count;
 };
 
@@ -1147,37 +1146,30 @@ core_process_module_section (bfd *abfd, asection *sect, void *obj)
   /* The first module is the .exe itself.  */
   if (data->module_count != 0)
     windows_xfer_shared_library (module_name, base_addr,
-				 NULL, data->gdbarch, data->obstack);
+				 NULL, data->gdbarch, data->xml);
   data->module_count++;
 }
 
 ULONGEST
 windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
-				  gdb_byte *readbuf,
-				  ULONGEST offset, ULONGEST len)
+				    gdb_byte *readbuf,
+				    ULONGEST offset, ULONGEST len)
 {
-  struct obstack obstack;
-  const char *buf;
-  ULONGEST len_avail;
-  struct cpms_data data = { gdbarch, &obstack, 0 };
-
-  obstack_init (&obstack);
-  obstack_grow_str (&obstack, "<library-list>\n");
+  cpms_data data { gdbarch, "<library-list>\n", 0 };
   bfd_map_over_sections (core_bfd,
 			 core_process_module_section,
 			 &data);
-  obstack_grow_str0 (&obstack, "</library-list>\n");
+  data.xml += "</library-list>\n";
 
-  buf = (const char *) obstack_finish (&obstack);
-  len_avail = strlen (buf);
+  ULONGEST len_avail = data.xml.length ();
   if (offset >= len_avail)
     return 0;
 
   if (len > len_avail - offset)
     len = len_avail - offset;
-  memcpy (readbuf, buf + offset, len);
 
-  obstack_free (&obstack, NULL);
+  memcpy (readbuf, data.xml.data () + offset, len);
+
   return len;
 }
 
diff --git a/gdb/windows-tdep.h b/gdb/windows-tdep.h
index be4a363f8ccb..740f72ef27ee 100644
--- a/gdb/windows-tdep.h
+++ b/gdb/windows-tdep.h
@@ -18,7 +18,6 @@
 #ifndef WINDOWS_TDEP_H
 #define WINDOWS_TDEP_H
 
-struct obstack;
 struct gdbarch;
 
 extern struct cmd_list_element *info_w32_cmdlist;
@@ -29,7 +28,7 @@ extern void windows_xfer_shared_library (const char* so_name,
 					 CORE_ADDR load_addr,
 					 CORE_ADDR *text_offset_cached,
 					 struct gdbarch *gdbarch,
-					 struct obstack *obstack);
+					 std::string &xml);
 
 extern ULONGEST windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
 						    gdb_byte *readbuf,
