• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
21 #include "buffered_output_stream.h"
22 #include "driver/compiler_driver.h"
23 #include "file_output_stream.h"
24 #include "globals.h"
25 #include "oat.h"
26 #include "oat_writer.h"
27 #include "utils.h"
28 
29 namespace art {
30 
ElfWriterQuick(const CompilerDriver & driver,File * elf_file)31 ElfWriterQuick::ElfWriterQuick(const CompilerDriver& driver, File* elf_file)
32   : ElfWriter(driver, elf_file) {}
33 
~ElfWriterQuick()34 ElfWriterQuick::~ElfWriterQuick() {}
35 
Create(File * elf_file,OatWriter & oat_writer,const std::vector<const DexFile * > & dex_files,const std::string & android_root,bool is_host,const CompilerDriver & driver)36 bool ElfWriterQuick::Create(File* elf_file,
37                             OatWriter& oat_writer,
38                             const std::vector<const DexFile*>& dex_files,
39                             const std::string& android_root,
40                             bool is_host,
41                             const CompilerDriver& driver) {
42   ElfWriterQuick elf_writer(driver, elf_file);
43   return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
44 }
45 
Write(OatWriter & oat_writer,const std::vector<const DexFile * > & dex_files_unused,const std::string & android_root_unused,bool is_host_unused)46 bool ElfWriterQuick::Write(OatWriter& oat_writer,
47                            const std::vector<const DexFile*>& dex_files_unused,
48                            const std::string& android_root_unused,
49                            bool is_host_unused) {
50   const bool debug = false;
51   // +-------------------------+
52   // | Elf32_Ehdr              |
53   // +-------------------------+
54   // | Elf32_Phdr PHDR         |
55   // | Elf32_Phdr LOAD R       | .dynsym .dynstr .hash .rodata
56   // | Elf32_Phdr LOAD R X     | .text
57   // | Elf32_Phdr LOAD RW      | .dynamic
58   // | Elf32_Phdr DYNAMIC      | .dynamic
59   // +-------------------------+
60   // | .dynsym                 |
61   // | Elf32_Sym  STN_UNDEF    |
62   // | Elf32_Sym  oatdata      |
63   // | Elf32_Sym  oatexec      |
64   // | Elf32_Sym  oatlastword  |
65   // +-------------------------+
66   // | .dynstr                 |
67   // | \0                      |
68   // | oatdata\0               |
69   // | oatexec\0               |
70   // | oatlastword\0           |
71   // | boot.oat\0              |
72   // +-------------------------+
73   // | .hash                   |
74   // | Elf32_Word nbucket = 1  |
75   // | Elf32_Word nchain  = 3  |
76   // | Elf32_Word bucket[0] = 0|
77   // | Elf32_Word chain[0]  = 1|
78   // | Elf32_Word chain[1]  = 2|
79   // | Elf32_Word chain[2]  = 3|
80   // +-------------------------+
81   // | .rodata                 |
82   // | oatdata..oatexec-4      |
83   // +-------------------------+
84   // | .text                   |
85   // | oatexec..oatlastword    |
86   // +-------------------------+
87   // | .dynamic                |
88   // | Elf32_Dyn DT_SONAME     |
89   // | Elf32_Dyn DT_HASH       |
90   // | Elf32_Dyn DT_SYMTAB     |
91   // | Elf32_Dyn DT_SYMENT     |
92   // | Elf32_Dyn DT_STRTAB     |
93   // | Elf32_Dyn DT_STRSZ      |
94   // | Elf32_Dyn DT_NULL       |
95   // +-------------------------+
96   // | .shstrtab               |
97   // | \0                      |
98   // | .dynamic\0              |
99   // | .dynsym\0               |
100   // | .dynstr\0               |
101   // | .hash\0                 |
102   // | .rodata\0               |
103   // | .text\0                 |
104   // | .shstrtab\0             |
105   // +-------------------------+
106   // | Elf32_Shdr NULL         |
107   // | Elf32_Shdr .dynsym      |
108   // | Elf32_Shdr .dynstr      |
109   // | Elf32_Shdr .hash        |
110   // | Elf32_Shdr .text        |
111   // | Elf32_Shdr .rodata      |
112   // | Elf32_Shdr .dynamic     |
113   // | Elf32_Shdr .shstrtab    |
114   // +-------------------------+
115 
116   // phase 1: computing offsets
117   uint32_t expected_offset = 0;
118 
119   // Elf32_Ehdr
120   expected_offset += sizeof(llvm::ELF::Elf32_Ehdr);
121 
122   // PHDR
123   uint32_t phdr_alignment = sizeof(llvm::ELF::Elf32_Word);
124   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;
131   uint32_t phdr_size = sizeof(llvm::ELF::Elf32_Phdr) * PH_NUM;
132   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
139   uint32_t dynsym_alignment = sizeof(llvm::ELF::Elf32_Word);
140   uint32_t dynsym_offset = expected_offset = RoundUp(expected_offset, dynsym_alignment);
141   const uint8_t SYM_UNDEF       = 0;  // aka STN_UNDEF
142   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;
146   uint32_t dynsym_size = sizeof(llvm::ELF::Elf32_Sym) * SYM_NUM;
147   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
183   uint32_t hash_alignment = sizeof(llvm::ELF::Elf32_Word);  // Even for 64-bit
184   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;
189   uint32_t hash_size = sizeof(llvm::ELF::Elf32_Word) * HASH_NUM;
190   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);
199   const OatHeader& oat_header = oat_writer.GetOatHeader();
200   CHECK(oat_header.IsValid());
201   uint32_t oat_data_size = oat_header.GetExecutableOffset();
202   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);
212   uint32_t oat_exec_size = oat_writer.GetSize() - oat_data_size;
213   expected_offset += oat_exec_size;
214   CHECK_EQ(oat_data_offset + oat_writer.GetSize(), expected_offset);
215   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
221   // alignment would naturally be sizeof(llvm::ELF::Elf32_Word), but we want this in a new segment
222   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;
232   uint32_t dynamic_size = sizeof(llvm::ELF::Elf32_Dyn) * DH_NUM;
233   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';
266   uint32_t shstrtab_size = shstrtab.size();
267   expected_offset += shstrtab_size;
268   if (debug) {
269     LOG(INFO) << "shstrtab_offset=" << shstrtab_offset << std::hex << " " << shstrtab_offset;
270     LOG(INFO) << "shstrtab_size=" << shstrtab_size << std::hex << " " << shstrtab_size;
271   }
272 
273   // section headers (after all sections)
274   uint32_t shdr_alignment = sizeof(llvm::ELF::Elf32_Word);
275   uint32_t shdr_offset = expected_offset = RoundUp(expected_offset, shdr_alignment);
276   const uint8_t SH_NULL     = 0;
277   const uint8_t SH_DYNSYM   = 1;
278   const uint8_t SH_DYNSTR   = 2;
279   const uint8_t SH_HASH     = 3;
280   const uint8_t SH_RODATA   = 4;
281   const uint8_t SH_TEXT     = 5;
282   const uint8_t SH_DYNAMIC  = 6;
283   const uint8_t SH_SHSTRTAB = 7;
284   const uint8_t SH_NUM      = 8;
285   uint32_t shdr_size = sizeof(llvm::ELF::Elf32_Shdr) * SH_NUM;
286   expected_offset += shdr_size;
287   if (debug) {
288     LOG(INFO) << "shdr_offset=" << shdr_offset << std::hex << " " << shdr_offset;
289     LOG(INFO) << "shdr_size=" << shdr_size << std::hex << " " << shdr_size;
290   }
291 
292   // phase 2: initializing data
293 
294   // Elf32_Ehdr
295   llvm::ELF::Elf32_Ehdr elf_header;
296   memset(&elf_header, 0, sizeof(elf_header));
297   elf_header.e_ident[llvm::ELF::EI_MAG0]       = llvm::ELF::ElfMagic[0];
298   elf_header.e_ident[llvm::ELF::EI_MAG1]       = llvm::ELF::ElfMagic[1];
299   elf_header.e_ident[llvm::ELF::EI_MAG2]       = llvm::ELF::ElfMagic[2];
300   elf_header.e_ident[llvm::ELF::EI_MAG3]       = llvm::ELF::ElfMagic[3];
301   elf_header.e_ident[llvm::ELF::EI_CLASS]      = llvm::ELF::ELFCLASS32;
302   elf_header.e_ident[llvm::ELF::EI_DATA]       = llvm::ELF::ELFDATA2LSB;
303   elf_header.e_ident[llvm::ELF::EI_VERSION]    = llvm::ELF::EV_CURRENT;
304   elf_header.e_ident[llvm::ELF::EI_OSABI]      = llvm::ELF::ELFOSABI_LINUX;
305   elf_header.e_ident[llvm::ELF::EI_ABIVERSION] = 0;
306   elf_header.e_type = llvm::ELF::ET_DYN;
307   switch (compiler_driver_->GetInstructionSet()) {
308     case kThumb2: {
309       elf_header.e_machine = llvm::ELF::EM_ARM;
310       elf_header.e_flags = llvm::ELF::EF_ARM_EABI_VER5;
311       break;
312     }
313     case kX86: {
314       elf_header.e_machine = llvm::ELF::EM_386;
315       elf_header.e_flags = 0;
316       break;
317     }
318     case kMips: {
319       elf_header.e_machine = llvm::ELF::EM_MIPS;
320       elf_header.e_flags = (llvm::ELF::EF_MIPS_NOREORDER |
321                             llvm::ELF::EF_MIPS_PIC       |
322                             llvm::ELF::EF_MIPS_CPIC      |
323                             llvm::ELF::EF_MIPS_ABI_O32   |
324                             llvm::ELF::EF_MIPS_ARCH_32R2);
325       break;
326     }
327     case kArm:
328     default: {
329       LOG(FATAL) << "Unknown instruction set: " << compiler_driver_->GetInstructionSet();
330       break;
331     }
332   }
333   elf_header.e_version = 1;
334   elf_header.e_entry = 0;
335   elf_header.e_phoff = phdr_offset;
336   elf_header.e_shoff = shdr_offset;
337   elf_header.e_ehsize = sizeof(llvm::ELF::Elf32_Ehdr);
338   elf_header.e_phentsize = sizeof(llvm::ELF::Elf32_Phdr);
339   elf_header.e_phnum = PH_NUM;
340   elf_header.e_shentsize = sizeof(llvm::ELF::Elf32_Shdr);
341   elf_header.e_shnum = SH_NUM;
342   elf_header.e_shstrndx = SH_SHSTRTAB;
343 
344   // PHDR
345   llvm::ELF::Elf32_Phdr program_headers[PH_NUM];
346   memset(&program_headers, 0, sizeof(program_headers));
347 
348   program_headers[PH_PHDR].p_type    = llvm::ELF::PT_PHDR;
349   program_headers[PH_PHDR].p_offset  = phdr_offset;
350   program_headers[PH_PHDR].p_vaddr   = phdr_offset;
351   program_headers[PH_PHDR].p_paddr   = phdr_offset;
352   program_headers[PH_PHDR].p_filesz  = sizeof(program_headers);
353   program_headers[PH_PHDR].p_memsz   = sizeof(program_headers);
354   program_headers[PH_PHDR].p_flags   = llvm::ELF::PF_R;
355   program_headers[PH_PHDR].p_align   = phdr_alignment;
356 
357   program_headers[PH_LOAD_R__].p_type    = llvm::ELF::PT_LOAD;
358   program_headers[PH_LOAD_R__].p_offset  = 0;
359   program_headers[PH_LOAD_R__].p_vaddr   = 0;
360   program_headers[PH_LOAD_R__].p_paddr   = 0;
361   program_headers[PH_LOAD_R__].p_filesz  = oat_data_offset + oat_data_size;
362   program_headers[PH_LOAD_R__].p_memsz   = oat_data_offset + oat_data_size;
363   program_headers[PH_LOAD_R__].p_flags   = llvm::ELF::PF_R;
364   program_headers[PH_LOAD_R__].p_align   = oat_data_alignment;
365 
366   program_headers[PH_LOAD_R_X].p_type    = llvm::ELF::PT_LOAD;
367   program_headers[PH_LOAD_R_X].p_offset  = oat_exec_offset;
368   program_headers[PH_LOAD_R_X].p_vaddr   = oat_exec_offset;
369   program_headers[PH_LOAD_R_X].p_paddr   = oat_exec_offset;
370   program_headers[PH_LOAD_R_X].p_filesz  = oat_exec_size;
371   program_headers[PH_LOAD_R_X].p_memsz   = oat_exec_size;
372   program_headers[PH_LOAD_R_X].p_flags   = llvm::ELF::PF_R | llvm::ELF::PF_X;
373   program_headers[PH_LOAD_R_X].p_align   = oat_exec_alignment;
374 
375   // TODO: PF_W for DYNAMIC is considered processor specific, do we need it?
376   program_headers[PH_LOAD_RW_].p_type    = llvm::ELF::PT_LOAD;
377   program_headers[PH_LOAD_RW_].p_offset  = dynamic_offset;
378   program_headers[PH_LOAD_RW_].p_vaddr   = dynamic_offset;
379   program_headers[PH_LOAD_RW_].p_paddr   = dynamic_offset;
380   program_headers[PH_LOAD_RW_].p_filesz  = dynamic_size;
381   program_headers[PH_LOAD_RW_].p_memsz   = dynamic_size;
382   program_headers[PH_LOAD_RW_].p_flags   = llvm::ELF::PF_R | llvm::ELF::PF_W;
383   program_headers[PH_LOAD_RW_].p_align   = dynamic_alignment;
384 
385   // TODO: PF_W for DYNAMIC is considered processor specific, do we need it?
386   program_headers[PH_DYNAMIC].p_type    = llvm::ELF::PT_DYNAMIC;
387   program_headers[PH_DYNAMIC].p_offset  = dynamic_offset;
388   program_headers[PH_DYNAMIC].p_vaddr   = dynamic_offset;
389   program_headers[PH_DYNAMIC].p_paddr   = dynamic_offset;
390   program_headers[PH_DYNAMIC].p_filesz  = dynamic_size;
391   program_headers[PH_DYNAMIC].p_memsz   = dynamic_size;
392   program_headers[PH_DYNAMIC].p_flags   = llvm::ELF::PF_R | llvm::ELF::PF_W;
393   program_headers[PH_DYNAMIC].p_align   = dynamic_alignment;
394 
395   // .dynsym
396   llvm::ELF::Elf32_Sym dynsym[SYM_NUM];
397   memset(&dynsym, 0, sizeof(dynsym));
398 
399   dynsym[SYM_UNDEF].st_name  = 0;
400   dynsym[SYM_UNDEF].st_value = 0;
401   dynsym[SYM_UNDEF].st_size  = 0;
402   dynsym[SYM_UNDEF].st_info  = 0;
403   dynsym[SYM_UNDEF].st_other = 0;
404   dynsym[SYM_UNDEF].st_shndx = 0;
405 
406   dynsym[SYM_OATDATA].st_name  = dynstr_oatdata_offset;
407   dynsym[SYM_OATDATA].st_value = oat_data_offset;
408   dynsym[SYM_OATDATA].st_size  = oat_data_size;
409   dynsym[SYM_OATDATA].setBindingAndType(llvm::ELF::STB_GLOBAL, llvm::ELF::STT_OBJECT);
410   dynsym[SYM_OATDATA].st_other = llvm::ELF::STV_DEFAULT;
411   dynsym[SYM_OATDATA].st_shndx = SH_RODATA;
412 
413   dynsym[SYM_OATEXEC].st_name  = dynstr_oatexec_offset;
414   dynsym[SYM_OATEXEC].st_value = oat_exec_offset;
415   dynsym[SYM_OATEXEC].st_size  = oat_exec_size;
416   dynsym[SYM_OATEXEC].setBindingAndType(llvm::ELF::STB_GLOBAL, llvm::ELF::STT_OBJECT);
417   dynsym[SYM_OATEXEC].st_other = llvm::ELF::STV_DEFAULT;
418   dynsym[SYM_OATEXEC].st_shndx = SH_TEXT;
419 
420   dynsym[SYM_OATLASTWORD].st_name  = dynstr_oatlastword_offset;
421   dynsym[SYM_OATLASTWORD].st_value = oat_exec_offset + oat_exec_size - 4;
422   dynsym[SYM_OATLASTWORD].st_size  = 4;
423   dynsym[SYM_OATLASTWORD].setBindingAndType(llvm::ELF::STB_GLOBAL, llvm::ELF::STT_OBJECT);
424   dynsym[SYM_OATLASTWORD].st_other = llvm::ELF::STV_DEFAULT;
425   dynsym[SYM_OATLASTWORD].st_shndx = SH_TEXT;
426 
427   // .dynstr initialized above as dynstr
428 
429   // .hash
430   llvm::ELF::Elf32_Word hash[HASH_NUM];  // Note this is Elf32_Word even on 64-bit
431   hash[HASH_NBUCKET] = 1;
432   hash[HASH_NCHAIN]  = SYM_NUM;
433   hash[HASH_BUCKET0] = SYM_OATDATA;
434   hash[HASH_BUCKET0 + 1 + SYM_UNDEF]       = SYM_UNDEF;
435   hash[HASH_BUCKET0 + 1 + SYM_OATDATA]     = SYM_OATEXEC;
436   hash[HASH_BUCKET0 + 1 + SYM_OATEXEC]     = SYM_OATLASTWORD;
437   hash[HASH_BUCKET0 + 1 + SYM_OATLASTWORD] = SYM_UNDEF;
438 
439   // .rodata and .text content come from oat_contents
440 
441   // .dynamic
442   llvm::ELF::Elf32_Dyn dynamic_headers[DH_NUM];
443   memset(&dynamic_headers, 0, sizeof(dynamic_headers));
444 
445   dynamic_headers[DH_SONAME].d_tag = llvm::ELF::DT_SONAME;
446   dynamic_headers[DH_SONAME].d_un.d_val = dynstr_soname_offset;
447 
448   dynamic_headers[DH_HASH].d_tag = llvm::ELF::DT_HASH;
449   dynamic_headers[DH_HASH].d_un.d_ptr = hash_offset;
450 
451   dynamic_headers[DH_SYMTAB].d_tag = llvm::ELF::DT_SYMTAB;
452   dynamic_headers[DH_SYMTAB].d_un.d_ptr = dynsym_offset;
453 
454   dynamic_headers[DH_SYMENT].d_tag = llvm::ELF::DT_SYMENT;
455   dynamic_headers[DH_SYMENT].d_un.d_val = sizeof(llvm::ELF::Elf32_Sym);
456 
457   dynamic_headers[DH_STRTAB].d_tag = llvm::ELF::DT_STRTAB;
458   dynamic_headers[DH_STRTAB].d_un.d_ptr = dynstr_offset;
459 
460   dynamic_headers[DH_STRSZ].d_tag = llvm::ELF::DT_STRSZ;
461   dynamic_headers[DH_STRSZ].d_un.d_val = dynstr_size;
462 
463   dynamic_headers[DH_NULL].d_tag = llvm::ELF::DT_NULL;
464   dynamic_headers[DH_NULL].d_un.d_val = 0;
465 
466   // .shstrtab initialized above as shstrtab
467 
468   // section headers (after all sections)
469   llvm::ELF::Elf32_Shdr section_headers[SH_NUM];
470   memset(&section_headers, 0, sizeof(section_headers));
471 
472   section_headers[SH_NULL].sh_name      = 0;
473   section_headers[SH_NULL].sh_type      = llvm::ELF::SHT_NULL;
474   section_headers[SH_NULL].sh_flags     = 0;
475   section_headers[SH_NULL].sh_addr      = 0;
476   section_headers[SH_NULL].sh_offset    = 0;
477   section_headers[SH_NULL].sh_size      = 0;
478   section_headers[SH_NULL].sh_link      = 0;
479   section_headers[SH_NULL].sh_info      = 0;
480   section_headers[SH_NULL].sh_addralign = 0;
481   section_headers[SH_NULL].sh_entsize   = 0;
482 
483   section_headers[SH_DYNSYM].sh_name      = shstrtab_dynsym_offset;
484   section_headers[SH_DYNSYM].sh_type      = llvm::ELF::SHT_DYNSYM;
485   section_headers[SH_DYNSYM].sh_flags     = llvm::ELF::SHF_ALLOC;
486   section_headers[SH_DYNSYM].sh_addr      = dynsym_offset;
487   section_headers[SH_DYNSYM].sh_offset    = dynsym_offset;
488   section_headers[SH_DYNSYM].sh_size      = dynsym_size;
489   section_headers[SH_DYNSYM].sh_link      = SH_DYNSTR;
490   section_headers[SH_DYNSYM].sh_info      = 1;  // 1 because we have not STB_LOCAL symbols
491   section_headers[SH_DYNSYM].sh_addralign = dynsym_alignment;
492   section_headers[SH_DYNSYM].sh_entsize   = sizeof(llvm::ELF::Elf32_Sym);
493 
494   section_headers[SH_DYNSTR].sh_name      = shstrtab_dynstr_offset;
495   section_headers[SH_DYNSTR].sh_type      = llvm::ELF::SHT_STRTAB;
496   section_headers[SH_DYNSTR].sh_flags     = llvm::ELF::SHF_ALLOC;
497   section_headers[SH_DYNSTR].sh_addr      = dynstr_offset;
498   section_headers[SH_DYNSTR].sh_offset    = dynstr_offset;
499   section_headers[SH_DYNSTR].sh_size      = dynstr_size;
500   section_headers[SH_DYNSTR].sh_link      = 0;
501   section_headers[SH_DYNSTR].sh_info      = 0;
502   section_headers[SH_DYNSTR].sh_addralign = dynstr_alignment;
503   section_headers[SH_DYNSTR].sh_entsize   = 0;
504 
505   section_headers[SH_HASH].sh_name      = shstrtab_hash_offset;
506   section_headers[SH_HASH].sh_type      = llvm::ELF::SHT_HASH;
507   section_headers[SH_HASH].sh_flags     = llvm::ELF::SHF_ALLOC;
508   section_headers[SH_HASH].sh_addr      = hash_offset;
509   section_headers[SH_HASH].sh_offset    = hash_offset;
510   section_headers[SH_HASH].sh_size      = hash_size;
511   section_headers[SH_HASH].sh_link      = SH_DYNSYM;
512   section_headers[SH_HASH].sh_info      = 0;
513   section_headers[SH_HASH].sh_addralign = hash_alignment;
514   section_headers[SH_HASH].sh_entsize   = sizeof(llvm::ELF::Elf32_Word);  // This is Elf32_Word even on 64-bit
515 
516   section_headers[SH_RODATA].sh_name      = shstrtab_rodata_offset;
517   section_headers[SH_RODATA].sh_type      = llvm::ELF::SHT_PROGBITS;
518   section_headers[SH_RODATA].sh_flags     = llvm::ELF::SHF_ALLOC;
519   section_headers[SH_RODATA].sh_addr      = oat_data_offset;
520   section_headers[SH_RODATA].sh_offset    = oat_data_offset;
521   section_headers[SH_RODATA].sh_size      = oat_data_size;
522   section_headers[SH_RODATA].sh_link      = 0;
523   section_headers[SH_RODATA].sh_info      = 0;
524   section_headers[SH_RODATA].sh_addralign = oat_data_alignment;
525   section_headers[SH_RODATA].sh_entsize   = 0;
526 
527   section_headers[SH_TEXT].sh_name      = shstrtab_text_offset;
528   section_headers[SH_TEXT].sh_type      = llvm::ELF::SHT_PROGBITS;
529   section_headers[SH_TEXT].sh_flags     = llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR;
530   section_headers[SH_TEXT].sh_addr      = oat_exec_offset;
531   section_headers[SH_TEXT].sh_offset    = oat_exec_offset;
532   section_headers[SH_TEXT].sh_size      = oat_exec_size;
533   section_headers[SH_TEXT].sh_link      = 0;
534   section_headers[SH_TEXT].sh_info      = 0;
535   section_headers[SH_TEXT].sh_addralign = oat_exec_alignment;
536   section_headers[SH_TEXT].sh_entsize   = 0;
537 
538   // TODO: SHF_WRITE for .dynamic is considered processor specific, do we need it?
539   section_headers[SH_DYNAMIC].sh_name      = shstrtab_dynamic_offset;
540   section_headers[SH_DYNAMIC].sh_type      = llvm::ELF::SHT_DYNAMIC;
541   section_headers[SH_DYNAMIC].sh_flags     = llvm::ELF::SHF_WRITE | llvm::ELF::SHF_ALLOC;
542   section_headers[SH_DYNAMIC].sh_addr      = dynamic_offset;
543   section_headers[SH_DYNAMIC].sh_offset    = dynamic_offset;
544   section_headers[SH_DYNAMIC].sh_size      = dynamic_size;
545   section_headers[SH_DYNAMIC].sh_link      = SH_DYNSTR;
546   section_headers[SH_DYNAMIC].sh_info      = 0;
547   section_headers[SH_DYNAMIC].sh_addralign = dynamic_alignment;
548   section_headers[SH_DYNAMIC].sh_entsize   = sizeof(llvm::ELF::Elf32_Dyn);
549 
550   section_headers[SH_SHSTRTAB].sh_name      = shstrtab_shstrtab_offset;
551   section_headers[SH_SHSTRTAB].sh_type      = llvm::ELF::SHT_STRTAB;
552   section_headers[SH_SHSTRTAB].sh_flags     = 0;
553   section_headers[SH_SHSTRTAB].sh_addr      = shstrtab_offset;
554   section_headers[SH_SHSTRTAB].sh_offset    = shstrtab_offset;
555   section_headers[SH_SHSTRTAB].sh_size      = shstrtab_size;
556   section_headers[SH_SHSTRTAB].sh_link      = 0;
557   section_headers[SH_SHSTRTAB].sh_info      = 0;
558   section_headers[SH_SHSTRTAB].sh_addralign = shstrtab_alignment;
559   section_headers[SH_SHSTRTAB].sh_entsize   = 0;
560 
561   // phase 3: writing file
562 
563   // Elf32_Ehdr
564   if (!elf_file_->WriteFully(&elf_header, sizeof(elf_header))) {
565     PLOG(ERROR) << "Failed to write ELF header for " << elf_file_->GetPath();
566     return false;
567   }
568 
569   // PHDR
570   if (static_cast<off_t>(phdr_offset) != lseek(elf_file_->Fd(), 0, SEEK_CUR)) {
571     PLOG(ERROR) << "Failed to be at expected ELF program header offset phdr_offset "
572                 << " for " << elf_file_->GetPath();
573     return false;
574   }
575   if (!elf_file_->WriteFully(program_headers, sizeof(program_headers))) {
576     PLOG(ERROR) << "Failed to write ELF program headers for " << elf_file_->GetPath();
577     return false;
578   }
579 
580   // .dynsym
581   DCHECK_LE(phdr_offset + phdr_size, dynsym_offset);
582   if (static_cast<off_t>(dynsym_offset) != lseek(elf_file_->Fd(), dynsym_offset, SEEK_SET)) {
583     PLOG(ERROR) << "Failed to seek to .dynsym offset location " << dynsym_offset
584                 << " for " << elf_file_->GetPath();
585     return false;
586   }
587   if (!elf_file_->WriteFully(dynsym, sizeof(dynsym))) {
588     PLOG(ERROR) << "Failed to write .dynsym for " << elf_file_->GetPath();
589     return false;
590   }
591 
592   // .dynstr
593   DCHECK_LE(dynsym_offset + dynsym_size, dynstr_offset);
594   if (static_cast<off_t>(dynstr_offset) != lseek(elf_file_->Fd(), dynstr_offset, SEEK_SET)) {
595     PLOG(ERROR) << "Failed to seek to .dynstr offset " << dynstr_offset
596                 << " for " << elf_file_->GetPath();
597     return false;
598   }
599   if (!elf_file_->WriteFully(&dynstr[0], dynstr_size)) {
600     PLOG(ERROR) << "Failed to write .dynsym for " << elf_file_->GetPath();
601     return false;
602   }
603 
604   // .hash
605   DCHECK_LE(dynstr_offset + dynstr_size, hash_offset);
606   if (static_cast<off_t>(hash_offset) != lseek(elf_file_->Fd(), hash_offset, SEEK_SET)) {
607     PLOG(ERROR) << "Failed to seek to .hash offset " << hash_offset
608                 << " for " << elf_file_->GetPath();
609     return false;
610   }
611   if (!elf_file_->WriteFully(hash, sizeof(hash))) {
612     PLOG(ERROR) << "Failed to write .dynsym for " << elf_file_->GetPath();
613     return false;
614   }
615 
616   // .rodata .text
617   DCHECK_LE(hash_offset + hash_size, oat_data_offset);
618   if (static_cast<off_t>(oat_data_offset) != lseek(elf_file_->Fd(), oat_data_offset, SEEK_SET)) {
619     PLOG(ERROR) << "Failed to seek to .rodata offset " << oat_data_offset
620                 << " for " << elf_file_->GetPath();
621     return false;
622   }
623   BufferedOutputStream output_stream(new FileOutputStream(elf_file_));
624   if (!oat_writer.Write(output_stream)) {
625     PLOG(ERROR) << "Failed to write .rodata and .text for " << elf_file_->GetPath();
626     return false;
627   }
628 
629   // .dynamic
630   DCHECK_LE(oat_data_offset + oat_writer.GetSize(), dynamic_offset);
631   if (static_cast<off_t>(dynamic_offset) != lseek(elf_file_->Fd(), dynamic_offset, SEEK_SET)) {
632     PLOG(ERROR) << "Failed to seek to .dynamic offset " << dynamic_offset
633                 << " for " << elf_file_->GetPath();
634     return false;
635   }
636   if (!elf_file_->WriteFully(&dynamic_headers[0], dynamic_size)) {
637     PLOG(ERROR) << "Failed to write .dynamic for " << elf_file_->GetPath();
638     return false;
639   }
640 
641   // .shstrtab
642   DCHECK_LE(dynamic_offset + dynamic_size, shstrtab_offset);
643   if (static_cast<off_t>(shstrtab_offset) != lseek(elf_file_->Fd(), shstrtab_offset, SEEK_SET)) {
644     PLOG(ERROR) << "Failed to seek to .shstrtab offset " << shstrtab_offset
645                 << " for " << elf_file_->GetPath();
646     return false;
647   }
648   if (!elf_file_->WriteFully(&shstrtab[0], shstrtab_size)) {
649     PLOG(ERROR) << "Failed to write .shstrtab for " << elf_file_->GetPath();
650     return false;
651   }
652 
653   // section headers (after all sections)
654   DCHECK_LE(shstrtab_offset + shstrtab_size, shdr_offset);
655   if (static_cast<off_t>(shdr_offset) != lseek(elf_file_->Fd(), shdr_offset, SEEK_SET)) {
656     PLOG(ERROR) << "Failed to seek to ELF section headers offset " << shdr_offset
657                 << " for " << elf_file_->GetPath();
658     return false;
659   }
660   if (!elf_file_->WriteFully(section_headers, sizeof(section_headers))) {
661     PLOG(ERROR) << "Failed to write ELF section headers for " << elf_file_->GetPath();
662     return false;
663   }
664 
665   VLOG(compiler) << "ELF file written successfully: " << elf_file_->GetPath();
666   return true;
667 }
668 
669 }  // namespace art
670