This is an archive of the discontinued LLVM Phabricator instance.

[LLD] [COFF] Interpret a period as a separator for section suffix just like '$'
ClosedPublic

Authored by mstorsjo on Nov 24 2017, 12:56 AM.

Details

Summary

This allows grouping all sections like ".ctors.12345" into ".ctors".

For MinGW, the numerical values for such ctors are all zero-padded, so a lexical sort is good enough.

Diff Detail

Repository
rL LLVM

Event Timeline

mstorsjo created this revision.Nov 24 2017, 12:56 AM
ruiu edited edge metadata.Nov 24 2017, 1:26 AM

Ah, okay, this is the lld-side patch. I wonder if you can't just create .ctors$12345 instead of .ctors.12345 for COFF. Do you need this?

In D40408#934167, @ruiu wrote:

Ah, okay, this is the lld-side patch. I wonder if you can't just create .ctors$12345 instead of .ctors.12345 for COFF. Do you need this?

On the LLVM side, I'm trying to match what GCC does, to allow using this with clang as compiler while using the GNU binutils in MinGW.

(Even if we'd make LLVM produce .ctors$12345, we need to sort them numerically so that .ctors$5 would end up before .ctors$12345.)

ruiu added inline comments.Nov 24 2017, 2:17 AM
COFF/Writer.cpp
329 ↗(On Diff #124134)

Please add a comment saying that this is for MinGW. I'd also mention that this could have been technically avoided because we could have used '$' instead of '.', but we do this for compatibility with GNU.

329 ↗(On Diff #124134)

Passing a StringRef by reference seems a bit odd. The class is designed to be passed by value, because it is essentially a pointer with size.

329 ↗(On Diff #124134)

Can you describe the rule that this function is trying to implement briefly?

mstorsjo added inline comments.Nov 24 2017, 2:45 AM
COFF/Writer.cpp
329 ↗(On Diff #124134)

Please add a comment saying that this is for MinGW.

Sure

I'd also mention that this could have been technically avoided because we could have used '$' instead of '.',

No, this still couldn't have been avoided by using '$' instead of '.', since half of the point is to order sections according to the numerical value, not alphabetically. Alphabetically, 123 would be ordered before 5.

Passing a StringRef by reference seems a bit odd. The class is designed to be passed by value, because it is essentially a pointer with size.

Yes, that's obviously better. I had tried that, but missed that I needed to update the prototype in the declaration of std::map<..> Map as well.

Can you describe the rule that this function is trying to implement briefly?

Order sections alphabetically, as before, with two exceptions:

  • If the base section name is followed by a period, it should be sorted before section names followed by a dollar sign. An alphabetical sort would give the order .ctors, .ctors$zzz, .ctors.123.
  • If both base section names are followed by a period, order them according to the numerical value following the period, instead of the lexical sort of the strings.

I can update with the redundant const StringRef& removed and some comments added.

mstorsjo updated this revision to Diff 124149.Nov 24 2017, 2:57 AM

Added comments, removed redundant checking for periods (replaced with asserts), removed the redundant StringRef references.

mstorsjo updated this revision to Diff 124316.Nov 26 2017, 2:16 PM
mstorsjo retitled this revision from [LLD] [COFF] Implement numerical sorting of constructors with priority to [LLD] [COFF] Interpret a period as a separator for section suffix just like '$'.
mstorsjo edited the summary of this revision. (Show Details)

I noticed that GCC actually does zero pad these, and GNU ld expects them zero padded, so this simplifies this patch quite significantly. The .ctors$zzz part is only used in the llvm/lld specific parts of mingw-w64, and can be replaced with .ctors.99999 or something similar once this goes in, to avoid the need for the special sorting of them.

mstorsjo updated this revision to Diff 124327.Nov 26 2017, 11:01 PM

Added a comment in the source to clarify that this is for MinGW.

ruiu accepted this revision.Nov 27 2017, 8:59 AM

Nice! LGTM

COFF/Writer.cpp
322–324 ↗(On Diff #124327)

nit: add blank lines before and after this code block so that MinGW code looks separated from other code.

This revision is now accepted and ready to land.Nov 27 2017, 8:59 AM
This revision was automatically updated to reflect the committed changes.