Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout of structs in Linux/x86-64 syscalls for assembly programmers?

A number of linux/x86-64 syscalls accept pointers to structs as arguments.

For example the second parameter of stat(2) is struct stat*...

   struct stat {
       dev_t     st_dev;     /* ID of device containing file */
       ino_t     st_ino;     /* inode number */
       mode_t    st_mode;    /* protection */
       nlink_t   st_nlink;   /* number of hard links */
       uid_t     st_uid;     /* user ID of owner */
       gid_t     st_gid;     /* group ID of owner */
       dev_t     st_rdev;    /* device ID (if special file) */
       off_t     st_size;    /* total size, in bytes */
       blksize_t st_blksize; /* blocksize for file system I/O */
       blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
       time_t    st_atime;   /* time of last access */
       time_t    st_mtime;   /* time of last modification */
       time_t    st_ctime;   /* time of last status change */
   };

This means that if you want to call the syscall from pure assembly than you have to know the rules about how big each type is, and whether there is any padding in between members for alignment purposes, and so on.

Does the C standard leave this open to be (compiler) implementation defined or can it be determined from the standard (assuming the primitive type sizes are known)?

If it is left open, does the kernel or the x86-64 architecture define it in anyway? Or is it just a matter of which compiler the kernel happened to be compiled with?

(That is given some member of the struct I need to calculate the starting offset of that member relative to the address of the struct)

like image 586
Andrew Tomazos Avatar asked Jan 16 '13 09:01

Andrew Tomazos


1 Answers

The layout of structs is not defined in the C standard, but in the ABI definition, in your case the System V AMD64 ABI. That is, in general the layout is OS dependent, and all compilers targeting that OS must conform to the ABI (though most will have options to generate different layout if you know what you are doing). The ABI also defines how parameters are passed to functions, how values are returned, which registers must be preserved across calls, and so on.

The ABI definition you need should be available on http://www.x86-64.org/ (seems to be down)

like image 167
Chris Avatar answered Oct 12 '22 06:10

Chris