1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
//===--- XCOFFObjectFile.cpp - XCOFF object file implementation -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the XCOFFObjectFile class.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/XCOFFObjectFile.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include <cstddef>
#include <cstring>
namespace llvm {
namespace object {
enum { XCOFF32FileHeaderSize = 20 };
static_assert(sizeof(XCOFFFileHeader) == XCOFF32FileHeaderSize,
"Wrong size for XCOFF file header.");
// Sets EC and returns false if there is less than 'Size' bytes left in the
// buffer at 'Offset'.
static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Offset,
uint64_t Size) {
if (M.getBufferSize() < Offset + Size) {
EC = object_error::unexpected_eof;
return false;
}
return true;
}
// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
// Returns unexpected_eof on error.
template <typename T>
static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
const void *Ptr,
const uint64_t Size = sizeof(T)) {
uintptr_t Addr = uintptr_t(Ptr);
if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
return EC;
Obj = reinterpret_cast<const T *>(Addr);
return std::error_code();
}
template <typename T> static const T *viewAs(uintptr_t in) {
return reinterpret_cast<const T *>(in);
}
static StringRef generateStringRef(const char *Name, uint64_t Size) {
auto NulCharPtr = static_cast<const char *>(memchr(Name, '\0', Size));
return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
: StringRef(Name, Size);
}
const XCOFFSectionHeader *XCOFFObjectFile::toSection(DataRefImpl Ref) const {
auto Sec = viewAs<XCOFFSectionHeader>(Ref.p);
#ifndef NDEBUG
if (Sec < SectionHdrTablePtr ||
Sec >= (SectionHdrTablePtr + getNumberOfSections()))
report_fatal_error("Section header outside of section header table.");
uintptr_t Offset = uintptr_t(Sec) - uintptr_t(SectionHdrTablePtr);
if (Offset % getSectionHeaderSize() != 0)
report_fatal_error(
"Section header pointer does not point to a valid section header.");
#endif
return Sec;
}
const XCOFFSymbolEntry *XCOFFObjectFile::toSymbolEntry(DataRefImpl Ref) const {
assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!");
auto SymEntPtr = viewAs<XCOFFSymbolEntry>(Ref.p);
return SymEntPtr;
}
// The next 2 functions are not exactly necessary yet, but they are useful to
// abstract over the size difference between XCOFF32 and XCOFF64 structure
// definitions.
size_t XCOFFObjectFile::getFileHeaderSize() const {
return sizeof(XCOFFFileHeader);
}
size_t XCOFFObjectFile::getSectionHeaderSize() const {
return sizeof(XCOFFSectionHeader);
}
uint16_t XCOFFObjectFile::getMagic() const { return FileHdrPtr->Magic; }
void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
SymEntPtr += SymEntPtr->NumberOfAuxEntries + 1;
Symb.p = reinterpret_cast<uintptr_t>(SymEntPtr);
}
Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const {
const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
if (SymEntPtr->NameInStrTbl.Magic != XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC)
return generateStringRef(SymEntPtr->SymbolName, XCOFF::SymbolNameSize);
// A storage class value with the high-order bit on indicates that the name is
// a symbolic debugger stabstring.
if (SymEntPtr->StorageClass & 0x80)
return StringRef("Unimplemented Debug Name");
uint32_t Offset = SymEntPtr->NameInStrTbl.Offset;
// The byte offset is relative to the start of the string table
// or .debug section. A byte offset value of 0 is a null or zero-length symbol
// name. A byte offset in the range 1 to 3 (inclusive) points into the length
// field; as a soft-error recovery mechanism, we treat such cases as having an
// offset of 0.
if (Offset < 4)
return StringRef(nullptr, 0);
if (StringTable.Data != nullptr && StringTable.Size > Offset)
return (StringTable.Data + Offset);
return make_error<GenericBinaryError>("Symbol Name parse failed",
object_error::parse_failed);
}
Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
uint64_t Result = 0;
llvm_unreachable("Not yet implemented!");
return Result;
}
uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
return toSymbolEntry(Symb)->Value;
}
uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
uint64_t Result = 0;
llvm_unreachable("Not yet implemented!");
return Result;
}
Expected<SymbolRef::Type>
XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const {
llvm_unreachable("Not yet implemented!");
return SymbolRef::ST_Other;
}
Expected<section_iterator>
XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const {
const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
int16_t SectNum = SymEntPtr->SectionNumber;
if (isReservedSectionNumber(SectNum))
return section_end();
const XCOFFSectionHeader *Sec;
if (std::error_code EC = getSectionByNum(SectNum, Sec))
return errorCodeToError(EC);
DataRefImpl SecDRI;
SecDRI.p = reinterpret_cast<uintptr_t>(Sec);
return section_iterator(SectionRef(SecDRI, this));
}
void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const {
const char *Ptr = reinterpret_cast<const char *>(Sec.p);
Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
}
Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
const char *Name = toSection(Sec)->Name;
auto NulCharPtr =
static_cast<const char *>(memchr(Name, '\0', XCOFF::SectionNameSize));
return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
: StringRef(Name, XCOFF::SectionNameSize);
}
uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
return toSection(Sec)->VirtualAddress;
}
uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
// Section numbers in XCOFF are numbered beginning at 1. A section number of
// zero is used to indicate that a symbol is being imported or is undefined.
return toSection(Sec) - SectionHdrTablePtr + 1;
}
uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const {
return toSection(Sec)->SectionSize;
}
Expected<ArrayRef<uint8_t>>
XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
llvm_unreachable("Not yet implemented!");
}
uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
uint64_t Result = 0;
llvm_unreachable("Not yet implemented!");
return Result;
}
bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
bool Result = false;
llvm_unreachable("Not yet implemented!");
return Result;
}
bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const {
return toSection(Sec)->Flags & XCOFF::STYP_TEXT;
}
bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const {
unsigned Flags = toSection(Sec)->Flags;
return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA);
}
bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const {
unsigned Flags = toSection(Sec)->Flags;
return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS);
}
bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
bool Result = false;
llvm_unreachable("Not yet implemented!");
return Result;
}
relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
llvm_unreachable("Not yet implemented!");
return relocation_iterator(RelocationRef());
}
relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const {
llvm_unreachable("Not yet implemented!");
return relocation_iterator(RelocationRef());
}
void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
llvm_unreachable("Not yet implemented!");
return;
}
uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
llvm_unreachable("Not yet implemented!");
uint64_t Result = 0;
return Result;
}
symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
llvm_unreachable("Not yet implemented!");
return symbol_iterator(SymbolRef());
}
uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const {
llvm_unreachable("Not yet implemented!");
uint64_t Result = 0;
return Result;
}
void XCOFFObjectFile::getRelocationTypeName(
DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
llvm_unreachable("Not yet implemented!");
return;
}
uint32_t XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const {
uint32_t Result = 0;
llvm_unreachable("Not yet implemented!");
return Result;
}
basic_symbol_iterator XCOFFObjectFile::symbol_begin() const {
DataRefImpl SymDRI;
SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr);
return basic_symbol_iterator(SymbolRef(SymDRI, this));
}
basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
DataRefImpl SymDRI;
SymDRI.p = reinterpret_cast<uintptr_t>(
SymbolTblPtr + getLogicalNumberOfSymbolTableEntries());
return basic_symbol_iterator(SymbolRef(SymDRI, this));
}
section_iterator XCOFFObjectFile::section_begin() const {
DataRefImpl DRI;
DRI.p = reinterpret_cast<uintptr_t>(SectionHdrTablePtr);
return section_iterator(SectionRef(DRI, this));
}
section_iterator XCOFFObjectFile::section_end() const {
DataRefImpl DRI;
DRI.p =
reinterpret_cast<uintptr_t>(SectionHdrTablePtr + getNumberOfSections());
return section_iterator(SectionRef(DRI, this));
}
uint8_t XCOFFObjectFile::getBytesInAddress() const {
// Only support 32-bit object files for now ...
assert(getFileHeaderSize() == XCOFF32FileHeaderSize);
return 4;
}
StringRef XCOFFObjectFile::getFileFormatName() const {
assert(getFileHeaderSize() == XCOFF32FileHeaderSize);
return "aixcoff-rs6000";
}
Triple::ArchType XCOFFObjectFile::getArch() const {
assert(getFileHeaderSize() == XCOFF32FileHeaderSize);
return Triple::ppc;
}
SubtargetFeatures XCOFFObjectFile::getFeatures() const {
llvm_unreachable("Not yet implemented!");
return SubtargetFeatures();
}
bool XCOFFObjectFile::isRelocatableObject() const {
bool Result = false;
llvm_unreachable("Not yet implemented!");
return Result;
}
Expected<uint64_t> XCOFFObjectFile::getStartAddress() const {
// TODO FIXME Should get from auxiliary_header->o_entry when support for the
// auxiliary_header is added.
return 0;
}
std::error_code
XCOFFObjectFile::getSectionByNum(int16_t Num,
const XCOFFSectionHeader *&Result) const {
if (Num > 0 && static_cast<uint16_t>(Num) <= getNumberOfSections()) {
Result = SectionHdrTablePtr + (Num - 1);
return std::error_code();
}
return object_error::invalid_section_index;
}
Expected<StringRef>
XCOFFObjectFile::getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const {
int16_t SectionNum = SymEntPtr->SectionNumber;
switch (SectionNum) {
case XCOFF::N_DEBUG:
return "N_DEBUG";
case XCOFF::N_ABS:
return "N_ABS";
case XCOFF::N_UNDEF:
return "N_UNDEF";
default: {
const XCOFFSectionHeader *SectHeaderPtr;
std::error_code EC;
if ((EC = getSectionByNum(SectionNum, SectHeaderPtr)))
return errorCodeToError(EC);
else
return generateStringRef(SectHeaderPtr->Name, XCOFF::SectionNameSize);
}
}
}
bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
return (SectionNumber <= 0 && SectionNumber >= -2);
}
uint16_t XCOFFObjectFile::getNumberOfSections() const {
return FileHdrPtr->NumberOfSections;
}
int32_t XCOFFObjectFile::getTimeStamp() const { return FileHdrPtr->TimeStamp; }
uint32_t XCOFFObjectFile::getSymbolTableOffset() const {
return FileHdrPtr->SymbolTableOffset;
}
int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries() const {
return FileHdrPtr->NumberOfSymTableEntries;
}
uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries() const {
return (FileHdrPtr->NumberOfSymTableEntries >= 0
? FileHdrPtr->NumberOfSymTableEntries
: 0);
}
uint16_t XCOFFObjectFile::getOptionalHeaderSize() const {
return FileHdrPtr->AuxHeaderSize;
}
XCOFFObjectFile::XCOFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
: ObjectFile(Binary::ID_XCOFF32, Object) {
// Current location within the file.
uint64_t CurPtr = 0;
if ((EC = getObject(FileHdrPtr, Data, base() + CurPtr)))
return;
CurPtr += getFileHeaderSize();
// TODO FIXME we don't have support for an optional header yet, so just skip
// past it.
CurPtr += FileHdrPtr->AuxHeaderSize;
if (getNumberOfSections() != 0) {
if ((EC = getObject(SectionHdrTablePtr, Data, base() + CurPtr,
getNumberOfSections() * getSectionHeaderSize())))
return;
}
if (getLogicalNumberOfSymbolTableEntries() == 0)
return;
// Get pointer to the symbol table.
CurPtr = FileHdrPtr->SymbolTableOffset;
uint64_t SymbolTableSize = (uint64_t)(sizeof(XCOFFSymbolEntry)) *
getLogicalNumberOfSymbolTableEntries();
if ((EC = getObject(SymbolTblPtr, Data, base() + CurPtr, SymbolTableSize)))
return;
// Move pointer to the string table.
CurPtr += SymbolTableSize;
if (CurPtr + 4 > Data.getBufferSize())
return;
StringTable.Size = support::endian::read32be(base() + CurPtr);
if (StringTable.Size <= 4)
return;
// Check for whether the String table has the size indicated by length
// field
if (!checkSize(Data, EC, CurPtr, StringTable.Size))
return;
StringTable.Data = reinterpret_cast<const char *>(base() + CurPtr);
if (StringTable.Data[StringTable.Size - 1] != '\0') {
EC = object_error::string_table_non_null_end;
return;
}
}
Expected<std::unique_ptr<ObjectFile>>
ObjectFile::createXCOFFObjectFile(MemoryBufferRef Object) {
StringRef Data = Object.getBuffer();
file_magic Type = identify_magic(Data);
std::error_code EC;
std::unique_ptr<ObjectFile> Ret;
if (Type == file_magic::xcoff_object_32) {
Ret.reset(new XCOFFObjectFile(Object, EC));
} else {
llvm_unreachable("Encountered an unexpected binary file type!");
}
if (EC)
return errorCodeToError(EC);
return std::move(Ret);
}
} // namespace object
} // namespace llvm
|