flat assembler
Message board for the users of flat assembler.

Index > Main > Using 'virtual' to move the func's data into data section

Author
Thread Post new topic Reply to topic
hellomachine



Joined: 18 May 2023
Posts: 22
Location: I don't even exist
hellomachine 18 May 2023, 12:31
Hello, I'm using FASM (not FASMg).
I know that it's possible to have a function with data at its end (or anywhere else near that function (data, for example a JMP table)).

For example,

Code:
func:
        ...
        jmp     QWORD [.jmp_table]
        ...
 .jmp_table:
        dq ...
        dq ...
        dq ...
    


But I want to move each function's data into the data section without
putting that data in a separate file and include it in that section (data).
So I searched, and I found virtual directive which seems going to fix my problem and this is my code:

Code:
format MS64 COFF

public func

virtual at 0x00
 data8::
end virtual

section '.text' code readable executable align 64

func:
        mov     rax, .func_data
        mov     rax, QWORD [rax]
        ret

 virtual data8
  .func_data dq 1024
 end virtual

section '.rdata' data readable align 64

 db 'BeginData'

 ; I expect data8 content to be placed here.
 virtual data8
  data8.size = $ - $$
 end virtual
 repeat data8.size
  load a byte from data8:%-1
  db a
 end repeat

 db 'EndData'
    



Everything is seems OK to me, but I got Segmentation fault error when I tried to run 'func' and the error is about this line :
Code:
mov     rax, QWORD [rax]    


What is the problem? I check the binary file and data was there too !!![/b]
Post 18 May 2023, 12:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 18 May 2023, 12:34
You have defined the base address of data8 as "virtual at 0x00", so the value in rax is null.

You can define a label before the repeat line and use that label for the table address.
Code:
some_label: ; <--- your data is here
repeat data8.size
; ...    
Post 18 May 2023, 12:34
View user's profile Send private message Visit poster's website Reply with quote
hellomachine



Joined: 18 May 2023
Posts: 22
Location: I don't even exist
hellomachine 18 May 2023, 12:44
revolution wrote:
You have defined the base address of data8 as "virtual at 0x00", so the value in rax is null.

You can define a label before the repeat line and use that label for the table address.
Code:
some_label: ; <--- your data is here
repeat data8.size
; ...    


Well I have too many functions and each function has its own jmp-table and in each function, I need a local name (for each function) to call that jmp-table so defining a name before the repeat is just like to not working with virtual and putting the jmp-tables in data section directly ...
Post 18 May 2023, 12:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 18 May 2023, 12:48
You can use the label as an offset.
Code:
mov     rax, .func_data + some_label
;...
mov     rax, .func2_data + some_label    
Post 18 May 2023, 12:48
View user's profile Send private message Visit poster's website Reply with quote
hellomachine



Joined: 18 May 2023
Posts: 22
Location: I don't even exist
hellomachine 18 May 2023, 12:57
revolution wrote:
You can use the label as an offset.
Code:
mov     rax, .func_data + some_label
;...
mov     rax, .func2_data + some_label    


Nice Solution, thank you. I'm going to use it, but isn't there any clean solution for this problem? For example, how about
Code:
'virtual at some_label'    
?
Post 18 May 2023, 12:57
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.