diff options
author | 2019-02-12 16:35:48 +0000 | |
---|---|---|
committer | 2019-03-20 15:20:54 +0000 | |
commit | 2faab0064bccdf06a454ba5fc37f2cfeceab78bc (patch) | |
tree | bc51a211068019d77e7739164adf5be5c6333b7b /compiler/linker | |
parent | 5f1465ff689eccbb1b6d15160d1a3700793956da (diff) |
Create libelffile library for ELF file manipulation.
Move some of our tooling to library to make it reusable.
Remove MIPS support from the ELF builder. This is slightly
easier than making it independent of the runtime.
Bug: 110133331
Test: test.py -b --host
Change-Id: I93343808d0e27ee8e1117e713a2503e8179fc245
Diffstat (limited to 'compiler/linker')
-rw-r--r-- | compiler/linker/buffered_output_stream.cc | 72 | ||||
-rw-r--r-- | compiler/linker/buffered_output_stream.h | 56 | ||||
-rw-r--r-- | compiler/linker/elf_builder.h | 1079 | ||||
-rw-r--r-- | compiler/linker/error_delaying_output_stream.h | 105 | ||||
-rw-r--r-- | compiler/linker/file_output_stream.cc | 42 | ||||
-rw-r--r-- | compiler/linker/file_output_stream.h | 48 | ||||
-rw-r--r-- | compiler/linker/output_stream.cc | 33 | ||||
-rw-r--r-- | compiler/linker/output_stream.h | 66 | ||||
-rw-r--r-- | compiler/linker/output_stream_test.cc | 7 | ||||
-rw-r--r-- | compiler/linker/vector_output_stream.cc | 50 | ||||
-rw-r--r-- | compiler/linker/vector_output_stream.h | 71 |
11 files changed, 3 insertions, 1626 deletions
diff --git a/compiler/linker/buffered_output_stream.cc b/compiler/linker/buffered_output_stream.cc deleted file mode 100644 index 07066b76ac..0000000000 --- a/compiler/linker/buffered_output_stream.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "buffered_output_stream.h" - -#include <string.h> - -namespace art { -namespace linker { - -BufferedOutputStream::BufferedOutputStream(std::unique_ptr<OutputStream> out) - : OutputStream(out->GetLocation()), // Before out is moved to out_. - out_(std::move(out)), - used_(0) {} - -BufferedOutputStream::~BufferedOutputStream() { - FlushBuffer(); -} - -bool BufferedOutputStream::WriteFully(const void* buffer, size_t byte_count) { - if (byte_count > kBufferSize) { - if (!FlushBuffer()) { - return false; - } - return out_->WriteFully(buffer, byte_count); - } - if (used_ + byte_count > kBufferSize) { - if (!FlushBuffer()) { - return false; - } - } - const uint8_t* src = reinterpret_cast<const uint8_t*>(buffer); - memcpy(&buffer_[used_], src, byte_count); - used_ += byte_count; - return true; -} - -bool BufferedOutputStream::Flush() { - return FlushBuffer() && out_->Flush(); -} - -bool BufferedOutputStream::FlushBuffer() { - bool success = true; - if (used_ > 0) { - success = out_->WriteFully(&buffer_[0], used_); - used_ = 0; - } - return success; -} - -off_t BufferedOutputStream::Seek(off_t offset, Whence whence) { - if (!FlushBuffer()) { - return -1; - } - return out_->Seek(offset, whence); -} - -} // namespace linker -} // namespace art diff --git a/compiler/linker/buffered_output_stream.h b/compiler/linker/buffered_output_stream.h deleted file mode 100644 index cb1c44ba23..0000000000 --- a/compiler/linker/buffered_output_stream.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ART_COMPILER_LINKER_BUFFERED_OUTPUT_STREAM_H_ -#define ART_COMPILER_LINKER_BUFFERED_OUTPUT_STREAM_H_ - -#include <memory> - -#include "output_stream.h" - -#include "base/globals.h" - -namespace art { -namespace linker { - -class BufferedOutputStream final : public OutputStream { - public: - explicit BufferedOutputStream(std::unique_ptr<OutputStream> out); - - ~BufferedOutputStream() override; - - bool WriteFully(const void* buffer, size_t byte_count) override; - - off_t Seek(off_t offset, Whence whence) override; - - bool Flush() override; - - private: - static const size_t kBufferSize = 8 * KB; - - bool FlushBuffer(); - - std::unique_ptr<OutputStream> const out_; - uint8_t buffer_[kBufferSize]; - size_t used_; - - DISALLOW_COPY_AND_ASSIGN(BufferedOutputStream); -}; - -} // namespace linker -} // namespace art - -#endif // ART_COMPILER_LINKER_BUFFERED_OUTPUT_STREAM_H_ diff --git a/compiler/linker/elf_builder.h b/compiler/linker/elf_builder.h deleted file mode 100644 index 33e1866c50..0000000000 --- a/compiler/linker/elf_builder.h +++ /dev/null @@ -1,1079 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ART_COMPILER_LINKER_ELF_BUILDER_H_ -#define ART_COMPILER_LINKER_ELF_BUILDER_H_ - -#include <vector> -#include <deque> - -#include "arch/instruction_set.h" -#include "arch/mips/instruction_set_features_mips.h" -#include "base/array_ref.h" -#include "base/bit_utils.h" -#include "base/casts.h" -#include "base/leb128.h" -#include "base/unix_file/fd_file.h" -#include "elf_utils.h" -#include "linker/error_delaying_output_stream.h" - -namespace art { -namespace linker { - -// Writes ELF file. -// -// The basic layout of the elf file: -// Elf_Ehdr - The ELF header. -// Elf_Phdr[] - Program headers for the linker. -// .note.gnu.build-id - Optional build ID section (SHA-1 digest). -// .rodata - Oat metadata. -// .text - Compiled code. -// .bss - Zero-initialized writeable section. -// .dex - Reserved NOBITS space for dex-related data. -// .MIPS.abiflags - MIPS specific section. -// .dynstr - Names for .dynsym. -// .dynsym - A few oat-specific dynamic symbols. -// .hash - Hash-table for .dynsym. -// .dynamic - Tags which let the linker locate .dynsym. -// .strtab - Names for .symtab. -// .symtab - Debug symbols. -// .debug_frame - Unwind information (CFI). -// .debug_info - Debug information. -// .debug_abbrev - Decoding information for .debug_info. -// .debug_str - Strings for .debug_info. -// .debug_line - Line number tables. -// .shstrtab - Names of ELF sections. -// Elf_Shdr[] - Section headers. -// -// Some section are optional (the debug sections in particular). -// -// We try write the section data directly into the file without much -// in-memory buffering. This means we generally write sections based on the -// dependency order (e.g. .dynamic points to .dynsym which points to .text). -// -// In the cases where we need to buffer, we write the larger section first -// and buffer the smaller one (e.g. .strtab is bigger than .symtab). -// -// The debug sections are written last for easier stripping. -// -template <typename ElfTypes> -class ElfBuilder final { - public: - static constexpr size_t kMaxProgramHeaders = 16; - // SHA-1 digest. Not using SHA_DIGEST_LENGTH from openssl/sha.h to avoid - // spreading this header dependency for just this single constant. - static constexpr size_t kBuildIdLen = 20; - - using Elf_Addr = typename ElfTypes::Addr; - using Elf_Off = typename ElfTypes::Off; - using Elf_Word = typename ElfTypes::Word; - using Elf_Sword = typename ElfTypes::Sword; - using Elf_Ehdr = typename ElfTypes::Ehdr; - using Elf_Shdr = typename ElfTypes::Shdr; - using Elf_Sym = typename ElfTypes::Sym; - using Elf_Phdr = typename ElfTypes::Phdr; - using Elf_Dyn = typename ElfTypes::Dyn; - - // Base class of all sections. - class Section : public OutputStream { - public: - Section(ElfBuilder<ElfTypes>* owner, - const std::string& name, - Elf_Word type, - Elf_Word flags, - const Section* link, - Elf_Word info, - Elf_Word align, - Elf_Word entsize) - : OutputStream(name), - owner_(owner), - header_(), - section_index_(0), - name_(name), - link_(link), - phdr_flags_(PF_R), - phdr_type_(0) { - DCHECK_GE(align, 1u); - header_.sh_type = type; - header_.sh_flags = flags; - header_.sh_info = info; - header_.sh_addralign = align; - header_.sh_entsize = entsize; - } - - // Allocate chunk of virtual memory for this section from the owning ElfBuilder. - // This must be done at the start for all SHF_ALLOC sections (i.e. mmaped by linker). - // It is fine to allocate section but never call Start/End() (e.g. the .bss section). - void AllocateVirtualMemory(Elf_Word size) { - AllocateVirtualMemory(owner_->virtual_address_, size); - } - - void AllocateVirtualMemory(Elf_Addr addr, Elf_Word size) { - CHECK_NE(header_.sh_flags & SHF_ALLOC, 0u); - Elf_Word align = AddSection(); - CHECK_EQ(header_.sh_addr, 0u); - header_.sh_addr = RoundUp(addr, align); - CHECK(header_.sh_size == 0u || header_.sh_size == size); - header_.sh_size = size; - CHECK_LE(owner_->virtual_address_, header_.sh_addr); - owner_->virtual_address_ = header_.sh_addr + header_.sh_size; - } - - // Start writing file data of this section. - void Start() { - CHECK(owner_->current_section_ == nullptr); - Elf_Word align = AddSection(); - CHECK_EQ(header_.sh_offset, 0u); - header_.sh_offset = owner_->AlignFileOffset(align); - owner_->current_section_ = this; - } - - // Finish writing file data of this section. - void End() { - CHECK(owner_->current_section_ == this); - Elf_Word position = GetPosition(); - CHECK(header_.sh_size == 0u || header_.sh_size == position); - header_.sh_size = position; - owner_->current_section_ = nullptr; - } - - // Get the number of bytes written so far. - // Only valid while writing the section. - Elf_Word GetPosition() const { - CHECK(owner_->current_section_ == this); - off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent); - DCHECK_GE(file_offset, (off_t)header_.sh_offset); - return file_offset - header_.sh_offset; - } - - // Get the location of this section in virtual memory. - Elf_Addr GetAddress() const { - DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u); - DCHECK_NE(header_.sh_addr, 0u); - return header_.sh_addr; - } - - // This function always succeeds to simplify code. - // Use builder's Good() to check the actual status. - bool WriteFully(const void* buffer, size_t byte_count) override { - CHECK(owner_->current_section_ == this); - return owner_->stream_.WriteFully(buffer, byte_count); - } - - // This function always succeeds to simplify code. - // Use builder's Good() to check the actual status. - off_t Seek(off_t offset, Whence whence) override { - // Forward the seek as-is and trust the caller to use it reasonably. - return owner_->stream_.Seek(offset, whence); - } - - // This function flushes the output and returns whether it succeeded. - // If there was a previous failure, this does nothing and returns false, i.e. failed. - bool Flush() override { - return owner_->stream_.Flush(); - } - - Elf_Word GetSectionIndex() const { - DCHECK_NE(section_index_, 0u); - return section_index_; - } - - // Returns true if this section has been added. - bool Exists() const { - return section_index_ != 0; - } - - protected: - // Add this section to the list of generated ELF sections (if not there already). - // It also ensures the alignment is sufficient to generate valid program headers, - // since that depends on the previous section. It returns the required alignment. - Elf_Word AddSection() { - if (section_index_ == 0) { - std::vector<Section*>& sections = owner_->sections_; - Elf_Word last = sections.empty() ? PF_R : sections.back()->phdr_flags_; - if (phdr_flags_ != last) { - header_.sh_addralign = kPageSize; // Page-align if R/W/X flags changed. - } - sections.push_back(this); - section_index_ = sections.size(); // First ELF section has index 1. - } - return owner_->write_program_headers_ ? header_.sh_addralign : 1; - } - - ElfBuilder<ElfTypes>* owner_; - Elf_Shdr header_; - Elf_Word section_index_; - const std::string name_; - const Section* const link_; - Elf_Word phdr_flags_; - Elf_Word phdr_type_; - - friend class ElfBuilder; - - DISALLOW_COPY_AND_ASSIGN(Section); - }; - - class CachedSection : public Section { - public: - CachedSection(ElfBuilder<ElfTypes>* owner, - const std::string& name, - Elf_Word type, - Elf_Word flags, - const Section* link, - Elf_Word info, - Elf_Word align, - Elf_Word entsize) - : Section(owner, name, type, flags, link, info, align, entsize), cache_() { } - - Elf_Word Add(const void* data, size_t length) { - Elf_Word offset = cache_.size(); - const uint8_t* d = reinterpret_cast<const uint8_t*>(data); - cache_.insert(cache_.end(), d, d + length); - return offset; - } - - Elf_Word GetCacheSize() { - return cache_.size(); - } - - void Write() { - this->WriteFully(cache_.data(), cache_.size()); - cache_.clear(); - cache_.shrink_to_fit(); - } - - void WriteCachedSection() { - this->Start(); - Write(); - this->End(); - } - - private: - std::vector<uint8_t> cache_; - }; - - // Writer of .dynstr section. - class CachedStringSection final : public CachedSection { - public: - CachedStringSection(ElfBuilder<ElfTypes>* owner, - const std::string& name, - Elf_Word flags, - Elf_Word align) - : CachedSection(owner, - name, - SHT_STRTAB, - flags, - /* link= */ nullptr, - /* info= */ 0, - align, - /* entsize= */ 0) { } - - Elf_Word Add(const std::string& name) { - if (CachedSection::GetCacheSize() == 0u) { - DCHECK(name.empty()); - } - return CachedSection::Add(name.c_str(), name.length() + 1); - } - }; - - // Writer of .strtab and .shstrtab sections. - class StringSection final : public Section { - public: - StringSection(ElfBuilder<ElfTypes>* owner, - const std::string& name, - Elf_Word flags, - Elf_Word align) - : Section(owner, - name, - SHT_STRTAB, - flags, - /* link= */ nullptr, - /* info= */ 0, - align, - /* entsize= */ 0) { - Reset(); - } - - void Reset() { - current_offset_ = 0; - last_name_ = ""; - last_offset_ = 0; - } - - Elf_Word Write(const std::string& name) { - if (current_offset_ == 0) { - DCHECK(name.empty()); - } else if (name == last_name_) { - return last_offset_; // Very simple string de-duplication. - } - last_name_ = name; - last_offset_ = current_offset_; - this->WriteFully(name.c_str(), name.length() + 1); - current_offset_ += name.length() + 1; - return last_offset_; - } - - private: - Elf_Word current_offset_; - std::string last_name_; - Elf_Word last_offset_; - }; - - // Writer of .dynsym and .symtab sections. - class SymbolSection final : public Section { - public: - SymbolSection(ElfBuilder<ElfTypes>* owner, - const std::string& name, - Elf_Word type, - Elf_Word flags, - Section* strtab) - : Section(owner, - name, - type, - flags, - strtab, - /* info= */ 1, - sizeof(Elf_Off), - sizeof(Elf_Sym)) { - syms_.push_back(Elf_Sym()); // The symbol table always has to start with NULL symbol. - } - - // Buffer symbol for this section. It will be written later. - void Add(Elf_Word name, - const Section* section, - Elf_Addr addr, - Elf_Word size, - uint8_t binding, - uint8_t type) { - Elf_Sym sym = Elf_Sym(); - sym.st_name = name; - sym.st_value = addr; - sym.st_size = size; - sym.st_other = 0; - sym.st_info = (binding << 4) + (type & 0xf); - Add(sym, section); - } - - // Buffer symbol for this section. It will be written later. - void Add(Elf_Sym sym, const Section* section) { - DCHECK(section != nullptr); - DCHECK_LE(section->GetAddress(), sym.st_value); - DCHECK_LE(sym.st_value, section->GetAddress() + section->header_.sh_size); - sym.st_shndx = section->GetSectionIndex(); - - // The sh_info file must be set to index one-past the last local symbol. - if (sym.getBinding() == STB_LOCAL) { - DCHECK_EQ(syms_.back().getBinding(), STB_LOCAL); - this->header_.sh_info = syms_.size() + 1; - } - - syms_.push_back(sym); - } - - Elf_Word GetCacheSize() { return syms_.size() * sizeof(Elf_Sym); } - - void WriteCachedSection() { - this->Start(); - for (; !syms_.empty(); syms_.pop_front()) { - this->WriteFully(&syms_.front(), sizeof(Elf_Sym)); - } - this->End(); - } - - private: - std::deque<Elf_Sym> syms_; // Buffered/cached content of the whole section. - }; - - class AbiflagsSection final : public Section { - public: - // Section with Mips abiflag info. - static constexpr uint8_t MIPS_AFL_REG_NONE = 0; // no registers - static constexpr uint8_t MIPS_AFL_REG_32 = 1; // 32-bit registers - static constexpr uint8_t MIPS_AFL_REG_64 = 2; // 64-bit registers - static constexpr uint32_t MIPS_AFL_FLAGS1_ODDSPREG = 1; // Uses odd single-prec fp regs - static constexpr uint8_t MIPS_ABI_FP_DOUBLE = 1; // -mdouble-float - static constexpr uint8_t MIPS_ABI_FP_XX = 5; // -mfpxx - static constexpr uint8_t MIPS_ABI_FP_64A = 7; // -mips32r* -mfp64 -mno-odd-spreg - - AbiflagsSection(ElfBuilder<ElfTypes>* owner, - const std::string& name, - Elf_Word type, - Elf_Word flags, - const Section* link, - Elf_Word info, - Elf_Word align, - Elf_Word entsize, - InstructionSet isa, - const InstructionSetFeatures* features) - : Section(owner, name, type, flags, link, info, align, entsize) { - if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) { - bool fpu32 = false; // assume mips64 values - uint8_t isa_rev = 6; // assume mips64 values - if (isa == InstructionSet::kMips) { - // adjust for mips32 values - fpu32 = features->AsMipsInstructionSetFeatures()->Is32BitFloatingPoint(); - isa_rev = features->AsMipsInstructionSetFeatures()->IsR6() - ? 6 - : features->AsMipsInstructionSetFeatures()->IsMipsIsaRevGreaterThanEqual2() - ? (fpu32 ? 2 : 5) - : 1; - } - abiflags_.version = 0; // version of flags structure - abiflags_.isa_level = (isa == InstructionSet::kMips) ? 32 : 64; - abiflags_.isa_rev = isa_rev; - abiflags_.gpr_size = (isa == InstructionSet::kMips) ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64; - abiflags_.cpr1_size = fpu32 ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64; - abiflags_.cpr2_size = MIPS_AFL_REG_NONE; - // Set the fp_abi to MIPS_ABI_FP_64A for mips32 with 64-bit FPUs (ie: mips32 R5 and R6). - // Otherwise set to MIPS_ABI_FP_DOUBLE. - abiflags_.fp_abi = - (isa == InstructionSet::kMips && !fpu32) ? MIPS_ABI_FP_64A : MIPS_ABI_FP_DOUBLE; - abiflags_.isa_ext = 0; - abiflags_.ases = 0; - // To keep the code simple, we are not using odd FP reg for single floats for both - // mips32 and mips64 ART. Therefore we are not setting the MIPS_AFL_FLAGS1_ODDSPREG bit. - abiflags_.flags1 = 0; - abiflags_.flags2 = 0; - } - } - - Elf_Word GetSize() const { - return sizeof(abiflags_); - } - - void Write() { - this->WriteFully(&abiflags_, sizeof(abiflags_)); - } - - private: - struct { - uint16_t version; // version of this structure - uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size; - uint8_t fp_abi; - uint32_t isa_ext, ases, flags1, flags2; - } abiflags_; - }; - - class BuildIdSection final : public Section { - public: - BuildIdSection(ElfBuilder<ElfTypes>* owner, - const std::string& name, - Elf_Word type, - Elf_Word flags, - const Section* link, - Elf_Word info, - Elf_Word align, - Elf_Word entsize) - : Section(owner, name, type, flags, link, info, align, entsize), - digest_start_(-1) { - } - - Elf_Word GetSize() { - return 16 + kBuildIdLen; - } - - void Write() { - // The size fields are 32-bit on both 32-bit and 64-bit systems, confirmed - // with the 64-bit linker and libbfd code. The size of name and desc must - // be a multiple of 4 and it currently is. - this->WriteUint32(4); // namesz. - this->WriteUint32(kBuildIdLen); // descsz. - this->WriteUint32(3); // type = NT_GNU_BUILD_ID. - this->WriteFully("GNU", 4); // name. - digest_start_ = this->Seek(0, kSeekCurrent); - static_assert(kBuildIdLen % 4 == 0, "expecting a mutliple of 4 for build ID length"); - this->WriteFully(std::string(kBuildIdLen, '\0').c_str(), kBuildIdLen); // desc. - DCHECK_EQ(this->GetPosition(), GetSize()); - } - - off_t GetDigestStart() { - CHECK_GT(digest_start_, 0); - return digest_start_; - } - - private: - bool WriteUint32(uint32_t v) { - return this->WriteFully(&v, sizeof(v)); - } - - // File offset where the build ID digest starts. - // Populated with zeros first, then updated with the actual value as the - // very last thing in the output file creation. - off_t digest_start_; - }; - - ElfBuilder(InstructionSet isa, const InstructionSetFeatures* features, OutputStream* output) - : isa_(isa), - features_(features), - stream_(output), - rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), - text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0), - data_bimg_rel_ro_( - this, ".data.bimg.rel.ro", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), - bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), - dex_(this, ".dex", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0), - dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize), - dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_), - hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)), - dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)), - strtab_(this, ".strtab", 0, 1), - symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_), - debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0), - debug_frame_hdr_( - this, ".debug_frame_hdr.android", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0), - debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0), - debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0), - shstrtab_(this, ".shstrtab", 0, 1), - abiflags_(this, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, nullptr, 0, kPageSize, 0, - isa, features), - build_id_(this, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, nullptr, 0, 4, 0), - current_section_(nullptr), - started_(false), - finished_(false), - write_program_headers_(false), - loaded_size_(0u), - virtual_address_(0) { - text_.phdr_flags_ = PF_R | PF_X; - data_bimg_rel_ro_.phdr_flags_ = PF_R | PF_W; // Shall be made read-only at run time. - bss_.phdr_flags_ = PF_R | PF_W; - dex_.phdr_flags_ = PF_R; - dynamic_.phdr_flags_ = PF_R | PF_W; - dynamic_.phdr_type_ = PT_DYNAMIC; - abiflags_.phdr_type_ = PT_MIPS_ABIFLAGS; - build_id_.phdr_type_ = PT_NOTE; - } - ~ElfBuilder() {} - - InstructionSet GetIsa() { return isa_; } - BuildIdSection* GetBuildId() { return &build_id_; } - Section* GetRoData() { return &rodata_; } - Section* GetText() { return &text_; } - Section* GetDataBimgRelRo() { return &data_bimg_rel_ro_; } - Section* GetBss() { return &bss_; } - Section* GetDex() { return &dex_; } - StringSection* GetStrTab() { return &strtab_; } - SymbolSection* GetSymTab() { return &symtab_; } - Section* GetDebugFrame() { return &debug_frame_; } - Section* GetDebugFrameHdr() { return &debug_frame_hdr_; } - Section* GetDebugInfo() { return &debug_info_; } - Section* GetDebugLine() { return &debug_line_; } - - void WriteSection(const char* name, const std::vector<uint8_t>* buffer) { - std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0)); - s->Start(); - s->WriteFully(buffer->data(), buffer->size()); - s->End(); - other_sections_.push_back(std::move(s)); - } - - // Reserve space for ELF header and program headers. - // We do not know the number of headers until later, so - // it is easiest to just reserve a fixed amount of space. - // Program headers are required for loading by the linker. - // It is possible to omit them for ELF files used for debugging. - void Start(bool write_program_headers = true) { - int size = sizeof(Elf_Ehdr); - if (write_program_headers) { - size += sizeof(Elf_Phdr) * kMaxProgramHeaders; - } - stream_.Seek(size, kSeekSet); - started_ = true; - virtual_address_ += size; - write_program_headers_ = write_program_headers; - } - - off_t End() { - DCHECK(started_); - DCHECK(!finished_); - finished_ = true; - - // Note: loaded_size_ == 0 for tests that don't write .rodata, .text, .bss, - // .dynstr, dynsym, .hash and .dynamic. These tests should not read loaded_size_. - CHECK(loaded_size_ == 0 || loaded_size_ == RoundUp(virtual_address_, kPageSize)) - << loaded_size_ << " " << virtual_address_; - - // Write section names and finish the section headers. - shstrtab_.Start(); - shstrtab_.Write(""); - for (auto* section : sections_) { - section->header_.sh_name = shstrtab_.Write(section->name_); - if (section->link_ != nullptr) { - section->header_.sh_link = section->link_->GetSectionIndex(); - } - if (section->header_.sh_offset == 0) { - section->header_.sh_type = SHT_NOBITS; - } - } - shstrtab_.End(); - - // Write section headers at the end of the ELF file. - std::vector<Elf_Shdr> shdrs; - shdrs.reserve(1u + sections_.size()); - shdrs.push_back(Elf_Shdr()); // NULL at index 0. - for (auto* section : sections_) { - shdrs.push_back(section->header_); - } - Elf_Off section_headers_offset; - section_headers_offset = AlignFileOffset(sizeof(Elf_Off)); - stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0])); - off_t file_size = stream_.Seek(0, kSeekCurrent); - - // Flush everything else before writing the program headers. This should prevent - // the OS from reordering writes, so that we don't end up with valid headers - // and partially written data if we suddenly lose power, for example. - stream_.Flush(); - - // The main ELF header. - Elf_Ehdr elf_header = MakeElfHeader(isa_, features_); - elf_header.e_shoff = section_headers_offset; - elf_header.e_shnum = shdrs.size(); - elf_header.e_shstrndx = shstrtab_.GetSectionIndex(); - - // Program headers (i.e. mmap instructions). - std::vector<Elf_Phdr> phdrs; - if (write_program_headers_) { - phdrs = MakeProgramHeaders(); - CHECK_LE(phdrs.size(), kMaxProgramHeaders); - elf_header.e_phoff = sizeof(Elf_Ehdr); - elf_header.e_phnum = phdrs.size(); - } - - stream_.Seek(0, kSeekSet); - stream_.WriteFully(&elf_header, sizeof(elf_header)); - stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0])); - stream_.Flush(); - - return file_size; - } - - // This has the same effect as running the "strip" command line tool. - // It removes all debugging sections (but it keeps mini-debug-info). - // It returns the ELF file size (as the caller needs to truncate it). - off_t Strip() { - DCHECK(finished_); - finished_ = false; - Elf_Off end = 0; - std::vector<Section*> non_debug_sections; - for (Section* section : sections_) { - if (section == &shstrtab_ || // Section names will be recreated. - section == &symtab_ || - section == &strtab_ || - section->name_.find(".debug_") == 0) { - section->header_.sh_offset = 0; - section->header_.sh_size = 0; - section->section_index_ = 0; - } else { - if (section->header_.sh_type != SHT_NOBITS) { - DCHECK_LE(section->header_.sh_offset, end + kPageSize) << "Large gap between sections"; - end = std::max<off_t>(end, section->header_.sh_offset + section->header_.sh_size); - } - non_debug_sections.push_back(section); - } - } - shstrtab_.Reset(); - // Write the non-debug section headers, program headers, and ELF header again. - sections_ = std::move(non_debug_sections); - stream_.Seek(end, kSeekSet); - return End(); - } - - // The running program does not have access to section headers - // and the loader is not supposed to use them either. - // The dynamic sections therefore replicates some of the layout - // information like the address and size of .rodata and .text. - // It also contains other metadata like the SONAME. - // The .dynamic section is found using the PT_DYNAMIC program header. - void PrepareDynamicSection(const std::string& elf_file_path, - Elf_Word rodata_size, - Elf_Word text_size, - Elf_Word data_bimg_rel_ro_size, - Elf_Word bss_size, - Elf_Word bss_methods_offset, - Elf_Word bss_roots_offset, - Elf_Word dex_size) { - std::string soname(elf_file_path); - size_t directory_separator_pos = soname.rfind('/'); - if (directory_separator_pos != std::string::npos) { - soname = soname.substr(directory_separator_pos + 1); - } - - // Allocate all pre-dynamic sections. - rodata_.AllocateVirtualMemory(rodata_size); - text_.AllocateVirtualMemory(text_size); - if (data_bimg_rel_ro_size != 0) { - data_bimg_rel_ro_.AllocateVirtualMemory(data_bimg_rel_ro_size); - } - if (bss_size != 0) { - bss_.AllocateVirtualMemory(bss_size); - } - if (dex_size != 0) { - dex_.AllocateVirtualMemory(dex_size); - } - if (isa_ == InstructionSet::kMips || isa_ == InstructionSet::kMips64) { - abiflags_.AllocateVirtualMemory(abiflags_.GetSize()); - } - - // Cache .dynstr, .dynsym and .hash data. - dynstr_.Add(""); // dynstr should start with empty string. - Elf_Word oatdata = dynstr_.Add("oatdata"); - dynsym_.Add(oatdata, &rodata_, rodata_.GetAddress(), rodata_size, STB_GLOBAL, STT_OBJECT); - if (text_size != 0u) { - // The runtime does not care about the size of this symbol (it uses the "lastword" symbol). - // We use size 0 (meaning "unknown size" in ELF) to prevent overlap with the debug symbols. - Elf_Word oatexec = dynstr_.Add("oatexec"); - dynsym_.Add(oatexec, &text_, text_.GetAddress(), /* size= */ 0, STB_GLOBAL, STT_OBJECT); - Elf_Word oatlastword = dynstr_.Add("oatlastword"); - Elf_Word oatlastword_address = text_.GetAddress() + text_size - 4; - dynsym_.Add(oatlastword, &text_, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT); - } else if (rodata_size != 0) { - // rodata_ can be size 0 for dwarf_test. - Elf_Word oatlastword = dynstr_.Add("oatlastword"); - Elf_Word oatlastword_address = rodata_.GetAddress() + rodata_size - 4; - dynsym_.Add(oatlastword, &rodata_, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT); - } - if (data_bimg_rel_ro_size != 0u) { - Elf_Word oatdatabimgrelro = dynstr_.Add("oatdatabimgrelro"); - dynsym_.Add(oatdatabimgrelro, - &data_bimg_rel_ro_, - data_bimg_rel_ro_.GetAddress(), - data_bimg_rel_ro_size, - STB_GLOBAL, - STT_OBJECT); - Elf_Word oatdatabimgrelrolastword = dynstr_.Add("oatdatabimgrelrolastword"); - Elf_Word oatdatabimgrelrolastword_address = - data_bimg_rel_ro_.GetAddress() + data_bimg_rel_ro_size - 4; - dynsym_.Add(oatdatabimgrelrolastword, - &data_bimg_rel_ro_, - oatdatabimgrelrolastword_address, - 4, - STB_GLOBAL, - STT_OBJECT); - } - DCHECK_LE(bss_roots_offset, bss_size); - if (bss_size != 0u) { - Elf_Word oatbss = dynstr_.Add("oatbss"); - dynsym_.Add(oatbss, &bss_, bss_.GetAddress(), bss_roots_offset, STB_GLOBAL, STT_OBJECT); - DCHECK_LE(bss_methods_offset, bss_roots_offset); - DCHECK_LE(bss_roots_offset, bss_size); - // Add a symbol marking the start of the methods part of the .bss, if not empty. - if (bss_methods_offset != bss_roots_offset) { - Elf_Word bss_methods_address = bss_.GetAddress() + bss_methods_offset; - Elf_Word bss_methods_size = bss_roots_offset - bss_methods_offset; - Elf_Word oatbssroots = dynstr_.Add("oatbssmethods"); - dynsym_.Add( - oatbssroots, &bss_, bss_methods_address, bss_methods_size, STB_GLOBAL, STT_OBJECT); - } - // Add a symbol marking the start of the GC roots part of the .bss, if not empty. - if (bss_roots_offset != bss_size) { - Elf_Word bss_roots_address = bss_.GetAddress() + bss_roots_offset; - Elf_Word bss_roots_size = bss_size - bss_roots_offset; - Elf_Word oatbssroots = dynstr_.Add("oatbssroots"); - dynsym_.Add( - oatbssroots, &bss_, bss_roots_address, bss_roots_size, STB_GLOBAL, STT_OBJECT); - } - Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword"); - Elf_Word bsslastword_address = bss_.GetAddress() + bss_size - 4; - dynsym_.Add(oatbsslastword, &bss_, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT); - } - if (dex_size != 0u) { - Elf_Word oatdex = dynstr_.Add("oatdex"); - dynsym_.Add(oatdex, &dex_, dex_.GetAddress(), /* size= */ 0, STB_GLOBAL, STT_OBJECT); - Elf_Word oatdexlastword = dynstr_.Add("oatdexlastword"); - Elf_Word oatdexlastword_address = dex_.GetAddress() + dex_size - 4; - dynsym_.Add(oatdexlastword, &dex_, oatdexlastword_address, 4, STB_GLOBAL, STT_OBJECT); - } - - Elf_Word soname_offset = dynstr_.Add(soname); - - // We do not really need a hash-table since there is so few entries. - // However, the hash-table is the only way the linker can actually - // determine the number of symbols in .dynsym so it is required. - int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym); // Includes NULL. - std::vector<Elf_Word> hash; - hash.push_back(1); // Number of buckets. - hash.push_back(count); // Number of chains. - // Buckets. Having just one makes it linear search. - hash.push_back(1); // Point to first non-NULL symbol. - // Chains. This creates linked list of symbols. - hash.push_back(0); // Dummy entry for the NULL symbol. - for (int i = 1; i < count - 1; i++) { - hash.push_back(i + 1); // Each symbol points to the next one. - } - hash.push_back(0); // Last symbol terminates the chain. - hash_.Add(hash.data(), hash.size() * sizeof(hash[0])); - - // Allocate all remaining sections. - dynstr_.AllocateVirtualMemory(dynstr_.GetCacheSize()); - dynsym_.AllocateVirtualMemory(dynsym_.GetCacheSize()); - hash_.AllocateVirtualMemory(hash_.GetCacheSize()); - - Elf_Dyn dyns[] = { - { DT_HASH, { hash_.GetAddress() } }, - { DT_STRTAB, { dynstr_.GetAddress() } }, - { DT_SYMTAB, { dynsym_.GetAddress() } }, - { DT_SYMENT, { sizeof(Elf_Sym) } }, - { DT_STRSZ, { dynstr_.GetCacheSize() } }, - { DT_SONAME, { soname_offset } }, - { DT_NULL, { 0 } }, - }; - dynamic_.Add(&dyns, sizeof(dyns)); - dynamic_.AllocateVirtualMemory(dynamic_.GetCacheSize()); - - loaded_size_ = RoundUp(virtual_address_, kPageSize); - } - - void WriteDynamicSection() { - dynstr_.WriteCachedSection(); - dynsym_.WriteCachedSection(); - hash_.WriteCachedSection(); - dynamic_.WriteCachedSection(); - } - - Elf_Word GetLoadedSize() { - CHECK_NE(loaded_size_, 0u); - return loaded_size_; - } - - void WriteMIPSabiflagsSection() { - abiflags_.Start(); - abiflags_.Write(); - abiflags_.End(); - } - - void WriteBuildIdSection() { - build_id_.Start(); - build_id_.Write(); - build_id_.End(); - } - - void WriteBuildId(uint8_t build_id[kBuildIdLen]) { - stream_.Seek(build_id_.GetDigestStart(), kSeekSet); - stream_.WriteFully(build_id, kBuildIdLen); - stream_.Flush(); - } - - // Returns true if all writes and seeks on the output stream succeeded. - bool Good() { - return stream_.Good(); - } - - // Returns the builder's internal stream. - OutputStream* GetStream() { - return &stream_; - } - - off_t AlignFileOffset(size_t alignment) { - return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet); - } - - private: - static Elf_Ehdr MakeElfHeader(InstructionSet isa, const InstructionSetFeatures* features) { - Elf_Ehdr elf_header = Elf_Ehdr(); - switch (isa) { - case InstructionSet::kArm: - // Fall through. - case InstructionSet::kThumb2: { - elf_header.e_machine = EM_ARM; - elf_header.e_flags = EF_ARM_EABI_VER5; - break; - } - case InstructionSet::kArm64: { - elf_header.e_machine = EM_AARCH64; - elf_header.e_flags = 0; - break; - } - case InstructionSet::kX86: { - elf_header.e_machine = EM_386; - elf_header.e_flags = 0; - break; - } - case InstructionSet::kX86_64: { - elf_header.e_machine = EM_X86_64; - elf_header.e_flags = 0; - break; - } - case InstructionSet::kMips: { - elf_header.e_machine = EM_MIPS; - elf_header.e_flags = (EF_MIPS_NOREORDER | - EF_MIPS_PIC | - EF_MIPS_CPIC | - EF_MIPS_ABI_O32 | - (features->AsMipsInstructionSetFeatures()->IsR6() - ? EF_MIPS_ARCH_32R6 - : EF_MIPS_ARCH_32R2)); - break; - } - case InstructionSet::kMips64: { - elf_header.e_machine = EM_MIPS; - elf_header.e_flags = (EF_MIPS_NOREORDER | - EF_MIPS_PIC | - EF_MIPS_CPIC | - EF_MIPS_ARCH_64R6); - break; - } - case InstructionSet::kNone: { - LOG(FATAL) << "No instruction set"; - break; - } - default: { - LOG(FATAL) << "Unknown instruction set " << isa; - } - } - - elf_header.e_ident[EI_MAG0] = ELFMAG0; - elf_header.e_ident[EI_MAG1] = ELFMAG1; - elf_header.e_ident[EI_MAG2] = ELFMAG2; - elf_header.e_ident[EI_MAG3] = ELFMAG3; - elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr)) - ? ELFCLASS32 : ELFCLASS64; - elf_header.e_ident[EI_DATA] = ELFDATA2LSB; - elf_header.e_ident[EI_VERSION] = EV_CURRENT; - elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX; - elf_header.e_ident[EI_ABIVERSION] = 0; - elf_header.e_type = ET_DYN; - elf_header.e_version = 1; - elf_header.e_entry = 0; - elf_header.e_ehsize = sizeof(Elf_Ehdr); - elf_header.e_phentsize = sizeof(Elf_Phdr); - elf_header.e_shentsize = sizeof(Elf_Shdr); - return elf_header; - } - - // Create program headers based on written sections. - std::vector<Elf_Phdr> MakeProgramHeaders() { - CHECK(!sections_.empty()); - std::vector<Elf_Phdr> phdrs; - { - // The program headers must start with PT_PHDR which is used in - // loaded process to determine the number of program headers. - Elf_Phdr phdr = Elf_Phdr(); - phdr.p_type = PT_PHDR; - phdr.p_flags = PF_R; - phdr.p_offset = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr); - phdr.p_filesz = phdr.p_memsz = 0; // We need to fill this later. - phdr.p_align = sizeof(Elf_Off); - phdrs.push_back(phdr); - // Tell the linker to mmap the start of file to memory. - Elf_Phdr load = Elf_Phdr(); - load.p_type = PT_LOAD; - load.p_flags = PF_R; - load.p_offset = load.p_vaddr = load.p_paddr = 0; - load.p_filesz = load.p_memsz = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * kMaxProgramHeaders; - load.p_align = kPageSize; - phdrs.push_back(load); - } - // Create program headers for sections. - for (auto* section : sections_) { - const Elf_Shdr& shdr = section->header_; - if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) { - DCHECK(shdr.sh_addr != 0u) << "Allocate virtual memory for the section"; - // PT_LOAD tells the linker to mmap part of the file. - // The linker can only mmap page-aligned sections. - // Single PT_LOAD may contain several ELF sections. - Elf_Phdr& prev = phdrs.back(); - Elf_Phdr load = Elf_Phdr(); - load.p_type = PT_LOAD; - load.p_flags = section->phdr_flags_; - load.p_offset = shdr.sh_offset; - load.p_vaddr = load.p_paddr = shdr.sh_addr; - load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u); - load.p_memsz = shdr.sh_size; - load.p_align = shdr.sh_addralign; - if (prev.p_type == load.p_type && - prev.p_flags == load.p_flags && - prev.p_filesz == prev.p_memsz && // Do not merge .bss - load.p_filesz == load.p_memsz) { // Do not merge .bss - // Merge this PT_LOAD with the previous one. - Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset; - prev.p_filesz = size; - prev.p_memsz = size; - } else { - // If we are adding new load, it must be aligned. - CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize); - phdrs.push_back(load); - } - } - } - for (auto* section : sections_) { - const Elf_Shdr& shdr = section->header_; - if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) { - // Other PT_* types allow the program to locate interesting - // parts of memory at runtime. They must overlap with PT_LOAD. - if (section->phdr_type_ != 0) { - Elf_Phdr phdr = Elf_Phdr(); - phdr.p_type = section->phdr_type_; - phdr.p_flags = section->phdr_flags_; - phdr.p_offset = shdr.sh_offset; - phdr.p_vaddr = phdr.p_paddr = shdr.sh_addr; - phdr.p_filesz = phdr.p_memsz = shdr.sh_size; - phdr.p_align = shdr.sh_addralign; - phdrs.push_back(phdr); - } - } - } - // Set the size of the initial PT_PHDR. - CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR); - phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr); - - return phdrs; - } - - InstructionSet isa_; - const InstructionSetFeatures* features_; - - ErrorDelayingOutputStream stream_; - - Section rodata_; - Section text_; - Section data_bimg_rel_ro_; - Section bss_; - Section dex_; - CachedStringSection dynstr_; - SymbolSection dynsym_; - CachedSection hash_; - CachedSection dynamic_; - StringSection strtab_; - SymbolSection symtab_; - Section debug_frame_; - Section debug_frame_hdr_; - Section debug_info_; - Section debug_line_; - StringSection shstrtab_; - AbiflagsSection abiflags_; - BuildIdSection build_id_; - std::vector<std::unique_ptr<Section>> other_sections_; - - // List of used section in the order in which they were written. - std::vector<Section*> sections_; - Section* current_section_; // The section which is currently being written. - - bool started_; - bool finished_; - bool write_program_headers_; - - // The size of the memory taken by the ELF file when loaded. - size_t loaded_size_; - - // Used for allocation of virtual address space. - Elf_Addr virtual_address_; - - DISALLOW_COPY_AND_ASSIGN(ElfBuilder); -}; - -} // namespace linker -} // namespace art - -#endif // ART_COMPILER_LINKER_ELF_BUILDER_H_ diff --git a/compiler/linker/error_delaying_output_stream.h b/compiler/linker/error_delaying_output_stream.h deleted file mode 100644 index cadd71c3f0..0000000000 --- a/compiler/linker/error_delaying_output_stream.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_ -#define ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_ - -#include "output_stream.h" - -#include <android-base/logging.h> - -#include "base/macros.h" - -namespace art { -namespace linker { - -// OutputStream wrapper that delays reporting an error until Flush(). -class ErrorDelayingOutputStream final : public OutputStream { - public: - explicit ErrorDelayingOutputStream(OutputStream* output) - : OutputStream(output->GetLocation()), - output_(output), - output_good_(true), - output_offset_(0) { } - - // This function always succeeds to simplify code. - // Use Good() to check the actual status of the output stream. - bool WriteFully(const void* buffer, size_t byte_count) override { - if (output_good_) { - if (!output_->WriteFully(buffer, byte_count)) { - PLOG(ERROR) << "Failed to write " << byte_count - << " bytes to " << GetLocation() << " at offset " << output_offset_; - output_good_ = false; - } - } - output_offset_ += byte_count; - return true; - } - - // This function always succeeds to simplify code. - // Use Good() to check the actual status of the output stream. - off_t Seek(off_t offset, Whence whence) override { - // We keep shadow copy of the offset so that we return - // the expected value even if the output stream failed. - off_t new_offset; - switch (whence) { - case kSeekSet: - new_offset = offset; - break; - case kSeekCurrent: - new_offset = output_offset_ + offset; - break; - default: - LOG(FATAL) << "Unsupported seek type: " << whence; - UNREACHABLE(); - } - if (output_good_) { - off_t actual_offset = output_->Seek(offset, whence); - if (actual_offset == static_cast<off_t>(-1)) { - PLOG(ERROR) << "Failed to seek in " << GetLocation() << ". Offset=" << offset - << " whence=" << whence << " new_offset=" << new_offset; - output_good_ = false; - } - DCHECK_EQ(actual_offset, new_offset); - } - output_offset_ = new_offset; - return new_offset; - } - - // Flush the output and return whether all operations have succeeded. - // Do nothing if we already have a pending error. - bool Flush() override { - if (output_good_) { - output_good_ = output_->Flush(); - } - return output_good_; - } - - // Check (without flushing) whether all operations have succeeded so far. - bool Good() const { - return output_good_; - } - - private: - OutputStream* output_; - bool output_good_; // True if all writes to output succeeded. - off_t output_offset_; // Keep track of the current position in the stream. -}; - -} // namespace linker -} // namespace art - -#endif // ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_ diff --git a/compiler/linker/file_output_stream.cc b/compiler/linker/file_output_stream.cc deleted file mode 100644 index 477846ec65..0000000000 --- a/compiler/linker/file_output_stream.cc +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "file_output_stream.h" - -#include <sys/types.h> -#include <unistd.h> - -#include "base/unix_file/fd_file.h" - -namespace art { -namespace linker { - -FileOutputStream::FileOutputStream(File* file) : OutputStream(file->GetPath()), file_(file) {} - -bool FileOutputStream::WriteFully(const void* buffer, size_t byte_count) { - return file_->WriteFully(buffer, byte_count); -} - -off_t FileOutputStream::Seek(off_t offset, Whence whence) { - return lseek(file_->Fd(), offset, static_cast<int>(whence)); -} - -bool FileOutputStream::Flush() { - return file_->Flush() == 0; -} - -} // namespace linker -} // namespace art diff --git a/compiler/linker/file_output_stream.h b/compiler/linker/file_output_stream.h deleted file mode 100644 index 1417132981..0000000000 --- a/compiler/linker/file_output_stream.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ART_COMPILER_LINKER_FILE_OUTPUT_STREAM_H_ -#define ART_COMPILER_LINKER_FILE_OUTPUT_STREAM_H_ - -#include "base/os.h" - -#include "output_stream.h" - -namespace art { -namespace linker { - -class FileOutputStream final : public OutputStream { - public: - explicit FileOutputStream(File* file); - - ~FileOutputStream() override {} - - bool WriteFully(const void* buffer, size_t byte_count) override; - - off_t Seek(off_t offset, Whence whence) override; - - bool Flush() override; - - private: - File* const file_; - - DISALLOW_COPY_AND_ASSIGN(FileOutputStream); -}; - -} // namespace linker -} // namespace art - -#endif // ART_COMPILER_LINKER_FILE_OUTPUT_STREAM_H_ diff --git a/compiler/linker/output_stream.cc b/compiler/linker/output_stream.cc deleted file mode 100644 index f5a19138a5..0000000000 --- a/compiler/linker/output_stream.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "output_stream.h" - -namespace art { -namespace linker { - -std::ostream& operator<<(std::ostream& os, const Whence& rhs) { - switch (rhs) { - case kSeekSet: os << "SEEK_SET"; break; - case kSeekCurrent: os << "SEEK_CUR"; break; - case kSeekEnd: os << "SEEK_END"; break; - default: UNREACHABLE(); - } - return os; -} - -} // namespace linker -} // namespace art diff --git a/compiler/linker/output_stream.h b/compiler/linker/output_stream.h deleted file mode 100644 index 5310e2fa41..0000000000 --- a/compiler/linker/output_stream.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ART_COMPILER_LINKER_OUTPUT_STREAM_H_ -#define ART_COMPILER_LINKER_OUTPUT_STREAM_H_ - -#include <ostream> -#include <string> - -#include "base/macros.h" - -namespace art { -namespace linker { - -enum Whence { - kSeekSet = SEEK_SET, - kSeekCurrent = SEEK_CUR, - kSeekEnd = SEEK_END, -}; -std::ostream& operator<<(std::ostream& os, const Whence& rhs); - -class OutputStream { - public: - explicit OutputStream(const std::string& location) : location_(location) {} - - virtual ~OutputStream() {} - - const std::string& GetLocation() const { - return location_; - } - - virtual bool WriteFully(const void* buffer, size_t byte_count) = 0; - - virtual off_t Seek(off_t offset, Whence whence) = 0; - - /* - * Flushes the stream. Returns whether the operation was successful. - * - * An OutputStream may delay reporting errors from WriteFully() or - * Seek(). In that case, Flush() shall report any pending error. - */ - virtual bool Flush() = 0; - - private: - const std::string location_; - - DISALLOW_COPY_AND_ASSIGN(OutputStream); -}; - -} // namespace linker -} // namespace art - -#endif // ART_COMPILER_LINKER_OUTPUT_STREAM_H_ diff --git a/compiler/linker/output_stream_test.cc b/compiler/linker/output_stream_test.cc index bcb129c2da..00231b1240 100644 --- a/compiler/linker/output_stream_test.cc +++ b/compiler/linker/output_stream_test.cc @@ -14,15 +14,14 @@ * limitations under the License. */ -#include "file_output_stream.h" -#include "vector_output_stream.h" - #include <android-base/logging.h> #include "base/macros.h" #include "base/unix_file/fd_file.h" -#include "buffered_output_stream.h" #include "common_runtime_test.h" +#include "stream/buffered_output_stream.h" +#include "stream/file_output_stream.h" +#include "stream/vector_output_stream.h" namespace art { namespace linker { diff --git a/compiler/linker/vector_output_stream.cc b/compiler/linker/vector_output_stream.cc deleted file mode 100644 index f2cae5b1d5..0000000000 --- a/compiler/linker/vector_output_stream.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "vector_output_stream.h" - -#include <android-base/logging.h> - -namespace art { -namespace linker { - -VectorOutputStream::VectorOutputStream(const std::string& location, std::vector<uint8_t>* vector) - : OutputStream(location), offset_(vector->size()), vector_(vector) {} - -off_t VectorOutputStream::Seek(off_t offset, Whence whence) { - CHECK(whence == kSeekSet || whence == kSeekCurrent || whence == kSeekEnd) << whence; - off_t new_offset = 0; - switch (whence) { - case kSeekSet: { - new_offset = offset; - break; - } - case kSeekCurrent: { - new_offset = offset_ + offset; - break; - } - case kSeekEnd: { - new_offset = vector_->size() + offset; - break; - } - } - EnsureCapacity(new_offset); - offset_ = new_offset; - return offset_; -} - -} // namespace linker -} // namespace art diff --git a/compiler/linker/vector_output_stream.h b/compiler/linker/vector_output_stream.h deleted file mode 100644 index 0d34da6cba..0000000000 --- a/compiler/linker/vector_output_stream.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ART_COMPILER_LINKER_VECTOR_OUTPUT_STREAM_H_ -#define ART_COMPILER_LINKER_VECTOR_OUTPUT_STREAM_H_ - -#include "output_stream.h" - -#include <string.h> -#include <string> -#include <vector> - -namespace art { -namespace linker { - -class VectorOutputStream final : public OutputStream { - public: - VectorOutputStream(const std::string& location, std::vector<uint8_t>* vector); - - ~VectorOutputStream() override {} - - bool WriteFully(const void* buffer, size_t byte_count) override { - if (static_cast<size_t>(offset_) == vector_->size()) { - const uint8_t* start = reinterpret_cast<const uint8_t*>(buffer); - vector_->insert(vector_->end(), &start[0], &start[byte_count]); - offset_ += byte_count; - } else { - off_t new_offset = offset_ + byte_count; - EnsureCapacity(new_offset); - memcpy(&(*vector_)[offset_], buffer, byte_count); - offset_ = new_offset; - } - return true; - } - - off_t Seek(off_t offset, Whence whence) override; - - bool Flush() override { - return true; - } - - private: - void EnsureCapacity(off_t new_offset) { - if (new_offset > static_cast<off_t>(vector_->size())) { - vector_->resize(new_offset); - } - } - - off_t offset_; - std::vector<uint8_t>* const vector_; - - DISALLOW_COPY_AND_ASSIGN(VectorOutputStream); -}; - -} // namespace linker -} // namespace art - -#endif // ART_COMPILER_LINKER_VECTOR_OUTPUT_STREAM_H_ |