Skip to content

Commit

Permalink
Improve stubs + remove obsolete shims
Browse files Browse the repository at this point in the history
  • Loading branch information
jchv committed Aug 3, 2024
1 parent f1c4478 commit f6ceef8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 76 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ LDFLAGS := \
-lkernel32 \
-luser32 \
third_party/msvcrt/msvcrt.lib \
third_party/msvcrt/rtsyms.o \
-Wl,--enable-stdcall-fixup \
-s

Expand All @@ -35,7 +34,8 @@ OBJS := \
obj/ijl15.o \
obj/json.o \
obj/patch.o \
obj/regex.o
obj/regex.o \
obj/stubs.o

TESTOBJS := \
$(OBJS) \
Expand All @@ -56,6 +56,9 @@ all: $(OUT) $(TESTOUT)
$(OBJDIR)%.o: $(SRCDIR)%.c
@mkdir -p "$(dir $@)"
$(CC) -c $(CFLAGS) "$<" -o "$@"
$(OBJDIR)%.o: $(SRCDIR)%.S
@mkdir -p "$(dir $@)"
$(CC) -c $(CFLAGS) "$<" -o "$@"
$(OUT): $(OBJS)
@mkdir -p "$(dir $@)"
$(CC) $(OBJS) $(LDFLAGS) -shared -o "$@" export.def -Wl,-e_DllMain
Expand Down
22 changes: 19 additions & 3 deletions rugburn.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,31 @@
<ClCompile Include="src\ld.c" />
<ClCompile Include="src\hooks\msvcr100\msvcr100.c" />
<ClCompile Include="src\hooks\projectg\us852\ranking.c" />
<CustomBuild Include="src\stubs.asm">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">ml /c /Fo$(OutDir)%(Filename).obj %(FullPath)</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)%(Filename).obj</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">ml /c /Fo$(OutDir)%(Filename).obj %(FullPath)</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)%(Filename).obj</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">ml /c /Fo$(OutDir)%(Filename).obj %(FullPath)</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)%(Filename).obj</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">ml /c /Fo$(OutDir)%(Filename).obj %(FullPath)</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)%(Filename).obj</Outputs>
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</LinkObjects>
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkObjects>
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</LinkObjects>
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkObjects>
<OutputItemType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Object</OutputItemType>
<OutputItemType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Object</OutputItemType>
<OutputItemType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Object</OutputItemType>
<OutputItemType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Object</OutputItemType>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<None Include="export.def" />
<None Include="LICENSE.md" />
<None Include="README.md" />
</ItemGroup>
<ItemGroup>
<Object Include="third_party\msvcrt\rtsyms.o" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
71 changes: 0 additions & 71 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,82 +27,11 @@ static PSTR pszLogPrefix = NULL;
#pragma function(memcmp)
#pragma function(memcpy)
#pragma function(memset)

void __declspec(naked) _aullshr() {
__asm {
cmp cl,40h
jae RETZERO
cmp cl,20h
jae MORE32
shrd eax,edx,cl
shr edx,cl
ret
MORE32:
mov eax,edx
xor edx,edx
and cl,1Fh
shr eax,cl
ret
RETZERO:
xor eax,eax
xor edx,edx
ret
}
}
#else
void __chkstk_ms(void) { return; }
unsigned long long __stdcall _aullshr(unsigned long long a, long b) { return a >> b; }
#endif

// C standard library functions
int strcmp(LPCSTR dest, LPCSTR src) {
int cmp;
do {
cmp = *dest - *src;
if (cmp > 0) {
return 1;
} else if (cmp < 0) {
return -1;
}
} while (*dest++ && *src++);
return 0;
}

int memcmp(LPCVOID dest, LPCVOID src, size_t size) {
LPCSTR bdest = (LPCSTR)dest;
LPCSTR bsrc = (LPCSTR)src;
int cmp;
while (size > 0) {
cmp = *bdest++ - *bsrc++;
if (cmp > 0) {
return 1;
} else if (cmp < 0) {
return -1;
}
--size;
}
return 0;
}

PVOID memcpy(PVOID dest, VOID const *src, size_t size) {
BYTE *bdest = (BYTE *)dest;
BYTE const *bsrc = (BYTE const *)src;
while (size > 0) {
*bdest++ = *bsrc++;
--size;
}
return dest;
}

PVOID memset(PVOID p, INT c, UINT size) {
PCHAR data = (PCHAR)p;
while (size > 0) {
*data++ = c;
--size;
}
return p;
}

// String formatting
int VSPrintfZ(LPSTR dest, LPCSTR fmt, va_list args) { return wvsprintfA(dest, fmt, args); }

Expand Down
34 changes: 34 additions & 0 deletions src/stubs.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.intel_syntax noprefix

.global __fltused
.set __fltused, 0x9876

.global __ldused
.set __ldused, 0x9876

.global __except_list
.set __except_list, 0

.section .text

.global __aullshr
__aullshr:
cmp cl, 0x40
jae retzero
cmp cl, 0x20
jae more32
shrd eax, edx, cl
shr edx, cl
ret
more32:
mov eax, edx
xor edx, edx
and cl, 0x1f
shr eax, cl
ret
retzero:
xor eax, eax
xor edx, edx
ret

.end
36 changes: 36 additions & 0 deletions src/stubs.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.386
.model flat

.code

public __fltused
__fltused equ 9876h

public __ldused
__ldused equ 9876h

public __except_list
__except_list equ 0

public __aullshr
__aullshr PROC
cmp cl, 40h
jae retzero
cmp cl, 20h
jae more32
shrd eax, edx, cl
shr edx, cl
ret
more32:
mov eax, edx
xor edx, edx
and cl, 1Fh
shr eax, cl
ret
retzero:
xor eax, eax
xor edx, edx
ret
__aullshr ENDP

END
Binary file removed third_party/msvcrt/rtsyms.o
Binary file not shown.

0 comments on commit f6ceef8

Please sign in to comment.