1) Create data.txt
echo "Hello World" > data.txt
2) Convert data.txt to data.o
objcopy -I binary -O elf64-x86-64 -B i386 data.txt data.o
3) Get Symbols
hexdump -C data.o
000000b0 00 5f 62 69 6e 61 72 79 5f 64 61 74 61 5f 74 78 |._binary_data_tx| 000000c0 74 5f 73 74 61 72 74 00 5f 62 69 6e 61 72 79 5f |t_start._binary_| 000000d0 64 61 74 61 5f 74 78 74 5f 65 6e 64 00 5f 62 69 |data_txt_end._bi| 000000e0 6e 61 72 79 5f 64 61 74 61 5f 74 78 74 5f 73 69 |nary_data_txt_si| 000000f0 7a 65 00 00 2e 73 79 6d 74 61 62 00 2e 73 74 72 |ze...symtab..str| 00000100 74 61 62 00 2e 73 68 73 74 72 74 61 62 00 2e 64 |tab..shstrtab..d| 00000110 61 74 61 00 00 00 00 00 00 00 00 00 00 00 00 00 |ata.............| 00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
_binary_data_txt_start _binary_data_txt_end _binary_data_txt_size
4) Create main.c
#include <stdio.h> extern char _binary_data_txt_start; extern char _binary_data_txt_end; int main(){ char* p = &_binary_data_txt_start; while ( p != &_binary_data_txt_end ) putchar(*p++); return 0; }
5) Compile
gcc main.c data.o -o myprogram
6) Run
./myprogram
Compile windows exe
1) Install
sudo apt install gcc-mingw-w64-i686-win32 sudo apt install g++-mingw-w64-i686-win32
2) Test
i686-w64-mingw32-gcc --version i686-w64-mingw32-g++ --version
3) Convert data.txt to data.o
i686-w64-mingw32-objcopy -I binary -O pe-i386 -B i386 data.txt data.o
4) Get Symbols
hexdump -C data.o
00000070 30 00 00 00 0c 00 00 00 ff ff 00 00 02 00 46 00 |0.............F.| 00000080 00 00 5f 62 69 6e 61 72 79 5f 64 61 74 61 5f 74 |.._binary_data_t| 00000090 78 74 5f 73 74 61 72 74 00 5f 62 69 6e 61 72 79 |xt_start._binary| 000000a0 5f 64 61 74 61 5f 74 78 74 5f 65 6e 64 00 5f 62 |_data_txt_end._b| 000000b0 69 6e 61 72 79 5f 64 61 74 61 5f 74 78 74 5f 73 |inary_data_txt_s| 000000c0 69 7a 65 00
5) Remove initial underscores
binary_data_txt_start binary_data_txt_end binary_data_txt_size
6) Create main.c or main.cpp
#include <stdio.h> extern char binary_data_txt_start; extern char binary_data_txt_end; int main(){ char* p = &binary_data_txt_start; while ( p != &binary_data_txt_end ) putchar(*p++); return 0; }
#include <iostream> extern char binary_data_txt_start; extern char binary_data_txt_end; int main(){ char* p = &binary_data_txt_start; while (p != &binary_data_txt_end) std::cout << *p++; return 0; }
7) Compile
i686-w64-mingw32-gcc main.c data.o -o main.exe or i686-w64-mingw32-g++ main.cpp data.o -o main.exe
Part 2
https://dev.to/gurigraphics/embedding-files-in-an-executable-windows-part-2-3gja
Top comments (0)