blob: 4b823ef5ec31a58e83a49b24a5368dda24e2b938 [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 Carlstrom7940e442013-07-12 13:46:57 -070032bool ElfWriterQuick::Create(File* elf_file,
Ian Rogers3d504072014-03-01 09:16:49 -080033 OatWriter* oat_writer,
Brian Carlstrom7940e442013-07-12 13:46:57 -070034 const std::vector<const DexFile*>& dex_files,
35 const std::string& android_root,
36 bool is_host,
37 const CompilerDriver& driver) {
38 ElfWriterQuick elf_writer(driver, elf_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070039 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
Brian Carlstrom7940e442013-07-12 13:46:57 -070040}
41
Ian Rogers3d504072014-03-01 09:16:49 -080042bool ElfWriterQuick::Write(OatWriter* oat_writer,
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 const std::vector<const DexFile*>& dex_files_unused,
44 const std::string& android_root_unused,
45 bool is_host_unused) {
46 const bool debug = false;
47 // +-------------------------+
48 // | Elf32_Ehdr |
49 // +-------------------------+
50 // | Elf32_Phdr PHDR |
51 // | Elf32_Phdr LOAD R | .dynsym .dynstr .hash .rodata
52 // | Elf32_Phdr LOAD R X | .text
53 // | Elf32_Phdr LOAD RW | .dynamic
54 // | Elf32_Phdr DYNAMIC | .dynamic
55 // +-------------------------+
56 // | .dynsym |
57 // | Elf32_Sym STN_UNDEF |
58 // | Elf32_Sym oatdata |
59 // | Elf32_Sym oatexec |
60 // | Elf32_Sym oatlastword |
61 // +-------------------------+
62 // | .dynstr |
63 // | \0 |
64 // | oatdata\0 |
65 // | oatexec\0 |
66 // | oatlastword\0 |
67 // | boot.oat\0 |
68 // +-------------------------+
69 // | .hash |
70 // | Elf32_Word nbucket = 1 |
71 // | Elf32_Word nchain = 3 |
72 // | Elf32_Word bucket[0] = 0|
73 // | Elf32_Word chain[0] = 1|
74 // | Elf32_Word chain[1] = 2|
75 // | Elf32_Word chain[2] = 3|
76 // +-------------------------+
77 // | .rodata |
78 // | oatdata..oatexec-4 |
79 // +-------------------------+
80 // | .text |
81 // | oatexec..oatlastword |
82 // +-------------------------+
83 // | .dynamic |
84 // | Elf32_Dyn DT_SONAME |
85 // | Elf32_Dyn DT_HASH |
86 // | Elf32_Dyn DT_SYMTAB |
87 // | Elf32_Dyn DT_SYMENT |
88 // | Elf32_Dyn DT_STRTAB |
89 // | Elf32_Dyn DT_STRSZ |
90 // | Elf32_Dyn DT_NULL |
91 // +-------------------------+
92 // | .shstrtab |
93 // | \0 |
94 // | .dynamic\0 |
95 // | .dynsym\0 |
96 // | .dynstr\0 |
97 // | .hash\0 |
98 // | .rodata\0 |
99 // | .text\0 |
100 // | .shstrtab\0 |
Mark Mendellae9fd932014-02-10 16:14:35 -0800101 // | .debug_frame\0 |
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 // +-------------------------+
103 // | Elf32_Shdr NULL |
104 // | Elf32_Shdr .dynsym |
105 // | Elf32_Shdr .dynstr |
106 // | Elf32_Shdr .hash |
107 // | Elf32_Shdr .text |
108 // | Elf32_Shdr .rodata |
109 // | Elf32_Shdr .dynamic |
110 // | Elf32_Shdr .shstrtab |
Mark Mendellae9fd932014-02-10 16:14:35 -0800111 // | Elf32_Shdr .debug_info | (Optional)
112 // | Elf32_Shdr .debug_abbrev| (Optional)
113 // | Elf32_Shdr .debug_frame | (Optional)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 // +-------------------------+
115
116 // phase 1: computing offsets
117 uint32_t expected_offset = 0;
118
119 // Elf32_Ehdr
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000120 expected_offset += sizeof(Elf32_Ehdr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121
122 // PHDR
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000123 uint32_t phdr_alignment = sizeof(Elf32_Word);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 uint32_t phdr_offset = expected_offset;
125 const uint8_t PH_PHDR = 0;
126 const uint8_t PH_LOAD_R__ = 1;
127 const uint8_t PH_LOAD_R_X = 2;
128 const uint8_t PH_LOAD_RW_ = 3;
129 const uint8_t PH_DYNAMIC = 4;
130 const uint8_t PH_NUM = 5;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000131 uint32_t phdr_size = sizeof(Elf32_Phdr) * PH_NUM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 expected_offset += phdr_size;
133 if (debug) {
134 LOG(INFO) << "phdr_offset=" << phdr_offset << std::hex << " " << phdr_offset;
135 LOG(INFO) << "phdr_size=" << phdr_size << std::hex << " " << phdr_size;
136 }
137
138 // .dynsym
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000139 uint32_t dynsym_alignment = sizeof(Elf32_Word);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 uint32_t dynsym_offset = expected_offset = RoundUp(expected_offset, dynsym_alignment);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700141 const uint8_t SYM_UNDEF = 0; // aka STN_UNDEF
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 const uint8_t SYM_OATDATA = 1;
143 const uint8_t SYM_OATEXEC = 2;
144 const uint8_t SYM_OATLASTWORD = 3;
145 const uint8_t SYM_NUM = 4;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000146 uint32_t dynsym_size = sizeof(Elf32_Sym) * SYM_NUM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 expected_offset += dynsym_size;
148 if (debug) {
149 LOG(INFO) << "dynsym_offset=" << dynsym_offset << std::hex << " " << dynsym_offset;
150 LOG(INFO) << "dynsym_size=" << dynsym_size << std::hex << " " << dynsym_size;
151 }
152
153 // .dynstr
154 uint32_t dynstr_alignment = 1;
155 uint32_t dynstr_offset = expected_offset = RoundUp(expected_offset, dynstr_alignment);
156 std::string dynstr;
157 dynstr += '\0';
158 uint32_t dynstr_oatdata_offset = dynstr.size();
159 dynstr += "oatdata";
160 dynstr += '\0';
161 uint32_t dynstr_oatexec_offset = dynstr.size();
162 dynstr += "oatexec";
163 dynstr += '\0';
164 uint32_t dynstr_oatlastword_offset = dynstr.size();
165 dynstr += "oatlastword";
166 dynstr += '\0';
167 uint32_t dynstr_soname_offset = dynstr.size();
168 std::string file_name(elf_file_->GetPath());
169 size_t directory_separator_pos = file_name.rfind('/');
170 if (directory_separator_pos != std::string::npos) {
171 file_name = file_name.substr(directory_separator_pos + 1);
172 }
173 dynstr += file_name;
174 dynstr += '\0';
175 uint32_t dynstr_size = dynstr.size();
176 expected_offset += dynstr_size;
177 if (debug) {
178 LOG(INFO) << "dynstr_offset=" << dynstr_offset << std::hex << " " << dynstr_offset;
179 LOG(INFO) << "dynstr_size=" << dynstr_size << std::hex << " " << dynstr_size;
180 }
181
182 // .hash
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000183 uint32_t hash_alignment = sizeof(Elf32_Word); // Even for 64-bit
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 uint32_t hash_offset = expected_offset = RoundUp(expected_offset, hash_alignment);
185 const uint8_t HASH_NBUCKET = 0;
186 const uint8_t HASH_NCHAIN = 1;
187 const uint8_t HASH_BUCKET0 = 2;
188 const uint8_t HASH_NUM = HASH_BUCKET0 + 1 + SYM_NUM;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000189 uint32_t hash_size = sizeof(Elf32_Word) * HASH_NUM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 expected_offset += hash_size;
191 if (debug) {
192 LOG(INFO) << "hash_offset=" << hash_offset << std::hex << " " << hash_offset;
193 LOG(INFO) << "hash_size=" << hash_size << std::hex << " " << hash_size;
194 }
195
196 // .rodata
197 uint32_t oat_data_alignment = kPageSize;
198 uint32_t oat_data_offset = expected_offset = RoundUp(expected_offset, oat_data_alignment);
Ian Rogers3d504072014-03-01 09:16:49 -0800199 const OatHeader& oat_header = oat_writer->GetOatHeader();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700200 CHECK(oat_header.IsValid());
201 uint32_t oat_data_size = oat_header.GetExecutableOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 expected_offset += oat_data_size;
203 if (debug) {
204 LOG(INFO) << "oat_data_offset=" << oat_data_offset << std::hex << " " << oat_data_offset;
205 LOG(INFO) << "oat_data_size=" << oat_data_size << std::hex << " " << oat_data_size;
206 }
207
208 // .text
209 uint32_t oat_exec_alignment = kPageSize;
210 CHECK_ALIGNED(expected_offset, kPageSize);
211 uint32_t oat_exec_offset = expected_offset = RoundUp(expected_offset, oat_exec_alignment);
Ian Rogers3d504072014-03-01 09:16:49 -0800212 uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 expected_offset += oat_exec_size;
Ian Rogers3d504072014-03-01 09:16:49 -0800214 CHECK_EQ(oat_data_offset + oat_writer->GetSize(), expected_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 if (debug) {
216 LOG(INFO) << "oat_exec_offset=" << oat_exec_offset << std::hex << " " << oat_exec_offset;
217 LOG(INFO) << "oat_exec_size=" << oat_exec_size << std::hex << " " << oat_exec_size;
218 }
219
220 // .dynamic
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000221 // alignment would naturally be sizeof(Elf32_Word), but we want this in a new segment
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 uint32_t dynamic_alignment = kPageSize;
223 uint32_t dynamic_offset = expected_offset = RoundUp(expected_offset, dynamic_alignment);
224 const uint8_t DH_SONAME = 0;
225 const uint8_t DH_HASH = 1;
226 const uint8_t DH_SYMTAB = 2;
227 const uint8_t DH_SYMENT = 3;
228 const uint8_t DH_STRTAB = 4;
229 const uint8_t DH_STRSZ = 5;
230 const uint8_t DH_NULL = 6;
231 const uint8_t DH_NUM = 7;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000232 uint32_t dynamic_size = sizeof(Elf32_Dyn) * DH_NUM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 expected_offset += dynamic_size;
234 if (debug) {
235 LOG(INFO) << "dynamic_offset=" << dynamic_offset << std::hex << " " << dynamic_offset;
236 LOG(INFO) << "dynamic_size=" << dynamic_size << std::hex << " " << dynamic_size;
237 }
238
239 // .shstrtab
240 uint32_t shstrtab_alignment = 1;
241 uint32_t shstrtab_offset = expected_offset = RoundUp(expected_offset, shstrtab_alignment);
242 std::string shstrtab;
243 shstrtab += '\0';
244 uint32_t shstrtab_dynamic_offset = shstrtab.size();
245 CHECK_EQ(1U, shstrtab_dynamic_offset);
246 shstrtab += ".dynamic";
247 shstrtab += '\0';
248 uint32_t shstrtab_dynsym_offset = shstrtab.size();
249 shstrtab += ".dynsym";
250 shstrtab += '\0';
251 uint32_t shstrtab_dynstr_offset = shstrtab.size();
252 shstrtab += ".dynstr";
253 shstrtab += '\0';
254 uint32_t shstrtab_hash_offset = shstrtab.size();
255 shstrtab += ".hash";
256 shstrtab += '\0';
257 uint32_t shstrtab_rodata_offset = shstrtab.size();
258 shstrtab += ".rodata";
259 shstrtab += '\0';
260 uint32_t shstrtab_text_offset = shstrtab.size();
261 shstrtab += ".text";
262 shstrtab += '\0';
263 uint32_t shstrtab_shstrtab_offset = shstrtab.size();
264 shstrtab += ".shstrtab";
265 shstrtab += '\0';
Mark Mendellae9fd932014-02-10 16:14:35 -0800266 uint32_t shstrtab_debug_info_offset = shstrtab.size();
267 shstrtab += ".debug_info";
268 shstrtab += '\0';
269 uint32_t shstrtab_debug_abbrev_offset = shstrtab.size();
270 shstrtab += ".debug_abbrev";
271 shstrtab += '\0';
272 uint32_t shstrtab_debug_str_offset = shstrtab.size();
273 shstrtab += ".debug_str";
274 shstrtab += '\0';
275 uint32_t shstrtab_debug_frame_offset = shstrtab.size();
276 shstrtab += ".debug_frame";
277 shstrtab += '\0';
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 uint32_t shstrtab_size = shstrtab.size();
279 expected_offset += shstrtab_size;
280 if (debug) {
281 LOG(INFO) << "shstrtab_offset=" << shstrtab_offset << std::hex << " " << shstrtab_offset;
282 LOG(INFO) << "shstrtab_size=" << shstrtab_size << std::hex << " " << shstrtab_size;
283 }
284
Mark Mendellae9fd932014-02-10 16:14:35 -0800285 // Create debug informatin, if we have it.
286 bool generateDebugInformation = compiler_driver_->GetCallFrameInformation() != nullptr;
287 std::vector<uint8_t> dbg_info;
288 std::vector<uint8_t> dbg_abbrev;
289 std::vector<uint8_t> dbg_str;
290 if (generateDebugInformation) {
291 FillInCFIInformation(oat_writer, &dbg_info, &dbg_abbrev, &dbg_str);
292 }
293
294 uint32_t shdbg_info_alignment = 1;
295 uint32_t shdbg_info_offset = expected_offset;
296 uint32_t shdbg_info_size = dbg_info.size();
297 expected_offset += shdbg_info_size;
298 if (debug) {
299 LOG(INFO) << "shdbg_info_offset=" << shdbg_info_offset << std::hex << " " << shdbg_info_offset;
300 LOG(INFO) << "shdbg_info_size=" << shdbg_info_size << std::hex << " " << shdbg_info_size;
301 }
302
303 uint32_t shdbg_abbrev_alignment = 1;
304 uint32_t shdbg_abbrev_offset = expected_offset;
305 uint32_t shdbg_abbrev_size = dbg_abbrev.size();
306 expected_offset += shdbg_abbrev_size;
307 if (debug) {
308 LOG(INFO) << "shdbg_abbrev_offset=" << shdbg_abbrev_offset << std::hex << " " << shdbg_abbrev_offset;
309 LOG(INFO) << "shdbg_abbrev_size=" << shdbg_abbrev_size << std::hex << " " << shdbg_abbrev_size;
310 }
311
312 uint32_t shdbg_frm_alignment = 4;
313 uint32_t shdbg_frm_offset = expected_offset = RoundUp(expected_offset, shdbg_frm_alignment);
314 uint32_t shdbg_frm_size =
315 generateDebugInformation ? compiler_driver_->GetCallFrameInformation()->size() : 0;
316 expected_offset += shdbg_frm_size;
317 if (debug) {
318 LOG(INFO) << "shdbg_frm_offset=" << shdbg_frm_offset << std::hex << " " << shdbg_frm_offset;
319 LOG(INFO) << "shdbg_frm_size=" << shdbg_frm_size << std::hex << " " << shdbg_frm_size;
320 }
321
322 uint32_t shdbg_str_alignment = 1;
323 uint32_t shdbg_str_offset = expected_offset;
324 uint32_t shdbg_str_size = dbg_str.size();
325 expected_offset += shdbg_str_size;
326 if (debug) {
327 LOG(INFO) << "shdbg_str_offset=" << shdbg_str_offset << std::hex << " " << shdbg_str_offset;
328 LOG(INFO) << "shdbg_str_size=" << shdbg_str_size << std::hex << " " << shdbg_str_size;
329 }
330
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 // section headers (after all sections)
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000332 uint32_t shdr_alignment = sizeof(Elf32_Word);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 uint32_t shdr_offset = expected_offset = RoundUp(expected_offset, shdr_alignment);
334 const uint8_t SH_NULL = 0;
335 const uint8_t SH_DYNSYM = 1;
336 const uint8_t SH_DYNSTR = 2;
337 const uint8_t SH_HASH = 3;
338 const uint8_t SH_RODATA = 4;
339 const uint8_t SH_TEXT = 5;
340 const uint8_t SH_DYNAMIC = 6;
341 const uint8_t SH_SHSTRTAB = 7;
Mark Mendellae9fd932014-02-10 16:14:35 -0800342 const uint8_t SH_DBG_INFO = 8;
343 const uint8_t SH_DBG_ABRV = 9;
344 const uint8_t SH_DBG_FRM = 10;
345 const uint8_t SH_DBG_STR = 11;
346 const uint8_t SH_NUM = generateDebugInformation ? 12 : 8;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000347 uint32_t shdr_size = sizeof(Elf32_Shdr) * SH_NUM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 expected_offset += shdr_size;
349 if (debug) {
350 LOG(INFO) << "shdr_offset=" << shdr_offset << std::hex << " " << shdr_offset;
351 LOG(INFO) << "shdr_size=" << shdr_size << std::hex << " " << shdr_size;
352 }
353
354 // phase 2: initializing data
355
356 // Elf32_Ehdr
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000357 Elf32_Ehdr elf_header;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 memset(&elf_header, 0, sizeof(elf_header));
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000359 elf_header.e_ident[EI_MAG0] = ELFMAG0;
360 elf_header.e_ident[EI_MAG1] = ELFMAG1;
361 elf_header.e_ident[EI_MAG2] = ELFMAG2;
362 elf_header.e_ident[EI_MAG3] = ELFMAG3;
363 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
364 elf_header.e_ident[EI_DATA] = ELFDATA2LSB;
365 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
366 elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX;
367 elf_header.e_ident[EI_ABIVERSION] = 0;
368 elf_header.e_type = ET_DYN;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 switch (compiler_driver_->GetInstructionSet()) {
370 case kThumb2: {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000371 elf_header.e_machine = EM_ARM;
372 elf_header.e_flags = EF_ARM_EABI_VER5;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 break;
374 }
375 case kX86: {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000376 elf_header.e_machine = EM_386;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 elf_header.e_flags = 0;
378 break;
379 }
380 case kMips: {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000381 elf_header.e_machine = EM_MIPS;
382 elf_header.e_flags = (EF_MIPS_NOREORDER |
383 EF_MIPS_PIC |
384 EF_MIPS_CPIC |
385 EF_MIPS_ABI_O32 |
386 EF_MIPS_ARCH_32R2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 break;
388 }
389 case kArm:
390 default: {
391 LOG(FATAL) << "Unknown instruction set: " << compiler_driver_->GetInstructionSet();
392 break;
393 }
394 }
395 elf_header.e_version = 1;
396 elf_header.e_entry = 0;
397 elf_header.e_phoff = phdr_offset;
398 elf_header.e_shoff = shdr_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000399 elf_header.e_ehsize = sizeof(Elf32_Ehdr);
400 elf_header.e_phentsize = sizeof(Elf32_Phdr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 elf_header.e_phnum = PH_NUM;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000402 elf_header.e_shentsize = sizeof(Elf32_Shdr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 elf_header.e_shnum = SH_NUM;
404 elf_header.e_shstrndx = SH_SHSTRTAB;
405
406 // PHDR
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000407 Elf32_Phdr program_headers[PH_NUM];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 memset(&program_headers, 0, sizeof(program_headers));
409
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000410 program_headers[PH_PHDR].p_type = PT_PHDR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 program_headers[PH_PHDR].p_offset = phdr_offset;
412 program_headers[PH_PHDR].p_vaddr = phdr_offset;
413 program_headers[PH_PHDR].p_paddr = phdr_offset;
414 program_headers[PH_PHDR].p_filesz = sizeof(program_headers);
415 program_headers[PH_PHDR].p_memsz = sizeof(program_headers);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000416 program_headers[PH_PHDR].p_flags = PF_R;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 program_headers[PH_PHDR].p_align = phdr_alignment;
418
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000419 program_headers[PH_LOAD_R__].p_type = PT_LOAD;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 program_headers[PH_LOAD_R__].p_offset = 0;
421 program_headers[PH_LOAD_R__].p_vaddr = 0;
422 program_headers[PH_LOAD_R__].p_paddr = 0;
423 program_headers[PH_LOAD_R__].p_filesz = oat_data_offset + oat_data_size;
424 program_headers[PH_LOAD_R__].p_memsz = oat_data_offset + oat_data_size;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000425 program_headers[PH_LOAD_R__].p_flags = PF_R;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 program_headers[PH_LOAD_R__].p_align = oat_data_alignment;
427
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000428 program_headers[PH_LOAD_R_X].p_type = PT_LOAD;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 program_headers[PH_LOAD_R_X].p_offset = oat_exec_offset;
430 program_headers[PH_LOAD_R_X].p_vaddr = oat_exec_offset;
431 program_headers[PH_LOAD_R_X].p_paddr = oat_exec_offset;
432 program_headers[PH_LOAD_R_X].p_filesz = oat_exec_size;
433 program_headers[PH_LOAD_R_X].p_memsz = oat_exec_size;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000434 program_headers[PH_LOAD_R_X].p_flags = PF_R | PF_X;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 program_headers[PH_LOAD_R_X].p_align = oat_exec_alignment;
436
437 // TODO: PF_W for DYNAMIC is considered processor specific, do we need it?
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000438 program_headers[PH_LOAD_RW_].p_type = PT_LOAD;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 program_headers[PH_LOAD_RW_].p_offset = dynamic_offset;
440 program_headers[PH_LOAD_RW_].p_vaddr = dynamic_offset;
441 program_headers[PH_LOAD_RW_].p_paddr = dynamic_offset;
442 program_headers[PH_LOAD_RW_].p_filesz = dynamic_size;
443 program_headers[PH_LOAD_RW_].p_memsz = dynamic_size;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000444 program_headers[PH_LOAD_RW_].p_flags = PF_R | PF_W;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 program_headers[PH_LOAD_RW_].p_align = dynamic_alignment;
446
447 // TODO: PF_W for DYNAMIC is considered processor specific, do we need it?
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000448 program_headers[PH_DYNAMIC].p_type = PT_DYNAMIC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 program_headers[PH_DYNAMIC].p_offset = dynamic_offset;
450 program_headers[PH_DYNAMIC].p_vaddr = dynamic_offset;
451 program_headers[PH_DYNAMIC].p_paddr = dynamic_offset;
452 program_headers[PH_DYNAMIC].p_filesz = dynamic_size;
453 program_headers[PH_DYNAMIC].p_memsz = dynamic_size;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000454 program_headers[PH_DYNAMIC].p_flags = PF_R | PF_W;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 program_headers[PH_DYNAMIC].p_align = dynamic_alignment;
456
457 // .dynsym
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000458 Elf32_Sym dynsym[SYM_NUM];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 memset(&dynsym, 0, sizeof(dynsym));
460
461 dynsym[SYM_UNDEF].st_name = 0;
462 dynsym[SYM_UNDEF].st_value = 0;
463 dynsym[SYM_UNDEF].st_size = 0;
464 dynsym[SYM_UNDEF].st_info = 0;
465 dynsym[SYM_UNDEF].st_other = 0;
466 dynsym[SYM_UNDEF].st_shndx = 0;
467
468 dynsym[SYM_OATDATA].st_name = dynstr_oatdata_offset;
469 dynsym[SYM_OATDATA].st_value = oat_data_offset;
470 dynsym[SYM_OATDATA].st_size = oat_data_size;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000471 SetBindingAndType(&dynsym[SYM_OATDATA], STB_GLOBAL, STT_OBJECT);
472 dynsym[SYM_OATDATA].st_other = STV_DEFAULT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 dynsym[SYM_OATDATA].st_shndx = SH_RODATA;
474
475 dynsym[SYM_OATEXEC].st_name = dynstr_oatexec_offset;
476 dynsym[SYM_OATEXEC].st_value = oat_exec_offset;
477 dynsym[SYM_OATEXEC].st_size = oat_exec_size;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000478 SetBindingAndType(&dynsym[SYM_OATEXEC], STB_GLOBAL, STT_OBJECT);
479 dynsym[SYM_OATEXEC].st_other = STV_DEFAULT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 dynsym[SYM_OATEXEC].st_shndx = SH_TEXT;
481
482 dynsym[SYM_OATLASTWORD].st_name = dynstr_oatlastword_offset;
483 dynsym[SYM_OATLASTWORD].st_value = oat_exec_offset + oat_exec_size - 4;
484 dynsym[SYM_OATLASTWORD].st_size = 4;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000485 SetBindingAndType(&dynsym[SYM_OATLASTWORD], STB_GLOBAL, STT_OBJECT);
486 dynsym[SYM_OATLASTWORD].st_other = STV_DEFAULT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 dynsym[SYM_OATLASTWORD].st_shndx = SH_TEXT;
488
489 // .dynstr initialized above as dynstr
490
491 // .hash
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000492 Elf32_Word hash[HASH_NUM]; // Note this is Elf32_Word even on 64-bit
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 hash[HASH_NBUCKET] = 1;
494 hash[HASH_NCHAIN] = SYM_NUM;
495 hash[HASH_BUCKET0] = SYM_OATDATA;
496 hash[HASH_BUCKET0 + 1 + SYM_UNDEF] = SYM_UNDEF;
497 hash[HASH_BUCKET0 + 1 + SYM_OATDATA] = SYM_OATEXEC;
498 hash[HASH_BUCKET0 + 1 + SYM_OATEXEC] = SYM_OATLASTWORD;
499 hash[HASH_BUCKET0 + 1 + SYM_OATLASTWORD] = SYM_UNDEF;
500
501 // .rodata and .text content come from oat_contents
502
503 // .dynamic
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000504 Elf32_Dyn dynamic_headers[DH_NUM];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 memset(&dynamic_headers, 0, sizeof(dynamic_headers));
506
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000507 dynamic_headers[DH_SONAME].d_tag = DT_SONAME;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 dynamic_headers[DH_SONAME].d_un.d_val = dynstr_soname_offset;
509
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000510 dynamic_headers[DH_HASH].d_tag = DT_HASH;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 dynamic_headers[DH_HASH].d_un.d_ptr = hash_offset;
512
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000513 dynamic_headers[DH_SYMTAB].d_tag = DT_SYMTAB;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 dynamic_headers[DH_SYMTAB].d_un.d_ptr = dynsym_offset;
515
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000516 dynamic_headers[DH_SYMENT].d_tag = DT_SYMENT;
517 dynamic_headers[DH_SYMENT].d_un.d_val = sizeof(Elf32_Sym);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000519 dynamic_headers[DH_STRTAB].d_tag = DT_STRTAB;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 dynamic_headers[DH_STRTAB].d_un.d_ptr = dynstr_offset;
521
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000522 dynamic_headers[DH_STRSZ].d_tag = DT_STRSZ;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 dynamic_headers[DH_STRSZ].d_un.d_val = dynstr_size;
524
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000525 dynamic_headers[DH_NULL].d_tag = DT_NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 dynamic_headers[DH_NULL].d_un.d_val = 0;
527
528 // .shstrtab initialized above as shstrtab
529
530 // section headers (after all sections)
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000531 Elf32_Shdr section_headers[SH_NUM];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 memset(&section_headers, 0, sizeof(section_headers));
533
534 section_headers[SH_NULL].sh_name = 0;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000535 section_headers[SH_NULL].sh_type = SHT_NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 section_headers[SH_NULL].sh_flags = 0;
537 section_headers[SH_NULL].sh_addr = 0;
538 section_headers[SH_NULL].sh_offset = 0;
539 section_headers[SH_NULL].sh_size = 0;
540 section_headers[SH_NULL].sh_link = 0;
541 section_headers[SH_NULL].sh_info = 0;
542 section_headers[SH_NULL].sh_addralign = 0;
543 section_headers[SH_NULL].sh_entsize = 0;
544
545 section_headers[SH_DYNSYM].sh_name = shstrtab_dynsym_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000546 section_headers[SH_DYNSYM].sh_type = SHT_DYNSYM;
547 section_headers[SH_DYNSYM].sh_flags = SHF_ALLOC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 section_headers[SH_DYNSYM].sh_addr = dynsym_offset;
549 section_headers[SH_DYNSYM].sh_offset = dynsym_offset;
550 section_headers[SH_DYNSYM].sh_size = dynsym_size;
551 section_headers[SH_DYNSYM].sh_link = SH_DYNSTR;
552 section_headers[SH_DYNSYM].sh_info = 1; // 1 because we have not STB_LOCAL symbols
553 section_headers[SH_DYNSYM].sh_addralign = dynsym_alignment;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000554 section_headers[SH_DYNSYM].sh_entsize = sizeof(Elf32_Sym);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555
556 section_headers[SH_DYNSTR].sh_name = shstrtab_dynstr_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000557 section_headers[SH_DYNSTR].sh_type = SHT_STRTAB;
558 section_headers[SH_DYNSTR].sh_flags = SHF_ALLOC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 section_headers[SH_DYNSTR].sh_addr = dynstr_offset;
560 section_headers[SH_DYNSTR].sh_offset = dynstr_offset;
561 section_headers[SH_DYNSTR].sh_size = dynstr_size;
562 section_headers[SH_DYNSTR].sh_link = 0;
563 section_headers[SH_DYNSTR].sh_info = 0;
564 section_headers[SH_DYNSTR].sh_addralign = dynstr_alignment;
565 section_headers[SH_DYNSTR].sh_entsize = 0;
566
567 section_headers[SH_HASH].sh_name = shstrtab_hash_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000568 section_headers[SH_HASH].sh_type = SHT_HASH;
569 section_headers[SH_HASH].sh_flags = SHF_ALLOC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 section_headers[SH_HASH].sh_addr = hash_offset;
571 section_headers[SH_HASH].sh_offset = hash_offset;
572 section_headers[SH_HASH].sh_size = hash_size;
573 section_headers[SH_HASH].sh_link = SH_DYNSYM;
574 section_headers[SH_HASH].sh_info = 0;
575 section_headers[SH_HASH].sh_addralign = hash_alignment;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000576 section_headers[SH_HASH].sh_entsize = sizeof(Elf32_Word); // This is Elf32_Word even on 64-bit
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577
578 section_headers[SH_RODATA].sh_name = shstrtab_rodata_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000579 section_headers[SH_RODATA].sh_type = SHT_PROGBITS;
580 section_headers[SH_RODATA].sh_flags = SHF_ALLOC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 section_headers[SH_RODATA].sh_addr = oat_data_offset;
582 section_headers[SH_RODATA].sh_offset = oat_data_offset;
583 section_headers[SH_RODATA].sh_size = oat_data_size;
584 section_headers[SH_RODATA].sh_link = 0;
585 section_headers[SH_RODATA].sh_info = 0;
586 section_headers[SH_RODATA].sh_addralign = oat_data_alignment;
587 section_headers[SH_RODATA].sh_entsize = 0;
588
589 section_headers[SH_TEXT].sh_name = shstrtab_text_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000590 section_headers[SH_TEXT].sh_type = SHT_PROGBITS;
591 section_headers[SH_TEXT].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 section_headers[SH_TEXT].sh_addr = oat_exec_offset;
593 section_headers[SH_TEXT].sh_offset = oat_exec_offset;
594 section_headers[SH_TEXT].sh_size = oat_exec_size;
595 section_headers[SH_TEXT].sh_link = 0;
596 section_headers[SH_TEXT].sh_info = 0;
597 section_headers[SH_TEXT].sh_addralign = oat_exec_alignment;
598 section_headers[SH_TEXT].sh_entsize = 0;
599
600 // TODO: SHF_WRITE for .dynamic is considered processor specific, do we need it?
601 section_headers[SH_DYNAMIC].sh_name = shstrtab_dynamic_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000602 section_headers[SH_DYNAMIC].sh_type = SHT_DYNAMIC;
603 section_headers[SH_DYNAMIC].sh_flags = SHF_WRITE | SHF_ALLOC;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 section_headers[SH_DYNAMIC].sh_addr = dynamic_offset;
605 section_headers[SH_DYNAMIC].sh_offset = dynamic_offset;
606 section_headers[SH_DYNAMIC].sh_size = dynamic_size;
607 section_headers[SH_DYNAMIC].sh_link = SH_DYNSTR;
608 section_headers[SH_DYNAMIC].sh_info = 0;
609 section_headers[SH_DYNAMIC].sh_addralign = dynamic_alignment;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000610 section_headers[SH_DYNAMIC].sh_entsize = sizeof(Elf32_Dyn);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611
612 section_headers[SH_SHSTRTAB].sh_name = shstrtab_shstrtab_offset;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000613 section_headers[SH_SHSTRTAB].sh_type = SHT_STRTAB;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 section_headers[SH_SHSTRTAB].sh_flags = 0;
615 section_headers[SH_SHSTRTAB].sh_addr = shstrtab_offset;
616 section_headers[SH_SHSTRTAB].sh_offset = shstrtab_offset;
617 section_headers[SH_SHSTRTAB].sh_size = shstrtab_size;
618 section_headers[SH_SHSTRTAB].sh_link = 0;
619 section_headers[SH_SHSTRTAB].sh_info = 0;
620 section_headers[SH_SHSTRTAB].sh_addralign = shstrtab_alignment;
621 section_headers[SH_SHSTRTAB].sh_entsize = 0;
622
Mark Mendellae9fd932014-02-10 16:14:35 -0800623 if (generateDebugInformation) {
624 section_headers[SH_DBG_INFO].sh_name = shstrtab_debug_info_offset;
625 section_headers[SH_DBG_INFO].sh_type = SHT_PROGBITS;
626 section_headers[SH_DBG_INFO].sh_flags = 0;
627 section_headers[SH_DBG_INFO].sh_addr = 0;
628 section_headers[SH_DBG_INFO].sh_offset = shdbg_info_offset;
629 section_headers[SH_DBG_INFO].sh_size = shdbg_info_size;
630 section_headers[SH_DBG_INFO].sh_link = 0;
631 section_headers[SH_DBG_INFO].sh_info = 0;
632 section_headers[SH_DBG_INFO].sh_addralign = shdbg_info_alignment;
633 section_headers[SH_DBG_INFO].sh_entsize = 0;
634
635 section_headers[SH_DBG_ABRV].sh_name = shstrtab_debug_abbrev_offset;
636 section_headers[SH_DBG_ABRV].sh_type = SHT_PROGBITS;
637 section_headers[SH_DBG_ABRV].sh_flags = 0;
638 section_headers[SH_DBG_ABRV].sh_addr = 0;
639 section_headers[SH_DBG_ABRV].sh_offset = shdbg_abbrev_offset;
640 section_headers[SH_DBG_ABRV].sh_size = shdbg_abbrev_size;
641 section_headers[SH_DBG_ABRV].sh_link = 0;
642 section_headers[SH_DBG_ABRV].sh_info = 0;
643 section_headers[SH_DBG_ABRV].sh_addralign = shdbg_abbrev_alignment;
644 section_headers[SH_DBG_ABRV].sh_entsize = 0;
645
646 section_headers[SH_DBG_FRM].sh_name = shstrtab_debug_frame_offset;
647 section_headers[SH_DBG_FRM].sh_type = SHT_PROGBITS;
648 section_headers[SH_DBG_FRM].sh_flags = 0;
649 section_headers[SH_DBG_FRM].sh_addr = 0;
650 section_headers[SH_DBG_FRM].sh_offset = shdbg_frm_offset;
651 section_headers[SH_DBG_FRM].sh_size = shdbg_frm_size;
652 section_headers[SH_DBG_FRM].sh_link = 0;
653 section_headers[SH_DBG_FRM].sh_info = 0;
654 section_headers[SH_DBG_FRM].sh_addralign = shdbg_frm_alignment;
655 section_headers[SH_DBG_FRM].sh_entsize = 0;
656
657 section_headers[SH_DBG_STR].sh_name = shstrtab_debug_str_offset;
658 section_headers[SH_DBG_STR].sh_type = SHT_PROGBITS;
659 section_headers[SH_DBG_STR].sh_flags = 0;
660 section_headers[SH_DBG_STR].sh_addr = 0;
661 section_headers[SH_DBG_STR].sh_offset = shdbg_str_offset;
662 section_headers[SH_DBG_STR].sh_size = shdbg_str_size;
663 section_headers[SH_DBG_STR].sh_link = 0;
664 section_headers[SH_DBG_STR].sh_info = 0;
665 section_headers[SH_DBG_STR].sh_addralign = shdbg_str_alignment;
666 section_headers[SH_DBG_STR].sh_entsize = 0;
667 }
668
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 // phase 3: writing file
670
671 // Elf32_Ehdr
672 if (!elf_file_->WriteFully(&elf_header, sizeof(elf_header))) {
673 PLOG(ERROR) << "Failed to write ELF header for " << elf_file_->GetPath();
674 return false;
675 }
676
677 // PHDR
678 if (static_cast<off_t>(phdr_offset) != lseek(elf_file_->Fd(), 0, SEEK_CUR)) {
679 PLOG(ERROR) << "Failed to be at expected ELF program header offset phdr_offset "
680 << " for " << elf_file_->GetPath();
681 return false;
682 }
683 if (!elf_file_->WriteFully(program_headers, sizeof(program_headers))) {
684 PLOG(ERROR) << "Failed to write ELF program headers for " << elf_file_->GetPath();
685 return false;
686 }
687
688 // .dynsym
689 DCHECK_LE(phdr_offset + phdr_size, dynsym_offset);
690 if (static_cast<off_t>(dynsym_offset) != lseek(elf_file_->Fd(), dynsym_offset, SEEK_SET)) {
691 PLOG(ERROR) << "Failed to seek to .dynsym offset location " << dynsym_offset
692 << " for " << elf_file_->GetPath();
693 return false;
694 }
695 if (!elf_file_->WriteFully(dynsym, sizeof(dynsym))) {
696 PLOG(ERROR) << "Failed to write .dynsym for " << elf_file_->GetPath();
697 return false;
698 }
699
700 // .dynstr
701 DCHECK_LE(dynsym_offset + dynsym_size, dynstr_offset);
702 if (static_cast<off_t>(dynstr_offset) != lseek(elf_file_->Fd(), dynstr_offset, SEEK_SET)) {
703 PLOG(ERROR) << "Failed to seek to .dynstr offset " << dynstr_offset
704 << " for " << elf_file_->GetPath();
705 return false;
706 }
Mathieu Chartiere2be9da2013-08-26 11:38:54 -0700707 if (!elf_file_->WriteFully(&dynstr[0], dynstr_size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 PLOG(ERROR) << "Failed to write .dynsym for " << elf_file_->GetPath();
709 return false;
710 }
711
712 // .hash
713 DCHECK_LE(dynstr_offset + dynstr_size, hash_offset);
714 if (static_cast<off_t>(hash_offset) != lseek(elf_file_->Fd(), hash_offset, SEEK_SET)) {
715 PLOG(ERROR) << "Failed to seek to .hash offset " << hash_offset
716 << " for " << elf_file_->GetPath();
717 return false;
718 }
719 if (!elf_file_->WriteFully(hash, sizeof(hash))) {
720 PLOG(ERROR) << "Failed to write .dynsym for " << elf_file_->GetPath();
721 return false;
722 }
723
724 // .rodata .text
725 DCHECK_LE(hash_offset + hash_size, oat_data_offset);
726 if (static_cast<off_t>(oat_data_offset) != lseek(elf_file_->Fd(), oat_data_offset, SEEK_SET)) {
727 PLOG(ERROR) << "Failed to seek to .rodata offset " << oat_data_offset
728 << " for " << elf_file_->GetPath();
729 return false;
730 }
Brian Carlstromc6dfdac2013-08-26 18:57:31 -0700731 BufferedOutputStream output_stream(new FileOutputStream(elf_file_));
Ian Rogers3d504072014-03-01 09:16:49 -0800732 if (!oat_writer->Write(&output_stream)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 PLOG(ERROR) << "Failed to write .rodata and .text for " << elf_file_->GetPath();
734 return false;
735 }
736
737 // .dynamic
Ian Rogers3d504072014-03-01 09:16:49 -0800738 DCHECK_LE(oat_data_offset + oat_writer->GetSize(), dynamic_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 if (static_cast<off_t>(dynamic_offset) != lseek(elf_file_->Fd(), dynamic_offset, SEEK_SET)) {
740 PLOG(ERROR) << "Failed to seek to .dynamic offset " << dynamic_offset
741 << " for " << elf_file_->GetPath();
742 return false;
743 }
744 if (!elf_file_->WriteFully(&dynamic_headers[0], dynamic_size)) {
745 PLOG(ERROR) << "Failed to write .dynamic for " << elf_file_->GetPath();
746 return false;
747 }
748
749 // .shstrtab
750 DCHECK_LE(dynamic_offset + dynamic_size, shstrtab_offset);
751 if (static_cast<off_t>(shstrtab_offset) != lseek(elf_file_->Fd(), shstrtab_offset, SEEK_SET)) {
752 PLOG(ERROR) << "Failed to seek to .shstrtab offset " << shstrtab_offset
753 << " for " << elf_file_->GetPath();
754 return false;
755 }
756 if (!elf_file_->WriteFully(&shstrtab[0], shstrtab_size)) {
757 PLOG(ERROR) << "Failed to write .shstrtab for " << elf_file_->GetPath();
758 return false;
759 }
760
Mark Mendellae9fd932014-02-10 16:14:35 -0800761 if (generateDebugInformation) {
762 // .debug_info
763 DCHECK_LE(shstrtab_offset + shstrtab_size, shdbg_info_offset);
764 if (static_cast<off_t>(shdbg_info_offset) != lseek(elf_file_->Fd(), shdbg_info_offset, SEEK_SET)) {
765 PLOG(ERROR) << "Failed to seek to .shdbg_info offset " << shdbg_info_offset
766 << " for " << elf_file_->GetPath();
767 return false;
768 }
769 if (!elf_file_->WriteFully(&dbg_info[0], shdbg_info_size)) {
770 PLOG(ERROR) << "Failed to write .debug_info for " << elf_file_->GetPath();
771 return false;
772 }
773
774 // .debug_abbrev
775 DCHECK_LE(shdbg_info_offset + shdbg_info_size, shdbg_abbrev_offset);
776 if (static_cast<off_t>(shdbg_abbrev_offset) != lseek(elf_file_->Fd(), shdbg_abbrev_offset, SEEK_SET)) {
777 PLOG(ERROR) << "Failed to seek to .shdbg_abbrev offset " << shdbg_abbrev_offset
778 << " for " << elf_file_->GetPath();
779 return false;
780 }
781 if (!elf_file_->WriteFully(&dbg_abbrev[0], shdbg_abbrev_size)) {
782 PLOG(ERROR) << "Failed to write .debug_abbrev for " << elf_file_->GetPath();
783 return false;
784 }
785
786 // .debug_frame
787 DCHECK_LE(shdbg_abbrev_offset + shdbg_abbrev_size, shdbg_frm_offset);
788 if (static_cast<off_t>(shdbg_frm_offset) != lseek(elf_file_->Fd(), shdbg_frm_offset, SEEK_SET)) {
789 PLOG(ERROR) << "Failed to seek to .shdbg_frm offset " << shdbg_frm_offset
790 << " for " << elf_file_->GetPath();
791 return false;
792 }
793 if (!elf_file_->WriteFully(&((*compiler_driver_->GetCallFrameInformation())[0]), shdbg_frm_size)) {
794 PLOG(ERROR) << "Failed to write .debug_frame for " << elf_file_->GetPath();
795 return false;
796 }
797
798 // .debug_str
799 DCHECK_LE(shdbg_frm_offset + shdbg_frm_size, shdbg_str_offset);
800 if (static_cast<off_t>(shdbg_str_offset) != lseek(elf_file_->Fd(), shdbg_str_offset, SEEK_SET)) {
801 PLOG(ERROR) << "Failed to seek to .shdbg_str offset " << shdbg_str_offset
802 << " for " << elf_file_->GetPath();
803 return false;
804 }
805 if (!elf_file_->WriteFully(&dbg_str[0], shdbg_str_size)) {
806 PLOG(ERROR) << "Failed to write .debug_frame for " << elf_file_->GetPath();
807 return false;
808 }
809 }
810
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 // section headers (after all sections)
Mark Mendellae9fd932014-02-10 16:14:35 -0800812 if (generateDebugInformation) {
813 DCHECK_LE(shdbg_str_offset + shdbg_str_size, shdr_offset);
814 } else {
815 DCHECK_LE(shstrtab_offset + shstrtab_size, shdr_offset);
816 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 if (static_cast<off_t>(shdr_offset) != lseek(elf_file_->Fd(), shdr_offset, SEEK_SET)) {
818 PLOG(ERROR) << "Failed to seek to ELF section headers offset " << shdr_offset
819 << " for " << elf_file_->GetPath();
820 return false;
821 }
822 if (!elf_file_->WriteFully(section_headers, sizeof(section_headers))) {
823 PLOG(ERROR) << "Failed to write ELF section headers for " << elf_file_->GetPath();
824 return false;
825 }
826
Anwar Ghuloum87183592013-08-14 12:12:19 -0700827 VLOG(compiler) << "ELF file written successfully: " << elf_file_->GetPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 return true;
Mark Mendellae9fd932014-02-10 16:14:35 -0800829} // NOLINT(readability/fn_size)
830
831static void UpdateWord(std::vector<uint8_t>*buf, int offset, int data) {
832 (*buf)[offset+0] = data;
833 (*buf)[offset+1] = data >> 8;
834 (*buf)[offset+2] = data >> 16;
835 (*buf)[offset+3] = data >> 24;
836}
837
838static void PushWord(std::vector<uint8_t>*buf, int data) {
839 buf->push_back(data & 0xff);
840 buf->push_back((data >> 8) & 0xff);
841 buf->push_back((data >> 16) & 0xff);
842 buf->push_back((data >> 24) & 0xff);
843}
844
845static void PushHalf(std::vector<uint8_t>*buf, int data) {
846 buf->push_back(data & 0xff);
847 buf->push_back((data >> 8) & 0xff);
848}
849
850// DWARF constants needed to generate CFI information.
851enum {
852 // Tag encodings.
853 DW_TAG_compile_unit = 0x11,
854 DW_TAG_subprogram = 0X2e,
855
856 // Attribute encodings.
857 DW_AT_name = 0x03,
858 DW_AT_low_pc = 0x11,
859 DW_AT_high_pc = 0x12,
860 DW_AT_language = 0x13,
861
862 // Constant encoding.
863 DW_CHILDREN_no = 0x00,
864 DW_CHILDREN_yes = 0x01,
865
866 // Attribute form encodings.
867 DW_FORM_addr = 0x01,
868 DW_FORM_data1 = 0x0b,
869 DW_FORM_strp = 0x0e,
870
871 // Language encoding.
872 DW_LANG_Java = 0x000b
873};
874
875void ElfWriterQuick::FillInCFIInformation(OatWriter* oat_writer,
876 std::vector<uint8_t>* dbg_info,
877 std::vector<uint8_t>* dbg_abbrev,
878 std::vector<uint8_t>* dbg_str) {
879 // Create the debug_abbrev section with boilerplate information.
880 // We only care about low_pc and high_pc right now for the compilation
881 // unit and methods.
882
883 // Tag 1: Compilation unit: DW_TAG_compile_unit.
884 dbg_abbrev->push_back(1);
885 dbg_abbrev->push_back(DW_TAG_compile_unit);
886
887 // There are children (the methods).
888 dbg_abbrev->push_back(DW_CHILDREN_yes);
889
890 // DW_LANG_Java DW_FORM_data1.
891 dbg_abbrev->push_back(DW_AT_language);
892 dbg_abbrev->push_back(DW_FORM_data1);
893
894 // DW_AT_low_pc DW_FORM_addr.
895 dbg_abbrev->push_back(DW_AT_low_pc);
896 dbg_abbrev->push_back(DW_FORM_addr);
897
898 // DW_AT_high_pc DW_FORM_addr.
899 dbg_abbrev->push_back(DW_AT_high_pc);
900 dbg_abbrev->push_back(DW_FORM_addr);
901
902 // End of DW_TAG_compile_unit.
903 PushHalf(dbg_abbrev, 0);
904
905 // Tag 2: Compilation unit: DW_TAG_subprogram.
906 dbg_abbrev->push_back(2);
907 dbg_abbrev->push_back(DW_TAG_subprogram);
908
909 // There are no children.
910 dbg_abbrev->push_back(DW_CHILDREN_no);
911
912 // Name of the method.
913 dbg_abbrev->push_back(DW_AT_name);
914 dbg_abbrev->push_back(DW_FORM_strp);
915
916 // DW_AT_low_pc DW_FORM_addr.
917 dbg_abbrev->push_back(DW_AT_low_pc);
918 dbg_abbrev->push_back(DW_FORM_addr);
919
920 // DW_AT_high_pc DW_FORM_addr.
921 dbg_abbrev->push_back(DW_AT_high_pc);
922 dbg_abbrev->push_back(DW_FORM_addr);
923
924 // End of DW_TAG_subprogram.
925 PushHalf(dbg_abbrev, 0);
926
927 // Start the debug_info section with the header information
928 // 'unit_length' will be filled in later.
929 PushWord(dbg_info, 0);
930
931 // 'version' - 3.
932 PushHalf(dbg_info, 3);
933
934 // Offset into .debug_abbrev section (always 0).
935 PushWord(dbg_info, 0);
936
937 // Address size: 4.
938 dbg_info->push_back(4);
939
940 // Start the description for the compilation unit.
941 // This uses tag 1.
942 dbg_info->push_back(1);
943
944 // The language is Java.
945 dbg_info->push_back(DW_LANG_Java);
946
947 // Leave space for low_pc and high_pc.
948 int low_pc_offset = dbg_info->size();
949 PushWord(dbg_info, 0);
950 PushWord(dbg_info, 0);
951
952 // Walk through the information in the method table, and enter into dbg_info.
953 const std::vector<OatWriter::DebugInfo>& dbg = oat_writer->GetCFIMethodInfo();
954 uint32_t low_pc = 0xFFFFFFFFU;
955 uint32_t high_pc = 0;
956
957 for (uint32_t i = 0; i < dbg.size(); i++) {
958 const OatWriter::DebugInfo& info = dbg[i];
959 if (info.low_pc_ < low_pc) {
960 low_pc = info.low_pc_;
961 }
962 if (info.high_pc_ > high_pc) {
963 high_pc = info.high_pc_;
964 }
965
966 // Start a new TAG: subroutine (2).
967 dbg_info->push_back(2);
968
969 // Enter the name into the string table (and NUL terminate).
970 uint32_t str_offset = dbg_str->size();
971 dbg_str->insert(dbg_str->end(), info.method_name_.begin(), info.method_name_.end());
972 dbg_str->push_back('\0');
973
974 // Enter name, low_pc, high_pc.
975 PushWord(dbg_info, str_offset);
976 PushWord(dbg_info, info.low_pc_);
977 PushWord(dbg_info, info.high_pc_);
978 }
979
980 // One byte terminator
981 dbg_info->push_back(0);
982
983 // We have now walked all the methods. Fill in lengths and low/high PCs.
984 UpdateWord(dbg_info, 0, dbg_info->size() - 4);
985 UpdateWord(dbg_info, low_pc_offset, low_pc);
986 UpdateWord(dbg_info, low_pc_offset + 4, high_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987}
988
989} // namespace art