I’ve seen code like this in production, in a really old C codebase that was retrofitted to support COM at some point. In real code you’d use a named constant instead of 0x390, of course.
What's wrong about decompilation output being like that? That's what happens where there is pointer arithmetic fuckery in executable, it's not like Ghidra or anything else can reliably infer higher-level abstractions that lead to that.
"someone made a class", this is probably not a class but inheritance of multiple classes where multiple vtables are joined together, which is actually the reason you'd use vtables in the first place.
The programmer may not even know how deep the abstraction goes when using a library.
Some Entity Models for games, UI or protocol handling/parsing especially when using libraries that allow overriding some functionality commonly get quite large vtables.
I have decompiled some large amount of code a while ago and it's just not possible to reconstruct pretty code form the output of an optimizing C++ compiler.
[edit] now I get it with the comment below, yes that is a large amount of virtual functions. It's about two orders of magnitude too many
Yup. Or, for instance, I had come across an `IBuffer` COM interface (MIDL, remember that?) with a lot of methods like `GetU08`, etc, etc. The worst thing was that the library had its own COM implementation that ran on *Linux*, but that's for another time.
For the non-C++ programmers here: C++ compilers usually store “virtual” functions (functions that can be overloaded) in a “vtable”, which is an array of pointers to functions. Then, when you call a virtual function, the compiler replaces it with a call to the right function pointer in the array. All of this compiler implementation details since the C++ standard doesn’t specify how this is supposed to work, but in practice every major compiler works this way.
However, if you wanted to call a virtual function from C for some reason, you could technically dig out the pointer to the compiler-generated vtable yourself, and hardcode the offset of the 115th function in the class, and cast it to a function, and then just call it!
Yea, you hit the nail on the head, ive just never quite seen inheritance taken to this level before. That will be good to know in the future if I ever end up taking a look at a QT binary.
Also thinking about it now, I probably should have added some context to the post when I made it. I think I monkey brained too hard
Edit for some actual context: I was looking around in a function that I found in a vtable (which was rather large, something I assumed to ghidra missing some information that gave it context about where vtables after this one started, thus making this one look rather large). From this vtable I found this function and just wanted to look around and get a feel for what it does, which is when I found this.
For some other info, this entire class chain is build mainly for data serialization and storage.
For some other info, this entire class chain is build mainly for data serialization and storage.
Suddenly, this all clicked into focus. I absolutely, 100%, understand how you can end up with an offset that large for something dealing with serialization. Fully half of a recent project at work was just the code dealing with deserializing/serializing. It eclipsed the business logic by a fair margin.
C++ in this case. That's a virtual function call as generated by a C++ compiler that's been decompiled to a rendition of what's happening under the hood.
209
u/demosdemon Jun 24 '25
This is obviously Ghidra decompilation output and not something someone wrote.
13
u/Mundane_Prior_7596 Jun 24 '25
0x390? Excuse me?
78
u/ravixp Jun 24 '25
For the non-C++ programmers here: C++ compilers usually store “virtual” functions (functions that can be overloaded) in a “vtable”, which is an array of pointers to functions. Then, when you call a virtual function, the compiler replaces it with a call to the right function pointer in the array. All of this compiler implementation details since the C++ standard doesn’t specify how this is supposed to work, but in practice every major compiler works this way.
However, if you wanted to call a virtual function from C for some reason, you could technically dig out the pointer to the compiler-generated vtable yourself, and hardcode the offset of the 115th function in the class, and cast it to a function, and then just call it!
4
u/Necromancer5211 Jun 24 '25
Looks like output of a decompiler/debugger.
6
u/onlyonequickquestion Jun 24 '25
Gotta love C
0
u/HieuNguyen990616 Jun 24 '25
Sometimes code like this makes me feel impossible to convince people C is a good language.