Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/XCOFFObjectFile.h
Line
Count
Source (jump to first uncovered line)
1
//===- XCOFFObjectFile.h - XCOFF object file implementation -----*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file declares the XCOFFObjectFile class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_OBJECT_XCOFFOBJECTFILE_H
14
#define LLVM_OBJECT_XCOFFOBJECTFILE_H
15
16
#include "llvm/BinaryFormat/XCOFF.h"
17
#include "llvm/Object/ObjectFile.h"
18
19
namespace llvm {
20
namespace object {
21
22
struct XCOFFFileHeader32 {
23
  support::ubig16_t Magic;
24
  support::ubig16_t NumberOfSections;
25
26
  // Unix time value, value of 0 indicates no timestamp.
27
  // Negative values are reserved.
28
  support::big32_t TimeStamp;
29
30
  support::ubig32_t SymbolTableOffset; // File offset to symbol table.
31
  support::big32_t NumberOfSymTableEntries;
32
  support::ubig16_t AuxHeaderSize;
33
  support::ubig16_t Flags;
34
};
35
36
struct XCOFFFileHeader64 {
37
  support::ubig16_t Magic;
38
  support::ubig16_t NumberOfSections;
39
40
  // Unix time value, value of 0 indicates no timestamp.
41
  // Negative values are reserved.
42
  support::big32_t TimeStamp;
43
44
  support::ubig64_t SymbolTableOffset; // File offset to symbol table.
45
  support::ubig16_t AuxHeaderSize;
46
  support::ubig16_t Flags;
47
  support::ubig32_t NumberOfSymTableEntries;
48
};
49
50
struct XCOFFSectionHeader32 {
51
  char Name[XCOFF::SectionNameSize];
52
  support::ubig32_t PhysicalAddress;
53
  support::ubig32_t VirtualAddress;
54
  support::ubig32_t SectionSize;
55
  support::ubig32_t FileOffsetToRawData;
56
  support::ubig32_t FileOffsetToRelocationInfo;
57
  support::ubig32_t FileOffsetToLineNumberInfo;
58
  support::ubig16_t NumberOfRelocations;
59
  support::ubig16_t NumberOfLineNumbers;
60
  support::big32_t Flags;
61
62
  StringRef getName() const;
63
};
64
65
struct XCOFFSectionHeader64 {
66
  char Name[XCOFF::SectionNameSize];
67
  support::ubig64_t PhysicalAddress;
68
  support::ubig64_t VirtualAddress;
69
  support::ubig64_t SectionSize;
70
  support::big64_t FileOffsetToRawData;
71
  support::big64_t FileOffsetToRelocationInfo;
72
  support::big64_t FileOffsetToLineNumberInfo;
73
  support::ubig32_t NumberOfRelocations;
74
  support::ubig32_t NumberOfLineNumbers;
75
  support::big32_t Flags;
76
  char Padding[4];
77
78
  StringRef getName() const;
79
};
80
81
struct XCOFFSymbolEntry {
82
  enum { NAME_IN_STR_TBL_MAGIC = 0x0 };
83
  typedef struct {
84
    support::big32_t Magic; // Zero indicates name in string table.
85
    support::ubig32_t Offset;
86
  } NameInStrTblType;
87
88
  typedef struct {
89
    uint8_t LanguageId;
90
    uint8_t CpuTypeId;
91
  } CFileLanguageIdAndTypeIdType;
92
93
  union {
94
    char SymbolName[XCOFF::SymbolNameSize];
95
    NameInStrTblType NameInStrTbl;
96
  };
97
98
  support::ubig32_t Value; // Symbol value; storage class-dependent.
99
  support::big16_t SectionNumber;
100
101
  union {
102
    support::ubig16_t SymbolType;
103
    CFileLanguageIdAndTypeIdType CFileLanguageIdAndTypeId;
104
  };
105
106
  XCOFF::StorageClass StorageClass;
107
  uint8_t NumberOfAuxEntries;
108
};
109
110
struct XCOFFStringTable {
111
  uint32_t Size;
112
  const char *Data;
113
};
114
115
class XCOFFObjectFile : public ObjectFile {
116
private:
117
  const void *FileHeader = nullptr;
118
  const void *SectionHeaderTable = nullptr;
119
120
  const XCOFFSymbolEntry *SymbolTblPtr = nullptr;
121
  XCOFFStringTable StringTable = {0, nullptr};
122
123
  const XCOFFFileHeader32 *fileHeader32() const;
124
  const XCOFFFileHeader64 *fileHeader64() const;
125
126
  const XCOFFSectionHeader32 *sectionHeaderTable32() const;
127
  const XCOFFSectionHeader64 *sectionHeaderTable64() const;
128
129
  size_t getFileHeaderSize() const;
130
  size_t getSectionHeaderSize() const;
131
132
  const XCOFFSectionHeader32 *toSection32(DataRefImpl Ref) const;
133
  const XCOFFSectionHeader64 *toSection64(DataRefImpl Ref) const;
134
  void checkSectionAddress(uintptr_t Addr, uintptr_t TableAddr) const;
135
  uintptr_t getSectionHeaderTableAddress() const;
136
137
  // This returns a pointer to the start of the storage for the name field of
138
  // the 32-bit or 64-bit SectionHeader struct. This string is *not* necessarily
139
  // null-terminated.
140
  const char *getSectionNameInternal(DataRefImpl Sec) const;
141
142
  int32_t getSectionFlags(DataRefImpl Sec) const;
143
144
  static bool isReservedSectionNumber(int16_t SectionNumber);
145
  Expected<DataRefImpl> getSectionByNum(int16_t Num) const;
146
147
  // Constructor and "create" factory function. The constructor is only a thin
148
  // wrapper around the base constructor. The "create" function fills out the
149
  // XCOFF-specific information and performs the error checking along the way.
150
  XCOFFObjectFile(unsigned Type, MemoryBufferRef Object);
151
  static Expected<std::unique_ptr<XCOFFObjectFile>> create(unsigned Type,
152
                                                           MemoryBufferRef MBR);
153
154
  // Helper for parsing the StringTable. Returns an 'Error' if parsing failed
155
  // and an XCOFFStringTable if parsing succeeded.
156
  static Expected<XCOFFStringTable> parseStringTable(const XCOFFObjectFile *Obj,
157
                                                     uint64_t Offset);
158
159
  // Make a friend so it can call the private 'create' function.
160
  friend Expected<std::unique_ptr<ObjectFile>>
161
  ObjectFile::createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType);
162
163
public:
164
  // Interface inherited from base classes.
165
  void moveSymbolNext(DataRefImpl &Symb) const override;
166
  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
167
  basic_symbol_iterator symbol_begin() const override;
168
  basic_symbol_iterator symbol_end() const override;
169
170
  Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
171
  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
172
  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
173
  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
174
  Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
175
  Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
176
177
  void moveSectionNext(DataRefImpl &Sec) const override;
178
  Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
179
  uint64_t getSectionAddress(DataRefImpl Sec) const override;
180
  uint64_t getSectionIndex(DataRefImpl Sec) const override;
181
  uint64_t getSectionSize(DataRefImpl Sec) const override;
182
  Expected<ArrayRef<uint8_t>>
183
  getSectionContents(DataRefImpl Sec) const override;
184
  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
185
  bool isSectionCompressed(DataRefImpl Sec) const override;
186
  bool isSectionText(DataRefImpl Sec) const override;
187
  bool isSectionData(DataRefImpl Sec) const override;
188
  bool isSectionBSS(DataRefImpl Sec) const override;
189
190
  bool isSectionVirtual(DataRefImpl Sec) const override;
191
  relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
192
  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
193
194
  void moveRelocationNext(DataRefImpl &Rel) const override;
195
  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
196
  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
197
  uint64_t getRelocationType(DataRefImpl Rel) const override;
198
  void getRelocationTypeName(DataRefImpl Rel,
199
                             SmallVectorImpl<char> &Result) const override;
200
201
  section_iterator section_begin() const override;
202
  section_iterator section_end() const override;
203
  uint8_t getBytesInAddress() const override;
204
  StringRef getFileFormatName() const override;
205
  Triple::ArchType getArch() const override;
206
  SubtargetFeatures getFeatures() const override;
207
  Expected<uint64_t> getStartAddress() const override;
208
  bool isRelocatableObject() const override;
209
210
  // Below here is the non-inherited interface.
211
  bool is64Bit() const;
212
213
0
  const XCOFFSymbolEntry *getPointerToSymbolTable() const {
214
0
    assert(!is64Bit() && "Symbol table handling not supported yet.");
215
0
    return SymbolTblPtr;
216
0
  }
217
218
  Expected<StringRef>
219
  getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const;
220
221
  const XCOFFSymbolEntry *toSymbolEntry(DataRefImpl Ref) const;
222
223
  // File header related interfaces.
224
  uint16_t getMagic() const;
225
  uint16_t getNumberOfSections() const;
226
  int32_t getTimeStamp() const;
227
228
  // Symbol table offset and entry count are handled differently between
229
  // XCOFF32 and XCOFF64.
230
  uint32_t getSymbolTableOffset32() const;
231
  uint64_t getSymbolTableOffset64() const;
232
233
  // Note that this value is signed and might return a negative value. Negative
234
  // values are reserved for future use.
235
  int32_t getRawNumberOfSymbolTableEntries32() const;
236
237
  // The sanitized value appropriate to use as an index into the symbol table.
238
  uint32_t getLogicalNumberOfSymbolTableEntries32() const;
239
240
  uint32_t getNumberOfSymbolTableEntries64() const;
241
242
  uint16_t getOptionalHeaderSize() const;
243
  uint16_t getFlags() const;
244
245
  // Section header table related interfaces.
246
  ArrayRef<XCOFFSectionHeader32> sections32() const;
247
  ArrayRef<XCOFFSectionHeader64> sections64() const;
248
}; // XCOFFObjectFile
249
250
} // namespace object
251
} // namespace llvm
252
253
#endif // LLVM_OBJECT_XCOFFOBJECTFILE_H