This is an archive of the discontinued LLVM Phabricator instance.

[LLD] [COFF] Provide __CTOR_LIST__ and __DTOR_LIST__ symbols for MinGW
ClosedPublic

Authored by mstorsjo on Sep 13 2018, 12:53 PM.

Details

Summary

MinGW uses these kind of list terminator symbols for traversing the constructor/destructor lists. These list terminators are actual pointers entries in the lists, with the values 0 and (uintptr_t)-1 (instead of just symbols pointing to the start/end of the list).

(This mechanism exists in both the mingw-w64 crt startup code and in libgcc; normally the mingw-w64 one is used, but a DLL build of libgcc uses the libgcc one. Therefore it's not trivial to change the mechanism without lots of cross-project synchronization and potentially invalidating some combinations of old/new versions of them.)

When mingw-w64 has been used with lld so far, the CRT startup object files have so far provided these symbols, ending up with different, incompatible builds of the CRT startup object files depending on whether binutils or lld are going to be used.

In order to avoid the need of different configuration of the CRT startup object files depending on what linker to be used, provide these symbols in lld instead. (Mingw-w64 checks at build time whether the linker provides these symbols or not.) This unifies this particular detail between the two linkers.

This does disallow the use of the very latest lld with older versions of mingw-w64 (the configure check for the list was added recently; earlier it simply checked whether the CRT was built with gcc or clang), and requires rebuilding the mingw-w64 CRT. But the number of users of lld+mingw still is low enough that such a change should be tolerable, and unifies this aspect of the toolchains, easing interoperability between the toolchains for the future.

The actual test for this feature is added in ctors_dtors_priority.s, but a number of other tests that checked absolute output addresses are updated.

This is one out of two missing features to make ld.lld a fully working drop-in replacement for ld.bfd in existing mingw sysroots.

Diff Detail

Repository
rL LLVM

Event Timeline

mstorsjo created this revision.Sep 13 2018, 12:53 PM
ruiu added inline comments.Sep 14 2018, 11:02 AM
COFF/Chunks.h
483 ↗(On Diff #165358)

... but should be okay since the data is small?

If so, I'd add that to the comment.

COFF/Writer.cpp
1392 ↗(On Diff #165358)

I'd explain the structure of .ctors and .dtors here. E.g. They have to start with a null pointer and end with a (void *)-1, so we add these sentinels in this function.

1408 ↗(On Diff #165358)

Looks like CustomDataChunk is a bit over-designed, because we are using it only to represent a single-word data chunk. Instead of generalizing it, you could define a chunk that takes an integer and handle it as a pointer-size data. Then you can eliminate std::vector from this function.

mstorsjo updated this revision to Diff 165590.Sep 14 2018, 2:07 PM

Applied @ruiu's suggestions.

ruiu accepted this revision.Sep 14 2018, 2:13 PM

LGTM

This revision is now accepted and ready to land.Sep 14 2018, 2:13 PM
This revision was automatically updated to reflect the committed changes.