[ELF] - Linkerscript: implemented ADDR command.

ADDR(section)
Return the absolute address (the VMA) of the named section.

Used in the wild, eg.: https://searchcode.com/file/53617342/arch/x86/kernel/vmlinux.lds.S

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

llvm-svn: 280070
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 16c3aef9..cd67d18 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -533,6 +533,16 @@
 }
 
 template <class ELFT>
+typename ELFT::uint
+LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
+  for (OutputSectionBase<ELFT> *Sec : *OutputSections)
+    if (Sec->getName() == Name)
+      return Sec->getVA();
+  error("undefined section " + Name);
+  return 0;
+}
+
+template <class ELFT>
 typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
     if (Sec->getName() == Name)
@@ -1044,6 +1054,21 @@
   }
 }
 
+static uint64_t getSectionAddress(StringRef Name) {
+  switch (Config->EKind) {
+  case ELF32LEKind:
+    return Script<ELF32LE>::X->getOutputSectionAddress(Name);
+  case ELF32BEKind:
+    return Script<ELF32BE>::X->getOutputSectionAddress(Name);
+  case ELF64LEKind:
+    return Script<ELF64LE>::X->getOutputSectionAddress(Name);
+  case ELF64BEKind:
+    return Script<ELF64BE>::X->getOutputSectionAddress(Name);
+  default:
+    llvm_unreachable("unsupported target");
+  }
+}
+
 static uint64_t getHeaderSize() {
   switch (Config->EKind) {
   case ELF32LEKind:
@@ -1154,6 +1179,12 @@
 
   // Built-in functions are parsed here.
   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
+  if (Tok == "ADDR") {
+    expect("(");
+    StringRef Name = next();
+    expect(")");
+    return [=](uint64_t Dot) { return getSectionAddress(Name); };
+  }
   if (Tok == "ASSERT")
     return readAssert();
   if (Tok == "ALIGN") {