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