'X86_64 call convention issue
As we know, X86_64 use register rdi, rsi, rdx, rcx, r8, r9 to store normal function's arguments, and use stack memory to store large argument, and use xmm to store float and double argument. But in my code, the function 'myuprobe_sum_dww_ptr' is wired. It doesn't use rdi to store the first argument but to store a local variable. Please see the code below, and I have commented the register usage information in the code. Could anyone help to explain?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
struct double_wraper
{
double data;
};
struct double_wraper_wraper
{
double_wraper_wraper()
{
dp = new double_wraper();
}
~double_wraper_wraper()
{
delete dp;
}
uint32_t a;
struct double_wraper d;
struct double_wraper* dp;
};
// a -> rdi, b -> rsi
uint32_t myuprobe_sum_int(uint32_t a, uint32_t b)
{
//printf("%u + %u = %u, addr = %p\n", a, b, &a);
return a + b;
}
// d1 -> rsi, d2 -> rdx, v -> rcx
double_wraper_wraper myuprobe_sum_dww_ptr(const double_wraper_wraper* d1, const double_wraper_wraper* d2, uint32_t v)
{
double_wraper_wraper d3; // d3 -> rdi, why??????????????
d3.a = d1->a + d2->a + v;
d3.d.data = d1->d.data + d2->d.data;
d3.dp->data = d1->dp->data + d2->dp->data;
//printf("%u + %u = %u, addr = %p\n", d1->a, d2->a, d3.a, d1);
//printf("%lf + %lf = %lf, addr = %p\n", d1->d.data, d2->d.data, d3.d.data, d1);
printf("%lf + %lf = %lf, addr = %p\n", d1->dp->data, d2->dp->data, d3.dp->data, d1);
return d3;
}
// d1 -> rdi, d2 -> rsi, d3 -> rdx, v -> rcx
double_wraper myuprobe_sum_dw_ptr(const double_wraper* d1, const double_wraper* d2, const double_wraper_wraper* d3, uint32_t v)
{
double_wraper d4;
d4.data = d1->data + d2->data + d3->d.data;
//printf("%lf + %lf + %lf = %lf, addr = %p\n", d1->data, d2->data, d3->d.data, d4.data, d1);
return d4;
}
int main()
{
while(1) {
double_wraper_wraper d4, d5;
d4.a = rand();
d4.d.data = rand() + (double)rand() / RAND_MAX;
d4.dp->data = rand() + (double)rand() / RAND_MAX;
d5.a = rand();
d5.d.data = rand() + (double)rand() / RAND_MAX;
d5.dp->data = rand() + (double)rand() / RAND_MAX;
auto d7 = myuprobe_sum_dww_ptr(&d4, &d5, 100);
uint32_t a,b;
a = rand();
b = rand();
auto c = myuprobe_sum_int(a, b);
double_wraper d8,d9;
d8.data = rand() + (double)rand() / RAND_MAX;
d9.data = rand() + (double)rand() / RAND_MAX;
auto d10 = myuprobe_sum_dw_ptr(&d8, &d9, &d4, 100);
usleep(5000000);
}
}
Compile: g++ test.cpp -o test -g -O0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
