int putPixel(int x,int y,uint8_t r,uint8_t g,uint8_t b) {
*(DrawFunc *)0x80000000 = this->draw;
return (*((DrawFunc *)0x80000000 - 0x1000))(x,y,r,g,b);
}
0x80000000에 저장되어있는 "DrawFunc 의 엔트리"에서 메모리 0x1000번지 이전의 주소에 있는 곳의 함수로 점프하고 싶은데 잘 안되네요.
C언어면 할줄 아는데 C++이라 할줄 몰라요.
해결해주시면 케이크 사드리겠습니다.
왜 0x1000 만큼 빼주냐면 ld.lld가 코드를 0x1000만큼 코드를 시프팅하는 아주 나쁜 버릇이 있어서 입니다
인라임 어셈 안 되나 ㅇㅅㅇ
인라인 어셈으로 어떻게 할까요??? ㅎㅎ
(*((DrawFunc *)((char *)0x80000000 - 0x1000)))(x, y, r, g, b); // 이걸로 한 번 해봐라.
main.cpp:61:54: error: called object type 'Screen::DrawFunc' (aka 'int (Screen::*)(int, int, uint8_t, uint8_t, uint8_t)') is not a function or function pointer return (*((DrawFunc *)((char *)0x80000000 - 0x1000)))(x, y, r, g, b);; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ 1 error generated. make: *** [Makefile:20: main.o] Error 1
typedef int (*DrawFunc)(int, int, uint8_t, uint8_t, uint8_t); int putPixel(int x, int y, uint8_t r, uint8_t g, uint8_t b) { // DrawFunc 포인터를 0x80000000 주소에 설정 *(DrawFunc *)0x80000000 = this->draw; // 0x80000000 - 0x1000 바이트 위치의 DrawFunc 호출 DrawFunc targetFunc = *(DrawFunc *)((uintptr_t)0x80000000 - 0x1000); return targetFunc(x, y, r, g, b); }
main.cpp:49:14: error: assigning to 'DrawFunc' (aka 'int (*)(int, int, unsigned char, unsigned char, unsigned char)') from incompatible type 'int (Screen::*)(int, int, uint8_t, uint8_t, uint8_t)' draw = &Screen::paintByRGB; ^~~~~~~~~~~~~~~~~~~ main.cpp:51:14: error: assigning to 'DrawFunc' (aka 'int (*)(int, int, unsigned char, unsigned char, unsigned char)') from incompatible t
너무 어렵습니다
0x80000000에 저장된 4바이트 값에 0x1000을 뺀 함수를 호출해야합니다
*(Screen::*)(int,int,uint8_t,uint8_t,uint8_t)(*(uint8_t *)0x80000000 - 0x1000)(x,y,r,g,b)
auto raw = *reinterpret_cast(0x80000000); // 코드 주소 읽기 auto adj = reinterpret_cast(raw) - 0x1000; // 바이트 단위 보정 auto fn = reinterpret_cast(adj); // 다시 함수 포인터로 int rc = fn(x, y, r, g, b);
아 이거 멤버함수입니다