在Linux中/ x86-64的系统调用结构组装程序员的布局? [英] Layout of structs in Linux/x86-64 syscalls for assembly programmers?

查看:163
本文介绍了在Linux中/ x86-64的系统调用结构组装程序员的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

若干的Linux / x86-64的系统调用接受指针结构作为参数。<​​/ P>

例如第二个参数 STAT(2) struct stat中* ...

  struct stat中{
       的dev_t st_dev; / *包含设备的ID文件* /
       ino_t过去st_ino中; / *索引节点号* /
       mode_t ST_MODE; / *保护* /
       nlink_t st_nlink; / *硬链接数* /
       将uid_t st_uid; / *用户所有者的ID * /
       gid_t st_gid; / *组所有者*的ID /
       的dev_t st_rdev; / *设备ID(如有特殊文件)* /
       off_t st_size; / *总字节大小* /
       blksize_t st_blksize; / *块大小文件系统I / O * /
       blkcnt_t的st_blocks; / *分配512B块数* /
       time_t的st_atime的; / *上次访问时间的* /
       time_t的st_mtime; / *最后修改时间* /
       time_t的st_ctime; / *最后状态改变的时间* /
   };

这意味着,如果你要调用从单纯的装配系统调用比你必须了解每种类型有多大规则,是否有成员对齐的目的,等等之间的空白。

请问C标准定义离开这个开放是(编译器)实现还是可以从标准确定(假设原始类型的大小都知道)?

如果它是开放的,没有内核或x86-64架构,无论如何界定呢?或者是刚刚的事情的发生编译器与编译内核?

(即给定的结构我需要计算的起始偏移量相对于该成员的结构地址的一些成员)


解决方案
结构的布局是不是在C标准定义,但在ABI的定义,你的情况和System V ABI AMD64

。也就是说,在一般的布局是依赖于操作系统的,和定位OS所有的编译器必须符合ABI(尽管大多数将选择产生不同的布局,如果你知道你在做什么)。该ABI还定义参数的方式传递给函数,值的方式返回,该寄存器必须在调用pserved $ P $,等等。

您需要应可在 http://www.x86-64.org/ 的ABI定义(似乎下来)

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)

解决方案

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)

这篇关于在Linux中/ x86-64的系统调用结构组装程序员的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆