blob: cec74ee76c3cfea9ff0e143da03a2e07edac8b51 [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.
Jason Liu0dc05722019-11-08 09:26:28 -050081using CsectGroup = std::deque<ControlSection>;
jasonliu78207e12019-10-24 15:19:12 +000082
83using CsectGroups = std::deque<CsectGroup *>;
84
Sean Fertile1e46d4c2019-08-20 22:03:18 +000085// Represents the data related to a section excluding the csects that make up
86// the raw data of the section. The csects are stored separately as not all
87// sections contain csects, and some sections contain csects which are better
88// stored separately, e.g. the .data section containing read-write, descriptor,
89// TOCBase and TOC-entry csects.
90struct Section {
91 char Name[XCOFF::NameSize];
92 // The physical/virtual address of the section. For an object file
93 // these values are equivalent.
94 uint32_t Address;
95 uint32_t Size;
96 uint32_t FileOffsetToData;
97 uint32_t FileOffsetToRelocations;
98 uint32_t RelocationCount;
99 int32_t Flags;
100
Digger Linfdfd6ab2019-10-15 17:40:41 +0000101 int16_t Index;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000102
103 // Virtual sections do not need storage allocated in the object file.
104 const bool IsVirtual;
105
jasonliu78207e12019-10-24 15:19:12 +0000106 // XCOFF has special section numbers for symbols:
107 // -2 Specifies N_DEBUG, a special symbolic debugging symbol.
108 // -1 Specifies N_ABS, an absolute symbol. The symbol has a value but is not
109 // relocatable.
110 // 0 Specifies N_UNDEF, an undefined external symbol.
111 // Therefore, we choose -3 (N_DEBUG - 1) to represent a section index that
112 // hasn't been initialized.
113 static constexpr int16_t UninitializedIndex =
114 XCOFF::ReservedSectionNum::N_DEBUG - 1;
115
116 CsectGroups Groups;
117
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000118 void reset() {
119 Address = 0;
120 Size = 0;
121 FileOffsetToData = 0;
122 FileOffsetToRelocations = 0;
123 RelocationCount = 0;
jasonliu78207e12019-10-24 15:19:12 +0000124 Index = UninitializedIndex;
125 // Clear any csects we have stored.
126 for (auto *Group : Groups)
Jason Liu0dc05722019-11-08 09:26:28 -0500127 Group->clear();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000128 }
129
jasonliu78207e12019-10-24 15:19:12 +0000130 Section(const char *N, XCOFF::SectionTypeFlags Flags, bool IsVirtual,
131 CsectGroups Groups)
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000132 : Address(0), Size(0), FileOffsetToData(0), FileOffsetToRelocations(0),
jasonliu78207e12019-10-24 15:19:12 +0000133 RelocationCount(0), Flags(Flags), Index(UninitializedIndex),
134 IsVirtual(IsVirtual), Groups(Groups) {
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000135 strncpy(Name, N, XCOFF::NameSize);
136 }
137};
138
Sean Fertilef09d54e2019-07-09 19:21:01 +0000139class XCOFFObjectWriter : public MCObjectWriter {
jasonliu78207e12019-10-24 15:19:12 +0000140
141 uint32_t SymbolTableEntryCount = 0;
142 uint32_t SymbolTableOffset = 0;
jasonliud83a2fa2019-10-28 21:46:22 +0000143 uint16_t SectionCount = 0;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000144
Sean Fertilef09d54e2019-07-09 19:21:01 +0000145 support::endian::Writer W;
146 std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000147 StringTableBuilder Strings;
148
jasonliu78207e12019-10-24 15:19:12 +0000149 // CsectGroups. These store the csects which make up different parts of
150 // the sections. Should have one for each set of csects that get mapped into
151 // the same section and get handled in a 'similar' way.
Jason Liu0dc05722019-11-08 09:26:28 -0500152 CsectGroup ProgramCodeCsects;
153 CsectGroup DataCsects;
154 CsectGroup BSSCsects;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000155
156 // The Predefined sections.
157 Section Text;
jasonliu8bd0c972019-10-30 18:31:31 +0000158 Section Data;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000159 Section BSS;
160
jasonliu78207e12019-10-24 15:19:12 +0000161 // All the XCOFF sections, in the order they will appear in the section header
162 // table.
jasonliu8bd0c972019-10-30 18:31:31 +0000163 std::array<Section *const, 3> Sections{{&Text, &Data, &BSS}};
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000164
jasonliu78207e12019-10-24 15:19:12 +0000165 CsectGroup &getCsectGroup(const MCSectionXCOFF *MCSec);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000166
167 virtual void reset() override;
Sean Fertilef09d54e2019-07-09 19:21:01 +0000168
169 void executePostLayoutBinding(MCAssembler &, const MCAsmLayout &) override;
170
171 void recordRelocation(MCAssembler &, const MCAsmLayout &, const MCFragment *,
172 const MCFixup &, MCValue, uint64_t &) override;
173
174 uint64_t writeObject(MCAssembler &, const MCAsmLayout &) override;
175
Digger Linfdfd6ab2019-10-15 17:40:41 +0000176 static bool nameShouldBeInStringTable(const StringRef &);
177 void writeSymbolName(const StringRef &);
178 void writeSymbolTableEntryForCsectMemberLabel(const Symbol &,
179 const ControlSection &, int16_t,
180 uint64_t);
181 void writeSymbolTableEntryForControlSection(const ControlSection &, int16_t,
182 XCOFF::StorageClass);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000183 void writeFileHeader();
184 void writeSectionHeaderTable();
Digger Linfdfd6ab2019-10-15 17:40:41 +0000185 void writeSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
186 void writeSymbolTable(const MCAsmLayout &Layout);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000187
188 // Called after all the csects and symbols have been processed by
189 // `executePostLayoutBinding`, this function handles building up the majority
190 // of the structures in the object file representation. Namely:
191 // *) Calculates physical/virtual addresses, raw-pointer offsets, and section
192 // sizes.
193 // *) Assigns symbol table indices.
194 // *) Builds up the section header table by adding any non-empty sections to
195 // `Sections`.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000196 void assignAddressesAndIndices(const MCAsmLayout &);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000197
198 bool
199 needsAuxiliaryHeader() const { /* TODO aux header support not implemented. */
200 return false;
201 }
202
203 // Returns the size of the auxiliary header to be written to the object file.
204 size_t auxiliaryHeaderSize() const {
205 assert(!needsAuxiliaryHeader() &&
206 "Auxiliary header support not implemented.");
207 return 0;
208 }
209
Sean Fertilef09d54e2019-07-09 19:21:01 +0000210public:
211 XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
212 raw_pwrite_stream &OS);
213};
214
215XCOFFObjectWriter::XCOFFObjectWriter(
216 std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000217 : W(OS, support::big), TargetObjectWriter(std::move(MOTW)),
218 Strings(StringTableBuilder::XCOFF),
jasonliu78207e12019-10-24 15:19:12 +0000219 Text(".text", XCOFF::STYP_TEXT, /* IsVirtual */ false,
220 CsectGroups{&ProgramCodeCsects}),
jasonliu8bd0c972019-10-30 18:31:31 +0000221 Data(".data", XCOFF::STYP_DATA, /* IsVirtual */ false,
222 CsectGroups{&DataCsects}),
jasonliu78207e12019-10-24 15:19:12 +0000223 BSS(".bss", XCOFF::STYP_BSS, /* IsVirtual */ true,
224 CsectGroups{&BSSCsects}) {}
Sean Fertilef09d54e2019-07-09 19:21:01 +0000225
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000226void XCOFFObjectWriter::reset() {
227 // Reset any sections we have written to, and empty the section header table.
228 for (auto *Sec : Sections)
229 Sec->reset();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000230
231 // Reset the symbol table and string table.
232 SymbolTableEntryCount = 0;
233 SymbolTableOffset = 0;
jasonliud83a2fa2019-10-28 21:46:22 +0000234 SectionCount = 0;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000235 Strings.clear();
236
237 MCObjectWriter::reset();
238}
239
jasonliu78207e12019-10-24 15:19:12 +0000240CsectGroup &XCOFFObjectWriter::getCsectGroup(const MCSectionXCOFF *MCSec) {
241 switch (MCSec->getMappingClass()) {
242 case XCOFF::XMC_PR:
243 assert(XCOFF::XTY_SD == MCSec->getCSectType() &&
244 "Only an initialized csect can contain program code.");
245 return ProgramCodeCsects;
246 case XCOFF::XMC_RW:
247 if (XCOFF::XTY_CM == MCSec->getCSectType())
248 return BSSCsects;
249
jasonliu8bd0c972019-10-30 18:31:31 +0000250 if (XCOFF::XTY_SD == MCSec->getCSectType())
251 return DataCsects;
252
jasonliu78207e12019-10-24 15:19:12 +0000253 report_fatal_error("Unhandled mapping of read-write csect to section.");
254 case XCOFF::XMC_BS:
255 assert(XCOFF::XTY_CM == MCSec->getCSectType() &&
256 "Mapping invalid csect. CSECT with bss storage class must be "
257 "common type.");
258 return BSSCsects;
259 default:
260 report_fatal_error("Unhandled mapping of csect to section.");
261 }
262}
263
Digger Linfdfd6ab2019-10-15 17:40:41 +0000264void XCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
265 const MCAsmLayout &Layout) {
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000266 if (TargetObjectWriter->is64Bit())
267 report_fatal_error("64-bit XCOFF object files are not supported yet.");
268
269 // Maps the MC Section representation to its corresponding ControlSection
270 // wrapper. Needed for finding the ControlSection to insert an MCSymbol into
271 // from its containing MCSectionXCOFF.
272 DenseMap<const MCSectionXCOFF *, ControlSection *> WrapperMap;
273
274 for (const auto &S : Asm) {
Simon Pilgrimef0cb272019-10-14 16:46:11 +0000275 const auto *MCSec = cast<const MCSectionXCOFF>(&S);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000276 assert(WrapperMap.find(MCSec) == WrapperMap.end() &&
277 "Cannot add a csect twice.");
278
Digger Linfdfd6ab2019-10-15 17:40:41 +0000279 // If the name does not fit in the storage provided in the symbol table
280 // entry, add it to the string table.
281 if (nameShouldBeInStringTable(MCSec->getSectionName()))
282 Strings.add(MCSec->getSectionName());
283
jasonliu78207e12019-10-24 15:19:12 +0000284 // TODO FIXME Handle emiting the TOC base.
285 if (MCSec->getMappingClass() == XCOFF::XMC_TC0)
286 continue;
287
288 CsectGroup &Group = getCsectGroup(MCSec);
Jason Liu0dc05722019-11-08 09:26:28 -0500289 Group.emplace_back(MCSec);
290 WrapperMap[MCSec] = &Group.back();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000291 }
292
293 for (const MCSymbol &S : Asm.symbols()) {
294 // Nothing to do for temporary symbols.
295 if (S.isTemporary())
296 continue;
297 const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(&S);
298
299 // Map the symbol into its containing csect.
Sean Fertile18fd1b02019-08-22 15:11:23 +0000300 const MCSectionXCOFF *ContainingCsect = XSym->getContainingCsect();
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000301 assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() &&
302 "Expected containing csect to exist in map");
303
Jason Liu0dc05722019-11-08 09:26:28 -0500304 // If the symbol is the Csect itself, we don't need to put the symbol
305 // into Csect's Syms.
306 if (XSym == ContainingCsect->getQualNameSymbol())
307 continue;
308
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000309 // Lookup the containing csect and add the symbol to it.
310 WrapperMap[ContainingCsect]->Syms.emplace_back(XSym);
311
312 // If the name does not fit in the storage provided in the symbol table
313 // entry, add it to the string table.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000314 if (nameShouldBeInStringTable(XSym->getName()))
315 Strings.add(XSym->getName());
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000316 }
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000317
318 Strings.finalize();
319 assignAddressesAndIndices(Layout);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000320}
321
322void XCOFFObjectWriter::recordRelocation(MCAssembler &, const MCAsmLayout &,
323 const MCFragment *, const MCFixup &,
324 MCValue, uint64_t &) {
325 report_fatal_error("XCOFF relocations not supported.");
326}
327
Digger Linfdfd6ab2019-10-15 17:40:41 +0000328void XCOFFObjectWriter::writeSections(const MCAssembler &Asm,
329 const MCAsmLayout &Layout) {
jasonliu78207e12019-10-24 15:19:12 +0000330 uint32_t CurrentAddressLocation = 0;
331 for (const auto *Section : Sections) {
332 // Nothing to write for this Section.
333 if (Section->Index == Section::UninitializedIndex || Section->IsVirtual)
334 continue;
Digger Linfdfd6ab2019-10-15 17:40:41 +0000335
jasonliu78207e12019-10-24 15:19:12 +0000336 assert(CurrentAddressLocation == Section->Address &&
337 "We should have no padding between sections.");
338 for (const auto *Group : Section->Groups) {
Jason Liu0dc05722019-11-08 09:26:28 -0500339 for (const auto &Csect : *Group) {
jasonliu78207e12019-10-24 15:19:12 +0000340 if (uint32_t PaddingSize = Csect.Address - CurrentAddressLocation)
341 W.OS.write_zeros(PaddingSize);
342 Asm.writeSectionData(W.OS, Csect.MCCsect, Layout);
343 CurrentAddressLocation = Csect.Address + Csect.Size;
344 }
345 }
346
Digger Linfdfd6ab2019-10-15 17:40:41 +0000347 // The size of the tail padding in a section is the end virtual address of
348 // the current section minus the the end virtual address of the last csect
349 // in that section.
350 if (uint32_t PaddingSize =
jasonliu78207e12019-10-24 15:19:12 +0000351 Section->Address + Section->Size - CurrentAddressLocation)
Digger Linfdfd6ab2019-10-15 17:40:41 +0000352 W.OS.write_zeros(PaddingSize);
353 }
354}
355
356uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm,
357 const MCAsmLayout &Layout) {
Sean Fertilef09d54e2019-07-09 19:21:01 +0000358 // We always emit a timestamp of 0 for reproducibility, so ensure incremental
359 // linking is not enabled, in case, like with Windows COFF, such a timestamp
360 // is incompatible with incremental linking of XCOFF.
361 if (Asm.isIncrementalLinkerCompatible())
362 report_fatal_error("Incremental linking not supported for XCOFF.");
363
364 if (TargetObjectWriter->is64Bit())
365 report_fatal_error("64-bit XCOFF object files are not supported yet.");
366
367 uint64_t StartOffset = W.OS.tell();
368
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000369 writeFileHeader();
370 writeSectionHeaderTable();
Digger Linfdfd6ab2019-10-15 17:40:41 +0000371 writeSections(Asm, Layout);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000372 // TODO writeRelocations();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000373
Digger Linfdfd6ab2019-10-15 17:40:41 +0000374 writeSymbolTable(Layout);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000375 // Write the string table.
376 Strings.write(W.OS);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000377
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000378 return W.OS.tell() - StartOffset;
379}
380
Digger Linfdfd6ab2019-10-15 17:40:41 +0000381bool XCOFFObjectWriter::nameShouldBeInStringTable(const StringRef &SymbolName) {
382 return SymbolName.size() > XCOFF::NameSize;
383}
384
385void XCOFFObjectWriter::writeSymbolName(const StringRef &SymbolName) {
386 if (nameShouldBeInStringTable(SymbolName)) {
387 W.write<int32_t>(0);
388 W.write<uint32_t>(Strings.getOffset(SymbolName));
389 } else {
390 char Name[XCOFF::NameSize];
391 std::strncpy(Name, SymbolName.data(), XCOFF::NameSize);
392 ArrayRef<char> NameRef(Name, XCOFF::NameSize);
393 W.write(NameRef);
394 }
395}
396
397void XCOFFObjectWriter::writeSymbolTableEntryForCsectMemberLabel(
398 const Symbol &SymbolRef, const ControlSection &CSectionRef,
399 int16_t SectionIndex, uint64_t SymbolOffset) {
400 // Name or Zeros and string table offset
401 writeSymbolName(SymbolRef.getName());
402 assert(SymbolOffset <= UINT32_MAX - CSectionRef.Address &&
403 "Symbol address overflows.");
404 W.write<uint32_t>(CSectionRef.Address + SymbolOffset);
405 W.write<int16_t>(SectionIndex);
406 // Basic/Derived type. See the description of the n_type field for symbol
407 // table entries for a detailed description. Since we don't yet support
408 // visibility, and all other bits are either optionally set or reserved, this
409 // is always zero.
410 // TODO FIXME How to assert a symbol's visibilty is default?
411 // TODO Set the function indicator (bit 10, 0x0020) for functions
412 // when debugging is enabled.
413 W.write<uint16_t>(0);
414 W.write<uint8_t>(SymbolRef.getStorageClass());
415 // Always 1 aux entry for now.
416 W.write<uint8_t>(1);
417
418 // Now output the auxiliary entry.
419 W.write<uint32_t>(CSectionRef.SymbolTableIndex);
420 // Parameter typecheck hash. Not supported.
421 W.write<uint32_t>(0);
422 // Typecheck section number. Not supported.
423 W.write<uint16_t>(0);
424 // Symbol type: Label
425 W.write<uint8_t>(XCOFF::XTY_LD);
426 // Storage mapping class.
427 W.write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
428 // Reserved (x_stab).
429 W.write<uint32_t>(0);
430 // Reserved (x_snstab).
431 W.write<uint16_t>(0);
432}
433
434void XCOFFObjectWriter::writeSymbolTableEntryForControlSection(
435 const ControlSection &CSectionRef, int16_t SectionIndex,
436 XCOFF::StorageClass StorageClass) {
437 // n_name, n_zeros, n_offset
438 writeSymbolName(CSectionRef.getName());
439 // n_value
440 W.write<uint32_t>(CSectionRef.Address);
441 // n_scnum
442 W.write<int16_t>(SectionIndex);
443 // Basic/Derived type. See the description of the n_type field for symbol
444 // table entries for a detailed description. Since we don't yet support
445 // visibility, and all other bits are either optionally set or reserved, this
446 // is always zero.
447 // TODO FIXME How to assert a symbol's visibilty is default?
448 // TODO Set the function indicator (bit 10, 0x0020) for functions
449 // when debugging is enabled.
450 W.write<uint16_t>(0);
451 // n_sclass
452 W.write<uint8_t>(StorageClass);
453 // Always 1 aux entry for now.
454 W.write<uint8_t>(1);
455
456 // Now output the auxiliary entry.
457 W.write<uint32_t>(CSectionRef.Size);
458 // Parameter typecheck hash. Not supported.
459 W.write<uint32_t>(0);
460 // Typecheck section number. Not supported.
461 W.write<uint16_t>(0);
462 // Symbol type.
463 W.write<uint8_t>(getEncodedType(CSectionRef.MCCsect));
464 // Storage mapping class.
465 W.write<uint8_t>(CSectionRef.MCCsect->getMappingClass());
466 // Reserved (x_stab).
467 W.write<uint32_t>(0);
468 // Reserved (x_snstab).
469 W.write<uint16_t>(0);
470}
471
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000472void XCOFFObjectWriter::writeFileHeader() {
Sean Fertilef09d54e2019-07-09 19:21:01 +0000473 // Magic.
474 W.write<uint16_t>(0x01df);
475 // Number of sections.
jasonliud83a2fa2019-10-28 21:46:22 +0000476 W.write<uint16_t>(SectionCount);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000477 // Timestamp field. For reproducible output we write a 0, which represents no
478 // timestamp.
479 W.write<int32_t>(0);
480 // Byte Offset to the start of the symbol table.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000481 W.write<uint32_t>(SymbolTableOffset);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000482 // Number of entries in the symbol table.
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000483 W.write<int32_t>(SymbolTableEntryCount);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000484 // Size of the optional header.
485 W.write<uint16_t>(0);
486 // Flags.
487 W.write<uint16_t>(0);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000488}
Sean Fertilef09d54e2019-07-09 19:21:01 +0000489
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000490void XCOFFObjectWriter::writeSectionHeaderTable() {
491 for (const auto *Sec : Sections) {
jasonliud83a2fa2019-10-28 21:46:22 +0000492 // Nothing to write for this Section.
493 if (Sec->Index == Section::UninitializedIndex)
494 continue;
495
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000496 // Write Name.
497 ArrayRef<char> NameRef(Sec->Name, XCOFF::NameSize);
498 W.write(NameRef);
499
500 // Write the Physical Address and Virtual Address. In an object file these
501 // are the same.
502 W.write<uint32_t>(Sec->Address);
503 W.write<uint32_t>(Sec->Address);
504
505 W.write<uint32_t>(Sec->Size);
506 W.write<uint32_t>(Sec->FileOffsetToData);
507
508 // Relocation pointer and Lineno pointer. Not supported yet.
509 W.write<uint32_t>(0);
510 W.write<uint32_t>(0);
511
512 // Relocation and line-number counts. Not supported yet.
513 W.write<uint16_t>(0);
514 W.write<uint16_t>(0);
515
516 W.write<int32_t>(Sec->Flags);
517 }
518}
519
Digger Linfdfd6ab2019-10-15 17:40:41 +0000520void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
jasonliu78207e12019-10-24 15:19:12 +0000521 for (const auto *Section : Sections) {
jasonliud83a2fa2019-10-28 21:46:22 +0000522 // Nothing to write for this Section.
523 if (Section->Index == Section::UninitializedIndex)
524 continue;
525
jasonliu78207e12019-10-24 15:19:12 +0000526 for (const auto *Group : Section->Groups) {
Jason Liu0dc05722019-11-08 09:26:28 -0500527 if (Group->empty())
jasonliu78207e12019-10-24 15:19:12 +0000528 continue;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000529
jasonliu78207e12019-10-24 15:19:12 +0000530 const int16_t SectionIndex = Section->Index;
Jason Liu0dc05722019-11-08 09:26:28 -0500531 for (const auto &Csect : *Group) {
jasonliu78207e12019-10-24 15:19:12 +0000532 // Write out the control section first and then each symbol in it.
533 writeSymbolTableEntryForControlSection(
534 Csect, SectionIndex, Csect.MCCsect->getStorageClass());
jasonliu78207e12019-10-24 15:19:12 +0000535
536 for (const auto Sym : Csect.Syms)
537 writeSymbolTableEntryForCsectMemberLabel(
538 Sym, Csect, SectionIndex, Layout.getSymbolOffset(*(Sym.MCSym)));
539 }
540 }
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000541 }
542}
543
Digger Linfdfd6ab2019-10-15 17:40:41 +0000544void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000545 // The address corrresponds to the address of sections and symbols in the
546 // object file. We place the shared address 0 immediately after the
547 // section header table.
548 uint32_t Address = 0;
549 // Section indices are 1-based in XCOFF.
jasonliu78207e12019-10-24 15:19:12 +0000550 int32_t SectionIndex = 1;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000551 // The first symbol table entry is for the file name. We are not emitting it
552 // yet, so start at index 0.
553 uint32_t SymbolTableIndex = 0;
554
jasonliu78207e12019-10-24 15:19:12 +0000555 for (auto *Section : Sections) {
556 const bool IsEmpty =
Jason Liu0dc05722019-11-08 09:26:28 -0500557 llvm::all_of(Section->Groups,
558 [](const CsectGroup *Group) { return Group->empty(); });
jasonliu78207e12019-10-24 15:19:12 +0000559 if (IsEmpty)
560 continue;
561
562 if (SectionIndex > MaxSectionIndex)
563 report_fatal_error("Section index overflow!");
564 Section->Index = SectionIndex++;
jasonliud83a2fa2019-10-28 21:46:22 +0000565 SectionCount++;
jasonliu78207e12019-10-24 15:19:12 +0000566
567 bool SectionAddressSet = false;
568 for (auto *Group : Section->Groups) {
Jason Liu0dc05722019-11-08 09:26:28 -0500569 if (Group->empty())
jasonliu78207e12019-10-24 15:19:12 +0000570 continue;
571
Jason Liu0dc05722019-11-08 09:26:28 -0500572 for (auto &Csect : *Group) {
jasonliu78207e12019-10-24 15:19:12 +0000573 const MCSectionXCOFF *MCSec = Csect.MCCsect;
574 Csect.Address = alignTo(Address, MCSec->getAlignment());
575 Csect.Size = Layout.getSectionAddressSize(MCSec);
576 Address = Csect.Address + Csect.Size;
577 Csect.SymbolTableIndex = SymbolTableIndex;
578 // 1 main and 1 auxiliary symbol table entry for the csect.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000579 SymbolTableIndex += 2;
Jason Liu0dc05722019-11-08 09:26:28 -0500580
jasonliu78207e12019-10-24 15:19:12 +0000581 for (auto &Sym : Csect.Syms) {
582 Sym.SymbolTableIndex = SymbolTableIndex;
583 // 1 main and 1 auxiliary symbol table entry for each contained
584 // symbol.
585 SymbolTableIndex += 2;
586 }
587 }
588
589 if (!SectionAddressSet) {
Jason Liu0dc05722019-11-08 09:26:28 -0500590 Section->Address = Group->front().Address;
jasonliu78207e12019-10-24 15:19:12 +0000591 SectionAddressSet = true;
Digger Linfdfd6ab2019-10-15 17:40:41 +0000592 }
593 }
jasonliu78207e12019-10-24 15:19:12 +0000594
595 // Make sure the address of the next section aligned to
596 // DefaultSectionAlign.
Digger Linfdfd6ab2019-10-15 17:40:41 +0000597 Address = alignTo(Address, DefaultSectionAlign);
jasonliu78207e12019-10-24 15:19:12 +0000598 Section->Size = Address - Section->Address;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000599 }
600
601 SymbolTableEntryCount = SymbolTableIndex;
602
603 // Calculate the RawPointer value for each section.
604 uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() +
jasonliud83a2fa2019-10-28 21:46:22 +0000605 SectionCount * sizeof(XCOFF::SectionHeader32);
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000606 for (auto *Sec : Sections) {
jasonliud83a2fa2019-10-28 21:46:22 +0000607 if (Sec->Index == Section::UninitializedIndex || Sec->IsVirtual)
608 continue;
609
610 Sec->FileOffsetToData = RawPointer;
611 RawPointer += Sec->Size;
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000612 }
613
614 // TODO Add in Relocation storage to the RawPointer Calculation.
615 // TODO What to align the SymbolTable to?
616 // TODO Error check that the number of symbol table entries fits in 32-bits
617 // signed ...
618 if (SymbolTableEntryCount)
619 SymbolTableOffset = RawPointer;
620}
621
622// Takes the log base 2 of the alignment and shifts the result into the 5 most
623// significant bits of a byte, then or's in the csect type into the least
624// significant 3 bits.
625uint8_t getEncodedType(const MCSectionXCOFF *Sec) {
626 unsigned Align = Sec->getAlignment();
627 assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.");
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000628 unsigned Log2Align = Log2_32(Align);
629 // Result is a number in the range [0, 31] which fits in the 5 least
630 // significant bits. Shift this value into the 5 most significant bits, and
631 // bitwise-or in the csect type.
632 uint8_t EncodedAlign = Log2Align << 3;
633 return EncodedAlign | Sec->getCSectType();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000634}
635
636} // end anonymous namespace
637
638std::unique_ptr<MCObjectWriter>
639llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW,
640 raw_pwrite_stream &OS) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000641 return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000642}