From a68573d0c5d6666638bf6decb3bd7fae053e910c Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Thu, 21 Jan 2016 17:08:33 +0000 Subject: [PATCH] [BOOTLIB]: Fuck you binutils. svn path=/trunk/; revision=70636 --- reactos/boot/environ/CMakeLists.txt | 4 ++-- reactos/boot/environ/lib/misc/image.c | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/reactos/boot/environ/CMakeLists.txt b/reactos/boot/environ/CMakeLists.txt index 0c51a85a843..f1ca4946736 100644 --- a/reactos/boot/environ/CMakeLists.txt +++ b/reactos/boot/environ/CMakeLists.txt @@ -113,9 +113,9 @@ add_executable(rosload ${ROSLOAD_BASE_SOURCE}) set_target_properties(rosload PROPERTIES SUFFIX ".efi") if(MSVC) - add_target_link_flags(rosload "/ignore:4078 /ignore:4254 /DRIVER /FIXED") + add_target_link_flags(rosload "/ignore:4078 /ignore:4254 /DRIVER") else() - add_target_link_flags(rosload "-Wl,--strip-all,--exclude-all-symbols") + add_target_link_flags(rosload "-Wl,--strip-all,--exclude-all-symbols,--dynamicbase,--pic-executable") endif() set_image_base(rosload 0x10000) diff --git a/reactos/boot/environ/lib/misc/image.c b/reactos/boot/environ/lib/misc/image.c index cd2e7ae923d..fd51887496a 100644 --- a/reactos/boot/environ/lib/misc/image.c +++ b/reactos/boot/environ/lib/misc/image.c @@ -677,8 +677,10 @@ ImgpLoadPEImage ( BOOLEAN First, ImageHashValid; UCHAR LocalBuffer[1024]; UCHAR TrustedBootInformation[52]; + ULONG WorkaroundForBinutils; /* Initialize locals */ + WorkaroundForBinutils = 0; LocalFile = NULL; ImageBuffer = NULL; FileSize = 0; @@ -881,6 +883,7 @@ ImgpLoadPEImage ( /* Record our current position (right after the headers) */ EndOfHeaders = (ULONG_PTR)VirtualAddress + HeaderSize; + EfiPrintf(L"here\r\n"); /* Get the first section and iterate through each one */ Section = IMAGE_FIRST_SECTION(NtHeaders); @@ -893,6 +896,7 @@ ImgpLoadPEImage ( if ((VirtualSize < Section->VirtualAddress) || ((PVOID)SectionStart < VirtualAddress)) { + EfiPrintf(L"fail 1\r\n"); Status = STATUS_INVALID_IMAGE_FORMAT; goto Quickie; } @@ -940,6 +944,7 @@ ImgpLoadPEImage ( &SectionEnd); if (!NT_SUCCESS(Status)) { + EfiPrintf(L"fail 21\r\n"); Status = STATUS_INVALID_IMAGE_FORMAT; goto Quickie; } @@ -957,6 +962,7 @@ ImgpLoadPEImage ( &SectionEnd); if (!NT_SUCCESS(Status)) { + EfiPrintf(L"fail 31\r\n"); Status = STATUS_INVALID_IMAGE_FORMAT; goto Quickie; } @@ -974,6 +980,20 @@ ImgpLoadPEImage ( /* Are we in the first iteration? */ if (!First) { + /* FUCK YOU BINUTILS */ + if ((*(PULONG)&Section->Name == 'ler.') && (RawSize < AlignSize)) + { + /* Piece of shit won't build relocations when you tell it to, + * either by using --emit-relocs or --dynamicbase. People online + * have found out that by using -pie-executable you can get this + * to happen, but then it turns out that the .reloc section is + * incorrectly sized, and results in a corrupt PE. However, they + * still compute the checksum using the correct value. What idiots. + */ + WorkaroundForBinutils = AlignSize - RawSize; + AlignSize -= WorkaroundForBinutils; + } + /* Yes, read the section data */ Status = ImgpReadAtFileOffset(LocalFile, AlignSize, @@ -994,6 +1014,7 @@ ImgpLoadPEImage ( AlignSize, BL_UTL_CHECKSUM_COMPLEMENT | BL_UTL_CHECKSUM_USHORT_BUFFER); + AlignSize += WorkaroundForBinutils; } } @@ -1075,7 +1096,7 @@ ImgpLoadPEImage ( } /* Finally, calculate the final checksum and compare it */ - FinalSum = FileSize + PartialSum; + FinalSum = FileSize + PartialSum + WorkaroundForBinutils; if ((FinalSum != CheckSum) && (PartialSum == 0xFFFF)) { /* It hit overflow, so set it to the file size */ @@ -1148,9 +1169,6 @@ ImgpLoadPEImage ( *ImageSize = VirtualSize; } - EfiPrintf(L"MORE PE TODO: %lx\r\n", NtHeaders->OptionalHeader.AddressOfEntryPoint); - EfiStall(100000000); - Quickie: /* Check if we computed the image hash OK */ if (ImageHashValid)