2013-12-28 01:06:33 +01:00
|
|
|
#region License
|
|
|
|
//
|
|
|
|
// NS.cs
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// Stefanos A. <stapostol@gmail.com>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006-2013 Stefanos Apostolopoulos
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.MacOS
|
|
|
|
{
|
2014-04-26 14:21:26 +02:00
|
|
|
[Flags]
|
|
|
|
enum AddImageFlags
|
|
|
|
{
|
|
|
|
ReturnOnError = 1,
|
|
|
|
WithSearching = 2,
|
|
|
|
ReturnOnlyIfLoaded = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
enum SymbolLookupFlags
|
|
|
|
{
|
|
|
|
Bind = 0,
|
|
|
|
BindNow = 1,
|
|
|
|
BindFully = 2,
|
|
|
|
ReturnOnError = 4
|
|
|
|
}
|
2013-12-28 01:06:33 +01:00
|
|
|
|
|
|
|
internal class NS
|
|
|
|
{
|
|
|
|
const string Library = "libdl.dylib";
|
|
|
|
|
2014-04-26 14:21:26 +02:00
|
|
|
[DllImport(Library, EntryPoint = "NSAddImage")]
|
|
|
|
internal static extern IntPtr AddImage(string s, AddImageFlags flags);
|
|
|
|
[DllImport(Library, EntryPoint = "NSAddressOfSymbol")]
|
|
|
|
internal static extern IntPtr AddressOfSymbol(IntPtr symbol);
|
2013-12-28 01:06:33 +01:00
|
|
|
[DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")]
|
2014-04-26 14:21:26 +02:00
|
|
|
internal static extern bool IsSymbolNameDefined(string s);
|
2013-12-28 01:06:33 +01:00
|
|
|
[DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")]
|
2014-04-26 14:21:26 +02:00
|
|
|
internal static extern bool IsSymbolNameDefined(IntPtr s);
|
2013-12-28 01:06:33 +01:00
|
|
|
[DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")]
|
2014-04-26 14:21:26 +02:00
|
|
|
internal static extern IntPtr LookupAndBindSymbol(string s);
|
2013-12-28 01:06:33 +01:00
|
|
|
[DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")]
|
2014-04-26 14:21:26 +02:00
|
|
|
internal static extern IntPtr LookupAndBindSymbol(IntPtr s);
|
|
|
|
[DllImport(Library, EntryPoint = "NSLookupSymbolInImage")]
|
|
|
|
internal static extern IntPtr LookupSymbolInImage(IntPtr image, IntPtr symbolName, SymbolLookupFlags options);
|
|
|
|
|
|
|
|
// Unfortunately, these are slower even if they are more
|
|
|
|
// portable and simpler to use.
|
2014-04-18 22:30:50 +02:00
|
|
|
[DllImport(Library)]
|
2014-04-26 14:21:26 +02:00
|
|
|
internal static extern IntPtr dlopen(String fileName, int flags);
|
2014-04-18 22:30:50 +02:00
|
|
|
[DllImport(Library)]
|
2014-04-26 14:21:26 +02:00
|
|
|
internal static extern int dlclose(IntPtr handle);
|
|
|
|
[DllImport (Library)]
|
|
|
|
internal static extern IntPtr dlsym (IntPtr handle, string symbol);
|
2014-04-18 22:30:50 +02:00
|
|
|
[DllImport (Library)]
|
2014-04-26 14:21:26 +02:00
|
|
|
internal static extern IntPtr dlsym (IntPtr handle, IntPtr symbol);
|
2013-12-28 01:06:33 +01:00
|
|
|
|
|
|
|
public static IntPtr GetAddress(string function)
|
|
|
|
{
|
|
|
|
// Instead of allocating and combining strings in managed memory
|
|
|
|
// we do that directly in unmanaged memory. This way, we avoid
|
|
|
|
// 2 string allocations every time this function is called.
|
|
|
|
|
|
|
|
// must add a '_' prefix and null-terminate the function name,
|
|
|
|
// hence we allocate +2 bytes
|
|
|
|
IntPtr ptr = Marshal.AllocHGlobal(function.Length + 2);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Marshal.WriteByte(ptr, (byte)'_');
|
|
|
|
for (int i = 0; i < function.Length; i++)
|
|
|
|
{
|
|
|
|
Marshal.WriteByte(ptr, i + 1, (byte)function[i]);
|
|
|
|
}
|
|
|
|
Marshal.WriteByte(ptr, function.Length + 1, 0); // null-terminate
|
|
|
|
|
2014-04-26 14:21:26 +02:00
|
|
|
IntPtr symbol = GetAddressInternal(ptr);
|
2013-12-28 01:06:33 +01:00
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Marshal.FreeHGlobal(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IntPtr GetAddress(IntPtr function)
|
2014-04-26 14:21:26 +02:00
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
const int max = 64;
|
|
|
|
byte* symbol = stackalloc byte[max];
|
|
|
|
byte* ptr = symbol;
|
|
|
|
byte* cur = (byte*)function.ToPointer();
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
*ptr++ = (byte)'_';
|
|
|
|
while (*cur != 0 && ++i < max)
|
|
|
|
{
|
|
|
|
*ptr++ = *cur++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= max - 1)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException(String.Format(
|
|
|
|
"Function {0} is too long. Please report a bug at https://github.com/opentk/issues/issues",
|
|
|
|
Marshal.PtrToStringAnsi(function)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetAddressInternal(new IntPtr(symbol));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntPtr GetAddressInternal(IntPtr function)
|
2013-12-28 01:06:33 +01:00
|
|
|
{
|
|
|
|
IntPtr symbol = IntPtr.Zero;
|
2014-04-26 14:21:26 +02:00
|
|
|
if (IsSymbolNameDefined(function))
|
2013-12-28 01:06:33 +01:00
|
|
|
{
|
2014-04-26 14:21:26 +02:00
|
|
|
symbol = LookupAndBindSymbol(function);
|
2013-12-28 01:06:33 +01:00
|
|
|
if (symbol != IntPtr.Zero)
|
2014-04-26 14:21:26 +02:00
|
|
|
symbol = AddressOfSymbol(symbol);
|
2013-12-28 01:06:33 +01:00
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
2014-04-18 22:30:50 +02:00
|
|
|
|
|
|
|
public static IntPtr GetSymbol(IntPtr handle, string symbol)
|
|
|
|
{
|
|
|
|
return dlsym(handle, symbol);
|
|
|
|
}
|
|
|
|
|
2014-04-26 14:21:26 +02:00
|
|
|
public static IntPtr GetSymbol(IntPtr handle, IntPtr symbol)
|
|
|
|
{
|
|
|
|
return dlsym(handle, symbol);
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:30:50 +02:00
|
|
|
public static IntPtr LoadLibrary(string fileName)
|
|
|
|
{
|
|
|
|
const int RTLD_NOW = 2;
|
|
|
|
return dlopen(fileName, RTLD_NOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void FreeLibrary(IntPtr handle)
|
|
|
|
{
|
|
|
|
dlclose(handle);
|
|
|
|
}
|
2013-12-28 01:06:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|