Einzelnen Beitrag anzeigen

Benutzerbild von paule32.jk
paule32.jk

Registriert seit: 24. Sep 2022
Ort: Planet Erde
218 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: NASM - Erstellung eines Win64-Bit Images für die Verwendung mit mehr als 2 Funcca

  Alt 29. Okt 2023, 14:55
Hi,
I don't want use a Linker.
I would like produce a Flat-Image on the fly.

The DLL Library Code looks like:

Delphi-Quellcode:
library kalle32;
uses windows;
procedure kalli(h: HWND; lpText: PChar; lpCaption: PChar; mb: UINT); stdcall; export;
begin
  MessageBoxA(h,Pchar(lpText),PChar(lpCaption),mb);
end;

exports kalli;
begin
  MessageBoxA(0,'hallo','kaaass',0);
end.
Then I have extend the Assembly Code:
Code:
BITS 64
 
%define align(n,r) (((n+(r-1))/r)*r)
 
; DOS Header
    dw 'MZ'                ; e_magic
    dw 0                    ; [UNUSED] e_cblp
    dw 0                    ; [UNUSED] c_cp
    dw 0                    ; [UNUSED] e_crlc
    dw 0                    ; [UNUSED] e_cparhdr
    dw 0                    ; [UNUSED] e_minalloc
    dw 0                    ; [UNUSED] e_maxalloc
    dw 0                    ; [UNUSED] e_ss
    dw 0                    ; [UNUSED] e_sp
    dw 0                    ; [UNUSED] e_csum
    dw 0                    ; [UNUSED] e_ip
    dw 0                    ; [UNUSED] e_cs
    dw 0                    ; [UNUSED] e_lfarlc
    dw 0                    ; [UNUSED] e_ovno
    times 4 dw 0            ; [UNUSED] e_res
    dw 0                    ; [UNUSED] e_oemid
    dw 0                    ; [UNUSED] e_oeminfo
    times 10 dw 0           ; [UNUSED] e_res2
    dd pe_hdr              ; e_lfanew
 
; PE Header
pe_hdr:
    dw 'PE', 0              ; Signature
 
; Image File Header
    dw 0x8664               ; Machine
    dw 0x01                 ; NumberOfSections
    dd 0                    ; [UNUSED] TimeDateStamp
    dd 0                    ; PointerToSymbolTable
    dd 0                    ; NumberOfSymbols
    dw opt_hdr_size        ; SizeOfOptionalHeader
    dw 0x22                 ; Characteristics
 
; Optional Header, COFF Standard Fields
opt_hdr:
    dw 0x020b              ; Magic (PE32+)
    db 0x0e                ; MajorLinkerVersion
    db 0x16                 ; MinorLinkerVersion
    dd code_size           ; SizeOfCode
    dd 0                    ; SizeOfInitializedData
    dd 0                    ; SizeOfUninitializedData
    dd entry               ; AddressOfEntryPoint
    dd iatbl               ; BaseOfCode
 
; Optional Header, NT Additional Fields
    dq 0x000140000000       ; ImageBase
    dd 0x10                 ; SectionAlignment
    dd 0x10                 ; FileAlignment
    dw 0x06                 ; MajorOperatingSystemVersion
    dw 0                    ; MinorOperatingSystemVersion
    dw 0                    ; MajorImageVersion
    dw 0                    ; MinorImageVersion
    dw 0x06                 ; MajorSubsystemVersion
    dw 0                    ; MinorSubsystemVersion
    dd 0                    ; Reserved1
    dd file_size           ; SizeOfImage
    dd hdr_size            ; SizeOfHeaders
    dd 0                    ; CheckSum
    dw 0x02                 ; Subsystem (Windows GUI)
    dw 0x8160               ; DllCharacteristics
    dq 0x100000             ; SizeOfStackReserve
    dq 0x1000               ; SizeOfStackCommit
    dq 0x100000             ; SizeOfHeapReserve
    dq 0x1000               ; SizeOfHeapCommit
    dd 0                    ; LoaderFlags
    dd 0x02                 ; NumberOfRvaAndSizes
 
; Optional Header, Data Directories
    dd 0                    ; Export, RVA
    dd 0                    ; Export, Size
    dd itbl                ; Import, RVA
    dd itbl_size           ; Import, Size
 
opt_hdr_size equ $-opt_hdr
 
; Section Table
    section_name db '.'    ; Name
    times 8-($-section_name) db 0
    dd sect_size           ; VirtualSize
    dd iatbl               ; VirtualAddress
    dd code_size           ; SizeOfRawData
    dd iatbl               ; PointerToRawData
    dd 0                    ; PointerToRelocations
    dd 0                    ; PointerToLinenumbers
    dw 0                    ; NumberOfRelocations
    dw 0                    ; NumberOfLinenumbers
    dd 0x60000020           ; Characteristics
 
hdr_size equ $-$$
 
code:
; Import Address Directory
iatbl:
    dq symbol_1
    dq symbol_2
    dq 0
iatbl_size equ $-iatbl
 
; Strings
title:
    db "Hallo Welt !!!", 0
content:
    db "ABCDEFGHIJKL", 0
 
; Entry
entry:
  mov r9d, 0x00240040     ; uType
  lea r8, [rel title]    ; lpCaption
  lea rdx, [rel content] ; lpText
  xor ecx, ecx           ; hWnd
  mov rax, [rel iatbl + 8] ; MessageBoxN
  call rax
 
 
    times align($-$$,16)-($-$$) db 0xcc
 
; Import Directory 1
itbl:
    dq intbl_1              ; OriginalFirstThunk
    dd 0                    ; TimeDateStamp
    dd dll_name_1           ; ForwarderChain
    dd iatbl               ; Name
    dq 0                    ; FirstThunk

; Import Directory 2
itbl_2:
    dq intbl_2              ; OriginalFirstThunk
    dd 0                    ; TimeDateStamp
    dd dll_name_2           ; ForwarderChain
    dd iatbl + 8            ; Name
    dq 0                    ; FirstThunk

itbl_size equ $-itbl
 
; Import Name Table 1
intbl_1:
    dq symbol_1
    dq 0

; Import Name Table 2
intbl_2:
    dq symbol_2
    dq 0
 
; Symbol 1
symbol_1:
    dw 0                    ; [UNUSED] Function Order
    db 'MessageBoxA', 0     ; Function Name

; Symbol 2
symbol_2:
    dw 0
    db 'kalli', 0

dll_name_2: db 'kalle32.dll', 0
dll_name_1: db 'USER32.dll' , 0
 
sect_size equ $-code
 
    times align($-$$,16)-($-$$) db 0
 
code_size equ $-code
file_size equ $-$$
There must be an mistake somewhere.
Because, when I use the Lines:

Code:
  mov r9d, 0x00240040     ; uType
  lea r8, [rel title]    ; lpCaption
  lea rdx, [rel content] ; lpText
  xor ecx, ecx           ; hWnd
  mov rax, [rel iatbl]   ; MessageBoxN
  call rax
The Application run fine on my Windows Station.
Frag doch einfach
Alles was nicht programmiert werden kann, wird gelötet
  Mit Zitat antworten Zitat