【问题标题】:gcc compilation error with inline assembly: operand type mismatch for ljmp内联汇编的 gcc 编译错误:ljmp 的操作数类型不匹配
【发布时间】:2019-02-17 12:07:36
【问题描述】:

无论出于何种原因,以下内联汇编(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’

非常感谢。

【问题讨论】:

  • 这看起来很可疑,好像您启用了 PIE(可能是默认情况下)。尝试以某种组合添加-fno-PIC -fno-pic -no-pie 开关。 gcc.godbolt.org 有效,除非我添加 -fPIC-fpic
  • 非常感谢@Jester - 添加-fno-PIC-fno-pic 似乎确实可以解决问题。将发布后续答案,包括更多详细信息。

标签: gcc assembly att osdev


【解决方案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 可以解析为编译时常量 => 可以生成该指令的有效机器代码。

【讨论】:

  • 恕我直言,更好的解决方案是构建 i686、i386 或 x86-64 ELF 交叉编译器和工具链,并停止使用本机主机编译器进行 OSDev 工作。
  • @MichaelPetch 在某些方面确实更好,但有一个很大的缺点:对于想要为您的爱好操作系统做出贡献的人来说,这会变得更加尴尬,因为他们也需要相同的交叉编译器。这就是我现在选择这种方法的原因——它通常工作得很好。现在,如果有人会做一个 gcc 交叉编译器的“干净”.deb 包,那当然很棒......
猜你喜欢
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
相关资源
最近更新 更多