Linux Compatibility on BSD for the PPC Platform: Part 2

Managing dynamic executables

In this article, we'll take a closer look at the problems that prevent
dynamic Linux binaries from working in compatibility mode on the NetBSD/PowerPC platform. This
includes the way the arguments are passed to the Linux program, and
ELF auxilliary table handling.

Passing arguments to the program

The first problem here is Linux's ld.so did not get its command-line arguments. In fact, no program running in Linux emulation -- either statically linked or dynamically linked -- was actually able to get its arguments. This could be outlined by building this sample program on a Linux box (statically, of course), and trying to run it on the NetBSD box:

This programs tests argument and return value passing between
the kernel and the emulated executable. When running it, we get no
output at all. The program got a null argc, which demonstrated the
problem passing command-line arguments.

The arguments are passed to the program using the stack. When preparing
the program launch, the kernel sets up the stack so the program
will be able to find argc, argv, and envp. To inspect this
mechanism a bit deeper, we can use a stack dumper, like the following
piece of code :

This program also uses global and local variables to help study argument
passing. It dumps the stack from an arbitrary address until it
reaches the end of the stack and crashes, because pages after the stack
are not accessible when running in user mode. We do not really care
about this crash because it displays what we are looking for. However,
this can be a problem if you are working on a terminal that is unable to scroll back and want to pipe the stack dump's output to more(1) or
less (1).

If you want to do this, you will have to modify the program so
it catches the SIGSEGV signal. You will also have to ensure that
linux_sendsig() in linux_machdep.c does not crash anything. Most likely,
you will keep that function empty. The easy solution is certainly
to get a terminal that has a scrollback feature.