[Lldb-commits] [lldb] r350923 - ELF: Fix base address computation code for files generated by yaml2obj

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Fri Jan 11 02:18:40 PST 2019


Author: labath
Date: Fri Jan 11 02:18:40 2019
New Revision: 350923

URL: http://llvm.org/viewvc/llvm-project?rev=350923&view=rev
Log:
ELF: Fix base address computation code for files generated by yaml2obj

The code was assuming that the elf file will have a PT_LOAD segment
starting from the first byte of the file. While this is true for files
generated by most linkers (it's a way of saving space), it is not a
requirement. And files not satisfying this constraint can still be
perfectly executable. yaml2obj is one of the tools which produces files
like this.

This patch relaxes the check in ObjectFileELF to take the address of the
first PT_LOAD segment as the base address of the object (instead of the
one with the offset 0). Since the PT_LOAD segments are supposed to be
sorted according to the VM address, this entry will also be the one with
the lowest VM address.

If we ever run into files which don't have the PT_LOAD segments sorted,
we can easily change this code to return the lowest VM address as the
base address (if that is the correct thing to do for these files).

Added:
    lldb/trunk/lit/Modules/ELF/base-address.yaml
Modified:
    lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Added: lldb/trunk/lit/Modules/ELF/base-address.yaml
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Modules/ELF/base-address.yaml?rev=350923&view=auto
==============================================================================
--- lldb/trunk/lit/Modules/ELF/base-address.yaml (added)
+++ lldb/trunk/lit/Modules/ELF/base-address.yaml Fri Jan 11 02:18:40 2019
@@ -0,0 +1,34 @@
+# RUN: yaml2obj %s > %t
+# RUN: lldb-test object-file %t | FileCheck %s
+
+# CHECK: Base VM address: 0x400000
+
+--- !ELF
+FileHeader:      
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+  Entry:           0x0000000000400078
+Sections:        
+  - Name:            .pad
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    Address:         0x0000000000400000
+    AddressAlign:    0x0000000000001000
+    Size:            0x78
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x0000000000400078
+    AddressAlign:    0x0000000000000001
+    Content:         48C7C0E700000048C7C72F0000000F05CC
+ProgramHeaders:
+  - Type: PT_LOAD
+    Flags: [ PF_X, PF_R ]
+    VAddr: 0x400000
+    Align: 0x200000
+    Sections:
+      - Section: .pad
+      - Section: .text
+...

Modified: lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp?rev=350923&r1=350922&r2=350923&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp Fri Jan 11 02:18:40 2019
@@ -1051,7 +1051,7 @@ lldb_private::Address ObjectFileELF::Get
 Address ObjectFileELF::GetBaseAddress() {
   for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
     const ELFProgramHeader &H = EnumPHdr.value();
-    if (H.p_type != PT_LOAD || H.p_offset != 0)
+    if (H.p_type != PT_LOAD)
       continue;
 
     return Address(




More information about the lldb-commits mailing list