Skip to content

Commit

Permalink
Straighten PE/COFF import tables in .EXEs
Browse files Browse the repository at this point in the history
This is to try to reduce the number of false positives
in the various antivirus programs.

This may be a breaking change w.r.t. object/library
files compiled with earlier versions of Smaller C
(especially the ones involved in importing from DLLs).

n2f still needs to be updated to support additional
assembly directives, most notably for alignment.
  • Loading branch information
alexfru committed Oct 26, 2019
1 parent bd39c40 commit 1776503
Show file tree
Hide file tree
Showing 47 changed files with 330 additions and 568 deletions.
Binary file modified v0100/bind/smlrl.exe
Binary file not shown.
Binary file modified v0100/bindp/smlrl.exe
Binary file not shown.
Binary file modified v0100/binl/smlrl
Binary file not shown.
Binary file modified v0100/binm/smlrl
Binary file not shown.
Binary file modified v0100/binw/n2f.dat
Binary file not shown.
Binary file modified v0100/binw/smlrc.dat
Binary file not shown.
Binary file modified v0100/binw/smlrcc.dat
Binary file not shown.
Binary file modified v0100/binw/smlrl.dat
Binary file not shown.
Binary file modified v0100/binw/smlrpp.dat
Binary file not shown.
10 changes: 5 additions & 5 deletions v0100/doc/smlrl.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ Options:
**\_\_start_stack\_\_** symbol. The symbols **\_\_stop_alldata\_\_** and
**\_\_start_stack\_\_** can be used to create a memory heap between the
two locations.
* The linker treats section **.dll_imports** and sections with names ending
in **_hints** and **_iat** specially when making a Windows/PE executable.
These sections construct the DLL import table and the linker won't
* When making a Windows/PE executable the linker treats specially sections
whose names begin with **.dll_import** and **.dll_iat**.
These sections construct the DLL import tables and the linker won't
generate relocation information for the data contained in them. Also
special is symbol **\_\_dll_imports**, which resides in section
**.dll_imports**. Don't use these names.
special are symbols **\_\_dll_imports**, **\_\_dll_imports_end**,
**\_\_dll_iats**, **\_\_dll_iats_end**. Don't use these names.
Binary file modified v0100/lib/lcw.a
Binary file not shown.
24 changes: 17 additions & 7 deletions v0100/smlrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3003,7 +3003,20 @@ void RwPe(void)

peImportsStart = FindSymbolAddress("__dll_imports");
if (peImportsStart)
{
uint32 peImportsStop = FindSymbolAddress("__dll_imports_end");
uint32 peIatsStart = FindSymbolAddress("__dll_iats");
PeOptionalHeader.DataDirectory[1].VirtualAddress = peImportsStart - imageBase;
if (peImportsStop)
PeOptionalHeader.DataDirectory[1].Size = peImportsStop - peImportsStart;
if (peIatsStart)
{
uint32 peIatsStop = FindSymbolAddress("__dll_iats_end");
PeOptionalHeader.DataDirectory[12].VirtualAddress = peIatsStart - imageBase;
if (peIatsStop)
PeOptionalHeader.DataDirectory[12].Size = peIatsStop - peIatsStart;
}
}

Fwrite(DosMzExeStub, sizeof DosMzExeStub, fout);
Fwrite("PE\0", sizeof "PE\0", fout);
Expand Down Expand Up @@ -3097,15 +3110,12 @@ void RwPe(void)
if (!pRelSect)
continue;

// Don't create relocations for anything in sections ".dll_imports",
// "*_hints", "*_iat".
// Don't create relocations for anything in sections whose names
// start with ".dll_import" or ".dll_iat".
sectName = pSectDescrs[j].pName;
sectNameLen = strlen(sectName);
if (!strcmp(sectName, ".dll_imports") ||
(sectNameLen >= sizeof "_hints" &&
!strcmp(sectName + sectNameLen - sizeof "_hints" + 1, "_hints")) ||
(sectNameLen >= sizeof "_iat" &&
!strcmp(sectName + sectNameLen - sizeof "_iat" + 1, "_iat")))
if (!strncmp(sectName, ".dll_import", sizeof ".dll_import" - 1) ||
!strncmp(sectName, ".dll_iat", sizeof ".dll_iat" - 1))
continue;

// Write relocation records
Expand Down
47 changes: 34 additions & 13 deletions v0100/srclib/dimports.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
/*
Copyright (c) 2014-2016, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .dll_import\n"
"align 4\n"
"dd 0\n" // make sure the section isn't empty
"global __dll_imports\n"
"__dll_imports:"
);
" section .dll_import write align=4\n"
" dd 0\n"
" global __dll_imports\n"
" __dll_imports:\n"

// The dll imports table (section .dll_imports) will reside between
// sections .dll_import and .dll_imports_trailer due to alphabetic
// ordering of sections by the linker
// .dll_import0_<dll> sections follow.

asm(
"section .dll_imports_trailer\n"
"dd 0, 0, 0, 0, 0"
" section .dll_import1 write align=4\n"
" dd 0, 0, 0, 0, 0\n"

// Pairs of sections follow:
// .dll_import2_<dll>
// .dll_import2_<dll>_trailer

// .dll_import3_<dll> sections follow.
// .dll_import4_<dll> sections follow.

" section .dll_imports_end write align=4\n"
" global __dll_imports_end\n"
" __dll_imports_end:\n"
" dd 0\n"

" section .dll_iat write align=4\n"
" dd 0\n"
" global __dll_iats\n"
" __dll_iats:\n"

// Pairs of sections follow:
// .dll_iat2_<dll>
// .dll_iat2_<dll>_trailer

" section .dll_iats_end write align=4\n"
" global __dll_iats_end\n"
" __dll_iats_end:\n"
" dd 0\n"
);

#endif // _WINDOWS
29 changes: 29 additions & 0 deletions v0100/srclib/dimports.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright (c) 2019, Alexey Frunze
2-clause BSD license.
*/

// Before including this file define DLL and FXN, e.g.:
//
// #define DLL "kernel32"
// #define FXN "ExitProcess"
// #include "../dimports.h"

asm(
" section .dll_import2_" DLL " write align=4\n"
" dd _hint_" FXN "\n"

" section .dll_import3_" DLL " write align=2\n"
" _hint_" FXN ":\n"
" db 0, 0, \"" FXN "\", 0\n"
" align 2, db 0\n"

" section .dll_iat2_" DLL " write align=4\n"
" global __imp__" FXN "\n"
" __imp__" FXN ":\n"
" dd _hint_" FXN "\n"

" section .data\n"
" extern __" DLL "_dll__\n"
" dd __" DLL "_dll__\n" // pull DLL.c
);
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/closehan.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_CloseHandle"
);

asm(
"section .kernel32_iat\n"
"__imp__CloseHandle: dd _hint_CloseHandle"
);

static char hint_CloseHandle[] = "\0\0CloseHandle";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "CloseHandle"
#include "../dimports.h"

int __CloseHandle(unsigned Handle)
{
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/createfi.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_CreateFileA"
);

asm(
"section .kernel32_iat\n"
"__imp__CreateFileA: dd _hint_CreateFileA"
);

static char hint_CreateFileA[] = "\0\0CreateFileA";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "CreateFileA"
#include "../dimports.h"

unsigned __CreateFileA(char* FileName,
unsigned DesiredAccess,
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/createpr.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_CreateProcessA"
);

asm(
"section .kernel32_iat\n"
"__imp__CreateProcessA: dd _hint_CreateProcessA"
);

static char hint_CreateProcessA[] = "\0\0CreateProcessA";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "CreateProcessA"
#include "../dimports.h"

struct _SECURITY_ATTRIBUTES;
struct _STARTUPINFO;
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/deletefi.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_DeleteFileA"
);

asm(
"section .kernel32_iat\n"
"__imp__DeleteFileA: dd _hint_DeleteFileA"
);

static char hint_DeleteFileA[] = "\0\0DeleteFileA";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "DeleteFileA"
#include "../dimports.h"

int __DeleteFileA(char* FileName)
{
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/exitproc.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_ExitProcess"
);

asm(
"section .kernel32_iat\n"
"__imp__ExitProcess: dd _hint_ExitProcess"
);

static char hint_ExitProcess[] = "\0\0ExitProcess";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "ExitProcess"
#include "../dimports.h"

void __ExitProcess(unsigned ExitCode)
{
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/freelibr.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_FreeLibrary"
);

asm(
"section .kernel32_iat\n"
"__imp__FreeLibrary: dd _hint_FreeLibrary"
);

static char hint_FreeLibrary[] = "\0\0FreeLibrary";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "FreeLibrary"
#include "../dimports.h"

int __FreeLibrary(void* hModule)
{
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/getcomma.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_GetCommandLineA"
);

asm(
"section .kernel32_iat\n"
"__imp__GetCommandLineA: dd _hint_GetCommandLineA"
);

static char hint_GetCommandLineA[] = "\0\0GetCommandLineA";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "GetCommandLineA"
#include "../dimports.h"

char* __GetCommandLineA(void)
{
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/getenvir.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_GetEnvironmentVariableA"
);

asm(
"section .kernel32_iat\n"
"__imp__GetEnvironmentVariableA: dd _hint_GetEnvironmentVariableA"
);

static char hint_GetEnvironmentVariableA[] = "\0\0GetEnvironmentVariableA";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "GetEnvironmentVariableA"
#include "../dimports.h"

unsigned __GetEnvironmentVariableA(char* lpName,
char* lpBuffer,
Expand Down
19 changes: 4 additions & 15 deletions v0100/srclib/kernel32/getexitc.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/*
Copyright (c) 2014-2018, Alexey Frunze
Copyright (c) 2014-2019, Alexey Frunze
2-clause BSD license.
*/
#ifdef _WINDOWS

asm(
"section .kernel32_hints\n"
"dd _hint_GetExitCodeProcess"
);

asm(
"section .kernel32_iat\n"
"__imp__GetExitCodeProcess: dd _hint_GetExitCodeProcess"
);

static char hint_GetExitCodeProcess[] = "\0\0GetExitCodeProcess";

extern char _kernel32_dll__[];
static char* pdll = _kernel32_dll__; // pull trailers for sections .kernel32_hints and .kernel32_iat
#define DLL "kernel32"
#define FXN "GetExitCodeProcess"
#include "../dimports.h"

int __GetExitCodeProcess(unsigned hProcess, unsigned* lpExitCode)
{
Expand Down
Loading

0 comments on commit 1776503

Please sign in to comment.