File: | lib/MC/XCOFFObjectWriter.cpp |
Warning: | line 225, column 13 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- lib/MC/XCOFFObjectWriter.cpp - XCOFF file writer ------------------===// | |||
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 implements XCOFF object file writer information. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "llvm/BinaryFormat/XCOFF.h" | |||
14 | #include "llvm/MC/MCAsmLayout.h" | |||
15 | #include "llvm/MC/MCAssembler.h" | |||
16 | #include "llvm/MC/MCObjectWriter.h" | |||
17 | #include "llvm/MC/MCSectionXCOFF.h" | |||
18 | #include "llvm/MC/MCSymbolXCOFF.h" | |||
19 | #include "llvm/MC/MCValue.h" | |||
20 | #include "llvm/MC/MCXCOFFObjectWriter.h" | |||
21 | #include "llvm/MC/StringTableBuilder.h" | |||
22 | #include "llvm/Support/Error.h" | |||
23 | #include "llvm/Support/MathExtras.h" | |||
24 | ||||
25 | #include <deque> | |||
26 | ||||
27 | using namespace llvm; | |||
28 | ||||
29 | // An XCOFF object file has a limited set of predefined sections. The most | |||
30 | // important ones for us (right now) are: | |||
31 | // .text --> contains program code and read-only data. | |||
32 | // .data --> contains initialized data, function descriptors, and the TOC. | |||
33 | // .bss --> contains uninitialized data. | |||
34 | // Each of these sections is composed of 'Control Sections'. A Control Section | |||
35 | // is more commonly referred to as a csect. A csect is an indivisible unit of | |||
36 | // code or data, and acts as a container for symbols. A csect is mapped | |||
37 | // into a section based on its storage-mapping class, with the exception of | |||
38 | // XMC_RW which gets mapped to either .data or .bss based on whether it's | |||
39 | // explicitly initialized or not. | |||
40 | // | |||
41 | // We don't represent the sections in the MC layer as there is nothing | |||
42 | // interesting about them at at that level: they carry information that is | |||
43 | // only relevant to the ObjectWriter, so we materialize them in this class. | |||
44 | namespace { | |||
45 | ||||
46 | constexpr unsigned DefaultSectionAlign = 4; | |||
47 | ||||
48 | // Packs the csect's alignment and type into a byte. | |||
49 | uint8_t getEncodedType(const MCSectionXCOFF *); | |||
50 | ||||
51 | // Wrapper around an MCSymbolXCOFF. | |||
52 | struct Symbol { | |||
53 | const MCSymbolXCOFF *const MCSym; | |||
54 | uint32_t SymbolTableIndex; | |||
55 | ||||
56 | XCOFF::StorageClass getStorageClass() const { | |||
57 | return MCSym->getStorageClass(); | |||
58 | } | |||
59 | StringRef getName() const { return MCSym->getName(); } | |||
60 | bool nameInStringTable() const { | |||
61 | return MCSym->getName().size() > XCOFF::NameSize; | |||
62 | } | |||
63 | ||||
64 | Symbol(const MCSymbolXCOFF *MCSym) : MCSym(MCSym), SymbolTableIndex(-1) {} | |||
65 | }; | |||
66 | ||||
67 | // Wrapper for an MCSectionXCOFF. | |||
68 | struct ControlSection { | |||
69 | const MCSectionXCOFF *const MCCsect; | |||
70 | uint32_t SymbolTableIndex; | |||
71 | uint32_t Address; | |||
72 | uint32_t Size; | |||
73 | ||||
74 | SmallVector<Symbol, 1> Syms; | |||
75 | ||||
76 | ControlSection(const MCSectionXCOFF *MCSec) | |||
77 | : MCCsect(MCSec), SymbolTableIndex(-1), Address(-1) {} | |||
78 | }; | |||
79 | ||||
80 | // Represents the data related to a section excluding the csects that make up | |||
81 | // the raw data of the section. The csects are stored separately as not all | |||
82 | // sections contain csects, and some sections contain csects which are better | |||
83 | // stored separately, e.g. the .data section containing read-write, descriptor, | |||
84 | // TOCBase and TOC-entry csects. | |||
85 | struct Section { | |||
86 | char Name[XCOFF::NameSize]; | |||
87 | // The physical/virtual address of the section. For an object file | |||
88 | // these values are equivalent. | |||
89 | uint32_t Address; | |||
90 | uint32_t Size; | |||
91 | uint32_t FileOffsetToData; | |||
92 | uint32_t FileOffsetToRelocations; | |||
93 | uint32_t RelocationCount; | |||
94 | int32_t Flags; | |||
95 | ||||
96 | uint16_t Index; | |||
97 | ||||
98 | // Virtual sections do not need storage allocated in the object file. | |||
99 | const bool IsVirtual; | |||
100 | ||||
101 | void reset() { | |||
102 | Address = 0; | |||
103 | Size = 0; | |||
104 | FileOffsetToData = 0; | |||
105 | FileOffsetToRelocations = 0; | |||
106 | RelocationCount = 0; | |||
107 | Index = -1; | |||
108 | } | |||
109 | ||||
110 | Section(const char *N, XCOFF::SectionTypeFlags Flags, bool IsVirtual) | |||
111 | : Address(0), Size(0), FileOffsetToData(0), FileOffsetToRelocations(0), | |||
112 | RelocationCount(0), Flags(Flags), Index(-1), IsVirtual(IsVirtual) { | |||
113 | strncpy(Name, N, XCOFF::NameSize); | |||
114 | } | |||
115 | }; | |||
116 | ||||
117 | class XCOFFObjectWriter : public MCObjectWriter { | |||
118 | // Type to be used for a container representing a set of csects with | |||
119 | // (approximately) the same storage mapping class. For example all the csects | |||
120 | // with a storage mapping class of `xmc_pr` will get placed into the same | |||
121 | // container. | |||
122 | using ControlSections = std::deque<ControlSection>; | |||
123 | ||||
124 | support::endian::Writer W; | |||
125 | std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter; | |||
126 | StringTableBuilder Strings; | |||
127 | ||||
128 | // The non-empty sections, in the order they will appear in the section header | |||
129 | // table. | |||
130 | std::vector<Section *> Sections; | |||
131 | ||||
132 | // The Predefined sections. | |||
133 | Section Text; | |||
134 | Section BSS; | |||
135 | ||||
136 | // ControlSections. These store the csects which make up different parts of | |||
137 | // the sections. Should have one for each set of csects that get mapped into | |||
138 | // the same section and get handled in a 'similar' way. | |||
139 | ControlSections ProgramCodeCsects; | |||
140 | ControlSections BSSCsects; | |||
141 | ||||
142 | uint32_t SymbolTableEntryCount = 0; | |||
143 | uint32_t SymbolTableOffset = 0; | |||
144 | ||||
145 | virtual void reset() override; | |||
146 | ||||
147 | void executePostLayoutBinding(MCAssembler &, const MCAsmLayout &) override; | |||
148 | ||||
149 | void recordRelocation(MCAssembler &, const MCAsmLayout &, const MCFragment *, | |||
150 | const MCFixup &, MCValue, uint64_t &) override; | |||
151 | ||||
152 | uint64_t writeObject(MCAssembler &, const MCAsmLayout &) override; | |||
153 | ||||
154 | void writeFileHeader(); | |||
155 | void writeSectionHeaderTable(); | |||
156 | void writeSymbolTable(); | |||
157 | ||||
158 | // Called after all the csects and symbols have been processed by | |||
159 | // `executePostLayoutBinding`, this function handles building up the majority | |||
160 | // of the structures in the object file representation. Namely: | |||
161 | // *) Calculates physical/virtual addresses, raw-pointer offsets, and section | |||
162 | // sizes. | |||
163 | // *) Assigns symbol table indices. | |||
164 | // *) Builds up the section header table by adding any non-empty sections to | |||
165 | // `Sections`. | |||
166 | void assignAddressesAndIndices(const llvm::MCAsmLayout &); | |||
167 | ||||
168 | bool | |||
169 | needsAuxiliaryHeader() const { /* TODO aux header support not implemented. */ | |||
170 | return false; | |||
171 | } | |||
172 | ||||
173 | // Returns the size of the auxiliary header to be written to the object file. | |||
174 | size_t auxiliaryHeaderSize() const { | |||
175 | assert(!needsAuxiliaryHeader() &&((!needsAuxiliaryHeader() && "Auxiliary header support not implemented." ) ? static_cast<void> (0) : __assert_fail ("!needsAuxiliaryHeader() && \"Auxiliary header support not implemented.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 176, __PRETTY_FUNCTION__)) | |||
176 | "Auxiliary header support not implemented.")((!needsAuxiliaryHeader() && "Auxiliary header support not implemented." ) ? static_cast<void> (0) : __assert_fail ("!needsAuxiliaryHeader() && \"Auxiliary header support not implemented.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 176, __PRETTY_FUNCTION__)); | |||
177 | return 0; | |||
178 | } | |||
179 | ||||
180 | public: | |||
181 | XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, | |||
182 | raw_pwrite_stream &OS); | |||
183 | }; | |||
184 | ||||
185 | XCOFFObjectWriter::XCOFFObjectWriter( | |||
186 | std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) | |||
187 | : W(OS, support::big), TargetObjectWriter(std::move(MOTW)), | |||
188 | Strings(StringTableBuilder::XCOFF), | |||
189 | Text(".text", XCOFF::STYP_TEXT, /* IsVirtual */ false), | |||
190 | BSS(".bss", XCOFF::STYP_BSS, /* IsVirtual */ true) {} | |||
191 | ||||
192 | void XCOFFObjectWriter::reset() { | |||
193 | // Reset any sections we have written to, and empty the section header table. | |||
194 | for (auto *Sec : Sections) | |||
195 | Sec->reset(); | |||
196 | Sections.clear(); | |||
197 | ||||
198 | // Clear any csects we have stored. | |||
199 | ProgramCodeCsects.clear(); | |||
200 | BSSCsects.clear(); | |||
201 | ||||
202 | // Reset the symbol table and string table. | |||
203 | SymbolTableEntryCount = 0; | |||
204 | SymbolTableOffset = 0; | |||
205 | Strings.clear(); | |||
206 | ||||
207 | MCObjectWriter::reset(); | |||
208 | } | |||
209 | ||||
210 | void XCOFFObjectWriter::executePostLayoutBinding( | |||
211 | llvm::MCAssembler &Asm, const llvm::MCAsmLayout &Layout) { | |||
212 | if (TargetObjectWriter->is64Bit()) | |||
| ||||
213 | report_fatal_error("64-bit XCOFF object files are not supported yet."); | |||
214 | ||||
215 | // Maps the MC Section representation to its corresponding ControlSection | |||
216 | // wrapper. Needed for finding the ControlSection to insert an MCSymbol into | |||
217 | // from its containing MCSectionXCOFF. | |||
218 | DenseMap<const MCSectionXCOFF *, ControlSection *> WrapperMap; | |||
219 | ||||
220 | for (const auto &S : Asm) { | |||
221 | const MCSectionXCOFF *MCSec = dyn_cast<const MCSectionXCOFF>(&S); | |||
222 | assert(WrapperMap.find(MCSec) == WrapperMap.end() &&((WrapperMap.find(MCSec) == WrapperMap.end() && "Cannot add a csect twice." ) ? static_cast<void> (0) : __assert_fail ("WrapperMap.find(MCSec) == WrapperMap.end() && \"Cannot add a csect twice.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 223, __PRETTY_FUNCTION__)) | |||
223 | "Cannot add a csect twice.")((WrapperMap.find(MCSec) == WrapperMap.end() && "Cannot add a csect twice." ) ? static_cast<void> (0) : __assert_fail ("WrapperMap.find(MCSec) == WrapperMap.end() && \"Cannot add a csect twice.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 223, __PRETTY_FUNCTION__)); | |||
224 | ||||
225 | switch (MCSec->getMappingClass()) { | |||
| ||||
226 | case XCOFF::XMC_PR: | |||
227 | assert(XCOFF::XTY_SD == MCSec->getCSectType() &&((XCOFF::XTY_SD == MCSec->getCSectType() && "Only an initialized csect can contain program code." ) ? static_cast<void> (0) : __assert_fail ("XCOFF::XTY_SD == MCSec->getCSectType() && \"Only an initialized csect can contain program code.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 228, __PRETTY_FUNCTION__)) | |||
228 | "Only an initialized csect can contain program code.")((XCOFF::XTY_SD == MCSec->getCSectType() && "Only an initialized csect can contain program code." ) ? static_cast<void> (0) : __assert_fail ("XCOFF::XTY_SD == MCSec->getCSectType() && \"Only an initialized csect can contain program code.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 228, __PRETTY_FUNCTION__)); | |||
229 | // TODO FIXME Handle .text section csects. | |||
230 | break; | |||
231 | case XCOFF::XMC_RW: | |||
232 | if (XCOFF::XTY_CM == MCSec->getCSectType()) { | |||
233 | BSSCsects.emplace_back(MCSec); | |||
234 | WrapperMap[MCSec] = &BSSCsects.back(); | |||
235 | break; | |||
236 | } | |||
237 | report_fatal_error("Unhandled mapping of read-write csect to section."); | |||
238 | case XCOFF::XMC_TC0: | |||
239 | // TODO FIXME Handle emiting the TOC base. | |||
240 | break; | |||
241 | case XCOFF::XMC_BS: | |||
242 | assert(XCOFF::XTY_CM == MCSec->getCSectType() &&((XCOFF::XTY_CM == MCSec->getCSectType() && "Mapping invalid csect. CSECT with bss storage class must be " "common type.") ? static_cast<void> (0) : __assert_fail ("XCOFF::XTY_CM == MCSec->getCSectType() && \"Mapping invalid csect. CSECT with bss storage class must be \" \"common type.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 244, __PRETTY_FUNCTION__)) | |||
243 | "Mapping invalid csect. CSECT with bss storage class must be "((XCOFF::XTY_CM == MCSec->getCSectType() && "Mapping invalid csect. CSECT with bss storage class must be " "common type.") ? static_cast<void> (0) : __assert_fail ("XCOFF::XTY_CM == MCSec->getCSectType() && \"Mapping invalid csect. CSECT with bss storage class must be \" \"common type.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 244, __PRETTY_FUNCTION__)) | |||
244 | "common type.")((XCOFF::XTY_CM == MCSec->getCSectType() && "Mapping invalid csect. CSECT with bss storage class must be " "common type.") ? static_cast<void> (0) : __assert_fail ("XCOFF::XTY_CM == MCSec->getCSectType() && \"Mapping invalid csect. CSECT with bss storage class must be \" \"common type.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 244, __PRETTY_FUNCTION__)); | |||
245 | BSSCsects.emplace_back(MCSec); | |||
246 | WrapperMap[MCSec] = &BSSCsects.back(); | |||
247 | break; | |||
248 | default: | |||
249 | report_fatal_error("Unhandled mapping of csect to section."); | |||
250 | } | |||
251 | } | |||
252 | ||||
253 | for (const MCSymbol &S : Asm.symbols()) { | |||
254 | // Nothing to do for temporary symbols. | |||
255 | if (S.isTemporary()) | |||
256 | continue; | |||
257 | const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(&S); | |||
258 | ||||
259 | // Map the symbol into its containing csect. | |||
260 | const MCSectionXCOFF *ContainingCsect = XSym->getContainingCsect(); | |||
261 | assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() &&((WrapperMap.find(ContainingCsect) != WrapperMap.end() && "Expected containing csect to exist in map") ? static_cast< void> (0) : __assert_fail ("WrapperMap.find(ContainingCsect) != WrapperMap.end() && \"Expected containing csect to exist in map\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 262, __PRETTY_FUNCTION__)) | |||
262 | "Expected containing csect to exist in map")((WrapperMap.find(ContainingCsect) != WrapperMap.end() && "Expected containing csect to exist in map") ? static_cast< void> (0) : __assert_fail ("WrapperMap.find(ContainingCsect) != WrapperMap.end() && \"Expected containing csect to exist in map\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 262, __PRETTY_FUNCTION__)); | |||
263 | ||||
264 | // Lookup the containing csect and add the symbol to it. | |||
265 | WrapperMap[ContainingCsect]->Syms.emplace_back(XSym); | |||
266 | ||||
267 | // If the name does not fit in the storage provided in the symbol table | |||
268 | // entry, add it to the string table. | |||
269 | const Symbol &WrapperSym = WrapperMap[ContainingCsect]->Syms.back(); | |||
270 | if (WrapperSym.nameInStringTable()) { | |||
271 | Strings.add(WrapperSym.getName()); | |||
272 | } | |||
273 | } | |||
274 | ||||
275 | Strings.finalize(); | |||
276 | assignAddressesAndIndices(Layout); | |||
277 | } | |||
278 | ||||
279 | void XCOFFObjectWriter::recordRelocation(MCAssembler &, const MCAsmLayout &, | |||
280 | const MCFragment *, const MCFixup &, | |||
281 | MCValue, uint64_t &) { | |||
282 | report_fatal_error("XCOFF relocations not supported."); | |||
283 | } | |||
284 | ||||
285 | uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &) { | |||
286 | // We always emit a timestamp of 0 for reproducibility, so ensure incremental | |||
287 | // linking is not enabled, in case, like with Windows COFF, such a timestamp | |||
288 | // is incompatible with incremental linking of XCOFF. | |||
289 | if (Asm.isIncrementalLinkerCompatible()) | |||
290 | report_fatal_error("Incremental linking not supported for XCOFF."); | |||
291 | ||||
292 | if (TargetObjectWriter->is64Bit()) | |||
293 | report_fatal_error("64-bit XCOFF object files are not supported yet."); | |||
294 | ||||
295 | uint64_t StartOffset = W.OS.tell(); | |||
296 | ||||
297 | writeFileHeader(); | |||
298 | writeSectionHeaderTable(); | |||
299 | // TODO writeSections(); | |||
300 | // TODO writeRelocations(); | |||
301 | ||||
302 | // TODO FIXME Finalize symbols. | |||
303 | writeSymbolTable(); | |||
304 | // Write the string table. | |||
305 | Strings.write(W.OS); | |||
306 | ||||
307 | return W.OS.tell() - StartOffset; | |||
308 | } | |||
309 | ||||
310 | void XCOFFObjectWriter::writeFileHeader() { | |||
311 | // Magic. | |||
312 | W.write<uint16_t>(0x01df); | |||
313 | // Number of sections. | |||
314 | W.write<uint16_t>(Sections.size()); | |||
315 | // Timestamp field. For reproducible output we write a 0, which represents no | |||
316 | // timestamp. | |||
317 | W.write<int32_t>(0); | |||
318 | // Byte Offset to the start of the symbol table. | |||
319 | W.write<uint32_t>(SymbolTableOffset); | |||
320 | // Number of entries in the symbol table. | |||
321 | W.write<int32_t>(SymbolTableEntryCount); | |||
322 | // Size of the optional header. | |||
323 | W.write<uint16_t>(0); | |||
324 | // Flags. | |||
325 | W.write<uint16_t>(0); | |||
326 | } | |||
327 | ||||
328 | void XCOFFObjectWriter::writeSectionHeaderTable() { | |||
329 | for (const auto *Sec : Sections) { | |||
330 | // Write Name. | |||
331 | ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize); | |||
332 | W.write(NameRef); | |||
333 | ||||
334 | // Write the Physical Address and Virtual Address. In an object file these | |||
335 | // are the same. | |||
336 | W.write<uint32_t>(Sec->Address); | |||
337 | W.write<uint32_t>(Sec->Address); | |||
338 | ||||
339 | W.write<uint32_t>(Sec->Size); | |||
340 | W.write<uint32_t>(Sec->FileOffsetToData); | |||
341 | ||||
342 | // Relocation pointer and Lineno pointer. Not supported yet. | |||
343 | W.write<uint32_t>(0); | |||
344 | W.write<uint32_t>(0); | |||
345 | ||||
346 | // Relocation and line-number counts. Not supported yet. | |||
347 | W.write<uint16_t>(0); | |||
348 | W.write<uint16_t>(0); | |||
349 | ||||
350 | W.write<int32_t>(Sec->Flags); | |||
351 | } | |||
352 | } | |||
353 | ||||
354 | void XCOFFObjectWriter::writeSymbolTable() { | |||
355 | assert(ProgramCodeCsects.size() == 0 && ".text csects not handled yet.")((ProgramCodeCsects.size() == 0 && ".text csects not handled yet." ) ? static_cast<void> (0) : __assert_fail ("ProgramCodeCsects.size() == 0 && \".text csects not handled yet.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 355, __PRETTY_FUNCTION__)); | |||
356 | ||||
357 | // The BSS Section is special in that the csects must contain a single symbol, | |||
358 | // and the contained symbol cannot be represented in the symbol table as a | |||
359 | // label definition. | |||
360 | for (auto &Sec : BSSCsects) { | |||
361 | assert(Sec.Syms.size() == 1 &&((Sec.Syms.size() == 1 && "Uninitialized csect cannot contain more then 1 symbol." ) ? static_cast<void> (0) : __assert_fail ("Sec.Syms.size() == 1 && \"Uninitialized csect cannot contain more then 1 symbol.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 362, __PRETTY_FUNCTION__)) | |||
362 | "Uninitialized csect cannot contain more then 1 symbol.")((Sec.Syms.size() == 1 && "Uninitialized csect cannot contain more then 1 symbol." ) ? static_cast<void> (0) : __assert_fail ("Sec.Syms.size() == 1 && \"Uninitialized csect cannot contain more then 1 symbol.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 362, __PRETTY_FUNCTION__)); | |||
363 | Symbol &Sym = Sec.Syms.back(); | |||
364 | ||||
365 | // Write the symbol's name. | |||
366 | if (Sym.nameInStringTable()) { | |||
367 | W.write<int32_t>(0); | |||
368 | W.write<uint32_t>(Strings.getOffset(Sym.getName())); | |||
369 | } else { | |||
370 | char Name[XCOFF::NameSize]; | |||
371 | std::strncpy(Name, Sym.getName().data(), XCOFF::NameSize); | |||
372 | ArrayRef<char> NameRef(Name, XCOFF::NameSize); | |||
373 | W.write(NameRef); | |||
374 | } | |||
375 | ||||
376 | W.write<uint32_t>(Sec.Address); | |||
377 | W.write<int16_t>(BSS.Index); | |||
378 | // Basic/Derived type. See the description of the n_type field for symbol | |||
379 | // table entries for a detailed description. Since we don't yet support | |||
380 | // visibility, and all other bits are either optionally set or reserved, | |||
381 | // this is always zero. | |||
382 | // TODO FIXME How to assert a symbols visibility is default? | |||
383 | W.write<uint16_t>(0); | |||
384 | ||||
385 | W.write<uint8_t>(Sym.getStorageClass()); | |||
386 | ||||
387 | // Always 1 aux entry for now. | |||
388 | W.write<uint8_t>(1); | |||
389 | ||||
390 | W.write<uint32_t>(Sec.Size); | |||
391 | ||||
392 | // Parameter typecheck hash. Not supported. | |||
393 | W.write<uint32_t>(0); | |||
394 | // Typecheck section number. Not supported. | |||
395 | W.write<uint16_t>(0); | |||
396 | // Symbol type. | |||
397 | W.write<uint8_t>(getEncodedType(Sec.MCCsect)); | |||
398 | // Storage mapping class. | |||
399 | W.write<uint8_t>(Sec.MCCsect->getMappingClass()); | |||
400 | // Reserved (x_stab). | |||
401 | W.write<uint32_t>(0); | |||
402 | // Reserved (x_snstab). | |||
403 | W.write<uint16_t>(0); | |||
404 | } | |||
405 | } | |||
406 | ||||
407 | void XCOFFObjectWriter::assignAddressesAndIndices( | |||
408 | const llvm::MCAsmLayout &Layout) { | |||
409 | // The address corrresponds to the address of sections and symbols in the | |||
410 | // object file. We place the shared address 0 immediately after the | |||
411 | // section header table. | |||
412 | uint32_t Address = 0; | |||
413 | // Section indices are 1-based in XCOFF. | |||
414 | uint16_t SectionIndex = 1; | |||
415 | // The first symbol table entry is for the file name. We are not emitting it | |||
416 | // yet, so start at index 0. | |||
417 | uint32_t SymbolTableIndex = 0; | |||
418 | ||||
419 | // Text section comes first. TODO | |||
420 | // Data section Second. TODO | |||
421 | ||||
422 | // BSS Section third. | |||
423 | if (!BSSCsects.empty()) { | |||
424 | Sections.push_back(&BSS); | |||
425 | BSS.Index = SectionIndex++; | |||
426 | assert(alignTo(Address, DefaultSectionAlign) == Address &&((alignTo(Address, DefaultSectionAlign) == Address && "Improperly aligned address for section.") ? static_cast< void> (0) : __assert_fail ("alignTo(Address, DefaultSectionAlign) == Address && \"Improperly aligned address for section.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 427, __PRETTY_FUNCTION__)) | |||
427 | "Improperly aligned address for section.")((alignTo(Address, DefaultSectionAlign) == Address && "Improperly aligned address for section.") ? static_cast< void> (0) : __assert_fail ("alignTo(Address, DefaultSectionAlign) == Address && \"Improperly aligned address for section.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 427, __PRETTY_FUNCTION__)); | |||
428 | uint32_t StartAddress = Address; | |||
429 | for (auto &Csect : BSSCsects) { | |||
430 | const MCSectionXCOFF *MCSec = Csect.MCCsect; | |||
431 | Address = alignTo(Address, MCSec->getAlignment()); | |||
432 | Csect.Address = Address; | |||
433 | Address += Layout.getSectionAddressSize(MCSec); | |||
434 | Csect.SymbolTableIndex = SymbolTableIndex; | |||
435 | // 1 main and 1 auxiliary symbol table entry for the csect. | |||
436 | SymbolTableIndex += 2; | |||
437 | Csect.Size = Layout.getSectionAddressSize(MCSec); | |||
438 | ||||
439 | assert(Csect.Syms.size() == 1 &&((Csect.Syms.size() == 1 && "csect in the BSS can only contain a single symbol." ) ? static_cast<void> (0) : __assert_fail ("Csect.Syms.size() == 1 && \"csect in the BSS can only contain a single symbol.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 440, __PRETTY_FUNCTION__)) | |||
440 | "csect in the BSS can only contain a single symbol.")((Csect.Syms.size() == 1 && "csect in the BSS can only contain a single symbol." ) ? static_cast<void> (0) : __assert_fail ("Csect.Syms.size() == 1 && \"csect in the BSS can only contain a single symbol.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 440, __PRETTY_FUNCTION__)); | |||
441 | Csect.Syms[0].SymbolTableIndex = Csect.SymbolTableIndex; | |||
442 | } | |||
443 | // Pad out Address to the default alignment. This is to match how the system | |||
444 | // assembler handles the .bss section. Its size is always a multiple of 4. | |||
445 | Address = alignTo(Address, DefaultSectionAlign); | |||
446 | BSS.Size = Address - StartAddress; | |||
447 | } | |||
448 | ||||
449 | SymbolTableEntryCount = SymbolTableIndex; | |||
450 | ||||
451 | // Calculate the RawPointer value for each section. | |||
452 | uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() + | |||
453 | Sections.size() * sizeof(XCOFF::SectionHeader32); | |||
454 | for (auto *Sec : Sections) { | |||
455 | if (!Sec->IsVirtual) { | |||
456 | Sec->FileOffsetToData = RawPointer; | |||
457 | RawPointer += Sec->Size; | |||
458 | } | |||
459 | } | |||
460 | ||||
461 | // TODO Add in Relocation storage to the RawPointer Calculation. | |||
462 | // TODO What to align the SymbolTable to? | |||
463 | // TODO Error check that the number of symbol table entries fits in 32-bits | |||
464 | // signed ... | |||
465 | if (SymbolTableEntryCount) | |||
466 | SymbolTableOffset = RawPointer; | |||
467 | } | |||
468 | ||||
469 | // Takes the log base 2 of the alignment and shifts the result into the 5 most | |||
470 | // significant bits of a byte, then or's in the csect type into the least | |||
471 | // significant 3 bits. | |||
472 | uint8_t getEncodedType(const MCSectionXCOFF *Sec) { | |||
473 | unsigned Align = Sec->getAlignment(); | |||
474 | assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.")((isPowerOf2_32(Align) && "Alignment must be a power of 2." ) ? static_cast<void> (0) : __assert_fail ("isPowerOf2_32(Align) && \"Alignment must be a power of 2.\"" , "/build/llvm-toolchain-snapshot-10~svn374710/lib/MC/XCOFFObjectWriter.cpp" , 474, __PRETTY_FUNCTION__)); | |||
475 | unsigned Log2Align = Log2_32(Align); | |||
476 | // Result is a number in the range [0, 31] which fits in the 5 least | |||
477 | // significant bits. Shift this value into the 5 most significant bits, and | |||
478 | // bitwise-or in the csect type. | |||
479 | uint8_t EncodedAlign = Log2Align << 3; | |||
480 | return EncodedAlign | Sec->getCSectType(); | |||
481 | } | |||
482 | ||||
483 | } // end anonymous namespace | |||
484 | ||||
485 | std::unique_ptr<MCObjectWriter> | |||
486 | llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, | |||
487 | raw_pwrite_stream &OS) { | |||
488 | return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS); | |||
489 | } |