[lld] r371085 - [ELF] Initialize PhdrEntry::p_align to maxPageSize for PT_LOAD

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 5 09:32:31 PDT 2019


Author: maskray
Date: Thu Sep  5 09:32:31 2019
New Revision: 371085

URL: http://llvm.org/viewvc/llvm-project?rev=371085&view=rev
Log:
[ELF] Initialize PhdrEntry::p_align to maxPageSize for PT_LOAD

```
Writer<ELFT>::run
  assignFileOffsets
    setFileOffset
      computeFileOffset
        os->ptLoad->p_align may be smaller than config->maxPageSize
  setPhdrs
    p_align = max(p_align, config->maxPageSize)
```

If we move the config->maxPageSize logic to the constructor of
PhdrEntry, computeFileOffset can be simplified.

Reviewed By: ruiu

Differential Revision: https://reviews.llvm.org/D67211

Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/ELF/Writer.h

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=371085&r1=371084&r2=371085&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Sep  5 09:32:31 2019
@@ -2270,12 +2270,9 @@ template <class ELFT> void Writer<ELFT>:
 // load executables without any address adjustment.
 static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
   // The first section in a PT_LOAD has to have congruent offset and address
-  // module the page size.
-  if (os->ptLoad && os->ptLoad->firstSec == os) {
-    uint64_t alignment =
-        std::max<uint64_t>(os->ptLoad->p_align, config->maxPageSize);
-    return alignTo(off, alignment, os->addr);
-  }
+  // modulo the maximum page size.
+  if (os->ptLoad && os->ptLoad->firstSec == os)
+    return alignTo(off, os->ptLoad->p_align, os->addr);
 
   // File offsets are not significant for .bss sections other than the first one
   // in a PT_LOAD. By convention, we keep section offsets monotonically
@@ -2385,9 +2382,7 @@ template <class ELFT> void Writer<ELFT>:
         p->p_paddr = first->getLMA();
     }
 
-    if (p->p_type == PT_LOAD) {
-      p->p_align = std::max<uint64_t>(p->p_align, config->maxPageSize);
-    } else if (p->p_type == PT_GNU_RELRO) {
+    if (p->p_type == PT_GNU_RELRO) {
       p->p_align = 1;
       // musl/glibc ld.so rounds the size down, so we need to round up
       // to protect the last page. This is a no-op on FreeBSD which always

Modified: lld/trunk/ELF/Writer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.h?rev=371085&r1=371084&r2=371085&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.h (original)
+++ lld/trunk/ELF/Writer.h Thu Sep  5 09:32:31 2019
@@ -9,6 +9,7 @@
 #ifndef LLD_ELF_WRITER_H
 #define LLD_ELF_WRITER_H
 
+#include "Config.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include <cstdint>
@@ -28,7 +29,9 @@ template <class ELFT> void writeResult()
 // Each contains type, access flags and range of output sections that will be
 // placed in it.
 struct PhdrEntry {
-  PhdrEntry(unsigned type, unsigned flags) : p_type(type), p_flags(flags) {}
+  PhdrEntry(unsigned type, unsigned flags)
+      : p_align(type == llvm::ELF::PT_LOAD ? config->maxPageSize : 0),
+        p_type(type), p_flags(flags) {}
   void add(OutputSection *sec);
 
   uint64_t p_paddr = 0;




More information about the llvm-commits mailing list