1 //===- OutputSegment.cpp --------------------------------------------------===//
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 #include "OutputSegment.h"
10 #include "ConcatOutputSection.h"
11 #include "InputSection.h"
12 #include "SyntheticSections.h"
13 
14 #include "lld/Common/ErrorHandler.h"
15 #include "lld/Common/Memory.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/BinaryFormat/MachO.h"
18 
19 using namespace llvm;
20 using namespace llvm::MachO;
21 using namespace lld;
22 using namespace lld::macho;
23 
24 static uint32_t initProt(StringRef name) {
25   auto it = find_if(
26       config->segmentProtections,
27       [&](const SegmentProtection &segprot) { return segprot.name == name; });
28   if (it != config->segmentProtections.end())
29     return it->initProt;
30 
31   if (name == segment_names::text)
32     return VM_PROT_READ | VM_PROT_EXECUTE;
33   if (name == segment_names::pageZero)
34     return 0;
35   if (name == segment_names::linkEdit)
36     return VM_PROT_READ;
37   return VM_PROT_READ | VM_PROT_WRITE;
38 }
39 
40 static uint32_t maxProt(StringRef name) {
41   assert(config->arch() != AK_i386 &&
42          "TODO: i386 has different maxProt requirements");
43   return initProt(name);
44 }
45 
46 size_t OutputSegment::numNonHiddenSections() const {
47   size_t count = 0;
48   for (const OutputSection *osec : sections)
49     count += (!osec->isHidden() ? 1 : 0);
50   return count;
51 }
52 
53 void OutputSegment::addOutputSection(OutputSection *osec) {
54   inputOrder = std::min(inputOrder, osec->inputOrder);
55 
56   osec->parent = this;
57   sections.push_back(osec);
58 
59   for (const SectionAlign &sectAlign : config->sectionAlignments)
60     if (sectAlign.segName == name && sectAlign.sectName == osec->name)
61       osec->align = sectAlign.align;
62 }
63 
64 template <typename T, typename F> static auto compareByOrder(F ord) {
65   return [=](T a, T b) { return ord(a) < ord(b); };
66 }
67 
68 static int segmentOrder(OutputSegment *seg) {
69   return StringSwitch<int>(seg->name)
70       .Case(segment_names::pageZero, -4)
71       .Case(segment_names::text, -3)
72       .Case(segment_names::dataConst, -2)
73       .Case(segment_names::data, -1)
74       .Case(segment_names::llvm, std::numeric_limits<int>::max() - 1)
75       // Make sure __LINKEDIT is the last segment (i.e. all its hidden
76       // sections must be ordered after other sections).
77       .Case(segment_names::linkEdit, std::numeric_limits<int>::max())
78       .Default(seg->inputOrder);
79 }
80 
81 static int sectionOrder(OutputSection *osec) {
82   StringRef segname = osec->parent->name;
83   // Sections are uniquely identified by their segment + section name.
84   if (segname == segment_names::text) {
85     return StringSwitch<int>(osec->name)
86         .Case(section_names::header, -4)
87         .Case(section_names::text, -3)
88         .Case(section_names::stubs, -2)
89         .Case(section_names::stubHelper, -1)
90         .Case(section_names::unwindInfo, std::numeric_limits<int>::max() - 1)
91         .Case(section_names::ehFrame, std::numeric_limits<int>::max())
92         .Default(osec->inputOrder);
93   } else if (segname == segment_names::data ||
94              segname == segment_names::dataConst) {
95     // For each thread spawned, dyld will initialize its TLVs by copying the
96     // address range from the start of the first thread-local data section to
97     // the end of the last one. We therefore arrange these sections contiguously
98     // to minimize the amount of memory used. Additionally, since zerofill
99     // sections must be at the end of their segments, and since TLV data
100     // sections can be zerofills, we end up putting all TLV data sections at the
101     // end of the segment.
102     switch (sectionType(osec->flags)) {
103     case S_THREAD_LOCAL_VARIABLE_POINTERS:
104       return std::numeric_limits<int>::max() - 3;
105     case S_THREAD_LOCAL_REGULAR:
106       return std::numeric_limits<int>::max() - 2;
107     case S_THREAD_LOCAL_ZEROFILL:
108       return std::numeric_limits<int>::max() - 1;
109     case S_ZEROFILL:
110       return std::numeric_limits<int>::max();
111     default:
112       return StringSwitch<int>(osec->name)
113           .Case(section_names::got, -3)
114           .Case(section_names::lazySymbolPtr, -2)
115           .Case(section_names::const_, -1)
116           .Default(osec->inputOrder);
117     }
118   } else if (segname == segment_names::linkEdit) {
119     return StringSwitch<int>(osec->name)
120         .Case(section_names::rebase, -10)
121         .Case(section_names::binding, -9)
122         .Case(section_names::weakBinding, -8)
123         .Case(section_names::lazyBinding, -7)
124         .Case(section_names::export_, -6)
125         .Case(section_names::functionStarts, -5)
126         .Case(section_names::dataInCode, -4)
127         .Case(section_names::symbolTable, -3)
128         .Case(section_names::indirectSymbolTable, -2)
129         .Case(section_names::stringTable, -1)
130         .Case(section_names::codeSignature, std::numeric_limits<int>::max())
131         .Default(osec->inputOrder);
132   }
133   // ZeroFill sections must always be the at the end of their segments:
134   // dyld checks if a segment's file size is smaller than its in-memory
135   // size to detect if a segment has zerofill sections, and if so it maps
136   // the missing tail as zerofill.
137   if (sectionType(osec->flags) == S_ZEROFILL)
138     return std::numeric_limits<int>::max();
139   return osec->inputOrder;
140 }
141 
142 void OutputSegment::sortOutputSections() {
143   // Must be stable_sort() to keep special sections such as
144   // S_THREAD_LOCAL_REGULAR in input order.
145   llvm::stable_sort(sections, compareByOrder<OutputSection *>(sectionOrder));
146 }
147 
148 void macho::sortOutputSegments() {
149   // sort() instead of stable_sort() is fine because segmentOrder() is
150   // name-based and getOrCreateOutputSegment() makes there's only a single
151   // segment for every name.
152   llvm::sort(outputSegments, compareByOrder<OutputSegment *>(segmentOrder));
153 }
154 
155 static DenseMap<StringRef, OutputSegment *> nameToOutputSegment;
156 std::vector<OutputSegment *> macho::outputSegments;
157 
158 OutputSegment *macho::getOrCreateOutputSegment(StringRef name) {
159   OutputSegment *&segRef = nameToOutputSegment[name];
160   if (segRef)
161     return segRef;
162 
163   segRef = make<OutputSegment>();
164   segRef->name = name;
165   segRef->maxProt = maxProt(name);
166   segRef->initProt = initProt(name);
167 
168   outputSegments.push_back(segRef);
169   return segRef;
170 }
171