@@ -92,14 +92,26 @@ void Machine::setup_linux(__u64& rsp,
9292 push_down (*this , dst, platform, sizeof (platform));
9393 const auto platform_addr = dst;
9494
95- /* Push program headers */
95+ /* ELF program headers */
9696 const auto * binary_ehdr = elf_offset<Elf64_Ehdr> (m_binary, 0 );
9797 const auto * binary_phdr = elf_offset<Elf64_Phdr> (m_binary, binary_ehdr->e_phoff );
9898 const unsigned phdr_count = binary_ehdr->e_phnum ;
99- dst -= phdr_count * sizeof (Elf64_Phdr);
100- dst &= ~0xFLL ;
101- const auto phdr_location = dst;
102- this ->copy_to_guest (dst, binary_phdr, phdr_count * sizeof (Elf64_Phdr));
99+
100+ /* Check if we have a PT_PHDR program header already loaded into memory */
101+ address_t phdr_location = 0 ;
102+ for (unsigned i = 0 ; i < phdr_count; i++) {
103+ if (binary_phdr[i].p_type == PT_PHDR) {
104+ phdr_location = this ->m_image_base + binary_phdr[i].p_vaddr ;
105+ break ;
106+ }
107+ }
108+ if (phdr_location == 0 ) {
109+ /* Push program headers */
110+ dst -= phdr_count * sizeof (Elf64_Phdr);
111+ dst &= ~0xFLL ;
112+ phdr_location = dst;
113+ this ->copy_to_guest (dst, binary_phdr, phdr_count * sizeof (Elf64_Phdr));
114+ }
103115
104116 /* Push arguments to main() */
105117 std::vector<address_t > argv;
0 commit comments