LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
GnuHash.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_ELF_GNU_HASH_H
17#define LIEF_ELF_GNU_HASH_H
18
19#include <string_view>
20#include <cstdint>
21#include <memory>
22#include <ostream>
23#include <vector>
24
25#include "LIEF/Object.hpp"
26#include "LIEF/errors.hpp"
27#include "LIEF/visibility.h"
28
29namespace LIEF {
30class SpanStream;
31namespace ELF {
32
33class Parser;
34class Builder;
35class Binary;
36class Segment;
37
41class LIEF_API GnuHash : public Object {
42
43 friend class Parser;
44 friend class Builder;
45 friend class Binary;
46
47 public:
48 GnuHash() = default;
49 GnuHash(uint32_t symbol_idx, uint32_t shift2,
50 std::vector<uint64_t> bloom_filters, std::vector<uint32_t> buckets,
51 std::vector<uint32_t> hash_values = {}) :
52 symbol_index_{symbol_idx},
53 shift2_{shift2},
54 bloom_filters_{std::move(bloom_filters)},
55 buckets_{std::move(buckets)},
56 hash_values_{std::move(hash_values)} {}
57
58 GnuHash& operator=(const GnuHash& copy) = default;
59 GnuHash(const GnuHash& copy) = default;
60
61 GnuHash(GnuHash&&) = default;
62 GnuHash& operator=(GnuHash&&) = default;
63
64 ~GnuHash() override = default;
65
68 uint32_t nb_buckets() const {
69 return buckets_.size();
70 }
71
74 uint32_t symbol_index() const {
75 return symbol_index_;
76 }
77
79 uint32_t shift2() const {
80 return shift2_;
81 }
82
85 uint32_t maskwords() const {
86 return bloom_filters_.size();
87 }
88
90 const std::vector<uint64_t>& bloom_filters() const LIEF_LIFETIMEBOUND {
91 return bloom_filters_;
92 }
93
95 const std::vector<uint32_t>& buckets() const LIEF_LIFETIMEBOUND {
96 return buckets_;
97 }
98
100 const std::vector<uint32_t>& hash_values() const LIEF_LIFETIMEBOUND {
101 return hash_values_;
102 }
103
105 bool check_bloom_filter(uint32_t hash) const;
106
108 bool check_bucket(uint32_t hash) const {
109 if (nb_buckets() == 0) {
110 return false;
111 }
112 return buckets_[hash % nb_buckets()] > 0;
113 }
114
119 bool check(std::string_view symbol_name) const;
120
125 bool check(uint32_t hash) const;
126
127
128 void accept(Visitor& visitor) const override;
129
130 LIEF_API friend std::ostream& operator<<(std::ostream& os,
131 const GnuHash& gnuhash);
132
133 template<class ELF_T>
134 static LIEF_LOCAL std::unique_ptr<GnuHash> parse(SpanStream& strm,
135 uint64_t dynsymcount);
136
137 template<class ELF_T>
139
140 LIEF_LOCAL uint64_t original_size() const {
141 return original_size_;
142 }
143
144 private:
145 uint32_t symbol_index_ = 0;
146 uint32_t shift2_ = 0;
147
148 std::vector<uint64_t> bloom_filters_;
149 std::vector<uint32_t> buckets_;
150 std::vector<uint32_t> hash_values_;
151
152 size_t c_ = 0;
153 uint64_t original_size_ = 0;
154};
155
156
157}
158}
159
160#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:61
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
bool check_bucket(uint32_t hash) const
Check if the given hash passes the bucket filter.
Definition GnuHash.hpp:108
static result< uint32_t > nb_symbols(SpanStream &strm)
GnuHash(uint32_t symbol_idx, uint32_t shift2, std::vector< uint64_t > bloom_filters, std::vector< uint32_t > buckets, std::vector< uint32_t > hash_values={})
Definition GnuHash.hpp:49
bool check(std::string_view symbol_name) const
Check if the symbol probably exists. If the returned value is false you can assume at 100% that the s...
GnuHash & operator=(const GnuHash &copy)=default
GnuHash & operator=(GnuHash &&)=default
static std::unique_ptr< GnuHash > parse(SpanStream &strm, uint64_t dynsymcount)
bool check(uint32_t hash) const
Check if the symbol associated with the given hash probably exists. If the returned value is false yo...
uint32_t symbol_index() const
Index of the first symbol in the dynamic symbols table which is accessible with the hash table.
Definition GnuHash.hpp:74
const std::vector< uint32_t > & hash_values() const
Hash values.
Definition GnuHash.hpp:100
friend class Builder
Definition GnuHash.hpp:44
void accept(Visitor &visitor) const override
uint32_t maskwords() const
Number of bloom filters used. It must be a power of 2.
Definition GnuHash.hpp:85
GnuHash(const GnuHash &copy)=default
~GnuHash() override=default
friend class Binary
Definition GnuHash.hpp:45
uint64_t original_size() const
Definition GnuHash.hpp:140
friend class Parser
Definition GnuHash.hpp:43
GnuHash(GnuHash &&)=default
uint32_t nb_buckets() const
Return the number of buckets.
Definition GnuHash.hpp:68
const std::vector< uint64_t > & bloom_filters() const
Bloom filters.
Definition GnuHash.hpp:90
bool check_bloom_filter(uint32_t hash) const
Check if the given hash passes the bloom filter.
friend std::ostream & operator<<(std::ostream &os, const GnuHash &gnuhash)
const std::vector< uint32_t > & buckets() const
Hash buckets.
Definition GnuHash.hpp:95
uint32_t shift2() const
Shift count used in the bloom filter.
Definition GnuHash.hpp:79
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:47
Class which represents the ELF segments.
Definition Segment.hpp:49
Definition SpanStream.hpp:32
Definition Visitor.hpp:212
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:42
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46