This is an archive of the discontinued LLVM Phabricator instance.

Handle more crt*.o filename variants.
ClosedPublic

Authored by saugustine on Oct 22 2019, 11:02 AM.

Details

Summary

Crt files are special cased by name when dealing with ctor and dtor
sections, but the current code misses certain variants. In particular, those
named when clang takes the code path in
clang/lib/Driver/ToolChain.cpp:416, where crtfiles are named:

clang_rt.<component>-<arch>-<env>.<suffix>

Previously, the code only handled:
clang_rt.<component>.<suffix>
<component>.<suffix>

This revision fixes that.

Event Timeline

saugustine created this revision.Oct 22 2019, 11:02 AM
ruiu added inline comments.Oct 22 2019, 11:53 AM
lld/ELF/OutputSections.cpp
390–391

At this point maybe we should consider start using regexps?

saugustine marked an inline comment as done.Oct 22 2019, 1:11 PM
saugustine added inline comments.
lld/ELF/OutputSections.cpp
390–391

Entirely up to you. My preference is not strong.

ruiu added inline comments.Oct 22 2019, 2:12 PM
lld/ELF/OutputSections.cpp
390–391

Let's try that to see how it looks. You can create a regex object by doing this

std::regex re(R"(^crtbegin[ST].o|^clang_rtbegin[ST]-.*\.o)");

and use re.match to see if an argument matches the regex.

  • Switch crt file matching to regular expressions.
saugustine marked an inline comment as done.Oct 22 2019, 3:11 PM

Thanks. Please take another look.

The biggest issue here is that embedding a string in the regex takes more code than it saves, so I just promoted the regex into the calling functions.

ruiu added inline comments.Oct 22 2019, 3:42 PM
lld/ELF/OutputSections.cpp
389

Please remove the mention about crtend.

391

ditto

396

Add static so that the regex is compiled only once.

396

Escaping dot?

^(clang_rt\.)?

Also I blieve [\-.*]? has to be

(-.*)?

MaskRay added inline comments.Oct 22 2019, 11:11 PM
lld/ELF/OutputSections.cpp
9

Place standard includes after llvm includes. clang-format can fix the problem.

396

^ and $ can be deleted because std::regex_match checks the entire target character sequence.

saugustine marked 3 inline comments as done.
  • Address additional comments.

All comments addressed. Please take another look when you get a chance.

ruiu added a comment.Oct 24 2019, 10:33 AM

Could you add a test?

lld/ELF/OutputSections.cpp
396

nit: You don't need to escape - as it is not a metacharacter

402

ditto

You may need to update the file test/ELF/ctors_dtors_priority.s, delete the existing %t-crtbegin.o RUN lines, mkdir %t, add crtbegin.o and clang_rt.crtbegin.o RUN lines. You can run the ELF specific tests with ninja check-lld-elf.

lld/ELF/OutputSections.cpp
388–397

driver -> compiler driver

ruiu added inline comments.Oct 24 2019, 11:24 AM
lld/ELF/OutputSections.cpp
395

Remove llvm::.

Please swap this line with the following line so that static variable definitions are at the beginning of the function.

ruiu added inline comments.Oct 24 2019, 11:27 AM
lld/ELF/OutputSections.cpp
397

A char array returned by StringRef::data() is not guaranteed to be null-terminated, so I don't think this line is safe.

saugustine marked 3 inline comments as done.
  • Add test. Address other comments.
  • remove llvm namespace.
saugustine marked 2 inline comments as done.Oct 24 2019, 12:17 PM
ruiu added inline comments.Oct 24 2019, 12:26 PM
lld/ELF/OutputSections.cpp
396

I believe you can avoid creating a copy of a string here. Does something like this work?

s = sys::path::filename(s);
return std::regex_match(s.begin(), s.end(), re);
  • Avoid a string copy.
ruiu accepted this revision.Oct 24 2019, 12:58 PM

LGTM

This revision is now accepted and ready to land.Oct 24 2019, 12:58 PM
MaskRay accepted this revision.Oct 24 2019, 1:48 PM

Switch to new repo.

saugustine closed this revision.Oct 31 2019, 1:45 PM

Committed in rG118ceea5c364, with the git transition obscuring the auto-reporting.

MaskRay added a comment.EditedOct 31 2019, 2:34 PM

You may install the PHP (yes..) tool arcanist. I usually do:

# say you are in branch foobar
% arc diff 'HEAD^' # answer yes, to create a differential (Dxxxxx)
# get approved
% arc amend  # will amend your git description with a `Reviewed By:` line. I usually delete `Subscribers:` and `Tags:` lines because they are really useless. Make sure to keep the `Differential Revision:` line.
% git rebase --onto origin/master 'HEAD^' HEAD
% ninja -C Release check-lld-elf  # Make sure tests are still good after the rebase.

% git checkout master
% git merge --ff foobar
% git push

The Differential Revision: line is important. It associates a git commit with a differential and causes the differential to be closed automatically.

(My arc lives in ~/projects/arc/arcanist/bin/arc so I actually use PATH=~/projects/arc/arcanist/bin:$PATH arc diff 'HEAD^'.)