[lld] [lld][macho] Support order cstrings with -order_file_cstring (PR #140307)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 16 16:25:32 PDT 2025
================
@@ -388,3 +388,74 @@ macho::PriorityBuilder::buildInputSectionPriorities() {
return sectionPriorities;
}
+
+void macho::PriorityBuilder::parseOrderFileCString(StringRef path) {
+ std::optional<MemoryBufferRef> buffer = readFile(path);
+ if (!buffer) {
+ error("Could not read cstring order file at " + path);
+ return;
+ }
+ MemoryBufferRef mbref = *buffer;
+ int priority = std::numeric_limits<int>::min();
+ for (StringRef line : args::getLines(mbref)) {
+ if (line.empty())
+ continue;
+ uint32_t hash = 0;
+ if (!to_integer(line, hash))
+ continue;
+ auto it = cStringPriorities.find(hash);
+ if (it == cStringPriorities.end())
+ cStringPriorities[hash] = ++priority;
+ else
+ assert(it->second <= priority);
+ }
+}
+
+std::vector<StringPiecePair> macho::PriorityBuilder::buildCStringPriorities(
+ ArrayRef<CStringInputSection *> inputs) {
+ std::vector<StringPiecePair> orderedStringPieces;
+ if (config->cStringOrderFilePath.empty()) {
+ for (CStringInputSection *isec : inputs) {
+ for (const auto &[stringPieceIdx, piece] :
+ llvm::enumerate(isec->pieces)) {
+ if (!piece.live)
+ continue;
+ orderedStringPieces.emplace_back(isec, stringPieceIdx);
+ }
+ }
+ return orderedStringPieces;
+ }
----------------
SharonXSharon wrote:
yes, i was thinking adding this early return can avoid the unnecessary checks of `cStringPriorities` when `cStringOrderFilePath` is empty, but perhaps those operations aren't really expensive?
https://github.com/llvm/llvm-project/pull/140307
More information about the llvm-commits
mailing list