1 //===- SectionPriorities.h --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_MACHO_SECTION_PRIORITIES_H
10 #define LLD_MACHO_SECTION_PRIORITIES_H
12 #include "InputSection.h"
13 #include "llvm/ADT/DenseMap.h"
15 namespace lld::macho
{
17 using SectionPair
= std::pair
<const InputSection
*, const InputSection
*>;
19 class PriorityBuilder
{
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();
26 // Reads the order file at `path` into config->priorities.
28 // An order file has one entry per line, in the following format:
30 // <cpu>:<object file>:<symbol name>
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.
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.)
41 // The file can also have line comments that start with '#'.
42 void parseOrderFile(StringRef path
);
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.
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.
53 // Each section gets assigned the priority of the highest-priority symbol it
55 llvm::DenseMap
<const InputSection
*, size_t> buildInputSectionPriorities();
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
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
;
72 llvm::Optional
<size_t> getSymbolPriority(const Defined
*sym
);
73 llvm::DenseMap
<llvm::StringRef
, SymbolPriorityEntry
> priorities
;
74 llvm::MapVector
<SectionPair
, uint64_t> callGraphProfile
;
77 extern PriorityBuilder priorityBuilder
;
78 } // namespace lld::macho