public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Crestez Dan Leonard <cdleonard@gmail.com>
To: elfutils-devel@lists.fedorahosted.org
Subject: MIPS and -msym32
Date: Tue, 22 Jul 2014 17:44:48 +0300	[thread overview]
Message-ID: <53CE78E0.1050905@gmail.com> (raw)

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

Hello,

I've been playing around with systemtap on mips.

I used the elfutils mips patch from debian:
	http://sources.debian.net/src/elfutils/0.159-4/debian/patches/mips_backend.diff

For systemtap I ported some old patches from cisco on top of release 2.5. Here is the link to the patches on top of 1.6:
	https://sourceware.org/git/gitweb.cgi?p=systemtap.git;a=log;h=refs/heads/systemtap-1.6-cisco-patches

One of the issues I encountered is that by default 64-bit mips kernels are compiled with -msym32. This causes gcc to emit dwarf information with a pointer size of 4, even though the file is 64bit. In theory this can be disabled by passing KBUILD_SYM32=no to the kernel make commandline. This is a bad idea. It would disable some optimizations which have been enabled on mips for many years. I actually tried to do it locally but it broke module loading and I was unable to boot.

The generated dwarf files confuse systemtap is multiple ways. 

When fetching some parameters systemtap relies on address_size from dwarf_diecu to determine max_fetch_size. This caused an error like "semantic error: single register too big for fetch/store ???: identifier '$data' at ..." for an unsigned long variable.

Here is a hack I used to get around this:
--- a/libdw/dwarf_diecu.c
+++ b/libdw/dwarf_diecu.c
@@ -47,7 +47,22 @@ dwarf_diecu (die, result, address_sizep, offset_sizep)
   *result = CUDIE (die->cu);
 
   if (address_sizep != NULL)
+  {
     *address_sizep = die->cu->address_size;
+    /* Hack: */
+    if (1)
+    {
+      struct Elf *elf = die->cu->dbg->elf;
+      GElf_Ehdr ehdr_mem;
+      GElf_Ehdr* ehdr = gelf_getehdr (elf, &ehdr_mem);
+      if (ehdr &&
+              ehdr->e_machine == EM_MIPS &&
+              ehdr->e_ident[EI_CLASS] == ELFCLASS64)
+      {
+        *address_sizep = 8;
+      }
+    }
+  }
   if (offset_sizep != NULL)
     *offset_sizep = die->cu->offset_size;

This is obviously evil. What is worse is that -msym32 seems to break systemtap address mapping. Earlier I had to do the following hack inside systemtap.

diff --git a/tapsets.cxx b/tapsets.cxx
index 4e42403..94ee230 100644
--- a/tapsets.cxx
+++ b/tapsets.cxx
@@ -1265,6 +1265,22 @@ dwarf_query::add_probe_point(const string& dw_funcname,
       else
         {
           assert (has_kernel || has_module);
+          if (1)
+            {
+              Dwarf_Addr bias;
+              Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias))
+                 ?: dwfl_module_getelf (dw.module, &bias));
+              GElf_Ehdr ehdr_mem;
+              GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
+              int elf_machine = em->e_machine;
+              if (elf_machine == EM_MIPS)
+                {
+                  if (sess.verbose > 1)
+                    clog << "Hack reloc=" << hex << reloc_addr
+                      << " from addr=" << hex << addr;
+                  reloc_addr = addr - 0xc0000400;
+                }
+            }
           results.push_back (new dwarf_derived_probe(funcname, filename, line,
                                                      module, reloc_section, addr, reloc_addr,
                                                      *this, scope_die));

At the time I did the hack I was unaware of -msym32, I just wanted something to work. If I compile with KBUILD_SYM32=no it seems that the above hack might no longer be necessary (the addresses "look" right). I can't actually test the generated probes because my system won't boot with KBUILD_SYM32=no.

Apparently the gcc folks decided that this -msym32 behavior was too confusing and changed it to generate dwarf with a pointer size of 8:
https://gcc.gnu.org/ml/gcc/2009-01/msg00611.html

In the future this might not matter, but my cross-compiler is based on gcc-4.3.3. I'm posting this here because maybe somebody has a better idea about how to deal with this strange flavor of dwarf.

If this could be detected and handled inside systemtap fully then maybe you could apply the patches. Elfutils seems mostly blameless to me, it's just reading the information that GCC wrote.

Regards,
Leonard

             reply	other threads:[~2014-07-22 14:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-22 14:44 Crestez Dan Leonard [this message]
2014-08-01 10:59 Mark Wielaard
2014-08-01 12:21 Crestez Dan Leonard
2014-08-15 14:38 Mark Wielaard
2014-08-15 15:24 Kurt Roeckx
2014-08-15 16:42 Kurt Roeckx

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=53CE78E0.1050905@gmail.com \
    --to=cdleonard@gmail.com \
    --cc=elfutils-devel@lists.fedorahosted.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).