[ELF] - Linkerscript: implement SIZEOF_HEADERS.

SIZEOF_HEADERS - Return the size in bytes of the output file’s headers.

It is is a feature used in FreeBsd script, for example.
There is a discussion on PR28688 page about it.

Differential revision: https://reviews.llvm.org/D23165

llvm-svn: 278204
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 8c73e53..2d64af3 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -238,7 +238,7 @@
   }
 
   // Assign addresses as instructed by linker script SECTIONS sub-commands.
-  Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
+  Dot = getSizeOfHeaders();
   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
   uintX_t ThreadBssOffset = 0;
 
@@ -430,6 +430,11 @@
   return 0;
 }
 
+template <class ELFT>
+typename ELFT::uint LinkerScript<ELFT>::getSizeOfHeaders() {
+  return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
+}
+
 // Returns indices of ELF headers containing specific section, identified
 // by Name. Each index is a zero based number of ELF header listed within
 // PHDRS {} script block.
@@ -915,6 +920,21 @@
   }
 }
 
+static uint64_t getSizeOfHeaders() {
+  switch (Config->EKind) {
+  case ELF32LEKind:
+    return Script<ELF32LE>::X->getSizeOfHeaders();
+  case ELF32BEKind:
+    return Script<ELF32BE>::X->getSizeOfHeaders();
+  case ELF64LEKind:
+    return Script<ELF64LE>::X->getSizeOfHeaders();
+  case ELF64BEKind:
+    return Script<ELF64BE>::X->getSizeOfHeaders();
+  default:
+    llvm_unreachable("unsupported target");
+  }
+}
+
 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
   StringRef Op = next();
   assert(Op == "=" || Op == "+=");
@@ -1063,6 +1083,8 @@
     expect(")");
     return [=](uint64_t Dot) { return getSectionSize(Name); };
   }
+  if (Tok == "SIZEOF_HEADERS")
+    return [=](uint64_t Dot) { return getSizeOfHeaders(); };
 
   // Parse a symbol name or a number literal.
   uint64_t V = 0;