summaryrefslogtreecommitdiff
path: root/src/tools/r2rdump/R2RImportSection.cs
blob: bf7a056d74c6935be2504d60333315e14d32dcdd (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Xml.Serialization;

namespace R2RDump
{
    public struct R2RImportSection
    {
        public enum CorCompileImportType
        {
            CORCOMPILE_IMPORT_TYPE_UNKNOWN = 0,
            CORCOMPILE_IMPORT_TYPE_EXTERNAL_METHOD = 1,
            CORCOMPILE_IMPORT_TYPE_STUB_DISPATCH = 2,
            CORCOMPILE_IMPORT_TYPE_STRING_HANDLE = 3,
            CORCOMPILE_IMPORT_TYPE_TYPE_HANDLE = 4,
            CORCOMPILE_IMPORT_TYPE_METHOD_HANDLE = 5,
            CORCOMPILE_IMPORT_TYPE_VIRTUAL_METHOD = 6,
        };

        public enum CorCompileImportFlags
        {
            CORCOMPILE_IMPORT_FLAGS_UNKNOWN = 0x0000,
            CORCOMPILE_IMPORT_FLAGS_EAGER = 0x0001,   // Section at module load time.
            CORCOMPILE_IMPORT_FLAGS_CODE = 0x0002,   // Section contains code.
            CORCOMPILE_IMPORT_FLAGS_PCODE = 0x0004,   // Section contains pointers to code.
        };

        public struct ImportSectionEntry
        {
            public int StartOffset { get; set; }
            public long Section { get; set; }
            [XmlAttribute("Index")]
            public uint SignatureRVA { get; set; }
            public byte[] SignatureSample { get; set; }
            public ImportSectionEntry(int startOffset, long section, uint signatureRVA, byte[] signatureSample)
            {
                StartOffset = startOffset;
                Section = section;
                SignatureRVA = signatureRVA;
                SignatureSample = signatureSample;
            }

            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append($@"+{StartOffset:X4}  Section: 0x{Section:X8}  SignatureRVA: 0x{SignatureRVA:X8}  ");
                foreach (byte b in SignatureSample)
                {
                    sb.AppendFormat("{0:X2} ", b);
                }
                sb.Append("...");
                return sb.ToString();
            }
        }

        /// <summary>
        /// Section containing values to be fixed up
        /// </summary>
        [XmlAttribute("Index")]
        public int SectionRVA { get; set; }
        public int SectionSize { get; set; }

        /// <summary>
        /// One or more of ImportSectionFlags
        /// </summary>
        public CorCompileImportFlags Flags { get; set; }

        /// <summary>
        /// One of ImportSectionType
        /// </summary>
        public CorCompileImportType Type { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public byte EntrySize { get; set; }

        /// <summary>
        /// RVA of optional signature descriptors
        /// </summary>
        public int SignatureRVA { get; set; }
        public List<ImportSectionEntry> Entries { get; set; }

        /// <summary>
        /// RVA of optional auxiliary data (typically GC info)
        /// </summary>
        public int AuxiliaryDataRVA { get; set; }
        public GcInfo AuxiliaryData { get; set; }

        public R2RImportSection(byte[] image, int rva, int size, CorCompileImportFlags flags, byte type, byte entrySize, int signatureRVA, List<ImportSectionEntry> entries, int auxDataRVA, int auxDataOffset, Machine machine, ushort majorVersion)
        {
            SectionRVA = rva;
            SectionSize = size;
            Flags = flags;
            Type = (CorCompileImportType)type;
            EntrySize = entrySize;

            SignatureRVA = signatureRVA;
            Entries = entries;

            AuxiliaryDataRVA = auxDataRVA;
            AuxiliaryData = null;
            if (AuxiliaryDataRVA != 0)
            {
                AuxiliaryData = new GcInfo(image, auxDataOffset, machine, majorVersion);
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"SectionRVA: 0x{SectionRVA:X8} ({SectionRVA})");
            sb.AppendLine($"SectionSize: {SectionSize} bytes");
            sb.AppendLine($"Flags: {Flags}");
            sb.AppendLine($"Type: {Type}");
            sb.AppendLine($"EntrySize: {EntrySize}");
            sb.AppendLine($"SignatureRVA: 0x{SignatureRVA:X8} ({SignatureRVA})");
            sb.AppendLine($"AuxiliaryDataRVA: 0x{AuxiliaryDataRVA:X8} ({AuxiliaryDataRVA})");
            if (AuxiliaryDataRVA != 0)
            {
                sb.AppendLine("AuxiliaryData:");
                sb.AppendLine(AuxiliaryData.ToString());
            }
            return sb.ToString();
        }
    }
}