Skip to content

Commit ff30bcd

Browse files
Merge pull request #28 from byeongkeunahn/short-4
Shorten the generated code
2 parents a4c6f51 + 1d40959 commit ff30bcd

7 files changed

+44
-20
lines changed

scripts/base91.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ def encode(x_in, use_rle=False):
1313
i += 1
1414
if len(x) > 0 and x[-1] == 0:
1515
zeros_cnt = 1
16-
while i - 1 + zeros_cnt < len(x_in) and zeros_cnt < 255 and x_in[i - 1 + zeros_cnt] == 0:
16+
while i - 1 + zeros_cnt < len(x_in) and zeros_cnt < 256 and x_in[i - 1 + zeros_cnt] == 0:
1717
zeros_cnt += 1
1818
if zeros_cnt >= 2:
1919
x.pop()
20-
x.append(zeros_cnt)
20+
x.append(zeros_cnt - 1)
2121
sharp_insertion_points.append((current_bits // 13 * 2) + len(sharp_insertion_points))
2222
i += zeros_cnt - 1
2323
sharp_insertion_points = list(reversed(sharp_insertion_points))

scripts/static-pie-elf2bin.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,28 @@ def load_elf64(elf):
160160
continue # since bytearray is zero-initialized
161161

162162
dst_off, src_off, cnt = sh_dict['sh_addr'], sh_dict['sh_offset'], sh_dict['sh_size']
163-
memory_bin[dst_off:dst_off+cnt] = elf[src_off:src_off+cnt]
163+
blob = elf[src_off:src_off+cnt]
164+
165+
if sh_dict['sh_type'] == SHT_DYNAMIC:
166+
# Trim the DYNAMIC section, leaving only relocation-related entries
167+
# 16 == sizeof(Elf64_Dyn)
168+
dst = 0
169+
for src in range(0, len(blob), 16):
170+
# Included entries:
171+
# DT_PLTRELSZ = 2, DT_RELA = 7, DT_RELASZ = 8, DT_RELAENT = 9,
172+
# DT_REL = 17, DT_RELSZ = 18, DT_RELENT = 19, DT_PLTREL = 20,
173+
# DT_TEXT_REL = 22, DT_JMPREL = 23.
174+
#
175+
# Note: DT_RELACOUNT = 0x6fff_fff9 and DT_RELCOUNT = 0x6fff_fffa
176+
# are not included since they are redundant since
177+
# DT_RELACOUNT = DT_RELASZ/DT_RELAENT and
178+
# DT_RELCOUNT = DT_RELSZ/DT_RELENT.
179+
if b2i(blob[src:src+8]) in [2, 7, 8, 9, 17, 18, 19, 20, 22, 23]:
180+
blob[dst:dst+16] = blob[src:src+16]
181+
dst += 16
182+
blob[dst:] = bytearray(len(blob[dst:])) # fill remaining part with zeros
183+
184+
memory_bin[dst_off:dst_off+cnt] = blob
164185

165186
entrypoint_offset = b2i(elf[24:32])
166187
return memory_bin, pos_begin, entrypoint_offset

scripts/static-pie-gen.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
sol = "".join(sol)
6969

7070
# binary (raw)
71+
# Since we append a little-endian 8-byte nonnegative integer, we can practically ensure that the last byte is zero.
7172
code_raw = memory_bin[:-8]
7273
code_raw += (len(code_raw) + 8 - loader_fdict['entrypoint_offset']).to_bytes(8, byteorder='little')
7374
code_raw_b91 = base91.encode(code_raw, use_rle=True).decode('ascii')
@@ -123,7 +124,7 @@
123124

124125
# template
125126
template_candidates = [template_path]
126-
if lang_name == "Rust" and "x86_64" in target_name and "short" in template_path and len(code_raw) <= 4096:
127+
if lang_name == "Rust" and "x86_64" in target_name and "short" in template_path and len(code_raw) <= 4096 - 256:
127128
template_candidates.append(template_path.replace("short", "shorter"))
128129

129130
out = None

scripts/static-pie-prestub-amd64-print.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@
2222
prestub = prestub[:j]
2323

2424
# settings
25-
SPECIFIER = ".quad"
26-
CHUNK_SIZE = 8
27-
ENTRIES_PER_LINE = 4
25+
if "--octa" in sys.argv:
26+
SPECIFIER = ".octa"
27+
CHUNK_SIZE = 16
28+
ENTRIES_PER_LINE = 10
29+
else:
30+
SPECIFIER = ".quad"
31+
CHUNK_SIZE = 8
32+
ENTRIES_PER_LINE = 4
2833

2934
# pad to align at `CHUNK_SIZE`-byte boundary
3035
while len(prestub) % CHUNK_SIZE != 0:

scripts/static-pie-prestub-amd64-shorter.asm

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ _svc_alloc_rwx:
1919
xor r9d, r9d ; offset
2020
push rsi ; save rsi
2121
xor edi, edi ; rdi=0
22-
push 1
23-
pop rsi ; size
22+
mov esi, eax ; size (anything in [1, 4096])
2423
mov dl, 7 ; protect (safe since we have ensured rdx=0)
2524
push 0x22
2625
pop r10 ; flags
@@ -39,27 +38,25 @@ _decode_loop:
3938
shl eax, 13
4039
_decode_loop_2:
4140
lodsb
41+
xor ecx, ecx ; ecx = 0
4242
sub al, 0x23
43-
cdq
44-
jc _jump_to_entrypoint
45-
jz _decode_zeros
43+
jbe _decode_zeros
4644
dec al
47-
xchg eax, edx
45+
xchg eax, ecx
4846
lodsb
4947
sub al, 0x24
5048
imul eax, eax, 91
51-
add eax, edx
49+
add eax, ecx
5250
_decode_output:
5351
stosb
5452
shr eax, 8
5553
test ah, 16
5654
jnz _decode_output
5755
jmp _decode_loop
5856
_decode_zeros:
59-
dec rdi
60-
movzx ecx, byte [rdi]
61-
rep stosb ; the fact we jumped to here ensures al=0
62-
jmp _decode_loop_2
57+
xchg byte [rdi-1], cl ; ecx = cl = ((number of zeros) - 1), byte [rdi-1] = 0
58+
rep stosb ; we have made sure the last byte is zero (in the packager)
59+
jz _decode_loop_2
6360

6461
; Jump to entrypoint
6562
_jump_to_entrypoint:

scripts/static-pie-prestub-amd64-shorter.bin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
j X�E1�V1�j^�j"AZj�AX^H�����,#�r"t�Ȓ�,$k�[Ъ����u���H������H+���
1+
j X�E1�V1��Ʋj"AZj�AX^H�����1�,#v�ȑ�,$k�[Ȫ����u����O��t�H+���

scripts/static-pie-template-amd64-shorter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
$$$$solution_src$$$$
66
}
77
// SOLUTION END
8-
#[no_link]extern crate std;#[no_mangle]unsafe fn _start(){std::arch::asm!(".octa 6a07b25e016aff3156c931459958096ah,0e0c11fb097485e050f5841ff6a5a4122h,6b242cac92c8fe1674227299232cac0dh,48dfebf77510c4f608e8c1aad0015bc0h,0d7fff87f2b48d8ebaaf30fb60fcfffh",in("rsi")r$$$$binary_raw_base91$$$$.as_ptr())}
8+
#[no_link]extern crate std;#[no_mangle]unsafe fn _start(){std::arch::asm!(".octa 226a07b2c689ff3156c931459958096ah,0de0c11fb097485e050f5841ff6a5a41h,5bc06b242cac91c8fe1676232cc931ach,0ff4f86e0ebf77510c4f608e8c1aac801h,0d7fff87f2b48dc74aaf3h",in("rsi")r$$$$binary_raw_base91$$$$.as_ptr())}

0 commit comments

Comments
 (0)