blob: bb5f7e0f9de2dd2ee6029c91f1319747c4a65d94 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
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
17#include "elf_writer_quick.h"
18
19#include "base/logging.h"
20#include "base/unix_file/fd_file.h"
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070021#include "buffered_output_stream.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "driver/compiler_driver.h"
Alex Light3470ab42014-06-18 10:35:45 -070023#include "dwarf.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000024#include "elf_utils.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070025#include "file_output_stream.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "globals.h"
Andreas Gampe79273802014-08-05 20:21:05 -070027#include "leb128.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "oat.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070029#include "oat_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "utils.h"
31
32namespace art {
33
Brian Carlstromb12f3472014-06-11 14:54:46 -070034static constexpr Elf32_Word NextOffset(const Elf32_Shdr& cur, const Elf32_Shdr& prev) {
35 return RoundUp(prev.sh_size + prev.sh_offset, cur.sh_addralign);
Brian Carlstrom7940e442013-07-12 13:46:57 -070036}
37
Brian Carlstromb12f3472014-06-11 14:54:46 -070038static uint8_t MakeStInfo(uint8_t binding, uint8_t type) {
39 return ((binding) << 4) + ((type) & 0xf);
40}
41
Andreas Gampe79273802014-08-05 20:21:05 -070042static void UpdateWord(std::vector<uint8_t>* buf, int offset, int data) {
43 (*buf)[offset+0] = data;
44 (*buf)[offset+1] = data >> 8;
45 (*buf)[offset+2] = data >> 16;
46 (*buf)[offset+3] = data >> 24;
47}
48
Andreas Gampe79273802014-08-05 20:21:05 -070049static void PushHalf(std::vector<uint8_t>* buf, int data) {
50 buf->push_back(data & 0xff);
51 buf->push_back((data >> 8) & 0xff);
52}
53
Brian Carlstromb12f3472014-06-11 14:54:46 -070054bool ElfWriterQuick::ElfBuilder::Write() {
55 // The basic layout of the elf file. Order may be different in final output.
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 // +-------------------------+
57 // | Elf32_Ehdr |
58 // +-------------------------+
59 // | Elf32_Phdr PHDR |
60 // | Elf32_Phdr LOAD R | .dynsym .dynstr .hash .rodata
61 // | Elf32_Phdr LOAD R X | .text
62 // | Elf32_Phdr LOAD RW | .dynamic
63 // | Elf32_Phdr DYNAMIC | .dynamic
64 // +-------------------------+
65 // | .dynsym |
66 // | Elf32_Sym STN_UNDEF |
67 // | Elf32_Sym oatdata |
68 // | Elf32_Sym oatexec |
69 // | Elf32_Sym oatlastword |
70 // +-------------------------+
71 // | .dynstr |
72 // | \0 |
73 // | oatdata\0 |
74 // | oatexec\0 |
75 // | oatlastword\0 |
76 // | boot.oat\0 |
77 // +-------------------------+
78 // | .hash |
Brian Carlstromb12f3472014-06-11 14:54:46 -070079 // | Elf32_Word nbucket = b |
80 // | Elf32_Word nchain = c |
81 // | Elf32_Word bucket[0] |
82 // | ... |
83 // | Elf32_Word bucket[b - 1]|
84 // | Elf32_Word chain[0] |
85 // | ... |
86 // | Elf32_Word chain[c - 1] |
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 // +-------------------------+
88 // | .rodata |
89 // | oatdata..oatexec-4 |
90 // +-------------------------+
91 // | .text |
92 // | oatexec..oatlastword |
93 // +-------------------------+
94 // | .dynamic |
95 // | Elf32_Dyn DT_SONAME |
96 // | Elf32_Dyn DT_HASH |
97 // | Elf32_Dyn DT_SYMTAB |
98 // | Elf32_Dyn DT_SYMENT |
99 // | Elf32_Dyn DT_STRTAB |
100 // | Elf32_Dyn DT_STRSZ |
101 // | Elf32_Dyn DT_NULL |
Brian Carlstromb12f3472014-06-11 14:54:46 -0700102 // +-------------------------+ (Optional)
103 // | .strtab | (Optional)
104 // | program symbol names | (Optional)
105 // +-------------------------+ (Optional)
106 // | .symtab | (Optional)
107 // | program symbols | (Optional)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 // +-------------------------+
109 // | .shstrtab |
110 // | \0 |
111 // | .dynamic\0 |
112 // | .dynsym\0 |
113 // | .dynstr\0 |
114 // | .hash\0 |
115 // | .rodata\0 |
116 // | .text\0 |
117 // | .shstrtab\0 |
Brian Carlstromb12f3472014-06-11 14:54:46 -0700118 // | .symtab\0 | (Optional)
119 // | .strtab\0 | (Optional)
120 // | .debug_str\0 | (Optional)
121 // | .debug_info\0 | (Optional)
Tong Shen35e1e6a2014-07-30 09:31:22 -0700122 // | .eh_frame\0 | (Optional)
Brian Carlstromb12f3472014-06-11 14:54:46 -0700123 // | .debug_abbrev\0 | (Optional)
124 // +-------------------------+ (Optional)
125 // | .debug_str | (Optional)
126 // +-------------------------+ (Optional)
127 // | .debug_info | (Optional)
128 // +-------------------------+ (Optional)
Tong Shen35e1e6a2014-07-30 09:31:22 -0700129 // | .eh_frame | (Optional)
Brian Carlstromb12f3472014-06-11 14:54:46 -0700130 // +-------------------------+ (Optional)
131 // | .debug_abbrev | (Optional)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 // +-------------------------+
133 // | Elf32_Shdr NULL |
134 // | Elf32_Shdr .dynsym |
135 // | Elf32_Shdr .dynstr |
136 // | Elf32_Shdr .hash |
137 // | Elf32_Shdr .text |
138 // | Elf32_Shdr .rodata |
139 // | Elf32_Shdr .dynamic |
140 // | Elf32_Shdr .shstrtab |
Brian Carlstromb12f3472014-06-11 14:54:46 -0700141 // | Elf32_Shdr .debug_str | (Optional)
Mark Mendellae9fd932014-02-10 16:14:35 -0800142 // | Elf32_Shdr .debug_info | (Optional)
Tong Shen35e1e6a2014-07-30 09:31:22 -0700143 // | Elf32_Shdr .eh_frame | (Optional)
Brian Carlstromb12f3472014-06-11 14:54:46 -0700144 // | Elf32_Shdr .debug_abbrev| (Optional)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 // +-------------------------+
146
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147
Brian Carlstromb12f3472014-06-11 14:54:46 -0700148 if (fatal_error_) {
149 return false;
150 }
151 // Step 1. Figure out all the offsets.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152
Brian Carlstromb12f3472014-06-11 14:54:46 -0700153 // What phdr is.
154 uint32_t phdr_offset = sizeof(Elf32_Ehdr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 const uint8_t PH_PHDR = 0;
156 const uint8_t PH_LOAD_R__ = 1;
157 const uint8_t PH_LOAD_R_X = 2;
158 const uint8_t PH_LOAD_RW_ = 3;
159 const uint8_t PH_DYNAMIC = 4;
160 const uint8_t PH_NUM = 5;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000161 uint32_t phdr_size = sizeof(Elf32_Phdr) * PH_NUM;
Brian Carlstromb12f3472014-06-11 14:54:46 -0700162 if (debug_logging_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 LOG(INFO) << "phdr_offset=" << phdr_offset << std::hex << " " << phdr_offset;
164 LOG(INFO) << "phdr_size=" << phdr_size << std::hex << " " << phdr_size;
165 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700166 Elf32_Phdr program_headers[PH_NUM];
167 memset(&program_headers, 0, sizeof(program_headers));
168 program_headers[PH_PHDR].p_type = PT_PHDR;
169 program_headers[PH_PHDR].p_offset = phdr_offset;
170 program_headers[PH_PHDR].p_vaddr = phdr_offset;
171 program_headers[PH_PHDR].p_paddr = phdr_offset;
172 program_headers[PH_PHDR].p_filesz = sizeof(program_headers);
173 program_headers[PH_PHDR].p_memsz = sizeof(program_headers);
174 program_headers[PH_PHDR].p_flags = PF_R;
175 program_headers[PH_PHDR].p_align = sizeof(Elf32_Word);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176
Brian Carlstromb12f3472014-06-11 14:54:46 -0700177 program_headers[PH_LOAD_R__].p_type = PT_LOAD;
178 program_headers[PH_LOAD_R__].p_offset = 0;
179 program_headers[PH_LOAD_R__].p_vaddr = 0;
180 program_headers[PH_LOAD_R__].p_paddr = 0;
181 program_headers[PH_LOAD_R__].p_flags = PF_R;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
Brian Carlstromb12f3472014-06-11 14:54:46 -0700183 program_headers[PH_LOAD_R_X].p_type = PT_LOAD;
184 program_headers[PH_LOAD_R_X].p_flags = PF_R | PF_X;
185
186 program_headers[PH_LOAD_RW_].p_type = PT_LOAD;
187 program_headers[PH_LOAD_RW_].p_flags = PF_R | PF_W;
188
189 program_headers[PH_DYNAMIC].p_type = PT_DYNAMIC;
190 program_headers[PH_DYNAMIC].p_flags = PF_R | PF_W;
191
192 // Get the dynstr string.
193 std::string dynstr(dynsym_builder_.GenerateStrtab());
194
195 // Add the SONAME to the dynstr.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 uint32_t dynstr_soname_offset = dynstr.size();
197 std::string file_name(elf_file_->GetPath());
198 size_t directory_separator_pos = file_name.rfind('/');
199 if (directory_separator_pos != std::string::npos) {
200 file_name = file_name.substr(directory_separator_pos + 1);
201 }
202 dynstr += file_name;
203 dynstr += '\0';
Brian Carlstromb12f3472014-06-11 14:54:46 -0700204 if (debug_logging_) {
205 LOG(INFO) << "dynstr size (bytes) =" << dynstr.size()
206 << std::hex << " " << dynstr.size();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700207 LOG(INFO) << "dynsym size (elements)=" << dynsym_builder_.GetSize()
208 << std::hex << " " << dynsym_builder_.GetSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 }
210
Brian Carlstromb12f3472014-06-11 14:54:46 -0700211 // get the strtab
212 std::string strtab;
213 if (IncludingDebugSymbols()) {
214 strtab = symtab_builder_.GenerateStrtab();
215 if (debug_logging_) {
216 LOG(INFO) << "strtab size (bytes) =" << strtab.size()
217 << std::hex << " " << strtab.size();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700218 LOG(INFO) << "symtab size (elements) =" << symtab_builder_.GetSize()
219 << std::hex << " " << symtab_builder_.GetSize();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700220 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 }
222
Brian Carlstromb12f3472014-06-11 14:54:46 -0700223 // Get the section header string table.
224 std::vector<Elf32_Shdr*> section_ptrs;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 std::string shstrtab;
226 shstrtab += '\0';
Brian Carlstromb12f3472014-06-11 14:54:46 -0700227
228 // Setup sym_undef
229 Elf32_Shdr null_hdr;
230 memset(&null_hdr, 0, sizeof(null_hdr));
231 null_hdr.sh_type = SHT_NULL;
232 null_hdr.sh_link = SHN_UNDEF;
233 section_ptrs.push_back(&null_hdr);
234
235 uint32_t section_index = 1;
236
237 // setup .dynsym
238 section_ptrs.push_back(&dynsym_builder_.section_);
239 AssignSectionStr(&dynsym_builder_, &shstrtab);
240 dynsym_builder_.section_index_ = section_index++;
241
242 // Setup .dynstr
243 section_ptrs.push_back(&dynsym_builder_.strtab_.section_);
244 AssignSectionStr(&dynsym_builder_.strtab_, &shstrtab);
245 dynsym_builder_.strtab_.section_index_ = section_index++;
246
247 // Setup .hash
248 section_ptrs.push_back(&hash_builder_.section_);
249 AssignSectionStr(&hash_builder_, &shstrtab);
250 hash_builder_.section_index_ = section_index++;
251
252 // Setup .rodata
253 section_ptrs.push_back(&rodata_builder_.section_);
254 AssignSectionStr(&rodata_builder_, &shstrtab);
255 rodata_builder_.section_index_ = section_index++;
256
257 // Setup .text
258 section_ptrs.push_back(&text_builder_.section_);
259 AssignSectionStr(&text_builder_, &shstrtab);
260 text_builder_.section_index_ = section_index++;
261
262 // Setup .dynamic
263 section_ptrs.push_back(&dynamic_builder_.section_);
264 AssignSectionStr(&dynamic_builder_, &shstrtab);
265 dynamic_builder_.section_index_ = section_index++;
266
267 if (IncludingDebugSymbols()) {
268 // Setup .symtab
269 section_ptrs.push_back(&symtab_builder_.section_);
270 AssignSectionStr(&symtab_builder_, &shstrtab);
271 symtab_builder_.section_index_ = section_index++;
272
273 // Setup .strtab
274 section_ptrs.push_back(&symtab_builder_.strtab_.section_);
275 AssignSectionStr(&symtab_builder_.strtab_, &shstrtab);
276 symtab_builder_.strtab_.section_index_ = section_index++;
277 }
278 ElfRawSectionBuilder* it = other_builders_.data();
279 for (uint32_t cnt = 0; cnt < other_builders_.size(); ++it, ++cnt) {
280 // Setup all the other sections.
281 section_ptrs.push_back(&it->section_);
282 AssignSectionStr(it, &shstrtab);
283 it->section_index_ = section_index++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 }
285
Brian Carlstromb12f3472014-06-11 14:54:46 -0700286 // Setup shstrtab
287 section_ptrs.push_back(&shstrtab_builder_.section_);
288 AssignSectionStr(&shstrtab_builder_, &shstrtab);
289 shstrtab_builder_.section_index_ = section_index++;
290
291 if (debug_logging_) {
292 LOG(INFO) << ".shstrtab size (bytes) =" << shstrtab.size()
293 << std::hex << " " << shstrtab.size();
294 LOG(INFO) << "section list size (elements)=" << section_ptrs.size()
295 << std::hex << " " << section_ptrs.size();
Mark Mendellae9fd932014-02-10 16:14:35 -0800296 }
297
Brian Carlstromb12f3472014-06-11 14:54:46 -0700298 // Fill in the hash section.
299 std::vector<Elf32_Word> hash = dynsym_builder_.GenerateHashContents();
300
301 if (debug_logging_) {
302 LOG(INFO) << ".hash size (bytes)=" << hash.size() * sizeof(Elf32_Word)
303 << std::hex << " " << hash.size() * sizeof(Elf32_Word);
Mark Mendellae9fd932014-02-10 16:14:35 -0800304 }
305
Brian Carlstromb12f3472014-06-11 14:54:46 -0700306 Elf32_Word base_offset = sizeof(Elf32_Ehdr) + sizeof(program_headers);
307 std::vector<ElfFilePiece> pieces;
308
309 // Get the layout in the sections.
310 //
311 // Get the layout of the dynsym section.
312 dynsym_builder_.section_.sh_offset = RoundUp(base_offset, dynsym_builder_.section_.sh_addralign);
313 dynsym_builder_.section_.sh_addr = dynsym_builder_.section_.sh_offset;
Brian Carlstrom8758c642014-06-11 14:22:02 -0700314 dynsym_builder_.section_.sh_size = dynsym_builder_.GetSize() * sizeof(Elf32_Sym);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700315 dynsym_builder_.section_.sh_link = dynsym_builder_.GetLink();
316
317 // Get the layout of the dynstr section.
318 dynsym_builder_.strtab_.section_.sh_offset = NextOffset(dynsym_builder_.strtab_.section_,
319 dynsym_builder_.section_);
320 dynsym_builder_.strtab_.section_.sh_addr = dynsym_builder_.strtab_.section_.sh_offset;
321 dynsym_builder_.strtab_.section_.sh_size = dynstr.size();
322 dynsym_builder_.strtab_.section_.sh_link = dynsym_builder_.strtab_.GetLink();
323
324 // Get the layout of the hash section
325 hash_builder_.section_.sh_offset = NextOffset(hash_builder_.section_,
326 dynsym_builder_.strtab_.section_);
327 hash_builder_.section_.sh_addr = hash_builder_.section_.sh_offset;
328 hash_builder_.section_.sh_size = hash.size() * sizeof(Elf32_Word);
329 hash_builder_.section_.sh_link = hash_builder_.GetLink();
330
331 // Get the layout of the rodata section.
332 rodata_builder_.section_.sh_offset = NextOffset(rodata_builder_.section_,
333 hash_builder_.section_);
334 rodata_builder_.section_.sh_addr = rodata_builder_.section_.sh_offset;
335 rodata_builder_.section_.sh_size = rodata_builder_.size_;
336 rodata_builder_.section_.sh_link = rodata_builder_.GetLink();
337
338 // Get the layout of the text section.
339 text_builder_.section_.sh_offset = NextOffset(text_builder_.section_, rodata_builder_.section_);
340 text_builder_.section_.sh_addr = text_builder_.section_.sh_offset;
341 text_builder_.section_.sh_size = text_builder_.size_;
342 text_builder_.section_.sh_link = text_builder_.GetLink();
343 CHECK_ALIGNED(rodata_builder_.section_.sh_offset + rodata_builder_.section_.sh_size, kPageSize);
344
345 // Get the layout of the dynamic section.
346 dynamic_builder_.section_.sh_offset = NextOffset(dynamic_builder_.section_,
347 text_builder_.section_);
348 dynamic_builder_.section_.sh_addr = dynamic_builder_.section_.sh_offset;
Brian Carlstrom8758c642014-06-11 14:22:02 -0700349 dynamic_builder_.section_.sh_size = dynamic_builder_.GetSize() * sizeof(Elf32_Dyn);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700350 dynamic_builder_.section_.sh_link = dynamic_builder_.GetLink();
351
352 Elf32_Shdr prev = dynamic_builder_.section_;
353 if (IncludingDebugSymbols()) {
354 // Get the layout of the symtab section.
355 symtab_builder_.section_.sh_offset = NextOffset(symtab_builder_.section_,
356 dynamic_builder_.section_);
357 symtab_builder_.section_.sh_addr = 0;
358 // Add to leave space for the null symbol.
Brian Carlstrom8758c642014-06-11 14:22:02 -0700359 symtab_builder_.section_.sh_size = symtab_builder_.GetSize() * sizeof(Elf32_Sym);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700360 symtab_builder_.section_.sh_link = symtab_builder_.GetLink();
361
362 // Get the layout of the dynstr section.
363 symtab_builder_.strtab_.section_.sh_offset = NextOffset(symtab_builder_.strtab_.section_,
364 symtab_builder_.section_);
365 symtab_builder_.strtab_.section_.sh_addr = 0;
366 symtab_builder_.strtab_.section_.sh_size = strtab.size();
367 symtab_builder_.strtab_.section_.sh_link = symtab_builder_.strtab_.GetLink();
368
369 prev = symtab_builder_.strtab_.section_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800370 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700371 if (debug_logging_) {
372 LOG(INFO) << "dynsym off=" << dynsym_builder_.section_.sh_offset
373 << " dynsym size=" << dynsym_builder_.section_.sh_size;
374 LOG(INFO) << "dynstr off=" << dynsym_builder_.strtab_.section_.sh_offset
375 << " dynstr size=" << dynsym_builder_.strtab_.section_.sh_size;
376 LOG(INFO) << "hash off=" << hash_builder_.section_.sh_offset
377 << " hash size=" << hash_builder_.section_.sh_size;
378 LOG(INFO) << "rodata off=" << rodata_builder_.section_.sh_offset
379 << " rodata size=" << rodata_builder_.section_.sh_size;
380 LOG(INFO) << "text off=" << text_builder_.section_.sh_offset
381 << " text size=" << text_builder_.section_.sh_size;
382 LOG(INFO) << "dynamic off=" << dynamic_builder_.section_.sh_offset
383 << " dynamic size=" << dynamic_builder_.section_.sh_size;
384 if (IncludingDebugSymbols()) {
385 LOG(INFO) << "symtab off=" << symtab_builder_.section_.sh_offset
386 << " symtab size=" << symtab_builder_.section_.sh_size;
387 LOG(INFO) << "strtab off=" << symtab_builder_.strtab_.section_.sh_offset
388 << " strtab size=" << symtab_builder_.strtab_.section_.sh_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 }
390 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700391 // Get the layout of the extra sections. (This will deal with the debug
392 // sections if they are there)
393 for (auto it = other_builders_.begin(); it != other_builders_.end(); ++it) {
394 it->section_.sh_offset = NextOffset(it->section_, prev);
395 it->section_.sh_addr = 0;
396 it->section_.sh_size = it->GetBuffer()->size();
397 it->section_.sh_link = it->GetLink();
398 pieces.push_back(ElfFilePiece(it->name_, it->section_.sh_offset,
399 it->GetBuffer()->data(), it->GetBuffer()->size()));
400 prev = it->section_;
401 if (debug_logging_) {
402 LOG(INFO) << it->name_ << " off=" << it->section_.sh_offset
403 << " " << it->name_ << " size=" << it->section_.sh_size;
404 }
405 }
406 // Get the layout of the shstrtab section
407 shstrtab_builder_.section_.sh_offset = NextOffset(shstrtab_builder_.section_, prev);
408 shstrtab_builder_.section_.sh_addr = 0;
409 shstrtab_builder_.section_.sh_size = shstrtab.size();
410 shstrtab_builder_.section_.sh_link = shstrtab_builder_.GetLink();
411 if (debug_logging_) {
412 LOG(INFO) << "shstrtab off=" << shstrtab_builder_.section_.sh_offset
413 << " shstrtab size=" << shstrtab_builder_.section_.sh_size;
Mark Mendellae9fd932014-02-10 16:14:35 -0800414 }
415
Brian Carlstromb12f3472014-06-11 14:54:46 -0700416 // The section list comes after come after.
417 Elf32_Word sections_offset = RoundUp(
418 shstrtab_builder_.section_.sh_offset + shstrtab_builder_.section_.sh_size,
419 sizeof(Elf32_Word));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420
Brian Carlstromb12f3472014-06-11 14:54:46 -0700421 // Setup the actual symbol arrays.
422 std::vector<Elf32_Sym> dynsym = dynsym_builder_.GenerateSymtab();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700423 CHECK_EQ(dynsym.size() * sizeof(Elf32_Sym), dynsym_builder_.section_.sh_size);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700424 std::vector<Elf32_Sym> symtab;
425 if (IncludingDebugSymbols()) {
426 symtab = symtab_builder_.GenerateSymtab();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700427 CHECK_EQ(symtab.size() * sizeof(Elf32_Sym), symtab_builder_.section_.sh_size);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700428 }
429
430 // Setup the dynamic section.
431 // This will add the 2 values we cannot know until now time, namely the size
432 // and the soname_offset.
433 std::vector<Elf32_Dyn> dynamic = dynamic_builder_.GetDynamics(dynstr.size(),
434 dynstr_soname_offset);
Brian Carlstrom8758c642014-06-11 14:22:02 -0700435 CHECK_EQ(dynamic.size() * sizeof(Elf32_Dyn), dynamic_builder_.section_.sh_size);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700436
437 // Finish setup of the program headers now that we know the layout of the
438 // whole file.
439 Elf32_Word load_r_size = rodata_builder_.section_.sh_offset + rodata_builder_.section_.sh_size;
440 program_headers[PH_LOAD_R__].p_filesz = load_r_size;
441 program_headers[PH_LOAD_R__].p_memsz = load_r_size;
442 program_headers[PH_LOAD_R__].p_align = rodata_builder_.section_.sh_addralign;
443
444 Elf32_Word load_rx_size = text_builder_.section_.sh_size;
445 program_headers[PH_LOAD_R_X].p_offset = text_builder_.section_.sh_offset;
446 program_headers[PH_LOAD_R_X].p_vaddr = text_builder_.section_.sh_offset;
447 program_headers[PH_LOAD_R_X].p_paddr = text_builder_.section_.sh_offset;
448 program_headers[PH_LOAD_R_X].p_filesz = load_rx_size;
449 program_headers[PH_LOAD_R_X].p_memsz = load_rx_size;
450 program_headers[PH_LOAD_R_X].p_align = text_builder_.section_.sh_addralign;
451
452 program_headers[PH_LOAD_RW_].p_offset = dynamic_builder_.section_.sh_offset;
453 program_headers[PH_LOAD_RW_].p_vaddr = dynamic_builder_.section_.sh_offset;
454 program_headers[PH_LOAD_RW_].p_paddr = dynamic_builder_.section_.sh_offset;
455 program_headers[PH_LOAD_RW_].p_filesz = dynamic_builder_.section_.sh_size;
456 program_headers[PH_LOAD_RW_].p_memsz = dynamic_builder_.section_.sh_size;
457 program_headers[PH_LOAD_RW_].p_align = dynamic_builder_.section_.sh_addralign;
458
459 program_headers[PH_DYNAMIC].p_offset = dynamic_builder_.section_.sh_offset;
460 program_headers[PH_DYNAMIC].p_vaddr = dynamic_builder_.section_.sh_offset;
461 program_headers[PH_DYNAMIC].p_paddr = dynamic_builder_.section_.sh_offset;
462 program_headers[PH_DYNAMIC].p_filesz = dynamic_builder_.section_.sh_size;
463 program_headers[PH_DYNAMIC].p_memsz = dynamic_builder_.section_.sh_size;
464 program_headers[PH_DYNAMIC].p_align = dynamic_builder_.section_.sh_addralign;
465
466 // Finish setup of the Ehdr values.
467 elf_header_.e_phoff = phdr_offset;
468 elf_header_.e_shoff = sections_offset;
469 elf_header_.e_phnum = PH_NUM;
470 elf_header_.e_shnum = section_ptrs.size();
471 elf_header_.e_shstrndx = shstrtab_builder_.section_index_;
472
473 // Add the rest of the pieces to the list.
474 pieces.push_back(ElfFilePiece("Elf Header", 0, &elf_header_, sizeof(elf_header_)));
475 pieces.push_back(ElfFilePiece("Program headers", phdr_offset,
476 &program_headers, sizeof(program_headers)));
477 pieces.push_back(ElfFilePiece(".dynamic", dynamic_builder_.section_.sh_offset,
478 dynamic.data(), dynamic_builder_.section_.sh_size));
479 pieces.push_back(ElfFilePiece(".dynsym", dynsym_builder_.section_.sh_offset,
Brian Carlstrom8758c642014-06-11 14:22:02 -0700480 dynsym.data(), dynsym.size() * sizeof(Elf32_Sym)));
Brian Carlstromb12f3472014-06-11 14:54:46 -0700481 pieces.push_back(ElfFilePiece(".dynstr", dynsym_builder_.strtab_.section_.sh_offset,
482 dynstr.c_str(), dynstr.size()));
483 pieces.push_back(ElfFilePiece(".hash", hash_builder_.section_.sh_offset,
484 hash.data(), hash.size() * sizeof(Elf32_Word)));
485 pieces.push_back(ElfFilePiece(".rodata", rodata_builder_.section_.sh_offset,
Alex Light3470ab42014-06-18 10:35:45 -0700486 nullptr, rodata_builder_.section_.sh_size));
Brian Carlstromb12f3472014-06-11 14:54:46 -0700487 pieces.push_back(ElfFilePiece(".text", text_builder_.section_.sh_offset,
Alex Light3470ab42014-06-18 10:35:45 -0700488 nullptr, text_builder_.section_.sh_size));
Brian Carlstromb12f3472014-06-11 14:54:46 -0700489 if (IncludingDebugSymbols()) {
490 pieces.push_back(ElfFilePiece(".symtab", symtab_builder_.section_.sh_offset,
491 symtab.data(), symtab.size() * sizeof(Elf32_Sym)));
492 pieces.push_back(ElfFilePiece(".strtab", symtab_builder_.strtab_.section_.sh_offset,
493 strtab.c_str(), strtab.size()));
494 }
495 pieces.push_back(ElfFilePiece(".shstrtab", shstrtab_builder_.section_.sh_offset,
496 &shstrtab[0], shstrtab.size()));
497 for (uint32_t i = 0; i < section_ptrs.size(); ++i) {
498 // Just add all the sections in induvidually since they are all over the
499 // place on the heap/stack.
500 Elf32_Word cur_off = sections_offset + i * sizeof(Elf32_Shdr);
501 pieces.push_back(ElfFilePiece("section table piece", cur_off,
502 section_ptrs[i], sizeof(Elf32_Shdr)));
503 }
504
505 if (!WriteOutFile(pieces)) {
506 LOG(ERROR) << "Unable to write to file " << elf_file_->GetPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 return false;
508 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700509 // write out the actual oat file data.
510 Elf32_Word oat_data_offset = rodata_builder_.section_.sh_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 if (static_cast<off_t>(oat_data_offset) != lseek(elf_file_->Fd(), oat_data_offset, SEEK_SET)) {
512 PLOG(ERROR) << "Failed to seek to .rodata offset " << oat_data_offset
513 << " for " << elf_file_->GetPath();
514 return false;
515 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700516 std::unique_ptr<BufferedOutputStream> output_stream(
517 new BufferedOutputStream(new FileOutputStream(elf_file_)));
518 if (!oat_writer_->Write(output_stream.get())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 PLOG(ERROR) << "Failed to write .rodata and .text for " << elf_file_->GetPath();
520 return false;
521 }
522
Brian Carlstrom35f72252014-06-11 21:24:53 +0000523 return true;
Brian Carlstromb12f3472014-06-11 14:54:46 -0700524}
525
526bool ElfWriterQuick::ElfBuilder::WriteOutFile(const std::vector<ElfFilePiece>& pieces) {
527 // TODO It would be nice if this checked for overlap.
528 for (auto it = pieces.begin(); it != pieces.end(); ++it) {
529 if (it->data_) {
530 if (static_cast<off_t>(it->offset_) != lseek(elf_file_->Fd(), it->offset_, SEEK_SET)) {
531 PLOG(ERROR) << "Failed to seek to " << it->dbg_name_ << " offset location "
532 << it->offset_ << " for " << elf_file_->GetPath();
533 return false;
534 }
535 if (!elf_file_->WriteFully(it->data_, it->size_)) {
536 PLOG(ERROR) << "Failed to write " << it->dbg_name_ << " for " << elf_file_->GetPath();
537 return false;
538 }
539 }
540 }
541 return true;
542}
543
544void ElfWriterQuick::ElfBuilder::SetupDynamic() {
545 dynamic_builder_.AddDynamicTag(DT_HASH, 0, &hash_builder_);
546 dynamic_builder_.AddDynamicTag(DT_STRTAB, 0, &dynsym_builder_.strtab_);
547 dynamic_builder_.AddDynamicTag(DT_SYMTAB, 0, &dynsym_builder_);
548 dynamic_builder_.AddDynamicTag(DT_SYMENT, sizeof(Elf32_Sym));
549}
550
551void ElfWriterQuick::ElfBuilder::SetupRequiredSymbols() {
552 dynsym_builder_.AddSymbol("oatdata", &rodata_builder_, 0, true,
553 rodata_builder_.size_, STB_GLOBAL, STT_OBJECT);
554 dynsym_builder_.AddSymbol("oatexec", &text_builder_, 0, true,
555 text_builder_.size_, STB_GLOBAL, STT_OBJECT);
556 dynsym_builder_.AddSymbol("oatlastword", &text_builder_, text_builder_.size_ - 4,
557 true, 4, STB_GLOBAL, STT_OBJECT);
558}
559
Brian Carlstrom8758c642014-06-11 14:22:02 -0700560void ElfWriterQuick::ElfDynamicBuilder::AddDynamicTag(Elf32_Sword tag, Elf32_Word d_un) {
Brian Carlstromb12f3472014-06-11 14:54:46 -0700561 if (tag == DT_NULL) {
562 return;
563 }
Alex Light3470ab42014-06-18 10:35:45 -0700564 dynamics_.push_back({nullptr, tag, d_un});
Brian Carlstromb12f3472014-06-11 14:54:46 -0700565}
566
Brian Carlstrom8758c642014-06-11 14:22:02 -0700567void ElfWriterQuick::ElfDynamicBuilder::AddDynamicTag(Elf32_Sword tag, Elf32_Word d_un,
Brian Carlstromb12f3472014-06-11 14:54:46 -0700568 ElfSectionBuilder* section) {
569 if (tag == DT_NULL) {
570 return;
571 }
572 dynamics_.push_back({section, tag, d_un});
573}
574
Brian Carlstrom8758c642014-06-11 14:22:02 -0700575std::vector<Elf32_Dyn> ElfWriterQuick::ElfDynamicBuilder::GetDynamics(Elf32_Word strsz,
576 Elf32_Word soname) {
Brian Carlstromb12f3472014-06-11 14:54:46 -0700577 std::vector<Elf32_Dyn> ret;
578 for (auto it = dynamics_.cbegin(); it != dynamics_.cend(); ++it) {
579 if (it->section_) {
580 // We are adding an address relative to a section.
581 ret.push_back(
Brian Carlstrom8758c642014-06-11 14:22:02 -0700582 {it->tag_, {it->off_ + it->section_->section_.sh_addr}});
Brian Carlstromb12f3472014-06-11 14:54:46 -0700583 } else {
584 ret.push_back({it->tag_, {it->off_}});
585 }
586 }
587 ret.push_back({DT_STRSZ, {strsz}});
588 ret.push_back({DT_SONAME, {soname}});
589 ret.push_back({DT_NULL, {0}});
590 return ret;
591}
592
593std::vector<Elf32_Sym> ElfWriterQuick::ElfSymtabBuilder::GenerateSymtab() {
594 std::vector<Elf32_Sym> ret;
595 Elf32_Sym undef_sym;
596 memset(&undef_sym, 0, sizeof(undef_sym));
597 undef_sym.st_shndx = SHN_UNDEF;
598 ret.push_back(undef_sym);
599
600 for (auto it = symbols_.cbegin(); it != symbols_.cend(); ++it) {
601 Elf32_Sym sym;
602 memset(&sym, 0, sizeof(sym));
603 sym.st_name = it->name_idx_;
604 if (it->is_relative_) {
605 sym.st_value = it->addr_ + it->section_->section_.sh_offset;
606 } else {
607 sym.st_value = it->addr_;
608 }
609 sym.st_size = it->size_;
610 sym.st_other = it->other_;
611 sym.st_shndx = it->section_->section_index_;
612 sym.st_info = it->info_;
613
614 ret.push_back(sym);
615 }
616 return ret;
617}
618
619std::string ElfWriterQuick::ElfSymtabBuilder::GenerateStrtab() {
620 std::string tab;
621 tab += '\0';
622 for (auto it = symbols_.begin(); it != symbols_.end(); ++it) {
623 it->name_idx_ = tab.size();
624 tab += it->name_;
625 tab += '\0';
626 }
627 strtab_.section_.sh_size = tab.size();
628 return tab;
629}
630
631void ElfWriterQuick::ElfBuilder::AssignSectionStr(
632 ElfSectionBuilder* builder, std::string* strtab) {
633 builder->section_.sh_name = strtab->size();
634 *strtab += builder->name_;
635 *strtab += '\0';
636 if (debug_logging_) {
637 LOG(INFO) << "adding section name \"" << builder->name_ << "\" "
638 << "to shstrtab at offset " << builder->section_.sh_name;
639 }
640}
641
642// from bionic
643static unsigned elfhash(const char *_name) {
644 const unsigned char *name = (const unsigned char *) _name;
645 unsigned h = 0, g;
646
647 while (*name) {
648 h = (h << 4) + *name++;
649 g = h & 0xf0000000;
650 h ^= g;
651 h ^= g >> 24;
652 }
653 return h;
654}
655
656
657std::vector<Elf32_Word> ElfWriterQuick::ElfSymtabBuilder::GenerateHashContents() {
658 // Here is how The ELF hash table works.
659 // There are 3 arrays to worry about.
660 // * The symbol table where the symbol information is.
661 // * The bucket array which is an array of indexes into the symtab and chain.
662 // * The chain array which is also an array of indexes into the symtab and chain.
663 //
664 // Lets say the state is something like this.
665 // +--------+ +--------+ +-----------+
666 // | symtab | | bucket | | chain |
Alex Light3470ab42014-06-18 10:35:45 -0700667 // | nullptr | | 1 | | STN_UNDEF |
Brian Carlstromb12f3472014-06-11 14:54:46 -0700668 // | <sym1> | | 4 | | 2 |
669 // | <sym2> | | | | 5 |
670 // | <sym3> | | | | STN_UNDEF |
671 // | <sym4> | | | | 3 |
672 // | <sym5> | | | | STN_UNDEF |
673 // +--------+ +--------+ +-----------+
674 //
675 // The lookup process (in python psudocode) is
676 //
677 // def GetSym(name):
678 // # NB STN_UNDEF == 0
679 // indx = bucket[elfhash(name) % num_buckets]
680 // while indx != STN_UNDEF:
681 // if GetSymbolName(symtab[indx]) == name:
682 // return symtab[indx]
683 // indx = chain[indx]
684 // return SYMBOL_NOT_FOUND
685 //
686 // Between bucket and chain arrays every symtab index must be present exactly
687 // once (except for STN_UNDEF, which must be present 1 + num_bucket times).
688
689 // Select number of buckets.
690 // This is essentially arbitrary.
691 Elf32_Word nbuckets;
Brian Carlstrom8758c642014-06-11 14:22:02 -0700692 Elf32_Word chain_size = GetSize();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700693 if (symbols_.size() < 8) {
694 nbuckets = 2;
695 } else if (symbols_.size() < 32) {
696 nbuckets = 4;
697 } else if (symbols_.size() < 256) {
698 nbuckets = 16;
699 } else {
700 // Have about 32 ids per bucket.
701 nbuckets = RoundUp(symbols_.size()/32, 2);
702 }
703 std::vector<Elf32_Word> hash;
704 hash.push_back(nbuckets);
705 hash.push_back(chain_size);
706 uint32_t bucket_offset = hash.size();
707 uint32_t chain_offset = bucket_offset + nbuckets;
708 hash.resize(hash.size() + nbuckets + chain_size, 0);
709
710 Elf32_Word* buckets = hash.data() + bucket_offset;
711 Elf32_Word* chain = hash.data() + chain_offset;
712
713 // Set up the actual hash table.
714 for (Elf32_Word i = 0; i < symbols_.size(); i++) {
715 // Add 1 since we need to have the null symbol that is not in the symbols
716 // list.
717 Elf32_Word index = i + 1;
718 Elf32_Word hash_val = static_cast<Elf32_Word>(elfhash(symbols_[i].name_.c_str())) % nbuckets;
719 if (buckets[hash_val] == 0) {
720 buckets[hash_val] = index;
721 } else {
722 hash_val = buckets[hash_val];
723 CHECK_LT(hash_val, chain_size);
724 while (chain[hash_val] != 0) {
725 hash_val = chain[hash_val];
726 CHECK_LT(hash_val, chain_size);
727 }
728 chain[hash_val] = index;
729 // Check for loops. Works because if this is non-empty then there must be
730 // another cell which already contains the same symbol index as this one,
731 // which means some symbol has more then one name, which isn't allowed.
732 CHECK_EQ(chain[index], static_cast<Elf32_Word>(0));
733 }
734 }
735
736 return hash;
737}
738
739void ElfWriterQuick::ElfBuilder::SetupEhdr() {
740 memset(&elf_header_, 0, sizeof(elf_header_));
741 elf_header_.e_ident[EI_MAG0] = ELFMAG0;
742 elf_header_.e_ident[EI_MAG1] = ELFMAG1;
743 elf_header_.e_ident[EI_MAG2] = ELFMAG2;
744 elf_header_.e_ident[EI_MAG3] = ELFMAG3;
745 elf_header_.e_ident[EI_CLASS] = ELFCLASS32;
746 elf_header_.e_ident[EI_DATA] = ELFDATA2LSB;
747 elf_header_.e_ident[EI_VERSION] = EV_CURRENT;
748 elf_header_.e_ident[EI_OSABI] = ELFOSABI_LINUX;
749 elf_header_.e_ident[EI_ABIVERSION] = 0;
750 elf_header_.e_type = ET_DYN;
751 elf_header_.e_version = 1;
752 elf_header_.e_entry = 0;
753 elf_header_.e_ehsize = sizeof(Elf32_Ehdr);
754 elf_header_.e_phentsize = sizeof(Elf32_Phdr);
755 elf_header_.e_shentsize = sizeof(Elf32_Shdr);
756 elf_header_.e_phoff = sizeof(Elf32_Ehdr);
757}
758
759void ElfWriterQuick::ElfBuilder::SetISA(InstructionSet isa) {
760 switch (isa) {
761 case kArm:
762 // Fall through.
763 case kThumb2: {
764 elf_header_.e_machine = EM_ARM;
765 elf_header_.e_flags = EF_ARM_EABI_VER5;
766 break;
767 }
768 case kArm64: {
769 elf_header_.e_machine = EM_AARCH64;
770 elf_header_.e_flags = 0;
771 break;
772 }
773 case kX86: {
774 elf_header_.e_machine = EM_386;
775 elf_header_.e_flags = 0;
776 break;
777 }
778 case kX86_64: {
779 elf_header_.e_machine = EM_X86_64;
780 elf_header_.e_flags = 0;
781 break;
782 }
783 case kMips: {
784 elf_header_.e_machine = EM_MIPS;
785 elf_header_.e_flags = (EF_MIPS_NOREORDER |
786 EF_MIPS_PIC |
787 EF_MIPS_CPIC |
788 EF_MIPS_ABI_O32 |
789 EF_MIPS_ARCH_32R2);
790 break;
791 }
792 default: {
793 fatal_error_ = true;
794 LOG(FATAL) << "Unknown instruction set: " << isa;
795 break;
796 }
797 }
798}
799
800void ElfWriterQuick::ElfSymtabBuilder::AddSymbol(
801 const std::string& name, const ElfSectionBuilder* section, Elf32_Addr addr,
802 bool is_relative, Elf32_Word size, uint8_t binding, uint8_t type, uint8_t other) {
803 CHECK(section);
804 ElfSymtabBuilder::ElfSymbolState state {name, section, addr, size, is_relative,
805 MakeStInfo(binding, type), other, 0};
806 symbols_.push_back(state);
807}
808
809bool ElfWriterQuick::Create(File* elf_file,
810 OatWriter* oat_writer,
811 const std::vector<const DexFile*>& dex_files,
812 const std::string& android_root,
813 bool is_host,
814 const CompilerDriver& driver) {
815 ElfWriterQuick elf_writer(driver, elf_file);
816 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
817}
818
Alex Light53cb16b2014-06-12 11:26:29 -0700819// Add patch information to this section. Each patch is a Elf32_Word that
820// identifies an offset from the start of the text section
821void ElfWriterQuick::ReservePatchSpace(std::vector<uint8_t>* buffer, bool debug) {
822 size_t size =
823 compiler_driver_->GetCodeToPatch().size() +
824 compiler_driver_->GetMethodsToPatch().size() +
825 compiler_driver_->GetClassesToPatch().size();
826 if (size == 0) {
827 if (debug) {
828 LOG(INFO) << "No patches to record";
829 }
830 return;
831 }
832 buffer->resize(size * sizeof(uintptr_t));
833 if (debug) {
834 LOG(INFO) << "Patches reserved for " << size;
835 }
836}
837
Andreas Gampe79273802014-08-05 20:21:05 -0700838std::vector<uint8_t>* ConstructCIEFrameX86(bool is_x86_64) {
839 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
840
841 // Length (will be filled in later in this routine).
842 PushWord(cfi_info, 0);
843
844 // CIE id: always 0.
845 PushWord(cfi_info, 0);
846
847 // Version: always 1.
848 cfi_info->push_back(0x01);
849
850 // Augmentation: 'zR\0'
851 cfi_info->push_back(0x7a);
852 cfi_info->push_back(0x52);
853 cfi_info->push_back(0x0);
854
855 // Code alignment: 1.
856 EncodeUnsignedLeb128(1, cfi_info);
857
858 // Data alignment.
859 if (is_x86_64) {
860 EncodeSignedLeb128(-8, cfi_info);
861 } else {
862 EncodeSignedLeb128(-4, cfi_info);
863 }
864
865 // Return address register.
866 if (is_x86_64) {
867 // R16(RIP)
868 cfi_info->push_back(0x10);
869 } else {
870 // R8(EIP)
871 cfi_info->push_back(0x08);
872 }
873
874 // Augmentation length: 1.
875 cfi_info->push_back(1);
876
877 // Augmentation data: 0x03 ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata4).
878 cfi_info->push_back(0x03);
879
880 // Initial instructions.
881 if (is_x86_64) {
882 // DW_CFA_def_cfa R7(RSP) 8.
883 cfi_info->push_back(0x0c);
884 cfi_info->push_back(0x07);
885 cfi_info->push_back(0x08);
886
887 // DW_CFA_offset R16(RIP) 1 (* -8).
888 cfi_info->push_back(0x90);
889 cfi_info->push_back(0x01);
890 } else {
891 // DW_CFA_def_cfa R4(ESP) 4.
892 cfi_info->push_back(0x0c);
893 cfi_info->push_back(0x04);
894 cfi_info->push_back(0x04);
895
896 // DW_CFA_offset R8(EIP) 1 (* -4).
897 cfi_info->push_back(0x88);
898 cfi_info->push_back(0x01);
899 }
900
901 // Padding to a multiple of 4
902 while ((cfi_info->size() & 3) != 0) {
903 // DW_CFA_nop is encoded as 0.
904 cfi_info->push_back(0);
905 }
906
907 // Set the length of the CIE inside the generated bytes.
908 uint32_t length = cfi_info->size() - 4;
909 (*cfi_info)[0] = length;
910 (*cfi_info)[1] = length >> 8;
911 (*cfi_info)[2] = length >> 16;
912 (*cfi_info)[3] = length >> 24;
913 return cfi_info;
914}
915
916std::vector<uint8_t>* ConstructCIEFrame(InstructionSet isa) {
917 switch (isa) {
918 case kX86:
919 return ConstructCIEFrameX86(false);
920 case kX86_64:
921 return ConstructCIEFrameX86(true);
922
923 default:
924 // Not implemented.
925 return nullptr;
926 }
927}
928
Brian Carlstromb12f3472014-06-11 14:54:46 -0700929bool ElfWriterQuick::Write(OatWriter* oat_writer,
930 const std::vector<const DexFile*>& dex_files_unused,
931 const std::string& android_root_unused,
932 bool is_host_unused) {
Andreas Gampe79273802014-08-05 20:21:05 -0700933 constexpr bool debug = false;
Brian Carlstromb12f3472014-06-11 14:54:46 -0700934 const OatHeader& oat_header = oat_writer->GetOatHeader();
935 Elf32_Word oat_data_size = oat_header.GetExecutableOffset();
936 uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size;
937
938 ElfBuilder builder(oat_writer, elf_file_, compiler_driver_->GetInstructionSet(), 0,
Andreas Gampe79273802014-08-05 20:21:05 -0700939 oat_data_size, oat_data_size, oat_exec_size,
940 compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols(),
941 debug);
Alex Light78382fa2014-06-06 15:45:32 -0700942
Andreas Gampe79273802014-08-05 20:21:05 -0700943 if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
944 WriteDebugSymbols(builder, oat_writer);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700945 }
946
Alex Light53cb16b2014-06-12 11:26:29 -0700947 if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation()) {
948 ElfRawSectionBuilder oat_patches(".oat_patches", SHT_OAT_PATCH, 0, NULL, 0,
Alex Light4b0d2d92014-08-06 13:37:23 -0700949 sizeof(uintptr_t), sizeof(uintptr_t));
Alex Light53cb16b2014-06-12 11:26:29 -0700950 ReservePatchSpace(oat_patches.GetBuffer(), debug);
951 builder.RegisterRawSection(oat_patches);
952 }
953
Brian Carlstromb12f3472014-06-11 14:54:46 -0700954 return builder.Write();
955}
Mark Mendellae9fd932014-02-10 16:14:35 -0800956
Andreas Gampe79273802014-08-05 20:21:05 -0700957void ElfWriterQuick::WriteDebugSymbols(ElfBuilder& builder, OatWriter* oat_writer) {
958 std::unique_ptr<std::vector<uint8_t>> cfi_info(
959 ConstructCIEFrame(compiler_driver_->GetInstructionSet()));
960
961 // Iterate over the compiled methods.
Alex Light78382fa2014-06-06 15:45:32 -0700962 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetCFIMethodInfo();
963 ElfSymtabBuilder* symtab = &builder.symtab_builder_;
964 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
965 symtab->AddSymbol(it->method_name_, &builder.text_builder_, it->low_pc_, true,
966 it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe79273802014-08-05 20:21:05 -0700967
968 // Include CFI for compiled method, if possible.
969 if (cfi_info.get() != nullptr) {
970 DCHECK(it->compiled_method_ != nullptr);
971
972 // Copy in the FDE, if present
973 const std::vector<uint8_t>* fde = it->compiled_method_->GetCFIInfo();
974 if (fde != nullptr) {
975 // Copy the information into cfi_info and then fix the address in the new copy.
976 int cur_offset = cfi_info->size();
977 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
978
979 // Set the 'CIE_pointer' field to cur_offset+4.
980 uint32_t CIE_pointer = cur_offset + 4;
981 uint32_t offset_to_update = cur_offset + sizeof(uint32_t);
982 (*cfi_info)[offset_to_update+0] = CIE_pointer;
983 (*cfi_info)[offset_to_update+1] = CIE_pointer >> 8;
984 (*cfi_info)[offset_to_update+2] = CIE_pointer >> 16;
985 (*cfi_info)[offset_to_update+3] = CIE_pointer >> 24;
986
987 // Set the 'initial_location' field to address the start of the method.
988 offset_to_update = cur_offset + 2*sizeof(uint32_t);
989 const uint32_t quick_code_start = it->low_pc_;
990 (*cfi_info)[offset_to_update+0] = quick_code_start;
991 (*cfi_info)[offset_to_update+1] = quick_code_start >> 8;
992 (*cfi_info)[offset_to_update+2] = quick_code_start >> 16;
993 (*cfi_info)[offset_to_update+3] = quick_code_start >> 24;
994 }
995 }
Alex Light78382fa2014-06-06 15:45:32 -0700996 }
Alex Light78382fa2014-06-06 15:45:32 -0700997
Andreas Gampe79273802014-08-05 20:21:05 -0700998 if (cfi_info.get() != nullptr) {
999 // Now lay down the Elf sections.
1000 ElfRawSectionBuilder debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
1001 ElfRawSectionBuilder debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
1002 ElfRawSectionBuilder debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
1003 ElfRawSectionBuilder eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0);
1004 eh_frame.SetBuffer(std::move(*cfi_info.get()));
Mark Mendellae9fd932014-02-10 16:14:35 -08001005
Andreas Gampe79273802014-08-05 20:21:05 -07001006 FillInCFIInformation(oat_writer, debug_info.GetBuffer(), debug_abbrev.GetBuffer(),
1007 debug_str.GetBuffer());
1008 builder.RegisterRawSection(debug_info);
1009 builder.RegisterRawSection(debug_abbrev);
1010 builder.RegisterRawSection(eh_frame);
1011 builder.RegisterRawSection(debug_str);
1012 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001013}
1014
Mark Mendellae9fd932014-02-10 16:14:35 -08001015void ElfWriterQuick::FillInCFIInformation(OatWriter* oat_writer,
1016 std::vector<uint8_t>* dbg_info,
1017 std::vector<uint8_t>* dbg_abbrev,
1018 std::vector<uint8_t>* dbg_str) {
1019 // Create the debug_abbrev section with boilerplate information.
1020 // We only care about low_pc and high_pc right now for the compilation
1021 // unit and methods.
1022
1023 // Tag 1: Compilation unit: DW_TAG_compile_unit.
1024 dbg_abbrev->push_back(1);
1025 dbg_abbrev->push_back(DW_TAG_compile_unit);
1026
1027 // There are children (the methods).
1028 dbg_abbrev->push_back(DW_CHILDREN_yes);
1029
1030 // DW_LANG_Java DW_FORM_data1.
1031 dbg_abbrev->push_back(DW_AT_language);
1032 dbg_abbrev->push_back(DW_FORM_data1);
1033
1034 // DW_AT_low_pc DW_FORM_addr.
1035 dbg_abbrev->push_back(DW_AT_low_pc);
1036 dbg_abbrev->push_back(DW_FORM_addr);
1037
1038 // DW_AT_high_pc DW_FORM_addr.
1039 dbg_abbrev->push_back(DW_AT_high_pc);
1040 dbg_abbrev->push_back(DW_FORM_addr);
1041
1042 // End of DW_TAG_compile_unit.
1043 PushHalf(dbg_abbrev, 0);
1044
1045 // Tag 2: Compilation unit: DW_TAG_subprogram.
1046 dbg_abbrev->push_back(2);
1047 dbg_abbrev->push_back(DW_TAG_subprogram);
1048
1049 // There are no children.
1050 dbg_abbrev->push_back(DW_CHILDREN_no);
1051
1052 // Name of the method.
1053 dbg_abbrev->push_back(DW_AT_name);
1054 dbg_abbrev->push_back(DW_FORM_strp);
1055
1056 // DW_AT_low_pc DW_FORM_addr.
1057 dbg_abbrev->push_back(DW_AT_low_pc);
1058 dbg_abbrev->push_back(DW_FORM_addr);
1059
1060 // DW_AT_high_pc DW_FORM_addr.
1061 dbg_abbrev->push_back(DW_AT_high_pc);
1062 dbg_abbrev->push_back(DW_FORM_addr);
1063
1064 // End of DW_TAG_subprogram.
1065 PushHalf(dbg_abbrev, 0);
1066
1067 // Start the debug_info section with the header information
1068 // 'unit_length' will be filled in later.
1069 PushWord(dbg_info, 0);
1070
1071 // 'version' - 3.
1072 PushHalf(dbg_info, 3);
1073
1074 // Offset into .debug_abbrev section (always 0).
1075 PushWord(dbg_info, 0);
1076
1077 // Address size: 4.
1078 dbg_info->push_back(4);
1079
1080 // Start the description for the compilation unit.
1081 // This uses tag 1.
1082 dbg_info->push_back(1);
1083
1084 // The language is Java.
1085 dbg_info->push_back(DW_LANG_Java);
1086
1087 // Leave space for low_pc and high_pc.
1088 int low_pc_offset = dbg_info->size();
1089 PushWord(dbg_info, 0);
1090 PushWord(dbg_info, 0);
1091
1092 // Walk through the information in the method table, and enter into dbg_info.
1093 const std::vector<OatWriter::DebugInfo>& dbg = oat_writer->GetCFIMethodInfo();
1094 uint32_t low_pc = 0xFFFFFFFFU;
1095 uint32_t high_pc = 0;
1096
1097 for (uint32_t i = 0; i < dbg.size(); i++) {
1098 const OatWriter::DebugInfo& info = dbg[i];
1099 if (info.low_pc_ < low_pc) {
1100 low_pc = info.low_pc_;
1101 }
1102 if (info.high_pc_ > high_pc) {
1103 high_pc = info.high_pc_;
1104 }
1105
1106 // Start a new TAG: subroutine (2).
1107 dbg_info->push_back(2);
1108
1109 // Enter the name into the string table (and NUL terminate).
1110 uint32_t str_offset = dbg_str->size();
1111 dbg_str->insert(dbg_str->end(), info.method_name_.begin(), info.method_name_.end());
1112 dbg_str->push_back('\0');
1113
1114 // Enter name, low_pc, high_pc.
1115 PushWord(dbg_info, str_offset);
1116 PushWord(dbg_info, info.low_pc_);
1117 PushWord(dbg_info, info.high_pc_);
1118 }
1119
1120 // One byte terminator
1121 dbg_info->push_back(0);
1122
1123 // We have now walked all the methods. Fill in lengths and low/high PCs.
1124 UpdateWord(dbg_info, 0, dbg_info->size() - 4);
1125 UpdateWord(dbg_info, low_pc_offset, low_pc);
1126 UpdateWord(dbg_info, low_pc_offset + 4, high_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127}
1128
1129} // namespace art