diff --git a/.gitignore b/.gitignore index bb18dcd..970b638 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.efi +*.efi.debug *.o *.a *.tar.* diff --git a/Make.defaults b/Make.defaults index f9c20f3..e1759f7 100644 --- a/Make.defaults +++ b/Make.defaults @@ -140,13 +140,14 @@ INCDIR += -I$(SRCDIR) -I$(TOPDIR)/inc -I$(TOPDIR)/inc/$(ARCH) \ -I$(TOPDIR)/inc/protocol ifeq (FreeBSD, $(findstring FreeBSD, $(OS))) -CFLAGS += $(ARCH3264) -O2 -fpic -Wall -fshort-wchar -fno-strict-aliasing \ +CFLAGS += $(ARCH3264) -g -O2 -fpic -Wall -fshort-wchar -fno-strict-aliasing \ -ffreestanding -fno-stack-protector else -CFLAGS += $(ARCH3264) -O2 -fpic -Wall -fshort-wchar -fno-strict-aliasing \ +CFLAGS += $(ARCH3264) -g -O2 -fpic -Wall -fshort-wchar -fno-strict-aliasing \ -fno-merge-constants -ffreestanding -fno-stack-protector \ -fno-stack-check endif ASFLAGS += $(ARCH3264) -LDFLAGS += -nostdlib --warn-common --no-undefined --fatal-warnings +LDFLAGS += -nostdlib --warn-common --no-undefined --fatal-warnings \ + --build-id=sha1 diff --git a/Make.rules b/Make.rules index 80edb3a..5b1c286 100644 --- a/Make.rules +++ b/Make.rules @@ -39,6 +39,12 @@ -j .rela -j .rel.* -j .rela.* -j .rel* -j .rela* \ -j .reloc $(FORMAT) $*.so $@ +%.efi.debug: %.so + $(OBJCOPY) -j .debug_info -j .debug_abbrev -j .debug_aranges \ + -j .debug_line -j .debug_str -j .debug_ranges \ + -j .note.gnu.build-id \ + $(FORMAT) $*.so $@ + %.so: %.o $(LD) $(LDFLAGS) $^ -o $@ $(LOADLIBES) diff --git a/apps/Makefile b/apps/Makefile index 1edec9a..cfae62d 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -61,7 +61,7 @@ LOADLIBES += -T $(LDSCRIPT) TARGET_APPS = t.efi t2.efi t3.efi t4.efi t5.efi t6.efi \ printenv.efi t7.efi t8.efi tcc.efi modelist.efi \ route80h.efi drv0_use.efi AllocPages.efi \ - FreePages.efi setjmp.efi + FreePages.efi setjmp.efi debughook.efi debughook.efi.debug TARGET_BSDRIVERS = drv0.efi TARGET_RTDRIVERS = diff --git a/apps/debughook.c b/apps/debughook.c new file mode 100644 index 0000000..36164d6 --- /dev/null +++ b/apps/debughook.c @@ -0,0 +1,92 @@ +#include +#include + +EFI_STATUS +GetVariableAttr(CHAR16 *var, UINT8 **data, UINTN *len, EFI_GUID owner, + UINT32 *attributes) +{ + EFI_STATUS efi_status; + + *len = 0; + + efi_status = uefi_call_wrapper(RT->GetVariable, 5, var, &owner, + NULL, len, NULL); + if (efi_status != EFI_BUFFER_TOO_SMALL) + return efi_status; + + *data = AllocateZeroPool(*len); + if (!*data) + return EFI_OUT_OF_RESOURCES; + + efi_status = uefi_call_wrapper(RT->GetVariable, 5, var, &owner, + attributes, len, *data); + + if (efi_status != EFI_SUCCESS) { + FreePool(*data); + *data = NULL; + } + return efi_status; +} + +EFI_STATUS +GetVariable(CHAR16 *var, UINT8 **data, UINTN *len, EFI_GUID owner) +{ + return GetVariableAttr(var, data, len, owner, NULL); +} + +EFI_GUID DUMMY_GUID = +{0x55aad538, 0x8f82, 0x4e2a, {0xa4,0xf0,0xbe, 0x59, 0x13, 0xb6, 0x5f, 0x1e}}; + +static void +__attribute__((__optimize__("0"))) +DebugHook(void) +{ + EFI_GUID guid = DUMMY_GUID; + UINT8 *data = NULL; + UINTN dataSize = 0; + EFI_STATUS efi_status; + volatile register UINTN x = 0; + extern char _text, _data; + + if (x) + return; + + efi_status = GetVariable(L"DUMMY_DEBUG", &data, &dataSize, guid); + if (EFI_ERROR(efi_status)) { + return; + } + + Print(L"add-symbol-file /usr/lib/debug/boot/efi/debughook.debug " + L"0x%08x -s .data 0x%08x\n", &_text, &_data); + + Print(L"Pausing for debugger attachment.\n"); + Print(L"To disable this, remove the EFI variable DUMMY_DEBUG-%g .\n", + &guid); + x = 1; + while (x++) { + /* Make this so it can't /totally/ DoS us. */ +#if defined(__x86_64__) || defined(__i386__) || defined(__i686__) + if (x > 4294967294) + break; + __asm__ __volatile__("pause"); +#elif defined(__aarch64__) + if (x > 1000) + break; + __asm__ __volatile__("wfi"); +#else + if (x > 12000) + break; + uefi_call_wrapper(BS->Stall, 1, 5000); +#endif + } + x = 1; +} + + +EFI_STATUS +efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab) +{ + InitializeLib(image, systab); + DebugHook(); + return EFI_SUCCESS; +} diff --git a/gnuefi/elf_aarch64_efi.lds b/gnuefi/elf_aarch64_efi.lds index 0fdb1f6..6494e59 100644 --- a/gnuefi/elf_aarch64_efi.lds +++ b/gnuefi/elf_aarch64_efi.lds @@ -4,6 +4,7 @@ ENTRY(_start) SECTIONS { .text 0x0 : { + _text = .; *(.text.head) *(.text) *(.text.*) @@ -11,11 +12,13 @@ SECTIONS *(.srodata) *(.rodata*) . = ALIGN(16); - _etext = .; } + _etext = .; + _text_size = . - _text; .dynamic : { *(.dynamic) } .data : { + _data = .; *(.sdata) *(.data) *(.data1) diff --git a/gnuefi/elf_arm_efi.lds b/gnuefi/elf_arm_efi.lds index 1bd6b78..63d31be 100644 --- a/gnuefi/elf_arm_efi.lds +++ b/gnuefi/elf_arm_efi.lds @@ -4,6 +4,7 @@ ENTRY(_start) SECTIONS { .text 0x0 : { + _text = .; *(.text.head) *(.text) *(.text.*) @@ -11,11 +12,13 @@ SECTIONS *(.srodata) *(.rodata*) . = ALIGN(16); - _etext = .; } + _etext = .; + _text_size = . - _text; .dynamic : { *(.dynamic) } .data : { + _data = .; *(.sdata) *(.data) *(.data1) diff --git a/gnuefi/elf_ia32_efi.lds b/gnuefi/elf_ia32_efi.lds index 975e36c..b164094 100644 --- a/gnuefi/elf_ia32_efi.lds +++ b/gnuefi/elf_ia32_efi.lds @@ -9,13 +9,18 @@ SECTIONS . = ALIGN(4096); .text : { + _text = .; *(.text) *(.text.*) *(.gnu.linkonce.t.*) + . = ALIGN(16); } + _etext = .; + _text_size = . - _text; . = ALIGN(4096); .sdata : { + _data = .; *(.got.plt) *(.got) *(.srodata) @@ -55,6 +60,8 @@ SECTIONS *(.data.rel.ro) *(.data.rel*) } + _edata = .; + _data_size = . - _etext; . = ALIGN(4096); .reloc : /* This is the PECOFF .reloc section! */ { diff --git a/gnuefi/elf_ia32_fbsd_efi.lds b/gnuefi/elf_ia32_fbsd_efi.lds index bc25b1f..1f56cd7 100644 --- a/gnuefi/elf_ia32_fbsd_efi.lds +++ b/gnuefi/elf_ia32_fbsd_efi.lds @@ -9,13 +9,18 @@ SECTIONS . = ALIGN(4096); .text : { + _text = .; *(.text) *(.text.*) *(.gnu.linkonce.t.*) + . = ALIGN(16); } + _etext = .; + _text_size = . - _text; . = ALIGN(4096); .sdata : { + _data = .; *(.got.plt) *(.got) *(.srodata) @@ -55,6 +60,8 @@ SECTIONS *(.data.rel.ro) *(.data.rel*) } + _edata = .; + _data_size = . - _etext; . = ALIGN(4096); .reloc : /* This is the PECOFF .reloc section! */ { diff --git a/gnuefi/elf_ia64_efi.lds b/gnuefi/elf_ia64_efi.lds index 1d9ffc1..a6ec717 100644 --- a/gnuefi/elf_ia64_efi.lds +++ b/gnuefi/elf_ia64_efi.lds @@ -9,14 +9,19 @@ SECTIONS . = ALIGN(4096); .text : { + _text = .; *(.text) *(.text.*) *(.gnu.linkonce.t.*) + . = ALIGN(16); } + _etext = .; + _text_size = . - _text; . = ALIGN(4096); __gp = ALIGN (8) + 0x200000; .sdata : { + _data = .; *(.got.plt) *(.got) *(.srodata) @@ -51,6 +56,8 @@ SECTIONS *(.rela.stab) *(.rela.ctors) } + _edata = .; + _data_size = . - _etext; . = ALIGN(4096); .reloc : /* This is the PECOFF .reloc section! */ { diff --git a/gnuefi/elf_x86_64_efi.lds b/gnuefi/elf_x86_64_efi.lds index c4df0a5..3862d9f 100644 --- a/gnuefi/elf_x86_64_efi.lds +++ b/gnuefi/elf_x86_64_efi.lds @@ -15,10 +15,14 @@ SECTIONS . = ALIGN(4096); .text : { + _text = .; *(.text) *(.text.*) *(.gnu.linkonce.t.*) + . = ALIGN(16); } + _etext = .; + _text_size = . - _text; . = ALIGN(4096); .reloc : { @@ -27,6 +31,7 @@ SECTIONS . = ALIGN(4096); .data : { + _data = .; *(.rodata*) *(.got.plt) *(.got) @@ -41,6 +46,8 @@ SECTIONS *(COMMON) *(.rel.local) } + _edata = .; + _data_size = . - _etext; . = ALIGN(4096); .dynamic : { *(.dynamic) } . = ALIGN(4096); diff --git a/gnuefi/elf_x86_64_fbsd_efi.lds b/gnuefi/elf_x86_64_fbsd_efi.lds index 2c64609..507fe43 100644 --- a/gnuefi/elf_x86_64_fbsd_efi.lds +++ b/gnuefi/elf_x86_64_fbsd_efi.lds @@ -15,8 +15,12 @@ SECTIONS . = ALIGN(4096); .text : { + _text = .; *(.text) + . = ALIGN(16); } + _etext = .; + _text_size = . - _text; .reloc : { *(.reloc) @@ -24,6 +28,7 @@ SECTIONS . = ALIGN(4096); .data : { + _data = .; *(.rodata*) *(.got.plt) *(.got) @@ -47,6 +52,8 @@ SECTIONS *(.rela.got) *(.rela.stab) } + _edata = .; + _data_size = . - _etext; . = ALIGN(4096); .dynsym : { *(.dynsym) } . = ALIGN(4096);