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<InputSection *> macho::inputSections; 29 30 uint64_t InputSection::getFileSize() const { 31 return isZeroFill(flags) ? 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(bool isText) const { 52 if (shouldOmitFromOutput()) 53 return false; 54 switch (sectionType(flags)) { 55 case S_REGULAR: 56 if (isText) 57 return !hasPersonality; 58 // One might hope that we could hash __TEXT,__const subsections to fold 59 // references to duplicated values, but alas, many tests fail. 60 return false; 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 numRefs += copy->numRefs; 102 copy->numRefs = 0; 103 copy->replacement = this; 104 } 105 106 void ConcatInputSection::writeTo(uint8_t *buf) { 107 assert(!shouldOmitFromOutput()); 108 109 if (getFileSize() == 0) 110 return; 111 112 memcpy(buf, data.data(), data.size()); 113 114 for (size_t i = 0; i < relocs.size(); i++) { 115 const Reloc &r = relocs[i]; 116 uint8_t *loc = buf + r.offset; 117 uint64_t referentVA = 0; 118 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) { 119 const Symbol *fromSym = r.referent.get<Symbol *>(); 120 const Reloc &minuend = relocs[++i]; 121 uint64_t minuendVA; 122 if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>()) 123 minuendVA = toSym->getVA() + minuend.addend; 124 else { 125 auto *referentIsec = minuend.referent.get<InputSection *>(); 126 assert(!::shouldOmitFromOutput(referentIsec)); 127 minuendVA = referentIsec->getVA(minuend.addend); 128 } 129 referentVA = minuendVA - fromSym->getVA(); 130 } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) { 131 if (target->hasAttr(r.type, RelocAttrBits::LOAD) && 132 !referentSym->isInGot()) 133 target->relaxGotLoad(loc, r.type); 134 referentVA = resolveSymbolVA(referentSym, r.type) + r.addend; 135 136 if (isThreadLocalVariables(flags)) { 137 // References from thread-local variable sections are treated as offsets 138 // relative to the start of the thread-local data memory area, which 139 // is initialized via copying all the TLV data sections (which are all 140 // contiguous). 141 if (isa<Defined>(referentSym)) 142 referentVA -= firstTLVDataSection->addr; 143 } 144 } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) { 145 assert(!::shouldOmitFromOutput(referentIsec)); 146 referentVA = referentIsec->getVA(r.addend); 147 } 148 target->relocateOne(loc, r, referentVA, getVA() + r.offset); 149 } 150 } 151 152 void CStringInputSection::splitIntoPieces() { 153 size_t off = 0; 154 StringRef s = toStringRef(data); 155 while (!s.empty()) { 156 size_t end = s.find(0); 157 if (end == StringRef::npos) 158 fatal(toString(this) + ": string is not null terminated"); 159 size_t size = end + 1; 160 uint32_t hash = config->dedupLiterals ? xxHash64(s.substr(0, size)) : 0; 161 pieces.emplace_back(off, hash); 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