GCC Function Name Conflict

I had some problems with the sample code that I tested, since my abs function did not return the correct result. abs (-2) output -2 (this, by the way, is considered a function of the absolute value, if it was unclear)

After a little despair, I finally got the following code

#include <stdio.h>

unsigned int abs(int x) {
    return 1;
}

int main() {
    printf("%d\n", abs(-2));
    return 0;
}

This is nothing useful, but it serves to demonstrate my problem. This printed -2 ​​when output 1 was expected.

if I change the name of the function to something else (e.g. abs2), the result is now correct. In addition, if I change it to receive two arguments instead of one, it will also fix the problem.

: . , -2 ( 2, abs). ( abs abs2)

:

23,25c23,25
< .globl abs
<   .type   abs, @function
< abs:
---
> .globl abs2
>   .type   abs2, @function
> abs2:
54c54
<   .size   abs, .-abs
---
>   .size   abs2, .-abs2
71c71,74
<   movl    -4(%rbp), %edx
---
>   movl    -4(%rbp), %eax
>   movl    %eax, %edi
>   call    abs2
>   movl    %eax, %edx

, , ( abs) , x abs (x)

, : , , - .

Squeeze Debian, ggc 4.4.5, gcc 4.1.2

+5
3

GCC - :

  • abs - ;
  • abs unsigned int, ( ) abs signed int.

gcc -fno-builtin; , 1. , abs, signed int, 2.

( - . , unsigned int %d.)

+7

gcc abs() abs(). , -fno-builtin ( abs() int), , . this ():

GCC C. _builtin , C -fno-builtin. (. C ) ; , , .

stdlib.h, abs(), -, .

+2

It sounds the same as this error , which has been marked as fixed since 2007.

You should, of course, try to compile without GCC intrinics, i.e. pass -fno-builtin(or just -fno-builtin-absto run only abs()) when compiling.

+2
source

All Articles