Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 1 | //===-- lib/MC/XCOFFObjectWriter.cpp - XCOFF file writer ------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements XCOFF object file writer information. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/XCOFF.h" |
| 14 | #include "llvm/MC/MCAsmLayout.h" |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAssembler.h" |
| 16 | #include "llvm/MC/MCObjectWriter.h" |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionXCOFF.h" |
| 18 | #include "llvm/MC/MCSymbolXCOFF.h" |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCValue.h" |
| 20 | #include "llvm/MC/MCXCOFFObjectWriter.h" |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 21 | #include "llvm/MC/StringTableBuilder.h" |
| 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/MathExtras.h" |
| 24 | |
| 25 | #include <deque> |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 29 | // An XCOFF object file has a limited set of predefined sections. The most |
| 30 | // important ones for us (right now) are: |
| 31 | // .text --> contains program code and read-only data. |
| 32 | // .data --> contains initialized data, function descriptors, and the TOC. |
| 33 | // .bss --> contains uninitialized data. |
| 34 | // Each of these sections is composed of 'Control Sections'. A Control Section |
| 35 | // is more commonly referred to as a csect. A csect is an indivisible unit of |
| 36 | // code or data, and acts as a container for symbols. A csect is mapped |
| 37 | // into a section based on its storage-mapping class, with the exception of |
| 38 | // XMC_RW which gets mapped to either .data or .bss based on whether it's |
| 39 | // explicitly initialized or not. |
| 40 | // |
| 41 | // We don't represent the sections in the MC layer as there is nothing |
| 42 | // interesting about them at at that level: they carry information that is |
| 43 | // only relevant to the ObjectWriter, so we materialize them in this class. |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 46 | constexpr unsigned DefaultSectionAlign = 4; |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 47 | constexpr int16_t MaxSectionIndex = INT16_MAX; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 48 | |
| 49 | // Packs the csect's alignment and type into a byte. |
| 50 | uint8_t getEncodedType(const MCSectionXCOFF *); |
| 51 | |
| 52 | // Wrapper around an MCSymbolXCOFF. |
| 53 | struct 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 61 | Symbol(const MCSymbolXCOFF *MCSym) : MCSym(MCSym), SymbolTableIndex(-1) {} |
| 62 | }; |
| 63 | |
| 64 | // Wrapper for an MCSectionXCOFF. |
| 65 | struct 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 72 | StringRef getName() const { return MCCsect->getSectionName(); } |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 73 | ControlSection(const MCSectionXCOFF *MCSec) |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 74 | : MCCsect(MCSec), SymbolTableIndex(-1), Address(-1), Size(0) {} |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 77 | // 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 Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 81 | using CsectGroup = std::deque<ControlSection>; |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 82 | |
| 83 | using CsectGroups = std::deque<CsectGroup *>; |
| 84 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 85 | // 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. |
| 90 | struct 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 101 | int16_t Index; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 102 | |
| 103 | // Virtual sections do not need storage allocated in the object file. |
| 104 | const bool IsVirtual; |
| 105 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 106 | // 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 118 | void reset() { |
| 119 | Address = 0; |
| 120 | Size = 0; |
| 121 | FileOffsetToData = 0; |
| 122 | FileOffsetToRelocations = 0; |
| 123 | RelocationCount = 0; |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 124 | Index = UninitializedIndex; |
| 125 | // Clear any csects we have stored. |
| 126 | for (auto *Group : Groups) |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 127 | Group->clear(); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 128 | } |
| 129 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 130 | Section(const char *N, XCOFF::SectionTypeFlags Flags, bool IsVirtual, |
| 131 | CsectGroups Groups) |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 132 | : Address(0), Size(0), FileOffsetToData(0), FileOffsetToRelocations(0), |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 133 | RelocationCount(0), Flags(Flags), Index(UninitializedIndex), |
| 134 | IsVirtual(IsVirtual), Groups(Groups) { |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 135 | strncpy(Name, N, XCOFF::NameSize); |
| 136 | } |
| 137 | }; |
| 138 | |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 139 | class XCOFFObjectWriter : public MCObjectWriter { |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 140 | |
| 141 | uint32_t SymbolTableEntryCount = 0; |
| 142 | uint32_t SymbolTableOffset = 0; |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 143 | uint16_t SectionCount = 0; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 144 | |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 145 | support::endian::Writer W; |
| 146 | std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 147 | StringTableBuilder Strings; |
| 148 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 149 | // 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 Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 152 | CsectGroup ProgramCodeCsects; |
| 153 | CsectGroup DataCsects; |
| 154 | CsectGroup BSSCsects; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 155 | |
| 156 | // The Predefined sections. |
| 157 | Section Text; |
jasonliu | 8bd0c97 | 2019-10-30 18:31:31 +0000 | [diff] [blame] | 158 | Section Data; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 159 | Section BSS; |
| 160 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 161 | // All the XCOFF sections, in the order they will appear in the section header |
| 162 | // table. |
jasonliu | 8bd0c97 | 2019-10-30 18:31:31 +0000 | [diff] [blame] | 163 | std::array<Section *const, 3> Sections{{&Text, &Data, &BSS}}; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 164 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 165 | CsectGroup &getCsectGroup(const MCSectionXCOFF *MCSec); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 166 | |
| 167 | virtual void reset() override; |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 168 | |
| 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 176 | 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 183 | void writeFileHeader(); |
| 184 | void writeSectionHeaderTable(); |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 185 | void writeSections(const MCAssembler &Asm, const MCAsmLayout &Layout); |
| 186 | void writeSymbolTable(const MCAsmLayout &Layout); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 187 | |
| 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 196 | void assignAddressesAndIndices(const MCAsmLayout &); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 197 | |
| 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 Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 210 | public: |
| 211 | XCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, |
| 212 | raw_pwrite_stream &OS); |
| 213 | }; |
| 214 | |
| 215 | XCOFFObjectWriter::XCOFFObjectWriter( |
| 216 | std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 217 | : W(OS, support::big), TargetObjectWriter(std::move(MOTW)), |
| 218 | Strings(StringTableBuilder::XCOFF), |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 219 | Text(".text", XCOFF::STYP_TEXT, /* IsVirtual */ false, |
| 220 | CsectGroups{&ProgramCodeCsects}), |
jasonliu | 8bd0c97 | 2019-10-30 18:31:31 +0000 | [diff] [blame] | 221 | Data(".data", XCOFF::STYP_DATA, /* IsVirtual */ false, |
| 222 | CsectGroups{&DataCsects}), |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 223 | BSS(".bss", XCOFF::STYP_BSS, /* IsVirtual */ true, |
| 224 | CsectGroups{&BSSCsects}) {} |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 225 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 226 | void 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 230 | |
| 231 | // Reset the symbol table and string table. |
| 232 | SymbolTableEntryCount = 0; |
| 233 | SymbolTableOffset = 0; |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 234 | SectionCount = 0; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 235 | Strings.clear(); |
| 236 | |
| 237 | MCObjectWriter::reset(); |
| 238 | } |
| 239 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 240 | CsectGroup &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 | |
jasonliu | 8bd0c97 | 2019-10-30 18:31:31 +0000 | [diff] [blame] | 250 | if (XCOFF::XTY_SD == MCSec->getCSectType()) |
| 251 | return DataCsects; |
| 252 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 253 | 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 264 | void XCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
| 265 | const MCAsmLayout &Layout) { |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 266 | 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 Pilgrim | ef0cb27 | 2019-10-14 16:46:11 +0000 | [diff] [blame] | 275 | const auto *MCSec = cast<const MCSectionXCOFF>(&S); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 276 | assert(WrapperMap.find(MCSec) == WrapperMap.end() && |
| 277 | "Cannot add a csect twice."); |
| 278 | |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 279 | // 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 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 284 | // TODO FIXME Handle emiting the TOC base. |
| 285 | if (MCSec->getMappingClass() == XCOFF::XMC_TC0) |
| 286 | continue; |
| 287 | |
| 288 | CsectGroup &Group = getCsectGroup(MCSec); |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 289 | Group.emplace_back(MCSec); |
| 290 | WrapperMap[MCSec] = &Group.back(); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 291 | } |
| 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 Fertile | 18fd1b0 | 2019-08-22 15:11:23 +0000 | [diff] [blame] | 300 | const MCSectionXCOFF *ContainingCsect = XSym->getContainingCsect(); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 301 | assert(WrapperMap.find(ContainingCsect) != WrapperMap.end() && |
| 302 | "Expected containing csect to exist in map"); |
| 303 | |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 304 | // 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 309 | // 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 314 | if (nameShouldBeInStringTable(XSym->getName())) |
| 315 | Strings.add(XSym->getName()); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 316 | } |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 317 | |
| 318 | Strings.finalize(); |
| 319 | assignAddressesAndIndices(Layout); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 328 | void XCOFFObjectWriter::writeSections(const MCAssembler &Asm, |
| 329 | const MCAsmLayout &Layout) { |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 330 | 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 335 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 336 | assert(CurrentAddressLocation == Section->Address && |
| 337 | "We should have no padding between sections."); |
| 338 | for (const auto *Group : Section->Groups) { |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 339 | for (const auto &Csect : *Group) { |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 340 | 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 347 | // 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 = |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 351 | Section->Address + Section->Size - CurrentAddressLocation) |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 352 | W.OS.write_zeros(PaddingSize); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | uint64_t XCOFFObjectWriter::writeObject(MCAssembler &Asm, |
| 357 | const MCAsmLayout &Layout) { |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 358 | // 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 369 | writeFileHeader(); |
| 370 | writeSectionHeaderTable(); |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 371 | writeSections(Asm, Layout); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 372 | // TODO writeRelocations(); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 373 | |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 374 | writeSymbolTable(Layout); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 375 | // Write the string table. |
| 376 | Strings.write(W.OS); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 377 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 378 | return W.OS.tell() - StartOffset; |
| 379 | } |
| 380 | |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 381 | bool XCOFFObjectWriter::nameShouldBeInStringTable(const StringRef &SymbolName) { |
| 382 | return SymbolName.size() > XCOFF::NameSize; |
| 383 | } |
| 384 | |
| 385 | void 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 | |
| 397 | void 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 | |
| 434 | void 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 472 | void XCOFFObjectWriter::writeFileHeader() { |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 473 | // Magic. |
| 474 | W.write<uint16_t>(0x01df); |
| 475 | // Number of sections. |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 476 | W.write<uint16_t>(SectionCount); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 477 | // 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 Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 481 | W.write<uint32_t>(SymbolTableOffset); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 482 | // Number of entries in the symbol table. |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 483 | W.write<int32_t>(SymbolTableEntryCount); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 484 | // Size of the optional header. |
| 485 | W.write<uint16_t>(0); |
| 486 | // Flags. |
| 487 | W.write<uint16_t>(0); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 488 | } |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 489 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 490 | void XCOFFObjectWriter::writeSectionHeaderTable() { |
| 491 | for (const auto *Sec : Sections) { |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 492 | // Nothing to write for this Section. |
| 493 | if (Sec->Index == Section::UninitializedIndex) |
| 494 | continue; |
| 495 | |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 496 | // 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 520 | void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) { |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 521 | for (const auto *Section : Sections) { |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 522 | // Nothing to write for this Section. |
| 523 | if (Section->Index == Section::UninitializedIndex) |
| 524 | continue; |
| 525 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 526 | for (const auto *Group : Section->Groups) { |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 527 | if (Group->empty()) |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 528 | continue; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 529 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 530 | const int16_t SectionIndex = Section->Index; |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 531 | for (const auto &Csect : *Group) { |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 532 | // Write out the control section first and then each symbol in it. |
| 533 | writeSymbolTableEntryForControlSection( |
| 534 | Csect, SectionIndex, Csect.MCCsect->getStorageClass()); |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 535 | |
| 536 | for (const auto Sym : Csect.Syms) |
| 537 | writeSymbolTableEntryForCsectMemberLabel( |
| 538 | Sym, Csect, SectionIndex, Layout.getSymbolOffset(*(Sym.MCSym))); |
| 539 | } |
| 540 | } |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 544 | void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) { |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 545 | // 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. |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 550 | int32_t SectionIndex = 1; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 551 | // 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 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 555 | for (auto *Section : Sections) { |
| 556 | const bool IsEmpty = |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 557 | llvm::all_of(Section->Groups, |
| 558 | [](const CsectGroup *Group) { return Group->empty(); }); |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 559 | if (IsEmpty) |
| 560 | continue; |
| 561 | |
| 562 | if (SectionIndex > MaxSectionIndex) |
| 563 | report_fatal_error("Section index overflow!"); |
| 564 | Section->Index = SectionIndex++; |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 565 | SectionCount++; |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 566 | |
| 567 | bool SectionAddressSet = false; |
| 568 | for (auto *Group : Section->Groups) { |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 569 | if (Group->empty()) |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 570 | continue; |
| 571 | |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 572 | for (auto &Csect : *Group) { |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 573 | 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 Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 579 | SymbolTableIndex += 2; |
Jason Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 580 | |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 581 | 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 Liu | 0dc0572 | 2019-11-08 09:26:28 -0500 | [diff] [blame] | 590 | Section->Address = Group->front().Address; |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 591 | SectionAddressSet = true; |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 594 | |
| 595 | // Make sure the address of the next section aligned to |
| 596 | // DefaultSectionAlign. |
Digger Lin | fdfd6ab | 2019-10-15 17:40:41 +0000 | [diff] [blame] | 597 | Address = alignTo(Address, DefaultSectionAlign); |
jasonliu | 78207e1 | 2019-10-24 15:19:12 +0000 | [diff] [blame] | 598 | Section->Size = Address - Section->Address; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | SymbolTableEntryCount = SymbolTableIndex; |
| 602 | |
| 603 | // Calculate the RawPointer value for each section. |
| 604 | uint64_t RawPointer = sizeof(XCOFF::FileHeader32) + auxiliaryHeaderSize() + |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 605 | SectionCount * sizeof(XCOFF::SectionHeader32); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 606 | for (auto *Sec : Sections) { |
jasonliu | d83a2fa | 2019-10-28 21:46:22 +0000 | [diff] [blame] | 607 | if (Sec->Index == Section::UninitializedIndex || Sec->IsVirtual) |
| 608 | continue; |
| 609 | |
| 610 | Sec->FileOffsetToData = RawPointer; |
| 611 | RawPointer += Sec->Size; |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 612 | } |
| 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. |
| 625 | uint8_t getEncodedType(const MCSectionXCOFF *Sec) { |
| 626 | unsigned Align = Sec->getAlignment(); |
| 627 | assert(isPowerOf2_32(Align) && "Alignment must be a power of 2."); |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 628 | 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 Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | } // end anonymous namespace |
| 637 | |
| 638 | std::unique_ptr<MCObjectWriter> |
| 639 | llvm::createXCOFFObjectWriter(std::unique_ptr<MCXCOFFObjectTargetWriter> MOTW, |
| 640 | raw_pwrite_stream &OS) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 641 | return std::make_unique<XCOFFObjectWriter>(std::move(MOTW), OS); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 642 | } |