Skip to content

Extending Import Macro

Rickey Bowers Jr edited this page Feb 9, 2021 · 6 revisions

How does this differ from the standard include file? Rather than listing all the DLLs twice and including all the API includes, the syntax gets changed to: libraries 'kernel32.dll','vulkan-1.dll' Adding a new library is just the change in this one place.

Because file names can have characters which are incompatible with symbol names, the standard include requires using a proxy name (ex. vulkan_1). This change converts the string to a numerical proxy internally -- just use the file name.

Side-effects are the API include file names are changed to {DLL file name}.fasmg (ex. vulkan-1.dll.fasmg). Also, the invocation of import must specify the DLL file name string.

A potential problem is using different letter casing on the string than the API include has. No different than the symbolic proxy.

macro libraries? definitions&
	section '.idata' import data readable writeable
	namespace PE
	Imports:
	iterate name, definitions
		repeat 1,N:name ; convert string to number
		if ~ redundant.N
			dd RVA lookup.N
			dd 0,0
			dd RVA string.N
			dd RVA address.N
		end if
		referred.N = 1
		end repeat
	end iterate
	dd 0,0,0,0,0
	iterate name, definitions
		repeat 1,N:name ; convert string to number
		if ~ redundant.N
			string.N db name,0
			align 2
		end if
		end repeat
	end iterate
	end namespace

	iterate name, definitions
		eval "include 'api/",name,".fasmg'"
	end iterate
end macro
macro import? name,definitions&
	align 8
	namespace PE
	repeat 1,N:name
	if defined referred.N
		lookup.N:
		iterate <_label,_string>, definitions
			if used _label
				if _string eqtype ''
					dq RVA _label.N
				else
					dq 8000000000000000h + _string
				end if
			end if
		end iterate
		if $ > lookup.N
			redundant.N = 0
			dq 0
		else
			redundant.N = 1
		end if
		address.N:
		end namespace

		iterate <_label,_string>, definitions
			if used _label
				if _string eqtype ''
					_label dq RVA _label.N
				else
					_label dq 8000000000000000h + _string
				end if
			end if
		end iterate

		namespace PE
		if ~ redundant.N
			dq 0
		end if
		iterate <_label,_string>, definitions
			if used _label & _string eqtype ''
				_label.N dw 0
				db _string,0
				align 2
			end if
		end iterate
	end if
	end repeat
	end namespace
end macro