问题
I'm working on some software which for a limited time runs bare-metal until the Linux port is ready for prime time. The software is being linked against uClibc which provides implementations of malloc, clock_gettime etc, but the problem is that they all rely on syscalls which will just trap the hardware since we don't have a kernel to handle them yet.
I have been using aliasing to override the functions that we need, i.e. our_mem.c:
void* our_malloc(size_t size) { .. }
void* malloc(size_t size) __attribute__ ((malloc, weak, alias("our_malloc")));
The problem is that this seems to work somewhat randomly. I can have one build where all the calls are aliased properly, in the next build where I modified something in an unrelated file it suddenly works up until a point and then one of the calls go to uClibc malloc instead. At first I thought it was a matter of link order but the build system always links in the same order and the same call will work in one build and fail in the next. The binaries are (obviously) statically linked.
I'm not sure this is exactly how aliasing is meant to be used since I haven't found much documentation on it but I have successfully used this technique a few years before on malloc and friends (also with uClibc) and then it worked consistently.
来源:https://stackoverflow.com/questions/18278235/aliasing-of-symbol-using-gcc-binutils-works-intermittently