Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent MSVC from replacing my memset with its memset?

I am building an EFI driver and therefore cannot use stdlib, so no memset. I am also running after ExitBootServices, so no edk2 CopyMem. This is my function:

void Set_Memory(VOID* Dest, UINTN Len, CHAR8 Val)
{
    for (int i = 0; i < Len; ++i)
    {
        ((UINT8*)Dest)[i] = Val;
    }
}

When compiling with optimizations, I get LNK2001 unresolved external symbol memset. Presumably the MSVC compiler is replacing my call to Set_Memory with a call to memset. I also cannot define my own memset, because I get the error C2169 'memset': intrinsic function, cannot be defined. How can I prevent this without losing other optimizations?

like image 741
EatingTechnobladesRemainsAt3am Avatar asked Dec 09 '25 00:12

EatingTechnobladesRemainsAt3am


1 Answers

Use the #pragma optimize("", off) to turn off optimizations for certain functions.

For example:

#pragma optimize("", off)
void Set_Memory(VOID* Dest, UINTN Len, CHAR8 Val)
{
    for (int i = 0; i < Len; ++i)
    {
        ((UINT8*)Dest)[i] = Val;
    }
}
#pragma optimize("", on)

optimize pragma is documented here

like image 102
user20716902 Avatar answered Dec 11 '25 13:12

user20716902