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