tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: gcc: optimizations, and stack traces



On Fri, Feb 09, 2018 at 11:38:47 +0100, Martin Husemann wrote:

> On Fri, Feb 09, 2018 at 11:23:17AM +0100, Maxime Villard wrote:
>
> > When I spotted this several months ago (while developing Live
> > Kernel ASLR), I tried to look for GCC options that say "optimize
> > with -O2, but keep the stack trace intact". I couldn't find one,
> > and the only thing I ended up doing was disabling -O2 in the
> > makefiles.
> 
> -fno-omit-frame-pointer?

That won't help.

     `-O' also turns on `-fomit-frame-pointer' on machines where doing
     so does not interfere with debugging.

so it's not turned off in the first place.  The problem is that some
of the later optimization passes may push frame pointer setup to some
place later in function.  E.g. on -7 

    void
    kernfs_get_rrootdev(void)
    {
            static int tried = 0;

            if (tried) {
                    /* Already did it once. */
                    return;
            }
            tried = 1;

            if (rootdev == NODEV)
                    return;
            rrootdev = devsw_blk2chr(rootdev);
            if (rrootdev != NODEV)
                    return;
            rrootdev = NODEV;
            printf("kernfs_get_rrootdev: no raw root device\n");
    }

is compiled to 

    c068f81b <kernfs_get_rrootdev>:
    c068f81b:       mov    0xc0fc6b40,%eax
    c068f820:       test   %eax,%eax
    c068f822:       jne    c068f867 <kernfs_get_rrootdev+0x4c>
    c068f824:       movl   $0x1,0xc0fc6b40
    c068f82e:       mov    0xc0fde0b8,%edx
    c068f834:       mov    0xc0fde0bc,%eax
    c068f839:       mov    %edx,%ecx
    c068f83b:       and    %eax,%ecx
    c068f83d:       cmp    $0xffffffff,%ecx
    c068f840:       je     c068f867 <kernfs_get_rrootdev+0x4c>
->  c068f842:       push   %ebp
->  c068f843:       mov    %esp,%ebp
    c068f845:       sub    $0x8,%esp
    c068f848:       mov    %edx,(%esp)
    c068f84b:       mov    %eax,0x4(%esp)
    c068f84f:       call   c091ce52 <devsw_blk2chr>

So the "tried" check and the first "rootdev" check happen before the
frame pointer is set up.

-uwe


Home | Main Index | Thread Index | Old Index