Removing RTTI from binaries

Mike via D.gnu d.gnu at puremagic.com
Thu Jan 15 20:31:16 PST 2015


On Thursday, 15 January 2015 at 12:01:05 UTC, Johannes Pfau wrote:

> My best guess is that the strings are always placed in rodata, 
> never in
> separate sections. If you do write("x"), "x" is also in rodata, 
> the
> rodata section can't be removed. If you delete the write call 
> there's
> no reference to rodata and it's possible to remove the complete
> section.
>
> After some google-fu:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192
> Considering this was filed in 2000 I'd say it's not very likely 
> to get
> fixed soon :-(
>
> So the best option is probably to get rid of this problem by 
> patching
> the compiler (@notypeinfo or -fnortti).

Here's a filthy sed hack to workaround this bug:

1) compile to assembly:
-----------------------------------------------
gdc -S -static -frelease -fno-emit-moduleinfo -nophoboslib 
-nostdlib test.d --entry=main -ffunction-sections -fdata-sections 
-Wl,--gc-sections -o test_temp.s

2) use sed to modify the assembly, putting each
string into its own section:
-----------------------------------------------
sed -e 's/^\(\.LC[0-9]*\)\(\:\)/\.section .rodata\1\n\1\2/g' 
test_temp.s >test.s

3) compile the new assembly:
-----------------------------------------------
as test.s -o test.o

4) link:
-----------------------------------------------
ld test.o --entry=main --gc-sections -o test

5) verify:
-----------------------------------------------
objdump -s -j .rodata test

Contents of section .rodata:
  400168 780a                                 x.

size test
    text    data     bss     dec     hex filename
     338       0       0     338     152 test

6) execute:
------------------------------------------------
./test
x

Filthy, but cheap and effective.  Fortunately it's all automated 
with rdmd.

Mike


More information about the D.gnu mailing list