1//===- SectionPriorities.h --------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_MACHO_SECTION_PRIORITIES_H
10#define LLD_MACHO_SECTION_PRIORITIES_H
11
12#include "InputSection.h"
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/MapVector.h"
15
16namespace lld::macho {
17
18using SectionPair = std::pair<const InputSection *, const InputSection *>;
19
20class PriorityBuilder {
21public:
22 // Reads every input section's call graph profile, and combines them into
23 // callGraphProfile. If an order file is present, any edges where one or both
24 // of the vertices are specified in the order file are discarded.
25 void extractCallGraphProfile();
26
27 // Reads the order file at `path` into config->priorities.
28 //
29 // An order file has one entry per line, in the following format:
30 //
31 // <cpu>:<object file>:<symbol name>
32 //
33 // <cpu> and <object file> are optional. If not specified, then that entry
34 // matches any symbol of that name. Parsing this format is not quite
35 // straightforward because the symbol name itself can contain colons, so when
36 // encountering a colon, we consider the preceding characters to decide if it
37 // can be a valid CPU type or file path.
38 //
39 // If a symbol is matched by multiple entries, then it takes the
40 // lowest-ordered entry (the one nearest to the front of the list.)
41 //
42 // The file can also have line comments that start with '#'.
43 void parseOrderFile(StringRef path);
44
45 // Returns layout priorities for some or all input sections. Sections are laid
46 // out in decreasing order; that is, a higher priority section will be closer
47 // to the beginning of its output section.
48 //
49 // If either an order file or a call graph profile are present, this is used
50 // as the source of priorities. If both are present, the order file takes
51 // precedence, but the call graph profile is still used for symbols that don't
52 // appear in the order file. If neither is present, an empty map is returned.
53 //
54 // Each section gets assigned the priority of the highest-priority symbol it
55 // contains.
56 llvm::DenseMap<const InputSection *, size_t> buildInputSectionPriorities();
57
58private:
59 // The symbol with the highest priority should be ordered first in the output
60 // section (modulo input section contiguity constraints). Using priority
61 // (highest first) instead of order (lowest first) has the convenient property
62 // that the default-constructed zero priority -- for symbols/sections without
63 // a user-defined order -- naturally ends up putting them at the end of the
64 // output.
65 struct SymbolPriorityEntry {
66 // The priority given to a matching symbol, regardless of which object file
67 // it originated from.
68 size_t anyObjectFile = 0;
69 // The priority given to a matching symbol from a particular object file.
70 llvm::DenseMap<llvm::StringRef, size_t> objectFiles;
71 };
72
73 std::optional<size_t> getSymbolPriority(const Defined *sym);
74 llvm::DenseMap<llvm::StringRef, SymbolPriorityEntry> priorities;
75 llvm::MapVector<SectionPair, uint64_t> callGraphProfile;
76};
77
78extern PriorityBuilder priorityBuilder;
79} // namespace lld::macho
80
81#endif
82