annotate lld/MachO/InputFiles.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 //===- InputFiles.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 // This file contains functions to parse Mach-O object files. In this comment,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // we describe the Mach-O file structure and how we parse it.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 // Mach-O is not very different from ELF or COFF. The notion of symbols,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 // sections and relocations exists in Mach-O as it does in ELF and COFF.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 // Perhaps the notion that is new to those who know ELF/COFF is "subsections".
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 // In ELF/COFF, sections are an atomic unit of data copied from input files to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 // output files. When we merge or garbage-collect sections, we treat each
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 // section as an atomic unit. In Mach-O, that's not the case. Sections can
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 // consist of multiple subsections, and subsections are a unit of merging and
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 // garbage-collecting. Therefore, Mach-O's subsections are more similar to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 // ELF/COFF's sections than Mach-O's sections are.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 // A section can have multiple symbols. A symbol that does not have the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 // N_ALT_ENTRY attribute indicates a beginning of a subsection. Therefore, by
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 // definition, a symbol is always present at the beginning of each subsection. A
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 // symbol with N_ALT_ENTRY attribute does not start a new subsection and can
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 // point to a middle of a subsection.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 // The notion of subsections also affects how relocations are represented in
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 // Mach-O. All references within a section need to be explicitly represented as
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 // relocations if they refer to different subsections, because we obviously need
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 // to fix up addresses if subsections are laid out in an output file differently
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 // than they were in object files. To represent that, Mach-O relocations can
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 // refer to an unnamed location via its address. Scattered relocations (those
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 // with the R_SCATTERED bit set) always refer to unnamed locations.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 // Non-scattered relocations refer to an unnamed location if r_extern is not set
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 // and r_symbolnum is zero.
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 // Without the above differences, I think you can use your knowledge about ELF
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 // and COFF for Mach-O.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 //===----------------------------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 #include "InputFiles.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 #include "Config.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
46 #include "Driver.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
47 #include "Dwarf.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 #include "ExportTrie.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 #include "InputSection.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
50 #include "MachOStructs.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
51 #include "ObjC.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 #include "OutputSection.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
53 #include "OutputSegment.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 #include "SymbolTable.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 #include "Symbols.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 #include "Target.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
58 #include "lld/Common/DWARF.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 #include "lld/Common/ErrorHandler.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 #include "lld/Common/Memory.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
61 #include "lld/Common/Reproduce.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
62 #include "llvm/ADT/iterator.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 #include "llvm/BinaryFormat/MachO.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
64 #include "llvm/LTO/LTO.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 #include "llvm/Support/Endian.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 #include "llvm/Support/MemoryBuffer.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 #include "llvm/Support/Path.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
68 #include "llvm/Support/TarWriter.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
69 #include "llvm/TextAPI/Architecture.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
70 #include "llvm/TextAPI/InterfaceFile.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 using namespace llvm;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 using namespace llvm::MachO;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 using namespace llvm::support::endian;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 using namespace llvm::sys;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 using namespace lld;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 using namespace lld::macho;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
79 // Returns "<internal>", "foo.a(bar.o)", or "baz.o".
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
80 std::string lld::toString(const InputFile *f) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
81 if (!f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
82 return "<internal>";
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 // Multiple dylibs can be defined in one .tbd file.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
85 if (auto dylibFile = dyn_cast<DylibFile>(f))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
86 if (f->getName().endswith(".tbd"))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
87 return (f->getName() + "(" + dylibFile->installName + ")").str();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
88
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
89 if (f->archiveName.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
90 return std::string(f->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
91 return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
94 SetVector<InputFile *> macho::inputFiles;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
95 std::unique_ptr<TarWriter> macho::tar;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
96 int InputFile::idCount = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
97
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
98 static VersionTuple decodeVersion(uint32_t version) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
99 unsigned major = version >> 16;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
100 unsigned minor = (version >> 8) & 0xffu;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
101 unsigned subMinor = version & 0xffu;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
102 return VersionTuple(major, minor, subMinor);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
103 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
104
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
105 static std::vector<PlatformInfo> getPlatformInfos(const InputFile *input) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
106 if (!isa<ObjFile>(input) && !isa<DylibFile>(input))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
107 return {};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
108
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
109 const char *hdr = input->mb.getBufferStart();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
110
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
111 std::vector<PlatformInfo> platformInfos;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
112 for (auto *cmd : findCommands<build_version_command>(hdr, LC_BUILD_VERSION)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
113 PlatformInfo info;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
114 info.target.Platform = static_cast<PlatformKind>(cmd->platform);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
115 info.minimum = decodeVersion(cmd->minos);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
116 platformInfos.emplace_back(std::move(info));
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 for (auto *cmd : findCommands<version_min_command>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
119 hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
120 LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
121 PlatformInfo info;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
122 switch (cmd->cmd) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
123 case LC_VERSION_MIN_MACOSX:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
124 info.target.Platform = PlatformKind::macOS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
125 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
126 case LC_VERSION_MIN_IPHONEOS:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
127 info.target.Platform = PlatformKind::iOS;
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 case LC_VERSION_MIN_TVOS:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
130 info.target.Platform = PlatformKind::tvOS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
131 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
132 case LC_VERSION_MIN_WATCHOS:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
133 info.target.Platform = PlatformKind::watchOS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
134 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
135 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
136 info.minimum = decodeVersion(cmd->version);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
137 platformInfos.emplace_back(std::move(info));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
138 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
139
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
140 return platformInfos;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
141 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
143 static PlatformKind removeSimulator(PlatformKind platform) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
144 // Mapping of platform to simulator and vice-versa.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
145 static const std::map<PlatformKind, PlatformKind> platformMap = {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
146 {PlatformKind::iOSSimulator, PlatformKind::iOS},
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
147 {PlatformKind::tvOSSimulator, PlatformKind::tvOS},
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
148 {PlatformKind::watchOSSimulator, PlatformKind::watchOS}};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
149
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
150 auto iter = platformMap.find(platform);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
151 if (iter == platformMap.end())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
152 return platform;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
153 return iter->second;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
154 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
155
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
156 static bool checkCompatibility(const InputFile *input) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
157 std::vector<PlatformInfo> platformInfos = getPlatformInfos(input);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
158 if (platformInfos.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
159 return true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 auto it = find_if(platformInfos, [&](const PlatformInfo &info) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
162 return removeSimulator(info.target.Platform) ==
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
163 removeSimulator(config->platform());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
164 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165 if (it == platformInfos.end()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
166 std::string platformNames;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
167 raw_string_ostream os(platformNames);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
168 interleave(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
169 platformInfos, os,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
170 [&](const PlatformInfo &info) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
171 os << getPlatformName(info.target.Platform);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
172 },
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
173 "/");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
174 error(toString(input) + " has platform " + platformNames +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
175 Twine(", which is different from target platform ") +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
176 getPlatformName(config->platform()));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
177 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
178 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
179
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
180 if (it->minimum <= config->platformInfo.minimum)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
181 return true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
182
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
183 error(toString(input) + " has version " + it->minimum.getAsString() +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
184 ", which is newer than target minimum of " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
185 config->platformInfo.minimum.getAsString());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
186 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
187 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 // Open a given file path and return it as a memory-mapped file.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 Optional<MemoryBufferRef> macho::readFile(StringRef path) {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
191 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = MemoryBuffer::getFile(path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
192 if (std::error_code ec = mbOrErr.getError()) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 error("cannot open " + path + ": " + ec.message());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 return None;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 MemoryBufferRef mbref = mb->getMemBufferRef();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 // If this is a regular non-fat file, return it.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 const char *buf = mbref.getBufferStart();
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
203 const auto *hdr = reinterpret_cast<const fat_header *>(buf);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
204 if (mbref.getBufferSize() < sizeof(uint32_t) ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
205 read32be(&hdr->magic) != FAT_MAGIC) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
206 if (tar)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
207 tar->append(relativeToRoot(path), mbref.getBuffer());
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 return mbref;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
209 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
211 // Object files and archive files may be fat files, which contain multiple
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
212 // real files for different CPU ISAs. Here, we search for a file that matches
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
213 // with the current link target and returns it as a MemoryBufferRef.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
214 const auto *arch = reinterpret_cast<const fat_arch *>(buf + sizeof(*hdr));
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 if (reinterpret_cast<const char *>(arch + i + 1) >
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 buf + mbref.getBufferSize()) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 error(path + ": fat_arch struct extends beyond end of file");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 return None;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
223 if (read32be(&arch[i].cputype) != static_cast<uint32_t>(target->cpuType) ||
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 read32be(&arch[i].cpusubtype) != target->cpuSubtype)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 continue;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 uint32_t offset = read32be(&arch[i].offset);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 uint32_t size = read32be(&arch[i].size);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 if (offset + size > mbref.getBufferSize())
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 error(path + ": slice extends beyond end of file");
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
231 if (tar)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
232 tar->append(relativeToRoot(path), mbref.getBuffer());
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 return MemoryBufferRef(StringRef(buf + offset, size), path.copy(bAlloc));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 error("unable to find matching architecture in " + path);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 return None;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
240 InputFile::InputFile(Kind kind, const InterfaceFile &interface)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
241 : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {}
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
243 template <class Section>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
244 void ObjFile::parseSections(ArrayRef<Section> sections) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 subsections.reserve(sections.size());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
248 for (const Section &sec : sections) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 InputSection *isec = make<InputSection>();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 isec->file = this;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
251 isec->name =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
252 StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
253 isec->segname =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
254 StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
255 isec->data = {isZeroFill(sec.flags) ? nullptr : buf + sec.offset,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
256 static_cast<size_t>(sec.size)};
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 if (sec.align >= 32)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 error("alignment " + std::to_string(sec.align) + " of section " +
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 isec->name + " is too large");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 else
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 isec->align = 1 << sec.align;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 isec->flags = sec.flags;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
263
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
264 if (!(isDebugSection(isec->flags) &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
265 isec->segname == segment_names::dwarf)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
266 subsections.push_back({{0, isec}});
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
267 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
268 // Instead of emitting DWARF sections, we emit STABS symbols to the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
269 // object files that contain them. We filter them out early to avoid
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
270 // parsing their relocations unnecessarily. But we must still push an
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
271 // empty map to ensure the indices line up for the remaining sections.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
272 subsections.push_back({});
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
273 debugSections.push_back(isec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
274 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 // Find the subsection corresponding to the greatest section offset that is <=
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 // that of the given offset.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 // offset: an offset relative to the start of the original InputSection (before
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 // any subsection splitting has occurred). It will be updated to represent the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 // same location as an offset relative to the start of the containing
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 // subsection.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 static InputSection *findContainingSubsection(SubsectionMap &map,
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
286 uint64_t *offset) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
287 auto it = std::prev(llvm::upper_bound(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
288 map, *offset, [](uint64_t value, SubsectionEntry subsecEntry) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
289 return value < subsecEntry.offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
290 }));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
291 *offset -= it->offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
292 return it->isec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
293 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
295 template <class Section>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
296 static bool validateRelocationInfo(InputFile *file, const Section &sec,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
297 relocation_info rel) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
298 const RelocAttrs &relocAttrs = target->getRelocAttrs(rel.r_type);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
299 bool valid = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
300 auto message = [relocAttrs, file, sec, rel, &valid](const Twine &diagnostic) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
301 valid = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
302 return (relocAttrs.name + " relocation " + diagnostic + " at offset " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
303 std::to_string(rel.r_address) + " of " + sec.segname + "," +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
304 sec.sectname + " in " + toString(file))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
305 .str();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
306 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
307
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
308 if (!relocAttrs.hasAttr(RelocAttrBits::LOCAL) && !rel.r_extern)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
309 error(message("must be extern"));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
310 if (relocAttrs.hasAttr(RelocAttrBits::PCREL) != rel.r_pcrel)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
311 error(message(Twine("must ") + (rel.r_pcrel ? "not " : "") +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
312 "be PC-relative"));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
313 if (isThreadLocalVariables(sec.flags) &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
314 !relocAttrs.hasAttr(RelocAttrBits::UNSIGNED))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
315 error(message("not allowed in thread-local section, must be UNSIGNED"));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
316 if (rel.r_length < 2 || rel.r_length > 3 ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
317 !relocAttrs.hasAttr(static_cast<RelocAttrBits>(1 << rel.r_length))) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
318 static SmallVector<StringRef, 4> widths{"0", "4", "8", "4 or 8"};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
319 error(message("has width " + std::to_string(1 << rel.r_length) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
320 " bytes, but must be " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
321 widths[(static_cast<int>(relocAttrs.bits) >> 2) & 3] +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
322 " bytes"));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
323 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
324 return valid;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
327 template <class Section>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
328 void ObjFile::parseRelocations(ArrayRef<Section> sectionHeaders,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
329 const Section &sec, SubsectionMap &subsecMap) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
331 ArrayRef<relocation_info> relInfos(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
332 reinterpret_cast<const relocation_info *>(buf + sec.reloff), sec.nreloc);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
334 for (size_t i = 0; i < relInfos.size(); i++) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
335 // Paired relocations serve as Mach-O's method for attaching a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
336 // supplemental datum to a primary relocation record. ELF does not
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
337 // need them because the *_RELOC_RELA records contain the extra
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
338 // addend field, vs. *_RELOC_REL which omit the addend.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
339 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
340 // The {X86_64,ARM64}_RELOC_SUBTRACTOR record holds the subtrahend,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
341 // and the paired *_RELOC_UNSIGNED record holds the minuend. The
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
342 // datum for each is a symbolic address. The result is the offset
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
343 // between two addresses.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
344 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
345 // The ARM64_RELOC_ADDEND record holds the addend, and the paired
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
346 // ARM64_RELOC_BRANCH26 or ARM64_RELOC_PAGE21/PAGEOFF12 holds the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
347 // base symbolic address.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
348 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
349 // Note: X86 does not use *_RELOC_ADDEND because it can embed an
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
350 // addend into the instruction stream. On X86, a relocatable address
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
351 // field always occupies an entire contiguous sequence of byte(s),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
352 // so there is no need to merge opcode bits with address
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
353 // bits. Therefore, it's easy and convenient to store addends in the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
354 // instruction-stream bytes that would otherwise contain zeroes. By
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
355 // contrast, RISC ISAs such as ARM64 mix opcode bits with with
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
356 // address bits so that bitwise arithmetic is necessary to extract
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
357 // and insert them. Storing addends in the instruction stream is
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
358 // possible, but inconvenient and more costly at link time.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
359
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
360 int64_t pairedAddend = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
361 relocation_info relInfo = relInfos[i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
362 if (target->hasAttr(relInfo.r_type, RelocAttrBits::ADDEND)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
363 pairedAddend = SignExtend64<24>(relInfo.r_symbolnum);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
364 relInfo = relInfos[++i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
365 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
366 assert(i < relInfos.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
367 if (!validateRelocationInfo(this, sec, relInfo))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
368 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
369 if (relInfo.r_address & R_SCATTERED)
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 fatal("TODO: Scattered relocations not supported");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
371
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
372 bool isSubtrahend =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
373 target->hasAttr(relInfo.r_type, RelocAttrBits::SUBTRAHEND);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
374 int64_t embeddedAddend = target->getEmbeddedAddend(mb, sec.offset, relInfo);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
375 assert(!(embeddedAddend && pairedAddend));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
376 int64_t totalAddend = pairedAddend + embeddedAddend;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 Reloc r;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
378 r.type = relInfo.r_type;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
379 r.pcrel = relInfo.r_pcrel;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
380 r.length = relInfo.r_length;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
381 r.offset = relInfo.r_address;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
382 if (relInfo.r_extern) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
383 r.referent = symbols[relInfo.r_symbolnum];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
384 r.addend = isSubtrahend ? 0 : totalAddend;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 } else {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
386 assert(!isSubtrahend);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
387 const Section &referentSec = sectionHeaders[relInfo.r_symbolnum - 1];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
388 uint64_t referentOffset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
389 if (relInfo.r_pcrel) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
390 // The implicit addend for pcrel section relocations is the pcrel offset
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
391 // in terms of the addresses in the input file. Here we adjust it so
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
392 // that it describes the offset from the start of the referent section.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
393 // FIXME This logic was written around x86_64 behavior -- ARM64 doesn't
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
394 // have pcrel section relocations. We may want to factor this out into
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
395 // the arch-specific .cpp file.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
396 assert(target->hasAttr(r.type, RelocAttrBits::BYTE4));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
397 referentOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
398 sec.addr + relInfo.r_address + 4 + totalAddend - referentSec.addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
399 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
400 // The addend for a non-pcrel relocation is its absolute address.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
401 referentOffset = totalAddend - referentSec.addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
402 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
403 SubsectionMap &referentSubsecMap = subsections[relInfo.r_symbolnum - 1];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
404 r.referent = findContainingSubsection(referentSubsecMap, &referentOffset);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
405 r.addend = referentOffset;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
407
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
408 InputSection *subsec = findContainingSubsection(subsecMap, &r.offset);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 subsec->relocs.push_back(r);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
410
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
411 if (isSubtrahend) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
412 relocation_info minuendInfo = relInfos[++i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
413 // SUBTRACTOR relocations should always be followed by an UNSIGNED one
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
414 // attached to the same address.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
415 assert(target->hasAttr(minuendInfo.r_type, RelocAttrBits::UNSIGNED) &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
416 relInfo.r_address == minuendInfo.r_address);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
417 Reloc p;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
418 p.type = minuendInfo.r_type;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
419 if (minuendInfo.r_extern) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
420 p.referent = symbols[minuendInfo.r_symbolnum];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
421 p.addend = totalAddend;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
422 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
423 uint64_t referentOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
424 totalAddend - sectionHeaders[minuendInfo.r_symbolnum - 1].addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
425 SubsectionMap &referentSubsecMap =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
426 subsections[minuendInfo.r_symbolnum - 1];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
427 p.referent =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
428 findContainingSubsection(referentSubsecMap, &referentOffset);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
429 p.addend = referentOffset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
430 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
431 subsec->relocs.push_back(p);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
432 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
434 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
435
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
436 template <class NList>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
437 static macho::Symbol *createDefined(const NList &sym, StringRef name,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
438 InputSection *isec, uint64_t value,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
439 uint64_t size) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
440 // Symbol scope is determined by sym.n_type & (N_EXT | N_PEXT):
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
441 // N_EXT: Global symbols. These go in the symbol table during the link,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
442 // and also in the export table of the output so that the dynamic
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
443 // linker sees them.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
444 // N_EXT | N_PEXT: Linkage unit (think: dylib) scoped. These go in the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
445 // symbol table during the link so that duplicates are
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
446 // either reported (for non-weak symbols) or merged
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
447 // (for weak symbols), but they do not go in the export
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
448 // table of the output.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
449 // N_PEXT: Does not occur in input files in practice,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
450 // a private extern must be external.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
451 // 0: Translation-unit scoped. These are not in the symbol table during
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
452 // link, and not in the export table of the output either.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
454 bool isWeakDefCanBeHidden =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
455 (sym.n_desc & (N_WEAK_DEF | N_WEAK_REF)) == (N_WEAK_DEF | N_WEAK_REF);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
457 if (sym.n_type & (N_EXT | N_PEXT)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
458 assert((sym.n_type & N_EXT) && "invalid input");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
459 bool isPrivateExtern = sym.n_type & N_PEXT;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
461 // lld's behavior for merging symbols is slightly different from ld64:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
462 // ld64 picks the winning symbol based on several criteria (see
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
463 // pickBetweenRegularAtoms() in ld64's SymbolTable.cpp), while lld
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
464 // just merges metadata and keeps the contents of the first symbol
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
465 // with that name (see SymbolTable::addDefined). For:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
466 // * inline function F in a TU built with -fvisibility-inlines-hidden
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
467 // * and inline function F in another TU built without that flag
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
468 // ld64 will pick the one from the file built without
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
469 // -fvisibility-inlines-hidden.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
470 // lld will instead pick the one listed first on the link command line and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
471 // give it visibility as if the function was built without
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
472 // -fvisibility-inlines-hidden.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
473 // If both functions have the same contents, this will have the same
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
474 // behavior. If not, it won't, but the input had an ODR violation in
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
475 // that case.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
476 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
477 // Similarly, merging a symbol
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
478 // that's isPrivateExtern and not isWeakDefCanBeHidden with one
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
479 // that's not isPrivateExtern but isWeakDefCanBeHidden technically
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
480 // should produce one
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
481 // that's not isPrivateExtern but isWeakDefCanBeHidden. That matters
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
482 // with ld64's semantics, because it means the non-private-extern
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
483 // definition will continue to take priority if more private extern
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
484 // definitions are encountered. With lld's semantics there's no observable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
485 // difference between a symbol that's isWeakDefCanBeHidden or one that's
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
486 // privateExtern -- neither makes it into the dynamic symbol table. So just
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
487 // promote isWeakDefCanBeHidden to isPrivateExtern here.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
488 if (isWeakDefCanBeHidden)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
489 isPrivateExtern = true;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
490
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
491 return symtab->addDefined(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
492 name, isec->file, isec, value, size, sym.n_desc & N_WEAK_DEF,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
493 isPrivateExtern, sym.n_desc & N_ARM_THUMB_DEF,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
494 sym.n_desc & REFERENCED_DYNAMICALLY, sym.n_desc & N_NO_DEAD_STRIP);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
495 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
497 assert(!isWeakDefCanBeHidden &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
498 "weak_def_can_be_hidden on already-hidden symbol?");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
499 return make<Defined>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
500 name, isec->file, isec, value, size, sym.n_desc & N_WEAK_DEF,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
501 /*isExternal=*/false, /*isPrivateExtern=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
502 sym.n_desc & N_ARM_THUMB_DEF, sym.n_desc & REFERENCED_DYNAMICALLY,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
503 sym.n_desc & N_NO_DEAD_STRIP);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
504 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
505
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
506 // Absolute symbols are defined symbols that do not have an associated
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
507 // InputSection. They cannot be weak.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
508 template <class NList>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
509 static macho::Symbol *createAbsolute(const NList &sym, InputFile *file,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
510 StringRef name) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
511 if (sym.n_type & (N_EXT | N_PEXT)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
512 assert((sym.n_type & N_EXT) && "invalid input");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
513 return symtab->addDefined(name, file, nullptr, sym.n_value, /*size=*/0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
514 /*isWeakDef=*/false, sym.n_type & N_PEXT,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
515 sym.n_desc & N_ARM_THUMB_DEF,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
516 /*isReferencedDynamically=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
517 sym.n_desc & N_NO_DEAD_STRIP);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
518 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
519 return make<Defined>(name, file, nullptr, sym.n_value, /*size=*/0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
520 /*isWeakDef=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
521 /*isExternal=*/false, /*isPrivateExtern=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
522 sym.n_desc & N_ARM_THUMB_DEF,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
523 /*isReferencedDynamically=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
524 sym.n_desc & N_NO_DEAD_STRIP);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
525 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
526
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
527 template <class NList>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
528 macho::Symbol *ObjFile::parseNonSectionSymbol(const NList &sym,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
529 StringRef name) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
530 uint8_t type = sym.n_type & N_TYPE;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
531 switch (type) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
532 case N_UNDF:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
533 return sym.n_value == 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
534 ? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
535 : symtab->addCommon(name, this, sym.n_value,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
536 1 << GET_COMM_ALIGN(sym.n_desc),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
537 sym.n_type & N_PEXT);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
538 case N_ABS:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
539 return createAbsolute(sym, this, name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
540 case N_PBUD:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
541 case N_INDR:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
542 error("TODO: support symbols of type " + std::to_string(type));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
543 return nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
544 case N_SECT:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
545 llvm_unreachable(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
546 "N_SECT symbols should not be passed to parseNonSectionSymbol");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
547 default:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
548 llvm_unreachable("invalid symbol type");
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
549 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
550 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
551
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
552 template <class LP>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
553 void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
554 ArrayRef<typename LP::nlist> nList,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
555 const char *strtab, bool subsectionsViaSymbols) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
556 using NList = typename LP::nlist;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
557
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
558 // Groups indices of the symbols by the sections that contain them.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
559 std::vector<std::vector<uint32_t>> symbolsBySection(subsections.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
560 symbols.resize(nList.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
561 for (uint32_t i = 0; i < nList.size(); ++i) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
562 const NList &sym = nList[i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
563 StringRef name = strtab + sym.n_strx;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
564 if ((sym.n_type & N_TYPE) == N_SECT) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
565 SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
566 // parseSections() may have chosen not to parse this section.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
567 if (subsecMap.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
568 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
569 symbolsBySection[sym.n_sect - 1].push_back(i);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
570 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
571 symbols[i] = parseNonSectionSymbol(sym, name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
572 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
573 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
574
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
575 // Calculate symbol sizes and create subsections by splitting the sections
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
576 // along symbol boundaries.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
577 for (size_t i = 0; i < subsections.size(); ++i) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
578 SubsectionMap &subsecMap = subsections[i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
579 if (subsecMap.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
580 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
581
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
582 std::vector<uint32_t> &symbolIndices = symbolsBySection[i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
583 llvm::sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
584 return nList[lhs].n_value < nList[rhs].n_value;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
585 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
586 uint64_t sectionAddr = sectionHeaders[i].addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
587 uint32_t sectionAlign = 1u << sectionHeaders[i].align;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
588
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
589 // We populate subsecMap by repeatedly splitting the last (highest address)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
590 // subsection.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
591 SubsectionEntry subsecEntry = subsecMap.back();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
592 for (size_t j = 0; j < symbolIndices.size(); ++j) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
593 uint32_t symIndex = symbolIndices[j];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
594 const NList &sym = nList[symIndex];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
595 StringRef name = strtab + sym.n_strx;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
596 InputSection *isec = subsecEntry.isec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
597
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
598 uint64_t subsecAddr = sectionAddr + subsecEntry.offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
599 uint64_t symbolOffset = sym.n_value - subsecAddr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
600 uint64_t symbolSize =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
601 j + 1 < symbolIndices.size()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
602 ? nList[symbolIndices[j + 1]].n_value - sym.n_value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
603 : isec->data.size() - symbolOffset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
604 // There are 3 cases where we do not need to create a new subsection:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
605 // 1. If the input file does not use subsections-via-symbols.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
606 // 2. Multiple symbols at the same address only induce one subsection.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
607 // (The symbolOffset == 0 check covers both this case as well as
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
608 // the first loop iteration.)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
609 // 3. Alternative entry points do not induce new subsections.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
610 if (!subsectionsViaSymbols || symbolOffset == 0 ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
611 sym.n_desc & N_ALT_ENTRY) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
612 symbols[symIndex] =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
613 createDefined(sym, name, isec, symbolOffset, symbolSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
614 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
615 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
616
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
617 auto *nextIsec = make<InputSection>(*isec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
618 nextIsec->data = isec->data.slice(symbolOffset);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
619 nextIsec->numRefs = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
620 nextIsec->wasCoalesced = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
621 isec->data = isec->data.slice(0, symbolOffset);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
622
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
623 // By construction, the symbol will be at offset zero in the new
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
624 // subsection.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
625 symbols[symIndex] =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
626 createDefined(sym, name, nextIsec, /*value=*/0, symbolSize);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
627 // TODO: ld64 appears to preserve the original alignment as well as each
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
628 // subsection's offset from the last aligned address. We should consider
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
629 // emulating that behavior.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
630 nextIsec->align = MinAlign(sectionAlign, sym.n_value);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
631 subsecMap.push_back({sym.n_value - sectionAddr, nextIsec});
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
632 subsecEntry = subsecMap.back();
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 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
635 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
636
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
637 OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
638 StringRef sectName)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
639 : InputFile(OpaqueKind, mb) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
640 InputSection *isec = make<InputSection>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
641 isec->file = this;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
642 isec->name = sectName.take_front(16);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
643 isec->segname = segName.take_front(16);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
644 const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
645 isec->data = {buf, mb.getBufferSize()};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
646 isec->live = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
647 subsections.push_back({{0, isec}});
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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
650 ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
651 : InputFile(ObjKind, mb), modTime(modTime) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
652 this->archiveName = std::string(archiveName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
653 if (target->wordSize == 8)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
654 parse<LP64>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
655 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
656 parse<ILP32>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
657 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
658
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
659 template <class LP> void ObjFile::parse() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
660 using Header = typename LP::mach_header;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
661 using SegmentCommand = typename LP::segment_command;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
662 using Section = typename LP::section;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
663 using NList = typename LP::nlist;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
664
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
665 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
666 auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
667
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
668 Architecture arch = getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
669 if (arch != config->arch()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
670 error(toString(this) + " has architecture " + getArchitectureName(arch) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
671 " which is incompatible with target architecture " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
672 getArchitectureName(config->arch()));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
673 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
674 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
676 if (!checkCompatibility(this))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
677 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
678
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
679 if (const load_command *cmd = findCommand(hdr, LC_LINKER_OPTION)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
680 auto *c = reinterpret_cast<const linker_option_command *>(cmd);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
681 StringRef data{reinterpret_cast<const char *>(c + 1),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
682 c->cmdsize - sizeof(linker_option_command)};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
683 parseLCLinkerOption(this, c->count, data);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
684 }
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 ArrayRef<Section> sectionHeaders;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
687 if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
688 auto *c = reinterpret_cast<const SegmentCommand *>(cmd);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
689 sectionHeaders =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
690 ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
691 parseSections(sectionHeaders);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
692 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
693
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
694 // TODO: Error on missing LC_SYMTAB?
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
695 if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
696 auto *c = reinterpret_cast<const symtab_command *>(cmd);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
697 ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
698 c->nsyms);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
699 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
700 bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
701 parseSymbols<LP>(sectionHeaders, nList, strtab, subsectionsViaSymbols);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
702 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
703
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
704 // The relocations may refer to the symbols, so we parse them after we have
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
705 // parsed all the symbols.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
706 for (size_t i = 0, n = subsections.size(); i < n; ++i)
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
707 if (!subsections[i].empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
708 parseRelocations(sectionHeaders, sectionHeaders[i], subsections[i]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
710 parseDebugInfo();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
711 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
713 void ObjFile::parseDebugInfo() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
714 std::unique_ptr<DwarfObject> dObj = DwarfObject::create(this);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
715 if (!dObj)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
716 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
717
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
718 auto *ctx = make<DWARFContext>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
719 std::move(dObj), "",
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
720 [&](Error err) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
721 warn(toString(this) + ": " + toString(std::move(err)));
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 [&](Error warning) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
724 warn(toString(this) + ": " + toString(std::move(warning)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
725 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
726
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
727 // TODO: Since object files can contain a lot of DWARF info, we should verify
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
728 // that we are parsing just the info we need
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
729 const DWARFContext::compile_unit_range &units = ctx->compile_units();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
730 // FIXME: There can be more than one compile unit per object file. See
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
731 // PR48637.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
732 auto it = units.begin();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
733 compileUnit = it->get();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
734 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
735
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
736 // The path can point to either a dylib or a .tbd file.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
737 static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
738 Optional<MemoryBufferRef> mbref = readFile(path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
739 if (!mbref) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
740 error("could not read dylib file at " + path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
741 return nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
742 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
743 return loadDylib(*mbref, umbrella);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
744 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
745
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
746 // TBD files are parsed into a series of TAPI documents (InterfaceFiles), with
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
747 // the first document storing child pointers to the rest of them. When we are
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
748 // processing a given TBD file, we store that top-level document in
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
749 // currentTopLevelTapi. When processing re-exports, we search its children for
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
750 // potentially matching documents in the same TBD file. Note that the children
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
751 // themselves don't point to further documents, i.e. this is a two-level tree.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
752 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
753 // Re-exports can either refer to on-disk files, or to documents within .tbd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
754 // files.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
755 static DylibFile *findDylib(StringRef path, DylibFile *umbrella,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
756 const InterfaceFile *currentTopLevelTapi) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
757 if (path::is_absolute(path, path::Style::posix))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
758 for (StringRef root : config->systemLibraryRoots)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
759 if (Optional<std::string> dylibPath =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
760 resolveDylibPath((root + path).str()))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
761 return loadDylib(*dylibPath, umbrella);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
762
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
763 // TODO: Handle -dylib_file
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
764
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
765 SmallString<128> newPath;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
766 if (config->outputType == MH_EXECUTE &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
767 path.consume_front("@executable_path/")) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
768 // ld64 allows overriding this with the undocumented flag -executable_path.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
769 // lld doesn't currently implement that flag.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
770 path::append(newPath, sys::path::parent_path(config->outputFile), path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
771 path = newPath;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
772 } else if (path.consume_front("@loader_path/")) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
773 path::append(newPath, sys::path::parent_path(umbrella->getName()), path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
774 path = newPath;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
775 } else if (path.startswith("@rpath/")) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
776 for (StringRef rpath : umbrella->rpaths) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
777 newPath.clear();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
778 if (rpath.consume_front("@loader_path/"))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
779 path::append(newPath, sys::path::parent_path(umbrella->getName()));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
780 path::append(newPath, rpath, path.drop_front(strlen("@rpath/")));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
781 if (Optional<std::string> dylibPath = resolveDylibPath(newPath))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
782 return loadDylib(*dylibPath, umbrella);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
783 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
784 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
785
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
786 if (currentTopLevelTapi) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
787 for (InterfaceFile &child :
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
788 make_pointee_range(currentTopLevelTapi->documents())) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
789 assert(child.documents().empty());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
790 if (path == child.getInstallName()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
791 auto file = make<DylibFile>(child, umbrella);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
792 file->parseReexports(child);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
793 return file;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
794 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
795 }
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 if (Optional<std::string> dylibPath = resolveDylibPath(path))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
799 return loadDylib(*dylibPath, umbrella);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
800
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
801 return nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
802 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
803
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
804 // If a re-exported dylib is public (lives in /usr/lib or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
805 // /System/Library/Frameworks), then it is considered implicitly linked: we
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
806 // should bind to its symbols directly instead of via the re-exporting umbrella
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
807 // library.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
808 static bool isImplicitlyLinked(StringRef path) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
809 if (!config->implicitDylibs)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
810 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
811
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
812 if (path::parent_path(path) == "/usr/lib")
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
813 return true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
814
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
815 // Match /System/Library/Frameworks/$FOO.framework/**/$FOO
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
816 if (path.consume_front("/System/Library/Frameworks/")) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
817 StringRef frameworkName = path.take_until([](char c) { return c == '.'; });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
818 return path::filename(path) == frameworkName;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
819 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
820
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
821 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
822 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
823
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
824 static void loadReexport(StringRef path, DylibFile *umbrella,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
825 const InterfaceFile *currentTopLevelTapi) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
826 DylibFile *reexport = findDylib(path, umbrella, currentTopLevelTapi);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
827 if (!reexport)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
828 error("unable to locate re-export with install name " + path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
829 else if (isImplicitlyLinked(path))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
830 inputFiles.insert(reexport);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
831 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
832
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
833 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
834 bool isBundleLoader)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
835 : InputFile(DylibKind, mb), refState(RefState::Unreferenced),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
836 isBundleLoader(isBundleLoader) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
837 assert(!isBundleLoader || !umbrella);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
838 if (umbrella == nullptr)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
839 umbrella = this;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
840 this->umbrella = umbrella;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
841
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
842 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
843 auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
844
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
845 // Initialize installName.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
846 if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
847 auto *c = reinterpret_cast<const dylib_command *>(cmd);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
848 currentVersion = read32le(&c->dylib.current_version);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
849 compatibilityVersion = read32le(&c->dylib.compatibility_version);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
850 installName =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
851 reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
852 } else if (!isBundleLoader) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
853 // macho_executable and macho_bundle don't have LC_ID_DYLIB,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
854 // so it's OK.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
855 error("dylib " + toString(this) + " missing LC_ID_DYLIB load command");
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
856 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
857 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
858
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
859 if (config->printEachFile)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
860 message(toString(this));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
861
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
862 deadStrippable = hdr->flags & MH_DEAD_STRIPPABLE_DYLIB;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
863
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
864 if (!checkCompatibility(this))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
865 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
866
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
867 for (auto *cmd : findCommands<rpath_command>(hdr, LC_RPATH)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
868 StringRef rpath{reinterpret_cast<const char *>(cmd) + cmd->path};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
869 rpaths.push_back(rpath);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
870 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
871
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
872 // Initialize symbols.
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
873 exportingFile = isImplicitlyLinked(installName) ? this : this->umbrella;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
874 if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
875 auto *c = reinterpret_cast<const dyld_info_command *>(cmd);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
876 parseTrie(buf + c->export_off, c->export_size,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
877 [&](const Twine &name, uint64_t flags) {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
878 StringRef savedName = saver.save(name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
879 if (handleLDSymbol(savedName))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
880 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
881 bool isWeakDef = flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
882 bool isTlv = flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
883 symbols.push_back(symtab->addDylib(savedName, exportingFile,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
884 isWeakDef, isTlv));
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
885 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
886 } else {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
887 error("LC_DYLD_INFO_ONLY not found in " + toString(this));
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
888 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
889 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
890 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
891
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
892 void DylibFile::parseLoadCommands(MemoryBufferRef mb) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
893 auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
894 const uint8_t *p = reinterpret_cast<const uint8_t *>(mb.getBufferStart()) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
895 target->headerSize;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
896 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
897 auto *cmd = reinterpret_cast<const load_command *>(p);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
898 p += cmd->cmdsize;
207
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 if (!(hdr->flags & MH_NO_REEXPORTED_DYLIBS) &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
901 cmd->cmd == LC_REEXPORT_DYLIB) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
902 const auto *c = reinterpret_cast<const dylib_command *>(cmd);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
903 StringRef reexportPath =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
904 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
905 loadReexport(reexportPath, exportingFile, nullptr);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
906 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
907
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
908 // FIXME: What about LC_LOAD_UPWARD_DYLIB, LC_LAZY_LOAD_DYLIB,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
909 // LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB (..are reexports from dylibs with
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
910 // MH_NO_REEXPORTED_DYLIBS loaded for -flat_namespace)?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
911 if (config->namespaceKind == NamespaceKind::flat &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
912 cmd->cmd == LC_LOAD_DYLIB) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
913 const auto *c = reinterpret_cast<const dylib_command *>(cmd);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
914 StringRef dylibPath =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
915 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
916 DylibFile *dylib = findDylib(dylibPath, umbrella, nullptr);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
917 if (!dylib)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
918 error(Twine("unable to locate library '") + dylibPath +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
919 "' loaded from '" + toString(this) + "' for -flat_namespace");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
920 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
921 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
922 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
923
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
924 // Some versions of XCode ship with .tbd files that don't have the right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
925 // platform settings.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
926 static constexpr std::array<StringRef, 3> skipPlatformChecks{
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
927 "/usr/lib/system/libsystem_kernel.dylib",
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
928 "/usr/lib/system/libsystem_platform.dylib",
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
929 "/usr/lib/system/libsystem_pthread.dylib"};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
930
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
931 DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
932 bool isBundleLoader)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
933 : InputFile(DylibKind, interface), refState(RefState::Unreferenced),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
934 isBundleLoader(isBundleLoader) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
935 // FIXME: Add test for the missing TBD code path.
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 if (umbrella == nullptr)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
938 umbrella = this;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
939 this->umbrella = umbrella;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
940
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
941 installName = saver.save(interface.getInstallName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
942 compatibilityVersion = interface.getCompatibilityVersion().rawValue();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
943 currentVersion = interface.getCurrentVersion().rawValue();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
944
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
945 if (config->printEachFile)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
946 message(toString(this));
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 if (!is_contained(skipPlatformChecks, installName) &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
949 !is_contained(interface.targets(), config->platformInfo.target)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
950 error(toString(this) + " is incompatible with " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
951 std::string(config->platformInfo.target));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
952 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
953 }
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 exportingFile = isImplicitlyLinked(installName) ? this : umbrella;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
956 auto addSymbol = [&](const Twine &name) -> void {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
957 symbols.push_back(symtab->addDylib(saver.save(name), exportingFile,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
958 /*isWeakDef=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
959 /*isTlv=*/false));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
960 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
961 // TODO(compnerd) filter out symbols based on the target platform
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
962 // TODO: handle weak defs, thread locals
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
963 for (const auto *symbol : interface.symbols()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
964 if (!symbol->getArchitectures().has(config->arch()))
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
965 continue;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
966
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
967 if (handleLDSymbol(symbol->getName()))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
968 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
969
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
970 switch (symbol->getKind()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
971 case SymbolKind::GlobalSymbol:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
972 addSymbol(symbol->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
973 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
974 case SymbolKind::ObjectiveCClass:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
975 // XXX ld64 only creates these symbols when -ObjC is passed in. We may
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
976 // want to emulate that.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
977 addSymbol(objc::klass + symbol->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
978 addSymbol(objc::metaclass + symbol->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
979 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
980 case SymbolKind::ObjectiveCClassEHType:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
981 addSymbol(objc::ehtype + symbol->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
982 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
983 case SymbolKind::ObjectiveCInstanceVariable:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
984 addSymbol(objc::ivar + symbol->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
985 break;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
986 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
987 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
988 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
989
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
990 void DylibFile::parseReexports(const InterfaceFile &interface) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
991 const InterfaceFile *topLevel =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
992 interface.getParent() == nullptr ? &interface : interface.getParent();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
993 for (InterfaceFileRef intfRef : interface.reexportedLibraries()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
994 InterfaceFile::const_target_range targets = intfRef.targets();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
995 if (is_contained(skipPlatformChecks, intfRef.getInstallName()) ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
996 is_contained(targets, config->platformInfo.target))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
997 loadReexport(intfRef.getInstallName(), exportingFile, topLevel);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
998 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
999 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1000
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1001 // $ld$ symbols modify the properties/behavior of the library (e.g. its install
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1002 // name, compatibility version or hide/add symbols) for specific target
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1003 // versions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1004 bool DylibFile::handleLDSymbol(StringRef originalName) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1005 if (!originalName.startswith("$ld$"))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1006 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1007
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1008 StringRef action;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1009 StringRef name;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1010 std::tie(action, name) = originalName.drop_front(strlen("$ld$")).split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1011 if (action == "previous")
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1012 handleLDPreviousSymbol(name, originalName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1013 else if (action == "install_name")
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1014 handleLDInstallNameSymbol(name, originalName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1015 return true;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1016 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1017
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1018 void DylibFile::handleLDPreviousSymbol(StringRef name, StringRef originalName) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1019 // originalName: $ld$ previous $ <installname> $ <compatversion> $
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1020 // <platformstr> $ <startversion> $ <endversion> $ <symbol-name> $
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1021 StringRef installName;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1022 StringRef compatVersion;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1023 StringRef platformStr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1024 StringRef startVersion;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1025 StringRef endVersion;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1026 StringRef symbolName;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1027 StringRef rest;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1028
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1029 std::tie(installName, name) = name.split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1030 std::tie(compatVersion, name) = name.split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1031 std::tie(platformStr, name) = name.split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1032 std::tie(startVersion, name) = name.split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1033 std::tie(endVersion, name) = name.split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1034 std::tie(symbolName, rest) = name.split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1035 // TODO: ld64 contains some logic for non-empty symbolName as well.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1036 if (!symbolName.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1037 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1038 unsigned platform;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1039 if (platformStr.getAsInteger(10, platform) ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1040 platform != static_cast<unsigned>(config->platform()))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1041 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1042
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1043 VersionTuple start;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1044 if (start.tryParse(startVersion)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1045 warn("failed to parse start version, symbol '" + originalName +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1046 "' ignored");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1047 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1048 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1049 VersionTuple end;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1050 if (end.tryParse(endVersion)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1051 warn("failed to parse end version, symbol '" + originalName + "' ignored");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1052 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1053 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1054 if (config->platformInfo.minimum < start ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1055 config->platformInfo.minimum >= end)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1056 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1057
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1058 this->installName = saver.save(installName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1059
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1060 if (!compatVersion.empty()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1061 VersionTuple cVersion;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1062 if (cVersion.tryParse(compatVersion)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1063 warn("failed to parse compatibility version, symbol '" + originalName +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1064 "' ignored");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1065 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1066 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1067 compatibilityVersion = encodeVersion(cVersion);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1068 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1069 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1070
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1071 void DylibFile::handleLDInstallNameSymbol(StringRef name,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1072 StringRef originalName) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1073 // originalName: $ld$ install_name $ os<version> $ install_name
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1074 StringRef condition, installName;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1075 std::tie(condition, installName) = name.split('$');
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1076 VersionTuple version;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1077 if (!condition.consume_front("os") || version.tryParse(condition))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1078 warn("failed to parse os version, symbol '" + originalName + "' ignored");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1079 else if (version == config->platformInfo.minimum)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1080 this->installName = saver.save(installName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1081 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1082
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1083 ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f)
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1084 : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1085 for (const object::Archive::Symbol &sym : file->symbols())
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1086 symtab->addLazy(sym.getName(), this, sym);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1087 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1088
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1089 void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1090 object::Archive::Child c =
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1091 CHECK(sym.getMember(), toString(this) +
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1092 ": could not get the member for symbol " +
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1093 toMachOString(sym));
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1094
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1095 if (!seen.insert(c.getChildOffset()).second)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1096 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1097
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1098 MemoryBufferRef mb =
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1099 CHECK(c.getMemoryBufferRef(),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1100 toString(this) +
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1101 ": could not get the buffer for the member defining symbol " +
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1102 toMachOString(sym));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1103
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1104 if (tar && c.getParent()->isThin())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1105 tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb.getBuffer());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1106
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1107 uint32_t modTime = toTimeT(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1108 CHECK(c.getLastModified(), toString(this) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1109 ": could not get the modification time "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1110 "for the member defining symbol " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1111 toMachOString(sym)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1112
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1113 // `sym` is owned by a LazySym, which will be replace<>()d by make<ObjFile>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1114 // and become invalid after that call. Copy it to the stack so we can refer
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1115 // to it later.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1116 const object::Archive::Symbol symCopy = sym;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1117
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1118 if (Optional<InputFile *> file =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1119 loadArchiveMember(mb, modTime, getName(), /*objCOnly=*/false)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1120 inputFiles.insert(*file);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1121 // ld64 doesn't demangle sym here even with -demangle.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1122 // Match that: intentionally don't call toMachOString().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1123 printArchiveMemberLoad(symCopy.getName(), *file);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1124 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1125 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1126
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1127 static macho::Symbol *createBitcodeSymbol(const lto::InputFile::Symbol &objSym,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1128 BitcodeFile &file) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1129 StringRef name = saver.save(objSym.getName());
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 // TODO: support weak references
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1132 if (objSym.isUndefined())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1133 return symtab->addUndefined(name, &file, /*isWeakRef=*/false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1134
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1135 assert(!objSym.isCommon() && "TODO: support common symbols in LTO");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1136
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1137 // TODO: Write a test demonstrating why computing isPrivateExtern before
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1138 // LTO compilation is important.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1139 bool isPrivateExtern = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1140 switch (objSym.getVisibility()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1141 case GlobalValue::HiddenVisibility:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1142 isPrivateExtern = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1143 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1144 case GlobalValue::ProtectedVisibility:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1145 error(name + " has protected visibility, which is not supported by Mach-O");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1146 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1147 case GlobalValue::DefaultVisibility:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1148 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1149 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1150
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1151 return symtab->addDefined(name, &file, /*isec=*/nullptr, /*value=*/0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1152 /*size=*/0, objSym.isWeak(), isPrivateExtern,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1153 /*isThumb=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1154 /*isReferencedDynamically=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1155 /*noDeadStrip=*/false);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1156 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1157
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1158 BitcodeFile::BitcodeFile(MemoryBufferRef mbref)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1159 : InputFile(BitcodeKind, mbref) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1160 obj = check(lto::InputFile::create(mbref));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1161
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1162 // Convert LTO Symbols to LLD Symbols in order to perform resolution. The
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1163 // "winning" symbol will then be marked as Prevailing at LTO compilation
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1164 // time.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1165 for (const lto::InputFile::Symbol &objSym : obj->symbols())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1166 symbols.push_back(createBitcodeSymbol(objSym, *this));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1167 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1168
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1169 template void ObjFile::parse<LP64>();