blob: 3199c18fc167d7ecca478ab664e54a598e8e4bed [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;
jasonliu78207e12019-10-24 15:19:12 +000047constexpr int16_t MaxSectionIndex = INT16_MAX;
Sean Fertile1e46d4c2019-08-20 22:03:18 +000048
49// Packs the csect's alignment and type into a byte.
50uint8_t getEncodedType(const MCSectionXCOFF *);
51
52// Wrapper around an MCSymbolXCOFF.
53struct Symbol {
54 const MCSymbolXCOFF *const MCSym;
55 uint32_t SymbolTableIndex;
56
57 XCOFF::StorageClass getStorageClass() const {
58 return MCSym->getStorageClass();
59 }
60 StringRef getName() const { return MCSym->getName(); }
Sean Fertile1e46d4c2019-08-20 22:03:18 +000061 Symbol(const MCSymbolXCOFF *MCSym) : MCSym(MCSym), SymbolTableIndex(-1) {}
62};
63
64// Wrapper for an MCSectionXCOFF.
65struct ControlSection {
66 const MCSectionXCOFF *const MCCsect;
67 uint32_t SymbolTableIndex;
68 uint32_t Address;
69 uint32_t Size;
70
71 SmallVector<Symbol, 1> Syms;
Digger Linfdfd6ab2019-10-15 17:40:41 +000072 StringRef getName() const { return MCCsect->getSectionName(); }
Sean Fertile1e46d4c2019-08-20 22:03:18 +000073 ControlSection(const MCSectionXCOFF *MCSec)
Digger Linfdfd6ab2019-10-15 17:40:41 +000074 : MCCsect(MCSec), SymbolTableIndex(-1), Address(-1), Size(0) {}
Sean Fertile1e46d4c2019-08-20 22:03:18 +000075};
76
jasonliu78207e12019-10-24 15:19:12 +000077// Type to be used for a container representing a set of csects with
78// (approximately) the same storage mapping class. For example all the csects
79// with a storage mapping class of `xmc_pr` will get placed into the same
80// container.
81struct CsectGroup {
82 enum LabelDefinitionSupport : bool {
83 LabelDefSupported = true,
84 LabelDefUnsupported = false
85 };
86
87 const LabelDefinitionSupport SupportLabelDef;
88 std::deque<ControlSection> Csects;
89};
90
91using CsectGroups = std::deque<CsectGroup *>;
92
Sean Fertile1e46d4c2019-08-20 22:03:18 +000093// Represents the data related to a section excluding the csects that make up
94// the raw data of the section. The csects are stored separately as not all
95// sections contain csects, and some sections contain csects which are better
96// stored separately, e.g. the .data section containing read-write, descriptor,
97// TOCBase and TOC-entry csects.
98struct Section {
99 char Name[XCOFF::NameSize];
100 // The physical/virtual address of the section. For an object file
101 // these values are equivalent.
102 uint32_t Address;
103 uint32_t Size;
104 uint32_t FileOffsetToData;
105 uint32_t FileOffsetToRelocations;
106 uint32_t RelocationCount;
107 int32_t Flags;
108
Digger Linfdfd6ab2019-10-15 17:40:41 +0000109 int16_t Index;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000110
111 // Virtual sections do not need storage allocated in the object file.
112 const bool IsVirtual;
113
jasonliu78207e12019-10-24 15:19:12 +0000114 // XCOFF has special section numbers for symbols:
115 // -2 Specifies N_DEBUG, a special symbolic debugging symbol.
116 // -1 Specifies N_ABS, an absolute symbol. The symbol has a value but is not
117 // relocatable.
118 // 0 Specifies N_UNDEF, an undefined external symbol.
119 // Therefore, we choose -3 (N_DEBUG - 1) to represent a section index that
120 // hasn't been initialized.
121 static constexpr int16_t UninitializedIndex =
122 XCOFF::ReservedSectionNum::N_DEBUG - 1;
123
124 CsectGroups Groups;
125
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000126 void reset() {
127 Address = 0;
128 Size = 0;
129 FileOffsetToData = 0;
130 FileOffsetToRelocations = 0;
131 RelocationCount = 0;
jasonliu78207e12019-10-24 15:19:12 +0000132 Index = UninitializedIndex;
133 // Clear any csects we have stored.
134 for (auto *Group : Groups)
135 Group->Csects.clear();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000136 }
137
jasonliu78207e12019-10-24 15:19:12 +0000138 Section(const char *N, XCOFF::SectionTypeFlags Flags, bool IsVirtual,
139 CsectGroups Groups)
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000140 : Address(0), Size(0), FileOffsetToData(0), FileOffsetToRelocations(0),
jasonliu78207e12019-10-24 15:19:12 +0000141 RelocationCount(0), Flags(Flags), Index(UninitializedIndex),
142 IsVirtual(IsVirtual), Groups(Groups) {
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000143 strncpy(Name, N, XCOFF::NameSize);
144 }
145};
146
Sean Fertilef09d54e2019-07-09 19:21:01 +0000147class XCOFFObjectWriter : public MCObjectWriter {
jasonliu78207e12019-10-24 15:19:12 +0000148
149 uint32_t SymbolTableEntryCount = 0;
150 uint32_t SymbolTableOffset = 0;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000151
Sean Fertilef09d54e2019-07-09 19:21:01 +0000152 support::endian::Writer W;
153 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000154 StringTableBuilder Strings;
155
jasonliu78207e12019-10-24 15:19:12 +0000156 // CsectGroups. These store the csects which make up different parts of
157 // the sections. Should have one for each set of csects that get mapped into
158 // the same section and get handled in a 'similar' way.
jasonliu95a18b82019-10-24 21:03:16 +0000159 CsectGroup ProgramCodeCsects{CsectGroup::LabelDefSupported, {}};
160 CsectGroup BSSCsects{CsectGroup::LabelDefUnsupported, {}};
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000161
162 // The Predefined sections.
163 Section Text;
164 Section BSS;
165
jasonliu78207e12019-10-24 15:19:12 +0000166 // All the XCOFF sections, in the order they will appear in the section header
167 // table.
Hans Wennborg55c223a2019-10-24 23:42:01 +0200168 std::array<Section *const, 2> Sections{{&Text, &BSS}};
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000169
jasonliu78207e12019-10-24 15:19:12 +0000170 CsectGroup &getCsectGroup(const MCSectionXCOFF *MCSec);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000171
172 virtual void reset() override;
Sean Fertilef09d54e2019-07-09 19:21:01 +0000173
174 void executePostLayoutBinding(MCAssembler &, const MCAsmLayout &) override;
175
176 void recordRelocation(MCAssembler &, const MCAsmLayout &, const MCFragment *,
177 const MCFixup &, MCValue, uint64_t &) override;
178
179 uint64_t writeObject(MCAssembler &, const MCAsmLayout &) override;
180
Digger Linfdfd6ab2019-10-15 17:40:41 +0000181 static bool nameShouldBeInStringTable(const StringRef &);
182 void writeSymbolName(const StringRef &);
183 void writeSymbolTableEntryForCsectMemberLabel(const Symbol &,
184 const ControlSection &, int16_t,
185 uint64_t);
186 void writeSymbolTableEntryForControlSection(const ControlSection &, int16_t,
187 XCOFF::StorageClass);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000188 void writeFileHeader();
189 void writeSectionHeaderTable();
Digger Linfdfd6ab2019-10-15 17:40:41 +0000190 void writeSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
191 void writeSymbolTable(const MCAsmLayout &Layout);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000192
193 // Called after all the csects and symbols have been processed by
194 // `executePostLayoutBinding`, this function handles building up the majority
195 // of the structures in the object file representation. Namely:
196 // *) Calculates physical/virtual addresses, raw-pointer offsets, and section
197 // sizes.
198 // *) Assigns symbol table indices.
199 // *) Builds up the section header table by adding any non-empty sections to
200 // `Sections`.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000201 void assignAddressesAndIndices(const MCAsmLayout &);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000202
203 bool
204 needsAuxiliaryHeader() const { /* TODO aux header support not implemented. */
205 return false;
206 }
207
208 // Returns the size of the auxiliary header to be written to the object file.
209 size_t auxiliaryHeaderSize() const {
210 assert(!needsAuxiliaryHeader() &&
211 "Auxiliary header support not implemented.");
212 return 0;
213 }
214
Sean Fertilef09d54e2019-07-09 19:21:01 +0000215public:
216 XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
217 raw_pwrite_stream &OS);
218};
219
220XCOFFObjectWriter::XCOFFObjectWriter(
221 std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000222 : W(OS, support::big), TargetObjectWriter(std::move(MOTW)),
223 Strings(StringTableBuilder::XCOFF),
jasonliu78207e12019-10-24 15:19:12 +0000224 Text(".text", XCOFF::STYP_TEXT, /* IsVirtual */ false,
225 CsectGroups{&ProgramCodeCsects}),
226 BSS(".bss", XCOFF::STYP_BSS, /* IsVirtual */ true,
227 CsectGroups{&BSSCsects}) {}
Sean Fertilef09d54e2019-07-09 19:21:01 +0000228
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000229void XCOFFObjectWriter::reset() {
230 // Reset any sections we have written to, and empty the section header table.
231 for (auto *Sec : Sections)
232 Sec->reset();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000233
234 // Reset the symbol table and string table.
235 SymbolTableEntryCount = 0;
236 SymbolTableOffset = 0;
237 Strings.clear();
238
239 MCObjectWriter::reset();
240}
241
jasonliu78207e12019-10-24 15:19:12 +0000242CsectGroup &XCOFFObjectWriter::getCsectGroup(const MCSectionXCOFF *MCSec) {
243 switch (MCSec->getMappingClass()) {
244 case XCOFF::XMC_PR:
245 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&
246 "Only an initialized csect can contain program code.");
247 return ProgramCodeCsects;
248 case XCOFF::XMC_RW:
249 if (XCOFF::XTY_CM == MCSec->getCSectType())
250 return BSSCsects;
251
252 report_fatal_error("Unhandled mapping of read-write csect to section.");
253 case XCOFF::XMC_BS:
254 assert(XCOFF::XTY_CM == MCSec->getCSectType() &&
255 "Mapping invalid csect. CSECT with bss storage class must be "
256 "common type.");
257 return BSSCsects;
258 default:
259 report_fatal_error("Unhandled mapping of csect to section.");
260 }
261}
262
Digger Linfdfd6ab2019-10-15 17:40:41 +0000263void XCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
264 const MCAsmLayout &Layout) {
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000265 if (TargetObjectWriter->is64Bit())
266 report_fatal_error("64-bit XCOFF object files are not supported yet.");
267
268 // Maps the MC Section representation to its corresponding ControlSection
269 // wrapper. Needed for finding the ControlSection to insert an MCSymbol into
270 // from its containing MCSectionXCOFF.
271 DenseMap<const MCSectionXCOFF *, ControlSection *> WrapperMap;
272
273 for (const auto &S : Asm) {
Simon Pilgrimef0cb272019-10-14 16:46:11 +0000274 const auto *MCSec = cast<const MCSectionXCOFF>(&S);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000275 assert(WrapperMap.find(MCSec) == WrapperMap.end() &&
276 "Cannot add a csect twice.");
277
Digger Linfdfd6ab2019-10-15 17:40:41 +0000278 // If the name does not fit in the storage provided in the symbol table
279 // entry, add it to the string table.
280 if (nameShouldBeInStringTable(MCSec->getSectionName()))
281 Strings.add(MCSec->getSectionName());
282
jasonliu78207e12019-10-24 15:19:12 +0000283 // TODO FIXME Handle emiting the TOC base.
284 if (MCSec->getMappingClass() == XCOFF::XMC_TC0)
285 continue;
286
287 CsectGroup &Group = getCsectGroup(MCSec);
288 Group.Csects.emplace_back(MCSec);
289 WrapperMap[MCSec] = &Group.Csects.back();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000290 }
291
292 for (const MCSymbol &S : Asm.symbols()) {
293 // Nothing to do for temporary symbols.
294 if (S.isTemporary())
295 continue;
296 const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(&S);
297
298 // Map the symbol into its containing csect.
Sean Fertile18fd1b02019-08-22 15:11:23 +0000299 const MCSectionXCOFF *ContainingCsect = XSym->getContainingCsect();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000300 assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() &&
301 "Expected containing csect to exist in map");
302
303 // Lookup the containing csect and add the symbol to it.
304 WrapperMap[ContainingCsect]->Syms.emplace_back(XSym);
305
306 // If the name does not fit in the storage provided in the symbol table
307 // entry, add it to the string table.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000308 if (nameShouldBeInStringTable(XSym->getName()))
309 Strings.add(XSym->getName());
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000310 }
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000311
312 Strings.finalize();
313 assignAddressesAndIndices(Layout);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000314}
315
316void XCOFFObjectWriter::recordRelocation(MCAssembler &, const MCAsmLayout &,
317 const MCFragment *, const MCFixup &,
318 MCValue, uint64_t &) {
319 report_fatal_error("XCOFF relocations not supported.");
320}
321
Digger Linfdfd6ab2019-10-15 17:40:41 +0000322void XCOFFObjectWriter::writeSections(const MCAssembler &Asm,
323 const MCAsmLayout &Layout) {
jasonliu78207e12019-10-24 15:19:12 +0000324 uint32_t CurrentAddressLocation = 0;
325 for (const auto *Section : Sections) {
326 // Nothing to write for this Section.
327 if (Section->Index == Section::UninitializedIndex || Section->IsVirtual)
328 continue;
Digger Linfdfd6ab2019-10-15 17:40:41 +0000329
jasonliu78207e12019-10-24 15:19:12 +0000330 assert(CurrentAddressLocation == Section->Address &&
331 "We should have no padding between sections.");
332 for (const auto *Group : Section->Groups) {
333 for (const auto &Csect : Group->Csects) {
334 if (uint32_t PaddingSize = Csect.Address - CurrentAddressLocation)
335 W.OS.write_zeros(PaddingSize);
336 Asm.writeSectionData(W.OS, Csect.MCCsect, Layout);
337 CurrentAddressLocation = Csect.Address + Csect.Size;
338 }
339 }
340
Digger Linfdfd6ab2019-10-15 17:40:41 +0000341 // The size of the tail padding in a section is the end virtual address of
342 // the current section minus the the end virtual address of the last csect
343 // in that section.
344 if (uint32_t PaddingSize =
jasonliu78207e12019-10-24 15:19:12 +0000345 Section->Address + Section->Size - CurrentAddressLocation)
Digger Linfdfd6ab2019-10-15 17:40:41 +0000346 W.OS.write_zeros(PaddingSize);
347 }
348}
349
350uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm,
351 const MCAsmLayout &Layout) {
Sean Fertilef09d54e2019-07-09 19:21:01 +0000352 // We always emit a timestamp of 0 for reproducibility, so ensure incremental
353 // linking is not enabled, in case, like with Windows COFF, such a timestamp
354 // is incompatible with incremental linking of XCOFF.
355 if (Asm.isIncrementalLinkerCompatible())
356 report_fatal_error("Incremental linking not supported for XCOFF.");
357
358 if (TargetObjectWriter->is64Bit())
359 report_fatal_error("64-bit XCOFF object files are not supported yet.");
360
361 uint64_t StartOffset = W.OS.tell();
362
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000363 writeFileHeader();
364 writeSectionHeaderTable();
Digger Linfdfd6ab2019-10-15 17:40:41 +0000365 writeSections(Asm, Layout);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000366 // TODO writeRelocations();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000367
Digger Linfdfd6ab2019-10-15 17:40:41 +0000368 writeSymbolTable(Layout);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000369 // Write the string table.
370 Strings.write(W.OS);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000371
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000372 return W.OS.tell() - StartOffset;
373}
374
Digger Linfdfd6ab2019-10-15 17:40:41 +0000375bool XCOFFObjectWriter::nameShouldBeInStringTable(const StringRef &SymbolName) {
376 return SymbolName.size() > XCOFF::NameSize;
377}
378
379void XCOFFObjectWriter::writeSymbolName(const StringRef &SymbolName) {
380 if (nameShouldBeInStringTable(SymbolName)) {
381 W.write<int32_t>(0);
382 W.write<uint32_t>(Strings.getOffset(SymbolName));
383 } else {
384 char Name[XCOFF::NameSize];
385 std::strncpy(Name, SymbolName.data(), XCOFF::NameSize);
386 ArrayRef<char> NameRef(Name, XCOFF::NameSize);
387 W.write(NameRef);
388 }
389}
390
391void XCOFFObjectWriter::writeSymbolTableEntryForCsectMemberLabel(
392 const Symbol &SymbolRef, const ControlSection &CSectionRef,
393 int16_t SectionIndex, uint64_t SymbolOffset) {
394 // Name or Zeros and string table offset
395 writeSymbolName(SymbolRef.getName());
396 assert(SymbolOffset <= UINT32_MAX - CSectionRef.Address &&
397 "Symbol address overflows.");
398 W.write<uint32_t>(CSectionRef.Address + SymbolOffset);
399 W.write<int16_t>(SectionIndex);
400 // Basic/Derived type. See the description of the n_type field for symbol
401 // table entries for a detailed description. Since we don't yet support
402 // visibility, and all other bits are either optionally set or reserved, this
403 // is always zero.
404 // TODO FIXME How to assert a symbol's visibilty is default?
405 // TODO Set the function indicator (bit 10, 0x0020) for functions
406 // when debugging is enabled.
407 W.write<uint16_t>(0);
408 W.write<uint8_t>(SymbolRef.getStorageClass());
409 // Always 1 aux entry for now.
410 W.write<uint8_t>(1);
411
412 // Now output the auxiliary entry.
413 W.write<uint32_t>(CSectionRef.SymbolTableIndex);
414 // Parameter typecheck hash. Not supported.
415 W.write<uint32_t>(0);
416 // Typecheck section number. Not supported.
417 W.write<uint16_t>(0);
418 // Symbol type: Label
419 W.write<uint8_t>(XCOFF::XTY_LD);
420 // Storage mapping class.
421 W.write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
422 // Reserved (x_stab).
423 W.write<uint32_t>(0);
424 // Reserved (x_snstab).
425 W.write<uint16_t>(0);
426}
427
428void XCOFFObjectWriter::writeSymbolTableEntryForControlSection(
429 const ControlSection &CSectionRef, int16_t SectionIndex,
430 XCOFF::StorageClass StorageClass) {
431 // n_name, n_zeros, n_offset
432 writeSymbolName(CSectionRef.getName());
433 // n_value
434 W.write<uint32_t>(CSectionRef.Address);
435 // n_scnum
436 W.write<int16_t>(SectionIndex);
437 // Basic/Derived type. See the description of the n_type field for symbol
438 // table entries for a detailed description. Since we don't yet support
439 // visibility, and all other bits are either optionally set or reserved, this
440 // is always zero.
441 // TODO FIXME How to assert a symbol's visibilty is default?
442 // TODO Set the function indicator (bit 10, 0x0020) for functions
443 // when debugging is enabled.
444 W.write<uint16_t>(0);
445 // n_sclass
446 W.write<uint8_t>(StorageClass);
447 // Always 1 aux entry for now.
448 W.write<uint8_t>(1);
449
450 // Now output the auxiliary entry.
451 W.write<uint32_t>(CSectionRef.Size);
452 // Parameter typecheck hash. Not supported.
453 W.write<uint32_t>(0);
454 // Typecheck section number. Not supported.
455 W.write<uint16_t>(0);
456 // Symbol type.
457 W.write<uint8_t>(getEncodedType(CSectionRef.MCCsect));
458 // Storage mapping class.
459 W.write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
460 // Reserved (x_stab).
461 W.write<uint32_t>(0);
462 // Reserved (x_snstab).
463 W.write<uint16_t>(0);
464}
465
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000466void XCOFFObjectWriter::writeFileHeader() {
Sean Fertilef09d54e2019-07-09 19:21:01 +0000467 // Magic.
468 W.write<uint16_t>(0x01df);
469 // Number of sections.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000470 W.write<uint16_t>(Sections.size());
Sean Fertilef09d54e2019-07-09 19:21:01 +0000471 // Timestamp field. For reproducible output we write a 0, which represents no
472 // timestamp.
473 W.write<int32_t>(0);
474 // Byte Offset to the start of the symbol table.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000475 W.write<uint32_t>(SymbolTableOffset);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000476 // Number of entries in the symbol table.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000477 W.write<int32_t>(SymbolTableEntryCount);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000478 // Size of the optional header.
479 W.write<uint16_t>(0);
480 // Flags.
481 W.write<uint16_t>(0);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000482}
Sean Fertilef09d54e2019-07-09 19:21:01 +0000483
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000484void XCOFFObjectWriter::writeSectionHeaderTable() {
485 for (const auto *Sec : Sections) {
486 // Write Name.
487 ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize);
488 W.write(NameRef);
489
490 // Write the Physical Address and Virtual Address. In an object file these
491 // are the same.
492 W.write<uint32_t>(Sec->Address);
493 W.write<uint32_t>(Sec->Address);
494
495 W.write<uint32_t>(Sec->Size);
496 W.write<uint32_t>(Sec->FileOffsetToData);
497
498 // Relocation pointer and Lineno pointer. Not supported yet.
499 W.write<uint32_t>(0);
500 W.write<uint32_t>(0);
501
502 // Relocation and line-number counts. Not supported yet.
503 W.write<uint16_t>(0);
504 W.write<uint16_t>(0);
505
506 W.write<int32_t>(Sec->Flags);
507 }
508}
509
Digger Linfdfd6ab2019-10-15 17:40:41 +0000510void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
jasonliu78207e12019-10-24 15:19:12 +0000511 for (const auto *Section : Sections) {
512 for (const auto *Group : Section->Groups) {
513 if (Group->Csects.empty())
514 continue;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000515
jasonliu78207e12019-10-24 15:19:12 +0000516 const bool SupportLabelDef = Group->SupportLabelDef;
517 const int16_t SectionIndex = Section->Index;
518 for (const auto &Csect : Group->Csects) {
519 // Write out the control section first and then each symbol in it.
520 writeSymbolTableEntryForControlSection(
521 Csect, SectionIndex, Csect.MCCsect->getStorageClass());
522 if (!SupportLabelDef) {
523 assert(Csect.Syms.size() == 1 && "Csect should only contain 1 symbol "
524 "which is its label definition.");
525 continue;
526 }
527
528 for (const auto Sym : Csect.Syms)
529 writeSymbolTableEntryForCsectMemberLabel(
530 Sym, Csect, SectionIndex, Layout.getSymbolOffset(*(Sym.MCSym)));
531 }
532 }
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000533 }
534}
535
Digger Linfdfd6ab2019-10-15 17:40:41 +0000536void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000537 // The address corrresponds to the address of sections and symbols in the
538 // object file. We place the shared address 0 immediately after the
539 // section header table.
540 uint32_t Address = 0;
541 // Section indices are 1-based in XCOFF.
jasonliu78207e12019-10-24 15:19:12 +0000542 int32_t SectionIndex = 1;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000543 // The first symbol table entry is for the file name. We are not emitting it
544 // yet, so start at index 0.
545 uint32_t SymbolTableIndex = 0;
546
jasonliu78207e12019-10-24 15:19:12 +0000547 for (auto *Section : Sections) {
548 const bool IsEmpty =
549 llvm::all_of(Section->Groups, [](const CsectGroup *Group) {
550 return Group->Csects.empty();
551 });
552 if (IsEmpty)
553 continue;
554
555 if (SectionIndex > MaxSectionIndex)
556 report_fatal_error("Section index overflow!");
557 Section->Index = SectionIndex++;
558
559 bool SectionAddressSet = false;
560 for (auto *Group : Section->Groups) {
561 if (Group->Csects.empty())
562 continue;
563
564 const bool SupportLabelDef = Group->SupportLabelDef;
565 for (auto &Csect : Group->Csects) {
566 const MCSectionXCOFF *MCSec = Csect.MCCsect;
567 Csect.Address = alignTo(Address, MCSec->getAlignment());
568 Csect.Size = Layout.getSectionAddressSize(MCSec);
569 Address = Csect.Address + Csect.Size;
570 Csect.SymbolTableIndex = SymbolTableIndex;
571 // 1 main and 1 auxiliary symbol table entry for the csect.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000572 SymbolTableIndex += 2;
jasonliu78207e12019-10-24 15:19:12 +0000573
574 if (!SupportLabelDef)
575 continue;
576
577 for (auto &Sym : Csect.Syms) {
578 Sym.SymbolTableIndex = SymbolTableIndex;
579 // 1 main and 1 auxiliary symbol table entry for each contained
580 // symbol.
581 SymbolTableIndex += 2;
582 }
583 }
584
585 if (!SectionAddressSet) {
586 Section->Address = Group->Csects.front().Address;
587 SectionAddressSet = true;
Digger Linfdfd6ab2019-10-15 17:40:41 +0000588 }
589 }
jasonliu78207e12019-10-24 15:19:12 +0000590
591 // Make sure the address of the next section aligned to
592 // DefaultSectionAlign.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000593 Address = alignTo(Address, DefaultSectionAlign);
jasonliu78207e12019-10-24 15:19:12 +0000594 Section->Size = Address - Section->Address;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000595 }
596
597 SymbolTableEntryCount = SymbolTableIndex;
598
599 // Calculate the RawPointer value for each section.
600 uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() +
601 Sections.size() * sizeof(XCOFF::SectionHeader32);
602 for (auto *Sec : Sections) {
603 if (!Sec->IsVirtual) {
604 Sec->FileOffsetToData = RawPointer;
605 RawPointer += Sec->Size;
606 }
607 }
608
609 // TODO Add in Relocation storage to the RawPointer Calculation.
610 // TODO What to align the SymbolTable to?
611 // TODO Error check that the number of symbol table entries fits in 32-bits
612 // signed ...
613 if (SymbolTableEntryCount)
614 SymbolTableOffset = RawPointer;
615}
616
617// Takes the log base 2 of the alignment and shifts the result into the 5 most
618// significant bits of a byte, then or's in the csect type into the least
619// significant 3 bits.
620uint8_t getEncodedType(const MCSectionXCOFF *Sec) {
621 unsigned Align = Sec->getAlignment();
622 assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.");
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000623 unsigned Log2Align = Log2_32(Align);
624 // Result is a number in the range [0, 31] which fits in the 5 least
625 // significant bits. Shift this value into the 5 most significant bits, and
626 // bitwise-or in the csect type.
627 uint8_t EncodedAlign = Log2Align << 3;
628 return EncodedAlign | Sec->getCSectType();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000629}
630
631} // end anonymous namespace
632
633std::unique_ptr<MCObjectWriter>
634llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
635 raw_pwrite_stream &OS) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000636 return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000637}