Search code examples
linuxlinux-kernelcross-compilingembedded-linuxelf

how to compile a C program as FDPIC_ELF without using Shared Libs


my platform:

Host:
   | OS: Ubuntu 20 LTS
   | Kernel: Microsoft WSL2 Linux Kernel
   | Cross compile Toolchain: arm-linux-gnueabi-gcc
   |__

Target:
   | Board: Waveshare CoreH7XXI
   | SOC: stm32h743 (Single Core Cortex M7 @400MHz)
   | Architecture: ARMV7e-M
   | Onboard DRAM: 8MB - 2.9MB(u-boot and kernel) = 5.1MB Free Space
   | Linux Kernel: 5.8.10 (stable 2020-09-17)
   | Busybox: latest stable version
   |__

Firstoff, my platform is MMU-less, and MMU-less platforms cannot run normal ET_EXEC type elf and the format of the executables should be FDPIC_ELF (an ELF with ET_DYN type and should be PICPIE/(Position Independent Code/Executable)).

this is my sample program:

#include <stdio.h>

int main() {
   printf("Hello World!");
   return 0;
}

I want to compile it in a way that it won't rely on external shared libraries (e.g. ld-linux.so.3 and libc.so.6), because the kernel loads the whole libraries into ram and they consume a lot of RAM!

How can I compile my program to be an elf with ET_DYN type and PIC/PIE but without using external shared libs. Note that if I use -static, the generated elf cannot be an ET_DYN and PIC/PIE type. So, what should I do???


Solution

  • What you're trying to achieve is not possible with the stated restrictions.

    ld-linux.so.3 is the dynamic loader (ld.so), which is indeed represented as a dynamic library.

    Any ET_DYN ELF file needs a dynamic loader — that's why it's a dynamic file. The only way not to need a dynamic loader is to have a static file — but then you wouldn't get an ET_DYN, but rather an ET_EXEC.