Hi,
the rumination point is this:the prototype of mmap is this:
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
At the end of the mmap procedure the mov [mappedfile],eax , mappedfile should contain the memory location of the mapped file, i.e. the starting point to address its content in memory, as the initial address of Buffer where I could load a file.
Is that right?
I ask because I can use [mappedfile] as argument for sys_write getting the a LEN part of the mmap on stdout, so the handler is also the address of the location in Hex?
The post started as a question and ended as an answer (perhaps).
I was stuck on the new indirection level needed, so could not immediately get access to the content of the mapped file [mappedfile + ecx] would change the handler, is not an offset in the mapped content, this are the very first "hands in jam" programs, and some comes up for the first time.
After some rumination, realized that moving the content of mappedfile, in a register, would have produced a way use the content in effective address calculation and so:
Buffer resb 4 > ecx Buff (in sys_write)
while
mappedfile resb 4 (+mmap 90 sys_call) > ecx [mappedfile] (in sys_write)
; mappedfile contains the address so we need to estract in a register to use
; dereference again to get its content in effective address calculation
mov ebp,[mappedfile]
change: mov byte [ebp+2+edx],'3'
dec edx
jnz change
make the job.
What is missing here a calculation of the size, that is set up. I will make also that and update the code.
section .bss
mappedfile: resb 4
section .data
mmap_args:
.addr: dd 0
.len: dd 512
.prot: dd 3
.flags: dd 1
.fd: dd -1
.offset: dd 0
section .text
;-------------------------------------------------------------------------
; FileSize: Calculates the size of a file.
; UPDATED: 2014-05-30
; IN:
; RETURNS:
; MODIFIES:
; CALLS:
; DESCRIPTION: Calculate the size of file for mmap
;
FileSize:
mov eax,140
mov ebp,ebx
mov ecx,0
mov edx,0
lea esi,[ebp-8]
mov edi,2
int 80h
mov eax,[ebp-8]
ret
global _start
_start:
; open(char *path, int flags, mode_t mode);
; Get our command line arguments.
nop
nop
pop ebx ; argc
pop ebx ; argv[0] (executable name)
pop ebx ; argv[1] (desired file name)
mov eax, 5 ; syscall number for open
mov ecx, 0102 ; O_RDONLY = 0
mov edx, 0644 ; Mode is ignored when O_CREAT isn't specified
int 80h ; Call the kernel
test eax, eax ; Check the output of open()
js BadFile
mov [mmap_args.fd],eax
; begin reading the file
; mmap
mov eax,90 ; set the system call value
lea ebx,[mmap_args]
int 80h
test eax,eax
js BadFile
add esp,24
;store the memory location of the memory mapped file
mov [mappedfile],eax
; Operation on files
mov edx,5
; mappedfile contains the address so we need to estract in a register to use
; dereference again to get its content in effective address calculation
mov ebp,[mappedfile]
change: mov byte [ebp+2+edx],'3'
dec edx
jnz change
mov eax,4
mov ebx,1
mov ecx, [mappedfile]
mov edx,30
int 80h
;unmap
mov eax,91
mov ebx, [mappedfile]
mov ecx,[mmap_args.len]
int 80h
test eax,eax
js BadFile
; close the open file handle
mov eax,6
mov ebx,[mmap_args.fd]
int 80h
exit:
mov eax, 1 ; 1 = syscall for exit
xor ebx, ebx ; makes ebx technically set to zero
int 80h
BadFile:
mov ebx,eax
mov eax,1
int 80h
Thanks
Fabio D'Alfonso