Skip to content

Commit a70850c

Browse files
committed
modify API_test.dll build process to reduce diff sizes between compiles
-split static CRT parts into a separate DLL, this code is not going to change unless someone uses a different VC version to compile the RTC DLL -delete the /Debug and /Release DLL files, I dont commit them since they are duplicates of the root DLLs and thus make the repo pointlessly larger -minimize use of CRT, use Win32 calls instead of CRT calls, this way less stdio/format string things need to be stored in the RTC DLL, use custom DllMain to not pull in CRT startup code, this makes API_test.dll smaller by less machine code and const/non-const data in it, so if something happen to cause the location of every function in the DLL to move and all relative/offset pointers in the machine code to .rdata/.data to numerically change, there is less to change in the diff -stop using PCH+delete PCH files, no perf gain, W32A isn't a massive C++ codebase, less files in tarball/on disk -removing PCH made the #pragma pack(1) actually take effect, this changed the offsets of the members in simple_struct to an very rarely seen in typical C code layout and caused a crash in 01_Struct.t . Remove pack(1).
1 parent 54f043a commit a70850c

23 files changed

+443
-479
lines changed

.gitignore

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
*.bs
22
*.c
3-
*.def
43
*.i
54
*.o
65
*.obj
76
*.pdb
87
*.xsc
8+
*.exp
9+
*.lib
10+
*.pch
11+
*.idb
12+
/*.def
913
Callback/dll.base
10-
Callback/dll.exp
14+
Callback/*.def
1115
dll.base
12-
dll.exp
1316
Makefile
1417
Makefile.old
1518
blib
@@ -20,3 +23,6 @@ MYMETA.*
2023
Win32-API-*
2124
/*.patch
2225
API_def.old
26+
api-test/Release
27+
api-test/Debug
28+
api-test/*.ncb

API_test.dll

-181 KB
Binary file not shown.

API_test64.dll

-7 KB
Binary file not shown.

Changes

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ History of Win32::API perl extension.
33
2015-??-?? Win32::API v0.?? bulk88
44
- Fixed, VC 6 build broken due to C syntax error since v0.76_02
55
http://git.io/vcZ08
6+
- Revised api-test for more binary diff stability between rebuilds
67

78
2015-05-26 Win32::API v0.82 bulk88
89
- Fixed, missing MANIFEST entry caused GCC 32 bit builds to be broken

MANIFEST

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ api-test/API_test.h
66
api-test/API_test.sln
77
api-test/API_test.suo
88
api-test/API_test.vcproj
9-
api-test/Debug/API_test.dll
10-
api-test/Debug/API_test.lib
9+
api-test/builddll.pl
1110
api-test/Makefile
12-
api-test/Release/API_test.dll
13-
api-test/Release/API_test.lib
14-
api-test/StdAfx.cpp
15-
api-test/StdAfx.h
11+
api-test/rtc.cpp
12+
api-test/rtc.def
13+
api-test/rtc.vcproj
14+
api-test/rtc64.def
1615
API.h
1716
API.pm
1817
API.xs
@@ -43,6 +42,8 @@ MANIFEST.SKIP
4342
META.yml
4443
ppport.h
4544
README
45+
rtc.dll
46+
rtc64.dll
4647
samples/callback1.pl
4748
samples/callback2.pl
4849
samples/callback3.pl

W32ATest.pm

+12-3
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,26 @@ sub compiler_version_from_shell () {
8686
sub find_test_dll {
8787
require File::Spec;
8888
my $dll;
89-
my $default_dll_name =
89+
my $test_dll_name =
9090
is_perl_64bit()
9191
? 'API_test64.dll'
9292
: 'API_test.dll';
9393

94-
my $dll_name = $_[0] || $default_dll_name;
94+
my $dll_name = $_[0] || $test_dll_name;
9595

9696
my @paths = qw(.. ../t ../t/dll . ./dll ./t/dll);
9797
while (my $path = shift @paths) {
9898
$dll = File::Spec->catfile($path, $dll_name);
99-
return $dll if -s $dll;
99+
if(-s $dll) { #preload the rtc dll to avoid changing PATH
100+
#leak the DLL, this is just unit testing
101+
die "can't load rtc DLL for API_test DLL"
102+
if ! Win32::API::LoadLibrary(
103+
File::Spec->catfile($path,
104+
is_perl_64bit()
105+
? 'rtc64.dll'
106+
: 'rtc.dll'));
107+
return $dll;
108+
}
100109
}
101110
return (undef);
102111
}

api-test/API_test.cpp

+81-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,38 @@
33
//
44
// $Id$
55

6-
#pragma pack(1)
7-
8-
#include "stdafx.h"
6+
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
7+
#include <windows.h>
98
#include <malloc.h>
109
#include <stdio.h>
1110
#include "API_test.h"
1211

12+
#ifdef _WIN64
13+
#define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232
14+
#else /* _WIN64 */
15+
#define DEFAULT_SECURITY_COOKIE 0xBB40E64E
16+
#endif /* _WIN64 */
17+
18+
extern "C" DWORD_PTR __security_cookie = DEFAULT_SECURITY_COOKIE;
19+
extern "C" __declspec(dllimport) DWORD_PTR rtc_security_cookie;
20+
1321
HMODULE g_hModule = NULL;
14-
BOOL APIENTRY DllMain( HANDLE hModule,
15-
DWORD ul_reason_for_call,
16-
LPVOID lpReserved
22+
BOOL APIENTRY ApiDllMain( HANDLE hModule,
23+
DWORD ul_reason_for_call,
24+
LPVOID lpReserved
1725
)
1826
{
1927
switch (ul_reason_for_call)
2028
{
2129
case DLL_PROCESS_ATTACH:
30+
/* it seems that __security_cookie cant be imported across DLLs,
31+
probably because an imported var has different machine code
32+
(2 reads instructions) vs a same DLL var which has 1 read, and VC doesnt
33+
listen to any C code declarations about __security_cookie and is hard coded
34+
to always use the 1 read variant
35+
API_test error LNK2019: unresolved external symbol ___security_cookie referenced in function _printf
36+
*/
37+
__security_cookie = rtc_security_cookie;
2238
g_hModule = (HMODULE)hModule;
2339
case DLL_THREAD_ATTACH:
2440
case DLL_THREAD_DETACH:
@@ -28,6 +44,36 @@ BOOL APIENTRY DllMain( HANDLE hModule,
2844
return TRUE;
2945
}
3046

47+
extern "C" __declspec(selectany) DWORD _fltused = 0;
48+
49+
/* CharUpperA not so efficient, calls LCMapStringW internally
50+
and LCMapStringW is slow compared to CRT toupper for low ascii */
51+
#define toupper(x) (char)CharUpperA((LPTSTR)x)
52+
#define sprintf wsprintf
53+
54+
int printf(const char * format, ...)
55+
{
56+
char buf[1024];
57+
int ret;
58+
DWORD NumberOfBytesWritten;
59+
va_list args;
60+
61+
va_start(args, format);
62+
/* wvsprintf is from user32.dll not any CRT,
63+
this makes API_test.dll almost CRT-free */
64+
ret = wvsprintf(buf, format, args );
65+
va_end( args );
66+
67+
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, ret,&NumberOfBytesWritten, NULL);
68+
/* not sure if ret is 100% accurate as real printf */
69+
return ret;
70+
}
71+
72+
/* know which VC version was used to build this DLL, helps in reproducibility
73+
and keeps git diff smallers when you use the same VC version */
74+
#define stringify(x) #x
75+
#define stringifym(x) stringify(x)
76+
extern API_TEST_API const char cc_name_str [] = "VC " stringifym(_MSC_VER);
3177

3278
// This is an example of an exported variable
3379
API_TEST_API int nAPI_test=0;
@@ -400,9 +446,10 @@ static const GUID wlanguid =
400446
#endif
401447
){
402448
DebugBreak();
449+
return ERROR_INVALID_ADDRESS;
403450
}
404451
else{
405-
return ERROR_SUCCESS;
452+
return ERROR_SUCCESS;
406453
}
407454
}
408455

@@ -477,3 +524,30 @@ API_TEST_API int __stdcall is_null(void * ptr){
477524
if(!ptr) return TRUE;
478525
else return FALSE;
479526
}
527+
528+
/* enlarge .text from Virtual Size 0xF34 which is pretty close to 0x1000,
529+
to 0x1024 Virtual size so that .text is 0x1400 bytes long in the PE file to
530+
allow room for API_test.dll to grow with large git diffs, .rdata starts at
531+
0x3000 instead of 0x2000 with this padding, which means pointers to .rdata
532+
won't change in future recompiles of this dll because there is atleast
533+
4 KB of space between the end of .text and start of .rdata
534+
535+
The amount of nulls bytes here can be reduced in the future if API_test
536+
gains new functions
537+
*/
538+
#pragma code_seg(push, ".text")
539+
__declspec(allocate(".text")) char code_padding []=
540+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
541+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
542+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
543+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
544+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
545+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
546+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
547+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
548+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
549+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
550+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
551+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
552+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
553+
#pragma code_seg(pop)

api-test/API_test.def

+1
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ EXPORTS
3939
GetTestDllHModule
4040
setlasterror_loop
4141
is_null
42+
cc_name_str

api-test/API_test.sln

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 10.00
3-
# Visual Studio 2008
4-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "API_test", "API_test.vcproj", "{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}"
1+
Microsoft Visual Studio Solution File, Format Version 8.00
2+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "API_test", "API_test.vcproj", "{EBD26AB1-6E08-4984-9C82-026A00BB055B}"
3+
ProjectSection(ProjectDependencies) = postProject
4+
EndProjectSection
5+
EndProject
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "rtc", "rtc.vcproj", "{3257D16D-21B0-4BE8-A76E-1E0589B3700F}"
7+
ProjectSection(ProjectDependencies) = postProject
8+
EndProjectSection
59
EndProject
610
Global
7-
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8-
Debug|Win32 = Debug|Win32
9-
Debug|x64 = Debug|x64
10-
Release|Win32 = Release|Win32
11-
Release|x64 = Release|x64
11+
GlobalSection(SolutionConfiguration) = preSolution
12+
Debug = Debug
13+
Release = Release
14+
EndGlobalSection
15+
GlobalSection(ProjectDependencies) = postSolution
16+
EndGlobalSection
17+
GlobalSection(ProjectConfiguration) = postSolution
18+
{EBD26AB1-6E08-4984-9C82-026A00BB055B}.Debug.ActiveCfg = Debug|Win32
19+
{EBD26AB1-6E08-4984-9C82-026A00BB055B}.Debug.Build.0 = Debug|Win32
20+
{EBD26AB1-6E08-4984-9C82-026A00BB055B}.Release.ActiveCfg = Release|Win32
21+
{EBD26AB1-6E08-4984-9C82-026A00BB055B}.Release.Build.0 = Release|Win32
22+
{3257D16D-21B0-4BE8-A76E-1E0589B3700F}.Debug.ActiveCfg = Debug|Win32
23+
{3257D16D-21B0-4BE8-A76E-1E0589B3700F}.Debug.Build.0 = Debug|Win32
24+
{3257D16D-21B0-4BE8-A76E-1E0589B3700F}.Release.ActiveCfg = Release|Win32
25+
{3257D16D-21B0-4BE8-A76E-1E0589B3700F}.Release.Build.0 = Release|Win32
1226
EndGlobalSection
13-
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Debug|Win32.ActiveCfg = Debug|Win32
15-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Debug|Win32.Build.0 = Debug|Win32
16-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Debug|x64.ActiveCfg = Debug|x64
17-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Debug|x64.Build.0 = Debug|x64
18-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Release|Win32.ActiveCfg = Release|Win32
19-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Release|Win32.Build.0 = Release|Win32
20-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Release|x64.ActiveCfg = Release|x64
21-
{DB35FD31-F92D-4A13-A382-0CD3C6A78F5B}.Release|x64.Build.0 = Release|x64
27+
GlobalSection(ExtensibilityGlobals) = postSolution
2228
EndGlobalSection
23-
GlobalSection(SolutionProperties) = preSolution
24-
HideSolutionNode = FALSE
29+
GlobalSection(ExtensibilityAddIns) = postSolution
2530
EndGlobalSection
2631
EndGlobal

0 commit comments

Comments
 (0)