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