1 //===- OutputSegment.cpp --------------------------------------------------===//
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 #include "OutputSegment.h"
10 #include "ConcatOutputSection.h"
11 #include "InputSection.h"
14 #include "SyntheticSections.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "lld/Common/Memory.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/BinaryFormat/MachO.h"
22 using namespace llvm::MachO
;
24 using namespace lld::macho
;
26 static uint32_t initProt(StringRef name
) {
28 config
->segmentProtections
,
29 [&](const SegmentProtection
&segprot
) { return segprot
.name
== name
; });
30 if (it
!= config
->segmentProtections
.end())
33 if (name
== segment_names::text
)
34 return VM_PROT_READ
| VM_PROT_EXECUTE
;
35 if (name
== segment_names::pageZero
)
37 if (name
== segment_names::linkEdit
)
39 return VM_PROT_READ
| VM_PROT_WRITE
;
42 static uint32_t maxProt(StringRef name
) {
43 assert(config
->arch() != AK_i386
&&
44 "TODO: i386 has different maxProt requirements");
46 config
->segmentProtections
,
47 [&](const SegmentProtection
&segprot
) { return segprot
.name
== name
; });
48 if (it
!= config
->segmentProtections
.end())
51 return initProt(name
);
54 static uint32_t flags(StringRef name
) {
55 // If we ever implement shared cache output support, SG_READ_ONLY should not
56 // be used for dylibs that can be placed in it.
57 return name
== segment_names::dataConst
? (uint32_t)SG_READ_ONLY
: 0;
60 size_t OutputSegment::numNonHiddenSections() const {
62 for (const OutputSection
*osec
: sections
)
63 count
+= (!osec
->isHidden() ? 1 : 0);
67 void OutputSegment::addOutputSection(OutputSection
*osec
) {
68 inputOrder
= std::min(inputOrder
, osec
->inputOrder
);
71 sections
.push_back(osec
);
73 for (const SectionAlign
§Align
: config
->sectionAlignments
)
74 if (sectAlign
.segName
== name
&& sectAlign
.sectName
== osec
->name
)
75 osec
->align
= sectAlign
.align
;
78 template <typename T
, typename F
> static auto compareByOrder(F ord
) {
79 return [=](T a
, T b
) { return ord(a
) < ord(b
); };
82 static int segmentOrder(OutputSegment
*seg
) {
83 return StringSwitch
<int>(seg
->name
)
84 .Case(segment_names::pageZero
, -4)
85 .Case(segment_names::text
, -3)
86 .Case(segment_names::dataConst
, -2)
87 .Case(segment_names::data
, -1)
88 .Case(segment_names::llvm
, std::numeric_limits
<int>::max() - 1)
89 // Make sure __LINKEDIT is the last segment (i.e. all its hidden
90 // sections must be ordered after other sections).
91 .Case(segment_names::linkEdit
, std::numeric_limits
<int>::max())
92 .Default(seg
->inputOrder
);
95 static int sectionOrder(OutputSection
*osec
) {
96 StringRef segname
= osec
->parent
->name
;
97 // Sections are uniquely identified by their segment + section name.
98 if (segname
== segment_names::text
) {
99 if (osec
->name
== section_names::header
)
101 // `__text` needs to precede the other code sections since its
102 // expected to be the largest. This means in effect that it will
103 // be the section that determines whether we need thunks or not.
104 if (osec
->name
== section_names::text
)
106 // Ensure all code sections are contiguous with `__text` for thunk
108 if (sections::isCodeSection(osec
->name
, segment_names::text
, osec
->flags
) &&
109 osec
->name
!= section_names::stubHelper
) {
112 return StringSwitch
<int>(osec
->name
)
113 .Case(section_names::stubs
, -4)
114 .Case(section_names::stubHelper
, -3)
115 .Case(section_names::objcStubs
, -2)
116 .Case(section_names::initOffsets
, -1)
117 .Case(section_names::unwindInfo
, std::numeric_limits
<int>::max() - 1)
118 .Case(section_names::ehFrame
, std::numeric_limits
<int>::max())
119 .Default(osec
->inputOrder
);
120 } else if (segname
== segment_names::data
||
121 segname
== segment_names::dataConst
) {
122 // For each thread spawned, dyld will initialize its TLVs by copying the
123 // address range from the start of the first thread-local data section to
124 // the end of the last one. We therefore arrange these sections contiguously
125 // to minimize the amount of memory used. Additionally, since zerofill
126 // sections must be at the end of their segments, and since TLV data
127 // sections can be zerofills, we end up putting all TLV data sections at the
128 // end of the segment.
129 switch (sectionType(osec
->flags
)) {
130 case S_THREAD_LOCAL_VARIABLE_POINTERS
:
131 return std::numeric_limits
<int>::max() - 3;
132 case S_THREAD_LOCAL_REGULAR
:
133 return std::numeric_limits
<int>::max() - 2;
134 case S_THREAD_LOCAL_ZEROFILL
:
135 return std::numeric_limits
<int>::max() - 1;
137 return std::numeric_limits
<int>::max();
139 return StringSwitch
<int>(osec
->name
)
140 .Case(section_names::got
, -3)
141 .Case(section_names::lazySymbolPtr
, -2)
142 .Case(section_names::const_
, -1)
143 .Default(osec
->inputOrder
);
145 } else if (segname
== segment_names::linkEdit
) {
146 return StringSwitch
<int>(osec
->name
)
147 .Case(section_names::chainFixups
, -11)
148 .Case(section_names::rebase
, -10)
149 .Case(section_names::binding
, -9)
150 .Case(section_names::weakBinding
, -8)
151 .Case(section_names::lazyBinding
, -7)
152 .Case(section_names::export_
, -6)
153 .Case(section_names::functionStarts
, -5)
154 .Case(section_names::dataInCode
, -4)
155 .Case(section_names::symbolTable
, -3)
156 .Case(section_names::indirectSymbolTable
, -2)
157 .Case(section_names::stringTable
, -1)
158 .Case(section_names::codeSignature
, std::numeric_limits
<int>::max())
159 .Default(osec
->inputOrder
);
161 // ZeroFill sections must always be the at the end of their segments:
162 // dyld checks if a segment's file size is smaller than its in-memory
163 // size to detect if a segment has zerofill sections, and if so it maps
164 // the missing tail as zerofill.
165 if (sectionType(osec
->flags
) == S_ZEROFILL
)
166 return std::numeric_limits
<int>::max();
167 return osec
->inputOrder
;
170 void OutputSegment::sortOutputSections() {
171 // Must be stable_sort() to keep special sections such as
172 // S_THREAD_LOCAL_REGULAR in input order.
173 llvm::stable_sort(sections
, compareByOrder
<OutputSection
*>(sectionOrder
));
176 void OutputSegment::assignAddressesToStartEndSymbols() {
177 for (Defined
*d
: segmentStartSymbols
)
179 for (Defined
*d
: segmentEndSymbols
)
180 d
->value
= addr
+ vmSize
;
183 void macho::sortOutputSegments() {
184 llvm::stable_sort(outputSegments
,
185 compareByOrder
<OutputSegment
*>(segmentOrder
));
188 static DenseMap
<StringRef
, OutputSegment
*> nameToOutputSegment
;
189 std::vector
<OutputSegment
*> macho::outputSegments
;
191 void macho::resetOutputSegments() {
192 outputSegments
.clear();
193 nameToOutputSegment
.clear();
196 static StringRef
maybeRenameSegment(StringRef name
) {
197 auto newName
= config
->segmentRenameMap
.find(name
);
198 if (newName
!= config
->segmentRenameMap
.end())
199 return newName
->second
;
203 OutputSegment
*macho::getOrCreateOutputSegment(StringRef name
) {
204 name
= maybeRenameSegment(name
);
206 OutputSegment
*&segRef
= nameToOutputSegment
[name
];
210 segRef
= make
<OutputSegment
>();
212 segRef
->maxProt
= maxProt(name
);
213 segRef
->initProt
= initProt(name
);
214 segRef
->flags
= flags(name
);
216 outputSegments
.push_back(segRef
);