1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4//
5// ZapRelocs.h
6//
7
8//
9// Zapping of relocations
10//
11// ======================================================================================
12
13#ifndef __ZAPRELOCS_H__
14#define __ZAPRELOCS_H__
15
16typedef BYTE ZapRelocationType; // IMAGE_REL_XXX enum
17
18// Special NGEN-specific relocation type for fixups (absolute RVA in the middle 30 bits)
19#define IMAGE_REL_BASED_ABSOLUTE_TAGGED 0x7E
20
21// Special NGEN-specific relocation type for relative pointer (used to make NGen relocation section smaller)
22#define IMAGE_REL_BASED_RELPTR 0x7D
23#define IMAGE_REL_BASED_RELPTR32 0x7C
24
25// Invalid reloc marker (used to mark end of the reloc array)
26#define IMAGE_REL_INVALID 0xFF
27
28// IMAGE_REL_BASED_PTR is architecture specific reloc of virtual address
29#ifdef _TARGET_64BIT_
30#define IMAGE_REL_BASED_PTR IMAGE_REL_BASED_DIR64
31#else
32#define IMAGE_REL_BASED_PTR IMAGE_REL_BASED_HIGHLOW
33#endif
34
35// Size of base relocs relocation page
36#define RELOCATION_PAGE_SIZE 0x1000
37
38//
39// The ZapNode for regular PE base relocs
40//
41
42class ZapBaseRelocs : public ZapNode
43{
44 ZapImage * m_pImage;
45
46 // The page currently being written
47 DWORD m_page;
48 COUNT_T m_pageIndex;
49
50 // Reloc writer output
51 SArray<USHORT> m_SerializedRelocs;
52
53 void FlushWriter();
54
55public:
56 ZapBaseRelocs(ZapImage * pImage)
57 : m_pImage(pImage)
58 {
59 // Everything is zero initialized by the allocator
60 }
61
62 void WriteReloc(PVOID pSrc, int offset, ZapNode * pTarget, int targetOffset, ZapRelocationType type);
63
64 virtual DWORD GetSize()
65 {
66 return m_SerializedRelocs.GetCount() * sizeof(USHORT);
67 }
68
69 virtual ZapNodeType GetType()
70 {
71 return ZapNodeType_Relocs;
72 }
73
74 virtual void Save(ZapWriter * pZapWriter);
75};
76
77//
78//
79// Blob with associated relocations. Used for compiled code.
80//
81
82struct ZapReloc
83{
84 ZapRelocationType m_type;
85 DWORD m_offset;
86 ZapNode * m_pTargetNode;
87};
88
89class ZapBlobWithRelocs : public ZapBlob
90{
91 ZapReloc * m_pRelocs;
92
93protected:
94 ZapBlobWithRelocs(SIZE_T cbSize)
95 : ZapBlob(cbSize)
96 {
97 }
98
99public:
100 void SetRelocs(ZapReloc * pRelocs)
101 {
102 _ASSERTE(m_pRelocs == NULL);
103 _ASSERTE(pRelocs != NULL);
104 m_pRelocs = pRelocs;
105 }
106
107 ZapReloc * GetRelocs()
108 {
109 return m_pRelocs;
110 }
111
112 virtual PBYTE GetData()
113 {
114 return (PBYTE)(this + 1);
115 }
116
117 virtual void Save(ZapWriter * pZapWriter);
118
119 // Returns number of straddler relocs, assuming RVA of the node is dwPos
120 COUNT_T GetCountOfStraddlerRelocations(DWORD dwPos);
121
122 // Create new zap blob node. The node *does* own copy of the memory.
123 static ZapBlobWithRelocs * NewBlob(ZapWriter * pWriter, PVOID pData, SIZE_T cbSize);
124
125 // Create new aligned zap blob node.
126 static ZapBlobWithRelocs * NewAlignedBlob(ZapWriter * pWriter, PVOID pData, SIZE_T cbSize, SIZE_T cbAlignment);
127};
128
129#endif // __ZAPRELOCS_H__
130