Skip to content

Commit

Permalink
Silence PE DECLARE warnings when compiling signatures
Browse files Browse the repository at this point in the history
This function _fail_missing_PE_HOOK_DECLAREor_PE_UNPACKER_DECLARE
is used to fail the build if the user is calling a PE support function without
declaring PE_HOOK_DECLARE or PE_UNPACKER_DECLARE.  
This function is intentionally not defined and should not cause warnings.
This commit silences the warnings.
  • Loading branch information
ragusaa authored Jun 23, 2022
1 parent 54c709f commit 34964b8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
58 changes: 48 additions & 10 deletions headers/bytecode_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ typedef struct signature {
*/
/* some other macro may use __COUNTER__, so we need to subtract its current\
* value to obtain zero-based indices */
#define SIGNATURES_DEF_BEGIN \
static const unsigned __signature_bias = __COUNTER__ + 1; \
const struct __Signatures Signatures = {/** \
\group_config \
* Defines the pattern for a previously declared subsignature. \
* @sa DECLARE_SIGNATURE \
* @param name the name of a previously declared subsignature \
* @param hex the pattern for this subsignature \
*/
#define SIGNATURES_DEF_BEGIN \
static const unsigned __signature_bias = __COUNTER__ + 1; \
const struct __Signatures Signatures = {/** \
\group_config \
* Defines the pattern for a previously declared subsignature. \
* @sa DECLARE_SIGNATURE \
* @param name the name of a previously declared subsignature \
* @param hex the pattern for this subsignature \
*/
#define DEFINE_SIGNATURE(name, hex) \
.name##_sig = (hex), \
.name = {__COUNTER__ - __signature_bias},
Expand Down Expand Up @@ -545,15 +545,17 @@ static force_inline bool hasPEInfo(void)
__fail_missing_PE_HOOK_DECLARE__or__PE_UNPACKER_DECLARE(); \
}


/**
\group_pe
* Returns whether this is a PE32+ executable.
* @return true if this is a PE32+ executable
*/
static force_inline bool isPE64(void)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.opt64.Magic) == 0x020b;
}

Expand Down Expand Up @@ -834,7 +836,10 @@ static force_inline uint32_t getPELoaderFlags(void)
*/
static force_inline uint16_t getPEMachine()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.file_hdr.Machine);
}

Expand All @@ -845,7 +850,10 @@ static force_inline uint16_t getPEMachine()
*/
static force_inline uint32_t getPETimeDateStamp()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.file_hdr.TimeDateStamp);
}

Expand All @@ -856,7 +864,10 @@ static force_inline uint32_t getPETimeDateStamp()
*/
static force_inline uint32_t getPEPointerToSymbolTable()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.file_hdr.PointerToSymbolTable);
}

Expand All @@ -867,7 +878,10 @@ static force_inline uint32_t getPEPointerToSymbolTable()
*/
static force_inline uint32_t getPENumberOfSymbols()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.file_hdr.NumberOfSymbols);
}

Expand All @@ -878,7 +892,10 @@ static force_inline uint32_t getPENumberOfSymbols()
*/
static force_inline uint16_t getPESizeOfOptionalHeader()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.file_hdr.SizeOfOptionalHeader);
}

Expand All @@ -889,7 +906,10 @@ static force_inline uint16_t getPESizeOfOptionalHeader()
* @return characteristic of PE file, or 0 if not in PE hook*/
static force_inline uint16_t getPECharacteristics()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le16_to_host(__clambc_pedata.file_hdr.Characteristics);
}

Expand All @@ -913,7 +933,10 @@ static force_inline bool getPEisDLL()
*/
static force_inline uint32_t getPEDataDirRVA(unsigned n)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
const struct pe_image_data_dir *p = &__clambc_pedata.opt64_dirs[n];
const struct pe_image_data_dir *p32 = &__clambc_pedata.opt32_dirs[n];
return n < 16 ? le32_to_host(isPE64() ? p->VirtualAddress : p32->VirtualAddress)
Expand All @@ -928,7 +951,10 @@ static force_inline uint32_t getPEDataDirRVA(unsigned n)
*/
static force_inline uint32_t getPEDataDirSize(unsigned n)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return n < 16 ? le32_to_host(isPE64() ? __clambc_pedata.opt64_dirs[n].Size : __clambc_pedata.opt32_dirs[n].Size)
: 0;
}
Expand All @@ -951,7 +977,10 @@ static force_inline uint16_t getNumberOfSections(void)
*/
static uint32_t getPELFANew(void)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.e_lfanew);
}

Expand All @@ -965,7 +994,10 @@ static uint32_t getPELFANew(void)
*/
static force_inline int readPESectionName(unsigned char name[8], unsigned n)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
if (n >= getNumberOfSections())
return -1;
uint32_t at = getPELFANew() + sizeof(struct pe_image_file_hdr) + sizeof(struct pe_image_optional_hdr32);
Expand Down Expand Up @@ -1016,7 +1048,10 @@ static force_inline uint32_t getExeOffset(void)
*/
static force_inline uint32_t getImageBase(void)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(__clambc_pedata.opt32.ImageBase);
}

Expand All @@ -1027,7 +1062,10 @@ static force_inline uint32_t getImageBase(void)
*/
static uint32_t getVirtualEntryPoint(void)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
NEED_PE_INFO;
#pragma GCC diagnostic pop
return le32_to_host(isPE64() ? __clambc_pedata.opt64.AddressOfEntryPoint : __clambc_pedata.opt32.AddressOfEntryPoint);
}

Expand Down
26 changes: 13 additions & 13 deletions libclambcc/ClamBCAnalyzer/ClamBCAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ bool ClamBCAnalyzer::runOnModule(Module &M)
GlobalVariable *GVKind = M.getGlobalVariable("__clambc_kind");
if (GVKind && GVKind->hasDefinitiveInitializer()) {
kind = cast<ConstantInt>(GVKind->getInitializer())->getValue().getZExtValue();
//GVKind->setLinkage(GlobalValue::InternalLinkage);
//Do not set the linkage type to internal, because the optimizer will remove it.
// GVKind->setLinkage(GlobalValue::InternalLinkage);
// Do not set the linkage type to internal, because the optimizer will remove it.
if (kind >= 65536) {
ClamBCStop("Bytecode kind cannot be higher than 64k\n", &M);
}
Expand All @@ -86,15 +86,15 @@ bool ClamBCAnalyzer::runOnModule(Module &M)
GlobalVariable *G = M.getGlobalVariable("__Copyright");
if (G && G->hasDefinitiveInitializer()) {
Constant *C = G->getInitializer();
//std::string c;
// std::string c;
StringRef c;
if (!getConstantStringInfo(C, c)) {
ClamBCStop("Failed to extract copyright string\n", &M);
}
//copyright = strdup(c.c_str());
// copyright = strdup(c.c_str());
copyright = c.str();
//G->setLinkage(GlobalValue::InternalLinkage);
//Do not set the linkage type to internal because the optimizer will remove it.
// G->setLinkage(GlobalValue::InternalLinkage);
// Do not set the linkage type to internal because the optimizer will remove it.
}

// Logical signature created by ClamBCLogicalCompiler.
Expand All @@ -105,7 +105,7 @@ bool ClamBCAnalyzer::runOnModule(Module &M)
virusnames = Node ? cast<MDString>(Node->getOperand(0)->getOperand(0))->getString() : "";

unsigned tid, fid;
//unsigned cid;
// unsigned cid;
startTID = tid = clamav::initTypeIDs(typeIDs, M.getContext());
// arrays of [2 x i8] .. [7 x i8] used for struct padding
for (unsigned i = 1; i < 8; i++) {
Expand All @@ -116,7 +116,7 @@ bool ClamBCAnalyzer::runOnModule(Module &M)
}

std::vector<const Type *> types;
//cid=1;
// cid=1;
fid = 1;
for (Module::global_iterator I = M.global_begin(); I != M.global_end(); ++I) {
GlobalVariable *gv = llvm::cast<GlobalVariable>(I);
Expand All @@ -129,7 +129,7 @@ bool ClamBCAnalyzer::runOnModule(Module &M)
* can't use global idx 0 or 1 in the interpreter, since the size will
* be incorrect in the interpreter. Look at line 2011 of bytecode.c
*/
for (size_t loop = 0; loop < 2; loop++){
for (size_t loop = 0; loop < 2; loop++) {
for (auto J : ces) {
ConstantExpr *CE = llvm::cast<ConstantExpr>(J);
// ClamAV bytecode doesn't support arbitrary constant expressions for
Expand Down Expand Up @@ -341,7 +341,7 @@ void ClamBCAnalyzer::printGlobals(uint16_t stid)
llvm::Module &M = *pMod;
// Describe types
maxApi = 0;
//std::vector<const Function *> apis;
// std::vector<const Function *> apis;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
llvm::Function *pFunc = llvm::cast<llvm::Function>(I);
// Skip dead declarations
Expand Down Expand Up @@ -403,8 +403,8 @@ void ClamBCAnalyzer::printGlobals(uint16_t stid)
specialGlobals.insert(GV);
}

//std::vector<Constant *> globalInits;
globalInits.push_back(0); //ConstantPointerNul placeholder
// std::vector<Constant *> globalInits;
globalInits.push_back(0); // ConstantPointerNul placeholder
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
GlobalVariable *pgv = llvm::cast<GlobalVariable>(I);
if (specialGlobals.count(pgv)) {
Expand Down Expand Up @@ -472,7 +472,7 @@ void ClamBCAnalyzer::printGlobals(uint16_t stid)
}
}

//need to use bytecode_api_decl.c.h
// need to use bytecode_api_decl.c.h
void ClamBCAnalyzer::populateAPIMap()
{
unsigned id = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ClamBCPrepareGEPsForWriter : public ModulePass
currType = llvm::cast<StructType>(tmp);
} else if (llvm::isa<ArrayType>(tmp)) {
currType = tmp;
}
}
} else if (ArrayType *pat = llvm::dyn_cast<ArrayType>(currType)) {

uint64_t size = getTypeSizeInBytes(pat->getArrayElementType());
Expand All @@ -218,7 +218,7 @@ class ClamBCPrepareGEPsForWriter : public ModulePass
currType = llvm::cast<StructType>(tmp);
} else if (llvm::isa<ArrayType>(tmp)) {
currType = tmp;
}
}

} else {
assert(0 && "Figure out what to do here");
Expand Down Expand Up @@ -325,7 +325,6 @@ class ClamBCPrepareGEPsForWriter : public ModulePass
return pInst;
}


virtual void processGEPI(GetElementPtrInst *pgepi)
{

Expand Down
2 changes: 1 addition & 1 deletion libclambcc/ClamBCWriter/ClamBCWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ class ClamBCWriter : public ModulePass, public InstVisitor<ClamBCWriter>

Function *pFunc = SI.getParent()->getParent();
for (auto i = pFunc->arg_begin(), e = pFunc->arg_end(); i != e; i++) {
Argument * pArg = llvm::cast<Argument>(i);
Argument *pArg = llvm::cast<Argument>(i);
if (pArg == V) {
printFixedNumber(OP_BC_COPY, 2);
printOperand(SI, SI.getOperand(0));
Expand Down

0 comments on commit 34964b8

Please sign in to comment.