Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Delete dead code in ilasm PE writer #111218

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/coreclr/dlls/mscorpe/ceefilegenwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,7 @@ HRESULT CeeFileGenWriter::setAddrReloc(UCHAR *instrAddr, DWORD value)

HRESULT CeeFileGenWriter::addAddrReloc(CeeSection &thisSection, UCHAR *instrAddr, DWORD offset, CeeSection *targetSection)
{
if (!targetSection) {
thisSection.addBaseReloc(offset, srRelocHighLow);
} else {
thisSection.addSectReloc(offset, *targetSection, srRelocHighLow);
}
thisSection.addSectReloc(offset, *targetSection, srRelocHighLow);
return S_OK;
} // HRESULT CeeFileGenWriter::addAddrReloc()

Expand Down
9 changes: 1 addition & 8 deletions src/coreclr/dlls/mscorpe/iceefilegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,7 @@ HRESULT ICeeFileGen::AddSectionReloc (HCEESECTION section, ULONG offset, HCEESEC
CeeSection *sec = reinterpret_cast<CeeSection*>(section);
CeeSection *relSec = reinterpret_cast<CeeSection*>(relativeTo);

if (relSec)
{
return(sec->addSectReloc(offset, *relSec, relocType));
}
else
{
return(sec->addBaseReloc(offset, relocType));
}
return(sec->addSectReloc(offset, *relSec, relocType));
}

HRESULT ICeeFileGen::SetOutputFileName (HCEEFILE ceeFile, _In_ LPWSTR outputFileName)
Expand Down
189 changes: 31 additions & 158 deletions src/coreclr/dlls/mscorpe/pewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
#ifdef LOGGING
#include "log.h"

static const char* const RelocName[] = {
"Absolute", "Unk1", "Unk2", "HighLow", "Unk4", "MapToken",
"Relative", "FilePos", "CodeRel", "Unk3", "Dir64", "AbsTag" };
static const char* const RelocName[] = { "Absolute", "HighLow", "MapToken", "FilePos" };
static const char RelocSpaces[] = " ";

#endif
Expand All @@ -36,18 +34,6 @@ inline static unsigned padLen(unsigned len, unsigned align) {
return(roundUp(len, align) - len);
}

#ifndef IMAGE_DLLCHARACTERISTICS_NO_SEH
#define IMAGE_DLLCHARACTERISTICS_NO_SEH 0x400
#endif

#ifndef IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
#define IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE 0x0040
#endif

#ifndef IMAGE_DLLCHARACTERISTICS_NX_COMPAT
#define IMAGE_DLLCHARACTERISTICS_NX_COMPAT 0x0100
#endif

#define COPY_AND_ADVANCE(target, src, size) { \
::memcpy((void *) (target), (const void *) (src), (size)); \
(char *&) (target) += (size); }
Expand Down Expand Up @@ -294,20 +280,11 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,

int curType = cur->type;
DWORD curOffset = cur->offset;
bool isRelocPtr = ((curType & srRelocPtr) != 0);
bool noBaseBaseReloc = ((curType & srNoBaseReloc) != 0);
UINT64 targetOffset = 0;
int slotNum = 0;
#ifdef LOGGING
INT64 oldStarPos;

// If cur->section is NULL then this is a pointer outside the module.
bool externalAddress = (cur->section == NULL);

curType &= ~(srRelocPtr | srNoBaseReloc);

/* If we see any srRelocHighLow's in a PE64 file we convert them into DIR64 relocs */
if (!isPE32 && (curType == srRelocHighLow))
curType = srRelocDir64;
#endif

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

#ifdef LOGGING
LOG((LF_ZAP, LL_INFO1000000,
" Reloc %s%s%s at %-7s+%04x (RVA=%08x) at" FMT_ADDR,
RelocName[curType], (isRelocPtr) ? "Ptr" : " ",
" Reloc %s%s at %-7s+%04x (RVA=%08x) at" FMT_ADDR,
&RelocSpaces[strlen(RelocName[curType])],
m_name, curOffset, curRVA, DBG_ADDR(pos)));
#endif
//
// 'pos' is the site of the reloc
// Compute 'targetOffset' from pointer if necessary
//

if (isRelocPtr)
{
// Calculate the value of ptr to pass to computeOffset
char * ptr = (char *) pos;

if (curType == srRelocRelative) {
//
// Here we add sizeof(int) because we need to calculate
// ptr as the true call target address (x86 pc-rel)
// We need to true call target address since pass it
// to computeOffset and this function would fall if
// the address we pass is before the start of a section
//
oldStarPos = (SSIZE_T) ptr;
IfFailRet(AddOvf_S_S32(oldStarPos, GET_UNALIGNED_INT32(pos)));
IfFailRet(AddOvf_S_U32(oldStarPos, sizeof(int)));
ptr = (char *) oldStarPos;
targetOffset = externalAddress ? (size_t) ptr
: cur->section->computeOffset(ptr);
// We subtract off the four bytes that we added previous
// since the code below depends upon this
IfFailRet(SubOvf_U_U32(targetOffset, sizeof(int)));
IfFailRet(UnsignedFitsIn32Bits(targetOffset)); // Check for overflow
SET_UNALIGNED_VAL32(pos, targetOffset);
}
else {
ptr = (char *) GET_UNALIGNED_VALPTR(ptr);
oldStarPos = (SSIZE_T) ptr;
targetOffset = externalAddress ? (size_t) ptr
: cur->section->computeOffset(ptr);
IfFailRet(UnsignedFitsIn32Bits(targetOffset)); // Check for overflow
SET_UNALIGNED_VAL32(pos, targetOffset);
/* Zero the upper 32-bits for a machine with 64-bit pointers */
if (!isPE32)
SET_UNALIGNED_VAL32(pos+1, 0);
}
}
#ifdef LOGGING
else
{
oldStarPos = GET_UNALIGNED_VAL32(pos);
}
oldStarPos = GET_UNALIGNED_VAL32(pos);
#endif

//
// 'targetOffset' has now been computed. Write out the appropriate value.
// Record base relocs as necessary.
//

bool fBaseReloc = false;
bool fNeedBrl = false;
int baseReloc = 0;
INT64 newStarPos = 0; // oldStarPos gets updated to newStarPos

if (curType == srRelocAbsolute || curType == srRelocAbsoluteTagged) {
_ASSERTE(!externalAddress);
if (curType == srRelocAbsolute) {

newStarPos = GET_UNALIGNED_INT32(pos);

if (curType == srRelocAbsoluteTagged)
newStarPos = (newStarPos & ~0x80000001) >> 1;

if (rdataRvaBase > 0 && ! strcmp((const char *)(cur->section->m_name), ".rdata"))
IfFailRet(AddOvf_S_U32(newStarPos, rdataRvaBase));
else if (dataRvaBase > 0 && ! strcmp((const char *)(cur->section->m_name), ".data"))
IfFailRet(AddOvf_S_U32(newStarPos, dataRvaBase));
else
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_baseRVA));

if (curType == srRelocAbsoluteTagged)
newStarPos = (newStarPos << 1) | 0x80000001;

SET_UNALIGNED_VAL32(pos, newStarPos);
}
else if (curType == srRelocMapToken)
Expand All @@ -411,110 +333,61 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,
}
else if (curType == srRelocFilePos)
{
_ASSERTE(!externalAddress);
newStarPos = GET_UNALIGNED_VAL32(pos);
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_filePos));
SET_UNALIGNED_VAL32(pos, newStarPos);
}
else if (curType == srRelocRelative)
{
if (externalAddress) {
#if defined(HOST_AMD64)
newStarPos = GET_UNALIGNED_INT32(pos);
#else // x86
UINT64 targetAddr = GET_UNALIGNED_VAL32(pos);
IfFailRet(SubOvf_U_U(newStarPos, targetAddr, imageBase));
#endif
}
else {
newStarPos = GET_UNALIGNED_INT32(pos);
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_baseRVA));
}
IfFailRet(SubOvf_S_U32(newStarPos, curRVA));
IfFailRet(SignedFitsIn31Bits(newStarPos)); // Check for overflow
SET_UNALIGNED_VAL32(pos, newStarPos);
}
else if (curType == srRelocCodeRelative)
{
newStarPos = GET_UNALIGNED_INT32(pos);
IfFailRet(SubOvf_S_U32(newStarPos, codeRvaBase));
if (externalAddress)
IfFailRet(SubOvf_S_U(newStarPos, imageBase));
else
IfFailRet(AddOvf_S_U32(newStarPos, cur->section->m_baseRVA));
IfFailRet(SignedFitsIn31Bits(newStarPos)); // Check for overflow
SET_UNALIGNED_VAL32(pos, newStarPos);

}
else if (curType == srRelocHighLow)
{
_ASSERTE(isPE32);

// we have a 32-bit value at pos
UINT64 value = GET_UNALIGNED_VAL32(pos);

if (!externalAddress)
if (isPE32)
{
// we have a 32-bit value at pos
UINT64 value = GET_UNALIGNED_VAL32(pos);

IfFailRet(AddOvf_U_U32(value, cur->section->m_baseRVA));
IfFailRet(AddOvf_U_U(value, imageBase));
}

IfFailRet(UnsignedFitsIn32Bits(value)); // Check for overflow
SET_UNALIGNED_VAL32(pos, value);
IfFailRet(UnsignedFitsIn32Bits(value)); // Check for overflow
SET_UNALIGNED_VAL32(pos, value);

newStarPos = value;
newStarPos = value;

fBaseReloc = true;
}
else if (curType == srRelocDir64)
{
_ASSERTE(!isPE32);

// we have a 64-bit value at pos
UINT64 UNALIGNED * p_value = (UINT64 *) pos;
targetOffset = *p_value;

if (!externalAddress)
baseReloc = IMAGE_REL_BASED_HIGHLOW;
}
else
{
// we have a 64-bit value at pos
UINT64 UNALIGNED * p_value = (UINT64 *) pos;
targetOffset = *p_value;

// The upper bits of targetOffset must be zero
IfFailRet(UnsignedFitsIn32Bits(targetOffset));

IfFailRet(AddOvf_U_U32(targetOffset, cur->section->m_baseRVA));
IfFailRet(AddOvf_U_U(targetOffset, imageBase));
}

*p_value = targetOffset;
newStarPos = targetOffset;
fBaseReloc = true;
*p_value = targetOffset;
newStarPos = targetOffset;

baseReloc = IMAGE_REL_BASED_DIR64;
}
}
else
{
_ASSERTE(!"Unknown Relocation type");
}

if (fBaseReloc && !noBaseBaseReloc)
if (baseReloc != 0)
{
pBaseRelocSection->AddBaseReloc(curRVA, curType);
pBaseRelocSection->AddBaseReloc(curRVA, baseReloc);
}

#ifdef LOGGING
const char* sectionName;

if (externalAddress)
{
sectionName = "external";
}
else
{
sectionName = cur->section->m_name;
}

LOG((LF_ZAP, LL_INFO1000000,
"to %-7s+%04x, old =" FMT_ADDR "new =" FMT_ADDR "%s%s\n",
sectionName, targetOffset,
"to %-7s+%04x, old =" FMT_ADDR "new =" FMT_ADDR "%s\n",
cur->section->m_name, targetOffset,
DBG_ADDR(oldStarPos), DBG_ADDR(newStarPos),
fBaseReloc ? "(BASE RELOC)" : "",
fNeedBrl ? "(BRL)" : "" ));
baseReloc ? "(BASE RELOC)" : ""));
#endif

}
Expand Down
12 changes: 0 additions & 12 deletions src/coreclr/inc/ceegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ class CeeSectionImpl {
CeeSection & relativeTo,
CeeSectionRelocType reloc = srRelocAbsolute,
CeeSectionRelocExtra * extra = NULL) = 0;
virtual HRESULT addBaseReloc(
unsigned offset,
CeeSectionRelocType reloc = srRelocHighLow,
CeeSectionRelocExtra * extra = NULL) = 0;
virtual HRESULT directoryEntry(unsigned num) = 0;
virtual unsigned char * name() = 0;
virtual char * computePointer(unsigned offset) const = 0;
Expand Down Expand Up @@ -150,8 +146,6 @@ class CeeSection {
// have the base of section 'relativeTo added to it
HRESULT addSectReloc(unsigned offset, CeeSection& relativeTo,
CeeSectionRelocType = srRelocAbsolute, CeeSectionRelocExtra *extra = 0);
// Add a base reloc for the given offset in the current section
virtual HRESULT addBaseReloc(unsigned offset, CeeSectionRelocType reloc = srRelocHighLow, CeeSectionRelocExtra *extra = 0);


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

inline HRESULT CeeSection::addBaseReloc(unsigned offset, CeeSectionRelocType reloc, CeeSectionRelocExtra *extra) {
WRAPPER_NO_CONTRACT;
return(m_impl.addBaseReloc(offset, reloc, extra));
}


inline HRESULT CeeSection::directoryEntry(unsigned num) {
WRAPPER_NO_CONTRACT;
TESTANDRETURN(num < IMAGE_NUMBEROF_DIRECTORY_ENTRIES, E_INVALIDARG);
Expand Down
33 changes: 1 addition & 32 deletions src/coreclr/inc/corpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,51 +314,20 @@ typedef enum {

// generate a .reloc for a pointer sized location,
// This is transformed into BASED_HIGHLOW or BASED_DIR64 based on the platform
srRelocHighLow = IMAGE_REL_BASED_HIGHLOW,

// generate a .reloc for the top 16-bits of a 32 bit number, where the
// bottom 16 bits are included in the next word in the .reloc table
srRelocHighAdj, // Never Used
srRelocHighLow,

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

// relative address fixup
srRelocRelative,

// Generate only a section-relative reloc, nothing into .reloc
// section. This reloc is relative to the file position of the
// section, not the section's virtual address.
srRelocFilePos,

// code relative address fixup
srRelocCodeRelative,

// generate a .reloc for a 64 bit address
srRelocDir64 = IMAGE_REL_BASED_DIR64,

// generate a 30-bit section-relative reloc, used for tagged pointer values
srRelocAbsoluteTagged,


// A sentinel value to help ensure any additions to this enum are reflected
// in PEWriter.cpp's RelocName array.
srRelocSentinel,

// Flags that can be used with the above reloc types

// do not emit base reloc
srNoBaseReloc = 0x4000,

// pre-fixup contents of memory are ptr rather than a section offset
srRelocPtr = 0x8000,

// legal enums which include the Ptr flag
srRelocAbsolutePtr = srRelocPtr + srRelocAbsolute,
srRelocHighLowPtr = srRelocPtr + srRelocHighLow,
srRelocRelativePtr = srRelocPtr + srRelocRelative,
srRelocDir64Ptr = srRelocPtr + srRelocDir64,

} CeeSectionRelocType;

typedef union {
Expand Down
Loading