calling-convention

x86 calling convention: should arguments passed by stack be read-only?

五迷三道 提交于 2019-12-03 11:36:10
问题 It seems state-of-art compilers treat arguments passed by stack as read-only. Note that in the x86 calling convention, the caller pushes arguments onto the stack and the callee uses the arguments in the stack. For example, the following C code: extern int goo(int *x); int foo(int x, int y) { goo(&x); return x; } is compiled by clang -O3 -c g.c -S -m32 in OS X 10.10 into: .section __TEXT,__text,regular,pure_instructions .macosx_version_min 10, 10 .globl _foo .align 4, 0x90 _foo: ## @foo ## BB

Is `extern “C”` a part of the type of a function?

不问归期 提交于 2019-12-03 10:53:45
I don't see any comment in the standard except linkage related things. Though the standard doesn't say anything about calling convention, the calling conventions might be different between C and C++ in the real world, so I expected that the types of a C function and a C++ function are different. But it seems not, especially in GCC. #include <type_traits> extern "C" { int c_func(int); } int cpp_func(int); static_assert(!std::is_same<decltype(c_func), decltype(cpp_func)>::value, "It should not be the same type"); static_assert fails since GCC considers those functions have the same type. Is

Question about Objective C calling convention and argument passing on ARM

你离开我真会死。 提交于 2019-12-03 07:29:47
I want to know how objective C runtime handle arguments when I call a objective C method like [NSString stringWithFomat:@"%@, %@", @"Hello", @"World"] There are three arguments for this objective C call, how does it work compared to typical way on a ARM system. I have known register r0, r1, r2, r3 will hold first 4 arguments, how about there are additional arguments? How does it put them on a stack and pop them later? For functions that returns a simple type: r0 = self (NSString) r1 = _cmd (@selector(stringWithFormat:)) r2 = 1st argument (@"%@, %@") r3 = 2nd argument (@"Hello") then the rest

Why can I invoke a function via a pointer with too many arguments?

萝らか妹 提交于 2019-12-03 06:49:55
问题 Say I have this function: int func2() { printf("func2\n"); return 0; } Now I declare a pointer: int (*fp)(double); This should point to a function that takes a double argument and returns an int . func2 does NOT have any argument, but still when I write: fp = func2; fp(2); (with 2 being just an arbitrary number), func2` is invoked correctly. Why is that? Is there no meaning to the number of parameters I declare for a function pointer? 回答1: Yes, there is a meaning. In C (but not in C++), a

x86_64 calling conventions and stack frames

无人久伴 提交于 2019-12-03 05:35:06
I am trying to make sense out of the executable code that GCC (4.4.3) is generating for an x86_64 machine running under Ubuntu Linux. In particular, I don't understand how the code keeps track of stack frames. In the old days, in 32-bit code, I was accustomed to seeing this "prologue" in just about every function: push %ebp movl %esp, %ebp Then, at the end of the function, there would come an "epilogue," either sub $xx, %esp # Where xx is a number based on GCC's accounting. pop %ebp ret or simply leave ret which accomplishes the same thing: Set the Stack Pointer to the top of the current frame

x86 calling convention: should arguments passed by stack be read-only?

拈花ヽ惹草 提交于 2019-12-03 03:01:11
It seems state-of-art compilers treat arguments passed by stack as read-only. Note that in the x86 calling convention, the caller pushes arguments onto the stack and the callee uses the arguments in the stack. For example, the following C code: extern int goo(int *x); int foo(int x, int y) { goo(&x); return x; } is compiled by clang -O3 -c g.c -S -m32 in OS X 10.10 into: .section __TEXT,__text,regular,pure_instructions .macosx_version_min 10, 10 .globl _foo .align 4, 0x90 _foo: ## @foo ## BB#0: pushl %ebp movl %esp, %ebp subl $8, %esp movl 8(%ebp), %eax movl %eax, -4(%ebp) leal -4(%ebp), %eax

Why can a T* be passed in register, but a unique_ptr<T> cannot?

*爱你&永不变心* 提交于 2019-12-03 01:25:27
问题 I'm watching Chandler Carruth's talk in CppCon 2019: There are no Zero-Cost Abstractions in it, he gives the example of how he was surprised by just how much overhead you incur by using an std::unique_ptr<int> over an int* ; that segment starts about at time point 17:25. You can have a look at the compilation results of his example pair-of-snippets (godbolt.org) - to witness that, indeed, it seems the compiler is not willing to pass the unique_ptr value - which in fact in the bottom line is

Why can I invoke a function via a pointer with too many arguments?

折月煮酒 提交于 2019-12-02 20:27:49
Say I have this function: int func2() { printf("func2\n"); return 0; } Now I declare a pointer: int (*fp)(double); This should point to a function that takes a double argument and returns an int . func2 does NOT have any argument, but still when I write: fp = func2; fp(2); (with 2 being just an arbitrary number), func2` is invoked correctly. Why is that? Is there no meaning to the number of parameters I declare for a function pointer? Yes, there is a meaning. In C (but not in C++), a function declared with an empty set of parentheses means it takes an unspecified number of parameters. When you

Why “mov rcx, rax” is required when calling printf in x64 assembler?

牧云@^-^@ 提交于 2019-12-02 17:04:10
问题 I am trying to learn x64 assembler. I wrote "hello world" and tried to call printf using the following code: EXTERN printf: PROC PUBLIC hello_world_asm .data hello_msg db "Hello world", 0 .code hello_world_asm PROC push rbp ; save frame pointer mov rbp, rsp ; fix stack pointer sub rsp, 8 * (4 + 2) ; shadow space (32bytes) lea rax, offset hello_msg mov rcx, rax ; <---- QUESTION ABOUT THIS LINE call printf ; epilog. restore stack pointer mov rsp, rbp pop rbp ret hello_world_asm ENDP END At the

X86-64 NASM calling extern c functions

我是研究僧i 提交于 2019-12-02 13:59:11
Im very new to assembly but know a bit of c. Im playing around with extern function calls like extern _printf str db "Hello", 0 push str call _printf but cant find any tutorials using extern functions except scanf and printf. For example strcmp? How can i call strcmp in my case? Micrified Here is my answer. It is specific to x86-64 though. Please know that when pushing arguments to a function, you usually place the first 6 in registers rdi , rsi , rdx , rcx , r8 , and r9 . The rest get pushed to the stack. The specification for this is called the System V ABI (Note that Windows uses a