summaryrefslogtreecommitdiff
path: root/src/zap/zaprelocs.cpp
blob: 059d9a59ac8fd19eb803afa8c814418dfbab333d (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// 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.
//
// ZapRelocs.cpp
//

//
// Zapping of relocations
// 
// ======================================================================================

#include "common.h"

#include "zaprelocs.h"

#ifdef REDHAWK
void PDB_NoticeReloc(ZapRelocationType type, DWORD rvaReloc, ZapNode * pTarget, int targetOffset);
#endif

void ZapBaseRelocs::WriteReloc(PVOID pSrc, int offset, ZapNode * pTarget, int targetOffset, ZapRelocationType type)
{
    _ASSERTE(pTarget != NULL);

    PBYTE pLocation = (PBYTE)pSrc + offset;
    DWORD rva = m_pImage->GetCurrentRVA() + offset;
    TADDR pActualTarget = (TADDR)m_pImage->GetBaseAddress() + pTarget->GetRVA() + targetOffset;

#ifdef REDHAWK
    PDB_NoticeReloc(type, rva, pTarget, targetOffset);
#endif

    switch (type)
    {
    case IMAGE_REL_BASED_ABSOLUTE:
        *(UNALIGNED DWORD *)pLocation = pTarget->GetRVA() + targetOffset;
        // IMAGE_REL_BASED_ABSOLUTE does not need base reloc entry
        return;

    case IMAGE_REL_BASED_ABSOLUTE_TAGGED:
        _ASSERTE(targetOffset == 0);
        *(UNALIGNED DWORD *)pLocation = (DWORD)CORCOMPILE_TAG_TOKEN(pTarget->GetRVA());
        // IMAGE_REL_BASED_ABSOLUTE_TAGGED does not need base reloc entry
        return;

    case IMAGE_REL_BASED_PTR:
#ifdef _TARGET_ARM_
        // Misaligned relocs disable ASLR on ARM. We should never ever emit them.
        _ASSERTE(IS_ALIGNED(rva, sizeof(TADDR)));
#endif
        *(UNALIGNED TADDR *)pLocation = pActualTarget;
        break;

    case IMAGE_REL_BASED_RELPTR:
        {
            TADDR pSite = (TADDR)m_pImage->GetBaseAddress() + rva;
            *(UNALIGNED TADDR *)pLocation = (INT32)(pActualTarget - pSite);
        }
        // neither IMAGE_REL_BASED_RELPTR nor IMAGE_REL_BASED_MD_METHODENTRY need base reloc entry
        return;

    case IMAGE_REL_BASED_RELPTR32:
        {
            TADDR pSite = (TADDR)m_pImage->GetBaseAddress() + rva;
            *(UNALIGNED INT32 *)pLocation = (INT32)(pActualTarget - pSite);
        }
        // IMAGE_REL_BASED_RELPTR32 does not need base reloc entry
        return;

#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
    case IMAGE_REL_BASED_REL32:
        {
            TADDR pSite = (TADDR)m_pImage->GetBaseAddress() + rva;
            *(UNALIGNED INT32 *)pLocation = (INT32)(pActualTarget - (pSite + sizeof(INT32)));
        }
        // IMAGE_REL_BASED_REL32 does not need base reloc entry
        return;
#endif // _TARGET_X86_ || _TARGET_AMD64_

#if defined(_TARGET_ARM_)
    case IMAGE_REL_BASED_THUMB_MOV32:
        {
            PutThumb2Mov32((UINT16 *)pLocation, (UINT32)pActualTarget);
            break;
        }

    case IMAGE_REL_BASED_REL_THUMB_MOV32_PCREL:
        {
            TADDR pSite = (TADDR)m_pImage->GetBaseAddress() + rva;

            // For details about how the value is calculated, see
            // description of IMAGE_REL_BASED_REL_THUMB_MOV32_PCREL
            const UINT32 offsetCorrection = 12;

            UINT32 imm32 = pActualTarget - (pSite + offsetCorrection);

            PutThumb2Mov32((UINT16 *)pLocation, imm32);

            // IMAGE_REL_BASED_REL_THUMB_MOV32_PCREL does not need base reloc entry
            return;
        }

    case IMAGE_REL_BASED_THUMB_BRANCH24:
        {
            TADDR pSite = (TADDR)m_pImage->GetBaseAddress() + rva;

            // Kind of a workaround: make this reloc work both for calls (which have the thumb bit set),
            // and for relative jumps used for hot/cold splitting (which don't).
            pActualTarget &= ~THUMB_CODE;

            // Calculate the reloffset without the ThumbBit set so that it can be correctly encoded.
            _ASSERTE(!(pActualTarget & THUMB_CODE));// we expect pActualTarget not to have the thumb bit set
            _ASSERTE(!(pSite & THUMB_CODE));        // we expect pSite not to have the thumb bit set
            INT32 relOffset = (INT32)(pActualTarget - (pSite + sizeof(INT32)));
            if (!FitsInThumb2BlRel24(relOffset))
            {
                // Retry the compilation with IMAGE_REL_BASED_THUMB_BRANCH24 relocations disabled
                // (See code:ZapInfo::getRelocTypeHint)
                ThrowHR(COR_E_OVERFLOW);
            }
            PutThumb2BlRel24((UINT16 *)pLocation, relOffset);
        }
        // IMAGE_REL_BASED_THUMB_BRANCH24 does not need base reloc entry
        return;
#endif
#if defined(_TARGET_ARM64_)
    case IMAGE_REL_ARM64_BRANCH26:
        {
            TADDR pSite = (TADDR)m_pImage->GetBaseAddress() + rva;

            INT32 relOffset = (INT32)(pActualTarget - pSite);
            if (!FitsInRel28(relOffset))
            {
                ThrowHR(COR_E_OVERFLOW);
            }
            PutArm64Rel28((UINT32 *)pLocation,relOffset);
        }
        return;

    case IMAGE_REL_ARM64_PAGEBASE_REL21:
        {
            TADDR pSitePage = ((TADDR)m_pImage->GetBaseAddress() + rva) & 0xFFFFFFFFFFFFF000LL;
            TADDR pActualTargetPage = pActualTarget & 0xFFFFFFFFFFFFF000LL;

            INT64 relPage = (INT64)(pActualTargetPage - pSitePage);
            INT32 imm21 = (INT32)(relPage >> 12) & 0x1FFFFF;
            PutArm64Rel21((UINT32 *)pLocation, imm21);
        }
        return;

    case IMAGE_REL_ARM64_PAGEOFFSET_12A:
        {
            INT32 imm12 = (INT32)(pActualTarget & 0xFFFLL);
            PutArm64Rel12((UINT32 *)pLocation, imm12);
        }
        return;
#endif

    default:
        _ASSERTE(!"Unknown relocation type");
        break;
    }

    DWORD page = AlignDown(rva, RELOCATION_PAGE_SIZE);

    if (page != m_page)
    {
        FlushWriter();

        m_page = page;
        m_pageIndex = m_SerializedRelocs.GetCount();

        // Reserve space for IMAGE_BASE_RELOCATION
        for (size_t iSpace = 0; iSpace < sizeof(IMAGE_BASE_RELOCATION) / sizeof(USHORT); iSpace++)
            m_SerializedRelocs.Append(0);
    }

    m_SerializedRelocs.Append((USHORT)(AlignmentTrim(rva, RELOCATION_PAGE_SIZE) | (type << 12)));
}

void ZapBaseRelocs::FlushWriter()
{
    if (m_page != 0)
    {
        // The blocks has to be 4-byte aligned
        if (m_SerializedRelocs.GetCount() & 1)
            m_SerializedRelocs.Append(0);

        IMAGE_BASE_RELOCATION * pBaseRelocation = (IMAGE_BASE_RELOCATION *)&(m_SerializedRelocs[m_pageIndex]);
        pBaseRelocation->VirtualAddress = m_page;
        pBaseRelocation->SizeOfBlock = (m_SerializedRelocs.GetCount() - m_pageIndex) * sizeof(USHORT);

        m_page = 0;
    }
}

void ZapBaseRelocs::Save(ZapWriter * pZapWriter)
{
    FlushWriter();

    pZapWriter->SetWritingRelocs();

    // Write the relocs as blob
    pZapWriter->Write(&m_SerializedRelocs[0], m_SerializedRelocs.GetCount() * sizeof(USHORT));
}

//////////////////////////////////////////////////////////////////////////////
//
// ZapBlobWithRelocs
//

int _cdecl CmpZapRelocs(const void *p1, const void *p2)
{
    LIMITED_METHOD_CONTRACT;

    const ZapReloc *relocTemp1 = (ZapReloc *)p1;
    const ZapReloc *relocTemp2 = (ZapReloc *)p2;
    if (relocTemp1->m_offset < relocTemp2->m_offset)
        return -1;
    else if (relocTemp1->m_offset > relocTemp2->m_offset)
        return 1;
    else
        return 0;
}

void ZapBlobWithRelocs::Save(ZapWriter * pZapWriter)
{
    if (m_pRelocs != NULL)
    {

        // pre-pass to figure out if we need to sort
        // if the offsets are not in ascending order AND the offsets within this
        // array ending up describing locations in different pages, the relocation
        // writer generates bad relocation info (e.g. multiple entries for the same page)
        // that is no longer accepted by the OS loader
        // Also, having relocs in ascending order allows a more compact representation.

        ZapReloc *pReloc = m_pRelocs;

        // we need to check only for more than one reloc entry 
        if (pReloc->m_type != IMAGE_REL_INVALID && pReloc[1].m_type != IMAGE_REL_INVALID)
        {
            bool isSorted = true;
            DWORD lastOffset = pReloc->m_offset;
            DWORD cReloc = 1;

            // we start with the second entry (the first entry is already consumed)
            while (pReloc[cReloc].m_type != IMAGE_REL_INVALID)
            {
                // we cannot abort the loop here because we need to count the entries
                // to properly sort the relocs!!!
                if (pReloc[cReloc].m_offset < lastOffset)
                    isSorted = false;
                lastOffset = pReloc[cReloc].m_offset;
                cReloc++;
            }
            if (!isSorted)
            {
                qsort(pReloc, cReloc, sizeof(ZapReloc), CmpZapRelocs);
            }
        }

        ZapImage * pImage = ZapImage::GetImage(pZapWriter);
        PBYTE pData = GetData();

        for (pReloc = m_pRelocs; pReloc->m_type != IMAGE_REL_INVALID; pReloc++)
        {
            PBYTE pLocation = pData + pReloc->m_offset;
            int targetOffset = 0;

            // Decode the offset
            switch (pReloc->m_type)
            {
            case IMAGE_REL_BASED_ABSOLUTE:
                targetOffset = *(UNALIGNED DWORD *)pLocation;
                break;

            case IMAGE_REL_BASED_ABSOLUTE_TAGGED:
                targetOffset = 0;
                break;

            case IMAGE_REL_BASED_PTR:
                targetOffset = (int)*(UNALIGNED TADDR *)pLocation;
                break;
            case IMAGE_REL_BASED_RELPTR:
                targetOffset = (int)*(UNALIGNED TADDR *)pLocation;
                break;

            case IMAGE_REL_BASED_RELPTR32:
                targetOffset = (int)*(UNALIGNED INT32 *)pLocation;
                break;

#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
            case IMAGE_REL_BASED_REL32:
                targetOffset = *(UNALIGNED INT32 *)pLocation;
                break;
#endif // _TARGET_X86_ || _TARGET_AMD64_

#if defined(_TARGET_ARM_)
            case IMAGE_REL_BASED_THUMB_MOV32:
            case IMAGE_REL_BASED_REL_THUMB_MOV32_PCREL:
                targetOffset = (int)GetThumb2Mov32((UINT16 *)pLocation);
                break;

            case IMAGE_REL_BASED_THUMB_BRANCH24:
                targetOffset = GetThumb2BlRel24((UINT16 *)pLocation);
                break;
#endif // defined(_TARGET_ARM_)

#if defined(_TARGET_ARM64_)
            case IMAGE_REL_ARM64_BRANCH26:
                targetOffset = (int)GetArm64Rel28((UINT32*)pLocation);
                break;

            case IMAGE_REL_ARM64_PAGEBASE_REL21:
                targetOffset = (int)GetArm64Rel21((UINT32*)pLocation);
                break;

            case IMAGE_REL_ARM64_PAGEOFFSET_12A:
                targetOffset = (int)GetArm64Rel12((UINT32*)pLocation);
                break;

#endif // defined(_TARGET_ARM64_)

            default:
                _ASSERTE(!"Unknown reloc type");
                break;
            }

            pImage->WriteReloc(pData, pReloc->m_offset,
                pReloc->m_pTargetNode, targetOffset, pReloc->m_type);
        }
    }

    ZapBlob::Save(pZapWriter);
}

COUNT_T ZapBlobWithRelocs::GetCountOfStraddlerRelocations(DWORD dwPos)
{
    if (m_pRelocs == NULL)
        return 0;

    // Straddlers can exist only if the node is crossing page boundary
    if (AlignDown(dwPos, RELOCATION_PAGE_SIZE) == AlignDown(dwPos + GetSize() - 1, RELOCATION_PAGE_SIZE))
        return 0;

    COUNT_T nStraddlers = 0;

    for (ZapReloc * pReloc = m_pRelocs; pReloc->m_type != IMAGE_REL_INVALID; pReloc++)
    {
        if (pReloc->m_type == IMAGE_REL_BASED_PTR)
        {
            if (AlignmentTrim(dwPos + pReloc->m_offset, RELOCATION_PAGE_SIZE) > RELOCATION_PAGE_SIZE - sizeof(TADDR))
                nStraddlers++;          
        }
    }

    return nStraddlers;
}

ZapBlobWithRelocs * ZapBlobWithRelocs::NewBlob(ZapWriter * pWriter, PVOID pData, SIZE_T cbSize)
{
    S_SIZE_T cbAllocSize = S_SIZE_T(sizeof(ZapBlobWithRelocs)) + S_SIZE_T(cbSize);
    if(cbAllocSize.IsOverflow())
        ThrowHR(COR_E_OVERFLOW);
    
    void * pMemory = new (pWriter->GetHeap()) BYTE[cbAllocSize.Value()];

    ZapBlobWithRelocs * pZapBlobWithRelocs = new (pMemory) ZapBlobWithRelocs(cbSize);
    
    if (pData != NULL)
        memcpy((void*)(pZapBlobWithRelocs + 1), pData, cbSize);

    return pZapBlobWithRelocs;
}

template <DWORD alignment>
class ZapAlignedBlobWithRelocsConst : public ZapBlobWithRelocs
{
protected:
    ZapAlignedBlobWithRelocsConst(SIZE_T cbSize)
        : ZapBlobWithRelocs(cbSize)
    {
    }

public:
    virtual UINT GetAlignment()
    {
        return alignment;
    }

    static ZapBlobWithRelocs * NewBlob(ZapWriter * pWriter, PVOID pData, SIZE_T cbSize)
    {
        S_SIZE_T cbAllocSize = S_SIZE_T(sizeof(ZapAlignedBlobWithRelocsConst<alignment>)) + S_SIZE_T(cbSize);
        if(cbAllocSize.IsOverflow())
            ThrowHR(COR_E_OVERFLOW);
        
        void * pMemory = new (pWriter->GetHeap()) BYTE[cbAllocSize.Value()];

        ZapAlignedBlobWithRelocsConst<alignment> * pZapBlob = new (pMemory) ZapAlignedBlobWithRelocsConst<alignment>(cbSize);

        if (pData != NULL)
            memcpy((void*)(pZapBlob + 1), pData, cbSize);

        return pZapBlob;
    }
};

ZapBlobWithRelocs * ZapBlobWithRelocs::NewAlignedBlob(ZapWriter * pWriter, PVOID pData, SIZE_T cbSize, SIZE_T cbAlignment)
{
    switch (cbAlignment)
    {
    case 1:
        return ZapBlobWithRelocs::NewBlob(pWriter, pData, cbSize);
    case 2:
        return ZapAlignedBlobWithRelocsConst<2>::NewBlob(pWriter, pData, cbSize);
    case 4:
        return ZapAlignedBlobWithRelocsConst<4>::NewBlob(pWriter, pData, cbSize);
    case 8:
        return ZapAlignedBlobWithRelocsConst<8>::NewBlob(pWriter, pData, cbSize);
    case 16:
        return ZapAlignedBlobWithRelocsConst<16>::NewBlob(pWriter, pData, cbSize);

    default:
        _ASSERTE(!"Requested alignment not supported");
        return NULL;
    }
}