'int main(){ return(0); }' での浮動小数点例外 (SIGFPE) 質問する

'int main(){ return(0); }' での浮動小数点例外 (SIGFPE) 質問する

2 つの異なる Linux 環境用に簡単な C プログラムを作成しようとしています。一方のデバイスではプログラムは正常に動作しますが、もう一方のデバイスではプログラムが浮動小数点例外を生成します。プログラムはメインから 0 を返すだけで、起動コード (おそらく ABI) との非互換性があると思われます。

このプログラムは、次のビルド仕様で gcc を使用してコンパイルされます。

組み込み仕様の使用。ターゲット: i386-redhat-linux 次を使用して設定: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux スレッド モデル: posix gcc version 4.1.2 20080704 (レッドハット 4.1.2-52)

プログラムのソースは次のとおりです。

int main()
{
        return(0);
}

Celeron デバイスでは、このプログラムは GDB で以下を生成します。

[root@n00200C30AA2F jrn]# /jrn/gdb fail GNU gdb Red Hat Linux (5.3post-0.20021129.18rh) (gdb) run Starting program: /jrn/fail 

Program received signal SIGFPE, Arithmetic exception. 0x40001cce in ?? () (gdb) bt
#0  0x40001cce in ?? ()
#1  0x4000c6b0 in ?? ()
#2  0x40000cb5 in ?? ()

以下は、何が起こっているのかを知るために私が収集できると思われる詳細です。

CELERON:  ( fails on this device )
2.6.8 #21 Mon Oct 1 11:41:47 PDT 2007 i686 i686 i386 GNU/Linux
============
[root@n00200C30AA2F proc]# cat cpuinfo 
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 9
model name      : Intel(R) Celeron(R) M processor          600MHz
stepping        : 5
cpu MHz         : 599.925
cache size      : 512 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr mce cx8 sep mtrr pge mca cmov pat clflush dts acpi mmx fxsr sse sse2 tm pbe
bogomips        : 1179.64

GNU C Library stable release version 2.3.2, by Roland McGrath et al.
Compiled by GNU CC version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
Compiled on a Linux 2.4.20 system on 2003-03-13.
Available extensions:
        GNU libio by Per Bothner
        crypt add-on version 2.1 by Michael Glad and others
        linuxthreads-0.10 by Xavier Leroy
        BIND-8.2.3-T5B
        libthread_db work sponsored by Alpha Processor Inc
        NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk

ATOM:  ( works fine on this device )
2.6.35 #25 SMP Mon Mar 12 09:02:45 PDT 2012 i686 i686 i386 GNU/Linux
==========
[root@n00E04B36ECE5 ~]# cat /proc/cpuinfo 
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 28
model name      : Genuine Intel(R) CPU N270   @ 1.60GHz
stepping        : 2
cpu MHz         : 1599.874
cache size      : 512 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx constant_tsc up arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 xtpr pdcm movbe lahf_lm
bogomips        : 3199.74
clflush size    : 64
cache_alignment : 64
address sizes   : 32 bits physical, 32 bits virtual
power management:


GNU C Library stable release version 2.5, by Roland McGrath et al.
Compiled by GNU CC version 4.1.2 20080704 (Red Hat 4.1.2-44).
Compiled on a Linux 2.6.9 system on 2009-09-02.
Available extensions:
        The C stubs add-on version 2.1.2.
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        GNU libio by Per Bothner
        NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
        RT using linux kernel aio
Thread-local storage support included.

この問題の原因を特定するにはどうすればよいでしょうか? 特定のバージョンの libc に対して静的にリンクしてみるのはいかがでしょうか?

GDB で障害が発生した後、次を実行します。

(gdb) x/1i $eip
0x40001cce:     divl   0x164(%ecx)
(gdb) info reg
eax            0x6c994f 7117135
ecx            0x40012858       1073817688
edx            0x0      0
ebx            0x40012680       1073817216
esp            0xbffff740       0xbffff740
ebp            0xbffff898       0xbffff898
esi            0x8049580        134518144
edi            0x400125cc       1073817036
eip            0x40001cce       0x40001cce
eflags         0x10246  66118
cs             0x73     115
ss             0x7b     123
ds             0x7b     123
es             0x7b     123
fs             0x0      0
gs             0x0      0
(gdb) x/1wx 0x164+$ecx
0x400129bc:     0x00000000
(gdb) 

私が受けた支援に基づくと、何らかの理由で libc の起動コードが 0 で除算しているようです。

ここでの疑問は、この明らかに悪い動作の原因は何なのかということです。何かが他のものと互換性がないのでしょうか?

アセンブリ出力:

[jrn@localhost ~]$ more fail.s
        .file   "fail.c"
        .text
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        movl    $0, %eax
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-52)"
        .section        .note.GNU-stack,"",@progbits

ベストアンサー1

これはかなり難しいことのように聞こえるかもしれませんが、次のことを試してみませんか?

$ readelf -a fail

GNU_HASH ダイナミックタグを探してみませんか? バイナリは を使用していますGNU_HASHが、あなたのはld.soそれを理解するには古すぎると思います。GNU ハッシュセクションのサポートは glibc に 2006 年頃に追加され、メインラインディストリビューションは 2007 年か 2008 年頃から GNU ハッシュのみになりました。あなたの Centrinoglibc2003GNU ハッシュより古いものです。

がGNUハッシュを理解できない場合はld.so、代わりに空の古いELFハッシュセクションを使用しようとします。特に、クラッシュは次の場所で発生していると思われます。この行のelf/do-lookup.h:

for (symidx = map->l_buckets[hash % map->l_nbuckets];

リンカーはおそらく GNU ハッシュを理解していないため、l_nbucketsは 0 となり、クラッシュが発生します。 はmap約 100 個の構造体要素を持つ大きな構造体であり、l_nbuckets新しい では構造体の約 90 番目のメンバーであることに注意してくださいld.so(0x164 = 4*89なので、古い ではld.soおそらくまさにこのメンバーです)。

これが決定的に問題が発生した場合は、-Wl,--hash-style=sysvまたは を使用してビルドし-Wl,--hash-style=both、クラッシュが解消されるかどうかを確認してください。

おすすめ記事