Skip to content

Commit 16b17d8

Browse files
authored
Delete dead code in ilasm PE writer (#111218)
* Delete dead code in ilasm PE writer * Delete magic enum values
1 parent db7e9ab commit 16b17d8

File tree

7 files changed

+34
-256
lines changed

7 files changed

+34
-256
lines changed

src/coreclr/dlls/mscorpe/ceefilegenwriter.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,7 @@ HRESULT CeeFileGenWriter::setAddrReloc(UCHAR *instrAddr, DWORD value)
495495

496496
HRESULT CeeFileGenWriter::addAddrReloc(CeeSection &thisSection, UCHAR *instrAddr, DWORD offset, CeeSection *targetSection)
497497
{
498-
if (!targetSection) {
499-
thisSection.addBaseReloc(offset, srRelocHighLow);
500-
} else {
501-
thisSection.addSectReloc(offset, *targetSection, srRelocHighLow);
502-
}
498+
thisSection.addSectReloc(offset, *targetSection, srRelocHighLow);
503499
return S_OK;
504500
} // HRESULT CeeFileGenWriter::addAddrReloc()
505501

src/coreclr/dlls/mscorpe/iceefilegen.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,7 @@ HRESULT ICeeFileGen::AddSectionReloc (HCEESECTION section, ULONG offset, HCEESEC
150150
CeeSection *sec = reinterpret_cast<CeeSection*>(section);
151151
CeeSection *relSec = reinterpret_cast<CeeSection*>(relativeTo);
152152

153-
if (relSec)
154-
{
155-
return(sec->addSectReloc(offset, *relSec, relocType));
156-
}
157-
else
158-
{
159-
return(sec->addBaseReloc(offset, relocType));
160-
}
153+
return(sec->addSectReloc(offset, *relSec, relocType));
161154
}
162155

163156
HRESULT ICeeFileGen::SetOutputFileName (HCEEFILE ceeFile, _In_ LPWSTR outputFileName)

src/coreclr/dlls/mscorpe/pewriter.cpp

Lines changed: 31 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
#ifdef LOGGING
1313
#include "log.h"
1414

15-
static const char* const RelocName[] = {
16-
"Absolute", "Unk1", "Unk2", "HighLow", "Unk4", "MapToken",
17-
"Relative", "FilePos", "CodeRel", "Unk3", "Dir64", "AbsTag" };
15+
static const char* const RelocName[] = { "Absolute", "HighLow", "MapToken", "FilePos" };
1816
static const char RelocSpaces[] = " ";
1917

2018
#endif
@@ -36,18 +34,6 @@ inline static unsigned padLen(unsigned len, unsigned align) {
3634
return(roundUp(len, align) - len);
3735
}
3836

39-
#ifndef IMAGE_DLLCHARACTERISTICS_NO_SEH
40-
#define IMAGE_DLLCHARACTERISTICS_NO_SEH 0x400
41-
#endif
42-
43-
#ifndef IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
44-
#define IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE 0x0040
45-
#endif
46-
47-
#ifndef IMAGE_DLLCHARACTERISTICS_NX_COMPAT
48-
#define IMAGE_DLLCHARACTERISTICS_NX_COMPAT 0x0100
49-
#endif
50-
5137
#define COPY_AND_ADVANCE(target, src, size) { \
5238
::memcpy((void *) (target), (const void *) (src), (size)); \
5339
(char *&) (target) += (size); }
@@ -294,20 +280,11 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,
294280

295281
int curType = cur->type;
296282
DWORD curOffset = cur->offset;
297-
bool isRelocPtr = ((curType & srRelocPtr) != 0);
298-
bool noBaseBaseReloc = ((curType & srNoBaseReloc) != 0);
299283
UINT64 targetOffset = 0;
300284
int slotNum = 0;
285+
#ifdef LOGGING
301286
INT64 oldStarPos;
302-
303-
// If cur->section is NULL then this is a pointer outside the module.
304-
bool externalAddress = (cur->section == NULL);
305-
306-
curType &= ~(srRelocPtr | srNoBaseReloc);
307-
308-
/* If we see any srRelocHighLow's in a PE64 file we convert them into DIR64 relocs */
309-
if (!isPE32 && (curType == srRelocHighLow))
310-
curType = srRelocDir64;
287+
#endif
311288

312289
DWORD curRVA = m_baseRVA; // RVA in the PE image of the reloc site
313290
IfFailRet(AddOvf_RVA(curRVA, curOffset));
@@ -317,87 +294,32 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,
317294

318295
#ifdef LOGGING
319296
LOG((LF_ZAP, LL_INFO1000000,
320-
" Reloc %s%s%s at %-7s+%04x (RVA=%08x) at" FMT_ADDR,
321-
RelocName[curType], (isRelocPtr) ? "Ptr" : " ",
297+
" Reloc %s%s at %-7s+%04x (RVA=%08x) at" FMT_ADDR,
322298
&RelocSpaces[strlen(RelocName[curType])],
323299
m_name, curOffset, curRVA, DBG_ADDR(pos)));
324-
#endif
325-
//
326-
// 'pos' is the site of the reloc
327-
// Compute 'targetOffset' from pointer if necessary
328-
//
329-
330-
if (isRelocPtr)
331-
{
332-
// Calculate the value of ptr to pass to computeOffset
333-
char * ptr = (char *) pos;
334300

335-
if (curType == srRelocRelative) {
336-
//
337-
// Here we add sizeof(int) because we need to calculate
338-
// ptr as the true call target address (x86 pc-rel)
339-
// We need to true call target address since pass it
340-
// to computeOffset and this function would fall if
341-
// the address we pass is before the start of a section
342-
//
343-
oldStarPos = (SSIZE_T) ptr;
344-
IfFailRet(AddOvf_S_S32(oldStarPos, GET_UNALIGNED_INT32(pos)));
345-
IfFailRet(AddOvf_S_U32(oldStarPos, sizeof(int)));
346-
ptr = (char *) oldStarPos;
347-
targetOffset = externalAddress ? (size_t) ptr
348-
: cur->section->computeOffset(ptr);
349-
// We subtract off the four bytes that we added previous
350-
// since the code below depends upon this
351-
IfFailRet(SubOvf_U_U32(targetOffset, sizeof(int)));
352-
IfFailRet(UnsignedFitsIn32Bits(targetOffset)); // Check for overflow
353-
SET_UNALIGNED_VAL32(pos, targetOffset);
354-
}
355-
else {
356-
ptr = (char *) GET_UNALIGNED_VALPTR(ptr);
357-
oldStarPos = (SSIZE_T) ptr;
358-
targetOffset = externalAddress ? (size_t) ptr
359-
: cur->section->computeOffset(ptr);
360-
IfFailRet(UnsignedFitsIn32Bits(targetOffset)); // Check for overflow
361-
SET_UNALIGNED_VAL32(pos, targetOffset);
362-
/* Zero the upper 32-bits for a machine with 64-bit pointers */
363-
if (!isPE32)
364-
SET_UNALIGNED_VAL32(pos+1, 0);
365-
}
366-
}
367-
#ifdef LOGGING
368-
else
369-
{
370-
oldStarPos = GET_UNALIGNED_VAL32(pos);
371-
}
301+
oldStarPos = GET_UNALIGNED_VAL32(pos);
372302
#endif
373303

374304
//
375305
// 'targetOffset' has now been computed. Write out the appropriate value.
376306
// Record base relocs as necessary.
377307
//
378308

379-
bool fBaseReloc = false;
380-
bool fNeedBrl = false;
309+
int baseReloc = 0;
381310
INT64 newStarPos = 0; // oldStarPos gets updated to newStarPos
382311

383-
if (curType == srRelocAbsolute || curType == srRelocAbsoluteTagged) {
384-
_ASSERTE(!externalAddress);
312+
if (curType == srRelocAbsolute) {
385313

386314
newStarPos = GET_UNALIGNED_INT32(pos);
387315

388-
if (curType == srRelocAbsoluteTagged)
389-
newStarPos = (newStarPos & ~0x80000001) >> 1;
390-
391316
if (rdataRvaBase > 0 && ! strcmp((const char *)(cur->section->m_name), ".rdata"))
392317
IfFailRet(AddOvf_S_U32(newStarPos, rdataRvaBase));
393318
else if (dataRvaBase > 0 && ! strcmp((const char *)(cur->section->m_name), ".data"))
394319
IfFailRet(AddOvf_S_U32(newStarPos, dataRvaBase));
395320
else
396321
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_baseRVA));
397322

398-
if (curType == srRelocAbsoluteTagged)
399-
newStarPos = (newStarPos << 1) | 0x80000001;
400-
401323
SET_UNALIGNED_VAL32(pos, newStarPos);
402324
}
403325
else if (curType == srRelocMapToken)
@@ -411,110 +333,61 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,
411333
}
412334
else if (curType == srRelocFilePos)
413335
{
414-
_ASSERTE(!externalAddress);
415336
newStarPos = GET_UNALIGNED_VAL32(pos);
416337
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_filePos));
417338
SET_UNALIGNED_VAL32(pos, newStarPos);
418339
}
419-
else if (curType == srRelocRelative)
420-
{
421-
if (externalAddress) {
422-
#if defined(HOST_AMD64)
423-
newStarPos = GET_UNALIGNED_INT32(pos);
424-
#else // x86
425-
UINT64 targetAddr = GET_UNALIGNED_VAL32(pos);
426-
IfFailRet(SubOvf_U_U(newStarPos, targetAddr, imageBase));
427-
#endif
428-
}
429-
else {
430-
newStarPos = GET_UNALIGNED_INT32(pos);
431-
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_baseRVA));
432-
}
433-
IfFailRet(SubOvf_S_U32(newStarPos, curRVA));
434-
IfFailRet(SignedFitsIn31Bits(newStarPos)); // Check for overflow
435-
SET_UNALIGNED_VAL32(pos, newStarPos);
436-
}
437-
else if (curType == srRelocCodeRelative)
438-
{
439-
newStarPos = GET_UNALIGNED_INT32(pos);
440-
IfFailRet(SubOvf_S_U32(newStarPos, codeRvaBase));
441-
if (externalAddress)
442-
IfFailRet(SubOvf_S_U(newStarPos, imageBase));
443-
else
444-
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_baseRVA));
445-
IfFailRet(SignedFitsIn31Bits(newStarPos)); // Check for overflow
446-
SET_UNALIGNED_VAL32(pos, newStarPos);
447-
448-
}
449340
else if (curType == srRelocHighLow)
450341
{
451-
_ASSERTE(isPE32);
452-
453-
// we have a 32-bit value at pos
454-
UINT64 value = GET_UNALIGNED_VAL32(pos);
455-
456-
if (!externalAddress)
342+
if (isPE32)
457343
{
344+
// we have a 32-bit value at pos
345+
UINT64 value = GET_UNALIGNED_VAL32(pos);
346+
458347
IfFailRet(AddOvf_U_U32(value, cur->section->m_baseRVA));
459348
IfFailRet(AddOvf_U_U(value, imageBase));
460-
}
461349

462-
IfFailRet(UnsignedFitsIn32Bits(value)); // Check for overflow
463-
SET_UNALIGNED_VAL32(pos, value);
350+
IfFailRet(UnsignedFitsIn32Bits(value)); // Check for overflow
351+
SET_UNALIGNED_VAL32(pos, value);
464352

465-
newStarPos = value;
353+
newStarPos = value;
466354

467-
fBaseReloc = true;
468-
}
469-
else if (curType == srRelocDir64)
470-
{
471-
_ASSERTE(!isPE32);
472-
473-
// we have a 64-bit value at pos
474-
UINT64 UNALIGNED * p_value = (UINT64 *) pos;
475-
targetOffset = *p_value;
476-
477-
if (!externalAddress)
355+
baseReloc = IMAGE_REL_BASED_HIGHLOW;
356+
}
357+
else
478358
{
359+
// we have a 64-bit value at pos
360+
UINT64 UNALIGNED * p_value = (UINT64 *) pos;
361+
targetOffset = *p_value;
362+
479363
// The upper bits of targetOffset must be zero
480364
IfFailRet(UnsignedFitsIn32Bits(targetOffset));
481365

482366
IfFailRet(AddOvf_U_U32(targetOffset, cur->section->m_baseRVA));
483367
IfFailRet(AddOvf_U_U(targetOffset, imageBase));
484-
}
485368

486-
*p_value = targetOffset;
487-
newStarPos = targetOffset;
488-
fBaseReloc = true;
369+
*p_value = targetOffset;
370+
newStarPos = targetOffset;
371+
372+
baseReloc = IMAGE_REL_BASED_DIR64;
373+
}
489374
}
490375
else
491376
{
492377
_ASSERTE(!"Unknown Relocation type");
493378
}
494379

495-
if (fBaseReloc && !noBaseBaseReloc)
380+
if (baseReloc != 0)
496381
{
497-
pBaseRelocSection->AddBaseReloc(curRVA, curType);
382+
pBaseRelocSection->AddBaseReloc(curRVA, baseReloc);
498383
}
499384

500385
#ifdef LOGGING
501-
const char* sectionName;
502-
503-
if (externalAddress)
504-
{
505-
sectionName = "external";
506-
}
507-
else
508-
{
509-
sectionName = cur->section->m_name;
510-
}
511-
512386
LOG((LF_ZAP, LL_INFO1000000,
513-
"to %-7s+%04x, old =" FMT_ADDR "new =" FMT_ADDR "%s%s\n",
514-
sectionName, targetOffset,
387+
"to %-7s+%04x, old =" FMT_ADDR "new =" FMT_ADDR "%s\n",
388+
cur->section->m_name, targetOffset,
515389
DBG_ADDR(oldStarPos), DBG_ADDR(newStarPos),
516-
fBaseReloc ? "(BASE RELOC)" : "",
517-
fNeedBrl ? "(BRL)" : "" ));
390+
baseReloc ? "(BASE RELOC)" : ""));
518391
#endif
519392

520393
}

src/coreclr/inc/ceegen.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ class CeeSectionImpl {
104104
CeeSection & relativeTo,
105105
CeeSectionRelocType reloc = srRelocAbsolute,
106106
CeeSectionRelocExtra * extra = NULL) = 0;
107-
virtual HRESULT addBaseReloc(
108-
unsigned offset,
109-
CeeSectionRelocType reloc = srRelocHighLow,
110-
CeeSectionRelocExtra * extra = NULL) = 0;
111107
virtual HRESULT directoryEntry(unsigned num) = 0;
112108
virtual unsigned char * name() = 0;
113109
virtual char * computePointer(unsigned offset) const = 0;
@@ -150,8 +146,6 @@ class CeeSection {
150146
// have the base of section 'relativeTo added to it
151147
HRESULT addSectReloc(unsigned offset, CeeSection& relativeTo,
152148
CeeSectionRelocType = srRelocAbsolute, CeeSectionRelocExtra *extra = 0);
153-
// Add a base reloc for the given offset in the current section
154-
virtual HRESULT addBaseReloc(unsigned offset, CeeSectionRelocType reloc = srRelocHighLow, CeeSectionRelocExtra *extra = 0);
155149

156150

157151
// this section will be directory entry 'num'
@@ -319,12 +313,6 @@ inline HRESULT CeeSection::addSectReloc(
319313
return(m_impl.addSectReloc(offset, relativeTo, reloc, extra));
320314
}
321315

322-
inline HRESULT CeeSection::addBaseReloc(unsigned offset, CeeSectionRelocType reloc, CeeSectionRelocExtra *extra) {
323-
WRAPPER_NO_CONTRACT;
324-
return(m_impl.addBaseReloc(offset, reloc, extra));
325-
}
326-
327-
328316
inline HRESULT CeeSection::directoryEntry(unsigned num) {
329317
WRAPPER_NO_CONTRACT;
330318
TESTANDRETURN(num < IMAGE_NUMBEROF_DIRECTORY_ENTRIES, E_INVALIDARG);

src/coreclr/inc/corpriv.h

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -314,51 +314,20 @@ typedef enum {
314314

315315
// generate a .reloc for a pointer sized location,
316316
// This is transformed into BASED_HIGHLOW or BASED_DIR64 based on the platform
317-
srRelocHighLow = IMAGE_REL_BASED_HIGHLOW,
318-
319-
// generate a .reloc for the top 16-bits of a 32 bit number, where the
320-
// bottom 16 bits are included in the next word in the .reloc table
321-
srRelocHighAdj, // Never Used
317+
srRelocHighLow,
322318

323319
// generate a token map relocation, nothing into .reloc section
324320
srRelocMapToken,
325321

326-
// relative address fixup
327-
srRelocRelative,
328-
329322
// Generate only a section-relative reloc, nothing into .reloc
330323
// section. This reloc is relative to the file position of the
331324
// section, not the section's virtual address.
332325
srRelocFilePos,
333326

334-
// code relative address fixup
335-
srRelocCodeRelative,
336-
337-
// generate a .reloc for a 64 bit address
338-
srRelocDir64 = IMAGE_REL_BASED_DIR64,
339-
340-
// generate a 30-bit section-relative reloc, used for tagged pointer values
341-
srRelocAbsoluteTagged,
342-
343-
344327
// A sentinel value to help ensure any additions to this enum are reflected
345328
// in PEWriter.cpp's RelocName array.
346329
srRelocSentinel,
347330

348-
// Flags that can be used with the above reloc types
349-
350-
// do not emit base reloc
351-
srNoBaseReloc = 0x4000,
352-
353-
// pre-fixup contents of memory are ptr rather than a section offset
354-
srRelocPtr = 0x8000,
355-
356-
// legal enums which include the Ptr flag
357-
srRelocAbsolutePtr = srRelocPtr + srRelocAbsolute,
358-
srRelocHighLowPtr = srRelocPtr + srRelocHighLow,
359-
srRelocRelativePtr = srRelocPtr + srRelocRelative,
360-
srRelocDir64Ptr = srRelocPtr + srRelocDir64,
361-
362331
} CeeSectionRelocType;
363332

364333
typedef union {

0 commit comments

Comments
 (0)