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
|
/*************************************************************************/
/* Copyright (c) 2015 dx, http://kaimi.ru */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following conditions: */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#pragma once
#include <memory>
#include <istream>
#include "pe_base.h"
#include "pe_directory.h"
namespace pe_bliss
{
//Class representing TLS info
//We use "DWORD" type to represent RVAs, because RVA is
//always 32bit even in PE+
class tls_info
{
public:
typedef std::vector<uint32_t> tls_callback_list;
public:
//Default constructor
tls_info();
//Returns start RVA of TLS raw data
uint32_t get_raw_data_start_rva() const;
//Returns end RVA of TLS raw data
uint32_t get_raw_data_end_rva() const;
//Returns TLS index RVA
uint32_t get_index_rva() const;
//Returns TLS callbacks RVA
uint32_t get_callbacks_rva() const;
//Returns size of zero fill
uint32_t get_size_of_zero_fill() const;
//Returns characteristics
uint32_t get_characteristics() const;
//Returns raw TLS data
const std::string& get_raw_data() const;
//Returns TLS callbacks addresses
const tls_callback_list& get_tls_callbacks() const;
public: //These functions do not change everything inside image, they are used by PE class
//You can also use them to rebuild TLS directory
//Sets start RVA of TLS raw data
void set_raw_data_start_rva(uint32_t rva);
//Sets end RVA of TLS raw data
void set_raw_data_end_rva(uint32_t rva);
//Sets TLS index RVA
void set_index_rva(uint32_t rva);
//Sets TLS callbacks RVA
void set_callbacks_rva(uint32_t rva);
//Sets size of zero fill
void set_size_of_zero_fill(uint32_t size);
//Sets characteristics
void set_characteristics(uint32_t characteristics);
//Sets raw TLS data
void set_raw_data(const std::string& data);
//Returns TLS callbacks addresses
tls_callback_list& get_tls_callbacks();
//Adds TLS callback
void add_tls_callback(uint32_t rva);
//Clears TLS callbacks list
void clear_tls_callbacks();
//Recalculates end address of raw TLS data
void recalc_raw_data_end_rva();
private:
uint32_t start_rva_, end_rva_, index_rva_, callbacks_rva_;
uint32_t size_of_zero_fill_, characteristics_;
//Raw TLS data
std::string raw_data_;
//TLS callback RVAs
tls_callback_list callbacks_;
};
//Represents type of expanding of TLS section containing raw data
//(Works only if you are writing TLS raw data to tls_section and it is the last one in the PE image on the moment of TLS rebuild)
enum tls_data_expand_type
{
tls_data_expand_raw, //If there is not enough raw space for raw TLS data, it can be expanded
tls_data_expand_virtual //If there is not enough virtual place for raw TLS data, it can be expanded
};
//Get TLS info
//If image does not have TLS, throws an exception
const tls_info get_tls_info(const pe_base& pe);
template<typename PEClassType>
const tls_info get_tls_info_base(const pe_base& pe);
//Rebuilder of TLS structures
//If write_tls_callbacks = true, TLS callbacks VAs will be written to their place
//If write_tls_data = true, TLS data will be written to its place
//If you have chosen to rewrite raw data, only (EndAddressOfRawData - StartAddressOfRawData) bytes will be written, not the full length of string
//representing raw data content
//auto_strip_last_section - if true and TLS are placed in the last section, it will be automatically stripped
const image_directory rebuild_tls(pe_base& pe, const tls_info& info, section& tls_section, uint32_t offset_from_section_start = 0, bool write_tls_callbacks = true, bool write_tls_data = true, tls_data_expand_type expand = tls_data_expand_raw, bool save_to_pe_header = true, bool auto_strip_last_section = true);
template<typename PEClassType>
const image_directory rebuild_tls_base(pe_base& pe, const tls_info& info, section& tls_section, uint32_t offset_from_section_start = 0, bool write_tls_callbacks = true, bool write_tls_data = true, tls_data_expand_type expand = tls_data_expand_raw, bool save_to_pe_header = true, bool auto_strip_last_section = true);
}
|