[lld] r221890 - [ELF] Fix the fileoffset according to the ELF spec.

Shankar Easwaran shankare at codeaurora.org
Thu Nov 13 06:30:21 PST 2014


Author: shankare
Date: Thu Nov 13 08:30:21 2014
New Revision: 221890

URL: http://llvm.org/viewvc/llvm-project?rev=221890&view=rev
Log:
[ELF] Fix the fileoffset according to the ELF spec.

With --no-align-segments, there is a bug that the fileoffset may not be
congruent to virtual address modulo page alignment.

This patch fixes the problem.

Added:
    lld/trunk/test/elf/X86_64/alignoffset.test
Modified:
    lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h

Modified: lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h?rev=221890&r1=221889&r2=221890&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h Thu Nov 13 08:30:21 2014
@@ -423,7 +423,8 @@ void Segment<ELFT>::assignFileOffsets(ui
           if (sect && sect->isLoadableSection() &&
               ((virtualAddress & (p_align - 1)) !=
                (fileOffset & (p_align - 1))))
-            fileOffset = llvm::RoundUpToAlignment(fileOffset, p_align);
+            fileOffset = llvm::RoundUpToAlignment(fileOffset, p_align) +
+                         (virtualAddress % p_align);
         }
       } else if (!isDataPageAlignedForNMagic && needAlign(section)) {
         fileOffset =

Added: lld/trunk/test/elf/X86_64/alignoffset.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/X86_64/alignoffset.test?rev=221890&view=auto
==============================================================================
--- lld/trunk/test/elf/X86_64/alignoffset.test (added)
+++ lld/trunk/test/elf/X86_64/alignoffset.test Thu Nov 13 08:30:21 2014
@@ -0,0 +1,119 @@
+# Checks that segments are aligned as per ELF spec. The segment virtual address
+# modulo page alignment should be equal to offset modulo page alignment.
+
+# Build executable
+# RUN: yaml2obj -format=elf -docnum 1 %s -o %t.o
+# RUN: lld -flavor gnu -target x86_64 %t.o -o %t.exe -static \
+# RUN: --no-align-segments --rosegment --noinhibit-exec
+# RUN: llvm-readobj -program-headers %t.exe | FileCheck %s
+#
+#CHECK:    Offset: 0x115C
+#CHECK:    VirtualAddress: 0x40015C
+#CHECK:    PhysicalAddress: 0x40015C
+#
+#
+#const int a = 0;
+#int main() {
+#  foo();
+#  return 0;
+#}
+#
+#int foo() { return 0; }
+
+# object
+---
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  OSABI:           ELFOSABI_GNU
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000010
+    Content:         554889E54883EC10C745FC00000000E81C000000B9000000008945F889C84883C4105DC36666662E0F1F840000000000554889E5B8000000005DC3
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000004
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000004
+    Content:         ''
+  - Name:            .rodata
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x0000000000000004
+    Content:         '00000000'
+  - Name:            .comment
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x0000000000000001
+    Content:         005562756E747520636C616E672076657273696F6E20332E352E302D73766E3231373330342D317E6578703120286272616E636865732F72656C656173655F33352920286261736564206F6E204C4C564D20332E352E302900
+  - Name:            .note.GNU-stack
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .eh_frame
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x0000000000000008
+    Content:         1400000000000000037A5200017810011B0C070890010000180000001C000000000000002400000000410E108602430D060000001800000038000000000000000B00000000410E108602430D06000000
+  - Name:            .rela.eh_frame
+    Type:            SHT_RELA
+    Link:            .symtab
+    AddressAlign:    0x0000000000000008
+    Info:            .eh_frame
+    Relocations:
+      - Offset:          0x0000000000000020
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+        Addend:          0
+      - Offset:          0x000000000000003C
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+        Addend:          48
+Symbols:
+  Local:
+    - Name:            1.c
+      Type:            STT_FILE
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .rodata
+      Type:            STT_SECTION
+      Section:         .rodata
+    - Name:            .comment
+      Type:            STT_SECTION
+      Section:         .comment
+    - Name:            .note.GNU-stack
+      Type:            STT_SECTION
+      Section:         .note.GNU-stack
+    - Name:            .eh_frame
+      Type:            STT_SECTION
+      Section:         .eh_frame
+  Global:
+    - Name:            a
+      Type:            STT_OBJECT
+      Section:         .rodata
+      Size:            0x0000000000000004
+    - Name:            foo
+      Type:            STT_FUNC
+      Section:         .text
+      Value:           0x0000000000000030
+      Size:            0x000000000000000B
+    - Name:            main
+      Type:            STT_FUNC
+      Section:         .text
+      Size:            0x0000000000000024
+...





More information about the llvm-commits mailing list