Search code examples
clinux-kernelembeddedembedded-linuxelf

how do i know A compiler basic datatype how large they are?


i was using arm-none-eabi-readelf -h test.o analizes a object file,it give me
Size of this header: 52 (bytes) Entry point address: 0x0 i go to the souce code of the arm-none-eabi-readelf ,but i can't figure out how large the datetype is,long,int,short.some answers say there limit.h can find some define,finally i analized long is 4B,int 4B short 2B (personally opinion)


 /* ELF Header */
#define EI_NIDENT   16      /* Size of e_ident[] */
typedef struct elf_internal_ehdr {
  unsigned char     e_ident[EI_NIDENT]; /* ELF "magic number" */           //16B
  bfd_vma       e_entry;/* Entry point virtual address */  //unsigned long but 4B
  bfd_size_type     e_phoff;/* Program header table file offset */ //unsigned long  but4B
  bfd_size_type     e_shoff;/* Section header table file offset */    //unsigned long  but4B
  unsigned long     e_version;/* Identifies object file version */    //unsigned long  but4B
  unsigned long     e_flags;/* Processor-specific flags */          //unsigned long  but4B
  unsigned short    e_type;     /* Identifies object file type */         //2B
  unsigned short    e_machine;  /* Specifies required architecture */        //2B
  unsigned int      e_ehsize;   /* ELF header size in bytes */             //   4B
  unsigned int      e_phentsize;    /* Program header table entry size */      //   4B
  unsigned int      e_phnum;    /* Program header table entry count */       //   4B
  unsigned int      e_shentsize;    /* Section header table entry size */      //     4B
  unsigned int      e_shnum;    /* Section header table entry count */    //       4B
  unsigned int      e_shstrndx; /* Section header string table index */   //         4B
} Elf_Internal_Ehdr;


but total doesn't fit Size of this header: 52 (bytes)

is there any method can figure out basic datatype how many bytes they are except sizeof()(cross compiler i don't know how to use it to printf sizof() result )


Solution

  • You're asking the wrong question. You asked what's the size of unsigned long in some unspecified environment. But you actually want to know the size of the fields in an ELF file. This information is present on Wikipedia.

    For example, the relevant Wikipedia page says e_version needs to be 32 bits wide. So you should use uint32_t for e_version.

    Don't forget to tell your compiler to not use any padding for this structure if you plan to memcpy from an ELF file into it.