-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[lld][ELF] Handle archive special casing in Input Sections #119293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[lld][ELF] Handle archive special casing in Input Sections #119293
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-lld @llvm/pr-subscribers-lld-elf Author: None (amosher-nvidia) ChangesAccording to the binutils spec: https://sourceware.org/binutils/docs/ld/Input-Section-Basics.htm You should be able to specify all files in an archive using this syntax This patch will, only when necessary, create a copy of the file specification and add an implicit wildcard Full diff: https://github.com/llvm/llvm-project/pull/119293.diff 2 Files Affected:
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index 328368fd3b4333..b7e84a1849637d 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -195,6 +195,7 @@ class SectionPattern {
class InputSectionDescription : public SectionCommand {
SingleStringMatcher filePat;
+ SmallString<0> implicitArchiveWildcardPat;
// Cache of the most recent input argument and result of matchesFile().
mutable std::optional<std::pair<const InputFile *, bool>> matchesFileCache;
@@ -203,9 +204,19 @@ class InputSectionDescription : public SectionCommand {
InputSectionDescription(StringRef filePattern, uint64_t withFlags = 0,
uint64_t withoutFlags = 0, StringRef classRef = {})
: SectionCommand(InputSectionKind), filePat(filePattern),
- classRef(classRef), withFlags(withFlags), withoutFlags(withoutFlags) {
+ implicitArchiveWildcardPat(), classRef(classRef), withFlags(withFlags),
+ withoutFlags(withoutFlags) {
assert((filePattern.empty() || classRef.empty()) &&
"file pattern and class reference are mutually exclusive");
+
+ // Fixes up the input file pattern, adding an implicit wildcard
+ // if the trailing character is an ':' to allow matching entire archives
+ if (!filePattern.empty() && filePattern.back() == ':') {
+ implicitArchiveWildcardPat.reserve(filePattern.size() + 1);
+ implicitArchiveWildcardPat.append(filePattern);
+ implicitArchiveWildcardPat.push_back('*');
+ filePat = SingleStringMatcher(implicitArchiveWildcardPat);
+ }
}
static bool classof(const SectionCommand *c) {
diff --git a/lld/test/ELF/linkerscript/filename-spec.s b/lld/test/ELF/linkerscript/filename-spec.s
index 4fc4f2f4217528..d91b4a76ca24e2 100644
--- a/lld/test/ELF/linkerscript/filename-spec.s
+++ b/lld/test/ELF/linkerscript/filename-spec.s
@@ -55,7 +55,7 @@
# RUN: llvm-objdump -s 8 | FileCheck --check-prefix=SECONDFIRST %s
## Verify matching of archive library names in KEEP.
-# RUN: echo 'SECTIONS{.foo :{ KEEP(*lib2*(.foo)) KEEP(*lib1*(.foo)) }}' > 9.t
+# RUN: echo 'SECTIONS{.foo :{ KEEP(*lib2.a:(.foo)) KEEP(*lib1*(.foo)) }}' > 9.t
# RUN: ld.lld -o 9 -T 9.t --whole-archive \
# RUN: dir1/lib1.a dir2/lib2.a
# RUN: llvm-objdump -s 9 | FileCheck --check-prefix=SECONDFIRST %s
|
|
missing |
lld/ELF/LinkerScript.h
Outdated
|
|
||
| class InputSectionDescription : public SectionCommand { | ||
| SingleStringMatcher filePat; | ||
| SmallString<0> implicitArchiveWildcardPat; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: move before since the lifetime of the SmallString should be larger than filePat
|
I happened to run across the same problem yesterday and didn't realise that this existed #119584 . I started a fix myself which I've attached. It is only work in progress, but uses a slightly different approach. I've pasted it here just in case you want to adopt it. This also fixes the EXCLUDE_FILE can also use the same problem. My plan for that was to extract the MatchType and filePat into a separate struct so that for each EXCLUDE_FILE we could use similar logic. |
eb7db25 to
4c1215f
Compare
|
Thanks @smithp35! I took your general idea and implemented it slightly differently for better caching and added/tweaked some unit tests. |
lld/ELF/LinkerScript.cpp
Outdated
| } | ||
|
|
||
| static inline StringRef getArchiveName(const InputFile *file) { | ||
| return file ? static_cast<StringRef>(file->archiveName) : StringRef(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringRef(file->archiveName)
https://en.cppreference.com/w/cpp/language/static_cast you were using the unnecessarily complex "If target-type is a reference type, the effect is the same as performing the declaration and initialization target-type temp(expression ); "
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, file is guaranteed to be non-nullptr. I am refactoring the caller to take a reference instead of a pointer to make this clear. Just delete this function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing, I also went ahead and removed getFilename since it serves the same purpose.
lld/ELF/LinkerScript.cpp
Outdated
| return file ? file->getNameForScript() : StringRef(); | ||
| } | ||
|
|
||
| static inline StringRef getArchiveName(const InputFile *file) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete inline
e0f5e18 to
2a895c4
Compare
Thank you! At some point we may need to look at the matching in EXCLUDE_FILES but I think that can be done separately. I don't have any other comments. |
|
|
||
| ## Verify that matching files excluded from an archive doesn't match files within one | ||
| # RUN: echo 'SECTIONS{.foo :{ KEEP(:*x.o(.foo)) KEEP(*y.o(.foo)) KEEP(*x.o(.foo)) }}' > 12.t | ||
| # RUN: ld.lld -o 12 -T 12.t --whole-archive \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
combined.a doesn't need to be wrapped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the delay, missed this comment somehow. Just pushed an updated commit with this changed and freshly re-based onto main. Thanks for taking the time to review (:
Co-authored-by: Peter Smith <[email protected]>
e30bcb6 to
9e35ee0
Compare
| # RUN: ld.lld -o 11 -T 11.t --whole-archive 'lib1().a' dir2/lib2.a | ||
| # RUN: llvm-objdump -s 11 | FileCheck --check-prefix=SECONDFIRST %s | ||
|
|
||
| ## Verify that matching files excluded from an archive doesn't match files within one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Complete sentences in comments end with .
|
@amosher-nvidia Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
According to the binutils spec: https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html You should be able to specify all files in an archive using this syntax `archivename:` , however, lld currently will only accept `archivename:*` to match all files within an archive. This patch will, only when necessary, create a copy of the file specification and add an implicit wildcard `*` to the end. It also updates the filename-spec linkerscript test to check for this behavior. --------- Co-authored-by: Peter Smith <[email protected]>
According to the binutils spec: https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html
You should be able to specify all files in an archive using this syntax
archivename:, however, lld currently will only acceptarchivename:*to match all files within an archive.This patch will, only when necessary, create a copy of the file specification and add an implicit wildcard
*to the end. It also updates the filename-spec linkerscript test to check for this behavior.