blob: a12ed70e8f49483ab31438257f062525af25fd29 [file] [log] [blame]
Mike Wittmanf1ff2df2020-07-28 19:58:071// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/debug/test_elf_image_builder.h"
6
7#include <cstring>
8#include <type_traits>
9#include <utility>
10
11#include "base/bits.h"
12#include "base/check.h"
Mike Wittman6a7b5172020-08-04 18:35:4413#include "base/notreached.h"
Mike Wittmanf1ff2df2020-07-28 19:58:0714#include "build/build_config.h"
15
16#if __SIZEOF_POINTER__ == 4
17using Dyn = Elf32_Dyn;
18using Nhdr = Elf32_Nhdr;
19using Shdr = Elf32_Shdr;
20#else
21using Dyn = Elf64_Dyn;
22using Nhdr = Elf64_Nhdr;
23using Shdr = Elf64_Shdr;
24#endif
25
26namespace base {
27
28namespace {
29// Sizes/alignments to use in the ELF image.
30static constexpr size_t kPageSize = 4096;
31static constexpr size_t kPhdrAlign = 0x4;
32static constexpr size_t kNoteAlign = 0x4;
33static constexpr size_t kLoadAlign = 0x1000;
34static constexpr size_t kDynamicAlign = 0x4;
35} // namespace
36
37struct TestElfImageBuilder::LoadSegment {
38 Word flags;
39 Word size;
40};
41
42TestElfImage::TestElfImage(std::vector<uint8_t> buffer, const void* elf_start)
43 : buffer_(std::move(buffer)), elf_start_(elf_start) {}
44
45TestElfImage::~TestElfImage() = default;
46
47TestElfImage::TestElfImage(TestElfImage&&) = default;
48
49TestElfImage& TestElfImage::operator=(TestElfImage&&) = default;
50
Mike Wittman6a7b5172020-08-04 18:35:4451TestElfImageBuilder::TestElfImageBuilder(MappingType mapping_type)
52 : mapping_type_(mapping_type) {}
Mike Wittmanf1ff2df2020-07-28 19:58:0753
54TestElfImageBuilder::~TestElfImageBuilder() = default;
55
56TestElfImageBuilder& TestElfImageBuilder::AddLoadSegment(Word flags,
57 size_t size) {
58 load_segments_.push_back({flags, size});
59 return *this;
60}
61
62TestElfImageBuilder& TestElfImageBuilder::AddNoteSegment(
63 Word type,
64 StringPiece name,
65 span<const uint8_t> desc) {
66 const size_t name_with_null_size = name.size() + 1;
67 std::vector<uint8_t> buffer(sizeof(Nhdr) +
68 bits::Align(name_with_null_size, 4) +
69 bits::Align(desc.size(), 4),
70 '\0');
71 uint8_t* loc = &buffer.front();
72 Nhdr* nhdr = reinterpret_cast<Nhdr*>(loc);
73 nhdr->n_namesz = name_with_null_size;
74 nhdr->n_descsz = desc.size();
75 nhdr->n_type = type;
76 loc += sizeof(Nhdr);
77
78 memcpy(loc, name.data(), name.size());
79 *(loc + name.size()) = '\0';
80 loc += bits::Align(name_with_null_size, 4);
81
82 memcpy(loc, &desc.front(), desc.size());
83 loc += bits::Align(desc.size(), 4);
84
85 DCHECK_EQ(&buffer.front() + buffer.size(), loc);
86
87 note_contents_.push_back(std::move(buffer));
88
89 return *this;
90}
91
92TestElfImageBuilder& TestElfImageBuilder::AddSoName(StringPiece soname) {
93 DCHECK(!soname_.has_value());
94 soname_.emplace(soname);
95 return *this;
96}
97
98struct TestElfImageBuilder::ImageMeasures {
99 size_t phdrs_required;
100 size_t note_start;
101 size_t note_size;
102 std::vector<size_t> load_segment_start;
103 size_t dynamic_start;
104 size_t strtab_start;
105 size_t total_size;
106};
107
Mike Wittman6a7b5172020-08-04 18:35:44108Addr TestElfImageBuilder::GetVirtualAddressForOffset(Off offset) const {
109 switch (mapping_type_) {
110 case RELOCATABLE:
111 return static_cast<Addr>(offset);
112
113 case RELOCATABLE_WITH_BIAS:
114 return static_cast<Addr>(offset + kLoadBias);
115 }
116}
117
Mike Wittmanf1ff2df2020-07-28 19:58:07118TestElfImageBuilder::ImageMeasures TestElfImageBuilder::MeasureSizesAndOffsets()
119 const {
120 ImageMeasures measures;
121
122 measures.phdrs_required = 1 + load_segments_.size();
123 if (!note_contents_.empty())
124 ++measures.phdrs_required;
125 if (soname_.has_value())
126 ++measures.phdrs_required;
127
128 // The current offset into the image, where the next bytes are to be written.
129 // Starts after the ELF header.
130 size_t offset = sizeof(Ehdr);
131
132 // Add space for the program header table.
133 offset = bits::Align(offset, kPhdrAlign);
134 offset += sizeof(Phdr) * measures.phdrs_required;
135
136 // Add space for the notes.
137 measures.note_start = offset;
138 if (!note_contents_.empty())
139 offset = bits::Align(offset, kNoteAlign);
140 for (const std::vector<uint8_t>& contents : note_contents_)
141 offset += contents.size();
142 measures.note_size = offset - measures.note_start;
143
144 // Add space for the load segments.
145 for (auto it = load_segments_.begin(); it != load_segments_.end(); ++it) {
146 size_t size = 0;
147 // The first non PT_PHDR program header is expected to be a PT_LOAD and
148 // start at the already-aligned start of the ELF header.
149 if (it == load_segments_.begin()) {
150 size = offset + it->size;
151 measures.load_segment_start.push_back(0);
152 } else {
153 offset = bits::Align(offset, kLoadAlign);
154 size = it->size;
155 measures.load_segment_start.push_back(offset);
156 }
157 offset += it->size;
158 }
159
160 // Add space for the dynamic segment.
161 measures.dynamic_start = bits::Align(offset, kDynamicAlign);
162 offset += sizeof(Dyn) * (soname_ ? 2 : 1);
163 measures.strtab_start = offset;
164
165 // Add space for the string table.
166 ++offset; // The first string table byte holds a null character.
167 if (soname_)
168 offset += soname_->size() + 1;
169
170 measures.total_size = offset;
171
172 return measures;
173}
174
175TestElfImage TestElfImageBuilder::Build() {
176 ImageMeasures measures = MeasureSizesAndOffsets();
177
Mike Wittman6a7b5172020-08-04 18:35:44178 // Write the ELF contents into |buffer|. Extends the buffer back to the 0
179 // address in the case of load bias, so that the memory between the 0 address
180 // and the image start is zero-initialized.
181 const size_t load_bias =
182 mapping_type_ == RELOCATABLE_WITH_BIAS ? kLoadBias : 0;
183 std::vector<uint8_t> buffer(load_bias + (kPageSize - 1) + measures.total_size,
184 '\0');
185 uint8_t* const elf_start =
186 bits::Align(&buffer.front() + load_bias, kPageSize);
Mike Wittmanf1ff2df2020-07-28 19:58:07187 uint8_t* loc = elf_start;
188
189 // Add the ELF header.
190 loc = AppendHdr(CreateEhdr(measures.phdrs_required), loc);
191
192 // Add the program header table.
193 loc = bits::Align(loc, kPhdrAlign);
Mike Wittman6a7b5172020-08-04 18:35:44194 loc = AppendHdr(CreatePhdr(PT_PHDR, PF_R, kPhdrAlign,
195 GetVirtualAddressForOffset(loc - elf_start),
Mike Wittmanf1ff2df2020-07-28 19:58:07196 sizeof(Phdr) * measures.phdrs_required),
197 loc);
198 for (size_t i = 0; i < load_segments_.size(); ++i) {
199 const LoadSegment& load_segment = load_segments_[i];
200 size_t size = load_segment.size;
201 // The first non PT_PHDR program header is expected to be a PT_LOAD and
202 // encompass all the preceding headers.
203 if (i == 0)
204 size += loc - elf_start;
Mike Wittman6a7b5172020-08-04 18:35:44205 loc = AppendHdr(
206 CreatePhdr(PT_LOAD, load_segment.flags, kLoadAlign,
207 GetVirtualAddressForOffset(measures.load_segment_start[i]),
208 size),
209 loc);
Mike Wittmanf1ff2df2020-07-28 19:58:07210 }
211 if (measures.note_size != 0) {
Mike Wittman6a7b5172020-08-04 18:35:44212 loc = AppendHdr(CreatePhdr(PT_NOTE, PF_R, kNoteAlign,
213 GetVirtualAddressForOffset(measures.note_start),
Mike Wittmanf1ff2df2020-07-28 19:58:07214 measures.note_size),
215 loc);
216 }
217 if (soname_) {
Mike Wittman6a7b5172020-08-04 18:35:44218 loc =
219 AppendHdr(CreatePhdr(PT_DYNAMIC, PF_R | PF_W, kDynamicAlign,
220 GetVirtualAddressForOffset(measures.dynamic_start),
221 sizeof(Dyn) * 2),
222 loc);
Mike Wittmanf1ff2df2020-07-28 19:58:07223 }
224
225 // Add the notes.
226 loc = bits::Align(loc, kNoteAlign);
227 for (const std::vector<uint8_t>& contents : note_contents_) {
228 memcpy(loc, &contents.front(), contents.size());
229 loc += contents.size();
230 }
231
232 // Add the load segments.
233 for (auto it = load_segments_.begin(); it != load_segments_.end(); ++it) {
234 if (it != load_segments_.begin())
235 loc = bits::Align(loc, kLoadAlign);
236 memset(loc, 0, it->size);
237 loc += it->size;
238 }
239
240 loc = bits::Align(loc, kDynamicAlign);
241
242 // Add the soname state.
243 if (soname_) {
244 // Add a DYNAMIC section for the soname.
245 Dyn* soname_dyn = reinterpret_cast<Dyn*>(loc);
246 soname_dyn->d_tag = DT_SONAME;
247 soname_dyn->d_un.d_val = 1; // One char into the string table.
248 loc += sizeof(Dyn);
249 }
250
251 Dyn* strtab_dyn = reinterpret_cast<Dyn*>(loc);
252 strtab_dyn->d_tag = DT_STRTAB;
253#if defined(OS_FUCHSIA) || defined(OS_ANDROID)
Mike Wittman6a7b5172020-08-04 18:35:44254 // Fuchsia and Android do not alter the symtab pointer on ELF load -- it's
255 // expected to remain a 'virutal address'.
256 strtab_dyn->d_un.d_ptr = GetVirtualAddressForOffset(measures.strtab_start);
Mike Wittmanf1ff2df2020-07-28 19:58:07257#else
Mike Wittman6a7b5172020-08-04 18:35:44258 // Linux relocates this value on ELF load, so produce the pointer value after
259 // relocation. That value will always be equal to the actual memory address.
Mike Wittmanf1ff2df2020-07-28 19:58:07260 strtab_dyn->d_un.d_ptr =
261 reinterpret_cast<uintptr_t>(elf_start + measures.strtab_start);
262#endif
263 loc += sizeof(Dyn);
264
265 // Add a string table with one entry for the soname, if necessary.
266 *loc++ = '\0'; // The first byte holds a null character.
267 if (soname_) {
268 memcpy(loc, soname_->data(), soname_->size());
269 *(loc + soname_->size()) = '\0';
270 loc += soname_->size() + 1;
271 }
272
273 // The offset past the end of the contents should be consistent with the size
274 // mmeasurement above.
275 DCHECK_EQ(loc, elf_start + measures.total_size);
276
277 return TestElfImage(std::move(buffer), elf_start);
278}
279
280// static
281template <typename T>
282uint8_t* TestElfImageBuilder::AppendHdr(const T& hdr, uint8_t* loc) {
283 static_assert(std::is_trivially_copyable<T>::value,
284 "T should be a plain struct");
285 memcpy(loc, &hdr, sizeof(T));
286 return loc + sizeof(T);
287}
288
289Ehdr TestElfImageBuilder::CreateEhdr(Half phnum) {
290 Ehdr ehdr;
291 ehdr.e_ident[EI_MAG0] = ELFMAG0;
292 ehdr.e_ident[EI_MAG1] = ELFMAG1;
293 ehdr.e_ident[EI_MAG2] = ELFMAG2;
294 ehdr.e_ident[EI_MAG3] = ELFMAG3;
295 ehdr.e_ident[EI_CLASS] = __SIZEOF_POINTER__ == 4 ? 1 : 2;
296 ehdr.e_ident[EI_DATA] = 1; // Little endian.
297 ehdr.e_ident[EI_VERSION] = 1;
298 ehdr.e_ident[EI_OSABI] = 0x00;
299 ehdr.e_ident[EI_ABIVERSION] = 0;
300 ehdr.e_ident[EI_PAD] = 0;
301 ehdr.e_type = ET_DYN;
302 ehdr.e_machine = 0x28; // ARM.
303 ehdr.e_version = 1;
304 ehdr.e_entry = 0;
305 ehdr.e_phoff = sizeof(Ehdr);
306 ehdr.e_shoff = 0;
307 ehdr.e_flags = 0;
308 ehdr.e_ehsize = sizeof(Ehdr);
309 ehdr.e_phentsize = sizeof(Phdr);
310 ehdr.e_phnum = phnum;
311 ehdr.e_shentsize = sizeof(Shdr);
312 ehdr.e_shnum = 0;
313 ehdr.e_shstrndx = 0;
314
315 return ehdr;
316}
317
318Phdr TestElfImageBuilder::CreatePhdr(Word type,
319 Word flags,
320 size_t align,
321 Off offset,
322 size_t size) {
323 Phdr phdr;
324 phdr.p_type = type;
325 phdr.p_flags = flags;
326 phdr.p_offset = offset;
327 phdr.p_filesz = size;
328 phdr.p_vaddr = phdr.p_offset;
329 phdr.p_paddr = 0;
330 phdr.p_memsz = phdr.p_filesz;
331 phdr.p_align = align;
332
333 return phdr;
334}
335
336} // namespace base