Skip to content

Conversation

@grynspan
Copy link
Contributor

@grynspan grynspan commented Oct 20, 2025

This PR replaces our use of __dso_handle and (indirectly) dladdr() when figuring out the base address of loaded Swift images on ELF-based platforms. Instead, we use __ehdr_start which refers to the start of the image's ELF header (i.e. the start of the file) and is provided by the compiler/linke for this purpose.

This pointer is consumed by swift_addNewDSOImage() and, later, by Swift Testing when running tests, but is otherwise unused, so the less work we do on it at runtime, the better.

Resolves #84997.

@grynspan grynspan self-assigned this Oct 20, 2025
@grynspan grynspan requested a review from rjmccall as a code owner October 20, 2025 19:17
@grynspan grynspan requested review from a team, al45tair and mikeash as code owners October 20, 2025 19:17
@grynspan grynspan added Linux Platform: Linux FreeBSD Platform: FreeBSD labels Oct 20, 2025
@grynspan grynspan added the OpenBSD Platform: OpenBSD label Oct 20, 2025
This PR replaces our use of `__dso_handle` and (indirectly) `dladdr()` when
figuring out the base address of loaded Swift images on ELF-based platforms.
Instead, we use `__ehdr_start` which refers to the start of the image's ELF
header (i.e. the start of the file) and is provided by the compiler/linke for
this purpose.

This pointer is consumed by `swift_addNewDSOImage()` and, later, by Swift
Testing when running tests, but is otherwise unused, so the less work we do on
it at runtime, the better.
@grynspan grynspan force-pushed the jgrynspan/use__ehdr_start branch from 4d954d8 to d164e95 Compare October 20, 2025 19:18
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan added the Android Platform: Android label Oct 20, 2025
@grynspan
Copy link
Contributor Author

@3405691582 Can you confirm this compiles on OpenBSD 7.7 before I go merging it? Thanks!

@grynspan
Copy link
Contributor Author

@swift-ci test

return __ehdr_start;
}

// We've hit an edge case where the linker was invoked such that the ELF
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about this? Can you point to the ELF specification where this would be the case? I believe that __ehdr_start is the edge case - it is a GNU extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both GNU binutils and LLVM's linker define it, and thus AFAICT Linux and FreeBSD define it. Android defines it too (see e.g. https://android.googlesource.com/platform/bionic/+/main/linker/linker_main.cpp). OpenBSD is an open question for me (no pun intended) which is why I pinged @3405691582 for assistance.

There appears to be chatter in the Google results about it either: not being defined when a linker script is used; or not being defined if the ELF header isn't in the same loadable section as the program headers. I'm having a fair bit of trouble actually getting concrete info here, which is why I've added this fallback path in the first place.

@3405691582
Copy link
Member

3405691582 commented Oct 21, 2025

@3405691582 Can you confirm this compiles on OpenBSD 7.7 before I go merging it? Thanks!

Will do, but HEAD appears to be broken for me right now.

Testing this in isolation seems to work, FWIW, e.g.,

extern "C" const char __ehdr_start[] __attribute__((__weak__));

static const void *addr(void) {
    return __ehdr_start;
}

yields a non-null pointer.

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test macOS

@grynspan grynspan requested a review from compnerd October 23, 2025 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Android Platform: Android FreeBSD Platform: FreeBSD Linux Platform: Linux OpenBSD Platform: OpenBSD performance

Projects

Status: Merge

Development

Successfully merging this pull request may close these issues.

Use __ehdr_start where available to find the image base address before calling swift_addNewDSOImage().

5 participants