blob: 78757ecfe27991979ac9e9f2c6c827b64749001e [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"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000023#include "elf_utils.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070024#include "file_output_stream.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "globals.h"
26#include "oat.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070027#include "oat_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "utils.h"
29
30namespace art {
31
Brian Carlstromb12f3472014-06-11 14:54:46 -070032static constexpr Elf32_Word NextOffset(const Elf32_Shdr& cur, const Elf32_Shdr& prev) {
33 return RoundUp(prev.sh_size + prev.sh_offset, cur.sh_addralign);
Brian Carlstrom7940e442013-07-12 13:46:57 -070034}
35
Brian Carlstromb12f3472014-06-11 14:54:46 -070036static uint8_t MakeStInfo(uint8_t binding, uint8_t type) {
37 return ((binding) << 4) + ((type) & 0xf);
38}
39
40bool ElfWriterQuick::ElfBuilder::Write() {
41 // The basic layout of the elf file. Order may be different in final output.
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 // +-------------------------+
43 // | Elf32_Ehdr |
44 // +-------------------------+
45 // | Elf32_Phdr PHDR |
46 // | Elf32_Phdr LOAD R | .dynsym .dynstr .hash .rodata
47 // | Elf32_Phdr LOAD R X | .text
48 // | Elf32_Phdr LOAD RW | .dynamic
49 // | Elf32_Phdr DYNAMIC | .dynamic
50 // +-------------------------+
51 // | .dynsym |
52 // | Elf32_Sym STN_UNDEF |
53 // | Elf32_Sym oatdata |
54 // | Elf32_Sym oatexec |
55 // | Elf32_Sym oatlastword |
56 // +-------------------------+
57 // | .dynstr |
58 // | \0 |
59 // | oatdata\0 |
60 // | oatexec\0 |
61 // | oatlastword\0 |
62 // | boot.oat\0 |
63 // +-------------------------+
64 // | .hash |
Brian Carlstromb12f3472014-06-11 14:54:46 -070065 // | Elf32_Word nbucket = b |
66 // | Elf32_Word nchain = c |
67 // | Elf32_Word bucket[0] |
68 // | ... |
69 // | Elf32_Word bucket[b - 1]|
70 // | Elf32_Word chain[0] |
71 // | ... |
72 // | Elf32_Word chain[c - 1] |
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 // +-------------------------+
74 // | .rodata |
75 // | oatdata..oatexec-4 |
76 // +-------------------------+
77 // | .text |
78 // | oatexec..oatlastword |
79 // +-------------------------+
80 // | .dynamic |
81 // | Elf32_Dyn DT_SONAME |
82 // | Elf32_Dyn DT_HASH |
83 // | Elf32_Dyn DT_SYMTAB |
84 // | Elf32_Dyn DT_SYMENT |
85 // | Elf32_Dyn DT_STRTAB |
86 // | Elf32_Dyn DT_STRSZ |
87 // | Elf32_Dyn DT_NULL |
Brian Carlstromb12f3472014-06-11 14:54:46 -070088 // +-------------------------+ (Optional)
89 // | .strtab | (Optional)
90 // | program symbol names | (Optional)
91 // +-------------------------+ (Optional)
92 // | .symtab | (Optional)
93 // | program symbols | (Optional)
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 // +-------------------------+
95 // | .shstrtab |
96 // | \0 |
97 // | .dynamic\0 |
98 // | .dynsym\0 |
99 // | .dynstr\0 |
100 // | .hash\0 |
101 // | .rodata\0 |
102 // | .text\0 |
103 // | .shstrtab\0 |
Brian Carlstromb12f3472014-06-11 14:54:46 -0700104 // | .symtab\0 | (Optional)
105 // | .strtab\0 | (Optional)
106 // | .debug_str\0 | (Optional)
107 // | .debug_info\0 | (Optional)
108 // | .debug_frame\0 | (Optional)
109 // | .debug_abbrev\0 | (Optional)
110 // +-------------------------+ (Optional)
111 // | .debug_str | (Optional)
112 // +-------------------------+ (Optional)
113 // | .debug_info | (Optional)
114 // +-------------------------+ (Optional)
115 // | .debug_frame | (Optional)
116 // +-------------------------+ (Optional)
117 // | .debug_abbrev | (Optional)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 // +-------------------------+
119 // | Elf32_Shdr NULL |
120 // | Elf32_Shdr .dynsym |
121 // | Elf32_Shdr .dynstr |
122 // | Elf32_Shdr .hash |
123 // | Elf32_Shdr .text |
124 // | Elf32_Shdr .rodata |
125 // | Elf32_Shdr .dynamic |
126 // | Elf32_Shdr .shstrtab |
Brian Carlstromb12f3472014-06-11 14:54:46 -0700127 // | Elf32_Shdr .debug_str | (Optional)
Mark Mendellae9fd932014-02-10 16:14:35 -0800128 // | Elf32_Shdr .debug_info | (Optional)
Brian Carlstrom35f72252014-06-11 21:24:53 +0000129 // | Elf32_Shdr .debug_frame | (Optional)
Brian Carlstromb12f3472014-06-11 14:54:46 -0700130 // | Elf32_Shdr .debug_abbrev| (Optional)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 // +-------------------------+
132
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133
Brian Carlstromb12f3472014-06-11 14:54:46 -0700134 if (fatal_error_) {
135 return false;
136 }
137 // Step 1. Figure out all the offsets.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138
Brian Carlstromb12f3472014-06-11 14:54:46 -0700139 // What phdr is.
140 uint32_t phdr_offset = sizeof(Elf32_Ehdr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 const uint8_t PH_PHDR = 0;
142 const uint8_t PH_LOAD_R__ = 1;
143 const uint8_t PH_LOAD_R_X = 2;
144 const uint8_t PH_LOAD_RW_ = 3;
145 const uint8_t PH_DYNAMIC = 4;
146 const uint8_t PH_NUM = 5;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000147 uint32_t phdr_size = sizeof(Elf32_Phdr) * PH_NUM;
Brian Carlstromb12f3472014-06-11 14:54:46 -0700148 if (debug_logging_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 LOG(INFO) << "phdr_offset=" << phdr_offset << std::hex << " " << phdr_offset;
150 LOG(INFO) << "phdr_size=" << phdr_size << std::hex << " " << phdr_size;
151 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700152 Elf32_Phdr program_headers[PH_NUM];
153 memset(&program_headers, 0, sizeof(program_headers));
154 program_headers[PH_PHDR].p_type = PT_PHDR;
155 program_headers[PH_PHDR].p_offset = phdr_offset;
156 program_headers[PH_PHDR].p_vaddr = phdr_offset;
157 program_headers[PH_PHDR].p_paddr = phdr_offset;
158 program_headers[PH_PHDR].p_filesz = sizeof(program_headers);
159 program_headers[PH_PHDR].p_memsz = sizeof(program_headers);
160 program_headers[PH_PHDR].p_flags = PF_R;
161 program_headers[PH_PHDR].p_align = sizeof(Elf32_Word);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162
Brian Carlstromb12f3472014-06-11 14:54:46 -0700163 program_headers[PH_LOAD_R__].p_type = PT_LOAD;
164 program_headers[PH_LOAD_R__].p_offset = 0;
165 program_headers[PH_LOAD_R__].p_vaddr = 0;
166 program_headers[PH_LOAD_R__].p_paddr = 0;
167 program_headers[PH_LOAD_R__].p_flags = PF_R;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168
Brian Carlstromb12f3472014-06-11 14:54:46 -0700169 program_headers[PH_LOAD_R_X].p_type = PT_LOAD;
170 program_headers[PH_LOAD_R_X].p_flags = PF_R | PF_X;
171
172 program_headers[PH_LOAD_RW_].p_type = PT_LOAD;
173 program_headers[PH_LOAD_RW_].p_flags = PF_R | PF_W;
174
175 program_headers[PH_DYNAMIC].p_type = PT_DYNAMIC;
176 program_headers[PH_DYNAMIC].p_flags = PF_R | PF_W;
177
178 // Get the dynstr string.
179 std::string dynstr(dynsym_builder_.GenerateStrtab());
180
181 // Add the SONAME to the dynstr.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 uint32_t dynstr_soname_offset = dynstr.size();
183 std::string file_name(elf_file_->GetPath());
184 size_t directory_separator_pos = file_name.rfind('/');
185 if (directory_separator_pos != std::string::npos) {
186 file_name = file_name.substr(directory_separator_pos + 1);
187 }
188 dynstr += file_name;
189 dynstr += '\0';
Brian Carlstromb12f3472014-06-11 14:54:46 -0700190 if (debug_logging_) {
191 LOG(INFO) << "dynstr size (bytes) =" << dynstr.size()
192 << std::hex << " " << dynstr.size();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700193 LOG(INFO) << "dynsym size (elements)=" << dynsym_builder_.GetSize()
194 << std::hex << " " << dynsym_builder_.GetSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 }
196
Brian Carlstromb12f3472014-06-11 14:54:46 -0700197 // get the strtab
198 std::string strtab;
199 if (IncludingDebugSymbols()) {
200 strtab = symtab_builder_.GenerateStrtab();
201 if (debug_logging_) {
202 LOG(INFO) << "strtab size (bytes) =" << strtab.size()
203 << std::hex << " " << strtab.size();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700204 LOG(INFO) << "symtab size (elements) =" << symtab_builder_.GetSize()
205 << std::hex << " " << symtab_builder_.GetSize();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700206 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 }
208
Brian Carlstromb12f3472014-06-11 14:54:46 -0700209 // Get the section header string table.
210 std::vector<Elf32_Shdr*> section_ptrs;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 std::string shstrtab;
212 shstrtab += '\0';
Brian Carlstromb12f3472014-06-11 14:54:46 -0700213
214 // Setup sym_undef
215 Elf32_Shdr null_hdr;
216 memset(&null_hdr, 0, sizeof(null_hdr));
217 null_hdr.sh_type = SHT_NULL;
218 null_hdr.sh_link = SHN_UNDEF;
219 section_ptrs.push_back(&null_hdr);
220
221 uint32_t section_index = 1;
222
223 // setup .dynsym
224 section_ptrs.push_back(&dynsym_builder_.section_);
225 AssignSectionStr(&dynsym_builder_, &shstrtab);
226 dynsym_builder_.section_index_ = section_index++;
227
228 // Setup .dynstr
229 section_ptrs.push_back(&dynsym_builder_.strtab_.section_);
230 AssignSectionStr(&dynsym_builder_.strtab_, &shstrtab);
231 dynsym_builder_.strtab_.section_index_ = section_index++;
232
233 // Setup .hash
234 section_ptrs.push_back(&hash_builder_.section_);
235 AssignSectionStr(&hash_builder_, &shstrtab);
236 hash_builder_.section_index_ = section_index++;
237
238 // Setup .rodata
239 section_ptrs.push_back(&rodata_builder_.section_);
240 AssignSectionStr(&rodata_builder_, &shstrtab);
241 rodata_builder_.section_index_ = section_index++;
242
243 // Setup .text
244 section_ptrs.push_back(&text_builder_.section_);
245 AssignSectionStr(&text_builder_, &shstrtab);
246 text_builder_.section_index_ = section_index++;
247
248 // Setup .dynamic
249 section_ptrs.push_back(&dynamic_builder_.section_);
250 AssignSectionStr(&dynamic_builder_, &shstrtab);
251 dynamic_builder_.section_index_ = section_index++;
252
253 if (IncludingDebugSymbols()) {
254 // Setup .symtab
255 section_ptrs.push_back(&symtab_builder_.section_);
256 AssignSectionStr(&symtab_builder_, &shstrtab);
257 symtab_builder_.section_index_ = section_index++;
258
259 // Setup .strtab
260 section_ptrs.push_back(&symtab_builder_.strtab_.section_);
261 AssignSectionStr(&symtab_builder_.strtab_, &shstrtab);
262 symtab_builder_.strtab_.section_index_ = section_index++;
263 }
264 ElfRawSectionBuilder* it = other_builders_.data();
265 for (uint32_t cnt = 0; cnt < other_builders_.size(); ++it, ++cnt) {
266 // Setup all the other sections.
267 section_ptrs.push_back(&it->section_);
268 AssignSectionStr(it, &shstrtab);
269 it->section_index_ = section_index++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 }
271
Brian Carlstromb12f3472014-06-11 14:54:46 -0700272 // Setup shstrtab
273 section_ptrs.push_back(&shstrtab_builder_.section_);
274 AssignSectionStr(&shstrtab_builder_, &shstrtab);
275 shstrtab_builder_.section_index_ = section_index++;
276
277 if (debug_logging_) {
278 LOG(INFO) << ".shstrtab size (bytes) =" << shstrtab.size()
279 << std::hex << " " << shstrtab.size();
280 LOG(INFO) << "section list size (elements)=" << section_ptrs.size()
281 << std::hex << " " << section_ptrs.size();
Mark Mendellae9fd932014-02-10 16:14:35 -0800282 }
283
Brian Carlstromb12f3472014-06-11 14:54:46 -0700284 // Fill in the hash section.
285 std::vector<Elf32_Word> hash = dynsym_builder_.GenerateHashContents();
286
287 if (debug_logging_) {
288 LOG(INFO) << ".hash size (bytes)=" << hash.size() * sizeof(Elf32_Word)
289 << std::hex << " " << hash.size() * sizeof(Elf32_Word);
Mark Mendellae9fd932014-02-10 16:14:35 -0800290 }
291
Brian Carlstromb12f3472014-06-11 14:54:46 -0700292 Elf32_Word base_offset = sizeof(Elf32_Ehdr) + sizeof(program_headers);
293 std::vector<ElfFilePiece> pieces;
294
295 // Get the layout in the sections.
296 //
297 // Get the layout of the dynsym section.
298 dynsym_builder_.section_.sh_offset = RoundUp(base_offset, dynsym_builder_.section_.sh_addralign);
299 dynsym_builder_.section_.sh_addr = dynsym_builder_.section_.sh_offset;
Brian Carlstrom8758c642014-06-11 14:22:02 -0700300 dynsym_builder_.section_.sh_size = dynsym_builder_.GetSize() * sizeof(Elf32_Sym);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700301 dynsym_builder_.section_.sh_link = dynsym_builder_.GetLink();
302
303 // Get the layout of the dynstr section.
304 dynsym_builder_.strtab_.section_.sh_offset = NextOffset(dynsym_builder_.strtab_.section_,
305 dynsym_builder_.section_);
306 dynsym_builder_.strtab_.section_.sh_addr = dynsym_builder_.strtab_.section_.sh_offset;
307 dynsym_builder_.strtab_.section_.sh_size = dynstr.size();
308 dynsym_builder_.strtab_.section_.sh_link = dynsym_builder_.strtab_.GetLink();
309
310 // Get the layout of the hash section
311 hash_builder_.section_.sh_offset = NextOffset(hash_builder_.section_,
312 dynsym_builder_.strtab_.section_);
313 hash_builder_.section_.sh_addr = hash_builder_.section_.sh_offset;
314 hash_builder_.section_.sh_size = hash.size() * sizeof(Elf32_Word);
315 hash_builder_.section_.sh_link = hash_builder_.GetLink();
316
317 // Get the layout of the rodata section.
318 rodata_builder_.section_.sh_offset = NextOffset(rodata_builder_.section_,
319 hash_builder_.section_);
320 rodata_builder_.section_.sh_addr = rodata_builder_.section_.sh_offset;
321 rodata_builder_.section_.sh_size = rodata_builder_.size_;
322 rodata_builder_.section_.sh_link = rodata_builder_.GetLink();
323
324 // Get the layout of the text section.
325 text_builder_.section_.sh_offset = NextOffset(text_builder_.section_, rodata_builder_.section_);
326 text_builder_.section_.sh_addr = text_builder_.section_.sh_offset;
327 text_builder_.section_.sh_size = text_builder_.size_;
328 text_builder_.section_.sh_link = text_builder_.GetLink();
329 CHECK_ALIGNED(rodata_builder_.section_.sh_offset + rodata_builder_.section_.sh_size, kPageSize);
330
331 // Get the layout of the dynamic section.
332 dynamic_builder_.section_.sh_offset = NextOffset(dynamic_builder_.section_,
333 text_builder_.section_);
334 dynamic_builder_.section_.sh_addr = dynamic_builder_.section_.sh_offset;
Brian Carlstrom8758c642014-06-11 14:22:02 -0700335 dynamic_builder_.section_.sh_size = dynamic_builder_.GetSize() * sizeof(Elf32_Dyn);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700336 dynamic_builder_.section_.sh_link = dynamic_builder_.GetLink();
337
338 Elf32_Shdr prev = dynamic_builder_.section_;
339 if (IncludingDebugSymbols()) {
340 // Get the layout of the symtab section.
341 symtab_builder_.section_.sh_offset = NextOffset(symtab_builder_.section_,
342 dynamic_builder_.section_);
343 symtab_builder_.section_.sh_addr = 0;
344 // Add to leave space for the null symbol.
Brian Carlstrom8758c642014-06-11 14:22:02 -0700345 symtab_builder_.section_.sh_size = symtab_builder_.GetSize() * sizeof(Elf32_Sym);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700346 symtab_builder_.section_.sh_link = symtab_builder_.GetLink();
347
348 // Get the layout of the dynstr section.
349 symtab_builder_.strtab_.section_.sh_offset = NextOffset(symtab_builder_.strtab_.section_,
350 symtab_builder_.section_);
351 symtab_builder_.strtab_.section_.sh_addr = 0;
352 symtab_builder_.strtab_.section_.sh_size = strtab.size();
353 symtab_builder_.strtab_.section_.sh_link = symtab_builder_.strtab_.GetLink();
354
355 prev = symtab_builder_.strtab_.section_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800356 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700357 if (debug_logging_) {
358 LOG(INFO) << "dynsym off=" << dynsym_builder_.section_.sh_offset
359 << " dynsym size=" << dynsym_builder_.section_.sh_size;
360 LOG(INFO) << "dynstr off=" << dynsym_builder_.strtab_.section_.sh_offset
361 << " dynstr size=" << dynsym_builder_.strtab_.section_.sh_size;
362 LOG(INFO) << "hash off=" << hash_builder_.section_.sh_offset
363 << " hash size=" << hash_builder_.section_.sh_size;
364 LOG(INFO) << "rodata off=" << rodata_builder_.section_.sh_offset
365 << " rodata size=" << rodata_builder_.section_.sh_size;
366 LOG(INFO) << "text off=" << text_builder_.section_.sh_offset
367 << " text size=" << text_builder_.section_.sh_size;
368 LOG(INFO) << "dynamic off=" << dynamic_builder_.section_.sh_offset
369 << " dynamic size=" << dynamic_builder_.section_.sh_size;
370 if (IncludingDebugSymbols()) {
371 LOG(INFO) << "symtab off=" << symtab_builder_.section_.sh_offset
372 << " symtab size=" << symtab_builder_.section_.sh_size;
373 LOG(INFO) << "strtab off=" << symtab_builder_.strtab_.section_.sh_offset
374 << " strtab size=" << symtab_builder_.strtab_.section_.sh_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 }
376 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700377 // Get the layout of the extra sections. (This will deal with the debug
378 // sections if they are there)
379 for (auto it = other_builders_.begin(); it != other_builders_.end(); ++it) {
380 it->section_.sh_offset = NextOffset(it->section_, prev);
381 it->section_.sh_addr = 0;
382 it->section_.sh_size = it->GetBuffer()->size();
383 it->section_.sh_link = it->GetLink();
384 pieces.push_back(ElfFilePiece(it->name_, it->section_.sh_offset,
385 it->GetBuffer()->data(), it->GetBuffer()->size()));
386 prev = it->section_;
387 if (debug_logging_) {
388 LOG(INFO) << it->name_ << " off=" << it->section_.sh_offset
389 << " " << it->name_ << " size=" << it->section_.sh_size;
390 }
391 }
392 // Get the layout of the shstrtab section
393 shstrtab_builder_.section_.sh_offset = NextOffset(shstrtab_builder_.section_, prev);
394 shstrtab_builder_.section_.sh_addr = 0;
395 shstrtab_builder_.section_.sh_size = shstrtab.size();
396 shstrtab_builder_.section_.sh_link = shstrtab_builder_.GetLink();
397 if (debug_logging_) {
398 LOG(INFO) << "shstrtab off=" << shstrtab_builder_.section_.sh_offset
399 << " shstrtab size=" << shstrtab_builder_.section_.sh_size;
Mark Mendellae9fd932014-02-10 16:14:35 -0800400 }
401
Brian Carlstromb12f3472014-06-11 14:54:46 -0700402 // The section list comes after come after.
403 Elf32_Word sections_offset = RoundUp(
404 shstrtab_builder_.section_.sh_offset + shstrtab_builder_.section_.sh_size,
405 sizeof(Elf32_Word));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406
Brian Carlstromb12f3472014-06-11 14:54:46 -0700407 // Setup the actual symbol arrays.
408 std::vector<Elf32_Sym> dynsym = dynsym_builder_.GenerateSymtab();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700409 CHECK_EQ(dynsym.size() * sizeof(Elf32_Sym), dynsym_builder_.section_.sh_size);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700410 std::vector<Elf32_Sym> symtab;
411 if (IncludingDebugSymbols()) {
412 symtab = symtab_builder_.GenerateSymtab();
Brian Carlstrom8758c642014-06-11 14:22:02 -0700413 CHECK_EQ(symtab.size() * sizeof(Elf32_Sym), symtab_builder_.section_.sh_size);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700414 }
415
416 // Setup the dynamic section.
417 // This will add the 2 values we cannot know until now time, namely the size
418 // and the soname_offset.
419 std::vector<Elf32_Dyn> dynamic = dynamic_builder_.GetDynamics(dynstr.size(),
420 dynstr_soname_offset);
Brian Carlstrom8758c642014-06-11 14:22:02 -0700421 CHECK_EQ(dynamic.size() * sizeof(Elf32_Dyn), dynamic_builder_.section_.sh_size);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700422
423 // Finish setup of the program headers now that we know the layout of the
424 // whole file.
425 Elf32_Word load_r_size = rodata_builder_.section_.sh_offset + rodata_builder_.section_.sh_size;
426 program_headers[PH_LOAD_R__].p_filesz = load_r_size;
427 program_headers[PH_LOAD_R__].p_memsz = load_r_size;
428 program_headers[PH_LOAD_R__].p_align = rodata_builder_.section_.sh_addralign;
429
430 Elf32_Word load_rx_size = text_builder_.section_.sh_size;
431 program_headers[PH_LOAD_R_X].p_offset = text_builder_.section_.sh_offset;
432 program_headers[PH_LOAD_R_X].p_vaddr = text_builder_.section_.sh_offset;
433 program_headers[PH_LOAD_R_X].p_paddr = text_builder_.section_.sh_offset;
434 program_headers[PH_LOAD_R_X].p_filesz = load_rx_size;
435 program_headers[PH_LOAD_R_X].p_memsz = load_rx_size;
436 program_headers[PH_LOAD_R_X].p_align = text_builder_.section_.sh_addralign;
437
438 program_headers[PH_LOAD_RW_].p_offset = dynamic_builder_.section_.sh_offset;
439 program_headers[PH_LOAD_RW_].p_vaddr = dynamic_builder_.section_.sh_offset;
440 program_headers[PH_LOAD_RW_].p_paddr = dynamic_builder_.section_.sh_offset;
441 program_headers[PH_LOAD_RW_].p_filesz = dynamic_builder_.section_.sh_size;
442 program_headers[PH_LOAD_RW_].p_memsz = dynamic_builder_.section_.sh_size;
443 program_headers[PH_LOAD_RW_].p_align = dynamic_builder_.section_.sh_addralign;
444
445 program_headers[PH_DYNAMIC].p_offset = dynamic_builder_.section_.sh_offset;
446 program_headers[PH_DYNAMIC].p_vaddr = dynamic_builder_.section_.sh_offset;
447 program_headers[PH_DYNAMIC].p_paddr = dynamic_builder_.section_.sh_offset;
448 program_headers[PH_DYNAMIC].p_filesz = dynamic_builder_.section_.sh_size;
449 program_headers[PH_DYNAMIC].p_memsz = dynamic_builder_.section_.sh_size;
450 program_headers[PH_DYNAMIC].p_align = dynamic_builder_.section_.sh_addralign;
451
452 // Finish setup of the Ehdr values.
453 elf_header_.e_phoff = phdr_offset;
454 elf_header_.e_shoff = sections_offset;
455 elf_header_.e_phnum = PH_NUM;
456 elf_header_.e_shnum = section_ptrs.size();
457 elf_header_.e_shstrndx = shstrtab_builder_.section_index_;
458
459 // Add the rest of the pieces to the list.
460 pieces.push_back(ElfFilePiece("Elf Header", 0, &elf_header_, sizeof(elf_header_)));
461 pieces.push_back(ElfFilePiece("Program headers", phdr_offset,
462 &program_headers, sizeof(program_headers)));
463 pieces.push_back(ElfFilePiece(".dynamic", dynamic_builder_.section_.sh_offset,
464 dynamic.data(), dynamic_builder_.section_.sh_size));
465 pieces.push_back(ElfFilePiece(".dynsym", dynsym_builder_.section_.sh_offset,
Brian Carlstrom8758c642014-06-11 14:22:02 -0700466 dynsym.data(), dynsym.size() * sizeof(Elf32_Sym)));
Brian Carlstromb12f3472014-06-11 14:54:46 -0700467 pieces.push_back(ElfFilePiece(".dynstr", dynsym_builder_.strtab_.section_.sh_offset,
468 dynstr.c_str(), dynstr.size()));
469 pieces.push_back(ElfFilePiece(".hash", hash_builder_.section_.sh_offset,
470 hash.data(), hash.size() * sizeof(Elf32_Word)));
471 pieces.push_back(ElfFilePiece(".rodata", rodata_builder_.section_.sh_offset,
472 NULL, rodata_builder_.section_.sh_size));
473 pieces.push_back(ElfFilePiece(".text", text_builder_.section_.sh_offset,
474 NULL, text_builder_.section_.sh_size));
475 if (IncludingDebugSymbols()) {
476 pieces.push_back(ElfFilePiece(".symtab", symtab_builder_.section_.sh_offset,
477 symtab.data(), symtab.size() * sizeof(Elf32_Sym)));
478 pieces.push_back(ElfFilePiece(".strtab", symtab_builder_.strtab_.section_.sh_offset,
479 strtab.c_str(), strtab.size()));
480 }
481 pieces.push_back(ElfFilePiece(".shstrtab", shstrtab_builder_.section_.sh_offset,
482 &shstrtab[0], shstrtab.size()));
483 for (uint32_t i = 0; i < section_ptrs.size(); ++i) {
484 // Just add all the sections in induvidually since they are all over the
485 // place on the heap/stack.
486 Elf32_Word cur_off = sections_offset + i * sizeof(Elf32_Shdr);
487 pieces.push_back(ElfFilePiece("section table piece", cur_off,
488 section_ptrs[i], sizeof(Elf32_Shdr)));
489 }
490
491 if (!WriteOutFile(pieces)) {
492 LOG(ERROR) << "Unable to write to file " << elf_file_->GetPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 return false;
494 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700495 // write out the actual oat file data.
496 Elf32_Word oat_data_offset = rodata_builder_.section_.sh_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 if (static_cast<off_t>(oat_data_offset) != lseek(elf_file_->Fd(), oat_data_offset, SEEK_SET)) {
498 PLOG(ERROR) << "Failed to seek to .rodata offset " << oat_data_offset
499 << " for " << elf_file_->GetPath();
500 return false;
501 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700502 std::unique_ptr<BufferedOutputStream> output_stream(
503 new BufferedOutputStream(new FileOutputStream(elf_file_)));
504 if (!oat_writer_->Write(output_stream.get())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 PLOG(ERROR) << "Failed to write .rodata and .text for " << elf_file_->GetPath();
506 return false;
507 }
508
Brian Carlstrom35f72252014-06-11 21:24:53 +0000509 return true;
Brian Carlstromb12f3472014-06-11 14:54:46 -0700510}
511
512bool ElfWriterQuick::ElfBuilder::WriteOutFile(const std::vector<ElfFilePiece>& pieces) {
513 // TODO It would be nice if this checked for overlap.
514 for (auto it = pieces.begin(); it != pieces.end(); ++it) {
515 if (it->data_) {
516 if (static_cast<off_t>(it->offset_) != lseek(elf_file_->Fd(), it->offset_, SEEK_SET)) {
517 PLOG(ERROR) << "Failed to seek to " << it->dbg_name_ << " offset location "
518 << it->offset_ << " for " << elf_file_->GetPath();
519 return false;
520 }
521 if (!elf_file_->WriteFully(it->data_, it->size_)) {
522 PLOG(ERROR) << "Failed to write " << it->dbg_name_ << " for " << elf_file_->GetPath();
523 return false;
524 }
525 }
526 }
527 return true;
528}
529
530void ElfWriterQuick::ElfBuilder::SetupDynamic() {
531 dynamic_builder_.AddDynamicTag(DT_HASH, 0, &hash_builder_);
532 dynamic_builder_.AddDynamicTag(DT_STRTAB, 0, &dynsym_builder_.strtab_);
533 dynamic_builder_.AddDynamicTag(DT_SYMTAB, 0, &dynsym_builder_);
534 dynamic_builder_.AddDynamicTag(DT_SYMENT, sizeof(Elf32_Sym));
535}
536
537void ElfWriterQuick::ElfBuilder::SetupRequiredSymbols() {
538 dynsym_builder_.AddSymbol("oatdata", &rodata_builder_, 0, true,
539 rodata_builder_.size_, STB_GLOBAL, STT_OBJECT);
540 dynsym_builder_.AddSymbol("oatexec", &text_builder_, 0, true,
541 text_builder_.size_, STB_GLOBAL, STT_OBJECT);
542 dynsym_builder_.AddSymbol("oatlastword", &text_builder_, text_builder_.size_ - 4,
543 true, 4, STB_GLOBAL, STT_OBJECT);
544}
545
Brian Carlstrom8758c642014-06-11 14:22:02 -0700546void ElfWriterQuick::ElfDynamicBuilder::AddDynamicTag(Elf32_Sword tag, Elf32_Word d_un) {
Brian Carlstromb12f3472014-06-11 14:54:46 -0700547 if (tag == DT_NULL) {
548 return;
549 }
550 dynamics_.push_back({NULL, tag, d_un});
551}
552
Brian Carlstrom8758c642014-06-11 14:22:02 -0700553void ElfWriterQuick::ElfDynamicBuilder::AddDynamicTag(Elf32_Sword tag, Elf32_Word d_un,
Brian Carlstromb12f3472014-06-11 14:54:46 -0700554 ElfSectionBuilder* section) {
555 if (tag == DT_NULL) {
556 return;
557 }
558 dynamics_.push_back({section, tag, d_un});
559}
560
Brian Carlstrom8758c642014-06-11 14:22:02 -0700561std::vector<Elf32_Dyn> ElfWriterQuick::ElfDynamicBuilder::GetDynamics(Elf32_Word strsz,
562 Elf32_Word soname) {
Brian Carlstromb12f3472014-06-11 14:54:46 -0700563 std::vector<Elf32_Dyn> ret;
564 for (auto it = dynamics_.cbegin(); it != dynamics_.cend(); ++it) {
565 if (it->section_) {
566 // We are adding an address relative to a section.
567 ret.push_back(
Brian Carlstrom8758c642014-06-11 14:22:02 -0700568 {it->tag_, {it->off_ + it->section_->section_.sh_addr}});
Brian Carlstromb12f3472014-06-11 14:54:46 -0700569 } else {
570 ret.push_back({it->tag_, {it->off_}});
571 }
572 }
573 ret.push_back({DT_STRSZ, {strsz}});
574 ret.push_back({DT_SONAME, {soname}});
575 ret.push_back({DT_NULL, {0}});
576 return ret;
577}
578
579std::vector<Elf32_Sym> ElfWriterQuick::ElfSymtabBuilder::GenerateSymtab() {
580 std::vector<Elf32_Sym> ret;
581 Elf32_Sym undef_sym;
582 memset(&undef_sym, 0, sizeof(undef_sym));
583 undef_sym.st_shndx = SHN_UNDEF;
584 ret.push_back(undef_sym);
585
586 for (auto it = symbols_.cbegin(); it != symbols_.cend(); ++it) {
587 Elf32_Sym sym;
588 memset(&sym, 0, sizeof(sym));
589 sym.st_name = it->name_idx_;
590 if (it->is_relative_) {
591 sym.st_value = it->addr_ + it->section_->section_.sh_offset;
592 } else {
593 sym.st_value = it->addr_;
594 }
595 sym.st_size = it->size_;
596 sym.st_other = it->other_;
597 sym.st_shndx = it->section_->section_index_;
598 sym.st_info = it->info_;
599
600 ret.push_back(sym);
601 }
602 return ret;
603}
604
605std::string ElfWriterQuick::ElfSymtabBuilder::GenerateStrtab() {
606 std::string tab;
607 tab += '\0';
608 for (auto it = symbols_.begin(); it != symbols_.end(); ++it) {
609 it->name_idx_ = tab.size();
610 tab += it->name_;
611 tab += '\0';
612 }
613 strtab_.section_.sh_size = tab.size();
614 return tab;
615}
616
617void ElfWriterQuick::ElfBuilder::AssignSectionStr(
618 ElfSectionBuilder* builder, std::string* strtab) {
619 builder->section_.sh_name = strtab->size();
620 *strtab += builder->name_;
621 *strtab += '\0';
622 if (debug_logging_) {
623 LOG(INFO) << "adding section name \"" << builder->name_ << "\" "
624 << "to shstrtab at offset " << builder->section_.sh_name;
625 }
626}
627
628// from bionic
629static unsigned elfhash(const char *_name) {
630 const unsigned char *name = (const unsigned char *) _name;
631 unsigned h = 0, g;
632
633 while (*name) {
634 h = (h << 4) + *name++;
635 g = h & 0xf0000000;
636 h ^= g;
637 h ^= g >> 24;
638 }
639 return h;
640}
641
642
643std::vector<Elf32_Word> ElfWriterQuick::ElfSymtabBuilder::GenerateHashContents() {
644 // Here is how The ELF hash table works.
645 // There are 3 arrays to worry about.
646 // * The symbol table where the symbol information is.
647 // * The bucket array which is an array of indexes into the symtab and chain.
648 // * The chain array which is also an array of indexes into the symtab and chain.
649 //
650 // Lets say the state is something like this.
651 // +--------+ +--------+ +-----------+
652 // | symtab | | bucket | | chain |
653 // | NULL | | 1 | | STN_UNDEF |
654 // | <sym1> | | 4 | | 2 |
655 // | <sym2> | | | | 5 |
656 // | <sym3> | | | | STN_UNDEF |
657 // | <sym4> | | | | 3 |
658 // | <sym5> | | | | STN_UNDEF |
659 // +--------+ +--------+ +-----------+
660 //
661 // The lookup process (in python psudocode) is
662 //
663 // def GetSym(name):
664 // # NB STN_UNDEF == 0
665 // indx = bucket[elfhash(name) % num_buckets]
666 // while indx != STN_UNDEF:
667 // if GetSymbolName(symtab[indx]) == name:
668 // return symtab[indx]
669 // indx = chain[indx]
670 // return SYMBOL_NOT_FOUND
671 //
672 // Between bucket and chain arrays every symtab index must be present exactly
673 // once (except for STN_UNDEF, which must be present 1 + num_bucket times).
674
675 // Select number of buckets.
676 // This is essentially arbitrary.
677 Elf32_Word nbuckets;
Brian Carlstrom8758c642014-06-11 14:22:02 -0700678 Elf32_Word chain_size = GetSize();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700679 if (symbols_.size() < 8) {
680 nbuckets = 2;
681 } else if (symbols_.size() < 32) {
682 nbuckets = 4;
683 } else if (symbols_.size() < 256) {
684 nbuckets = 16;
685 } else {
686 // Have about 32 ids per bucket.
687 nbuckets = RoundUp(symbols_.size()/32, 2);
688 }
689 std::vector<Elf32_Word> hash;
690 hash.push_back(nbuckets);
691 hash.push_back(chain_size);
692 uint32_t bucket_offset = hash.size();
693 uint32_t chain_offset = bucket_offset + nbuckets;
694 hash.resize(hash.size() + nbuckets + chain_size, 0);
695
696 Elf32_Word* buckets = hash.data() + bucket_offset;
697 Elf32_Word* chain = hash.data() + chain_offset;
698
699 // Set up the actual hash table.
700 for (Elf32_Word i = 0; i < symbols_.size(); i++) {
701 // Add 1 since we need to have the null symbol that is not in the symbols
702 // list.
703 Elf32_Word index = i + 1;
704 Elf32_Word hash_val = static_cast<Elf32_Word>(elfhash(symbols_[i].name_.c_str())) % nbuckets;
705 if (buckets[hash_val] == 0) {
706 buckets[hash_val] = index;
707 } else {
708 hash_val = buckets[hash_val];
709 CHECK_LT(hash_val, chain_size);
710 while (chain[hash_val] != 0) {
711 hash_val = chain[hash_val];
712 CHECK_LT(hash_val, chain_size);
713 }
714 chain[hash_val] = index;
715 // Check for loops. Works because if this is non-empty then there must be
716 // another cell which already contains the same symbol index as this one,
717 // which means some symbol has more then one name, which isn't allowed.
718 CHECK_EQ(chain[index], static_cast<Elf32_Word>(0));
719 }
720 }
721
722 return hash;
723}
724
725void ElfWriterQuick::ElfBuilder::SetupEhdr() {
726 memset(&elf_header_, 0, sizeof(elf_header_));
727 elf_header_.e_ident[EI_MAG0] = ELFMAG0;
728 elf_header_.e_ident[EI_MAG1] = ELFMAG1;
729 elf_header_.e_ident[EI_MAG2] = ELFMAG2;
730 elf_header_.e_ident[EI_MAG3] = ELFMAG3;
731 elf_header_.e_ident[EI_CLASS] = ELFCLASS32;
732 elf_header_.e_ident[EI_DATA] = ELFDATA2LSB;
733 elf_header_.e_ident[EI_VERSION] = EV_CURRENT;
734 elf_header_.e_ident[EI_OSABI] = ELFOSABI_LINUX;
735 elf_header_.e_ident[EI_ABIVERSION] = 0;
736 elf_header_.e_type = ET_DYN;
737 elf_header_.e_version = 1;
738 elf_header_.e_entry = 0;
739 elf_header_.e_ehsize = sizeof(Elf32_Ehdr);
740 elf_header_.e_phentsize = sizeof(Elf32_Phdr);
741 elf_header_.e_shentsize = sizeof(Elf32_Shdr);
742 elf_header_.e_phoff = sizeof(Elf32_Ehdr);
743}
744
745void ElfWriterQuick::ElfBuilder::SetISA(InstructionSet isa) {
746 switch (isa) {
747 case kArm:
748 // Fall through.
749 case kThumb2: {
750 elf_header_.e_machine = EM_ARM;
751 elf_header_.e_flags = EF_ARM_EABI_VER5;
752 break;
753 }
754 case kArm64: {
755 elf_header_.e_machine = EM_AARCH64;
756 elf_header_.e_flags = 0;
757 break;
758 }
759 case kX86: {
760 elf_header_.e_machine = EM_386;
761 elf_header_.e_flags = 0;
762 break;
763 }
764 case kX86_64: {
765 elf_header_.e_machine = EM_X86_64;
766 elf_header_.e_flags = 0;
767 break;
768 }
769 case kMips: {
770 elf_header_.e_machine = EM_MIPS;
771 elf_header_.e_flags = (EF_MIPS_NOREORDER |
772 EF_MIPS_PIC |
773 EF_MIPS_CPIC |
774 EF_MIPS_ABI_O32 |
775 EF_MIPS_ARCH_32R2);
776 break;
777 }
778 default: {
779 fatal_error_ = true;
780 LOG(FATAL) << "Unknown instruction set: " << isa;
781 break;
782 }
783 }
784}
785
786void ElfWriterQuick::ElfSymtabBuilder::AddSymbol(
787 const std::string& name, const ElfSectionBuilder* section, Elf32_Addr addr,
788 bool is_relative, Elf32_Word size, uint8_t binding, uint8_t type, uint8_t other) {
789 CHECK(section);
790 ElfSymtabBuilder::ElfSymbolState state {name, section, addr, size, is_relative,
791 MakeStInfo(binding, type), other, 0};
792 symbols_.push_back(state);
793}
794
795bool ElfWriterQuick::Create(File* elf_file,
796 OatWriter* oat_writer,
797 const std::vector<const DexFile*>& dex_files,
798 const std::string& android_root,
799 bool is_host,
800 const CompilerDriver& driver) {
801 ElfWriterQuick elf_writer(driver, elf_file);
802 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
803}
804
805bool ElfWriterQuick::Write(OatWriter* oat_writer,
806 const std::vector<const DexFile*>& dex_files_unused,
807 const std::string& android_root_unused,
808 bool is_host_unused) {
809 const bool debug = false;
Alex Light78382fa2014-06-06 15:45:32 -0700810 const bool add_symbols = oat_writer->DidAddSymbols();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700811 const OatHeader& oat_header = oat_writer->GetOatHeader();
812 Elf32_Word oat_data_size = oat_header.GetExecutableOffset();
813 uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size;
814
815 ElfBuilder builder(oat_writer, elf_file_, compiler_driver_->GetInstructionSet(), 0,
Alex Light78382fa2014-06-06 15:45:32 -0700816 oat_data_size, oat_data_size, oat_exec_size, add_symbols, debug);
817
818 if (add_symbols) {
819 AddDebugSymbols(builder, oat_writer, debug);
820 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700821
822 bool generateDebugInformation = compiler_driver_->GetCallFrameInformation() != nullptr;
823 if (generateDebugInformation) {
824 ElfRawSectionBuilder debug_info(".debug_info", SHT_PROGBITS, 0, NULL, 0, 1, 0);
825 ElfRawSectionBuilder debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, NULL, 0, 1, 0);
826 ElfRawSectionBuilder debug_str(".debug_str", SHT_PROGBITS, 0, NULL, 0, 1, 0);
827 ElfRawSectionBuilder debug_frame(".debug_frame", SHT_PROGBITS, 0, NULL, 0, 4, 0);
828 debug_frame.SetBuffer(*compiler_driver_->GetCallFrameInformation());
829
830 FillInCFIInformation(oat_writer, debug_info.GetBuffer(),
831 debug_abbrev.GetBuffer(), debug_str.GetBuffer());
832 builder.RegisterRawSection(debug_info);
833 builder.RegisterRawSection(debug_abbrev);
834 builder.RegisterRawSection(debug_frame);
835 builder.RegisterRawSection(debug_str);
836 }
837
838 return builder.Write();
839}
Mark Mendellae9fd932014-02-10 16:14:35 -0800840
Alex Light78382fa2014-06-06 15:45:32 -0700841void ElfWriterQuick::AddDebugSymbols(ElfBuilder& builder, OatWriter* oat_writer, bool debug) {
842 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetCFIMethodInfo();
843 ElfSymtabBuilder* symtab = &builder.symtab_builder_;
844 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
845 symtab->AddSymbol(it->method_name_, &builder.text_builder_, it->low_pc_, true,
846 it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
847 }
848}
849
Mark Mendellae9fd932014-02-10 16:14:35 -0800850static void UpdateWord(std::vector<uint8_t>*buf, int offset, int data) {
851 (*buf)[offset+0] = data;
852 (*buf)[offset+1] = data >> 8;
853 (*buf)[offset+2] = data >> 16;
854 (*buf)[offset+3] = data >> 24;
855}
856
857static void PushWord(std::vector<uint8_t>*buf, int data) {
858 buf->push_back(data & 0xff);
859 buf->push_back((data >> 8) & 0xff);
860 buf->push_back((data >> 16) & 0xff);
861 buf->push_back((data >> 24) & 0xff);
862}
863
864static void PushHalf(std::vector<uint8_t>*buf, int data) {
865 buf->push_back(data & 0xff);
866 buf->push_back((data >> 8) & 0xff);
867}
868
869// DWARF constants needed to generate CFI information.
870enum {
871 // Tag encodings.
872 DW_TAG_compile_unit = 0x11,
873 DW_TAG_subprogram = 0X2e,
874
875 // Attribute encodings.
876 DW_AT_name = 0x03,
877 DW_AT_low_pc = 0x11,
878 DW_AT_high_pc = 0x12,
879 DW_AT_language = 0x13,
880
881 // Constant encoding.
882 DW_CHILDREN_no = 0x00,
883 DW_CHILDREN_yes = 0x01,
884
885 // Attribute form encodings.
886 DW_FORM_addr = 0x01,
887 DW_FORM_data1 = 0x0b,
888 DW_FORM_strp = 0x0e,
889
890 // Language encoding.
891 DW_LANG_Java = 0x000b
892};
893
894void ElfWriterQuick::FillInCFIInformation(OatWriter* oat_writer,
895 std::vector<uint8_t>* dbg_info,
896 std::vector<uint8_t>* dbg_abbrev,
897 std::vector<uint8_t>* dbg_str) {
898 // Create the debug_abbrev section with boilerplate information.
899 // We only care about low_pc and high_pc right now for the compilation
900 // unit and methods.
901
902 // Tag 1: Compilation unit: DW_TAG_compile_unit.
903 dbg_abbrev->push_back(1);
904 dbg_abbrev->push_back(DW_TAG_compile_unit);
905
906 // There are children (the methods).
907 dbg_abbrev->push_back(DW_CHILDREN_yes);
908
909 // DW_LANG_Java DW_FORM_data1.
910 dbg_abbrev->push_back(DW_AT_language);
911 dbg_abbrev->push_back(DW_FORM_data1);
912
913 // DW_AT_low_pc DW_FORM_addr.
914 dbg_abbrev->push_back(DW_AT_low_pc);
915 dbg_abbrev->push_back(DW_FORM_addr);
916
917 // DW_AT_high_pc DW_FORM_addr.
918 dbg_abbrev->push_back(DW_AT_high_pc);
919 dbg_abbrev->push_back(DW_FORM_addr);
920
921 // End of DW_TAG_compile_unit.
922 PushHalf(dbg_abbrev, 0);
923
924 // Tag 2: Compilation unit: DW_TAG_subprogram.
925 dbg_abbrev->push_back(2);
926 dbg_abbrev->push_back(DW_TAG_subprogram);
927
928 // There are no children.
929 dbg_abbrev->push_back(DW_CHILDREN_no);
930
931 // Name of the method.
932 dbg_abbrev->push_back(DW_AT_name);
933 dbg_abbrev->push_back(DW_FORM_strp);
934
935 // DW_AT_low_pc DW_FORM_addr.
936 dbg_abbrev->push_back(DW_AT_low_pc);
937 dbg_abbrev->push_back(DW_FORM_addr);
938
939 // DW_AT_high_pc DW_FORM_addr.
940 dbg_abbrev->push_back(DW_AT_high_pc);
941 dbg_abbrev->push_back(DW_FORM_addr);
942
943 // End of DW_TAG_subprogram.
944 PushHalf(dbg_abbrev, 0);
945
946 // Start the debug_info section with the header information
947 // 'unit_length' will be filled in later.
948 PushWord(dbg_info, 0);
949
950 // 'version' - 3.
951 PushHalf(dbg_info, 3);
952
953 // Offset into .debug_abbrev section (always 0).
954 PushWord(dbg_info, 0);
955
956 // Address size: 4.
957 dbg_info->push_back(4);
958
959 // Start the description for the compilation unit.
960 // This uses tag 1.
961 dbg_info->push_back(1);
962
963 // The language is Java.
964 dbg_info->push_back(DW_LANG_Java);
965
966 // Leave space for low_pc and high_pc.
967 int low_pc_offset = dbg_info->size();
968 PushWord(dbg_info, 0);
969 PushWord(dbg_info, 0);
970
971 // Walk through the information in the method table, and enter into dbg_info.
972 const std::vector<OatWriter::DebugInfo>& dbg = oat_writer->GetCFIMethodInfo();
973 uint32_t low_pc = 0xFFFFFFFFU;
974 uint32_t high_pc = 0;
975
976 for (uint32_t i = 0; i < dbg.size(); i++) {
977 const OatWriter::DebugInfo& info = dbg[i];
978 if (info.low_pc_ < low_pc) {
979 low_pc = info.low_pc_;
980 }
981 if (info.high_pc_ > high_pc) {
982 high_pc = info.high_pc_;
983 }
984
985 // Start a new TAG: subroutine (2).
986 dbg_info->push_back(2);
987
988 // Enter the name into the string table (and NUL terminate).
989 uint32_t str_offset = dbg_str->size();
990 dbg_str->insert(dbg_str->end(), info.method_name_.begin(), info.method_name_.end());
991 dbg_str->push_back('\0');
992
993 // Enter name, low_pc, high_pc.
994 PushWord(dbg_info, str_offset);
995 PushWord(dbg_info, info.low_pc_);
996 PushWord(dbg_info, info.high_pc_);
997 }
998
999 // One byte terminator
1000 dbg_info->push_back(0);
1001
1002 // We have now walked all the methods. Fill in lengths and low/high PCs.
1003 UpdateWord(dbg_info, 0, dbg_info->size() - 4);
1004 UpdateWord(dbg_info, low_pc_offset, low_pc);
1005 UpdateWord(dbg_info, low_pc_offset + 4, high_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006}
1007
1008} // namespace art