Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)
(시리즈 글이 6개 있습니다.)
개발 환경 구성: 300. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법
; https://www.sysnet.pe.kr/2/0/11052

.NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/11884

개발 환경 구성: 466. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 세 번째 이야기
; https://www.sysnet.pe.kr/2/0/12118

.NET Framework: 878. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 네 번째 이야기(IL 코드로 직접 구현)
; https://www.sysnet.pe.kr/2/0/12120

.NET Framework: 880. C# - PE 파일로부터 IMAGE_COR20_HEADER 및 VTableFixups 테이블 분석
; https://www.sysnet.pe.kr/2/0/12126

.NET Framework: 881. C# DLL에서 제공하는 Win32 export 함수의 내부 동작 방식(VT Fix up Table)
; https://www.sysnet.pe.kr/2/0/12127




C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 네 번째 이야기(IL 코드로 직접 구현)

아래의 글을 쓰다 보니,

C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법
; https://www.sysnet.pe.kr/2/0/11052

C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/11884

C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 세 번째 이야기
; https://www.sysnet.pe.kr/2/0/12118

원론적인 이야기가 궁금해졌습니다. 찾아보면, IL 코드 수준에서 export하는 방법을 다음의 글에서 소개하고 있습니다.

Unmanaged code can wrap managed methods
; https://www.codeproject.com/Articles/8124/Unmanaged-code-can-wrap-managed-methods

그리고 저 글을 쓴 사람은 Inside IL Assembler 책에서 단서를 얻었다고 하는데 실제로 그 책에 실린 "Figure 18-3. Indirect referencing of v-table entries from the EAT"가 그 구조를 담고 있습니다.

[출처: https://books.google.co.kr/books?id=Xv_0AwAAQBAJ&pg=PA353]
il_export_1.png

(0x25ff는 절대 주소로의 JMP 구문인데 그것이 v-table의 MethodDef 값을 참조한다는 것이 직관적이지는 않습니다. 그냥 전체적인 동작 의미로 유추해 해석하면 될 듯합니다.)




일단, IL assembler 2.0 규격으로 export 함수를 구현하는 방법을 먼저 살펴보겠습니다. 예를 들기 위해 C#으로 다음과 같은 소스 코드를,

using System;

public class Class1
{
    public static void Func1() // export할 함수 1
    {
        Console.WriteLine("Func1");
        return;
    }

    public static int Func2() // export할 함수 2
    {
        return 5;
    }

    public static void Func3(int n)  // export할 함수 3
    {
        Console.WriteLine(n);
    }
}

빌드해 DLL을 얻고, 그것을 다시 ildasm.exe를 통해 IL 코드로 번역합니다.

ildasm ClassLibrary1.dll /out=ClassLibrary1.il

그럼 다음과 같은 출력 결과를 얻게 되는데,


// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly ClassLibrary1
{
  // ...[생략]...
  .hash algorithm 0x00008004
  .ver 1:0:0:0
}

.module ClassLibrary1.dll
// MVID: {16FD8023-BE13-4D5D-8491-55801E2D5C90}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x06B90000

// =============== CLASS MEMBERS DECLARATION ===================

.class public abstract auto ansi sealed beforefieldinit Class1
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Func1() cil managed
  {
    // Code size       15 (0xf)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      "Func1"
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  br.s       IL_000e

    IL_000e:  ret
  } // end of method Class1::Func1

  .method public hidebysig static int32  Func2() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  1
    .locals init ([0] int32 V_0)
    IL_0000:  nop
    IL_0001:  ldc.i4.5
    IL_0002:  stloc.0
    IL_0003:  br.s       IL_0005

    IL_0005:  ldloc.0
    IL_0006:  ret
  } // end of method Class1::Func2

  .method public hidebysig static void  Func3(int32 n) cil managed
  {
    // Code size       9 (0x9)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  call       void [mscorlib]System.Console::WriteLine(int32)
    IL_0007:  nop
    IL_0008:  ret
  } // end of method Class1::Func3

} // end of class Class1

export할 메서드에 대해 단순히 다음과 같이 IL 언어로 표시만 해주면 됩니다.

.method public hidebysig static void  Func1() cil managed
  {
    // Code size       15 (0xf)
    .export [1] // 1 == ordinal number
    .maxstack  8
    IL_0000:  nop
    ...[생략]...

    IL_000e:  ret
  } // end of method Class1::Func1

  .method public hidebysig static int32  Func2() cil managed
  {
    // Code size       7 (0x7)
    .export [2] // 2 == ordinal number
    .maxstack  1
    .locals init ([0] int32 V_0)
    IL_0000:  nop
    ...[생략]...
    IL_0006:  ret
  } // end of method Class1::Func2

  .method public hidebysig static void  Func3(int32 n) cil managed
  {
    // Code size       9 (0x9)
    .export [3] // 3 == ordinal number
    .maxstack  8
    IL_0000:  nop
    ...[생략]...
    IL_0008:  ret
  } // end of method Class1::Func3

그다음 IL 언어로만 구성되었다는 표시를 제거하는데,

.corflags 0x00000001    //  ILONLY

32비트 assembly를 생성하는 경우에는 2로, 64비트 assembly를 생성하는 경우에는 0으로 지정합니다.

// 32bit
.corflags 0x00000002

// 64bit
.corflags 0x00000000

끝입니다. 이렇게만 바꾸고 ilasm.exe로 빌드하면,

// 32bit
ilasm ClassLibrary1.il /RESOURCE=ClassLibrary1.res /DLL

// 64 bit
ilasm ClassLibrary1.il /RESOURCE=ClassLibrary1.res /DLL /X64

"Figure 18-3. Indirect referencing of v-table entries from the EAT"에서 보여준 V-Table, VT Fixup Table은 모두 자동으로 생성되고 ".export [<ordinal>] as <export_name>"으로 명시한 부분이 PE 포맷의 IMAGE_EXPORT_DIRECTORY에 기록됩니다. 자, 그럼 마저 확인을 해봐야겠지요. ^^ ilasm.exe로 다시 빌드하고,

C:\temp> ilasm ClassLibrary1.il /RESOURCE=ClassLibrary1.res /DLL /X64

64 bit target must be specified for machine type /ITANIUM or /X64. Target set to 64 bit.

Microsoft (R) .NET Framework IL Assembler.  Version 4.8.3752.0
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembling 'ClassLibrary1.il'  to DLL --> 'ClassLibrary1.dll'
Source file is ANSI

Assembled method Class1::Func1
Assembled method Class1::Func2
Assembled method Class1::Func3
Creating PE file

Emitting classes:
Class 1:        Class1

Emitting fields and methods:
Global
Class 1 Methods: 3;

Emitting events and properties:
Global
Class 1
Writing PE file
Operation completed successfully

dumpbin.exe 도구를 이용해 ilasm.exe가 출력한 DLL을 조사해 보면,

C:\temp> dumpbin /EXPORTS ClassLibrary1.dll
Microsoft (R) COFF/PE Dumper Version 14.24.28314.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file ClassLibrary1.dll

File Type: DLL

  Section contains the following exports for ClassLibrary1.dll

    00000000 characteristics
    5E23CEF5 time date stamp Sun Jan 19 12:37:25 2020
        0.00 version
           1 ordinal base
           3 number of functions
           3 number of names

    ordinal hint RVA      name

          1    0 00002622 Func1
          2    1 0000262E Func2
          3    2 0000263A Func3

  Summary

        2000 .reloc
        2000 .rsrc
        2000 .sdata
        2000 .text

Func1, Func2, Func3이 export되었고 이와 함께 전에는 없던 (_MEM_WRITE 속성이 있는) ".sdata" 섹션이 생긴 것을 확인할 수 있습니다. (.export에 의해 생성되는 EAT와 자동으로 구성되는 V-Table이 .sdata 섹션에 위치하고, VT Fix up Table은 .text 섹션에 위치한다고 합니다.)

export한 함수가 정상적으로 동작하는지, 또 다른 Console 응용 프로그램으로 DllImport를 이용해 호출까지 하는 것으로 확인을 완료할 수 있습니다.

using System;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("ClassLibrary1.dll")]
    public static extern void Func1();

    [DllImport("ClassLibrary1.dll")]
    public static extern int Func2();

    [DllImport("ClassLibrary1.dll")]
    public static extern void Func3(int n);

    static void Main(string[] args)
    {
        Func1();
        Console.WriteLine(Func2());
        Func3(3);
    }
}

/* 출력 결과
Func1
5
3
*/




그러니까, 결국 특정 DLL에 정의된 메서드를 export하고 싶다면 1) 그 메서드를 식별할 수 있어야 하고 2) IL disasm의 결과에서 .corflags와 .export를 재정의/삽입한 다음 3) 다시 ilasm시키면 됩니다.

그래서 다음의 글에 실린 ExportDll.exe 도구는,

How to Automate Exporting .NET Function to Unmanaged Programs
; https://www.codeproject.com/Articles/16310/How-to-Automate-Exporting-NET-Function-to-Unmanage

ildasm.exe로 얻은 il 코드에서 ExportDllAttribute가 설정된 메서드를 "텍스트"로써 찾은 다음 "// Code"라는 주석을 기준으로 ".export ..." 항목을 심어 다시 ilasm.exe하는 단순한 역할을 합니다. 즉, 복잡한 것은 ildasm.exe/ilasm.exe가 모두 대신하고 있는 것입니다.

정리하면, ExportDLL.exe의 IL 코드 변환 부분만 아주 쉽게 재작성해 보면 다음과 같은 정도로 나타낼 수 있는 것입니다.

using System.IO;
using System.Text;

namespace MakeExport
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = args[0];
            string targetPlatform = args[1];

            int index = 1;
            StringBuilder sb = new StringBuilder();

            foreach (string line in File.ReadAllLines(filePath))
            {
                if (line.IndexOf("// Code size") == -1)
                {
                    sb.AppendLine(line);
                }
                else if (line.IndexOf(".corflags") != -1)
                {
                    if (targetPlatform == "x64")
                    {
                        sb.AppendLine(".corflags 0x00000000");
                    }
                    else
                    {
                        sb.AppendLine(".corflags 0x00000002");
                    }
                }
                else
                {
                    sb.AppendLine(line);
                    sb.AppendLine($".export[{index}]");
                    index++;
                }
            }

            File.WriteAllText(filePath, sb.ToString());
        }
    }
}

그래서 이것을 적절하게 batch 스크립트와 함께 작성해 주면,

SET BUILDCONFIG=Debug
SET DLLNAME=ClassLibrary1
SET TARGETPLATFORM=x64

ildasm %DLLNAME%.dll /out=%DLLNAME%.il

MakeExport.exe %DLLNAME%.il %TARGETPLATFORM%

if '%TARGETPLATFORM%' == 'x64' (
    ilasm %DLLNAME%.il /RESOURCE=%DLLNAME%.res /DLL /X64
) else (
    ilasm %DLLNAME%.il /RESOURCE=%DLLNAME%.res /DLL
)

나름의 ExportDll.exe 역할을 하게 됩니다. ^^

(첨부 파일은 위의 예제를 모아두었고, 빌드한 다음 ClassLibrary1.csproj가 있는 폴더의 il_export.bat를 "Developer Command Prompt for VS 2019" 명령행에서 실행하면 됩니다.)




기왕 알아본 김에 조금 더 내려가보겠습니다. IL assembler 2.0 규격으로 ".export"를 지정하면 V-Table, VT Fixup Table을 자동으로 구성해 준다고 했는데요. 당연히 수작업으로 구성할 수도 있습니다.

VT Fixup Table은 ".vtfixup" 코드로 구성되는데,

.vtfixup [<num_slots>] <flags> at <data_label>

예)
.vtfixup [1] int32 fromunmanaged at VT_01
.data VT_01 = int32(0x0600001A)

".vtfixup"은 다중으로 정의할 수 있고 개별적으로 "테이블"이 구성됩니다. 해당 테이블에 담을 항목 수는 "[num_slots]"에 지정된 숫자가 결정하며 그 테이블에 들어갈 entry의 규격은 "data_label"로 연결된 항목으로 결정됩니다. 즉, 위와 같은 경우에는 int32로 지정되었기 때문에 slot 1개를 가진 테이블 하나를 정의한 것입니다.

가령 메서드 3개를 export하고 싶다면 다음과 같은 식으로 정의할 수 있습니다.

// 32비트인 경우
.vtfixup [1] int32 fromunmanaged at VT_01
.data VT_01 = int32(0x0600001A)

.vtfixup [1] int32 fromunmanaged at VT_02
.data VT_02 = int32(0x0600001B)

.vtfixup [1] int32 fromunmanaged at VT_03
.data VT_03 = int32(0x0600001C)

// 64비트인 경우
.vtfixup [1] int64 fromunmanaged at VT_01
.data VT_01 = int64(0x0600001A)

.vtfixup [1] int64 fromunmanaged at VT_02
.data VT_02 = int64(0x0600001B)

.vtfixup [1] int64 fromunmanaged at VT_03
.data VT_03 = int64(0x0600001C)

여기서 0x0600001A, 0x0600001B, 0x0600001C 값은 EAT와 연결될 메서드의 MethodToken 값이기 때문에 이것을 일일이 알아내서 연결하는 것은 불편하므로 ilasm.exe로 하여금 자동으로 연결하도록 0을 지정하는 것이 좋습니다.

.vtfixup [1] int32 fromunmanaged at VT_01
.data VT_01 = int32(0)

.vtfixup [1] int32 fromunmanaged at VT_02
.data VT_02 = int32(0)

.vtfixup [1] int32 fromunmanaged at VT_03
.data VT_03 = int32(0)

그리고 어차피 저렇게 생성된 3개의 테이블은 내부 slot의 규격이 같기 때문에 편의상 다음과 같이 1개로 정의하는 것도 가능합니다.

// 32비트인 경우
.vtfixup [3] int32 fromunmanaged at VT_01
.data VT_01 = int32(0)[3]

// 64비트인 경우
.vtfixup [3] int64 fromunmanaged at VT_01
.data VT_01 = int64(0)[3]

마지막으로, 이렇게 정의된 VT Fixup Table과 EAT와 연결해야 하는데 그 역할을 각 메서드의 ".vtentry"로 할 수 있습니다.

.vtentry <entry_number> : <slot_number>

첫 번째 entry_number는 VT Fixup Table을 지정하고, 두 번째 slot_number는 해당 Table 내의 slot 인덱스를 지정합니다. 따라서 3개의 vtfixup으로 정의한 경우라면 이렇게 .vtentry를 사용하고,

.method public hidebysig static void  Func1() cil managed
  {
    .vtentry 1:1
    .export [1]
    ...[생략]...
  } // end of method Class1::Func1

  .method public hidebysig static int32  Func2() cil managed
  {
    .vtentry 2:1
    .export [2]
    ...[생략]...
  } // end of method Class1::Func2

  .method public hidebysig static void  Func3(int32 n) cil managed
  {
    .vtentry 3:1
    .export [3]
    ...[생략]...
  } // end of method Class1::Func3

3개의 slot을 가진 한 개의 vtfixup으로 정의한 경우라면 다음과 같이 지정하면 됩니다.

.method public hidebysig static void  Func1() cil managed
  {
    .vtentry 1:1
    .export [1]
    ...[생략]...
  } // end of method Class1::Func1

  .method public hidebysig static int32  Func2() cil managed
  {
    .vtentry 1:2
    .export [2]
    ...[생략]...
  } // end of method Class1::Func2

  .method public hidebysig static void  Func3(int32 n) cil managed
  {
    .vtentry 1:3
    .export [3]
    ...[생략]...
  } // end of method Class1::Func3

다행히 여기까지가 IL 수준에서 들어갈 수 있는 최대 깊이입니다.




"Unmanaged code can wrap managed methods" 글에 실린 IL 코드를 ilasm.exe로 실행한다면 다음과 같은 오류가 발생합니다.

C:\temp> ilasm test.il

Microsoft (R) .NET Framework IL Assembler.  Version 4.8.3752.0
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembling 'test.il'  to EXE --> 'test.exe'
Source file is ANSI

test.il(43) : error : syntax error at token 'from' in: .vtfixup [1] int32 from unmanaged at VT_01

***** FAILURE *****

(그 당시의 상황에서는 잘 되었을지도 모르지만) 43라인의 코드가,

.vtfixup [1] int32 from unmanaged at VT_01

이렇게 바뀌어야 합니다.

.vtfixup [1] int32 fromunmanaged at VT_01




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 1/20/2020]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 121  122  123  124  125  126  127  128  129  130  131  132  133  [134]  135  ...
NoWriterDateCnt.TitleFile(s)
1739정성태8/24/201427778.NET Framework: 457. 교착상태(Dead-lock) 해결 방법 - Lock Leveling [2]파일 다운로드1
1738정성태8/23/201423452.NET Framework: 456. C# - CAS를 이용한 Lock 래퍼 클래스파일 다운로드1
1737정성태8/20/201420936VS.NET IDE: 93. Visual Studio 2013 동기화 문제
1736정성태8/19/201426919VC++: 79. [부연] CAS Lock 알고리즘은 과연 빠른가? [2]파일 다운로드1
1735정성태8/19/201419460.NET Framework: 455. 닷넷 사용자 정의 예외 클래스의 최소 구현 코드 - 두 번째 이야기
1734정성태8/13/201421207오류 유형: 237. Windows Media Player cannot access the file. The file might be in use, you might not have access to the computer where the file is stored, or your proxy settings might not be correct.
1733정성태8/13/201427521.NET Framework: 454. EmptyWorkingSet Win32 API를 사용하는 C# 예제파일 다운로드1
1732정성태8/13/201435834Windows: 99. INetCache 폴더가 다르게 보이는 이유
1731정성태8/11/201428302개발 환경 구성: 235. 점(.)으로 시작하는 파일명을 탐색기에서 만드는 방법
1730정성태8/11/201423486개발 환경 구성: 234. Royal TS의 터미널(Terminal) 연결에서 한글이 깨지는 현상 해결 방법
1729정성태8/11/201419487오류 유형: 236. SqlConnection - The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly.
1728정성태8/8/201431723.NET Framework: 453. C# - 오피스 파워포인트(Powerpoint) 파일을 WinForm에서 보는 방법파일 다운로드1
1727정성태8/6/201421926오류 유형: 235. SignalR 오류 메시지 - Counter 'Messages Bus Messages Published Total' does not exist in the specified Category. [2]
1726정성태8/6/201420709오류 유형: 234. IIS Express에서 COM+ 사용 시 SecurityException - "Requested registry access is not allowed" 발생
1725정성태8/6/201422659오류 유형: 233. Visual Studio 2013 Update3 적용 후 Microsoft.VisualStudio.Web.PageInspector.Runtime 모듈에 대한 FileNotFoundException 예외 발생
1724정성태8/5/201427456.NET Framework: 452. .NET System.Threading.Thread 개체에서 Native Thread Id를 구하는 방법 - 두 번째 이야기 [1]파일 다운로드1
1723정성태7/29/201459853개발 환경 구성: 233. DirectX 9 예제 프로젝트 빌드하는 방법 [3]파일 다운로드1
1722정성태7/25/201422215오류 유형: 232. IIS 500 Internal Server Error - NTFS 암호화된 폴더에 웹 애플리케이션이 위치한 경우
1721정성태7/24/201425520.NET Framework: 451. 함수형 프로그래밍 개념 - 리스트 해석(List Comprehension)과 순수 함수 [2]
1720정성태7/23/201423468개발 환경 구성: 232. C:\WINDOWS\system32\LogFiles\HTTPERR 폴더에 로그 파일을 남기지 않는 설정
1719정성태7/22/201427362Math: 13. 동전을 여러 더미로 나누는 경우의 수 세기(Partition Number) - 두 번째 이야기파일 다운로드1
1718정성태7/19/201436808Math: 12. HTML에서 수학 관련 기호/수식을 표현하기 위한 방법 - MathJax.js [4]
1716정성태7/17/201436502개발 환경 구성: 231. PC 용 무료 안드로이드 에뮬레이터 - genymotion
1715정성태7/13/201431586기타: 47. 운영체제 종료 후에도 USB 외장 하드의 전원이 꺼지지 않는 경우 [3]
1714정성태7/11/201421573VS.NET IDE: 92. Visual Studio 2013을 지원하는 IL Support 확장 도구
1713정성태7/11/201445351Windows: 98. 윈도우 시스템 디스크 용량 확보를 위한 "Package Cache" 폴더 이동 [1]
... 121  122  123  124  125  126  127  128  129  130  131  132  133  [134]  135  ...