Emulate /proc/self/exe when -q is used, and commit new value in execve() sysexit.

UMEQ (an awesome alternative to QEMU user-mode) does not emulate
"/proc/self/exe", moreover neither UMEQ nor QEMU can know how to
emulate "/proc/${pid}/exe", where ${pid} is different from self.

Also, new value of "/proc/self/exe" is committed only once execve(2)
is completed, that is, in exit stage.

Reported-by: Mickael GUENE <mickael.guene@st.com>
Update tracee's stack pointer before transfering data into tracee's stack.

I'm not sure this is required for remote transfer, but the Linux
kernel might not allow page faults below the stack pointer:

    Accessing the stack below %sp is always a bug.

    -- linux-3.1/arch/x86/mm/fault.c

Ref.: https://github.com/cedric-vincent/PRoot/issues/73
Fix name of proot-ed processes.

By default, proot-ed processes got their name replaced by the name of
the loader.  For examples:

    $ proot-v4.0.3 ps
      PID TTY          TIME CMD
     7885 pts/11   00:00:00 bash
     8131 pts/11   00:00:00 proot
     8132 pts/11   00:00:00 ld-2.17.so

    $ proot-v5.0.0 ps
      PID TTY          TIME CMD
     7885 pts/11   00:00:00 bash
     7916 pts/11   00:00:00 proot
     7917 pts/11   00:00:00 prooted-7916-Jb

This old behavior was confusing and it is now fixed:

    $ ./src/proot ps
      PID TTY          TIME CMD
     7885 pts/11   00:00:00 bash
     8585 pts/11   00:00:00 proot
     8586 pts/11   00:00:00 ps
Make build-system more cross-compilation friendly.
loader: Don't embed "build-id" section, it might conflict with loaded programs.

On Fedora, Debian, Ubuntu, and likely on many other distros, a
loadable segment is automatically added by the linker:

    The new section is canonically called .note.gnu.build-id [... it]
    has SHF_ALLOC in sh_flags, meaning it appears in the loaded memory
    image, and the normal link order puts allocated note sections very
    early in the memory image.

    -- https://fedoraproject.org/wiki/Releases/FeatureBuildId

By default this segment is mapped to a low address:

    $ readelf -lW src/loader/loader
    [...]
    Program Headers:
      Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
      LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x000144 0x000144 R   0x200000
      LOAD           0x200000 0x0000600000000000 0x0000600000000000 0x000388 0x000388 R E 0x200000
      NOTE           0x000120 0x0000000000400120 0x0000000000400120 0x000024 0x000024 R   0x4
      GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RWE 0x10

     Section to Segment mapping:
      Segment Sections...
       00     .note.gnu.build-id
       01     .text .rodata .eh_frame
       02     .note.gnu.build-id
       03

As a consequence, this unexpected segment in the loader might conflict
with PRooted processes:

    $ proot -q qemu-sh4 -r stlinux-2.4-sh4/ /bin/sh -c 'true; id'
    [...]
    qemu: uncaught target signal 11 (Segmentation fault) - core dumped

Reported-by: Jerome AUDU <jerome.audu@st.com>
Fix signedness issue in ptrace emulation for 32-bit programs on 64-bit systems.

Ptrace emulation for 32-bit programs on 64-bits systems works again.
Fix name of qemu-ed processes, see commit 2bccead0 for details.
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.
Update PRoot changelog.