Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Mihails Strasuns <mihails.strasuns@intel.com>,
	gdb-patches@sourceware.org
Subject: Re: [PATCH 7/7] [gdb/testsuite] add jit-elf-util.h and run jit function
Date: Sun, 22 Mar 2020 23:13:24 -0400	[thread overview]
Message-ID: <32519038-95ed-d46e-28cd-0670e0798a02@simark.ca> (raw)
In-Reply-To: <20200218124339.11270-8-mihails.strasuns@intel.com>

On 2020-02-18 7:43 a.m., Mihails Strasuns wrote:
> +/* Looks into string tables for entry
> +   "jit_function_XXXX" and updates it to use `rename_num`.  */
> +static void
> +update_name (ElfW (Addr) addr, int rename_num)
> +{
> +  const ElfW (Ehdr) *const ehdr = (ElfW (Ehdr) *) addr;
> +  ElfW (Shdr) *const shdr = (ElfW (Shdr) *) ((char *) addr + ehdr->e_shoff);
> +  ElfW (Phdr) *const phdr = (ElfW (Phdr) *) ((char *) addr + ehdr->e_phoff);
> +
> +  for (int i = 0; i < ehdr->e_shnum; ++i)
> +    {
> +      if (shdr[i].sh_type == SHT_STRTAB)
> +	{
> +	  /* Note: we update both .strtab and .dynstr.  The latter would
> +	     not be correct if this were a regular shared library (.hash
> +	     would be wrong), but this is a simulation -- the library is
> +	     never exposed to the dynamic loader, so it all ends up ok.  */
> +	  char *const strtab = (char *) (addr + shdr[i].sh_offset);
> +	  char *const strtab_end = strtab + shdr[i].sh_size;
> +	  char *p;
> +
> +	  for (p = strtab; p < strtab_end; p += strlen (p) + 1)
> +	    if (strcmp (p, "jit_function_XXXX") == 0)
> +	      sprintf (p, "jit_function_%04d", rename_num);
> +	}
> +    }
> +}

I was wondering about this function.  It updates the function name in the ELF string
table, but not in the DWARF debug info, so what is GDB going to display if the debug
info says that the function is called `jit_function_XXXX`?

In fact, since we generate multiple shared objects, could we just generate them with
the right function names directly?  The .exp would compile the first one with

  -DJIT_FUNCTION_NAME=jit_function_0001

the second one with

  -DJIT_FUNCTION_NAME=jit_function_0002

and so forth.  We wouldn't need any of this name munging.

> +
> +/* Find symbol with the name `sym_name`.  */
> +static ElfW (Addr)
> +load_symbol (ElfW (Addr) addr, const char *sym_name)
> +{
> +  const ElfW (Ehdr) *const ehdr = (ElfW (Ehdr) *) addr;
> +  ElfW (Shdr) *const shdr = (ElfW (Shdr) *) ((char *) addr + ehdr->e_shoff);
> +
> +  ElfW (Addr) sym_old_addr = 0;
> +  ElfW (Addr) sym_new_addr = 0;
> +
> +  /* Find `func_name` in symbol_table and adjust it from the addr.  */
> +
> +  for (int i = 0; i < ehdr->e_shnum; ++i)
> +    {
> +      if (shdr[i].sh_type == SHT_SYMTAB)
> +	{
> +	  ElfW (Sym) *symtab = (ElfW (Sym) *) (addr + shdr[i].sh_offset);
> +	  ElfW (Sym) *symtab_end
> +	      = (ElfW (Sym) *) (addr + shdr[i].sh_offset + shdr[i].sh_size);
> +	  char *const strtab
> +	      = (char *) (addr + shdr[shdr[i].sh_link].sh_offset);
> +
> +	  for (ElfW (Sym) *p = symtab; p < symtab_end; ++p)
> +	    {
> +	      const char *s = strtab + p->st_name;
> +	      if (strcmp (s, sym_name) == 0)
> +	        return p->st_value;
> +	    }
> +	}
> +    }
> +
> +  fprintf (stderr, "symbol '%s' not found\n", sym_name);
> +  exit (1);
> +  return 0;
> +}
> +
> +/* Open an elf binary file and memory map it with execution flag enabled.  */
> +ElfW (Addr)
> +load_elf (const char *libname, size_t *size, ElfW (Addr) load_addr)

I may not matter here, but since this is in a header, please make this function static.

> +{
> +  int fd;
> +  struct stat st;
> +
> +  if ((fd = open (libname, O_RDONLY)) == -1)
> +    {
> +      fprintf (stderr, "open (\"%s\", O_RDONLY): %s\n", libname,
> +	       strerror (errno));
> +      exit (1);
> +    }
> +
> +  if (fstat (fd, &st) != 0)
> +    {
> +      fprintf (stderr, "fstat (\"%d\"): %s\n", fd, strerror (errno));
> +      exit (1);
> +    }
> +
> +  void *addr = mmap ((void *) load_addr, st.st_size,
> +		     PROT_READ | PROT_WRITE | PROT_EXEC,
> +		     load_addr ? MAP_PRIVATE | MAP_FIXED : MAP_PRIVATE, fd, 0);

I see that you've used MAP_FIXED here, which means you changed the code in addition to
moving the function.  This makes it difficult to review, since I can't really look at
the diff for this function.

When moving code around, we try to avoid changing it at the same time (other than the
necessary adjustments).  Either make those changes part of the previous patch (it seems
to me like they are related enough), or make a separate patch for modifying the code vs
moving it.

Simon


  reply	other threads:[~2020-03-23  3:13 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-18 12:42 [PATCH 0/7] refactor and enhance jit testing Mihails Strasuns
2020-02-18 12:42 ` [PATCH 2/7] [gdb/testsuite] structured rename of jit test files Mihails Strasuns
2020-02-19 21:23   ` Tom Tromey
2020-03-22  2:47     ` Simon Marchi
2020-02-18 12:42 ` [PATCH 7/7] [gdb/testsuite] add jit-elf-util.h and run jit function Mihails Strasuns
2020-03-23  3:13   ` Simon Marchi [this message]
2020-03-23  9:23     ` Strasuns, Mihails
2020-03-23 11:14       ` Simon Marchi
2020-02-18 12:42 ` [PATCH 6/7] [gdb/testsuite] use -Ttext-segment for jit-elf tests Mihails Strasuns
2020-03-23  3:03   ` Simon Marchi
2020-02-18 12:42 ` [PATCH 4/7] [gdb/testsuite] use args as lib list " Mihails Strasuns
2020-03-23  0:04   ` Simon Marchi
2020-03-23  0:35     ` Simon Marchi
2020-02-18 12:42 ` [PATCH 1/7] [gdb/testsuite] allow more registers in reader test Mihails Strasuns
2020-02-19 21:22   ` Tom Tromey
2020-03-21 16:03   ` Simon Marchi
2020-03-22  2:09     ` Simon Marchi
2020-02-18 12:42 ` [PATCH 3/7] [gdb/testsuite] share jit-protocol.h by all jit tests Mihails Strasuns
2020-02-19 21:23   ` Tom Tromey
2020-03-22 16:00   ` Simon Marchi
2020-02-18 12:42 ` [PATCH 5/7] [gdb/testsuite] add lib/jit-elf-helpers.exp Mihails Strasuns
2020-03-23  0:52   ` Simon Marchi
2020-02-26 13:56 ` [PATCH 0/7] refactor and enhance jit testing Strasuns, Mihails
2020-03-18 12:48   ` Simon Marchi

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=32519038-95ed-d46e-28cd-0670e0798a02@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=mihails.strasuns@intel.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