build system: run linker scripts through the preprocessor

This allows combining and simplifying linker scripts.

This is inspired by the commit listed below, but rewritten to match
upstream, and split in smaller pieces to keep intent clear.

Change-Id: Ie5c11bd8495a399561cefde2f3e8dd300f4feb98
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Based-On-Change-Id: I50af7dacf616e0f8ff4c43f4acc679089ad7022b
Based-On-Signed-off-by: Julius Werner <jwerner@chromium.org>
Based-On-Reviewed-on: https://chromium-review.googlesource.com/219170
Reviewed-on: http://review.coreboot.org/9303
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/Makefile b/Makefile
index 97a14a8..395910f 100644
--- a/Makefile
+++ b/Makefile
@@ -79,6 +79,8 @@
 HOSTCFLAGS := -g
 HOSTCXXFLAGS := -g
 
+PREPROCESS_ONLY := -E -P -x assembler-with-cpp -undef -I .
+
 DOXYGEN := doxygen
 DOXYGEN_OUTPUT_DIR := doxygen
 
diff --git a/Makefile.inc b/Makefile.inc
index 92f2013..d444457 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -112,9 +112,9 @@
 #######################################################################
 # reduce command line length by linking the objects of each
 # directory into an intermediate file
-ramstage-postprocess=$(foreach d,$(sort $(dir $(1))), \
+ramstage-postprocess=$(foreach d,$(sort $(dir $(filter-out %.ld,$(1)))), \
 	$(eval $(d)ramstage.o: $(call files-in-dir,$(d),$(filter-out %.ld,$(1))); $$(LD_ramstage) -o $$@ -r $$^ ) \
-	$(eval ramstage-objs:=$(d)ramstage.o $(filter-out $(call files-in-dir,$(d),$(1)),$(filter-out %.ld,$(ramstage-objs)))))
+	$(eval ramstage-objs:=$(d)ramstage.o $(filter-out $(filter-out %.ld, $(call files-in-dir,$(d),$(1))),$(ramstage-objs))))
 
 romstage-generic-ccopts += -D__PRE_RAM__
 ifeq ($(CONFIG_TRACE),y)
@@ -142,9 +142,10 @@
 # Add handler to copy linker scripts
 define generic-objs_ld_template_gen
 de$(EMPTY)fine $(1)-objs_ld_template
-$$(call src-to-obj,$1,$$(1).ld): $$(1).ld
+$$(call src-to-obj,$1,$$(1).ld): $$(1).ld $(obj)/config.h
 	@printf "    CP         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
-	cp $$$$< $$$$@
+	$$(CC_$(1)) $$(CPPFLAGS_$(1)) $($(1)-ld-ccopts) $(PREPROCESS_ONLY) -include $(obj)/config.h $$$$< > $$$$@.tmp
+	mv $$$$@.tmp $$$$@
 en$(EMPTY)def
 endef
 
@@ -259,9 +260,6 @@
 	util/genbuild_h/genbuild_h.sh > $(obj)/build.ht
 	mv $(obj)/build.ht $(obj)/build.h
 
-$(obj)/ldoptions: $(obj)/config.h
-	awk '/^#define ([^"])* ([^"])*$$/ {gsub("\\r","",$$3); print "PROVIDE_HIDDEN(" $$2 " = " $$3 ");";}' $< > $@
-
 build-dirs:
 	mkdir -p $(objcbfs) $(objgenerated)
 
diff --git a/src/arch/arm/Makefile.inc b/src/arch/arm/Makefile.inc
index ffbc8413..cd6c4a4 100644
--- a/src/arch/arm/Makefile.inc
+++ b/src/arch/arm/Makefile.inc
@@ -63,9 +63,11 @@
 bootblock-y += div0.c
 bootblock-y += clock.c
 
-$(objcbfs)/bootblock.debug: $(src)/arch/arm/bootblock.ld $(obj)/ldoptions $$(bootblock-objs) $$(VERSTAGE_LIB)
+bootblock-y += bootblock.ld
+
+$(objcbfs)/bootblock.debug: $(obj)/arch/arm/bootblock.bootblock.ld $$(bootblock-objs) $$(VERSTAGE_LIB)
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(LD_bootblock) --gc-sections -static -o $@ -L$(obj) --start-group $(bootblock-objs) --end-group -T $(src)/arch/arm/bootblock.ld
+	$(LD_bootblock) --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/arm/bootblock.bootblock.ld --start-group $(bootblock-objs) --end-group
 
 endif # CONFIG_ARCH_BOOTBLOCK_ARM
 
@@ -73,9 +75,9 @@
 # verification stage
 ###############################################################################
 
-$(objcbfs)/verstage.debug: $$(verstage-objs) $(src)/arch/arm/verstage.ld $(obj)/ldoptions $$(VB2_LIB)
+$(objcbfs)/verstage.debug: $$(verstage-objs) $(obj)/arch/arm/verstage.verstage.ld $$(VB2_LIB)
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(LD_verstage) --gc-sections -static -o $@ -L$(obj) --start-group $(verstage-objs) --end-group -T $(src)/arch/arm/verstage.ld
+	$(LD_verstage) --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/arm/verstage.verstage.ld --start-group $(verstage-objs) --end-group
 
 verstage-y += div0.c
 verstage-y += eabi_compat.c
@@ -84,6 +86,8 @@
 verstage-y += memmove.S
 verstage-y += stages.c
 
+verstage-y += verstage.ld
+
 ###############################################################################
 # romstage
 ###############################################################################
@@ -103,11 +107,13 @@
 rmodules_arm-y += memmove.S
 rmodules_arm-y += eabi_compat.c
 
+romstage-y += romstage.ld
+
 VBOOT_STUB_DEPS += $(obj)/arch/arm/eabi_compat.rmodules_arm.o
 
-$(objcbfs)/romstage.debug: $$(romstage-objs) $(src)/arch/arm/romstage.ld $(obj)/ldoptions
+$(objcbfs)/romstage.debug: $$(romstage-objs) $(obj)/arch/arm/romstage.romstage.ld
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(LD_romstage) -nostdlib --gc-sections -static -o $@ -L$(obj) --start-group $(romstage-objs) --end-group -T $(src)/arch/arm/romstage.ld
+	$(LD_romstage) -nostdlib --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/arm/romstage.romstage.ld --start-group $(romstage-objs) --end-group
 
 endif # CONFIG_ARCH_ROMSTAGE_ARM
 
@@ -133,9 +139,11 @@
 rmodules_arm-y += eabi_compat.c
 ramstage-srcs += $(wildcard src/mainboard/$(MAINBOARDDIR)/mainboard.c)
 
-$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(src)/arch/arm/ramstage.ld $(obj)/ldoptions
+ramstage-y += ramstage.ld
+
+$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(obj)/arch/arm/ramstage.ramstage.ld
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(LD_ramstage) -nostdlib --gc-sections -o $@ -L$(obj) --start-group $(ramstage-objs) --end-group -T $(src)/arch/arm/ramstage.ld
+	$(LD_ramstage) -nostdlib --gc-sections -o $@ -L$(obj) -T $(obj)/arch/arm/ramstage.ramstage.ld --start-group $(ramstage-objs) --end-group
 
 $(objgenerated)/ramstage.o: $(stages_o) $$(ramstage-objs)
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
diff --git a/src/arch/arm/bootblock.ld b/src/arch/arm/bootblock.ld
index 23d66f1..091e3eb 100644
--- a/src/arch/arm/bootblock.ld
+++ b/src/arch/arm/bootblock.ld
@@ -21,7 +21,6 @@
 /* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
 OUTPUT_ARCH(arm)
-INCLUDE ldoptions
 
 PHDRS
 {
@@ -29,7 +28,6 @@
 }
 
 ENTRY(_start)
-TARGET(binary)
 SECTIONS
 {
 	. = CONFIG_BOOTBLOCK_BASE;
diff --git a/src/arch/arm/ramstage.ld b/src/arch/arm/ramstage.ld
index 5e0607d..7daa8cb 100644
--- a/src/arch/arm/ramstage.ld
+++ b/src/arch/arm/ramstage.ld
@@ -19,9 +19,6 @@
  *  2005.12 yhlu add ramstage cross the vga font buffer handling
  */
 
-/* We use ELF as output format. So that we can debug the code in some form. */
-INCLUDE ldoptions
-
 ENTRY(stage_entry)
 
 PHDRS
diff --git a/src/arch/arm/romstage.ld b/src/arch/arm/romstage.ld
index 873b8e0..d107687 100644
--- a/src/arch/arm/romstage.ld
+++ b/src/arch/arm/romstage.ld
@@ -22,7 +22,6 @@
 /* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
 OUTPUT_ARCH(arm)
-INCLUDE ldoptions
 
 ENTRY(stage_entry)
 
diff --git a/src/arch/arm/verstage.ld b/src/arch/arm/verstage.ld
index f0e88e5..88d4bc8 100644
--- a/src/arch/arm/verstage.ld
+++ b/src/arch/arm/verstage.ld
@@ -10,7 +10,6 @@
 /* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
 OUTPUT_ARCH(arm)
-INCLUDE ldoptions
 
 PHDRS
 {
diff --git a/src/arch/arm64/Makefile.inc b/src/arch/arm64/Makefile.inc
index c1f1112..a9fbde5 100644
--- a/src/arch/arm64/Makefile.inc
+++ b/src/arch/arm64/Makefile.inc
@@ -68,11 +68,13 @@
 bootblock-y += ../../lib/memcpy.c
 bootblock-y += ../../lib/memmove.c
 
+bootblock-y += bootblock.ld
+
 # Build the bootblock
 
-$(objcbfs)/bootblock.debug: $(src)/arch/arm64/bootblock.ld $(obj)/ldoptions $$(bootblock-objs) $(obj)/config.h
+$(objcbfs)/bootblock.debug: $(obj)/arch/arm64/bootblock.bootblock.ld $$(bootblock-objs) $(obj)/config.h
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(LD_bootblock) --gc-sections -static -o $@ -L$(obj) --start-group $(bootblock-objs) --end-group -T $(src)/arch/arm64/bootblock.ld
+	$(LD_bootblock) --gc-sections -static -o $@ -L$(obj) --start-group $(bootblock-objs) --end-group -T $(obj)/arch/arm64/bootblock.bootblock.ld
 
 endif # CONFIG_ARCH_BOOTBLOCK_ARM64
 
@@ -94,6 +96,8 @@
 romstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
 romstage-y += transition.c transition_asm.S
 
+romstage-y += romstage.ld
+
 rmodules_arm64-y += ../../lib/memset.c
 rmodules_arm64-y += ../../lib/memcpy.c
 rmodules_arm64-y += ../../lib/memmove.c
@@ -102,9 +106,9 @@
 # Build the romstage
 VBOOT_STUB_DEPS += $(obj)/arch/arm/eabi_compat.rmodules_arm64.o
 
-$(objcbfs)/romstage.debug: $$(romstage-objs) $(src)/arch/arm64/romstage.ld $(obj)/ldoptions
+$(objcbfs)/romstage.debug: $$(romstage-objs) $(obj)/arch/arm64/romstage.romstage.ld
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(LD_romstage) -nostdlib --gc-sections -static -o $@ -L$(obj) --start-group $(romstage-objs) --end-group -T $(src)/arch/arm64/romstage.ld
+	$(LD_romstage) -nostdlib --gc-sections -static -o $@ -L$(obj) --start-group $(romstage-objs) --end-group -T $(obj)/arch/arm64/romstage.romstage.ld
 
 endif # CONFIG_ARCH_ROMSTAGE_ARM64
 
@@ -140,14 +144,15 @@
 secmon-$(CONFIG_ARCH_USE_SECURE_MONITOR) += ../../lib/memcpy.c
 
 ramstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
+ramstage-y += ramstage.ld
 
 ramstage-srcs += $(wildcard src/mainboard/$(MAINBOARDDIR)/mainboard.c)
 
 # Build the ramstage
 
-$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(src)/arch/arm64/ramstage.ld $(obj)/ldoptions
+$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(obj)/arch/arm64/ramstage.ramstage.ld
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(LD_ramstage) -nostdlib --gc-sections -o $@ -L$(obj) --start-group $(ramstage-objs) --end-group -T $(src)/arch/arm64/ramstage.ld
+	$(LD_ramstage) -nostdlib --gc-sections -o $@ -L$(obj) --start-group $(ramstage-objs) --end-group -T $(obj)/arch/arm64/ramstage.ramstage.ld
 
 $(objgenerated)/ramstage.o: $(stages_o) $$(ramstage-objs)
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
diff --git a/src/arch/arm64/bootblock.ld b/src/arch/arm64/bootblock.ld
index 775111b..98c6454 100644
--- a/src/arch/arm64/bootblock.ld
+++ b/src/arch/arm64/bootblock.ld
@@ -21,7 +21,6 @@
 /* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf64-littleaarch64", "elf64-littleaarch64", "elf64-littleaarch64")
 OUTPUT_ARCH(aarch64)
-INCLUDE ldoptions
 
 PHDRS
 {
diff --git a/src/arch/arm64/ramstage.ld b/src/arch/arm64/ramstage.ld
index d767778..7022e4f 100644
--- a/src/arch/arm64/ramstage.ld
+++ b/src/arch/arm64/ramstage.ld
@@ -20,9 +20,6 @@
  *  2005.12 yhlu add ramstage cross the vga font buffer handling
  */
 
-/* We use ELF as output format. So that we can debug the code in some form. */
-INCLUDE ldoptions
-
 ENTRY(stage_entry)
 
 PHDRS
diff --git a/src/arch/arm64/romstage.ld b/src/arch/arm64/romstage.ld
index c56d5ad..fef2ac5 100644
--- a/src/arch/arm64/romstage.ld
+++ b/src/arch/arm64/romstage.ld
@@ -22,7 +22,6 @@
 /* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf64-littleaarch64", "elf64-littleaarch64", "elf64-littleaarch64")
 OUTPUT_ARCH(aarch64)
-INCLUDE ldoptions
 
 PHDRS
 {
diff --git a/src/arch/mips/Makefile.inc b/src/arch/mips/Makefile.inc
index 159d0e0..969a03d 100644
--- a/src/arch/mips/Makefile.inc
+++ b/src/arch/mips/Makefile.inc
@@ -51,9 +51,9 @@
 # prevent that.
 bootblock-S-ccopts += -undef
 
-$(objcbfs)/bootblock.debug:  $(src)/arch/mips/bootblock.ld $$(bootblock-objs) $(obj)/config.h $(obj)/ldoptions
+$(objcbfs)/bootblock.debug:  $(obj)/arch/mips/bootblock.bootblock.ld $$(bootblock-objs) $(obj)/config.h
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(LD_bootblock) --gc-sections -static -o $@ -L$(obj) -T $(src)/arch/mips/bootblock.ld --start-group $(bootblock-objs) --end-group
+	$(LD_bootblock) --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/mips/bootblock.bootblock.ld --start-group $(bootblock-objs) --end-group
 
 endif # CONFIG_ARCH_BOOTBLOCK_MIPS
 
@@ -71,9 +71,11 @@
 romstage-y += ../../lib/memmove.c
 romstage-y += ../../lib/memset.c
 
-$(objcbfs)/romstage.debug: $$(romstage-objs) $(src)/arch/mips/romstage.ld $(obj)/ldoptions
+romstage-y += romstage.ld
+
+$(objcbfs)/romstage.debug: $$(romstage-objs) $(obj)/arch/mips/romstage.romstage.ld
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(LD_romstage) --gc-sections -static -o $@ -L$(obj) -T $(src)/arch/mips/romstage.ld --start-group $(romstage-objs) --end-group
+	$(LD_romstage) --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/mips/romstage.romstage.ld --start-group $(romstage-objs) --end-group
 
 endif # CONFIG_ARCH_ROMSTAGE_MIPS
 
@@ -93,8 +95,10 @@
 ramstage-y += ../../lib/memset.c
 ramstage-srcs += $(wildcard src/mainboard/$(MAINBOARDDIR)/mainboard.c)
 
-$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(src)/arch/mips/ramstage.ld $(obj)/ldoptions
+ramstage-y += ramstage.ld
+
+$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(obj)/arch/mips/ramstage.ramstage.ld
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(LD_ramstage) --gc-sections -static -o $@ -L$(obj) -T $(src)/arch/mips/ramstage.ld --start-group $(ramstage-objs) --end-group
+	$(LD_ramstage) --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/mips/ramstage.ramstage.ld --start-group $(ramstage-objs) --end-group
 
 endif # CONFIG_ARCH_RAMSTAGE_MIPS
diff --git a/src/arch/mips/bootblock.ld b/src/arch/mips/bootblock.ld
index 8e1b1e3..3721f85 100644
--- a/src/arch/mips/bootblock.ld
+++ b/src/arch/mips/bootblock.ld
@@ -20,7 +20,6 @@
  */
 
 OUTPUT_ARCH(mips)
-INCLUDE ldoptions
 
 ENTRY(_start)
 
diff --git a/src/arch/mips/ramstage.ld b/src/arch/mips/ramstage.ld
index cba90eb..4052444 100644
--- a/src/arch/mips/ramstage.ld
+++ b/src/arch/mips/ramstage.ld
@@ -24,7 +24,6 @@
  */
 
 OUTPUT_ARCH(mips)
-INCLUDE ldoptions
 
 ENTRY(stage_entry)
 
diff --git a/src/arch/mips/romstage.ld b/src/arch/mips/romstage.ld
index f776bc8..8964285 100644
--- a/src/arch/mips/romstage.ld
+++ b/src/arch/mips/romstage.ld
@@ -20,7 +20,6 @@
  */
 
 OUTPUT_ARCH(mips)
-INCLUDE ldoptions
 
 ENTRY(stage_entry)
 
diff --git a/src/arch/riscv/Makefile.inc b/src/arch/riscv/Makefile.inc
index 599c7af..643facf 100644
--- a/src/arch/riscv/Makefile.inc
+++ b/src/arch/riscv/Makefile.inc
@@ -28,8 +28,6 @@
 ################################################################################
 ifeq ($(CONFIG_ARCH_BOOTBLOCK_RISCV),y)
 
-bootblock-y += bootblock.ld
-
 bootblock-y = bootblock.S stages.c
 bootblock-y += boot.c
 bootblock-y += rom_media.c
@@ -40,11 +38,13 @@
 	$(top)/src/lib/memmove.c \
 	$(top)/src/lib/memset.c
 
-$(objcbfs)/bootblock.debug: $(src)/arch/riscv/bootblock.ld $(obj)/ldoptions $$(bootblock-objs)
+bootblock-y += bootblock.ld
+
+$(objcbfs)/bootblock.debug: $(obj)/arch/riscv/bootblock.bootblock.ld $$(bootblock-objs)
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(CC_bootblock) $(CFLAGS_bootblock) -nostartfiles -Wl,--gc-sections -static -o $@ -L$(obj) \
-		-T $(src)/arch/riscv/bootblock.ld -Wl,--start-group $(bootblock-objs) \
-		$(LIBGCC_FILE_NAME_bootblock) -Wl,--end-group
+	$(LD_bootblock) --gc-sections -static -o $@ -L$(obj) \
+		-T $(obj)/arch/riscv/bootblock.bootblock.ld --start-group $(bootblock-objs) \
+		$(LIBGCC_FILE_NAME_bootblock) --end-group
 
 endif
 
@@ -65,11 +65,13 @@
 
 romstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
 
+romstage-y += romstage.ld
+
 # Build the romstage
 
-$(objcbfs)/romstage.debug: $$(romstage-objs) $(src)/arch/riscv/romstage.ld $(obj)/ldoptions
+$(objcbfs)/romstage.debug: $$(romstage-objs) $(obj)/arch/riscv/romstage.romstage.ld
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
-	$(CC_romstage) $(CFLAGS_romstage) -nostdlib -Wl,--gc-sections -static -o $@ -L$(obj) -T $(src)/arch/riscv/romstage.ld -Wl,--start-group $(romstage-objs) -Wl,--end-group
+	$(LD_romstage) --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/riscv/romstage.romstage.ld --start-group $(romstage-objs) --end-group
 
 romstage-c-ccopts += $(riscv_flags)
 romstage-S-ccopts += $(riscv_asm_flags)
@@ -101,13 +103,15 @@
 
 ramstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
 
+ramstage-y += ramstage.ld
+
 ramstage-srcs += src/mainboard/$(MAINBOARDDIR)/mainboard.c
 
 # Build the ramstage
 
-$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(src)/arch/riscv/ramstage.ld $(obj)/ldoptions
+$(objcbfs)/ramstage.debug: $$(ramstage-objs) $(obj)/arch/riscv/ramstage.ramstage.ld
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(CC_ramstage) $(CFLAGS_ramstage) -nostdlib -Wl,--gc-sections -static -o $@ -L$(obj) -Wl,--start-group $(ramstage-objs) -Wl,--end-group -T $(src)/arch/riscv/ramstage.ld
+	$(LD_ramstage) --gc-sections -static -o $@ -L$(obj) -T $(obj)/arch/riscv/ramstage.ramstage.ld --start-group $(ramstage-objs) --end-group
 
 ramstage-c-ccopts += $(riscv_flags)
 ramstage-S-ccopts += $(riscv_asm_flags)
diff --git a/src/arch/riscv/bootblock.ld b/src/arch/riscv/bootblock.ld
index 9f5f53b..e5cb851 100644
--- a/src/arch/riscv/bootblock.ld
+++ b/src/arch/riscv/bootblock.ld
@@ -21,7 +21,6 @@
 /* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf64-littleriscv", "elf64-littleriscv", "elf64-littleriscv")
 OUTPUT_ARCH(riscv)
-INCLUDE ldoptions
 
 PHDRS
 {
@@ -29,7 +28,6 @@
 }
 
 ENTRY(_start)
-TARGET(binary)
 SECTIONS
 {
 	. = CONFIG_BOOTBLOCK_BASE;
diff --git a/src/arch/riscv/ramstage.ld b/src/arch/riscv/ramstage.ld
index 2e97a7e..21c9638 100644
--- a/src/arch/riscv/ramstage.ld
+++ b/src/arch/riscv/ramstage.ld
@@ -20,9 +20,6 @@
  *  2005.12 yhlu add ramstage cross the vga font buffer handling
  */
 
-/* We use ELF as output format. So that we can debug the code in some form. */
-INCLUDE ldoptions
-
 ENTRY(stage_entry)
 
 PHDRS
diff --git a/src/arch/riscv/romstage.ld b/src/arch/riscv/romstage.ld
index 4cb0050..e49d1c1 100644
--- a/src/arch/riscv/romstage.ld
+++ b/src/arch/riscv/romstage.ld
@@ -20,14 +20,8 @@
  */
 
 /* We use ELF as output format. So that we can debug the code in some form. */
-/*
- INCLUDE ldoptions
- */
-
-/* We use ELF as output format. So that we can debug the code in some form. */
 OUTPUT_FORMAT("elf64-littleriscv", "elf64-littleriscv", "elf64-littleriscv")
 OUTPUT_ARCH(riscv)
-INCLUDE ldoptions
 
 PHDRS
 {
diff --git a/src/arch/x86/Makefile.inc b/src/arch/x86/Makefile.inc
index e4af06d..065b7778 100644
--- a/src/arch/x86/Makefile.inc
+++ b/src/arch/x86/Makefile.inc
@@ -103,9 +103,9 @@
 bootblock_romccflags := -mcpu=k7 -msse -O2 -D__PRE_RAM__ -D__BOOTBLOCK__
 endif
 
-$(objgenerated)/bootblock.ld: $(obj)/ldoptions $$(filter %.ld,$$(bootblock-srcs))
+$(objgenerated)/bootblock.ld: $(obj)/config.h $$(filter %.ld,$$(bootblock-srcs))
 	@printf "    GEN        $(subst $(obj)/,,$(@))\n"
-	printf '$(foreach ldscript,$(^),INCLUDE "$(ldscript)"\n)' > $@
+	printf '$(foreach ldscript,$(^),#include "$(ldscript)"\n)' | $(CC_bootblock) $(PREPROCESS_ONLY) - > $@
 
 $(objgenerated)/bootblock_inc.S: $$(bootblock_inc)
 	@printf "    GEN        $(subst $(obj)/,,$(@))\n"
@@ -212,18 +212,18 @@
 	@printf "    LINK       $(subst $(obj)/,,$(@))\n"
 	$(LD_romstage) --gc-sections -nostdlib -nostartfiles -static -o $@ -L$(obj) $(COMPILER_RT_FLAGS_romstage) --start-group $(filter-out %.ld,$(romstage-objs)) $(romstage-libs) $(COMPILER_RT_romstage) --end-group -T $(objgenerated)/romstage.ld
 
-$(objgenerated)/romstage_null.ld: $(obj)/ldoptions $$(filter %.ld,$$(romstage-srcs))
+$(objgenerated)/romstage_null.ld: $(obj)/config.h $$(filter %.ld,$$(romstage-srcs))
 	@printf "    GEN        $(subst $(obj)/,,$(@))\n"
 	rm -f $@
 	printf "ROMSTAGE_BASE = 0x0;\n" > $@.tmp
-	printf '$(foreach ldscript,$(^),INCLUDE "$(ldscript)"\n)' >> $@.tmp
-	mv $@.tmp $@
+	printf '$(foreach ldscript,$(^),#include "$(ldscript)"\n)' >> $@.tmp
+	$(CC_romstage) $(PREPROCESS_ONLY) $@.tmp > $@
 
 $(objgenerated)/romstage.ld: $(objgenerated)/romstage_null.ld $(objcbfs)/base_xip.txt
 	@printf "    GEN        $(subst $(obj)/,,$(@))\n"
 	rm -f $@
 	sed -e 's/^/ROMSTAGE_BASE = /g' -e 's/$$/;/g' $(objcbfs)/base_xip.txt > $@.tmp
-	sed -e '/ROMSTAGE_BASE/d' $(objgenerated)/romstage_null.ld >> $@.tmp
+	sed -e '/^ROMSTAGE_BASE/d' $(objgenerated)/romstage_null.ld >> $@.tmp
 	mv $@.tmp $@
 
 $(objcbfs)/base_xip.txt: $(obj)/coreboot.pre1 $(objcbfs)/romstage_null.bin
@@ -299,9 +299,11 @@
 
 else
 
-$(objcbfs)/ramstage.debug: $(objgenerated)/ramstage.o $(src)/arch/x86/ramstage.ld
+ramstage-srcs += $(src)/arch/x86/ramstage.ld
+
+$(objcbfs)/ramstage.debug: $(objgenerated)/ramstage.o $(obj)/arch/x86/ramstage.ramstage.ld
 	@printf "    CC         $(subst $(obj)/,,$(@))\n"
-	$(LD_ramstage) --gc-sections -o $@ -L$(obj) $< -T $(src)/arch/x86/ramstage.ld
+	$(LD_ramstage) --gc-sections -o $@ -L$(obj) $< -T $(obj)/arch/x86/ramstage.ramstage.ld
 
 endif
 
diff --git a/src/arch/x86/init/romstage.ld b/src/arch/x86/init/romstage.ld
index 95b9571..40ed354 100644
--- a/src/arch/x86/init/romstage.ld
+++ b/src/arch/x86/init/romstage.ld
@@ -75,8 +75,8 @@
 
 	. = 0xffffff00;
 	.illegal_globals . : {
-		*(EXCLUDE_FILE (*/libagesa.*.a: */buildOpts.romstage.o */agesawrapper.romstage.o */vendorcode/amd/agesa/* */vendorcode/amd/cimx/*) .data)
-		*(EXCLUDE_FILE (*/libagesa.*.a: */buildOpts.romstage.o */agesawrapper.romstage.o */vendorcode/amd/agesa/* */vendorcode/amd/cimx/*) .data.*)
+		*(EXCLUDE_FILE ("*/libagesa.*.a:" "*/buildOpts.romstage.o" "*/agesawrapper.romstage.o" "*/vendorcode/amd/agesa/*" "*/vendorcode/amd/cimx/*") .data)
+		*(EXCLUDE_FILE ("*/libagesa.*.a:" "*/buildOpts.romstage.o" "*/agesawrapper.romstage.o" "*/vendorcode/amd/agesa/*" "*/vendorcode/amd/cimx/*") .data.*)
 		*(.bss)
 		*(.bss.*)
 		*(.sbss)
diff --git a/src/arch/x86/ramstage.ld b/src/arch/x86/ramstage.ld
index 9c43bd7..f765cbe 100644
--- a/src/arch/x86/ramstage.ld
+++ b/src/arch/x86/ramstage.ld
@@ -19,9 +19,6 @@
  *  2005.12 yhlu add ramstage cross the vga font buffer handling
  */
 
-/* We use ELF as output format. So that we can debug the code in some form. */
-INCLUDE ldoptions
-
 ENTRY(_start)
 
 SECTIONS
diff --git a/src/cpu/x86/smm/Makefile.inc b/src/cpu/x86/smm/Makefile.inc
index 3e1599f..517f00b 100644
--- a/src/cpu/x86/smm/Makefile.inc
+++ b/src/cpu/x86/smm/Makefile.inc
@@ -34,7 +34,7 @@
 $(obj)/cpu/x86/smm/smm.o: $$(smm-objs) $(COMPILER_RT_smm)
 	$(LD_smm) -nostdlib -r -o $@ $(COMPILER_RT_FLAGS_smm) --start-group $(smm-objs) $(COMPILER_RT_smm) --end-group
 
-$(obj)/cpu/x86/smm/smm_wrap: $(obj)/cpu/x86/smm/smm.o $(src)/cpu/x86/smm/$(SMM_LDSCRIPT) $(obj)/ldoptions
+$(obj)/cpu/x86/smm/smm_wrap: $(obj)/cpu/x86/smm/smm.o $(src)/cpu/x86/smm/$(SMM_LDSCRIPT)
 	$(LD_smm) $(SMM_LDFLAGS) -nostdlib -nostartfiles -static -o $(obj)/cpu/x86/smm/smm.elf -T $(src)/cpu/x86/smm/$(SMM_LDSCRIPT) $(obj)/cpu/x86/smm/smm.o
 	$(NM_smm) -n $(obj)/cpu/x86/smm/smm.elf | sort > $(obj)/cpu/x86/smm/smm.map
 	$(OBJCOPY_smm) -O binary $(obj)/cpu/x86/smm/smm.elf $(obj)/cpu/x86/smm/smm
diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc
index 5f149c9..7d02934 100644
--- a/src/lib/Makefile.inc
+++ b/src/lib/Makefile.inc
@@ -137,7 +137,7 @@
 # It will create the necessary Make rules to create a rmodule. The resulting
 # rmdoule is named $(1).rmod
 define rmodule_link
-$(strip $(1)): $(strip $(2)) $$(RMODULE_LDSCRIPT) $$(obj)/ldoptions $$(RMODTOOL)
+$(strip $(1)): $(strip $(2)) $$(RMODULE_LDSCRIPT) $$(RMODTOOL)
 	$$(LD_rmodules_$(4)) $$(RMODULE_LDFLAGS) --defsym=__heap_size=$(strip $(3)) -o $$@ --start-group $(strip $(2)) $$(COMPILER_RT_rmodules_$(4)) --end-group
 	$$(NM_rmodules_$(4)) -n $$@ > $$(basename $$@).map