Jump to content
Tuts 4 You

[Question] Statically linking TitanEngine?


mrexodia

Recommended Posts

Posted (edited)

Hello everyone,

I'm currently coding a project that uses TitanEngine, but because of portability issues I want to link everything statically...

The problem is that the guide provided here doesn't really work cc_confused.gif

At first I tried compiling the original DLL version, to test if the code was correct. It wasn't (1>.\TitanEngine.rc(10) : fatal error RC1015: cannot open include file 'afxres.h'.) and I "fixed" it by replacing "afxres.h" by "windows.h"...

After that another error popped up because the author forgot to do: "#define IDC_STATIC -1". I defined it and the DLL compiles (and runs) without errors...

Then I changed the output type to .lib (Static Library), compiled without errors and changed my project settings to link the static library.

But now: when I hit compile on my original project (MingW C++ and CodeBlocks) I get all kinds of "missing references":

||=== ArmaCertTool, Release ===|
||Warning: .drectve `/DEFAULTLIB:"aplib.lib" /DEFAULTLIB:"libcpmt" /DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" /EXPORT:_ConvertVAtoFileOffsetEx@24 /EXPORT:_TranslateNativeName@4 /EXPORT:_DumpProcess@16 /EXPORT:_DumpProcessW@16 /EXPORT:_DumpProcessEx@16 /EXPORT:_DumpProcessExW@16 /EXPORT:_DumpMemory@16 /EXPORT:_DumpMemoryW@16 /EXPORT:_DumpMemoryEx@16 /EXPORT:_DumpMemoryExW@16 /EXPORT:_DumpRegions@12 /EXPORT:_DumpRegionsW@12 /EXPORT:_DumpRegionsEx@12 /EXPORT:_DumpRegionsExW@12 /EXPORT:_DumpModule@12 /EXPORT:_D|
||Warning: .drectve `/DEFAULTLIB:"aplib.lib" /DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" ' unrecognized|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleInformation@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetProcessImageFileNameA@12'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `@__security_check_cookie@4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_except_handler4'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `_chkstk'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `__security_cookie'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `EnumProcessModules@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleBaseNameA@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleFileNameExA@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleFileNameExA@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleBaseNameA@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleInformation@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleBaseNameA@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleInformation@16'|
sdk\TitanEngine.lib(.\Release\TitanEngine.obj)|| undefined reference to `GetModuleBaseNameA@16'|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 2 warnings (0 minutes, 11 seconds) ===|

I was able to fix the psapi.dll references by adding "psapi.lib" to the static library. the __security_cookie references could be fixed by adding "BufferOverflowU.lib" (from the WinDDK) to the project, but how to fix _chkstk and _except_handler4 references I googled a long time for this but I can't see in which library those functions are :s

I hope some1 here knows what to do (maybe I specified wrong arguments while compiling the static library)

Greetings,

Mr. eXoDia

PS I attached "BufferOverflowU.lib" for the ones who want to test and don't like dloading 650mb biggrin.png

BufferOverflowU.rar

Edited by Mr. eXoDia
Posted

Thanks ap0x, you're the actual author right??? I really appreciate your hard work on this project! Unfortunately the static build that you provided gives me the same errors as when I build it myself... Could it be that I link the program in the wrong order???

Greetings,

Mr. eXoDia

Posted

Probably you need to compile distorm and aplib with MingW C++. Every lib should be compiled/linked with the same compiler/linker.

Everthing works fine with Visual Studio 2008/2010, maybe you want to switch to Visual Studio 2010 Express, still a great IDE.

Posted (edited)

As has already been suggested, GCC and MSVC can cause troubles when used with each other.

_except_handler4

http://web17.webbpro...eption-handling

Time to discuss compiler based SEH, the exception handling used when using try/throw/catch (Msvcrt) and try/except/finally (C++) kewords. They are handled by the compiler, and do result in a custom exception handling way. Visual C++ uses an extended SEH frame, which points to the only one exception handler Msvcr80!_except_handler4. This special SEH handler exists (of course) per thread, because each thread has its own Thread Information Block. It is important to know that we are leaving here Windows internal exception handling, but this (compiler based SEH) is used with every modern application, although it must not.

__except_handler4 gets all the exceptions and determines where (in which function) the exception occurs, and calls the catch/except function blocks of that function. For doing that determination there exists the scopetable, which address is given in the extended SEH frame:

http://sourceforge.n...435&atid=102435

_chkstk

Details:

Background: Windows defines a function called _chkstk, calls to which are automatically inserted by the compiler at the beginning of functions that use more than a certain amount of stack space. GCC also defines such a function; it was formerly called _alloca and is now called __chkstk, presumably for better Windows compatibility.

Problem: When creating an object file with GCC under MSYS/MinGW, it may include undefined calls to __chkstk. However, linking this using another compiler (such as MS Visual Studio) is not possible, because in Windows, the function is called _chkstk (note: only one underscore, not two).

So what was meant to be compatible, is not really.

To be clear: in GCC, the function is called __chkstk (with two underscores), but the symbol in the object code is called ___chkstk (with 3 underscores), because of the extra "_" added to every symbol.

In Windows, the function is called _chkstk (with one underscore), but the symbol in the object code is called __chkstk (with 2 underscores), because of the extra "_" added to all symbols.

Presumably this lead to the confusion in the first place.

Suggested fix: the source file in question is gcc-3.4.4/gcc/config/i386/msys.asm.

Replace "___chkstk" (3 underscores) by "__chkstk" (2 underscores). Note that this has

already been done in another source file uwin.asm.

Presumably also GCC itself would need to be updated to insert calls to _chkstk and not __chkstk.

If backward compatibility is desired, ___chkstk can be retained as an alias for __chkstk

HR,

Ghandi

Edited by ghandi
Posted

Hey guys,

Thanks for all the suggestions! I'm currently porting TitanEngine fully to MingW :) I "fixed" all the exception handlers and currently I'm busy with all the "sizeof" macros which MingW requires to have the argument in braces "sizeof(something)" currently it's "sizeof something".

I'll post my results (ported TI) here if I'm done with the uber-boring work :)

Greetings,

Mr. eXoDia

Posted (edited)

Hello everyone,

I ported the whole TitanEngine DLL to MingW and here is my result (unfortunately it isn't working)

Greetings,

Mr. eXoDia

TitanEngine_MingW.rar

Edited by Mr. eXoDia
  • Like 1
  • 3 months later...
Posted

Wow, nice work! biggrin.png

I have a better version somewhere on my HDD, pm if you want it :P

Greetings

Posted (edited)

Speaking of that, I got a question.

For some reason, AddSection or AddSectionEx fails on the following file. For some odd reason, on init the file fails to load after the PE section is added.

Using Win7 x64 SP1. Weirdly enough, some MASM files I tried get the section contents added fine.

Any ideas?

EDIT: Tried some more files, including MSVC compiled files, and they work great. Which is extremely odd.

test.rar

Edited by mudlord
Posted

@mudlord: Try this exe some time... It adds a section to your calc.exe without many problems. Try also realigning the file...


/>http://www.mediafire.com/?9z7k2l719sba18n (MingW built exe+sources)

Greetings

PS I'm not on win7 btw :P

Posted (edited)

Fails to load on Windows 7 x64, before the main entry point is hit.

Other EXE's work great. Bug in TitanEngine perhaps?

EDIT: Seems on Win7, a realign is required to allow that particular file to work with the appended PE section. Strange how on XP it doesnt. Anyway, problem solved, will always realign when doing the PE modifications like the compression.

Edited by mudlord

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...