reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLD_WASM_OUTPUT_SECTIONS_H
#define LLD_WASM_OUTPUT_SECTIONS_H

#include "InputChunks.h"
#include "WriterUtils.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/LLVM.h"
#include "llvm/ADT/DenseMap.h"

namespace lld {

namespace wasm {
class OutputSection;
}
std::string toString(const wasm::OutputSection &section);

namespace wasm {

class OutputSegment;

class OutputSection {
public:
  OutputSection(uint32_t type, std::string name = "")
      : type(type), name(name) {}
  virtual ~OutputSection() = default;

  StringRef getSectionName() const;
  void setOffset(size_t newOffset) {
    log("setOffset: " + toString(*this) + ": " + Twine(newOffset));
    offset = newOffset;
  }
  void createHeader(size_t bodySize);
  virtual bool isNeeded() const { return true; }
  virtual size_t getSize() const = 0;
  virtual void writeTo(uint8_t *buf) = 0;
  virtual void finalizeContents() = 0;
  virtual uint32_t getNumRelocations() const { return 0; }
  virtual void writeRelocations(raw_ostream &os) const {}

  std::string header;
  uint32_t type;
  uint32_t sectionIndex = UINT32_MAX;
  std::string name;
  OutputSectionSymbol *sectionSym = nullptr;

protected:
  size_t offset = 0;
};

class CodeSection : public OutputSection {
public:
  explicit CodeSection(ArrayRef<InputFunction *> functions)
      : OutputSection(llvm::wasm::WASM_SEC_CODE), functions(functions) {}

  size_t getSize() const override { return header.size() + bodySize; }
  void writeTo(uint8_t *buf) override;
  uint32_t getNumRelocations() const override;
  void writeRelocations(raw_ostream &os) const override;
  bool isNeeded() const override { return functions.size() > 0; }
  void finalizeContents() override;

protected:
  ArrayRef<InputFunction *> functions;
  std::string codeSectionHeader;
  size_t bodySize = 0;
};

class DataSection : public OutputSection {
public:
  explicit DataSection(ArrayRef<OutputSegment *> segments)
      : OutputSection(llvm::wasm::WASM_SEC_DATA), segments(segments) {}

  size_t getSize() const override { return header.size() + bodySize; }
  void writeTo(uint8_t *buf) override;
  uint32_t getNumRelocations() const override;
  void writeRelocations(raw_ostream &os) const override;
  bool isNeeded() const override;
  void finalizeContents() override;

protected:
  ArrayRef<OutputSegment *> segments;
  std::string dataSectionHeader;
  size_t bodySize = 0;
};

// Represents a custom section in the output file.  Wasm custom sections are
// used for storing user-defined metadata.  Unlike the core sections types
// they are identified by their string name.
// The linker combines custom sections that have the same name by simply
// concatenating them.
// Note that some custom sections such as "name" and "linking" are handled
// separately and are instead synthesized by the linker.
class CustomSection : public OutputSection {
public:
  CustomSection(std::string name, ArrayRef<InputSection *> inputSections)
      : OutputSection(llvm::wasm::WASM_SEC_CUSTOM, name),
        inputSections(inputSections) {}
  size_t getSize() const override {
    return header.size() + nameData.size() + payloadSize;
  }
  void writeTo(uint8_t *buf) override;
  uint32_t getNumRelocations() const override;
  void writeRelocations(raw_ostream &os) const override;
  void finalizeContents() override;

protected:
  size_t payloadSize = 0;
  ArrayRef<InputSection *> inputSections;
  std::string nameData;
};

} // namespace wasm
} // namespace lld

#endif // LLD_WASM_OUTPUT_SECTIONS_H