You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WASM has a section that allows to define global variables that we then access from any function. We use it for internal WASM backend implementation of things like stack manipulation and memory allocator pointer.
Reserve registers. Not ideal, we need registers for computation
Allocate on stack in the main program, then remember the location somehow and use it (challenge: the location changes in nested functions relative to the current runtime stack location)
Use heap. Challenge: you get a pointer, where do you store it.
Store global variables just like we store strings, in the global section
I think in the ELF format we have to use a .data section (that should be read+write). We have to figure out how to reference data from it. I would start with these simple examples: https://cs.lmu.edu/~ray/notes/nasmtutorial/, such as:
global _startsection .text_start: movrax,1 ; system call for writemovrdi,1 ; file handle 1 is stdoutmovrsi, message ; address of string to outputmovrdx,13 ; number of bytessyscall ; invoke operating system to do the writemovrax,60 ; system call for exitxorrdi,rdi ; exit code 0syscall ; invoke operating system to exitsection .datamessage: db "Hello, World",10 ; note the newline at the end
Which references the string from a .data section using a label.
does this example work on Linux, as is?
let's modify the example to modify the "variable" message. Does it work? I.e., is the section .data read/write? If not, we need to get it to work
now once we have a working example, we need to figure out how to reproduce what NASM is doing ourselves. This might involve:
creating a proper ELF binary format class and use it to handle the ELF headers and creating sections
create the .data section with read/write flags
I think you simply emit the data from the section after all executable code directly into the binary. In the ELF header, you specify the location of this section in the binary, and set appropriate read/write flags and that it is a data section.
figure out how referencing memory locations from it works: what relative or absolute memory address to use for instructions like mov rsi, message.
doubt: What should be the virtual address for the data segment? Or how do we compute the virtual address for the data segment? I looked up websites but could not any exact approach (which works/is-working) to compute the virtual address for the data segment.
There does not seem a direct NASM example available. We can construct an example and then view its headers using the following:
(wasm_asm) lpython$ cat main.asm
global _start
section .text
_start: mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do the write
mov rax, 60 ; system call forexit
xor rdi, rdi ;exit code 0
syscall ; invoke operating system to exit
section .data
message: db "Hello, World", 10 ; note the newline at the end
(wasm_asm) lpython$ nasm -felf64 main.asm && ld main.o -o main
(wasm_asm) lpython$ ./main
Hello, World
(wasm_asm) lpython$ readelf -h main
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x401000 Start of program headers: 64 (bytes into file) Start of section headers: 8464 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 3 Size of section headers: 64 (bytes) Number of section headers: 6 Section header string table index: 5
(wasm_asm) lpython$ readelf -l main
Elf file type is EXEC (Executable file)
Entry point 0x401000
There are 3 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x00000000000000e8 0x00000000000000e8 R 0x1000
LOAD 0x0000000000001000 0x0000000000401000 0x0000000000401000
0x0000000000000025 0x0000000000000025 R E 0x1000
LOAD 0x0000000000002000 0x0000000000402000 0x0000000000402000
0x000000000000000d 0x000000000000000d RW 0x1000
Section to Segment mapping:
Segment Sections...
00
01 .text
02 .data
(wasm_asm) lpython$
The segment at index 1 (that is at offset 0x0000000000001000) is the text section. The segment at index 2 (that is at offset 0x0000000000002000) is the data section.
The text section seems read-only, which makes sense for code. The data section seems read/write, so it should be possible to modify the string and everything should work. So that's what we need. Can you try to modify your NASM file to actually modify the string and print it again, ensuring that everything works as expected?
If so, then we just need to reproduce this with our backend.
Can you try to modify your NASM file to actually modify the string and print it again, ensuring that everything works as expected?
The following example works. We already checked it as marked in #1539 (comment).
(wasm_asm) lpython$ cat main.asm
global _start
section .text
_start:
; print the string before modification
mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do the write
; modify the string
mov rax, message
mov byte [rax], 0b1000001 ; store A inplace of H
; print the string after modification
mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do the write
;exit the program with exit code 0
mov rax, 60 ; system call forexit
xor rdi, rdi ;exit code 0
syscall ; invoke operating system to exit
section .data
message: db "Hello, World", 10 ; note the newline at the end
(wasm_asm) lpython$ nasm -felf64 main.asm && ld main.o -o main
(wasm_asm) lpython$ ./main
Hello, World
Aello, World
(wasm_asm) lpython$ echo$?
0
Perfect, thanks for the example. So I think this shows how to do it. All we need is to just generate the same binary that NASM does. To get started, just use the same address that NASM gives it. We can later look into NASM sources to understand how it is computed.
In the above example, we have 3 program/segment headers with field values taken from the output of readelf -h main and readelf -l main from here #1539 (comment). I am currently omitting the section headers, because it seems sections headers are needed for object files and shared libraries. In our case we have an executable for which program/segment headers are required.
Nice. It currently segfaults, so I assume it doesn't work? Do you know what the issue is? We'll have to compare to the NASM generated binary and see what the differences are.
I would use the assembler to generate the instructions. Then I would separately do the headers, then at the end, you put everything together. You don't need to do this simultaneously, or if you do, I don't understand the issue.
Activity
certik commentedon Feb 21, 2023
Options how to implement this:
certik commentedon Feb 21, 2023
I think in the ELF format we have to use a
.datasection (that should be read+write). We have to figure out how to reference data from it. I would start with these simple examples: https://cs.lmu.edu/~ray/notes/nasmtutorial/, such as:Which references the string from a
.datasection using a label..dataread/write? If not, we need to get it to work.datasection with read/write flagsmov rsi, message.ubaidsk commentedon Mar 16, 2023
doubt: What should be the virtual address for the
datasegment? Or how do we compute the virtual address for thedatasegment? I looked up websites but could not any exact approach (which works/is-working) to compute the virtual address for thedatasegment.certik commentedon Mar 16, 2023
Post the NASM example that creates a RW data section, that works. Then we can see how NASM creates the ELF file and figure out the algorithm.
ubaidsk commentedon Mar 17, 2023
There does not seem a direct NASM example available. We can construct an example and then view its headers using the following:
(wasm_asm) lpython$ cat main.asm global _start section .text _start: mov rax, 1 ; system call for write mov rdi, 1 ; file handle 1 is stdout mov rsi, message ; address of string to output mov rdx, 13 ; number of bytes syscall ; invoke operating system to do the write mov rax, 60 ; system call for exit xor rdi, rdi ; exit code 0 syscall ; invoke operating system to exit section .data message: db "Hello, World", 10 ; note the newline at the end(wasm_asm) lpython$ nasm -felf64 main.asm && ld main.o -o main (wasm_asm) lpython$ ./main Hello, World(wasm_asm) lpython$ readelf -l main Elf file type is EXEC (Executable file) Entry point 0x401000 There are 3 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000 0x00000000000000e8 0x00000000000000e8 R 0x1000 LOAD 0x0000000000001000 0x0000000000401000 0x0000000000401000 0x0000000000000025 0x0000000000000025 R E 0x1000 LOAD 0x0000000000002000 0x0000000000402000 0x0000000000402000 0x000000000000000d 0x000000000000000d RW 0x1000 Section to Segment mapping: Segment Sections... 00 01 .text 02 .data (wasm_asm) lpython$The segment at index
1(that is at offset0x0000000000001000) is thetextsection. The segment at index2(that is at offset0x0000000000002000) is thedatasection.certik commentedon Mar 17, 2023
The
textsection seems read-only, which makes sense for code. Thedatasection seems read/write, so it should be possible to modify the string and everything should work. So that's what we need. Can you try to modify your NASM file to actually modify the string and print it again, ensuring that everything works as expected?If so, then we just need to reproduce this with our backend.
ubaidsk commentedon Mar 17, 2023
The following example works. We already checked it as marked in #1539 (comment).
(wasm_asm) lpython$ cat main.asm global _start section .text _start: ; print the string before modification mov rax, 1 ; system call for write mov rdi, 1 ; file handle 1 is stdout mov rsi, message ; address of string to output mov rdx, 13 ; number of bytes syscall ; invoke operating system to do the write ; modify the string mov rax, message mov byte [rax], 0b1000001 ; store A inplace of H ; print the string after modification mov rax, 1 ; system call for write mov rdi, 1 ; file handle 1 is stdout mov rsi, message ; address of string to output mov rdx, 13 ; number of bytes syscall ; invoke operating system to do the write ; exit the program with exit code 0 mov rax, 60 ; system call for exit xor rdi, rdi ; exit code 0 syscall ; invoke operating system to exit section .data message: db "Hello, World", 10 ; note the newline at the end (wasm_asm) lpython$ nasm -felf64 main.asm && ld main.o -o main (wasm_asm) lpython$ ./main Hello, World Aello, World (wasm_asm) lpython$ echo $? 0certik commentedon Mar 17, 2023
Perfect, thanks for the example. So I think this shows how to do it. All we need is to just generate the same binary that NASM does. To get started, just use the same address that NASM gives it. We can later look into NASM sources to understand how it is computed.
ubaidsk commentedon Mar 18, 2023
I formed the following example:
(wasm_asm) lpython$ cat main4.asm BITS 64 org 0x0000000000400000 ehdr: db 0x7f db 0x45 db 0x4c db 0x46 db 0x02 db 0x01 db 0x01 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 dw 0x0002 dw 0x003e dd 0x00000001 dq 0x0000000000401000 dq 0x0000000000000040 dq 0x0000000000000000 dd 0x00000000 dw 0x0040 dw 0x0038 dw 0x0003 dw 0x0000 dw 0x0000 dw 0x0000 phdr: dd 0x00000001 dd 0x00000004 dq 0x0000000000000000 dq 0x0000000000400000 dq 0x0000000000400000 dq 0x00000000000000e8 dq 0x00000000000000e8 dq 0x0000000000001000 text_phdr: dd 0x00000001 dd 0x00000005 dq 0x0000000000001000 dq 0x0000000000401000 dq 0x0000000000401000 dq 0x0000000000000025 dq 0x0000000000000025 dq 0x0000000000001000 data_phdr: dd 0x00000001 dd 0x00000006 dq 0x0000000000002000 dq 0x0000000000402000 dq 0x0000000000402000 dq 0x000000000000000d dq 0x000000000000000d dq 0x0000000000001000 global _start section .text _start: mov rax, 1 ; system call for write mov rdi, 1 ; file handle 1 is stdout mov rsi, message ; address of string to output mov rdx, 13 ; number of bytes syscall ; invoke operating system to do the write mov rax, 60 ; system call for exit xor rdi, rdi ; exit code 0 syscall ; invoke operating system to exit section .data message: db "Hello, World", 10 ; note the newline at the endIn the above example, we have
3program/segment headers with field values taken from the output ofreadelf -h mainandreadelf -l mainfrom here #1539 (comment). I am currently omitting the section headers, because it seems sections headers are needed for object files and shared libraries. In our case we have an executable for which program/segment headers are required.( Reference: https://github.com/cirosantilli/notes/blob/master/stack-overflow/section-vs-segment.md
certik commentedon Mar 18, 2023
Nice. It currently segfaults, so I assume it doesn't work? Do you know what the issue is? We'll have to compare to the NASM generated binary and see what the differences are.
certik commentedon Mar 21, 2023
Let's write a proper binary format class using the style of https://github.com/certik/test_wasm/blob/master/parse_macho.cpp.
ubaidsk commentedon Mar 23, 2023
The following example works. I think the padding bytes were needed to make the starting of a new segment aligned to alignment byte (which is
0x1000).(wasm_asm) lpython$ cat main.asm BITS 64 org 0x0000000000400000 ehdr: db 0x7f db 0x45 db 0x4c db 0x46 db 0x02 db 0x01 db 0x01 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 db 0x00 dw 0x0002 dw 0x003e dd 0x00000001 dq 0x0000000000401000 dq 0x0000000000000040 dq 0x0000000000000000 dd 0x00000000 dw 0x0040 dw 0x0038 dw 0x0003 dw 0x0000 dw 0x0000 dw 0x0000 phdr: dd 0x00000001 dd 0x00000004 dq 0x0000000000000000 dq 0x0000000000400000 dq 0x0000000000400000 dq 0x00000000000000e8 dq 0x00000000000000e8 dq 0x0000000000001000 text_phdr: dd 0x00000001 dd 0x00000005 dq 0x0000000000001000 dq 0x0000000000401000 dq 0x0000000000401000 dq 0x0000000000000025 dq 0x0000000000000025 dq 0x0000000000001000 data_phdr: dd 0x00000001 dd 0x00000006 dq 0x0000000000003000 dq 0x0000000000403000 dq 0x0000000000403000 dq 0x000000000000000d dq 0x000000000000000d dq 0x0000000000001000 filler1: times 3864 db 0 global _start section .text _start: mov rax, 1 ; system call for write mov rdi, 1 ; file handle 1 is stdout mov rsi, message ; address of string to output mov rdx, 13 ; number of bytes syscall ; invoke operating system to do the write mov rax, 60 ; system call for exit xor rdi, rdi ; exit code 0 syscall ; invoke operating system to exit filler2: times 8155 db 0 section .data message: db "Hello, World", 10 ; note the newline at the end (wasm_asm) lpython$ nasm -fbin main.asm && chmod +x main (wasm_asm) lpython$ ./main Hello, World (wasm_asm) lpython$certik commentedon Mar 23, 2023
Very good! Now let's do the same from our backend and we should be good to go.
ubaidsk commentedon Mar 24, 2023
Currently, we call our custom functions which generate binary as well as text assembly simultaneously.
For supporting #1539 (comment), if we define a structure as follows:
and use it as follows:
We can generate binary assembly in the above way.
doubt: How do we (simultaneously) generate the assembly text format by using structs for elf and program headers?
ubaidsk commentedon Mar 25, 2023
@certik please, could you share some hints on how to proceed with #1539 (comment)?
certik commentedon Mar 25, 2023
I would use the assembler to generate the instructions. Then I would separately do the headers, then at the end, you put everything together. You don't need to do this simultaneously, or if you do, I don't understand the issue.