]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Always fetch Ada "main" name from the executable
authorTom Tromey <tromey@adacore.com>
Wed, 29 Jul 2026 18:40:03 +0000 (12:40 -0600)
committerTom Tromey <tromey@adacore.com>
Mon, 3 Aug 2026 13:24:05 +0000 (07:24 -0600)
The gdb.ada/file-then-restart.exp test was failing with gnat-llvm.  I
tracked this down to the "main" name not being stored in a readonly
section, meaning that the code in ada_main_name using trust_readonly
did not work.

However, it seems to me that gdb should always prefer the data from
the executable in this particular case.  So, rather than relying on
trust_readonly, this patch changes gdb to do this directly.

Approved-By: Pedro Alves <pedro@palves.net>
gdb/ada-lang.c

index 3c6c9af488f8c9e41f92ae08b202a99f53a3329e..906c5cd34658ea5d8cfdb801d3df7bec6d89feb7 100644 (file)
@@ -775,8 +775,6 @@ ada_get_decoded_type (struct type *type)
 const char *
 ada_main_name ()
 {
-  static gdb::unique_xmalloc_ptr<char> main_program_name;
-
   /* For Ada, the name of the main procedure is stored in a specific
      string constant, generated by the binder.  Look for that symbol,
      extract its address, and then read that string.  If we didn't find
@@ -786,21 +784,31 @@ ada_main_name ()
     = lookup_minimal_symbol (current_program_space,
                             ADA_MAIN_PROGRAM_SYMBOL_NAME);
 
-  if (msym.minsym != NULL)
+  if (msym.minsym != nullptr)
     {
+      static gdb_byte main_program_name[1024];
+
       CORE_ADDR main_program_name_addr = msym.value_address ();
       if (main_program_name_addr == 0)
        error (_("Invalid address for Ada main program name."));
 
-      /* Force trust_readonly, because we always want to fetch this
-        string from the executable, not from inferior memory.  If the
-        user changes the exec-file and invokes "start", we want to
-        pick the "main" from the new executable, not one that may
-        come from the still-live inferior.  */
-      scoped_restore save_trust_readonly
-       = make_scoped_restore (&trust_readonly, true);
-      main_program_name = target_read_string (main_program_name_addr, 1024);
-      return main_program_name.get ();
+      /* We always want to fetch this string from the executable, not
+        from inferior memory.  If the user changes the exec-file and
+        invokes "start", we want to pick the "main" from the new
+        executable, not one that may come from the still-live
+        inferior.  */
+      ULONGEST xferred = 0;
+      const auto &sections = current_program_space->target_sections ();
+      if ((section_table_xfer_memory_partial (main_program_name, nullptr,
+                                             main_program_name_addr,
+                                             sizeof (main_program_name),
+                                             &xferred,
+                                             sections)
+          == TARGET_XFER_OK)
+         && xferred > 0
+         && (strnlen ((char *) main_program_name, sizeof (main_program_name))
+             < sizeof (main_program_name)))
+       return (char *) main_program_name;
     }
 
   /* The main procedure doesn't seem to be in Ada.  */