Skip to content

ELF: Please provide a way to statically access Swift metadata without using a runtime call. #76698

Open
@grynspan

Description

@grynspan

Motivation

ELF images are generally stripped of their section header info, which means it's impossible to find sections at runtime. Swift loves storing metadata in sections.

Proposed solution

What if we used a "note" program header to contain a copy of this information? Program headers are not stripped, and "note" PHs are intended for arbitrary vendor use. We could imagine a program header whose desc field (i.e. payload) consists of a sequence of structures:

struct SectionInfo {
  char name[24];
  uintptr_t start;
  uintptr_t stop;
};

// Compiler or linker then emits something equivalent to this C array:
const SectionInfo desc[] = {
  { "swift5_protocols", &__start_swift5_protocols, &__stop_swift5_protocols },
  { "swift5_type_metadata", &__start_swift5_swift5_type_metadata, &__stop_swift5_type_metadata },
  ...
};

This data is then discoverable statically or at runtime by tools that include elf.h. This solves the problem of @_section data being undiscoverable in ELF binaries and helps Swift Testing do runtime metadata lookup without relying on the runtime-internal function swift_enumerateAllMetadataSections().

Linux, FreeBSD, and at least some other OSes that use ELF include dl_iterate_phdr() for enumerating program headers easily.

Alternatives considered

N/A

Additional information

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    FreeBSDPlatform: FreeBSDLinuxPlatform: LinuxcompilerThe Swift compiler itselffeatureA feature request or implementationsource toolingArea: IDE support, SourceKit, and other source tooling

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions