5 ms·
If you are right then this can be mitigated by two additional options passed to objcopy, namely --add-section to create an empty section and --set-section-flags
by cnvogel 11y ago
If you are right then this can be mitigated by two additional options passed to objcopy, namely --add-section to create an empty section and --set-section-flags to adjust the flags of this empty section. I'm just recreating the section/flags that I see in a normally compiled file.
$ make
cc -Wall -Wextra -ggdb -Os -c -o testme.o testme.c
objcopy -I binary -O elf64-x86-64 -B i386:x86-64 \
--rename-section .data=.rodata,alloc,load,readonly,data,contents \
--add-section ".note.GNU-stack"=/dev/null \
--set-section-flags ".note.GNU-stack"=contents,readonly \
/etc/passwd passwd.o || (rm -f passwd.o ; exit 1)
cc testme.o passwd.o -o testme
(https://github.com/vogelchr/objcopy_to_carray https://github.com/vogelchr/objcopy_to_carray)
Without --add.../--set section:
$ readelf -lW testme
...
GNU_STACK (...) RWE 0x10
With --add.../--set-section:
$ readelf -lW testme
...
GNU_STACK (...) RW 0x10
- rwmj 11y agoLooks good. The reason I know a bit about this is I had the same "bin2o" hacky script that used objcopy. It broke every time someone found a new architecture or platform (ie. having to choose the correct -O and -B flags is non-trivial if you want to support every architecture). The solution (which is not really much better than yours) is a script that creates some assembler and assembles it: https://github.com/libguestfs/supermin/blob/master/src/bin2s.pl https://github.com/libguestfs/supermin/blob/master/src/bin2s... I think it's bad that such a simple thing is so hard to do. Edit: In fact looking at the script now I can see we really should be using .rodata instead of .data. That bug has survived for at least 4 years. Patch posted: https://www.redhat.com/archives/libguestfs/2015-December/msg00095.html https://www.redhat.com/archives/libguestfs/2015-December/msg...