Why do values change when casting a function pointer? (msvc ver20)

Viewed 73

To test storing a value in a text segment, I wrote the code below. The value of 'foo' and the value of 'v', 'bar' are different when viewed using debugging. Normal functions without '__declspec(naked)' also have their values changed. When debugging memory using the address of 'foo', '12345678' is displayed well. I'd appreciate it if you could tell me why the values change.

edit:
When I checked again, the only thing that the 'foo' value displayed in debugging was different from the value or output of the program. But I still don't know why it's different.

code:

__declspec(naked) void foo() {
    __asm {
        _emit 0x78
        _emit 0x56
        _emit 0x34
        _emit 0x12
    }
}

int main() {
    cout << sizeof(&foo) << '\n';

    void (*bar)() = foo;
    cout << bar << '\n';

    void* v = (void*)(&foo);
    cout <<  v << '\n';

    unsigned* p = (unsigned*)(v);
    cout << p << '\n';

    unsigned a = *p;
    cout << hex <<  a;

    cout << (void(*)())foo;
    cout << foo;
}

enter image description here

output:

4
00EA1357
00EA1357
00EA1357
00EA1357
00EA1357
c74e9

0 Answers
Related