Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 1 | /* |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 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 | #ifndef ART_COMPILER_ELF_BUILDER_H_ |
| 18 | #define ART_COMPILER_ELF_BUILDER_H_ |
| 19 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 22 | #include "arch/instruction_set.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "base/bit_utils.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 24 | #include "base/casts.h" |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 25 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 26 | #include "elf_utils.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 27 | #include "leb128.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 28 | #include "linker/error_delaying_output_stream.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 29 | #include "utils/array_ref.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 33 | // Writes ELF file. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 34 | // |
| 35 | // The basic layout of the elf file: |
| 36 | // Elf_Ehdr - The ELF header. |
| 37 | // Elf_Phdr[] - Program headers for the linker. |
| 38 | // .rodata - DEX files and oat metadata. |
| 39 | // .text - Compiled code. |
| 40 | // .bss - Zero-initialized writeable section. |
| 41 | // .dynstr - Names for .dynsym. |
| 42 | // .dynsym - A few oat-specific dynamic symbols. |
| 43 | // .hash - Hash-table for .dynsym. |
| 44 | // .dynamic - Tags which let the linker locate .dynsym. |
| 45 | // .strtab - Names for .symtab. |
| 46 | // .symtab - Debug symbols. |
| 47 | // .eh_frame - Unwind information (CFI). |
| 48 | // .eh_frame_hdr - Index of .eh_frame. |
| 49 | // .debug_frame - Unwind information (CFI). |
| 50 | // .debug_frame.oat_patches - Addresses for relocation. |
| 51 | // .debug_info - Debug information. |
| 52 | // .debug_info.oat_patches - Addresses for relocation. |
| 53 | // .debug_abbrev - Decoding information for .debug_info. |
| 54 | // .debug_str - Strings for .debug_info. |
| 55 | // .debug_line - Line number tables. |
| 56 | // .debug_line.oat_patches - Addresses for relocation. |
| 57 | // .text.oat_patches - Addresses for relocation. |
| 58 | // .shstrtab - Names of ELF sections. |
| 59 | // Elf_Shdr[] - Section headers. |
| 60 | // |
| 61 | // Some section are optional (the debug sections in particular). |
| 62 | // |
| 63 | // We try write the section data directly into the file without much |
| 64 | // in-memory buffering. This means we generally write sections based on the |
| 65 | // dependency order (e.g. .dynamic points to .dynsym which points to .text). |
| 66 | // |
| 67 | // In the cases where we need to buffer, we write the larger section first |
| 68 | // and buffer the smaller one (e.g. .strtab is bigger than .symtab). |
| 69 | // |
| 70 | // The debug sections are written last for easier stripping. |
| 71 | // |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 72 | template <typename ElfTypes> |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 73 | class ElfBuilder FINAL { |
| 74 | public: |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 75 | static constexpr size_t kMaxProgramHeaders = 16; |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 76 | using Elf_Addr = typename ElfTypes::Addr; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 77 | using Elf_Off = typename ElfTypes::Off; |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 78 | using Elf_Word = typename ElfTypes::Word; |
| 79 | using Elf_Sword = typename ElfTypes::Sword; |
| 80 | using Elf_Ehdr = typename ElfTypes::Ehdr; |
| 81 | using Elf_Shdr = typename ElfTypes::Shdr; |
| 82 | using Elf_Sym = typename ElfTypes::Sym; |
| 83 | using Elf_Phdr = typename ElfTypes::Phdr; |
| 84 | using Elf_Dyn = typename ElfTypes::Dyn; |
| 85 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 86 | // Base class of all sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 87 | class Section : public OutputStream { |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 88 | public: |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 89 | Section(ElfBuilder<ElfTypes>* owner, const std::string& name, |
| 90 | Elf_Word type, Elf_Word flags, const Section* link, |
| 91 | Elf_Word info, Elf_Word align, Elf_Word entsize) |
| 92 | : OutputStream(name), owner_(owner), header_(), |
| 93 | section_index_(0), name_(name), link_(link), |
| 94 | started_(false), finished_(false), phdr_flags_(PF_R), phdr_type_(0) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 95 | DCHECK_GE(align, 1u); |
David Srbecky | 491a7fe | 2015-05-28 00:59:08 +0100 | [diff] [blame] | 96 | header_.sh_type = type; |
| 97 | header_.sh_flags = flags; |
| 98 | header_.sh_info = info; |
| 99 | header_.sh_addralign = align; |
| 100 | header_.sh_entsize = entsize; |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 101 | } |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 102 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 103 | // Start writing of this section. |
| 104 | void Start() { |
| 105 | CHECK(!started_); |
| 106 | CHECK(!finished_); |
| 107 | started_ = true; |
| 108 | auto& sections = owner_->sections_; |
| 109 | // Check that the previous section is complete. |
| 110 | CHECK(sections.empty() || sections.back()->finished_); |
| 111 | // The first ELF section index is 1. Index 0 is reserved for NULL. |
| 112 | section_index_ = sections.size() + 1; |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 113 | // Page-align if we switch between allocated and non-allocated sections, |
| 114 | // or if we change the type of allocation (e.g. executable vs non-executable). |
| 115 | if (!sections.empty()) { |
| 116 | if (header_.sh_flags != sections.back()->header_.sh_flags) { |
| 117 | header_.sh_addralign = kPageSize; |
| 118 | } |
| 119 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 120 | // Align file position. |
| 121 | if (header_.sh_type != SHT_NOBITS) { |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 122 | header_.sh_offset = owner_->AlignFileOffset(header_.sh_addralign); |
| 123 | } else { |
| 124 | header_.sh_offset = 0; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 125 | } |
| 126 | // Align virtual memory address. |
| 127 | if ((header_.sh_flags & SHF_ALLOC) != 0) { |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 128 | header_.sh_addr = owner_->AlignVirtualAddress(header_.sh_addralign); |
| 129 | } else { |
| 130 | header_.sh_addr = 0; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 131 | } |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 132 | // Push this section on the list of written sections. |
| 133 | sections.push_back(this); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 134 | } |
| 135 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 136 | // Finish writing of this section. |
| 137 | void End() { |
| 138 | CHECK(started_); |
| 139 | CHECK(!finished_); |
| 140 | finished_ = true; |
| 141 | if (header_.sh_type == SHT_NOBITS) { |
| 142 | CHECK_GT(header_.sh_size, 0u); |
| 143 | } else { |
| 144 | // Use the current file position to determine section size. |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 145 | off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 146 | CHECK_GE(file_offset, (off_t)header_.sh_offset); |
| 147 | header_.sh_size = file_offset - header_.sh_offset; |
| 148 | } |
| 149 | if ((header_.sh_flags & SHF_ALLOC) != 0) { |
| 150 | owner_->virtual_address_ += header_.sh_size; |
| 151 | } |
| 152 | } |
| 153 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 154 | // Returns true if the section was written to disk. |
| 155 | // (Used to check whether we have .text when writing JIT debug info) |
| 156 | bool Exists() const { |
| 157 | return finished_; |
| 158 | } |
| 159 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 160 | // Get the location of this section in virtual memory. |
| 161 | Elf_Addr GetAddress() const { |
| 162 | CHECK(started_); |
| 163 | return header_.sh_addr; |
| 164 | } |
| 165 | |
| 166 | // Returns the size of the content of this section. |
| 167 | Elf_Word GetSize() const { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 168 | if (finished_) { |
| 169 | return header_.sh_size; |
| 170 | } else { |
| 171 | CHECK(started_); |
| 172 | CHECK_NE(header_.sh_type, (Elf_Word)SHT_NOBITS); |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 173 | return owner_->stream_.Seek(0, kSeekCurrent) - header_.sh_offset; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 174 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 175 | } |
| 176 | |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 177 | // Write this section as "NOBITS" section. (used for the .bss section) |
| 178 | // This means that the ELF file does not contain the initial data for this section |
| 179 | // and it will be zero-initialized when the ELF file is loaded in the running program. |
| 180 | void WriteNoBitsSection(Elf_Word size) { |
| 181 | DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u); |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 182 | header_.sh_type = SHT_NOBITS; |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 183 | Start(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 184 | header_.sh_size = size; |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 185 | End(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // This function always succeeds to simplify code. |
| 189 | // Use builder's Good() to check the actual status. |
| 190 | bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE { |
| 191 | CHECK(started_); |
| 192 | CHECK(!finished_); |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 193 | return owner_->stream_.WriteFully(buffer, byte_count); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // This function always succeeds to simplify code. |
| 197 | // Use builder's Good() to check the actual status. |
| 198 | off_t Seek(off_t offset, Whence whence) OVERRIDE { |
| 199 | // Forward the seek as-is and trust the caller to use it reasonably. |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 200 | return owner_->stream_.Seek(offset, whence); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 203 | // This function flushes the output and returns whether it succeeded. |
| 204 | // If there was a previous failure, this does nothing and returns false, i.e. failed. |
| 205 | bool Flush() OVERRIDE { |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 206 | return owner_->stream_.Flush(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 207 | } |
| 208 | |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 209 | Elf_Word GetSectionIndex() const { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 210 | DCHECK(started_); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 211 | DCHECK_NE(section_index_, 0u); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 212 | return section_index_; |
| 213 | } |
| 214 | |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 215 | private: |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 216 | ElfBuilder<ElfTypes>* owner_; |
David Srbecky | 491a7fe | 2015-05-28 00:59:08 +0100 | [diff] [blame] | 217 | Elf_Shdr header_; |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 218 | Elf_Word section_index_; |
| 219 | const std::string name_; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 220 | const Section* const link_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 221 | bool started_; |
| 222 | bool finished_; |
| 223 | Elf_Word phdr_flags_; |
| 224 | Elf_Word phdr_type_; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 225 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 226 | friend class ElfBuilder; |
David Srbecky | 4d247f7 | 2015-11-09 11:56:52 +0000 | [diff] [blame] | 227 | |
| 228 | DISALLOW_COPY_AND_ASSIGN(Section); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 229 | }; |
| 230 | |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 231 | // Writer of .dynstr .strtab and .shstrtab sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 232 | class StringSection FINAL : public Section { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 233 | public: |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 234 | StringSection(ElfBuilder<ElfTypes>* owner, const std::string& name, |
| 235 | Elf_Word flags, Elf_Word align) |
| 236 | : Section(owner, name, SHT_STRTAB, flags, nullptr, 0, align, 0), |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 237 | current_offset_(0) { |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 238 | } |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 239 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 240 | Elf_Word Write(const std::string& name) { |
| 241 | if (current_offset_ == 0) { |
| 242 | DCHECK(name.empty()); |
| 243 | } |
| 244 | Elf_Word offset = current_offset_; |
| 245 | this->WriteFully(name.c_str(), name.length() + 1); |
| 246 | current_offset_ += name.length() + 1; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 247 | return offset; |
| 248 | } |
| 249 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 250 | private: |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 251 | Elf_Word current_offset_; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 252 | }; |
| 253 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 254 | // Writer of .dynsym and .symtab sections. |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 255 | class SymbolSection FINAL : public Section { |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 256 | public: |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 257 | SymbolSection(ElfBuilder<ElfTypes>* owner, const std::string& name, |
| 258 | Elf_Word type, Elf_Word flags, StringSection* strtab) |
| 259 | : Section(owner, name, type, flags, strtab, 0, |
| 260 | sizeof(Elf_Off), sizeof(Elf_Sym)) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // Buffer symbol for this section. It will be written later. |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 264 | // If the symbol's section is null, it will be considered absolute (SHN_ABS). |
| 265 | // (we use this in JIT to reference code which is stored outside the debug ELF file) |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 266 | void Add(Elf_Word name, const Section* section, |
| 267 | Elf_Addr addr, bool is_relative, Elf_Word size, |
| 268 | uint8_t binding, uint8_t type, uint8_t other = 0) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 269 | Elf_Sym sym = Elf_Sym(); |
| 270 | sym.st_name = name; |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 271 | sym.st_value = addr + (is_relative ? section->GetAddress() : 0); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 272 | sym.st_size = size; |
| 273 | sym.st_other = other; |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 274 | sym.st_shndx = (section != nullptr ? section->GetSectionIndex() |
| 275 | : static_cast<Elf_Word>(SHN_ABS)); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 276 | sym.st_info = (binding << 4) + (type & 0xf); |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 277 | symbols_.push_back(sym); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 278 | } |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 279 | |
| 280 | void Write() { |
| 281 | // The symbol table always has to start with NULL symbol. |
| 282 | Elf_Sym null_symbol = Elf_Sym(); |
| 283 | this->WriteFully(&null_symbol, sizeof(null_symbol)); |
| 284 | this->WriteFully(symbols_.data(), symbols_.size() * sizeof(symbols_[0])); |
| 285 | symbols_.clear(); |
| 286 | symbols_.shrink_to_fit(); |
| 287 | } |
| 288 | |
| 289 | private: |
| 290 | std::vector<Elf_Sym> symbols_; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 291 | }; |
| 292 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 293 | ElfBuilder(InstructionSet isa, OutputStream* output) |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 294 | : isa_(isa), |
| 295 | stream_(output), |
| 296 | rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), |
| 297 | text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0), |
| 298 | bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), |
| 299 | dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize), |
| 300 | dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_), |
| 301 | hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)), |
| 302 | dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)), |
| 303 | eh_frame_(this, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), |
| 304 | eh_frame_hdr_(this, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0), |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 305 | strtab_(this, ".strtab", 0, 1), |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 306 | symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_), |
| 307 | debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0), |
| 308 | debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0), |
| 309 | debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0), |
| 310 | shstrtab_(this, ".shstrtab", 0, 1), |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 311 | started_(false), |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 312 | virtual_address_(0) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 313 | text_.phdr_flags_ = PF_R | PF_X; |
| 314 | bss_.phdr_flags_ = PF_R | PF_W; |
| 315 | dynamic_.phdr_flags_ = PF_R | PF_W; |
| 316 | dynamic_.phdr_type_ = PT_DYNAMIC; |
| 317 | eh_frame_hdr_.phdr_type_ = PT_GNU_EH_FRAME; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 318 | } |
| 319 | ~ElfBuilder() {} |
| 320 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 321 | InstructionSet GetIsa() { return isa_; } |
| 322 | Section* GetRoData() { return &rodata_; } |
| 323 | Section* GetText() { return &text_; } |
| 324 | Section* GetBss() { return &bss_; } |
| 325 | StringSection* GetStrTab() { return &strtab_; } |
| 326 | SymbolSection* GetSymTab() { return &symtab_; } |
| 327 | Section* GetEhFrame() { return &eh_frame_; } |
| 328 | Section* GetEhFrameHdr() { return &eh_frame_hdr_; } |
| 329 | Section* GetDebugFrame() { return &debug_frame_; } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 330 | Section* GetDebugInfo() { return &debug_info_; } |
| 331 | Section* GetDebugLine() { return &debug_line_; } |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 332 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 333 | // Encode patch locations as LEB128 list of deltas between consecutive addresses. |
| 334 | // (exposed publicly for tests) |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 335 | static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 336 | std::vector<uint8_t>* buffer) { |
| 337 | buffer->reserve(buffer->size() + locations.size() * 2); // guess 2 bytes per ULEB128. |
| 338 | uintptr_t address = 0; // relative to start of section. |
| 339 | for (uintptr_t location : locations) { |
| 340 | DCHECK_GE(location, address) << "Patch locations are not in sorted order"; |
| 341 | EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address)); |
| 342 | address = location; |
| 343 | } |
| 344 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 345 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 346 | void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 347 | std::vector<uint8_t> buffer; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 348 | EncodeOatPatches(patch_locations, &buffer); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 349 | std::unique_ptr<Section> s(new Section(this, name, SHT_OAT_PATCH, 0, nullptr, 0, 1, 0)); |
| 350 | s->Start(); |
| 351 | s->WriteFully(buffer.data(), buffer.size()); |
| 352 | s->End(); |
| 353 | other_sections_.push_back(std::move(s)); |
| 354 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 355 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 356 | void WriteSection(const char* name, const std::vector<uint8_t>* buffer) { |
| 357 | std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0)); |
| 358 | s->Start(); |
| 359 | s->WriteFully(buffer->data(), buffer->size()); |
| 360 | s->End(); |
| 361 | other_sections_.push_back(std::move(s)); |
| 362 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 363 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 364 | // Reserve space for ELF header and program headers. |
| 365 | // We do not know the number of headers until later, so |
| 366 | // it is easiest to just reserve a fixed amount of space. |
| 367 | // Program headers are required for loading by the linker. |
| 368 | // It is possible to omit them for ELF files used for debugging. |
| 369 | void Start(bool write_program_headers = true) { |
| 370 | int size = sizeof(Elf_Ehdr); |
| 371 | if (write_program_headers) { |
| 372 | size += sizeof(Elf_Phdr) * kMaxProgramHeaders; |
| 373 | } |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 374 | stream_.Seek(size, kSeekSet); |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 375 | started_ = true; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 376 | virtual_address_ += size; |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 377 | write_program_headers_ = write_program_headers; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 378 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 379 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 380 | void End() { |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 381 | DCHECK(started_); |
| 382 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 383 | // Write section names and finish the section headers. |
| 384 | shstrtab_.Start(); |
| 385 | shstrtab_.Write(""); |
| 386 | for (auto* section : sections_) { |
| 387 | section->header_.sh_name = shstrtab_.Write(section->name_); |
| 388 | if (section->link_ != nullptr) { |
| 389 | section->header_.sh_link = section->link_->GetSectionIndex(); |
David Srbecky | b0a962c | 2015-04-28 19:43:56 +0100 | [diff] [blame] | 390 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 391 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 392 | shstrtab_.End(); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 393 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 394 | // Write section headers at the end of the ELF file. |
| 395 | std::vector<Elf_Shdr> shdrs; |
| 396 | shdrs.reserve(1u + sections_.size()); |
| 397 | shdrs.push_back(Elf_Shdr()); // NULL at index 0. |
| 398 | for (auto* section : sections_) { |
| 399 | shdrs.push_back(section->header_); |
| 400 | } |
| 401 | Elf_Off section_headers_offset; |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 402 | section_headers_offset = AlignFileOffset(sizeof(Elf_Off)); |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 403 | stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0])); |
| 404 | |
| 405 | // Flush everything else before writing the program headers. This should prevent |
| 406 | // the OS from reordering writes, so that we don't end up with valid headers |
| 407 | // and partially written data if we suddenly lose power, for example. |
| 408 | stream_.Flush(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 409 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 410 | // The main ELF header. |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 411 | Elf_Ehdr elf_header = MakeElfHeader(isa_); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 412 | elf_header.e_shoff = section_headers_offset; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 413 | elf_header.e_shnum = shdrs.size(); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 414 | elf_header.e_shstrndx = shstrtab_.GetSectionIndex(); |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 415 | |
| 416 | // Program headers (i.e. mmap instructions). |
| 417 | std::vector<Elf_Phdr> phdrs; |
| 418 | if (write_program_headers_) { |
| 419 | phdrs = MakeProgramHeaders(); |
| 420 | CHECK_LE(phdrs.size(), kMaxProgramHeaders); |
| 421 | elf_header.e_phoff = sizeof(Elf_Ehdr); |
| 422 | elf_header.e_phnum = phdrs.size(); |
| 423 | } |
| 424 | |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 425 | stream_.Seek(0, kSeekSet); |
| 426 | stream_.WriteFully(&elf_header, sizeof(elf_header)); |
| 427 | stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0])); |
| 428 | stream_.Flush(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 429 | } |
| 430 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 431 | // The running program does not have access to section headers |
| 432 | // and the loader is not supposed to use them either. |
| 433 | // The dynamic sections therefore replicates some of the layout |
| 434 | // information like the address and size of .rodata and .text. |
| 435 | // It also contains other metadata like the SONAME. |
| 436 | // The .dynamic section is found using the PT_DYNAMIC program header. |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 437 | void WriteDynamicSection(const std::string& elf_file_path) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 438 | std::string soname(elf_file_path); |
| 439 | size_t directory_separator_pos = soname.rfind('/'); |
| 440 | if (directory_separator_pos != std::string::npos) { |
| 441 | soname = soname.substr(directory_separator_pos + 1); |
| 442 | } |
| 443 | |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 444 | dynstr_.Start(); |
| 445 | dynstr_.Write(""); // dynstr should start with empty string. |
| 446 | dynsym_.Add(dynstr_.Write("oatdata"), &rodata_, 0, true, |
| 447 | rodata_.GetSize(), STB_GLOBAL, STT_OBJECT); |
| 448 | if (text_.GetSize() != 0u) { |
| 449 | dynsym_.Add(dynstr_.Write("oatexec"), &text_, 0, true, |
| 450 | text_.GetSize(), STB_GLOBAL, STT_OBJECT); |
| 451 | dynsym_.Add(dynstr_.Write("oatlastword"), &text_, text_.GetSize() - 4, |
| 452 | true, 4, STB_GLOBAL, STT_OBJECT); |
| 453 | } else if (rodata_.GetSize() != 0) { |
Vladimir Marko | 6065402 | 2016-02-16 12:50:23 +0000 | [diff] [blame] | 454 | // rodata_ can be size 0 for dwarf_test. |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 455 | dynsym_.Add(dynstr_.Write("oatlastword"), &rodata_, rodata_.GetSize() - 4, |
| 456 | true, 4, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 6065402 | 2016-02-16 12:50:23 +0000 | [diff] [blame] | 457 | } |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 458 | if (bss_.finished_) { |
| 459 | dynsym_.Add(dynstr_.Write("oatbss"), &bss_, |
| 460 | 0, true, bss_.GetSize(), STB_GLOBAL, STT_OBJECT); |
| 461 | dynsym_.Add(dynstr_.Write("oatbsslastword"), &bss_, |
| 462 | bss_.GetSize() - 4, true, 4, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 6065402 | 2016-02-16 12:50:23 +0000 | [diff] [blame] | 463 | } |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 464 | Elf_Word soname_offset = dynstr_.Write(soname); |
| 465 | dynstr_.End(); |
| 466 | |
| 467 | dynsym_.Start(); |
| 468 | dynsym_.Write(); |
| 469 | dynsym_.End(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 470 | |
| 471 | // We do not really need a hash-table since there is so few entries. |
| 472 | // However, the hash-table is the only way the linker can actually |
| 473 | // determine the number of symbols in .dynsym so it is required. |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 474 | hash_.Start(); |
| 475 | int count = dynsym_.GetSize() / sizeof(Elf_Sym); // Includes NULL. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 476 | std::vector<Elf_Word> hash; |
| 477 | hash.push_back(1); // Number of buckets. |
| 478 | hash.push_back(count); // Number of chains. |
| 479 | // Buckets. Having just one makes it linear search. |
| 480 | hash.push_back(1); // Point to first non-NULL symbol. |
| 481 | // Chains. This creates linked list of symbols. |
| 482 | hash.push_back(0); // Dummy entry for the NULL symbol. |
| 483 | for (int i = 1; i < count - 1; i++) { |
| 484 | hash.push_back(i + 1); // Each symbol points to the next one. |
| 485 | } |
| 486 | hash.push_back(0); // Last symbol terminates the chain. |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 487 | hash_.WriteFully(hash.data(), hash.size() * sizeof(hash[0])); |
| 488 | hash_.End(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 489 | |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 490 | dynamic_.Start(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 491 | Elf_Dyn dyns[] = { |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 492 | { DT_HASH, { hash_.GetAddress() } }, |
| 493 | { DT_STRTAB, { dynstr_.GetAddress() } }, |
| 494 | { DT_SYMTAB, { dynsym_.GetAddress() } }, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 495 | { DT_SYMENT, { sizeof(Elf_Sym) } }, |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 496 | { DT_STRSZ, { dynstr_.GetSize() } }, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 497 | { DT_SONAME, { soname_offset } }, |
| 498 | { DT_NULL, { 0 } }, |
| 499 | }; |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 500 | dynamic_.WriteFully(&dyns, sizeof(dyns)); |
| 501 | dynamic_.End(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 502 | } |
| 503 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 504 | // Returns true if all writes and seeks on the output stream succeeded. |
| 505 | bool Good() { |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 506 | return stream_.Good(); |
| 507 | } |
| 508 | |
| 509 | // Returns the builder's internal stream. |
| 510 | OutputStream* GetStream() { |
| 511 | return &stream_; |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 512 | } |
| 513 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 514 | off_t AlignFileOffset(size_t alignment) { |
| 515 | return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet); |
| 516 | } |
| 517 | |
| 518 | Elf_Addr AlignVirtualAddress(size_t alignment) { |
| 519 | return virtual_address_ = RoundUp(virtual_address_, alignment); |
| 520 | } |
| 521 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 522 | private: |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 523 | static Elf_Ehdr MakeElfHeader(InstructionSet isa) { |
| 524 | Elf_Ehdr elf_header = Elf_Ehdr(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 525 | switch (isa) { |
| 526 | case kArm: |
| 527 | // Fall through. |
| 528 | case kThumb2: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 529 | elf_header.e_machine = EM_ARM; |
| 530 | elf_header.e_flags = EF_ARM_EABI_VER5; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 531 | break; |
| 532 | } |
| 533 | case kArm64: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 534 | elf_header.e_machine = EM_AARCH64; |
| 535 | elf_header.e_flags = 0; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | case kX86: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 539 | elf_header.e_machine = EM_386; |
| 540 | elf_header.e_flags = 0; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | case kX86_64: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 544 | elf_header.e_machine = EM_X86_64; |
| 545 | elf_header.e_flags = 0; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | case kMips: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 549 | elf_header.e_machine = EM_MIPS; |
| 550 | elf_header.e_flags = (EF_MIPS_NOREORDER | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 551 | EF_MIPS_PIC | |
| 552 | EF_MIPS_CPIC | |
| 553 | EF_MIPS_ABI_O32 | |
| 554 | EF_MIPS_ARCH_32R2); |
| 555 | break; |
| 556 | } |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 557 | case kMips64: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 558 | elf_header.e_machine = EM_MIPS; |
| 559 | elf_header.e_flags = (EF_MIPS_NOREORDER | |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 560 | EF_MIPS_PIC | |
| 561 | EF_MIPS_CPIC | |
| 562 | EF_MIPS_ARCH_64R6); |
| 563 | break; |
| 564 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 565 | case kNone: { |
| 566 | LOG(FATAL) << "No instruction set"; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | default: { |
| 570 | LOG(FATAL) << "Unknown instruction set " << isa; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 573 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 574 | elf_header.e_ident[EI_MAG0] = ELFMAG0; |
| 575 | elf_header.e_ident[EI_MAG1] = ELFMAG1; |
| 576 | elf_header.e_ident[EI_MAG2] = ELFMAG2; |
| 577 | elf_header.e_ident[EI_MAG3] = ELFMAG3; |
| 578 | elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr)) |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 579 | ? ELFCLASS32 : ELFCLASS64;; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 580 | elf_header.e_ident[EI_DATA] = ELFDATA2LSB; |
| 581 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; |
| 582 | elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX; |
| 583 | elf_header.e_ident[EI_ABIVERSION] = 0; |
| 584 | elf_header.e_type = ET_DYN; |
| 585 | elf_header.e_version = 1; |
| 586 | elf_header.e_entry = 0; |
| 587 | elf_header.e_ehsize = sizeof(Elf_Ehdr); |
| 588 | elf_header.e_phentsize = sizeof(Elf_Phdr); |
| 589 | elf_header.e_shentsize = sizeof(Elf_Shdr); |
| 590 | elf_header.e_phoff = sizeof(Elf_Ehdr); |
| 591 | return elf_header; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 592 | } |
| 593 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 594 | // Create program headers based on written sections. |
| 595 | std::vector<Elf_Phdr> MakeProgramHeaders() { |
| 596 | CHECK(!sections_.empty()); |
| 597 | std::vector<Elf_Phdr> phdrs; |
| 598 | { |
| 599 | // The program headers must start with PT_PHDR which is used in |
| 600 | // loaded process to determine the number of program headers. |
| 601 | Elf_Phdr phdr = Elf_Phdr(); |
| 602 | phdr.p_type = PT_PHDR; |
| 603 | phdr.p_flags = PF_R; |
| 604 | phdr.p_offset = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr); |
| 605 | phdr.p_filesz = phdr.p_memsz = 0; // We need to fill this later. |
| 606 | phdr.p_align = sizeof(Elf_Off); |
| 607 | phdrs.push_back(phdr); |
| 608 | // Tell the linker to mmap the start of file to memory. |
| 609 | Elf_Phdr load = Elf_Phdr(); |
| 610 | load.p_type = PT_LOAD; |
| 611 | load.p_flags = PF_R; |
| 612 | load.p_offset = load.p_vaddr = load.p_paddr = 0; |
| 613 | load.p_filesz = load.p_memsz = sections_[0]->header_.sh_offset; |
| 614 | load.p_align = kPageSize; |
| 615 | phdrs.push_back(load); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 616 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 617 | // Create program headers for sections. |
| 618 | for (auto* section : sections_) { |
| 619 | const Elf_Shdr& shdr = section->header_; |
| 620 | if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) { |
| 621 | // PT_LOAD tells the linker to mmap part of the file. |
| 622 | // The linker can only mmap page-aligned sections. |
| 623 | // Single PT_LOAD may contain several ELF sections. |
| 624 | Elf_Phdr& prev = phdrs.back(); |
| 625 | Elf_Phdr load = Elf_Phdr(); |
| 626 | load.p_type = PT_LOAD; |
| 627 | load.p_flags = section->phdr_flags_; |
| 628 | load.p_offset = shdr.sh_offset; |
| 629 | load.p_vaddr = load.p_paddr = shdr.sh_addr; |
| 630 | load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u); |
| 631 | load.p_memsz = shdr.sh_size; |
| 632 | load.p_align = shdr.sh_addralign; |
| 633 | if (prev.p_type == load.p_type && |
| 634 | prev.p_flags == load.p_flags && |
| 635 | prev.p_filesz == prev.p_memsz && // Do not merge .bss |
| 636 | load.p_filesz == load.p_memsz) { // Do not merge .bss |
| 637 | // Merge this PT_LOAD with the previous one. |
| 638 | Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset; |
| 639 | prev.p_filesz = size; |
| 640 | prev.p_memsz = size; |
| 641 | } else { |
| 642 | // If we are adding new load, it must be aligned. |
| 643 | CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize); |
| 644 | phdrs.push_back(load); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | for (auto* section : sections_) { |
| 649 | const Elf_Shdr& shdr = section->header_; |
| 650 | if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) { |
| 651 | // Other PT_* types allow the program to locate interesting |
| 652 | // parts of memory at runtime. They must overlap with PT_LOAD. |
| 653 | if (section->phdr_type_ != 0) { |
| 654 | Elf_Phdr phdr = Elf_Phdr(); |
| 655 | phdr.p_type = section->phdr_type_; |
| 656 | phdr.p_flags = section->phdr_flags_; |
| 657 | phdr.p_offset = shdr.sh_offset; |
| 658 | phdr.p_vaddr = phdr.p_paddr = shdr.sh_addr; |
| 659 | phdr.p_filesz = phdr.p_memsz = shdr.sh_size; |
| 660 | phdr.p_align = shdr.sh_addralign; |
| 661 | phdrs.push_back(phdr); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | // Set the size of the initial PT_PHDR. |
| 666 | CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR); |
| 667 | phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 668 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 669 | return phdrs; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 670 | } |
| 671 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 672 | InstructionSet isa_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 673 | |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 674 | ErrorDelayingOutputStream stream_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 675 | |
| 676 | Section rodata_; |
| 677 | Section text_; |
| 678 | Section bss_; |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 679 | StringSection dynstr_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 680 | SymbolSection dynsym_; |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 681 | Section hash_; |
| 682 | Section dynamic_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 683 | Section eh_frame_; |
| 684 | Section eh_frame_hdr_; |
| 685 | StringSection strtab_; |
| 686 | SymbolSection symtab_; |
| 687 | Section debug_frame_; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 688 | Section debug_info_; |
| 689 | Section debug_line_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 690 | StringSection shstrtab_; |
| 691 | std::vector<std::unique_ptr<Section>> other_sections_; |
| 692 | |
| 693 | // List of used section in the order in which they were written. |
| 694 | std::vector<Section*> sections_; |
| 695 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 696 | bool started_; |
| 697 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 698 | // Used for allocation of virtual address space. |
| 699 | Elf_Addr virtual_address_; |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 700 | |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame^] | 701 | size_t write_program_headers_; |
| 702 | |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 703 | DISALLOW_COPY_AND_ASSIGN(ElfBuilder); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 704 | }; |
| 705 | |
| 706 | } // namespace art |
| 707 | |
| 708 | #endif // ART_COMPILER_ELF_BUILDER_H_ |