-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Deduplicate minipal header helpers #132113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mdh1418
merged 6 commits into
dotnet:main
from
mdh1418:deduplicate-minipal-header-helpers
Aug 13, 2026
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
806b6e7
System.IO.Compression.Native: link minipal
mdh1418 95a8da5
minipal: add external fallbacks for inline header helpers
mdh1418 1dce0cb
minipal: deduplicate getexepath helper
mdh1418 670f632
minipal: deduplicate inline header helpers
mdh1418 693e79e
minipal: move non-critical helpers out of line
mdh1418 2a23ac8
minipal: use Windows strdup name
mdh1418 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #include "cpuid.h" | ||
|
|
||
| #if defined(HOST_X86) || defined(HOST_AMD64) | ||
| #if defined(HOST_UNIX) | ||
|
|
||
| #if !__has_builtin(__cpuid) | ||
| extern void __cpuid(int cpuInfo[4], int function_id); | ||
| #endif | ||
|
|
||
| #if !__has_builtin(__cpuidex) | ||
| extern void __cpuidex(int cpuInfo[4], int function_id, int subFunction_id); | ||
| #endif | ||
|
|
||
| #endif // HOST_UNIX | ||
| #endif // defined(HOST_X86) || defined(HOST_AMD64) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #include "entrypoints.h" | ||
|
|
||
| extern const void* minipal_resolve_dllimport(const Entry* resolutionTable, size_t tableLength, const char* name); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #include "minipalconfig.h" | ||
| #include "getexepath.h" | ||
|
|
||
| #include <errno.h> | ||
| #include <limits.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #if defined(__APPLE__) | ||
| #include <mach-o/dyld.h> | ||
| #elif defined(__FreeBSD__) | ||
| #include <sys/param.h> | ||
| #include <sys/sysctl.h> | ||
| #include <sys/types.h> | ||
| #elif defined(__OpenBSD__) | ||
| #include <sys/stat.h> | ||
| #include <sys/sysctl.h> | ||
| #include <unistd.h> | ||
| #elif defined(_WIN32) | ||
| #include <windows.h> | ||
| #elif defined(__HAIKU__) | ||
| #include <FindDirectory.h> | ||
| #include <StorageDefs.h> | ||
| #elif HAVE_GETAUXVAL | ||
| #include <sys/auxv.h> | ||
| #endif | ||
|
|
||
| char* minipal_getexepath(void) | ||
| { | ||
| #if defined(__APPLE__) | ||
| uint32_t len = PATH_MAX; | ||
| char pathBuf[PATH_MAX]; | ||
| if (_NSGetExecutablePath(pathBuf, &len) != 0) | ||
| { | ||
| errno = EINVAL; | ||
| return NULL; | ||
| } | ||
|
|
||
| return realpath(pathBuf, NULL); | ||
| #elif defined(__FreeBSD__) | ||
| static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; | ||
| char path[PATH_MAX]; | ||
| size_t len = sizeof(path); | ||
| if (sysctl(name, 4, path, &len, NULL, 0) != 0) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| return strdup(path); | ||
| #elif defined(__OpenBSD__) | ||
| const int name[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; | ||
| size_t len = 0; | ||
| if (sysctl(name, 4, NULL, &len, NULL, 0) != 0 || len == 0) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| char *buf = (char *)malloc(len); | ||
| if (buf == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| if (sysctl(name, 4, buf, &len, NULL, 0) != 0) | ||
| { | ||
| free(buf); | ||
| return NULL; | ||
| } | ||
|
|
||
| // Cast the start of the buffer to access the char * layout safely | ||
| char **argv = (char **)buf; | ||
| const char *exe = argv[0]; | ||
|
|
||
| if (strchr(exe, '/') == NULL) | ||
| { | ||
| const char *p = getenv("PATH"); | ||
| while (p != NULL && *p != '\0') | ||
| { | ||
| size_t seg = strcspn(p, ":"); | ||
| char path[PATH_MAX]; | ||
|
|
||
| if (snprintf(path, sizeof(path), "%.*s/%s", (int)seg, p, exe) < (int)sizeof(path)) | ||
| { | ||
| struct stat sb; | ||
| if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode)) | ||
| { | ||
| char *resolved = realpath(path, NULL); | ||
| free(buf); | ||
| return resolved; | ||
| } | ||
| } | ||
|
|
||
| p += seg; | ||
| if (*p == ':') | ||
| p++; | ||
| } | ||
| } | ||
|
|
||
| char *resolved = realpath(exe, NULL); | ||
| free(buf); | ||
| return resolved; | ||
| #elif defined(__sun) | ||
| const char* path = getexecname(); | ||
| if (path == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| return realpath(path, NULL); | ||
| #elif defined(__HAIKU__) | ||
| char path[B_PATH_NAME_LENGTH]; | ||
| status_t status = find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, path, B_PATH_NAME_LENGTH); | ||
| if (status != B_OK) | ||
| { | ||
| errno = status; | ||
| return NULL; | ||
| } | ||
|
|
||
| return realpath(path, NULL); | ||
| #elif defined(_WIN32) | ||
| char path[MAX_PATH]; | ||
| if (GetModuleFileNameA(NULL, path, MAX_PATH) == 0) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| return strdup(path); | ||
| #elif defined(TARGET_BROWSER) | ||
| const char *browserVirtualAppBase = "/"; // keep in sync other places that define browserVirtualAppBase | ||
| return strdup(browserVirtualAppBase); | ||
| #elif defined(TARGET_WASI) | ||
| // WASI has no /proc, no AT_EXECFN, and argv[0] is unreliable (often "/"). | ||
| // corerun.wasm is launched with the CORE_ROOT env var set to the directory | ||
| // that holds CoreCLR (System.Private.CoreLib.dll and friends). The PAL only | ||
| // needs a path whose dirname is that directory, so synthesize one here. | ||
| const char* coreRoot = getenv("CORE_ROOT"); | ||
| if (coreRoot == NULL || coreRoot[0] == '\0') | ||
| { | ||
| return strdup("/"); | ||
| } | ||
| size_t coreRootLen = strlen(coreRoot); | ||
| const char* suffix = "/corerun"; | ||
| size_t suffixLen = strlen(suffix); | ||
| char* result = (char*)malloc(coreRootLen + suffixLen + 1); | ||
| if (result == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
| memcpy(result, coreRoot, coreRootLen); | ||
| memcpy(result + coreRootLen, suffix, suffixLen + 1); | ||
| return result; | ||
| #else | ||
| #ifdef __linux__ | ||
| const char* symlinkEntrypointExecutable = "/proc/self/exe"; | ||
| #else | ||
| const char* symlinkEntrypointExecutable = "/proc/curproc/exe"; | ||
| #endif | ||
|
|
||
| // Resolve the symlink to the executable from /proc | ||
| char* path = realpath(symlinkEntrypointExecutable, NULL); | ||
| if (path) | ||
| { | ||
| return path; | ||
| } | ||
|
|
||
| #if HAVE_GETAUXVAL && defined(AT_EXECFN) | ||
| // fallback to AT_EXECFN, which does not work properly in rare cases | ||
| // when .NET process is set as interpreter (shebang). | ||
| const char* exePath = (const char *)(getauxval(AT_EXECFN)); | ||
| if (exePath) | ||
| { | ||
| return realpath(exePath, NULL); | ||
| } | ||
| #endif // HAVE_GETAUXVAL && defined(AT_EXECFN) | ||
|
|
||
| return NULL; | ||
| #endif // defined(__APPLE__) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.