GTIRB  v2.1.0
GrammaTech Intermediate Representation for Binaries: C++ API
SymbolicExpression.hpp
Go to the documentation of this file.
1 //===- SymbolicExpression.hpp -----------------------------------*- C++ -*-===//
2 //
3 // Copyright (C) 2020 GrammaTech, Inc.
4 //
5 // This code is licensed under the MIT license. See the LICENSE file in the
6 // project root for license terms.
7 //
8 // This project is sponsored by the Office of Naval Research, One Liberty
9 // Center, 875 N. Randolph Street, Arlington, VA 22203 under contract #
10 // N68335-17-C-0700. The content of the information does not necessarily
11 // reflect the position or policy of the Government and no official
12 // endorsement should be inferred.
13 //
14 //===----------------------------------------------------------------------===//
15 #ifndef GTIRB_SYMBOLICEXPRESSION_H
16 #define GTIRB_SYMBOLICEXPRESSION_H
17 
18 #include <gtirb/Addr.hpp>
19 #include <gtirb/proto/SymbolicExpression.pb.h>
20 #include <bitset>
21 #include <boost/range/iterator_range.hpp>
22 #include <cstdint>
23 #include <functional>
24 #include <map>
25 #include <string>
26 #include <variant>
27 
33 namespace gtirb {
34 class Symbol; // Forward refernece for Sym, Sym1, Sym2, etc.
35 
40 
47 enum class SymAttribute : uint16_t {
48  // Common ELF relocation labels.
49  GOT = proto::SymAttribute::GOT,
50  GOTPC = proto::SymAttribute::GOTPC,
51  GOTOFF = proto::SymAttribute::GOTOFF,
52  GOTREL = proto::SymAttribute::GOTREL,
53  PLT = proto::SymAttribute::PLT,
54  PLTOFF = proto::SymAttribute::PLTOFF,
55  PCREL = proto::SymAttribute::PCREL,
56  SECREL = proto::SymAttribute::SECREL,
57  TLS = proto::SymAttribute::TLS,
58  TLSGD = proto::SymAttribute::TLSGD,
59  TLSLD = proto::SymAttribute::TLSLD,
60  TLSLDM = proto::SymAttribute::TLSLDM,
61  TLSCALL = proto::SymAttribute::TLSCALL,
62  TLSDESC = proto::SymAttribute::TLSDESC,
63  TPREL = proto::SymAttribute::TPREL,
64  TPOFF = proto::SymAttribute::TPOFF,
65  DTPREL = proto::SymAttribute::DTPREL,
66  DTPOFF = proto::SymAttribute::DTPOFF,
67  DTPMOD = proto::SymAttribute::DTPMOD,
68  NTPOFF = proto::SymAttribute::NTPOFF,
69  PAGE = proto::SymAttribute::PAGE,
70  PAGEOFF = proto::SymAttribute::PAGEOFF,
71  CALL = proto::SymAttribute::CALL,
72  LO = proto::SymAttribute::LO,
73  HI = proto::SymAttribute::HI,
74  HIGHER = proto::SymAttribute::HIGHER,
75  HIGHEST = proto::SymAttribute::HIGHEST,
76 
77  // X86-specific relocation labels.
78  GOTNTPOFF = proto::SymAttribute::GOTNTPOFF,
79  INDNTPOFF = proto::SymAttribute::INDNTPOFF,
80 
81  // ARM-specific relocation labels.
82  G0 = proto::SymAttribute::G0,
83  G1 = proto::SymAttribute::G1,
84  G2 = proto::SymAttribute::G2,
85  G3 = proto::SymAttribute::G3,
86  UPPER16 = proto::SymAttribute::UPPER16,
87  LOWER16 = proto::SymAttribute::LOWER16,
88  LO12 = proto::SymAttribute::LO12,
89  LO15 = proto::SymAttribute::LO15,
90  LO14 = proto::SymAttribute::LO14,
91  HI12 = proto::SymAttribute::HI12,
92  HI21 = proto::SymAttribute::HI21,
93  S = proto::SymAttribute::S,
94  PG = proto::SymAttribute::PG,
95  NC = proto::SymAttribute::NC,
96  ABS = proto::SymAttribute::ABS,
97  PREL = proto::SymAttribute::PREL,
98  PREL31 = proto::SymAttribute::PREL31,
99  TARGET1 = proto::SymAttribute::TARGET1,
100  TARGET2 = proto::SymAttribute::TARGET2,
101  SBREL = proto::SymAttribute::SBREL,
102  TLSLDO = proto::SymAttribute::TLSLDO,
103 
104  // MIPS-specific relocation labels.
105  HI16 = proto::SymAttribute::HI16,
106  LO16 = proto::SymAttribute::LO16,
107  GPREL = proto::SymAttribute::GPREL,
108  DISP = proto::SymAttribute::DISP,
109  OFST = proto::SymAttribute::OFST,
110 
111  // PPC
112  H = proto::SymAttribute::H,
113  L = proto::SymAttribute::L,
114  HA = proto::SymAttribute::HA,
115  HIGH = proto::SymAttribute::HIGH,
116  HIGHA = proto::SymAttribute::HIGHA,
117  HIGHERA = proto::SymAttribute::HIGHERA,
118  HIGHESTA = proto::SymAttribute::HIGHESTA,
119  TOCBASE = proto::SymAttribute::TOCBASE,
120  TOC = proto::SymAttribute::TOC,
121  NOTOC = proto::SymAttribute::NOTOC,
122 };
123 
124 using SymAttributeSet = std::set<SymAttribute>;
125 
129 struct SymAddrConst {
130  int64_t Offset;
133 
134  friend bool operator==(const SymAddrConst& LHS, const SymAddrConst& RHS) {
135  return LHS.Offset == RHS.Offset && LHS.Sym == RHS.Sym &&
136  LHS.Attributes == RHS.Attributes;
137  }
138 
139  friend bool operator!=(const SymAddrConst& LHS, const SymAddrConst& RHS) {
140  return !operator==(LHS, RHS);
141  }
142 };
143 
147 struct SymAddrAddr {
148  int64_t Scale;
149  int64_t Offset;
153 
154  friend bool operator==(const SymAddrAddr& LHS, const SymAddrAddr& RHS) {
155  return LHS.Scale == RHS.Scale && LHS.Offset == RHS.Offset &&
156  LHS.Sym1 == RHS.Sym1 && LHS.Sym2 == RHS.Sym2 &&
157  LHS.Attributes == RHS.Attributes;
158  }
159 
160  friend bool operator!=(const SymAddrAddr& LHS, const SymAddrAddr& RHS) {
161  return !operator==(LHS, RHS);
162  }
163 };
164 
166 using SymbolicExpression = std::variant<SymAddrConst, SymAddrAddr>;
167 
169 // (end \defgroup SYMBOLIC_EXPRESSION_GROUP)
170 
171 } // namespace gtirb
172 
173 namespace std {
174 template <> struct hash<gtirb::SymAddrConst> {
176  typedef std::size_t result_type;
177 
178  result_type operator()(const argument_type& Obj) const noexcept {
179  const result_type Off = std::hash<int64_t>{}(Obj.Offset);
180  const result_type P = std::hash<gtirb::Symbol*>{}(Obj.Sym);
181  return Off ^ (P << 1);
182  }
183 };
184 
185 template <> struct hash<gtirb::SymAddrAddr> {
187  typedef std::size_t result_type;
188 
189  result_type operator()(const argument_type& Obj) const noexcept {
190  result_type S = std::hash<int64_t>{}(Obj.Scale);
191  comb(S, std::hash<int64_t>{}(Obj.Offset));
192  comb(S, std::hash<gtirb::Symbol*>{}(Obj.Sym1));
193  comb(S, std::hash<gtirb::Symbol*>{}(Obj.Sym2));
194  return S;
195  }
196 
197 private:
198  void comb(result_type& One, result_type Two) const noexcept {
199  One ^= Two + 0x9e3779b9 + (One << 6) + (One >> 2);
200  }
201 };
202 } // namespace std
203 
204 #endif // GTIRB_SYMBOLICEXPRESSION_H
gtirb::SymAttribute::TLSGD
@ TLSGD
gtirb::SymAttribute::GPREL
@ GPREL
std::hash< gtirb::SymAddrAddr >::result_type
std::size_t result_type
Definition: SymbolicExpression.hpp:187
gtirb::SymAttribute::CALL
@ CALL
gtirb::SymAttribute::INDNTPOFF
@ INDNTPOFF
gtirb::SymAttribute::TLSCALL
@ TLSCALL
gtirb::SymAttribute::HI12
@ HI12
gtirb::SymAddrAddr::Offset
int64_t Offset
Constant offset.
Definition: SymbolicExpression.hpp:149
gtirb::SymAttribute::SBREL
@ SBREL
gtirb::SymAttribute::NC
@ NC
gtirb::SymAttribute::PREL31
@ PREL31
gtirb::SymAttribute::HIGHA
@ HIGHA
gtirb::SymAttribute::LO15
@ LO15
gtirb::SymAttribute::ABS
@ ABS
gtirb::SymAttribute::L
@ L
gtirb::SymAddrAddr::operator!=
friend bool operator!=(const SymAddrAddr &LHS, const SymAddrAddr &RHS)
Definition: SymbolicExpression.hpp:160
gtirb::SymAttribute::LO14
@ LO14
gtirb::SymAddrAddr::Sym1
Symbol * Sym1
Symbol representing the base address.
Definition: SymbolicExpression.hpp:150
std::hash< gtirb::SymAddrAddr >::operator()
result_type operator()(const argument_type &Obj) const noexcept
Definition: SymbolicExpression.hpp:189
std::hash< gtirb::SymAddrConst >::result_type
std::size_t result_type
Definition: SymbolicExpression.hpp:176
gtirb::SymAttribute::TLSDESC
@ TLSDESC
gtirb::SymAttribute::PLT
@ PLT
gtirb::SymAttribute::DTPOFF
@ DTPOFF
gtirb::SymAttribute::TPOFF
@ TPOFF
gtirb::SymAttribute::NTPOFF
@ NTPOFF
gtirb::SymAttribute::TARGET2
@ TARGET2
std::hash< gtirb::SymAddrAddr >::argument_type
gtirb::SymAddrAddr argument_type
Definition: SymbolicExpression.hpp:186
gtirb::SymAddrConst::Offset
int64_t Offset
Constant offset.
Definition: SymbolicExpression.hpp:130
gtirb::SymAttribute::NOTOC
@ NOTOC
gtirb::SymAttribute::HIGHERA
@ HIGHERA
gtirb::SymAttribute::TARGET1
@ TARGET1
gtirb::SymAddrAddr
Represents a symbolic operand of the form "(Sym1 - Sym2) / Scale + Offset".
Definition: SymbolicExpression.hpp:147
gtirb::SymAttribute::PAGEOFF
@ PAGEOFF
gtirb::SymAttribute::PCREL
@ PCREL
gtirb::SymAttribute
SymAttribute
The space of attributes that can be applied to a symbolic expression.
Definition: SymbolicExpression.hpp:47
gtirb::SymAttribute::HI21
@ HI21
gtirb::SymAttribute::S
@ S
gtirb::SymAddrAddr::Sym2
Symbol * Sym2
Symbol to subtract from Sym1.
Definition: SymbolicExpression.hpp:151
gtirb::SymAttribute::TPREL
@ TPREL
gtirb::SymAttribute::GOTPC
@ GOTPC
gtirb
Main namespace for the GTIRB API.
Definition: Addr.hpp:28
gtirb::SymAddrAddr::Scale
int64_t Scale
Constant scale factor.
Definition: SymbolicExpression.hpp:148
gtirb::SymAttribute::HA
@ HA
gtirb::SymAttribute::LO
@ LO
gtirb::SymAttribute::LOWER16
@ LOWER16
gtirb::SymAttribute::OFST
@ OFST
gtirb::SymAttribute::LO12
@ LO12
gtirb::SymAttribute::PAGE
@ PAGE
gtirb::SymAttribute::G3
@ G3
gtirb::SymAttribute::TOCBASE
@ TOCBASE
gtirb::SymAttribute::PLTOFF
@ PLTOFF
gtirb::SymAttribute::GOTOFF
@ GOTOFF
gtirb::SymAttribute::GOTNTPOFF
@ GOTNTPOFF
gtirb::SymAddrAddr::Attributes
SymAttributeSet Attributes
Definition: SymbolicExpression.hpp:152
gtirb::SymAttribute::TLSLDO
@ TLSLDO
gtirb::SymAddrAddr::operator==
friend bool operator==(const SymAddrAddr &LHS, const SymAddrAddr &RHS)
Definition: SymbolicExpression.hpp:154
std::hash< gtirb::SymAddrConst >::operator()
result_type operator()(const argument_type &Obj) const noexcept
Definition: SymbolicExpression.hpp:178
gtirb::SymAttribute::LO16
@ LO16
gtirb::SymAttribute::UPPER16
@ UPPER16
gtirb::SymAddrConst::operator!=
friend bool operator!=(const SymAddrConst &LHS, const SymAddrConst &RHS)
Definition: SymbolicExpression.hpp:139
gtirb::SymAttribute::SECREL
@ SECREL
gtirb::SymAddrConst::Attributes
SymAttributeSet Attributes
Definition: SymbolicExpression.hpp:132
gtirb::SymAddrConst
Represents a symbolic operand of the form "Sym + Offset".
Definition: SymbolicExpression.hpp:129
gtirb::SymAttribute::H
@ H
gtirb::SymAttribute::TOC
@ TOC
gtirb::SymAttribute::HIGHER
@ HIGHER
gtirb::SymAttribute::HIGH
@ HIGH
gtirb::SymAttribute::DTPMOD
@ DTPMOD
gtirb::SymAttribute::HIGHESTA
@ HIGHESTA
gtirb::SymAttribute::HIGHEST
@ HIGHEST
gtirb::SymAttribute::G2
@ G2
gtirb::SymAttribute::TLSLD
@ TLSLD
std
Definition: Addr.hpp:359
gtirb::SymAttribute::HI
@ HI
gtirb::SymAttribute::PG
@ PG
gtirb::SymAddrConst::Sym
Symbol * Sym
Symbol representing an address.
Definition: SymbolicExpression.hpp:131
gtirb::SymAttributeSet
std::set< SymAttribute > SymAttributeSet
Definition: SymbolicExpression.hpp:124
gtirb::SymAttribute::G1
@ G1
gtirb::SymAttribute::GOT
@ GOT
gtirb::SymAttribute::GOTREL
@ GOTREL
gtirb::SymAddrConst::operator==
friend bool operator==(const SymAddrConst &LHS, const SymAddrConst &RHS)
Definition: SymbolicExpression.hpp:134
gtirb::SymAttribute::DTPREL
@ DTPREL
Addr.hpp
Class gtirb::Addr and related functions.
std::hash< gtirb::SymAddrConst >::argument_type
gtirb::SymAddrConst argument_type
Definition: SymbolicExpression.hpp:175
gtirb::SymAttribute::G0
@ G0
gtirb::SymAttribute::TLSLDM
@ TLSLDM
gtirb::SymAttribute::PREL
@ PREL
gtirb::SymAttribute::DISP
@ DISP
gtirb::SymAttribute::TLS
@ TLS
gtirb::Symbol
Represents a Symbol, which maps a name to an object in the IR.
Definition: Symbol.hpp:43
gtirb::SymAttribute::HI16
@ HI16
gtirb::SymbolicExpression
std::variant< SymAddrConst, SymAddrAddr > SymbolicExpression
A symbolic expression.
Definition: SymbolicExpression.hpp:166