Fake layout with code rather than the linker.

This begins to make build / environment dependencies controllable in
code since tests will need to control these at some point.

Change-Id: Ifc82eb227a7206da37e5208647003c9865330186
diff --git a/src/BUILD.gn b/src/BUILD.gn
index 4a730b4..0b4ea31 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -19,17 +19,10 @@
     "load.c",
     "main.c",
   ]
-
+  sources += [ "layout.c" ]
   deps = [
-    ":common",
-    ":fdt",
-    ":memiter",
     ":src_testable",
   ]
-
-  if (is_debug) {
-    deps += [ ":dlog" ]
-  }
 }
 
 # One day, this will contain all the hypervisor's source but only once it can
@@ -106,26 +99,11 @@
     "fdt_test.cc",
     "mm_test.cc",
   ]
+  sources += [ "layout_fake.c" ]
   cflags_cc = [
     "-Wno-c99-extensions",
     "-Wno-nested-anon-types",
   ]
-  ldflags = [
-    "-Xlinker",
-    "-defsym=text_begin=0",
-    "-Xlinker",
-    "-defsym=text_end=100",
-    "-Xlinker",
-    "-defsym=rodata_begin=200",
-    "-Xlinker",
-    "-defsym=rodata_end=300",
-    "-Xlinker",
-    "-defsym=data_begin=400",
-    "-Xlinker",
-    "-defsym=data_end=500",
-    "-Xlinker",
-    "-defsym=bin_end=600",
-  ]
   deps = [
     ":src_testable",
     "//third_party:gtest_main",
diff --git a/src/layout.c b/src/layout.c
new file mode 100644
index 0000000..68b645e
--- /dev/null
+++ b/src/layout.c
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2018 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "hf/layout.h"
+
+/**
+ * Get the address the .text section begins at.
+ */
+paddr_t layout_text_begin(void)
+{
+	extern uint8_t text_begin[];
+	return pa_init((uintpaddr_t)text_begin);
+}
+
+/**
+ * Get the address the .text section ends at.
+ */
+paddr_t layout_text_end(void)
+{
+	extern uint8_t text_end[];
+	return pa_init((uintpaddr_t)text_end);
+}
+
+/**
+ * Get the address the .rodata section begins at.
+ */
+paddr_t layout_rodata_begin(void)
+{
+	extern uint8_t rodata_begin[];
+	return pa_init((uintpaddr_t)rodata_begin);
+}
+
+/**
+ * Get the address the .rodata section ends at.
+ */
+paddr_t layout_rodata_end(void)
+{
+	extern uint8_t rodata_end[];
+	return pa_init((uintpaddr_t)rodata_end);
+}
+
+/**
+ * Get the address the .data section begins at.
+ */
+paddr_t layout_data_begin(void)
+{
+	extern uint8_t data_begin[];
+	return pa_init((uintpaddr_t)data_begin);
+}
+
+/**
+ * Get the address the .data section ends at.
+ */
+paddr_t layout_data_end(void)
+{
+	extern uint8_t data_end[];
+	return pa_init((uintpaddr_t)data_end);
+}
+
+/**
+ * Get the address the loaded image ends at.
+ */
+paddr_t layout_bin_end(void)
+{
+	extern uint8_t bin_end[];
+	return pa_init((uintpaddr_t)bin_end);
+}
+
+/**
+ * Get the address to load the primary VM at.
+ *
+ * This is placed just after the image.
+ */
+paddr_t layout_primary_begin(void)
+{
+	/* TODO: This is a hack. We must read the alignment from the binary. */
+	paddr_t bin_end = layout_bin_end();
+	return pa_init((pa_addr(bin_end) + 0x80000 - 1) & ~(0x80000 - 1));
+}
diff --git a/src/layout_fake.c b/src/layout_fake.c
new file mode 100644
index 0000000..bfa04ca
--- /dev/null
+++ b/src/layout_fake.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2018 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "hf/layout.h"
+
+paddr_t layout_text_begin(void)
+{
+	return pa_init(0);
+}
+
+paddr_t layout_text_end(void)
+{
+	return pa_init(100);
+}
+
+paddr_t layout_rodata_begin(void)
+{
+	return pa_init(200);
+}
+
+paddr_t layout_rodata_end(void)
+{
+	return pa_init(300);
+}
+
+paddr_t layout_data_begin(void)
+{
+	return pa_init(400);
+}
+
+paddr_t layout_data_end(void)
+{
+	return pa_init(500);
+}
+
+paddr_t layout_bin_end(void)
+{
+	return pa_init(600);
+}
+
+paddr_t layout_primary_begin(void)
+{
+	return pa_init(0x80000);
+}