LEB128 decoder and section parsing

This commit is contained in:
hrigar committed 2026-03-20 14:38:41 +05:30
1 parent 174bfd1d46
commit 2f165e7c8e
4 files changed
+137

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
; esi = pointer to LEB128 bytes
; eax = decoded value, esi advanced past encoded bytes
decode_leb128_core:
xor eax, eax
xor ecx, ecx
.next_byte:
movzx edx, byte [esi]
inc esi
mov bl, dl
and edx, 0x7F
shl edx, cl
or eax, edx
add cl, 7
test bl, 0x80
jnz .next_byte
ret
decode_leb128_u32:
push ebx ecx
call decode_leb128_core
pop ecx ebx
ret
decode_leb128_s32:
push ebx ecx
call decode_leb128_core
cmp cl, 32
jae .no_extend
test bl, 0x40
jz .no_extend
push edx
mov edx, 0xFFFFFFFF
shl edx, cl
or eax, edx
pop edx
.no_extend:
pop ecx ebx
ret
+68
View File
@@ -0,0 +1,68 @@
SECT_CUSTOM = 0
SECT_TYPE = 1
SECT_IMPORT = 2
SECT_FUNCTION = 3
SECT_TABLE = 4
SECT_MEMORY = 5
SECT_GLOBAL = 6
SECT_EXPORT = 7
SECT_START = 8
SECT_ELEMENT = 9
SECT_CODE = 10
SECT_DATA = 11
SECT_DATACOUNT = 12
parse_sections:
pushad
mov esi, file_buf + WASM_HEADER_SZ
mov edi, file_buf
add edi, [file_size]
.next:
cmp esi, edi
jae .done
movzx eax, byte [esi]
inc esi
mov [sec_id], eax
call decode_leb128_u32
mov [sec_size], eax
mov [sec_content], esi
add eax, esi
mov [sec_next], eax
push esi
mov esi, msg_sec_hdr
call print_str
mov eax, [sec_id]
call print_dec
call print_sec_name
mov esi, msg_sec_comma_sz
call print_str
mov eax, [sec_size]
call print_dec
mov esi, msg_bytes
call print_str
pop esi
mov esi, [sec_next]
jmp .next
.done:
popad
ret
print_sec_name:
push esi eax
mov eax, [sec_id]
cmp eax, 12
ja .unknown
mov esi, [sec_name_tbl + eax*4]
call print_str
pop eax esi
ret
.unknown:
pop eax esi
ret
+30
View File
@@ -64,6 +64,8 @@ start:
mov esi, msg_bytes
call print_str
call parse_sections
xor ebx, ebx
jmp .exit
@@ -113,6 +115,9 @@ start:
mov eax, SYS_EXIT
int 0x80
include 'leb128.inc'
include 'sections.inc'
; esi = null-terminated string
print_str:
push eax ebx ecx edx esi
@@ -200,6 +205,26 @@ msg_size db 'File size: ', 0
msg_bytes db ' bytes', 10, 0
msg_newline db 10, 0
msg_sec_hdr db 'Section ', 0
msg_sec_comma_sz db ', size: ', 0
sec_name_tbl dd sn_custom, sn_type, sn_import, sn_func, sn_table
dd sn_memory, sn_global, sn_export, sn_start, sn_element
dd sn_code, sn_data, sn_datacount
sn_custom db ' (Custom)', 0
sn_type db ' (Type)', 0
sn_import db ' (Import)', 0
sn_func db ' (Function)', 0
sn_table db ' (Table)', 0
sn_memory db ' (Memory)', 0
sn_global db ' (Global)', 0
sn_export db ' (Export)', 0
sn_start db ' (Start)', 0
sn_element db ' (Element)', 0
sn_code db ' (Code)', 0
sn_data db ' (Data)', 0
sn_datacount db ' (DataCount)', 0
hex_chars db '0123456789ABCDEF'
hex_buf rb 12
dec_buf rb 12
@@ -207,4 +232,9 @@ dec_buf rb 12
file_fd dd 0
file_size dd 0
sec_id dd 0
sec_size dd 0
sec_content dd 0
sec_next dd 0
file_buf rb FILE_BUF_SIZE
+1
View File
@@ -28,6 +28,7 @@ check "no arguments" "Usage:" $WASM
check "valid wasm" "Valid wasm file" $WASM test/wasm/valid.wasm
check "bad magic" "invalid wasm magic" $WASM test/wasm/badmagic.wasm
check "bad version" "unsupported wasm" $WASM test/wasm/badver.wasm
check "section list" "(Type)" $WASM test/wasm/valid.wasm
echo ""
echo "Results: $PASS/$TESTS passed, $FAIL failed"