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