Mercurial > hg > CbC > CbC_llvm
comparison lld/MachO/InputSection.cpp @ 207:2e18cbf3894f
LLVM12
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 08 Jun 2021 06:07:14 +0900 |
parents | 0572611fdcc8 |
children | 5f17cb93ff66 |
comparison
equal
deleted
inserted
replaced
173:0572611fdcc8 | 207:2e18cbf3894f |
---|---|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 // | 6 // |
7 //===----------------------------------------------------------------------===// | 7 //===----------------------------------------------------------------------===// |
8 | 8 |
9 #include "InputSection.h" | 9 #include "InputSection.h" |
10 #include "InputFiles.h" | |
10 #include "OutputSegment.h" | 11 #include "OutputSegment.h" |
11 #include "Symbols.h" | 12 #include "Symbols.h" |
13 #include "SyntheticSections.h" | |
12 #include "Target.h" | 14 #include "Target.h" |
15 #include "Writer.h" | |
13 #include "lld/Common/Memory.h" | 16 #include "lld/Common/Memory.h" |
14 #include "llvm/Support/Endian.h" | 17 #include "llvm/Support/Endian.h" |
15 | 18 |
19 using namespace llvm; | |
16 using namespace llvm::MachO; | 20 using namespace llvm::MachO; |
17 using namespace llvm::support; | 21 using namespace llvm::support; |
18 using namespace lld; | 22 using namespace lld; |
19 using namespace lld::macho; | 23 using namespace lld::macho; |
20 | 24 |
22 | 26 |
23 uint64_t InputSection::getFileOffset() const { | 27 uint64_t InputSection::getFileOffset() const { |
24 return parent->fileOff + outSecFileOff; | 28 return parent->fileOff + outSecFileOff; |
25 } | 29 } |
26 | 30 |
31 uint64_t InputSection::getFileSize() const { | |
32 return isZeroFill(flags) ? 0 : getSize(); | |
33 } | |
34 | |
27 uint64_t InputSection::getVA() const { return parent->addr + outSecOff; } | 35 uint64_t InputSection::getVA() const { return parent->addr + outSecOff; } |
28 | 36 |
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 | |
29 void InputSection::writeTo(uint8_t *buf) { | 48 void InputSection::writeTo(uint8_t *buf) { |
30 if (!data.empty()) | 49 assert(!shouldOmitFromOutput()); |
31 memcpy(buf, data.data(), data.size()); | |
32 | 50 |
33 for (Reloc &r : relocs) { | 51 if (getFileSize() == 0) |
34 uint64_t va = 0; | 52 return; |
35 if (auto *s = r.target.dyn_cast<Symbol *>()) { | 53 |
36 if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s)) { | 54 memcpy(buf, data.data(), data.size()); |
37 va = target->getDylibSymbolVA(*dylibSymbol, r.type); | 55 |
38 } else { | 56 for (size_t i = 0; i < relocs.size(); i++) { |
39 va = s->getVA(); | 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(); | |
40 } | 70 } |
41 } else if (auto *isec = r.target.dyn_cast<InputSection *>()) { | 71 referentVA = minuendVA - fromSym->getVA() + minuend.addend; |
42 va = isec->getVA(); | 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); | |
77 | |
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(); | |
43 } | 89 } |
44 | 90 target->relocateOne(loc, r, referentVA + r.addend, getVA() + r.offset); |
45 uint64_t val = va + r.addend; | |
46 if (r.pcrel) | |
47 val -= getVA() + r.offset; | |
48 target->relocateOne(buf + r.offset, r.type, val); | |
49 } | 91 } |
50 } | 92 } |
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 } |