loader: Fix call to prctl(2); thou shalt not use functions as macro parameters.

When building the loader in -O0, the call site to basename() was
placed inside the syscall arguments preparation sequence.  As a
consequence, all previously prepared argument registers were
scratched.

Thanks-to: Christophe MONAT <christophe.monat@st.com> for the hint.
ptrace: Fix deduplication of WIFEXITED/WIFSIGNALED events

The Linux kernel reports the terminating event (WIFEXITED/WIFSIGNALED)
issued by a process to both its parent and its tracer, except when
they are the same.  In this case the Linux kernel reports the
terminating event only once to the tracing parent.

Previously, PRoot tried to honor this behavior by reporting only the
first terminating event (toward the tracer) and by hiding the second
one (toward the parent).  Sadly, this strategy leads to zombie
processes since the child status is not collected by its parent from a
kernel point-of-view.

For instance, when using GDB against a simple program:

    $ proot gdb ./a.out
    [...]
    (gdb) break main
    [...]
    (gdb) run
    [...]
    Breakpoint 1, 0x000000000040062c in main ()

Then looking at the process hierarchy:

- before:

    $ pstree -pa 8786
    gdb,8786 ./a.out
      |-a.out,8788
      `-(a.out,8791)

- now (it is just like without PRoot):

    $ pstree -pa 8786
    gdb,8786 ./a.out
      `-a.out,8791

It works correcly because PRoot now hides the first terminating event
(toward the tracer) and make the second one (toward the parent) appear
by restarting the wait syscall if needed.

Ref.: https://github.com/cedric-vincent/PRoot/issues/67
ptrace: Make terminating event deduplication new mechanism more generic.

The previous commit broke this:

    while ./src/proot ./tests/rootfs/bin/test-ptrace00; do true; done
Fix ptrace emulation for multi-threaded programs.

Reported-by: Christian milkylainen@gmail.com
Ref.: https://groups.google.com/forum/?fromgroups#!topic/proot_me/jZclZLc959Q
Ref.: https://github.com/cedric-vincent/PRoot/issues/69
Fix support to execute 32-bit programs from 64-bit programs.

As stated in the is_32on64_mode() function, 32-bit/64-bit mode change
is effective immediately.  This was effective for 64-bit -> 32-bit
change, but not for the other way.

Reported-by: Erwan GOURIOU <erwan.gouriou@st.com> & Sebastien GANDON <sebastien.gandon@st.com>
Investigated-by: Yann Droneaud <yann@droneaud.fr>
Ref.: https://github.com/cedric-vincent/PRoot/issues/73
Add test for commit c47aeb7d "Fix ptrace emulation for multi-threaded programs"
Add initial support for ptrace ARCH_PRCTL request.

This is required to debug multi-threaded programs with versions of GDB
that are built against old kernel headers:

    PTRACE_ARCH_PRCTL is obsolete since [Linux] 2.6.25, where the
    fs_base and gs_base fields of user_regs_struct can be used
    directly.

    -- gdb/amd64-linux-nat.c

Note: GDB uses only GET_{F,G}S requests, that's why SET_{F,G}S
requests are not supported yet, I don't know how to test them anyway.
Prepare PRoot release v5.1.0.
Use "-sh" instead of "-" as the default command name, this is less confusing.
Load dynamic shared objects to their explicit base address if any.

Usually, dynamic shared objects are mapped to the beginning of the
virtual memory.  For example:

    $ readelf -aW /lib/ld-linux.so.2
    [...]
    Program Headers:
      Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
      LOAD           0x000000 0x00000000 0x00000000 0x1ec58 0x1ec58 R E 0x1000
      LOAD           0x01ec80 0x0001fc80 0x0001fc80 0x00bb8 0x00c78 RW  0x1000
    [...]

However, it appears that some versions of Fedora-20/i686 ship an ELF
interpreter with a non-null base address:

    $ readelf -aW /lib/ld-linux.so.2
    [...]
    Program Headers:
      Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
      LOAD           0x000000 0x43acf000 0x43acf000 0x1ec58 0x1ec58 R E 0x1000
      LOAD           0x01ec80 0x43aeec80 0x43aeec80 0x00bb8 0x00c78 RW  0x1000
    [...]

In such cases, PRoot should not add a base address to the load
address, otherwise this ELF interpreter might get mapped to an
unexpected location:

    proot info: translate("/" + "/lib/ld-linux.so.2")
    proot info:          -> "/usr/lib/ld-2.18.so"
    proot info: sysenter end: open(0xbfa63970, 0x0, 0x0, 0xbfa63a18, 0x0, 0x3) = 0xffffffda [0xbfa63970, 0]
    proot info: sysexit start: open(0xbfa63970, 0x0, 0x0, 0xbfa63a18, 0x0, 0x3) = 0x3 [0xbfa63970, 0]
    proot info: sysexit end: open(0xbfa63a7a, 0x0, 0x0, 0xbfa63a18, 0x0, 0x3) = 0x3 [0xbfa63984, 0]
    proot info: sysenter start: mmap2(0xf2acf000, 0x1f000, 0x5, 0x12, 0x3, 0x0) = 0xffffffda [0xbfa63978, 0]
    proot info: sysenter end: mmap2(0xf2acf000, 0x1f000, 0x5, 0x12, 0x3, 0x0) = 0xffffffda [0xbfa63978, 0]
    proot info: sysexit start: mmap2(0xf2acf000, 0x1f000, 0x5, 0x12, 0x3, 0x0) = 0xfffffff4 [0xbfa63978, 0]
    proot info: sysexit end: mmap2(0xf2acf000, 0x1f000, 0x5, 0x12, 0x3, 0x0) = 0xfffffff4 [0xbfa63978, 0]
    proot info: sysenter start: exit(0xb6, 0x1f000, 0xb6, 0xbfa63a20, 0x1, 0x3) = 0xffffffda [0xbfa63984, 0]
    proot info: sysenter end: exit(0xb6, 0x1f000, 0xb6, 0xbfa63a20, 0x1, 0x3) = 0xffffffda [0xbfa63984, 0]
    proot info: exited with status 182

I verified that the Linux kernel behaves the same:

        else if (loc->elf_ex.e_type == ET_DYN) {
    #ifdef CONFIG_ARCH_BINFMT_ELF_RANDOMIZE_PIE
            if (current->flags & PF_RANDOMIZE)
                load_bias = 0;
            else
                load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
    #else
            load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
    #endif
        }

Reported-by: Frank TEO <frank.teo@st.com>
Fix use-after-free: there's only one spot where tracees can be safely freed.

This spot is right before calling waitpid(): at this moment no tracees
are supposed to be pointed to.