Skip to content

Commit

Permalink
Merge pull request mono#3707 from lateralusX/jlorenss/win-api-family-…
Browse files Browse the repository at this point in the history
…support-libmonoutils

Build libmonoutils under none desktop Windows API family.
  • Loading branch information
lateralusX authored Oct 6, 2016
2 parents 5022f7d + 5233231 commit 390697c
Show file tree
Hide file tree
Showing 26 changed files with 862 additions and 367 deletions.
7 changes: 7 additions & 0 deletions mono/utils/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ monoutils_sources = \
mono-dl-darwin.c \
mono-dl-posix.c \
mono-dl.h \
mono-dl-windows.h \
mono-log-windows.c \
mono-log-common.c \
mono-log-posix.c \
Expand All @@ -40,16 +41,20 @@ monoutils_sources = \
mono-filemap.c \
mono-math.c \
mono-mmap.c \
mono-mmap-windows.c \
mono-mmap.h \
mono-mmap-internals.h \
mono-mmap-windows.h \
mono-os-mutex.h \
mono-coop-mutex.h \
mono-once.h \
mono-lazy-init.h \
mono-networkinterfaces.c \
mono-networkinterfaces.h \
mono-proclib.c \
mono-proclib-windows.c \
mono-proclib.h \
mono-proclib-windows.h \
mono-publib.c \
mono-string.h \
mono-time.c \
Expand Down Expand Up @@ -149,7 +154,9 @@ monoutils_sources = \
networking-windows.c \
networking.h \
mono-rand.c \
mono-rand-windows.c \
mono-rand.h \
mono-rand-windows.h \
memfuncs.c \
memfuncs.h \
parse.c \
Expand Down
4 changes: 4 additions & 0 deletions mono/utils/atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ InterlockedCompareExchange64(volatile gint64 *dest, gint64 exch, gint64 comp)
return(old);
}

#endif
#endif

#if defined(HOST_WIN32) && defined(_MSC_VER)
// Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
void __mono_win32_atomic_lnk4221(void) {}
#endif
3 changes: 0 additions & 3 deletions mono/utils/mono-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,6 @@
#include <direct.h>
#define mkdir(x) _mkdir(x)

/* GCC specific functions aren't available */
#define __builtin_return_address(x) NULL

#define __func__ __FUNCTION__

#include <BaseTsd.h>
Expand Down
44 changes: 44 additions & 0 deletions mono/utils/mono-dl-windows-uwp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* mono-dl-windows-uwp.c: UWP dl support for Mono.
*
* Copyright 2016 Microsoft
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include <config.h>
#include <glib.h>

#if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
#include <Windows.h>
#include "mono/utils/mono-dl-windows.h"

void*
mono_dl_lookup_symbol_in_process (const char *symbol_name)
{
g_unsupported_api ("EnumProcessModules");
SetLastError (ERROR_NOT_SUPPORTED);

return NULL;
}

char*
mono_dl_current_error_string (void)
{
char *ret = NULL;
TCHAR buf [1024];
DWORD code = GetLastError ();

if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, G_N_ELEMENTS(buf) - 1, NULL))
buf[0] = TEXT('\0');

ret = u16to8 (buf);
return ret;
}

#else /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */

#ifdef _MSC_VER
// Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
void __mono_win32_mono_dl_windows_uwp_quiet_lnk4221(void) {}
#endif
#endif /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */
42 changes: 30 additions & 12 deletions mono/utils/mono-dl-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#if defined(HOST_WIN32)

#include "mono/utils/mono-dl.h"
#include "mono/utils/mono-dl-windows.h"
#include "mono/utils/mono-embed.h"
#include "mono/utils/mono-path.h"

Expand All @@ -25,7 +26,6 @@
#include <windows.h>
#include <psapi.h>


const char*
mono_dl_get_so_prefix (void)
{
Expand All @@ -48,14 +48,20 @@ mono_dl_open_file (const char *file, int flags)
gpointer hModule = NULL;
if (file) {
gunichar2* file_utf16 = g_utf8_to_utf16 (file, strlen (file), NULL, NULL, NULL);

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
guint last_sem = SetErrorMode (SEM_FAILCRITICALERRORS);
#endif
guint32 last_error = 0;

hModule = LoadLibrary (file_utf16);
if (!hModule)
last_error = GetLastError ();

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
SetErrorMode (last_sem);
#endif

g_free (file_utf16);

if (!hModule)
Expand All @@ -73,23 +79,15 @@ mono_dl_close_handle (MonoDl *module)
FreeLibrary (module->handle);
}

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
void*
mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
mono_dl_lookup_symbol_in_process (const char *symbol_name)
{
HMODULE *modules;
DWORD buffer_size = sizeof (HMODULE) * 1024;
DWORD needed, i;
gpointer proc = NULL;

/* get the symbol directly from the specified module */
if (!module->main_module)
return GetProcAddress (module->handle, symbol_name);

/* get the symbol from the main module */
proc = GetProcAddress (module->handle, symbol_name);
if (proc != NULL)
return proc;

/* get the symbol from the loaded DLLs */
modules = (HMODULE *) g_malloc (buffer_size);
if (modules == NULL)
Expand Down Expand Up @@ -129,13 +127,33 @@ mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
g_free (modules);
return NULL;
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */

void*
mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
{
gpointer proc = NULL;

/* get the symbol directly from the specified module */
if (!module->main_module)
return GetProcAddress (module->handle, symbol_name);

/* get the symbol from the main module */
proc = GetProcAddress (module->handle, symbol_name);
if (proc != NULL)
return proc;

/* get the symbol from the loaded DLLs */
return mono_dl_lookup_symbol_in_process (symbol_name);
}

int
mono_dl_convert_flags (int flags)
{
return 0;
}

#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
char*
mono_dl_current_error_string (void)
{
Expand All @@ -153,6 +171,7 @@ mono_dl_current_error_string (void)
}
return ret;
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */

int
mono_dl_get_executable_path (char *buf, int buflen)
Expand All @@ -165,5 +184,4 @@ mono_dl_get_system_dir (void)
{
return NULL;
}

#endif
14 changes: 14 additions & 0 deletions mono/utils/mono-dl-windows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __MONO_UTILS_DL_WINDOWS_H__
#define __MONO_UTILS_DL_WINDOWS_H__

#include <config.h>
#include <glib.h>

#ifdef HOST_WIN32
#include "mono/utils/mono-dl.h"

void*
mono_dl_lookup_symbol_in_process (const char *symbol_name);
#endif /* HOST_WIN32 */
#endif /* __MONO_UTILS_DL_WINDOWS_H__ */

7 changes: 7 additions & 0 deletions mono/utils/mono-io-portability.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,11 @@ static inline gchar *mono_portability_find_file_internal (GString **report, cons
g_free (new_pathname);
return(NULL);
}

#else /* DISABLE_PORTABILITY */

#ifdef _MSC_VER
// Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
void __mono_win32_mono_io_portability_quiet_lnk4221(void) {}
#endif
#endif /* DISABLE_PORTABILITY */
3 changes: 2 additions & 1 deletion mono/utils/mono-log-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <process.h>
#endif
#include "mono-logger-internals.h"
#include "mono-proclib.h"

static FILE *logFile = NULL;
static void *logUserData = NULL;
Expand Down Expand Up @@ -119,7 +120,7 @@ mono_log_write_logfile (const char *log_domain, GLogLevelFlags level, mono_bool
struct tm *tod;
time(&t);
tod = localtime(&t);
pid = _getpid();
pid = mono_process_current_pid ();
strftime(logTime, sizeof(logTime), "%F %T", tod);
#endif
fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
Expand Down
5 changes: 3 additions & 2 deletions mono/utils/mono-log-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <time.h>
#include <process.h>
#include "mono-logger-internals.h"
#include "mono-proclib.h"

static FILE *logFile = NULL;
static void *logUserData = NULL;
Expand Down Expand Up @@ -88,7 +89,7 @@ void
mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *message)
{
time_t t;
pid_t pid;
int pid;
char logTime [80];

if (logFile == NULL)
Expand All @@ -97,7 +98,7 @@ mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, c
struct tm *tod;
time(&t);
tod = localtime(&t);
pid = _getpid();
pid = mono_process_current_pid ();
strftime(logTime, sizeof(logTime), "%F %T", tod);

fprintf (logFile, "%s level[%c] mono[%d]: %s\n", logTime, mapLogFileLevel (level), pid, message);
Expand Down
2 changes: 1 addition & 1 deletion mono/utils/mono-logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mono_trace_set_mask_string (const char *value)
continue;
}
for (i = 0; valid_flags[i]; i++) {
int len = strlen (valid_flags[i]);
size_t len = strlen (valid_flags[i]);
if (strncmp (tok, valid_flags[i], len) == 0 && (tok[len] == 0 || tok[len] == ',')) {
flags |= valid_masks[i];
tok += len;
Expand Down
12 changes: 11 additions & 1 deletion mono/utils/mono-mmap-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@

#include "mono-compiler.h"

int mono_pages_not_faulted (void *addr, size_t length);
void *
malloc_shared_area (int pid);

char*
aligned_address (char *mem, size_t size, size_t alignment);

void
account_mem (MonoMemAccountType type, ssize_t size);

int
mono_pages_not_faulted (void *addr, size_t length);

#endif /* __MONO_UTILS_MMAP_INTERNAL_H__ */

57 changes: 57 additions & 0 deletions mono/utils/mono-mmap-windows-uwp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* mono-dl-windows-uwp.c: UWP dl support for Mono.
*
* Copyright 2016 Microsoft
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include <config.h>
#include <glib.h>

#if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
#include <Windows.h>
#include <mono/utils/mono-mmap-windows.h>

void*
mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
{
void *ptr;
int mflags = 0;
HANDLE file, mapping;
int prot = mono_mmap_win_prot_from_flags (flags);

mflags = FILE_MAP_READ;
if (flags & MONO_MMAP_WRITE)
mflags = FILE_MAP_COPY;

file = (HANDLE) _get_osfhandle (fd);
mapping = CreateFileMappingFromApp (file, NULL, prot, length, NULL);

if (mapping == NULL)
return NULL;

ptr = MapViewOfFileFromApp (mapping, mflags, offset, length);

if (ptr == NULL) {
CloseHandle (mapping);
return NULL;
}

*ret_handle = (void*)mapping;
return ptr;
}

int
mono_file_unmap (void *addr, void *handle)
{
UnmapViewOfFile (addr);
CloseHandle ((HANDLE)handle);
return 0;
}

#else /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */

#ifdef _MSC_VER
// Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
void __mono_win32_mono_mmap_windows_uwp_quiet_lnk4221(void) {}
#endif
#endif /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */
Loading

0 comments on commit 390697c

Please sign in to comment.