brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.2 KiB · 4b0329d Raw
139 lines · c
1//===- OutputSections.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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLD_WASM_OUTPUT_SECTIONS_H10#define LLD_WASM_OUTPUT_SECTIONS_H11 12#include "InputChunks.h"13#include "WriterUtils.h"14#include "lld/Common/ErrorHandler.h"15#include "lld/Common/LLVM.h"16#include "llvm/ADT/DenseMap.h"17 18namespace lld {19 20namespace wasm {21class OutputSection;22}23std::string toString(const wasm::OutputSection &section);24 25namespace wasm {26 27class OutputSegment;28 29class OutputSection {30public:31  OutputSection(uint32_t type, std::string name = "")32      : type(type), name(name) {}33  virtual ~OutputSection() = default;34 35  StringRef getSectionName() const;36  void setOffset(size_t newOffset) { offset = newOffset; }37  void createHeader(size_t bodySize);38  virtual bool isNeeded() const { return true; }39  virtual size_t getSize() const = 0;40  virtual size_t getOffset() { return offset; }41  virtual void writeTo(uint8_t *buf) = 0;42  virtual void finalizeContents() = 0;43  virtual uint32_t getNumRelocations() const { return 0; }44  virtual uint32_t getNumLiveRelocations() const { return getNumRelocations(); }45  virtual void writeRelocations(raw_ostream &os) const {}46 47  std::string header;48  uint32_t type;49  uint32_t sectionIndex = UINT32_MAX;50  std::string name;51  OutputSectionSymbol *sectionSym = nullptr;52 53protected:54  size_t offset = 0;55};56 57class CodeSection : public OutputSection {58public:59  explicit CodeSection(ArrayRef<InputFunction *> functions)60      : OutputSection(llvm::wasm::WASM_SEC_CODE), functions(functions) {}61 62  static bool classof(const OutputSection *sec) {63    return sec->type == llvm::wasm::WASM_SEC_CODE;64  }65 66  size_t getSize() const override { return header.size() + bodySize; }67  void writeTo(uint8_t *buf) override;68  uint32_t getNumRelocations() const override;69  void writeRelocations(raw_ostream &os) const override;70  bool isNeeded() const override { return functions.size() > 0; }71  void finalizeContents() override;72 73  ArrayRef<InputFunction *> functions;74 75protected:76  std::string codeSectionHeader;77  size_t bodySize = 0;78};79 80class DataSection : public OutputSection {81public:82  explicit DataSection(ArrayRef<OutputSegment *> segments)83      : OutputSection(llvm::wasm::WASM_SEC_DATA), segments(segments) {}84 85  static bool classof(const OutputSection *sec) {86    return sec->type == llvm::wasm::WASM_SEC_DATA;87  }88 89  size_t getSize() const override { return header.size() + bodySize; }90  void writeTo(uint8_t *buf) override;91  uint32_t getNumRelocations() const override;92  void writeRelocations(raw_ostream &os) const override;93  bool isNeeded() const override;94  void finalizeContents() override;95 96  ArrayRef<OutputSegment *> segments;97 98protected:99  std::string dataSectionHeader;100  size_t bodySize = 0;101};102 103// Represents a custom section in the output file.  Wasm custom sections are104// used for storing user-defined metadata.  Unlike the core sections types105// they are identified by their string name.106// The linker combines custom sections that have the same name by simply107// concatenating them.108// Note that some custom sections such as "name" and "linking" are handled109// separately and are instead synthesized by the linker.110class CustomSection : public OutputSection {111public:112  CustomSection(std::string name, ArrayRef<InputChunk *> inputSections)113      : OutputSection(llvm::wasm::WASM_SEC_CUSTOM, name),114        inputSections(inputSections) {}115 116  static bool classof(const OutputSection *sec) {117    return sec->type == llvm::wasm::WASM_SEC_CUSTOM;118  }119 120  size_t getSize() const override {121    return header.size() + nameData.size() + payloadSize;122  }123  void writeTo(uint8_t *buf) override;124  uint32_t getNumRelocations() const override;125  void writeRelocations(raw_ostream &os) const override;126  void finalizeContents() override;127 128protected:129  void finalizeInputSections();130  size_t payloadSize = 0;131  std::vector<InputChunk *> inputSections;132  std::string nameData;133};134 135} // namespace wasm136} // namespace lld137 138#endif // LLD_WASM_OUTPUT_SECTIONS_H139