├── LibGen
├── LibGen.def
├── VerInfo.rc
├── LibFactory.h
├── HowTo.txt
├── LibInterfaces.h
├── LibGen.vcxproj.filters
├── testmain.cpp
├── LibGen.vcxproj
└── LibImpl.cpp
├── VerInfo.rc
├── CoffGen
├── VerInfo.rc
├── CoffGen.def
├── CoffGen.vcxproj.user
├── cofffactory.h
├── HowTo.txt
├── CoffGen.vcxproj.filters
├── testmain.cpp
├── coffInterfaces.h
├── CoffGen.vcxproj
└── coffImpl.cpp
├── ImpGen
├── VerInfo.rc
├── resource.h
├── ImpGen.def
├── ImpFactory.h
├── HowTo.txt
├── ImpGen.vcxproj.filters
├── ImpInterfaces.h
├── testmain.cpp
├── ImpGen.vcxproj
└── impImpl.cpp
├── MakeImpLib
├── VerInfo.rc
├── MakeImpLib.vcxproj.filters
├── MakeImpLib.vcxproj
└── main.cpp
├── ImpLibFix
├── ImpLibFix.def
├── ImpLibFix.vcxproj.user
├── ImpLibFix.h
├── testmain.cpp
├── ImpLibFix.vcxproj.filters
├── ImpLibFix.cpp
└── ImpLibFix.vcxproj
├── LibGenHelper
├── VerInfo.rc
├── LibGenHelper.def
├── LibGenHelperFactory.h
├── LibGenHelperInterfaces.h
├── HowTo.txt
├── LibGenHelper.vcxproj.filters
├── LibGenHelperImpl.cpp
└── LibGenHelper.vcxproj
├── LICENSE
├── README
└── implib.sln
/LibGen/LibGen.def:
--------------------------------------------------------------------------------
1 | LIBRARY LibGen
2 | EXPORTS
3 | CreateLibraryBuilder
4 |
--------------------------------------------------------------------------------
/VerInfo.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sorayuki/implibGenerator/HEAD/VerInfo.rc
--------------------------------------------------------------------------------
/CoffGen/VerInfo.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sorayuki/implibGenerator/HEAD/CoffGen/VerInfo.rc
--------------------------------------------------------------------------------
/ImpGen/VerInfo.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sorayuki/implibGenerator/HEAD/ImpGen/VerInfo.rc
--------------------------------------------------------------------------------
/ImpGen/resource.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sorayuki/implibGenerator/HEAD/ImpGen/resource.h
--------------------------------------------------------------------------------
/LibGen/VerInfo.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sorayuki/implibGenerator/HEAD/LibGen/VerInfo.rc
--------------------------------------------------------------------------------
/MakeImpLib/VerInfo.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sorayuki/implibGenerator/HEAD/MakeImpLib/VerInfo.rc
--------------------------------------------------------------------------------
/ImpLibFix/ImpLibFix.def:
--------------------------------------------------------------------------------
1 | LIBRARY ImpLibFix
2 | EXPORTS
3 | GetMaxNameLength
4 | RenameImpLibObjects
5 |
--------------------------------------------------------------------------------
/LibGenHelper/VerInfo.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sorayuki/implibGenerator/HEAD/LibGenHelper/VerInfo.rc
--------------------------------------------------------------------------------
/ImpGen/ImpGen.def:
--------------------------------------------------------------------------------
1 | LIBRARY ImpGen
2 | EXPORTS
3 | GetX86ImpSectionBuilder
4 | GetX64ImpSectionBuilder
5 |
--------------------------------------------------------------------------------
/LibGenHelper/LibGenHelper.def:
--------------------------------------------------------------------------------
1 | LIBRARY LibGenHelper
2 | EXPORTS
3 | CreateX86ImpLibBuilder
4 | CreateX64ImpLibBuilder
5 |
--------------------------------------------------------------------------------
/CoffGen/CoffGen.def:
--------------------------------------------------------------------------------
1 | LIBRARY CoffGen
2 | EXPORTS
3 | GetX86CoffFactory
4 | GetX64CoffFactory
5 | GetIA64CoffFactory
6 |
--------------------------------------------------------------------------------
/CoffGen/CoffGen.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LibGen/LibFactory.h:
--------------------------------------------------------------------------------
1 | #ifndef LIBFACTORY_H
2 | #define LIBFACTORY_H
3 |
4 | #include "LibInterfaces.h"
5 |
6 | namespace Sora
7 | {
8 | extern "C" ILibraryBuilder* WINAPI CreateLibraryBuilder();
9 | };
10 |
11 | #endif
12 |
--------------------------------------------------------------------------------
/ImpGen/ImpFactory.h:
--------------------------------------------------------------------------------
1 | #ifndef IMPFACTORY_H
2 | #define IMPFACTORY_H
3 |
4 | #include "ImpInterfaces.h"
5 |
6 | namespace Sora {
7 | extern "C" {
8 | IImpSectionBuilder* WINAPI GetX86ImpSectionBuilder();
9 | IImpSectionBuilder* WINAPI GetX64ImpSectionBuilder();
10 | }
11 | };
12 |
13 | #endif
14 |
--------------------------------------------------------------------------------
/CoffGen/cofffactory.h:
--------------------------------------------------------------------------------
1 | #ifndef COFFFACTORIES_H
2 | #define COFFFACTORIES_H
3 |
4 | #include "coffInterfaces.h"
5 |
6 | namespace Sora {
7 | extern "C" {
8 | ICoffFactory* WINAPI GetX86CoffFactory();
9 | ICoffFactory* WINAPI GetX64CoffFactory();
10 | ICoffFactory* WINAPI GetIA64CoffFactory();
11 | }
12 | };
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/ImpLibFix/ImpLibFix.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(OutputPath)
5 | WindowsLocalDebugger
6 |
7 |
--------------------------------------------------------------------------------
/LibGenHelper/LibGenHelperFactory.h:
--------------------------------------------------------------------------------
1 | #ifndef LIBGENHELPERFACTORY_H
2 | #define LIBGENHELPERFACTORY_H
3 |
4 | #include "LibGenHelperInterfaces.h"
5 |
6 | namespace Sora
7 | {
8 | extern "C" IImportLibraryBuilder* WINAPI CreateX86ImpLibBuilder(LPCSTR szDllName, LPCSTR szMemberName);
9 | extern "C" IImportLibraryBuilder* WINAPI CreateX64ImpLibBuilder(LPCSTR szDllName, LPCSTR szMemberName);
10 | };
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/ImpLibFix/ImpLibFix.h:
--------------------------------------------------------------------------------
1 | #ifndef IMPLIBFIX_H
2 | #define IMPLIBFIX_H
3 |
4 | #include
5 |
6 | namespace Sora {
7 | //not include the \0
8 | extern "C" int WINAPI GetMaxNameLength();
9 |
10 | //return: how many members renamed.
11 | //first link member and second link member and longname member won't be renamed
12 | extern "C" int WINAPI RenameImpLibObjects(LPCSTR szNewName, PBYTE pData, int nDataLen);
13 | };
14 |
15 | #endif
16 |
--------------------------------------------------------------------------------
/ImpLibFix/testmain.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "ImpLibFix.h"
3 |
4 | int main()
5 | {
6 | FILE* f = fopen("ims.lib", "rb+");
7 | fseek(f, 0, SEEK_END);
8 | int fs = ftell(f);
9 | fseek(f, 0, SEEK_SET);
10 |
11 | PBYTE buf = new BYTE[fs];
12 | fread(buf, 1, fs, f);
13 |
14 | Sora::RenameImpLibObjects("member/", buf, fs);
15 |
16 | fseek(f, 0, SEEK_SET);
17 | fwrite(buf, 1, fs, f);
18 |
19 | fclose(f);
20 |
21 | delete[] buf;
22 | }
23 |
--------------------------------------------------------------------------------
/ImpGen/HowTo.txt:
--------------------------------------------------------------------------------
1 | This module is used for build sections for import data from DLL.
2 |
3 | How to use:
4 | 1. Create CoffBuilder object by using the CoffGen module
5 | 2. Call methods of ImpSectionBuilder to add section(s) to CoffBuilder
6 | 3. Call PushRelocs of CoffBuilder object
7 | 4. Get data from CoffBuilder object
8 |
9 | I think the method name is self-explaining.
10 |
11 | Recommend usage:
12 | Put ImportDescriptor, ImportThunk, NullDescriptor, NullThunk in different coff object files,
13 | or I don't know if it works.
14 |
--------------------------------------------------------------------------------
/LibGen/HowTo.txt:
--------------------------------------------------------------------------------
1 | This module is used for creating Object Archive (library, .lib) file.
2 |
3 | How to use:
4 | 1. Create CoffBuilder object by using CoffGen module, and add section to it
5 | 2. Call PushRelocs of CoffBuilder object, like what you do before save this object file
6 | 3. Call AddObject method of LibraryBuilder object with member name. For import library, all member name is the same.
7 | 4. Call FillOffsets to calculate and fill all member's offset(file pointer) for first link member and second link member
8 | 5. Get raw data from LibraryBuilder object and save them into file.
9 |
--------------------------------------------------------------------------------
/LibGen/LibInterfaces.h:
--------------------------------------------------------------------------------
1 | #ifndef LIBINTERFACES_H
2 | #define LIBINTERFACES_H
3 |
4 | #include "coffInterfaces.h"
5 |
6 | namespace Sora
7 | {
8 | class ILibraryBuilder : public IDispose, public IHasRawData
9 | {
10 | public:
11 | //the name is limited to 14 bytes. No longname is supported.
12 | virtual void WINAPI AddObject(LPCSTR szName, ICoffBuilder*) = 0;
13 |
14 | //call this method to calculate the offset for first and second link member before retrive raw data
15 | virtual void WINAPI FillOffsets() = 0;
16 | };
17 | };
18 |
19 | #endif
20 |
--------------------------------------------------------------------------------
/LibGenHelper/LibGenHelperInterfaces.h:
--------------------------------------------------------------------------------
1 | #ifndef LIBGENHELPERINTERFACES_H
2 | #define LIBGENHELPERINTERFACES_H
3 |
4 | #include "coffInterfaces.h"
5 |
6 | namespace Sora
7 | {
8 | class IImportLibraryBuilder : public IHasRawData, public IDispose
9 | {
10 | public:
11 | //szImpName: __imp__Sleep@8
12 | //szFuncName: _Sleep@8, can be null, will not generate stub
13 | //szImportName: Sleep
14 | virtual void WINAPI AddImportFunctionByName(LPCSTR szImpName, LPCSTR szFuncName, LPCSTR szImportName) = 0;
15 | virtual void WINAPI AddImportFunctionByOrdinal(LPCSTR szImpName, LPCSTR szFuncName, int nOrdinal) = 0;
16 |
17 | //call after add all import items
18 | virtual void WINAPI Build() = 0;
19 |
20 | //hint means ordinal
21 | virtual void WINAPI AddImportFunctionByNameWithHint(LPCSTR szImpName, LPCSTR szFuncName, LPCSTR szImportName, int nOrdinal) = 0;
22 | };
23 | };
24 |
25 | #endif
26 |
--------------------------------------------------------------------------------
/CoffGen/HowTo.txt:
--------------------------------------------------------------------------------
1 | This module is used for build Coff Object File.
2 |
3 | How to build a coff file using this module:
4 |
5 | 1. Get the proper factory to create other objects.
6 | 2. Create a CoffBuilder object
7 | 3. Create SectionBuilder object
8 | 4. Add the SectionBuilder object to CoffBuilder
9 | 5. Set section name and characteristic, add data to the section
10 | 6. Add necessary symbols to symbol table of CoffBuilder
11 | 7. Call PushRelocs to generate necessary symbols from relocation table
12 | 8. Call methods of CoffBuilder object from IHasRawData to get the Coff Object File's raw data
13 | 9. Save the data to file, then you get a Coff Object File.
14 |
15 | Notice:
16 | 1. If you need aux symbols, create these symbols by CreateAuxSymbol method from SectionBuilder
17 | 2. Currently longname section is not supported
18 | 3. SymbolTable and StringTable is automately created during creating the CoffBuilder object,
19 | RelocationTable is automately created during creating the SectionBuilder object.
20 |
--------------------------------------------------------------------------------
/MakeImpLib/MakeImpLib.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
23 |
24 | 资源文件
25 |
26 |
27 |
--------------------------------------------------------------------------------
/LibGenHelper/HowTo.txt:
--------------------------------------------------------------------------------
1 | This module offsets simple interfaces to create import library.
2 |
3 | How to use:
4 | 1. Create a ImportLibraryBuilder object, specified the dll's filename and the member name in library (often the same as dll's name, but others is also accepted).
5 | 2. Add import functions.
6 | 3. Call Builde method.
7 | 4. Get raw data from the object and save them into file.
8 |
9 | Notice:
10 | szFuncName can be null (0), for that the function stub is not a must.
11 |
12 | What is function stub:
13 |
14 | When calling a function from DLL, there are two ways declare the function,
15 | take Sleep as example,
16 | 1. extern "C" void __stdcall Sleep(unsigned int);
17 | 2. extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned int);
18 | For 1, the linker will link it to _Sleep@8 symbol, which is a function;
19 | for 2, the linker will link it to __imp__Sleep@8 symbol, which is a function pointer.
20 |
21 | In x86 architecture, the first one will generate the code
22 | call _Sleep@8
23 | and the second one will generate the code
24 | call dword ptr [__imp__Sleep@8]
25 | Then what happened? the first one will jump to a function like this
26 | jmp dword ptr [__imp__Sleep@8]
27 |
28 | So they work as the same.
29 | But for 1, it's a must for _Sleep@8 existing.
30 |
--------------------------------------------------------------------------------
/ImpLibFix/ImpLibFix.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 | 源文件
23 |
24 |
25 |
26 |
27 | 头文件
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2016, sorayuki
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions are met:
6 |
7 | * Redistributions of source code must retain the above copyright notice, this
8 | list of conditions and the following disclaimer.
9 |
10 | * Redistributions in binary form must reproduce the above copyright notice,
11 | this list of conditions and the following disclaimer in the documentation
12 | and/or other materials provided with the distribution.
13 |
14 | * Neither the name of implibGenerator nor the names of its
15 | contributors may be used to endorse or promote products derived from
16 | this software without specific prior written permission.
17 |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
--------------------------------------------------------------------------------
/LibGenHelper/LibGenHelper.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | 资源文件
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ImpGen/ImpGen.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 | 源文件
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | 资源文件
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/LibGen/LibGen.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 | 源文件
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | 资源文件
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/CoffGen/CoffGen.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 源文件
6 |
7 |
8 | 源文件
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | {df62f9c9-d624-47ec-81af-aa65e5bb8c2a}
17 | h;hh;hpp;hxx;hm;inl;inc;xsd
18 |
19 |
20 | {f91026dd-fe0b-4127-93c1-b9d585a41855}
21 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
22 |
23 |
24 | {10571c3b-10d8-4883-a5a8-4d7a8f8b5547}
25 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
26 |
27 |
28 |
29 |
30 | 头文件
31 |
32 |
33 | 头文件
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | 资源文件
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ImpGen/ImpInterfaces.h:
--------------------------------------------------------------------------------
1 | #ifndef IMPINTERFACES_H
2 | #define IMPINTERFACES_H
3 |
4 | #include "coffInterfaces.h"
5 |
6 | namespace Sora {
7 | class IImpSectionBuilder {
8 | public:
9 | virtual ICoffFactory* GetCoffFactory() = 0;
10 |
11 | //the pointer length. 4 for x86 and 8 for x64
12 | virtual int GetPtrSize() = 0;
13 |
14 | virtual void WINAPI BuildImportDescriptor(LPCSTR szDllName, ICoffBuilder*) = 0;
15 |
16 | //szImpName: __imp__xxx@nn, referred by linker
17 | //szDllExpName: the name that exported from dll
18 | //szFuncName: _xxx@nn, used if declared without __declspec(dllimport). will not generated if szStubName is NULL
19 | //include Thunk, OriginalThunk, importbyname
20 | virtual void WINAPI BuildImportByNameThunk(LPCSTR szDllName, LPCSTR szImpName, LPCSTR szFuncName, LPCSTR szDllExpName, ICoffBuilder*) = 0;
21 | virtual void WINAPI BuildImportByOrdinalThunk(LPCSTR szDllName, LPCSTR szImpName, LPCSTR szFuncName, WORD szDllExpOrdinal, ICoffBuilder*) = 0;
22 |
23 | virtual void WINAPI BuildNullThunk(LPCSTR szDllName, ICoffBuilder*) = 0;
24 | virtual void WINAPI BuildNullDescriptor(ICoffBuilder*) = 0;
25 |
26 | //import by name, but with [hint of import_by_name] = nDllExpOrdinal
27 | //when szDllExpName = NULL, it equals BuildImportByOrdinalThunk
28 | //when nDllExpOrdinal = 0, it equals BuildImportByNameThunk
29 | //don't let both = 0 (NULL)
30 | virtual void WINAPI BuildImportThunk(LPCSTR szDllName, LPCSTR szImpName, LPCSTR szFuncName, LPCSTR szDllExpName, WORD nDllExpOrdinal, ICoffBuilder* cb) = 0;
31 | };
32 | }
33 |
34 | #endif
35 |
--------------------------------------------------------------------------------
/ImpGen/testmain.cpp:
--------------------------------------------------------------------------------
1 | #include "../CoffGen/coffInterfaces.h"
2 | #include "../CoffGen/cofffactory.h"
3 | #include "ImpInterfaces.h"
4 | #include "ImpFactory.h"
5 |
6 | using namespace Sora;
7 |
8 | bool SaveCoff(LPCTSTR fn, ICoffBuilder* cb)
9 | {
10 | cb->PushRelocs();
11 |
12 | HANDLE hFile = CreateFile(fn, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
13 | if (hFile != INVALID_HANDLE_VALUE) {
14 | int len = cb->GetDataLength();
15 | BYTE* buf = new BYTE[len];
16 | cb->GetRawData(buf);
17 |
18 | DWORD olen;
19 | WriteFile(hFile, buf, len, &olen, 0);
20 | CloseHandle(hFile);
21 |
22 | return olen == len;
23 | }
24 | return false;
25 | }
26 |
27 | int main()
28 | {
29 | IImpSectionBuilder* isf = GetX86ImpSectionBuilder();
30 | ICoffFactory* cf = isf->GetCoffFactory();
31 |
32 | LPCSTR dllname = "a.dll";
33 |
34 | ICoffBuilder* desc = cf->CreateCoffBuilder();
35 | isf->BuildImportDescriptor(dllname, desc);
36 | SaveCoff(TEXT("_impdesc.obj"), desc);
37 | desc->Dispose();
38 |
39 | ICoffBuilder* NullThunk = cf->CreateCoffBuilder();
40 | isf->BuildNullThunk(dllname, NullThunk);
41 | SaveCoff(TEXT("_NullThunk.obj"), NullThunk);
42 | NullThunk->Dispose();
43 |
44 | ICoffBuilder* impadd = cf->CreateCoffBuilder();
45 | isf->BuildImportByNameThunk(dllname, "__imp__add@8", "_add@8", "add", impadd);
46 | SaveCoff(TEXT("func1.obj"), impadd);
47 | impadd->Dispose();
48 |
49 | ICoffBuilder* impsub = cf->CreateCoffBuilder();
50 | isf->BuildImportByNameThunk(dllname, "__imp__sub@8", "_sub@8", "sub", impsub);
51 | SaveCoff(TEXT("func2.obj"), impsub);
52 | impsub->Dispose();
53 |
54 | ICoffBuilder* nuldesc = cf->CreateCoffBuilder();
55 | isf->BuildNullDescriptor(nuldesc);
56 | SaveCoff(TEXT("nuldesc.obj"), nuldesc);
57 | nuldesc->Dispose();
58 |
59 | return 0;
60 | }
61 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | This tool is inspired by implib sdk project( http://implib.sourceforge.net ).
2 |
3 | For "stdcall" convention exported from DLL file without decoration
4 | (prefix, suffix, like _ and @4, @8, etc) like Windows APIs, it seems to be
5 | not easy to get an import library by yourself. The LIB.exe tool from VC can
6 | construct .lib from .def but it doesn't allow importing without decoration
7 | and linking with decoration.
8 |
9 | For example, I want to use Sleep function from kernel32.dll. I would like to
10 | build an import library from a def file like this:
11 |
12 | LIBRARY kernel32.dll
13 | EXPORTS
14 | Sleep
15 |
16 | and create it by
17 |
18 | lib /def:k32.def /out:k32.lib
19 |
20 | but when linking, it will say
21 | error LNK2019: unresolved external symbol _Sleep@4 referenced
22 | ( use "cl test.c k32.lib /link /nodefaultlib" to have this test please. )
23 |
24 | The linker looks for _Sleep@4 instead of Sleep.
25 |
26 | But if the .lib is build from this:
27 |
28 | LIBRARY kernel32.dll
29 | EXPORTS
30 | Sleep@4
31 |
32 | The build exe file won't run because there is no function named Sleep@4
33 | in kernel32.dll.
34 |
35 | So I need a .lib that imports Sleep from kernel32.dll and offers _Sleep@4
36 | for linking.
37 |
38 | ImpLib SDK project make use of fasm preprocess macros to construct a binary
39 | formed import library of DLL. It's very amazing.
40 |
41 | After some trying, I found it has some limitation, like long DLL file name is
42 | not supported, unicode function name is not supported, no x86-64 architecture
43 | supported, etc.
44 |
45 | But I have a lack of knowledge of fasm macro usage. To overcome these
46 | limitation, I decided to write my own.
47 |
48 | The structure of .obj file and .lib file is from Microsoft:
49 | http://download.microsoft.com/download/e/b/a/eba1050f-a31d-436b-9281-92cdfeae4b45/pecoff.doc
50 |
51 | No dependency library is required except standard c runtime and windows sdk.
52 |
53 | LeiMing (sorayuki)
54 | 2016-01-27
--------------------------------------------------------------------------------
/LibGen/testmain.cpp:
--------------------------------------------------------------------------------
1 | #include "LibInterfaces.h"
2 | #include "LibFactory.h"
3 |
4 | #include "../CoffGen/coffInterfaces.h"
5 | #include "../ImpGen/ImpInterfaces.h"
6 | #include "../ImpGen/ImpFactory.h"
7 |
8 | using namespace Sora;
9 |
10 | bool SaveRawData(LPCTSTR fn, IHasRawData* cb)
11 | {
12 | HANDLE hFile = CreateFile(fn, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
13 | if (hFile != INVALID_HANDLE_VALUE) {
14 | int len = cb->GetDataLength();
15 | BYTE* buf = new BYTE[len];
16 | cb->GetRawData(buf);
17 |
18 | DWORD olen;
19 | WriteFile(hFile, buf, len, &olen, 0);
20 | CloseHandle(hFile);
21 |
22 | return olen == len;
23 | }
24 | return false;
25 | }
26 |
27 | int main()
28 | {
29 | IImpSectionBuilder* isf = GetX86ImpSectionBuilder();
30 | ICoffFactory* cf = isf->GetCoffFactory();
31 |
32 | LPCSTR dllname = "a.dll";
33 |
34 | ICoffBuilder* desc = cf->CreateCoffBuilder();
35 | isf->BuildImportDescriptor(dllname, desc);
36 | desc->PushRelocs();
37 |
38 | ICoffBuilder* NullThunk = cf->CreateCoffBuilder();
39 | isf->BuildNullThunk(dllname, NullThunk);
40 | NullThunk->PushRelocs();
41 |
42 | ICoffBuilder* impadd = cf->CreateCoffBuilder();
43 | isf->BuildImportByNameThunk(dllname, "__imp__add@8", "_add@8", "add", impadd);
44 | impadd->PushRelocs();
45 |
46 | ICoffBuilder* impsub = cf->CreateCoffBuilder();
47 | isf->BuildImportByNameThunk(dllname, "__imp__sub@8", "_sub@8", "sub", impsub);
48 | impsub->PushRelocs();
49 |
50 | ICoffBuilder* nuldesc = cf->CreateCoffBuilder();
51 | isf->BuildNullDescriptor(nuldesc);
52 | nuldesc->PushRelocs();
53 |
54 | ILibraryBuilder* lib = CreateLibraryBuilder();
55 | lib->AddObject("a.dll", desc);
56 | lib->AddObject("a.dll", nuldesc);
57 | lib->AddObject("a.dll", impadd);
58 | lib->AddObject("a.dll", impsub);
59 | lib->AddObject("a.dll", NullThunk);
60 |
61 | lib->FillOffsets();
62 |
63 | SaveRawData(TEXT("as.lib"), lib);
64 |
65 | return 0;
66 | }
67 |
--------------------------------------------------------------------------------
/CoffGen/testmain.cpp:
--------------------------------------------------------------------------------
1 | #include "coffinterfaces.h"
2 | #include "cofffactory.h"
3 | #include
4 | #include
5 | using namespace Sora;
6 |
7 | #define OFFSET(stru, memb) ((DWORD)&((stru*)0)->memb)
8 |
9 | int main()
10 | {
11 | ICoffFactory* fac = GetX86CoffFactory();
12 | ICoffBuilder* coff = fac->CreateCoffBuilder();
13 | ISectionBuilder* id2 = fac->CreateSectionBuilder();
14 | coff->AppendSection(id2);
15 |
16 | id2->SetName(".idata$2");
17 | id2->SetCharacteristics(SECH_READ | SECH_WRITE | SECH_ALIGN4);
18 |
19 | IMAGE_IMPORT_DESCRIPTOR iid;
20 | ZeroMemory(&iid, sizeof(iid));
21 |
22 | IRelocatableVar* relVar[3];
23 | relVar[0] = fac->CreateRelocatableVar();
24 | relVar[0]->Set("dllName", id2, OFFSET(IMAGE_IMPORT_DESCRIPTOR, Name), 0, RVARelocate);
25 | relVar[1] = fac->CreateRelocatableVar();
26 | relVar[1]->Set("FirstThunk", id2, OFFSET(IMAGE_IMPORT_DESCRIPTOR, FirstThunk), 4, RVARelocate);
27 | relVar[2] = fac->CreateRelocatableVar();
28 | relVar[2]->Set("OriginalFirstThunk", id2, OFFSET(IMAGE_IMPORT_DESCRIPTOR, OriginalFirstThunk), 4, RVARelocate);
29 | id2->AppendData((PBYTE)&iid, sizeof(iid), relVar, 3);
30 |
31 | ISectionBuilder* id5 = fac->CreateSectionBuilder();
32 | id5->SetName(".idata$5");
33 | id5->SetCharacteristics(SECH_READ | SECH_WRITE | SECH_ALIGN4);
34 | coff->AppendSection(id5);
35 | DWORD iatval = 0;
36 | coff->GetSymbolTableBuilder()->AddSymbol(id5, 0, "FirstThunk", SYST_STATIC, 0);
37 | relVar[0] = fac->CreateRelocatableVar();
38 | relVar[0]->Set("LookUp_add", id5, 0, 0, RVARelocate);
39 | id5->AppendData((PBYTE)&iatval, sizeof(DWORD), relVar, 1);
40 | id5->AppendData((PBYTE)&iatval, sizeof(DWORD), 0, 0);
41 | coff->GetSymbolTableBuilder()->AddSymbol(id5, 0, "__imp__add@8", SYST_EXTERN, 0);
42 |
43 | ISectionBuilder* id4 = fac->CreateSectionBuilder();
44 | id4->SetName(".idata$4");
45 | id4->SetCharacteristics(SECH_READ | SECH_WRITE | SECH_ALIGN4);
46 | coff->AppendSection(id4);
47 | relVar[0] = fac->CreateRelocatableVar();
48 | relVar[0]->Set("LookUp_add", id5, 0, 0, RVARelocate);
49 | id4->AppendData((PBYTE)&iatval, sizeof(DWORD), relVar, 1);
50 | id4->AppendData((PBYTE)&iatval, sizeof(DWORD), 0, 0);
51 | coff->GetSymbolTableBuilder()->AddSymbol(id4, 0, "OriginalFirstThunk", SYST_STATIC, 0);
52 |
53 | char dln[] = "a.dll";
54 | ISectionBuilder* id6 = fac->CreateSectionBuilder();
55 | id6->SetName(".idata$6");
56 | id6->SetCharacteristics(SECH_READ | SECH_WRITE | SECH_ALIGN2);
57 | coff->AppendSection(id6);
58 | id6->AppendData((PBYTE)dln, sizeof(dln), 0, 0);
59 | coff->GetSymbolTableBuilder()->AddSymbol(id6, 0, "dllName", SYST_STATIC, 0);
60 |
61 | char fn[] = "add";
62 | WORD hint = 0;
63 | ISectionBuilder* id7 = fac->CreateSectionBuilder();
64 | id7->SetName(".idata");
65 | id7->SetCharacteristics(SECH_READ | SECH_WRITE | SECH_ALIGN2);
66 | coff->AppendSection(id7);
67 | id7->AppendData((PBYTE)&hint, sizeof(hint), 0, 0);
68 | id7->AppendData((PBYTE)fn, sizeof(fn), 0, 0);
69 | coff->GetSymbolTableBuilder()->AddSymbol(id7, 0, "LookUp_add", SYST_STATIC, 0);
70 |
71 | coff->PushRelocs();
72 |
73 | int l = coff->GetDataLength();
74 | BYTE* p = new BYTE[l];
75 | coff->GetRawData(p);
76 |
77 | coff->Dispose();
78 |
79 | std::fstream f("im.obj", std::ios::binary | std::ios::out);
80 | f.write((char*)p, l);
81 | f.close();
82 |
83 | delete[] p;
84 | }
85 |
--------------------------------------------------------------------------------
/LibGenHelper/LibGenHelperImpl.cpp:
--------------------------------------------------------------------------------
1 | #include "LibGenHelperInterfaces.h"
2 | #include "LibGenHelperFactory.h"
3 |
4 | #include "LibInterfaces.h"
5 | #include "LibFactory.h"
6 |
7 | #include "ImpInterfaces.h"
8 | #include "ImpFactory.h"
9 |
10 | #include
11 | #include
12 |
13 | namespace Sora
14 | {
15 | struct ArchX86 {};
16 | struct ArchX64 {};
17 |
18 | template
19 | struct ArchTraits;
20 |
21 | template<>
22 | struct ArchTraits
23 | {
24 | static IImpSectionBuilder* GetImpSectionBuilder();
25 | };
26 |
27 | IImpSectionBuilder* ArchTraits::GetImpSectionBuilder()
28 | {
29 | return GetX86ImpSectionBuilder();
30 | }
31 |
32 | template<>
33 | struct ArchTraits
34 | {
35 | static IImpSectionBuilder* GetImpSectionBuilder();
36 | };
37 |
38 | IImpSectionBuilder* ArchTraits::GetImpSectionBuilder()
39 | {
40 | return GetX64ImpSectionBuilder();
41 | }
42 |
43 | template
44 | class CImportLibraryBuilder : public IImportLibraryBuilder
45 | {
46 | IImpSectionBuilder* m_secBuilder;
47 | ILibraryBuilder* m_libBuilder;
48 | std::string m_dllName;
49 | std::string m_memName;
50 |
51 | std::vector m_todispose;
52 |
53 | ICoffBuilder* CreateObject()
54 | {
55 | ICoffBuilder* r = m_secBuilder->GetCoffFactory()->CreateCoffBuilder();
56 | m_todispose.push_back(r);
57 | return r;
58 | }
59 | public:
60 | CImportLibraryBuilder(LPCSTR szDllName, LPCSTR szMemName)
61 | {
62 | m_dllName = szDllName;
63 | m_memName = szMemName;
64 | m_libBuilder = CreateLibraryBuilder();
65 | m_secBuilder = ArchTraits::GetImpSectionBuilder();
66 |
67 | ICoffBuilder* impdesc = CreateObject();
68 | m_secBuilder->BuildImportDescriptor(szDllName, impdesc);
69 | m_libBuilder->AddObject(szMemName, impdesc);
70 |
71 | ICoffBuilder* nuldesc = CreateObject();
72 | m_secBuilder->BuildNullDescriptor(nuldesc);
73 | m_libBuilder->AddObject(szMemName, nuldesc);
74 | }
75 |
76 | void WINAPI Dispose()
77 | {
78 | std::vector::iterator i, iend;
79 | i = m_todispose.begin();
80 | iend = m_todispose.end();
81 | for(; i != iend; ++i)
82 | (*i)->Dispose();
83 | delete this;
84 | }
85 |
86 | void WINAPI AddImportFunctionByName(LPCSTR szImpName, LPCSTR szFuncName, LPCSTR szDllExpName)
87 | {
88 | ICoffBuilder* impMember = CreateObject();
89 | m_secBuilder->BuildImportByNameThunk(m_dllName.c_str(), szImpName, szFuncName, szDllExpName, impMember);
90 | m_libBuilder->AddObject(m_memName.c_str(), impMember);
91 | }
92 |
93 | void WINAPI AddImportFunctionByOrdinal(LPCSTR szImpName, LPCSTR szFuncName, int nOrdinal)
94 | {
95 | ICoffBuilder* impMember = CreateObject();
96 | m_secBuilder->BuildImportByOrdinalThunk(m_dllName.c_str(), szImpName, szFuncName, nOrdinal, impMember);
97 | m_libBuilder->AddObject(m_memName.c_str(), impMember);
98 | }
99 |
100 | void WINAPI AddImportFunctionByNameWithHint(LPCSTR szImpName, LPCSTR szFuncName, LPCSTR szImportName, int nOrdinal)
101 | {
102 | ICoffBuilder* impMember = CreateObject();
103 | m_secBuilder->BuildImportThunk(m_dllName.c_str(), szImpName, szFuncName, szImportName, nOrdinal, impMember);
104 | m_libBuilder->AddObject(m_memName.c_str(), impMember);
105 | }
106 |
107 | void WINAPI Build()
108 | {
109 | ICoffBuilder* nullThunk = CreateObject();
110 | m_secBuilder->BuildNullThunk(m_dllName.c_str(), nullThunk);
111 | m_libBuilder->AddObject(m_memName.c_str(), nullThunk);
112 |
113 | m_libBuilder->FillOffsets();
114 | }
115 |
116 | void WINAPI GetRawData(PBYTE buf)
117 | {
118 | m_libBuilder->GetRawData(buf);
119 | }
120 |
121 | int WINAPI GetDataLength()
122 | {
123 | return m_libBuilder->GetDataLength();
124 | }
125 | };
126 |
127 | extern "C" IImportLibraryBuilder* WINAPI CreateX86ImpLibBuilder(LPCSTR szDllName, LPCSTR szMemberName)
128 | {
129 | return new CImportLibraryBuilder(szDllName, szMemberName);
130 | }
131 |
132 | extern "C" IImportLibraryBuilder* WINAPI CreateX64ImpLibBuilder(LPCSTR szDllName, LPCSTR szMemberName)
133 | {
134 | return new CImportLibraryBuilder(szDllName, szMemberName);
135 | }
136 | };
137 |
--------------------------------------------------------------------------------
/ImpLibFix/ImpLibFix.cpp:
--------------------------------------------------------------------------------
1 | #include "ImpLibFix.h"
2 |
3 | #include
4 |
5 | #include
6 | #include
7 |
8 | namespace Sora {
9 | class ImpLibRenameImpl {
10 | PBYTE m_pData;
11 | PBYTE m_pDataEnd;
12 |
13 | PIMAGE_ARCHIVE_MEMBER_HEADER GetMemberHeader()
14 | {
15 | return (PIMAGE_ARCHIVE_MEMBER_HEADER)m_pData;
16 | }
17 |
18 | bool GetNumberValue(PBYTE pStart, int nLen, DWORD* val)
19 | {
20 | int r = 0;
21 | PBYTE pEnd = pStart + nLen;
22 | for(;;) {
23 | if (pStart >= pEnd)
24 | break;
25 | if (*pStart == ' ')
26 | break;
27 |
28 | int n = *pStart - '0';
29 | if (n <= 9 && n >= 0) {
30 | r = r * 10 + n;
31 | } else
32 | return false;
33 |
34 | ++pStart;
35 | }
36 | *val = r;
37 | return true;
38 | }
39 |
40 | bool CheckLeftDataLen(int len)
41 | {
42 | if (m_pData + len < m_pDataEnd)
43 | return true;
44 | else
45 | return false;
46 | }
47 |
48 | bool Step1_CheckSign()
49 | {
50 | if (!CheckLeftDataLen(IMAGE_ARCHIVE_START_SIZE))
51 | return false;
52 | if (!std::equal(m_pData, m_pData + IMAGE_ARCHIVE_START_SIZE, IMAGE_ARCHIVE_START))
53 | return false;
54 |
55 | m_pData += IMAGE_ARCHIVE_START_SIZE;
56 | return true;
57 | }
58 |
59 | bool Step2_GetMemberName(std::string& mn)
60 | {
61 | if(!CheckLeftDataLen(sizeof(IMAGE_ARCHIVE_MEMBER_HEADER)))
62 | return false;
63 | PIMAGE_ARCHIVE_MEMBER_HEADER pHeader = GetMemberHeader();
64 | mn = std::string(pHeader->Name, pHeader->Name + sizeof(pHeader->Name));
65 |
66 | return true;
67 | }
68 |
69 | bool Step3_CheckIfReservedName(std::string& mn)
70 | {
71 | if (!mn.empty() && mn[0] != '/')
72 | return false;
73 |
74 | return true;
75 | }
76 |
77 | bool Step4_Rename(LPCSTR szNewName)
78 | {
79 | PIMAGE_ARCHIVE_MEMBER_HEADER pHeader = GetMemberHeader();
80 | std::fill(pHeader->Name, pHeader->Name + sizeof(pHeader->Name), ' ');
81 | int nNewNameLen = lstrlenA(szNewName);
82 | if (nNewNameLen > sizeof(pHeader->Name))
83 | std::copy(szNewName, szNewName + sizeof(pHeader->Name), pHeader->Name);
84 | else
85 | std::copy(szNewName, szNewName + nNewNameLen, pHeader->Name);
86 |
87 | return true;
88 | }
89 |
90 | bool Step5_MoveNext()
91 | {
92 | PIMAGE_ARCHIVE_MEMBER_HEADER pHeader = GetMemberHeader();
93 | m_pData += sizeof(IMAGE_ARCHIVE_MEMBER_HEADER);
94 |
95 | DWORD nSize;
96 | if (!GetNumberValue(pHeader->Size, sizeof(pHeader->Size), &nSize))
97 | return false;
98 |
99 | if (!CheckLeftDataLen(nSize))
100 | return false;
101 |
102 | m_pData += nSize;
103 |
104 | //pad to 2B align
105 | if (nSize % 2 == 1)
106 | ++m_pData;
107 |
108 | return true;
109 | }
110 |
111 | bool Step6_IsEof()
112 | {
113 | if (CheckLeftDataLen(sizeof(IMAGE_ARCHIVE_MEMBER_HEADER)))
114 | return false;
115 | else
116 | return true;
117 | }
118 |
119 | public:
120 | ImpLibRenameImpl(PBYTE pData, int len)
121 | {
122 | m_pData = pData;
123 | m_pDataEnd = pData + len;
124 | }
125 |
126 | int DoWork(LPCSTR szNewName)
127 | {
128 | int cnt = 0;
129 |
130 | if(!Step1_CheckSign())
131 | return -1;
132 |
133 | doNext:
134 | std::string mn;
135 |
136 | if(!Step2_GetMemberName(mn))
137 | return -1;
138 |
139 | if( Step3_CheckIfReservedName(mn) == false )
140 | {
141 | Step4_Rename(szNewName);
142 | ++cnt;
143 | }
144 |
145 | if (Step5_MoveNext() == false)
146 | return -1;
147 |
148 | if (Step6_IsEof() == false)
149 | goto doNext;
150 |
151 | return cnt;
152 | }
153 | };
154 |
155 | int WINAPI RenameImpLibObjects(LPCSTR szNewName, PBYTE pData, int nDataLen)
156 | {
157 | ImpLibRenameImpl impl(pData, nDataLen);
158 | return impl.DoWork(szNewName);
159 | }
160 |
161 | int WINAPI GetMaxNameLength()
162 | {
163 | return sizeof(((PIMAGE_ARCHIVE_MEMBER_HEADER)0)->Name);
164 | }
165 | };
166 |
--------------------------------------------------------------------------------
/MakeImpLib/MakeImpLib.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release_Static
10 | Win32
11 |
12 |
13 | Release
14 | Win32
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}
25 | Win32Proj
26 | MakeImpLib
27 |
28 |
29 |
30 | Application
31 | true
32 | v120_xp
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v120_xp
39 | false
40 | Unicode
41 |
42 |
43 | Application
44 | false
45 | v120_xp
46 | false
47 | Unicode
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 | true
64 | $(OutDir);C:\WDK\7600.16385.1\lib\ATL\i386;$(LibraryPath)
65 | ../CoffGen;../LibGen;../ImpGen;C:\WDK\7600.16385.1\inc\atl71;$(IncludePath)
66 |
67 |
68 | false
69 | $(OutDir);C:\WDK\7600.16385.1\lib\ATL\i386;$(LibraryPath)
70 | ../CoffGen;../LibGen;../ImpGen;C:\WDK\7600.16385.1\inc\atl71;$(IncludePath)
71 |
72 |
73 | false
74 | $(OutDir);C:\WDK\7600.16385.1\lib\ATL\i386;$(LibraryPath)
75 | ../CoffGen;../LibGen;../ImpGen;C:\WDK\7600.16385.1\inc\atl71;$(IncludePath)
76 |
77 |
78 |
79 |
80 |
81 | Level3
82 | Disabled
83 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
84 |
85 |
86 | Console
87 | true
88 | LibGenHelper.lib;%(AdditionalDependencies)
89 |
90 |
91 |
92 |
93 | Level3
94 |
95 |
96 | MaxSpeed
97 | true
98 | true
99 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
100 | MultiThreaded
101 |
102 |
103 | Console
104 | false
105 | true
106 | true
107 | LibGenHelper.lib;%(AdditionalDependencies)
108 |
109 |
110 |
111 |
112 | Level3
113 |
114 |
115 | MaxSpeed
116 | true
117 | true
118 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
119 | MultiThreaded
120 |
121 |
122 | Console
123 | false
124 | true
125 | true
126 | LibGenHelper.lib;LibGen.lib;ImpGen.lib;CoffGen.lib;%(AdditionalDependencies)
127 |
128 |
129 |
130 |
131 |
132 |
--------------------------------------------------------------------------------
/implib.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Express 2013 for Windows Desktop
4 | VisualStudioVersion = 12.0.30110.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CoffGen", "CoffGen\CoffGen.vcxproj", "{A3F71BEF-1263-4682-8A22-A2966B4BA075}"
7 | EndProject
8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ImpGen", "ImpGen\ImpGen.vcxproj", "{5E0B0EF6-C023-44B1-820A-F8EA26D5297D}"
9 | ProjectSection(ProjectDependencies) = postProject
10 | {A3F71BEF-1263-4682-8A22-A2966B4BA075} = {A3F71BEF-1263-4682-8A22-A2966B4BA075}
11 | EndProjectSection
12 | EndProject
13 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ImpLibFix", "ImpLibFix\ImpLibFix.vcxproj", "{D2693F3C-593D-41A7-B936-4D962095DFD4}"
14 | EndProject
15 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibGen", "LibGen\LibGen.vcxproj", "{79742C1B-1D28-4DF4-B5AA-D161599491C3}"
16 | ProjectSection(ProjectDependencies) = postProject
17 | {A3F71BEF-1263-4682-8A22-A2966B4BA075} = {A3F71BEF-1263-4682-8A22-A2966B4BA075}
18 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D} = {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}
19 | EndProjectSection
20 | EndProject
21 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibGenHelper", "LibGenHelper\LibGenHelper.vcxproj", "{EA713B3F-F9E0-4283-8632-250077CE0768}"
22 | ProjectSection(ProjectDependencies) = postProject
23 | {79742C1B-1D28-4DF4-B5AA-D161599491C3} = {79742C1B-1D28-4DF4-B5AA-D161599491C3}
24 | {A3F71BEF-1263-4682-8A22-A2966B4BA075} = {A3F71BEF-1263-4682-8A22-A2966B4BA075}
25 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D} = {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}
26 | EndProjectSection
27 | EndProject
28 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MakeImpLib", "MakeImpLib\MakeImpLib.vcxproj", "{6E63662B-403E-4DBD-A27F-3038B3BC6D2B}"
29 | ProjectSection(ProjectDependencies) = postProject
30 | {EA713B3F-F9E0-4283-8632-250077CE0768} = {EA713B3F-F9E0-4283-8632-250077CE0768}
31 | EndProjectSection
32 | EndProject
33 | Global
34 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
35 | Debug|Win32 = Debug|Win32
36 | Release_Static|Win32 = Release_Static|Win32
37 | Release|Win32 = Release|Win32
38 | TestCoffGen|Win32 = TestCoffGen|Win32
39 | TestImpGen|Win32 = TestImpGen|Win32
40 | TestLibGen|Win32 = TestLibGen|Win32
41 | EndGlobalSection
42 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
43 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.Debug|Win32.ActiveCfg = Debug|Win32
44 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.Debug|Win32.Build.0 = Debug|Win32
45 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.Release_Static|Win32.ActiveCfg = Release_Static|Win32
46 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.Release_Static|Win32.Build.0 = Release_Static|Win32
47 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.Release|Win32.ActiveCfg = Release|Win32
48 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.Release|Win32.Build.0 = Release|Win32
49 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.TestCoffGen|Win32.ActiveCfg = TestCoffGen|Win32
50 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.TestCoffGen|Win32.Build.0 = TestCoffGen|Win32
51 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.TestImpGen|Win32.ActiveCfg = TestImpGen|Win32
52 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.TestImpGen|Win32.Build.0 = TestImpGen|Win32
53 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.TestLibGen|Win32.ActiveCfg = TestLibGen|Win32
54 | {A3F71BEF-1263-4682-8A22-A2966B4BA075}.TestLibGen|Win32.Build.0 = TestLibGen|Win32
55 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.Debug|Win32.ActiveCfg = Debug|Win32
56 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.Debug|Win32.Build.0 = Debug|Win32
57 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.Release_Static|Win32.ActiveCfg = Release_Static|Win32
58 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.Release_Static|Win32.Build.0 = Release_Static|Win32
59 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.Release|Win32.ActiveCfg = Release|Win32
60 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.Release|Win32.Build.0 = Release|Win32
61 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.TestCoffGen|Win32.ActiveCfg = TestCoffGen|Win32
62 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.TestImpGen|Win32.ActiveCfg = TestImpGen|Win32
63 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.TestImpGen|Win32.Build.0 = TestImpGen|Win32
64 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.TestLibGen|Win32.ActiveCfg = TestLibGen|Win32
65 | {5E0B0EF6-C023-44B1-820A-F8EA26D5297D}.TestLibGen|Win32.Build.0 = TestLibGen|Win32
66 | {D2693F3C-593D-41A7-B936-4D962095DFD4}.Debug|Win32.ActiveCfg = Debug|Win32
67 | {D2693F3C-593D-41A7-B936-4D962095DFD4}.Release_Static|Win32.ActiveCfg = Release_Static|Win32
68 | {D2693F3C-593D-41A7-B936-4D962095DFD4}.Release|Win32.ActiveCfg = Release|Win32
69 | {D2693F3C-593D-41A7-B936-4D962095DFD4}.TestCoffGen|Win32.ActiveCfg = TestCoffGen|Win32
70 | {D2693F3C-593D-41A7-B936-4D962095DFD4}.TestImpGen|Win32.ActiveCfg = TestImpGen|Win32
71 | {D2693F3C-593D-41A7-B936-4D962095DFD4}.TestLibGen|Win32.ActiveCfg = TestLibGen|Win32
72 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.Debug|Win32.ActiveCfg = Debug|Win32
73 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.Debug|Win32.Build.0 = Debug|Win32
74 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.Release_Static|Win32.ActiveCfg = Release_Static|Win32
75 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.Release_Static|Win32.Build.0 = Release_Static|Win32
76 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.Release|Win32.ActiveCfg = Release|Win32
77 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.Release|Win32.Build.0 = Release|Win32
78 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.TestCoffGen|Win32.ActiveCfg = TestCoffGen|Win32
79 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.TestImpGen|Win32.ActiveCfg = TestImpGen|Win32
80 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.TestLibGen|Win32.ActiveCfg = TestLibGen|Win32
81 | {79742C1B-1D28-4DF4-B5AA-D161599491C3}.TestLibGen|Win32.Build.0 = TestLibGen|Win32
82 | {EA713B3F-F9E0-4283-8632-250077CE0768}.Debug|Win32.ActiveCfg = Debug|Win32
83 | {EA713B3F-F9E0-4283-8632-250077CE0768}.Debug|Win32.Build.0 = Debug|Win32
84 | {EA713B3F-F9E0-4283-8632-250077CE0768}.Release_Static|Win32.ActiveCfg = Release_Static|Win32
85 | {EA713B3F-F9E0-4283-8632-250077CE0768}.Release_Static|Win32.Build.0 = Release_Static|Win32
86 | {EA713B3F-F9E0-4283-8632-250077CE0768}.Release|Win32.ActiveCfg = Release|Win32
87 | {EA713B3F-F9E0-4283-8632-250077CE0768}.Release|Win32.Build.0 = Release|Win32
88 | {EA713B3F-F9E0-4283-8632-250077CE0768}.TestCoffGen|Win32.ActiveCfg = Release|Win32
89 | {EA713B3F-F9E0-4283-8632-250077CE0768}.TestImpGen|Win32.ActiveCfg = Release|Win32
90 | {EA713B3F-F9E0-4283-8632-250077CE0768}.TestLibGen|Win32.ActiveCfg = Release|Win32
91 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.Debug|Win32.ActiveCfg = Debug|Win32
92 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.Debug|Win32.Build.0 = Debug|Win32
93 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.Release_Static|Win32.ActiveCfg = Release_Static|Win32
94 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.Release_Static|Win32.Build.0 = Release_Static|Win32
95 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.Release|Win32.ActiveCfg = Release|Win32
96 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.Release|Win32.Build.0 = Release|Win32
97 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.TestCoffGen|Win32.ActiveCfg = Release|Win32
98 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.TestImpGen|Win32.ActiveCfg = Release|Win32
99 | {6E63662B-403E-4DBD-A27F-3038B3BC6D2B}.TestLibGen|Win32.ActiveCfg = Release|Win32
100 | EndGlobalSection
101 | GlobalSection(SolutionProperties) = preSolution
102 | HideSolutionNode = FALSE
103 | EndGlobalSection
104 | EndGlobal
105 |
--------------------------------------------------------------------------------
/LibGenHelper/LibGenHelper.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release_Static
10 | Win32
11 |
12 |
13 | Release
14 | Win32
15 |
16 |
17 |
18 | {EA713B3F-F9E0-4283-8632-250077CE0768}
19 | LibGenHelper
20 |
21 |
22 |
23 | DynamicLibrary
24 | true
25 | v120_xp
26 | Unicode
27 |
28 |
29 | DynamicLibrary
30 | false
31 | v120_xp
32 | false
33 | Unicode
34 |
35 |
36 | StaticLibrary
37 | false
38 | v120_xp
39 | false
40 | Unicode
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | $(OutDir);$(LibraryPath)
57 | ../CoffGen;../LibGen;../ImpGen;$(VC_IncludePath);$(WindowsSdk_71A_IncludePath);$(IncludePath)
58 |
59 |
60 | $(OutDir);$(LibraryPath)
61 | ../CoffGen;../LibGen;../ImpGen;$(VC_IncludePath);$(WindowsSdk_71A_IncludePath);$(IncludePath)
62 |
63 |
64 | $(OutDir);$(LibraryPath)
65 | ../CoffGen;../LibGen;../ImpGen;$(VC_IncludePath);$(WindowsSdk_71A_IncludePath);$(IncludePath)
66 |
67 |
68 |
69 | Level3
70 | Disabled
71 | true
72 |
73 |
74 | true
75 | Windows
76 | LibGen.lib;ImpGen.lib;%(AdditionalDependencies)
77 | LibGenHelper.def
78 |
79 |
80 |
81 |
82 | Level3
83 | MaxSpeed
84 | true
85 | true
86 | true
87 | MultiThreaded
88 | _USING_V110_SDK71_;%(PreprocessorDefinitions)
89 |
90 |
91 | false
92 | true
93 | true
94 | Windows
95 | LibGen.lib;ImpGen.lib;%(AdditionalDependencies)
96 | LibGenHelper.def
97 |
98 |
99 | _USING_V110_SDK71_;BUILD_DLL;%(PreprocessorDefinitions)
100 |
101 |
102 |
103 |
104 | Level3
105 | MaxSpeed
106 | true
107 | true
108 | false
109 | MultiThreaded
110 |
111 |
112 | true
113 | true
114 | true
115 | Windows
116 | LibGen.lib;ImpGen.lib;%(AdditionalDependencies)
117 |
118 |
119 |
120 |
121 | MachineX86
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
--------------------------------------------------------------------------------
/CoffGen/coffInterfaces.h:
--------------------------------------------------------------------------------
1 | #ifndef COFFINTERFACES_H
2 | #define COFFINTERFACES_H
3 |
4 | #include
5 |
6 | namespace Sora {
7 | //indicate that the object offers method to free
8 | class IDispose
9 | {
10 | public:
11 | virtual void WINAPI Dispose() = 0;
12 | };
13 |
14 | //indicate that the object can output binary datas
15 | class IHasRawData
16 | {
17 | public:
18 | virtual int WINAPI GetDataLength() = 0;
19 |
20 | //the memory should be allocated by the caller
21 | virtual void WINAPI GetRawData(PBYTE) = 0;
22 | };
23 |
24 | class ICoffBuilder; //for building coff object file
25 | class ISectionBuilder; //for building section of coff object file
26 | class IStringTableBuilder; //for building string table of coff object file
27 | class ISymbolStrings; //for enum each symbol string in symbol table
28 | class ISymbolTableBuilder; //for building symbol table of coff object file
29 | class ISectionAuxSymbol; //present a aux symbol in symbol table
30 | class IRelocatableVar; //present a relocation item in relocation table
31 | class IRelocationTableBuilder; //for building relocation table of coff object file
32 |
33 | class ICoffFactory
34 | {
35 | public:
36 | virtual ICoffBuilder* WINAPI CreateCoffBuilder() = 0;
37 | virtual ISectionBuilder* WINAPI CreateSectionBuilder() = 0;
38 |
39 | //generally this function is not called by user.
40 | virtual ISymbolTableBuilder* WINAPI CreateSymbolTableBuilder() = 0;
41 |
42 | //generally this function is not called by user.
43 | virtual IStringTableBuilder* WINAPI CreateStringTableBuilder() = 0;
44 |
45 | virtual IRelocatableVar* WINAPI CreateRelocatableVar() = 0;
46 |
47 | //generally this function is not called by user.
48 | virtual IRelocationTableBuilder* WINAPI CreateRelocationTableBuilder() = 0;
49 | };
50 |
51 | class ICoffBuilder : public IDispose, public IHasRawData
52 | {
53 | public:
54 | //return: index in secions, 1-based. this function will set the section index
55 | //the ownership of object is transferred
56 | virtual int WINAPI AppendSection(ISectionBuilder*) = 0;
57 |
58 | virtual IStringTableBuilder* WINAPI GetStringTableBuilder() = 0;
59 | virtual ISymbolTableBuilder* WINAPI GetSymbolTableBuilder() = 0;
60 |
61 | //call turns: add symbol -> PushRelocs. the function will reference relocs to previous added symbols
62 | virtual void WINAPI PushRelocs() = 0;
63 | };
64 |
65 | enum SectionCharacteristic
66 | {
67 | SECH_READ = 1,
68 | SECH_WRITE = 2,
69 | SECH_EXEC = 4,
70 | SECH_CODE = 8,
71 | SECH_ALIGN1 = 16,
72 | SECH_ALIGN2 = 32,
73 | SECH_ALIGN4 = 64,
74 | SECH_ALIGN8 = 128,
75 | SECH_ALIGN16 = 256,
76 | SECH_ALIGN32 = 512,
77 | SECH_ALIGN64 = 1024,
78 | SECH_UNINIT = 2048,
79 | SECH_COMDAT = 4096
80 | };
81 |
82 | enum SectionComdat
83 | {
84 | SECO_NODUPLICATE = 1,
85 | SECO_SELECTANY,
86 | SECO_SELECTSAMESIZE,
87 | SECO_SELECTSAME,
88 | SECO_ASSOCIATIVE,
89 | SECO_SELECTLARGEST
90 | };
91 |
92 | class ISectionBuilder : public IDispose
93 | {
94 | public:
95 | //ownership of relocationItems will transfer to this object
96 | //the offset in relocatableVar is relative to the beginning of pData
97 | virtual void WINAPI AppendData(LPCBYTE pData, int len, IRelocatableVar*[], int relocCount) = 0;
98 |
99 | virtual void WINAPI SetCharacteristics(DWORD) = 0;
100 | virtual void WINAPI SetName(LPCSTR) = 0; //set section name, max length: 8 chars
101 |
102 | virtual int WINAPI GetHeaderLength() = 0;
103 |
104 | //param 2: offset (base) of the section in the data part
105 | virtual void WINAPI GetRawHeader(PBYTE, DWORD rawOffset) = 0;
106 |
107 | virtual int WINAPI GetDataLength() = 0; //not include the header
108 | virtual void WINAPI GetRawData(PBYTE) = 0; //not include the header either
109 |
110 | //please don't call setsectionindex by hand unless you know what you are doing.
111 | //it's called by the coff builder
112 | virtual void WINAPI SetSectionIndex(int) = 0;
113 |
114 | virtual int WINAPI GetSectionIndex() = 0; //1-based
115 |
116 | //please don't call pushrelocs by hand unless you know what you are doing.
117 | //this will be called by coff builder's pushrelocs
118 | virtual void WINAPI PushRelocs(ISymbolTableBuilder*) = 0;
119 |
120 | //input: SectionCharacteristic output: raw characteristic
121 | virtual DWORD WINAPI GetRawCharacteristic(DWORD) = 0; //object independent
122 |
123 | //return the raw characteristic of this object
124 | virtual DWORD WINAPI GetRawCharacteristic() = 0;
125 |
126 | //no crc is generated in aux symbol
127 | //associated section can be null
128 | virtual ISectionAuxSymbol* WINAPI CreateAuxSymbol(ISectionBuilder* associatedSection, SectionComdat selection) = 0;
129 | };
130 |
131 | class IStringTableBuilder : public IDispose, public IHasRawData
132 | {
133 | public:
134 | //return the offset of appended string
135 | virtual int WINAPI AppendString(LPCSTR) = 0;
136 |
137 | //don't free the returned value, it's inside the string table
138 | virtual LPCSTR WINAPI GetString(int offset) = 0;
139 | };
140 |
141 | enum StorageType
142 | {
143 | SYST_EXTERN = 1,
144 | SYST_STATIC,
145 | SYST_SECTION,
146 | SYST_FUNCTION,
147 | SYST_STATICFUNCTION
148 | };
149 |
150 | class ISymbolStrings : public IDispose
151 | {
152 | public:
153 | virtual int WINAPI GetCount() = 0;
154 | virtual LPCSTR WINAPI GetString(int nIndex) = 0;
155 | };
156 |
157 | class ISymbolTableBuilder : public IDispose, public IHasRawData
158 | {
159 | public:
160 | //return the index of added item, 0-based
161 | //value: often the offset.
162 | //value could be size when add extern reference though often 0 here.
163 | virtual int WINAPI AddSymbol(ISectionBuilder* section, int value, LPCSTR name, StorageType, int auxCnt) = 0;
164 |
165 | //data is copied. after the function returned the parameter can be freed
166 | //often ISectionAuxSymbol*
167 | //ownership is not transfered, don't forget to free it
168 | virtual int WINAPI AddAuxData(IHasRawData*) = 0;
169 |
170 | virtual int WINAPI GetSymbolCount() = 0;
171 |
172 | //please don't call setsectionindex by hand.
173 | virtual void WINAPI SetStringTable(IStringTableBuilder*) = 0;
174 |
175 | //the caller has responsibility to free the returned object
176 | virtual ISymbolStrings* GetPublicSymbolNames() = 0;
177 | };
178 |
179 | enum RelocateType {
180 | VARelocate32 = 1,
181 | VARelocate64,
182 | RVARelocate
183 | };
184 |
185 | class IRelocatableVar : public IDispose
186 | {
187 | public:
188 | //change the inside offset value of this object by add the parameter value
189 | virtual void WINAPI Offset(int offset) = 0;
190 |
191 | //section and offset point to the mem that should be rewrite during linking
192 | virtual void WINAPI Set(LPCSTR symbol, ISectionBuilder* section, DWORD offset, int size, DWORD relocType) = 0;
193 |
194 | virtual void WINAPI Get(LPCSTR* symbol, ISectionBuilder** section, DWORD* offset, int* size, DWORD* relocType) = 0;
195 | };
196 |
197 | class IRelocationTableBuilder : public IDispose, public IHasRawData
198 | {
199 | public:
200 | //get the bytes of a pointer. 4 for x86 and 8 for x64
201 | virtual int WINAPI GetPtrLength() = 0;
202 |
203 | //it's used during build section header
204 | //generally you don't need call this method unless you know what you are doing
205 | virtual int WINAPI GetCount() = 0;
206 |
207 | //ownership is transfered
208 | virtual void WINAPI AppendRelocationItem(IRelocatableVar*) = 0;
209 |
210 | //this will be called by section builder's pushrelocs
211 | virtual void WINAPI PushToSymbolTable(ISymbolTableBuilder*) = 0;
212 | };
213 |
214 | class ISectionAuxSymbol : public IHasRawData, public IDispose
215 | {
216 | };
217 | };
218 |
219 | #endif
220 |
--------------------------------------------------------------------------------
/MakeImpLib/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include
6 | #include
7 | #include
8 |
9 | #include "../LibGenHelper/LibGenHelperInterfaces.h"
10 | #include "../LibGenHelper/LibGenHelperFactory.h"
11 |
12 | struct MyMsgException
13 | {
14 | CString fmt;
15 | CString msg;
16 | MyMsgException(LPCTSTR p) : msg(p), fmt("%s") {}
17 | MyMsgException(LPCTSTR p1, LPCTSTR p2) : fmt(p1), msg(p2) {}
18 | };
19 |
20 | int _tmain(int argc, TCHAR* argv[])
21 | {
22 | CoInitializeEx(NULL, COINIT_MULTITHREADED);
23 |
24 | try
25 | {
26 | if (argv[1] != 0 && argv[2] != 0 && argv[3] == 0) {
27 | CComPtr xdoc;
28 | if (xdoc.CoCreateInstance(L"Msxml2.DOMDocument.6.0") != S_OK)
29 | throw MyMsgException(TEXT("Fail to create XML parser object!"));
30 |
31 | CComVariant strFileName;
32 | strFileName = SysAllocString( CT2CW(argv[1]) );
33 | VARIANT_BOOL bIsSucc;
34 | xdoc->load(strFileName, &bIsSucc);
35 | if (bIsSucc != VARIANT_TRUE)
36 | throw MyMsgException(TEXT("Fail to load input file %s!"), argv[1]);
37 |
38 | CComPtr xDllNameNode;
39 | CComBSTR strXPath;
40 | strXPath = L"/ImportLibrary/DllName";
41 | xdoc->selectSingleNode(strXPath, &xDllNameNode);
42 | if (xDllNameNode == NULL)
43 | throw MyMsgException(TEXT("No DllName node!"));
44 |
45 | Sora::IImportLibraryBuilder* impBuilder;
46 | CComBSTR strDllName;
47 | xDllNameNode->get_text(&strDllName);
48 | CW2AEX<> cvtstrDllName(strDllName, CP_ACP);
49 |
50 | CComPtr xAmd64Node;
51 | strXPath = L"/ImportLibrary/ArchAMD64";
52 | xdoc->selectSingleNode(strXPath, &xAmd64Node);
53 | if (xAmd64Node == NULL)
54 | impBuilder = Sora::CreateX86ImpLibBuilder(cvtstrDllName, cvtstrDllName);
55 | else
56 | impBuilder = Sora::CreateX64ImpLibBuilder(cvtstrDllName, cvtstrDllName);
57 |
58 | strXPath = L"/ImportLibrary/Import";
59 | CComPtr xImportNodes;
60 | if (xdoc->selectNodes(strXPath, &xImportNodes) != S_OK)
61 | throw MyMsgException(TEXT("No Import nodes found!"));
62 |
63 | for(;;) {
64 | CComPtr xImportNode;
65 | if (xImportNodes->nextNode(&xImportNode) != S_OK)
66 | break;
67 |
68 | CComPtr xLinkNameNode, xStubNameNode, xImportNameNode, xImportOrdinalNode;
69 | CComBSTR strLinkName, strStubName, strImportName, strImportOrdinal;
70 |
71 | strXPath = L"./LinkName";
72 | if (xImportNode->selectSingleNode(strXPath, &xLinkNameNode) != S_OK)
73 | throw MyMsgException(TEXT("No LinkName Node!"));
74 |
75 | strXPath = L"./StubName";
76 | xImportNode->selectSingleNode(strXPath, &xStubNameNode);
77 | //if no stubname node found, will not generate callstub
78 |
79 | xLinkNameNode->get_text(&strLinkName);
80 | if (xStubNameNode != NULL) xStubNameNode->get_text(&strStubName);
81 |
82 | HRESULT selectImportName;
83 | HRESULT selectOrdinal;
84 | strXPath = L"./ImportName";
85 | selectImportName = xImportNode->selectSingleNode(strXPath, &xImportNameNode);
86 | strXPath = L"./Ordinal";
87 | selectOrdinal = xImportNode->selectSingleNode(strXPath, &xImportOrdinalNode);
88 | int nOrdinal = 0;
89 |
90 | if (selectImportName == S_OK || selectOrdinal == S_OK) {
91 | if (selectImportName == S_OK)
92 | xImportNameNode->get_text(&strImportName);
93 |
94 | if (selectOrdinal == S_OK) {
95 | xImportOrdinalNode->get_text(&strImportOrdinal);
96 | LPCWSTR pszImportOrdinal = strImportOrdinal;
97 |
98 | while(*pszImportOrdinal != 0) {
99 | nOrdinal *= 10;
100 | nOrdinal += *pszImportOrdinal - L'0';
101 | ++pszImportOrdinal;
102 | }
103 | }
104 |
105 | if (selectImportName == S_OK && selectOrdinal == S_OK) {
106 | impBuilder->AddImportFunctionByNameWithHint(
107 | CW2AEX<>(strLinkName, CP_UTF8),
108 | CW2AEX<>(strStubName, CP_UTF8),
109 | CW2AEX<>(strImportName, CP_UTF8),
110 | nOrdinal
111 | );
112 | } else if (selectImportName == S_OK) {
113 | impBuilder->AddImportFunctionByName(
114 | CW2AEX<>(strLinkName, CP_UTF8),
115 | CW2AEX<>(strStubName, CP_UTF8),
116 | CW2AEX<>(strImportName, CP_UTF8)
117 | );
118 | } else if (selectOrdinal == S_OK) {
119 | impBuilder->AddImportFunctionByOrdinal(
120 | CW2AEX<>(strLinkName, CP_UTF8),
121 | CW2AEX<>(strStubName, CP_UTF8),
122 | nOrdinal
123 | );
124 | }
125 | } else {
126 | throw MyMsgException(TEXT("No ImportName or Ordinal Node!"));
127 | }
128 | }
129 |
130 | //save file
131 | impBuilder->Build();
132 |
133 | int nFileSize = impBuilder->GetDataLength();
134 | CHandle hFile( CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, NULL) );
135 | if ( (HANDLE)hFile == INVALID_HANDLE_VALUE )
136 | throw MyMsgException(TEXT("Fail to create library File %s!"), argv[2]);
137 |
138 | if (SetFilePointer(hFile, nFileSize, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
139 | if (GetLastError() != 0)
140 | throw MyMsgException(TEXT("Can't allocate disk space for output file!"));
141 |
142 | if (SetEndOfFile(hFile) == FALSE)
143 | throw MyMsgException(TEXT("Can't allocate disk space for output file!"));
144 |
145 | CHandle hFileMap( CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, nFileSize, 0) );
146 | if ((HANDLE)hFileMap == NULL)
147 | throw MyMsgException(TEXT("Can't map output file for writing!"));
148 |
149 | LPVOID pFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);
150 | if (pFile == 0)
151 | throw MyMsgException(TEXT("Can't map output file for writing!"));
152 |
153 | impBuilder->GetRawData((PBYTE)pFile);
154 | impBuilder->Dispose();
155 | UnmapViewOfFile(pFile);
156 | } else {
157 | fprintf(stdout, "%s",
158 | "Make import library from XML\n"
159 | "using: MakeImpLib