LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
GnuHash.hpp
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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 <vector>
20#include <ostream>
21#include <cstdint>
22
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25
26namespace LIEF {
27namespace ELF {
28
29class Parser;
30class Builder;
31class Binary;
32
36class LIEF_API GnuHash : public Object {
37
38 friend class Parser;
39 friend class Builder;
40 friend class Binary;
41
42 public:
43 GnuHash() = default;
44 GnuHash(uint32_t symbol_idx, uint32_t shift2,
45 std::vector<uint64_t> bloom_filters, std::vector<uint32_t> buckets,
46 std::vector<uint32_t> hash_values = {});
47
48
49 GnuHash& operator=(const GnuHash& copy) = default;
50 GnuHash(const GnuHash& copy) = default;
51
52 GnuHash(GnuHash&&) = default;
53 GnuHash& operator=(GnuHash&&) = default;
54
55 ~GnuHash() override = default;
56
59 uint32_t nb_buckets() const {
60 return buckets_.size();
61 }
62
65 uint32_t symbol_index() const {
66 return symbol_index_;
67 }
68
70 uint32_t shift2() const {
71 return shift2_;
72 }
73
76 uint32_t maskwords() const {
77 return bloom_filters_.size();
78 }
79
81 const std::vector<uint64_t>& bloom_filters() const {
82 return bloom_filters_;
83 }
84
86 const std::vector<uint32_t>& buckets() const {
87 return buckets_;
88 }
89
91 const std::vector<uint32_t>& hash_values() const {
92 return hash_values_;
93 }
94
96 bool check_bloom_filter(uint32_t hash) const;
97
99 bool check_bucket(uint32_t hash) const {
100 return buckets_[hash % nb_buckets()] > 0;
101 }
102
107 bool check(const std::string& symbol_name) const;
108
113 bool check(uint32_t hash) const;
114
115
116 void accept(Visitor& visitor) const override;
117
118 LIEF_API friend std::ostream& operator<<(std::ostream& os, const GnuHash& gnuhash);
119
120 private:
121 uint32_t symbol_index_ = 0;
122 uint32_t shift2_ = 0;
123
124 std::vector<uint64_t> bloom_filters_;
125 std::vector<uint32_t> buckets_;
126 std::vector<uint32_t> hash_values_;
127
128 size_t c_ = 0;
129};
130
131
132} // namepsace ELF
133} // namespace LIEF
134
135#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:51
Class which provides a view over the GNU Hash implementation. Most of the fields are read-only since ...
Definition GnuHash.hpp:36
bool check_bucket(uint32_t hash) const
Check if the given hash passes the bucket filter.
Definition GnuHash.hpp:99
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 accessible with the hash table.
Definition GnuHash.hpp:65
const std::vector< uint32_t > & hash_values() const
Hash values.
Definition GnuHash.hpp:91
uint32_t maskwords() const
Number of bloom filters used. It must be a power of 2.
Definition GnuHash.hpp:76
bool check(const std::string &symbol_name) const
Check if the symbol probably exists. If the returned value is false you can assume at 100% that the s...
uint32_t nb_buckets() const
Return the number of buckets.
Definition GnuHash.hpp:59
const std::vector< uint64_t > & bloom_filters() const
Bloom filters.
Definition GnuHash.hpp:81
bool check_bloom_filter(uint32_t hash) const
Check if the given hash passes the bloom filter.
const std::vector< uint32_t > & buckets() const
Hash buckets.
Definition GnuHash.hpp:86
uint32_t shift2() const
Shift count used in the bloom filter.
Definition GnuHash.hpp:70
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Definition Object.hpp:25
Definition Visitor.hpp:219
LIEF namespace.
Definition Abstract/Binary.hpp:32