121
|
1 //===- Archive.cpp - ar File Format implementation ------------------------===//
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // This file defines the ArchiveObjectFile class.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 #include "llvm/Object/Archive.h"
|
121
|
15 #include "llvm/ADT/Optional.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/ADT/SmallString.h"
|
121
|
17 #include "llvm/ADT/StringRef.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 #include "llvm/ADT/Twine.h"
|
121
|
19 #include "llvm/Object/Binary.h"
|
|
20 #include "llvm/Object/Error.h"
|
|
21 #include "llvm/Support/Chrono.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 #include "llvm/Support/Endian.h"
|
121
|
23 #include "llvm/Support/Error.h"
|
|
24 #include "llvm/Support/ErrorOr.h"
|
|
25 #include "llvm/Support/FileSystem.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 #include "llvm/Support/MemoryBuffer.h"
|
95
|
27 #include "llvm/Support/Path.h"
|
121
|
28 #include "llvm/Support/raw_ostream.h"
|
|
29 #include <algorithm>
|
|
30 #include <cassert>
|
|
31 #include <cstddef>
|
|
32 #include <cstdint>
|
|
33 #include <cstring>
|
|
34 #include <memory>
|
|
35 #include <string>
|
|
36 #include <system_error>
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 using namespace object;
|
95
|
40 using namespace llvm::support::endian;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 static const char *const Magic = "!<arch>\n";
|
83
|
43 static const char *const ThinMagic = "!<thin>\n";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44
|
121
|
45 void Archive::anchor() {}
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46
|
120
|
47 static Error
|
|
48 malformedError(Twine Msg) {
|
|
49 std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
|
|
50 return make_error<GenericBinaryError>(std::move(StringMsg),
|
|
51 object_error::parse_failed);
|
|
52 }
|
|
53
|
|
54 ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
|
|
55 const char *RawHeaderPtr,
|
|
56 uint64_t Size, Error *Err)
|
|
57 : Parent(Parent),
|
|
58 ArMemHdr(reinterpret_cast<const ArMemHdrType *>(RawHeaderPtr)) {
|
|
59 if (RawHeaderPtr == nullptr)
|
|
60 return;
|
|
61 ErrorAsOutParameter ErrAsOutParam(Err);
|
|
62
|
|
63 if (Size < sizeof(ArMemHdrType)) {
|
|
64 if (Err) {
|
|
65 std::string Msg("remaining size of archive too small for next archive "
|
|
66 "member header ");
|
|
67 Expected<StringRef> NameOrErr = getName(Size);
|
|
68 if (!NameOrErr) {
|
|
69 consumeError(NameOrErr.takeError());
|
|
70 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
|
|
71 *Err = malformedError(Msg + "at offset " + Twine(Offset));
|
|
72 } else
|
|
73 *Err = malformedError(Msg + "for " + NameOrErr.get());
|
|
74 }
|
|
75 return;
|
|
76 }
|
|
77 if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
|
|
78 if (Err) {
|
|
79 std::string Buf;
|
|
80 raw_string_ostream OS(Buf);
|
121
|
81 OS.write_escaped(StringRef(ArMemHdr->Terminator,
|
|
82 sizeof(ArMemHdr->Terminator)));
|
120
|
83 OS.flush();
|
|
84 std::string Msg("terminator characters in archive member \"" + Buf +
|
|
85 "\" not the correct \"`\\n\" values for the archive "
|
|
86 "member header ");
|
|
87 Expected<StringRef> NameOrErr = getName(Size);
|
|
88 if (!NameOrErr) {
|
|
89 consumeError(NameOrErr.takeError());
|
|
90 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
|
|
91 *Err = malformedError(Msg + "at offset " + Twine(Offset));
|
|
92 } else
|
|
93 *Err = malformedError(Msg + "for " + NameOrErr.get());
|
|
94 }
|
|
95 return;
|
|
96 }
|
|
97 }
|
|
98
|
|
99 // This gets the raw name from the ArMemHdr->Name field and checks that it is
|
|
100 // valid for the kind of archive. If it is not valid it returns an Error.
|
|
101 Expected<StringRef> ArchiveMemberHeader::getRawName() const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 char EndCond;
|
120
|
103 auto Kind = Parent->kind();
|
|
104 if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) {
|
|
105 if (ArMemHdr->Name[0] == ' ') {
|
|
106 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
107 Parent->getData().data();
|
|
108 return malformedError("name contains a leading space for archive member "
|
|
109 "header at offset " + Twine(Offset));
|
|
110 }
|
|
111 EndCond = ' ';
|
|
112 }
|
|
113 else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 EndCond = ' ';
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115 else
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116 EndCond = '/';
|
121
|
117 StringRef::size_type end =
|
|
118 StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
|
|
119 if (end == StringRef::npos)
|
120
|
120 end = sizeof(ArMemHdr->Name);
|
|
121 assert(end <= sizeof(ArMemHdr->Name) && end > 0);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
122 // Don't include the EndCond if there is one.
|
121
|
123 return StringRef(ArMemHdr->Name, end);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
124 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
125
|
120
|
126 // This gets the name looking up long names. Size is the size of the archive
|
|
127 // member including the header, so the size of any name following the header
|
|
128 // is checked to make sure it does not overflow.
|
|
129 Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
|
|
130
|
|
131 // This can be called from the ArchiveMemberHeader constructor when the
|
|
132 // archive header is truncated to produce an error message with the name.
|
|
133 // Make sure the name field is not truncated.
|
|
134 if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
|
|
135 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
136 Parent->getData().data();
|
|
137 return malformedError("archive header truncated before the name field "
|
|
138 "for archive member header at offset " +
|
|
139 Twine(ArchiveOffset));
|
|
140 }
|
|
141
|
|
142 // The raw name itself can be invalid.
|
|
143 Expected<StringRef> NameOrErr = getRawName();
|
|
144 if (!NameOrErr)
|
|
145 return NameOrErr.takeError();
|
|
146 StringRef Name = NameOrErr.get();
|
|
147
|
|
148 // Check if it's a special name.
|
|
149 if (Name[0] == '/') {
|
|
150 if (Name.size() == 1) // Linker member.
|
|
151 return Name;
|
|
152 if (Name.size() == 2 && Name[1] == '/') // String table.
|
|
153 return Name;
|
|
154 // It's a long name.
|
|
155 // Get the string table offset.
|
|
156 std::size_t StringOffset;
|
|
157 if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
|
|
158 std::string Buf;
|
|
159 raw_string_ostream OS(Buf);
|
|
160 OS.write_escaped(Name.substr(1).rtrim(' '));
|
|
161 OS.flush();
|
|
162 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
163 Parent->getData().data();
|
|
164 return malformedError("long name offset characters after the '/' are "
|
|
165 "not all decimal numbers: '" + Buf + "' for "
|
|
166 "archive member header at offset " +
|
|
167 Twine(ArchiveOffset));
|
|
168 }
|
|
169
|
|
170 // Verify it.
|
|
171 if (StringOffset >= Parent->getStringTable().size()) {
|
|
172 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
173 Parent->getData().data();
|
|
174 return malformedError("long name offset " + Twine(StringOffset) + " past "
|
|
175 "the end of the string table for archive member "
|
|
176 "header at offset " + Twine(ArchiveOffset));
|
|
177 }
|
|
178 const char *addr = Parent->getStringTable().begin() + StringOffset;
|
|
179
|
|
180 // GNU long file names end with a "/\n".
|
|
181 if (Parent->kind() == Archive::K_GNU ||
|
121
|
182 Parent->kind() == Archive::K_GNU64) {
|
120
|
183 StringRef::size_type End = StringRef(addr).find('\n');
|
|
184 return StringRef(addr, End - 1);
|
|
185 }
|
|
186 return addr;
|
|
187 }
|
|
188
|
|
189 if (Name.startswith("#1/")) {
|
|
190 uint64_t NameLength;
|
|
191 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
|
|
192 std::string Buf;
|
|
193 raw_string_ostream OS(Buf);
|
|
194 OS.write_escaped(Name.substr(3).rtrim(' '));
|
|
195 OS.flush();
|
|
196 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
197 Parent->getData().data();
|
|
198 return malformedError("long name length characters after the #1/ are "
|
|
199 "not all decimal numbers: '" + Buf + "' for "
|
|
200 "archive member header at offset " +
|
|
201 Twine(ArchiveOffset));
|
|
202 }
|
|
203 if (getSizeOf() + NameLength > Size) {
|
|
204 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
205 Parent->getData().data();
|
|
206 return malformedError("long name length: " + Twine(NameLength) +
|
|
207 " extends past the end of the member or archive "
|
|
208 "for archive member header at offset " +
|
|
209 Twine(ArchiveOffset));
|
|
210 }
|
|
211 return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
|
|
212 NameLength).rtrim('\0');
|
|
213 }
|
|
214
|
|
215 // It is not a long name so trim the blanks at the end of the name.
|
|
216 if (Name[Name.size() - 1] != '/')
|
|
217 return Name.rtrim(' ');
|
|
218
|
|
219 // It's a simple name.
|
|
220 return Name.drop_back(1);
|
|
221 }
|
|
222
|
|
223 Expected<uint32_t> ArchiveMemberHeader::getSize() const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 uint32_t Ret;
|
121
|
225 if (StringRef(ArMemHdr->Size,
|
|
226 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
|
120
|
227 std::string Buf;
|
|
228 raw_string_ostream OS(Buf);
|
121
|
229 OS.write_escaped(StringRef(ArMemHdr->Size,
|
|
230 sizeof(ArMemHdr->Size)).rtrim(" "));
|
120
|
231 OS.flush();
|
|
232 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
233 Parent->getData().data();
|
|
234 return malformedError("characters in size field in archive header are not "
|
|
235 "all decimal numbers: '" + Buf + "' for archive "
|
|
236 "member header at offset " + Twine(Offset));
|
|
237 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
238 return Ret;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
239 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
240
|
120
|
241 Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
242 unsigned Ret;
|
120
|
243 if (StringRef(ArMemHdr->AccessMode,
|
|
244 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
|
|
245 std::string Buf;
|
|
246 raw_string_ostream OS(Buf);
|
121
|
247 OS.write_escaped(StringRef(ArMemHdr->AccessMode,
|
|
248 sizeof(ArMemHdr->AccessMode)).rtrim(" "));
|
120
|
249 OS.flush();
|
|
250 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
251 Parent->getData().data();
|
|
252 return malformedError("characters in AccessMode field in archive header "
|
|
253 "are not all decimal numbers: '" + Buf + "' for the "
|
|
254 "archive member header at offset " + Twine(Offset));
|
|
255 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
256 return static_cast<sys::fs::perms>(Ret);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
257 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
258
|
120
|
259 Expected<sys::TimePoint<std::chrono::seconds>>
|
|
260 ArchiveMemberHeader::getLastModified() const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
261 unsigned Seconds;
|
120
|
262 if (StringRef(ArMemHdr->LastModified,
|
|
263 sizeof(ArMemHdr->LastModified)).rtrim(' ')
|
|
264 .getAsInteger(10, Seconds)) {
|
|
265 std::string Buf;
|
|
266 raw_string_ostream OS(Buf);
|
121
|
267 OS.write_escaped(StringRef(ArMemHdr->LastModified,
|
|
268 sizeof(ArMemHdr->LastModified)).rtrim(" "));
|
120
|
269 OS.flush();
|
|
270 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
271 Parent->getData().data();
|
|
272 return malformedError("characters in LastModified field in archive header "
|
|
273 "are not all decimal numbers: '" + Buf + "' for the "
|
|
274 "archive member header at offset " + Twine(Offset));
|
|
275 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
276
|
120
|
277 return sys::toTimePoint(Seconds);
|
|
278 }
|
|
279
|
|
280 Expected<unsigned> ArchiveMemberHeader::getUID() const {
|
|
281 unsigned Ret;
|
|
282 StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
|
|
283 if (User.empty())
|
|
284 return 0;
|
|
285 if (User.getAsInteger(10, Ret)) {
|
|
286 std::string Buf;
|
|
287 raw_string_ostream OS(Buf);
|
|
288 OS.write_escaped(User);
|
|
289 OS.flush();
|
|
290 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
291 Parent->getData().data();
|
|
292 return malformedError("characters in UID field in archive header "
|
|
293 "are not all decimal numbers: '" + Buf + "' for the "
|
|
294 "archive member header at offset " + Twine(Offset));
|
|
295 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
296 return Ret;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
297 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
298
|
120
|
299 Expected<unsigned> ArchiveMemberHeader::getGID() const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
300 unsigned Ret;
|
120
|
301 StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
|
|
302 if (Group.empty())
|
|
303 return 0;
|
|
304 if (Group.getAsInteger(10, Ret)) {
|
|
305 std::string Buf;
|
|
306 raw_string_ostream OS(Buf);
|
|
307 OS.write_escaped(Group);
|
|
308 OS.flush();
|
|
309 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
|
|
310 Parent->getData().data();
|
|
311 return malformedError("characters in GID field in archive header "
|
|
312 "are not all decimal numbers: '" + Buf + "' for the "
|
|
313 "archive member header at offset " + Twine(Offset));
|
|
314 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
315 return Ret;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
316 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
317
|
100
|
318 Archive::Child::Child(const Archive *Parent, StringRef Data,
|
|
319 uint16_t StartOfFile)
|
120
|
320 : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
|
|
321 Data(Data), StartOfFile(StartOfFile) {
|
|
322 }
|
100
|
323
|
120
|
324 Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
|
|
325 : Parent(Parent),
|
|
326 Header(Parent, Start,
|
|
327 Parent
|
|
328 ? Parent->getData().size() - (Start - Parent->getData().data())
|
|
329 : 0, Err) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
330 if (!Start)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
331 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
332
|
120
|
333 // If we are pointed to real data, Start is not a nullptr, then there must be
|
|
334 // a non-null Err pointer available to report malformed data on. Only in
|
|
335 // the case sentinel value is being constructed is Err is permitted to be a
|
|
336 // nullptr.
|
|
337 assert(Err && "Err can't be nullptr if Start is not a nullptr");
|
|
338
|
|
339 ErrorAsOutParameter ErrAsOutParam(Err);
|
|
340
|
121
|
341 // If there was an error in the construction of the Header
|
120
|
342 // then just return with the error now set.
|
|
343 if (*Err)
|
|
344 return;
|
|
345
|
|
346 uint64_t Size = Header.getSizeOf();
|
95
|
347 Data = StringRef(Start, Size);
|
120
|
348 Expected<bool> isThinOrErr = isThinMember();
|
|
349 if (!isThinOrErr) {
|
|
350 *Err = isThinOrErr.takeError();
|
|
351 return;
|
|
352 }
|
|
353 bool isThin = isThinOrErr.get();
|
|
354 if (!isThin) {
|
|
355 Expected<uint64_t> MemberSize = getRawSize();
|
|
356 if (!MemberSize) {
|
|
357 *Err = MemberSize.takeError();
|
100
|
358 return;
|
120
|
359 }
|
100
|
360 Size += MemberSize.get();
|
95
|
361 Data = StringRef(Start, Size);
|
|
362 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
363
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
364 // Setup StartOfFile and PaddingBytes.
|
120
|
365 StartOfFile = Header.getSizeOf();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
366 // Don't include attached name.
|
120
|
367 Expected<StringRef> NameOrErr = getRawName();
|
|
368 if (!NameOrErr){
|
|
369 *Err = NameOrErr.takeError();
|
|
370 return;
|
|
371 }
|
|
372 StringRef Name = NameOrErr.get();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
373 if (Name.startswith("#1/")) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
374 uint64_t NameSize;
|
120
|
375 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
|
|
376 std::string Buf;
|
|
377 raw_string_ostream OS(Buf);
|
|
378 OS.write_escaped(Name.substr(3).rtrim(' '));
|
|
379 OS.flush();
|
|
380 uint64_t Offset = Start - Parent->getData().data();
|
|
381 *Err = malformedError("long name length characters after the #1/ are "
|
|
382 "not all decimal numbers: '" + Buf + "' for "
|
|
383 "archive member header at offset " +
|
|
384 Twine(Offset));
|
|
385 return;
|
|
386 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
387 StartOfFile += NameSize;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
388 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
389 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
390
|
120
|
391 Expected<uint64_t> Archive::Child::getSize() const {
|
100
|
392 if (Parent->IsThin) {
|
120
|
393 Expected<uint32_t> Size = Header.getSize();
|
|
394 if (!Size)
|
|
395 return Size.takeError();
|
100
|
396 return Size.get();
|
|
397 }
|
83
|
398 return Data.size() - StartOfFile;
|
|
399 }
|
|
400
|
120
|
401 Expected<uint64_t> Archive::Child::getRawSize() const {
|
|
402 return Header.getSize();
|
83
|
403 }
|
|
404
|
120
|
405 Expected<bool> Archive::Child::isThinMember() const {
|
|
406 Expected<StringRef> NameOrErr = Header.getRawName();
|
|
407 if (!NameOrErr)
|
|
408 return NameOrErr.takeError();
|
|
409 StringRef Name = NameOrErr.get();
|
95
|
410 return Parent->IsThin && Name != "/" && Name != "//";
|
|
411 }
|
|
412
|
120
|
413 Expected<std::string> Archive::Child::getFullName() const {
|
|
414 Expected<bool> isThin = isThinMember();
|
|
415 if (!isThin)
|
|
416 return isThin.takeError();
|
|
417 assert(isThin.get());
|
|
418 Expected<StringRef> NameOrErr = getName();
|
|
419 if (!NameOrErr)
|
|
420 return NameOrErr.takeError();
|
|
421 StringRef Name = *NameOrErr;
|
|
422 if (sys::path::is_absolute(Name))
|
|
423 return Name;
|
|
424
|
|
425 SmallString<128> FullName = sys::path::parent_path(
|
|
426 Parent->getMemoryBufferRef().getBufferIdentifier());
|
|
427 sys::path::append(FullName, Name);
|
|
428 return StringRef(FullName);
|
|
429 }
|
|
430
|
|
431 Expected<StringRef> Archive::Child::getBuffer() const {
|
|
432 Expected<bool> isThinOrErr = isThinMember();
|
|
433 if (!isThinOrErr)
|
|
434 return isThinOrErr.takeError();
|
|
435 bool isThin = isThinOrErr.get();
|
|
436 if (!isThin) {
|
|
437 Expected<uint32_t> Size = getSize();
|
|
438 if (!Size)
|
|
439 return Size.takeError();
|
100
|
440 return StringRef(Data.data() + StartOfFile, Size.get());
|
|
441 }
|
120
|
442 Expected<std::string> FullNameOrErr = getFullName();
|
|
443 if (!FullNameOrErr)
|
|
444 return FullNameOrErr.takeError();
|
|
445 const std::string &FullName = *FullNameOrErr;
|
95
|
446 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
|
|
447 if (std::error_code EC = Buf.getError())
|
120
|
448 return errorCodeToError(EC);
|
95
|
449 Parent->ThinBuffers.push_back(std::move(*Buf));
|
|
450 return Parent->ThinBuffers.back()->getBuffer();
|
|
451 }
|
|
452
|
120
|
453 Expected<Archive::Child> Archive::Child::getNext() const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
454 size_t SpaceToSkip = Data.size();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
455 // If it's odd, add 1 to make it even.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
456 if (SpaceToSkip & 1)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
457 ++SpaceToSkip;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
458
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
459 const char *NextLoc = Data.data() + SpaceToSkip;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
460
|
100
|
461 // Check to see if this is at the end of the archive.
|
|
462 if (NextLoc == Parent->Data.getBufferEnd())
|
120
|
463 return Child(nullptr, nullptr, nullptr);
|
100
|
464
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
465 // Check to see if this is past the end of the archive.
|
120
|
466 if (NextLoc > Parent->Data.getBufferEnd()) {
|
|
467 std::string Msg("offset to next archive member past the end of the archive "
|
|
468 "after member ");
|
|
469 Expected<StringRef> NameOrErr = getName();
|
|
470 if (!NameOrErr) {
|
|
471 consumeError(NameOrErr.takeError());
|
|
472 uint64_t Offset = Data.data() - Parent->getData().data();
|
|
473 return malformedError(Msg + "at offset " + Twine(Offset));
|
|
474 } else
|
|
475 return malformedError(Msg + NameOrErr.get());
|
|
476 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
477
|
120
|
478 Error Err = Error::success();
|
|
479 Child Ret(Parent, NextLoc, &Err);
|
|
480 if (Err)
|
|
481 return std::move(Err);
|
100
|
482 return Ret;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
483 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
484
|
83
|
485 uint64_t Archive::Child::getChildOffset() const {
|
|
486 const char *a = Parent->Data.getBuffer().data();
|
|
487 const char *c = Data.data();
|
|
488 uint64_t offset = c - a;
|
|
489 return offset;
|
|
490 }
|
|
491
|
120
|
492 Expected<StringRef> Archive::Child::getName() const {
|
|
493 Expected<uint64_t> RawSizeOrErr = getRawSize();
|
|
494 if (!RawSizeOrErr)
|
|
495 return RawSizeOrErr.takeError();
|
|
496 uint64_t RawSize = RawSizeOrErr.get();
|
|
497 Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize);
|
|
498 if (!NameOrErr)
|
|
499 return NameOrErr.takeError();
|
|
500 StringRef Name = NameOrErr.get();
|
|
501 return Name;
|
77
|
502 }
|
|
503
|
120
|
504 Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
|
|
505 Expected<StringRef> NameOrErr = getName();
|
|
506 if (!NameOrErr)
|
|
507 return NameOrErr.takeError();
|
77
|
508 StringRef Name = NameOrErr.get();
|
120
|
509 Expected<StringRef> Buf = getBuffer();
|
|
510 if (!Buf)
|
|
511 return Buf.takeError();
|
95
|
512 return MemoryBufferRef(*Buf, Name);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
513 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
514
|
120
|
515 Expected<std::unique_ptr<Binary>>
|
77
|
516 Archive::Child::getAsBinary(LLVMContext *Context) const {
|
120
|
517 Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
|
|
518 if (!BuffOrErr)
|
|
519 return BuffOrErr.takeError();
|
77
|
520
|
120
|
521 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
|
|
522 if (BinaryOrErr)
|
|
523 return std::move(*BinaryOrErr);
|
|
524 return BinaryOrErr.takeError();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
525 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
526
|
120
|
527 Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
|
|
528 Error Err = Error::success();
|
|
529 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
|
|
530 if (Err)
|
|
531 return std::move(Err);
|
77
|
532 return std::move(Ret);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
533 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
534
|
100
|
535 void Archive::setFirstRegular(const Child &C) {
|
|
536 FirstRegularData = C.Data;
|
|
537 FirstRegularStartOfFile = C.StartOfFile;
|
|
538 }
|
|
539
|
120
|
540 Archive::Archive(MemoryBufferRef Source, Error &Err)
|
100
|
541 : Binary(Binary::ID_Archive, Source) {
|
120
|
542 ErrorAsOutParameter ErrAsOutParam(&Err);
|
83
|
543 StringRef Buffer = Data.getBuffer();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
544 // Check for sufficient magic.
|
83
|
545 if (Buffer.startswith(ThinMagic)) {
|
|
546 IsThin = true;
|
|
547 } else if (Buffer.startswith(Magic)) {
|
|
548 IsThin = false;
|
|
549 } else {
|
120
|
550 Err = make_error<GenericBinaryError>("File too small to be an archive",
|
|
551 object_error::invalid_file_type);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
552 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
553 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
554
|
120
|
555 // Make sure Format is initialized before any call to
|
|
556 // ArchiveMemberHeader::getName() is made. This could be a valid empty
|
|
557 // archive which is the same in all formats. So claiming it to be gnu to is
|
|
558 // fine if not totally correct before we look for a string table or table of
|
|
559 // contents.
|
|
560 Format = K_GNU;
|
|
561
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
562 // Get the special members.
|
120
|
563 child_iterator I = child_begin(Err, false);
|
|
564 if (Err)
|
100
|
565 return;
|
|
566 child_iterator E = child_end();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
567
|
120
|
568 // See if this is a valid empty archive and if so return.
|
100
|
569 if (I == E) {
|
120
|
570 Err = Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
571 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
572 }
|
120
|
573 const Child *C = &*I;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
574
|
100
|
575 auto Increment = [&]() {
|
|
576 ++I;
|
120
|
577 if (Err)
|
100
|
578 return true;
|
120
|
579 C = &*I;
|
100
|
580 return false;
|
|
581 };
|
|
582
|
120
|
583 Expected<StringRef> NameOrErr = C->getRawName();
|
|
584 if (!NameOrErr) {
|
|
585 Err = NameOrErr.takeError();
|
|
586 return;
|
|
587 }
|
|
588 StringRef Name = NameOrErr.get();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
589
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
590 // Below is the pattern that is used to figure out the archive format
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
591 // GNU archive format
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
592 // First member : / (may exist, if it exists, points to the symbol table )
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
593 // Second member : // (may exist, if it exists, points to the string table)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
594 // Note : The string table is used if the filename exceeds 15 characters
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
595 // BSD archive format
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
596 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
597 // There is no string table, if the filename exceeds 15 characters or has a
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
598 // embedded space, the filename has #1/<size>, The size represents the size
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
599 // of the filename that needs to be read after the archive header
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
600 // COFF archive format
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
601 // First member : /
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
602 // Second member : / (provides a directory of symbols)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
603 // Third member : // (may exist, if it exists, contains the string table)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
604 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
605 // even if the string table is empty. However, lib.exe does not in fact
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
606 // seem to create the third member if there's no member whose filename
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
607 // exceeds 15 characters. So the third member is optional.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
608
|
120
|
609 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
|
|
610 if (Name == "__.SYMDEF")
|
|
611 Format = K_BSD;
|
|
612 else // Name == "__.SYMDEF_64"
|
|
613 Format = K_DARWIN64;
|
|
614 // We know that the symbol table is not an external file, but we still must
|
|
615 // check any Expected<> return value.
|
|
616 Expected<StringRef> BufOrErr = C->getBuffer();
|
|
617 if (!BufOrErr) {
|
|
618 Err = BufOrErr.takeError();
|
|
619 return;
|
|
620 }
|
|
621 SymbolTable = BufOrErr.get();
|
100
|
622 if (Increment())
|
|
623 return;
|
|
624 setFirstRegular(*C);
|
|
625
|
120
|
626 Err = Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
627 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
628 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
629
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
630 if (Name.startswith("#1/")) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
631 Format = K_BSD;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
632 // We know this is BSD, so getName will work since there is no string table.
|
120
|
633 Expected<StringRef> NameOrErr = C->getName();
|
|
634 if (!NameOrErr) {
|
|
635 Err = NameOrErr.takeError();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
636 return;
|
120
|
637 }
|
77
|
638 Name = NameOrErr.get();
|
83
|
639 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
|
120
|
640 // We know that the symbol table is not an external file, but we still
|
|
641 // must check any Expected<> return value.
|
|
642 Expected<StringRef> BufOrErr = C->getBuffer();
|
|
643 if (!BufOrErr) {
|
|
644 Err = BufOrErr.takeError();
|
|
645 return;
|
|
646 }
|
|
647 SymbolTable = BufOrErr.get();
|
|
648 if (Increment())
|
|
649 return;
|
|
650 }
|
|
651 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
|
|
652 Format = K_DARWIN64;
|
|
653 // We know that the symbol table is not an external file, but we still
|
|
654 // must check any Expected<> return value.
|
|
655 Expected<StringRef> BufOrErr = C->getBuffer();
|
|
656 if (!BufOrErr) {
|
|
657 Err = BufOrErr.takeError();
|
|
658 return;
|
|
659 }
|
|
660 SymbolTable = BufOrErr.get();
|
100
|
661 if (Increment())
|
|
662 return;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
663 }
|
100
|
664 setFirstRegular(*C);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
665 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
666 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
667
|
83
|
668 // MIPS 64-bit ELF archives use a special format of a symbol table.
|
|
669 // This format is marked by `ar_name` field equals to "/SYM64/".
|
|
670 // For detailed description see page 96 in the following document:
|
|
671 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
|
|
672
|
|
673 bool has64SymTable = false;
|
|
674 if (Name == "/" || Name == "/SYM64/") {
|
120
|
675 // We know that the symbol table is not an external file, but we still
|
|
676 // must check any Expected<> return value.
|
|
677 Expected<StringRef> BufOrErr = C->getBuffer();
|
|
678 if (!BufOrErr) {
|
|
679 Err = BufOrErr.takeError();
|
|
680 return;
|
|
681 }
|
|
682 SymbolTable = BufOrErr.get();
|
83
|
683 if (Name == "/SYM64/")
|
|
684 has64SymTable = true;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
685
|
100
|
686 if (Increment())
|
|
687 return;
|
|
688 if (I == E) {
|
120
|
689 Err = Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
690 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
691 }
|
120
|
692 Expected<StringRef> NameOrErr = C->getRawName();
|
|
693 if (!NameOrErr) {
|
|
694 Err = NameOrErr.takeError();
|
|
695 return;
|
|
696 }
|
|
697 Name = NameOrErr.get();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
698 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
699
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
700 if (Name == "//") {
|
121
|
701 Format = has64SymTable ? K_GNU64 : K_GNU;
|
120
|
702 // The string table is never an external member, but we still
|
|
703 // must check any Expected<> return value.
|
|
704 Expected<StringRef> BufOrErr = C->getBuffer();
|
|
705 if (!BufOrErr) {
|
|
706 Err = BufOrErr.takeError();
|
|
707 return;
|
|
708 }
|
|
709 StringTable = BufOrErr.get();
|
100
|
710 if (Increment())
|
|
711 return;
|
|
712 setFirstRegular(*C);
|
120
|
713 Err = Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
714 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
715 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
716
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
717 if (Name[0] != '/') {
|
121
|
718 Format = has64SymTable ? K_GNU64 : K_GNU;
|
100
|
719 setFirstRegular(*C);
|
120
|
720 Err = Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
721 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
722 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
723
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
724 if (Name != "/") {
|
120
|
725 Err = errorCodeToError(object_error::parse_failed);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
726 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
727 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
728
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
729 Format = K_COFF;
|
120
|
730 // We know that the symbol table is not an external file, but we still
|
|
731 // must check any Expected<> return value.
|
|
732 Expected<StringRef> BufOrErr = C->getBuffer();
|
|
733 if (!BufOrErr) {
|
|
734 Err = BufOrErr.takeError();
|
|
735 return;
|
|
736 }
|
|
737 SymbolTable = BufOrErr.get();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
738
|
100
|
739 if (Increment())
|
|
740 return;
|
|
741
|
|
742 if (I == E) {
|
|
743 setFirstRegular(*C);
|
120
|
744 Err = Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
745 return;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
746 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
747
|
120
|
748 NameOrErr = C->getRawName();
|
|
749 if (!NameOrErr) {
|
|
750 Err = NameOrErr.takeError();
|
|
751 return;
|
|
752 }
|
|
753 Name = NameOrErr.get();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
754
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
755 if (Name == "//") {
|
120
|
756 // The string table is never an external member, but we still
|
|
757 // must check any Expected<> return value.
|
|
758 Expected<StringRef> BufOrErr = C->getBuffer();
|
|
759 if (!BufOrErr) {
|
|
760 Err = BufOrErr.takeError();
|
|
761 return;
|
|
762 }
|
|
763 StringTable = BufOrErr.get();
|
100
|
764 if (Increment())
|
|
765 return;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
766 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
767
|
100
|
768 setFirstRegular(*C);
|
120
|
769 Err = Error::success();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
770 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
771
|
120
|
772 Archive::child_iterator Archive::child_begin(Error &Err,
|
|
773 bool SkipInternal) const {
|
|
774 if (isEmpty())
|
77
|
775 return child_end();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
776
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
777 if (SkipInternal)
|
120
|
778 return child_iterator(Child(this, FirstRegularData,
|
|
779 FirstRegularStartOfFile),
|
|
780 &Err);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
781
|
77
|
782 const char *Loc = Data.getBufferStart() + strlen(Magic);
|
120
|
783 Child C(this, Loc, &Err);
|
|
784 if (Err)
|
|
785 return child_end();
|
|
786 return child_iterator(C, &Err);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
787 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
788
|
77
|
789 Archive::child_iterator Archive::child_end() const {
|
120
|
790 return child_iterator(Child(nullptr, nullptr, nullptr), nullptr);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
791 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
792
|
77
|
793 StringRef Archive::Symbol::getName() const {
|
95
|
794 return Parent->getSymbolTable().begin() + StringIndex;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
795 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
796
|
120
|
797 Expected<Archive::Child> Archive::Symbol::getMember() const {
|
95
|
798 const char *Buf = Parent->getSymbolTable().begin();
|
83
|
799 const char *Offsets = Buf;
|
121
|
800 if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64)
|
83
|
801 Offsets += sizeof(uint64_t);
|
|
802 else
|
|
803 Offsets += sizeof(uint32_t);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
804 uint32_t Offset = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
805 if (Parent->kind() == K_GNU) {
|
95
|
806 Offset = read32be(Offsets + SymbolIndex * 4);
|
121
|
807 } else if (Parent->kind() == K_GNU64) {
|
95
|
808 Offset = read64be(Offsets + SymbolIndex * 8);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
809 } else if (Parent->kind() == K_BSD) {
|
77
|
810 // The SymbolIndex is an index into the ranlib structs that start at
|
|
811 // Offsets (the first uint32_t is the number of bytes of the ranlib
|
|
812 // structs). The ranlib structs are a pair of uint32_t's the first
|
|
813 // being a string table offset and the second being the offset into
|
|
814 // the archive of the member that defines the symbol. Which is what
|
|
815 // is needed here.
|
95
|
816 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
|
120
|
817 } else if (Parent->kind() == K_DARWIN64) {
|
|
818 // The SymbolIndex is an index into the ranlib_64 structs that start at
|
|
819 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
|
|
820 // structs). The ranlib_64 structs are a pair of uint64_t's the first
|
|
821 // being a string table offset and the second being the offset into
|
|
822 // the archive of the member that defines the symbol. Which is what
|
|
823 // is needed here.
|
|
824 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
825 } else {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
826 // Skip offsets.
|
95
|
827 uint32_t MemberCount = read32le(Buf);
|
|
828 Buf += MemberCount * 4 + 4;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
829
|
95
|
830 uint32_t SymbolCount = read32le(Buf);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
831 if (SymbolIndex >= SymbolCount)
|
120
|
832 return errorCodeToError(object_error::parse_failed);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
833
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
834 // Skip SymbolCount to get to the indices table.
|
95
|
835 const char *Indices = Buf + 4;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
836
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
837 // Get the index of the offset in the file member offset table for this
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
838 // symbol.
|
95
|
839 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
840 // Subtract 1 since OffsetIndex is 1 based.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
841 --OffsetIndex;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
842
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
843 if (OffsetIndex >= MemberCount)
|
120
|
844 return errorCodeToError(object_error::parse_failed);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
845
|
95
|
846 Offset = read32le(Offsets + OffsetIndex * 4);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
847 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
848
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
849 const char *Loc = Parent->getData().begin() + Offset;
|
120
|
850 Error Err = Error::success();
|
|
851 Child C(Parent, Loc, &Err);
|
|
852 if (Err)
|
|
853 return std::move(Err);
|
100
|
854 return C;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
855 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
856
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
857 Archive::Symbol Archive::Symbol::getNext() const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
858 Symbol t(*this);
|
77
|
859 if (Parent->kind() == K_BSD) {
|
|
860 // t.StringIndex is an offset from the start of the __.SYMDEF or
|
|
861 // "__.SYMDEF SORTED" member into the string table for the ranlib
|
|
862 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
|
|
863 // offset in the string table for t.SymbolIndex+1 we subtract the
|
|
864 // its offset from the start of the string table for t.SymbolIndex
|
|
865 // and add the offset of the string table for t.SymbolIndex+1.
|
|
866
|
|
867 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
|
|
868 // which is the number of bytes of ranlib structs that follow. The ranlib
|
|
869 // structs are a pair of uint32_t's the first being a string table offset
|
|
870 // and the second being the offset into the archive of the member that
|
|
871 // define the symbol. After that the next uint32_t is the byte count of
|
|
872 // the string table followed by the string table.
|
95
|
873 const char *Buf = Parent->getSymbolTable().begin();
|
77
|
874 uint32_t RanlibCount = 0;
|
95
|
875 RanlibCount = read32le(Buf) / 8;
|
77
|
876 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
|
|
877 // don't change the t.StringIndex as we don't want to reference a ranlib
|
|
878 // past RanlibCount.
|
|
879 if (t.SymbolIndex + 1 < RanlibCount) {
|
|
880 const char *Ranlibs = Buf + 4;
|
|
881 uint32_t CurRanStrx = 0;
|
|
882 uint32_t NextRanStrx = 0;
|
95
|
883 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
|
|
884 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
|
77
|
885 t.StringIndex -= CurRanStrx;
|
|
886 t.StringIndex += NextRanStrx;
|
|
887 }
|
|
888 } else {
|
|
889 // Go to one past next null.
|
95
|
890 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
|
77
|
891 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
892 ++t.SymbolIndex;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
893 return t;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
894 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
895
|
77
|
896 Archive::symbol_iterator Archive::symbol_begin() const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
897 if (!hasSymbolTable())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
898 return symbol_iterator(Symbol(this, 0, 0));
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
899
|
95
|
900 const char *buf = getSymbolTable().begin();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
901 if (kind() == K_GNU) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
902 uint32_t symbol_count = 0;
|
95
|
903 symbol_count = read32be(buf);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
904 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
|
121
|
905 } else if (kind() == K_GNU64) {
|
95
|
906 uint64_t symbol_count = read64be(buf);
|
83
|
907 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
908 } else if (kind() == K_BSD) {
|
77
|
909 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
|
|
910 // which is the number of bytes of ranlib structs that follow. The ranlib
|
|
911 // structs are a pair of uint32_t's the first being a string table offset
|
|
912 // and the second being the offset into the archive of the member that
|
|
913 // define the symbol. After that the next uint32_t is the byte count of
|
|
914 // the string table followed by the string table.
|
|
915 uint32_t ranlib_count = 0;
|
95
|
916 ranlib_count = read32le(buf) / 8;
|
77
|
917 const char *ranlibs = buf + 4;
|
|
918 uint32_t ran_strx = 0;
|
95
|
919 ran_strx = read32le(ranlibs);
|
77
|
920 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
|
|
921 // Skip the byte count of the string table.
|
|
922 buf += sizeof(uint32_t);
|
|
923 buf += ran_strx;
|
120
|
924 } else if (kind() == K_DARWIN64) {
|
|
925 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
|
|
926 // which is the number of bytes of ranlib_64 structs that follow. The
|
|
927 // ranlib_64 structs are a pair of uint64_t's the first being a string
|
|
928 // table offset and the second being the offset into the archive of the
|
|
929 // member that define the symbol. After that the next uint64_t is the byte
|
|
930 // count of the string table followed by the string table.
|
|
931 uint64_t ranlib_count = 0;
|
|
932 ranlib_count = read64le(buf) / 16;
|
|
933 const char *ranlibs = buf + 8;
|
|
934 uint64_t ran_strx = 0;
|
|
935 ran_strx = read64le(ranlibs);
|
|
936 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
|
|
937 // Skip the byte count of the string table.
|
|
938 buf += sizeof(uint64_t);
|
|
939 buf += ran_strx;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
940 } else {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
941 uint32_t member_count = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
942 uint32_t symbol_count = 0;
|
95
|
943 member_count = read32le(buf);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
944 buf += 4 + (member_count * 4); // Skip offsets.
|
95
|
945 symbol_count = read32le(buf);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
946 buf += 4 + (symbol_count * 2); // Skip indices.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
947 }
|
95
|
948 uint32_t string_start_offset = buf - getSymbolTable().begin();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
949 return symbol_iterator(Symbol(this, 0, string_start_offset));
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
950 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
951
|
77
|
952 Archive::symbol_iterator Archive::symbol_end() const {
|
95
|
953 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
|
|
954 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
955
|
95
|
956 uint32_t Archive::getNumberOfSymbols() const {
|
|
957 if (!hasSymbolTable())
|
|
958 return 0;
|
|
959 const char *buf = getSymbolTable().begin();
|
|
960 if (kind() == K_GNU)
|
|
961 return read32be(buf);
|
121
|
962 if (kind() == K_GNU64)
|
95
|
963 return read64be(buf);
|
|
964 if (kind() == K_BSD)
|
|
965 return read32le(buf) / 8;
|
120
|
966 if (kind() == K_DARWIN64)
|
|
967 return read64le(buf) / 16;
|
95
|
968 uint32_t member_count = 0;
|
|
969 member_count = read32le(buf);
|
|
970 buf += 4 + (member_count * 4); // Skip offsets.
|
|
971 return read32le(buf);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
972 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
973
|
120
|
974 Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
|
77
|
975 Archive::symbol_iterator bs = symbol_begin();
|
|
976 Archive::symbol_iterator es = symbol_end();
|
|
977
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
978 for (; bs != es; ++bs) {
|
77
|
979 StringRef SymName = bs->getName();
|
|
980 if (SymName == name) {
|
120
|
981 if (auto MemberOrErr = bs->getMember())
|
|
982 return Child(*MemberOrErr);
|
|
983 else
|
|
984 return MemberOrErr.takeError();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
985 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
986 }
|
120
|
987 return Optional<Child>();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
988 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
989
|
120
|
990 // Returns true if archive file contains no member file.
|
|
991 bool Archive::isEmpty() const { return Data.getBufferSize() == 8; }
|
|
992
|
100
|
993 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
|