Path: blob/main/contrib/llvm-project/lld/COFF/Writer.h
34870 views
//===- Writer.h -------------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLD_COFF_WRITER_H9#define LLD_COFF_WRITER_H1011#include "Chunks.h"12#include "llvm/ADT/StringRef.h"13#include "llvm/Object/COFF.h"14#include <chrono>15#include <cstdint>16#include <vector>1718namespace lld::coff {19static const int pageSize = 4096;20class COFFLinkerContext;2122void writeResult(COFFLinkerContext &ctx);2324class PartialSection {25public:26PartialSection(StringRef n, uint32_t chars)27: name(n), characteristics(chars) {}28StringRef name;29unsigned characteristics;30std::vector<Chunk *> chunks;31};3233// OutputSection represents a section in an output file. It's a34// container of chunks. OutputSection and Chunk are 1:N relationship.35// Chunks cannot belong to more than one OutputSections. The writer36// creates multiple OutputSections and assign them unique,37// non-overlapping file offsets and RVAs.38class OutputSection {39public:40OutputSection(llvm::StringRef n, uint32_t chars) : name(n) {41header.Characteristics = chars;42}43void addChunk(Chunk *c);44void insertChunkAtStart(Chunk *c);45void merge(OutputSection *other);46void setPermissions(uint32_t c);47uint64_t getRVA() const { return header.VirtualAddress; }48uint64_t getFileOff() const { return header.PointerToRawData; }49void writeHeaderTo(uint8_t *buf, bool isDebug);50void addContributingPartialSection(PartialSection *sec);5152// Returns the size of this section in an executable memory image.53// This may be smaller than the raw size (the raw size is multiple54// of disk sector size, so there may be padding at end), or may be55// larger (if that's the case, the loader reserves spaces after end56// of raw data).57uint64_t getVirtualSize() { return header.VirtualSize; }5859// Returns the size of the section in the output file.60uint64_t getRawSize() { return header.SizeOfRawData; }6162// Set offset into the string table storing this section name.63// Used only when the name is longer than 8 bytes.64void setStringTableOff(uint32_t v) { stringTableOff = v; }6566bool isCodeSection() const {67return (header.Characteristics & llvm::COFF::IMAGE_SCN_CNT_CODE) &&68(header.Characteristics & llvm::COFF::IMAGE_SCN_MEM_READ) &&69(header.Characteristics & llvm::COFF::IMAGE_SCN_MEM_EXECUTE);70}7172// N.B. The section index is one based.73uint32_t sectionIndex = 0;7475llvm::StringRef name;76llvm::object::coff_section header = {};7778std::vector<Chunk *> chunks;79std::vector<Chunk *> origChunks;8081std::vector<PartialSection *> contribSections;8283private:84uint32_t stringTableOff = 0;85};8687} // namespace lld::coff8889#endif909192