1 //===- InputSection.h -------------------------------------------*- C++ -*-===// 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 #ifndef LLD_MACHO_INPUT_SECTION_H 10 #define LLD_MACHO_INPUT_SECTION_H 11 12 #include "Config.h" 13 #include "Relocations.h" 14 #include "Symbols.h" 15 16 #include "lld/Common/LLVM.h" 17 #include "lld/Common/Memory.h" 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/BitVector.h" 20 #include "llvm/ADT/CachedHashString.h" 21 #include "llvm/ADT/TinyPtrVector.h" 22 #include "llvm/BinaryFormat/MachO.h" 23 24 namespace lld { 25 namespace macho { 26 27 class InputFile; 28 class OutputSection; 29 30 class InputSection { 31 public: 32 enum Kind : uint8_t { 33 ConcatKind, 34 CStringLiteralKind, 35 WordLiteralKind, 36 }; 37 38 Kind kind() const { return sectionKind; } 39 virtual ~InputSection() = default; 40 virtual uint64_t getSize() const { return data.size(); } 41 virtual bool empty() const { return data.empty(); } 42 InputFile *getFile() const { return section.file; } 43 StringRef getName() const { return section.name; } 44 StringRef getSegName() const { return section.segname; } 45 uint32_t getFlags() const { return section.flags; } 46 uint64_t getFileSize() const; 47 // Translates \p off -- an offset relative to this InputSection -- into an 48 // offset from the beginning of its parent OutputSection. 49 virtual uint64_t getOffset(uint64_t off) const = 0; 50 // The offset from the beginning of the file. 51 uint64_t getVA(uint64_t off) const; 52 // Return a user-friendly string for use in diagnostics. 53 std::string getLocation(uint64_t off) const; 54 // Whether the data at \p off in this InputSection is live. 55 virtual bool isLive(uint64_t off) const = 0; 56 virtual void markLive(uint64_t off) = 0; 57 virtual InputSection *canonical() { return this; } 58 virtual const InputSection *canonical() const { return this; } 59 60 protected: 61 InputSection(Kind kind, const Section §ion, ArrayRef<uint8_t> data, 62 uint32_t align) 63 : sectionKind(kind), align(align), data(data), section(section) {} 64 65 InputSection(const InputSection &rhs) 66 : sectionKind(rhs.sectionKind), align(rhs.align), data(rhs.data), 67 section(rhs.section) {} 68 69 Kind sectionKind; 70 71 public: 72 // is address assigned? 73 bool isFinal = false; 74 uint32_t align = 1; 75 76 OutputSection *parent = nullptr; 77 ArrayRef<uint8_t> data; 78 std::vector<Reloc> relocs; 79 // The symbols that belong to this InputSection, sorted by value. With 80 // .subsections_via_symbols, there is typically only one element here. 81 llvm::TinyPtrVector<Defined *> symbols; 82 83 protected: 84 const Section §ion; 85 }; 86 87 // ConcatInputSections are combined into (Concat)OutputSections through simple 88 // concatenation, in contrast with literal sections which may have their 89 // contents merged before output. 90 class ConcatInputSection final : public InputSection { 91 public: 92 ConcatInputSection(const Section §ion, ArrayRef<uint8_t> data, 93 uint32_t align = 1) 94 : InputSection(ConcatKind, section, data, align) {} 95 96 uint64_t getOffset(uint64_t off) const override { return outSecOff + off; } 97 uint64_t getVA() const { return InputSection::getVA(0); } 98 // ConcatInputSections are entirely live or dead, so the offset is irrelevant. 99 bool isLive(uint64_t off) const override { return live; } 100 void markLive(uint64_t off) override { live = true; } 101 bool isCoalescedWeak() const { return wasCoalesced && symbols.empty(); } 102 bool shouldOmitFromOutput() const { return !live || isCoalescedWeak(); } 103 void writeTo(uint8_t *buf); 104 105 void foldIdentical(ConcatInputSection *redundant); 106 ConcatInputSection *canonical() override { 107 return replacement ? replacement : this; 108 } 109 const InputSection *canonical() const override { 110 return replacement ? replacement : this; 111 } 112 113 static bool classof(const InputSection *isec) { 114 return isec->kind() == ConcatKind; 115 } 116 117 // Points to the surviving section after this one is folded by ICF 118 ConcatInputSection *replacement = nullptr; 119 // Equivalence-class ID for ICF 120 uint32_t icfEqClass[2] = {0, 0}; 121 122 // With subsections_via_symbols, most symbols have their own InputSection, 123 // and for weak symbols (e.g. from inline functions), only the 124 // InputSection from one translation unit will make it to the output, 125 // while all copies in other translation units are coalesced into the 126 // first and not copied to the output. 127 bool wasCoalesced = false; 128 bool live = !config->deadStrip; 129 bool hasCallSites = false; 130 // This variable has two usages. Initially, it represents the input order. 131 // After assignAddresses is called, it represents the offset from the 132 // beginning of the output section this section was assigned to. 133 uint64_t outSecOff = 0; 134 }; 135 136 // Initialize a fake InputSection that does not belong to any InputFile. 137 ConcatInputSection *makeSyntheticInputSection(StringRef segName, 138 StringRef sectName, 139 uint32_t flags = 0, 140 ArrayRef<uint8_t> data = {}, 141 uint32_t align = 1); 142 143 // Helper functions to make it easy to sprinkle asserts. 144 145 inline bool shouldOmitFromOutput(InputSection *isec) { 146 return isa<ConcatInputSection>(isec) && 147 cast<ConcatInputSection>(isec)->shouldOmitFromOutput(); 148 } 149 150 inline bool isCoalescedWeak(InputSection *isec) { 151 return isa<ConcatInputSection>(isec) && 152 cast<ConcatInputSection>(isec)->isCoalescedWeak(); 153 } 154 155 // We allocate a lot of these and binary search on them, so they should be as 156 // compact as possible. Hence the use of 31 rather than 64 bits for the hash. 157 struct StringPiece { 158 // Offset from the start of the containing input section. 159 uint32_t inSecOff; 160 uint32_t live : 1; 161 // Only set if deduplicating literals 162 uint32_t hash : 31; 163 // Offset from the start of the containing output section. 164 uint64_t outSecOff = 0; 165 166 StringPiece(uint64_t off, uint32_t hash) 167 : inSecOff(off), live(!config->deadStrip), hash(hash) {} 168 }; 169 170 static_assert(sizeof(StringPiece) == 16, "StringPiece is too big!"); 171 172 // CStringInputSections are composed of multiple null-terminated string 173 // literals, which we represent using StringPieces. These literals can be 174 // deduplicated and tail-merged, so translating offsets between the input and 175 // outputs sections is more complicated. 176 // 177 // NOTE: One significant difference between LLD and ld64 is that we merge all 178 // cstring literals, even those referenced directly by non-private symbols. 179 // ld64 is more conservative and does not do that. This was mostly done for 180 // implementation simplicity; if we find programs that need the more 181 // conservative behavior we can certainly implement that. 182 class CStringInputSection final : public InputSection { 183 public: 184 CStringInputSection(const Section §ion, ArrayRef<uint8_t> data, 185 uint32_t align) 186 : InputSection(CStringLiteralKind, section, data, align) {} 187 uint64_t getOffset(uint64_t off) const override; 188 bool isLive(uint64_t off) const override { return getStringPiece(off).live; } 189 void markLive(uint64_t off) override { getStringPiece(off).live = true; } 190 // Find the StringPiece that contains this offset. 191 StringPiece &getStringPiece(uint64_t off); 192 const StringPiece &getStringPiece(uint64_t off) const; 193 // Split at each null byte. 194 void splitIntoPieces(); 195 196 LLVM_ATTRIBUTE_ALWAYS_INLINE 197 StringRef getStringRef(size_t i) const { 198 size_t begin = pieces[i].inSecOff; 199 size_t end = 200 (pieces.size() - 1 == i) ? data.size() : pieces[i + 1].inSecOff; 201 return toStringRef(data.slice(begin, end - begin)); 202 } 203 204 // Returns i'th piece as a CachedHashStringRef. This function is very hot when 205 // string merging is enabled, so we want to inline. 206 LLVM_ATTRIBUTE_ALWAYS_INLINE 207 llvm::CachedHashStringRef getCachedHashStringRef(size_t i) const { 208 assert(config->dedupLiterals); 209 return {getStringRef(i), pieces[i].hash}; 210 } 211 212 static bool classof(const InputSection *isec) { 213 return isec->kind() == CStringLiteralKind; 214 } 215 216 std::vector<StringPiece> pieces; 217 }; 218 219 class WordLiteralInputSection final : public InputSection { 220 public: 221 WordLiteralInputSection(const Section §ion, ArrayRef<uint8_t> data, 222 uint32_t align); 223 uint64_t getOffset(uint64_t off) const override; 224 bool isLive(uint64_t off) const override { 225 return live[off >> power2LiteralSize]; 226 } 227 void markLive(uint64_t off) override { 228 live[off >> power2LiteralSize] = true; 229 } 230 231 static bool classof(const InputSection *isec) { 232 return isec->kind() == WordLiteralKind; 233 } 234 235 private: 236 unsigned power2LiteralSize; 237 // The liveness of data[off] is tracked by live[off >> power2LiteralSize]. 238 llvm::BitVector live; 239 }; 240 241 inline uint8_t sectionType(uint32_t flags) { 242 return flags & llvm::MachO::SECTION_TYPE; 243 } 244 245 inline bool isZeroFill(uint32_t flags) { 246 return llvm::MachO::isVirtualSection(sectionType(flags)); 247 } 248 249 inline bool isThreadLocalVariables(uint32_t flags) { 250 return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_VARIABLES; 251 } 252 253 // These sections contain the data for initializing thread-local variables. 254 inline bool isThreadLocalData(uint32_t flags) { 255 return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_REGULAR || 256 sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_ZEROFILL; 257 } 258 259 inline bool isDebugSection(uint32_t flags) { 260 return (flags & llvm::MachO::SECTION_ATTRIBUTES_USR) == 261 llvm::MachO::S_ATTR_DEBUG; 262 } 263 264 inline bool isWordLiteralSection(uint32_t flags) { 265 return sectionType(flags) == llvm::MachO::S_4BYTE_LITERALS || 266 sectionType(flags) == llvm::MachO::S_8BYTE_LITERALS || 267 sectionType(flags) == llvm::MachO::S_16BYTE_LITERALS; 268 } 269 270 bool isCodeSection(const InputSection *); 271 bool isCfStringSection(const InputSection *); 272 bool isClassRefsSection(const InputSection *); 273 274 extern std::vector<ConcatInputSection *> inputSections; 275 276 namespace section_names { 277 278 constexpr const char authGot[] = "__auth_got"; 279 constexpr const char authPtr[] = "__auth_ptr"; 280 constexpr const char binding[] = "__binding"; 281 constexpr const char bitcodeBundle[] = "__bundle"; 282 constexpr const char cString[] = "__cstring"; 283 constexpr const char cfString[] = "__cfstring"; 284 constexpr const char cgProfile[] = "__cg_profile"; 285 constexpr const char codeSignature[] = "__code_signature"; 286 constexpr const char common[] = "__common"; 287 constexpr const char compactUnwind[] = "__compact_unwind"; 288 constexpr const char data[] = "__data"; 289 constexpr const char debugAbbrev[] = "__debug_abbrev"; 290 constexpr const char debugInfo[] = "__debug_info"; 291 constexpr const char debugStr[] = "__debug_str"; 292 constexpr const char ehFrame[] = "__eh_frame"; 293 constexpr const char gccExceptTab[] = "__gcc_except_tab"; 294 constexpr const char export_[] = "__export"; 295 constexpr const char dataInCode[] = "__data_in_code"; 296 constexpr const char functionStarts[] = "__func_starts"; 297 constexpr const char got[] = "__got"; 298 constexpr const char header[] = "__mach_header"; 299 constexpr const char indirectSymbolTable[] = "__ind_sym_tab"; 300 constexpr const char const_[] = "__const"; 301 constexpr const char lazySymbolPtr[] = "__la_symbol_ptr"; 302 constexpr const char lazyBinding[] = "__lazy_binding"; 303 constexpr const char literals[] = "__literals"; 304 constexpr const char moduleInitFunc[] = "__mod_init_func"; 305 constexpr const char moduleTermFunc[] = "__mod_term_func"; 306 constexpr const char nonLazySymbolPtr[] = "__nl_symbol_ptr"; 307 constexpr const char objcCatList[] = "__objc_catlist"; 308 constexpr const char objcClassList[] = "__objc_classlist"; 309 constexpr const char objcClassRefs[] = "__objc_classrefs"; 310 constexpr const char objcConst[] = "__objc_const"; 311 constexpr const char objcImageInfo[] = "__objc_imageinfo"; 312 constexpr const char objcNonLazyCatList[] = "__objc_nlcatlist"; 313 constexpr const char objcNonLazyClassList[] = "__objc_nlclslist"; 314 constexpr const char objcProtoList[] = "__objc_protolist"; 315 constexpr const char pageZero[] = "__pagezero"; 316 constexpr const char pointers[] = "__pointers"; 317 constexpr const char rebase[] = "__rebase"; 318 constexpr const char staticInit[] = "__StaticInit"; 319 constexpr const char stringTable[] = "__string_table"; 320 constexpr const char stubHelper[] = "__stub_helper"; 321 constexpr const char stubs[] = "__stubs"; 322 constexpr const char swift[] = "__swift"; 323 constexpr const char symbolTable[] = "__symbol_table"; 324 constexpr const char textCoalNt[] = "__textcoal_nt"; 325 constexpr const char text[] = "__text"; 326 constexpr const char threadPtrs[] = "__thread_ptrs"; 327 constexpr const char threadVars[] = "__thread_vars"; 328 constexpr const char unwindInfo[] = "__unwind_info"; 329 constexpr const char weakBinding[] = "__weak_binding"; 330 constexpr const char zeroFill[] = "__zerofill"; 331 332 } // namespace section_names 333 334 } // namespace macho 335 336 std::string toString(const macho::InputSection *); 337 338 } // namespace lld 339 340 #endif 341