From 3a77a940752825ff67ba5ccf917447be1427c6c5 Mon Sep 17 00:00:00 2001
From: Aditya Kamath <Aditya.Kamath1@ibm.com>
Date: Mon, 22 Jun 2026 03:51:36 -0500
Subject: [PATCH] Fix crash in AIX when current working directory is NULL

In AIX if we do not have read permission in a current directory we get,

gdb ~/gdb_tests/simple_test
gdb: warning: error finding working directory: A parameter must be a directory.
GNU gdb (GDB) 18.0.50.20260619-git
Copyright (C) 2026 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "powerpc64-ibm-aix7.2.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.

+------------------------------------------------------------------------------+
| Find the GDB manual online at:                                               |
| http://www.gnu.org/software/gdb/documentation/.                              |
| For help, type "help".                                                       |
| Type "apropos word" to search for commands related to "word".                |
+------------------------------------------------------------------------------+

Reading symbols from //gdb_tests/simple_test...
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string: construction from null is not valid

Fatal signal: IOT/Abort trap
----- Backtrace -----
0x100943f1b gdb_internal_backtrace_1
        //home/binutils-gdb/gdb/bt-utils.c:122
0x100944027 _Z22gdb_internal_backtracev
        //home/binutils-gdb/gdb/bt-utils.c:173
0x10060faa7 handle_fatal_signal
        //home/binutils-gdb/gdb/event-top.c:1008
0x4fdf ???
---------------------
A fatal error internal to GDB has been detected, further
debugging is not possible.  GDB will now terminate.

This is a bug, please report it.  For instructions, see:
<https://www.gnu.org/software/gdb/bugs/>.

=============================

The reason for the same above is that in AIX, the variable current_directory in dwarf2/read.c
can be NULL if getcwd () fails during GDB initialisation. When this happens the dwarf2_per_bfd
construction initialisation list captured_cwd to NULL resulting in this segmentation fault shown above.

There are two reasons this happened:
1: The current working directory got deleted in another terminal.
2: Insufficient permission to read the current directory or its parent/predicissor directory.

This patch is a fix to the same.

Afer this fix we get GDB loaded correctly.
gdb ~/gdb_tests/simple_test
gdb: warning: error finding working directory: A parameter must be a directory.
GNU gdb (GDB) 18.0.50.20260619-git
Copyright (C) 2026 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "powerpc64-ibm-aix7.2.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.

+------------------------------------------------------------------------------+
| Find the GDB manual online at:                                               |
| http://www.gnu.org/software/gdb/documentation/.                              |
| For help, type "help".                                                       |
| Type "apropos word" to search for commands related to "word".                |
+------------------------------------------------------------------------------+

Reading symbols from //gdb_tests/simple_test...
(gdb) q
---
 gdb/dwarf2/read.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 84b1480dbcb..7962acc784d 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -859,7 +859,7 @@ dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
 				bool can_copy_)
   : obfd (obfd),
     can_copy (can_copy_),
-    captured_cwd (current_directory),
+    captured_cwd (current_directory == NULL ? "" : current_directory),
     captured_debug_dir (debug_file_directory)
 {
   if (names == NULL)
-- 
2.51.2

