live-bootstrap icon indicating copy to clipboard operation
live-bootstrap copied to clipboard

musl dynamic linking support

Open doraskayo opened this issue 3 years ago • 1 comments

Overview

This PR enables a native musl toolchain by patching gcc 4.7.4. Combined with binutils 2.38 (https://github.com/fosslinux/live-bootstrap/pull/191), it allows dynamic linking in musl to work without a need for musl-specific wrapper scripts. musl 1.2.3 is then rebuilt to actually enable dynamic linking and create libc.so, which also acts as musl's dynamic linker.

The GCC patches that achieve this were backported by me from GCC upstream without any additional changes.

See commit messages for more information.

Result

bash-5.1# cat test.cc 
#include <iostream>

int main(int argc, char **argv)
{
	std::cout << "Hello World" << std::endl;
	return 0;
}
bash-5.1# g++ test.cc -o test-musl-dynamic
bash-5.1# ./test-musl-dynamic 
Hello World
bash-5.1# readelf -d test-musl-dynamic | grep NEEDED
 0x00000001 (NEEDED)                     Shared library: [libc.so]
bash-5.1# readelf -a test-musl-dynamic | grep interpreter                                           
      [Requesting program interpreter: /lib/ld-musl-i386.so.1]

In addition to the above, this PR got me further towards a fully working Python 3 in the freedesktop-sdk side (Python is necessary for glibc and newer gcc). This change allows it to load and execute built-in modules successfully at runtime (requires support for dlopen, which in turn requires support for dynamic linking). Only a few built-in modules are now missing to get it fully working.

Non-goals

  • Some changes in the backported patches were not included as described in the commit message, mostly because they are irrelavant to live-bootstrap at this time, but also because they had conflicts. Building a newer GCC version would likely be a better approach to get them "for free".
  • I haven't investigated whether a native toolchain can be achieved earlier in the bootstrap using older binutils or gcc versions. It may be possible, but exploring this was outside my intended scope.
  • Removing the --disable-shared option from various packages that follow wasn't done as part of this PR. This can be explored in a future change.

Testing

This was tested using the bwrap and qemu bootstrap modes. Each commit was tested individually and includes its package hash changes.

Dependencies

Depends on https://github.com/fosslinux/live-bootstrap/pull/191 and therefore includes it.

cc: @stikonas, @fosslinux

doraskayo avatar Sep 11 '22 06:09 doraskayo

The latest revision has some fixes and improvements:

  1. Library search path configuration. This was missing in the previous revisions.
  2. Changing the dynamic linker's symlink location to /usr/lib to avoid overriding the /lib -> usr/lib symlink. I also made it relative while at it.
  3. Addition of ldd, which is simply a symlink to the dynamic linker.
    bash-5.1# ldd ./test-musl-dynamic                  
        /lib/ld-musl-i386.so.1 (0xf7e76000)
        libtest.so => /usr/lib/musl/libtest.so (0xf7e6b000)
        libc.so => /lib/ld-musl-i386.so.1 (0xf7e76000)
    

Hopefully this means all the issues were resolved and this PR should be acceptable for merging.

doraskayo avatar Sep 15 '22 06:09 doraskayo