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