Skip to content

Commit 5410287

Browse files
committed
Update README.md
1 parent 5c66a5c commit 5410287

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,86 @@ This plugin comes with a set of particular python generator scripts located in t
6363
* ``-n`` ``--no-comments``: Don't generate help comments for resulting domain definitions.
6464
* ``-g`` ``--generate-domains``: A list of domain definitions to generate. Use "all" to generate all domains (Default).
6565

66+
## IDAClang usage example
67+
68+
One of the main advantages of this plugin and its scripts is that it allows for a full idaclang compilation support! But it requires a small setup beforehand if you are new to idaclang and only starting to setting up, some key notes to know:
69+
* This guide was written with windows in mind, it should be possible to do the same for linux binaries, but be aware that schema differs per platform, so you'd need a linux dump first, as well as some corrections to compile arguments!
70+
* Only versions of ida 7.7 and above are supported, as that was the first version where idaclang was introduced;
71+
* Make sure compiler used by ida is clang, ``Options->Compiler`` locate ``Source parser``, should be set to ``clang``. To parse c++ file, use ``File->Load file->Parse C header file``;
72+
* Compile arguments:
73+
* Target platform, make sure you are targetting correct platform (example: ``-target x86_64-pc-windows-msvc19.16.27045``);
74+
* C++ version, 17 and above should be supported, but was tested on c++17 (example: ``-std=c++17``);
75+
* Some hl2sdk code expects certain defines set, notably: ``X64BITS``, ``PLATFORM_64BITS`` (example: ``-DX64BITS -DPLATFORM_64BITS``);
76+
* There are few other handy defines and arguments I'd suggest using (especially for hl2sdk support), so here's the complete argument string for reference that was used during test (ida 7.7 but should work for further versions as well):
77+
```
78+
-x c++ -std=c++17 -Wno-c++11-narrowing -target x86_64-pc-windows-msvc19.16.27045 -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -D_GLIBCXX_DEBUG=0 -D_ITERATOR_DEBUG_LEVEL=0 -DDEBUG -D_DEBUG -DWIN32 -D_WINDOWS -DCOMPILER_MSVC -DCOMPILER_MSVC64 -DX64BITS -DPLATFORM_64BITS -D_WIN32=1 -D_MSC_VER=1900 -D__clang__ -D__clang_major__=17 -D_CRT_USE_BUILTIN_OFFSETOF=1
79+
```
80+
* Include paths:
81+
* Generally for non-hl2sdk build you should be fine with whatever idaclang has already, but if you want to have hl2sdk to build correctly, here's the paths list you need to have for it to locate everything correctly:
82+
* ``{HL2SDKPATH}\public``
83+
* ``{HL2SDKPATH}\game\shared``
84+
* ``{HL2SDKPATH}\game\server``
85+
* ``{HL2SDKPATH}\public\tier0``
86+
* ``{HL2SDKPATH}\public\tier1``
87+
* ``{HL2SDKPATH}\public\mathlib``
88+
* ``{HL2SDKPATH}\public\entity2``
89+
* ``{HL2SDKPATH}\public\engine``
90+
* ``{HL2SDKPATH}\public\game\server``
91+
* ``{HL2SDKPATH}\thirdparty\protobuf-3.21.8\src``
92+
* ``{HL2SDKPATH}\common``
93+
* Replace ``{HL2SDKPATH}`` with an absolute path to hl2sdk for a game you are targetting (like ``hl2sdk-cs2``);
94+
* If you want to preserve arguments and include paths between binaries, you can edit ``{IDAPATH}/cfg/idaclang.cfg`` in there edit ``CLANG_ARGV``, where include directories could be supplied with ``-I`` argument (example ``-ID:\\hl2sdk-cs2\\public``);
95+
96+
### Some gotchas & ABI differences
97+
98+
Since idaclang uses clang under the hood to compile c++, it would mismatch on some stuff with MSVC, you can see what is supported [here](https://clang.llvm.org/docs/MSVCCompatibility.html). But here's the general gist of what you'd most likely encounter:
99+
* [Member function pointers](https://isocpp.org/wiki/faq/pointers-to-members) are of a different size (16 on clang, 8 on MSVC) which would produce different sized structs. That's not used in schema, but is used in hl2sdk if you plan on not only limiting with schema (Notable cases in hl2sdk: ``CUtlDelegate``, ``CUtlBuffer``);
100+
* Inlined structs/classes of templated classes might not be picked up correctly by idaclang and might be missing, example:
101+
```cpp
102+
template <typename T>
103+
class TemplateTest
104+
{
105+
struct SubStruct
106+
{
107+
T m_Val;
108+
};
109+
110+
SubStruct *m_Dummy;
111+
};
112+
113+
TemplateTest<int> testvar;
114+
```
115+
in this example, ``TemplateTest<int>::SubStruct`` would be only be declared but not defined in ida's type database, producing incomplete results. There's no global fix for this (if you know any lmk!!!), you can only fix it locally per template instance by providing [explicit template specialization](https://en.cppreference.com/w/cpp/language/template_specialization.html) of said type which would compile everything there's for it, example fix for the above case would be adding this before any usages of said template specialization:
116+
```cpp
117+
template class TemplateTest<int>;
118+
```
119+
Example hl2sdk code that is affected by this: ``CUtlLinkedList`` and its variations;
120+
121+
### CUtlDelegate compilation issues
122+
123+
That's not part of this plugin scope, as nothing in schema uses it, but I'll anyway provide a "fix" for this case for anyone who might wonder how to deal with errors such as ``fatal: failed to forward declare type: name="utldelegate::SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + sizeof(int)>" code=-3`` and alike, put this cpp code before any hl2sdk header files:
124+
```cpp
125+
// Hack fix as clang compiles member function pointers to 16 bytes and msvc uses 8,
126+
// so this struct ends up +8 bytes longer than msvc produces
127+
#define UTLDELEGATE_H
128+
template <typename Signature>
129+
class CUtlDelegate;
130+
131+
template<typename R>
132+
class CUtlDelegate< R( ) > { char pad[16]; };
133+
template<typename R, class p1>
134+
class CUtlDelegate< R( p1 ) > { char pad[16]; };
135+
template<typename R, class p1, class p2>
136+
class CUtlDelegate< R( p1, p2 ) > { char pad[16]; };
137+
template<typename R, class p1, class p2, class p3>
138+
class CUtlDelegate< R( p1, p2, p3 ) > { char pad[16]; };
139+
template<typename R, class p1, class p2, class p3, class p4>
140+
class CUtlDelegate< R( p1, p2, p3, p4 ) > { char pad[16]; };
141+
template<typename R, class p1, class p2, class p3, class p4, class p5>
142+
class CUtlDelegate< R( p1, p2, p3, p4, p5 ) > { char pad[16]; };
143+
class CUtlAbstractDelegate { char pad[16]; };
144+
```
145+
66146
## Manual building example
67147

68148
### Prerequisites

0 commit comments

Comments
 (0)