[JITLink] Add support of R_X86_64_32S relocation
[llvm-project.git] / lld / wasm / InputElement.h
blobd00e60b4ae96bcab38d20c62afc2e7b82260feeb
1 //===- InputElement.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 //===----------------------------------------------------------------------===//
9 #ifndef LLD_WASM_INPUT_ELEMENT_H
10 #define LLD_WASM_INPUT_ELEMENT_H
12 #include "Config.h"
13 #include "InputFiles.h"
14 #include "WriterUtils.h"
15 #include "lld/Common/LLVM.h"
16 #include "llvm/Object/Wasm.h"
18 namespace lld {
19 namespace wasm {
21 // Represents a single element (Global, Tag, Table, etc) within an input
22 // file.
23 class InputElement {
24 protected:
25 InputElement(StringRef name, ObjFile *f)
26 : file(f), live(!config->gcSections), name(name) {}
28 public:
29 StringRef getName() const { return name; }
30 uint32_t getAssignedIndex() const { return assignedIndex.getValue(); }
31 bool hasAssignedIndex() const { return assignedIndex.hasValue(); }
32 void assignIndex(uint32_t index) {
33 assert(!hasAssignedIndex());
34 assignedIndex = index;
37 ObjFile *file;
38 bool live = false;
40 protected:
41 StringRef name;
42 llvm::Optional<uint32_t> assignedIndex;
45 inline WasmInitExpr intConst(uint64_t value, bool is64) {
46 WasmInitExpr ie;
47 if (is64) {
48 ie.Opcode = llvm::wasm::WASM_OPCODE_I64_CONST;
49 ie.Value.Int64 = static_cast<int64_t>(value);
50 } else {
51 ie.Opcode = llvm::wasm::WASM_OPCODE_I32_CONST;
52 ie.Value.Int32 = static_cast<int32_t>(value);
54 return ie;
57 class InputGlobal : public InputElement {
58 public:
59 InputGlobal(const WasmGlobal &g, ObjFile *f)
60 : InputElement(g.SymbolName, f), type(g.Type), initExpr(g.InitExpr) {}
62 const WasmGlobalType &getType() const { return type; }
63 const WasmInitExpr &getInitExpr() const { return initExpr; }
65 void setPointerValue(uint64_t value) {
66 initExpr = intConst(value, config->is64.getValueOr(false));
69 private:
70 WasmGlobalType type;
71 WasmInitExpr initExpr;
74 class InputTag : public InputElement {
75 public:
76 InputTag(const WasmSignature &s, const WasmTag &t, ObjFile *f)
77 : InputElement(t.SymbolName, f), signature(s), type(t.Type) {}
79 const WasmTagType &getType() const { return type; }
81 const WasmSignature &signature;
83 private:
84 WasmTagType type;
87 class InputTable : public InputElement {
88 public:
89 InputTable(const WasmTable &t, ObjFile *f)
90 : InputElement(t.SymbolName, f), type(t.Type) {}
92 const WasmTableType &getType() const { return type; }
93 void setLimits(const WasmLimits &limits) { type.Limits = limits; }
95 private:
96 WasmTableType type;
99 } // namespace wasm
101 inline std::string toString(const wasm::InputElement *d) {
102 return (toString(d->file) + ":(" + d->getName() + ")").str();
105 } // namespace lld
107 #endif // LLD_WASM_INPUT_ELEMENT_H