long long int值打印不正确

时间:2013-08-16 11:34:57

标签: c embedded arm gcc4.7

当我使用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. 即使单词被交换了,为什么下面的单词会出现垃圾。
  3. 编译器信息:

    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) 
    

1 个答案:

答案 0 :(得分: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

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

<击> 1.实际:5 = 0(高32)和5(低32)

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

  2. 因此,您的代码最有可能将0加载到R1,将5加载到R2,但printfR2解释为R3的高部分和垃圾低部分。您可以通过检查生成的组件轻松检查它。

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

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

    1. 实际:5 = 0(高32)和5(低32)

      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

    3. 因此,证据确实表明相反:您的代码使用EABI而printf期待OABI。