- eliminate dependency on linking order for autosegs registration

This commit is contained in:
alexey.lysiuk 2020-11-13 11:03:16 +02:00
commit 18b5928f49
14 changed files with 218 additions and 235 deletions

View file

@ -0,0 +1,139 @@
/*
** autostart.cpp
** This file contains the heads of lists stored in special data segments
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
** The particular scheme used here was chosen because it's small.
**
** An alternative that will work with any C++ compiler is to use static
** classes to build these lists at run time. Under Visual C++, doing things
** that way can require a lot of extra space, which is why I'm doing things
** this way.
**
** In the case of PClass lists (section creg), I orginally used the
** constructor to do just that, and the code for that still exists if you
** compile with something other than Visual C++ or GCC.
*/
#include "autosegs.h"
#ifdef _WIN32
#include <windows.h>
#include <dbghelp.h>
#elif defined __MACH__
#include <mach-o/getsect.h>
#endif
#if defined _WIN32 || defined __MACH__
#define AUTOSEG_VARIABLE(name, autoseg) namespace AutoSegs{ FAutoSeg name{ AUTOSEG_STR(autoseg) }; }
#else // Linux and others with ELF executables
#define AUTOSEG_START(name) __start_##name
#define AUTOSEG_STOP(name) __stop_##name
#define AUTOSEG_VARIABLE(name, autoseg) \
void* name##DummyPointer __attribute__((section(AUTOSEG_STR(autoseg)))) __attribute__((used)); \
extern void* AUTOSEG_START(autoseg); \
extern void* AUTOSEG_STOP(autoseg); \
namespace AutoSegs { FAutoSeg name{ &AUTOSEG_START(autoseg), &AUTOSEG_STOP(autoseg) }; }
#endif
AUTOSEG_VARIABLE(ActionFunctons, AUTOSEG_AREG)
AUTOSEG_VARIABLE(TypeInfos, AUTOSEG_CREG)
AUTOSEG_VARIABLE(ClassFields, AUTOSEG_FREG)
AUTOSEG_VARIABLE(Properties, AUTOSEG_GREG)
AUTOSEG_VARIABLE(MapInfoOptions, AUTOSEG_YREG)
#undef AUTOSEG_VARIABLE
#undef AUTOSEG_STOP
#undef AUTOSEG_START
void FAutoSeg::Initialize()
{
#ifdef _WIN32
const HMODULE selfModule = GetModuleHandle(nullptr);
const SIZE_T baseAddress = reinterpret_cast<SIZE_T>(selfModule);
const PIMAGE_NT_HEADERS header = ImageNtHeader(selfModule);
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(header);
for (WORD i = 0; i < header->FileHeader.NumberOfSections; ++i, ++section)
{
if (strncmp(reinterpret_cast<char *>(section->Name), name, IMAGE_SIZEOF_SHORT_NAME) == 0)
{
begin = reinterpret_cast<void **>(baseAddress + section->VirtualAddress);
end = reinterpret_cast<void **>(baseAddress + section->VirtualAddress + section->SizeOfRawData);
break;
}
}
#elif defined __MACH__
if (const struct section_64 *const section = getsectbyname(AUTOSEG_MACH_SEGMENT, name))
{
begin = reinterpret_cast<void **>(section->addr);
end = reinterpret_cast<void **>(section->addr + section->size);
}
#else // Linux and others with ELF executables
assert(false);
#endif
}
#if defined(_MSC_VER)
// We want visual styles support under XP
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#endif

View file

@ -35,6 +35,8 @@
#ifndef AUTOSEGS_H
#define AUTOSEGS_H
#include <type_traits>
#if defined(__clang__)
#if defined(__has_feature) && __has_feature(address_sanitizer)
#define NO_SANITIZE __attribute__((no_sanitize("address")))
@ -45,69 +47,126 @@
#define NO_SANITIZE
#endif
#define REGMARKER(x) (x)
typedef void * const REGINFO;
typedef void * NCREGINFO;
// List of Action functons
extern REGINFO ARegHead;
extern REGINFO ARegTail;
// List of TypeInfos
extern REGINFO CRegHead;
extern REGINFO CRegTail;
// List of class fields
extern REGINFO FRegHead;
extern REGINFO FRegTail;
// List of properties
extern REGINFO GRegHead;
extern REGINFO GRegTail;
// List of MAPINFO map options
extern REGINFO YRegHead;
extern REGINFO YRegTail;
class FAutoSegIterator
class FAutoSeg
{
public:
FAutoSegIterator(REGINFO &head, REGINFO &tail)
{
// Weirdness. Mingw's linker puts these together backwards.
if (&head <= &tail)
{
Head = &head;
Tail = &tail;
}
else
{
Head = &tail;
Tail = &head;
}
Probe = Head;
}
NCREGINFO operator*() const NO_SANITIZE
{
return *Probe;
}
FAutoSegIterator &operator++() NO_SANITIZE
{
do
{
++Probe;
} while (*Probe == 0 && Probe < Tail);
return *this;
}
void Reset()
{
Probe = Head;
}
const char *name;
void **begin;
void **end;
protected:
REGINFO *Probe;
REGINFO *Head;
REGINFO *Tail;
template <typename T>
struct ArgumentType;
template <typename Ret, typename Func, typename Arg>
struct ArgumentType<Ret(Func:: *)(Arg) const>
{
using Type = Arg;
};
template <typename Func>
using ArgumentTypeT = typename ArgumentType<Func>::Type;
template <typename Func>
struct ReturnType
{
using Type = std::invoke_result_t<Func, ArgumentTypeT<decltype(&Func::operator())>>;
};
template <typename Func>
using ReturnTypeT = typename ReturnType<Func>::Type;
template <typename Func, typename Ret>
struct HasReturnType
{
static constexpr bool Value = std::is_same_v<ReturnTypeT<Func>, Ret>;
};
template <typename Func, typename Ret>
static constexpr bool HasReturnTypeV = HasReturnType<Func, Ret>::Value;
void Initialize();
public:
explicit FAutoSeg(const char *name)
: name(name)
, begin(nullptr)
, end(nullptr)
{
Initialize();
}
FAutoSeg(void** begin, void** end)
: name(nullptr)
, begin(begin)
, end(end)
{
}
template <typename Func>
void ForEach(Func func, std::enable_if_t<HasReturnTypeV<Func, void>> * = nullptr)
{
using CallableType = decltype(&Func::operator());
using ArgType = typename ArgumentType<CallableType>::Type;
for (void **it = begin; it < end; ++it)
{
if (*it)
{
func(reinterpret_cast<ArgType>(*it));
}
}
}
template <typename Func>
void ForEach(Func func, std::enable_if_t<HasReturnTypeV<Func, bool>> * = nullptr)
{
using CallableType = decltype(&Func::operator());
using ArgType = typename ArgumentType<CallableType>::Type;
for (void **it = begin; it < end; ++it)
{
if (*it)
{
if (!func(reinterpret_cast<ArgType>(*it)))
{
return;
};
}
}
}
};
namespace AutoSegs
{
extern FAutoSeg ActionFunctons;
extern FAutoSeg TypeInfos;
extern FAutoSeg ClassFields;
extern FAutoSeg Properties;
extern FAutoSeg MapInfoOptions;
}
#define AUTOSEG_AREG areg
#define AUTOSEG_CREG creg
#define AUTOSEG_FREG freg
#define AUTOSEG_GREG greg
#define AUTOSEG_YREG yreg
#define AUTOSEG_STR(string) AUTOSEG_STR2(string)
#define AUTOSEG_STR2(string) #string
#ifdef __MACH__
#define AUTOSEG_MACH_SEGMENT "__DATA"
#define AUTOSEG_MACH_SECTION(section) AUTOSEG_MACH_SEGMENT "," AUTOSEG_STR(section)
#define SECTION_AREG AUTOSEG_MACH_SECTION(AUTOSEG_AREG)
#define SECTION_CREG AUTOSEG_MACH_SECTION(AUTOSEG_CREG)
#define SECTION_FREG AUTOSEG_MACH_SECTION(AUTOSEG_FREG)
#define SECTION_GREG AUTOSEG_MACH_SECTION(AUTOSEG_GREG)
#define SECTION_YREG AUTOSEG_MACH_SECTION(AUTOSEG_YREG)
#else
#define SECTION_AREG AUTOSEG_STR(AUTOSEG_AREG)
#define SECTION_CREG AUTOSEG_STR(AUTOSEG_CREG)
#define SECTION_FREG AUTOSEG_STR(AUTOSEG_FREG)
#define SECTION_GREG AUTOSEG_STR(AUTOSEG_GREG)
#define SECTION_YREG AUTOSEG_STR(AUTOSEG_YREG)
#endif
#endif

View file

@ -41,6 +41,7 @@
#include "name.h"
#include "palentry.h"
#include "textureid.h"
#include "autosegs.h"
class PClass;
class PType;
@ -134,8 +135,8 @@ public: \
static const size_t PointerOffsets[];
#if defined(_MSC_VER)
# pragma section(".creg$u",read)
# define _DECLARE_TI(cls) __declspec(allocate(".creg$u")) ClassReg * const cls::RegistrationInfoPtr = &cls::RegistrationInfo;
# pragma section(SECTION_CREG,read)
# define _DECLARE_TI(cls) __declspec(allocate(SECTION_CREG)) ClassReg * const cls::RegistrationInfoPtr = &cls::RegistrationInfo;
#else
# define _DECLARE_TI(cls) ClassReg * const cls::RegistrationInfoPtr __attribute__((section(SECTION_CREG))) = &cls::RegistrationInfo;
#endif

View file

@ -206,13 +206,10 @@ void PClass::StaticInit ()
{
Namespaces.GlobalNamespace = Namespaces.NewNamespace(0);
FAutoSegIterator probe(CRegHead, CRegTail);
while (*++probe != nullptr)
AutoSegs::TypeInfos.ForEach([](ClassReg* typeInfo)
{
((ClassReg *)*probe)->RegisterClass ();
}
probe.Reset();
typeInfo->RegisterClass();
});
// Keep built-in classes in consistant order. I did this before, though
// I'm not sure if this is really necessary to maintain any sort of sync.
@ -268,14 +265,10 @@ void PClass::StaticShutdown ()
AllClasses.Clear();
ClassMap.Clear();
FAutoSegIterator probe(CRegHead, CRegTail);
while (*++probe != nullptr)
AutoSegs::TypeInfos.ForEach([](ClassReg* typeInfo)
{
auto cr = ((ClassReg *)*probe);
cr->MyClass = nullptr;
}
typeInfo->MyClass = nullptr;
});
}
//==========================================================================
@ -953,4 +946,4 @@ void PClass::InitializeDefaults()
}
}
}
}
}