From 9db3f7a311fa2a6e5347157dd70f032745292bbd Mon Sep 17 00:00:00 2001
From: John Hodge <tpg@mutabah.net>
Date: Sun, 17 Mar 2019 09:25:40 +0800
Subject: [PATCH] Usermode - Semi-working xargo build (using custom xargo)

---
 Usermode/Makefile                | 13 ++++++++-----
 Usermode/Xargo.toml              |  9 +++++++++
 Usermode/libasync/Cargo.toml     |  2 +-
 Usermode/libbyteorder/Cargo.toml |  2 +-
 Usermode/libvec_ring/Cargo.toml  |  3 +--
 Usermode/libwtk/Cargo.toml       |  2 +-
 Usermode/link-amd64.ld           |  3 ++-
 Usermode/loader/bin/Cargo.toml   |  8 +++++---
 Usermode/loader/bin/src/main.rs  |  2 +-
 Usermode/loader/bin/start.S      |  2 ++
 Usermode/rustrt0/amd64.S         |  2 +-
 11 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/Usermode/Makefile b/Usermode/Makefile
index a2f0eb6d..ed494fd2 100644
--- a/Usermode/Makefile
+++ b/Usermode/Makefile
@@ -90,10 +90,10 @@ LOADER_DEPS := $(patsubst %,$(LIBDIR)lib%.rlib,$(call fn_getdeps,loader/bin/src/
 # loader: Requires an assembly stub
 # > Linking is done in two steps so that --gc-sections works
 #$(BINDIR)loader: loader/bin/link.ld $(OBJDIR)loader/bin/start.ao $(OBJDIR)loader/bin/src/main.o
-$(BINDIR)loader: loader/bin/link.ld $(OBJDIR)loader/bin/start.ao $(OBJDIR)loader/bin/src/main.o $(LOADER_DEPS)
+$(BINDIR)loader: loader/bin/link.ld $(OBJDIR)loader/bin/start.ao $(OBJDIR)loader/bin/src/main.o $(OBJDIR)rustrt0.o $(LOADER_DEPS)
 	@mkdir -p $(dir $@)
 	@echo [LD] $@
-	$V$(LD) -r --gc-sections --undefined image_header --entry start -o $(OBJDIR)loader.o $(OBJDIR)loader/bin/start.ao $(OBJDIR)loader/bin/src/main.o $(LOADER_DEPS)
+	$V$(LD) -r --gc-sections --undefined image_header --entry start -o $(OBJDIR)loader.o $(OBJDIR)rustrt0.o $(OBJDIR)loader/bin/start.ao $(OBJDIR)loader/bin/src/main.o $(LOADER_DEPS)
 	$V$(LD) -o $(OBJDIR)loader.elf -T loader/bin/link.ld $(OBJDIR)loader.o -Map $(OBJDIR)loader_map.txt
 	$V$(OBJCOPY) $(OBJDIR)loader.elf -O binary $@
 	$V$(OBJDUMP) -CS $(OBJDIR)loader.elf > $(OBJDIR)loader.dsm
@@ -102,12 +102,15 @@ $(OBJDIR)loader/bin/src/main.o: $(LOADER_DEPS)
 #	$(XARGO) build $(XARGO_FLAGS) --manifest-path loader/Cargo.toml
 
 .PHONY: xargo
-xargo: $(LIBDIR)libloader_dyn.so $(OBJDIR)loader/bin/start.ao	# Referenced by link-$(TARGET).ld
-	$(XARGO) build $(XARGO_FLAGS) --manifest-path libstd/Cargo.toml
+xargo: $(OBJDIR)rustrt0.o $(LIBDIR)libloader_dyn.so $(LIBDIR)libloader_start.a	# Referenced by link-$(TARGET).ld
+	#$(XARGO) build $(XARGO_FLAGS) --manifest-path libstd/Cargo.toml
 	$(XARGO) build $(XARGO_FLAGS) --manifest-path loader/bin/Cargo.toml
-	cp target/$(XARGO_TARGET)/$(XARGO_PROFILE)/libstd.rlib ../.prefix/xargo/lib/rustlib/$(XARGO_TARGET)/lib/
+	#cp target/$(XARGO_TARGET)/$(XARGO_PROFILE)/libstd.rlib ../.prefix/xargo/lib/rustlib/$(XARGO_TARGET)/lib/
 	$(XARGO) build $(XARGO_FLAGS)
 
+$(LIBDIR)libloader_start.a: $(OBJDIR)loader/bin/start.ao
+	@mkdir -p $(dir $@)
+	$V$(AR) rcu $@ $<
 # Low-level loader dummy dynamic library
 # - Used to tell ld that `loader`'s exported symbols actually exist
 $(LIBDIR)libloader_dyn.so: loader/bin/lib.S	$(makefile_deps)
diff --git a/Usermode/Xargo.toml b/Usermode/Xargo.toml
index 775329b1..f2723450 100644
--- a/Usermode/Xargo.toml
+++ b/Usermode/Xargo.toml
@@ -1,2 +1,11 @@
+[xargo]
+track-sysroot = true
+[xargo.stage1]
+disable-staged-api = true
+
 [dependencies.alloc]
 stage = 0
+[dependencies.std]
+stage = 1
+path = "libstd"
+
diff --git a/Usermode/libasync/Cargo.toml b/Usermode/libasync/Cargo.toml
index 90b6096c..25984cf8 100644
--- a/Usermode/libasync/Cargo.toml
+++ b/Usermode/libasync/Cargo.toml
@@ -6,5 +6,5 @@ version = "0.0.1"
 path = "lib.rs"
 
 [dependencies]
-std = { path = "../libstd" }
+#std = { path = "../libstd" }
 syscalls = { path = "../libsyscalls" }
diff --git a/Usermode/libbyteorder/Cargo.toml b/Usermode/libbyteorder/Cargo.toml
index 78949bc2..e390c012 100644
--- a/Usermode/libbyteorder/Cargo.toml
+++ b/Usermode/libbyteorder/Cargo.toml
@@ -3,6 +3,6 @@ name = "byteorder"
 version = "0.0.1"
 
 [dependencies]
-std = { path = "../libstd" }
+#std = { path = "../libstd" }
 
 
diff --git a/Usermode/libvec_ring/Cargo.toml b/Usermode/libvec_ring/Cargo.toml
index 13978813..8623cb9c 100644
--- a/Usermode/libvec_ring/Cargo.toml
+++ b/Usermode/libvec_ring/Cargo.toml
@@ -6,5 +6,4 @@ version = "0.0.1"
 path = "lib.rs"
 
 [dependencies]
-
-std = { path = "../libstd" }
+#std = { path = "../libstd" }
diff --git a/Usermode/libwtk/Cargo.toml b/Usermode/libwtk/Cargo.toml
index f86ce3e5..a6bc0700 100644
--- a/Usermode/libwtk/Cargo.toml
+++ b/Usermode/libwtk/Cargo.toml
@@ -11,4 +11,4 @@ macros = { path = "../libmacros" }
 async = { path = "../libasync" }
 byteorder = { path = "../libbyteorder" }
 
-std = { path = "../libstd" }
+#std = { path = "../libstd" }
diff --git a/Usermode/link-amd64.ld b/Usermode/link-amd64.ld
index d20cecf2..3eace4d7 100644
--- a/Usermode/link-amd64.ld
+++ b/Usermode/link-amd64.ld
@@ -1,6 +1,7 @@
 ENTRY(start)
 
-SEARCH_DIR( .ojs/amd64/libs )
+/*SEARCH_DIR( .objs/amd64/libs )*/
+SEARCH_DIR( .output/amd64/libs )
 STARTUP( .objs/amd64/rustrt0.o )
 
 PHDRS {
diff --git a/Usermode/loader/bin/Cargo.toml b/Usermode/loader/bin/Cargo.toml
index ed015e4b..3f5df146 100644
--- a/Usermode/loader/bin/Cargo.toml
+++ b/Usermode/loader/bin/Cargo.toml
@@ -2,12 +2,14 @@
 name = "loader-binary"
 version = "0.0.0"
 
-[lib]
-path = "src/main.rs"
+#[bin]
+#path = "src/main.rs"
 
 [dependencies]
 loader = { path = "../lib" }
-syscalls = { path = "../../libsyscalls" }
 byteorder = { path = "../../libbyteorder" }
 macros = { path = "../../libmacros" }
 cmdline_words_parser = { path = "../../../externals/crates.io/cmdline_words_parser" }
+
+# Part of sysroot
+#syscalls = { path = "../../libsyscalls" }
diff --git a/Usermode/loader/bin/src/main.rs b/Usermode/loader/bin/src/main.rs
index ac3afc93..0618b66b 100644
--- a/Usermode/loader/bin/src/main.rs
+++ b/Usermode/loader/bin/src/main.rs
@@ -3,7 +3,7 @@
 //
 // This program is both the initial entrypoint for the userland, and the default dynamic linker.
 #![feature(const_fn)]
-#![crate_type="lib"]
+//#![crate_type="lib"]
 #![no_main]
 
 use cmdline_words_parser::StrExt as CmdlineStrExt;
diff --git a/Usermode/loader/bin/start.S b/Usermode/loader/bin/start.S
index 8d732591..2bc014bd 100644
--- a/Usermode/loader/bin/start.S
+++ b/Usermode/loader/bin/start.S
@@ -57,6 +57,7 @@ ENTRY(start)
 .loop:
 	jmp .loop
 
+/*
 // RDI = Address
 // RSI = Value
 // RDX = Count
@@ -93,6 +94,7 @@ ENTRY(memcmp)
 	ret
 ENTRY(_Unwind_Resume)
 	jmp .
+*/
 
 #elif defined(ARCH_armv7)
 # define DEFPTR	.long
diff --git a/Usermode/rustrt0/amd64.S b/Usermode/rustrt0/amd64.S
index d504e7d5..6ace32e7 100644
--- a/Usermode/rustrt0/amd64.S
+++ b/Usermode/rustrt0/amd64.S
@@ -6,7 +6,7 @@
 .extern main
 .extern register_arguments
 
-.globl start
+.weak start
 start:
 	call *start_ra(%rip)
 	mov $0, %rdi
-- 
GitLab