How to implement fold expression in case of extern "C"

Viewed 80

I am a beginner in red team development.Trying to implement hellsgate in C++. Simply put, in this program, the C program directly calls the function from the asm file.

The function in asm file:

    HellDescent PROC
        mov r10, rcx
        mov eax, wSystemCall
        syscall
        ret
    HellDescent ENDP

The function in C:

//define
extern HellDescent();
//call
HellDescent(-1,...);

In C++, this gives the error: “Too many arguments to function.”After referring to the material, I found the way of Fold expressions. When I used the method in the demo to create the function.


template <class... Args>
extern "C" int HellDescent(Args&&... args){}

Visual Studio prompted me: “linkage-specification-is-not-allowed."

I tried to search for information to solve but no results. Can anyone tell how to fix it.

1 Answers

In C++ extern HellDescent(); declares a function with no arguments, regardless of linkage type. extern "C" doesn't change rules of language as a popular belief states, it only changes way how names are represented. There is no "function with undeclared count of arguments" in C++. You have to declare possible overloads and ditch "C" linkage as it doesn't support overloads. The way how you had defined template it isn't compatible with "C" calls because it would require decoration of names, aka name mangling, to resolve overloads.

It's unclear what are use cases here, but it might be possible to create some adapter, a C++ function which would call asm-function if there is a limited set arguments or refactor whole thing to take a data structure containing all arguments in portable form. C++ is a high-level language and assembler approach of "call whatever address we can and feed it whatever we want as arguments" doesn't work in it because of vast variety of implementations supported.

Related