NASM call for external C++ function

前端 未结 1 879
野性不改
野性不改 2021-01-14 18:46

I am trying to call external C++ function from NASM. As I was searching on google I did not find any related solution.
C++

void kerne         


        
1条回答
  •  别那么骄傲
    2021-01-14 19:20

    There is no standardized method of calling C++ functions from assembly, as of now. This is due to a feature called name-mangling. The C++ compiler toolchain does not emit symbols with the names exactly written in the code. Therefore, you don't know what the name will be for the symbol representing the function coded with the name kernel_main or kernelMain, whatever.

    Why is name-mangling required?

    You can declare multiple entities (classes, functions, methods, namespaces, etc.) with the same name in C++, but under different parent namespaces. This causes symbol conflicts if two entities with the name local name (e.g. local name of class SomeContainer in namespace SymbolDomain is SomeContainer but global name is SymbolDomain::SomeContainer, atleast to talk in this answer, okay) have the same symbol name.

    Conflicts also occur with method overloading, therefore, the types of each argument are also emitted (in some form) for methods of classes. To cope with this, the C++ toolchain will somehow mangle the actual names in the ELF binary object.

    So, can't I use the C++ mangled name in assembly?

    Yes, this is one solution. You can use readelf -s fileName with the object-file for kernel_main. You'll have to search for a symbol having some similarity with kernel_main. Once you think you got it, then confirm that with echo _ZnSymbolName | c++filt which should output kernel_main.

    You use this name in assembly instead of kernel_main.

    The problem with this solution is that, if for some reason, you change the arguments, return value, or anything else (we don't know what affects name-mangling), your assembly code may break. Therefore, you have to be careful about this. On the other hand, this is not a good practice, as your going into non-standard stuff.

    Note that name-mangling is not standardized, and varies from toolchain to toolchain. By depending on it, your sticking to the same compiler too.

    Can't I do something standardized?

    Yep. You could use a C function in C++ by declaring the function extern "C" like this

    extern "C" void kernelMain(void);
    

    This is the best solution in your case, as your kernel_main is already a C-style function with no parent class and namespace. Note that, the C function is written in C++ and still uses C++ features (internally).

    Other solutions include using a macro indirection, where a C function calls the C++ function, if you really need to. Something like this -

    ///
    /// Simple class containing a method to illustrate the concept of
    /// indirection.
    ///
    class SomeContainer
    {
    public:
         int execute(int y)
         {
    
         }
    }
    
    #define _SepArg_ , // Comma macro, to pass into args, comma not used directly
    
    ///
    /// Indirection for methods having return values and arguments (other than
    /// this). For methods returning void or having no arguments, make something
    /// similar).
    ///
    #define _Generate_Indirection_RetEArgs(ret, name, ThisType, thisArg, eargs) \
    extern "C" ret name ( ThisType thisArg, eargs ) \
    {                                     \
        return thisArg -> name ( eargs );         \
    }                                     \
    
    _Generate_Indirection_RetEArgs(int, execute, SomeContainer, x, int y);
    

    0 讨论(0)
提交回复
热议问题