Merging .text and .rdata on windows
Par zogzog, dimanche 29 juin 2008 à 15:17 :: Dev
Not sure that this may ever be useful to someone else, but here is how to merge the .text and .rdata of a windows PE file when compiling with mingw or cygwin. The msvc linker has a built-in option to do that
/MERGE:.rdata=.text
But the gnu linker do not provide such an option. Instead it relies on linker scripts. The following linker script "ld_script_merge_rdata_and_text.x" does the trick:
SECTIONS { .text : { *(.text) *(.rdata) *(SORT(.rdata$*)) }
I won't explain how it works because I don't really understand it /o\ there was some amount of trial and errors before I converged to this script. Here is an example of use (note that is just passed to the linker as an implicit linker script, the default linker script is not overridden):
echo 'int main() { printf("Quack quack !"); return 0; }' > quack.c gcc -Wl,ld_script_merge_rdata_and_text.x quack.c && objdump -h a.exe
The output is:
a.exe: file format pei-i386 Sections: Idx Name Size VMA LMA File off Algn 0 .text 000003a8 00401000 00401000 00000200 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE, DATA 1 .bss 00000040 00402000 00402000 00000000 2**2 ALLOC 2 .idata 00000188 00403000 00403000 00000600 2**2 CONTENTS, ALLOC, LOAD, DATA
Look mum ! no .rdata !