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 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 17 | #ifndef ART_COMPILER_LINKER_ELF_BUILDER_H_ |
| 18 | #define ART_COMPILER_LINKER_ELF_BUILDER_H_ |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 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" |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 23 | #include "arch/mips/instruction_set_features_mips.h" |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 24 | #include "base/array_ref.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 26 | #include "base/casts.h" |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 27 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 28 | #include "elf_utils.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 29 | #include "leb128.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 30 | #include "linker/error_delaying_output_stream.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 33 | namespace linker { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 34 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 35 | // Writes ELF file. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 36 | // |
| 37 | // The basic layout of the elf file: |
| 38 | // Elf_Ehdr - The ELF header. |
| 39 | // Elf_Phdr[] - Program headers for the linker. |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 40 | // .note.gnu.build-id - Optional build ID section (SHA-1 digest). |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 41 | // .rodata - Oat metadata. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 42 | // .text - Compiled code. |
| 43 | // .bss - Zero-initialized writeable section. |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 44 | // .dex - Reserved NOBITS space for dex-related data. |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 45 | // .MIPS.abiflags - MIPS specific section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 46 | // .dynstr - Names for .dynsym. |
| 47 | // .dynsym - A few oat-specific dynamic symbols. |
| 48 | // .hash - Hash-table for .dynsym. |
| 49 | // .dynamic - Tags which let the linker locate .dynsym. |
| 50 | // .strtab - Names for .symtab. |
| 51 | // .symtab - Debug symbols. |
| 52 | // .eh_frame - Unwind information (CFI). |
| 53 | // .eh_frame_hdr - Index of .eh_frame. |
| 54 | // .debug_frame - Unwind information (CFI). |
| 55 | // .debug_frame.oat_patches - Addresses for relocation. |
| 56 | // .debug_info - Debug information. |
| 57 | // .debug_info.oat_patches - Addresses for relocation. |
| 58 | // .debug_abbrev - Decoding information for .debug_info. |
| 59 | // .debug_str - Strings for .debug_info. |
| 60 | // .debug_line - Line number tables. |
| 61 | // .debug_line.oat_patches - Addresses for relocation. |
| 62 | // .text.oat_patches - Addresses for relocation. |
| 63 | // .shstrtab - Names of ELF sections. |
| 64 | // Elf_Shdr[] - Section headers. |
| 65 | // |
| 66 | // Some section are optional (the debug sections in particular). |
| 67 | // |
| 68 | // We try write the section data directly into the file without much |
| 69 | // in-memory buffering. This means we generally write sections based on the |
| 70 | // dependency order (e.g. .dynamic points to .dynsym which points to .text). |
| 71 | // |
| 72 | // In the cases where we need to buffer, we write the larger section first |
| 73 | // and buffer the smaller one (e.g. .strtab is bigger than .symtab). |
| 74 | // |
| 75 | // The debug sections are written last for easier stripping. |
| 76 | // |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 77 | template <typename ElfTypes> |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 78 | class ElfBuilder FINAL { |
| 79 | public: |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 80 | static constexpr size_t kMaxProgramHeaders = 16; |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 81 | // SHA-1 digest. Not using SHA_DIGEST_LENGTH from openssl/sha.h to avoid |
| 82 | // spreading this header dependency for just this single constant. |
| 83 | static constexpr size_t kBuildIdLen = 20; |
| 84 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 85 | using Elf_Addr = typename ElfTypes::Addr; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 86 | using Elf_Off = typename ElfTypes::Off; |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 87 | using Elf_Word = typename ElfTypes::Word; |
| 88 | using Elf_Sword = typename ElfTypes::Sword; |
| 89 | using Elf_Ehdr = typename ElfTypes::Ehdr; |
| 90 | using Elf_Shdr = typename ElfTypes::Shdr; |
| 91 | using Elf_Sym = typename ElfTypes::Sym; |
| 92 | using Elf_Phdr = typename ElfTypes::Phdr; |
| 93 | using Elf_Dyn = typename ElfTypes::Dyn; |
| 94 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 95 | // Base class of all sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 96 | class Section : public OutputStream { |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 97 | public: |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 98 | Section(ElfBuilder<ElfTypes>* owner, |
| 99 | const std::string& name, |
| 100 | Elf_Word type, |
| 101 | Elf_Word flags, |
| 102 | const Section* link, |
| 103 | Elf_Word info, |
| 104 | Elf_Word align, |
| 105 | Elf_Word entsize) |
| 106 | : OutputStream(name), |
| 107 | owner_(owner), |
| 108 | header_(), |
| 109 | section_index_(0), |
| 110 | name_(name), |
| 111 | link_(link), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 112 | phdr_flags_(PF_R), |
| 113 | phdr_type_(0) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 114 | DCHECK_GE(align, 1u); |
David Srbecky | 491a7fe | 2015-05-28 00:59:08 +0100 | [diff] [blame] | 115 | header_.sh_type = type; |
| 116 | header_.sh_flags = flags; |
| 117 | header_.sh_info = info; |
| 118 | header_.sh_addralign = align; |
| 119 | header_.sh_entsize = entsize; |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 120 | } |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 121 | |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 122 | // Allocate chunk of virtual memory for this section from the owning ElfBuilder. |
| 123 | // This must be done at the start for all SHF_ALLOC sections (i.e. mmaped by linker). |
| 124 | // It is fine to allocate section but never call Start/End() (e.g. the .bss section). |
| 125 | void AllocateVirtualMemory(Elf_Word size) { |
| 126 | AllocateVirtualMemory(owner_->virtual_address_, size); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 127 | } |
| 128 | |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 129 | void AllocateVirtualMemory(Elf_Addr addr, Elf_Word size) { |
| 130 | CHECK_NE(header_.sh_flags & SHF_ALLOC, 0u); |
| 131 | Elf_Word align = AddSection(); |
| 132 | CHECK_EQ(header_.sh_addr, 0u); |
| 133 | header_.sh_addr = RoundUp(addr, align); |
| 134 | CHECK(header_.sh_size == 0u || header_.sh_size == size); |
| 135 | header_.sh_size = size; |
| 136 | CHECK_LE(owner_->virtual_address_, header_.sh_addr); |
| 137 | owner_->virtual_address_ = header_.sh_addr + header_.sh_size; |
| 138 | } |
| 139 | |
| 140 | // Start writing file data of this section. |
| 141 | void Start() { |
| 142 | CHECK(owner_->current_section_ == nullptr); |
| 143 | Elf_Word align = AddSection(); |
| 144 | CHECK_EQ(header_.sh_offset, 0u); |
| 145 | header_.sh_offset = owner_->AlignFileOffset(align); |
| 146 | owner_->current_section_ = this; |
| 147 | } |
| 148 | |
| 149 | // Finish writing file data of this section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 150 | void End() { |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 151 | CHECK(owner_->current_section_ == this); |
| 152 | Elf_Word position = GetPosition(); |
| 153 | CHECK(header_.sh_size == 0u || header_.sh_size == position); |
| 154 | header_.sh_size = position; |
| 155 | owner_->current_section_ = nullptr; |
| 156 | } |
| 157 | |
| 158 | // Get the number of bytes written so far. |
| 159 | // Only valid while writing the section. |
| 160 | Elf_Word GetPosition() const { |
| 161 | CHECK(owner_->current_section_ == this); |
| 162 | off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent); |
| 163 | DCHECK_GE(file_offset, (off_t)header_.sh_offset); |
| 164 | return file_offset - header_.sh_offset; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Get the location of this section in virtual memory. |
| 168 | Elf_Addr GetAddress() const { |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 169 | DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 170 | DCHECK_NE(header_.sh_addr, 0u); |
| 171 | return header_.sh_addr; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // This function always succeeds to simplify code. |
| 175 | // Use builder's Good() to check the actual status. |
| 176 | bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE { |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 177 | CHECK(owner_->current_section_ == this); |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 178 | return owner_->stream_.WriteFully(buffer, byte_count); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // This function always succeeds to simplify code. |
| 182 | // Use builder's Good() to check the actual status. |
| 183 | off_t Seek(off_t offset, Whence whence) OVERRIDE { |
| 184 | // 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] | 185 | return owner_->stream_.Seek(offset, whence); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 188 | // This function flushes the output and returns whether it succeeded. |
| 189 | // If there was a previous failure, this does nothing and returns false, i.e. failed. |
| 190 | bool Flush() OVERRIDE { |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 191 | return owner_->stream_.Flush(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 192 | } |
| 193 | |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 194 | Elf_Word GetSectionIndex() const { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 195 | DCHECK_NE(section_index_, 0u); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 196 | return section_index_; |
| 197 | } |
| 198 | |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 199 | private: |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 200 | // Add this section to the list of generated ELF sections (if not there already). |
| 201 | // It also ensures the alignment is sufficient to generate valid program headers, |
| 202 | // since that depends on the previous section. It returns the required alignment. |
| 203 | Elf_Word AddSection() { |
| 204 | if (section_index_ == 0) { |
| 205 | std::vector<Section*>& sections = owner_->sections_; |
| 206 | Elf_Word last = sections.empty() ? PF_R : sections.back()->phdr_flags_; |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 207 | if (phdr_flags_ != last) { |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 208 | header_.sh_addralign = kPageSize; // Page-align if R/W/X flags changed. |
| 209 | } |
| 210 | sections.push_back(this); |
| 211 | section_index_ = sections.size(); // First ELF section has index 1. |
| 212 | } |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 213 | return owner_->write_program_headers_ ? header_.sh_addralign : 1; |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 214 | } |
| 215 | |
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 | Elf_Word phdr_flags_; |
| 222 | Elf_Word phdr_type_; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 223 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 224 | friend class ElfBuilder; |
David Srbecky | 4d247f7 | 2015-11-09 11:56:52 +0000 | [diff] [blame] | 225 | |
| 226 | DISALLOW_COPY_AND_ASSIGN(Section); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 227 | }; |
| 228 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 229 | class CachedSection : public Section { |
| 230 | public: |
| 231 | CachedSection(ElfBuilder<ElfTypes>* owner, |
| 232 | const std::string& name, |
| 233 | Elf_Word type, |
| 234 | Elf_Word flags, |
| 235 | const Section* link, |
| 236 | Elf_Word info, |
| 237 | Elf_Word align, |
| 238 | Elf_Word entsize) |
| 239 | : Section(owner, name, type, flags, link, info, align, entsize), cache_() { } |
| 240 | |
| 241 | Elf_Word Add(const void* data, size_t length) { |
| 242 | Elf_Word offset = cache_.size(); |
| 243 | const uint8_t* d = reinterpret_cast<const uint8_t*>(data); |
| 244 | cache_.insert(cache_.end(), d, d + length); |
| 245 | return offset; |
| 246 | } |
| 247 | |
| 248 | Elf_Word GetCacheSize() { |
| 249 | return cache_.size(); |
| 250 | } |
| 251 | |
| 252 | void Write() { |
| 253 | this->WriteFully(cache_.data(), cache_.size()); |
| 254 | cache_.clear(); |
| 255 | cache_.shrink_to_fit(); |
| 256 | } |
| 257 | |
| 258 | void WriteCachedSection() { |
| 259 | this->Start(); |
| 260 | Write(); |
| 261 | this->End(); |
| 262 | } |
| 263 | |
| 264 | private: |
| 265 | std::vector<uint8_t> cache_; |
| 266 | }; |
| 267 | |
| 268 | // Writer of .dynstr section. |
| 269 | class CachedStringSection FINAL : public CachedSection { |
| 270 | public: |
| 271 | CachedStringSection(ElfBuilder<ElfTypes>* owner, |
| 272 | const std::string& name, |
| 273 | Elf_Word flags, |
| 274 | Elf_Word align) |
| 275 | : CachedSection(owner, |
| 276 | name, |
| 277 | SHT_STRTAB, |
| 278 | flags, |
| 279 | /* link */ nullptr, |
| 280 | /* info */ 0, |
| 281 | align, |
| 282 | /* entsize */ 0) { } |
| 283 | |
| 284 | Elf_Word Add(const std::string& name) { |
| 285 | if (CachedSection::GetCacheSize() == 0u) { |
| 286 | DCHECK(name.empty()); |
| 287 | } |
| 288 | return CachedSection::Add(name.c_str(), name.length() + 1); |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | // Writer of .strtab and .shstrtab sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 293 | class StringSection FINAL : public Section { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 294 | public: |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 295 | StringSection(ElfBuilder<ElfTypes>* owner, |
| 296 | const std::string& name, |
| 297 | Elf_Word flags, |
| 298 | Elf_Word align) |
| 299 | : Section(owner, |
| 300 | name, |
| 301 | SHT_STRTAB, |
| 302 | flags, |
| 303 | /* link */ nullptr, |
| 304 | /* info */ 0, |
| 305 | align, |
| 306 | /* entsize */ 0), |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 307 | current_offset_(0) { |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 308 | } |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 309 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 310 | Elf_Word Write(const std::string& name) { |
| 311 | if (current_offset_ == 0) { |
| 312 | DCHECK(name.empty()); |
| 313 | } |
| 314 | Elf_Word offset = current_offset_; |
| 315 | this->WriteFully(name.c_str(), name.length() + 1); |
| 316 | current_offset_ += name.length() + 1; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 317 | return offset; |
| 318 | } |
| 319 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 320 | private: |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 321 | Elf_Word current_offset_; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 322 | }; |
| 323 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 324 | // Writer of .dynsym and .symtab sections. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 325 | class SymbolSection FINAL : public CachedSection { |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 326 | public: |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 327 | SymbolSection(ElfBuilder<ElfTypes>* owner, |
| 328 | const std::string& name, |
| 329 | Elf_Word type, |
| 330 | Elf_Word flags, |
| 331 | Section* strtab) |
| 332 | : CachedSection(owner, |
| 333 | name, |
| 334 | type, |
| 335 | flags, |
| 336 | strtab, |
| 337 | /* info */ 0, |
| 338 | sizeof(Elf_Off), |
| 339 | sizeof(Elf_Sym)) { |
| 340 | // The symbol table always has to start with NULL symbol. |
| 341 | Elf_Sym null_symbol = Elf_Sym(); |
| 342 | CachedSection::Add(&null_symbol, sizeof(null_symbol)); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // Buffer symbol for this section. It will be written later. |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 346 | // If the symbol's section is null, it will be considered absolute (SHN_ABS). |
| 347 | // (we use this in JIT to reference code which is stored outside the debug ELF file) |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 348 | void Add(Elf_Word name, |
| 349 | const Section* section, |
| 350 | Elf_Addr addr, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 351 | Elf_Word size, |
| 352 | uint8_t binding, |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 353 | uint8_t type) { |
| 354 | Elf_Word section_index; |
| 355 | if (section != nullptr) { |
| 356 | DCHECK_LE(section->GetAddress(), addr); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 357 | DCHECK_LE(addr, section->GetAddress() + section->header_.sh_size); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 358 | section_index = section->GetSectionIndex(); |
| 359 | } else { |
| 360 | section_index = static_cast<Elf_Word>(SHN_ABS); |
| 361 | } |
| 362 | Add(name, section_index, addr, size, binding, type); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void Add(Elf_Word name, |
| 366 | Elf_Word section_index, |
| 367 | Elf_Addr addr, |
| 368 | Elf_Word size, |
| 369 | uint8_t binding, |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 370 | uint8_t type) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 371 | Elf_Sym sym = Elf_Sym(); |
| 372 | sym.st_name = name; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 373 | sym.st_value = addr; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 374 | sym.st_size = size; |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 375 | sym.st_other = 0; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 376 | sym.st_shndx = section_index; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 377 | sym.st_info = (binding << 4) + (type & 0xf); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 378 | CachedSection::Add(&sym, sizeof(sym)); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame] | 379 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 380 | }; |
| 381 | |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 382 | class AbiflagsSection FINAL : public Section { |
| 383 | public: |
| 384 | // Section with Mips abiflag info. |
| 385 | static constexpr uint8_t MIPS_AFL_REG_NONE = 0; // no registers |
| 386 | static constexpr uint8_t MIPS_AFL_REG_32 = 1; // 32-bit registers |
| 387 | static constexpr uint8_t MIPS_AFL_REG_64 = 2; // 64-bit registers |
| 388 | static constexpr uint32_t MIPS_AFL_FLAGS1_ODDSPREG = 1; // Uses odd single-prec fp regs |
| 389 | static constexpr uint8_t MIPS_ABI_FP_DOUBLE = 1; // -mdouble-float |
| 390 | static constexpr uint8_t MIPS_ABI_FP_XX = 5; // -mfpxx |
| 391 | static constexpr uint8_t MIPS_ABI_FP_64A = 7; // -mips32r* -mfp64 -mno-odd-spreg |
| 392 | |
| 393 | AbiflagsSection(ElfBuilder<ElfTypes>* owner, |
| 394 | const std::string& name, |
| 395 | Elf_Word type, |
| 396 | Elf_Word flags, |
| 397 | const Section* link, |
| 398 | Elf_Word info, |
| 399 | Elf_Word align, |
| 400 | Elf_Word entsize, |
| 401 | InstructionSet isa, |
| 402 | const InstructionSetFeatures* features) |
| 403 | : Section(owner, name, type, flags, link, info, align, entsize) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 404 | if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) { |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 405 | bool fpu32 = false; // assume mips64 values |
| 406 | uint8_t isa_rev = 6; // assume mips64 values |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 407 | if (isa == InstructionSet::kMips) { |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 408 | // adjust for mips32 values |
| 409 | fpu32 = features->AsMipsInstructionSetFeatures()->Is32BitFloatingPoint(); |
| 410 | isa_rev = features->AsMipsInstructionSetFeatures()->IsR6() |
| 411 | ? 6 |
| 412 | : features->AsMipsInstructionSetFeatures()->IsMipsIsaRevGreaterThanEqual2() |
| 413 | ? (fpu32 ? 2 : 5) |
| 414 | : 1; |
| 415 | } |
| 416 | abiflags_.version = 0; // version of flags structure |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 417 | abiflags_.isa_level = (isa == InstructionSet::kMips) ? 32 : 64; |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 418 | abiflags_.isa_rev = isa_rev; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 419 | abiflags_.gpr_size = (isa == InstructionSet::kMips) ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64; |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 420 | abiflags_.cpr1_size = fpu32 ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64; |
| 421 | abiflags_.cpr2_size = MIPS_AFL_REG_NONE; |
| 422 | // Set the fp_abi to MIPS_ABI_FP_64A for mips32 with 64-bit FPUs (ie: mips32 R5 and R6). |
| 423 | // Otherwise set to MIPS_ABI_FP_DOUBLE. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 424 | abiflags_.fp_abi = |
| 425 | (isa == InstructionSet::kMips && !fpu32) ? MIPS_ABI_FP_64A : MIPS_ABI_FP_DOUBLE; |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 426 | abiflags_.isa_ext = 0; |
| 427 | abiflags_.ases = 0; |
| 428 | // To keep the code simple, we are not using odd FP reg for single floats for both |
| 429 | // mips32 and mips64 ART. Therefore we are not setting the MIPS_AFL_FLAGS1_ODDSPREG bit. |
| 430 | abiflags_.flags1 = 0; |
| 431 | abiflags_.flags2 = 0; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | Elf_Word GetSize() const { |
| 436 | return sizeof(abiflags_); |
| 437 | } |
| 438 | |
| 439 | void Write() { |
| 440 | this->WriteFully(&abiflags_, sizeof(abiflags_)); |
| 441 | } |
| 442 | |
| 443 | private: |
| 444 | struct { |
| 445 | uint16_t version; // version of this structure |
| 446 | uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size; |
| 447 | uint8_t fp_abi; |
| 448 | uint32_t isa_ext, ases, flags1, flags2; |
| 449 | } abiflags_; |
| 450 | }; |
| 451 | |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 452 | class BuildIdSection FINAL : public Section { |
| 453 | public: |
| 454 | BuildIdSection(ElfBuilder<ElfTypes>* owner, |
| 455 | const std::string& name, |
| 456 | Elf_Word type, |
| 457 | Elf_Word flags, |
| 458 | const Section* link, |
| 459 | Elf_Word info, |
| 460 | Elf_Word align, |
| 461 | Elf_Word entsize) |
| 462 | : Section(owner, name, type, flags, link, info, align, entsize), |
| 463 | digest_start_(-1) { |
| 464 | } |
| 465 | |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 466 | Elf_Word GetSize() { |
| 467 | return 16 + kBuildIdLen; |
| 468 | } |
| 469 | |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 470 | void Write() { |
| 471 | // The size fields are 32-bit on both 32-bit and 64-bit systems, confirmed |
| 472 | // with the 64-bit linker and libbfd code. The size of name and desc must |
| 473 | // be a multiple of 4 and it currently is. |
| 474 | this->WriteUint32(4); // namesz. |
| 475 | this->WriteUint32(kBuildIdLen); // descsz. |
| 476 | this->WriteUint32(3); // type = NT_GNU_BUILD_ID. |
| 477 | this->WriteFully("GNU", 4); // name. |
| 478 | digest_start_ = this->Seek(0, kSeekCurrent); |
| 479 | static_assert(kBuildIdLen % 4 == 0, "expecting a mutliple of 4 for build ID length"); |
| 480 | this->WriteFully(std::string(kBuildIdLen, '\0').c_str(), kBuildIdLen); // desc. |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 481 | DCHECK_EQ(this->GetPosition(), GetSize()); |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | off_t GetDigestStart() { |
| 485 | CHECK_GT(digest_start_, 0); |
| 486 | return digest_start_; |
| 487 | } |
| 488 | |
| 489 | private: |
| 490 | bool WriteUint32(uint32_t v) { |
| 491 | return this->WriteFully(&v, sizeof(v)); |
| 492 | } |
| 493 | |
| 494 | // File offset where the build ID digest starts. |
| 495 | // Populated with zeros first, then updated with the actual value as the |
| 496 | // very last thing in the output file creation. |
| 497 | off_t digest_start_; |
| 498 | }; |
| 499 | |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 500 | ElfBuilder(InstructionSet isa, const InstructionSetFeatures* features, OutputStream* output) |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 501 | : isa_(isa), |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 502 | features_(features), |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 503 | stream_(output), |
| 504 | rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), |
| 505 | text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0), |
| 506 | bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 507 | dex_(this, ".dex", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 508 | dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize), |
| 509 | dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_), |
| 510 | hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)), |
| 511 | dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)), |
| 512 | eh_frame_(this, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), |
| 513 | 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] | 514 | strtab_(this, ".strtab", 0, 1), |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 515 | symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_), |
| 516 | debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0), |
| 517 | debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0), |
| 518 | debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0), |
| 519 | shstrtab_(this, ".shstrtab", 0, 1), |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 520 | abiflags_(this, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, nullptr, 0, kPageSize, 0, |
| 521 | isa, features), |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 522 | build_id_(this, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, nullptr, 0, 4, 0), |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 523 | current_section_(nullptr), |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 524 | started_(false), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 525 | write_program_headers_(false), |
| 526 | loaded_size_(0u), |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 527 | virtual_address_(0) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 528 | text_.phdr_flags_ = PF_R | PF_X; |
| 529 | bss_.phdr_flags_ = PF_R | PF_W; |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 530 | dex_.phdr_flags_ = PF_R; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 531 | dynamic_.phdr_flags_ = PF_R | PF_W; |
| 532 | dynamic_.phdr_type_ = PT_DYNAMIC; |
| 533 | eh_frame_hdr_.phdr_type_ = PT_GNU_EH_FRAME; |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 534 | abiflags_.phdr_type_ = PT_MIPS_ABIFLAGS; |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 535 | build_id_.phdr_type_ = PT_NOTE; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 536 | } |
| 537 | ~ElfBuilder() {} |
| 538 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 539 | InstructionSet GetIsa() { return isa_; } |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 540 | BuildIdSection* GetBuildId() { return &build_id_; } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 541 | Section* GetRoData() { return &rodata_; } |
| 542 | Section* GetText() { return &text_; } |
| 543 | Section* GetBss() { return &bss_; } |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 544 | Section* GetDex() { return &dex_; } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 545 | StringSection* GetStrTab() { return &strtab_; } |
| 546 | SymbolSection* GetSymTab() { return &symtab_; } |
| 547 | Section* GetEhFrame() { return &eh_frame_; } |
| 548 | Section* GetEhFrameHdr() { return &eh_frame_hdr_; } |
| 549 | Section* GetDebugFrame() { return &debug_frame_; } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 550 | Section* GetDebugInfo() { return &debug_info_; } |
| 551 | Section* GetDebugLine() { return &debug_line_; } |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 552 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 553 | // Encode patch locations as LEB128 list of deltas between consecutive addresses. |
| 554 | // (exposed publicly for tests) |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 555 | static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 556 | std::vector<uint8_t>* buffer) { |
| 557 | buffer->reserve(buffer->size() + locations.size() * 2); // guess 2 bytes per ULEB128. |
| 558 | uintptr_t address = 0; // relative to start of section. |
| 559 | for (uintptr_t location : locations) { |
| 560 | DCHECK_GE(location, address) << "Patch locations are not in sorted order"; |
| 561 | EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address)); |
| 562 | address = location; |
| 563 | } |
| 564 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 565 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 566 | void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 567 | std::vector<uint8_t> buffer; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 568 | EncodeOatPatches(patch_locations, &buffer); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 569 | std::unique_ptr<Section> s(new Section(this, name, SHT_OAT_PATCH, 0, nullptr, 0, 1, 0)); |
| 570 | s->Start(); |
| 571 | s->WriteFully(buffer.data(), buffer.size()); |
| 572 | s->End(); |
| 573 | other_sections_.push_back(std::move(s)); |
| 574 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 575 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 576 | void WriteSection(const char* name, const std::vector<uint8_t>* buffer) { |
| 577 | std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0)); |
| 578 | s->Start(); |
| 579 | s->WriteFully(buffer->data(), buffer->size()); |
| 580 | s->End(); |
| 581 | other_sections_.push_back(std::move(s)); |
| 582 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 583 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 584 | // Reserve space for ELF header and program headers. |
| 585 | // We do not know the number of headers until later, so |
| 586 | // it is easiest to just reserve a fixed amount of space. |
| 587 | // Program headers are required for loading by the linker. |
| 588 | // It is possible to omit them for ELF files used for debugging. |
| 589 | void Start(bool write_program_headers = true) { |
| 590 | int size = sizeof(Elf_Ehdr); |
| 591 | if (write_program_headers) { |
| 592 | size += sizeof(Elf_Phdr) * kMaxProgramHeaders; |
| 593 | } |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 594 | stream_.Seek(size, kSeekSet); |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 595 | started_ = true; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 596 | virtual_address_ += size; |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 597 | write_program_headers_ = write_program_headers; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 598 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 599 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 600 | void End() { |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 601 | DCHECK(started_); |
| 602 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 603 | // Note: loaded_size_ == 0 for tests that don't write .rodata, .text, .bss, |
| 604 | // .dynstr, dynsym, .hash and .dynamic. These tests should not read loaded_size_. |
| 605 | // TODO: Either refactor the .eh_frame creation so that it counts towards loaded_size_, |
| 606 | // or remove all support for .eh_frame. (The currently unused .eh_frame counts towards |
| 607 | // the virtual_address_ but we don't consider it for loaded_size_.) |
| 608 | CHECK(loaded_size_ == 0 || loaded_size_ == RoundUp(virtual_address_, kPageSize)) |
| 609 | << loaded_size_ << " " << virtual_address_; |
| 610 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 611 | // Write section names and finish the section headers. |
| 612 | shstrtab_.Start(); |
| 613 | shstrtab_.Write(""); |
| 614 | for (auto* section : sections_) { |
| 615 | section->header_.sh_name = shstrtab_.Write(section->name_); |
| 616 | if (section->link_ != nullptr) { |
| 617 | section->header_.sh_link = section->link_->GetSectionIndex(); |
David Srbecky | b0a962c | 2015-04-28 19:43:56 +0100 | [diff] [blame] | 618 | } |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 619 | if (section->header_.sh_offset == 0) { |
| 620 | section->header_.sh_type = SHT_NOBITS; |
| 621 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 622 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 623 | shstrtab_.End(); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 624 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 625 | // Write section headers at the end of the ELF file. |
| 626 | std::vector<Elf_Shdr> shdrs; |
| 627 | shdrs.reserve(1u + sections_.size()); |
| 628 | shdrs.push_back(Elf_Shdr()); // NULL at index 0. |
| 629 | for (auto* section : sections_) { |
| 630 | shdrs.push_back(section->header_); |
| 631 | } |
| 632 | Elf_Off section_headers_offset; |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 633 | section_headers_offset = AlignFileOffset(sizeof(Elf_Off)); |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 634 | stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0])); |
| 635 | |
| 636 | // Flush everything else before writing the program headers. This should prevent |
| 637 | // the OS from reordering writes, so that we don't end up with valid headers |
| 638 | // and partially written data if we suddenly lose power, for example. |
| 639 | stream_.Flush(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 640 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 641 | // The main ELF header. |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 642 | Elf_Ehdr elf_header = MakeElfHeader(isa_, features_); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 643 | elf_header.e_shoff = section_headers_offset; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 644 | elf_header.e_shnum = shdrs.size(); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 645 | elf_header.e_shstrndx = shstrtab_.GetSectionIndex(); |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 646 | |
| 647 | // Program headers (i.e. mmap instructions). |
| 648 | std::vector<Elf_Phdr> phdrs; |
| 649 | if (write_program_headers_) { |
| 650 | phdrs = MakeProgramHeaders(); |
| 651 | CHECK_LE(phdrs.size(), kMaxProgramHeaders); |
| 652 | elf_header.e_phoff = sizeof(Elf_Ehdr); |
| 653 | elf_header.e_phnum = phdrs.size(); |
| 654 | } |
| 655 | |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 656 | stream_.Seek(0, kSeekSet); |
| 657 | stream_.WriteFully(&elf_header, sizeof(elf_header)); |
| 658 | stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0])); |
| 659 | stream_.Flush(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 660 | } |
| 661 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 662 | // The running program does not have access to section headers |
| 663 | // and the loader is not supposed to use them either. |
| 664 | // The dynamic sections therefore replicates some of the layout |
| 665 | // information like the address and size of .rodata and .text. |
| 666 | // It also contains other metadata like the SONAME. |
| 667 | // The .dynamic section is found using the PT_DYNAMIC program header. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 668 | void PrepareDynamicSection(const std::string& elf_file_path, |
| 669 | Elf_Word rodata_size, |
| 670 | Elf_Word text_size, |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 671 | Elf_Word bss_size, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 672 | Elf_Word bss_methods_offset, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 673 | Elf_Word bss_roots_offset, |
| 674 | Elf_Word dex_size) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 675 | std::string soname(elf_file_path); |
| 676 | size_t directory_separator_pos = soname.rfind('/'); |
| 677 | if (directory_separator_pos != std::string::npos) { |
| 678 | soname = soname.substr(directory_separator_pos + 1); |
| 679 | } |
| 680 | |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 681 | // Allocate all pre-dynamic sections. |
| 682 | rodata_.AllocateVirtualMemory(rodata_size); |
| 683 | text_.AllocateVirtualMemory(text_size); |
| 684 | if (bss_size != 0) { |
| 685 | bss_.AllocateVirtualMemory(bss_size); |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 686 | } |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 687 | if (dex_size != 0) { |
| 688 | dex_.AllocateVirtualMemory(dex_size); |
| 689 | } |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 690 | if (isa_ == InstructionSet::kMips || isa_ == InstructionSet::kMips64) { |
| 691 | abiflags_.AllocateVirtualMemory(abiflags_.GetSize()); |
| 692 | } |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame] | 693 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 694 | // Cache .dynstr, .dynsym and .hash data. |
| 695 | dynstr_.Add(""); // dynstr should start with empty string. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 696 | Elf_Word oatdata = dynstr_.Add("oatdata"); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 697 | dynsym_.Add(oatdata, &rodata_, rodata_.GetAddress(), rodata_size, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 698 | if (text_size != 0u) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 699 | Elf_Word oatexec = dynstr_.Add("oatexec"); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 700 | dynsym_.Add(oatexec, &text_, text_.GetAddress(), text_size, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 701 | Elf_Word oatlastword = dynstr_.Add("oatlastword"); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 702 | Elf_Word oatlastword_address = text_.GetAddress() + text_size - 4; |
| 703 | dynsym_.Add(oatlastword, &text_, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 704 | } else if (rodata_size != 0) { |
| 705 | // rodata_ can be size 0 for dwarf_test. |
| 706 | Elf_Word oatlastword = dynstr_.Add("oatlastword"); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 707 | Elf_Word oatlastword_address = rodata_.GetAddress() + rodata_size - 4; |
| 708 | dynsym_.Add(oatlastword, &rodata_, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 709 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 710 | DCHECK_LE(bss_roots_offset, bss_size); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 711 | if (bss_size != 0u) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 712 | Elf_Word oatbss = dynstr_.Add("oatbss"); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 713 | dynsym_.Add(oatbss, &bss_, bss_.GetAddress(), bss_roots_offset, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 714 | DCHECK_LE(bss_methods_offset, bss_roots_offset); |
| 715 | DCHECK_LE(bss_roots_offset, bss_size); |
| 716 | // Add a symbol marking the start of the methods part of the .bss, if not empty. |
| 717 | if (bss_methods_offset != bss_roots_offset) { |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 718 | Elf_Word bss_methods_address = bss_.GetAddress() + bss_methods_offset; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 719 | Elf_Word bss_methods_size = bss_roots_offset - bss_methods_offset; |
| 720 | Elf_Word oatbssroots = dynstr_.Add("oatbssmethods"); |
| 721 | dynsym_.Add( |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 722 | oatbssroots, &bss_, bss_methods_address, bss_methods_size, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 723 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 724 | // Add a symbol marking the start of the GC roots part of the .bss, if not empty. |
| 725 | if (bss_roots_offset != bss_size) { |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 726 | Elf_Word bss_roots_address = bss_.GetAddress() + bss_roots_offset; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 727 | Elf_Word bss_roots_size = bss_size - bss_roots_offset; |
| 728 | Elf_Word oatbssroots = dynstr_.Add("oatbssroots"); |
| 729 | dynsym_.Add( |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 730 | oatbssroots, &bss_, bss_roots_address, bss_roots_size, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 731 | } |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 732 | Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword"); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 733 | Elf_Word bsslastword_address = bss_.GetAddress() + bss_size - 4; |
| 734 | dynsym_.Add(oatbsslastword, &bss_, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 735 | } |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 736 | if (dex_size != 0u) { |
| 737 | Elf_Word oatdex = dynstr_.Add("oatdex"); |
| 738 | dynsym_.Add(oatdex, &dex_, dex_.GetAddress(), dex_size, STB_GLOBAL, STT_OBJECT); |
| 739 | Elf_Word oatdexlastword = dynstr_.Add("oatdexlastword"); |
| 740 | Elf_Word oatdexlastword_address = dex_.GetAddress() + dex_size - 4; |
| 741 | dynsym_.Add(oatdexlastword, &dex_, oatdexlastword_address, 4, STB_GLOBAL, STT_OBJECT); |
| 742 | } |
| 743 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 744 | Elf_Word soname_offset = dynstr_.Add(soname); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 745 | |
| 746 | // We do not really need a hash-table since there is so few entries. |
| 747 | // However, the hash-table is the only way the linker can actually |
| 748 | // determine the number of symbols in .dynsym so it is required. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 749 | int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym); // Includes NULL. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 750 | std::vector<Elf_Word> hash; |
| 751 | hash.push_back(1); // Number of buckets. |
| 752 | hash.push_back(count); // Number of chains. |
| 753 | // Buckets. Having just one makes it linear search. |
| 754 | hash.push_back(1); // Point to first non-NULL symbol. |
| 755 | // Chains. This creates linked list of symbols. |
| 756 | hash.push_back(0); // Dummy entry for the NULL symbol. |
| 757 | for (int i = 1; i < count - 1; i++) { |
| 758 | hash.push_back(i + 1); // Each symbol points to the next one. |
| 759 | } |
| 760 | hash.push_back(0); // Last symbol terminates the chain. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 761 | hash_.Add(hash.data(), hash.size() * sizeof(hash[0])); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 762 | |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 763 | // Allocate all remaining sections. |
| 764 | dynstr_.AllocateVirtualMemory(dynstr_.GetCacheSize()); |
| 765 | dynsym_.AllocateVirtualMemory(dynsym_.GetCacheSize()); |
| 766 | hash_.AllocateVirtualMemory(hash_.GetCacheSize()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 767 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 768 | Elf_Dyn dyns[] = { |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 769 | { DT_HASH, { hash_.GetAddress() } }, |
| 770 | { DT_STRTAB, { dynstr_.GetAddress() } }, |
| 771 | { DT_SYMTAB, { dynsym_.GetAddress() } }, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 772 | { DT_SYMENT, { sizeof(Elf_Sym) } }, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 773 | { DT_STRSZ, { dynstr_.GetCacheSize() } }, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 774 | { DT_SONAME, { soname_offset } }, |
| 775 | { DT_NULL, { 0 } }, |
| 776 | }; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 777 | dynamic_.Add(&dyns, sizeof(dyns)); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 778 | dynamic_.AllocateVirtualMemory(dynamic_.GetCacheSize()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 779 | |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 780 | loaded_size_ = RoundUp(virtual_address_, kPageSize); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void WriteDynamicSection() { |
| 784 | dynstr_.WriteCachedSection(); |
| 785 | dynsym_.WriteCachedSection(); |
| 786 | hash_.WriteCachedSection(); |
| 787 | dynamic_.WriteCachedSection(); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | Elf_Word GetLoadedSize() { |
| 791 | CHECK_NE(loaded_size_, 0u); |
| 792 | return loaded_size_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 795 | void WriteMIPSabiflagsSection() { |
| 796 | abiflags_.Start(); |
| 797 | abiflags_.Write(); |
| 798 | abiflags_.End(); |
| 799 | } |
| 800 | |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 801 | void WriteBuildIdSection() { |
| 802 | build_id_.Start(); |
| 803 | build_id_.Write(); |
| 804 | build_id_.End(); |
| 805 | } |
| 806 | |
| 807 | void WriteBuildId(uint8_t build_id[kBuildIdLen]) { |
| 808 | stream_.Seek(build_id_.GetDigestStart(), kSeekSet); |
| 809 | stream_.WriteFully(build_id, kBuildIdLen); |
| 810 | } |
| 811 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 812 | // Returns true if all writes and seeks on the output stream succeeded. |
| 813 | bool Good() { |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 814 | return stream_.Good(); |
| 815 | } |
| 816 | |
| 817 | // Returns the builder's internal stream. |
| 818 | OutputStream* GetStream() { |
| 819 | return &stream_; |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 820 | } |
| 821 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 822 | off_t AlignFileOffset(size_t alignment) { |
| 823 | return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet); |
| 824 | } |
| 825 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 826 | private: |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 827 | static Elf_Ehdr MakeElfHeader(InstructionSet isa, const InstructionSetFeatures* features) { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 828 | Elf_Ehdr elf_header = Elf_Ehdr(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 829 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 830 | case InstructionSet::kArm: |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 831 | // Fall through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 832 | case InstructionSet::kThumb2: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 833 | elf_header.e_machine = EM_ARM; |
| 834 | elf_header.e_flags = EF_ARM_EABI_VER5; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 835 | break; |
| 836 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 837 | case InstructionSet::kArm64: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 838 | elf_header.e_machine = EM_AARCH64; |
| 839 | elf_header.e_flags = 0; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 840 | break; |
| 841 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 842 | case InstructionSet::kX86: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 843 | elf_header.e_machine = EM_386; |
| 844 | elf_header.e_flags = 0; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 845 | break; |
| 846 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 847 | case InstructionSet::kX86_64: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 848 | elf_header.e_machine = EM_X86_64; |
| 849 | elf_header.e_flags = 0; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 850 | break; |
| 851 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 852 | case InstructionSet::kMips: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 853 | elf_header.e_machine = EM_MIPS; |
| 854 | elf_header.e_flags = (EF_MIPS_NOREORDER | |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 855 | EF_MIPS_PIC | |
| 856 | EF_MIPS_CPIC | |
| 857 | EF_MIPS_ABI_O32 | |
Greg Kaiser | 81a1899 | 2016-06-16 15:55:15 -0700 | [diff] [blame] | 858 | (features->AsMipsInstructionSetFeatures()->IsR6() |
| 859 | ? EF_MIPS_ARCH_32R6 |
| 860 | : EF_MIPS_ARCH_32R2)); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 861 | break; |
| 862 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 863 | case InstructionSet::kMips64: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 864 | elf_header.e_machine = EM_MIPS; |
| 865 | elf_header.e_flags = (EF_MIPS_NOREORDER | |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 866 | EF_MIPS_PIC | |
| 867 | EF_MIPS_CPIC | |
| 868 | EF_MIPS_ARCH_64R6); |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 869 | break; |
| 870 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 871 | case InstructionSet::kNone: { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 872 | LOG(FATAL) << "No instruction set"; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | default: { |
| 876 | LOG(FATAL) << "Unknown instruction set " << isa; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 879 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 880 | elf_header.e_ident[EI_MAG0] = ELFMAG0; |
| 881 | elf_header.e_ident[EI_MAG1] = ELFMAG1; |
| 882 | elf_header.e_ident[EI_MAG2] = ELFMAG2; |
| 883 | elf_header.e_ident[EI_MAG3] = ELFMAG3; |
| 884 | elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr)) |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 885 | ? ELFCLASS32 : ELFCLASS64; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 886 | elf_header.e_ident[EI_DATA] = ELFDATA2LSB; |
| 887 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; |
| 888 | elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX; |
| 889 | elf_header.e_ident[EI_ABIVERSION] = 0; |
| 890 | elf_header.e_type = ET_DYN; |
| 891 | elf_header.e_version = 1; |
| 892 | elf_header.e_entry = 0; |
| 893 | elf_header.e_ehsize = sizeof(Elf_Ehdr); |
| 894 | elf_header.e_phentsize = sizeof(Elf_Phdr); |
| 895 | elf_header.e_shentsize = sizeof(Elf_Shdr); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 896 | return elf_header; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 897 | } |
| 898 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 899 | // Create program headers based on written sections. |
| 900 | std::vector<Elf_Phdr> MakeProgramHeaders() { |
| 901 | CHECK(!sections_.empty()); |
| 902 | std::vector<Elf_Phdr> phdrs; |
| 903 | { |
| 904 | // The program headers must start with PT_PHDR which is used in |
| 905 | // loaded process to determine the number of program headers. |
| 906 | Elf_Phdr phdr = Elf_Phdr(); |
| 907 | phdr.p_type = PT_PHDR; |
| 908 | phdr.p_flags = PF_R; |
| 909 | phdr.p_offset = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr); |
| 910 | phdr.p_filesz = phdr.p_memsz = 0; // We need to fill this later. |
| 911 | phdr.p_align = sizeof(Elf_Off); |
| 912 | phdrs.push_back(phdr); |
| 913 | // Tell the linker to mmap the start of file to memory. |
| 914 | Elf_Phdr load = Elf_Phdr(); |
| 915 | load.p_type = PT_LOAD; |
| 916 | load.p_flags = PF_R; |
| 917 | load.p_offset = load.p_vaddr = load.p_paddr = 0; |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 918 | load.p_filesz = load.p_memsz = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * kMaxProgramHeaders; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 919 | load.p_align = kPageSize; |
| 920 | phdrs.push_back(load); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 921 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 922 | // Create program headers for sections. |
| 923 | for (auto* section : sections_) { |
| 924 | const Elf_Shdr& shdr = section->header_; |
| 925 | if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) { |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 926 | DCHECK(shdr.sh_addr != 0u) << "Allocate virtual memory for the section"; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 927 | // PT_LOAD tells the linker to mmap part of the file. |
| 928 | // The linker can only mmap page-aligned sections. |
| 929 | // Single PT_LOAD may contain several ELF sections. |
| 930 | Elf_Phdr& prev = phdrs.back(); |
| 931 | Elf_Phdr load = Elf_Phdr(); |
| 932 | load.p_type = PT_LOAD; |
| 933 | load.p_flags = section->phdr_flags_; |
| 934 | load.p_offset = shdr.sh_offset; |
| 935 | load.p_vaddr = load.p_paddr = shdr.sh_addr; |
| 936 | load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u); |
| 937 | load.p_memsz = shdr.sh_size; |
| 938 | load.p_align = shdr.sh_addralign; |
| 939 | if (prev.p_type == load.p_type && |
| 940 | prev.p_flags == load.p_flags && |
| 941 | prev.p_filesz == prev.p_memsz && // Do not merge .bss |
| 942 | load.p_filesz == load.p_memsz) { // Do not merge .bss |
| 943 | // Merge this PT_LOAD with the previous one. |
| 944 | Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset; |
| 945 | prev.p_filesz = size; |
| 946 | prev.p_memsz = size; |
| 947 | } else { |
| 948 | // If we are adding new load, it must be aligned. |
| 949 | CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize); |
| 950 | phdrs.push_back(load); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | for (auto* section : sections_) { |
| 955 | const Elf_Shdr& shdr = section->header_; |
| 956 | if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) { |
| 957 | // Other PT_* types allow the program to locate interesting |
| 958 | // parts of memory at runtime. They must overlap with PT_LOAD. |
| 959 | if (section->phdr_type_ != 0) { |
| 960 | Elf_Phdr phdr = Elf_Phdr(); |
| 961 | phdr.p_type = section->phdr_type_; |
| 962 | phdr.p_flags = section->phdr_flags_; |
| 963 | phdr.p_offset = shdr.sh_offset; |
| 964 | phdr.p_vaddr = phdr.p_paddr = shdr.sh_addr; |
| 965 | phdr.p_filesz = phdr.p_memsz = shdr.sh_size; |
| 966 | phdr.p_align = shdr.sh_addralign; |
| 967 | phdrs.push_back(phdr); |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | // Set the size of the initial PT_PHDR. |
| 972 | CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR); |
| 973 | 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] | 974 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 975 | return phdrs; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 976 | } |
| 977 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 978 | InstructionSet isa_; |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 979 | const InstructionSetFeatures* features_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 980 | |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 981 | ErrorDelayingOutputStream stream_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 982 | |
| 983 | Section rodata_; |
| 984 | Section text_; |
| 985 | Section bss_; |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 986 | Section dex_; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 987 | CachedStringSection dynstr_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 988 | SymbolSection dynsym_; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 989 | CachedSection hash_; |
| 990 | CachedSection dynamic_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 991 | Section eh_frame_; |
| 992 | Section eh_frame_hdr_; |
| 993 | StringSection strtab_; |
| 994 | SymbolSection symtab_; |
| 995 | Section debug_frame_; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 996 | Section debug_info_; |
| 997 | Section debug_line_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 998 | StringSection shstrtab_; |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 999 | AbiflagsSection abiflags_; |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 1000 | BuildIdSection build_id_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1001 | std::vector<std::unique_ptr<Section>> other_sections_; |
| 1002 | |
| 1003 | // List of used section in the order in which they were written. |
| 1004 | std::vector<Section*> sections_; |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 1005 | Section* current_section_; // The section which is currently being written. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1006 | |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 1007 | bool started_; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1008 | bool write_program_headers_; |
| 1009 | |
| 1010 | // The size of the memory taken by the ELF file when loaded. |
| 1011 | size_t loaded_size_; |
David Srbecky | 579942f | 2016-01-28 20:01:28 +0000 | [diff] [blame] | 1012 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1013 | // Used for allocation of virtual address space. |
| 1014 | Elf_Addr virtual_address_; |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 1015 | |
| 1016 | DISALLOW_COPY_AND_ASSIGN(ElfBuilder); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 1017 | }; |
| 1018 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 1019 | } // namespace linker |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 1020 | } // namespace art |
| 1021 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 1022 | #endif // ART_COMPILER_LINKER_ELF_BUILDER_H_ |