1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

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.
This commit is contained in:
Achilleas Anagnostopoulos 2017-03-29 07:54:08 +01:00
parent c15f27235c
commit 6dd00b5934

View File

@ -18,12 +18,15 @@ GOARCH := 386
LD_FLAGS := -n -melf_i386 -T arch/$(ARCH)/script/linker.ld -static --no-ld-generated-unwind-info 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/ 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_src_files := $(wildcard arch/$(ARCH)/asm/*.s)
asm_obj_files := $(patsubst arch/$(ARCH)/asm/%.s, $(BUILD_DIR)/arch/$(ARCH)/asm/%.o, $(asm_src_files)) 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 $(kernel_target): $(asm_obj_files) go.o
@echo "[$(LD)] linking kernel-$(ARCH).bin" @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 @# 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 @# asm entrypoint code needs to know the address to 'main.main' so we use
@# objcopy to make that symbol exportable @# objcopy to make that symbol exportable. Since nasm does not support externs
@echo "[objcopy] export 'main.main' symbol in go.o" @# with slashes we create a global symbol alias for kernel.Kmain
@objcopy --globalize-symbol='main.main' $(BUILD_DIR)/go.o $(BUILD_DIR)/go.o @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 $(BUILD_DIR)/arch/$(ARCH)/asm/%.o: arch/$(ARCH)/asm/%.s
@mkdir -p $(shell dirname $@) @mkdir -p $(shell dirname $@)