gcc - 内联汇编的 gcc 编译错误:ljmp 的操作数类型不匹配
问题描述
无论出于何种原因,以下内联汇编(AT&T/gcc 风格)在新安装的 Debian GNU/Linux 64 位机器上不起作用;它可以在其他机器上编译并正常工作:
static void INIT_CODE kernel_entry (void) NORETURN;
void _start(void)
{
// ...other code here...
asm ("ljmp %0, %1"
:
: "n" (SELECTOR_KERNEL_CODE),
"p" (&kernel_entry));
}
static void INIT_CODE kernel_entry(void)
{
// ...
}
我得到的编译错误是 gcc 7 和 8:
$ gcc-7 -o init.o -Wall -Wextra -Wshadow -Wpointer-arith -Waggregate-return \
-Wredundant-decls -Winline -Werror -Wcast-align -Wsign-compare -Wmissing-declarations \
-Wmissing-noreturn -pipe -O0 -fno-builtin -fno-asynchronous-unwind-tables \
-funsigned-char -g -fomit-frame-pointer -ffreestanding -DPACKAGE_NAME=\"storm\" \
-DPACKAGE_VERSION=\"0.5.1+\" \
-DREVISION=\"`git rev-list HEAD --max-count 1 --abbrev-commit`\" \
-DCREATOR=\"`whoami`@`hostname -s`\" --std=gnu99 -Wbad-function-cast \
-Wmissing-prototypes -Wnested-externs -Wstrict-prototypes -m32 -I../include \
-I.. -I. -c init.c
init.c: Assembler messages:
init.c:151: Error: operand type mismatch for `ljmp'
我还尝试查看汇编代码(用 编译-S
),它为什么不编译是完全有道理的:
#NO_APP
leal kernel_entry@GOTOFF(%eax), %eax
#APP
# 151 "init.c" 1
ljmp $8, %eax
# 0 "" 2
这行不通;该ljmp
指令只接受两个常量操作数(即不%eax
作为第二个操作数)。
那么,我该如何gcc
理解这一点呢?我需要更改p
参数约束吗?我尝试将其更改为n
已经,但后来我收到此错误:
init.c: In function ‘_start’:
init.c:151:5: warning: asm operand 1 probably doesn’t match constraints
asm ("ljmp %0, %1"
^~~
init.c:151:5: error: impossible constraint in ‘asm’
提前谢谢了。
解决方案
正如评论中所建议的,事实证明问题是我默认gcc
使用的是PIC:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8.2.0-5' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 8.2.0 (Debian 8.2.0-5)
--enable-default-pie
标志是这里的问题;它默认启用 PIC/PIE 模式。
显然,出于安全原因,许多 Linux 发行版(包括 Debian)默认情况下已经转移到与位置无关的代码: https ://wiki.debian.org/Hardening/PIEByDefaultTransition
像这样的代码(在与位置无关的模式下确实不起作用)的解决方法是添加-fno-pic
到编译器参数列表中 - 这给了我们以下 asm 输出,而不是效果更好:
# 0 "" 2
.loc 1 151 0
# 151 "init.c" 1
ljmp $8, $kernel_entry
# 0 "" 2
组装时,$kernel_entry
可以将其解析为编译时常量 => 可以生成该指令的有效机器代码。
推荐阅读
- winapi - 为什么使用 SM_REMOTESESSION 查询成功但 WTSIsRemoteSession 失败?
- amazon-web-services - 如何在 aws-cli 中对“list-tasks”的结果进行排序?
- arrays - MongoDB $elemMatch 查找多个元素
- python - 更改熊猫中的列值应用另一个函数
- c++ - count_if 中外部值的比较
- terminal - Tmux 垂直拆分窗口并定义顶部窗格的高度
- python - 如何在我的系统上找到 MacPorts Python?
- dataweave - 骡子 4:Dataweave 2.0:java.lang.StackOverflowError。在连接期间并减少大于 10k 的记录大小
- amp-email - AMP 电子邮件图像自动加载?
- html - 如何使用 HTML、Django 中的选项正确呈现“if”条件