elf_writer_quick.cc revision 8758c64a40e74ebb492a348556ec7b25a89c1491
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 "elf_utils.h"
24#include "file_output_stream.h"
25#include "globals.h"
26#include "oat.h"
27#include "oat_writer.h"
28#include "utils.h"
29
30namespace art {
31
32static constexpr Elf32_Word NextOffset(const Elf32_Shdr& cur, const Elf32_Shdr& prev) {
33  return RoundUp(prev.sh_size + prev.sh_offset, cur.sh_addralign);
34}
35
36static 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.
42  // +-------------------------+
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                   |
65  // | 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] |
73  // +-------------------------+
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       |
88  // +-------------------------+  (Optional)
89  // | .strtab                 |  (Optional)
90  // | program symbol names    |  (Optional)
91  // +-------------------------+  (Optional)
92  // | .symtab                 |  (Optional)
93  // | program symbols         |  (Optional)
94  // +-------------------------+
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             |
104  // | .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)
118  // +-------------------------+
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    |
127  // | Elf32_Shdr .debug_str   |  (Optional)
128  // | Elf32_Shdr .debug_info  |  (Optional)
129  // | Elf32_Shdr .debug_frame |  (Optional)
130  // | Elf32_Shdr .debug_abbrev|  (Optional)
131  // +-------------------------+
132
133
134  if (fatal_error_) {
135    return false;
136  }
137  // Step 1. Figure out all the offsets.
138
139  // What phdr is.
140  uint32_t phdr_offset = sizeof(Elf32_Ehdr);
141  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;
147  uint32_t phdr_size = sizeof(Elf32_Phdr) * PH_NUM;
148  if (debug_logging_) {
149    LOG(INFO) << "phdr_offset=" << phdr_offset << std::hex << " " << phdr_offset;
150    LOG(INFO) << "phdr_size=" << phdr_size << std::hex << " " << phdr_size;
151  }
152  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);
162
163  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;
168
169  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.
182  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';
190  if (debug_logging_) {
191    LOG(INFO) << "dynstr size (bytes)   =" << dynstr.size()
192              << std::hex << " " << dynstr.size();
193    LOG(INFO) << "dynsym size (elements)=" << dynsym_builder_.GetSize()
194              << std::hex << " " << dynsym_builder_.GetSize();
195  }
196
197  // 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();
204      LOG(INFO) << "symtab size (elements) =" << symtab_builder_.GetSize()
205                << std::hex << " " << symtab_builder_.GetSize();
206    }
207  }
208
209  // Get the section header string table.
210  std::vector<Elf32_Shdr*> section_ptrs;
211  std::string shstrtab;
212  shstrtab += '\0';
213
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++;
270  }
271
272  // 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();
282  }
283
284  // 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);
290  }
291
292  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;
300  dynsym_builder_.section_.sh_size = dynsym_builder_.GetSize() * sizeof(Elf32_Sym);
301  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;
335  dynamic_builder_.section_.sh_size = dynamic_builder_.GetSize() * sizeof(Elf32_Dyn);
336  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.
345    symtab_builder_.section_.sh_size = symtab_builder_.GetSize() * sizeof(Elf32_Sym);
346    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_;
356  }
357  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;
375    }
376  }
377  // 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;
400  }
401
402  // 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));
406
407  // Setup the actual symbol arrays.
408  std::vector<Elf32_Sym> dynsym = dynsym_builder_.GenerateSymtab();
409  CHECK_EQ(dynsym.size() * sizeof(Elf32_Sym), dynsym_builder_.section_.sh_size);
410  std::vector<Elf32_Sym> symtab;
411  if (IncludingDebugSymbols()) {
412    symtab = symtab_builder_.GenerateSymtab();
413    CHECK_EQ(symtab.size() * sizeof(Elf32_Sym), symtab_builder_.section_.sh_size);
414  }
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);
421  CHECK_EQ(dynamic.size() * sizeof(Elf32_Dyn), dynamic_builder_.section_.sh_size);
422
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,
466                                dynsym.data(), dynsym.size() * sizeof(Elf32_Sym)));
467  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();
493    return false;
494  }
495  // write out the actual oat file data.
496  Elf32_Word oat_data_offset = rodata_builder_.section_.sh_offset;
497  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  }
502  std::unique_ptr<BufferedOutputStream> output_stream(
503      new BufferedOutputStream(new FileOutputStream(elf_file_)));
504  if (!oat_writer_->Write(output_stream.get())) {
505    PLOG(ERROR) << "Failed to write .rodata and .text for " << elf_file_->GetPath();
506    return false;
507  }
508
509  return true;
510}
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
546void ElfWriterQuick::ElfDynamicBuilder::AddDynamicTag(Elf32_Sword tag, Elf32_Word d_un) {
547  if (tag == DT_NULL) {
548    return;
549  }
550  dynamics_.push_back({NULL, tag, d_un});
551}
552
553void ElfWriterQuick::ElfDynamicBuilder::AddDynamicTag(Elf32_Sword tag, Elf32_Word d_un,
554                                                      ElfSectionBuilder* section) {
555  if (tag == DT_NULL) {
556    return;
557  }
558  dynamics_.push_back({section, tag, d_un});
559}
560
561std::vector<Elf32_Dyn> ElfWriterQuick::ElfDynamicBuilder::GetDynamics(Elf32_Word strsz,
562                                                                      Elf32_Word soname) {
563  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(
568          {it->tag_, {it->off_ + it->section_->section_.sh_addr}});
569    } 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;
678  Elf32_Word chain_size = GetSize();
679  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;
810  const OatHeader& oat_header = oat_writer->GetOatHeader();
811  Elf32_Word oat_data_size = oat_header.GetExecutableOffset();
812  uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size;
813
814  ElfBuilder builder(oat_writer, elf_file_, compiler_driver_->GetInstructionSet(), 0,
815                     oat_data_size, oat_data_size, oat_exec_size, false, debug);
816
817  bool generateDebugInformation = compiler_driver_->GetCallFrameInformation() != nullptr;
818  if (generateDebugInformation) {
819    ElfRawSectionBuilder debug_info(".debug_info",   SHT_PROGBITS, 0, NULL, 0, 1, 0);
820    ElfRawSectionBuilder debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, NULL, 0, 1, 0);
821    ElfRawSectionBuilder debug_str(".debug_str",    SHT_PROGBITS, 0, NULL, 0, 1, 0);
822    ElfRawSectionBuilder debug_frame(".debug_frame",  SHT_PROGBITS, 0, NULL, 0, 4, 0);
823    debug_frame.SetBuffer(*compiler_driver_->GetCallFrameInformation());
824
825    FillInCFIInformation(oat_writer, debug_info.GetBuffer(),
826                         debug_abbrev.GetBuffer(), debug_str.GetBuffer());
827    builder.RegisterRawSection(debug_info);
828    builder.RegisterRawSection(debug_abbrev);
829    builder.RegisterRawSection(debug_frame);
830    builder.RegisterRawSection(debug_str);
831  }
832
833  return builder.Write();
834}
835
836static void UpdateWord(std::vector<uint8_t>*buf, int offset, int data) {
837  (*buf)[offset+0] = data;
838  (*buf)[offset+1] = data >> 8;
839  (*buf)[offset+2] = data >> 16;
840  (*buf)[offset+3] = data >> 24;
841}
842
843static void PushWord(std::vector<uint8_t>*buf, int data) {
844  buf->push_back(data & 0xff);
845  buf->push_back((data >> 8) & 0xff);
846  buf->push_back((data >> 16) & 0xff);
847  buf->push_back((data >> 24) & 0xff);
848}
849
850static void PushHalf(std::vector<uint8_t>*buf, int data) {
851  buf->push_back(data & 0xff);
852  buf->push_back((data >> 8) & 0xff);
853}
854
855// DWARF constants needed to generate CFI information.
856enum {
857  // Tag encodings.
858  DW_TAG_compile_unit = 0x11,
859  DW_TAG_subprogram = 0X2e,
860
861  // Attribute encodings.
862  DW_AT_name = 0x03,
863  DW_AT_low_pc = 0x11,
864  DW_AT_high_pc = 0x12,
865  DW_AT_language = 0x13,
866
867  // Constant encoding.
868  DW_CHILDREN_no = 0x00,
869  DW_CHILDREN_yes = 0x01,
870
871  // Attribute form encodings.
872  DW_FORM_addr = 0x01,
873  DW_FORM_data1 = 0x0b,
874  DW_FORM_strp = 0x0e,
875
876  // Language encoding.
877  DW_LANG_Java = 0x000b
878};
879
880void ElfWriterQuick::FillInCFIInformation(OatWriter* oat_writer,
881                                          std::vector<uint8_t>* dbg_info,
882                                          std::vector<uint8_t>* dbg_abbrev,
883                                          std::vector<uint8_t>* dbg_str) {
884  // Create the debug_abbrev section with boilerplate information.
885  // We only care about low_pc and high_pc right now for the compilation
886  // unit and methods.
887
888  // Tag 1: Compilation unit: DW_TAG_compile_unit.
889  dbg_abbrev->push_back(1);
890  dbg_abbrev->push_back(DW_TAG_compile_unit);
891
892  // There are children (the methods).
893  dbg_abbrev->push_back(DW_CHILDREN_yes);
894
895  // DW_LANG_Java DW_FORM_data1.
896  dbg_abbrev->push_back(DW_AT_language);
897  dbg_abbrev->push_back(DW_FORM_data1);
898
899  // DW_AT_low_pc DW_FORM_addr.
900  dbg_abbrev->push_back(DW_AT_low_pc);
901  dbg_abbrev->push_back(DW_FORM_addr);
902
903  // DW_AT_high_pc DW_FORM_addr.
904  dbg_abbrev->push_back(DW_AT_high_pc);
905  dbg_abbrev->push_back(DW_FORM_addr);
906
907  // End of DW_TAG_compile_unit.
908  PushHalf(dbg_abbrev, 0);
909
910  // Tag 2: Compilation unit: DW_TAG_subprogram.
911  dbg_abbrev->push_back(2);
912  dbg_abbrev->push_back(DW_TAG_subprogram);
913
914  // There are no children.
915  dbg_abbrev->push_back(DW_CHILDREN_no);
916
917  // Name of the method.
918  dbg_abbrev->push_back(DW_AT_name);
919  dbg_abbrev->push_back(DW_FORM_strp);
920
921  // DW_AT_low_pc DW_FORM_addr.
922  dbg_abbrev->push_back(DW_AT_low_pc);
923  dbg_abbrev->push_back(DW_FORM_addr);
924
925  // DW_AT_high_pc DW_FORM_addr.
926  dbg_abbrev->push_back(DW_AT_high_pc);
927  dbg_abbrev->push_back(DW_FORM_addr);
928
929  // End of DW_TAG_subprogram.
930  PushHalf(dbg_abbrev, 0);
931
932  // Start the debug_info section with the header information
933  // 'unit_length' will be filled in later.
934  PushWord(dbg_info, 0);
935
936  // 'version' - 3.
937  PushHalf(dbg_info, 3);
938
939  // Offset into .debug_abbrev section (always 0).
940  PushWord(dbg_info, 0);
941
942  // Address size: 4.
943  dbg_info->push_back(4);
944
945  // Start the description for the compilation unit.
946  // This uses tag 1.
947  dbg_info->push_back(1);
948
949  // The language is Java.
950  dbg_info->push_back(DW_LANG_Java);
951
952  // Leave space for low_pc and high_pc.
953  int low_pc_offset = dbg_info->size();
954  PushWord(dbg_info, 0);
955  PushWord(dbg_info, 0);
956
957  // Walk through the information in the method table, and enter into dbg_info.
958  const std::vector<OatWriter::DebugInfo>& dbg = oat_writer->GetCFIMethodInfo();
959  uint32_t low_pc = 0xFFFFFFFFU;
960  uint32_t high_pc = 0;
961
962  for (uint32_t i = 0; i < dbg.size(); i++) {
963    const OatWriter::DebugInfo& info = dbg[i];
964    if (info.low_pc_ < low_pc) {
965      low_pc = info.low_pc_;
966    }
967    if (info.high_pc_ > high_pc) {
968      high_pc = info.high_pc_;
969    }
970
971    // Start a new TAG: subroutine (2).
972    dbg_info->push_back(2);
973
974    // Enter the name into the string table (and NUL terminate).
975    uint32_t str_offset = dbg_str->size();
976    dbg_str->insert(dbg_str->end(), info.method_name_.begin(), info.method_name_.end());
977    dbg_str->push_back('\0');
978
979    // Enter name, low_pc, high_pc.
980    PushWord(dbg_info, str_offset);
981    PushWord(dbg_info, info.low_pc_);
982    PushWord(dbg_info, info.high_pc_);
983  }
984
985  // One byte terminator
986  dbg_info->push_back(0);
987
988  // We have now walked all the methods.  Fill in lengths and low/high PCs.
989  UpdateWord(dbg_info, 0, dbg_info->size() - 4);
990  UpdateWord(dbg_info, low_pc_offset, low_pc);
991  UpdateWord(dbg_info, low_pc_offset + 4, high_pc);
992}
993
994}  // namespace art
995