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

source code of lld/MachO/SectionPriorities.h