2020-02-15 15:51:18 +01:00
|
|
|
//===-- crtbegin.c - Start of constructors and destructors ----------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-02-18 19:49:54 +01:00
|
|
|
#if 0
|
2020-02-15 15:51:18 +01:00
|
|
|
#define DBG(...) DebugLog(2, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DBG(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
2020-03-13 14:11:58 +01:00
|
|
|
// #include <stddef.h>
|
2020-02-15 15:51:18 +01:00
|
|
|
}
|
2020-02-21 10:44:15 +01:00
|
|
|
#include "globals_ctor.h"
|
2020-08-17 21:40:52 +02:00
|
|
|
#include <Platform.h> // Only use angled for Platform, else, xcode project won't compile
|
|
|
|
#include <Protocol/LoadedImage.h>
|
2024-01-02 12:51:17 +01:00
|
|
|
#include <IndustryStandard/PeImage.h> // for EFI_IMAGE_DOS_HEADER
|
2020-02-15 15:51:18 +01:00
|
|
|
|
2022-05-24 15:34:14 +02:00
|
|
|
//#define DUMP_EFI_FROM_MEMORY
|
2020-02-15 15:51:18 +01:00
|
|
|
|
2022-05-24 15:34:14 +02:00
|
|
|
#ifdef DUMP_EFI_FROM_MEMORY
|
|
|
|
#include "../../libeg/libeg.h"
|
|
|
|
#include "../../Settings/Self.h"
|
|
|
|
#endif
|
2020-02-15 15:51:18 +01:00
|
|
|
|
|
|
|
typedef void (ctor)(void);
|
|
|
|
typedef ctor* ctor_ptr;
|
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
|
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
void construct_globals_objects(EFI_HANDLE ImageHandle)
|
2020-02-15 15:51:18 +01:00
|
|
|
{
|
2020-08-09 17:55:30 +02:00
|
|
|
EFI_LOADED_IMAGE* LoadedImage;
|
2020-10-03 19:02:31 +02:00
|
|
|
EFI_STATUS Status = gBS->HandleProtocol(gImageHandle, &gEfiLoadedImageProtocolGuid, (void **) &LoadedImage);
|
2020-08-09 17:55:30 +02:00
|
|
|
if ( EFI_ERROR(Status) ) {
|
|
|
|
panic("construct_globals_objects: Cannot get LoadedImage protocol");
|
|
|
|
}
|
2020-02-15 15:51:18 +01:00
|
|
|
|
|
|
|
UINT32 PeCoffHeaderOffset = 0;
|
2020-08-09 17:55:30 +02:00
|
|
|
EFI_IMAGE_DOS_HEADER* DosHdr = (EFI_IMAGE_DOS_HEADER*)LoadedImage->ImageBase;
|
2020-02-15 15:51:18 +01:00
|
|
|
if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
|
|
|
|
// DOS image header is present, so read the PE header after the DOS image header
|
|
|
|
PeCoffHeaderOffset = DosHdr->e_lfanew;
|
|
|
|
}
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("ImageContext.PeCoffHeaderOffset: %08X %d\n", PeCoffHeaderOffset, PeCoffHeaderOffset);
|
2020-02-15 15:51:18 +01:00
|
|
|
|
|
|
|
|
2022-05-24 15:34:14 +02:00
|
|
|
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);
|
2020-02-15 15:51:18 +01:00
|
|
|
|
2022-05-24 15:34:14 +02:00
|
|
|
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)
|
|
|
|
{
|
2020-02-15 15:51:18 +01:00
|
|
|
|
2022-05-24 15:34:14 +02:00
|
|
|
ctor_ptr* currentCtor = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData);
|
|
|
|
ctor_ptr* ctorEnd = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData + SectionHeader->Misc.VirtualSize);
|
2021-04-16 11:55:51 +02:00
|
|
|
DBG("currentBegin %llX, ctorEnd %llX, %lld ctors to call\n", (UINTN)(currentCtor), (UINTN)(ctorEnd), (UINTN)(ctorEnd-currentCtor));
|
2023-11-06 23:51:16 +01:00
|
|
|
size_t i = 0; (void)i; // avoid warning if DBG is an empty macro
|
2022-05-24 15:34:14 +02:00
|
|
|
while (currentCtor < ctorEnd)
|
|
|
|
{
|
|
|
|
DBG("[%03zu] &ctor %08llX, will call %08llX\n", i, (UINTN)(currentCtor), (UINTN)(*currentCtor));
|
|
|
|
if (*currentCtor != NULL) (*currentCtor)();
|
|
|
|
currentCtor++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 15:51:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
|
|
|
__attribute__((visibility("hidden"))) void *__dso_handle = &__dso_handle;
|
|
|
|
|
|
|
|
extern int __beginning_of_section_ctors, __end_of_section_ctors;
|
|
|
|
|
|
|
|
ctor_ptr* p = (ctor_ptr*)&__beginning_of_section_ctors;
|
|
|
|
ctor_ptr* pend = (ctor_ptr*)&__end_of_section_ctors;
|
|
|
|
|
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
void construct_globals_objects(EFI_HANDLE ImageHandle)
|
|
|
|
{
|
|
|
|
(void)ImageHandle;
|
2022-05-24 15:34:14 +02:00
|
|
|
|
|
|
|
#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);
|
|
|
|
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 );
|
2023-11-18 11:04:54 +01:00
|
|
|
#else
|
|
|
|
DBG("CTORS %llX(%lld), size %ld\n", (UINTN)p, (UINTN)p, pend-p );
|
|
|
|
#endif
|
|
|
|
size_t ctor_idx = 0;
|
2020-02-15 15:51:18 +01:00
|
|
|
while ( p < pend ) {
|
2023-11-18 11:04:54 +01:00
|
|
|
DBG("CTOR %zu adr=%llX(%lld)\n", ctor_idx, (UINTN)p[0], (UINTN)p[0]);
|
2022-05-24 15:34:14 +02:00
|
|
|
(*p)();
|
|
|
|
p++;
|
2023-11-18 11:04:54 +01:00
|
|
|
ctor_idx++;
|
2020-02-15 15:51:18 +01:00
|
|
|
}
|
2020-03-25 19:32:44 +01:00
|
|
|
// DBG("CTOR %X %d\n", (UINTN)__CTOR_LIST__, (UINTN)__CTOR_LIST__);
|
2020-02-15 15:51:18 +01:00
|
|
|
// __do_init();
|
|
|
|
// const size_t n = __CTOR_LIST_END__ - __CTOR_LIST__ - 1;
|
|
|
|
// size_t n = 10;
|
|
|
|
// for (size_t i = 0; i < n; i++) {
|
2020-03-25 19:32:44 +01:00
|
|
|
// DBG("CTOR %X %d\n", (UINTN)__CTOR_LIST__[i], (UINTN)__CTOR_LIST__[i]);
|
2020-02-15 15:51:18 +01:00
|
|
|
// }
|
|
|
|
// __CTOR_LIST__[0]();
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
2020-09-28 17:57:50 +02:00
|
|
|
void construct_globals_objects(EFI_HANDLE ImageHandle)
|
2020-02-21 10:44:15 +01:00
|
|
|
{
|
|
|
|
DBG("Work in progress\n");
|
2020-11-02 14:45:11 +01:00
|
|
|
|
2022-05-24 15:34:14 +02:00
|
|
|
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");
|
|
|
|
}
|
2020-11-02 14:45:11 +01:00
|
|
|
|
2020-03-04 14:37:39 +01:00
|
|
|
UINT32 PeCoffHeaderOffset = 0;
|
2020-11-02 14:45:11 +01:00
|
|
|
EFI_IMAGE_DOS_HEADER* DosHdr = (EFI_IMAGE_DOS_HEADER*)LoadedImage->ImageBase;
|
2020-03-04 14:37:39 +01:00
|
|
|
if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
|
|
|
|
// DOS image header is present, so read the PE header after the DOS image header
|
|
|
|
PeCoffHeaderOffset = DosHdr->e_lfanew;
|
|
|
|
}
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("ImageContext.PeCoffHeaderOffset: %08X %d\n", PeCoffHeaderOffset, PeCoffHeaderOffset);
|
2020-03-04 14:37:39 +01:00
|
|
|
|
|
|
|
|
2022-05-24 15:34:14 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 10:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-15 15:51:18 +01:00
|
|
|
#endif
|