From b6292481be3cd4e9855ad40e3e4f87679a4a6c47 Mon Sep 17 00:00:00 2001 From: Regia König Date: Wed, 17 Aug 2022 15:36:55 +0200 Subject: Subtask 'hello.efi with gdb' completed --- GNU_efi_HelloWorld/Makefile | 28 ++++++---- GNU_efi_HelloWorld/example_Makefile | 96 +++++++++++++++++++++++++++++++++ GNU_efi_HelloWorld/hello.c | 1 - GNU_efi_HelloWorld/hello.debug | Bin 0 -> 123240 bytes GNU_efi_HelloWorld/hello.efi | Bin 46888 -> 46888 bytes documentation/GDB commands | 22 +++++++- documentation/GNU-efi | 77 ++++++++++++++++++++++++-- documentation/gdb with efi application | 0 test_code/OVMF_VARS.fd | Bin 540672 -> 540672 bytes 9 files changed, 210 insertions(+), 14 deletions(-) create mode 100644 GNU_efi_HelloWorld/example_Makefile create mode 100755 GNU_efi_HelloWorld/hello.debug delete mode 100644 documentation/gdb with efi application diff --git a/GNU_efi_HelloWorld/Makefile b/GNU_efi_HelloWorld/Makefile index e3a5956..80f1398 100644 --- a/GNU_efi_HelloWorld/Makefile +++ b/GNU_efi_HelloWorld/Makefile @@ -2,6 +2,7 @@ ARCH = $(shell uname -m | sed s,i[3456789]86,ia32,) OBJS = hello.o TARGET = hello.efi +TARGET_DEBUG = hello.debug EFIINC = /usr/include/efi EFFINCS = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol @@ -10,28 +11,37 @@ EFI_CRT_OBJS = $(EFILIB)/crt0-efi-$(ARCH).o EFI_LDS = $(EFILIB)/elf_$(ARCH)_efi.lds CFLAGS = $(EFFINCS) -fno-stack-protector -fpic \ - -fshort-wchar -mno-red-zone -Wall \ - -ggdb + -fshort-wchar -mno-red-zone -Wall +CFLAGS += -ggdb3 -O0 -DDEBUG -DEFI_DEBUG=1 ifeq ($(ARCH),x86_64) CFLAGS += -DEFI_FUNCTION_WRAPPER endif LDFLAGS = -nostdlib -znocombreloc -T $(EFI_LDS) -shared \ - -Bsymbolic -L $(EFILIB) $(EFI_CRT_OBJS) + -Bsymbolic -L $(EFILIB) $(EFI_CRT_OBJS) -all: $(TARGET) +SECTIONS = .text .sdata .data .dynamic .dynsym .rel .rela .reloc + +DEBUG_SECTIONS = .debug_info .debug_abbrev .debug_aranges \ + .debug_line .debug_str + +all: clear $(TARGET) $(TARGET_DEBUG) cp hello.efi ../test_code/hda-contents/ hello.so: $(OBJS) ld $(LDFLAGS) $(OBJS) -o $@ -lefi -lgnuefi %.efi: %.so - objcopy -j .text -j .sdata -j .data -j .dynamic \ - -j .dynsym -j .rel -j .rela -j .reloc \ + objcopy $(foreach sec,$(SECTIONS),-j $(sec)) \ + --target=efi-app-$(ARCH) $^ $@ + +%.debug: %.so + objcopy $(foreach sec,$(SECTIONS) $(DEBUG_SECTIONS),-j $(sec)) \ --target=efi-app-$(ARCH) $^ $@ clear: - rm *.efi - rm *.o - rm *.so + rm -f *.efi + rm -f *.debug + rm -f *.o + rm -f *.so diff --git a/GNU_efi_HelloWorld/example_Makefile b/GNU_efi_HelloWorld/example_Makefile new file mode 100644 index 0000000..f62df60 --- /dev/null +++ b/GNU_efi_HelloWorld/example_Makefile @@ -0,0 +1,96 @@ +# /phoenix/kernel/boot/Makefile +# +# This file is a part of Phoenix operating system. +# Copyright (c) 2011-2012, Artyom Lebedev +# All rights reserved. +# See COPYING file for copyright details. + +include $(PHOENIX_ROOT)/make/makevar.mak + +LOADER_BINARY = phoenix.efi + +COMPILE_DIR = build +OBJ_DIR = $(COMPILE_DIR)/$(PHOENIX_TARGET) + +SUBDIRS = gnuefi lib lib/runtime lib/x86_64 loader \ + $(PHOENIX_ROOT)/lib/common $(PHOENIX_ROOT)/lib/elf + +EFI_IMAGE = $(OBJ_DIR)/$(LOADER_BINARY) +EFI_DEBUG_IMAGE = $(OBJ_DIR)/debug.$(LOADER_BINARY) +ELF_IMAGE = $(OBJ_DIR)/elf_image.so + +SRCS = $(foreach dir,$(SUBDIRS),$(wildcard $(dir)/*.S $(dir)/*.c $(dir)/*.cpp)) + +$(foreach dir,$(SUBDIRS),$(eval vpath %.S $(dir)) $(eval vpath %.c $(dir)) $(eval vpath %.cpp $(dir))) + +OBJS = $(foreach src,$(SRCS), \ + $(OBJ_DIR)/$(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(patsubst %.S,%.o,$(notdir $(src)))))) +DEPS = $(OBJS:.o=.d) + +CFLAGS = $(GLOBAL_C_FLAGS) $(C_STANDARD) -Wno-pointer-sign + +CPP_FLAGS = $(GLOBAL_CXX_FLAGS) $(CXX_STANDARD) $(CXX_RESTRICTIONS) \ + -fno-exceptions -fno-rtti + +COMMON_FLAGS = $(GLOBAL_FLAGS) -fpic -Wall -Werror -fshort-wchar \ + -fno-strict-aliasing -fno-merge-constants -DEFI_FUNCTION_WRAPPER \ + -mno-red-zone -DCONFIG_x86_64 -DBOOT_LOADER -DEFI_APP \ + -DKERNEL_ADDRESS=$(KERNEL_ADDRESS) + +SECTIONS = .text .sdata .data .dynamic .dynsym .rel .rela .reloc +DEBUG_SECTIONS = .debug_info .debug_abbrev .debug_loc .debug_aranges \ + .debug_line .debug_macinfo .debug_str + +ifeq ($(PHOENIX_TARGET),RELEASE) +COMMON_FLAGS += -O2 +else ifeq ($(PHOENIX_TARGET),DEBUG) +COMMON_FLAGS += -ggdb3 -O0 -DDEBUG -DEFI_DEBUG=1 +else +$(error Target not supported: $(PHOENIX_TARGET)) +endif + +INCLUDE_DIRS = inc inc/protocol inc/x86_64 lib \ + $(PHOENIX_ROOT)/kernel/sys $(PHOENIX_ROOT)/kernel/sys/arch/$(PHOENIX_ARCH) \ + $(PHOENIX_ROOT)/include $(PHOENIX_ROOT)/lib/elf + +IFLAGS = $(foreach incdir,$(INCLUDE_DIRS),-I$(incdir)) + +LD_SCRIPT = gnuefi/elf_x86_64_efi.lds + +LDFLAGS = -nostdlib -T $(LD_SCRIPT) -shared -Bsymbolic --unresolved-symbols=report-all + +.PHONY: all clean + +all: $(EFI_IMAGE) $(EFI_DEBUG_IMAGE) + +# include dependencies if exist +-include $(DEPS) + +$(EFI_IMAGE): $(ELF_IMAGE) + $(OBJCOPY) $(foreach sec,$(SECTIONS),-j $(sec)) --target=efi-app-x86_64 $< $@ + +$(EFI_DEBUG_IMAGE): $(ELF_IMAGE) + $(OBJCOPY) $(foreach sec,$(SECTIONS) $(DEBUG_SECTIONS),-j $(sec)) --target=efi-app-x86_64 $< $@ + +$(ELF_IMAGE): $(OBJ_DIR) $(OBJS) + $(LD) $(LDFLAGS) $(OBJS) -o $@ + +$(OBJ_DIR)/%.o: %.S + $(CC) -DCONFIG_x86_64 -c -o $@ $< + +$(OBJ_DIR)/%.o: %.c + $(CC) $(IFLAGS) $(COMMON_FLAGS) $(CFLAGS) -c -o $@ $< + $(CC) -MM -MT '$@' $(IFLAGS) $(COMMON_FLAGS) $(CFLAGS) -c -o $(@:.o=.d) $< + +$(OBJ_DIR)/%.o: %.cpp + $(CC) $(IFLAGS) $(COMMON_FLAGS) $(CPP_FLAGS) -c -o $@ $< + $(CC) -MM -MT '$@' $(IFLAGS) $(COMMON_FLAGS) $(CPP_FLAGS) -c -o $(@:.o=.d) $< + +$(COMPILE_DIR): + if [ ! -d $@ ]; then mkdir $@; fi + +$(OBJ_DIR): $(COMPILE_DIR) + if [ ! -d $@ ]; then mkdir $@; fi + +clean: + $(RM) -rf $(COMPILE_DIR) diff --git a/GNU_efi_HelloWorld/hello.c b/GNU_efi_HelloWorld/hello.c index f40df6e..06b2b0b 100644 --- a/GNU_efi_HelloWorld/hello.c +++ b/GNU_efi_HelloWorld/hello.c @@ -1,6 +1,5 @@ #include #include -#include EFI_STATUS EFIAPI diff --git a/GNU_efi_HelloWorld/hello.debug b/GNU_efi_HelloWorld/hello.debug new file mode 100755 index 0000000..f7fd077 Binary files /dev/null and b/GNU_efi_HelloWorld/hello.debug differ diff --git a/GNU_efi_HelloWorld/hello.efi b/GNU_efi_HelloWorld/hello.efi index f8ea7e2..59e1a03 100755 Binary files a/GNU_efi_HelloWorld/hello.efi and b/GNU_efi_HelloWorld/hello.efi differ diff --git a/documentation/GDB commands b/documentation/GDB commands index 1cbd2ac..ef02570 100644 --- a/documentation/GDB commands +++ b/documentation/GDB commands @@ -14,7 +14,7 @@ QEMU CONFIGURATION # SYMBOL FILES - add-symbol-file Hello.debug 0x... -s .data 0x... + add-symbol-file /path/to/Hello.debug 0x... -s .data 0x... CONNECTION @@ -98,3 +98,23 @@ gdb --tui - next - ctrl+x o || change active window + +VARIABLES + info locals + info variables + info args + + || Set is the same as print except that the expression's value is not printed + || and is not put in the value history. + || if your program has a variable width, you get an error if you try to set + || a new value with just ‘set width=13’, because GDB has the command set width + || To avoid to set silently general variables to invalid values, ALWAYS use + || set var x=4 + print x=4 + set x=4 or set variable x=4 + whatis x -> type = double + + + + + diff --git a/documentation/GNU-efi b/documentation/GNU-efi index 1efe81c..40cd91f 100644 --- a/documentation/GNU-efi +++ b/documentation/GNU-efi @@ -59,7 +59,18 @@ hello.so: $(OBJS) -j .dynsym -j .rel -j .rela -j .reloc \ --target=efi-app-$(ARCH) $^ $@ +----- +Now expanded version in git +Get ImageBase: + Run ./test.sh and then hello.efi...this prints ImageBase + +Get Offsets: + GDB + file hello.efi + -> get text and data offset + file + add-symbol-file hello.efi (ImageBase+text-off) -s .data (ImageBase+data-off) ################################################################################# @@ -210,12 +221,72 @@ you need to add ImageBase address to their offsets: * 3000 - ... is .text * * c00 - ... is .data * * (gdb) file || unload file * -* add-symbol-table hello.efi (ImageBase+text-off) -s .data (ImageBase+data-off) * +* add-symbol-file hello.efi (ImageBase+text-off) -s .data (ImageBase+data-off) * ********************************************************************************* +################################################################################# +# https://www.rodsbooks.com/efi-programming/hello.html # +################################################################################# - - +You should not normally include regular C header files, such as stdlib.h, +because most of these header files define data types and functions that are used +by the C library. This library is not available in EFI. + +************************************ +* efi.h and efilib.h always needed * +************************************ + +Entry point: efi_main() in GNU-efi + +CFLAGS: +* -fno-stack-protector: + Stack protection isn't suppoerted by EFI, so there's no point in + building a binary with this feature active. + +* -fpic: + EFI requires that code be position-independet, hence the use + of this option. + +* -fshort-wchar: + GCC defines the wchar_t type to be 32 bits by default, but EFI requires + it to be 16 bits for 16-bit strings to work correctly. + +* fmno-red-zone: + On x86-64 systems, the red zone is an area that follows the stack pointer + that can be used for temporary variables. The EFI may modify this area, + though, so it's not safe to use, and you must compile EFI binaries with + this option. + +* -Wall: + When developing EFI applications, you might want to pay extra attention to + compiler warnings, and this switch (which causes warnings to be treated as + errors) can help. + +* -DEFI_FUNCTION_WRAPPER: + This option is required on the x86_64 platform, but is not defined on the + 32-bit x86 platform. It relates to th calling conventions for EFI functions, + described on the Using EFI Services page. + +LDFLAGS: +* -nostdlib: + An EFI application should not be linked against standard libraries, and this + argument accomplishes this goal. + +* -nocombreloc: + This argument causes the linker to not combine relocation sections. + +* -T $(EFI_LDS): + To create an EFI binary, a non-standard linker script must be used, and this + option tells ld where to find it. + +* -shared: + Even with GNU-EFI's new linker script, ld can't create the final executable. + Instead, it creates a shared library, which is subsequently + turned into the final binary. + +* -Bsymbolic: + This option causes references to global symbols to be bound to the + definitions within the shared library. diff --git a/documentation/gdb with efi application b/documentation/gdb with efi application deleted file mode 100644 index e69de29..0000000 diff --git a/test_code/OVMF_VARS.fd b/test_code/OVMF_VARS.fd index d1888bc..b00bb60 100644 Binary files a/test_code/OVMF_VARS.fd and b/test_code/OVMF_VARS.fd differ -- cgit v1.2.3-55-g7522