Some code clean-up.
Change-Id: I4b745fd5298cd61c793e3b57514b48347bd66c0e
diff --git a/runtime/elf_file.cc b/runtime/elf_file.cc
index c3a2559..18053c3 100644
--- a/runtime/elf_file.cc
+++ b/runtime/elf_file.cc
@@ -23,7 +23,10 @@
#include "base/logging.h"
#include "base/stringprintf.h"
#include "base/stl_util.h"
+#include "base/unix_file/fd_file.h"
#include "dwarf.h"
+#include "elf_file_impl.h"
+#include "elf_utils.h"
#include "leb128.h"
#include "utils.h"
#include "instruction_set.h"
@@ -1661,7 +1664,7 @@
}
};
-class DebugLineInstructionIterator {
+class DebugLineInstructionIterator FINAL {
public:
static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) {
std::unique_ptr<DebugLineInstructionIterator> line_iter(
@@ -1688,11 +1691,11 @@
}
}
- uint8_t* GetInstruction() {
+ uint8_t* GetInstruction() const {
return current_instruction_;
}
- bool IsExtendedOpcode() {
+ bool IsExtendedOpcode() const {
return header_->IsExtendedOpcode(current_instruction_);
}
@@ -1719,8 +1722,8 @@
: header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size),
current_instruction_(header->GetDebugLineData()) {}
- DebugLineHeader* header_;
- uint8_t* last_instruction_;
+ DebugLineHeader* const header_;
+ uint8_t* const last_instruction_;
uint8_t* current_instruction_;
};
@@ -1781,9 +1784,8 @@
}
}
-class DebugTag {
+class DebugTag FINAL {
public:
- const uint32_t index_;
~DebugTag() {}
// Creates a new tag and moves data pointer up to the start of the next one.
// nullptr means error.
@@ -1820,14 +1822,18 @@
return size_;
}
- bool HasChild() {
+ bool HasChild() const {
return has_child_;
}
- uint32_t GetTagNumber() {
+ uint32_t GetTagNumber() const {
return tag_;
}
+ uint32_t GetIndex() const {
+ return index_;
+ }
+
// Gets the offset of a particular attribute in this tag structure.
// Interpretation of the data is left to the consumer. 0 is returned if the
// tag does not contain the attribute.
@@ -1857,6 +1863,8 @@
size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
size_ += attr_size;
}
+
+ const uint32_t index_;
std::map<uint32_t, uint32_t> off_map_;
std::map<uint32_t, uint32_t> size_map_;
uint32_t size_;
@@ -1884,7 +1892,7 @@
if (tag.get() == nullptr) {
return false;
} else {
- tags_.insert(std::pair<uint32_t, uint32_t>(tag->index_, tag_list_.size()));
+ tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size()));
tag_list_.push_back(std::move(tag));
}
}
@@ -1904,8 +1912,8 @@
private:
DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {}
- const uint8_t* begin_;
- const uint8_t* end_;
+ const uint8_t* const begin_;
+ const uint8_t* const end_;
std::map<uint32_t, uint32_t> tags_;
std::vector<std::unique_ptr<DebugTag>> tag_list_;
};
@@ -1983,10 +1991,10 @@
last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size),
current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)),
current_tag_(abbrev_->ReadTag(current_entry_)) {}
- DebugAbbrev* abbrev_;
+ DebugAbbrev* const abbrev_;
DebugInfoHeader* current_cu_;
DebugInfoHeader* next_cu_;
- uint8_t* last_entry_;
+ uint8_t* const last_entry_;
uint8_t* current_entry_;
DebugTag* current_tag_;
};
@@ -2406,24 +2414,15 @@
template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word,
Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>;
-ElfFile::ElfFile(ElfFileImpl32* elf32) : is_elf64_(false) {
- CHECK_NE(elf32, static_cast<ElfFileImpl32*>(nullptr));
- elf_.elf32_ = elf32;
+ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
}
-ElfFile::ElfFile(ElfFileImpl64* elf64) : is_elf64_(true) {
- CHECK_NE(elf64, static_cast<ElfFileImpl64*>(nullptr));
- elf_.elf64_ = elf64;
+ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
}
ElfFile::~ElfFile() {
- if (is_elf64_) {
- CHECK_NE(elf_.elf64_, static_cast<ElfFileImpl64*>(nullptr));
- delete elf_.elf64_;
- } else {
- CHECK_NE(elf_.elf32_, static_cast<ElfFileImpl32*>(nullptr));
- delete elf_.elf32_;
- }
+ // Should never have 32 and 64-bit impls.
+ CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
}
ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg) {
@@ -2445,8 +2444,9 @@
return new ElfFile(elf_file_impl);
} else if (header[EI_CLASS] == ELFCLASS32) {
ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only, error_msg);
- if (elf_file_impl == nullptr)
+ if (elf_file_impl == nullptr) {
return nullptr;
+ }
return new ElfFile(elf_file_impl);
} else {
*error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
@@ -2471,13 +2471,15 @@
uint8_t* header = map->Begin();
if (header[EI_CLASS] == ELFCLASS64) {
ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
- if (elf_file_impl == nullptr)
+ if (elf_file_impl == nullptr) {
return nullptr;
+ }
return new ElfFile(elf_file_impl);
} else if (header[EI_CLASS] == ELFCLASS32) {
ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
- if (elf_file_impl == nullptr)
+ if (elf_file_impl == nullptr) {
return nullptr;
+ }
return new ElfFile(elf_file_impl);
} else {
*error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
@@ -2489,12 +2491,11 @@
}
#define DELEGATE_TO_IMPL(func, ...) \
- if (is_elf64_) { \
- CHECK_NE(elf_.elf64_, static_cast<ElfFileImpl64*>(nullptr)); \
- return elf_.elf64_->func(__VA_ARGS__); \
+ if (elf64_.get() != nullptr) { \
+ return elf64_->func(__VA_ARGS__); \
} else { \
- CHECK_NE(elf_.elf32_, static_cast<ElfFileImpl32*>(nullptr)); \
- return elf_.elf32_->func(__VA_ARGS__); \
+ DCHECK(elf32_.get() != nullptr); \
+ return elf32_->func(__VA_ARGS__); \
}
bool ElfFile::Load(bool executable, std::string* error_msg) {
@@ -2522,29 +2523,31 @@
}
bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) {
- if (is_elf64_) {
- CHECK_NE(elf_.elf64_, static_cast<ElfFileImpl64*>(nullptr));
+ if (elf32_.get() == nullptr) {
+ CHECK(elf64_.get() != nullptr);
- Elf64_Shdr *shdr = elf_.elf64_->FindSectionByName(section_name);
- if (shdr == nullptr)
+ Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
+ if (shdr == nullptr) {
return false;
-
- if (offset != nullptr)
+ }
+ if (offset != nullptr) {
*offset = shdr->sh_offset;
- if (size != nullptr)
+ }
+ if (size != nullptr) {
*size = shdr->sh_size;
+ }
return true;
} else {
- CHECK_NE(elf_.elf32_, static_cast<ElfFileImpl32*>(nullptr));
-
- Elf32_Shdr *shdr = elf_.elf32_->FindSectionByName(section_name);
- if (shdr == nullptr)
+ Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
+ if (shdr == nullptr) {
return false;
-
- if (offset != nullptr)
+ }
+ if (offset != nullptr) {
*offset = shdr->sh_offset;
- if (size != nullptr)
+ }
+ if (size != nullptr) {
*size = shdr->sh_size;
+ }
return true;
}
}
@@ -2565,26 +2568,14 @@
return false;
}
- if (elf_file->is_elf64_)
- return elf_file->elf_.elf64_->Strip(error_msg);
+ if (elf_file->elf64_.get() != nullptr)
+ return elf_file->elf64_->Strip(error_msg);
else
- return elf_file->elf_.elf32_->Strip(error_msg);
+ return elf_file->elf32_->Strip(error_msg);
}
bool ElfFile::Fixup(uintptr_t base_address) {
DELEGATE_TO_IMPL(Fixup, base_address);
}
-ElfFileImpl32* ElfFile::GetImpl32() const {
- CHECK(!is_elf64_);
- CHECK_NE(elf_.elf32_, static_cast<ElfFileImpl32*>(nullptr));
- return elf_.elf32_;
-}
-
-ElfFileImpl64* ElfFile::GetImpl64() const {
- CHECK(is_elf64_);
- CHECK_NE(elf_.elf64_, static_cast<ElfFileImpl64*>(nullptr));
- return elf_.elf64_;
-}
-
} // namespace art
diff --git a/runtime/elf_file.h b/runtime/elf_file.h
index a7f3056..10d6360 100644
--- a/runtime/elf_file.h
+++ b/runtime/elf_file.h
@@ -17,12 +17,25 @@
#ifndef ART_RUNTIME_ELF_FILE_H_
#define ART_RUNTIME_ELF_FILE_H_
+#include <memory>
#include <string>
-#include "base/unix_file/fd_file.h"
-#include "elf_file_impl.h"
+#include "base/macros.h"
+// Explicitly include our own elf.h to avoid Linux and other dependencies.
+#include "./elf.h"
+#include "os.h"
namespace art {
+template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
+ typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
+ typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
+class ElfFileImpl;
+
+// Explicitly instantiated in elf_file.cc
+typedef ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word, Elf32_Sword,
+ Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off> ElfFileImpl32;
+typedef ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word, Elf64_Sword,
+ Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off> ElfFileImpl64;
// Used for compile time and runtime for ElfFile access. Because of
// the need for use at runtime, cannot directly use LLVM classes such as
@@ -35,8 +48,6 @@
static ElfFile* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg);
~ElfFile();
- const bool is_elf64_;
-
// Load segments into memory based on PT_LOAD program headers
bool Load(bool executable, std::string* error_msg);
@@ -68,17 +79,26 @@
bool Fixup(uintptr_t base_address);
- ElfFileImpl32* GetImpl32() const;
- ElfFileImpl64* GetImpl64() const;
+ bool Is64Bit() const {
+ return elf64_.get() != nullptr;
+ }
+
+ ElfFileImpl32* GetImpl32() const {
+ return elf32_.get();
+ }
+
+ ElfFileImpl64* GetImpl64() const {
+ return elf64_.get();
+ }
private:
explicit ElfFile(ElfFileImpl32* elf32);
explicit ElfFile(ElfFileImpl64* elf64);
- union ElfFileContainer {
- ElfFileImpl32* elf32_;
- ElfFileImpl64* elf64_;
- } elf_;
+ const std::unique_ptr<ElfFileImpl32> elf32_;
+ const std::unique_ptr<ElfFileImpl64> elf64_;
+
+ DISALLOW_COPY_AND_ASSIGN(ElfFile);
};
} // namespace art
diff --git a/runtime/elf_file_impl.h b/runtime/elf_file_impl.h
index a2fc422..a8bb465 100644
--- a/runtime/elf_file_impl.h
+++ b/runtime/elf_file_impl.h
@@ -21,11 +21,9 @@
#include <memory>
#include <vector>
-#include "base/unix_file/fd_file.h"
-#include "globals.h"
-#include "elf_utils.h"
+// Explicitly include our own elf.h to avoid Linux and other dependencies.
+#include "./elf.h"
#include "mem_map.h"
-#include "os.h"
namespace art {
@@ -207,13 +205,9 @@
Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel,
Elf_Rela, Elf_Dyn, Elf_Off>> gdb_file_mapping_;
void GdbJITSupport();
-};
-// Explicitly instantiated in elf_file.cc
-typedef ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word, Elf32_Sword,
- Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off> ElfFileImpl32;
-typedef ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word, Elf64_Sword,
- Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off> ElfFileImpl64;
+ DISALLOW_COPY_AND_ASSIGN(ElfFileImpl);
+};
} // namespace art
diff --git a/runtime/elf_utils.h b/runtime/elf_utils.h
index 5966d05..676cd52 100644
--- a/runtime/elf_utils.h
+++ b/runtime/elf_utils.h
@@ -67,11 +67,11 @@
// Patching section type
#define SHT_OAT_PATCH SHT_LOUSER
-inline void SetBindingAndType(Elf32_Sym* sym, unsigned char b, unsigned char t) {
+static inline void SetBindingAndType(Elf32_Sym* sym, unsigned char b, unsigned char t) {
sym->st_info = (b << 4) + (t & 0x0f);
}
-inline bool IsDynamicSectionPointer(Elf32_Word d_tag, Elf32_Word e_machine) {
+static inline bool IsDynamicSectionPointer(Elf32_Word d_tag, Elf32_Word e_machine) {
switch (d_tag) {
// case 1: well known d_tag values that imply Elf32_Dyn.d_un contains an address in d_ptr
case DT_PLTGOT:
diff --git a/runtime/memory_region.h b/runtime/memory_region.h
index 6459963..4eb6d47 100644
--- a/runtime/memory_region.h
+++ b/runtime/memory_region.h
@@ -21,6 +21,7 @@
#include "base/logging.h"
#include "base/macros.h"
+#include "base/value_object.h"
#include "globals.h"
namespace art {
@@ -28,9 +29,9 @@
// Memory regions are useful for accessing memory with bounds check in
// debug mode. They can be safely passed by value and do not assume ownership
// of the region.
-class MemoryRegion {
+class MemoryRegion FINAL : public ValueObject {
public:
- MemoryRegion() : pointer_(NULL), size_(0) {}
+ MemoryRegion() : pointer_(nullptr), size_(0) {}
MemoryRegion(void* pointer, uintptr_t size) : pointer_(pointer), size_(size) {}
void* pointer() const { return pointer_; }
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index e366084..9c4a61e 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -43,6 +43,7 @@
#include "arch/x86/registers_x86.h"
#include "arch/x86_64/quick_method_frame_info_x86_64.h"
#include "arch/x86_64/registers_x86_64.h"
+#include "base/unix_file/fd_file.h"
#include "atomic.h"
#include "class_linker.h"
#include "debugger.h"