OpenWrt Forum Archive

Topic: gcc optimization question

The content of this topic has been archived on 15 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

I'm compiling this simple program:

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

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

If I compile it with these flags all works fine when I run the binary on my router:

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

This is the output:

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

The problem is when I remove -Os from the flags above and keeping the rest of the flags untouched. It compiles with out errors but at run time I get this output:

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

Why is removing the -Os flag causing this error at run time ?

Thank you.

I did more digging and I got the assembly code for each situation.

Assembly code when using -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

Assembly code without -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    mips16
    .set    nomicromips
    .ent    main
    .type    main, @function
main:
    .frame    $17,24,$31        # vars= 8, regs= 2/0, args= 16, gp= 8
    .mask    0x80020000,-4
    .fmask    0x00000000,0
    save    40,$17,$31
    addiu    $17,$sp,16
    lw    $4,$L2
    jal    puts
    li    $4,120
    jal    toupper
    sw    $2,8($17)
    lw    $2,8($17)
    move    $24,$2
    move    $5,$24
    lw    $4,$L3
    jal    printf
    move    $2,$24
    move    $sp,$17
    restore    24,$17,$31
    j    $31
    .align    2
$L2:
    .word    $LC0
$L3:
    .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

The discussion might have continued from here.