From c22b8a5631b289e3aa9f54aa0da59af580d2d186 Mon Sep 17 00:00:00 2001
From: Alessandro Rubini <rubini@gnudd.com>
Date: Tue, 20 Nov 2012 14:26:21 +0100
Subject: [PATCH] Makefile: build an intermediate object

Pretty often it happens that during trials we pick wrong external
functions (e.g., "printf") and the final link claims horribly about
dozens of undefined symbols.  By having an intermediate wrc.o we can
easily check which are the symbols we really leave undefined for libc
and libgcc to fill, before they resolve them by asking for something
else.

Signed-off-by: Alessandro Rubini <rubini@gnudd.com>
---
 Makefile | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 7b93505..b247971 100644
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,7 @@ BOARD = spec
 CROSS_COMPILE ?= lm32-elf-
 
 CC =		$(CROSS_COMPILE)gcc
+LD =		$(CROSS_COMPILE)ld
 OBJDUMP =	$(CROSS_COMPILE)objdump
 OBJCOPY =	$(CROSS_COMPILE)objcopy
 SIZE =		$(CROSS_COMPILE)size
@@ -93,12 +94,15 @@ all: tools $(OUTPUT).ram $(OUTPUT).vhd
 .PRECIOUS: %.elf %.bin
 .PHONY: all tools clean gitmodules
 
-$(OUTPUT).elf: $(LDS) silentoldconfig gitmodules $(OBJS)
+$(OUTPUT).elf: $(LDS) silentoldconfig gitmodules $(OUTPUT).o
 	$(CC) $(CFLAGS) -DGIT_REVISION=\"$(REVISION)\" -c revision.c
-	${CC} -o $@ revision.o $(OBJS) $(LDFLAGS)
+	${CC} -o $@ revision.o $(OUTPUT).o $(LDFLAGS)
 	${OBJDUMP} -d $(OUTPUT).elf > $(OUTPUT)_disasm.S
 	$(SIZE) $@
 
+$(OUTPUT).o: $(OBJS)
+	$(LD) --gc-sections -e _start -r $(OBJS) -o $@
+
 %.bin: %.elf
 	${OBJCOPY} -O binary $^ $@
 
-- 
GitLab