内联汇编的gcc编译错误:ljmp的操作数类型不匹配

问题描述 投票:2回答:1

无论出于何种原因,以下内联汇编(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 argument constraint吗?我尝试将其更改为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 assembly att osdev
1个回答
1
投票

正如评论中所建议的那样,问题是我的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解析为编译时常量=>可以生成该指令的有效机器代码。

© www.soinside.com 2019 - 2024. All rights reserved.