← 输入框|github.com|刷新
Skip to content

WASM->x86: add support for global variables #1539

Closed

Description

@certik

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.

Activity

  1. certik commented on Feb 21, 2023

    @certik
    ContributorAuthor

    Options how to implement this:

    • 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
  2. certik commented on Feb 21, 2023

    @certik
    ContributorAuthor

    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    _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

    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.
  3. ubaidsk commented on Mar 16, 2023

    @ubaidsk
    Collaborator

    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.

  4. certik commented on Mar 16, 2023

    @certik
    ContributorAuthor

    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.

  5. ubaidsk commented on Mar 17, 2023

    @ubaidsk
    Collaborator

    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 -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.

  6. certik commented on Mar 17, 2023

    @certik
    ContributorAuthor

    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.

  7. ubaidsk commented on Mar 17, 2023

    @ubaidsk
    Collaborator

    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 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 $?
    0
  8. certik commented on Mar 17, 2023

    @certik
    ContributorAuthor

    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.

  9. ubaidsk commented on Mar 18, 2023

    @ubaidsk
    Collaborator

    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 end
    (wasm_asm) lpython$ nasm -fbin main4.asm
    (wasm_asm) lpython$ ./main4
    Bus error (core dumped)
    (wasm_asm) lpython$ echo $?
    135

    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.

    ( Reference: https://github.com/cirosantilli/notes/blob/master/stack-overflow/section-vs-segment.md

    • sections live in object files (.o extension)
    • segments live in executable files (usually no extension)
    • Executables can also contain sections, but that information is only useful for debugging: it can be stripped and it will still run just fine. )
  10. certik commented on Mar 18, 2023

    @certik
    ContributorAuthor

    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.

  11. certik commented on Mar 21, 2023

    @certik
    ContributorAuthor

    Let's write a proper binary format class using the style of https://github.com/certik/test_wasm/blob/master/parse_macho.cpp.

  12. ubaidsk commented on Mar 23, 2023

    @ubaidsk
    Collaborator

    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$ 
  13. certik commented on Mar 23, 2023

    @certik
    ContributorAuthor

    Very good! Now let's do the same from our backend and we should be good to go.

  14. ubaidsk commented on Mar 24, 2023

    @ubaidsk
    Collaborator

    Currently, we call our custom functions which generate binary as well as text assembly simultaneously.

    ...
    a.add_label("phdr");
    a.asm_dd_imm32(1);  // p_type
    a.asm_dd_imm32(4);  // p_flags (permission to read only)
    a.asm_dq_imm64(0);        // p_offset
    ...

    For supporting #1539 (comment), if we define a structure as follows:

    struct Elf64_Phdr {
        uint32_t type;
        uint32_t flags;
        uint64_t offset;
        uint64_t vaddr;
        uint64_t paddr;
        ...
    };

    and use it as follows:

    Elf64_Phdr elf64_phdr;
    elf64_phdr.type = 1;
    elf64_phdr.flags = 5;
    ...
    memcpy (m_code.p, &elf64_phdr, sizeof(elf64_phdr));

    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?

  15. ubaidsk commented on Mar 25, 2023

    @ubaidsk
    Collaborator

    @certik please, could you share some hints on how to proceed with #1539 (comment)?

  16. certik commented on Mar 25, 2023

    @certik
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions