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