From cec450188d051bf29671457aa86fb8cf96b94a2a Mon Sep 17 00:00:00 2001 From: jief666 Date: Tue, 24 May 2022 15:34:14 +0200 Subject: [PATCH] gcc12 fix. --- BaseTools/Source/C/GenFw/Elf64Convert.c | 38 +++++- .../PlatformEFI/cpp_util/globals_ctor.cpp | 123 ++++++++++-------- 2 files changed, 104 insertions(+), 57 deletions(-) diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c index b3f89adc5..d4e303be4 100755 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -528,6 +528,7 @@ ScanSections64 ( // // List all sections. // +// printf("%d sections\n", mEhdr->e_shnum); // for (i = 0; i < mEhdr->e_shnum; i++) { // Elf_Shdr *shdr = GetShdrByIndex(i); // Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx); @@ -555,7 +556,7 @@ ScanSections64 ( // if the section address is aligned we must align PE/COFF UINT32 mCoffOffsetNew = (UINT32) ((shdr->sh_addr + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1)); // mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1)); -//printf("Section %d %s mCoffOffset=%d(0x%x) mCoffOffsetNew=%d(0x%x) diff=%d(0x%x), size=%llu\n", i, sectName, mCoffOffset, mCoffOffset, mCoffOffsetNew, mCoffOffsetNew, mCoffOffsetNew-mCoffOffset, mCoffOffsetNew-mCoffOffset, shdr->sh_size); +//printf("Section %d %s mCoffOffset=%d(0x%x) mCoffOffsetNew=%d(0x%x) diff=%d(0x%x), size=%llx\n", i, sectName, mCoffOffset, mCoffOffset, mCoffOffsetNew, mCoffOffsetNew, mCoffOffsetNew-mCoffOffset, mCoffOffsetNew-mCoffOffset, shdr->sh_size); mCoffOffset=mCoffOffsetNew; } else { Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment."); @@ -605,6 +606,7 @@ ScanSections64 ( Elf_Shdr *shdr = GetShdrByIndex(i); // /*debug*/Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx); // /*debug*/CHAR8* sectName = ((CHAR8*)mEhdr) + Namedr->sh_offset + shdr->sh_name; +// /*debug*/printf("Section %d %s shdr->sh_flags=%llx IsTextShdr(shdr)=%d\n", i, sectName, shdr->sh_flags, IsDataShdr(shdr)); if (IsDataShdr(shdr)) { if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) { // the alignment field is valid @@ -612,7 +614,7 @@ ScanSections64 ( // if the section address is aligned we must align PE/COFF UINT32 mCoffOffsetNew = (UINT32) ((shdr->sh_addr + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1)); // mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1)); -//printf("Section %d %s mCoffOffset=%d(0x%x) mCoffOffsetNew=%d(0x%x) diff=%d(0x%x), size=%llu\n", i, sectName, mCoffOffset, mCoffOffset, mCoffOffsetNew, mCoffOffsetNew, mCoffOffsetNew-mCoffOffset, mCoffOffsetNew-mCoffOffset, shdr->sh_size); +//printf("Section %d %s mCoffOffset=%d(0x%x) mCoffOffsetNew=%d(0x%x) diff=%d(0x%x), size=%llx\n", i, sectName, mCoffOffset, mCoffOffset, mCoffOffsetNew, mCoffOffsetNew, mCoffOffsetNew-mCoffOffset, mCoffOffsetNew-mCoffOffset, shdr->sh_size); mCoffOffset=mCoffOffsetNew; } else { Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment."); @@ -820,19 +822,37 @@ WriteSections64 ( // for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) { Elf_Shdr *Shdr = GetShdrByIndex(Idx); + Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx); + CHAR8* sectName = ((CHAR8*)mEhdr) + Namedr->sh_offset + Shdr->sh_name; + //printf("filter %s =%d\n", sectName, (*Filter)(Shdr)); if ((*Filter)(Shdr)) { switch (Shdr->sh_type) { case SHT_PROGBITS: /* Copy. */ if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) { + VerboseMsg("Section %s is too big\n", sectName); return FALSE; } + ///*debug*/printf("copy %s in mCoffFile at %x-%llx from %llx-%llx, size %llx\n", sectName, mCoffSectionsOffset[Idx], mCoffSectionsOffset[Idx]+Shdr->sh_size, Shdr->sh_offset, Shdr->sh_offset+Shdr->sh_size, Shdr->sh_size); + memcpy(mCoffFile + mCoffSectionsOffset[Idx], + (UINT8*)mEhdr + Shdr->sh_offset, + (size_t) Shdr->sh_size); + break; + case SHT_INIT_ARRAY: + /* Copy. */ + if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) { + VerboseMsg("Section %s is too big\n", sectName); + return FALSE; + } + ///*debug*/printf("copy %s in mCoffFile at %x-%llx from %llx-%llx, size %llx\n", sectName, mCoffSectionsOffset[Idx], mCoffSectionsOffset[Idx]+Shdr->sh_size, Shdr->sh_offset, Shdr->sh_offset+Shdr->sh_size, Shdr->sh_size); memcpy(mCoffFile + mCoffSectionsOffset[Idx], (UINT8*)mEhdr + Shdr->sh_offset, (size_t) Shdr->sh_size); break; + case SHT_NOBITS: + ///*debug*/printf("Erase %s in mCoffFile at %x-%llx\n", sectName, mCoffSectionsOffset[Idx], mCoffSectionsOffset[Idx]+Shdr->sh_size); memset(mCoffFile + mCoffSectionsOffset[Idx], 0, (size_t) Shdr->sh_size); break; @@ -840,7 +860,7 @@ WriteSections64 ( // // Ignore for unknown section type. // - VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName, (unsigned)Shdr->sh_type); + VerboseMsg ("%s unknown section '%s' type %x. We ignore this unknown section type.", mInImageName, sectName, (unsigned)Shdr->sh_type); break; } } @@ -920,14 +940,20 @@ WriteSections64 ( if (SymName == NULL) { SymName = (const UINT8 *)""; } -/* + if ( strcmp((const char *)SymName, "__cxa_pure_virtual") == 0 ) { + static int already_printed_once = 0; + if ( !already_printed_once ) { + printf("FIXME reminder : __cxa_pure_virtual is undefined -> ignored. This is happening since gcc 12.1 for unknown reasons.\n"); + already_printed_once = 1; + } + continue; + } Error (NULL, 0, 3000, "Invalid", "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. " "For example, absolute and undefined symbols are not supported.", mInImageName, SymName, Sym->st_value); - exit(EXIT_FAILURE); */ - continue; + exit(EXIT_FAILURE); } SymShdr = GetShdrByIndex(Sym->st_shndx); diff --git a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp index 8b1f49492..b18687c14 100755 --- a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp +++ b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp @@ -19,7 +19,12 @@ extern "C" { #include // Only use angled for Platform, else, xcode project won't compile #include +//#define DUMP_EFI_FROM_MEMORY +#ifdef DUMP_EFI_FROM_MEMORY +#include "../../libeg/libeg.h" +#include "../../Settings/Self.h" +#endif typedef void (ctor)(void); typedef ctor* ctor_ptr; @@ -44,31 +49,31 @@ void construct_globals_objects(EFI_HANDLE ImageHandle) DBG("ImageContext.PeCoffHeaderOffset: %08X %d\n", PeCoffHeaderOffset, PeCoffHeaderOffset); - EFI_IMAGE_OPTIONAL_HEADER_UNION* ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) (LoadedImage->ImageBase) + PeCoffHeaderOffset); - EFI_IMAGE_SECTION_HEADER* SectionHeader = (EFI_IMAGE_SECTION_HEADER *) ((UINTN) ImgHdr + sizeof(UINT32) + sizeof(EFI_IMAGE_FILE_HEADER) + ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader); + EFI_IMAGE_OPTIONAL_HEADER_UNION* ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) (LoadedImage->ImageBase) + PeCoffHeaderOffset); + EFI_IMAGE_SECTION_HEADER* SectionHeader = (EFI_IMAGE_SECTION_HEADER *) ((UINTN) ImgHdr + sizeof(UINT32) + sizeof(EFI_IMAGE_FILE_HEADER) + ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader); - for (int Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index++, SectionHeader++) - { - DBG("SectionHeader->Name=%s\n", SectionHeader->Name); -// DBG("SectionHeader->PointerToRawData=%8X\n", SectionHeader->PointerToRawData); -// DBG("SectionHeader->SizeOfRawData=%8X\n", SectionHeader->SizeOfRawData); - DBG("SectionHeader->VirtualSize=%8X\n", SectionHeader->Misc.VirtualSize); - if (strcmp((CONST CHAR8*) SectionHeader->Name, ".ctorss") == 0) - { + for (int Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index++, SectionHeader++) + { + DBG("SectionHeader->Name=%s\n", SectionHeader->Name); +// DBG("SectionHeader->PointerToRawData=%8X\n", SectionHeader->PointerToRawData); +// DBG("SectionHeader->SizeOfRawData=%8X\n", SectionHeader->SizeOfRawData); + DBG("SectionHeader->VirtualSize=%8X\n", SectionHeader->Misc.VirtualSize); + if (strcmp((CONST CHAR8*) SectionHeader->Name, ".ctorss") == 0) + { - ctor_ptr* currentCtor = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData); - ctor_ptr* ctorEnd = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData + SectionHeader->Misc.VirtualSize); + ctor_ptr* currentCtor = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData); + ctor_ptr* ctorEnd = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData + SectionHeader->Misc.VirtualSize); DBG("currentBegin %llX, ctorEnd %llX, %lld ctors to call\n", (UINTN)(currentCtor), (UINTN)(ctorEnd), (UINTN)(ctorEnd-currentCtor)); - size_t i = 0; - while (currentCtor < ctorEnd) - { - DBG("[%03zu] &ctor %08llX, will call %08llX\n", i, (UINTN)(currentCtor), (UINTN)(*currentCtor)); - if (*currentCtor != NULL) (*currentCtor)(); - currentCtor++; - i++; - } - } - } + size_t i = 0; + while (currentCtor < ctorEnd) + { + DBG("[%03zu] &ctor %08llX, will call %08llX\n", i, (UINTN)(currentCtor), (UINTN)(*currentCtor)); + if (*currentCtor != NULL) (*currentCtor)(); + currentCtor++; + i++; + } + } + } } #elif defined(__GNUC__) @@ -84,11 +89,27 @@ ctor_ptr* pend = (ctor_ptr*)&__end_of_section_ctors; void construct_globals_objects(EFI_HANDLE ImageHandle) { (void)ImageHandle; - DBG("CTORS %llX(%lld) size %ld\n", (UINTN)p, (UINTN)p, pend-p ); + +#ifdef DUMP_EFI_FROM_MEMORY + EFI_LOADED_IMAGE* LoadedImage; + EFI_STATUS Status = gBS->HandleProtocol(gImageHandle, &gEfiLoadedImageProtocolGuid, (void **) &LoadedImage); + if ( EFI_ERROR(Status) ) { + panic("construct_globals_objects: Cannot get LoadedImage protocol"); + } + + Self self; + self.initialize(ImageHandle); +// for (size_t i=0 ; i<2467264 ; i +=16) DBG("%08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx %08llx\n", (UINTN)p[i], (UINTN)p[i+1], (UINTN)p[i+2], (UINTN)p[i+3], (UINTN)p[i+4], (UINTN)p[i+5], (UINTN)p[i+6], (UINTN)p[i+7], (UINTN)p[i+8], (UINTN)p[i+9], (UINTN)p[i+10], (UINTN)p[i+11], (UINTN)p[i+12], (UINTN)p[i+13], (UINTN)p[i+14], (UINTN)p[i+15]); +// unsigned char* cp = (unsigned char*)p; +// for (size_t i=0 ; i<2467264 ; i +=16) DBG("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",cp[0]>=32 ? cp[i+0] : '.',cp[i+1]>=32 ? cp[i+1] : '.',cp[i+2]>=32 ? cp[i+2] : '.',cp[i+3]>=32 ? cp[i+3] : '.',cp[i+4]>=32 ? cp[i+4] : '.',cp[i+5]>=32 ? cp[i+5] : '.',cp[i+6]>=32 ? cp[i+6] : '.',cp[i+7]>=32 ? cp[i+7] : '.',cp[i+8]>=32 ? cp[i+8] : '.',cp[i+9]>=32 ? cp[i+9] : '.',cp[i+10]>=32 ? cp[i+10] : '.',cp[i+11]>=32 ? cp[i+11] : '.',cp[i+12]>=32 ? cp[i+12] : '.',cp[i+13]>=32 ? cp[i+13] : '.',cp[i+14]>=32 ? cp[i+14] : '.',cp[i+15]>=32 ? cp[i+15] : '.'); +// egSaveFile(&self.getCloverDir(), L"dump.bin", LoadedImage->ImageBase, 3000000); +#endif + + DBG("CTORS %llX(%lld), offset from file beginning %llX(%lld) size %ld\n", (UINTN)p, (UINTN)p, (UINTN)p - (UINTN)LoadedImage->ImageBase, (UINTN)p - (UINTN)LoadedImage->ImageBase, pend-p ); while ( p < pend ) { DBG("CTOR %llX(%lld)\n", (UINTN)p[0], (UINTN)p[0]); - (*p)(); - p++; + (*p)(); + p++; } // DBG("CTOR %X %d\n", (UINTN)__CTOR_LIST__, (UINTN)__CTOR_LIST__); // __do_init(); @@ -106,11 +127,11 @@ void construct_globals_objects(EFI_HANDLE ImageHandle) { DBG("Work in progress\n"); - EFI_LOADED_IMAGE* LoadedImage; - EFI_STATUS Status = gBS->HandleProtocol(gImageHandle, &gEfiLoadedImageProtocolGuid, (void **)&LoadedImage); - if (EFI_ERROR(Status)) { - panic("construct_globals_objects: Cannot get LoadedImage protocol"); - } + EFI_LOADED_IMAGE* LoadedImage; + EFI_STATUS Status = gBS->HandleProtocol(gImageHandle, &gEfiLoadedImageProtocolGuid, (void **)&LoadedImage); + if (EFI_ERROR(Status)) { + panic("construct_globals_objects: Cannot get LoadedImage protocol"); + } UINT32 PeCoffHeaderOffset = 0; EFI_IMAGE_DOS_HEADER* DosHdr = (EFI_IMAGE_DOS_HEADER*)LoadedImage->ImageBase; @@ -121,29 +142,29 @@ void construct_globals_objects(EFI_HANDLE ImageHandle) DBG("ImageContext.PeCoffHeaderOffset: %08X %d\n", PeCoffHeaderOffset, PeCoffHeaderOffset); - EFI_IMAGE_OPTIONAL_HEADER_UNION* ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) (LoadedImage->ImageBase) + PeCoffHeaderOffset); - EFI_IMAGE_SECTION_HEADER* SectionHeader = (EFI_IMAGE_SECTION_HEADER *) ((UINTN) ImgHdr + sizeof(UINT32) + sizeof(EFI_IMAGE_FILE_HEADER) + ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader); + EFI_IMAGE_OPTIONAL_HEADER_UNION* ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) (LoadedImage->ImageBase) + PeCoffHeaderOffset); + EFI_IMAGE_SECTION_HEADER* SectionHeader = (EFI_IMAGE_SECTION_HEADER *) ((UINTN) ImgHdr + sizeof(UINT32) + sizeof(EFI_IMAGE_FILE_HEADER) + ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader); - for (int Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index++, SectionHeader++) - { - DBG("SectionHeader->Name=%s\n", SectionHeader->Name); - // DBG("SectionHeader->PointerToRawData=%8X\n", SectionHeader->PointerToRawData); - // DBG("SectionHeader->SizeOfRawData=%8X\n", SectionHeader->SizeOfRawData); - DBG("SectionHeader->VirtualSize=%8X\n", SectionHeader->Misc.VirtualSize); - if (strcmp((CONST CHAR8*) SectionHeader->Name, ".CRT") == 0) - { + for (int Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index++, SectionHeader++) + { + DBG("SectionHeader->Name=%s\n", SectionHeader->Name); + // DBG("SectionHeader->PointerToRawData=%8X\n", SectionHeader->PointerToRawData); + // DBG("SectionHeader->SizeOfRawData=%8X\n", SectionHeader->SizeOfRawData); + DBG("SectionHeader->VirtualSize=%8X\n", SectionHeader->Misc.VirtualSize); + if (strcmp((CONST CHAR8*) SectionHeader->Name, ".CRT") == 0) + { - ctor_ptr* currentCtor = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData); - ctor_ptr* ctorend = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData + SectionHeader->Misc.VirtualSize); - while (currentCtor < ctorend) - { - DBG("¤tCtor %X %d\n", (UINTN) (currentCtor), (UINTN) (currentCtor)); - DBG("currentCtor %X %d\n", (UINTN) (*currentCtor), (UINTN) (*currentCtor)); - if (*currentCtor != NULL) (*currentCtor)(); - currentCtor++; - } - } - } + ctor_ptr* currentCtor = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData); + ctor_ptr* ctorend = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData + SectionHeader->Misc.VirtualSize); + while (currentCtor < ctorend) + { + DBG("¤tCtor %X %d\n", (UINTN) (currentCtor), (UINTN) (currentCtor)); + DBG("currentCtor %X %d\n", (UINTN) (*currentCtor), (UINTN) (*currentCtor)); + if (*currentCtor != NULL) (*currentCtor)(); + currentCtor++; + } + } + } }