public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 2/2] Use file offset instead of virtual address for a function address
@ 2022-03-21 12:19 Balasubrmanian, Vignesh
  2022-03-21 13:49 ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Balasubrmanian, Vignesh @ 2022-03-21 12:19 UTC (permalink / raw)
  To: gdb-patches; +Cc: George, Jini Susan, Kumar N, Bhuvanendra

[-- Attachment #1: Type: text/plain, Size: 875 bytes --]


Follow-up patch to fix test case "jit-elf.exp" when compiled with LLD (clang).

For shared libraries, the "st_value" of the symbol from symtab holds the virtual address for the symbol's definition.
To make a call to a function using a function pointer, the address in "st_value" works as long as the library is loaded through "dlopen()".
As we use "mmap()" to map the entire file to the specific address in the memory, we need to find the offset at which the symbol's definition is placed in the file (".so").
So we use "st_value" and "st_shndx" to find the offset of the function from its section start and then use it to find the file offset for the function.
With gcc, the test points are not failing because the offset at which function is placed in the mapped ".so" and virtual address at which it is supposed to be loaded are the same.

Thanks,
vigneshbalu

[-- Attachment #2: 0002-Use-file-offset-instead-of-virtual-address-for-a-fun.patch --]
[-- Type: application/octet-stream, Size: 2079 bytes --]

From 7d1363b8b44b0974b24241a37be2f73b8ae7d996 Mon Sep 17 00:00:00 2001
From: Vignesh Balasubramanian <Vignesh.Balasubrmanian@amd.com>
Date: Thu, 17 Mar 2022 14:57:50 +0530
Subject: [PATCH 2/2] Use file offset instead of virtual address for a function
 address

For shared libraries, the "st_value" of the symbol from symtab
holds the virtual address for the symbol's definition.
To make a call to a function using a function pointer, the address
in "st_value" works as long as the library is loaded through
"dlopen()".
As we use "mmap()" to map the entire file to the specific address
in the memory, we need to find the offset at which the symbol's
definition is placed in the file (".so").
So we use "st_value" and "st_shndx" to find the offset of the
function from its section start and then use it to find the
file offset for the function.
With gcc, the test points are not failing because the offset at
which function is placed in the mapped ".so" and virtual address
at which it supposed to be loaded are the same.
---
 gdb/testsuite/gdb.base/jit-elf-util.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/gdb/testsuite/gdb.base/jit-elf-util.h b/gdb/testsuite/gdb.base/jit-elf-util.h
index 419a781f865..3e768e04686 100644
--- a/gdb/testsuite/gdb.base/jit-elf-util.h
+++ b/gdb/testsuite/gdb.base/jit-elf-util.h
@@ -69,8 +69,18 @@ load_symbol (void *addr, const char *sym_name)
 	  for (p = symtab; p < symtab_end; ++p)
 	    {
 	      const char *s = strtab + p->st_name;
-	      if (strcmp (s, sym_name) == 0)
-	        return (void *) p->st_value;
+	      if (strcmp (s, sym_name) == 0) {
+	        ElfW (Addr) off_frm_section
+			 = p->st_value - shdr[p->st_shndx].sh_addr;
+	/*
+	 As we map the entire file to the specific address in the memory,
+	 we need to find the offset at which symbol's definition is placed
+	 in the file.
+	 file start addr + section offset + symbol_offset_from_Section_start
+	*/
+	        return (void *) (addr
+			 + shdr[p->st_shndx].sh_offset + off_frm_section);
+	      }
 	    }
 	}
     }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] Use file offset instead of virtual address for a function address
  2022-03-21 12:19 [PATCH 2/2] Use file offset instead of virtual address for a function address Balasubrmanian, Vignesh
@ 2022-03-21 13:49 ` Simon Marchi
  2022-03-22 16:22   ` Balasubrmanian, Vignesh
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2022-03-21 13:49 UTC (permalink / raw)
  To: Balasubrmanian, Vignesh, gdb-patches
  Cc: George, Jini Susan, Kumar N, Bhuvanendra

On 2022-03-21 08:19, Balasubrmanian, Vignesh via Gdb-patches wrote:
>
> Follow-up patch to fix test case "jit-elf.exp" when compiled with LLD (clang).
>
> For shared libraries, the "st_value" of the symbol from symtab holds the virtual address for the symbol's definition.
> To make a call to a function using a function pointer, the address in "st_value" works as long as the library is loaded through "dlopen()".
> As we use "mmap()" to map the entire file to the specific address in the memory, we need to find the offset at which the symbol's definition is placed in the file (".so").
> So we use "st_value" and "st_shndx" to find the offset of the function from its section start and then use it to find the file offset for the function.
> With gcc, the test points are not failing because the offset at which function is placed in the mapped ".so" and virtual address at which it is supposed to be loaded are the same.
>
> Thanks,
> vigneshbalu

As with the previous patch, please mention the command line you use to
reproduce this, to allow others to try to reproduce it too.

It would be useful to show the relevant differences in readelf output
between the test file when linked with ld and when linked with lld.
That would help understand where the difference lies.

Simon

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH 2/2] Use file offset instead of virtual address for a function address
  2022-03-21 13:49 ` Simon Marchi
@ 2022-03-22 16:22   ` Balasubrmanian, Vignesh
  0 siblings, 0 replies; 3+ messages in thread
From: Balasubrmanian, Vignesh @ 2022-03-22 16:22 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: George, Jini Susan, Kumar N, Bhuvanendra

[-- Attachment #1: Type: text/plain, Size: 2851 bytes --]

Simon,

Added the readelf output and make check command to the commit message.

For jit-elf-solib.1.so
(readelf -s):
gcc:  000000000700111a    11 FUNC    GLOBAL DEFAULT    9 jit_function_0001
clang:00000000070015d0    11 FUNC    GLOBAL DEFAULT   10 jit_function_0001

(readelf -l):
Type  Offset             VirtAddr           PhysAddr
      FileSiz            MemSiz              Flags  Align
GCC:
LOAD  0x0000000000001000 0x0000000007001000 0x0000000007001000
      0x0000000000000131 0x0000000000000131  R E    0x1000
Offset and VirtAddr are same, so 0x700111a on both file and memory
are at same location

CLANG:
LOAD  0x0000000000000510 0x0000000007001510 0x0000000007001510
      0x0000000000000110 0x0000000000000110  R E    0x1000
Here 0x70015d0 in file at 0x70005d0 and in memory at 0x70015d0.

make check command:
make check RUNTESTFLAGS="--all -v -v -v GDB='${GDB_INSTALL_DIR}/bin/gdb'
CFLAGS_FOR_TARGET='-w -gdwarf-4' CXXFLAGS_FOR_TARGET='-w -gdwarf-4'
CPPFLAGS_FOR_TARGET='-w -gdwarf-4' CC_FOR_TARGET='clang'
CXX_FOR_TARGET='clang'" TESTS="gdb.base/jit-elf.exp"

Thanks,
Vigneshbalu

-----Original Message-----
From: Simon Marchi <simark@simark.ca> 
Sent: Monday, March 21, 2022 7:19 PM
To: Balasubrmanian, Vignesh <Vignesh.Balasubrmanian@amd.com>; gdb-patches@sourceware.org
Cc: George, Jini Susan <JiniSusan.George@amd.com>; Kumar N, Bhuvanendra <Bhuvanendra.KumarN@amd.com>
Subject: Re: [PATCH 2/2] Use file offset instead of virtual address for a function address

[CAUTION: External Email]

On 2022-03-21 08:19, Balasubrmanian, Vignesh via Gdb-patches wrote:
>
> Follow-up patch to fix test case "jit-elf.exp" when compiled with LLD (clang).
>
> For shared libraries, the "st_value" of the symbol from symtab holds the virtual address for the symbol's definition.
> To make a call to a function using a function pointer, the address in "st_value" works as long as the library is loaded through "dlopen()".
> As we use "mmap()" to map the entire file to the specific address in the memory, we need to find the offset at which the symbol's definition is placed in the file (".so").
> So we use "st_value" and "st_shndx" to find the offset of the function from its section start and then use it to find the file offset for the function.
> With gcc, the test points are not failing because the offset at which function is placed in the mapped ".so" and virtual address at which it is supposed to be loaded are the same.
>
> Thanks,
> vigneshbalu

As with the previous patch, please mention the command line you use to reproduce this, to allow others to try to reproduce it too.

It would be useful to show the relevant differences in readelf output between the test file when linked with ld and when linked with lld.
That would help understand where the difference lies.

Simon

[-- Attachment #2: 0002-Use-file-offset-instead-of-virtual-address-for-a-fun.patch --]
[-- Type: application/octet-stream, Size: 3070 bytes --]

From 884a8f335399f36dc0aac256bef1874ea489cdd2 Mon Sep 17 00:00:00 2001
From: Vignesh Balasubramanian <Vignesh.Balasubrmanian@amd.com>
Date: Tue, 22 Mar 2022 18:11:53 +0530
Subject: [PATCH 2/2] Use file offset instead of virtual address for a function
 address

For shared libraries, the "st_value" of the symbol from symtab
holds the virtual address for the symbol's definition.
To make a call to a function using a function pointer, the address
in "st_value" works as long as the library is loaded through
"dlopen()".
As we use "mmap()" to map the entire file to the specific address
in the memory, we need to find the offset at which the symbol's
definition is placed in the file (".so").
So we use "st_value" and "st_shndx" to find the offset of the
function from its section start and then use it to find the
file offset for the function.
With gcc, the test points are not failing because the offset at
which function is placed in the mapped ".so" and virtual address
at which it supposed to be loaded are the same.
For jit-elf-solib.1.so
(readelf -s):
gcc:  000000000700111a    11 FUNC    GLOBAL DEFAULT    9 jit_function_0001
clang:00000000070015d0    11 FUNC    GLOBAL DEFAULT   10 jit_function_0001
(readelf -l):
Type  Offset             VirtAddr           PhysAddr
      FileSiz            MemSiz              Flags  Align
GCC:
LOAD  0x0000000000001000 0x0000000007001000 0x0000000007001000
      0x0000000000000131 0x0000000000000131  R E    0x1000
Offset and VirtAddr are same, so 0x700111a on both file and memory
are at same location
CLANG:
LOAD  0x0000000000000510 0x0000000007001510 0x0000000007001510
      0x0000000000000110 0x0000000000000110  R E    0x1000
Here 0x70015d0 in file at 0x70005d0 and in memory at 0x70015d0.
make check command:
make check RUNTESTFLAGS="--all -v -v -v GDB='${GDB_INSTALL_DIR}/bin/gdb'
CFLAGS_FOR_TARGET='-w -gdwarf-4' CXXFLAGS_FOR_TARGET='-w -gdwarf-4'
CPPFLAGS_FOR_TARGET='-w -gdwarf-4' CC_FOR_TARGET='clang'
CXX_FOR_TARGET='clang'" TESTS="gdb.base/jit-elf.exp"
---
 gdb/testsuite/gdb.base/jit-elf-util.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/gdb/testsuite/gdb.base/jit-elf-util.h b/gdb/testsuite/gdb.base/jit-elf-util.h
index 419a781f865..3e768e04686 100644
--- a/gdb/testsuite/gdb.base/jit-elf-util.h
+++ b/gdb/testsuite/gdb.base/jit-elf-util.h
@@ -69,8 +69,18 @@ load_symbol (void *addr, const char *sym_name)
 	  for (p = symtab; p < symtab_end; ++p)
 	    {
 	      const char *s = strtab + p->st_name;
-	      if (strcmp (s, sym_name) == 0)
-	        return (void *) p->st_value;
+	      if (strcmp (s, sym_name) == 0) {
+	        ElfW (Addr) off_frm_section
+			 = p->st_value - shdr[p->st_shndx].sh_addr;
+	/*
+	 As we map the entire file to the specific address in the memory,
+	 we need to find the offset at which symbol's definition is placed
+	 in the file.
+	 file start addr + section offset + symbol_offset_from_Section_start
+	*/
+	        return (void *) (addr
+			 + shdr[p->st_shndx].sh_offset + off_frm_section);
+	      }
 	    }
 	}
     }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-22 16:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-21 12:19 [PATCH 2/2] Use file offset instead of virtual address for a function address Balasubrmanian, Vignesh
2022-03-21 13:49 ` Simon Marchi
2022-03-22 16:22   ` Balasubrmanian, Vignesh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).