Discussion:
A GCC bug related to inline?
Bin.Cheng
2018-11-11 06:58:26 UTC
Permalink
Hi,
Given below simple code:

inline int foo (int a) {
return a + 1;
}
int g = 5;
int main(void) {
return foo(g);
}

When compiled with -O0, GCC generates below assembly:
.file "test.c"
.text
.globl g
.data
.align 4
.type g, @object
.size g, 4
g:
.long 5
.text
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl g(%rip), %eax
movl %eax, %edi
call foo
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (GNU) 9.0.0 20181031 (experimental)"
.section .note.GNU-stack,"",@progbits

Note the body of function foo is removed while call to foo isn't
inlined, this results in undefined reference to "foo" at linking.

Guess this is a bug?

Thanks,
bin
Kumar, Venkataramanan
2018-11-11 07:22:46 UTC
Permalink
Hi Bin,
-----Original Message-----
Bin.Cheng
Sent: Sunday, November 11, 2018 12:28 PM
Subject: A GCC bug related to inline?
Hi,
inline int foo (int a) {
return a + 1;
}
int g = 5;
int main(void) {
return foo(g);
}
.file "test.c"
.text
.globl g
.data
.align 4
.size g, 4
.long 5
.text
.globl main
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl g(%rip), %eax
movl %eax, %edi
call foo
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.size main, .-main
.ident "GCC: (GNU) 9.0.0 20181031 (experimental)"
Note the body of function foo is removed while call to foo isn't inlined, this
results in undefined reference to "foo" at linking.
Guess this is a bug?
Not sure if this is a bug.
Looks like -std=c99 or -std=gnu99 inline won't generate function body. Older standard do emit, I used extra -fgnu89-inline.
Thanks,
Jakub Jelinek
2018-11-11 11:28:11 UTC
Permalink
Post by Bin.Cheng
inline int foo (int a) {
return a + 1;
}
int g = 5;
int main(void) {
return foo(g);
}
This is the standard C99 inlining behavior. inline means
provide something that can be inlined, but don't generate an external
definition. One needs to use the extern keyword in one of the TUs to emit
it. See
https://gcc.gnu.org/onlinedocs/gcc/Inline.html
https://gcc.gnu.org/gcc-4.2/changes.html
for more info. You can use -fgnu89-inline, or -std=gnu89, or gnu_inline
attribute if you are looking for the GNU inlining semantics rather than C99
one. And of course, in C++ the inlining behavior is yet different (comdat).

Jakub

Continue reading on narkive:
Loading...