1

I have a problem. Requirement for the project is that we cannot link our app with standard library ( so -nostdlib is on in gcc).

my_stdlib.c contains implementation of all functions my_memset, my_memcpy ... but linker needs memcpy to copy structs

MyStruct struct = my_struct;

and is complaining about "undefined reference to `memcpy'", which is of course correct.

Is it possible to remap memcpy to my_memcpy using linker script, parameters passed to ld or other way, so linker can use our implementation to copy structs?

Probably -wrap,function could help but I cannot change my_memcpy to __wrap_memcpy.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
bataliero1234
  • 145
  • 10

2 Answers2

1

At the GCC level, you can redirect the memcpy symbol to a different symbol using:

void *memcpy (void *, const void *, size_t) __asm__ ("my_memcpy");

This will apply to internally-generated memcpy calls, too. (With GCC. I think it does not change the internal call sites with Clang.)

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
  • Other interesting things beyond OPs scope: complie your own glibc: https://stackoverflow.com/questions/10763394/how-to-build-a-c-program-using-a-custom-version-of-glibc-and-static-linking and Newlib https://electronics.stackexchange.com/questions/223929/c-standard-libraries-on-bare-metal/223931 – Ciro Santilli OurBigBook.com Dec 06 '18 at 14:46
0

compile with -fno-builtin. This should avoid it.

mfro
  • 3,286
  • 1
  • 19
  • 28
  • If you compile with `-fno-builtin` that will not solve the issue. No builtin will still allow the compiler to generate the non builtin form which may be a call to an external `memcpy`. The OP would still have to map `memcpy` to `my_memcpy` routine. – Michael Petch Jul 26 '17 at 16:59
  • Don't think so. If this is indeed the case, I would consider it a bug (which might be avoided with `-ffreestanding`, but this might cause other issues). – mfro Jul 26 '17 at 17:15
  • At a glance, I didn't find any `libgcc` (for a host of usual and unusual platforms I have available) that would provide it's own `memcpy()` function. – mfro Jul 26 '17 at 17:24