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         minuendVA = referentIsec->getVA(minuend.addend);
68       }
69       referentVA = minuendVA - fromSym->getVA();
70     } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
71       if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
72           !referentSym->isInGot())
73         target->relaxGotLoad(loc, r.type);
74       referentVA = resolveSymbolVA(referentSym, r.type) + r.addend;
75 
76       if (isThreadLocalVariables(flags)) {
77         // References from thread-local variable sections are treated as offsets
78         // relative to the start of the thread-local data memory area, which
79         // is initialized via copying all the TLV data sections (which are all
80         // contiguous).
81         if (isa<Defined>(referentSym))
82           referentVA -= firstTLVDataSection->addr;
83       }
84     } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
85       referentVA = referentIsec->getVA(r.addend);
86     }
87     target->relocateOne(loc, r, referentVA, getVA() + r.offset);
88   }
89 }
90 
91 void CStringInputSection::splitIntoPieces() {
92   size_t off = 0;
93   StringRef s = toStringRef(data);
94   while (!s.empty()) {
95     size_t end = s.find(0);
96     if (end == StringRef::npos)
97       fatal(toString(this) + ": string is not null terminated");
98     size_t size = end + 1;
99     pieces.emplace_back(off, xxHash64(s.substr(0, size)));
100     s = s.substr(size);
101     off += size;
102   }
103 }
104 
105 StringPiece &CStringInputSection::getStringPiece(uint64_t off) {
106   if (off >= data.size())
107     fatal(toString(this) + ": offset is outside the section");
108 
109   auto it =
110       partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; });
111   return it[-1];
112 }
113 
114 const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const {
115   return const_cast<CStringInputSection *>(this)->getStringPiece(off);
116 }
117 
118 uint64_t CStringInputSection::getOffset(uint64_t off) const {
119   const StringPiece &piece = getStringPiece(off);
120   uint64_t addend = off - piece.inSecOff;
121   return piece.outSecOff + addend;
122 }
123 
124 WordLiteralInputSection::WordLiteralInputSection(StringRef segname,
125                                                  StringRef name,
126                                                  InputFile *file,
127                                                  ArrayRef<uint8_t> data,
128                                                  uint32_t align, uint32_t flags)
129     : InputSection(WordLiteralKind, segname, name, file, data, align, flags) {
130   switch (sectionType(flags)) {
131   case S_4BYTE_LITERALS:
132     power2LiteralSize = 2;
133     break;
134   case S_8BYTE_LITERALS:
135     power2LiteralSize = 3;
136     break;
137   case S_16BYTE_LITERALS:
138     power2LiteralSize = 4;
139     break;
140   default:
141     llvm_unreachable("invalid literal section type");
142   }
143 
144   live.resize(data.size() >> power2LiteralSize, !config->deadStrip);
145 }
146 
147 uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
148   auto *osec = cast<WordLiteralSection>(parent);
149   const uint8_t *buf = data.data();
150   switch (sectionType(flags)) {
151   case S_4BYTE_LITERALS:
152     return osec->getLiteral4Offset(buf + off);
153   case S_8BYTE_LITERALS:
154     return osec->getLiteral8Offset(buf + off);
155   case S_16BYTE_LITERALS:
156     return osec->getLiteral16Offset(buf + off);
157   default:
158     llvm_unreachable("invalid literal section type");
159   }
160 }
161 
162 bool macho::isCodeSection(const InputSection *isec) {
163   uint32_t type = sectionType(isec->flags);
164   if (type != S_REGULAR && type != S_COALESCED)
165     return false;
166 
167   uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR;
168   if (attr == S_ATTR_PURE_INSTRUCTIONS)
169     return true;
170 
171   if (isec->segname == segment_names::text)
172     return StringSwitch<bool>(isec->name)
173         .Cases(section_names::textCoalNt, section_names::staticInit, true)
174         .Default(false);
175 
176   return false;
177 }
178 
179 std::string lld::toString(const InputSection *isec) {
180   return (toString(isec->file) + ":(" + isec->name + ")").str();
181 }
182