Bug 72750 - Not possible to use -fno-builtin to suppress optimization of some standard functions like atoi
Summary: Not possible to use -fno-builtin to suppress optimization of some standard fu...
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2016-07-29 20:04 UTC by David Kreitzer
Modified: 2016-07-30 14:03 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description David Kreitzer 2016-07-29 20:04:48 UTC
For some standard functions like memset, the programmer can override the standard function with a custom implementation using -fno-builtin or -fno-builtin-<name>.

That doesn't work for functions like atoi that have inline implementations in the headers. For example:

---- t.c ----
#include <stdlib.h>
#include <string.h>
int f1(void *p, const char *s)
  memset(p, 0, 16);
  return atoi(s);

With "gcc -S -O2 t.c", both calls get optimized. Adding -fno-builtin-memset suppresses the memset optimization, enabling the programmer to provide their own implementation of memset. Adding -fno-builtin-atoi has no effect.

Would this be considered a bug or a feature? And if the latter, is there a recommended method for suppressing the optimization of atoi without completely disabling inlining or optimization in general?
Comment 1 joseph@codesourcery.com 2016-07-29 20:18:31 UTC
This is not a bug.  You could make the file using atoi declare it itself 
rather than including <stdlib.h> (because the type of atoi doesn't involve 
any types from standard headers, doing so is valid).  Or, again, separate 
the use of atoi into a separate file and use -fno-inline for that file.
Comment 2 Andrew Pinski 2016-07-29 21:35:05 UTC
This is not a bug, the headers are declaring atoi for optimized version.
Comment 3 David Kreitzer 2016-07-30 11:07:48 UTC
Thanks for the prompt responses.

Please consider the fact that the same behavior also occurs with "gcc -S -O2 -ffreestanding t.c".

Wouldn't you agree that using the inline implementation of atoi from the headers is a clear violation of freestanding semantics?
Comment 4 Andreas Schwab 2016-07-30 14:03:51 UTC
<stdlib.h> is not a standard part of a freestanding implementation.