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 "Writer.h"
16 #include "lld/Common/Memory.h"
17 #include "llvm/Support/Endian.h"
18 
19 using namespace llvm;
20 using namespace llvm::MachO;
21 using namespace llvm::support;
22 using namespace lld;
23 using namespace lld::macho;
24 
25 std::vector<InputSection *> macho::inputSections;
26 
27 uint64_t InputSection::getFileOffset() const {
28   return parent->fileOff + outSecFileOff;
29 }
30 
31 uint64_t InputSection::getFileSize() const {
32   return isZeroFill(flags) ? 0 : getSize();
33 }
34 
35 uint64_t InputSection::getVA() const { return parent->addr + outSecOff; }
36 
37 static uint64_t resolveSymbolVA(uint8_t *loc, const lld::macho::Symbol &sym,
38                                 uint8_t type) {
39   const TargetInfo::RelocAttrs &relocAttrs = target->getRelocAttrs(type);
40   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
41     if (sym.isInStubs())
42       return in.stubs->addr + sym.stubsIndex * target->stubSize;
43   } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) {
44     if (sym.isInGot())
45       return in.got->addr + sym.gotIndex * WordSize;
46   } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) {
47     if (sym.isInGot())
48       return in.tlvPointers->addr + sym.gotIndex * WordSize;
49     assert(isa<Defined>(&sym));
50   }
51   return sym.getVA();
52 }
53 
54 void InputSection::writeTo(uint8_t *buf) {
55   if (getFileSize() == 0)
56     return;
57 
58   memcpy(buf, data.data(), data.size());
59 
60   for (size_t i = 0; i < relocs.size(); i++) {
61     const Symbol *fromSym =
62         target->hasAttr(relocs[i].type, RelocAttrBits::SUBTRAHEND)
63             ? relocs[i++].referent.get<Symbol *>()
64             : nullptr;
65     const Reloc &r = relocs[i];
66     uint8_t *loc = buf + r.offset;
67     uint64_t referentVA = 0;
68     if (fromSym) {
69       const Symbol *toSym = r.referent.get<Symbol *>();
70       referentVA = toSym->getVA() - fromSym->getVA();
71     } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
72       if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
73           !referentSym->isInGot())
74         target->relaxGotLoad(loc, r.type);
75       referentVA = resolveSymbolVA(loc, *referentSym, r.type);
76 
77       if (isThreadLocalVariables(flags)) {
78         // References from thread-local variable sections are treated as offsets
79         // relative to the start of the thread-local data memory area, which
80         // is initialized via copying all the TLV data sections (which are all
81         // contiguous).
82         if (isa<Defined>(referentSym))
83           referentVA -= firstTLVDataSection->addr;
84       }
85     } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
86       referentVA = referentIsec->getVA();
87     }
88     target->relocateOne(loc, r, referentVA, getVA() + r.offset);
89   }
90 }
91 
92 bool macho::isCodeSection(InputSection *isec) {
93   uint32_t type = isec->flags & MachO::SECTION_TYPE;
94   if (type != S_REGULAR && type != S_COALESCED)
95     return false;
96 
97   uint32_t attr = isec->flags & MachO::SECTION_ATTRIBUTES_USR;
98   if (attr == S_ATTR_PURE_INSTRUCTIONS)
99     return true;
100 
101   if (isec->segname == segment_names::text)
102     return StringSwitch<bool>(isec->name)
103         .Cases("__textcoal_nt", "__StaticInit", true)
104         .Default(false);
105 
106   return false;
107 }
108 
109 std::string lld::toString(const InputSection *isec) {
110   return (toString(isec->file) + ":(" + isec->name + ")").str();
111 }
112