This Article IsCreated at 2024-03-05Last Modified at 2024-03-06Referenced as ia.www.f22

C Type Format - Type Reflection

C has reflection too, in the form of libctf.

unfinished example usage of libctf: https://git.envs.net/iacore/weighted_db.

I learned about the use of pahole during the process. The tool is useful even on Zig-compiled binaries (or any other binary compiled with DWARF or CTF). Here are some type layouts in Zig (example output of pahole):

struct ?u32 {
        u32                        data __attribute__((__aligned__(4))); /*     0     4 */
        u8                         some __attribute__((__aligned__(1))); /*     4     1 */

        /* size: 8, cachelines: 1, members: 2 */
        /* padding: 3 */
        /* forced alignments: 2 */
        /* last cacheline: 8 bytes */
} __attribute__((__aligned__(4)));
struct ?mem.Allocator {
        struct mem.Allocator       data __attribute__((__aligned__(8))); /*     0    16 */
        u8                         some __attribute__((__aligned__(1))); /*    16     1 */

        /* size: 24, cachelines: 1, members: 2 */
        /* padding: 7 */
        /* forced alignments: 2 */
        /* last cacheline: 24 bytes */
} __attribute__((__aligned__(8)));
struct ?u29 {
        u29                        data __attribute__((__aligned__(4))); /*     0     4 */
        u8                         some __attribute__((__aligned__(1))); /*     4     1 */

        /* size: 8, cachelines: 1, members: 2 */
        /* padding: 3 */
        /* forced alignments: 2 */
        /* last cacheline: 8 bytes */
} __attribute__((__aligned__(4)));