-4

In my program i use memcpy. Gcc generate code, that use __builtin___memcpy_chk. I have downloaded gcc source code https://github.com/gcc-mirror/gcc and do grep -A 10 -R "__builtin___memcpy_chk" . but i can't find implementation __builtin___memcpy_chk. Where can i find implementation for this function?

  • 1
    What is it you want to do/find and why? It's not clear (to me) what you are actually trying to accomplish (or asking). – Jesper Juhl Aug 08 '19 at 16:09
  • There isn't a source file with C code for memcpy in gcc that you could copy-paste elsewhere. It generates code directly. If you really want to look, it is somewhere in `gcc/config/i386/`. And glibc has a gazillion implementations of memcpy, if you didn't find one using SSE, you probably didn't look hard enough. – Marc Glisse Aug 08 '19 at 16:16
  • Are you looking for the `memcpy` source code **OR** the parsing / semantics for choosing the version of `memcpy`? – Thomas Matthews Aug 08 '19 at 16:16
  • @JesperJuhl this site did not allow me to write a short question, I had to write a lot. I’m only interested in why I can’t find an implementation of the __builtin___memcpy_chk function – Danyal Mugutdinov Aug 08 '19 at 16:31
  • @DanyalMugutdinov simple answer from the functions name: It's built into the compiler there is no supporting library function the compiler generates the CPU instructions itself. – Richard Critten Aug 08 '19 at 16:36
  • @RichardCritten It doesn’t scare me that this function is built into the compiler. I downloaded its source code and just wanted to see the place where this happens. Even if it is an assembly instruction or an intrinsics – Danyal Mugutdinov Aug 08 '19 at 16:43
  • If there is no such function and the compiler processes this place without calling the function - it's okay. But miracles do not happen, somewhere there is a place where assembler instructions appear instead of functions – Danyal Mugutdinov Aug 08 '19 at 16:45
  • _"... i have downloaded glibc source code..."_ you need the compiler source not the library source. – Richard Critten Aug 08 '19 at 16:45
  • @RichardCritten I wrote below that I also downloaded the gcc source code – Danyal Mugutdinov Aug 08 '19 at 16:48

1 Answers1

3

Builtins such as __builtin___memcpy_chk are not functions as such, so you may not find a "definition" for it.

GCC is a compiler. It implements the language. It reads your function definitions an emits a binary with instructions for the CPU. In this case it emits instructions for the CPU without reading a C or C++ source file. Instead, it directly manipulates the internal data structure that it uses to represent the program that it is compiling.

You may want to take a look at expand_builtin_memory_chk defined in gcc/builtins.c for what it does.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • then what is it? How is this implemented if it is not a function? thanks for your reply – Danyal Mugutdinov Aug 08 '19 at 16:28
  • 2
    @DanyalMugutdinov GCC is a compiler. It implements the language. It reads your function definitions an emits a binary with instructions for the CPU. In this case it emits instructions for the CPU without reading a C or C++ source file. You can learn more by reading the function that I linked, and by following into functions that it calls. Forewarning: Compilers are complex beasts. – eerorika Aug 08 '19 at 16:32