forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayout.cpp
More file actions
141 lines (118 loc) · 4.29 KB
/
Copy pathLayout.cpp
File metadata and controls
141 lines (118 loc) · 4.29 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright 2021 - 2025 R. Thomas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Layout.hpp"
#include "LIEF/ELF/Binary.hpp"
#include "LIEF/ELF/Symbol.hpp"
#include "LIEF/ELF/Section.hpp"
#include "internal_utils.hpp"
#include <LIEF/iostream.hpp>
namespace LIEF {
namespace ELF {
bool Layout::is_strtab_shared_shstrtab() const {
// Check if the .strtab is shared with the .shstrtab
const size_t shstrtab_idx = binary_->header().section_name_table_idx();
size_t strtab_idx = 0;
const Section* symtab = binary_->get(Section::TYPE::SYMTAB);
if (symtab == nullptr) {
return false;
}
strtab_idx = symtab->link();
bool is_shared = true;
const size_t nb_sections = binary_->sections().size();
is_shared = is_shared && strtab_idx > 0 && shstrtab_idx > 0;
is_shared = is_shared && strtab_idx < nb_sections && shstrtab_idx < nb_sections;
is_shared = is_shared && strtab_idx == shstrtab_idx;
return is_shared;
}
size_t Layout::section_strtab_size() {
// could be moved in the class base.
if (!raw_strtab_.empty()) {
return raw_strtab_.size();
}
if (is_strtab_shared_shstrtab()) {
// The content of .strtab is merged with .shstrtab
return 0;
}
vector_iostream raw_strtab(should_swap());
raw_strtab.write<uint8_t>(0);
size_t offset_counter = raw_strtab.tellp();
if (binary_->symtab_symbols_.empty()) {
return 0;
}
std::vector<std::string> symstr_opt = optimize(binary_->symtab_symbols_,
[] (const std::unique_ptr<Symbol>& sym) { return sym->name(); },
offset_counter, &strtab_name_map_);
for (const std::string& name : symstr_opt) {
raw_strtab.write(name);
}
raw_strtab.move(raw_strtab_);
return raw_strtab_.size();
}
size_t Layout::section_shstr_size() {
if (!raw_shstrtab_.empty()) {
// Already in the cache
return raw_shstrtab_.size();
}
vector_iostream raw_shstrtab(should_swap());
// In the ELF format all the .str sections
// start with a null entry.
raw_shstrtab.write<uint8_t>(0);
std::vector<std::string> sec_names;
sec_names.reserve(binary_->sections_.size());
std::transform(std::begin(binary_->sections_), std::end(binary_->sections_),
std::back_inserter(sec_names),
[] (const std::unique_ptr<Section>& s) {
return s->name();
});
if (!binary_->symtab_symbols_.empty()) {
if (binary_->get(Section::TYPE::SYMTAB) == nullptr) {
sec_names.emplace_back(".symtab");
}
if (binary_->get(Section::TYPE::SYMTAB) == nullptr) {
sec_names.emplace_back(".strtab");
}
}
for (const Note& note : binary_->notes()) {
const std::string& secname = note.section_name();
if (secname.empty()) {
continue;
}
if (const Section* sec = binary_->get_section(secname); sec == nullptr) {
sec_names.push_back(secname);
}
}
// First write section names
size_t offset_counter = raw_shstrtab.tellp();
std::vector<std::string> shstrtab_opt = optimize(sec_names, [] (const std::string& s) { return s; },
offset_counter, &shstr_name_map_);
for (const std::string& name : shstrtab_opt) {
raw_shstrtab.write(name);
}
// Check if the .shstrtab and the .strtab are shared (optimization used by clang)
// in this case, include the symtab symbol names
if (!binary_->symtab_symbols_.empty() && is_strtab_shared_shstrtab()) {
offset_counter = raw_shstrtab.tellp();
std::vector<std::string> symstr_opt = optimize(binary_->symtab_symbols_,
[] (const std::unique_ptr<Symbol>& sym) { return sym->name(); },
offset_counter, &shstr_name_map_);
for (const std::string& name : symstr_opt) {
raw_shstrtab.write(name);
}
}
raw_shstrtab.move(raw_shstrtab_);
return raw_shstrtab_.size();
}
}
}