calling-convention

Calling C function from x64 assembly with registers instead of stack

旧时模样 提交于 2019-12-06 04:08:39
This answer puzzled me. According to the standard C calling conventions , the standard way to call C functions is to push arguments to the stack and to call the subroutine. That is clearly different from syscalls , where you set different registers with appropriate arguments and then syscall . However, the answer mentioned above gives this GAS code: .global main .section .data hello: .asciz "Hello\n" .section .text main: movq $hello, %rdi movq $0, %rax call printf movq $0, %rax ret which works with gcc hello.s -o hello . The part that calls printf is: movq $hello, %rdi movq $0, %rax call

Can I programatically deduce the calling convention used by a C++ dll?

旧街凉风 提交于 2019-12-06 02:41:48
问题 Imagine you'd like to write a program that tests functions in a c++ dll file. You should enable the user to select a dll (we assume we are talking about c++ dlls). He should be able to obtain a list of all functions exported by the dll. Then, the user should be able to select a function name from the list, manually input a list of arguments ( the arguments are all basic types, like int, double, bool or char arrays (e.g. c-type strings) ) and attempt to run the selected function with the

Calling C function which takes no parameters with parameters

人走茶凉 提交于 2019-12-06 00:28:14
I have some weird question about probably undefined behavior between C calling convention and 64/32 bits compilation. First here is my code: int f() { return 0; } int main() { int x = 42; return f(x); } As you can see I am calling f with an argument while f takes no parameters. My first question was does this argument is really given to f while calling it. The mysterious lines After a little objdump I obtained curious results. While passing x as argument of f: 00000000004004b6 <f>: 4004b6: 55 push %rbp 4004b7: 48 89 e5 mov %rsp,%rbp 4004ba: b8 00 00 00 00 mov $0x0,%eax 4004bf: 5d pop %rbp

C callback functions defined in an unnamed namespace?

£可爱£侵袭症+ 提交于 2019-12-06 00:21:44
问题 I have a C++ project that uses a C bison parser. The C parser uses a struct of function pointers to call functions that create proper AST nodes when productions are reduced by bison: typedef void Node; struct Actions { Node *(*newIntLit)(int val); Node *(*newAsgnExpr)(Node *left, Node *right); /* ... */ }; Now, in the C++ part of the project, i fill those pointers class AstNode { /* ... */ }; class IntLit : public AstNode { /* ... */ }; extern "C" { Node *newIntLit(int val) { return (Node*

__cdecl results in larger executable than __stdcall?

一世执手 提交于 2019-12-05 16:44:40
I found this: Because the stack is cleaned by the called function, the __stdcall calling convention creates smaller executables than __cdecl, in which the code for stack cleanup must be generated for each function call . Suppose I got 2 functions: void __cdecl func1(int x) { //do some stuff using x } void __stdcall func2(int x, int y) { //do some stuff using x, y } and here in the main() : int main() { func1(5); func2(5, 6); } IMO, it is main() 's responsibility to clean up the stack of the call to func1(5) , and func2 will clean up the stack of the call to func2(5,6) , right? Four questions:

What's the default calling convention of a C++ lambda function?

浪尽此生 提交于 2019-12-05 11:20:40
问题 The following code was compiled with VC++ 2012: void f1(void (__stdcall *)()) {} void f2(void (__cdecl *)()) {} void __cdecl h1() {} void __stdcall h2() {} int main() { f1(h1); // error C2664 f2(h2); // error C2664 f1([](){}); // OK f2([](){}); // OK auto fn = [](){}; f1(fn); // OK f2(fn); // OK } I think the errors are normal yet the OKs are abnormal. So, my questions are: What's the calling convention of a C++ lambda function? How to specify the calling convention of a C++ lambda function?

x64 calling convention (stack) and varargs

≯℡__Kan透↙ 提交于 2019-12-05 10:34:54
I've read Microsoft's documentation , but the scheme is so awkward, I thought I'd double-check to make sure I'm understanding it correctly... My understanding is the generic method by which parameters are passed is this: --- bottom of stack --- (return address) [shadow space for arg 1] [shadow space for arg 2] [shadow space for arg 3] [shadow space for arg 4] arg N arg N - 1 arg N - 2 ... arg 6 arg 5 ---- top of stack ----- It seems so awkward when implementing va_arg and such... is this actually correct? Raymond Chen The correct diagram is --- Bottom of stack --- RSP + size (higher addresses)

x86 Assembly - Why is [e]bx preserved in calling conventions?

半腔热情 提交于 2019-12-05 10:18:32
I've noticed that a lot of calling conventions insist that [e]bx be preserved for the callee. Now, I can understand why they'd preserve something like [e]sp or [e]bp, since that can mess up the callee's stack. I can also understand why you might want to preserve [e]si or [e]di since that can break the callee's string instructions if they aren't particularly careful. But [e]bx? What on earth is so important about [e]bx? What makes [e]bx so special that multiple calling conventions insist that it be preserved throughout function calls? Is there some sort of subtle bug/gotcha that can arise from

Why did Microsoft choose stdcall as their API convention?

孤人 提交于 2019-12-05 10:00:56
Is there a good reason? Are their internal functions (not exported) also stdcall convention? It was an adaptation to the pascal calling convention for 32-bit code. Pascal was the calling convention for 16-bit operating systems like OS/2 and Windows 3. Why pascal was chosen is a bit of a guess, even I was a small pup back then, but it is slightly more efficient. Which mattered back when 640 KB was all you had to work with. Most Win32 functions aren't true stdcall as it also prescribes how the exported function is decorated before presented to the linker. Like void Mumble(int arg) becomes

When to use calling conventions

☆樱花仙子☆ 提交于 2019-12-05 06:52:54
What are the key factors on using different calling conventions? When does someone know to use specific calling conventions such as __cdecl or __stdcall or __fastcall in different occasions. Examples would be really apprciated. Most of the time you don't need to worry about it. Usually you'll use __cdecl , but only because that's the default in Visual C++. C++ member functions, however, use the __thiscall convention by default in Visual C++ A (rather common) situation where you really have to worry about calling conventions is when you pass callbacks to API functions, like those in the Windows