annotate lld/MachO/SyntheticSections.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 //===- SyntheticSections.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 "SyntheticSections.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
10 #include "ConcatOutputSection.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "Config.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "ExportTrie.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "InputFiles.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
14 #include "MachOStructs.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "OutputSegment.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "SymbolTable.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "Symbols.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "Writer.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "lld/Common/ErrorHandler.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
21 #include "lld/Common/Memory.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
22 #include "llvm/ADT/STLExtras.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
23 #include "llvm/Config/llvm-config.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "llvm/Support/EndianStream.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
25 #include "llvm/Support/FileSystem.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 #include "llvm/Support/LEB128.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
27 #include "llvm/Support/Path.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
28 #include "llvm/Support/SHA256.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
29
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
30 #if defined(__APPLE__)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
31 #include <sys/mman.h>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
32 #endif
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 #ifdef LLVM_HAVE_LIBXAR
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
35 #include <fcntl.h>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
36 #include <xar/xar.h>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
37 #endif
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 using namespace llvm;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 using namespace llvm::MachO;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 using namespace llvm::support;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 using namespace llvm::support::endian;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
43 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
44 using namespace lld::macho;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
46 InStruct macho::in;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
47 std::vector<SyntheticSection *> macho::syntheticSections;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 SyntheticSection::SyntheticSection(const char *segname, const char *name)
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
50 : OutputSection(SyntheticKind, name), segname(segname) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
51 isec = make<InputSection>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
52 isec->segname = segname;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
53 isec->name = name;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 isec->parent = this;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
55 isec->outSecOff = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
56 syntheticSections.push_back(this);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 // dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 // from the beginning of the file (i.e. the header).
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 MachHeaderSection::MachHeaderSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
62 : SyntheticSection(segment_names::text, section_names::header) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
63 // XXX: This is a hack. (See D97007)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
64 // Setting the index to 1 to pretend that this section is the text
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
65 // section.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
66 index = 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
67 isec->isFinal = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
68 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 void MachHeaderSection::addLoadCommand(LoadCommand *lc) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 loadCommands.push_back(lc);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 sizeOfCmds += lc->getSize();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
75 uint64_t MachHeaderSection::getSize() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
76 uint64_t size = target->headerSize + sizeOfCmds + config->headerPad;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
77 // If we are emitting an encryptable binary, our load commands must have a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
78 // separate (non-encrypted) page to themselves.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
79 if (config->emitEncryptionInfo)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
80 size = alignTo(size, target->getPageSize());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
81 return size;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
82 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
83
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
84 static uint32_t cpuSubtype() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
85 uint32_t subtype = target->cpuSubtype;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
86
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
87 if (config->outputType == MH_EXECUTE && !config->staticLink &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
88 target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
89 config->platform() == PlatformKind::macOS &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
90 config->platformInfo.minimum >= VersionTuple(10, 5))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
91 subtype |= CPU_SUBTYPE_LIB64;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
92
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
93 return subtype;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 void MachHeaderSection::writeTo(uint8_t *buf) const {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
97 auto *hdr = reinterpret_cast<mach_header *>(buf);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
98 hdr->magic = target->magic;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
99 hdr->cputype = target->cpuType;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
100 hdr->cpusubtype = cpuSubtype();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 hdr->filetype = config->outputType;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 hdr->ncmds = loadCommands.size();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 hdr->sizeofcmds = sizeOfCmds;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
104 hdr->flags = MH_DYLDLINK;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
105
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
106 if (config->namespaceKind == NamespaceKind::twolevel)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
107 hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
108
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 if (config->outputType == MH_DYLIB && !config->hasReexports)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 hdr->flags |= MH_NO_REEXPORTED_DYLIBS;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
112 if (config->markDeadStrippableDylib)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
113 hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
114
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
115 if (config->outputType == MH_EXECUTE && config->isPic)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
116 hdr->flags |= MH_PIE;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
117
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
118 if (in.exports->hasWeakSymbol || in.weakBinding->hasNonWeakDefinition())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
119 hdr->flags |= MH_WEAK_DEFINES;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
121 if (in.exports->hasWeakSymbol || in.weakBinding->hasEntry())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
122 hdr->flags |= MH_BINDS_TO_WEAK;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
123
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
124 for (const OutputSegment *seg : outputSegments) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
125 for (const OutputSection *osec : seg->getSections()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
126 if (isThreadLocalVariables(osec->flags)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
127 hdr->flags |= MH_HAS_TLV_DESCRIPTORS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
128 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
129 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
130 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
131 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
132
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
133 uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
134 for (const LoadCommand *lc : loadCommands) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 lc->writeTo(p);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 p += lc->getSize();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 PageZeroSection::PageZeroSection()
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 : SyntheticSection(segment_names::pageZero, section_names::pageZero) {}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
143 RebaseSection::RebaseSection()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
144 : LinkEditSection(segment_names::linkEdit, section_names::rebase) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
145
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
146 namespace {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
147 struct Rebase {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
148 OutputSegment *segment = nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
149 uint64_t offset = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
150 uint64_t consecutiveCount = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
151 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
152 } // namespace
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
154 // Rebase opcodes allow us to describe a contiguous sequence of rebase location
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
155 // using a single DO_REBASE opcode. To take advantage of it, we delay emitting
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
156 // `DO_REBASE` until we have reached the end of a contiguous sequence.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
157 static void encodeDoRebase(Rebase &rebase, raw_svector_ostream &os) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
158 assert(rebase.consecutiveCount != 0);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
159 if (rebase.consecutiveCount <= REBASE_IMMEDIATE_MASK) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160 os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES |
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 rebase.consecutiveCount);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
162 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
163 os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
164 encodeULEB128(rebase.consecutiveCount, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
166 rebase.consecutiveCount = 0;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
169 static void encodeRebase(const OutputSection *osec, uint64_t outSecOff,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
170 Rebase &lastRebase, raw_svector_ostream &os) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
171 OutputSegment *seg = osec->parent;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
172 uint64_t offset = osec->getSegmentOffset() + outSecOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
173 if (lastRebase.segment != seg || lastRebase.offset != offset) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
174 if (lastRebase.consecutiveCount != 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
175 encodeDoRebase(lastRebase, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
176
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
177 if (lastRebase.segment != seg) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
178 os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
179 seg->index);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
180 encodeULEB128(offset, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
181 lastRebase.segment = seg;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
182 lastRebase.offset = offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
183 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
184 assert(lastRebase.offset != offset);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
185 os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
186 encodeULEB128(offset - lastRebase.offset, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
187 lastRebase.offset = offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
188 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
189 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
190 ++lastRebase.consecutiveCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
191 // DO_REBASE causes dyld to both perform the binding and increment the offset
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
192 lastRebase.offset += target->wordSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
193 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
194
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
195 void RebaseSection::finalizeContents() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
196 if (locations.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
197 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
198
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
199 raw_svector_ostream os{contents};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
200 Rebase lastRebase;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
201
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
202 os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
203
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
204 llvm::sort(locations, [](const Location &a, const Location &b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
205 return a.isec->getVA() < b.isec->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
206 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
207 for (const Location &loc : locations)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
208 encodeRebase(loc.isec->parent, loc.isec->outSecOff + loc.offset, lastRebase,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
209 os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
210 if (lastRebase.consecutiveCount != 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
211 encodeDoRebase(lastRebase, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
213 os << static_cast<uint8_t>(REBASE_OPCODE_DONE);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
214 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
216 void RebaseSection::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
217 memcpy(buf, contents.data(), contents.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
218 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
219
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
220 NonLazyPointerSectionBase::NonLazyPointerSectionBase(const char *segname,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
221 const char *name)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
222 : SyntheticSection(segname, name) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
223 align = target->wordSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
224 flags = S_NON_LAZY_SYMBOL_POINTERS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
225 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
226
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
227 void macho::addNonLazyBindingEntries(const Symbol *sym,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
228 const InputSection *isec, uint64_t offset,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
229 int64_t addend) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
230 if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
231 in.binding->addEntry(dysym, isec, offset, addend);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
232 if (dysym->isWeakDef())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
233 in.weakBinding->addEntry(sym, isec, offset, addend);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
234 } else if (const auto *defined = dyn_cast<Defined>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
235 in.rebase->addEntry(isec, offset);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
236 if (defined->isExternalWeakDef())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
237 in.weakBinding->addEntry(sym, isec, offset, addend);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
238 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
239 // Undefined symbols are filtered out in scanRelocations(); we should never
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
240 // get here
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
241 llvm_unreachable("cannot bind to an undefined symbol");
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
245 void NonLazyPointerSectionBase::addEntry(Symbol *sym) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
246 if (entries.insert(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
247 assert(!sym->isInGot());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
248 sym->gotIndex = entries.size() - 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
249
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
250 addNonLazyBindingEntries(sym, isec, sym->gotIndex * target->wordSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
251 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
252 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
253
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
254 void NonLazyPointerSectionBase::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
255 for (size_t i = 0, n = entries.size(); i < n; ++i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
256 if (auto *defined = dyn_cast<Defined>(entries[i]))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
257 write64le(&buf[i * target->wordSize], defined->getVA());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
258 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
259
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 BindingSection::BindingSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
261 : LinkEditSection(segment_names::linkEdit, section_names::binding) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
262
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
263 namespace {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
264 struct Binding {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
265 OutputSegment *segment = nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
266 uint64_t offset = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
267 int64_t addend = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
268 int16_t ordinal = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
269 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
270 } // namespace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
271
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
272 // Encode a sequence of opcodes that tell dyld to write the address of symbol +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
273 // addend at osec->addr + outSecOff.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
274 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
275 // The bind opcode "interpreter" remembers the values of each binding field, so
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
276 // we only need to encode the differences between bindings. Hence the use of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
277 // lastBinding.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
278 static void encodeBinding(const Symbol *sym, const OutputSection *osec,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
279 uint64_t outSecOff, int64_t addend,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
280 bool isWeakBinding, Binding &lastBinding,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
281 raw_svector_ostream &os) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
282 OutputSegment *seg = osec->parent;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
283 uint64_t offset = osec->getSegmentOffset() + outSecOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
284 if (lastBinding.segment != seg) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
285 os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
286 seg->index);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
287 encodeULEB128(offset, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
288 lastBinding.segment = seg;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
289 lastBinding.offset = offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
290 } else if (lastBinding.offset != offset) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
291 os << static_cast<uint8_t>(BIND_OPCODE_ADD_ADDR_ULEB);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
292 encodeULEB128(offset - lastBinding.offset, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
293 lastBinding.offset = offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
294 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
296 if (lastBinding.addend != addend) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
297 os << static_cast<uint8_t>(BIND_OPCODE_SET_ADDEND_SLEB);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
298 encodeSLEB128(addend, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
299 lastBinding.addend = addend;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
300 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
301
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
302 uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
303 if (!isWeakBinding && sym->isWeakRef())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
304 flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
305
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
306 os << flags << sym->getName() << '\0'
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
307 << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
308 << static_cast<uint8_t>(BIND_OPCODE_DO_BIND);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
309 // DO_BIND causes dyld to both perform the binding and increment the offset
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
310 lastBinding.offset += target->wordSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
311 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
312
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
313 // Non-weak bindings need to have their dylib ordinal encoded as well.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
314 static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
315 if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
316 return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
317 assert(dysym.getFile()->isReferenced());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
318 return dysym.getFile()->ordinal;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
319 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
320
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
321 static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
322 if (ordinal <= 0) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
323 os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
324 (ordinal & BIND_IMMEDIATE_MASK));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
325 } else if (ordinal <= BIND_IMMEDIATE_MASK) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
326 os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
327 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
328 os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
329 encodeULEB128(ordinal, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
330 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
331 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
332
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
333 static void encodeWeakOverride(const Defined *defined,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
334 raw_svector_ostream &os) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
335 os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM |
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
336 BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
337 << defined->getName() << '\0';
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
338 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 // Emit bind opcodes, which are a stream of byte-sized opcodes that dyld
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 // interprets to update a record with the following fields:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 // * segment index (of the segment to write the symbol addresses to, typically
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 // the __DATA_CONST segment which contains the GOT)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 // * offset within the segment, indicating the next location to write a binding
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 // * symbol type
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 // * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 // * symbol name
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 // * addend
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 // When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 // a symbol in the GOT, and increments the segment offset to point to the next
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 // entry. It does *not* clear the record state after doing the bind, so
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 // subsequent opcodes only need to encode the differences between bindings.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 void BindingSection::finalizeContents() {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
354 raw_svector_ostream os{contents};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
355 Binding lastBinding;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
357 // Since bindings are delta-encoded, sorting them allows for a more compact
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
358 // result. Note that sorting by address alone ensures that bindings for the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
359 // same segment / section are located together.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
360 llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
361 return a.target.getVA() < b.target.getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
362 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
363 for (const BindingEntry &b : bindings) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
364 int16_t ordinal = ordinalForDylibSymbol(*b.dysym);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
365 if (ordinal != lastBinding.ordinal) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
366 encodeDylibOrdinal(ordinal, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
367 lastBinding.ordinal = ordinal;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
369 encodeBinding(b.dysym, b.target.isec->parent,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
370 b.target.isec->outSecOff + b.target.offset, b.addend,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
371 /*isWeakBinding=*/false, lastBinding, os);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
373 if (!bindings.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
374 os << static_cast<uint8_t>(BIND_OPCODE_DONE);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
376
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 void BindingSection::writeTo(uint8_t *buf) const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 memcpy(buf, contents.data(), contents.size());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
381 WeakBindingSection::WeakBindingSection()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
382 : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
383
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
384 void WeakBindingSection::finalizeContents() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
385 raw_svector_ostream os{contents};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
386 Binding lastBinding;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
387
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
388 for (const Defined *defined : definitions)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
389 encodeWeakOverride(defined, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
390
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
391 // Since bindings are delta-encoded, sorting them allows for a more compact
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
392 // result.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
393 llvm::sort(bindings,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
394 [](const WeakBindingEntry &a, const WeakBindingEntry &b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
395 return a.target.getVA() < b.target.getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
396 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
397 for (const WeakBindingEntry &b : bindings)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
398 encodeBinding(b.symbol, b.target.isec->parent,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
399 b.target.isec->outSecOff + b.target.offset, b.addend,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
400 /*isWeakBinding=*/true, lastBinding, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
401 if (!bindings.empty() || !definitions.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
402 os << static_cast<uint8_t>(BIND_OPCODE_DONE);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
403 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
404
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
405 void WeakBindingSection::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
406 memcpy(buf, contents.data(), contents.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
407 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
408
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 StubsSection::StubsSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
410 : SyntheticSection(segment_names::text, section_names::stubs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
411 flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
412 // The stubs section comprises machine instructions, which are aligned to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
413 // 4 bytes on the archs we care about.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
414 align = 4;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
415 reserved2 = target->stubSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
416 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
417
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
418 uint64_t StubsSection::getSize() const {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 return entries.size() * target->stubSize;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
420 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 void StubsSection::writeTo(uint8_t *buf) const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
423 size_t off = 0;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
424 for (const Symbol *sym : entries) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
425 target->writeStub(buf + off, *sym);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
426 off += target->stubSize;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
428 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
429
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
430 void StubsSection::finalize() { isFinal = true; }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
431
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
432 bool StubsSection::addEntry(Symbol *sym) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
433 bool inserted = entries.insert(sym);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
434 if (inserted)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
435 sym->stubsIndex = entries.size() - 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
436 return inserted;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
437 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
438
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
439 StubHelperSection::StubHelperSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
440 : SyntheticSection(segment_names::text, section_names::stubHelper) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
441 flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
442 align = 4; // This section comprises machine instructions
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
443 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
444
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
445 uint64_t StubHelperSection::getSize() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
446 return target->stubHelperHeaderSize +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
447 in.lazyBinding->getEntries().size() * target->stubHelperEntrySize;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
448 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
449
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
450 bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
451
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 void StubHelperSection::writeTo(uint8_t *buf) const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 target->writeStubHelperHeader(buf);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
454 size_t off = target->stubHelperHeaderSize;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
455 for (const DylibSymbol *sym : in.lazyBinding->getEntries()) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 target->writeStubHelperEntry(buf + off, *sym, addr + off);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 off += target->stubHelperEntrySize;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 void StubHelperSection::setup() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 stubBinder = dyn_cast_or_null<DylibSymbol>(symtab->find("dyld_stub_binder"));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
463 if (stubBinder == nullptr) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
464 error("symbol dyld_stub_binder not found (normally in libSystem.dylib). "
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
465 "Needed to perform lazy binding.");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
466 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
468 stubBinder->reference(RefState::Strong);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
469 in.got->addEntry(stubBinder);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
471 inputSections.push_back(in.imageLoaderCache);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
472 // Since this isn't in the symbol table or in any input file, the noDeadStrip
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
473 // argument doesn't matter. It's kept alive by ImageLoaderCacheSection()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
474 // setting `live` to true on the backing InputSection.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
475 dyldPrivate =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
476 make<Defined>("__dyld_private", nullptr, in.imageLoaderCache, 0, 0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
477 /*isWeakDef=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
478 /*isExternal=*/false, /*isPrivateExtern=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
479 /*isThumb=*/false, /*isReferencedDynamically=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
480 /*noDeadStrip=*/false);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
482
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
483 ImageLoaderCacheSection::ImageLoaderCacheSection() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
484 segname = segment_names::data;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
485 name = section_names::data;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
486 uint8_t *arr = bAlloc.Allocate<uint8_t>(target->wordSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
487 memset(arr, 0, target->wordSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
488 data = {arr, target->wordSize};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
489 align = target->wordSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
490 live = true;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
492
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 LazyPointerSection::LazyPointerSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
494 : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
495 align = target->wordSize;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496 flags = S_LAZY_SYMBOL_POINTERS;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
497 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
498
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
499 uint64_t LazyPointerSection::getSize() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
500 return in.stubs->getEntries().size() * target->wordSize;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
501 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
502
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
503 bool LazyPointerSection::isNeeded() const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
504 return !in.stubs->getEntries().empty();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
506
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
507 void LazyPointerSection::writeTo(uint8_t *buf) const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
508 size_t off = 0;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
509 for (const Symbol *sym : in.stubs->getEntries()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
510 if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
511 if (dysym->hasStubsHelper()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
512 uint64_t stubHelperOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
513 target->stubHelperHeaderSize +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
514 dysym->stubsHelperIndex * target->stubHelperEntrySize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
515 write64le(buf + off, in.stubHelper->addr + stubHelperOffset);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
516 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
517 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
518 write64le(buf + off, sym->getVA());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
519 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
520 off += target->wordSize;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
523
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
524 LazyBindingSection::LazyBindingSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
525 : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {}
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
526
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
527 void LazyBindingSection::finalizeContents() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
528 // TODO: Just precompute output size here instead of writing to a temporary
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
529 // buffer
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
530 for (DylibSymbol *sym : entries)
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 sym->lazyBindOffset = encode(*sym);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
532 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
533
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 void LazyBindingSection::writeTo(uint8_t *buf) const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 memcpy(buf, contents.data(), contents.size());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
537
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
538 void LazyBindingSection::addEntry(DylibSymbol *dysym) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
539 if (entries.insert(dysym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
540 dysym->stubsHelperIndex = entries.size() - 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
541 in.rebase->addEntry(in.lazyPointers->isec,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
542 dysym->stubsIndex * target->wordSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
543 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
544 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
545
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
546 // Unlike the non-lazy binding section, the bind opcodes in this section aren't
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
547 // interpreted all at once. Rather, dyld will start interpreting opcodes at a
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
548 // given offset, typically only binding a single symbol before it finds a
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
549 // BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
550 // we cannot encode just the differences between symbols; we have to emit the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 // complete bind information for each symbol.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
552 uint32_t LazyBindingSection::encode(const DylibSymbol &sym) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
553 uint32_t opstreamOffset = contents.size();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
554 OutputSegment *dataSeg = in.lazyPointers->parent;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
555 os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
556 dataSeg->index);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
557 uint64_t offset = in.lazyPointers->addr - dataSeg->firstSection()->addr +
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
558 sym.stubsIndex * target->wordSize;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
559 encodeULEB128(offset, os);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
560 encodeDylibOrdinal(ordinalForDylibSymbol(sym), os);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
561
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
562 uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
563 if (sym.isWeakRef())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
564 flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
565
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
566 os << flags << sym.getName() << '\0'
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
567 << static_cast<uint8_t>(BIND_OPCODE_DO_BIND)
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
568 << static_cast<uint8_t>(BIND_OPCODE_DONE);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 return opstreamOffset;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
571
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 ExportSection::ExportSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
573 : LinkEditSection(segment_names::linkEdit, section_names::export_) {}
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
574
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
575 void ExportSection::finalizeContents() {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
576 trieBuilder.setImageBase(in.header->addr);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
577 for (const Symbol *sym : symtab->getSymbols()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
578 if (const auto *defined = dyn_cast<Defined>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
579 if (defined->privateExtern || !defined->isLive())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
580 continue;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
581 trieBuilder.addSymbol(*defined);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
582 hasWeakSymbol = hasWeakSymbol || sym->isWeakDef();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
583 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
584 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
585 size = trieBuilder.build();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
586 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
587
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
588 void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
589
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
590 FunctionStartsSection::FunctionStartsSection()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
591 : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
592
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
593 void FunctionStartsSection::finalizeContents() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
594 raw_svector_ostream os{contents};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
595 uint64_t addr = in.header->addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
596 for (const Symbol *sym : symtab->getSymbols()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
597 if (const auto *defined = dyn_cast<Defined>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
598 if (!defined->isec || !isCodeSection(defined->isec) || !defined->isLive())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
599 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
600 // TODO: Add support for thumbs, in that case
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
601 // the lowest bit of nextAddr needs to be set to 1.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
602 uint64_t nextAddr = defined->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
603 uint64_t delta = nextAddr - addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
604 if (delta == 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
605 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
606 encodeULEB128(delta, os);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
607 addr = nextAddr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
608 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
609 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
610 os << '\0';
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
611 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
612
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
613 void FunctionStartsSection::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
614 memcpy(buf, contents.data(), contents.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
615 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
616
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
617 SymtabSection::SymtabSection(StringTableSection &stringTableSection)
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
618 : LinkEditSection(segment_names::linkEdit, section_names::symbolTable),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
619 stringTableSection(stringTableSection) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
621 void SymtabSection::emitBeginSourceStab(DWARFUnit *compileUnit) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
622 StabsEntry stab(N_SO);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
623 SmallString<261> dir(compileUnit->getCompilationDir());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
624 StringRef sep = sys::path::get_separator();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
625 // We don't use `path::append` here because we want an empty `dir` to result
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
626 // in an absolute path. `append` would give us a relative path for that case.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
627 if (!dir.endswith(sep))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
628 dir += sep;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
629 stab.strx = stringTableSection.addString(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
630 saver.save(dir + compileUnit->getUnitDIE().getShortName()));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
631 stabs.emplace_back(std::move(stab));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
632 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
633
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
634 void SymtabSection::emitEndSourceStab() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
635 StabsEntry stab(N_SO);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
636 stab.sect = 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
637 stabs.emplace_back(std::move(stab));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
638 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
639
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
640 void SymtabSection::emitObjectFileStab(ObjFile *file) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
641 StabsEntry stab(N_OSO);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
642 stab.sect = target->cpuSubtype;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
643 SmallString<261> path(!file->archiveName.empty() ? file->archiveName
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
644 : file->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
645 std::error_code ec = sys::fs::make_absolute(path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
646 if (ec)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
647 fatal("failed to get absolute path for " + path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
648
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
649 if (!file->archiveName.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
650 path.append({"(", file->getName(), ")"});
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
651
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
652 stab.strx = stringTableSection.addString(saver.save(path.str()));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
653 stab.desc = 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
654 stab.value = file->modTime;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
655 stabs.emplace_back(std::move(stab));
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
656 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
657
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
658 void SymtabSection::emitEndFunStab(Defined *defined) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
659 StabsEntry stab(N_FUN);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
660 stab.value = defined->size;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
661 stabs.emplace_back(std::move(stab));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
662 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
663
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
664 void SymtabSection::emitStabs() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
665 for (const std::string &s : config->astPaths) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
666 StabsEntry astStab(N_AST);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
667 astStab.strx = stringTableSection.addString(s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
668 stabs.emplace_back(std::move(astStab));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
669 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
670
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
671 std::vector<Defined *> symbolsNeedingStabs;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
672 for (const SymtabEntry &entry :
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
673 concat<SymtabEntry>(localSymbols, externalSymbols)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
674 Symbol *sym = entry.sym;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
675 assert(sym->isLive() &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
676 "dead symbols should not be in localSymbols, externalSymbols");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
677 if (auto *defined = dyn_cast<Defined>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
678 if (defined->isAbsolute())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
679 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
680 InputSection *isec = defined->isec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
681 ObjFile *file = dyn_cast_or_null<ObjFile>(isec->file);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
682 if (!file || !file->compileUnit)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
683 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
684 symbolsNeedingStabs.push_back(defined);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
685 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
686 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
688 llvm::stable_sort(symbolsNeedingStabs, [&](Defined *a, Defined *b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
689 return a->isec->file->id < b->isec->file->id;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
690 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
692 // Emit STABS symbols so that dsymutil and/or the debugger can map address
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
693 // regions in the final binary to the source and object files from which they
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
694 // originated.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
695 InputFile *lastFile = nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
696 for (Defined *defined : symbolsNeedingStabs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
697 InputSection *isec = defined->isec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
698 ObjFile *file = cast<ObjFile>(isec->file);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
699
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
700 if (lastFile == nullptr || lastFile != file) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
701 if (lastFile != nullptr)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
702 emitEndSourceStab();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
703 lastFile = file;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
704
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
705 emitBeginSourceStab(file->compileUnit);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
706 emitObjectFileStab(file);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
707 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
708
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
709 StabsEntry symStab;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
710 symStab.sect = defined->isec->parent->index;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
711 symStab.strx = stringTableSection.addString(defined->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
712 symStab.value = defined->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
714 if (isCodeSection(isec)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
715 symStab.type = N_FUN;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
716 stabs.emplace_back(std::move(symStab));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
717 emitEndFunStab(defined);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
718 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
719 symStab.type = defined->isExternal() ? N_GSYM : N_STSYM;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
720 stabs.emplace_back(std::move(symStab));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
721 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
722 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
724 if (!stabs.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
725 emitEndSourceStab();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
726 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
727
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
728 void SymtabSection::finalizeContents() {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
729 auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
730 uint32_t strx = stringTableSection.addString(sym->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
731 symbols.push_back({sym, strx});
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
732 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
733
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
734 // Local symbols aren't in the SymbolTable, so we walk the list of object
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
735 // files to gather them.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
736 for (const InputFile *file : inputFiles) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
737 if (auto *objFile = dyn_cast<ObjFile>(file)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
738 for (Symbol *sym : objFile->symbols) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
739 if (auto *defined = dyn_cast_or_null<Defined>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
740 if (!defined->isExternal() && defined->isLive()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
741 StringRef name = defined->getName();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
742 if (!name.startswith("l") && !name.startswith("L"))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
743 addSymbol(localSymbols, sym);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
744 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
745 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
746 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
747 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
748 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
750 // __dyld_private is a local symbol too. It's linker-created and doesn't
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
751 // exist in any object file.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
752 if (Defined *dyldPrivate = in.stubHelper->dyldPrivate)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
753 addSymbol(localSymbols, dyldPrivate);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
755 for (Symbol *sym : symtab->getSymbols()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
756 if (!sym->isLive())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
757 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
758 if (auto *defined = dyn_cast<Defined>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
759 if (!defined->includeInSymtab)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
760 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
761 assert(defined->isExternal());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
762 if (defined->privateExtern)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
763 addSymbol(localSymbols, defined);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
764 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
765 addSymbol(externalSymbols, defined);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
766 } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
767 if (dysym->isReferenced())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
768 addSymbol(undefinedSymbols, sym);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
769 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
770 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
771
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
772 emitStabs();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
773 uint32_t symtabIndex = stabs.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
774 for (const SymtabEntry &entry :
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
775 concat<SymtabEntry>(localSymbols, externalSymbols, undefinedSymbols)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
776 entry.sym->symtabIndex = symtabIndex++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
777 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
778 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
779
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
780 uint32_t SymtabSection::getNumSymbols() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
781 return stabs.size() + localSymbols.size() + externalSymbols.size() +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
782 undefinedSymbols.size();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
783 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
784
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
785 // This serves to hide (type-erase) the template parameter from SymtabSection.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
786 template <class LP> class SymtabSectionImpl : public SymtabSection {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
787 public:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
788 SymtabSectionImpl(StringTableSection &stringTableSection)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
789 : SymtabSection(stringTableSection) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
790 uint64_t getRawSize() const override;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
791 void writeTo(uint8_t *buf) const override;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
792 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
793
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
794 template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
795 return getNumSymbols() * sizeof(typename LP::nlist);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
796 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
797
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
798 template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
799 auto *nList = reinterpret_cast<typename LP::nlist *>(buf);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
800 // Emit the stabs entries before the "real" symbols. We cannot emit them
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
801 // after as that would render Symbol::symtabIndex inaccurate.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
802 for (const StabsEntry &entry : stabs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
803 nList->n_strx = entry.strx;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
804 nList->n_type = entry.type;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
805 nList->n_sect = entry.sect;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
806 nList->n_desc = entry.desc;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
807 nList->n_value = entry.value;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
808 ++nList;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
809 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
810
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
811 for (const SymtabEntry &entry : concat<const SymtabEntry>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
812 localSymbols, externalSymbols, undefinedSymbols)) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
813 nList->n_strx = entry.strx;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
814 // TODO populate n_desc with more flags
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
815 if (auto *defined = dyn_cast<Defined>(entry.sym)) {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
816 uint8_t scope = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
817 if (defined->privateExtern) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
818 // Private external -- dylib scoped symbol.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
819 // Promote to non-external at link time.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
820 scope = N_PEXT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
821 } else if (defined->isExternal()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
822 // Normal global symbol.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
823 scope = N_EXT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
824 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
825 // TU-local symbol from localSymbols.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
826 scope = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
827 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
828
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
829 if (defined->isAbsolute()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
830 nList->n_type = scope | N_ABS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
831 nList->n_sect = NO_SECT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
832 nList->n_value = defined->value;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
833 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
834 nList->n_type = scope | N_SECT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
835 nList->n_sect = defined->isec->parent->index;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
836 // For the N_SECT symbol type, n_value is the address of the symbol
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
837 nList->n_value = defined->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
838 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
839 nList->n_desc |= defined->thumb ? N_ARM_THUMB_DEF : 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
840 nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
841 nList->n_desc |=
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
842 defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
843 } else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
844 uint16_t n_desc = nList->n_desc;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
845 int16_t ordinal = ordinalForDylibSymbol(*dysym);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
846 if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
847 SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
848 else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
849 SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
850 else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
851 assert(ordinal > 0);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
852 SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
853 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
854
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
855 nList->n_type = N_EXT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
856 n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
857 n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
858 nList->n_desc = n_desc;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
859 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
860 ++nList;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
861 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
862 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
863
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
864 template <class LP>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
865 SymtabSection *
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
866 macho::makeSymtabSection(StringTableSection &stringTableSection) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
867 return make<SymtabSectionImpl<LP>>(stringTableSection);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
868 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
869
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
870 IndirectSymtabSection::IndirectSymtabSection()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
871 : LinkEditSection(segment_names::linkEdit,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
872 section_names::indirectSymbolTable) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
873
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
874 uint32_t IndirectSymtabSection::getNumSymbols() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
875 return in.got->getEntries().size() + in.tlvPointers->getEntries().size() +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
876 in.stubs->getEntries().size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
877 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
878
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
879 bool IndirectSymtabSection::isNeeded() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
880 return in.got->isNeeded() || in.tlvPointers->isNeeded() ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
881 in.stubs->isNeeded();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
882 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
883
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
884 void IndirectSymtabSection::finalizeContents() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
885 uint32_t off = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
886 in.got->reserved1 = off;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
887 off += in.got->getEntries().size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
888 in.tlvPointers->reserved1 = off;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
889 off += in.tlvPointers->getEntries().size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
890 // There is a 1:1 correspondence between stubs and LazyPointerSection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
891 // entries, so they can share the same sub-array in the table.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
892 in.stubs->reserved1 = in.lazyPointers->reserved1 = off;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
893 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
894
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
895 static uint32_t indirectValue(const Symbol *sym) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
896 return sym->symtabIndex != UINT32_MAX ? sym->symtabIndex
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
897 : INDIRECT_SYMBOL_LOCAL;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
898 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
899
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
900 void IndirectSymtabSection::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
901 uint32_t off = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
902 for (const Symbol *sym : in.got->getEntries()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
903 write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
904 ++off;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
905 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
906 for (const Symbol *sym : in.tlvPointers->getEntries()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
907 write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
908 ++off;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
909 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
910 for (const Symbol *sym : in.stubs->getEntries()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
911 write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
912 ++off;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
913 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
914 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
915
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
916 StringTableSection::StringTableSection()
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
917 : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {}
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
918
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
919 uint32_t StringTableSection::addString(StringRef str) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
920 uint32_t strx = size;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
921 strings.push_back(str); // TODO: consider deduplicating strings
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
922 size += str.size() + 1; // account for null terminator
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
923 return strx;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
924 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
925
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
926 void StringTableSection::writeTo(uint8_t *buf) const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
927 uint32_t off = 0;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
928 for (StringRef str : strings) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
929 memcpy(buf + off, str.data(), str.size());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
930 off += str.size() + 1; // account for null terminator
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
931 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
932 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
933
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
934 static_assert((CodeSignatureSection::blobHeadersSize % 8) == 0, "");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
935 static_assert((CodeSignatureSection::fixedHeadersSize % 8) == 0, "");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
936
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
937 CodeSignatureSection::CodeSignatureSection()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
938 : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
939 align = 16; // required by libstuff
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
940 fileName = config->outputFile;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
941 size_t slashIndex = fileName.rfind("/");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
942 if (slashIndex != std::string::npos)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
943 fileName = fileName.drop_front(slashIndex + 1);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
944 allHeadersSize = alignTo<16>(fixedHeadersSize + fileName.size() + 1);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
945 fileNamePad = allHeadersSize - fixedHeadersSize - fileName.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
946 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
947
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
948 uint32_t CodeSignatureSection::getBlockCount() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
949 return (fileOff + blockSize - 1) / blockSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
950 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
951
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
952 uint64_t CodeSignatureSection::getRawSize() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
953 return allHeadersSize + getBlockCount() * hashSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
954 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
955
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
956 void CodeSignatureSection::writeHashes(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
957 uint8_t *code = buf;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
958 uint8_t *codeEnd = buf + fileOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
959 uint8_t *hashes = codeEnd + allHeadersSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
960 while (code < codeEnd) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
961 StringRef block(reinterpret_cast<char *>(code),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
962 std::min(codeEnd - code, static_cast<ssize_t>(blockSize)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
963 SHA256 hasher;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
964 hasher.update(block);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
965 StringRef hash = hasher.final();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
966 assert(hash.size() == hashSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
967 memcpy(hashes, hash.data(), hashSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
968 code += blockSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
969 hashes += hashSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
970 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
971 #if defined(__APPLE__)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
972 // This is macOS-specific work-around and makes no sense for any
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
973 // other host OS. See https://openradar.appspot.com/FB8914231
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
974 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
975 // The macOS kernel maintains a signature-verification cache to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
976 // quickly validate applications at time of execve(2). The trouble
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
977 // is that for the kernel creates the cache entry at the time of the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
978 // mmap(2) call, before we have a chance to write either the code to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
979 // sign or the signature header+hashes. The fix is to invalidate
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
980 // all cached data associated with the output file, thus discarding
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
981 // the bogus prematurely-cached signature.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
982 msync(buf, fileOff + getSize(), MS_INVALIDATE);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
983 #endif
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
984 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
985
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
986 void CodeSignatureSection::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
987 uint32_t signatureSize = static_cast<uint32_t>(getSize());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
988 auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
989 write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
990 write32be(&superBlob->length, signatureSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
991 write32be(&superBlob->count, 1);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
992 auto *blobIndex = reinterpret_cast<CS_BlobIndex *>(&superBlob[1]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
993 write32be(&blobIndex->type, CSSLOT_CODEDIRECTORY);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
994 write32be(&blobIndex->offset, blobHeadersSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
995 auto *codeDirectory =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
996 reinterpret_cast<CS_CodeDirectory *>(buf + blobHeadersSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
997 write32be(&codeDirectory->magic, CSMAGIC_CODEDIRECTORY);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
998 write32be(&codeDirectory->length, signatureSize - blobHeadersSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
999 write32be(&codeDirectory->version, CS_SUPPORTSEXECSEG);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1000 write32be(&codeDirectory->flags, CS_ADHOC | CS_LINKER_SIGNED);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1001 write32be(&codeDirectory->hashOffset,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1002 sizeof(CS_CodeDirectory) + fileName.size() + fileNamePad);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1003 write32be(&codeDirectory->identOffset, sizeof(CS_CodeDirectory));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1004 codeDirectory->nSpecialSlots = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1005 write32be(&codeDirectory->nCodeSlots, getBlockCount());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1006 write32be(&codeDirectory->codeLimit, fileOff);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1007 codeDirectory->hashSize = static_cast<uint8_t>(hashSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1008 codeDirectory->hashType = kSecCodeSignatureHashSHA256;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1009 codeDirectory->platform = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1010 codeDirectory->pageSize = blockSizeShift;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1011 codeDirectory->spare2 = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1012 codeDirectory->scatterOffset = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1013 codeDirectory->teamOffset = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1014 codeDirectory->spare3 = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1015 codeDirectory->codeLimit64 = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1016 OutputSegment *textSeg = getOrCreateOutputSegment(segment_names::text);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1017 write64be(&codeDirectory->execSegBase, textSeg->fileOff);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1018 write64be(&codeDirectory->execSegLimit, textSeg->fileSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1019 write64be(&codeDirectory->execSegFlags,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1020 config->outputType == MH_EXECUTE ? CS_EXECSEG_MAIN_BINARY : 0);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1021 auto *id = reinterpret_cast<char *>(&codeDirectory[1]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1022 memcpy(id, fileName.begin(), fileName.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1023 memset(id + fileName.size(), 0, fileNamePad);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1024 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1025
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1026 BitcodeBundleSection::BitcodeBundleSection()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1027 : SyntheticSection(segment_names::llvm, section_names::bitcodeBundle) {}
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1028
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1029 class ErrorCodeWrapper {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1030 public:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1031 explicit ErrorCodeWrapper(std::error_code ec) : errorCode(ec.value()) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1032 explicit ErrorCodeWrapper(int ec) : errorCode(ec) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1033 operator int() const { return errorCode; }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1034
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1035 private:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1036 int errorCode;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1037 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1038
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1039 #define CHECK_EC(exp) \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1040 do { \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1041 ErrorCodeWrapper ec(exp); \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1042 if (ec) \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1043 fatal(Twine("operation failed with error code ") + Twine(ec) + ": " + \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1044 #exp); \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1045 } while (0);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1046
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1047 void BitcodeBundleSection::finalize() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1048 #ifdef LLVM_HAVE_LIBXAR
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1049 using namespace llvm::sys::fs;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1050 CHECK_EC(createTemporaryFile("bitcode-bundle", "xar", xarPath));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1051
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1052 xar_t xar(xar_open(xarPath.data(), O_RDWR));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1053 if (!xar)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1054 fatal("failed to open XAR temporary file at " + xarPath);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1055 CHECK_EC(xar_opt_set(xar, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1056 // FIXME: add more data to XAR
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1057 CHECK_EC(xar_close(xar));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1058
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1059 file_size(xarPath, xarSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1060 #endif // defined(LLVM_HAVE_LIBXAR)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1061 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1062
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1063 void BitcodeBundleSection::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1064 using namespace llvm::sys::fs;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1065 file_t handle =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1066 CHECK(openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1067 "failed to open XAR file");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1068 std::error_code ec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1069 mapped_file_region xarMap(handle, mapped_file_region::mapmode::readonly,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1070 xarSize, 0, ec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1071 if (ec)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1072 fatal("failed to map XAR file");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1073 memcpy(buf, xarMap.const_data(), xarSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1074
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1075 closeFile(handle);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1076 remove(xarPath);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1077 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1078
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1079 void macho::createSyntheticSymbols() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1080 auto addHeaderSymbol = [](const char *name) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1081 symtab->addSynthetic(name, in.header->isec, /*value=*/0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1082 /*privateExtern=*/true, /*includeInSymtab=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1083 /*referencedDynamically=*/false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1084 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1085
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1086 switch (config->outputType) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1087 // FIXME: Assign the right address value for these symbols
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1088 // (rather than 0). But we need to do that after assignAddresses().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1089 case MH_EXECUTE:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1090 // If linking PIE, __mh_execute_header is a defined symbol in
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1091 // __TEXT, __text)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1092 // Otherwise, it's an absolute symbol.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1093 if (config->isPic)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1094 symtab->addSynthetic("__mh_execute_header", in.header->isec, /*value=*/0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1095 /*privateExtern=*/false, /*includeInSymtab=*/true,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1096 /*referencedDynamically=*/true);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1097 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1098 symtab->addSynthetic("__mh_execute_header", /*isec=*/nullptr, /*value=*/0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1099 /*privateExtern=*/false, /*includeInSymtab=*/true,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1100 /*referencedDynamically=*/true);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1101 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1102
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1103 // The following symbols are N_SECT symbols, even though the header is not
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1104 // part of any section and that they are private to the bundle/dylib/object
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1105 // they are part of.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1106 case MH_BUNDLE:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1107 addHeaderSymbol("__mh_bundle_header");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1108 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1109 case MH_DYLIB:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1110 addHeaderSymbol("__mh_dylib_header");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1111 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1112 case MH_DYLINKER:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1113 addHeaderSymbol("__mh_dylinker_header");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1114 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1115 case MH_OBJECT:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1116 addHeaderSymbol("__mh_object_header");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1117 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1118 default:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1119 llvm_unreachable("unexpected outputType");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1120 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1121 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1122
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1123 // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1124 // which does e.g. cleanup of static global variables. The ABI document
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1125 // says that the pointer can point to any address in one of the dylib's
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1126 // segments, but in practice ld64 seems to set it to point to the header,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1127 // so that's what's implemented here.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1128 addHeaderSymbol("___dso_handle");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1129 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1130
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1131 template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1132 template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &);