annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- InputSection.cpp ---------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "InputSection.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
10 #include "InputFiles.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "OutputSegment.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "Symbols.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
13 #include "SyntheticSections.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "Target.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
15 #include "Writer.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "lld/Common/Memory.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/Support/Endian.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
19 using namespace llvm;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 using namespace llvm::MachO;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 using namespace llvm::support;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 using namespace lld;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 using namespace lld::macho;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 std::vector<InputSection *> macho::inputSections;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 uint64_t InputSection::getFileOffset() const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 return parent->fileOff + outSecFileOff;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
31 uint64_t InputSection::getFileSize() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
32 return isZeroFill(flags) ? 0 : getSize();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
33 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
34
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 uint64_t InputSection::getVA() const { return parent->addr + outSecOff; }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
37 static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
38 const RelocAttrs &relocAttrs = target->getRelocAttrs(type);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
39 if (relocAttrs.hasAttr(RelocAttrBits::BRANCH))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
40 return sym->resolveBranchVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
41 else if (relocAttrs.hasAttr(RelocAttrBits::GOT))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
42 return sym->resolveGotVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
43 else if (relocAttrs.hasAttr(RelocAttrBits::TLV))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
44 return sym->resolveTlvVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
45 return sym->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
46 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
47
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 void InputSection::writeTo(uint8_t *buf) {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
49 assert(!shouldOmitFromOutput());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
50
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
51 if (getFileSize() == 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
52 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
53
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 memcpy(buf, data.data(), data.size());
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
56 for (size_t i = 0; i < relocs.size(); i++) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
57 const Reloc &r = relocs[i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
58 uint8_t *loc = buf + r.offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
59 uint64_t referentVA = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
60 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
61 const Symbol *fromSym = r.referent.get<Symbol *>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
62 const Reloc &minuend = relocs[++i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
63 uint64_t minuendVA;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
64 if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
65 minuendVA = toSym->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
66 else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
67 auto *referentIsec = minuend.referent.get<InputSection *>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
68 assert(!referentIsec->shouldOmitFromOutput());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
69 minuendVA = referentIsec->getVA();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
71 referentVA = minuendVA - fromSym->getVA() + minuend.addend;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
72 } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
73 if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
74 !referentSym->isInGot())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
75 target->relaxGotLoad(loc, r.type);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
76 referentVA = resolveSymbolVA(referentSym, r.type);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
78 if (isThreadLocalVariables(flags)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
79 // References from thread-local variable sections are treated as offsets
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
80 // relative to the start of the thread-local data memory area, which
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
81 // is initialized via copying all the TLV data sections (which are all
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
82 // contiguous).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
83 if (isa<Defined>(referentSym))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
84 referentVA -= firstTLVDataSection->addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
85 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
86 } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
87 assert(!referentIsec->shouldOmitFromOutput());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
88 referentVA = referentIsec->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
89 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
90 target->relocateOne(loc, r, referentVA + r.addend, getVA() + r.offset);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
93
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
94 bool macho::isCodeSection(const InputSection *isec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
95 uint32_t type = isec->flags & SECTION_TYPE;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
96 if (type != S_REGULAR && type != S_COALESCED)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
97 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
98
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
99 uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
100 if (attr == S_ATTR_PURE_INSTRUCTIONS)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
101 return true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
102
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
103 if (isec->segname == segment_names::text)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
104 return StringSwitch<bool>(isec->name)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
105 .Cases(section_names::textCoalNt, section_names::staticInit, true)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
106 .Default(false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
107
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
108 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
109 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
110
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
111 std::string lld::toString(const InputSection *isec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
112 return (toString(isec->file) + ":(" + isec->name + ")").str();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
113 }