apache / apache/nuttx

Loading ELFs which contain a global offset table.

Open
#8,886 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
4k
Forks
1.7k
Avg merge
1d 17h
Merged PRs (30d)
237

Description

I've been experimenting with building the some of the risc-v targets, primarily on `boards/risc-v/litex/arty-a7` and `boards/risc-v/qemu-rv` with newer versions of the [riscv-gnu-toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain). The intention being to enable some of the newer language features, and a more recent version of libcxx (#8244).

I've noticed that some toolchain configurations, particularly when musl is used, generate elfs which contain relocations involving a global offset table (GOT). From what I can see in `binfmt/libelf`, there's currently no support for relocations through a GOT, either during loading or at the architecture level (`libs/libc/machine/*/arch_elf.c`).

I've seen comments in `boards/sim/sim/sim/scripts/Make.defs` regarding using `-fno-pic` to avoid a GOT being generated, so possibly this is by design, and I'm overlooking something more fundamental here.

Currently, it seems that building the toolchain with newlib avoids a GOT being generated at all for the applications I've tested. However, I'm not sure if this is the case in general.

Perhaps someone has some insight on this?

The toolchain configuration which currently works:
```
$ riscv32-unknown-elf-gcc -v
Using built-in specs.
COLLECT_GCC=riscv32-unknown-elf-gcc
COLLECT_LTO_WRAPPER=/home/xx/projects/riscv-gnu-toolchain/bin/libexec/gcc/riscv32-unknown-elf/12.2.0/lto-wrapper
Target: riscv32-unknown-elf
Configured with: /home/xx/projects/riscv-gnu-toolchain/build/../gcc/configure --target=riscv32-unknown-elf --prefix=/home/xx/projects/riscv-gnu-toolchain/bin --disable-shared --disable-threads --enable-languages=c,c++ --with-pkgversion=g2ee5e430018 --with-system-zlib --enable-tls --with-newlib --with-sysroot=/home/xx/projects/riscv-gnu-toolchain/bin/riscv32-unknown-elf --with-native-system-header-dir=/include --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=/home/xx/projects/riscv-gnu-toolchain/gcc --disable-multilib --with-abi=ilp32 --with-arch=rv32imac --with-tune=rocket --with-isa-spec=2.2 'CFLAGS_FOR_TARGET=-Os -mcmodel=medany' 'CXXFLAGS_FOR_TARGET=-Os -mcmodel=medany'
Thread model: single
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (g2ee5e430018)
```

The same configuration with musl generates a GOT and relocation relative to it.

Contributor guide

Open the contributing guide

Research direction

Start by reading the ELF handling in binfmt/libelf and the architecture-specific loaders under libs/libc/machine/*/arch_elf.c, then compare the -fno-pic guidance in boards/sim/sim/sim/scripts/Make.defs. Determine the expected behavior for RISC-V ELFs containing GOT relocations and identify the relevant loading and architecture-level changes before defining completion criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot, operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.