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 Symbol &sym, uint8_t type) {
38   const RelocAttrs &relocAttrs = target->getRelocAttrs(type);
39   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
40     if (sym.isInStubs())
41       return in.stubs->addr + sym.stubsIndex * target->stubSize;
42   } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) {
43     if (sym.isInGot())
44       return in.got->addr + sym.gotIndex * target->wordSize;
45   } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) {
46     if (sym.isInGot())
47       return in.tlvPointers->addr + sym.gotIndex * target->wordSize;
48     assert(isa<Defined>(&sym));
49   }
50   return sym.getVA();
51 }
52 
53 void InputSection::writeTo(uint8_t *buf) {
54   assert(!shouldOmitFromOutput());
55 
56   if (getFileSize() == 0)
57     return;
58 
59   memcpy(buf, data.data(), data.size());
60 
61   for (size_t i = 0; i < relocs.size(); i++) {
62     const Reloc &r = relocs[i];
63     uint8_t *loc = buf + r.offset;
64     uint64_t referentVA = 0;
65     if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
66       const Symbol *fromSym = r.referent.get<Symbol *>();
67       const Reloc &minuend = relocs[++i];
68       uint64_t minuendVA;
69       if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
70         minuendVA = toSym->getVA();
71       else {
72         auto *referentIsec = minuend.referent.get<InputSection *>();
73         assert(!referentIsec->shouldOmitFromOutput());
74         minuendVA = referentIsec->getVA();
75       }
76       referentVA = minuendVA - fromSym->getVA() + minuend.addend;
77     } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
78       if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
79           !referentSym->isInGot())
80         target->relaxGotLoad(loc, r.type);
81       referentVA = resolveSymbolVA(loc, *referentSym, r.type);
82 
83       if (isThreadLocalVariables(flags)) {
84         // References from thread-local variable sections are treated as offsets
85         // relative to the start of the thread-local data memory area, which
86         // is initialized via copying all the TLV data sections (which are all
87         // contiguous).
88         if (isa<Defined>(referentSym))
89           referentVA -= firstTLVDataSection->addr;
90       }
91     } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
92       assert(!referentIsec->shouldOmitFromOutput());
93       referentVA = referentIsec->getVA();
94     }
95     target->relocateOne(loc, r, referentVA + r.addend, getVA() + r.offset);
96   }
97 }
98 
99 bool macho::isCodeSection(InputSection *isec) {
100   uint32_t type = isec->flags & SECTION_TYPE;
101   if (type != S_REGULAR && type != S_COALESCED)
102     return false;
103 
104   uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR;
105   if (attr == S_ATTR_PURE_INSTRUCTIONS)
106     return true;
107 
108   if (isec->segname == segment_names::text)
109     return StringSwitch<bool>(isec->name)
110         .Cases(section_names::textCoalNt, section_names::staticInit, true)
111         .Default(false);
112 
113   return false;
114 }
115 
116 std::string lld::toString(const InputSection *isec) {
117   return (toString(isec->file) + ":(" + isec->name + ")").str();
118 }
119