[llvm-objdump] - Remove one overload of reportError. NFCI.
[llvm-complete.git] / lib / MC / XCOFFObjectWriter.cpp
blobd4dfd467976ab9ed271766b0a41469aa9b6b6a07
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.
11 //===----------------------------------------------------------------------===//
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"
25 #include <deque>
27 using namespace llvm;
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.
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 {
46 constexpr unsigned DefaultSectionAlign = 4;
48 // Packs the csect's alignment and type into a byte.
49 uint8_t getEncodedType(const MCSectionXCOFF *);
51 // Wrapper around an MCSymbolXCOFF.
52 struct Symbol {
53 const MCSymbolXCOFF *const MCSym;
54 uint32_t SymbolTableIndex;
56 XCOFF::StorageClass getStorageClass() const {
57 return MCSym->getStorageClass();
59 StringRef getName() const { return MCSym->getName(); }
60 bool nameInStringTable() const {
61 return MCSym->getName().size() > XCOFF::NameSize;
64 Symbol(const MCSymbolXCOFF *MCSym) : MCSym(MCSym), SymbolTableIndex(-1) {}
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;
74 SmallVector<Symbol, 1> Syms;
76 ControlSection(const MCSectionXCOFF *MCSec)
77 : MCCsect(MCSec), SymbolTableIndex(-1), Address(-1) {}
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;
96 uint16_t Index;
98 // Virtual sections do not need storage allocated in the object file.
99 const bool IsVirtual;
101 void reset() {
102 Address = 0;
103 Size = 0;
104 FileOffsetToData = 0;
105 FileOffsetToRelocations = 0;
106 RelocationCount = 0;
107 Index = -1;
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);
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>;
124 support::endian::Writer W;
125 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
126 StringTableBuilder Strings;
128 // The non-empty sections, in the order they will appear in the section header
129 // table.
130 std::vector<Section *> Sections;
132 // The Predefined sections.
133 Section Text;
134 Section BSS;
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;
142 uint32_t SymbolTableEntryCount = 0;
143 uint32_t SymbolTableOffset = 0;
145 virtual void reset() override;
147 void executePostLayoutBinding(MCAssembler &, const MCAsmLayout &) override;
149 void recordRelocation(MCAssembler &, const MCAsmLayout &, const MCFragment *,
150 const MCFixup &, MCValue, uint64_t &) override;
152 uint64_t writeObject(MCAssembler &, const MCAsmLayout &) override;
154 void writeFileHeader();
155 void writeSectionHeaderTable();
156 void writeSymbolTable();
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 &);
168 bool
169 needsAuxiliaryHeader() const { /* TODO aux header support not implemented. */
170 return false;
173 // Returns the size of the auxiliary header to be written to the object file.
174 size_t auxiliaryHeaderSize() const {
175 assert(!needsAuxiliaryHeader() &&
176 "Auxiliary header support not implemented.");
177 return 0;
180 public:
181 XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
182 raw_pwrite_stream &OS);
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) {}
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();
198 // Clear any csects we have stored.
199 ProgramCodeCsects.clear();
200 BSSCsects.clear();
202 // Reset the symbol table and string table.
203 SymbolTableEntryCount = 0;
204 SymbolTableOffset = 0;
205 Strings.clear();
207 MCObjectWriter::reset();
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.");
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;
220 for (const auto &S : Asm) {
221 const MCSectionXCOFF *MCSec = dyn_cast<const MCSectionXCOFF>(&S);
222 assert(WrapperMap.find(MCSec) == WrapperMap.end() &&
223 "Cannot add a csect twice.");
225 switch (MCSec->getMappingClass()) {
226 case XCOFF::XMC_PR:
227 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&
228 "Only an initialized csect can contain program code.");
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;
237 report_fatal_error("Unhandled mapping of read-write csect to section.");
238 default:
239 report_fatal_error("Unhandled mapping of csect to section.");
243 for (const MCSymbol &S : Asm.symbols()) {
244 // Nothing to do for temporary symbols.
245 if (S.isTemporary())
246 continue;
247 const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(&S);
249 // Map the symbol into its containing csect.
250 const MCSectionXCOFF *ContainingCsect = XSym->getContainingCsect();
251 assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() &&
252 "Expected containing csect to exist in map");
254 // Lookup the containing csect and add the symbol to it.
255 WrapperMap[ContainingCsect]->Syms.emplace_back(XSym);
257 // If the name does not fit in the storage provided in the symbol table
258 // entry, add it to the string table.
259 const Symbol &WrapperSym = WrapperMap[ContainingCsect]->Syms.back();
260 if (WrapperSym.nameInStringTable()) {
261 Strings.add(WrapperSym.getName());
265 Strings.finalize();
266 assignAddressesAndIndices(Layout);
269 void XCOFFObjectWriter::recordRelocation(MCAssembler &, const MCAsmLayout &,
270 const MCFragment *, const MCFixup &,
271 MCValue, uint64_t &) {
272 report_fatal_error("XCOFF relocations not supported.");
275 uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &) {
276 // We always emit a timestamp of 0 for reproducibility, so ensure incremental
277 // linking is not enabled, in case, like with Windows COFF, such a timestamp
278 // is incompatible with incremental linking of XCOFF.
279 if (Asm.isIncrementalLinkerCompatible())
280 report_fatal_error("Incremental linking not supported for XCOFF.");
282 if (TargetObjectWriter->is64Bit())
283 report_fatal_error("64-bit XCOFF object files are not supported yet.");
285 uint64_t StartOffset = W.OS.tell();
287 writeFileHeader();
288 writeSectionHeaderTable();
289 // TODO writeSections();
290 // TODO writeRelocations();
292 // TODO FIXME Finalize symbols.
293 writeSymbolTable();
294 // Write the string table.
295 Strings.write(W.OS);
297 return W.OS.tell() - StartOffset;
300 void XCOFFObjectWriter::writeFileHeader() {
301 // Magic.
302 W.write<uint16_t>(0x01df);
303 // Number of sections.
304 W.write<uint16_t>(Sections.size());
305 // Timestamp field. For reproducible output we write a 0, which represents no
306 // timestamp.
307 W.write<int32_t>(0);
308 // Byte Offset to the start of the symbol table.
309 W.write<uint32_t>(SymbolTableOffset);
310 // Number of entries in the symbol table.
311 W.write<int32_t>(SymbolTableEntryCount);
312 // Size of the optional header.
313 W.write<uint16_t>(0);
314 // Flags.
315 W.write<uint16_t>(0);
318 void XCOFFObjectWriter::writeSectionHeaderTable() {
319 for (const auto *Sec : Sections) {
320 // Write Name.
321 ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize);
322 W.write(NameRef);
324 // Write the Physical Address and Virtual Address. In an object file these
325 // are the same.
326 W.write<uint32_t>(Sec->Address);
327 W.write<uint32_t>(Sec->Address);
329 W.write<uint32_t>(Sec->Size);
330 W.write<uint32_t>(Sec->FileOffsetToData);
332 // Relocation pointer and Lineno pointer. Not supported yet.
333 W.write<uint32_t>(0);
334 W.write<uint32_t>(0);
336 // Relocation and line-number counts. Not supported yet.
337 W.write<uint16_t>(0);
338 W.write<uint16_t>(0);
340 W.write<int32_t>(Sec->Flags);
344 void XCOFFObjectWriter::writeSymbolTable() {
345 assert(ProgramCodeCsects.size() == 0 && ".text csects not handled yet.");
347 // The BSS Section is special in that the csects must contain a single symbol,
348 // and the contained symbol cannot be represented in the symbol table as a
349 // label definition.
350 for (auto &Sec : BSSCsects) {
351 assert(Sec.Syms.size() == 1 &&
352 "Uninitialized csect cannot contain more then 1 symbol.");
353 Symbol &Sym = Sec.Syms.back();
355 // Write the symbol's name.
356 if (Sym.nameInStringTable()) {
357 W.write<int32_t>(0);
358 W.write<uint32_t>(Strings.getOffset(Sym.getName()));
359 } else {
360 char Name[XCOFF::NameSize];
361 std::strncpy(Name, Sym.getName().data(), XCOFF::NameSize);
362 ArrayRef<char> NameRef(Name, XCOFF::NameSize);
363 W.write(NameRef);
366 W.write<uint32_t>(Sec.Address);
367 W.write<int16_t>(BSS.Index);
368 // Basic/Derived type. See the description of the n_type field for symbol
369 // table entries for a detailed description. Since we don't yet support
370 // visibility, and all other bits are either optionally set or reserved,
371 // this is always zero.
372 // TODO FIXME How to assert a symbols visibility is default?
373 W.write<uint16_t>(0);
375 W.write<uint8_t>(Sym.getStorageClass());
377 // Always 1 aux entry for now.
378 W.write<uint8_t>(1);
380 W.write<uint32_t>(Sec.Size);
382 // Parameter typecheck hash. Not supported.
383 W.write<uint32_t>(0);
384 // Typecheck section number. Not supported.
385 W.write<uint16_t>(0);
386 // Symbol type.
387 W.write<uint8_t>(getEncodedType(Sec.MCCsect));
388 // Storage mapping class.
389 W.write<uint8_t>(Sec.MCCsect->getMappingClass());
390 // Reserved (x_stab).
391 W.write<uint32_t>(0);
392 // Reserved (x_snstab).
393 W.write<uint16_t>(0);
397 void XCOFFObjectWriter::assignAddressesAndIndices(
398 const llvm::MCAsmLayout &Layout) {
399 // The address corrresponds to the address of sections and symbols in the
400 // object file. We place the shared address 0 immediately after the
401 // section header table.
402 uint32_t Address = 0;
403 // Section indices are 1-based in XCOFF.
404 uint16_t SectionIndex = 1;
405 // The first symbol table entry is for the file name. We are not emitting it
406 // yet, so start at index 0.
407 uint32_t SymbolTableIndex = 0;
409 // Text section comes first. TODO
410 // Data section Second. TODO
412 // BSS Section third.
413 if (!BSSCsects.empty()) {
414 Sections.push_back(&BSS);
415 BSS.Index = SectionIndex++;
416 assert(alignTo(Address, DefaultSectionAlign) == Address &&
417 "Improperly aligned address for section.");
418 uint32_t StartAddress = Address;
419 for (auto &Csect : BSSCsects) {
420 const MCSectionXCOFF *MCSec = Csect.MCCsect;
421 Address = alignTo(Address, MCSec->getAlignment());
422 Csect.Address = Address;
423 Address += Layout.getSectionAddressSize(MCSec);
424 Csect.SymbolTableIndex = SymbolTableIndex;
425 // 1 main and 1 auxiliary symbol table entry for the csect.
426 SymbolTableIndex += 2;
427 Csect.Size = Layout.getSectionAddressSize(MCSec);
429 assert(Csect.Syms.size() == 1 &&
430 "csect in the BSS can only contain a single symbol.");
431 Csect.Syms[0].SymbolTableIndex = Csect.SymbolTableIndex;
433 // Pad out Address to the default alignment. This is to match how the system
434 // assembler handles the .bss section. Its size is always a multiple of 4.
435 Address = alignTo(Address, DefaultSectionAlign);
436 BSS.Size = Address - StartAddress;
439 SymbolTableEntryCount = SymbolTableIndex;
441 // Calculate the RawPointer value for each section.
442 uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() +
443 Sections.size() * sizeof(XCOFF::SectionHeader32);
444 for (auto *Sec : Sections) {
445 if (!Sec->IsVirtual) {
446 Sec->FileOffsetToData = RawPointer;
447 RawPointer += Sec->Size;
451 // TODO Add in Relocation storage to the RawPointer Calculation.
452 // TODO What to align the SymbolTable to?
453 // TODO Error check that the number of symbol table entries fits in 32-bits
454 // signed ...
455 if (SymbolTableEntryCount)
456 SymbolTableOffset = RawPointer;
459 // Takes the log base 2 of the alignment and shifts the result into the 5 most
460 // significant bits of a byte, then or's in the csect type into the least
461 // significant 3 bits.
462 uint8_t getEncodedType(const MCSectionXCOFF *Sec) {
463 unsigned Align = Sec->getAlignment();
464 assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.");
465 unsigned Log2Align = Log2_32(Align);
466 // Result is a number in the range [0, 31] which fits in the 5 least
467 // significant bits. Shift this value into the 5 most significant bits, and
468 // bitwise-or in the csect type.
469 uint8_t EncodedAlign = Log2Align << 3;
470 return EncodedAlign | Sec->getCSectType();
473 } // end anonymous namespace
475 std::unique_ptr<MCObjectWriter>
476 llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
477 raw_pwrite_stream &OS) {
478 return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS);