-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinuxInterop.cs
64 lines (56 loc) · 2.52 KB
/
LinuxInterop.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace VaListInterop
{
using System;
using System.Runtime.InteropServices;
public static class LinuxInterop
{
[DllImport("libdl", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dlsym(IntPtr handle, string symbol);
[DllImport("libdl", CallingConvention = CallingConvention.Cdecl)]
public static extern bool dlclose(IntPtr handle);
public const int RTLD_LAZY = 1;
[DllImport("libdl", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dlopen(string lpFileName, int flags);
[DllImport("libdl", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern string dlerror();
/// <summary>
/// Format a string using printf style markers
/// </summary>
/// <remarks>
/// See https://stackoverflow.com/a/37629480/2663813
/// </remarks>
/// <param name="buffer">The output buffer (should be large enough, use _vscprintf)</param>
/// <param name="format">The message format</param>
/// <param name="args">The variable arguments list pointer. We do not know what it is, but the pointer must be given as-is from C back to sprintf.</param>
/// <returns>A negative value on failure, the number of characters written otherwise.</returns>
[DllImport("libc", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int vsprintf(
IntPtr buffer,
[In][MarshalAs(UnmanagedType.LPStr)] string format,
IntPtr args);
/// <summary>
/// Compute the size required by vsprintf to print the parameters.
/// </summary>
/// <param name="format"></param>
/// <param name="ptr"></param>
/// <returns></returns>
[DllImport("libc", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int vsnprintf(
IntPtr buffer,
UIntPtr size,
[In][MarshalAs(UnmanagedType.LPStr)] string format,
IntPtr args);
}
/// <summary>
/// The va_list structure of linux x64
/// https://www.uclibc.org/docs/psABI-x86_64.pdf page 52
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct VaListLinuxX64
{
private UInt32 gp_offset;
private UInt32 fp_offset;
private IntPtr overflow_arg_area;
private IntPtr reg_save_area;
}
}