1 //===- InputSection.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 "InputSection.h"
10 #include "InputFiles.h"
11 #include "OutputSegment.h"
12 #include "Symbols.h"
13 #include "SyntheticSections.h"
14 #include "Target.h"
15 #include "Writer.h"
16 #include "lld/Common/Memory.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/xxhash.h"
19 
20 using namespace llvm;
21 using namespace llvm::MachO;
22 using namespace llvm::support;
23 using namespace lld;
24 using namespace lld::macho;
25 
26 std::vector<InputSection *> macho::inputSections;
27 
28 uint64_t InputSection::getFileSize() const {
29   return isZeroFill(flags) ? 0 : getSize();
30 }
31 
32 uint64_t InputSection::getVA(uint64_t off) const {
33   return parent->addr + getOffset(off);
34 }
35 
36 static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) {
37   const RelocAttrs &relocAttrs = target->getRelocAttrs(type);
38   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH))
39     return sym->resolveBranchVA();
40   else if (relocAttrs.hasAttr(RelocAttrBits::GOT))
41     return sym->resolveGotVA();
42   else if (relocAttrs.hasAttr(RelocAttrBits::TLV))
43     return sym->resolveTlvVA();
44   return sym->getVA();
45 }
46 
47 void ConcatInputSection::writeTo(uint8_t *buf) {
48   assert(!shouldOmitFromOutput());
49 
50   if (getFileSize() == 0)
51     return;
52 
53   memcpy(buf, data.data(), data.size());
54 
55   for (size_t i = 0; i < relocs.size(); i++) {
56     const Reloc &r = relocs[i];
57     uint8_t *loc = buf + r.offset;
58     uint64_t referentVA = 0;
59     if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
60       const Symbol *fromSym = r.referent.get<Symbol *>();
61       const Reloc &minuend = relocs[++i];
62       uint64_t minuendVA;
63       if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
64         minuendVA = toSym->getVA() + minuend.addend;
65       else {
66         auto *referentIsec = minuend.referent.get<InputSection *>();
67         assert(!::shouldOmitFromOutput(referentIsec));
68         minuendVA = referentIsec->getVA(minuend.addend);
69       }
70       referentVA = minuendVA - fromSym->getVA();
71     } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
72       if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
73           !referentSym->isInGot())
74         target->relaxGotLoad(loc, r.type);
75       referentVA = resolveSymbolVA(referentSym, r.type) + r.addend;
76 
77       if (isThreadLocalVariables(flags)) {
78         // References from thread-local variable sections are treated as offsets
79         // relative to the start of the thread-local data memory area, which
80         // is initialized via copying all the TLV data sections (which are all
81         // contiguous).
82         if (isa<Defined>(referentSym))
83           referentVA -= firstTLVDataSection->addr;
84       }
85     } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
86       assert(!::shouldOmitFromOutput(referentIsec));
87       referentVA = referentIsec->getVA(r.addend);
88     }
89     target->relocateOne(loc, r, referentVA, getVA() + r.offset);
90   }
91 }
92 
93 void CStringInputSection::splitIntoPieces() {
94   size_t off = 0;
95   StringRef s = toStringRef(data);
96   while (!s.empty()) {
97     size_t end = s.find(0);
98     if (end == StringRef::npos)
99       fatal(toString(this) + ": string is not null terminated");
100     size_t size = end + 1;
101     pieces.emplace_back(off, xxHash64(s.substr(0, size)));
102     s = s.substr(size);
103     off += size;
104   }
105 }
106 
107 StringPiece &CStringInputSection::getStringPiece(uint64_t off) {
108   if (off >= data.size())
109     fatal(toString(this) + ": offset is outside the section");
110 
111   auto it =
112       partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; });
113   return it[-1];
114 }
115 
116 const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const {
117   return const_cast<CStringInputSection *>(this)->getStringPiece(off);
118 }
119 
120 uint64_t CStringInputSection::getOffset(uint64_t off) const {
121   const StringPiece &piece = getStringPiece(off);
122   uint64_t addend = off - piece.inSecOff;
123   return piece.outSecOff + addend;
124 }
125 
126 WordLiteralInputSection::WordLiteralInputSection(StringRef segname,
127                                                  StringRef name,
128                                                  InputFile *file,
129                                                  ArrayRef<uint8_t> data,
130                                                  uint32_t align, uint32_t flags)
131     : InputSection(WordLiteralKind, segname, name, file, data, align, flags) {
132   switch (sectionType(flags)) {
133   case S_4BYTE_LITERALS:
134     power2LiteralSize = 2;
135     break;
136   case S_8BYTE_LITERALS:
137     power2LiteralSize = 3;
138     break;
139   case S_16BYTE_LITERALS:
140     power2LiteralSize = 4;
141     break;
142   default:
143     llvm_unreachable("invalid literal section type");
144   }
145 
146   live.resize(data.size() >> power2LiteralSize, !config->deadStrip);
147 }
148 
149 uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
150   auto *osec = cast<WordLiteralSection>(parent);
151   const uint8_t *buf = data.data();
152   switch (sectionType(flags)) {
153   case S_4BYTE_LITERALS:
154     return osec->getLiteral4Offset(buf + off);
155   case S_8BYTE_LITERALS:
156     return osec->getLiteral8Offset(buf + off);
157   case S_16BYTE_LITERALS:
158     return osec->getLiteral16Offset(buf + off);
159   default:
160     llvm_unreachable("invalid literal section type");
161   }
162 }
163 
164 bool macho::isCodeSection(const InputSection *isec) {
165   uint32_t type = sectionType(isec->flags);
166   if (type != S_REGULAR && type != S_COALESCED)
167     return false;
168 
169   uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR;
170   if (attr == S_ATTR_PURE_INSTRUCTIONS)
171     return true;
172 
173   if (isec->segname == segment_names::text)
174     return StringSwitch<bool>(isec->name)
175         .Cases(section_names::textCoalNt, section_names::staticInit, true)
176         .Default(false);
177 
178   return false;
179 }
180 
181 std::string lld::toString(const InputSection *isec) {
182   return (toString(isec->file) + ":(" + isec->name + ")").str();
183 }
184