Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- eliminate dependency on linking order for autosegs registration
  • Loading branch information
alexey-lysiuk committed Nov 13, 2020
1 parent 013078a commit 18b5928
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 235 deletions.
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Expand Up @@ -1097,6 +1097,7 @@ set (PCH_SOURCES
common/engine/serializer.cpp
common/engine/m_joy.cpp
common/engine/m_random.cpp
common/objects/autosegs.cpp
common/objects/dobject.cpp
common/objects/dobjgc.cpp
common/objects/dobjtype.cpp
Expand Down Expand Up @@ -1182,7 +1183,6 @@ endif()
add_executable( zdoom WIN32 MACOSX_BUNDLE
${HEADER_FILES}
${NOT_COMPILED_SOURCE_FILES}
__autostart.cpp
${SYSTEM_SOURCES}
${FASTMATH_SOURCES}
${PCH_SOURCES}
Expand All @@ -1208,7 +1208,6 @@ add_executable( zdoom WIN32 MACOSX_BUNDLE
common/thirdparty/math/tan.c
common/thirdparty/math/tanh.c
common/thirdparty/math/fastsin.cpp
zzautozend.cpp
)

set_source_files_properties( ${FASTMATH_SOURCES} PROPERTIES COMPILE_FLAGS ${ZD_FASTMATH_FLAG} )
Expand Down
102 changes: 65 additions & 37 deletions src/__autostart.cpp → src/common/objects/autosegs.cpp
Expand Up @@ -44,31 +44,78 @@

#include "autosegs.h"

#if defined(_MSC_VER)
#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

// The various reg sections are used to group pointers spread across multiple
// source files into cohesive arrays in the final executable. We don't
// actually care about these sections themselves and merge them all into
// a single section during the final link. (.rdata is the standard section
// for initialized read-only data.)
const HMODULE selfModule = GetModuleHandle(nullptr);
const SIZE_T baseAddress = reinterpret_cast<SIZE_T>(selfModule);

#pragma comment(linker, "/merge:.areg=.rdata /merge:.creg=.rdata /merge:.freg=.rdata")
#pragma comment(linker, "/merge:.greg=.rdata /merge:.yreg=.rdata")
const PIMAGE_NT_HEADERS header = ImageNtHeader(selfModule);
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(header);

#pragma section(".areg$a",read)
__declspec(allocate(".areg$a")) void *const ARegHead = 0;
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;
}
}

#pragma section(".creg$a",read)
__declspec(allocate(".creg$a")) void *const CRegHead = 0;
#elif defined __MACH__

#pragma section(".freg$a",read)
__declspec(allocate(".freg$a")) void *const FRegHead = 0;
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);
}

#pragma section(".greg$a",read)
__declspec(allocate(".greg$a")) void *const GRegHead = 0;
#else // Linux and others with ELF executables

#pragma section(".yreg$a",read)
__declspec(allocate(".yreg$a")) void *const YRegHead = 0;
assert(false);

#endif
}


#if defined(_MSC_VER)

// We want visual styles support under XP
#if defined _M_IX86
Expand All @@ -89,23 +136,4 @@ __declspec(allocate(".yreg$a")) void *const YRegHead = 0;

#endif

#elif defined(__GNUC__)

#include "doomtype.h"

// I don't know of an easy way to merge sections together with the GNU linker,
// so GCC users will see all of these sections appear in the final executable.
// (There are linker scripts, but that apparently involves extracting the
// default script from ld and then modifying it.)

void *const ARegHead __attribute__((section(SECTION_AREG))) = 0;
void *const CRegHead __attribute__((section(SECTION_CREG))) = 0;
void *const FRegHead __attribute__((section(SECTION_FREG))) = 0;
void *const GRegHead __attribute__((section(SECTION_GREG))) = 0;
void *const YRegHead __attribute__((section(SECTION_YREG))) = 0;

#else

#error Please fix autostart.cpp for your compiler

#endif
159 changes: 109 additions & 50 deletions src/common/objects/autosegs.h
Expand Up @@ -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")))
Expand All @@ -45,69 +47,126 @@
#define NO_SANITIZE
#endif

#define REGMARKER(x) (x)
typedef void * const REGINFO;
typedef void * NCREGINFO;
class FAutoSeg
{
const char *name;
void **begin;
void **end;

template <typename T>
struct ArgumentType;

// List of Action functons
extern REGINFO ARegHead;
extern REGINFO ARegTail;
template <typename Ret, typename Func, typename Arg>
struct ArgumentType<Ret(Func:: *)(Arg) const>
{
using Type = Arg;
};

// List of TypeInfos
extern REGINFO CRegHead;
extern REGINFO CRegTail;
template <typename Func>
using ArgumentTypeT = typename ArgumentType<Func>::Type;

// List of class fields
extern REGINFO FRegHead;
extern REGINFO FRegTail;
template <typename Func>
struct ReturnType
{
using Type = std::invoke_result_t<Func, ArgumentTypeT<decltype(&Func::operator())>>;
};

// List of properties
extern REGINFO GRegHead;
extern REGINFO GRegTail;
template <typename Func>
using ReturnTypeT = typename ReturnType<Func>::Type;

// List of MAPINFO map options
extern REGINFO YRegHead;
extern REGINFO YRegTail;
template <typename Func, typename Ret>
struct HasReturnType
{
static constexpr bool Value = std::is_same_v<ReturnTypeT<Func>, Ret>;
};

class FAutoSegIterator
{
public:
FAutoSegIterator(REGINFO &head, REGINFO &tail)
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)
{
// Weirdness. Mingw's linker puts these together backwards.
if (&head <= &tail)
{
Head = &head;
Tail = &tail;
}
else
if (*it)
{
Head = &tail;
Tail = &head;
func(reinterpret_cast<ArgType>(*it));
}
Probe = Head;
}
NCREGINFO operator*() const NO_SANITIZE
{
return *Probe;
}
FAutoSegIterator &operator++() NO_SANITIZE
}

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)
{
do
if (*it)
{
++Probe;
} while (*Probe == 0 && Probe < Tail);
return *this;
}
void Reset()
{
Probe = Head;
if (!func(reinterpret_cast<ArgType>(*it)))
{
return;
};
}
}

protected:
REGINFO *Probe;
REGINFO *Head;
REGINFO *Tail;
}
};

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
5 changes: 3 additions & 2 deletions src/common/objects/dobject.h
Expand Up @@ -41,6 +41,7 @@
#include "name.h"
#include "palentry.h"
#include "textureid.h"
#include "autosegs.h"

class PClass;
class PType;
Expand Down Expand Up @@ -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
Expand Down
21 changes: 7 additions & 14 deletions src/common/objects/dobjtype.cpp
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
});
}

//==========================================================================
Expand Down Expand Up @@ -953,4 +946,4 @@ void PClass::InitializeDefaults()
}
}
}
}
}

0 comments on commit 18b5928

Please sign in to comment.