blob: 1feec0e6d0bfa467d441262411c0aba46fe3d43d [file] [log] [blame]
Sean Fertilef09d54e2019-07-09 19:21:01 +00001//===-- 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
Sean Fertile1e46d4c2019-08-20 22:03:18 +000013#include "llvm/BinaryFormat/XCOFF.h"
14#include "llvm/MC/MCAsmLayout.h"
Sean Fertilef09d54e2019-07-09 19:21:01 +000015#include "llvm/MC/MCAssembler.h"
16#include "llvm/MC/MCObjectWriter.h"
Sean Fertile1e46d4c2019-08-20 22:03:18 +000017#include "llvm/MC/MCSectionXCOFF.h"
18#include "llvm/MC/MCSymbolXCOFF.h"
Sean Fertilef09d54e2019-07-09 19:21:01 +000019#include "llvm/MC/MCValue.h"
20#include "llvm/MC/MCXCOFFObjectWriter.h"
Sean Fertile1e46d4c2019-08-20 22:03:18 +000021#include "llvm/MC/StringTableBuilder.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/MathExtras.h"
24
25#include <deque>
Sean Fertilef09d54e2019-07-09 19:21:01 +000026
27using namespace llvm;
28
Sean Fertile1e46d4c2019-08-20 22:03:18 +000029// 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.
Sean Fertilef09d54e2019-07-09 19:21:01 +000044namespace {
45
Sean Fertile1e46d4c2019-08-20 22:03:18 +000046constexpr unsigned DefaultSectionAlign = 4;
47
48// Packs the csect's alignment and type into a byte.
49uint8_t getEncodedType(const MCSectionXCOFF *);
50
51// Wrapper around an MCSymbolXCOFF.
52struct 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.
68struct 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.
85struct 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
Sean Fertilef09d54e2019-07-09 19:21:01 +0000117class XCOFFObjectWriter : public MCObjectWriter {
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000118 // 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
Sean Fertilef09d54e2019-07-09 19:21:01 +0000124 support::endian::Writer W;
125 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000126 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;
Sean Fertilef09d54e2019-07-09 19:21:01 +0000146
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
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000154 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() &&
176 "Auxiliary header support not implemented.");
177 return 0;
178 }
179
Sean Fertilef09d54e2019-07-09 19:21:01 +0000180public:
181 XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
182 raw_pwrite_stream &OS);
183};
184
185XCOFFObjectWriter::XCOFFObjectWriter(
186 std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000187 : 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) {}
Sean Fertilef09d54e2019-07-09 19:21:01 +0000191
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000192void 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
210void 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() &&
223 "Cannot add a csect twice.");
224
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;
236 }
237 report_fatal_error("Unhandled mapping of read-write csect to section.");
238 default:
239 report_fatal_error("Unhandled mapping of csect to section.");
240 }
241 }
242
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);
248
249 // Map the symbol into its containing csect.
250 MCSectionXCOFF *ContainingCsect =
251 dyn_cast<MCSectionXCOFF>(XSym->getFragment(false)->getParent());
252 assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() &&
253 "Expected containing csect to exist in map");
254
255 // Lookup the containing csect and add the symbol to it.
256 WrapperMap[ContainingCsect]->Syms.emplace_back(XSym);
257
258 // If the name does not fit in the storage provided in the symbol table
259 // entry, add it to the string table.
260 const Symbol &WrapperSym = WrapperMap[ContainingCsect]->Syms.back();
261 if (WrapperSym.nameInStringTable()) {
262 Strings.add(WrapperSym.getName());
263 }
264 }
265
266 Strings.finalize();
267 assignAddressesAndIndices(Layout);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000268}
269
270void XCOFFObjectWriter::recordRelocation(MCAssembler &, const MCAsmLayout &,
271 const MCFragment *, const MCFixup &,
272 MCValue, uint64_t &) {
273 report_fatal_error("XCOFF relocations not supported.");
274}
275
276uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &) {
277 // We always emit a timestamp of 0 for reproducibility, so ensure incremental
278 // linking is not enabled, in case, like with Windows COFF, such a timestamp
279 // is incompatible with incremental linking of XCOFF.
280 if (Asm.isIncrementalLinkerCompatible())
281 report_fatal_error("Incremental linking not supported for XCOFF.");
282
283 if (TargetObjectWriter->is64Bit())
284 report_fatal_error("64-bit XCOFF object files are not supported yet.");
285
286 uint64_t StartOffset = W.OS.tell();
287
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000288 writeFileHeader();
289 writeSectionHeaderTable();
290 // TODO writeSections();
291 // TODO writeRelocations();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000292
293 // TODO FIXME Finalize symbols.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000294 writeSymbolTable();
295 // Write the string table.
296 Strings.write(W.OS);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000297
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000298 return W.OS.tell() - StartOffset;
299}
300
301void XCOFFObjectWriter::writeFileHeader() {
Sean Fertilef09d54e2019-07-09 19:21:01 +0000302 // Magic.
303 W.write<uint16_t>(0x01df);
304 // Number of sections.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000305 W.write<uint16_t>(Sections.size());
Sean Fertilef09d54e2019-07-09 19:21:01 +0000306 // Timestamp field. For reproducible output we write a 0, which represents no
307 // timestamp.
308 W.write<int32_t>(0);
309 // Byte Offset to the start of the symbol table.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000310 W.write<uint32_t>(SymbolTableOffset);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000311 // Number of entries in the symbol table.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000312 W.write<int32_t>(SymbolTableEntryCount);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000313 // Size of the optional header.
314 W.write<uint16_t>(0);
315 // Flags.
316 W.write<uint16_t>(0);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000317}
Sean Fertilef09d54e2019-07-09 19:21:01 +0000318
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000319void XCOFFObjectWriter::writeSectionHeaderTable() {
320 for (const auto *Sec : Sections) {
321 // Write Name.
322 ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize);
323 W.write(NameRef);
324
325 // Write the Physical Address and Virtual Address. In an object file these
326 // are the same.
327 W.write<uint32_t>(Sec->Address);
328 W.write<uint32_t>(Sec->Address);
329
330 W.write<uint32_t>(Sec->Size);
331 W.write<uint32_t>(Sec->FileOffsetToData);
332
333 // Relocation pointer and Lineno pointer. Not supported yet.
334 W.write<uint32_t>(0);
335 W.write<uint32_t>(0);
336
337 // Relocation and line-number counts. Not supported yet.
338 W.write<uint16_t>(0);
339 W.write<uint16_t>(0);
340
341 W.write<int32_t>(Sec->Flags);
342 }
343}
344
345void XCOFFObjectWriter::writeSymbolTable() {
346 assert((ProgramCodeCsects.size() == 1 &&
347 ProgramCodeCsects.back().Syms.size() == 0) &&
348 ".text csects not handled yet.");
349
350 // The BSS Section is special in that the csects must contain a single symbol,
351 // and the contained symbol cannot be represented in the symbol table as a
352 // label definition.
353 for (auto &Sec : BSSCsects) {
354 assert(Sec.Syms.size() == 1 &&
355 "Uninitialized csect cannot contain more then 1 symbol.");
356 Symbol &Sym = Sec.Syms.back();
357
358 // Write the symbol's name.
359 if (Sym.nameInStringTable()) {
360 W.write<int32_t>(0);
361 W.write<uint32_t>(Strings.getOffset(Sym.getName()));
362 } else {
363 char Name[XCOFF::NameSize];
364 std::strncpy(Name, Sym.getName().data(), XCOFF::NameSize);
365 ArrayRef<char> NameRef(Name, XCOFF::NameSize);
366 W.write(NameRef);
367 }
368
369 W.write<uint32_t>(Sec.Address);
370 W.write<int16_t>(BSS.Index);
371 // Basic/Derived type. See the description of the n_type field for symbol
372 // table entries for a detailed description. Since we don't yet support
373 // visibility, and all other bits are either optionally set or reserved,
374 // this is always zero.
375 // TODO FIXME How to assert a symbols visibility is default?
376 W.write<uint16_t>(0);
377
378 W.write<uint8_t>(Sym.getStorageClass());
379
380 // Always 1 aux entry for now.
381 W.write<uint8_t>(1);
382
383 W.write<uint32_t>(Sec.Size);
384
385 // Parameter typecheck hash. Not supported.
386 W.write<uint32_t>(0);
387 // Typecheck section number. Not supported.
388 W.write<uint16_t>(0);
389 // Symbol type.
390 W.write<uint8_t>(getEncodedType(Sec.MCCsect));
391 // Storage mapping class.
392 W.write<uint8_t>(Sec.MCCsect->getMappingClass());
393 // Reserved (x_stab).
394 W.write<uint32_t>(0);
395 // Reserved (x_snstab).
396 W.write<uint16_t>(0);
397 }
398}
399
400void XCOFFObjectWriter::assignAddressesAndIndices(
401 const llvm::MCAsmLayout &Layout) {
402 // The address corrresponds to the address of sections and symbols in the
403 // object file. We place the shared address 0 immediately after the
404 // section header table.
405 uint32_t Address = 0;
406 // Section indices are 1-based in XCOFF.
407 uint16_t SectionIndex = 1;
408 // The first symbol table entry is for the file name. We are not emitting it
409 // yet, so start at index 0.
410 uint32_t SymbolTableIndex = 0;
411
412 // Text section comes first. TODO
413 // Data section Second. TODO
414
415 // BSS Section third.
416 if (!BSSCsects.empty()) {
417 Sections.push_back(&BSS);
418 BSS.Index = SectionIndex++;
419 assert(alignTo(Address, DefaultSectionAlign) == Address &&
420 "Improperly aligned address for section.");
421 uint32_t StartAddress = Address;
422 for (auto &Csect : BSSCsects) {
423 const MCSectionXCOFF *MCSec = Csect.MCCsect;
424 Address = alignTo(Address, MCSec->getAlignment());
425 Csect.Address = Address;
426 Address += Layout.getSectionAddressSize(MCSec);
427 Csect.SymbolTableIndex = SymbolTableIndex;
428 // 1 main and 1 auxiliary symbol table entry for the csect.
429 SymbolTableIndex += 2;
430 Csect.Size = Layout.getSectionAddressSize(MCSec);
431
432 assert(Csect.Syms.size() == 1 &&
433 "csect in the BSS can only contain a single symbol.");
434 Csect.Syms[0].SymbolTableIndex = Csect.SymbolTableIndex;
435 }
436 // Pad out Address to the default alignment. This is to match how the system
437 // assembler handles the .bss section. Its size is always a multiple of 4.
438 Address = alignTo(Address, DefaultSectionAlign);
439 BSS.Size = Address - StartAddress;
440 }
441
442 SymbolTableEntryCount = SymbolTableIndex;
443
444 // Calculate the RawPointer value for each section.
445 uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() +
446 Sections.size() * sizeof(XCOFF::SectionHeader32);
447 for (auto *Sec : Sections) {
448 if (!Sec->IsVirtual) {
449 Sec->FileOffsetToData = RawPointer;
450 RawPointer += Sec->Size;
451 }
452 }
453
454 // TODO Add in Relocation storage to the RawPointer Calculation.
455 // TODO What to align the SymbolTable to?
456 // TODO Error check that the number of symbol table entries fits in 32-bits
457 // signed ...
458 if (SymbolTableEntryCount)
459 SymbolTableOffset = RawPointer;
460}
461
462// Takes the log base 2 of the alignment and shifts the result into the 5 most
463// significant bits of a byte, then or's in the csect type into the least
464// significant 3 bits.
465uint8_t getEncodedType(const MCSectionXCOFF *Sec) {
466 unsigned Align = Sec->getAlignment();
467 assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.");
468 assert((Sec->getCSectType() <= 0x07u) && "csect type exceeds 3 bits.");
469 unsigned Log2Align = Log2_32(Align);
470 // Result is a number in the range [0, 31] which fits in the 5 least
471 // significant bits. Shift this value into the 5 most significant bits, and
472 // bitwise-or in the csect type.
473 uint8_t EncodedAlign = Log2Align << 3;
474 return EncodedAlign | Sec->getCSectType();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000475}
476
477} // end anonymous namespace
478
479std::unique_ptr<MCObjectWriter>
480llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
481 raw_pwrite_stream &OS) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000482 return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000483}