Я хочу создать двоичный файл в MacOS Catalina (64-разрядный) с сегментом данных, который можно сделать исполняемым (см. здесь для подробностей), но не исполняемым с самого начала.
Я создаю свой двоичный файл с помощью
gcc -nostdlib -segprot __DATA rwx rw- ....
Я также создал объектный файл с g cc и затем напрямую вызвал ld. Версия ld:
$ ld -v
@(#)PROGRAM:ld PROJECT:ld64-530
BUILD 18:57:17 Dec 13 2019
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 11.0.0, (clang-1100.0.33.17) (static support for 23, runtime is 23)
TAPI support using: Apple TAPI version 11.0.0 (tapi-1100.0.11)
Этот должен сделать сегмент данных изначально RW, но разрешите мне использовать mprotect для расширения разрешений в этом сегменте на RWX.
Однако , Я заметил, что сегмент __DATA является RW для начального и максимального разрешения:
$ otool -l jonesforth
.
.
.
Load command 2
cmd LC_SEGMENT_64
cmdsize 312
segname __DATA
vmaddr 0x0000000100001000
vmsize 0x0000000000024000
fileoff 4096
filesize 4096
maxprot 0x00000003
initprot 0x00000003
nsects 3
flags 0x0
Section
.
.
.
Что-то мне не хватает? Документация darwin здесь говорит:
-segprot name max init (32-bit only)
Specifies the maximum and initial virtual memory protection of
the named segment, name, to be max and init ,respectively. The
values for max and init are any combination of the characters
`r' (for read), `w' (for write), `x' (for execute) and '-' (no
access). The default is `rwx' for the maximum protection for
all segments for PowerPC architecures and `rw` for the all Intel
architecures. The default for the initial protection for all
segments is `rw' unless the segment contains a section which
contains some machine instructions, in which case the default
for the initial protection is `rwx' (and for Intel architecures
it also sets the maximum protection to `rwx' in this case). The
default for the initial protection for the ``__TEXT'' segment is
`rx' (not writable).
Конечно, это документация darwin (только 32-битная), но это единственное, что я нашел. Я подозреваю, что либо g cc «неправильно» поддерживает синтаксис защиты darwin, либо он сломан, либо что-то в darwin изменилось с x86 на x64.
Любые указатели были бы хороши, заранее спасибо.