From: Can Acar <canacar@imcan.dev>
To: tom@tromey.com
Cc: gdb-patches@sourceware.org, "canacar@imcan.dev" <canacar@imcan.dev>
Subject: [PATCH v2] Fix: Sign extensions for DW_FORM_addrx were never considered
Date: Mon, 27 Oct 2025 18:03:47 -0700 [thread overview]
Message-ID: <20251028010346.20187-2-canacar@imcan.dev> (raw)
In-Reply-To: <CAKB9nm+y0_iRYFNuna4hN5YrW35JjjGchs76+mTwz=VXb38AMQ@mail.gmail.com>
From: "canacar@imcan.dev" <canacar@imcan.dev>
DW_FORM_addr is converted (sign-extended) to a signed value when
the dwarf size is less than the size of unrelocated_addr, if the
target architecture "naturally" sign extends an address (bfd.c).
However, the same handling was not done for DW_FORM_addrx. This
meant that for example, trying to `list` a function with an
address >= 0x80000000 on (some?) 32-bit mips targets, when
that address was encoded using DW_FORM_addrx, was broken.
This patch fixes this issue by plumbing read_addr_index_1 into
unit_head::read_address, which is the function used to extract
information from DW_FORM_addr, and so it handles this case
correctly.
---
Changes since v1:
* read_addr_index_1 has been removed, merging its functionality into
read_addr_index.
* dwarf2_read_addr_index has been modified to change the reference to the cu
in the newly-created cu case to something that seems safer.
* fix a formatting issue in a call to read_address.
gdb/dwarf2/read.c | 44 ++++++++++++++++----------------------------
1 file changed, 16 insertions(+), 28 deletions(-)
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 431b7c9ea2c..cca42722471 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -15352,14 +15352,20 @@ dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
ADDR_SIZE is the size of addresses from the CU header. */
+/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
+
static unrelocated_addr
-read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
- std::optional<ULONGEST> addr_base, int addr_size)
+read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
{
+ const gdb_byte *info_ptr;
+ struct dwarf2_per_objfile *per_objfile = cu->per_objfile;
+ std::optional<ULONGEST> addr_base = cu->addr_base;
+ struct unit_head *cu_header = &cu->header;
struct objfile *objfile = per_objfile->objfile;
bfd *abfd = objfile->obfd.get ();
- const gdb_byte *info_ptr;
ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
+ unsigned int ignore_bytes_read;
+ unsigned char addr_size = cu_header->addr_size;
per_objfile->per_bfd->addr.read (objfile);
if (per_objfile->per_bfd->addr.buffer == NULL)
@@ -15372,20 +15378,9 @@ read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
objfile_name (objfile));
info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
+ addr_index * addr_size);
- if (addr_size == 4)
- return (unrelocated_addr) bfd_get_32 (abfd, info_ptr);
- else
- return (unrelocated_addr) bfd_get_64 (abfd, info_ptr);
+ return cu_header->read_address (abfd, info_ptr, &ignore_bytes_read);
}
-/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
-
-static unrelocated_addr
-read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
-{
- return read_addr_index_1 (cu->per_objfile, addr_index,
- cu->addr_base, cu->header.addr_size);
-}
/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
@@ -15405,11 +15400,10 @@ unrelocated_addr
dwarf2_read_addr_index (dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile,
unsigned int addr_index)
{
+ std::optional<cutu_reader> reader;
struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
- std::optional<ULONGEST> addr_base;
- int addr_size;
- /* We need addr_base and addr_size.
+ /* read_addr_index requires some fields from cu.
If we don't have PER_CU->cu, we have to get it.
Nasty, but the alternative is storing the needed info in PER_CU,
which at this point doesn't seem justified: it's not clear how frequently
@@ -15425,20 +15419,14 @@ dwarf2_read_addr_index (dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile,
IWBN to use the aging mechanism to let us lazily later discard the CU.
For now we skip this optimization. */
- if (cu != NULL)
+ if (cu == NULL)
{
- addr_base = cu->addr_base;
- addr_size = cu->header.addr_size;
- }
- else
- {
- cutu_reader reader (*per_cu, *per_objfile, nullptr, nullptr, false,
+ reader.emplace (*per_cu, *per_objfile, nullptr, nullptr, false,
language_minimal);
- addr_base = reader.cu ()->addr_base;
- addr_size = reader.cu ()->header.addr_size;
+ cu = reader->cu ();
}
- return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
+ return read_addr_index (cu, addr_index);
}
/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
--
2.50.1 (Apple Git-155)
prev parent reply other threads:[~2025-10-28 1:07 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-23 21:42 [PATCH] " Can Acar
2025-10-23 21:44 ` Can Acar
2025-10-24 13:43 ` Tom Tromey
2025-10-28 1:02 ` Can Acar
2025-10-28 1:03 ` Can Acar [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251028010346.20187-2-canacar@imcan.dev \
--to=canacar@imcan.dev \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox