184487f11SMichael J. Spencer //===- InputFiles.cpp -----------------------------------------------------===//
284487f11SMichael J. Spencer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
684487f11SMichael J. Spencer //
784487f11SMichael J. Spencer //===----------------------------------------------------------------------===//
884487f11SMichael J. Spencer 
984487f11SMichael J. Spencer #include "InputFiles.h"
101a590232SFangrui Song #include "Config.h"
1127bb7990SFangrui Song #include "DWARF.h"
121d16515fSBen Dunbobbin #include "Driver.h"
139d13d041SRafael Espindola #include "InputSection.h"
1467e3ff83SGeorge Rimar #include "LinkerScript.h"
154f952706SPeter Collingbourne #include "SymbolTable.h"
1684487f11SMichael J. Spencer #include "Symbols.h"
17532bc984SPeter Smith #include "SyntheticSections.h"
1817a39aecSFangrui Song #include "Target.h"
1983d59e05SAlexandre Ganea #include "lld/Common/CommonLinkerContext.h"
20adfad4d7SReid Kleckner #include "lld/Common/DWARF.h"
2127bb7990SFangrui Song #include "llvm/ADT/CachedHashString.h"
2284487f11SMichael J. Spencer #include "llvm/ADT/STLExtras.h"
23786d8e33SDavide Italiano #include "llvm/LTO/LTO.h"
2427bb7990SFangrui Song #include "llvm/Object/IRObjectFile.h"
2557eb0469SPeter Smith #include "llvm/Support/ARMAttributeParser.h"
2657eb0469SPeter Smith #include "llvm/Support/ARMBuildAttributes.h"
272057f836SRui Ueyama #include "llvm/Support/Endian.h"
2827bb7990SFangrui Song #include "llvm/Support/FileSystem.h"
29e02ba98fSDavide Italiano #include "llvm/Support/Path.h"
30bef38e86SJessica Clarke #include "llvm/Support/RISCVAttributeParser.h"
31ec1c75e0SRui Ueyama #include "llvm/Support/TarWriter.h"
329f77ef0cSRafael Espindola #include "llvm/Support/raw_ostream.h"
3384487f11SMichael J. Spencer 
341b348a68SMichael J. Spencer using namespace llvm;
3584487f11SMichael J. Spencer using namespace llvm::ELF;
36f98d6d84SRafael Espindola using namespace llvm::object;
379a84f6b9SRafael Espindola using namespace llvm::sys;
38f5c4aca9SRui Ueyama using namespace llvm::sys::fs;
392057f836SRui Ueyama using namespace llvm::support::endian;
4007837b8fSFangrui Song using namespace lld;
4107837b8fSFangrui Song using namespace lld::elf;
4284487f11SMichael J. Spencer 
4307837b8fSFangrui Song bool InputFile::isInGroup;
4407837b8fSFangrui Song uint32_t InputFile::nextGroupId;
4507837b8fSFangrui Song 
4607837b8fSFangrui Song std::unique_ptr<TarWriter> elf::tar;
4707837b8fSFangrui Song 
48bd8cfe65SFangrui Song // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
toString(const InputFile * f)4907837b8fSFangrui Song std::string lld::toString(const InputFile *f) {
50bd8cfe65SFangrui Song   if (!f)
51bd8cfe65SFangrui Song     return "<internal>";
5284487f11SMichael J. Spencer 
53bd8cfe65SFangrui Song   if (f->toStringCache.empty()) {
54bd8cfe65SFangrui Song     if (f->archiveName.empty())
551ff1d50dSFangrui Song       f->toStringCache = f->getName();
56bd8cfe65SFangrui Song     else
571ff1d50dSFangrui Song       (f->archiveName + "(" + f->getName() + ")").toVector(f->toStringCache);
58bd8cfe65SFangrui Song   }
591ff1d50dSFangrui Song   return std::string(f->toStringCache);
60bd8cfe65SFangrui Song }
61bd8cfe65SFangrui Song 
getELFKind(MemoryBufferRef mb,StringRef archiveName)623837f427SRui Ueyama static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) {
633837f427SRui Ueyama   unsigned char size;
643837f427SRui Ueyama   unsigned char endian;
653837f427SRui Ueyama   std::tie(size, endian) = getElfArchType(mb.getBuffer());
6676737f4dSRui Ueyama 
673837f427SRui Ueyama   auto report = [&](StringRef msg) {
683837f427SRui Ueyama     StringRef filename = mb.getBufferIdentifier();
693837f427SRui Ueyama     if (archiveName.empty())
703837f427SRui Ueyama       fatal(filename + ": " + msg);
7176737f4dSRui Ueyama     else
723837f427SRui Ueyama       fatal(archiveName + "(" + filename + "): " + msg);
7376737f4dSRui Ueyama   };
7476737f4dSRui Ueyama 
753837f427SRui Ueyama   if (!mb.getBuffer().startswith(ElfMagic))
763837f427SRui Ueyama     report("not an ELF file");
773837f427SRui Ueyama   if (endian != ELFDATA2LSB && endian != ELFDATA2MSB)
783837f427SRui Ueyama     report("corrupted ELF file: invalid data encoding");
793837f427SRui Ueyama   if (size != ELFCLASS32 && size != ELFCLASS64)
803837f427SRui Ueyama     report("corrupted ELF file: invalid file class");
8176737f4dSRui Ueyama 
823837f427SRui Ueyama   size_t bufSize = mb.getBuffer().size();
833837f427SRui Ueyama   if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) ||
843837f427SRui Ueyama       (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr)))
853837f427SRui Ueyama     report("corrupted ELF file: file is too short");
8676737f4dSRui Ueyama 
873837f427SRui Ueyama   if (size == ELFCLASS32)
883837f427SRui Ueyama     return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
893837f427SRui Ueyama   return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
9076737f4dSRui Ueyama }
9176737f4dSRui Ueyama 
InputFile(Kind k,MemoryBufferRef m)923837f427SRui Ueyama InputFile::InputFile(Kind k, MemoryBufferRef m)
933837f427SRui Ueyama     : mb(m), groupId(nextGroupId), fileKind(k) {
941d92aa73SRui Ueyama   // All files within the same --{start,end}-group get the same group ID.
951d92aa73SRui Ueyama   // Otherwise, a new file will get a new group ID.
963837f427SRui Ueyama   if (!isInGroup)
973837f427SRui Ueyama     ++nextGroupId;
981d92aa73SRui Ueyama }
9937e60a52SRui Ueyama 
readFile(StringRef path)10007837b8fSFangrui Song Optional<MemoryBufferRef> elf::readFile(StringRef path) {
101439341b9SJames Henderson   llvm::TimeTraceScope timeScope("Load input files", path);
102439341b9SJames Henderson 
103875ae82bSRui Ueyama   // The --chroot option changes our virtual root directory.
104875ae82bSRui Ueyama   // This is useful when you are dealing with files created by --reproduce.
1053837f427SRui Ueyama   if (!config->chroot.empty() && path.startswith("/"))
10683d59e05SAlexandre Ganea     path = saver().save(config->chroot + path);
107875ae82bSRui Ueyama 
1083837f427SRui Ueyama   log(path);
10981eeabbdSPetr Hosek   config->dependencyFiles.insert(llvm::CachedHashString(path));
110875ae82bSRui Ueyama 
111c83cd8feSAbhina Sreeskantharajan   auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false,
112c83cd8feSAbhina Sreeskantharajan                                        /*RequiresNullTerminator=*/false);
1133837f427SRui Ueyama   if (auto ec = mbOrErr.getError()) {
1143837f427SRui Ueyama     error("cannot open " + path + ": " + ec.message());
115ec1c75e0SRui Ueyama     return None;
116ec1c75e0SRui Ueyama   }
117e6e206d4SRui Ueyama 
118a96fe1bfSFangrui Song   MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef();
1199a572164SFangrui Song   ctx->memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership
120ec1c75e0SRui Ueyama 
1213837f427SRui Ueyama   if (tar)
1223837f427SRui Ueyama     tar->append(relativeToRoot(path), mbref.getBuffer());
1233837f427SRui Ueyama   return mbref;
124ec1c75e0SRui Ueyama }
125ec1c75e0SRui Ueyama 
1262dd5283dSRui Ueyama // All input object files must be for the same architecture
1272dd5283dSRui Ueyama // (e.g. it does not make sense to link x86 object files with
1282dd5283dSRui Ueyama // MIPS object files.) This function checks for that error.
isCompatible(InputFile * file)1293837f427SRui Ueyama static bool isCompatible(InputFile *file) {
1303837f427SRui Ueyama   if (!file->isElf() && !isa<BitcodeFile>(file))
1312dd5283dSRui Ueyama     return true;
1322dd5283dSRui Ueyama 
1333837f427SRui Ueyama   if (file->ekind == config->ekind && file->emachine == config->emachine) {
1343837f427SRui Ueyama     if (config->emachine != EM_MIPS)
1352dd5283dSRui Ueyama       return true;
1363837f427SRui Ueyama     if (isMipsN32Abi(file) == config->mipsN32Abi)
1372dd5283dSRui Ueyama       return true;
1382dd5283dSRui Ueyama   }
1392dd5283dSRui Ueyama 
1402822852fSShoaib Meenai   StringRef target =
1412822852fSShoaib Meenai       !config->bfdname.empty() ? config->bfdname : config->emulation;
1422822852fSShoaib Meenai   if (!target.empty()) {
1432822852fSShoaib Meenai     error(toString(file) + " is incompatible with " + target);
144d524c170SRui Ueyama     return false;
145d524c170SRui Ueyama   }
146d524c170SRui Ueyama 
147bad1b7fbSFangrui Song   InputFile *existing = nullptr;
1489a572164SFangrui Song   if (!ctx->objectFiles.empty())
1499a572164SFangrui Song     existing = ctx->objectFiles[0];
1509a572164SFangrui Song   else if (!ctx->sharedFiles.empty())
1519a572164SFangrui Song     existing = ctx->sharedFiles[0];
1529a572164SFangrui Song   else if (!ctx->bitcodeFiles.empty())
1539a572164SFangrui Song     existing = ctx->bitcodeFiles[0];
154bad1b7fbSFangrui Song   std::string with;
155bad1b7fbSFangrui Song   if (existing)
156bad1b7fbSFangrui Song     with = " with " + toString(existing);
157bad1b7fbSFangrui Song   error(toString(file) + " is incompatible" + with);
1582dd5283dSRui Ueyama   return false;
1592dd5283dSRui Ueyama }
1602dd5283dSRui Ueyama 
doParseFile(InputFile * file)1613837f427SRui Ueyama template <class ELFT> static void doParseFile(InputFile *file) {
1623837f427SRui Ueyama   if (!isCompatible(file))
1632dd5283dSRui Ueyama     return;
1642dd5283dSRui Ueyama 
1652dd5283dSRui Ueyama   // Binary file
1663837f427SRui Ueyama   if (auto *f = dyn_cast<BinaryFile>(file)) {
1679a572164SFangrui Song     ctx->binaryFiles.push_back(f);
1683837f427SRui Ueyama     f->parse();
1692dd5283dSRui Ueyama     return;
1702dd5283dSRui Ueyama   }
1712dd5283dSRui Ueyama 
1722dd5283dSRui Ueyama   // Lazy object file
1733a5fb573SFangrui Song   if (file->lazy) {
1743a5fb573SFangrui Song     if (auto *f = dyn_cast<BitcodeFile>(file)) {
1759a572164SFangrui Song       ctx->lazyBitcodeFiles.push_back(f);
1763a5fb573SFangrui Song       f->parseLazy();
1773a5fb573SFangrui Song     } else {
1783a5fb573SFangrui Song       cast<ObjFile<ELFT>>(file)->parseLazy();
1793a5fb573SFangrui Song     }
1802dd5283dSRui Ueyama     return;
1812dd5283dSRui Ueyama   }
1822dd5283dSRui Ueyama 
1833837f427SRui Ueyama   if (config->trace)
1843837f427SRui Ueyama     message(toString(file));
1852dd5283dSRui Ueyama 
1862dd5283dSRui Ueyama   // .so file
1873837f427SRui Ueyama   if (auto *f = dyn_cast<SharedFile>(file)) {
1883837f427SRui Ueyama     f->parse<ELFT>();
1892dd5283dSRui Ueyama     return;
1902dd5283dSRui Ueyama   }
1912dd5283dSRui Ueyama 
1922dd5283dSRui Ueyama   // LLVM bitcode file
1933837f427SRui Ueyama   if (auto *f = dyn_cast<BitcodeFile>(file)) {
1949a572164SFangrui Song     ctx->bitcodeFiles.push_back(f);
1953837f427SRui Ueyama     f->parse<ELFT>();
1962dd5283dSRui Ueyama     return;
1972dd5283dSRui Ueyama   }
1982dd5283dSRui Ueyama 
1992dd5283dSRui Ueyama   // Regular object file
2009a572164SFangrui Song   ctx->objectFiles.push_back(cast<ELFFileBase>(file));
2013837f427SRui Ueyama   cast<ObjFile<ELFT>>(file)->parse();
2022dd5283dSRui Ueyama }
2032dd5283dSRui Ueyama 
204943cd005SRui Ueyama // Add symbols in File to the symbol table.
parseFile(InputFile * file)20517a39aecSFangrui Song void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); }
206943cd005SRui Ueyama 
2079a84f6b9SRafael Espindola // Concatenates arguments to construct a string representing an error location.
createFileLineMsg(StringRef path,unsigned line)2083837f427SRui Ueyama static std::string createFileLineMsg(StringRef path, unsigned line) {
209adcd0268SBenjamin Kramer   std::string filename = std::string(path::filename(path));
2103837f427SRui Ueyama   std::string lineno = ":" + std::to_string(line);
2113837f427SRui Ueyama   if (filename == path)
2123837f427SRui Ueyama     return filename + lineno;
2133837f427SRui Ueyama   return filename + lineno + " (" + path.str() + lineno + ")";
2149a84f6b9SRafael Espindola }
2159a84f6b9SRafael Espindola 
2169a84f6b9SRafael Espindola template <class ELFT>
getSrcMsgAux(ObjFile<ELFT> & file,const Symbol & sym,InputSectionBase & sec,uint64_t offset)2173837f427SRui Ueyama static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym,
2183837f427SRui Ueyama                                 InputSectionBase &sec, uint64_t offset) {
2199a84f6b9SRafael Espindola   // In DWARF, functions and variables are stored to different places.
2209a84f6b9SRafael Espindola   // First, look up a function for a given offset.
2213837f427SRui Ueyama   if (Optional<DILineInfo> info = file.getDILineInfo(&sec, offset))
2223837f427SRui Ueyama     return createFileLineMsg(info->FileName, info->Line);
2239a84f6b9SRafael Espindola 
2249a84f6b9SRafael Espindola   // If it failed, look up again as a variable.
2253837f427SRui Ueyama   if (Optional<std::pair<std::string, unsigned>> fileLine =
2263837f427SRui Ueyama           file.getVariableLoc(sym.getName()))
2273837f427SRui Ueyama     return createFileLineMsg(fileLine->first, fileLine->second);
2289a84f6b9SRafael Espindola 
22947cfe8f3SFangrui Song   // File.sourceFile contains STT_FILE symbol, and that is a last resort.
230adcd0268SBenjamin Kramer   return std::string(file.sourceFile);
2319a84f6b9SRafael Espindola }
2329a84f6b9SRafael Espindola 
getSrcMsg(const Symbol & sym,InputSectionBase & sec,uint64_t offset)2333837f427SRui Ueyama std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec,
2343837f427SRui Ueyama                                  uint64_t offset) {
2359a84f6b9SRafael Espindola   if (kind() != ObjKind)
2369a84f6b9SRafael Espindola     return "";
2373837f427SRui Ueyama   switch (config->ekind) {
2389a84f6b9SRafael Espindola   default:
2399a84f6b9SRafael Espindola     llvm_unreachable("Invalid kind");
2409a84f6b9SRafael Espindola   case ELF32LEKind:
2413837f427SRui Ueyama     return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset);
2429a84f6b9SRafael Espindola   case ELF32BEKind:
2433837f427SRui Ueyama     return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset);
2449a84f6b9SRafael Espindola   case ELF64LEKind:
2453837f427SRui Ueyama     return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset);
2469a84f6b9SRafael Espindola   case ELF64BEKind:
2473837f427SRui Ueyama     return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset);
2489a84f6b9SRafael Espindola   }
2499a84f6b9SRafael Espindola }
2509a84f6b9SRafael Espindola 
getNameForScript() const25177152a6bSAndrew Ng StringRef InputFile::getNameForScript() const {
25277152a6bSAndrew Ng   if (archiveName.empty())
25377152a6bSAndrew Ng     return getName();
25477152a6bSAndrew Ng 
25577152a6bSAndrew Ng   if (nameForScriptCache.empty())
25677152a6bSAndrew Ng     nameForScriptCache = (archiveName + Twine(':') + getName()).str();
25777152a6bSAndrew Ng 
25877152a6bSAndrew Ng   return nameForScriptCache;
25977152a6bSAndrew Ng }
26077152a6bSAndrew Ng 
getDwarf()261dcf6494aSAlexey Lapshin template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() {
262dcf6494aSAlexey Lapshin   llvm::call_once(initDwarf, [this]() {
263dcf6494aSAlexey Lapshin     dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
264dcf6494aSAlexey Lapshin         std::make_unique<LLDDwarfObj<ELFT>>(this), "",
265dcf6494aSAlexey Lapshin         [&](Error err) { warn(getName() + ": " + toString(std::move(err))); },
266dcf6494aSAlexey Lapshin         [&](Error warning) {
267dcf6494aSAlexey Lapshin           warn(getName() + ": " + toString(std::move(warning)));
268dcf6494aSAlexey Lapshin         }));
269dcf6494aSAlexey Lapshin   });
270dcf6494aSAlexey Lapshin 
271dcf6494aSAlexey Lapshin   return dwarf.get();
27282f0c42dSGeorge Rimar }
27382f0c42dSGeorge Rimar 
27482f0c42dSGeorge Rimar // Returns the pair of file name and line number describing location of data
27582f0c42dSGeorge Rimar // object (variable, array, etc) definition.
27682f0c42dSGeorge Rimar template <class ELFT>
27782f0c42dSGeorge Rimar Optional<std::pair<std::string, unsigned>>
getVariableLoc(StringRef name)2783837f427SRui Ueyama ObjFile<ELFT>::getVariableLoc(StringRef name) {
279dcf6494aSAlexey Lapshin   return getDwarf()->getVariableLoc(name);
280b380b24eSEugene Leviant }
281b380b24eSEugene Leviant 
2827556f6b2SRui Ueyama // Returns source line information for a given offset
2837556f6b2SRui Ueyama // using DWARF debug info.
284c4681203SEugene Leviant template <class ELFT>
getDILineInfo(InputSectionBase * s,uint64_t offset)2853837f427SRui Ueyama Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s,
2863837f427SRui Ueyama                                                   uint64_t offset) {
28777fc1f60SAlexey Lapshin   // Detect SectionIndex for specified section.
2883837f427SRui Ueyama   uint64_t sectionIndex = object::SectionedAddress::UndefSection;
2893837f427SRui Ueyama   ArrayRef<InputSectionBase *> sections = s->file->getSections();
2903837f427SRui Ueyama   for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) {
2913837f427SRui Ueyama     if (s == sections[curIndex]) {
2923837f427SRui Ueyama       sectionIndex = curIndex;
29377fc1f60SAlexey Lapshin       break;
29477fc1f60SAlexey Lapshin     }
29577fc1f60SAlexey Lapshin   }
29677fc1f60SAlexey Lapshin 
297dcf6494aSAlexey Lapshin   return getDwarf()->getDILineInfo(offset, sectionIndex);
298b8760203SRui Ueyama }
299b8760203SRui Ueyama 
ELFFileBase(Kind k,MemoryBufferRef mb)3003837f427SRui Ueyama ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) {
3013837f427SRui Ueyama   ekind = getELFKind(mb, "");
302883ab235SPeter Collingbourne 
3033837f427SRui Ueyama   switch (ekind) {
30492069605SRui Ueyama   case ELF32LEKind:
30592069605SRui Ueyama     init<ELF32LE>();
30692069605SRui Ueyama     break;
30792069605SRui Ueyama   case ELF32BEKind:
30892069605SRui Ueyama     init<ELF32BE>();
30992069605SRui Ueyama     break;
31092069605SRui Ueyama   case ELF64LEKind:
31192069605SRui Ueyama     init<ELF64LE>();
31292069605SRui Ueyama     break;
31392069605SRui Ueyama   case ELF64BEKind:
31492069605SRui Ueyama     init<ELF64BE>();
31592069605SRui Ueyama     break;
31692069605SRui Ueyama   default:
31792069605SRui Ueyama     llvm_unreachable("getELFKind");
31892069605SRui Ueyama   }
3195e64d3fbSRui Ueyama }
3205e64d3fbSRui Ueyama 
32192069605SRui Ueyama template <typename Elf_Shdr>
findSection(ArrayRef<Elf_Shdr> sections,uint32_t type)3223837f427SRui Ueyama static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {
3233837f427SRui Ueyama   for (const Elf_Shdr &sec : sections)
3243837f427SRui Ueyama     if (sec.sh_type == type)
3253837f427SRui Ueyama       return &sec;
32692069605SRui Ueyama   return nullptr;
32792069605SRui Ueyama }
32892069605SRui Ueyama 
init()32992069605SRui Ueyama template <class ELFT> void ELFFileBase::init() {
33092069605SRui Ueyama   using Elf_Shdr = typename ELFT::Shdr;
33192069605SRui Ueyama   using Elf_Sym = typename ELFT::Sym;
33292069605SRui Ueyama 
33392069605SRui Ueyama   // Initialize trivial attributes.
3343837f427SRui Ueyama   const ELFFile<ELFT> &obj = getObj<ELFT>();
3354845531fSGeorgii Rymar   emachine = obj.getHeader().e_machine;
3364845531fSGeorgii Rymar   osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI];
3374845531fSGeorgii Rymar   abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION];
33892069605SRui Ueyama 
3393837f427SRui Ueyama   ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this);
340b5a0f0f3SFangrui Song   elfShdrs = sections.data();
341b5a0f0f3SFangrui Song   numELFShdrs = sections.size();
34292069605SRui Ueyama 
34392069605SRui Ueyama   // Find a symbol table.
3443837f427SRui Ueyama   bool isDSO =
3453837f427SRui Ueyama       (identify_magic(mb.getBuffer()) == file_magic::elf_shared_object);
3463837f427SRui Ueyama   const Elf_Shdr *symtabSec =
3473837f427SRui Ueyama       findSection(sections, isDSO ? SHT_DYNSYM : SHT_SYMTAB);
34892069605SRui Ueyama 
3493837f427SRui Ueyama   if (!symtabSec)
35092069605SRui Ueyama     return;
35192069605SRui Ueyama 
35292069605SRui Ueyama   // Initialize members corresponding to a symbol table.
3533837f427SRui Ueyama   firstGlobal = symtabSec->sh_info;
35492069605SRui Ueyama 
3553837f427SRui Ueyama   ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this);
3563837f427SRui Ueyama   if (firstGlobal == 0 || firstGlobal > eSyms.size())
3573fc0f7e5SRui Ueyama     fatal(toString(this) + ": invalid sh_info in symbol table");
3586dcf4c68SRafael Espindola 
3593837f427SRui Ueyama   elfSyms = reinterpret_cast<const void *>(eSyms.data());
3601ff1d50dSFangrui Song   numELFSyms = uint32_t(eSyms.size());
3613837f427SRui Ueyama   stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this);
362330e52b0SRui Ueyama }
363e1901cc3SRafael Espindola 
36482386042SPeter Collingbourne template <class ELFT>
getSectionIndex(const Elf_Sym & sym) const3653837f427SRui Ueyama uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {
366883ab235SPeter Collingbourne   return CHECK(
3674845531fSGeorgii Rymar       this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable),
36882386042SPeter Collingbourne       this);
36982386042SPeter Collingbourne }
37082386042SPeter Collingbourne 
parse(bool ignoreComdats)3713837f427SRui Ueyama template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
372ee7720acSFangrui Song   object::ELFFile<ELFT> obj = this->getObj();
37347cfe8f3SFangrui Song   // Read a section table. justSymbols is usually false.
3743837f427SRui Ueyama   if (this->justSymbols)
3755a67a6ecSRui Ueyama     initializeJustSymbols();
3765a67a6ecSRui Ueyama   else
377ee7720acSFangrui Song     initializeSections(ignoreComdats, obj);
3785a67a6ecSRui Ueyama 
3795a67a6ecSRui Ueyama   // Read a symbol table.
380ee7720acSFangrui Song   initializeSymbols(obj);
38184487f11SMichael J. Spencer }
38284487f11SMichael J. Spencer 
3833f11c8c9SRui Ueyama // Sections with SHT_GROUP and comdat bits define comdat section groups.
3843f11c8c9SRui Ueyama // They are identified and deduplicated by group name. This function
3853f11c8c9SRui Ueyama // returns a group name.
386444576d4SRafael Espindola template <class ELFT>
getShtGroupSignature(ArrayRef<Elf_Shdr> sections,const Elf_Shdr & sec)3873837f427SRui Ueyama StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
3883837f427SRui Ueyama                                               const Elf_Shdr &sec) {
3898d9b9f6bSGeorge Rimar   typename ELFT::SymRange symbols = this->getELFSyms<ELFT>();
3908d9b9f6bSGeorge Rimar   if (sec.sh_info >= symbols.size())
3918d9b9f6bSGeorge Rimar     fatal(toString(this) + ": invalid symbol index");
3928d9b9f6bSGeorge Rimar   const typename ELFT::Sym &sym = symbols[sec.sh_info];
39325da8700SFangrui Song   return CHECK(sym.getName(this->stringTable), this);
394444576d4SRafael Espindola }
395444576d4SRafael Espindola 
396d7ead5b5SRui Ueyama template <class ELFT>
shouldMerge(const Elf_Shdr & sec,StringRef name)397d7ead5b5SRui Ueyama bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
39835aad41cSRafael Espindola   // On a regular link we don't merge sections if -O0 (default is -O1). This
39935aad41cSRafael Espindola   // sometimes makes the linker significantly faster, although the output will
40035aad41cSRafael Espindola   // be bigger.
40135aad41cSRafael Espindola   //
40235aad41cSRafael Espindola   // Doing the same for -r would create a problem as it would combine sections
40335aad41cSRafael Espindola   // with different sh_entsize. One option would be to just copy every SHF_MERGE
40435aad41cSRafael Espindola   // section as is to the output. While this would produce a valid ELF file with
40535aad41cSRafael Espindola   // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
40635aad41cSRafael Espindola   // they see two .debug_str. We could have separate logic for combining
40735aad41cSRafael Espindola   // SHF_MERGE sections based both on their name and sh_entsize, but that seems
40835aad41cSRafael Espindola   // to be more trouble than it is worth. Instead, we just use the regular (-O1)
40935aad41cSRafael Espindola   // logic for -r.
4103837f427SRui Ueyama   if (config->optimize == 0 && !config->relocatable)
411fb6d499eSRui Ueyama     return false;
412fb6d499eSRui Ueyama 
4133ebc71ebSRui Ueyama   // A mergeable section with size 0 is useless because they don't have
4143ebc71ebSRui Ueyama   // any data to merge. A mergeable string section with size 0 can be
4153ebc71ebSRui Ueyama   // argued as invalid because it doesn't end with a null character.
4163ebc71ebSRui Ueyama   // We'll avoid a mess by handling them as if they were non-mergeable.
4173837f427SRui Ueyama   if (sec.sh_size == 0)
4183ebc71ebSRui Ueyama     return false;
4193ebc71ebSRui Ueyama 
420c75ef85eSRui Ueyama   // Check for sh_entsize. The ELF spec is not clear about the zero
421c75ef85eSRui Ueyama   // sh_entsize. It says that "the member [sh_entsize] contains 0 if
422c75ef85eSRui Ueyama   // the section does not hold a table of fixed-size entries". We know
423c75ef85eSRui Ueyama   // that Rust 1.13 produces a string mergeable section with a zero
424c75ef85eSRui Ueyama   // sh_entsize. Here we just accept it rather than being picky about it.
4253837f427SRui Ueyama   uint64_t entSize = sec.sh_entsize;
4263837f427SRui Ueyama   if (entSize == 0)
427c75ef85eSRui Ueyama     return false;
4283837f427SRui Ueyama   if (sec.sh_size % entSize)
429d7ead5b5SRui Ueyama     fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" +
430d7ead5b5SRui Ueyama           Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" +
431d7ead5b5SRui Ueyama           Twine(entSize) + ")");
432c75ef85eSRui Ueyama 
43356decd98SFangrui Song   if (sec.sh_flags & SHF_WRITE)
434d7ead5b5SRui Ueyama     fatal(toString(this) + ":(" + name +
435d7ead5b5SRui Ueyama           "): writable SHF_MERGE section is not supported");
436f82ed2a2SRafael Espindola 
4377efa5be2SRafael Espindola   return true;
438f82ed2a2SRafael Espindola }
439f82ed2a2SRafael Espindola 
4405a67a6ecSRui Ueyama // This is for --just-symbols.
4415a67a6ecSRui Ueyama //
4425a67a6ecSRui Ueyama // --just-symbols is a very minor feature that allows you to link your
4435a67a6ecSRui Ueyama // output against other existing program, so that if you load both your
4445a67a6ecSRui Ueyama // program and the other program into memory, your output can refer the
4455a67a6ecSRui Ueyama // other program's symbols.
4465a67a6ecSRui Ueyama //
4475a67a6ecSRui Ueyama // When the option is given, we link "just symbols". The section table is
4485a67a6ecSRui Ueyama // initialized with null pointers.
initializeJustSymbols()4495a67a6ecSRui Ueyama template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
450b5a0f0f3SFangrui Song   sections.resize(numELFShdrs);
4515a67a6ecSRui Ueyama }
4525a67a6ecSRui Ueyama 
4531d16515fSBen Dunbobbin // An ELF object file may contain a `.deplibs` section. If it exists, the
4541d16515fSBen Dunbobbin // section contains a list of library specifiers such as `m` for libm. This
4551d16515fSBen Dunbobbin // function resolves a given name by finding the first matching library checking
4561d16515fSBen Dunbobbin // the various ways that a library can be specified to LLD. This ELF extension
4571d16515fSBen Dunbobbin // is a form of autolinking and is called `dependent libraries`. It is currently
4581d16515fSBen Dunbobbin // unique to LLVM and lld.
addDependentLibrary(StringRef specifier,const InputFile * f)4593837f427SRui Ueyama static void addDependentLibrary(StringRef specifier, const InputFile *f) {
4603837f427SRui Ueyama   if (!config->dependentLibraries)
4611d16515fSBen Dunbobbin     return;
46271dcd9bdSPetr Hosek   if (Optional<std::string> s = searchLibraryBaseName(specifier))
463b3270888SFangrui Song     driver->addFile(saver().save(*s), /*withLOption=*/true);
4643837f427SRui Ueyama   else if (Optional<std::string> s = findFromSearchPaths(specifier))
465b3270888SFangrui Song     driver->addFile(saver().save(*s), /*withLOption=*/true);
46671dcd9bdSPetr Hosek   else if (fs::exists(specifier))
46771dcd9bdSPetr Hosek     driver->addFile(specifier, /*withLOption=*/false);
4681d16515fSBen Dunbobbin   else
4693837f427SRui Ueyama     error(toString(f) +
4701d16515fSBen Dunbobbin           ": unable to find library from dependent library specifier: " +
4713837f427SRui Ueyama           specifier);
4721d16515fSBen Dunbobbin }
4731d16515fSBen Dunbobbin 
474ba8149e2SFangrui Song // Record the membership of a section group so that in the garbage collection
475ba8149e2SFangrui Song // pass, section group members are kept or discarded as a unit.
476f82ed2a2SRafael Espindola template <class ELFT>
handleSectionGroup(ArrayRef<InputSectionBase * > sections,ArrayRef<typename ELFT::Word> entries)47760ce444eSFangrui Song static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
47860ce444eSFangrui Song                                ArrayRef<typename ELFT::Word> entries) {
47960ce444eSFangrui Song   bool hasAlloc = false;
48060ce444eSFangrui Song   for (uint32_t index : entries.slice(1)) {
48160ce444eSFangrui Song     if (index >= sections.size())
48260ce444eSFangrui Song       return;
48360ce444eSFangrui Song     if (InputSectionBase *s = sections[index])
48460ce444eSFangrui Song       if (s != &InputSection::discarded && s->flags & SHF_ALLOC)
48560ce444eSFangrui Song         hasAlloc = true;
48660ce444eSFangrui Song   }
48760ce444eSFangrui Song 
48860ce444eSFangrui Song   // If any member has the SHF_ALLOC flag, the whole group is subject to garbage
48960ce444eSFangrui Song   // collection. See the comment in markLive(). This rule retains .debug_types
49060ce444eSFangrui Song   // and .rela.debug_types.
49160ce444eSFangrui Song   if (!hasAlloc)
49260ce444eSFangrui Song     return;
49360ce444eSFangrui Song 
49460ce444eSFangrui Song   // Connect the members in a circular doubly-linked list via
49560ce444eSFangrui Song   // nextInSectionGroup.
49660ce444eSFangrui Song   InputSectionBase *head;
49760ce444eSFangrui Song   InputSectionBase *prev = nullptr;
49860ce444eSFangrui Song   for (uint32_t index : entries.slice(1)) {
49960ce444eSFangrui Song     InputSectionBase *s = sections[index];
50060ce444eSFangrui Song     if (!s || s == &InputSection::discarded)
50160ce444eSFangrui Song       continue;
50260ce444eSFangrui Song     if (prev)
50360ce444eSFangrui Song       prev->nextInSectionGroup = s;
50460ce444eSFangrui Song     else
50560ce444eSFangrui Song       head = s;
50660ce444eSFangrui Song     prev = s;
50760ce444eSFangrui Song   }
50860ce444eSFangrui Song   if (prev)
50960ce444eSFangrui Song     prev->nextInSectionGroup = head;
51060ce444eSFangrui Song }
51160ce444eSFangrui Song 
51260ce444eSFangrui Song template <class ELFT>
initializeSections(bool ignoreComdats,const llvm::object::ELFFile<ELFT> & obj)513ee7720acSFangrui Song void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
514ee7720acSFangrui Song                                        const llvm::object::ELFFile<ELFT> &obj) {
515b5a0f0f3SFangrui Song   ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();
51625da8700SFangrui Song   StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this);
5173837f427SRui Ueyama   uint64_t size = objSections.size();
5183837f427SRui Ueyama   this->sections.resize(size);
519240b9515SRui Ueyama 
5206b0eb5a6SFangrui Song   std::vector<ArrayRef<Elf_Word>> selectedGroups;
5216b0eb5a6SFangrui Song 
52284944b63SFangrui Song   for (size_t i = 0; i != size; ++i) {
5233837f427SRui Ueyama     if (this->sections[i] == &InputSection::discarded)
524444576d4SRafael Espindola       continue;
5253837f427SRui Ueyama     const Elf_Shdr &sec = objSections[i];
526444576d4SRafael Espindola 
527af9793d1SRui Ueyama     // SHF_EXCLUDE'ed sections are discarded by the linker. However,
528af9793d1SRui Ueyama     // if -r is given, we'll let the final link discard such sections.
529af9793d1SRui Ueyama     // This is compatible with GNU.
5303837f427SRui Ueyama     if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) {
5311eaa9b43SFangrui Song       if (sec.sh_type == SHT_LLVM_CALL_GRAPH_PROFILE)
5321eaa9b43SFangrui Song         cgProfileSectionIndex = i;
5333837f427SRui Ueyama       if (sec.sh_type == SHT_LLVM_ADDRSIG) {
534a327a4c3SPeter Collingbourne         // We ignore the address-significance table if we know that the object
535a327a4c3SPeter Collingbourne         // file was created by objcopy or ld -r. This is because these tools
536a327a4c3SPeter Collingbourne         // will reorder the symbols in the symbol table, invalidating the data
537a327a4c3SPeter Collingbourne         // in the address-significance table, which refers to symbols by index.
5383837f427SRui Ueyama         if (sec.sh_link != 0)
5393837f427SRui Ueyama           this->addrsigSec = &sec;
5403837f427SRui Ueyama         else if (config->icf == ICFLevel::Safe)
5415f4d7b2fSFangrui Song           warn(toString(this) +
5425f4d7b2fSFangrui Song                ": --icf=safe conservatively ignores "
5435f4d7b2fSFangrui Song                "SHT_LLVM_ADDRSIG [index " +
5445f4d7b2fSFangrui Song                Twine(i) +
5455f4d7b2fSFangrui Song                "] with sh_link=0 "
5465f4d7b2fSFangrui Song                "(likely created using objcopy or ld -r)");
547a327a4c3SPeter Collingbourne       }
5483837f427SRui Ueyama       this->sections[i] = &InputSection::discarded;
54927be542cSEugene Leviant       continue;
55027be542cSEugene Leviant     }
55127be542cSEugene Leviant 
5523837f427SRui Ueyama     switch (sec.sh_type) {
5533b189d16SGeorge Rimar     case SHT_GROUP: {
554a1ba859aSRui Ueyama       // De-duplicate section groups by their signatures.
5553837f427SRui Ueyama       StringRef signature = getShtGroupSignature(objSections, sec);
5563837f427SRui Ueyama       this->sections[i] = &InputSection::discarded;
5573b189d16SGeorge Rimar 
5583837f427SRui Ueyama       ArrayRef<Elf_Word> entries =
5594845531fSGeorgii Rymar           CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);
5603837f427SRui Ueyama       if (entries.empty())
5611fa239f5SSerge Guelton         fatal(toString(this) + ": empty SHT_GROUP");
5621fa239f5SSerge Guelton 
563bfa4235eSPetr Hosek       Elf_Word flag = entries[0];
564bfa4235eSPetr Hosek       if (flag && flag != GRP_COMDAT)
5651fa239f5SSerge Guelton         fatal(toString(this) + ": unsupported SHT_GROUP format");
5661fa239f5SSerge Guelton 
567bfa4235eSPetr Hosek       bool keepGroup =
568bfa4235eSPetr Hosek           (flag & GRP_COMDAT) == 0 || ignoreComdats ||
5693837f427SRui Ueyama           symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this)
570579c8df7SSam Clegg               .second;
571bfa4235eSPetr Hosek       if (keepGroup) {
5723837f427SRui Ueyama         if (config->relocatable)
573ee7720acSFangrui Song           this->sections[i] = createInputSection(
574d86435c2SFangrui Song               i, sec, check(obj.getSectionName(sec, shstrtab)));
5756b0eb5a6SFangrui Song         selectedGroups.push_back(entries);
576a1ba859aSRui Ueyama         continue;
577a1ba859aSRui Ueyama       }
578a1ba859aSRui Ueyama 
579a1ba859aSRui Ueyama       // Otherwise, discard group members.
5803837f427SRui Ueyama       for (uint32_t secIndex : entries.slice(1)) {
5813837f427SRui Ueyama         if (secIndex >= size)
5823f7c3df6SGeorge Rimar           fatal(toString(this) +
5833837f427SRui Ueyama                 ": invalid section index in group: " + Twine(secIndex));
5843837f427SRui Ueyama         this->sections[secIndex] = &InputSection::discarded;
585444576d4SRafael Espindola       }
586444576d4SRafael Espindola       break;
5873b189d16SGeorge Rimar     }
5881130935cSRafael Espindola     case SHT_SYMTAB_SHNDX:
5893837f427SRui Ueyama       shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this);
59020348229SRafael Espindola       break;
59192069605SRui Ueyama     case SHT_SYMTAB:
592cde25137SRafael Espindola     case SHT_STRTAB:
593af16a456SGeorgii Rymar     case SHT_REL:
594af16a456SGeorgii Rymar     case SHT_RELA:
595cde25137SRafael Espindola     case SHT_NULL:
596cde25137SRafael Espindola       break;
5971a590232SFangrui Song     case SHT_LLVM_SYMPART:
5981a590232SFangrui Song       ctx->hasSympart.store(true, std::memory_order_relaxed);
5991a590232SFangrui Song       LLVM_FALLTHROUGH;
600e79b09a6SRui Ueyama     default:
601d86435c2SFangrui Song       this->sections[i] =
602d86435c2SFangrui Song           createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));
6033f11c8c9SRui Ueyama     }
604cb2d8e91SPeter Collingbourne   }
605cb2d8e91SPeter Collingbourne 
606af16a456SGeorgii Rymar   // We have a second loop. It is used to:
607af16a456SGeorgii Rymar   // 1) handle SHF_LINK_ORDER sections.
608af16a456SGeorgii Rymar   // 2) create SHT_REL[A] sections. In some cases the section header index of a
609af16a456SGeorgii Rymar   //    relocation section may be smaller than that of the relocated section. In
610af16a456SGeorgii Rymar   //    such cases, the relocation section would attempt to reference a target
611af16a456SGeorgii Rymar   //    section that has not yet been created. For simplicity, delay creation of
612af16a456SGeorgii Rymar   //    relocation sections until now.
61384944b63SFangrui Song   for (size_t i = 0; i != size; ++i) {
614cb2d8e91SPeter Collingbourne     if (this->sections[i] == &InputSection::discarded)
615cb2d8e91SPeter Collingbourne       continue;
616cb2d8e91SPeter Collingbourne     const Elf_Shdr &sec = objSections[i];
617af16a456SGeorgii Rymar 
618288082d4SFangrui Song     if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) {
619288082d4SFangrui Song       // Find a relocation target section and associate this section with that.
620288082d4SFangrui Song       // Target may have been discarded if it is in a different section group
621288082d4SFangrui Song       // and the group is discarded, even though it's a violation of the spec.
622288082d4SFangrui Song       // We handle that situation gracefully by discarding dangling relocation
623288082d4SFangrui Song       // sections.
624288082d4SFangrui Song       const uint32_t info = sec.sh_info;
625288082d4SFangrui Song       InputSectionBase *s = getRelocTarget(i, sec, info);
626288082d4SFangrui Song       if (!s)
627288082d4SFangrui Song         continue;
628288082d4SFangrui Song 
629288082d4SFangrui Song       // ELF spec allows mergeable sections with relocations, but they are rare,
630288082d4SFangrui Song       // and it is in practice hard to merge such sections by contents, because
631288082d4SFangrui Song       // applying relocations at end of linking changes section contents. So, we
632288082d4SFangrui Song       // simply handle such sections as non-mergeable ones. Degrading like this
633288082d4SFangrui Song       // is acceptable because section merging is optional.
634288082d4SFangrui Song       if (auto *ms = dyn_cast<MergeInputSection>(s)) {
635288082d4SFangrui Song         s = make<InputSection>(ms->file, ms->flags, ms->type, ms->alignment,
636288082d4SFangrui Song                                ms->data(), ms->name);
637288082d4SFangrui Song         sections[info] = s;
638288082d4SFangrui Song       }
639288082d4SFangrui Song 
640288082d4SFangrui Song       if (s->relSecIdx != 0)
641288082d4SFangrui Song         error(
642288082d4SFangrui Song             toString(s) +
643288082d4SFangrui Song             ": multiple relocation sections to one section are not supported");
644288082d4SFangrui Song       s->relSecIdx = i;
645288082d4SFangrui Song 
646288082d4SFangrui Song       // Relocation sections are usually removed from the output, so return
647288082d4SFangrui Song       // `nullptr` for the normal case. However, if -r or --emit-relocs is
648288082d4SFangrui Song       // specified, we need to copy them to the output. (Some post link analysis
649288082d4SFangrui Song       // tools specify --emit-relocs to obtain the information.)
650288082d4SFangrui Song       if (config->copyRelocs) {
651288082d4SFangrui Song         auto *isec = make<InputSection>(
652288082d4SFangrui Song             *this, sec, check(obj.getSectionName(sec, shstrtab)));
653288082d4SFangrui Song         // If the relocated section is discarded (due to /DISCARD/ or
654288082d4SFangrui Song         // --gc-sections), the relocation section should be discarded as well.
655288082d4SFangrui Song         s->dependentSections.push_back(isec);
656288082d4SFangrui Song         sections[i] = isec;
657288082d4SFangrui Song       }
658288082d4SFangrui Song       continue;
659288082d4SFangrui Song     }
660af16a456SGeorgii Rymar 
661b216c80cSFangrui Song     // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have
662b216c80cSFangrui Song     // the flag.
663288082d4SFangrui Song     if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER))
664cb2d8e91SPeter Collingbourne       continue;
6653f11c8c9SRui Ueyama 
6663837f427SRui Ueyama     InputSectionBase *linkSec = nullptr;
66784944b63SFangrui Song     if (sec.sh_link < size)
6683837f427SRui Ueyama       linkSec = this->sections[sec.sh_link];
6693837f427SRui Ueyama     if (!linkSec)
670cb2d8e91SPeter Collingbourne       fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link));
6711136ec64SGeorge Rimar 
672b216c80cSFangrui Song     // A SHF_LINK_ORDER section is discarded if its linked-to section is
673b216c80cSFangrui Song     // discarded.
6743837f427SRui Ueyama     InputSection *isec = cast<InputSection>(this->sections[i]);
6753837f427SRui Ueyama     linkSec->dependentSections.push_back(isec);
6763837f427SRui Ueyama     if (!isa<InputSection>(linkSec))
6773837f427SRui Ueyama       error("a section " + isec->name +
678cb2d8e91SPeter Collingbourne             " with SHF_LINK_ORDER should not refer a non-regular section: " +
6793837f427SRui Ueyama             toString(linkSec));
6800760605aSPeter Smith   }
6816b0eb5a6SFangrui Song 
68260ce444eSFangrui Song   for (ArrayRef<Elf_Word> entries : selectedGroups)
68360ce444eSFangrui Song     handleSectionGroup<ELFT>(this->sections, entries);
6840760605aSPeter Smith }
6850760605aSPeter Smith 
68670997f9aSPeter Smith // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
68770997f9aSPeter Smith // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
68870997f9aSPeter Smith // the input objects have been compiled.
updateARMVFPArgs(const ARMAttributeParser & attributes,const InputFile * f)6893837f427SRui Ueyama static void updateARMVFPArgs(const ARMAttributeParser &attributes,
6903837f427SRui Ueyama                              const InputFile *f) {
691581ba352SKai Wang   Optional<unsigned> attr =
692581ba352SKai Wang       attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
6935413bf1bSKazu Hirata   if (!attr)
69470997f9aSPeter Smith     // If an ABI tag isn't present then it is implicitly given the value of 0
69570997f9aSPeter Smith     // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
69670997f9aSPeter Smith     // including some in glibc that don't use FP args (and should have value 3)
69770997f9aSPeter Smith     // don't have the attribute so we do not consider an implicit value of 0
69870997f9aSPeter Smith     // as a clash.
69970997f9aSPeter Smith     return;
70070997f9aSPeter Smith 
701ed8fceaaSKazu Hirata   unsigned vfpArgs = *attr;
7023837f427SRui Ueyama   ARMVFPArgKind arg;
7033837f427SRui Ueyama   switch (vfpArgs) {
70470997f9aSPeter Smith   case ARMBuildAttrs::BaseAAPCS:
7053837f427SRui Ueyama     arg = ARMVFPArgKind::Base;
70670997f9aSPeter Smith     break;
70770997f9aSPeter Smith   case ARMBuildAttrs::HardFPAAPCS:
7083837f427SRui Ueyama     arg = ARMVFPArgKind::VFP;
70970997f9aSPeter Smith     break;
71070997f9aSPeter Smith   case ARMBuildAttrs::ToolChainFPPCS:
71170997f9aSPeter Smith     // Tool chain specific convention that conforms to neither AAPCS variant.
7123837f427SRui Ueyama     arg = ARMVFPArgKind::ToolChain;
71370997f9aSPeter Smith     break;
71470997f9aSPeter Smith   case ARMBuildAttrs::CompatibleFPAAPCS:
71570997f9aSPeter Smith     // Object compatible with all conventions.
71670997f9aSPeter Smith     return;
71770997f9aSPeter Smith   default:
7183837f427SRui Ueyama     error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs));
71970997f9aSPeter Smith     return;
72070997f9aSPeter Smith   }
72170997f9aSPeter Smith   // Follow ld.bfd and error if there is a mix of calling conventions.
7223837f427SRui Ueyama   if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default)
7233837f427SRui Ueyama     error(toString(f) + ": incompatible Tag_ABI_VFP_args");
72470997f9aSPeter Smith   else
7253837f427SRui Ueyama     config->armVFPArgs = arg;
72670997f9aSPeter Smith }
72770997f9aSPeter Smith 
72857eb0469SPeter Smith // The ARM support in lld makes some use of instructions that are not available
72957eb0469SPeter Smith // on all ARM architectures. Namely:
73057eb0469SPeter Smith // - Use of BLX instruction for interworking between ARM and Thumb state.
73157eb0469SPeter Smith // - Use of the extended Thumb branch encoding in relocation.
73257eb0469SPeter Smith // - Use of the MOVT/MOVW instructions in Thumb Thunks.
73357eb0469SPeter Smith // The ARM Attributes section contains information about the architecture chosen
73457eb0469SPeter Smith // at compile time. We follow the convention that if at least one input object
73557eb0469SPeter Smith // is compiled with an architecture that supports these features then lld is
73657eb0469SPeter Smith // permitted to use them.
updateSupportedARMFeatures(const ARMAttributeParser & attributes)7373837f427SRui Ueyama static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) {
738581ba352SKai Wang   Optional<unsigned> attr =
739581ba352SKai Wang       attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
740586fb81eSKazu Hirata   if (!attr)
74157eb0469SPeter Smith     return;
7425cff5142SKazu Hirata   auto arch = attr.value();
7433837f427SRui Ueyama   switch (arch) {
74457eb0469SPeter Smith   case ARMBuildAttrs::Pre_v4:
74557eb0469SPeter Smith   case ARMBuildAttrs::v4:
74657eb0469SPeter Smith   case ARMBuildAttrs::v4T:
74757eb0469SPeter Smith     // Architectures prior to v5 do not support BLX instruction
74857eb0469SPeter Smith     break;
74957eb0469SPeter Smith   case ARMBuildAttrs::v5T:
75057eb0469SPeter Smith   case ARMBuildAttrs::v5TE:
75157eb0469SPeter Smith   case ARMBuildAttrs::v5TEJ:
75257eb0469SPeter Smith   case ARMBuildAttrs::v6:
75357eb0469SPeter Smith   case ARMBuildAttrs::v6KZ:
75457eb0469SPeter Smith   case ARMBuildAttrs::v6K:
7553837f427SRui Ueyama     config->armHasBlx = true;
75657eb0469SPeter Smith     // Architectures used in pre-Cortex processors do not support
75757eb0469SPeter Smith     // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
75857eb0469SPeter Smith     // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
75957eb0469SPeter Smith     break;
76057eb0469SPeter Smith   default:
76157eb0469SPeter Smith     // All other Architectures have BLX and extended branch encoding
7623837f427SRui Ueyama     config->armHasBlx = true;
7633837f427SRui Ueyama     config->armJ1J2BranchEncoding = true;
7643837f427SRui Ueyama     if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M)
76557eb0469SPeter Smith       // All Architectures used in Cortex processors with the exception
76657eb0469SPeter Smith       // of v6-M and v6S-M have the MOVT and MOVW instructions.
7673837f427SRui Ueyama       config->armHasMovtMovw = true;
76857eb0469SPeter Smith     break;
76957eb0469SPeter Smith   }
77057eb0469SPeter Smith }
77157eb0469SPeter Smith 
7722057f836SRui Ueyama // If a source file is compiled with x86 hardware-assisted call flow control
7732057f836SRui Ueyama // enabled, the generated object file contains feature flags indicating that
7742057f836SRui Ueyama // fact. This function reads the feature flags and returns it.
7752057f836SRui Ueyama //
7762057f836SRui Ueyama // Essentially we want to read a single 32-bit value in this function, but this
7772057f836SRui Ueyama // function is rather complicated because the value is buried deep inside a
7782057f836SRui Ueyama // .note.gnu.property section.
7792057f836SRui Ueyama //
7802057f836SRui Ueyama // The section consists of one or more NOTE records. Each NOTE record consists
7812057f836SRui Ueyama // of zero or more type-length-value fields. We want to find a field of a
7822057f836SRui Ueyama // certain type. It seems a bit too much to just store a 32-bit value, perhaps
7832057f836SRui Ueyama // the ABI is unnecessarily complicated.
readAndFeatures(const InputSection & sec)78425863cc5SFangrui Song template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
7852057f836SRui Ueyama   using Elf_Nhdr = typename ELFT::Nhdr;
7862057f836SRui Ueyama   using Elf_Note = typename ELFT::Note;
7872057f836SRui Ueyama 
7883837f427SRui Ueyama   uint32_t featuresSet = 0;
789ae1ba619SFangrui Song   ArrayRef<uint8_t> data = sec.rawData;
79025863cc5SFangrui Song   auto reportFatal = [&](const uint8_t *place, const char *msg) {
79125863cc5SFangrui Song     fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
792ae1ba619SFangrui Song           Twine::utohexstr(place - sec.rawData.data()) + "): " + msg);
79325863cc5SFangrui Song   };
7943837f427SRui Ueyama   while (!data.empty()) {
7952057f836SRui Ueyama     // Read one NOTE record.
7963837f427SRui Ueyama     auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
79725863cc5SFangrui Song     if (data.size() < sizeof(Elf_Nhdr) || data.size() < nhdr->getSize())
79825863cc5SFangrui Song       reportFatal(data.data(), "data is too short");
7992057f836SRui Ueyama 
8003837f427SRui Ueyama     Elf_Note note(*nhdr);
8013837f427SRui Ueyama     if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {
8023837f427SRui Ueyama       data = data.slice(nhdr->getSize());
8032057f836SRui Ueyama       continue;
8042057f836SRui Ueyama     }
8052057f836SRui Ueyama 
8063837f427SRui Ueyama     uint32_t featureAndType = config->emachine == EM_AARCH64
807e208208aSPeter Smith                                   ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
808e208208aSPeter Smith                                   : GNU_PROPERTY_X86_FEATURE_1_AND;
809e208208aSPeter Smith 
8102057f836SRui Ueyama     // Read a body of a NOTE record, which consists of type-length-value fields.
8113837f427SRui Ueyama     ArrayRef<uint8_t> desc = note.getDesc();
8123837f427SRui Ueyama     while (!desc.empty()) {
81325863cc5SFangrui Song       const uint8_t *place = desc.data();
8143837f427SRui Ueyama       if (desc.size() < 8)
81525863cc5SFangrui Song         reportFatal(place, "program property is too short");
81625863cc5SFangrui Song       uint32_t type = read32<ELFT::TargetEndianness>(desc.data());
81725863cc5SFangrui Song       uint32_t size = read32<ELFT::TargetEndianness>(desc.data() + 4);
81825863cc5SFangrui Song       desc = desc.slice(8);
81925863cc5SFangrui Song       if (desc.size() < size)
82025863cc5SFangrui Song         reportFatal(place, "program property is too short");
8212057f836SRui Ueyama 
8223837f427SRui Ueyama       if (type == featureAndType) {
823e12334a0SPeter Smith         // We found a FEATURE_1_AND field. There may be more than one of these
8245976a3f5SNico Weber         // in a .note.gnu.property section, for a relocatable object we
825e12334a0SPeter Smith         // accumulate the bits set.
82625863cc5SFangrui Song         if (size < 4)
82725863cc5SFangrui Song           reportFatal(place, "FEATURE_1_AND entry is too short");
82825863cc5SFangrui Song         featuresSet |= read32<ELFT::TargetEndianness>(desc.data());
8292057f836SRui Ueyama       }
8302057f836SRui Ueyama 
83125863cc5SFangrui Song       // Padding is present in the note descriptor, if necessary.
83225863cc5SFangrui Song       desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
8332057f836SRui Ueyama     }
8342057f836SRui Ueyama 
835e12334a0SPeter Smith     // Go to next NOTE record to look for more FEATURE_1_AND descriptions.
8363837f427SRui Ueyama     data = data.slice(nhdr->getSize());
8372057f836SRui Ueyama   }
8382057f836SRui Ueyama 
8393837f427SRui Ueyama   return featuresSet;
8402057f836SRui Ueyama }
8412057f836SRui Ueyama 
842f1d598c2SRafael Espindola template <class ELFT>
getRelocTarget(uint32_t idx,const Elf_Shdr & sec,uint32_t info)843288082d4SFangrui Song InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx,
844288082d4SFangrui Song                                                 const Elf_Shdr &sec,
845288082d4SFangrui Song                                                 uint32_t info) {
846213d1849SFangrui Song   if (info < this->sections.size()) {
847213d1849SFangrui Song     InputSectionBase *target = this->sections[info];
848e270c0a1SRui Ueyama 
849e270c0a1SRui Ueyama     // Strictly speaking, a relocation section must be included in the
850e270c0a1SRui Ueyama     // group of the section it relocates. However, LLVM 3.3 and earlier
851e270c0a1SRui Ueyama     // would fail to do so, so we gracefully handle that case.
8523837f427SRui Ueyama     if (target == &InputSection::discarded)
853e270c0a1SRui Ueyama       return nullptr;
854e270c0a1SRui Ueyama 
855213d1849SFangrui Song     if (target != nullptr)
8563837f427SRui Ueyama       return target;
857e270c0a1SRui Ueyama   }
858e270c0a1SRui Ueyama 
859288082d4SFangrui Song   error(toString(this) + Twine(": relocation section (index ") + Twine(idx) +
860288082d4SFangrui Song         ") has invalid sh_info (" + Twine(info) + ")");
861213d1849SFangrui Song   return nullptr;
862213d1849SFangrui Song }
863213d1849SFangrui Song 
864e270c0a1SRui Ueyama template <class ELFT>
createInputSection(uint32_t idx,const Elf_Shdr & sec,StringRef name)865d86435c2SFangrui Song InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
866d86435c2SFangrui Song                                                     const Elf_Shdr &sec,
867d86435c2SFangrui Song                                                     StringRef name) {
868ee7720acSFangrui Song   if (sec.sh_type == SHT_ARM_ATTRIBUTES && config->emachine == EM_ARM) {
8693837f427SRui Ueyama     ARMAttributeParser attributes;
8704845531fSGeorgii Rymar     ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
871791efb14SFangrui Song     if (Error e = attributes.parse(contents, config->ekind == ELF32LEKind
872791efb14SFangrui Song                                                  ? support::little
873791efb14SFangrui Song                                                  : support::big)) {
874791efb14SFangrui Song       auto *isec = make<InputSection>(*this, sec, name);
875791efb14SFangrui Song       warn(toString(isec) + ": " + llvm::toString(std::move(e)));
876bef38e86SJessica Clarke     } else {
8773837f427SRui Ueyama       updateSupportedARMFeatures(attributes);
8783837f427SRui Ueyama       updateARMVFPArgs(attributes, this);
87970997f9aSPeter Smith 
88057eb0469SPeter Smith       // FIXME: Retain the first attribute section we see. The eglibc ARM
88157eb0469SPeter Smith       // dynamic loaders require the presence of an attribute section for dlopen
882bef38e86SJessica Clarke       // to work. In a full implementation we would merge all attribute
883bef38e86SJessica Clarke       // sections.
884bef38e86SJessica Clarke       if (in.attributes == nullptr) {
885cb203f3fSFangrui Song         in.attributes = std::make_unique<InputSection>(*this, sec, name);
886cb203f3fSFangrui Song         return in.attributes.get();
887532bc984SPeter Smith       }
8883837f427SRui Ueyama       return &InputSection::discarded;
88957eb0469SPeter Smith     }
890bef38e86SJessica Clarke   }
891bef38e86SJessica Clarke 
892ee7720acSFangrui Song   if (sec.sh_type == SHT_RISCV_ATTRIBUTES && config->emachine == EM_RISCV) {
893bef38e86SJessica Clarke     RISCVAttributeParser attributes;
8944845531fSGeorgii Rymar     ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
895bef38e86SJessica Clarke     if (Error e = attributes.parse(contents, support::little)) {
896bef38e86SJessica Clarke       auto *isec = make<InputSection>(*this, sec, name);
897bef38e86SJessica Clarke       warn(toString(isec) + ": " + llvm::toString(std::move(e)));
898bef38e86SJessica Clarke     } else {
899bef38e86SJessica Clarke       // FIXME: Validate arch tag contains C if and only if EF_RISCV_RVC is
900bef38e86SJessica Clarke       // present.
901bef38e86SJessica Clarke 
902bef38e86SJessica Clarke       // FIXME: Retain the first attribute section we see. Tools such as
903bef38e86SJessica Clarke       // llvm-objdump make use of the attribute section to determine which
904bef38e86SJessica Clarke       // standard extensions to enable. In a full implementation we would merge
905bef38e86SJessica Clarke       // all attribute sections.
906bef38e86SJessica Clarke       if (in.attributes == nullptr) {
907cb203f3fSFangrui Song         in.attributes = std::make_unique<InputSection>(*this, sec, name);
908cb203f3fSFangrui Song         return in.attributes.get();
909bef38e86SJessica Clarke       }
910bef38e86SJessica Clarke       return &InputSection::discarded;
911bef38e86SJessica Clarke     }
912bef38e86SJessica Clarke   }
913bef38e86SJessica Clarke 
914288082d4SFangrui Song   if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !config->relocatable) {
9153837f427SRui Ueyama     ArrayRef<char> data =
9164845531fSGeorgii Rymar         CHECK(this->getObj().template getSectionContentsAsArray<char>(sec), this);
9173837f427SRui Ueyama     if (!data.empty() && data.back() != '\0') {
9181d16515fSBen Dunbobbin       error(toString(this) +
9191d16515fSBen Dunbobbin             ": corrupted dependent libraries section (unterminated string): " +
9203837f427SRui Ueyama             name);
9213837f427SRui Ueyama       return &InputSection::discarded;
9221d16515fSBen Dunbobbin     }
9233837f427SRui Ueyama     for (const char *d = data.begin(), *e = data.end(); d < e;) {
9243837f427SRui Ueyama       StringRef s(d);
9253837f427SRui Ueyama       addDependentLibrary(s, this);
9263837f427SRui Ueyama       d += s.size() + 1;
9271d16515fSBen Dunbobbin     }
9283837f427SRui Ueyama     return &InputSection::discarded;
9291d16515fSBen Dunbobbin   }
930042a3f20SRafael Espindola 
93150187d2dSFangrui Song   if (name.startswith(".n")) {
9320633273dSRui Ueyama     // The GNU linker uses .note.GNU-stack section as a marker indicating
9330633273dSRui Ueyama     // that the code in the object file does not expect that the stack is
9340633273dSRui Ueyama     // executable (in terms of NX bit). If all input files have the marker,
9350633273dSRui Ueyama     // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
93665efe356SRui Ueyama     // make the stack non-executable. Most object files have this section as
93765efe356SRui Ueyama     // of 2017.
9380633273dSRui Ueyama     //
9390633273dSRui Ueyama     // But making the stack non-executable is a norm today for security
94065efe356SRui Ueyama     // reasons. Failure to do so may result in a serious security issue.
94165efe356SRui Ueyama     // Therefore, we make LLD always add PT_GNU_STACK unless it is
9420633273dSRui Ueyama     // explicitly told to do otherwise (by -z execstack). Because the stack
9430633273dSRui Ueyama     // executable-ness is controlled solely by command line options,
9440633273dSRui Ueyama     // .note.GNU-stack sections are simply ignored.
9453837f427SRui Ueyama     if (name == ".note.GNU-stack")
9463837f427SRui Ueyama       return &InputSection::discarded;
9473f11c8c9SRui Ueyama 
948e208208aSPeter Smith     // Object files that use processor features such as Intel Control-Flow
949e208208aSPeter Smith     // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a
950e208208aSPeter Smith     // .note.gnu.property section containing a bitfield of feature bits like the
9512057f836SRui Ueyama     // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag.
9522057f836SRui Ueyama     //
9532057f836SRui Ueyama     // Since we merge bitmaps from multiple object files to create a new
9542057f836SRui Ueyama     // .note.gnu.property containing a single AND'ed bitmap, we discard an input
9552057f836SRui Ueyama     // file's .note.gnu.property section.
9563837f427SRui Ueyama     if (name == ".note.gnu.property") {
95725863cc5SFangrui Song       this->andFeatures = readAndFeatures<ELFT>(InputSection(*this, sec, name));
9583837f427SRui Ueyama       return &InputSection::discarded;
9592057f836SRui Ueyama     }
9602057f836SRui Ueyama 
9614fd84c18SSterling Augustine     // Split stacks is a feature to support a discontiguous stack,
9624fd84c18SSterling Augustine     // commonly used in the programming language Go. For the details,
9634fd84c18SSterling Augustine     // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
9644fd84c18SSterling Augustine     // for split stack will include a .note.GNU-split-stack section.
9653837f427SRui Ueyama     if (name == ".note.GNU-split-stack") {
9663837f427SRui Ueyama       if (config->relocatable) {
96750187d2dSFangrui Song         error(
96850187d2dSFangrui Song             "cannot mix split-stack and non-split-stack in a relocatable link");
9693837f427SRui Ueyama         return &InputSection::discarded;
9704fd84c18SSterling Augustine       }
9713837f427SRui Ueyama       this->splitStack = true;
9723837f427SRui Ueyama       return &InputSection::discarded;
9734fd84c18SSterling Augustine     }
9744fd84c18SSterling Augustine 
9754fd84c18SSterling Augustine     // An object file cmpiled for split stack, but where some of the
9764fd84c18SSterling Augustine     // functions were compiled with the no_split_stack_attribute will
9774fd84c18SSterling Augustine     // include a .note.GNU-no-split-stack section.
9783837f427SRui Ueyama     if (name == ".note.GNU-no-split-stack") {
9793837f427SRui Ueyama       this->someNoSplitStack = true;
9803837f427SRui Ueyama       return &InputSection::discarded;
981fc6a4b04SRui Ueyama     }
982fc6a4b04SRui Ueyama 
98350187d2dSFangrui Song     // Strip existing .note.gnu.build-id sections so that the output won't have
98450187d2dSFangrui Song     // more than one build-id. This is not usually a problem because input
98550187d2dSFangrui Song     // object files normally don't have .build-id sections, but you can create
98650187d2dSFangrui Song     // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard
98750187d2dSFangrui Song     // against it.
98850187d2dSFangrui Song     if (name == ".note.gnu.build-id")
98950187d2dSFangrui Song       return &InputSection::discarded;
99050187d2dSFangrui Song   }
99150187d2dSFangrui Song 
992eba9b63cSRui Ueyama   // The linker merges EH (exception handling) frames and creates a
993eba9b63cSRui Ueyama   // .eh_frame_hdr section for runtime. So we handle them with a special
994eba9b63cSRui Ueyama   // class. For relocatable outputs, they are just passed through.
9953837f427SRui Ueyama   if (name == ".eh_frame" && !config->relocatable)
9963837f427SRui Ueyama     return make<EhInputSection>(*this, sec, name);
997eba9b63cSRui Ueyama 
99850187d2dSFangrui Song   if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name))
9993837f427SRui Ueyama     return make<MergeInputSection>(*this, sec, name);
10003837f427SRui Ueyama   return make<InputSection>(*this, sec, name);
10010c6a4f19SRafael Espindola }
100284487f11SMichael J. Spencer 
100342548403SRui Ueyama // Initialize this->Symbols. this->Symbols is a parallel array as
100442548403SRui Ueyama // its corresponding ELF symbol table.
1005ee7720acSFangrui Song template <class ELFT>
initializeSymbols(const object::ELFFile<ELFT> & obj)1006ee7720acSFangrui Song void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
10070d749e13SFangrui Song   SymbolTable &symtab = *elf::symtab;
10080d749e13SFangrui Song 
10093837f427SRui Ueyama   ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
10100d749e13SFangrui Song   symbols.resize(eSyms.size());
1011c4d13f72SFangrui Song 
1012509153f1SFangrui Song   // Some entries have been filled by LazyObjFile.
1013509153f1SFangrui Song   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
10140d749e13SFangrui Song     if (!symbols[i])
10150d749e13SFangrui Song       symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this));
1016509153f1SFangrui Song 
1017509153f1SFangrui Song   // Perform symbol resolution on non-local symbols.
1018fbb45947SNico Weber   SmallVector<unsigned, 32> undefineds;
1019c4d13f72SFangrui Song   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
1020c4d13f72SFangrui Song     const Elf_Sym &eSym = eSyms[i];
1021ee647d4cSFangrui Song     uint32_t secIdx = eSym.st_shndx;
102250460b80SFangrui Song     if (secIdx == SHN_UNDEF) {
102350460b80SFangrui Song       undefineds.push_back(i);
102450460b80SFangrui Song       continue;
102550460b80SFangrui Song     }
102650460b80SFangrui Song 
1027c9dbf407SFangrui Song     uint8_t binding = eSym.getBinding();
10283837f427SRui Ueyama     uint8_t stOther = eSym.st_other;
10293837f427SRui Ueyama     uint8_t type = eSym.getType();
10303837f427SRui Ueyama     uint64_t value = eSym.st_value;
10313837f427SRui Ueyama     uint64_t size = eSym.st_size;
1032a13efc2aSRui Ueyama 
10330d749e13SFangrui Song     Symbol *sym = symbols[i];
10348ca46bbaSFangrui Song     sym->isUsedInRegularObj = true;
10350d749e13SFangrui Song     if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {
10363837f427SRui Ueyama       if (value == 0 || value >= UINT32_MAX)
1037977a1a52SFangrui Song         fatal(toString(this) + ": common symbol '" + sym->getName() +
10383837f427SRui Ueyama               "' has invalid alignment: " + Twine(value));
103940fae4d8SFangrui Song       hasCommonSyms = true;
1040a596a5fcSFangrui Song       sym->resolve(
1041977a1a52SFangrui Song           CommonSymbol{this, StringRef(), binding, stOther, type, value, size});
104242548403SRui Ueyama       continue;
104351d4690bSRafael Espindola     }
104420348229SRafael Espindola 
10457c7702b3SFangrui Song     // Handle global defined symbols. Defined::section will be set in postParse.
1046c9dbf407SFangrui Song     sym->resolve(Defined{this, StringRef(), binding, stOther, type, value, size,
1047c9dbf407SFangrui Song                          nullptr});
1048444576d4SRafael Espindola   }
10490557b1bdSFangrui Song 
10500557b1bdSFangrui Song   // Undefined symbols (excluding those defined relative to non-prevailing
105109401dfcSFangrui Song   // sections) can trigger recursive extract. Process defined symbols first so
10520557b1bdSFangrui Song   // that the relative order between a defined symbol and an undefined symbol
10530557b1bdSFangrui Song   // does not change the symbol resolution behavior. In addition, a set of
10540557b1bdSFangrui Song   // interconnected symbols will all be resolved to the same file, instead of
10550557b1bdSFangrui Song   // being resolved to different files.
1056fbb45947SNico Weber   for (unsigned i : undefineds) {
10570557b1bdSFangrui Song     const Elf_Sym &eSym = eSyms[i];
10580d749e13SFangrui Song     Symbol *sym = symbols[i];
1059977a1a52SFangrui Song     sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other,
1060977a1a52SFangrui Song                            eSym.getType()});
10618ca46bbaSFangrui Song     sym->isUsedInRegularObj = true;
1062b5805b78SFangrui Song     sym->referenced = true;
10630557b1bdSFangrui Song   }
1064b13df658SRafael Espindola }
106584487f11SMichael J. Spencer 
initializeLocalSymbols()1066a815424cSFangrui Song template <class ELFT> void ObjFile<ELFT>::initializeLocalSymbols() {
1067a815424cSFangrui Song   if (!firstGlobal)
1068a815424cSFangrui Song     return;
1069a815424cSFangrui Song   localSymStorage = std::make_unique<SymbolUnion[]>(firstGlobal);
1070a815424cSFangrui Song   SymbolUnion *locals = localSymStorage.get();
1071a815424cSFangrui Song 
1072a815424cSFangrui Song   ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
1073a815424cSFangrui Song   for (size_t i = 0, end = firstGlobal; i != end; ++i) {
1074a815424cSFangrui Song     const Elf_Sym &eSym = eSyms[i];
1075a815424cSFangrui Song     uint32_t secIdx = eSym.st_shndx;
1076a815424cSFangrui Song     if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))
1077a815424cSFangrui Song       secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
1078a815424cSFangrui Song     else if (secIdx >= SHN_LORESERVE)
1079a815424cSFangrui Song       secIdx = 0;
1080a815424cSFangrui Song     if (LLVM_UNLIKELY(secIdx >= sections.size()))
1081a815424cSFangrui Song       fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
1082a815424cSFangrui Song     if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))
1083a815424cSFangrui Song       error(toString(this) + ": non-local symbol (" + Twine(i) +
1084a815424cSFangrui Song             ") found at index < .symtab's sh_info (" + Twine(end) + ")");
1085a815424cSFangrui Song 
1086a815424cSFangrui Song     InputSectionBase *sec = sections[secIdx];
1087a815424cSFangrui Song     uint8_t type = eSym.getType();
1088a815424cSFangrui Song     if (type == STT_FILE)
1089a815424cSFangrui Song       sourceFile = CHECK(eSym.getName(stringTable), this);
1090a815424cSFangrui Song     if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name))
1091a815424cSFangrui Song       fatal(toString(this) + ": invalid symbol name offset");
1092a815424cSFangrui Song     StringRef name(stringTable.data() + eSym.st_name);
1093a815424cSFangrui Song 
1094a815424cSFangrui Song     symbols[i] = reinterpret_cast<Symbol *>(locals + i);
1095a815424cSFangrui Song     if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded)
1096a815424cSFangrui Song       new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type,
1097a815424cSFangrui Song                                  /*discardedSecIdx=*/secIdx);
1098a815424cSFangrui Song     else
1099a815424cSFangrui Song       new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type,
1100a815424cSFangrui Song                                eSym.st_value, eSym.st_size, sec);
1101a815424cSFangrui Song     symbols[i]->isUsedInRegularObj = true;
1102a815424cSFangrui Song   }
1103a815424cSFangrui Song }
1104a815424cSFangrui Song 
110588d66f6eSFangrui Song // Called after all ObjFile::parse is called for all ObjFiles. This checks
110688d66f6eSFangrui Song // duplicate symbols and may do symbol property merge in the future.
postParse()110788d66f6eSFangrui Song template <class ELFT> void ObjFile<ELFT>::postParse() {
11087c7702b3SFangrui Song   static std::mutex mu;
110988d66f6eSFangrui Song   ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
111088d66f6eSFangrui Song   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
111188d66f6eSFangrui Song     const Elf_Sym &eSym = eSyms[i];
11127c7702b3SFangrui Song     Symbol &sym = *symbols[i];
11137c7702b3SFangrui Song     uint32_t secIdx = eSym.st_shndx;
1114c9dbf407SFangrui Song     uint8_t binding = eSym.getBinding();
1115c9dbf407SFangrui Song     if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK &&
1116c9dbf407SFangrui Song                       binding != STB_GNU_UNIQUE))
1117c9dbf407SFangrui Song       errorOrWarn(toString(this) + ": symbol (" + Twine(i) +
1118c9dbf407SFangrui Song                   ") has invalid binding: " + Twine((int)binding));
1119ba061713SFangrui Song 
1120ba061713SFangrui Song     // st_value of STT_TLS represents the assigned offset, not the actual
1121ba061713SFangrui Song     // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can
1122ba061713SFangrui Song     // only be referenced by special TLS relocations. It is usually an error if
1123ba061713SFangrui Song     // a STT_TLS symbol is replaced by a non-STT_TLS symbol, vice versa.
1124ba061713SFangrui Song     if (LLVM_UNLIKELY(sym.isTls()) && eSym.getType() != STT_TLS &&
1125ba061713SFangrui Song         eSym.getType() != STT_NOTYPE)
1126ba061713SFangrui Song       errorOrWarn("TLS attribute mismatch: " + toString(sym) + "\n>>> in " +
1127ba061713SFangrui Song                   toString(sym.file) + "\n>>> in " + toString(this));
1128ba061713SFangrui Song 
11297c7702b3SFangrui Song     // Handle non-COMMON defined symbol below. !sym.file allows a symbol
11307c7702b3SFangrui Song     // assignment to redefine a symbol without an error.
11317c7702b3SFangrui Song     if (!sym.file || !sym.isDefined() || secIdx == SHN_UNDEF ||
11327c7702b3SFangrui Song         secIdx == SHN_COMMON)
113388d66f6eSFangrui Song       continue;
11347c7702b3SFangrui Song 
113588d66f6eSFangrui Song     if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))
11367c7702b3SFangrui Song       secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
113788d66f6eSFangrui Song     else if (secIdx >= SHN_LORESERVE)
113888d66f6eSFangrui Song       secIdx = 0;
11397c7702b3SFangrui Song     if (LLVM_UNLIKELY(secIdx >= sections.size()))
11407c7702b3SFangrui Song       fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
11417c7702b3SFangrui Song     InputSectionBase *sec = sections[secIdx];
11427c7702b3SFangrui Song     if (sec == &InputSection::discarded) {
11437c7702b3SFangrui Song       if (sym.traced) {
11447c7702b3SFangrui Song         printTraceSymbol(Undefined{this, sym.getName(), sym.binding,
11457c7702b3SFangrui Song                                    sym.stOther, sym.type, secIdx},
11467c7702b3SFangrui Song                          sym.getName());
11477c7702b3SFangrui Song       }
11487c7702b3SFangrui Song       if (sym.file == this) {
11497c7702b3SFangrui Song         std::lock_guard<std::mutex> lock(mu);
11507c7702b3SFangrui Song         ctx->nonPrevailingSyms.emplace_back(&sym, secIdx);
11517c7702b3SFangrui Song       }
115288d66f6eSFangrui Song       continue;
11537c7702b3SFangrui Song     }
11547c7702b3SFangrui Song 
11557c7702b3SFangrui Song     if (sym.file == this) {
11567c7702b3SFangrui Song       cast<Defined>(sym).section = sec;
115788d66f6eSFangrui Song       continue;
11587c7702b3SFangrui Song     }
11597c7702b3SFangrui Song 
1160*dccd0613SFangrui Song     if (sym.binding == STB_WEAK || binding == STB_WEAK)
11617c7702b3SFangrui Song       continue;
11627c7702b3SFangrui Song     std::lock_guard<std::mutex> lock(mu);
11637c7702b3SFangrui Song     ctx->duplicates.push_back({&sym, this, sec, eSym.st_value});
116488d66f6eSFangrui Song   }
116588d66f6eSFangrui Song }
116688d66f6eSFangrui Song 
11678f91f381SSean Fertile // The handling of tentative definitions (COMMON symbols) in archives is murky.
1168cb4df6ebSNico Weber // A tentative definition will be promoted to a global definition if there are
1169cb4df6ebSNico Weber // no non-tentative definitions to dominate it. When we hold a tentative
1170cb4df6ebSNico Weber // definition to a symbol and are inspecting archive members for inclusion
1171cb4df6ebSNico Weber // there are 2 ways we can proceed:
11728f91f381SSean Fertile //
11738f91f381SSean Fertile // 1) Consider the tentative definition a 'real' definition (ie promotion from
11748f91f381SSean Fertile //    tentative to real definition has already happened) and not inspect
11758f91f381SSean Fertile //    archive members for Global/Weak definitions to replace the tentative
11768f91f381SSean Fertile //    definition. An archive member would only be included if it satisfies some
11778f91f381SSean Fertile //    other undefined symbol. This is the behavior Gold uses.
11788f91f381SSean Fertile //
11798f91f381SSean Fertile // 2) Consider the tentative definition as still undefined (ie the promotion to
1180cb4df6ebSNico Weber //    a real definition happens only after all symbol resolution is done).
11817de2173cSFangrui Song //    The linker searches archive members for STB_GLOBAL definitions to
11828f91f381SSean Fertile //    replace the tentative definition with. This is the behavior used by
11838f91f381SSean Fertile //    GNU ld.
11848f91f381SSean Fertile //
11858f91f381SSean Fertile //  The second behavior is inherited from SysVR4, which based it on the FORTRAN
1186221388f4SNico Weber //  COMMON BLOCK model. This behavior is needed for proper initialization in old
11878f91f381SSean Fertile //  (pre F90) FORTRAN code that is packaged into an archive.
11888f91f381SSean Fertile //
1189cb4df6ebSNico Weber //  The following functions search archive members for definitions to replace
1190cb4df6ebSNico Weber //  tentative definitions (implementing behavior 2).
isBitcodeNonCommonDef(MemoryBufferRef mb,StringRef symName,StringRef archiveName)11918f91f381SSean Fertile static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName,
11928f91f381SSean Fertile                                   StringRef archiveName) {
11938f91f381SSean Fertile   IRSymtabFile symtabFile = check(readIRSymtab(mb));
11948f91f381SSean Fertile   for (const irsymtab::Reader::SymbolRef &sym :
11958f91f381SSean Fertile        symtabFile.TheReader.symbols()) {
11968f91f381SSean Fertile     if (sym.isGlobal() && sym.getName() == symName)
11977de2173cSFangrui Song       return !sym.isUndefined() && !sym.isWeak() && !sym.isCommon();
11988f91f381SSean Fertile   }
11998f91f381SSean Fertile   return false;
12008f91f381SSean Fertile }
12018f91f381SSean Fertile 
12028f91f381SSean Fertile template <class ELFT>
isNonCommonDef(MemoryBufferRef mb,StringRef symName,StringRef archiveName)12038f91f381SSean Fertile static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
12048f91f381SSean Fertile                            StringRef archiveName) {
12058f91f381SSean Fertile   ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(mb, archiveName);
12068f91f381SSean Fertile   StringRef stringtable = obj->getStringTable();
12078f91f381SSean Fertile 
12088f91f381SSean Fertile   for (auto sym : obj->template getGlobalELFSyms<ELFT>()) {
12098f91f381SSean Fertile     Expected<StringRef> name = sym.getName(stringtable);
12108f91f381SSean Fertile     if (name && name.get() == symName)
12117de2173cSFangrui Song       return sym.isDefined() && sym.getBinding() == STB_GLOBAL &&
12127de2173cSFangrui Song              !sym.isCommon();
12138f91f381SSean Fertile   }
12148f91f381SSean Fertile   return false;
12158f91f381SSean Fertile }
12168f91f381SSean Fertile 
isNonCommonDef(MemoryBufferRef mb,StringRef symName,StringRef archiveName)12178f91f381SSean Fertile static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
12188f91f381SSean Fertile                            StringRef archiveName) {
12198f91f381SSean Fertile   switch (getELFKind(mb, archiveName)) {
12208f91f381SSean Fertile   case ELF32LEKind:
12218f91f381SSean Fertile     return isNonCommonDef<ELF32LE>(mb, symName, archiveName);
12228f91f381SSean Fertile   case ELF32BEKind:
12238f91f381SSean Fertile     return isNonCommonDef<ELF32BE>(mb, symName, archiveName);
12248f91f381SSean Fertile   case ELF64LEKind:
12258f91f381SSean Fertile     return isNonCommonDef<ELF64LE>(mb, symName, archiveName);
12268f91f381SSean Fertile   case ELF64BEKind:
12278f91f381SSean Fertile     return isNonCommonDef<ELF64BE>(mb, symName, archiveName);
12288f91f381SSean Fertile   default:
12298f91f381SSean Fertile     llvm_unreachable("getELFKind");
12308f91f381SSean Fertile   }
12318f91f381SSean Fertile }
12328f91f381SSean Fertile 
12333837f427SRui Ueyama unsigned SharedFile::vernauxNum;
1234d3e20705SPeter Collingbourne 
1235cc1618e6SPeter Collingbourne // Parse the version definitions in the object file if present, and return a
1236cc1618e6SPeter Collingbourne // vector whose nth element contains a pointer to the Elf_Verdef for version
1237cc1618e6SPeter Collingbourne // identifier n. Version identifiers that are not definitions map to nullptr.
1238cc1618e6SPeter Collingbourne template <typename ELFT>
1239c720b16aSFangrui Song static SmallVector<const void *, 0>
parseVerdefs(const uint8_t * base,const typename ELFT::Shdr * sec)1240c720b16aSFangrui Song parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) {
12413837f427SRui Ueyama   if (!sec)
1242cc1618e6SPeter Collingbourne     return {};
1243cc1618e6SPeter Collingbourne 
1244cc1618e6SPeter Collingbourne   // Build the Verdefs array by following the chain of Elf_Verdef objects
1245cc1618e6SPeter Collingbourne   // from the start of the .gnu.version_d section.
1246c720b16aSFangrui Song   SmallVector<const void *, 0> verdefs;
12473837f427SRui Ueyama   const uint8_t *verdef = base + sec->sh_offset;
1248c720b16aSFangrui Song   for (unsigned i = 0, e = sec->sh_info; i != e; ++i) {
12493837f427SRui Ueyama     auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef);
12503837f427SRui Ueyama     verdef += curVerdef->vd_next;
12513837f427SRui Ueyama     unsigned verdefIndex = curVerdef->vd_ndx;
1252c720b16aSFangrui Song     if (verdefIndex >= verdefs.size())
12533837f427SRui Ueyama       verdefs.resize(verdefIndex + 1);
12543837f427SRui Ueyama     verdefs[verdefIndex] = curVerdef;
1255cc1618e6SPeter Collingbourne   }
12563837f427SRui Ueyama   return verdefs;
1257883ab235SPeter Collingbourne }
125818173d42SRafael Espindola 
1259e32f04cdSFangrui Song // Parse SHT_GNU_verneed to properly set the name of a versioned undefined
1260e32f04cdSFangrui Song // symbol. We detect fatal issues which would cause vulnerabilities, but do not
1261e32f04cdSFangrui Song // implement sophisticated error checking like in llvm-readobj because the value
1262e32f04cdSFangrui Song // of such diagnostics is low.
1263e32f04cdSFangrui Song template <typename ELFT>
parseVerneed(const ELFFile<ELFT> & obj,const typename ELFT::Shdr * sec)1264e32f04cdSFangrui Song std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,
1265e32f04cdSFangrui Song                                                const typename ELFT::Shdr *sec) {
1266e32f04cdSFangrui Song   if (!sec)
1267e32f04cdSFangrui Song     return {};
1268e32f04cdSFangrui Song   std::vector<uint32_t> verneeds;
12694845531fSGeorgii Rymar   ArrayRef<uint8_t> data = CHECK(obj.getSectionContents(*sec), this);
1270e32f04cdSFangrui Song   const uint8_t *verneedBuf = data.begin();
1271e32f04cdSFangrui Song   for (unsigned i = 0; i != sec->sh_info; ++i) {
1272b8a3c618SFangrui Song     if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end())
1273e32f04cdSFangrui Song       fatal(toString(this) + " has an invalid Verneed");
1274e32f04cdSFangrui Song     auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf);
1275e32f04cdSFangrui Song     const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux;
1276e32f04cdSFangrui Song     for (unsigned j = 0; j != vn->vn_cnt; ++j) {
1277b8a3c618SFangrui Song       if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end())
1278e32f04cdSFangrui Song         fatal(toString(this) + " has an invalid Vernaux");
1279e32f04cdSFangrui Song       auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf);
1280e32f04cdSFangrui Song       if (aux->vna_name >= this->stringTable.size())
1281e32f04cdSFangrui Song         fatal(toString(this) + " has a Vernaux with an invalid vna_name");
1282e32f04cdSFangrui Song       uint16_t version = aux->vna_other & VERSYM_VERSION;
1283e32f04cdSFangrui Song       if (version >= verneeds.size())
1284e32f04cdSFangrui Song         verneeds.resize(version + 1);
1285e32f04cdSFangrui Song       verneeds[version] = aux->vna_name;
1286e32f04cdSFangrui Song       vernauxBuf += aux->vna_next;
1287e32f04cdSFangrui Song     }
1288e32f04cdSFangrui Song     verneedBuf += vn->vn_next;
1289e32f04cdSFangrui Song   }
1290e32f04cdSFangrui Song   return verneeds;
1291e32f04cdSFangrui Song }
1292e32f04cdSFangrui Song 
1293cc1618e6SPeter Collingbourne // We do not usually care about alignments of data in shared object
1294cc1618e6SPeter Collingbourne // files because the loader takes care of it. However, if we promote a
1295cc1618e6SPeter Collingbourne // DSO symbol to point to .bss due to copy relocation, we need to keep
1296cc1618e6SPeter Collingbourne // the original alignment requirements. We infer it in this function.
1297cc1618e6SPeter Collingbourne template <typename ELFT>
getAlignment(ArrayRef<typename ELFT::Shdr> sections,const typename ELFT::Sym & sym)12983837f427SRui Ueyama static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections,
12993837f427SRui Ueyama                              const typename ELFT::Sym &sym) {
13003837f427SRui Ueyama   uint64_t ret = UINT64_MAX;
13013837f427SRui Ueyama   if (sym.st_value)
13023837f427SRui Ueyama     ret = 1ULL << countTrailingZeros((uint64_t)sym.st_value);
13033837f427SRui Ueyama   if (0 < sym.st_shndx && sym.st_shndx < sections.size())
13043837f427SRui Ueyama     ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign);
13053837f427SRui Ueyama   return (ret > UINT32_MAX) ? 0 : ret;
1306cc1618e6SPeter Collingbourne }
1307cc1618e6SPeter Collingbourne 
1308cc1618e6SPeter Collingbourne // Fully parse the shared object file.
1309cc1618e6SPeter Collingbourne //
1310cc1618e6SPeter Collingbourne // This function parses symbol versions. If a DSO has version information,
1311cc1618e6SPeter Collingbourne // the file has a ".gnu.version_d" section which contains symbol version
1312cc1618e6SPeter Collingbourne // definitions. Each symbol is associated to one version through a table in
1313cc1618e6SPeter Collingbourne // ".gnu.version" section. That table is a parallel array for the symbol
1314cc1618e6SPeter Collingbourne // table, and each table entry contains an index in ".gnu.version_d".
1315cc1618e6SPeter Collingbourne //
1316cc1618e6SPeter Collingbourne // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for
1317cc1618e6SPeter Collingbourne // VER_NDX_GLOBAL. There's no table entry for these special versions in
1318cc1618e6SPeter Collingbourne // ".gnu.version_d".
1319cc1618e6SPeter Collingbourne //
1320cc1618e6SPeter Collingbourne // The file format for symbol versioning is perhaps a bit more complicated
1321cc1618e6SPeter Collingbourne // than necessary, but you can easily understand the code if you wrap your
1322cc1618e6SPeter Collingbourne // head around the data structure described above.
parse()1323cc1618e6SPeter Collingbourne template <class ELFT> void SharedFile::parse() {
1324cc1618e6SPeter Collingbourne   using Elf_Dyn = typename ELFT::Dyn;
1325cc1618e6SPeter Collingbourne   using Elf_Shdr = typename ELFT::Shdr;
1326cc1618e6SPeter Collingbourne   using Elf_Sym = typename ELFT::Sym;
1327cc1618e6SPeter Collingbourne   using Elf_Verdef = typename ELFT::Verdef;
1328cc1618e6SPeter Collingbourne   using Elf_Versym = typename ELFT::Versym;
1329cc1618e6SPeter Collingbourne 
13303837f427SRui Ueyama   ArrayRef<Elf_Dyn> dynamicTags;
13313837f427SRui Ueyama   const ELFFile<ELFT> obj = this->getObj<ELFT>();
1332b5a0f0f3SFangrui Song   ArrayRef<Elf_Shdr> sections = getELFShdrs<ELFT>();
13333233d3ebSRui Ueyama 
13343837f427SRui Ueyama   const Elf_Shdr *versymSec = nullptr;
13353837f427SRui Ueyama   const Elf_Shdr *verdefSec = nullptr;
1336e32f04cdSFangrui Song   const Elf_Shdr *verneedSec = nullptr;
1337cc1618e6SPeter Collingbourne 
13383233d3ebSRui Ueyama   // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
13393837f427SRui Ueyama   for (const Elf_Shdr &sec : sections) {
13403837f427SRui Ueyama     switch (sec.sh_type) {
1341115f0f36SRafael Espindola     default:
1342115f0f36SRafael Espindola       continue;
1343115f0f36SRafael Espindola     case SHT_DYNAMIC:
13443837f427SRui Ueyama       dynamicTags =
13454845531fSGeorgii Rymar           CHECK(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this);
1346115f0f36SRafael Espindola       break;
134721a12fc6SPeter Collingbourne     case SHT_GNU_versym:
13483837f427SRui Ueyama       versymSec = &sec;
134921a12fc6SPeter Collingbourne       break;
135021a12fc6SPeter Collingbourne     case SHT_GNU_verdef:
13513837f427SRui Ueyama       verdefSec = &sec;
135221a12fc6SPeter Collingbourne       break;
1353e32f04cdSFangrui Song     case SHT_GNU_verneed:
1354e32f04cdSFangrui Song       verneedSec = &sec;
1355e32f04cdSFangrui Song       break;
1356115f0f36SRafael Espindola     }
1357115f0f36SRafael Espindola   }
1358c8b15815SRafael Espindola 
13593837f427SRui Ueyama   if (versymSec && numELFSyms == 0) {
1360bcba39abSGeorge Rimar     error("SHT_GNU_versym should be associated with symbol table");
1361cc1618e6SPeter Collingbourne     return;
1362cc1618e6SPeter Collingbourne   }
1363bcba39abSGeorge Rimar 
136447cfe8f3SFangrui Song   // Search for a DT_SONAME tag to initialize this->soName.
13653837f427SRui Ueyama   for (const Elf_Dyn &dyn : dynamicTags) {
13663837f427SRui Ueyama     if (dyn.d_tag == DT_NEEDED) {
13673837f427SRui Ueyama       uint64_t val = dyn.getVal();
13683837f427SRui Ueyama       if (val >= this->stringTable.size())
1369b4744d30SFangrui Song         fatal(toString(this) + ": invalid DT_NEEDED entry");
13703837f427SRui Ueyama       dtNeeded.push_back(this->stringTable.data() + val);
13713837f427SRui Ueyama     } else if (dyn.d_tag == DT_SONAME) {
13723837f427SRui Ueyama       uint64_t val = dyn.getVal();
13733837f427SRui Ueyama       if (val >= this->stringTable.size())
13743fc0f7e5SRui Ueyama         fatal(toString(this) + ": invalid DT_SONAME entry");
13753837f427SRui Ueyama       soName = this->stringTable.data() + val;
137618173d42SRafael Espindola     }
1377c8b15815SRafael Espindola   }
137818173d42SRafael Espindola 
1379cc1618e6SPeter Collingbourne   // DSOs are uniquified not by filename but by soname.
1380769057a5SFangrui Song   DenseMap<CachedHashStringRef, SharedFile *>::iterator it;
13813837f427SRui Ueyama   bool wasInserted;
1382769057a5SFangrui Song   std::tie(it, wasInserted) =
1383769057a5SFangrui Song       symtab->soNames.try_emplace(CachedHashStringRef(soName), this);
1384cc1618e6SPeter Collingbourne 
1385cc1618e6SPeter Collingbourne   // If a DSO appears more than once on the command line with and without
1386cc1618e6SPeter Collingbourne   // --as-needed, --no-as-needed takes precedence over --as-needed because a
1387cc1618e6SPeter Collingbourne   // user can add an extra DSO with --no-as-needed to force it to be added to
1388cc1618e6SPeter Collingbourne   // the dependency list.
13893837f427SRui Ueyama   it->second->isNeeded |= isNeeded;
13903837f427SRui Ueyama   if (!wasInserted)
1391cc1618e6SPeter Collingbourne     return;
1392cc1618e6SPeter Collingbourne 
13939a572164SFangrui Song   ctx->sharedFiles.push_back(this);
1394cc1618e6SPeter Collingbourne 
13953837f427SRui Ueyama   verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec);
1396e32f04cdSFangrui Song   std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec);
1397cc1618e6SPeter Collingbourne 
1398cc1618e6SPeter Collingbourne   // Parse ".gnu.version" section which is a parallel array for the symbol
1399cc1618e6SPeter Collingbourne   // table. If a given file doesn't have a ".gnu.version" section, we use
1400cc1618e6SPeter Collingbourne   // VER_NDX_GLOBAL.
14013837f427SRui Ueyama   size_t size = numELFSyms - firstGlobal;
1402e32f04cdSFangrui Song   std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL);
14033837f427SRui Ueyama   if (versymSec) {
14043837f427SRui Ueyama     ArrayRef<Elf_Versym> versym =
14054845531fSGeorgii Rymar         CHECK(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec),
1406cc1618e6SPeter Collingbourne               this)
14073837f427SRui Ueyama             .slice(firstGlobal);
14083837f427SRui Ueyama     for (size_t i = 0; i < size; ++i)
14093837f427SRui Ueyama       versyms[i] = versym[i].vs_index;
1410b391288aSRui Ueyama   }
1411b391288aSRui Ueyama 
1412b4d3dfefSRafael Espindola   // System libraries can have a lot of symbols with versions. Using a
1413b4d3dfefSRafael Espindola   // fixed buffer for computing the versions name (foo@ver) can save a
1414b4d3dfefSRafael Espindola   // lot of allocations.
14153837f427SRui Ueyama   SmallString<0> versionedNameBuffer;
1416b4d3dfefSRafael Espindola 
14177f9694a4SRui Ueyama   // Add symbols to the symbol table.
14180d749e13SFangrui Song   SymbolTable &symtab = *elf::symtab;
14193837f427SRui Ueyama   ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>();
142060f56149SFangrui Song   for (size_t i = 0, e = syms.size(); i != e; ++i) {
14213837f427SRui Ueyama     const Elf_Sym &sym = syms[i];
1422fb4f2fedSRafael Espindola 
14236883d882SShoaib Meenai     // ELF spec requires that all local symbols precede weak or global
14246883d882SShoaib Meenai     // symbols in each symbol table, and the index of first non-local symbol
14256883d882SShoaib Meenai     // is stored to sh_info. If a local symbol appears after some non-local
14266883d882SShoaib Meenai     // symbol, that's a violation of the spec.
14270d749e13SFangrui Song     StringRef name = CHECK(sym.getName(stringTable), this);
14283837f427SRui Ueyama     if (sym.getBinding() == STB_LOCAL) {
14293837f427SRui Ueyama       warn("found local symbol '" + name +
14306883d882SShoaib Meenai            "' in global part of symbol table in file " + toString(this));
14316883d882SShoaib Meenai       continue;
14326883d882SShoaib Meenai     }
14336883d882SShoaib Meenai 
1434e32f04cdSFangrui Song     uint16_t idx = versyms[i] & ~VERSYM_HIDDEN;
14353837f427SRui Ueyama     if (sym.isUndefined()) {
1436e32f04cdSFangrui Song       // For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but
1437e32f04cdSFangrui Song       // as of binutils 2.34, GNU ld produces VER_NDX_LOCAL.
1438e32f04cdSFangrui Song       if (idx != VER_NDX_LOCAL && idx != VER_NDX_GLOBAL) {
1439e32f04cdSFangrui Song         if (idx >= verneeds.size()) {
1440e32f04cdSFangrui Song           error("corrupt input file: version need index " + Twine(idx) +
1441e32f04cdSFangrui Song                 " for symbol " + name + " is out of bounds\n>>> defined in " +
1442e32f04cdSFangrui Song                 toString(this));
1443e32f04cdSFangrui Song           continue;
1444e32f04cdSFangrui Song         }
14450d749e13SFangrui Song         StringRef verName = stringTable.data() + verneeds[idx];
1446e32f04cdSFangrui Song         versionedNameBuffer.clear();
144783d59e05SAlexandre Ganea         name = saver().save(
144883d59e05SAlexandre Ganea             (name + "@" + verName).toStringRef(versionedNameBuffer));
1449e32f04cdSFangrui Song       }
14500d749e13SFangrui Song       Symbol *s = symtab.addSymbol(
14513837f427SRui Ueyama           Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()});
14523837f427SRui Ueyama       s->exportDynamic = true;
1453ce25eb12SIgor Kudrin       if (s->isUndefined() && sym.getBinding() != STB_WEAK &&
145470c23e23SIgor Kudrin           config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore)
145570c23e23SIgor Kudrin         requiredSymbols.push_back(s);
145618da0e58SRafael Espindola       continue;
145718da0e58SRafael Espindola     }
145818da0e58SRafael Espindola 
1459bd63adcbSRui Ueyama     // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1460bd63adcbSRui Ueyama     // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1461bd63adcbSRui Ueyama     // workaround for this bug.
14623837f427SRui Ueyama     if (config->emachine == EM_MIPS && idx == VER_NDX_LOCAL &&
14633837f427SRui Ueyama         name == "_gp_disp")
146485815a31SSimon Atanasyan       continue;
146585815a31SSimon Atanasyan 
14663837f427SRui Ueyama     uint32_t alignment = getAlignment<ELFT>(sections, sym);
14673837f427SRui Ueyama     if (!(versyms[i] & VERSYM_HIDDEN)) {
14685ad2aae2SFangrui Song       auto *s = symtab.addSymbol(
14695ad2aae2SFangrui Song           SharedSymbol{*this, name, sym.getBinding(), sym.st_other,
14705ad2aae2SFangrui Song                        sym.getType(), sym.st_value, sym.st_size, alignment});
14715ad2aae2SFangrui Song       if (s->file == this)
14725ad2aae2SFangrui Song         s->verdefIndex = idx;
14737d476192SRui Ueyama     }
14742756e04fSRafael Espindola 
14752756e04fSRafael Espindola     // Also add the symbol with the versioned name to handle undefined symbols
14762756e04fSRafael Espindola     // with explicit versions.
14773837f427SRui Ueyama     if (idx == VER_NDX_GLOBAL)
1478b391288aSRui Ueyama       continue;
1479b391288aSRui Ueyama 
14803837f427SRui Ueyama     if (idx >= verdefs.size() || idx == VER_NDX_LOCAL) {
14813837f427SRui Ueyama       error("corrupt input file: version definition index " + Twine(idx) +
14823837f427SRui Ueyama             " for symbol " + name + " is out of bounds\n>>> defined in " +
1483b391288aSRui Ueyama             toString(this));
1484b391288aSRui Ueyama       continue;
14852756e04fSRafael Espindola     }
1486b391288aSRui Ueyama 
14873837f427SRui Ueyama     StringRef verName =
14880d749e13SFangrui Song         stringTable.data() +
14893837f427SRui Ueyama         reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name;
14903837f427SRui Ueyama     versionedNameBuffer.clear();
14913837f427SRui Ueyama     name = (name + "@" + verName).toStringRef(versionedNameBuffer);
14925ad2aae2SFangrui Song     auto *s = symtab.addSymbol(
14935ad2aae2SFangrui Song         SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other,
14945ad2aae2SFangrui Song                      sym.getType(), sym.st_value, sym.st_size, alignment});
14955ad2aae2SFangrui Song     if (s->file == this)
14965ad2aae2SFangrui Song       s->verdefIndex = idx;
149718173d42SRafael Espindola   }
149818173d42SRafael Espindola }
1499f98d6d84SRafael Espindola 
getBitcodeELFKind(const Triple & t)15003837f427SRui Ueyama static ELFKind getBitcodeELFKind(const Triple &t) {
15013837f427SRui Ueyama   if (t.isLittleEndian())
15023837f427SRui Ueyama     return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
15033837f427SRui Ueyama   return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
150460976ba8SDavide Italiano }
150560976ba8SDavide Italiano 
getBitcodeMachineKind(StringRef path,const Triple & t)1506a9cefc3dSChristian Iversen static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) {
15073837f427SRui Ueyama   switch (t.getArch()) {
150860976ba8SDavide Italiano   case Triple::aarch64:
15097605a9a0SFangrui Song   case Triple::aarch64_be:
151060976ba8SDavide Italiano     return EM_AARCH64;
15118d6c7a63SMatt Arsenault   case Triple::amdgcn:
15128d6c7a63SMatt Arsenault   case Triple::r600:
15138d6c7a63SMatt Arsenault     return EM_AMDGPU;
151460976ba8SDavide Italiano   case Triple::arm:
15151d96185fSSean Silva   case Triple::thumb:
151660976ba8SDavide Italiano     return EM_ARM;
151749ba795dSLeslie Zhai   case Triple::avr:
151849ba795dSLeslie Zhai     return EM_AVR;
1519aa4dfba5SFangrui Song   case Triple::hexagon:
1520aa4dfba5SFangrui Song     return EM_HEXAGON;
152160976ba8SDavide Italiano   case Triple::mips:
152260976ba8SDavide Italiano   case Triple::mipsel:
152360976ba8SDavide Italiano   case Triple::mips64:
152460976ba8SDavide Italiano   case Triple::mips64el:
152560976ba8SDavide Italiano     return EM_MIPS;
152673af3d40SGeorge Rimar   case Triple::msp430:
152773af3d40SGeorge Rimar     return EM_MSP430;
152860976ba8SDavide Italiano   case Triple::ppc:
1529275eb828SBrandon Bergren   case Triple::ppcle:
153060976ba8SDavide Italiano     return EM_PPC;
153160976ba8SDavide Italiano   case Triple::ppc64:
153269e09116SSean Fertile   case Triple::ppc64le:
153360976ba8SDavide Italiano     return EM_PPC64;
1534eb9bc382SKito Cheng   case Triple::riscv32:
1535eb9bc382SKito Cheng   case Triple::riscv64:
1536eb9bc382SKito Cheng     return EM_RISCV;
153760976ba8SDavide Italiano   case Triple::x86:
15383837f427SRui Ueyama     return t.isOSIAMCU() ? EM_IAMCU : EM_386;
153960976ba8SDavide Italiano   case Triple::x86_64:
154060976ba8SDavide Italiano     return EM_X86_64;
154160976ba8SDavide Italiano   default:
15423837f427SRui Ueyama     error(path + ": could not infer e_machine from bitcode target triple " +
15433837f427SRui Ueyama           t.str());
15443ad27e92SSam Clegg     return EM_NONE;
154560976ba8SDavide Italiano   }
154660976ba8SDavide Italiano }
154760976ba8SDavide Italiano 
getOsAbi(const Triple & t)1548f218652aSKonstantin Zhuravlyov static uint8_t getOsAbi(const Triple &t) {
1549f218652aSKonstantin Zhuravlyov   switch (t.getOS()) {
1550f218652aSKonstantin Zhuravlyov   case Triple::AMDHSA:
1551f218652aSKonstantin Zhuravlyov     return ELF::ELFOSABI_AMDGPU_HSA;
1552f218652aSKonstantin Zhuravlyov   case Triple::AMDPAL:
1553f218652aSKonstantin Zhuravlyov     return ELF::ELFOSABI_AMDGPU_PAL;
1554f218652aSKonstantin Zhuravlyov   case Triple::Mesa3D:
1555f218652aSKonstantin Zhuravlyov     return ELF::ELFOSABI_AMDGPU_MESA3D;
1556f218652aSKonstantin Zhuravlyov   default:
1557f218652aSKonstantin Zhuravlyov     return ELF::ELFOSABI_NONE;
1558f218652aSKonstantin Zhuravlyov   }
1559f218652aSKonstantin Zhuravlyov }
1560f218652aSKonstantin Zhuravlyov 
BitcodeFile(MemoryBufferRef mb,StringRef archiveName,uint64_t offsetInArchive,bool lazy)15613837f427SRui Ueyama BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
15623a5fb573SFangrui Song                          uint64_t offsetInArchive, bool lazy)
15633837f427SRui Ueyama     : InputFile(BitcodeKind, mb) {
15641ff1d50dSFangrui Song   this->archiveName = archiveName;
15653a5fb573SFangrui Song   this->lazy = lazy;
15668446f1feSPeter Collingbourne 
15673837f427SRui Ueyama   std::string path = mb.getBufferIdentifier().str();
15683837f427SRui Ueyama   if (config->thinLTOIndexOnly)
15693837f427SRui Ueyama     path = replaceThinLTOSuffix(mb.getBufferIdentifier());
1570d2eb089aSRumeet Dhindsa 
1571f06d494fSRui Ueyama   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1572f06d494fSRui Ueyama   // name. If two archives define two members with the same name, this
1573f06d494fSRui Ueyama   // causes a collision which result in only one of the objects being taken
1574f06d494fSRui Ueyama   // into consideration at LTO time (which very likely causes undefined
1575f06d494fSRui Ueyama   // symbols later in the link stage). So we append file offset to make
1576f06d494fSRui Ueyama   // filename unique.
157783d59e05SAlexandre Ganea   StringRef name = archiveName.empty()
157883d59e05SAlexandre Ganea                        ? saver().save(path)
157983d59e05SAlexandre Ganea                        : saver().save(archiveName + "(" + path::filename(path) +
158083d59e05SAlexandre Ganea                                       " at " + utostr(offsetInArchive) + ")");
15813837f427SRui Ueyama   MemoryBufferRef mbref(mb.getBuffer(), name);
1582f06d494fSRui Ueyama 
15833837f427SRui Ueyama   obj = CHECK(lto::InputFile::create(mbref), this);
15848446f1feSPeter Collingbourne 
15853837f427SRui Ueyama   Triple t(obj->getTargetTriple());
15863837f427SRui Ueyama   ekind = getBitcodeELFKind(t);
15873837f427SRui Ueyama   emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t);
1588f218652aSKonstantin Zhuravlyov   osabi = getOsAbi(t);
158960976ba8SDavide Italiano }
15909f77ef0cSRafael Espindola 
mapVisibility(GlobalValue::VisibilityTypes gvVisibility)15913837f427SRui Ueyama static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
15923837f427SRui Ueyama   switch (gvVisibility) {
159368fae234SRui Ueyama   case GlobalValue::DefaultVisibility:
159468fae234SRui Ueyama     return STV_DEFAULT;
1595fd4fee59SRui Ueyama   case GlobalValue::HiddenVisibility:
1596fd4fee59SRui Ueyama     return STV_HIDDEN;
1597fd4fee59SRui Ueyama   case GlobalValue::ProtectedVisibility:
1598fd4fee59SRui Ueyama     return STV_PROTECTED;
1599fd4fee59SRui Ueyama   }
1600777f9630SGeorge Rimar   llvm_unreachable("unknown visibility");
1601f7149554SRui Ueyama }
1602f7149554SRui Ueyama 
16034f952706SPeter Collingbourne template <class ELFT>
1604dabac5feSFangrui Song static void
createBitcodeSymbol(Symbol * & sym,const std::vector<bool> & keptComdats,const lto::InputFile::Symbol & objSym,BitcodeFile & f)1605dabac5feSFangrui Song createBitcodeSymbol(Symbol *&sym, const std::vector<bool> &keptComdats,
1606dabac5feSFangrui Song                     const lto::InputFile::Symbol &objSym, BitcodeFile &f) {
16073837f427SRui Ueyama   uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL;
16083837f427SRui Ueyama   uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE;
16093837f427SRui Ueyama   uint8_t visibility = mapVisibility(objSym.getVisibility());
161029fa6ab7SDavide Italiano 
1611977a1a52SFangrui Song   if (!sym)
1612977a1a52SFangrui Song     sym = symtab->insert(saver().save(objSym.getName()));
1613dabac5feSFangrui Song 
16143837f427SRui Ueyama   int c = objSym.getComdatIndex();
16153837f427SRui Ueyama   if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) {
1616977a1a52SFangrui Song     Undefined newSym(&f, StringRef(), binding, visibility, type);
1617dabac5feSFangrui Song     sym->resolve(newSym);
1618dabac5feSFangrui Song     sym->referenced = true;
1619dabac5feSFangrui Song     return;
16207d476192SRui Ueyama   }
16219f8efffeSDavide Italiano 
1622dabac5feSFangrui Song   if (objSym.isCommon()) {
1623977a1a52SFangrui Song     sym->resolve(CommonSymbol{&f, StringRef(), binding, visibility, STT_OBJECT,
1624dabac5feSFangrui Song                               objSym.getCommonAlignment(),
1625dabac5feSFangrui Song                               objSym.getCommonSize()});
1626dabac5feSFangrui Song   } else {
1627977a1a52SFangrui Song     Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr);
1628ac2911e7SFangrui Song     if (objSym.canBeOmittedFromSymbolTable())
1629ab04ad6aSFangrui Song       newSym.exportDynamic = false;
1630dabac5feSFangrui Song     sym->resolve(newSym);
1631dabac5feSFangrui Song   }
16329b3acf90SRafael Espindola }
16339b3acf90SRafael Espindola 
parse()1634579c8df7SSam Clegg template <class ELFT> void BitcodeFile::parse() {
1635db5e0786SFangrui Song   for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable()) {
16363837f427SRui Ueyama     keptComdats.push_back(
1637db5e0786SFangrui Song         s.second == Comdat::NoDeduplicate ||
1638db5e0786SFangrui Song         symtab->comdatGroups.try_emplace(CachedHashStringRef(s.first), this)
1639db5e0786SFangrui Song             .second);
1640db5e0786SFangrui Song   }
16413db70210SRafael Espindola 
1642dabac5feSFangrui Song   symbols.resize(obj->symbols().size());
1643bd448f01SFangrui Song   // Process defined symbols first. See the comment in
1644bd448f01SFangrui Song   // ObjFile<ELFT>::initializeSymbols.
1645bd448f01SFangrui Song   for (auto it : llvm::enumerate(obj->symbols()))
1646bd448f01SFangrui Song     if (!it.value().isUndefined()) {
1647bd448f01SFangrui Song       Symbol *&sym = symbols[it.index()];
1648bd448f01SFangrui Song       createBitcodeSymbol<ELFT>(sym, keptComdats, it.value(), *this);
1649bd448f01SFangrui Song     }
1650bd448f01SFangrui Song   for (auto it : llvm::enumerate(obj->symbols()))
1651bd448f01SFangrui Song     if (it.value().isUndefined()) {
1652dabac5feSFangrui Song       Symbol *&sym = symbols[it.index()];
1653dabac5feSFangrui Song       createBitcodeSymbol<ELFT>(sym, keptComdats, it.value(), *this);
1654dabac5feSFangrui Song     }
16551d16515fSBen Dunbobbin 
16563837f427SRui Ueyama   for (auto l : obj->getDependentLibraries())
16573837f427SRui Ueyama     addDependentLibrary(l, this);
16589f77ef0cSRafael Espindola }
16599f77ef0cSRafael Espindola 
parseLazy()16603a5fb573SFangrui Song void BitcodeFile::parseLazy() {
16610d749e13SFangrui Song   SymbolTable &symtab = *elf::symtab;
1662dabac5feSFangrui Song   symbols.resize(obj->symbols().size());
1663dabac5feSFangrui Song   for (auto it : llvm::enumerate(obj->symbols()))
1664977a1a52SFangrui Song     if (!it.value().isUndefined()) {
1665977a1a52SFangrui Song       auto *sym = symtab.insert(saver().save(it.value().getName()));
1666977a1a52SFangrui Song       sym->resolve(LazyObject{*this});
1667977a1a52SFangrui Song       symbols[it.index()] = sym;
1668977a1a52SFangrui Song     }
16693a5fb573SFangrui Song }
16703a5fb573SFangrui Song 
postParse()167188d66f6eSFangrui Song void BitcodeFile::postParse() {
167288d66f6eSFangrui Song   for (auto it : llvm::enumerate(obj->symbols())) {
167388d66f6eSFangrui Song     const Symbol &sym = *symbols[it.index()];
167488d66f6eSFangrui Song     const auto &objSym = it.value();
167588d66f6eSFangrui Song     if (sym.file == this || !sym.isDefined() || objSym.isUndefined() ||
167688d66f6eSFangrui Song         objSym.isCommon() || objSym.isWeak())
167788d66f6eSFangrui Song       continue;
167888d66f6eSFangrui Song     int c = objSym.getComdatIndex();
167988d66f6eSFangrui Song     if (c != -1 && !keptComdats[c])
168088d66f6eSFangrui Song       continue;
168188d66f6eSFangrui Song     reportDuplicate(sym, this, nullptr, 0);
168288d66f6eSFangrui Song   }
168388d66f6eSFangrui Song }
168488d66f6eSFangrui Song 
parse()16859a84f6b9SRafael Espindola void BinaryFile::parse() {
16863837f427SRui Ueyama   ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer());
16873837f427SRui Ueyama   auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
16883837f427SRui Ueyama                                      8, data, ".data");
16893837f427SRui Ueyama   sections.push_back(section);
1690093abab8SRafael Espindola 
1691c9d82b9eSRui Ueyama   // For each input file foo that is embedded to a result as a binary
1692c9d82b9eSRui Ueyama   // blob, we define _binary_foo_{start,end,size} symbols, so that
1693c9d82b9eSRui Ueyama   // user programs can access blobs by name. Non-alphanumeric
1694c9d82b9eSRui Ueyama   // characters in a filename are replaced with underscore.
16953837f427SRui Ueyama   std::string s = "_binary_" + mb.getBufferIdentifier().str();
16963837f427SRui Ueyama   for (size_t i = 0; i < s.size(); ++i)
16973837f427SRui Ueyama     if (!isAlnum(s[i]))
16983837f427SRui Ueyama       s[i] = '_';
1699c9d82b9eSRui Ueyama 
170083d59e05SAlexandre Ganea   llvm::StringSaver &saver = lld::saver();
170183d59e05SAlexandre Ganea 
170288d66f6eSFangrui Song   symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_start"),
170388d66f6eSFangrui Song                                        STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 0,
170488d66f6eSFangrui Song                                        0, section});
170588d66f6eSFangrui Song   symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_end"),
170688d66f6eSFangrui Song                                        STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
170788d66f6eSFangrui Song                                        data.size(), 0, section});
170888d66f6eSFangrui Song   symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_size"),
170988d66f6eSFangrui Song                                        STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
171088d66f6eSFangrui Song                                        data.size(), 0, nullptr});
1711a9424f39SMichael J. Spencer }
1712a9424f39SMichael J. Spencer 
createObjFile(MemoryBufferRef mb,StringRef archiveName,bool lazy)1713242316bcSFangrui Song ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName,
1714242316bcSFangrui Song                                 bool lazy) {
1715242316bcSFangrui Song   ELFFileBase *f;
17163837f427SRui Ueyama   switch (getELFKind(mb, archiveName)) {
1717330e52b0SRui Ueyama   case ELF32LEKind:
1718242316bcSFangrui Song     f = make<ObjFile<ELF32LE>>(mb, archiveName);
1719242316bcSFangrui Song     break;
1720330e52b0SRui Ueyama   case ELF32BEKind:
1721242316bcSFangrui Song     f = make<ObjFile<ELF32BE>>(mb, archiveName);
1722242316bcSFangrui Song     break;
1723330e52b0SRui Ueyama   case ELF64LEKind:
1724242316bcSFangrui Song     f = make<ObjFile<ELF64LE>>(mb, archiveName);
1725242316bcSFangrui Song     break;
1726330e52b0SRui Ueyama   case ELF64BEKind:
1727242316bcSFangrui Song     f = make<ObjFile<ELF64BE>>(mb, archiveName);
1728242316bcSFangrui Song     break;
1729330e52b0SRui Ueyama   default:
1730330e52b0SRui Ueyama     llvm_unreachable("getELFKind");
1731330e52b0SRui Ueyama   }
1732242316bcSFangrui Song   f->lazy = lazy;
1733242316bcSFangrui Song   return f;
1734808f2d3cSRafael Espindola }
1735808f2d3cSRafael Espindola 
parseLazy()17363a5fb573SFangrui Song template <class ELFT> void ObjFile<ELFT>::parseLazy() {
17371d285f2dSFangrui Song   const ArrayRef<typename ELFT::Sym> eSyms = this->getELFSyms<ELFT>();
17380d749e13SFangrui Song   SymbolTable &symtab = *elf::symtab;
173942548403SRui Ueyama 
17401d285f2dSFangrui Song   symbols.resize(eSyms.size());
17413837f427SRui Ueyama   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
17423837f427SRui Ueyama     if (eSyms[i].st_shndx != SHN_UNDEF)
17430d749e13SFangrui Song       symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this));
174442548403SRui Ueyama 
174542548403SRui Ueyama   // Replace existing symbols with LazyObject symbols.
174642548403SRui Ueyama   //
17471d285f2dSFangrui Song   // resolve() may trigger this->extract() if an existing symbol is an undefined
17481d285f2dSFangrui Song   // symbol. If that happens, this function has served its purpose, and we can
17491d285f2dSFangrui Song   // exit from the loop early.
17505e3403bdSFangrui Song   for (Symbol *sym : makeArrayRef(symbols).slice(firstGlobal))
17511d285f2dSFangrui Song     if (sym) {
1752977a1a52SFangrui Song       sym->resolve(LazyObject{*this});
17533a5fb573SFangrui Song       if (!lazy)
175442548403SRui Ueyama         return;
17557d476192SRui Ueyama     }
1756c13d858bSRui Ueyama }
1757c13d858bSRui Ueyama 
shouldExtractForCommon(StringRef name)17583a5fb573SFangrui Song bool InputFile::shouldExtractForCommon(StringRef name) {
1759e8fd49f2SFangrui Song   if (isa<BitcodeFile>(this))
17608f91f381SSean Fertile     return isBitcodeNonCommonDef(mb, name, archiveName);
17618f91f381SSean Fertile 
17628f91f381SSean Fertile   return isNonCommonDef(mb, name, archiveName);
17638f91f381SSean Fertile }
17648f91f381SSean Fertile 
replaceThinLTOSuffix(StringRef path)176507837b8fSFangrui Song std::string elf::replaceThinLTOSuffix(StringRef path) {
17663837f427SRui Ueyama   StringRef suffix = config->thinLTOObjectSuffixReplace.first;
17673837f427SRui Ueyama   StringRef repl = config->thinLTOObjectSuffixReplace.second;
1768f06d494fSRui Ueyama 
17693837f427SRui Ueyama   if (path.consume_back(suffix))
17703837f427SRui Ueyama     return (path + repl).str();
1771adcd0268SBenjamin Kramer   return std::string(path);
1772f06d494fSRui Ueyama }
1773f06d494fSRui Ueyama 
1774579c8df7SSam Clegg template void BitcodeFile::parse<ELF32LE>();
1775579c8df7SSam Clegg template void BitcodeFile::parse<ELF32BE>();
1776579c8df7SSam Clegg template void BitcodeFile::parse<ELF64LE>();
1777579c8df7SSam Clegg template void BitcodeFile::parse<ELF64BE>();
17784f952706SPeter Collingbourne 
177907837b8fSFangrui Song template class elf::ObjFile<ELF32LE>;
178007837b8fSFangrui Song template class elf::ObjFile<ELF32BE>;
178107837b8fSFangrui Song template class elf::ObjFile<ELF64LE>;
178207837b8fSFangrui Song template class elf::ObjFile<ELF64BE>;
1783f98d6d84SRafael Espindola 
1784cc1618e6SPeter Collingbourne template void SharedFile::parse<ELF32LE>();
1785cc1618e6SPeter Collingbourne template void SharedFile::parse<ELF32BE>();
1786cc1618e6SPeter Collingbourne template void SharedFile::parse<ELF64LE>();
1787cc1618e6SPeter Collingbourne template void SharedFile::parse<ELF64BE>();
1788