Linker failure that depends on -g switch
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
GNU Arm Embedded Toolchain |
New
|
Undecided
|
Unassigned |
Bug Description
I have a strange issue that is probably related to how gcc treats builtin functions memcpy, memmove etc.
Possibly related : https:/
Below is a pasted three-file demo of the issue. When -g switch is removed from CFLAGS, this sample fails to link with:
: undefined reference to `memcpy'
Tested with arm-none-
The issue seems to be, that when compiling with -g linker will automatically reference libg for finding memcpy as below:
arm-none-
However, when this line in makefile is commented out:
#CFLAGS+=-g
The linking fails, as for some reason linker does not find libg anymore. This is very obscure behaviour - not really a big issue once you find the magic switch, but using -g or not using should not have anything to do with this sample building or not.
I ran into this while working on a baremetal target using a custom minimized libc implementation, that by default is built as libc.a, which i believe causes gcc to trigger some special logic and rules for builtin symbol lookups.
diff -ubwN nobugs/bug.c bugs/bug.c
--- nobugs/bug.c 1969-12-31 16:00:00.000000000 -0800
+++ bugs/bug.c 2015-10-22 19:16:43.745472300 -0700
@@ -0,0 +1,10 @@
+
+int main(int argc, char **argv) {
+ memcpy(
+ return 0;
+}
+
+void _start() {
+ main(1,0);
+}
+
diff -ubwN nobugs/libc.c bugs/libc.c
--- nobugs/libc.c 1969-12-31 16:00:00.000000000 -0800
+++ bugs/libc.c 2015-10-22 18:56:18.151341600 -0700
@@ -0,0 +1,2 @@
+
+void no_functions() {}
diff -ubwN nobugs/Makefile bugs/Makefile
--- nobugs/Makefile 1969-12-31 16:00:00.000000000 -0800
+++ bugs/Makefile 2015-10-22 19:18:45.067378200 -0700
@@ -0,0 +1,13 @@
+CFLAGS+=-g
+CC=arm-
+
+LDFLAGS=
+LDFLAGS+=-L . -lc
+LDFLAGS+
+
+libc.a: libc.a(libc.o)
+
+all: libc.a bug
+
+clean:
+ rm -f *.o *.a bug