Table of Contents

Introduction

Earlier in the Embedded Linux with Raspberry Pi 3B series, we have generated minimal root filesystem statically for the Raspberry Pi using busybox build system. The filesystem thus generated doesn’t have any shared libraries.

If we keep cross compiling statically for each and every program we have written, we will end up with the binaries and the final Embedded Linux system of size a lot more.

The goal of this article is to generate the filesystem with shared libraries in place, make binaries size smaller.

Busybox filesystem with shared libraries
Busybox filesystem with shared libraries

Finding the necessary shared libraries

Let’s write, compile a simple Hello World! program for the Raspeberry Pi 3B and execute on the board.

cd ~/rpi3/nfs/

cat << EOF >> helloworld.c
#include <stdio.h>

int main(void)
{
	printf("Hello World!\n");
	return 0;
}
EOF

Compile the program using cross-toolchain gcc.

export PATH=$PATH:~/x-tools/aarch64-rpi3-linux-uclibc/bin/
aarch64-rpi3-linux-uclibc-gcc helloworld.c -o helloworld

Now try to execute helloworld program on the Raspberry Pi by running the following command.

/helloworld

You will face the following error

/bin/sh: /helloworld: not found

It’s not like the file is missing. The file is still present in your root directory. You can confirm with ls -l /helloworld command on the board.

It’s because the the linker library (ld-uClibc.so.0) is missing. Let’s copy the library from the toolchain path to the filesystem /lib directory.

mkdir -p ~/rpi3/nfs/lib/
cp ~/x-tools/aarch64-rpi3-linux-uclibc/aarch64-rpi3-linux-uclibc/sysroot/lib/ld-uClibc* ~/rpi3/nfs/lib

Try to execute the helloworld binary again. Let’s see if any other shared libaries missing. Copy all the shared libraries reported from the toolchain diriectory to ~/rpi3/lib directory.

cp ~/x-tools/aarch64-rpi3-linux-uclibc/aarch64-rpi3-linux-uclibc/sysroot/lib/libc.so* ~/rpi3/nfs/lib
cp ~/x-tools/aarch64-rpi3-linux-uclibc/aarch64-rpi3-linux-uclibc/sysroot/lib/libuClibc* ~/rpi3/nfs/lib
In the above commands, libc.so is just a short link to libuClibc-*.so. So copy actual libraries also


Cross-compile busybox filesystem again

Let’s check the size of the busybox binary before disabling the static building.

ls -lh ~/rpi3/nfs/bin/busybox

It’s approximately 1.2M size when I checked it. Since the shared libraries are in place now, we can cross-compile the busybox filesystem by disabling the Settings -> Build static binary (no shared libraries) option in make menuconfig.

cd ~/rpi3/busybox/
make menuconfig

Save the config file, compile the filesystem and install.

make
make install

Now check the size of busybox binary again. It’s 977K for me. The size definitely reduced as it is going to use the shared libraries we copied above.