C 试图理解linux内核中的一段内嵌arm程序集
我试图理解这段内联程序集的功能,它是从CC 试图理解linux内核中的一段内嵌arm程序集,c,gcc,assembly,gnu-assembler,arm64,C,Gcc,Assembly,Gnu Assembler,Arm64,我试图理解这段内联程序集的功能,它是从CDEFINE宏生成的: #define DEFINE(sym, val) asm volatile("\n->" #sym " %0 " #val : : "i" (val)) #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) 在内核fork中进行AArch64smc调用时使用了它。使用上述C宏定义值的位置是: 稍后,这些定义将在程序集宏中使用(或者至少看起
DEFINE
宏生成的:
#define DEFINE(sym, val) asm volatile("\n->" #sym " %0 " #val : : "i" (val))
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
在内核fork中进行AArch64smc
调用时使用了它。使用上述C宏定义值的位置是:
稍后,这些定义将在程序集宏中使用(或者至少看起来是这样):
最终在实践中使用,例如在
我试图将代码隔离到一个单独的裸机AArch64程序中,以查看宏在实践中是如何扩展和工作的,但我得到的只是编译错误
// main.c
// the struct arm_smccc_res and others are inserted here to satisfy the compiler
int
main()
{
DEFINE(ARM_SMCCC_RES_X0_OFFS, offsetof(struct arm_smccc_res, a0));
DEFINE(ARM_SMCCC_RES_X2_OFFS, offsetof(struct arm_smccc_res, a2));
DEFINE(ARM_SMCCC_QUIRK_ID_OFFS, offsetof(struct arm_smccc_quirk, id));
DEFINE(ARM_SMCCC_QUIRK_STATE_OFFS, offsetof(struct arm_smccc_quirk, state));
return 0;
}
// gcc output
/tmp/cccqaaY3.s: Assembler messages:
/tmp/cccqaaY3.s:459: Error: junk at end of line, first unrecognized character is `-'
/tmp/cccqaaY3.s:464: Error: junk at end of line, first unrecognized character is `-'
/tmp/cccqaaY3.s:469: Error: junk at end of line, first unrecognized character is `-'
/tmp/cccqaaY3.s:474: Error: junk at end of line, first unrecognized character is `-'
宏的偏移量是不言自明的,但是我在理解定义宏中字符串化和->
的用法时遇到了困难
你知道它扩展到什么,或者如何成功编译它吗?你会出错,因为该文件不应该编译为对象。它仅用于生成程序集输出,然后对其进行处理以创建标题asm\u offsets.h
。这个标题就是其他地方包含的内容。您可以在include/generated
中找到它。创建它的规则位于顶层KBuild
:
# Kbuild for top-level directory of the kernel
# This file takes care of the following:
# 1) Generate bounds.h
# 2) Generate timeconst.h
# 3) Generate asm-offsets.h (may need bounds.h and timeconst.h)
# 4) Check for missing system calls
# 5) Generate constants.py (may need bounds.h)
# Default sed regexp - multiline due to syntax constraints
define sed-y
"/^->/{s:->#\(.*\):/* \1 */:; \
s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
s:->::; p;}"
endef
您可以看到它使用sed
将程序集输出转换为标题。可以在arch//kernel/asm offsets.s
中找到临时程序集文件。采样线如下所示:
->pt_regs_bx $40 offsetof(struct pt_regs, bx) #
->pt_regs_cx $88 offsetof(struct pt_regs, cx) #
->pt_regs_dx $96 offsetof(struct pt_regs, dx) #
#define pt_regs_bx 40 /* offsetof(struct pt_regs, bx) # */
#define pt_regs_cx 88 /* offsetof(struct pt_regs, cx) # */
#define pt_regs_dx 96 /* offsetof(struct pt_regs, dx) # */
请注意,这不是有效的汇编语法,但编译器并不在意,它会在执行参数替换后无意识地发出放入asm
块中的任何内容。然后,sed
命令对其进行转换,使标题中的匹配行如下所示:
->pt_regs_bx $40 offsetof(struct pt_regs, bx) #
->pt_regs_cx $88 offsetof(struct pt_regs, cx) #
->pt_regs_dx $96 offsetof(struct pt_regs, dx) #
#define pt_regs_bx 40 /* offsetof(struct pt_regs, bx) # */
#define pt_regs_cx 88 /* offsetof(struct pt_regs, cx) # */
#define pt_regs_dx 96 /* offsetof(struct pt_regs, dx) # */
[macro]标记是其中最不重要的一个。。。无法在源文件上安装内联程序集Usegcc-S
,以生成该文件的程序集。大概<<代码>定义 S在汇编文件的中间是有意义的,这就是<代码> ASM Value是为了确保它们不被移动…