Skip to content

Commit 4748e66

Browse files
committed
cpuid.c: implement DllMain replacement from build/win64/dll.c.
1 parent 4eb0d7c commit 4748e66

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/cpuid.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ static int __blst_cpuid(void)
4747
return 0;
4848
}
4949

50-
# if defined(_MSC_VER) && !defined(__clang__)
50+
# if defined(_MSC_VER) && !defined(__clang__) && !defined(__BLST_DLL_MAIN__)
5151
# pragma section(".CRT$XCU",read)
5252
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = __blst_cpuid;
5353
# elif defined(__SUNPRO_C)
5454
# pragma init(__blst_cpuid)
5555
# endif
5656

57-
#elif defined(__aarch64__) || defined(__aarch64) || defined(_M_ARM64)
57+
#elif defined(__aarch64__) || defined(__aarch64) || defined(_M_ARM64) || defined(_M_ARM64EC)
5858

5959
# if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
6060
extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
@@ -105,10 +105,56 @@ static int __blst_cpuid(void)
105105
return 0;
106106
}
107107

108-
# if defined(_MSC_VER) && !defined(__clang__)
108+
# if defined(_MSC_VER) && !defined(__clang__) && !defined(__BLST_DLL_MAIN__)
109109
# pragma section(".CRT$XCU",read)
110110
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = __blst_cpuid;
111111
# endif
112112
# endif
113113

114114
#endif
115+
116+
#if defined(_WIN64) && defined(__BLST_DLL_MAIN__)
117+
# define IsProcessorFeaturePresent mask_IsProcessorFeaturePresent
118+
# define WIN32_LEAN_AND_MEAN
119+
# include <windows.h>
120+
121+
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
122+
{
123+
if (dwReason == DLL_PROCESS_ATTACH) {
124+
DisableThreadLibraryCalls(hinstDLL);
125+
__blst_cpuid();
126+
}
127+
128+
return TRUE;
129+
130+
(void)lpvReserved;
131+
}
132+
133+
# if defined(_MSC_VER)
134+
/*
135+
* Even though we don't have memcpy/memset anywhere, MSVC compiler
136+
* generates calls to them as it recognizes corresponding patterns.
137+
*/
138+
#pragma function(memcpy)
139+
void *memcpy(unsigned char *dst, const unsigned char *src, size_t n)
140+
{
141+
void *ret = dst;
142+
143+
while(n--)
144+
*dst++ = *src++;
145+
146+
return ret;
147+
}
148+
149+
#pragma function(memset)
150+
void *memset(unsigned char *dst, int c, size_t n)
151+
{
152+
void *ret = dst;
153+
154+
while(n--)
155+
*dst++ = (unsigned char)c;
156+
157+
return ret;
158+
}
159+
# endif
160+
#endif

0 commit comments

Comments
 (0)