1 //===- Writer.h -------------------------------------------------*- C++ -*-===// 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 #ifndef LLD_COFF_WRITER_H 10 #define LLD_COFF_WRITER_H 11 12 #include "Chunks.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/Object/COFF.h" 15 #include <chrono> 16 #include <cstdint> 17 #include <vector> 18 19 namespace lld { 20 namespace coff { 21 static const int pageSize = 4096; 22 class COFFLinkerContext; 23 24 void writeResult(COFFLinkerContext &ctx); 25 26 class PartialSection { 27 public: 28 PartialSection(StringRef n, uint32_t chars) 29 : name(n), characteristics(chars) {} 30 StringRef name; 31 unsigned characteristics; 32 std::vector<Chunk *> chunks; 33 }; 34 35 // OutputSection represents a section in an output file. It's a 36 // container of chunks. OutputSection and Chunk are 1:N relationship. 37 // Chunks cannot belong to more than one OutputSections. The writer 38 // creates multiple OutputSections and assign them unique, 39 // non-overlapping file offsets and RVAs. 40 class OutputSection { 41 public: 42 OutputSection(llvm::StringRef n, uint32_t chars) : name(n) { 43 header.Characteristics = chars; 44 } 45 void addChunk(Chunk *c); 46 void insertChunkAtStart(Chunk *c); 47 void merge(OutputSection *other); 48 void setPermissions(uint32_t c); 49 uint64_t getRVA() { return header.VirtualAddress; } 50 uint64_t getFileOff() { return header.PointerToRawData; } 51 void writeHeaderTo(uint8_t *buf); 52 void addContributingPartialSection(PartialSection *sec); 53 54 // Returns the size of this section in an executable memory image. 55 // This may be smaller than the raw size (the raw size is multiple 56 // of disk sector size, so there may be padding at end), or may be 57 // larger (if that's the case, the loader reserves spaces after end 58 // of raw data). 59 uint64_t getVirtualSize() { return header.VirtualSize; } 60 61 // Returns the size of the section in the output file. 62 uint64_t getRawSize() { return header.SizeOfRawData; } 63 64 // Set offset into the string table storing this section name. 65 // Used only when the name is longer than 8 bytes. 66 void setStringTableOff(uint32_t v) { stringTableOff = v; } 67 68 // N.B. The section index is one based. 69 uint32_t sectionIndex = 0; 70 71 llvm::StringRef name; 72 llvm::object::coff_section header = {}; 73 74 std::vector<Chunk *> chunks; 75 std::vector<Chunk *> origChunks; 76 77 std::vector<PartialSection *> contribSections; 78 79 private: 80 uint32_t stringTableOff = 0; 81 }; 82 83 } // namespace coff 84 } // namespace lld 85 86 #endif 87