1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:37:34 +00:00

LibELF: Add support for loading objects with multiple data and text segments

This enables loading executables with multiple data and text segments. Also
it fixes loading executables where the text segment has a non-zero offset.

Example:

  $ echo "main () {}" > test.c
  $ gcc -Wl,-z,separate-code -o test test.c
  $ objdump -p test
  test:     file format elf32-i386

  Program Header:
      PHDR off    0x00000034 vaddr 0x00000034 paddr 0x00000034 align 2**2
           filesz 0x000000e0 memsz 0x000000e0 flags r--
    INTERP off    0x00000114 vaddr 0x00000114 paddr 0x00000114 align 2**0
           filesz 0x00000013 memsz 0x00000013 flags r--
      LOAD off    0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**12
           filesz 0x000003c4 memsz 0x000003c4 flags r--
      LOAD off    0x00001000 vaddr 0x00001000 paddr 0x00001000 align 2**12
           filesz 0x00000279 memsz 0x00000279 flags r-x
      LOAD off    0x00002000 vaddr 0x00002000 paddr 0x00002000 align 2**12
           filesz 0x00000004 memsz 0x00000004 flags r--
      LOAD off    0x00002004 vaddr 0x00003004 paddr 0x00003004 align 2**12
           filesz 0x00000100 memsz 0x00000124 flags rw-
   DYNAMIC off    0x00002014 vaddr 0x00003014 paddr 0x00003014 align 2**2
           filesz 0x000000c8 memsz 0x000000c8 flags rw-
This commit is contained in:
Gunnar Beutner 2021-04-13 19:31:34 +02:00 committed by Andreas Kling
parent c3ee70591a
commit cd7512a2ad
4 changed files with 121 additions and 77 deletions

View file

@ -38,6 +38,22 @@
namespace ELF {
class LoadedSegment {
public:
LoadedSegment(VirtualAddress address, size_t size)
: m_address(address)
, m_size(size)
{
}
VirtualAddress address() const { return m_address; }
size_t size() const { return m_size; }
private:
VirtualAddress m_address;
size_t m_size;
};
class DynamicLoader : public RefCounted<DynamicLoader> {
public:
static RefPtr<DynamicLoader> try_create(int fd, String filename);
@ -74,7 +90,8 @@ public:
template<typename F>
void for_each_needed_library(F) const;
VirtualAddress text_segment_load_address() const { return m_text_segment_load_address; }
VirtualAddress base_address() const { return m_base_address; }
const Vector<LoadedSegment> text_segments() const { return m_text_segments; }
bool is_dynamic() const { return m_elf_image.is_dynamic(); }
static Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&);
@ -141,8 +158,8 @@ private:
RefPtr<DynamicObject> m_dynamic_object;
VirtualAddress m_text_segment_load_address;
size_t m_text_segment_size { 0 };
VirtualAddress m_base_address;
Vector<LoadedSegment> m_text_segments;
VirtualAddress m_relro_segment_address;
size_t m_relro_segment_size { 0 };