Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 2 Aug 2018 18:01:17 +0000 (UTC)
From:      Dimitry Andric <dim@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r337151 - in projects/clang700-import/contrib/llvm/tools/lld: COFF ELF ELF/Arch
Message-ID:  <201808021801.w72I1HZY040588@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dim
Date: Thu Aug  2 18:01:17 2018
New Revision: 337151
URL: https://svnweb.freebsd.org/changeset/base/337151

Log:
  Merge lld trunk r338150 (just before the 7.0.0 branch point), and
  resolve conflicts.

Modified:
  projects/clang700-import/contrib/llvm/tools/lld/COFF/ICF.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/ARM.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/Hexagon.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/Config.h
  projects/clang700-import/contrib/llvm/tools/lld/ELF/Driver.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/ICF.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/InputFiles.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/InputSection.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/Options.td
  projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp
  projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.h
  projects/clang700-import/contrib/llvm/tools/lld/ELF/Writer.cpp
Directory Properties:
  projects/clang700-import/contrib/llvm/tools/lld/   (props changed)

Modified: projects/clang700-import/contrib/llvm/tools/lld/COFF/ICF.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/COFF/ICF.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/COFF/ICF.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -27,6 +27,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Parallel.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/xxhash.h"
 #include <algorithm>
 #include <atomic>
 #include <vector>
@@ -65,13 +66,6 @@ class ICF { (private)
   std::atomic<bool> Repeat = {false};
 };
 
-// Returns a hash value for S.
-uint32_t ICF::getHash(SectionChunk *C) {
-  return hash_combine(C->getOutputCharacteristics(), C->SectionName,
-                      C->Relocs.size(), uint32_t(C->Header->SizeOfRawData),
-                      C->Checksum, C->getContents());
-}
-
 // Returns true if section S is subject of ICF.
 //
 // Microsoft's documentation
@@ -265,7 +259,7 @@ void ICF::run(ArrayRef<Chunk *> Vec) {
   // Initially, we use hash values to partition sections.
   for_each(parallel::par, Chunks.begin(), Chunks.end(), [&](SectionChunk *SC) {
     // Set MSB to 1 to avoid collisions with non-hash classs.
-    SC->Class[0] = getHash(SC) | (1 << 31);
+    SC->Class[0] = xxHash64(SC->getContents()) | (1 << 31);
   });
 
   // From now on, sections in Chunks are ordered so that sections in

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/ARM.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/ARM.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/ARM.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -97,10 +97,19 @@ ARM::ARM() {
 }
 
 uint32_t ARM::calcEFlags() const {
+  // The ABIFloatType is used by loaders to detect the floating point calling
+  // convention.
+  uint32_t ABIFloatType = 0;
+  if (Config->ARMVFPArgs == ARMVFPArgKind::Base ||
+      Config->ARMVFPArgs == ARMVFPArgKind::Default)
+    ABIFloatType = EF_ARM_ABI_FLOAT_SOFT;
+  else if (Config->ARMVFPArgs == ARMVFPArgKind::VFP)
+    ABIFloatType = EF_ARM_ABI_FLOAT_HARD;
+
   // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
   // but we don't have any firm guarantees of conformance. Linux AArch64
   // kernels (as of 2016) require an EABI version to be set.
-  return EF_ARM_EABI_VER5;
+  return EF_ARM_EABI_VER5 | ABIFloatType;
 }
 
 RelExpr ARM::getRelExpr(RelType Type, const Symbol &S,

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/Hexagon.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/Hexagon.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/Arch/Hexagon.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -70,6 +70,12 @@ void Hexagon::relocateOne(uint8_t *Loc, RelType Type, 
   switch (Type) {
   case R_HEX_NONE:
     break;
+  case R_HEX_12_X:
+    or32le(Loc, applyMask(0x000007e0, Val));
+    break;
+  case R_HEX_32_6_X:
+    or32le(Loc, applyMask(0x0fff3fff, Val >> 6));
+    break;
   case R_HEX_B15_PCREL:
     or32le(Loc, applyMask(0x00df20fe, Val >> 2));
     break;

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/Config.h
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/Config.h	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/Config.h	Thu Aug  2 18:01:17 2018	(r337151)
@@ -58,6 +58,9 @@ enum class SortSectionPolicy { Default, None, Alignmen
 // For --target2
 enum class Target2Policy { Abs, Rel, GotRel };
 
+// For tracking ARM Float Argument PCS
+enum class ARMVFPArgKind { Default, Base, VFP, ToolChain };
+
 struct SymbolVersion {
   llvm::StringRef Name;
   bool IsExternCpp;
@@ -133,6 +136,7 @@ struct Configuration {
   bool EhFrameHdr;
   bool EmitRelocs;
   bool EnableNewDtags;
+  bool ExecuteOnly;
   bool ExportDynamic;
   bool FixCortexA53Errata843419;
   bool GcSections;
@@ -195,6 +199,7 @@ struct Configuration {
   StripPolicy Strip;
   UnresolvedPolicy UnresolvedSymbols;
   Target2Policy Target2;
+  ARMVFPArgKind ARMVFPArgs = ARMVFPArgKind::Default;
   BuildIdKind BuildId = BuildIdKind::None;
   ELFKind EKind = ELFNoneKind;
   uint16_t DefaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL;

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/Driver.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/Driver.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/Driver.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -302,6 +302,14 @@ static void checkOptions(opt::InputArgList &Args) {
     if (Config->Pie)
       error("-r and -pie may not be used together");
   }
+
+  if (Config->ExecuteOnly) {
+    if (Config->EMachine != EM_AARCH64)
+      error("-execute-only is only supported on AArch64 targets");
+
+    if (Config->SingleRoRx && !Script->HasSectionsCommand)
+      error("-execute-only and -no-rosegment cannot be used together");
+  }
 }
 
 static const char *getReproduceOption(opt::InputArgList &Args) {
@@ -493,6 +501,8 @@ static bool isOutputFormatBinary(opt::InputArgList &Ar
     StringRef S = Arg->getValue();
     if (S == "binary")
       return true;
+    if (S.startswith("elf"))
+      return false;
     error("unknown --oformat value: " + S);
   }
   return false;
@@ -747,6 +757,8 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args
   Config->EnableNewDtags =
       Args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
   Config->Entry = Args.getLastArgValue(OPT_entry);
+  Config->ExecuteOnly =
+      Args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
   Config->ExportDynamic =
       Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
   Config->FilterList = args::getStrings(Args, OPT_filter);
@@ -1303,6 +1315,12 @@ static void findKeepUniqueSections(opt::InputArgList &
   }
 }
 
+static const char *LibcallRoutineNames[] = {
+#define HANDLE_LIBCALL(code, name) name,
+#include "llvm/IR/RuntimeLibcalls.def"
+#undef HANDLE_LIBCALL
+};
+
 // Do actual linking. Note that when this function is called,
 // all linker scripts have already been parsed.
 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
@@ -1369,10 +1387,20 @@ template <class ELFT> void LinkerDriver::link(opt::Inp
   for (StringRef S : Config->Undefined)
     handleUndefined<ELFT>(S);
 
-  // If an entry symbol is in a static archive, pull out that file now
-  // to complete the symbol table. After this, no new names except a
-  // few linker-synthesized ones will be added to the symbol table.
+  // If an entry symbol is in a static archive, pull out that file now.
   handleUndefined<ELFT>(Config->Entry);
+
+  // If any of our inputs are bitcode files, the LTO code generator may create
+  // references to certain library functions that might not be explicit in the
+  // bitcode file's symbol table. If any of those library functions are defined
+  // in a bitcode file in an archive member, we need to arrange to use LTO to
+  // compile those archive members by adding them to the link beforehand.
+  //
+  // With this the symbol table should be complete. After this, no new names
+  // except a few linker-synthesized ones will be added to the symbol table.
+  if (!BitcodeFiles.empty())
+    for (const char *S : LibcallRoutineNames)
+      handleUndefined<ELFT>(S);
 
   // Return if there were name resolution errors.
   if (errorCount())

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/ICF.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/ICF.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/ICF.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -80,9 +80,10 @@
 #include "SyntheticSections.h"
 #include "Writer.h"
 #include "lld/Common/Threads.h"
-#include "llvm/ADT/Hashing.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/Object/ELF.h"
+#include "llvm/Support/xxhash.h"
 #include <algorithm>
 #include <atomic>
 
@@ -155,12 +156,6 @@ template <class ELFT> class ICF { (private)
 };
 }
 
-// Returns a hash value for S. Note that the information about
-// relocation targets is not included in the hash value.
-template <class ELFT> static uint32_t getHash(InputSection *S) {
-  return hash_combine(S->Flags, S->getSize(), S->NumRelocations, S->Data);
-}
-
 // Returns true if section S is subject of ICF.
 static bool isEligible(InputSection *S) {
   if (!S->Live || S->KeepUnique || !(S->Flags & SHF_ALLOC))
@@ -441,7 +436,7 @@ template <class ELFT> void ICF<ELFT>::run() {
   // Initially, we use hash values to partition sections.
   parallelForEach(Sections, [&](InputSection *S) {
     // Set MSB to 1 to avoid collisions with non-hash IDs.
-    S->Class[0] = getHash<ELFT>(S) | (1U << 31);
+    S->Class[0] = xxHash64(S->Data) | (1U << 31);
   });
 
   // From now on, sections in Sections vector are ordered so that sections

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/InputFiles.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/InputFiles.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/InputFiles.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -494,6 +494,46 @@ void ObjFile<ELFT>::initializeSections(
   }
 }
 
+// For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
+// flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
+// the input objects have been compiled.
+static void updateARMVFPArgs(const ARMAttributeParser &Attributes,
+                             const InputFile *F) {
+  if (!Attributes.hasAttribute(ARMBuildAttrs::ABI_VFP_args))
+    // If an ABI tag isn't present then it is implicitly given the value of 0
+    // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
+    // including some in glibc that don't use FP args (and should have value 3)
+    // don't have the attribute so we do not consider an implicit value of 0
+    // as a clash.
+    return;
+
+  unsigned VFPArgs = Attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
+  ARMVFPArgKind Arg;
+  switch (VFPArgs) {
+  case ARMBuildAttrs::BaseAAPCS:
+    Arg = ARMVFPArgKind::Base;
+    break;
+  case ARMBuildAttrs::HardFPAAPCS:
+    Arg = ARMVFPArgKind::VFP;
+    break;
+  case ARMBuildAttrs::ToolChainFPPCS:
+    // Tool chain specific convention that conforms to neither AAPCS variant.
+    Arg = ARMVFPArgKind::ToolChain;
+    break;
+  case ARMBuildAttrs::CompatibleFPAAPCS:
+    // Object compatible with all conventions.
+    return;
+  default:
+    error(toString(F) + ": unknown Tag_ABI_VFP_args value: " + Twine(VFPArgs));
+    return;
+  }
+  // Follow ld.bfd and error if there is a mix of calling conventions.
+  if (Config->ARMVFPArgs != Arg && Config->ARMVFPArgs != ARMVFPArgKind::Default)
+    error(toString(F) + ": incompatible Tag_ABI_VFP_args");
+  else
+    Config->ARMVFPArgs = Arg;
+}
+
 // The ARM support in lld makes some use of instructions that are not available
 // on all ARM architectures. Namely:
 // - Use of BLX instruction for interworking between ARM and Thumb state.
@@ -573,6 +613,8 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(co
     ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
     Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
     updateSupportedARMFeatures(Attributes);
+    updateARMVFPArgs(Attributes, this);
+
     // FIXME: Retain the first attribute section we see. The eglibc ARM
     // dynamic loaders require the presence of an attribute section for dlopen
     // to work. In a full implementation we would merge all attribute sections.

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/InputSection.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/InputSection.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/InputSection.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -221,8 +221,8 @@ template <class ELFT>
 Defined *InputSectionBase::getEnclosingFunction(uint64_t Offset) {
   for (Symbol *B : File->getSymbols())
     if (Defined *D = dyn_cast<Defined>(B))
-      if (D->Section == this && D->Type == STT_FUNC &&
-          D->Value <= Offset && Offset < D->Value + D->Size)
+      if (D->Section == this && D->Type == STT_FUNC && D->Value <= Offset &&
+          Offset < D->Value + D->Size)
         return D;
   return nullptr;
 }
@@ -671,7 +671,7 @@ static uint64_t getRelocTargetVA(const InputFile *File
   case R_TLSLD_GOT_FROM_END:
     return InX::Got->getTlsIndexOff() + A - InX::Got->getSize();
   case R_TLSLD_GOT:
-      return InX::Got->getTlsIndexOff() + A;
+    return InX::Got->getTlsIndexOff() + A;
   case R_TLSLD_PC:
     return InX::Got->getTlsIndexVA() + A - P;
   }
@@ -842,8 +842,7 @@ void InputSectionBase::relocateAlloc(uint8_t *Buf, uin
 // For each function-defining prologue, find any calls to __morestack,
 // and replace them with calls to __morestack_non_split.
 static void switchMorestackCallsToMorestackNonSplit(
-    llvm::DenseSet<Defined *>& Prologues,
-    std::vector<Relocation *>& MorestackCalls) {
+    DenseSet<Defined *> &Prologues, std::vector<Relocation *> &MorestackCalls) {
 
   // If the target adjusted a function's prologue, all calls to
   // __morestack inside that function should be switched to
@@ -873,9 +872,8 @@ static void switchMorestackCallsToMorestackNonSplit(
   }
 }
 
-static bool
-enclosingPrologueAdjusted(uint64_t Offset,
-                          const llvm::DenseSet<Defined *> &Prologues) {
+static bool enclosingPrologueAdjusted(uint64_t Offset,
+                                      const DenseSet<Defined *> &Prologues) {
   for (Defined *F : Prologues)
     if (F->Value <= Offset && Offset < F->Value + F->Size)
       return true;
@@ -891,7 +889,7 @@ void InputSectionBase::adjustSplitStackFunctionPrologu
                                                          uint8_t *End) {
   if (!getFile<ELFT>()->SplitStack)
     return;
-  llvm::DenseSet<Defined *> AdjustedPrologues;
+  DenseSet<Defined *> AdjustedPrologues;
   std::vector<Relocation *> MorestackCalls;
 
   for (Relocation &Rel : Relocations) {
@@ -1071,8 +1069,7 @@ void MergeInputSection::splitNonStrings(ArrayRef<uint8
   bool IsAlloc = Flags & SHF_ALLOC;
 
   for (size_t I = 0; I != Size; I += EntSize)
-    Pieces.emplace_back(I, xxHash64(toStringRef(Data.slice(I, EntSize))),
-                        !IsAlloc);
+    Pieces.emplace_back(I, xxHash64(Data.slice(I, EntSize)), !IsAlloc);
 }
 
 template <class ELFT>

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/Options.td
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/Options.td	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/Options.td	Thu Aug  2 18:01:17 2018	(r337151)
@@ -58,8 +58,8 @@ defm allow_multiple_definition: B<"allow-multiple-defi
     "Do not allow multiple definitions (default)">;
 
 defm apply_dynamic_relocs: B<"apply-dynamic-relocs",
-    "Apply dynamic relocations to place",
-    "Do not apply dynamic relocations to place">;
+    "Apply link-time values for dynamic relocations",
+    "Do not apply link-time values for dynamic relocations (default)">;
 
 defm as_needed: B<"as-needed",
     "Only set DT_NEEDED for shared libraries if used",
@@ -130,6 +130,10 @@ def error_unresolved_symbols: F<"error-unresolved-symb
   HelpText<"Report unresolved symbols as errors">;
 
 defm exclude_libs: Eq<"exclude-libs", "Exclude static libraries from automatic export">;
+
+defm execute_only: B<"execute-only",
+    "Do not mark executable sections readable",
+    "Mark executable sections readable (default)">;
 
 defm export_dynamic: B<"export-dynamic",
     "Put symbols in the dynamic symbol table",

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -335,7 +335,7 @@ void BuildIdSection::writeBuildId(ArrayRef<uint8_t> Bu
   switch (Config->BuildId) {
   case BuildIdKind::Fast:
     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
-      write64le(Dest, xxHash64(toStringRef(Arr)));
+      write64le(Dest, xxHash64(Arr));
     });
     break;
   case BuildIdKind::Md5:
@@ -1935,6 +1935,23 @@ SymbolTableSection<ELFT>::SymbolTableSection(StringTab
   this->Entsize = sizeof(Elf_Sym);
 }
 
+static BssSection *getCommonSec(Symbol *Sym) {
+  if (!Config->DefineCommon)
+    if (auto *D = dyn_cast<Defined>(Sym))
+      return dyn_cast_or_null<BssSection>(D->Section);
+  return nullptr;
+}
+
+static uint32_t getSymSectionIndex(Symbol *Sym) {
+  if (getCommonSec(Sym))
+    return SHN_COMMON;
+  if (!isa<Defined>(Sym) || Sym->NeedsPltAddr)
+    return SHN_UNDEF;
+  if (const OutputSection *OS = Sym->getOutputSection())
+    return OS->SectionIndex >= SHN_LORESERVE ? SHN_XINDEX : OS->SectionIndex;
+  return SHN_ABS;
+}
+
 // Write the internal symbol table contents to the output symbol table.
 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
   // The first entry is a null entry as per the ELF spec.
@@ -1956,23 +1973,8 @@ template <class ELFT> void SymbolTableSection<ELFT>::w
     }
 
     ESym->st_name = Ent.StrTabOffset;
+    ESym->st_shndx = getSymSectionIndex(Ent.Sym);
 
-    // Set a section index.
-    BssSection *CommonSec = nullptr;
-    if (!Config->DefineCommon)
-      if (auto *D = dyn_cast<Defined>(Sym))
-        CommonSec = dyn_cast_or_null<BssSection>(D->Section);
-    if (CommonSec)
-      ESym->st_shndx = SHN_COMMON;
-    else if (Sym->NeedsPltAddr)
-      ESym->st_shndx = SHN_UNDEF;
-    else if (const OutputSection *OutSec = Sym->getOutputSection())
-      ESym->st_shndx = OutSec->SectionIndex;
-    else if (isa<Defined>(Sym))
-      ESym->st_shndx = SHN_ABS;
-    else
-      ESym->st_shndx = SHN_UNDEF;
-
     // Copy symbol size if it is a defined symbol. st_size is not significant
     // for undefined symbols, so whether copying it or not is up to us if that's
     // the case. We'll leave it as zero because by not setting a value, we can
@@ -1986,7 +1988,7 @@ template <class ELFT> void SymbolTableSection<ELFT>::w
     // st_value is usually an address of a symbol, but that has a
     // special meaining for uninstantiated common symbols (this can
     // occur if -r is given).
-    if (CommonSec)
+    if (BssSection *CommonSec = getCommonSec(Ent.Sym))
       ESym->st_value = CommonSec->Alignment;
     else
       ESym->st_value = Sym->getVA();
@@ -2026,6 +2028,44 @@ template <class ELFT> void SymbolTableSection<ELFT>::w
   }
 }
 
+SymtabShndxSection::SymtabShndxSection()
+    : SyntheticSection(0, SHT_SYMTAB_SHNDX, 4, ".symtab_shndxr") {
+  this->Entsize = 4;
+}
+
+void SymtabShndxSection::writeTo(uint8_t *Buf) {
+  // We write an array of 32 bit values, where each value has 1:1 association
+  // with an entry in .symtab. If the corresponding entry contains SHN_XINDEX,
+  // we need to write actual index, otherwise, we must write SHN_UNDEF(0).
+  Buf += 4; // Ignore .symtab[0] entry.
+  for (const SymbolTableEntry &Entry : InX::SymTab->getSymbols()) {
+    if (getSymSectionIndex(Entry.Sym) == SHN_XINDEX)
+      write32(Buf, Entry.Sym->getOutputSection()->SectionIndex);
+    Buf += 4;
+  }
+}
+
+bool SymtabShndxSection::empty() const {
+  // SHT_SYMTAB can hold symbols with section indices values up to
+  // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
+  // section. Problem is that we reveal the final section indices a bit too
+  // late, and we do not know them here. For simplicity, we just always create
+  // a .symtab_shndxr section when the amount of output sections is huge.
+  size_t Size = 0;
+  for (BaseCommand *Base : Script->SectionCommands)
+    if (isa<OutputSection>(Base))
+      ++Size;
+  return Size < SHN_LORESERVE;
+}
+
+void SymtabShndxSection::finalizeContents() {
+  getParent()->Link = InX::SymTab->getParent()->SectionIndex;
+}
+
+size_t SymtabShndxSection::getSize() const {
+  return InX::SymTab->getNumSymbols() * 4;
+}
+
 // .hash and .gnu.hash sections contain on-disk hash tables that map
 // symbol names to their dynamic symbol table indices. Their purpose
 // is to help the dynamic linker resolve symbols quickly. If ELF files
@@ -3025,6 +3065,7 @@ RelocationBaseSection *InX::RelaIplt;
 StringTableSection *InX::ShStrTab;
 StringTableSection *InX::StrTab;
 SymbolTableBaseSection *InX::SymTab;
+SymtabShndxSection *InX::SymTabShndx;
 
 template GdbIndexSection *GdbIndexSection::create<ELF32LE>();
 template GdbIndexSection *GdbIndexSection::create<ELF32BE>();

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.h
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.h	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/SyntheticSections.h	Thu Aug  2 18:01:17 2018	(r337151)
@@ -588,6 +588,16 @@ class SymbolTableSection final : public SymbolTableBas
   void writeTo(uint8_t *Buf) override;
 };
 
+class SymtabShndxSection final : public SyntheticSection {
+public:
+  SymtabShndxSection();
+
+  void writeTo(uint8_t *Buf) override;
+  size_t getSize() const override;
+  bool empty() const override;
+  void finalizeContents() override;
+};
+
 // Outputs GNU Hash section. For detailed explanation see:
 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
 class GnuHashTableSection final : public SyntheticSection {
@@ -992,6 +1002,7 @@ struct InX {
   static StringTableSection *ShStrTab;
   static StringTableSection *StrTab;
   static SymbolTableBaseSection *SymTab;
+  static SymtabShndxSection* SymTabShndx;
 };
 
 template <class ELFT> struct In {

Modified: projects/clang700-import/contrib/llvm/tools/lld/ELF/Writer.cpp
==============================================================================
--- projects/clang700-import/contrib/llvm/tools/lld/ELF/Writer.cpp	Thu Aug  2 17:59:51 2018	(r337150)
+++ projects/clang700-import/contrib/llvm/tools/lld/ELF/Writer.cpp	Thu Aug  2 18:01:17 2018	(r337151)
@@ -287,6 +287,7 @@ template <class ELFT> static void createSyntheticSecti
   if (Config->Strip != StripPolicy::All) {
     InX::StrTab = make<StringTableSection>(".strtab", false);
     InX::SymTab = make<SymbolTableSection<ELFT>>(*InX::StrTab);
+    InX::SymTabShndx = make<SymtabShndxSection>();
   }
 
   if (Config->BuildId != BuildIdKind::None) {
@@ -409,6 +410,8 @@ template <class ELFT> static void createSyntheticSecti
 
   if (InX::SymTab)
     Add(InX::SymTab);
+  if (InX::SymTabShndx)
+    Add(InX::SymTabShndx);
   Add(InX::ShStrTab);
   if (InX::StrTab)
     Add(InX::StrTab);
@@ -518,7 +521,6 @@ static bool shouldKeepInSymtab(SectionBase *Sec, Strin
   if (B.isSection())
     return false;
 
-
   if (Config->Discard == DiscardPolicy::None)
     return true;
 
@@ -1605,6 +1607,15 @@ template <class ELFT> void Writer<ELFT>::finalizeSecti
     if (auto *Sec = dyn_cast<OutputSection>(Base))
       OutputSections.push_back(Sec);
 
+  // Ensure data sections are not mixed with executable sections when
+  // -execute-only is used.
+  if (Config->ExecuteOnly)
+    for (OutputSection *OS : OutputSections)
+      if (OS->Flags & SHF_EXECINSTR)
+        for (InputSection *IS : getInputSections(OS))
+          if (!(IS->Flags & SHF_EXECINSTR))
+            error("-execute-only does not support intermingling data and code");
+
   // Prefer command line supplied address over other constraints.
   for (OutputSection *Sec : OutputSections) {
     auto I = Config->SectionStartMap.find(Sec->Name);
@@ -1639,12 +1650,13 @@ template <class ELFT> void Writer<ELFT>::finalizeSecti
   // Dynamic section must be the last one in this list and dynamic
   // symbol table section (DynSymTab) must be the first one.
   applySynthetic(
-      {InX::DynSymTab,   InX::Bss,         InX::BssRelRo,     InX::GnuHashTab,
-       InX::HashTab,     InX::SymTab,      InX::ShStrTab,     InX::StrTab,
-       In<ELFT>::VerDef, InX::DynStrTab,   InX::Got,          InX::MipsGot,
-       InX::IgotPlt,     InX::GotPlt,      InX::RelaDyn,      InX::RelrDyn,
-       InX::RelaIplt,    InX::RelaPlt,     InX::Plt,          InX::Iplt,
-       InX::EhFrameHdr,  In<ELFT>::VerSym, In<ELFT>::VerNeed, InX::Dynamic},
+      {InX::DynSymTab, InX::Bss,         InX::BssRelRo,    InX::GnuHashTab,
+       InX::HashTab,   InX::SymTab,      InX::SymTabShndx, InX::ShStrTab,
+       InX::StrTab,    In<ELFT>::VerDef, InX::DynStrTab,   InX::Got,
+       InX::MipsGot,   InX::IgotPlt,     InX::GotPlt,      InX::RelaDyn,
+       InX::RelrDyn,   InX::RelaIplt,    InX::RelaPlt,     InX::Plt,
+       InX::Iplt,      InX::EhFrameHdr,  In<ELFT>::VerSym, In<ELFT>::VerNeed,
+       InX::Dynamic},
       [](SyntheticSection *SS) { SS->finalizeContents(); });
 
   if (!Script->HasSectionsCommand && !Config->Relocatable)
@@ -1763,6 +1775,8 @@ static bool needsPtLoad(OutputSection *Sec) {
 static uint64_t computeFlags(uint64_t Flags) {
   if (Config->Omagic)
     return PF_R | PF_W | PF_X;
+  if (Config->ExecuteOnly && (Flags & PF_X))
+    return Flags & ~PF_R;
   if (Config->SingleRoRx && !(Flags & PF_W))
     return Flags | PF_X;
   return Flags;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808021801.w72I1HZY040588>