[lld] r323456 - Move LMAOffset from the OutputSection to the PhdrEntry. NFC.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 11:02:09 PST 2018


Author: rafael
Date: Thu Jan 25 11:02:08 2018
New Revision: 323456

URL: http://llvm.org/viewvc/llvm-project?rev=323456&view=rev
Log:
Move LMAOffset from the OutputSection to the PhdrEntry. NFC.

If two sections are in the same PT_LOAD, their relatives offsets,
virtual address and physical addresses are all the same.

I initially wanted to have a single global LMAOffset, on the
assumption that every ELF file was in practiced loaded contiguously in
both physical and virtual memory.

Unfortunately that is not the case. The linux kernel has:

  LOAD           0x200000 0xffffffff81000000 0x0000000001000000 0xced000 0xced000 R E 0x200000
  LOAD           0x1000000 0xffffffff81e00000 0x0000000001e00000 0x15f000 0x15f000 RW  0x200000
  LOAD           0x1200000 0x0000000000000000 0x0000000001f5f000 0x01b198 0x01b198 RW  0x200000
  LOAD           0x137b000 0xffffffff81f7b000 0x0000000001f7b000 0x116000 0x1ec000 RWE 0x200000

The delta for all but the third PT_LOAD is the same:
0xffffffff80000000. I think the 3rd one is a hack for implementing per
cpu data, but we can't break that.

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

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=323456&r1=323455&r2=323456&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Jan 25 11:02:08 2018
@@ -671,8 +671,8 @@ void LinkerScript::assignOffsets(OutputS
   // will set the LMA such that the difference between VMA and LMA for the
   // section is the same as the preceding output section in the same region
   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
-  if (Ctx->LMAOffset)
-    Ctx->OutSec->LMAOffset = Ctx->LMAOffset;
+  if (PhdrEntry *L = Ctx->OutSec->PtLoad)
+    L->LMAOffset = Ctx->LMAOffset;
 
   // The Size previously denoted how many InputSections had been added to this
   // section, and was used for sorting SHF_LINK_ORDER sections. Reset it to

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=323456&r1=323455&r2=323456&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Thu Jan 25 11:02:08 2018
@@ -49,7 +49,7 @@ public:
 
   static bool classof(const BaseCommand *C);
 
-  uint64_t getLMA() const { return Addr + LMAOffset; }
+  uint64_t getLMA() const { return PtLoad ? Addr + PtLoad->LMAOffset : Addr; }
   template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
 
   unsigned SectionIndex;
@@ -78,7 +78,6 @@ public:
 
   // The following fields correspond to Elf_Shdr members.
   uint64_t Offset = 0;
-  uint64_t LMAOffset = 0;
   uint64_t Addr = 0;
   uint32_t ShName = 0;
 

Modified: lld/trunk/ELF/Writer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.h?rev=323456&r1=323455&r2=323456&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.h (original)
+++ lld/trunk/ELF/Writer.h Thu Jan 25 11:02:08 2018
@@ -44,6 +44,7 @@ struct PhdrEntry {
   OutputSection *FirstSec = nullptr;
   OutputSection *LastSec = nullptr;
   bool HasLMA = false;
+  uint64_t LMAOffset = 0;
 };
 
 void addReservedSymbols();




More information about the llvm-commits mailing list