inline assembly with microsoft cl tool - inline-assembly

How can I build inline assembly with the Microsoft cl tool? when I try the standard
asm(nop);
it says unresolved external symbol asm. Any ideas? thanks!

Try with the following:
__asm
{
nop
mov eax, 4
}

Related

How to resolve function name in elf

I wanted to write an elf parser and disassemble the .text section, so I parsed the elf file and gave the .text section to the capstone to disassemble it for me. Unfortunately, capstone doesn't resolve function names.
According to the below assembly code in my elf file, there is a call to a function that I want to resolve its name.
call 8048380
I checked .symtab section but functions that need relocation like printf has a 0 address in the table because their address is unknown until load time.
So how am I gonna resolve its name?
I checked .symtab section but functions that need relocation like printf
The function you are interested in (the one at address 0x8048380) is not like printf and doesn't require runtime relocation.
It's unclear from your question how you obtained this dissassembly:
call 8048380
Chances are you need to use better tool, or you pointed your tool at a stripped binary (don't do that).
Here is an example of what the reasonable output should look like:
int foo() { return 42; }
int main() { return foo(); }
$ gcc t.c
$ gdb -q ./a.out
(gdb) disas main
Dump of assembler code for function main:
0x08048410 <+0>: push %ebp
0x08048411 <+1>: mov %esp,%ebp
0x08048413 <+3>: call 0x8048406 <foo> // GDB resolves the address
0x08048418 <+8>: pop %ebp
0x08048419 <+9>: ret
End of assembler dump.

Can't figure out how to write interrupt handler for Z80 using SDCC

I'm developing a program in C for the Z80 and compiling using SDCC. I can't figure out how to create interrupt handlers for the NMI interrupt that starts at 0x0066 and the IM1 interrupt that starts at 0x0038. I'm using these calls:
void IM1_InterruptHandler(void) __interrupt
and
void NMI_InterruptHandler (void) __critical __interrupt
and the resulting assembly looks about right but they aren't located at the proper addresses. I did spot this thread:
https://sourceforge.net/p/sdcc/feature-requests/519/
but can't figure out how to use the above example crt0.s file with SDCC for a Z80 target.
Using the --use-crt switch doesn't seem to work.
unknown compiler option '--use-crt=crt0.s' ignored
Anyone experienced with Z80 development with SDCC that can provide some guidance?
Edit:
Still not quite there yet. My crt.s file looks like this:
.module crt0
.globl _main
.globl _IM1_InterruptHandler
.globl _NMI_InterruptHandler
.area _HEADER (ABS)
;; Reset vector
.org 0
jp init
.org 0x08
reti
.org 0x10
reti
.org 0x18
reti
.org 0x20
reti
.org 0x28
reti
.org 0x30
reti
.org 0x38
jp _IM1_InterruptHandler
.org 0x66
jp _NMI_InterruptHandler
.org 0x100
init:
;; Stack at the top of memory.
ld sp,#0x8300
call _main
;; Ordering of segments for the linker.
.area _HOME
.area _CODE
.area _DATA
.area _CODE
And I'm doing the following:
sdasz80 -l -o mycrt.rel crt0.s
sdcc -mz80 --no-std-crt0 --code-loc 0x0000 --data-loc 0x8000 mycrt.rel ppclone_menu.c
Every thing seems to compile just fine but when I bring up the code in the disassembler I don't see any of the crt0 code being inserted above at locations 0x08 through 0x66.
To use a custom crt0 file you first need to compile it using sdasz80, which should be part of your SDCC install:
sdasz80 -o crt0_int.rel crt0_int.asm
Then you compile your program adding the following to the SDCC command line:
--no-std-crt0 crt0_int.rel
So the full command line would be something like:
sdcc --code-loc 0xWhatever --data-loc 0xWhatever -mz80 --no-std-crt0 crt0_int.rel somelibrary.lib yoursource.c
If you need examples of complete crt0 files, you have one in my MSX software repository.
Edit: You are passing --code-loc 0x0000 to sdcc when compiling your source, this will cause the code section to overwrite whatever was defined in crt0. Change it to a more suitable value (for your crt0 looks like 0x0110 would be fine) or leave it out, so the compiler will choose an appropriate value by itself.

Cannot create .lib file for a 64 bit Fortran DLL

I have written some wrapper functions in c++ for some old Fortran (g77, I think) code. I created the .dll and .lib using an old Visual Fortran compiler, and have everything linked up and working in visual studio.
The problem I have now is creating an equivalent 64 bit version. I have installed gfortran and created a 64 bit .dll but no .lib was generated along with it, and I can't seem to link to the DLL or call any of the fortran functions without it.
I created my 64 bit .dll with the commands...
$ gfortran -c {filenames.for}
$ gfortran -m64 -shared -mrtd -o dll_foo.dll {filenames.o}
And here is a sample subroutine from the fortran...
SUBROUTINE KFACT(TR, RHOL, RHOV, FLIQ, FVAP, XK, IWANT, PROPR)
!DEC$ ATTRIBUTES DLLEXPORT :: KFACT
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
INCLUDE 'nprop.cmn'
DIMENSION PROPR(NPROP)
DIMENSION IWANT(NPROP)
C
C SET IWANT VECTOR TO RETURN FUGACITY COEFFICIENT
C
CALL IVZERO(IWANT, NPROP)
IWANT(13) = 1
C
C CALL PROP2 TO RETURN VAP AND LIQ FUGACITY COEFFICIENTS
C
CALL PROPS2(IWANT, 0, TR, RHOL, PROPR)
FLIQ = PROPR(13)
CALL PROPS2(IWANT, 0, TR, RHOV, PROPR)
FVAP = PROPR(13)
C
XK = FLIQ / FVAP
C
RETURN
END
C
SUBROUTINE PDP(DEL, TR, PR, DPRDD, IWANT, PROPR)
How can I create a .dll and .lib file to be linked to, from my Visual Studio (2005) c++ project?
/////////////////////////////UPDATE/////////////////////////////
I have been able to create a .lib file by using the following commands...
ar -cru libName.lib {filenames.o}
ranlib libName.lib
After adding this lib to my Additional Dependencies in VS, I get LNK errors for all of the fortran functions when they are called. The fortran declarations in the c++ look like...
extern "C" void PSAT(double& TK, double& PMPA, double& RHOL, double& RHOV, int IWORK[], double PROPR[], int& IERR);
and the LNK errors for this function...
error LNK2028: unresolved token (0A00004A) "extern "C" void __cdecl PSAT(double &,double &,double &,double &,int * const,double * const,int &)" (?PSAT##$$J0YAXAEAN000QEAHQEANAEAH#Z) referenced in function "double __cdecl PFTH(double,double,int)" (?PFTH##$$FYANNNH#Z)
error LNK2019: unresolved external symbol "extern "C" void __cdecl PSAT(double &,double &,double &,double &,int * const,double * const,int &)" (?PSAT##$$J0YAXAEAN000QEAHQEANAEAH#Z) referenced in function "double __cdecl PFTH(double,double,int)" (?PFTH##$$FYANNNH#Z)
I have tried adding different directives in the fortran code to resolve the names like but I have been unsuccessful.
I don't know if the problem lies in the c code, the fortran code, or the creation of the .lib file.
Any help would be greatly appreciated!
Unlike MS based linkers, GNU based linkers do not need a .lib file. All you need to do is prefix the dll with lib. In your case, it would be libdll_foo.dll. To link, just drop the lib. For instance, to link to a program called main
gfortran -o main.exe -ldll_foo main.f95
It needs libdll_foo to be on the path. Alternatively, you could tell it where to find libdll_foo.dll with the -L parameter.
I ended going with CMake to create my X64 .lib file. Here is the CMakeLists if anyone is interested.
cmake_minimum_required(VERSION 2.8)
enable_language(Fortran)
set(CMAKE_Fortran_CREATE_SHARED_LIBRARY ON)
set(CMAKE_Fortran_COMPILER gfortran)
add_library(Steam64 SHARED AUXPK.o eospk.o INTPK.o PROPPK.o SOLVPK.o coef.cmn coefig.cmn nprop.cmn wconst.cmn)
SET_TARGET_PROPERTIES(Steam64 PROPERTIES LINKER_LANGUAGE C ARCHIVE_OUTPUT_DIRECTORY "C:/Users/MYNAH/Desktop/fortranSource/build")
add_executable(testf AUXPK.o eospk.o INTPK.o PROPPK.o SOLVPK.o coef.cmn coefig.cmn nprop.cmn wconst.cmn)
target_link_libraries(testf Steam64)

YAML with VC++ 2010 will compile to Release, but not to Debug

I'm trying to learn YAML with C++, i made the given yaml-cpp files into a .dll and .lib file with VC++ Express 2010 by using CMake. I have set up my project the same way i set up other libraries like SFML.
My issue is, when i try to build a Release version of the example code given on the yaml-cpp site i get:
Ogre
Dragon
Wizzard
However, when i try to build a Debug version, i get:
Assertion failed: false, file d:\microsoft visual studio 10.0\vc\include\yaml-cp
p\nodeimpl.h, line 39
I don't know how to handle this. Do i need to build a debug version of the library? If yes, how? I don't know which project options could affect this if i managed to change something.
When i'm compiling, i get a warning:
d:\microsoft visual studio 10.0\vc\include\yaml-cpp\conversion.h(51): warning C4146: unary minus operator applied to unsigned type, result still unsigned
With alot of template printouts, f.e. :
1> d:\microsoft visual studio 10.0\vc\include\yaml-cpp\nodereadimpl.h(35) : see reference to function template instantiation 'bool YAML::ConvertScalar<T>(const YAML::Node &,T &)' being compiled
1> with
1> [
1> T=unsigned int
1> ]
Is this a problem on my side? Bad CMake file and compilation?
> yaml_test.exe!main() Line 108 C++
yaml_test.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
yaml_test.exe!mainCRTStartup() Line 371 C
kernel32.dll!7c817077()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
Aborts at:
doc[i] >> monster;
The program doesn't try to enter the overloaded function.
In my own code it breaks when i try to use my first >> operator, which is the build in one for int.
I'm using the code from http://pastebin.com/PdKWDgQa, though for the original yaml-cpp example code it does the same. The output in Release mode is right, Debug stops and returns the same assert code.
For reference, the stack call in Release mode at the { bracket in the >> function for monster looks like this:
> yaml_test.exe!operator>>(const YAML::Node & node={...}, Monster & monster={...}) Line 36 C++
yaml_test.exe!main() Line 109 C++
msvcr100.dll!_initterm(void (void)* * pfbegin=0x00000001, void (void)* * pfend=0x003a5050) Line 873 C
yaml_test.exe!__tmainCRTStartup() Line 555 + 0x17 bytes C
kernel32.dll!7c817077()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
EDIT:
Actually, i have rebuilt the yaml-cpp project i made with CMake as Debug, everything runs fine when i use it now. I'm sorry if this is obvious, i'm new to these kind of issues.

Exported function symbol name mangling

I have a D DLL that is being loaded by a C++ program that I have no control over. The program LoadLibrarys my DLL and uses GetProcAddress to find a function named "extension_load" that takes one argument (a pointer). In my D DLL I have:
extern (C) int extension_load(void* ptr) {
return 0;
}
And this name needs to be exported as extension_load but it is being exported as extension_load#4, so GetProcAddress cannot find it. How do I make it plain extension_load without the name mangling?
You'll need to provide the linker with a .def file that renames the export. Docs are here, you need EXPORTS.
I got it working with some help from Hans Passant's link. Here is my .def file for anyone who will need it in the future (probably myself too):
EXETYPE NT
EXPORTS
extension_load
DllMain
The .def file I have is named dll.def. I have the function written as:
extern (C++) int extension_load(void* ptr) {
and the IDE I use is D-IDE, so to give the linker the def file, go to Project > Properties > Build Options and type
nameofdef.def
in the Extra Linking arguments text box. This assumes that the nameofdef.def file exists in your main project directory for D-IDE to find.
There is really no need for a def file. Just prepend your functions with export, e.g.:
export extern (C) int extension_load(void* ptr) {
return 0;
}
And compile via: dmd -ofmydll.dll mydll.d. Of course you'll need to define DllMain() as well.