Index: arch/amd64/Makefile.inc
===================================================================
--- arch/amd64/Makefile.inc	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ arch/amd64/Makefile.inc	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -23,5 +23,8 @@
 CPPFLAGS=$(DEFS) -nostdinc -I../include
 CFLAGS=$(CPPFLAGS) -nostdlib -fno-builtin -fno-unwind-tables -Werror-implicit-function-declaration -Wmissing-prototypes -Werror -O3 -march=opteron -m64 -mcmodel=kernel -mno-red-zone
-LFLAGS=-M -T ../arch/amd64/_link.ld
+LFLAGS=-M 
+
+../arch/amd64/_link.ld: ../arch/amd64/_link.ld.in
+	$(CC) $(CFLAGS) -E -x c $< | grep -v "^\#" > $@
 
 arch_sources = arch/dummy.s \
Index: ch/amd64/_link.ld
===================================================================
--- arch/amd64/_link.ld	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ 	(revision )
@@ -1,68 +1,0 @@
-/** AMD64 linker script
- *  
- * umapped section:
- * 	kernel text
- * 	kernel data
- * mapped section:
- *	kernel text
- *	kernel data 
- */
-
-OUTPUT_FORMAT(binary)
-ENTRY(kernel_image_start)
-
-SECTIONS {
-	.unmapped 0x8000: AT (0x8000) {
-		unmapped_ktext_start = .;
-		*(K_TEXT_START);
-		*(K_TEXT_START_2);
-		unmapped_ktext_end = .;
-
-		unmapped_kdata_start = .;
-		*(K_DATA_START);
-		unmapped_kdata_end = .;
-	}
-
-	.mapped (0xffffffff80100000+0x8000+SIZEOF(.unmapped)) : AT (0x8000+SIZEOF(.unmapped)) {
-		ktext_start = .;
-		*(BOOT_DATA);
-		*(.text);
-		ktext_end = .;
-
-		kdata_start = .;
-		*(.data);		/* initialized data */
-		*(.rodata*);		/* string literals */
-		hardcoded_load_address = .;
-		QUAD(ktext_start - SIZEOF(.unmapped));
-		hardcoded_ktext_size = .;
-		QUAD(ktext_end - ktext_start + (unmapped_ktext_end - unmapped_ktext_start));
-		hardcoded_kdata_size = .;
-		QUAD(kdata_end - kdata_start + (unmapped_kdata_end - unmapped_kdata_start));
-		hardcoded_unmapped_ktext_size = .;
-		LONG(unmapped_ktext_end - unmapped_ktext_start);
-		hardcoded_unmapped_kdata_size = .;
-		LONG(unmapped_kdata_end - unmapped_kdata_start);
-		*(COMMON);		/* global variables */
-
-
-
-		*(.eh_frame);
-		*(.bss);		/* uninitialized static variables */
-		*(K_DATA_END);
-
-		symbol_table = .;
-		*(symtab.*);            /* Symbol table, must be LAST symbol!*/
-
-		kdata_end = .;
-	}
-
-	_hardcoded_kernel_size = (ktext_end - ktext_start) + (unmapped_ktext_end - unmapped_ktext_start) + (kdata_end - kdata_start) + (unmapped_kdata_end - unmapped_kdata_start);
-	_boot_offset = 0x100000;
-	_ka2pa_offset = 0xffffffff80000000;
-	_map_address = _ka2pa_offset + _boot_offset;
-
-
-	e820table_boot = e820table - _map_address;
-	e820counter_boot = e820counter - _map_address;
-	real_bootstrap_gdtr = real_bootstrap_gdtr_boot + _ka2pa_offset;
-}
Index: arch/amd64/_link.ld.in
===================================================================
--- arch/amd64/_link.ld.in	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
+++ arch/amd64/_link.ld.in	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -0,0 +1,72 @@
+/** AMD64 linker script
+ *  
+ * umapped section:
+ * 	kernel text
+ * 	kernel data
+ * mapped section:
+ *	kernel text
+ *	kernel data 
+ */
+
+#define __ASM__
+#include <arch/boot/boot.h>
+#include <arch/mm/page.h>
+
+OUTPUT_FORMAT(binary)
+ENTRY(kernel_image_start)
+
+SECTIONS {
+	.unmapped BOOTSTRAP_OFFSET: AT (BOOTSTRAP_OFFSET) {
+		unmapped_ktext_start = .;
+		*(K_TEXT_START);
+		*(K_TEXT_START_2);
+		unmapped_ktext_end = .;
+
+		unmapped_kdata_start = .;
+		*(K_DATA_START);
+		unmapped_kdata_end = .;
+	}
+
+	.mapped (PA2KA(BOOT_OFFSET+BOOTSTRAP_OFFSET)+SIZEOF(.unmapped)) : AT (BOOTSTRAP_OFFSET+SIZEOF(.unmapped)) {
+		ktext_start = .;
+		*(BOOT_DATA);
+		*(.text);
+		ktext_end = .;
+
+		kdata_start = .;
+		*(.data);		/* initialized data */
+		*(.rodata*);		/* string literals */
+		hardcoded_load_address = .;
+		QUAD(ktext_start - SIZEOF(.unmapped));
+		hardcoded_ktext_size = .;
+		QUAD(ktext_end - ktext_start + (unmapped_ktext_end - unmapped_ktext_start));
+		hardcoded_kdata_size = .;
+		QUAD(kdata_end - kdata_start + (unmapped_kdata_end - unmapped_kdata_start));
+		hardcoded_unmapped_ktext_size = .;
+		LONG(unmapped_ktext_end - unmapped_ktext_start);
+		hardcoded_unmapped_kdata_size = .;
+		LONG(unmapped_kdata_end - unmapped_kdata_start);
+		*(COMMON);		/* global variables */
+
+
+
+		*(.eh_frame);
+		*(.bss);		/* uninitialized static variables */
+		*(K_DATA_END);
+
+		symbol_table = .;
+		*(symtab.*);            /* Symbol table, must be LAST symbol!*/
+
+		kdata_end = .;
+	}
+
+	_hardcoded_kernel_size = (ktext_end - ktext_start) + (unmapped_ktext_end - unmapped_ktext_start) + (kdata_end - kdata_start) + (unmapped_kdata_end - unmapped_kdata_start);
+	_boot_offset = 0x100000;
+	_ka2pa_offset = 0xffffffff80000000;
+	_map_address = _ka2pa_offset + _boot_offset;
+
+
+	e820table_boot = KA2PA(e820table) - BOOT_OFFSET;
+	e820counter_boot = KA2PA(e820counter) - BOOT_OFFSET;
+	real_bootstrap_gdtr = PA2KA(real_bootstrap_gdtr_boot);
+}
Index: arch/amd64/include/mm/page.h
===================================================================
--- arch/amd64/include/mm/page.h	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ arch/amd64/include/mm/page.h	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -39,9 +39,9 @@
 
 #ifndef __ASM__
-# define KA2PA(x)      (((__address) (x)) + 0x80000000)
-# define PA2KA(x)      (((__address) (x)) - 0x80000000)
+# define KA2PA(x)      (((__address) (x)) - 0xffffffff80000000)
+# define PA2KA(x)      (((__address) (x)) + 0xffffffff80000000)
 #else
-# define KA2PA(x)      ((x) + 0x80000000)
-# define PA2KA(x)      ((x) - 0x80000000)
+# define KA2PA(x)      ((x) - 0xffffffff80000000)
+# define PA2KA(x)      ((x) + 0xffffffff80000000)
 #endif
 
Index: arch/ia32/Makefile.inc
===================================================================
--- arch/ia32/Makefile.inc	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ arch/ia32/Makefile.inc	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -19,5 +19,5 @@
 CPPFLAGS=$(DEFS) -nostdinc -I../include
 CFLAGS=$(CPPFLAGS) -nostdlib -fno-builtin -fomit-frame-pointer -Werror-implicit-function-declaration -Wmissing-prototypes -Werror -O3
-LFLAGS=-M -no-check-sections -T ../arch/ia32/_link.ld
+LFLAGS=-M -no-check-sections
 
 arch_sources= \
Index: arch/ia64/Makefile.inc
===================================================================
--- arch/ia64/Makefile.inc	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ arch/ia64/Makefile.inc	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -17,5 +17,5 @@
 CPPFLAGS=$(DEFS) -nostdinc -I../include
 CFLAGS=$(CPPFLAGS) -mconstant-gp -nostdlib -fno-builtin -fno-unwind-tables -Werror-implicit-function-declaration -Wmissing-prototypes -Werror -O3
-LFLAGS=-EL -M -T ../arch/ia64/_link.ld
+LFLAGS=-EL -M
 
 
Index: arch/mips/Makefile.inc
===================================================================
--- arch/mips/Makefile.inc	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ arch/mips/Makefile.inc	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -16,5 +16,5 @@
 CPPFLAGS=$(DEFS) -mno-abicalls -nostdinc -I../include
 CFLAGS=$(CPPFLAGS) -mips2 -G 0 -nostdlib -fno-builtin -Werror-implicit-function-declaration -Wmissing-prototypes -Werror -O2 -msoft-float
-LFLAGS=-mips2 -M -no-check-sections -T ../arch/mips/_link.ld
+LFLAGS=-mips2 -M -no-check-sections
 
 arch_sources= \
Index: arch/ppc/Makefile.inc
===================================================================
--- arch/ppc/Makefile.inc	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ arch/ppc/Makefile.inc	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -16,5 +16,5 @@
 CPPFLAGS=$(DEFS) -nostdinc -I../include
 CFLAGS=$(CPPFLAGS) -nostdlib -fno-builtin -Werror-implicit-function-declaration -Wmissing-prototypes -Werror -O2
-LFLAGS=-M -no-check-sections -N -T ../arch/ppc/_link.ld
+LFLAGS=-M -no-check-sections -N
 
 arch_sources= \
Index: src/Makefile
===================================================================
--- src/Makefile	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ src/Makefile	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -74,10 +74,10 @@
 	-$(MAKE) clean
 
-kernel.bin: $(arch_objects) $(objects) $(test_objects)
+kernel.bin: $(arch_objects) $(objects) $(test_objects) ../arch/$(ARCH)/_link.ld
 	$(OBJCOPY) -I binary -O $(BFD_NAME) -B $(BFD_ARCH) --prefix-sections=symtab Makefile debug/empty_map.o
-	$(LD) $(LFLAGS) $(arch_objects) $(objects) $(test_objects) debug/empty_map.o -o $@ -Map kernel.map.pre
+	$(LD) -T ../arch/$(ARCH)/_link.ld $(LFLAGS) $(arch_objects) $(objects) $(test_objects) debug/empty_map.o -o $@ -Map kernel.map.pre
 	../tools/genmap.py kernel.map.pre debug/real_map.bin
 	$(OBJCOPY) -I binary -O $(BFD_NAME) -B $(BFD_ARCH) --prefix-sections=symtab debug/real_map.bin debug/real_map.o
-	$(LD) $(LFLAGS) $(arch_objects) $(objects) $(test_objects) debug/real_map.o -o $@ -Map kernel.map	
+	$(LD) -T ../arch/$(ARCH)/_link.ld $(LFLAGS) $(arch_objects) $(objects) $(test_objects) debug/real_map.o -o $@ -Map kernel.map
 
 %.s: %.S
Index: src/clean.amd64
===================================================================
--- src/clean.amd64	(revision ee7e6aceffda0d9cc6f708f24c23e4be36546df3)
+++ src/clean.amd64	(revision 4f1475d4bdd833a419cd7695bc6ada543af57191)
@@ -6,3 +6,4 @@
 find ../arch/amd64 -type l | xargs rm
 rm ../arch/amd64/src/context_offset.h
+rm ../arch/amd64/_link.ld
 rm ../tools/amd64/gencontext
