[llvm-dev] strdup vs __strdup?

Hans Wennborg via llvm-dev llvm-dev at lists.llvm.org
Wed Oct 14 15:25:41 PDT 2015

Hi Scott,

On Wed, Oct 14, 2015 at 3:04 PM, carr27 via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> For background, I'm writing a pass and would like to find all instructions
> that could allocate memory, but this question mainly concerns strdup.
> I'm seeing that calls to strdup are getting replaced by calls to __strdup
> when I compile with -O2.  Can anyone tell me why and what the difference is?
> The replacement is a minor issue for me because isAllocationFn doesn't
> return true for calls to __strdup (but it does for strdup). My current
> workaround is to check the name of every called function.
> Also, if there are any other functions that are replaced (like strdup is
> with __strdup), that'd be useful information to me.
> My test case is [1].  I'm getting the bitcode using Module.print inside my
> pass. [2] is without -O2 and [3] is with -O2.  My pass is in CodeGen in case
> that is relevant.

I don't think it's LLVM that's replacing the strdup call with
__strdup, but the header files in your standard library.

On my machine, /usr/include/string.h includes
/usr/include/x86_64-linux-gnu/bits/string2.h when __OPTIMIZE__ is
defined (which would happen at -O2 but not -O0). string2.h then
defines strdup(s) to __strdup(s) (line 1278 on my machine). string.h
says this can be inhibited by defining __NO_STRING_INLINES.

 - Hans

More information about the llvm-dev mailing list