From 6dd00b5934d8b8bda76e5990145de2397140b6aa Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Wed, 29 Mar 2017 07:54:08 +0100 Subject: [PATCH] Create a global symbol alias for kernel.Kmain The go compiler exposes a fully qualified symbol for kernel.Kmain that also includes the full package name (github.com/.../kernel.Kmain). Since nasm cannot work with external symbols that include slashes we use objcopy to create an alias symbol "kernel.Kmain" that points to the symbol generated by the go compiler. To use the "--add-symbol" argument we need to use objcopy 2.6+. The makefile was modified to include an additional pre-compile check for the installed objcopy version. --- Makefile | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 57b8ce6..8fd2d6d 100644 --- a/Makefile +++ b/Makefile @@ -18,12 +18,15 @@ GOARCH := 386 LD_FLAGS := -n -melf_i386 -T arch/$(ARCH)/script/linker.ld -static --no-ld-generated-unwind-info AS_FLAGS := -g -f elf32 -F dwarf -I arch/$(ARCH)/asm/ +MIN_OBJCOPY_VERSION := 2.26.0 +HAVE_VALID_OBJCOPY := $(shell objcopy -V | head -1 | awk -F ' ' '{print "$(MIN_OBJCOPY_VERSION)\n" $$NF}' | sort -ct. -k1,1n -k2,2n && echo "y") + asm_src_files := $(wildcard arch/$(ARCH)/asm/*.s) asm_obj_files := $(patsubst arch/$(ARCH)/asm/%.s, $(BUILD_DIR)/arch/$(ARCH)/asm/%.o, $(asm_src_files)) -.PHONY: kernel iso clean +.PHONY: kernel iso clean binutils_version_check -kernel: $(kernel_target) +kernel: binutils_version_check $(kernel_target) $(kernel_target): $(asm_obj_files) go.o @echo "[$(LD)] linking kernel-$(ARCH).bin" @@ -45,9 +48,16 @@ go.o: @# build/go.o is a elf32 object file but all go symbols are unexported. Our @# asm entrypoint code needs to know the address to 'main.main' so we use - @# objcopy to make that symbol exportable - @echo "[objcopy] export 'main.main' symbol in go.o" - @objcopy --globalize-symbol='main.main' $(BUILD_DIR)/go.o $(BUILD_DIR)/go.o + @# objcopy to make that symbol exportable. Since nasm does not support externs + @# with slashes we create a global symbol alias for kernel.Kmain + @echo "[objcopy] creating global symbol alias 'kernel.Kmain' for 'github.com/achilleasa/gopher-os/kernel.Kmain' in go.o" + @objcopy \ + --add-symbol kernel.Kmain=.text:0x`nm $(BUILD_DIR)/go.o | grep "kernel.Kmain" | cut -d' ' -f1` \ + $(BUILD_DIR)/go.o $(BUILD_DIR)/go.o + +binutils_version_check: + @echo "[binutils] checking that installed objcopy version is >= $(MIN_OBJCOPY_VERSION)" + @if [ "$(HAVE_VALID_OBJCOPY)" != "y" ]; then echo "[binutils] error: a more up to date binutils installation is required" ; exit 1 ; fi $(BUILD_DIR)/arch/$(ARCH)/asm/%.o: arch/$(ARCH)/asm/%.s @mkdir -p $(shell dirname $@)