Допустим, есть программа, который полностью загружает файл в rwx область и запускает его на выполнение, например таким образом
int main(void)
{
struct stat st;
stat("somefile.bin", &st);
void* ptr = mmap(0, st.st_size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
int fd = open("somefile.bin", O_RDONLY);
read(fd, ptr, st.st_size);
void (*foo)(void) = ptr;
foo();
}
Я натыкаюсь на какие-то дурацкие ошибки линкера, вот например если пытаюсь с таким ld-скриптом сделать:
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
ENTRY(_start)
SECTIONS
{
.text 0x0 : AT ( 0 ) {
*(.text)
*(.text*)
*(.init)
*(.fini)
*(.rodata)
*(.data)
*(.bss)
*(.ARM.exidx*)
*(.gnu.linkonce.armexidx.*)
*(COMMON)
}
}
$ arm-linux-gnueabihf-gcc-4.8 -static -nostartfiles -Wl,--strip-all,-T ldscript main.c -O3 -fPIC -o main
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: BFD (GNU Binutils for Ubuntu) 2.24 assertion fail ../../bfd/elf.c:4214
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: BFD (GNU Binutils for Ubuntu) 2.24 assertion fail ../../bfd/elf.c:4214
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: BFD (GNU Binutils for Ubuntu) 2.24 assertion fail ../../bfd/elf.c:4214
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: section .note.gnu.build-id loaded at [0000000000000000,0000000000000023] overlaps section .text loaded at [0000000000000000,00000000000559f7]
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: main: section __libc_freeres_ptrs lma 0x5639c adjusted to 0x563b4
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: .text has both ordered [`.ARM.exidx' in /usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/lib/../lib/libc.a(mmap.o)] and unordered [`.bss' in /usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/libgcc_eh.a(unwind-c.o)] sections
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status