Fix DLL export forwarding

Message ID 20231204165029.408-1-ssbssa@yahoo.de
State New
Headers
Series Fix DLL export forwarding |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Hannes Domani Dec. 4, 2023, 4:50 p.m. UTC
  I noticed it when I was trying to set a breakpoint at ExitProcess:
```
(gdb) b ExitProcess
Breakpoint 1 at 0x14001fdd0
(gdb) r
Starting program: C:\qiewer\heob\heob64.exe
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x3dbf4120
Cannot insert breakpoint 1.
Cannot access memory at address 0x77644120
```

The problem doesn't exist in gdb 13.2, and the difference can easily be
seen when printing ExitProcess.
gdb 14.1:
```
(gdb) p ExitProcess
$1 = {<text variable, no debug info>} 0x77644120 <UserHandleGrantAccess+36128>
```
gdb 13.2:
```
(gdb) p ExitProcess
$1 = {<text variable, no debug info>} 0x77734120 <ntdll!RtlExitUserProcess>
```

The new behavior started with 9675da25357c7a3f472731ddc6eb3becc65b469a,
where VMA was then calculated relative to FORWARD_DLL_NAME, while it was
relative to DLL_NAME before.

Fixed by calculating VMA relative to DLL_NAME again.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31112
---
 gdb/coff-pe-read.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
  

Comments

Lancelot SIX Dec. 4, 2023, 5:34 p.m. UTC | #1
Hi Hannes,

I am not familiar with this part of the code so I'll let someone else
comment on the actual change, but found a minor typo.

Other than this, and FWIW, the change seem reasonable to me.

On Mon, Dec 04, 2023 at 05:50:29PM +0100, Hannes Domani wrote:
> I noticed it when I was trying to set a breakpoint at ExitProcess:
> ```
> (gdb) b ExitProcess
> Breakpoint 1 at 0x14001fdd0
> (gdb) r
> Starting program: C:\qiewer\heob\heob64.exe
> Warning:
> Cannot insert breakpoint 1.
> Cannot access memory at address 0x3dbf4120
> Cannot insert breakpoint 1.
> Cannot access memory at address 0x77644120
> ```
> 
> The problem doesn't exist in gdb 13.2, and the difference can easily be
> seen when printing ExitProcess.
> gdb 14.1:
> ```
> (gdb) p ExitProcess
> $1 = {<text variable, no debug info>} 0x77644120 <UserHandleGrantAccess+36128>
> ```
> gdb 13.2:
> ```
> (gdb) p ExitProcess
> $1 = {<text variable, no debug info>} 0x77734120 <ntdll!RtlExitUserProcess>
> ```
> 
> The new behavior started with 9675da25357c7a3f472731ddc6eb3becc65b469a,
> where VMA was then calculated relative to FORWARD_DLL_NAME, while it was
> relative to DLL_NAME before.
> 
> Fixed by calculating VMA relative to DLL_NAME again.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31112
> ---
>  gdb/coff-pe-read.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/gdb/coff-pe-read.c b/gdb/coff-pe-read.c
> index 56137ac4dd0..c9d6d86a085 100644
> --- a/gdb/coff-pe-read.c
> +++ b/gdb/coff-pe-read.c
> @@ -210,7 +210,10 @@ add_pe_forwarded_sym (minimal_symbol_reader &reader,
>  			      " \"%s\" in dll \"%s\", pointing to \"%s\"\n"),
>  		sym_name, dll_name, forward_qualified_name.c_str ());
>  
> -  unrelocated_addr vma = msymbol.minsym->unrelocated_address ();
> +  /* Calculate VMA as if if where relative to DLL_NAME/OBJFILE, even though
                            ^

s/if if/if it/

> +     it actually points inside another dll (FORWARD_DLL_NAME).  */
> +  unrelocated_addr vma = unrelocated_addr(msymbol.value_address ()
> +					  - objfile->text_section_offset ());
>    msymtype = msymbol.minsym->type ();
>    section = msymbol.minsym->section_index ();
>  
> -- 
> 2.35.1
> 

Best,
Lancelot.
  
Tom Tromey Dec. 6, 2023, 7:31 p.m. UTC | #2
>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> I noticed it when I was trying to set a breakpoint at ExitProcess:
...

Thank you for the patch.

I have a couple of tiny nits, this is ok with those fixed -- you don't
have to re-send it.

You can also apply this to the gdb-14 branch if you want.

Hannes> -  unrelocated_addr vma = msymbol.minsym->unrelocated_address ();
Hannes> +  /* Calculate VMA as if if where relative to DLL_NAME/OBJFILE, even though

s/if where/it were/

Hannes> +     it actually points inside another dll (FORWARD_DLL_NAME).  */
Hannes> +  unrelocated_addr vma = unrelocated_addr(msymbol.value_address ()

Space before the first "(".

Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Hannes Domani Dec. 6, 2023, 8:16 p.m. UTC | #3
Am Mittwoch, 6. Dezember 2023, 20:31:20 MEZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:

> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
>
> Hannes> I noticed it when I was trying to set a breakpoint at ExitProcess:
> ...
>
> Thank you for the patch.
>
> I have a couple of tiny nits, this is ok with those fixed -- you don't
> have to re-send it.
>
> You can also apply this to the gdb-14 branch if you want.
>
> Hannes> -  unrelocated_addr vma = msymbol.minsym->unrelocated_address ();
> Hannes> +  /* Calculate VMA as if if where relative to DLL_NAME/OBJFILE, even though
>
> s/if where/it were/
>
> Hannes> +    it actually points inside another dll (FORWARD_DLL_NAME).  */
> Hannes> +  unrelocated_addr vma = unrelocated_addr(msymbol.value_address ()
>
> Space before the first "(".
>
> Approved-By: Tom Tromey <tom@tromey.com>

Thank you both Lancelot and Tom for noticing my typos.
They are fixed and it's pushed to both master and gdb-14-branch.
  

Patch

diff --git a/gdb/coff-pe-read.c b/gdb/coff-pe-read.c
index 56137ac4dd0..c9d6d86a085 100644
--- a/gdb/coff-pe-read.c
+++ b/gdb/coff-pe-read.c
@@ -210,7 +210,10 @@  add_pe_forwarded_sym (minimal_symbol_reader &reader,
 			      " \"%s\" in dll \"%s\", pointing to \"%s\"\n"),
 		sym_name, dll_name, forward_qualified_name.c_str ());
 
-  unrelocated_addr vma = msymbol.minsym->unrelocated_address ();
+  /* Calculate VMA as if if where relative to DLL_NAME/OBJFILE, even though
+     it actually points inside another dll (FORWARD_DLL_NAME).  */
+  unrelocated_addr vma = unrelocated_addr(msymbol.value_address ()
+					  - objfile->text_section_offset ());
   msymtype = msymbol.minsym->type ();
   section = msymbol.minsym->section_index ();