Search code examples
windowswinapiportable-executablecoff

PE/COFF symbol type field


Microsoft's documentation for PE/COFF says of the type field in the symbol table:

"The most significant byte specifies whether the symbol is a pointer to, function returning, or array of the base type that is specified in the LSB. Microsoft tools use this field only to indicate whether the symbol is a function, so that the only two resulting values are 0x0 and 0x20 for the Type field."

However, the documentation and winnt.h both specify that IMAGE_SYM_DTYPE_FUNCTION = 2, not 0x20. Even if this is taken to be the value of the MSB, that would give a value for the entire field of 0x200, not 0x20.

What am I missing?


Solution

  • Check winnt.h for following lines:

    // type packing constants
    
    #define N_BTMASK                            0x000F
    #define N_TMASK                             0x0030
    #define N_TMASK1                            0x00C0
    #define N_TMASK2                            0x00F0
    #define N_BTSHFT                            4
    #define N_TSHIFT                            2
    // MACROS
    
    // Basic Type of  x
    #define BTYPE(x) ((x) & N_BTMASK)
    
    // Is x a pointer?
    #ifndef ISPTR
    #define ISPTR(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_POINTER << N_BTSHFT))
    #endif
    
    // Is x a function?
    #ifndef ISFCN
    #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
    #endif
    

    So it seems official MSB, LSB description is wrong - they are not bytes but nibbles. So 0x20 would be a function (MS nibble = 2) returning base type of IMAGE_SYM_TYPE_NULL (LS nibble = 0) .