Linker failure that depends on -g switch

Bug #1509163 reported by kert
6
This bug affects 1 person
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://gcc.gnu.org/bugzilla/show_bug.cgi?id=56888

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-eabi-gcc.exe (GNU Tools for ARM Embedded Processors) 4.9.3 20150303 (release)

The issue seems to be, that when compiling with -g linker will automatically reference libg for finding memcpy as below:
arm-none-eabi/lib\libg.a)lib_a-memcpy.o

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(&argc,&argc,1);
+ 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-none-eabi-gcc
+
+LDFLAGS=-ffreestanding -nostartfiles
+LDFLAGS+=-L . -lc
+LDFLAGS+=-Wl,--trace
+
+libc.a: libc.a(libc.o)
+
+all: libc.a bug
+
+clean:
+ rm -f *.o *.a bug

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.