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 "UnwindInfoSection.h" 16 #include "Writer.h" 17 #include "lld/Common/Memory.h" 18 #include "llvm/Support/Endian.h" 19 #include "llvm/Support/xxhash.h" 20 21 using namespace llvm; 22 using namespace llvm::MachO; 23 using namespace llvm::support; 24 using namespace lld; 25 using namespace lld::macho; 26 27 std::vector<InputSection *> macho::inputSections; 28 29 uint64_t InputSection::getFileSize() const { 30 return isZeroFill(flags) ? 0 : getSize(); 31 } 32 33 uint64_t InputSection::getVA(uint64_t off) const { 34 return parent->addr + getOffset(off); 35 } 36 37 static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) { 38 const RelocAttrs &relocAttrs = target->getRelocAttrs(type); 39 if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) 40 return sym->resolveBranchVA(); 41 else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) 42 return sym->resolveGotVA(); 43 else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) 44 return sym->resolveTlvVA(); 45 return sym->getVA(); 46 } 47 48 // ICF needs to hash any section that might potentially be duplicated so 49 // that it can match on content rather than identity. 50 bool ConcatInputSection::isHashableForICF(bool isText) const { 51 if (shouldOmitFromOutput()) 52 return false; 53 switch (sectionType(flags)) { 54 case S_REGULAR: 55 if (isText) 56 return !hasPersonality; 57 // One might hope that we could hash __TEXT,__const subsections to fold 58 // references to duplicated values, but alas, many tests fail. 59 return false; 60 case S_CSTRING_LITERALS: 61 case S_4BYTE_LITERALS: 62 case S_8BYTE_LITERALS: 63 case S_16BYTE_LITERALS: 64 case S_LITERAL_POINTERS: 65 // FIXME(jezng): We should not have any ConcatInputSections of these types 66 // when running ICF. 67 return false; 68 case S_ZEROFILL: 69 case S_GB_ZEROFILL: 70 case S_NON_LAZY_SYMBOL_POINTERS: 71 case S_LAZY_SYMBOL_POINTERS: 72 case S_SYMBOL_STUBS: 73 case S_MOD_INIT_FUNC_POINTERS: 74 case S_MOD_TERM_FUNC_POINTERS: 75 case S_COALESCED: 76 case S_INTERPOSING: 77 case S_DTRACE_DOF: 78 case S_LAZY_DYLIB_SYMBOL_POINTERS: 79 case S_THREAD_LOCAL_REGULAR: 80 case S_THREAD_LOCAL_ZEROFILL: 81 case S_THREAD_LOCAL_VARIABLES: 82 case S_THREAD_LOCAL_VARIABLE_POINTERS: 83 case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS: 84 return false; 85 default: 86 llvm_unreachable("Section type"); 87 } 88 } 89 90 void ConcatInputSection::hashForICF() { 91 assert(data.data()); // zeroFill section data has nullptr with non-zero size 92 assert(icfEqClass[0] == 0); // don't overwrite a unique ID! 93 // Turn-on the top bit to guarantee that valid hashes have no collisions 94 // with the small-integer unique IDs for ICF-ineligible sections 95 icfEqClass[0] = xxHash64(data) | (1ull << 63); 96 } 97 98 void ConcatInputSection::foldIdentical(ConcatInputSection *copy) { 99 align = std::max(align, copy->align); 100 copy->live = false; 101 copy->wasCoalesced = true; 102 numRefs += copy->numRefs; 103 copy->numRefs = 0; 104 copy->replacement = this; 105 } 106 107 void ConcatInputSection::writeTo(uint8_t *buf) { 108 assert(!shouldOmitFromOutput()); 109 110 if (getFileSize() == 0) 111 return; 112 113 memcpy(buf, data.data(), data.size()); 114 115 for (size_t i = 0; i < relocs.size(); i++) { 116 const Reloc &r = relocs[i]; 117 uint8_t *loc = buf + r.offset; 118 uint64_t referentVA = 0; 119 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) { 120 const Symbol *fromSym = r.referent.get<Symbol *>(); 121 const Reloc &minuend = relocs[++i]; 122 uint64_t minuendVA; 123 if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>()) 124 minuendVA = toSym->getVA() + minuend.addend; 125 else { 126 auto *referentIsec = minuend.referent.get<InputSection *>(); 127 assert(!::shouldOmitFromOutput(referentIsec)); 128 minuendVA = referentIsec->getVA(minuend.addend); 129 } 130 referentVA = minuendVA - fromSym->getVA(); 131 } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) { 132 if (target->hasAttr(r.type, RelocAttrBits::LOAD) && 133 !referentSym->isInGot()) 134 target->relaxGotLoad(loc, r.type); 135 referentVA = resolveSymbolVA(referentSym, r.type) + r.addend; 136 137 if (isThreadLocalVariables(flags)) { 138 // References from thread-local variable sections are treated as offsets 139 // relative to the start of the thread-local data memory area, which 140 // is initialized via copying all the TLV data sections (which are all 141 // contiguous). 142 if (isa<Defined>(referentSym)) 143 referentVA -= firstTLVDataSection->addr; 144 } 145 } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) { 146 assert(!::shouldOmitFromOutput(referentIsec)); 147 referentVA = referentIsec->getVA(r.addend); 148 } 149 target->relocateOne(loc, r, referentVA, getVA() + r.offset); 150 } 151 } 152 153 void CStringInputSection::splitIntoPieces() { 154 size_t off = 0; 155 StringRef s = toStringRef(data); 156 while (!s.empty()) { 157 size_t end = s.find(0); 158 if (end == StringRef::npos) 159 fatal(toString(this) + ": string is not null terminated"); 160 size_t size = end + 1; 161 pieces.emplace_back(off, xxHash64(s.substr(0, size))); 162 s = s.substr(size); 163 off += size; 164 } 165 } 166 167 StringPiece &CStringInputSection::getStringPiece(uint64_t off) { 168 if (off >= data.size()) 169 fatal(toString(this) + ": offset is outside the section"); 170 171 auto it = 172 partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; }); 173 return it[-1]; 174 } 175 176 const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const { 177 return const_cast<CStringInputSection *>(this)->getStringPiece(off); 178 } 179 180 uint64_t CStringInputSection::getOffset(uint64_t off) const { 181 const StringPiece &piece = getStringPiece(off); 182 uint64_t addend = off - piece.inSecOff; 183 return piece.outSecOff + addend; 184 } 185 186 WordLiteralInputSection::WordLiteralInputSection(StringRef segname, 187 StringRef name, 188 InputFile *file, 189 ArrayRef<uint8_t> data, 190 uint32_t align, uint32_t flags) 191 : InputSection(WordLiteralKind, segname, name, file, data, align, flags) { 192 switch (sectionType(flags)) { 193 case S_4BYTE_LITERALS: 194 power2LiteralSize = 2; 195 break; 196 case S_8BYTE_LITERALS: 197 power2LiteralSize = 3; 198 break; 199 case S_16BYTE_LITERALS: 200 power2LiteralSize = 4; 201 break; 202 default: 203 llvm_unreachable("invalid literal section type"); 204 } 205 206 live.resize(data.size() >> power2LiteralSize, !config->deadStrip); 207 } 208 209 uint64_t WordLiteralInputSection::getOffset(uint64_t off) const { 210 auto *osec = cast<WordLiteralSection>(parent); 211 const uint8_t *buf = data.data(); 212 switch (sectionType(flags)) { 213 case S_4BYTE_LITERALS: 214 return osec->getLiteral4Offset(buf + off); 215 case S_8BYTE_LITERALS: 216 return osec->getLiteral8Offset(buf + off); 217 case S_16BYTE_LITERALS: 218 return osec->getLiteral16Offset(buf + off); 219 default: 220 llvm_unreachable("invalid literal section type"); 221 } 222 } 223 224 bool macho::isCodeSection(const InputSection *isec) { 225 uint32_t type = sectionType(isec->flags); 226 if (type != S_REGULAR && type != S_COALESCED) 227 return false; 228 229 uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR; 230 if (attr == S_ATTR_PURE_INSTRUCTIONS) 231 return true; 232 233 if (isec->segname == segment_names::text) 234 return StringSwitch<bool>(isec->name) 235 .Cases(section_names::textCoalNt, section_names::staticInit, true) 236 .Default(false); 237 238 return false; 239 } 240 241 std::string lld::toString(const InputSection *isec) { 242 return (toString(isec->file) + ":(" + isec->name + ")").str(); 243 } 244