Do not use absolute addresses in image test oat files.
We have not been using oat files with absolute addresses
for a long time, except that we forgot to remove code that
creates such files for image tests (gtests). Remove that
code now as well as related code including code that deals
with obsolete `oat_patches` sections.
Test: m test-art-host-gtest
Change-Id: If3e752f01c453e2a2628ac82c3a990c68047610e
diff --git a/dex2oat/linker/elf_writer.cc b/dex2oat/linker/elf_writer.cc
index ca34864..aa5097a 100644
--- a/dex2oat/linker/elf_writer.cc
+++ b/dex2oat/linker/elf_writer.cc
@@ -48,17 +48,5 @@
CHECK_NE(0U, *oat_data_offset);
}
-bool ElfWriter::Fixup(File* file, uintptr_t oat_data_begin) {
- std::string error_msg;
- std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb*/false, &error_msg));
- CHECK(elf_file.get() != nullptr) << error_msg;
-
- // Lookup "oatdata" symbol address.
- uintptr_t oatdata_address = ElfWriter::GetOatDataAddress(elf_file.get());
- uintptr_t base_address = oat_data_begin - oatdata_address;
-
- return elf_file->Fixup(base_address);
-}
-
} // namespace linker
} // namespace art
diff --git a/dex2oat/linker/elf_writer.h b/dex2oat/linker/elf_writer.h
index a60c708..e41377e 100644
--- a/dex2oat/linker/elf_writer.h
+++ b/dex2oat/linker/elf_writer.h
@@ -50,8 +50,6 @@
// Returns runtime oat_data runtime address for an opened ElfFile.
static uintptr_t GetOatDataAddress(ElfFile* elf_file);
- static bool Fixup(File* file, uintptr_t oat_data_begin);
-
virtual ~ElfWriter() {}
virtual void Start() = 0;
diff --git a/dex2oat/linker/image_test.h b/dex2oat/linker/image_test.h
index 0b5386a..13a424b 100644
--- a/dex2oat/linker/image_test.h
+++ b/dex2oat/linker/image_test.h
@@ -342,16 +342,6 @@
image_filenames,
image_filenames.size());
ASSERT_TRUE(success_image);
-
- for (size_t i = 0, size = oat_filenames.size(); i != size; ++i) {
- const char* oat_filename = oat_filenames[i].c_str();
- std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
- ASSERT_TRUE(oat_file != nullptr);
- bool success_fixup = ElfWriter::Fixup(oat_file.get(), writer->GetOatDataBegin(i));
- ASSERT_TRUE(success_fixup);
- ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
- << oat_filename;
- }
}
}
diff --git a/runtime/elf_file.cc b/runtime/elf_file.cc
index b6aed7f..0b4f1f3 100644
--- a/runtime/elf_file.cc
+++ b/runtime/elf_file.cc
@@ -1368,56 +1368,6 @@
}
template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) {
- if (base_address_delta == 0) {
- return true;
- }
- return ApplyOatPatchesTo(".debug_frame", base_address_delta) &&
- ApplyOatPatchesTo(".debug_info", base_address_delta) &&
- ApplyOatPatchesTo(".debug_line", base_address_delta);
-}
-
-template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo(
- const char* target_section_name, Elf_Addr delta) {
- auto target_section = FindSectionByName(target_section_name);
- if (target_section == nullptr) {
- return true;
- }
- std::string patches_name = target_section_name + std::string(".oat_patches");
- auto patches_section = FindSectionByName(patches_name.c_str());
- if (patches_section == nullptr) {
- LOG(ERROR) << patches_name << " section not found.";
- return false;
- }
- if (patches_section->sh_type != SHT_OAT_PATCH) {
- LOG(ERROR) << "Unexpected type of " << patches_name;
- return false;
- }
- ApplyOatPatches(
- Begin() + patches_section->sh_offset,
- Begin() + patches_section->sh_offset + patches_section->sh_size,
- delta,
- Begin() + target_section->sh_offset,
- Begin() + target_section->sh_offset + target_section->sh_size);
- return true;
-}
-
-// Apply LEB128 encoded patches to given section.
-template <typename ElfTypes>
-void ElfFileImpl<ElfTypes>::ApplyOatPatches(
- const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
- uint8_t* to_patch, const uint8_t* to_patch_end) {
- using UnalignedAddress __attribute__((__aligned__(1))) = Elf_Addr;
- while (patches < patches_end) {
- to_patch += DecodeUnsignedLeb128(&patches);
- DCHECK_LE(patches, patches_end) << "Unexpected end of patch list.";
- DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section.";
- *reinterpret_cast<UnalignedAddress*>(to_patch) += delta;
- }
-}
-
-template <typename ElfTypes>
bool ElfFileImpl<ElfTypes>::Strip(File* file, std::string* error_msg) {
// ELF files produced by MCLinker look roughly like this
//
@@ -1519,163 +1469,6 @@
return true;
}
-static const bool DEBUG_FIXUP = false;
-
-template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) {
- if (!FixupDynamic(base_address)) {
- LOG(WARNING) << "Failed to fixup .dynamic in " << file_path_;
- return false;
- }
- if (!FixupSectionHeaders(base_address)) {
- LOG(WARNING) << "Failed to fixup section headers in " << file_path_;
- return false;
- }
- if (!FixupProgramHeaders(base_address)) {
- LOG(WARNING) << "Failed to fixup program headers in " << file_path_;
- return false;
- }
- if (!FixupSymbols(base_address, true)) {
- LOG(WARNING) << "Failed to fixup .dynsym in " << file_path_;
- return false;
- }
- if (!FixupSymbols(base_address, false)) {
- LOG(WARNING) << "Failed to fixup .symtab in " << file_path_;
- return false;
- }
- if (!FixupRelocations(base_address)) {
- LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_path_;
- return false;
- }
- static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision.");
- if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) {
- LOG(WARNING) << "Failed to fixup debug sections in " << file_path_;
- return false;
- }
- return true;
-}
-
-template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) {
- for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
- Elf_Dyn& elf_dyn = GetDynamic(i);
- Elf_Word d_tag = elf_dyn.d_tag;
- if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
- Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
- if (DEBUG_FIXUP) {
- LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
- file_path_.c_str(), i,
- static_cast<uint64_t>(d_ptr),
- static_cast<uint64_t>(d_ptr + base_address));
- }
- d_ptr += base_address;
- elf_dyn.d_un.d_ptr = d_ptr;
- }
- }
- return true;
-}
-
-template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) {
- for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
- Elf_Shdr* sh = GetSectionHeader(i);
- CHECK(sh != nullptr);
- // 0 implies that the section will not exist in the memory of the process
- if (sh->sh_addr == 0) {
- continue;
- }
- if (DEBUG_FIXUP) {
- LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
- file_path_.c_str(), i,
- static_cast<uint64_t>(sh->sh_addr),
- static_cast<uint64_t>(sh->sh_addr + base_address));
- }
- sh->sh_addr += base_address;
- }
- return true;
-}
-
-template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) {
- // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
- for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
- Elf_Phdr* ph = GetProgramHeader(i);
- CHECK(ph != nullptr);
- CHECK_EQ(ph->p_vaddr, ph->p_paddr) << file_path_ << " i=" << i;
- CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
- << file_path_ << " i=" << i;
- if (DEBUG_FIXUP) {
- LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
- file_path_.c_str(), i,
- static_cast<uint64_t>(ph->p_vaddr),
- static_cast<uint64_t>(ph->p_vaddr + base_address));
- }
- ph->p_vaddr += base_address;
- ph->p_paddr += base_address;
- CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
- << file_path_ << " i=" << i;
- }
- return true;
-}
-
-template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) {
- Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
- // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
- Elf_Shdr* symbol_section = FindSectionByType(section_type);
- if (symbol_section == nullptr) {
- // file is missing optional .symtab
- CHECK(!dynamic) << file_path_;
- return true;
- }
- for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
- Elf_Sym* symbol = GetSymbol(section_type, i);
- CHECK(symbol != nullptr);
- if (symbol->st_value != 0) {
- if (DEBUG_FIXUP) {
- LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
- file_path_.c_str(), i,
- static_cast<uint64_t>(symbol->st_value),
- static_cast<uint64_t>(symbol->st_value + base_address));
- }
- symbol->st_value += base_address;
- }
- }
- return true;
-}
-
-template <typename ElfTypes>
-bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) {
- for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
- Elf_Shdr* sh = GetSectionHeader(i);
- CHECK(sh != nullptr);
- if (sh->sh_type == SHT_REL) {
- for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
- Elf_Rel& rel = GetRel(*sh, j);
- if (DEBUG_FIXUP) {
- LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
- file_path_.c_str(), j,
- static_cast<uint64_t>(rel.r_offset),
- static_cast<uint64_t>(rel.r_offset + base_address));
- }
- rel.r_offset += base_address;
- }
- } else if (sh->sh_type == SHT_RELA) {
- for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
- Elf_Rela& rela = GetRela(*sh, j);
- if (DEBUG_FIXUP) {
- LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
- file_path_.c_str(), j,
- static_cast<uint64_t>(rela.r_offset),
- static_cast<uint64_t>(rela.r_offset + base_address));
- }
- rela.r_offset += base_address;
- }
- }
- }
- return true;
-}
-
// Explicit instantiations
template class ElfFileImpl<ElfTypes32>;
template class ElfFileImpl<ElfTypes64>;
@@ -1889,15 +1682,4 @@
}
}
-bool ElfFile::Fixup(uint64_t base_address) {
- if (elf64_.get() != nullptr) {
- return elf64_->Fixup(static_cast<Elf64_Addr>(base_address));
- } else {
- DCHECK(elf32_.get() != nullptr);
- CHECK(IsUint<32>(base_address)) << std::hex << base_address;
- return elf32_->Fixup(static_cast<Elf32_Addr>(base_address));
- }
- DELEGATE_TO_IMPL(Fixup, base_address);
-}
-
} // namespace art
diff --git a/runtime/elf_file.h b/runtime/elf_file.h
index ccfd445..0e9dd19 100644
--- a/runtime/elf_file.h
+++ b/runtime/elf_file.h
@@ -86,12 +86,6 @@
// Returns true on success, false on failure.
static bool Strip(File* file, std::string* error_msg);
- // Fixup an ELF file so that that oat header will be loaded at oat_begin.
- // Returns true on success, false on failure.
- static bool Fixup(File* file, uint64_t oat_data_begin);
-
- bool Fixup(uint64_t base_address);
-
bool Is64Bit() const {
return elf64_.get() != nullptr;
}
diff --git a/runtime/elf_file_impl.h b/runtime/elf_file_impl.h
index 9900c76..ba02d62 100644
--- a/runtime/elf_file_impl.h
+++ b/runtime/elf_file_impl.h
@@ -119,17 +119,6 @@
/*inout*/MemMap* reservation,
/*out*/std::string* error_msg);
- bool Fixup(Elf_Addr base_address);
- bool FixupDynamic(Elf_Addr base_address);
- bool FixupSectionHeaders(Elf_Addr base_address);
- bool FixupProgramHeaders(Elf_Addr base_address);
- bool FixupSymbols(Elf_Addr base_address, bool dynamic);
- bool FixupRelocations(Elf_Addr base_address);
- bool FixupDebugSections(Elf_Addr base_address_delta);
- bool ApplyOatPatchesTo(const char* target_section_name, Elf_Addr base_address_delta);
- static void ApplyOatPatches(const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
- uint8_t* to_patch, const uint8_t* to_patch_end);
-
bool Strip(File* file, std::string* error_msg);
private: