Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 16/27] gdb/coffread: Don't relocate absolute symbols
Date: Thu, 23 Jul 2026 14:01:07 +0100	[thread overview]
Message-ID: <20260723130118.206735-17-pedro@palves.net> (raw)
In-Reply-To: <20260723130118.206735-1-pedro@palves.net>

A COFF absolute symbol (i.e., one with COFF section number N_ABS) has
a value that is a plain constant, not an offset into any section.
Such a symbol must not be adjusted when the objfile it belongs to is
relocated, e.g. when a DLL is loaded at an address other than its
preferred image base.

gdb/coffread.c records absolute symbols as mst_abs, which is correct,
but it derives the symbol's section from cs_to_section, which in turn
asks cs_to_bfd_section for the BFD section.  cs_to_bfd_section matches
by section number and finds nothing for an N_ABS symbol, returning
NULL, so cs_to_section falls back to the text section.  The symbol
then inherits the text section's relocation offset (the DLL's load
bias) and its value is wrongly shifted by that amount.

For a DLL loaded at its preferred base the bias is zero and the bug is
invisible, but when two DLLs are loaded overlapping so that one is
relocated, its absolute symbols come out shifted.  This can be seen in
gdb.base/shreloc.exp, which loads two libraries at the same base and
checks that absolute symbols keep the same value in both:

 (gdb) maint print msymbols ...
 [4] A 0x5bc10000 __minor_os_version__ section .text
 [4] A 0x5bbe0000 __minor_os_version__ section .text

__minor_os_version__ has value 0 in both DLLs, but GDB reports its
value as each symbols DLL's load base.  Note also the contradictory "A
... section .text" -- an absolute symbol should not be tied to a
section at all.

Fix this in cs_to_bfd_section, by returning BFD's absolute section for
an N_ABS symbol rather than NULL.  Its relocation offset is always
zero, so the symbol's value is left untouched, matching what
elf_symtab_read does for absolute ELF symbols.  cs_to_section then
naturally returns the absolute section's index.

While at it, fold the N_ABS case in symtab_read into the general path.

Without this fix, gdb.base/shreloc.exp fails on all Windows-based
targets, and it now passes cleanly:

 -FAIL: gdb.base/shreloc.exp: absolute symbols not relocated
 +PASS: gdb.base/shreloc.exp: absolute symbols not relocated

Tested on:

 x86_64-pc-linux-gnu
 x86_64-pc-cygwin
 x86_64-w64-mingw32
 x86_64-pc-windows-msvc

Change-Id: I91b9882b6918cbd46dfdf1aa18f72cbbace70535
---
 gdb/coffread.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/gdb/coffread.c b/gdb/coffread.c
index 0a5da42e563..9f792edcd26 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -116,6 +116,13 @@ struct coff_reader
 asection *
 coff_reader::cs_to_bfd_section (struct coff_symbol *cs)
 {
+  /* An absolute symbol (N_ABS) is not tied to any of the input file's
+     sections.  Its value is a plain constant, so map it to BFD's
+     absolute section, so that callers treat it as absolute and, in
+     particular, do not relocate it.  */
+  if (cs->c_secnum == N_ABS)
+    return bfd_abs_section_ptr;
+
   for (asection *sect : gdb_bfd_sections (symfile_bfd))
     if (sect->target_index == cs->c_secnum)
       return sect;
@@ -469,14 +476,6 @@ coff_reader::symtab_read (minimal_symbol_reader &reader,
 		   symbol lookup which returned no match.  */
 		break;
 	      }
-	    else if (cs->c_secnum == N_ABS)
-	      {
-		/* Use the correct minimal symbol type (and don't
-		   relocate) for absolute values.  */
-		ms_type = mst_abs;
-		sec = cs_to_section (cs);
-		tmpaddr = cs->c_value;
-	      }
 	    else
 	      {
 		asection *bfd_section = cs_to_bfd_section (cs);
@@ -484,7 +483,13 @@ coff_reader::symtab_read (minimal_symbol_reader &reader,
 		sec = cs_to_section (cs);
 		tmpaddr = cs->c_value;
 
-		if (bfd_section->flags & SEC_CODE)
+		if (bfd_is_abs_section (bfd_section))
+		  {
+		    /* Use the correct minimal symbol type (and don't
+		       relocate) for absolute values.  */
+		    ms_type = mst_abs;
+		  }
+		else if (bfd_section->flags & SEC_CODE)
 		  {
 		    ms_type =
 		      cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
-- 
2.54.0


  parent reply	other threads:[~2026-07-23 13:05 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23 13:00 [PATCH 00/27] Teach the testsuite about the Windows/MSVC target Pedro Alves
2026-07-23 13:00 ` [PATCH 01/27] gdb/testsuite: Don't link with -lm on windows-msvc Pedro Alves
2026-07-23 13:00 ` [PATCH 02/27] gdb/testsuite: Use unprefixed runtest for a native Windows/MSVC build Pedro Alves
2026-07-23 13:00 ` [PATCH 03/27] gdb/testsuite: Find host binutils in a native Windows/MSVC config Pedro Alves
2026-07-23 13:00 ` [PATCH 04/27] gdb/testsuite: Compile with -Wno-deprecated-declarations on windows-msvc Pedro Alves
2026-07-23 13:00 ` [PATCH 05/27] gdb/testsuite: Recognize windows-msvc across lib/gdb.exp procedures Pedro Alves
2026-07-23 13:00 ` [PATCH 06/27] gdb/testsuite: Don't pass -fPIC on PE/COFF targets Pedro Alves
2026-07-23 13:00 ` [PATCH 07/27] gdb/testsuite: Use /IMPLIB on windows-msvc to name import libraries Pedro Alves
2026-07-23 13:00 ` [PATCH 08/27] gdb/testsuite: Export all DLL symbols on windows-msvc via generated .def Pedro Alves
2026-07-23 13:01 ` [PATCH 09/27] gdb/testsuite: Restrict --no-as-needed to ELF targets Pedro Alves
2026-07-23 13:01 ` [PATCH 10/27] gdb.base/set-cwd.exp: Use is_windows_native_target Pedro Alves
2026-07-23 13:01 ` [PATCH 11/27] gdb.base/exitsignal.exp: " Pedro Alves
2026-07-23 13:01 ` [PATCH 12/27] gdb.base/exitsignal.exp: Relax SIGSEGV second-chance pattern Pedro Alves
2026-07-23 13:01 ` [PATCH 13/27] gdb/testsuite: Skip -Ttext-segment on PE Pedro Alves
2026-07-23 13:01 ` [PATCH 14/27] gdb/testsuite: Support text_segment on windows-msvc via /BASE Pedro Alves
2026-07-23 13:01 ` [PATCH 15/27] gdb.base/shreloc.exp: Use gdb_compile text_segment to set image base Pedro Alves
2026-07-23 13:01 ` Pedro Alves [this message]
2026-07-23 13:01 ` [PATCH 17/27] gdb.base/shreloc.exp: Test absolute symbols portably Pedro Alves
2026-07-23 13:01 ` [PATCH 18/27] gdb: %p => host_address_to_string, target-section owner token Pedro Alves
2026-07-23 13:01 ` [PATCH 19/27] gdb: %p => host_address_to_string, dump_for_expression Pedro Alves
2026-07-23 13:01 ` [PATCH 20/27] gdb: %p => host_address_to_string, find_symtab_matching_filename Pedro Alves
2026-07-23 13:01 ` [PATCH 21/27] gdb: %p => host_address_to_string, handle_output_debug_string Pedro Alves
2026-07-23 13:01 ` [PATCH 22/27] gdb.base/maint-info-sections.exp: Match exec file name with optional .exe Pedro Alves
2026-07-23 13:01 ` [PATCH 23/27] gdb.base/maint-info-sections.exp: Remove stale Windows DATA xfail Pedro Alves
2026-07-23 13:01 ` [PATCH 24/27] gdb.base/solib-weak.exp: Skip on all PE/COFF targets Pedro Alves
2026-07-23 13:01 ` [PATCH 25/27] gdb.server/wrapper.exp: Skip on all Windows targets Pedro Alves
2026-07-23 13:01 ` [PATCH 26/27] gdb/testsuite: Factor out dlopen/LoadLibrary shim into lib/gdb-dlfcn.h Pedro Alves
2026-07-23 13:01 ` [PATCH 27/27] gdb/testsuite/lib/gdb-dlfcn.h: __WIN32__ => _WIN32 Pedro Alves

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=20260723130118.206735-17-pedro@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    /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