| Rafael Espindola | beee25e | 2015-08-14 14:12:54 +0000 | [diff] [blame] | 1 | //===- Writer.h -------------------------------------------------*- C++ -*-===// |
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLD_COFF_WRITER_H |
| 11 | #define LLD_COFF_WRITER_H |
| 12 | |
| Peter Collingbourne | 6f24fdb | 2017-01-14 03:14:46 +0000 | [diff] [blame] | 13 | #include "Chunks.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/Object/COFF.h" |
| 16 | #include <cstdint> |
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | namespace lld { |
| 20 | namespace coff { |
| Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 21 | class SymbolTable; |
| Rui Ueyama | 685c41c | 2015-08-05 23:43:53 +0000 | [diff] [blame] | 22 | |
| Peter Collingbourne | 6f24fdb | 2017-01-14 03:14:46 +0000 | [diff] [blame] | 23 | static const int PageSize = 4096; |
| 24 | |
| Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 25 | void writeResult(SymbolTable *T); |
| Rui Ueyama | 685c41c | 2015-08-05 23:43:53 +0000 | [diff] [blame] | 26 | |
| Peter Collingbourne | 6f24fdb | 2017-01-14 03:14:46 +0000 | [diff] [blame] | 27 | // OutputSection represents a section in an output file. It's a |
| 28 | // container of chunks. OutputSection and Chunk are 1:N relationship. |
| 29 | // Chunks cannot belong to more than one OutputSections. The writer |
| 30 | // creates multiple OutputSections and assign them unique, |
| 31 | // non-overlapping file offsets and RVAs. |
| 32 | class OutputSection { |
| 33 | public: |
| 34 | OutputSection(llvm::StringRef N) : Name(N), Header({}) {} |
| 35 | void setRVA(uint64_t); |
| 36 | void setFileOffset(uint64_t); |
| 37 | void addChunk(Chunk *C); |
| 38 | llvm::StringRef getName() { return Name; } |
| 39 | std::vector<Chunk *> &getChunks() { return Chunks; } |
| 40 | void addPermissions(uint32_t C); |
| 41 | void setPermissions(uint32_t C); |
| 42 | uint32_t getPermissions() { return Header.Characteristics & PermMask; } |
| 43 | uint32_t getCharacteristics() { return Header.Characteristics; } |
| 44 | uint64_t getRVA() { return Header.VirtualAddress; } |
| 45 | uint64_t getFileOff() { return Header.PointerToRawData; } |
| 46 | void writeHeaderTo(uint8_t *Buf); |
| 47 | |
| 48 | // Returns the size of this section in an executable memory image. |
| 49 | // This may be smaller than the raw size (the raw size is multiple |
| 50 | // of disk sector size, so there may be padding at end), or may be |
| 51 | // larger (if that's the case, the loader reserves spaces after end |
| 52 | // of raw data). |
| 53 | uint64_t getVirtualSize() { return Header.VirtualSize; } |
| 54 | |
| 55 | // Returns the size of the section in the output file. |
| 56 | uint64_t getRawSize() { return Header.SizeOfRawData; } |
| 57 | |
| 58 | // Set offset into the string table storing this section name. |
| 59 | // Used only when the name is longer than 8 bytes. |
| 60 | void setStringTableOff(uint32_t V) { StringTableOff = V; } |
| 61 | |
| 62 | // N.B. The section index is one based. |
| 63 | uint32_t SectionIndex = 0; |
| 64 | |
| 65 | private: |
| 66 | llvm::StringRef Name; |
| 67 | llvm::object::coff_section Header; |
| 68 | uint32_t StringTableOff = 0; |
| 69 | std::vector<Chunk *> Chunks; |
| 70 | }; |
| 71 | |
| Rui Ueyama | 685c41c | 2015-08-05 23:43:53 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 74 | |
| 75 | #endif |