gcc优化奇怪的行为

时间:2015-03-18 12:46:30

标签: linux gcc openwrt

我正在为我的openwrt路由器编译这个简单的程序(我有工具链和暂存目录):

#include <stdio.h>
#include <ctype.h>

main()
{
    printf("Hello World\n");
    int x = toupper('x');
    printf ("GOT:%d\n", x);
}

如果我使用这些标志编译它,当我在路由器上运行二进制文件时,一切正常:

mips-openwrt-linux-uclibc-gcc -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float -mips16 -minterlink-mips16 -I/home/boil/Documents/opewrt/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include   -c -o hello.o hello.c
mips-openwrt-linux-uclibc-gcc -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float -mips16 -minterlink-mips16 -I/home/boil/Documents/opewrt/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include -L/home/boil/Documents/opewrt/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib  hello.o  -o myprogram

这是输出:

root@OpenWrt:/tmp# ./myprogram 
Hello World
GOT:88

问题是当我从上面的标志中删除-Os并保持其余标志不变时。它编译出错误,但在运行时我得到这个输出:

root@OpenWrt:/tmp# ./myprogram 
Hello World
./myprogram: can't resolve symbol 'toupper' in lib './myprogram'.

为什么在运行时删除导致此错误的-Os标志? 谢谢。

以下是-Os:

的汇编代码
    .file   1 "hello.c"
    .section .mdebug.abi32
    .previous
    .nan    legacy
    .gnu_attribute 4, 3
    .abicalls
    .option pic0
    .section    .rodata.str1.4,"aMS",@progbits,1
    .align  2
$LC0:
    .ascii  "Hello World\000"
    .align  2
$LC1:
    .ascii  "GOT:%d\012\000"
    .section    .text.startup,"ax",@progbits
    .align  2
    .globl  main
    .set    mips16
    .set    nomicromips
    .ent    main
    .type   main, @function
main:
    .frame  $sp,32,$31      # vars= 0, regs= 1/0, args= 16, gp= 8
    .mask   0x80000000,-4
    .fmask  0x00000000,0
    save    32,$31
    lw  $4,$L3
    jal puts
    lw  $2,$L4
    lw  $4,$L5
    lw  $2,0($2)
    lh  $5,240($2)
    jal printf
    restore 32,$31
    j   $31
    .align  2
$L3:
    .word   $LC0
$L4:
    .word   __ctype_toupper
$L5:
    .word   $LC1
    .end    main
    .size   main, .-main
    .ident  "GCC: (OpenWrt/Linaro GCC 4.9-2014.10 r44053) 4.9.2"
    .section    .note.GNU-stack,"",@progbits

这是没有-Os的汇编代码

    .file   1 "hello.c"
    .section .mdebug.abi32
    .previous
    .nan    legacy
    .gnu_attribute 4, 3
    .abicalls
    .option pic0
    .rdata
    .align  2
$LC0:
    .ascii  "Hello World\000"
    .align  2
$LC1:
    .ascii  "GOT:%d\012\000"
    .text
    .align  2
    .globl  main
    .set    nomips16
    .set    nomicromips
    .ent    main
    .type   main, @function
main:
    .frame  $fp,40,$31      # vars= 8, regs= 2/0, args= 16, gp= 8
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    .set    noreorder
    .set    nomacro
    addiu   $sp,$sp,-40
    sw  $31,36($sp)
    sw  $fp,32($sp)
    move    $fp,$sp
    lui $2,%hi($LC0)
    addiu   $4,$2,%lo($LC0)
    jal puts
    nop

    li  $4,120          # 0x78
    jal toupper
    nop

    sw  $2,24($fp)
    lw  $5,24($fp)
    lui $2,%hi($LC1)
    addiu   $4,$2,%lo($LC1)
    jal printf
    nop

    move    $sp,$fp
    lw  $31,36($sp)
    lw  $fp,32($sp)
    addiu   $sp,$sp,40
    j   $31
    nop

    .set    macro
    .set    reorder
    .end    main
    .size   main, .-main
    .ident  "GCC: (OpenWrt/Linaro GCC 4.9-2014.10 r44053) 4.9.2"
    .section    .note.GNU-stack,"",@progbits

0 个答案:

没有答案