Files
swift-mirror/stdlib/public/runtime/SwiftRT-ELF.cpp
Saleem Abdulrasool 0c42b57962 ELF: restructure image metadata registration
Restructure the ELF handling to be completely agnostic to the OS.
Rather than usng the loader to query the section information, use the
linker to construct linker tables and synthetic markers for the
beginning and of the table.  Save off the values of these pointers and
pass them along through the constructor to the runtime for registration.

This removes the need for the begin/end objects.  Remove the special
construction of the begin/end objects through the special assembly
constructs, preferring to do this in C with a bit of inline assembly to
ensure that the section is always allocated.

Remove the special handling for the various targets, the empty object
file can be linked on all the targets.

The new object file has no requirements on the ordering.  It needs to
simply be injected into the link.

Name the replacement file `swiftrt.o` mirroring `crt.o` from libc.  Merge
the constructor and the definition into a single object file.

This approach is generally more portable, overall simpler to implement,
and more robust.

Thanks to Orlando Bassotto for help analyzing some of the odd behaviours
when switching over.
2017-11-28 10:04:04 -08:00

69 lines
2.2 KiB
C++

//===--- SwiftRT-ELF.cpp --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "ImageInspectionELF.h"
#include <cstddef>
// Create empty sections to ensure that the start/stop symbols are synthesized
// by the linker. Otherwise, we may end up with undefined symbol references as
// the linker table section was never constructed.
#define DECLARE_SWIFT_SECTION(name) \
__asm__("\t.section " #name ",\"a\"\n"); \
__attribute__((__visibility__("hidden"))) extern const char __start_##name; \
__attribute__((__visibility__("hidden"))) extern const char __stop_##name;
extern "C" {
DECLARE_SWIFT_SECTION(swift2_protocol_conformances)
DECLARE_SWIFT_SECTION(swift2_type_metadata)
DECLARE_SWIFT_SECTION(swift3_typeref)
DECLARE_SWIFT_SECTION(swift3_reflstr)
DECLARE_SWIFT_SECTION(swift3_fieldmd)
DECLARE_SWIFT_SECTION(swift3_assocty)
}
#undef DECLARE_SWIFT_SECTION
namespace {
static swift::MetadataSections sections{};
}
__attribute__((__constructor__))
static void swift_image_constructor() {
#define SWIFT_SECTION_RANGE(name) \
{ reinterpret_cast<uintptr_t>(&__start_##name), \
static_cast<uintptr_t>(&__stop_##name - &__start_##name) }
sections = {
swift::CurrentSectionMetadataVersion,
0,
nullptr,
nullptr,
SWIFT_SECTION_RANGE(swift2_protocol_conformances),
SWIFT_SECTION_RANGE(swift2_type_metadata),
SWIFT_SECTION_RANGE(swift3_typeref),
SWIFT_SECTION_RANGE(swift3_reflstr),
SWIFT_SECTION_RANGE(swift3_fieldmd),
SWIFT_SECTION_RANGE(swift3_assocty),
};
#undef SWIFT_SECTION_RANGE
swift_addNewDSOImage(&sections);
}