1

当我使用 Armv7 内核交叉编译目标应用程序时,错误地打印了带有“long long int”的变量。

typedef long long int vmm_int64;
typedef unsigned long long int vmm_uint64;

int main(int argc, char *argv[])
{
    vmm_int64 a, b, result;

    a = 5;
    b = 24;
    result = 0;

    printf("Initial Values are:\n");
    printf("\t a : %lld \n", a);
    printf("\t b : %lld \n", b);
    printf("\t result : %lld \n", result);

    fflush(stdout);

    result = a + b;

    printf("Final Values are:\n");
    printf("\t a : %lld \n", a);
    printf("\t b : %lld \n", b);
    printf("\t result : %lld \n", result);

    fflush(stdout);

    return 0;
}

输出如下:

Initial Values are:
     a : 23639177792
     b : 105243556416
     result : 2164341312
Final Values are:
     a : 23639177792
     b : 105243556416
     result : 126718392896

有人可以解释一下这里发生了什么吗?我应该怎么做才能使它正确?

经过更多分析,我刚刚观察到

a = 0x5 (0x00000005 81013a44  which is hex for  23639177792)
b = 0x18 (0x00000018 81013a40 which is hex for 105243556416)

i. result = 0 (0x00000000 81013a40 which is hex for 2164341312)
ii. result = 0x1D (0x0000001D 81013a40 which is hex for 126718392896)

高 32 位包含正确的值。我只是不明白为什么

  1. 结果是交换了上/下字。
  2. 就算换了字,为什么下面的字还有垃圾。

编译器信息:

arm-none-eabi-gcc -v
Using built-in specs.
COLLECT_GCC=c:\Program Files\CodeSourcery\Sourcery G++ Lite\bin\arm-none-eabi-gcc.exe
COLLECT_LTO_WRAPPER=c:/program files/codesourcery/sourcery g++ lite/bin/../libexec/gcc/arm-none-eabi/4.5.2/lto-wrapper.exe
Target: arm-none-eabi
Configured with: /scratch/janisjo/arm-eabi-lite/src/gcc-4.5-2011.03/configure --build=i686-pc-linux-gnu --host=i686-mingw32 --target=arm-none-eabi --enable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --enable-extra-sgxxlite-multilibs --with-gnu-as --with-gnu-ld --with-specs='%{save-temps: -fverbose-asm} -D__CS_SOURCERYGXX_MAJ__=2011 -D__CS_SOURCERYGXX_MIN__=3 -D__CS_SOURCERYGXX_REV__=42 %{O2:%{!fno-remove-local-statics: -fremove-local-statics}} %{O*:%{O|O0|O1|O2|Os:;:%{!fno-remove-local-statics: -fremove-local-statics}}}' --enable-languages=c,c++ --disable-shared --enable-lto --with-newlib --with-pkgversion='Sourcery G++ Lite 2011.03-42' --with-bugurl=https://support.codesourcery.com/GNUToolchain/ --disable-nls --prefix=/opt/codesourcery --with-headers=yes --with-sysroot=/opt/codesourcery/arm-none-eabi --with-build-sysroot=/scratch/janisjo/arm-eabi-lite/install/host-i686-mingw32/arm-none-eabi --with-libiconv-prefix=/scratch/janisjo/arm-eabi-lite/obj/host-libs-2011.03-42-arm-none-eabi-i686-mingw32/usr --with-gmp=/scratch/janisjo/arm-eabi-lite/obj/host-libs-2011.03-42-arm-none-eabi-i686-mingw32/usr --with-mpfr=/scratch/janisjo/arm-eabi-lite/obj/host-libs-2011.03-42-arm-none-eabi-i686-mingw32/usr --with-mpc=/scratch/janisjo/arm-eabi-lite/obj/host-libs-2011.03-42-arm-none-eabi-i686-mingw32/usr --with-ppl=/scratch/janisjo/arm-eabi-lite/obj/host-libs-2011.03-42-arm-none-eabi-i686-mingw32/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-cloog=/scratch/janisjo/arm-eabi-lite/obj/host-libs-2011.03-42-arm-none-eabi-i686-mingw32/usr --with-libelf=/scratch/janisjo/arm-eabi-lite/obj/host-libs-2011.03-42-arm-none-eabi-i686-mingw32/usr --disable-libgomp --enable-poison-system-directories --with-build-time-tools=/scratch/janisjo/arm-eabi-lite/obj/tools-i686-pc-linux-gnu-2011.03-42-arm-none-eabi-i686-mingw32/arm-none-eabi/bin --with-build-time-tools=/scratch/janisjo/arm-eabi-lite/obj/tools-i686-pc-linux-gnu-2011.03-42-arm-none-eabi-i686-mingw32/arm-none-eabi/bin
Thread model: single
gcc version 4.5.2 (Sourcery G++ Lite 2011.03-42) 
4

1 回答 1

1

看起来你正在混合 ABI。可能您的交叉编译器正在使用 APCS(又名旧 ABI),而 Android 运行时需要 EABI。

两者之间的一个重要区别是 64 位值如何在寄存器中传递。旧 ABI 使用下一个可用的寄存器对,例如

; printf("\t a : %lld \n", a);
load format string into R0
load 'a' into R1 and R2
call printf

而 EABI 使用下一个偶数对齐的寄存器对:

; printf("\t a : %lld \n", a);
load format string into R0
load 'a' into R2 and R3
call printf

让我们看看如何a为两个 ABI 传递:

1. 实际:5 = 0 (high32) 和 5 (low32)

OABI:R1 = 0,R2 = 5
EABI:R2 = 0,R3 = 5

  1. 打印:23639177792 = 0x581013A40 = 0x5(high32)和 0x81013A40(low32)

    OABI:R1 = 5,R2 = 0x81013A40
    EABI:R2 = 5,R3 = 0x81013A40

因此,您的代码很可能将 0 加载到R15R2中,但将其printf解释R2为高部分,而将垃圾解释为R3低部分。您可以通过检查生成的程序集轻松检查它。

修复应该很容易 - 使用编译器选项来生成EABI代码,或者只使用专门针对 Android 的工具链,例如 Android NDK。

编辑:我交换了高低部分。这是正确的版本:

  1. 实际:5 = 0 (high32) 和 5 (low32)

    OABI:R1 = 5,R2 = 0
    EABI:R2 = 5,R3 = 0

  2. 打印:23639177792 = 0x581013A40 = 0x5(high32)和 0x81013A40(low32)

    OABI:R1 = 0x81013A40,R2 = 5
    EABI:R2 = 0x81013A40,R3 = 5

因此,证据实际上表明相反:您的代码正在使用 EABI,而printf正在使用 OABI。

于 2013-08-16T12:14:23.990 回答