blob: f25ef793597f4aaf13caa4b95d6f505d4c47728a [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- InputFiles.cpp -----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "InputFiles.h"
Rafael Espindola9d13d042016-02-11 15:24:48 +000011#include "InputSection.h"
George Rimar67e3ff82016-08-12 19:56:57 +000012#include "LinkerScript.h"
Peter Collingbourne4f952702016-05-01 04:55:03 +000013#include "SymbolTable.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014#include "Symbols.h"
Peter Smith532bc982016-12-14 10:36:12 +000015#include "SyntheticSections.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000016#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000017#include "lld/Common/Memory.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018#include "llvm/ADT/STLExtras.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000019#include "llvm/CodeGen/Analysis.h"
Eugene Leviantb380b242016-10-26 11:07:09 +000020#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000021#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000022#include "llvm/IR/Module.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000023#include "llvm/LTO/LTO.h"
Michael J. Spencera9424f32016-09-09 22:08:04 +000024#include "llvm/MC/StringTableBuilder.h"
Eugene Leviantc4681202016-11-01 09:17:50 +000025#include "llvm/Object/ELFObjectFile.h"
Peter Smith57eb0462017-11-28 13:51:48 +000026#include "llvm/Support/ARMAttributeParser.h"
27#include "llvm/Support/ARMBuildAttributes.h"
Davide Italianoe02ba982016-09-08 21:18:38 +000028#include "llvm/Support/Path.h"
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000029#include "llvm/Support/TarWriter.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
Michael J. Spencer1b348a62015-09-04 22:28:10 +000032using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000033using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000034using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000035using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036
37using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000038using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000039
George Rimar696a7f92017-09-19 09:20:54 +000040std::vector<BinaryFile *> elf::BinaryFiles;
41std::vector<BitcodeFile *> elf::BitcodeFiles;
42std::vector<InputFile *> elf::ObjectFiles;
43std::vector<InputFile *> elf::SharedFiles;
44
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000045TarWriter *elf::Tar;
46
Rui Ueyama4e4e8662017-04-03 19:11:23 +000047InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
Rui Ueyama37e60a52017-03-30 21:13:00 +000048
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000049Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
Rui Ueyama875ae822017-07-20 18:17:55 +000050 // The --chroot option changes our virtual root directory.
51 // This is useful when you are dealing with files created by --reproduce.
52 if (!Config->Chroot.empty() && Path.startswith("/"))
53 Path = Saver.save(Config->Chroot + Path);
54
Rui Ueyamae6e206d2017-02-21 23:22:56 +000055 log(Path);
Rui Ueyama875ae822017-07-20 18:17:55 +000056
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000057 auto MBOrErr = MemoryBuffer::getFile(Path);
58 if (auto EC = MBOrErr.getError()) {
Rui Ueyamac8d3a832017-01-12 22:18:04 +000059 error("cannot open " + Path + ": " + EC.message());
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000060 return None;
61 }
Rui Ueyamae6e206d2017-02-21 23:22:56 +000062
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000063 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
64 MemoryBufferRef MBRef = MB->getMemBufferRef();
65 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
66
67 if (Tar)
68 Tar->append(relativeToRoot(Path), MBRef.getBuffer());
69 return MBRef;
70}
71
George Rimar82f0c422017-11-01 07:42:38 +000072template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
Rafael Espindola8b1afd52017-07-19 22:27:35 +000073 DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
74 const DWARFObject &Obj = Dwarf.getDWARFObj();
Paul Robinsonc60318a2017-06-29 16:52:29 +000075 DwarfLine.reset(new DWARFDebugLine);
Rafael Espindola8b1afd52017-07-19 22:27:35 +000076 DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
Paul Robinsonc60318a2017-06-29 16:52:29 +000077 Config->Wordsize);
Rui Ueyama7556f6b2016-11-02 18:42:13 +000078
Eugene Leviantb380b242016-10-26 11:07:09 +000079 // The second parameter is offset in .debug_line section
80 // for compilation unit (CU) of interest. We have only one
81 // CU (object file), so offset is always 0.
Paul Robinsone5400f82017-11-07 19:57:12 +000082 // FIXME: Provide the associated DWARFUnit if there is one. DWARF v5
83 // needs it in order to find indirect strings.
George Rimar82f0c422017-11-01 07:42:38 +000084 const DWARFDebugLine::LineTable *LT =
Paul Robinsone5400f82017-11-07 19:57:12 +000085 DwarfLine->getOrParseLineTable(LineData, 0, nullptr);
George Rimar82f0c422017-11-01 07:42:38 +000086
87 // Return if there is no debug information about CU available.
88 if (!Dwarf.getNumCompileUnits())
89 return;
90
91 // Loop over variable records and insert them to VariableLoc.
92 DWARFCompileUnit *CU = Dwarf.getCompileUnitAtIndex(0);
93 for (const auto &Entry : CU->dies()) {
94 DWARFDie Die(CU, &Entry);
95 // Skip all tags that are not variables.
96 if (Die.getTag() != dwarf::DW_TAG_variable)
97 continue;
98
99 // Skip if a local variable because we don't need them for generating error
100 // messages. In general, only non-local symbols can fail to be linked.
101 if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
102 continue;
103
104 // Get the source filename index for the variable.
105 unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
106 if (!LT->hasFileAtIndex(File))
107 continue;
108
109 // Get the line number on which the variable is declared.
110 unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
111
112 // Get the name of the variable and add the collected information to
113 // VariableLoc. Usually Name is non-empty, but it can be empty if the input
114 // object file lacks some debug info.
115 StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
116 if (!Name.empty())
117 VariableLoc.insert({Name, {File, Line}});
118 }
119}
120
121// Returns the pair of file name and line number describing location of data
122// object (variable, array, etc) definition.
123template <class ELFT>
124Optional<std::pair<std::string, unsigned>>
125ObjFile<ELFT>::getVariableLoc(StringRef Name) {
126 llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
127
128 // There is always only one CU so it's offset is 0.
129 const DWARFDebugLine::LineTable *LT = DwarfLine->getLineTable(0);
130 if (!LT)
131 return None;
132
133 // Return if we have no debug information about data object.
134 auto It = VariableLoc.find(Name);
135 if (It == VariableLoc.end())
136 return None;
137
138 // Take file name string from line table.
139 std::string FileName;
140 if (!LT->getFileNameByIndex(
141 It->second.first /* File */, nullptr,
142 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
143 return None;
144
145 return std::make_pair(FileName, It->second.second /*Line*/);
Eugene Leviantb380b242016-10-26 11:07:09 +0000146}
147
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000148// Returns source line information for a given offset
149// using DWARF debug info.
Eugene Leviantc4681202016-11-01 09:17:50 +0000150template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000151Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S,
152 uint64_t Offset) {
George Rimar82f0c422017-11-01 07:42:38 +0000153 llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
Eugene Leviantb380b242016-10-26 11:07:09 +0000154
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000155 // The offset to CU is 0.
156 const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
157 if (!Tbl)
Rui Ueyamab8760202017-03-30 19:13:47 +0000158 return None;
Eugene Leviantc4681202016-11-01 09:17:50 +0000159
160 // Use fake address calcuated by adding section file offset and offset in
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000161 // section. See comments for ObjectInfo class.
162 DILineInfo Info;
George Rimar92c88a42016-12-07 19:42:25 +0000163 Tbl->getFileLineInfoForAddress(
Rafael Espindola35ae65e2017-03-08 15:57:17 +0000164 S->getOffsetInFile() + Offset, nullptr,
George Rimar92c88a42016-12-07 19:42:25 +0000165 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000166 if (Info.Line == 0)
Rui Ueyamab8760202017-03-30 19:13:47 +0000167 return None;
168 return Info;
169}
170
171// Returns source line information for a given offset
172// using DWARF debug info.
173template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000174std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
Rui Ueyamab8760202017-03-30 19:13:47 +0000175 if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
176 return Info->FileName + ":" + std::to_string(Info->Line);
177 return "";
Eugene Leviantb380b242016-10-26 11:07:09 +0000178}
179
Rui Ueyama8a3ef952017-04-28 20:00:09 +0000180// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
Rui Ueyamace039262017-01-06 10:04:08 +0000181std::string lld::toString(const InputFile *F) {
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000182 if (!F)
Rui Ueyama8a3ef952017-04-28 20:00:09 +0000183 return "<internal>";
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000184
185 if (F->ToStringCache.empty()) {
186 if (F->ArchiveName.empty())
187 F->ToStringCache = F->getName();
188 else
189 F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
190 }
191 return F->ToStringCache;
Rafael Espindola78db5a92016-05-09 21:40:06 +0000192}
193
Rui Ueyama2022e812015-11-20 02:10:52 +0000194template <class ELFT>
Rafael Espindolae19abab2016-11-03 20:44:50 +0000195ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000196 if (ELFT::TargetEndianness == support::little)
197 EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
198 else
199 EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
200
Rafael Espindolae19abab2016-11-03 20:44:50 +0000201 EMachine = getObj().getHeader()->e_machine;
202 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000203}
204
205template <class ELFT>
Rafael Espindola3a8e4d92017-08-02 17:35:18 +0000206typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
207 return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
Davide Italiano6d328d32015-09-16 20:45:57 +0000208}
209
Rafael Espindola115f0f32015-11-03 14:13:40 +0000210template <class ELFT>
211uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamac0081632017-12-07 03:24:57 +0000212 return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000213}
214
Rafael Espindola6d18d382016-11-03 13:24:29 +0000215template <class ELFT>
Rafael Espindola21d8be92016-11-03 15:43:47 +0000216void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
217 const Elf_Shdr *Symtab) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000218 FirstNonLocal = Symtab->sh_info;
Rui Ueyamac0081632017-12-07 03:24:57 +0000219 ELFSyms = CHECK(getObj().symbols(Symtab), this);
Rafael Espindola3a8e4d92017-08-02 17:35:18 +0000220 if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000221 fatal(toString(this) + ": invalid sh_info in symbol table");
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000222
Rui Ueyamac0081632017-12-07 03:24:57 +0000223 StringTable =
224 CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000225}
226
227template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000228ObjFile<ELFT>::ObjFile(MemoryBufferRef M, StringRef ArchiveName)
Rui Ueyama42479e02017-08-19 00:13:54 +0000229 : ELFFileBase<ELFT>(Base::ObjKind, M) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000230 this->ArchiveName = ArchiveName;
231}
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000232
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000233template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
George Rimar70ecb822017-08-04 11:07:42 +0000234 if (this->Symbols.empty())
235 return {};
236 return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000237}
238
Rafael Espindola444576d2015-10-09 19:25:07 +0000239template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000240void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000241 // Read section and symbol tables.
Rafael Espindola73c3a362016-11-08 15:51:00 +0000242 initializeSections(ComdatGroups);
243 initializeSymbols();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000244}
245
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000246// Sections with SHT_GROUP and comdat bits define comdat section groups.
247// They are identified and deduplicated by group name. This function
248// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000249template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000250StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
251 const Elf_Shdr &Sec) {
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000252 // Group signatures are stored as symbol names in object files.
253 // sh_info contains a symbol index, so we fetch a symbol and read its name.
Rafael Espindola3a8e4d92017-08-02 17:35:18 +0000254 if (this->ELFSyms.empty())
Rui Ueyamabdc51502017-12-06 22:08:17 +0000255 this->initSymtab(
Rui Ueyamac0081632017-12-07 03:24:57 +0000256 Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000257
Rui Ueyamac0081632017-12-07 03:24:57 +0000258 const Elf_Sym *Sym =
259 CHECK(object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), this);
260 StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000261
262 // As a special case, if a symbol is a section symbol and has no name,
263 // we use a section name as a signature.
264 //
265 // Such SHT_GROUP sections are invalid from the perspective of the ELF
266 // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
267 // older produce such sections as outputs for the -r option, so we need
268 // a bug-compatibility.
269 if (Signature.empty() && Sym->getType() == STT_SECTION)
270 return getSectionName(Sec);
271 return Signature;
Rafael Espindola444576d2015-10-09 19:25:07 +0000272}
273
274template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000275ArrayRef<typename ObjFile<ELFT>::Elf_Word>
276ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000277 const ELFFile<ELFT> &Obj = this->getObj();
Rui Ueyamac0081632017-12-07 03:24:57 +0000278 ArrayRef<Elf_Word> Entries =
279 CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
Rafael Espindola444576d2015-10-09 19:25:07 +0000280 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000281 fatal(toString(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000282 return Entries.slice(1);
283}
284
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000285template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000286 // We don't merge sections if -O0 (default is -O1). This makes sometimes
287 // the linker significantly faster, although the output will be bigger.
288 if (Config->Optimize == 0)
289 return false;
290
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000291 // A mergeable section with size 0 is useless because they don't have
292 // any data to merge. A mergeable string section with size 0 can be
293 // argued as invalid because it doesn't end with a null character.
294 // We'll avoid a mess by handling them as if they were non-mergeable.
295 if (Sec.sh_size == 0)
296 return false;
297
Rui Ueyamac75ef852016-09-21 03:22:18 +0000298 // Check for sh_entsize. The ELF spec is not clear about the zero
299 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
300 // the section does not hold a table of fixed-size entries". We know
301 // that Rust 1.13 produces a string mergeable section with a zero
302 // sh_entsize. Here we just accept it rather than being picky about it.
Rui Ueyama9cc84382017-02-24 19:52:52 +0000303 uint64_t EntSize = Sec.sh_entsize;
Rui Ueyamac75ef852016-09-21 03:22:18 +0000304 if (EntSize == 0)
305 return false;
306 if (Sec.sh_size % EntSize)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000307 fatal(toString(this) +
Rui Ueyamac75ef852016-09-21 03:22:18 +0000308 ": SHF_MERGE section size must be a multiple of sh_entsize");
309
Rui Ueyama9cc84382017-02-24 19:52:52 +0000310 uint64_t Flags = Sec.sh_flags;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000311 if (!(Flags & SHF_MERGE))
312 return false;
313 if (Flags & SHF_WRITE)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000314 fatal(toString(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000315
Rafael Espindola3f0b5752017-11-15 17:31:27 +0000316 return true;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000317}
318
319template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000320void ObjFile<ELFT>::initializeSections(
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000321 DenseSet<CachedHashStringRef> &ComdatGroups) {
Rui Ueyama240b9512017-05-26 02:17:30 +0000322 const ELFFile<ELFT> &Obj = this->getObj();
323
Rui Ueyamac0081632017-12-07 03:24:57 +0000324 ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
Rafael Espindola235d82c2016-11-02 14:42:20 +0000325 uint64_t Size = ObjSections.size();
George Rimar3f7c3df2017-03-21 08:44:25 +0000326 this->Sections.resize(Size);
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000327 this->SectionStringTable =
Rui Ueyamac0081632017-12-07 03:24:57 +0000328 CHECK(Obj.getSectionStringTable(ObjSections), this);
Rui Ueyama240b9512017-05-26 02:17:30 +0000329
330 for (size_t I = 0, E = ObjSections.size(); I < E; I++) {
George Rimar3f7c3df2017-03-21 08:44:25 +0000331 if (this->Sections[I] == &InputSection::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000332 continue;
Rui Ueyama240b9512017-05-26 02:17:30 +0000333 const Elf_Shdr &Sec = ObjSections[I];
Rafael Espindola444576d2015-10-09 19:25:07 +0000334
Rui Ueyamaaf9793d2016-10-04 16:47:49 +0000335 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
336 // if -r is given, we'll let the final link discard such sections.
337 // This is compatible with GNU.
338 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
George Rimar3f7c3df2017-03-21 08:44:25 +0000339 this->Sections[I] = &InputSection::Discarded;
Eugene Leviant27be5422016-09-28 08:42:02 +0000340 continue;
341 }
342
Rafael Espindolacde25132015-08-13 14:45:44 +0000343 switch (Sec.sh_type) {
George Rimar3b189d12017-05-29 08:37:50 +0000344 case SHT_GROUP: {
Rui Ueyamaa1ba8592017-06-09 02:42:20 +0000345 // De-duplicate section groups by their signatures.
346 StringRef Signature = getShtGroupSignature(ObjSections, Sec);
347 bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
348 this->Sections[I] = &InputSection::Discarded;
George Rimar3b189d12017-05-29 08:37:50 +0000349
Rui Ueyamaa1ba8592017-06-09 02:42:20 +0000350 // If it is a new section group, we want to keep group members.
351 // Group leader sections, which contain indices of group members, are
352 // discarded because they are useless beyond this point. The only
353 // exception is the -r option because in order to produce re-linkable
354 // object files, we want to pass through basically everything.
355 if (IsNew) {
356 if (Config->Relocatable)
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000357 this->Sections[I] = createInputSection(Sec);
Rui Ueyamaa1ba8592017-06-09 02:42:20 +0000358 continue;
359 }
360
361 // Otherwise, discard group members.
Rui Ueyama33b3f212016-01-06 20:30:02 +0000362 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000363 if (SecIndex >= Size)
George Rimar3f7c3df2017-03-21 08:44:25 +0000364 fatal(toString(this) +
365 ": invalid section index in group: " + Twine(SecIndex));
366 this->Sections[SecIndex] = &InputSection::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000367 }
368 break;
George Rimar3b189d12017-05-29 08:37:50 +0000369 }
Rafael Espindolacde25132015-08-13 14:45:44 +0000370 case SHT_SYMTAB:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000371 this->initSymtab(ObjSections, &Sec);
Rafael Espindolacde25132015-08-13 14:45:44 +0000372 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000373 case SHT_SYMTAB_SHNDX:
Rui Ueyamac0081632017-12-07 03:24:57 +0000374 this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
Rafael Espindola20348222015-08-24 21:43:25 +0000375 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000376 case SHT_STRTAB:
377 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000378 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000379 default:
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000380 this->Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000381 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000382
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000383 // .ARM.exidx sections have a reverse dependency on the InputSection they
384 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
385 if (Sec.sh_flags & SHF_LINK_ORDER) {
George Rimarf56cadd2017-03-21 08:57:13 +0000386 if (Sec.sh_link >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000387 fatal(toString(this) + ": invalid sh_link index: " +
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000388 Twine(Sec.sh_link));
George Rimar3f7c3df2017-03-21 08:44:25 +0000389 this->Sections[Sec.sh_link]->DependentSections.push_back(
Rui Ueyama05433432017-10-11 04:01:13 +0000390 cast<InputSection>(this->Sections[I]));
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000391 }
Peter Smith07606052016-10-10 10:10:27 +0000392 }
393}
394
Peter Smith57eb0462017-11-28 13:51:48 +0000395// The ARM support in lld makes some use of instructions that are not available
396// on all ARM architectures. Namely:
397// - Use of BLX instruction for interworking between ARM and Thumb state.
398// - Use of the extended Thumb branch encoding in relocation.
399// - Use of the MOVT/MOVW instructions in Thumb Thunks.
400// The ARM Attributes section contains information about the architecture chosen
401// at compile time. We follow the convention that if at least one input object
402// is compiled with an architecture that supports these features then lld is
403// permitted to use them.
404static void updateSupportedARMFeatures(const ARMAttributeParser &Attributes) {
405 if (!Attributes.hasAttribute(ARMBuildAttrs::CPU_arch))
406 return;
407 auto Arch = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
408 switch (Arch) {
409 case ARMBuildAttrs::Pre_v4:
410 case ARMBuildAttrs::v4:
411 case ARMBuildAttrs::v4T:
412 // Architectures prior to v5 do not support BLX instruction
413 break;
414 case ARMBuildAttrs::v5T:
415 case ARMBuildAttrs::v5TE:
416 case ARMBuildAttrs::v5TEJ:
417 case ARMBuildAttrs::v6:
418 case ARMBuildAttrs::v6KZ:
419 case ARMBuildAttrs::v6K:
420 Config->ARMHasBlx = true;
421 // Architectures used in pre-Cortex processors do not support
422 // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
423 // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
424 break;
425 default:
426 // All other Architectures have BLX and extended branch encoding
427 Config->ARMHasBlx = true;
428 Config->ARMJ1J2BranchEncoding = true;
429 if (Arch != ARMBuildAttrs::v6_M && Arch != ARMBuildAttrs::v6S_M)
430 // All Architectures used in Cortex processors with the exception
431 // of v6-M and v6S-M have the MOVT and MOVW instructions.
432 Config->ARMHasMovtMovw = true;
433 break;
434 }
435}
436
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000437template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000438InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000439 uint32_t Idx = Sec.sh_info;
George Rimar3f7c3df2017-03-21 08:44:25 +0000440 if (Idx >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000441 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
George Rimar3f7c3df2017-03-21 08:44:25 +0000442 InputSectionBase *Target = this->Sections[Idx];
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000443
444 // Strictly speaking, a relocation section must be included in the
445 // group of the section it relocates. However, LLVM 3.3 and earlier
446 // would fail to do so, so we gracefully handle that case.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000447 if (Target == &InputSection::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000448 return nullptr;
449
450 if (!Target)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000451 fatal(toString(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000452 return Target;
453}
454
Rui Ueyama92a8d792017-05-01 20:49:09 +0000455// Create a regular InputSection class that has the same contents
456// as a given section.
457InputSectionBase *toRegularSection(MergeInputSection *Sec) {
458 auto *Ret = make<InputSection>(Sec->Flags, Sec->Type, Sec->Alignment,
459 Sec->Data, Sec->Name);
460 Ret->File = Sec->File;
461 return Ret;
462}
463
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000464template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000465InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000466 StringRef Name = getSectionName(Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000467
Rafael Espindola042a3f22016-09-08 14:06:08 +0000468 switch (Sec.sh_type) {
Peter Smith57eb0462017-11-28 13:51:48 +0000469 case SHT_ARM_ATTRIBUTES: {
Peter Smith8fca2e12017-11-29 10:20:46 +0000470 if (Config->EMachine != EM_ARM)
471 break;
Peter Smith57eb0462017-11-28 13:51:48 +0000472 ARMAttributeParser Attributes;
473 ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
474 Attributes.Parse(Contents, /*isLittle*/Config->EKind == ELF32LEKind);
475 updateSupportedARMFeatures(Attributes);
476 // FIXME: Retain the first attribute section we see. The eglibc ARM
477 // dynamic loaders require the presence of an attribute section for dlopen
478 // to work. In a full implementation we would merge all attribute sections.
Rafael Espindola895aea62017-05-11 22:02:41 +0000479 if (InX::ARMAttributes == nullptr) {
480 InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
481 return InX::ARMAttributes;
Peter Smith532bc982016-12-14 10:36:12 +0000482 }
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000483 return &InputSection::Discarded;
Peter Smith57eb0462017-11-28 13:51:48 +0000484 }
Rafael Espindola042a3f22016-09-08 14:06:08 +0000485 case SHT_RELA:
486 case SHT_REL: {
George Rimaree6f22c2017-02-14 16:42:38 +0000487 // Find the relocation target section and associate this
488 // section with it. Target can be discarded, for example
489 // if it is a duplicated member of SHT_GROUP section, we
490 // do not create or proccess relocatable sections then.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000491 InputSectionBase *Target = getRelocTarget(Sec);
George Rimaree6f22c2017-02-14 16:42:38 +0000492 if (!Target)
493 return nullptr;
494
Rafael Espindola042a3f22016-09-08 14:06:08 +0000495 // This section contains relocation information.
496 // If -r is given, we do not interpret or apply relocation
497 // but just copy relocation sections to output.
498 if (Config->Relocatable)
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000499 return make<InputSection>(this, &Sec, Name);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000500
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000501 if (Target->FirstRelocation)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000502 fatal(toString(this) +
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000503 ": multiple relocation sections to one section are not supported");
Rui Ueyama92a8d792017-05-01 20:49:09 +0000504
505 // Mergeable sections with relocations are tricky because relocations
506 // need to be taken into account when comparing section contents for
Rui Ueyamac3443132017-05-02 21:16:06 +0000507 // merging. It's not worth supporting such mergeable sections because
Rui Ueyamadfb1e2a2017-05-02 02:58:04 +0000508 // they are rare and it'd complicates the internal design (we usually
509 // have to determine if two sections are mergeable early in the link
510 // process much before applying relocations). We simply handle mergeable
511 // sections with relocations as non-mergeable.
Rui Ueyama92a8d792017-05-01 20:49:09 +0000512 if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
513 Target = toRegularSection(MS);
514 this->Sections[Sec.sh_info] = Target;
Davide Italianod7656382017-04-29 01:24:34 +0000515 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000516
517 size_t NumRelocations;
518 if (Sec.sh_type == SHT_RELA) {
Rui Ueyamac0081632017-12-07 03:24:57 +0000519 ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000520 Target->FirstRelocation = Rels.begin();
521 NumRelocations = Rels.size();
522 Target->AreRelocsRela = true;
523 } else {
Rui Ueyamac0081632017-12-07 03:24:57 +0000524 ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000525 Target->FirstRelocation = Rels.begin();
526 NumRelocations = Rels.size();
527 Target->AreRelocsRela = false;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000528 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000529 assert(isUInt<31>(NumRelocations));
530 Target->NumRelocations = NumRelocations;
George Rimar82bd8be2017-02-08 16:18:10 +0000531
532 // Relocation sections processed by the linker are usually removed
533 // from the output, so returning `nullptr` for the normal case.
534 // However, if -emit-relocs is given, we need to leave them in the output.
535 // (Some post link analysis tools need this information.)
George Rimar858a6592017-02-17 19:46:47 +0000536 if (Config->EmitRelocs) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000537 InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
George Rimar858a6592017-02-17 19:46:47 +0000538 // We will not emit relocation section if target was discarded.
539 Target->DependentSections.push_back(RelocSec);
540 return RelocSec;
541 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000542 return nullptr;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000543 }
544 }
545
Rui Ueyama06332732017-02-23 07:06:43 +0000546 // The GNU linker uses .note.GNU-stack section as a marker indicating
547 // that the code in the object file does not expect that the stack is
548 // executable (in terms of NX bit). If all input files have the marker,
549 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
Rui Ueyama65efe352017-02-23 07:15:46 +0000550 // make the stack non-executable. Most object files have this section as
551 // of 2017.
Rui Ueyama06332732017-02-23 07:06:43 +0000552 //
553 // But making the stack non-executable is a norm today for security
Rui Ueyama65efe352017-02-23 07:15:46 +0000554 // reasons. Failure to do so may result in a serious security issue.
555 // Therefore, we make LLD always add PT_GNU_STACK unless it is
Rui Ueyama06332732017-02-23 07:06:43 +0000556 // explicitly told to do otherwise (by -z execstack). Because the stack
557 // executable-ness is controlled solely by command line options,
558 // .note.GNU-stack sections are simply ignored.
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000559 if (Name == ".note.GNU-stack")
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000560 return &InputSection::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000561
Rui Ueyamac1a0ac22017-02-23 07:35:11 +0000562 // Split stacks is a feature to support a discontiguous stack. At least
563 // as of 2017, it seems that the feature is not being used widely.
564 // Only GNU gold supports that. We don't. For the details about that,
565 // see https://gcc.gnu.org/wiki/SplitStacks
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000566 if (Name == ".note.GNU-split-stack") {
Rui Ueyamab7f39b02017-02-23 07:35:30 +0000567 error(toString(this) +
568 ": object file compiled with -fsplit-stack is not supported");
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000569 return &InputSection::Discarded;
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000570 }
571
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000572 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
573 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
574 // sections. Drop those sections to avoid duplicate symbol errors.
575 // FIXME: This is glibc PR20543, we should remove this hack once that has been
576 // fixed for a while.
577 if (Name.startswith(".gnu.linkonce."))
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000578 return &InputSection::Discarded;
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000579
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000580 // The linker merges EH (exception handling) frames and creates a
581 // .eh_frame_hdr section for runtime. So we handle them with a special
582 // class. For relocatable outputs, they are just passed through.
583 if (Name == ".eh_frame" && !Config->Relocatable)
Rafael Espindola5c02b742017-03-06 21:17:18 +0000584 return make<EhInputSection>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000585
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000586 if (shouldMerge(Sec))
Rafael Espindola6119b862017-03-06 20:23:56 +0000587 return make<MergeInputSection>(this, &Sec, Name);
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000588 return make<InputSection>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000589}
590
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000591template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000592StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
Rui Ueyamac0081632017-12-07 03:24:57 +0000593 return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000594}
595
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000596template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
George Rimar70ecb822017-08-04 11:07:42 +0000597 this->Symbols.reserve(this->ELFSyms.size());
Rafael Espindola3a8e4d92017-08-02 17:35:18 +0000598 for (const Elf_Sym &Sym : this->ELFSyms)
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000599 this->Symbols.push_back(createSymbol(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000600}
601
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000602template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000603 int Binding = Sym->getBinding();
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000604
Rui Ueyamad44a81c2017-12-13 22:53:59 +0000605 uint32_t SecIdx = this->getSectionIndex(*Sym);
606 if (SecIdx >= this->Sections.size())
607 fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
608
609 InputSectionBase *Sec = this->Sections[SecIdx];
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000610 uint8_t StOther = Sym->st_other;
611 uint8_t Type = Sym->getType();
Rui Ueyama9cc84382017-02-24 19:52:52 +0000612 uint64_t Value = Sym->st_value;
613 uint64_t Size = Sym->st_size;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000614
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000615 if (Binding == STB_LOCAL) {
Eugene Leviantb380b242016-10-26 11:07:09 +0000616 if (Sym->getType() == STT_FILE)
Rui Ueyamac0081632017-12-07 03:24:57 +0000617 SourceFile = CHECK(Sym->getName(this->StringTable), this);
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000618
619 if (this->StringTable.size() <= Sym->st_name)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000620 fatal(toString(this) + ": invalid symbol name offset");
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000621
Rui Ueyama84e65a72016-11-29 19:11:39 +0000622 StringRefZ Name = this->StringTable.data() + Sym->st_name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000623 if (Sym->st_shndx == SHN_UNDEF)
Rafael Espindoladfebd362017-11-29 22:47:35 +0000624 return make<Undefined>(this, Name, Binding, StOther, Type);
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000625
Rafael Espindoladfebd362017-11-29 22:47:35 +0000626 return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000627 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000628
Rui Ueyamac0081632017-12-07 03:24:57 +0000629 StringRef Name = CHECK(Sym->getName(this->StringTable), this);
Rafael Espindola20348222015-08-24 21:43:25 +0000630
Rafael Espindola4cda5812015-10-16 15:29:48 +0000631 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000632 case SHN_UNDEF:
Rafael Espindolabec37652017-11-17 01:37:50 +0000633 return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000634 /*CanOmitFromDynSym=*/false, this);
Rafael Espindola51d46902015-08-28 21:26:51 +0000635 case SHN_COMMON:
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000636 if (Value == 0 || Value >= UINT32_MAX)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000637 fatal(toString(this) + ": common symbol '" + Name +
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000638 "' has invalid alignment: " + Twine(Value));
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000639 return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this);
Rafael Espindola51d46902015-08-28 21:26:51 +0000640 }
Rafael Espindola20348222015-08-24 21:43:25 +0000641
Rafael Espindola67d72c02016-03-11 12:06:30 +0000642 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000643 default:
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000644 fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000645 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000646 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000647 case STB_GNU_UNIQUE:
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000648 if (Sec == &InputSection::Discarded)
Rafael Espindolabec37652017-11-17 01:37:50 +0000649 return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000650 /*CanOmitFromDynSym=*/false, this);
651 return Symtab->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding,
652 Sec, this);
Rafael Espindola444576d2015-10-09 19:25:07 +0000653 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000654}
655
Rui Ueyamafd7deda2017-05-03 21:03:08 +0000656ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
657 : InputFile(ArchiveKind, File->getMemoryBufferRef()),
658 File(std::move(File)) {}
659
Peter Collingbourne4f952702016-05-01 04:55:03 +0000660template <class ELFT> void ArchiveFile::parse() {
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000661 Symbols.reserve(File->getNumberOfSymbols());
Rui Ueyamafd7deda2017-05-03 21:03:08 +0000662 for (const Archive::Symbol &Sym : File->symbols())
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000663 Symbols.push_back(Symtab->addLazyArchive<ELFT>(Sym.getName(), this, Sym));
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000664}
665
666// Returns a buffer pointing to a member file containing a given symbol.
Davide Italianobcdd6c62016-10-12 19:35:54 +0000667std::pair<MemoryBufferRef, uint64_t>
668ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000669 Archive::Child C =
Rui Ueyamabdc51502017-12-06 22:08:17 +0000670 CHECK(Sym->getMember(), toString(this) +
Rui Ueyama37e60a52017-03-30 21:13:00 +0000671 ": could not get the member for symbol " +
672 Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000673
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000674 if (!Seen.insert(C.getChildOffset()).second)
Davide Italianobcdd6c62016-10-12 19:35:54 +0000675 return {MemoryBufferRef(), 0};
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000676
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000677 MemoryBufferRef Ret =
Rui Ueyamabdc51502017-12-06 22:08:17 +0000678 CHECK(C.getMemoryBufferRef(),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000679 toString(this) +
Rui Ueyama37e60a52017-03-30 21:13:00 +0000680 ": could not get the buffer for the member defining symbol " +
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000681 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000682
Rui Ueyamaec1c75e2017-01-09 01:42:02 +0000683 if (C.getParent()->isThin() && Tar)
Rui Ueyamac0081632017-12-07 03:24:57 +0000684 Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), Ret.getBuffer());
Davide Italianobcdd6c62016-10-12 19:35:54 +0000685 if (C.getParent()->isThin())
686 return {Ret, 0};
687 return {Ret, C.getChildOffset()};
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000688}
689
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000690template <class ELFT>
Rafael Espindola3460cdd2017-04-24 21:44:20 +0000691SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
692 : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName),
Rafael Espindolade563432017-11-22 17:50:42 +0000693 IsNeeded(!Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000694
Rui Ueyama7c713312016-01-06 01:56:36 +0000695// Partially parse the shared object file so that we can call
696// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000697template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rafael Espindolac8b15812015-10-01 15:47:50 +0000698 const Elf_Shdr *DynamicSec = nullptr;
Rafael Espindolae19abab2016-11-03 20:44:50 +0000699 const ELFFile<ELFT> Obj = this->getObj();
Rui Ueyamac0081632017-12-07 03:24:57 +0000700 ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
Rui Ueyama3233d3e2017-04-13 00:23:32 +0000701
702 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
Rafael Espindola84d6a172016-11-03 12:21:00 +0000703 for (const Elf_Shdr &Sec : Sections) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000704 switch (Sec.sh_type) {
705 default:
706 continue;
707 case SHT_DYNSYM:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000708 this->initSymtab(Sections, &Sec);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000709 break;
710 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000711 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000712 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000713 case SHT_SYMTAB_SHNDX:
Rui Ueyamac0081632017-12-07 03:24:57 +0000714 this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000715 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000716 case SHT_GNU_versym:
717 this->VersymSec = &Sec;
718 break;
719 case SHT_GNU_verdef:
720 this->VerdefSec = &Sec;
721 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000722 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000723 }
724
Rafael Espindola3a8e4d92017-08-02 17:35:18 +0000725 if (this->VersymSec && this->ELFSyms.empty())
George Rimarbcba39a2016-11-02 10:16:25 +0000726 error("SHT_GNU_versym should be associated with symbol table");
727
Rui Ueyama3233d3e2017-04-13 00:23:32 +0000728 // Search for a DT_SONAME tag to initialize this->SoName.
Rui Ueyama361d8b92015-10-12 15:49:02 +0000729 if (!DynamicSec)
730 return;
George Rimar53cf2a82016-10-07 09:01:04 +0000731 ArrayRef<Elf_Dyn> Arr =
Rui Ueyamac0081632017-12-07 03:24:57 +0000732 CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
George Rimar53cf2a82016-10-07 09:01:04 +0000733 for (const Elf_Dyn &Dyn : Arr) {
Rui Ueyama361d8b92015-10-12 15:49:02 +0000734 if (Dyn.d_tag == DT_SONAME) {
Rui Ueyama9cc84382017-02-24 19:52:52 +0000735 uint64_t Val = Dyn.getVal();
Rui Ueyama361d8b92015-10-12 15:49:02 +0000736 if (Val >= this->StringTable.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000737 fatal(toString(this) + ": invalid DT_SONAME entry");
Rui Ueyamae5ad2982017-04-26 23:00:32 +0000738 SoName = this->StringTable.data() + Val;
Rui Ueyama361d8b92015-10-12 15:49:02 +0000739 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000740 }
741 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000742}
Rafael Espindola18173d42015-09-08 15:50:05 +0000743
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000744// Parse the version definitions in the object file if present. Returns a vector
745// whose nth element contains a pointer to the Elf_Verdef for version identifier
746// n. Version identifiers that are not definitions map to nullptr. The array
747// always has at least length 1.
748template <class ELFT>
749std::vector<const typename ELFT::Verdef *>
750SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
751 std::vector<const Elf_Verdef *> Verdefs(1);
752 // We only need to process symbol versions for this DSO if it has both a
753 // versym and a verdef section, which indicates that the DSO contains symbol
754 // version definitions.
755 if (!VersymSec || !VerdefSec)
756 return Verdefs;
757
758 // The location of the first global versym entry.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000759 const char *Base = this->MB.getBuffer().data();
760 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000761 this->FirstNonLocal;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000762
763 // We cannot determine the largest verdef identifier without inspecting
764 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
765 // sequentially starting from 1, so we predict that the largest identifier
766 // will be VerdefCount.
767 unsigned VerdefCount = VerdefSec->sh_info;
768 Verdefs.resize(VerdefCount + 1);
769
770 // Build the Verdefs array by following the chain of Elf_Verdef objects
771 // from the start of the .gnu.version_d section.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000772 const char *Verdef = Base + VerdefSec->sh_offset;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000773 for (unsigned I = 0; I != VerdefCount; ++I) {
774 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
775 Verdef += CurVerdef->vd_next;
776 unsigned VerdefIndex = CurVerdef->vd_ndx;
777 if (Verdefs.size() <= VerdefIndex)
778 Verdefs.resize(VerdefIndex + 1);
779 Verdefs[VerdefIndex] = CurVerdef;
780 }
781
782 return Verdefs;
783}
784
Rui Ueyama7c713312016-01-06 01:56:36 +0000785// Fully parse the shared object file. This must be called after parseSoName().
786template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000787 // Create mapping from version identifiers to Elf_Verdef entries.
788 const Elf_Versym *Versym = nullptr;
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000789 Verdefs = parseVerdefs(Versym);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000790
Rui Ueyamac0081632017-12-07 03:24:57 +0000791 ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000792
793 // Add symbols to the symbol table.
Rafael Espindola3a8e4d92017-08-02 17:35:18 +0000794 Elf_Sym_Range Syms = this->getGlobalELFSyms();
Rafael Espindola18173d42015-09-08 15:50:05 +0000795 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000796 unsigned VersymIndex = 0;
797 if (Versym) {
798 VersymIndex = Versym->vs_index;
799 ++Versym;
800 }
Rafael Espindola2756e042017-01-06 22:30:35 +0000801 bool Hidden = VersymIndex & VERSYM_HIDDEN;
802 VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000803
Rui Ueyamac0081632017-12-07 03:24:57 +0000804 StringRef Name = CHECK(Sym.getName(this->StringTable), this);
Rafael Espindola18da0e52016-04-29 16:23:31 +0000805 if (Sym.isUndefined()) {
806 Undefs.push_back(Name);
807 continue;
808 }
809
Rui Ueyamafbe68a32017-12-15 00:01:33 +0000810 if (Sym.getBinding() == STB_LOCAL) {
Rui Ueyama29ceba72017-12-15 00:07:15 +0000811 warn("found local symbol '" + Name +
Rui Ueyamafbe68a32017-12-15 00:01:33 +0000812 "' in global part of symbol table in file " + toString(this));
813 continue;
814 }
815
Rafael Espindola2756e042017-01-06 22:30:35 +0000816 // Ignore local symbols.
817 if (Versym && VersymIndex == VER_NDX_LOCAL)
818 continue;
Rui Ueyama1392e842017-10-28 20:16:11 +0000819 const Elf_Verdef *Ver = nullptr;
Alexander Richardson57f08972017-10-05 23:28:29 +0000820 if (VersymIndex != VER_NDX_GLOBAL) {
821 if (VersymIndex >= Verdefs.size()) {
822 error("corrupt input file: version definition index " +
823 Twine(VersymIndex) + " for symbol " + Name +
Alexander Richardsonb9aa9a52017-10-05 23:28:34 +0000824 " is out of bounds\n>>> defined in " + toString(this));
Alexander Richardson57f08972017-10-05 23:28:29 +0000825 continue;
826 }
Rui Ueyama1392e842017-10-28 20:16:11 +0000827 Ver = Verdefs[VersymIndex];
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000828 } else {
829 VersymIndex = 0;
Alexander Richardson57f08972017-10-05 23:28:29 +0000830 }
Rafael Espindola2756e042017-01-06 22:30:35 +0000831
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000832 // We do not usually care about alignments of data in shared object
833 // files because the loader takes care of it. However, if we promote a
834 // DSO symbol to point to .bss due to copy relocation, we need to keep
835 // the original alignment requirements. We infer it here.
Rui Ueyamad5e2e832017-11-07 00:12:05 +0000836 uint64_t Alignment = 1;
Rui Ueyamab6f4e722017-10-29 16:49:42 +0000837 if (Sym.st_value)
838 Alignment = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000839 if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) {
Rui Ueyamad5e2e832017-11-07 00:12:05 +0000840 uint64_t SecAlign = Sections[Sym.st_shndx].sh_addralign;
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000841 Alignment = std::min(Alignment, SecAlign);
842 }
Rui Ueyamad5e2e832017-11-07 00:12:05 +0000843 if (Alignment > UINT32_MAX)
844 error(toString(this) + ": alignment too large: " + Name);
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000845
Rafael Espindola2756e042017-01-06 22:30:35 +0000846 if (!Hidden)
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000847 Symtab->addShared(Name, this, Sym, Alignment, VersymIndex);
Rafael Espindola2756e042017-01-06 22:30:35 +0000848
849 // Also add the symbol with the versioned name to handle undefined symbols
850 // with explicit versions.
Rui Ueyama1392e842017-10-28 20:16:11 +0000851 if (Ver) {
852 StringRef VerName = this->StringTable.data() + Ver->getAux()->vda_name;
Rui Ueyama63d48e52017-04-27 04:01:14 +0000853 Name = Saver.save(Name + "@" + VerName);
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000854 Symtab->addShared(Name, this, Sym, Alignment, VersymIndex);
Rafael Espindola2756e042017-01-06 22:30:35 +0000855 }
Rafael Espindola18173d42015-09-08 15:50:05 +0000856 }
857}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000858
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000859static ELFKind getBitcodeELFKind(const Triple &T) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000860 if (T.isLittleEndian())
861 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
862 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000863}
864
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000865static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000866 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000867 case Triple::aarch64:
868 return EM_AARCH64;
869 case Triple::arm:
Sean Silva1d961852017-02-28 03:00:48 +0000870 case Triple::thumb:
Rui Ueyama523744d2016-07-07 02:46:30 +0000871 return EM_ARM;
Leslie Zhai49ba7952017-06-15 02:27:50 +0000872 case Triple::avr:
873 return EM_AVR;
Rui Ueyama523744d2016-07-07 02:46:30 +0000874 case Triple::mips:
875 case Triple::mipsel:
876 case Triple::mips64:
877 case Triple::mips64el:
878 return EM_MIPS;
879 case Triple::ppc:
880 return EM_PPC;
881 case Triple::ppc64:
882 return EM_PPC64;
883 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000884 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000885 case Triple::x86_64:
886 return EM_X86_64;
887 default:
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000888 fatal(Path + ": could not infer e_machine from bitcode target triple " +
889 T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000890 }
891}
892
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000893BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
894 uint64_t OffsetInArchive)
895 : InputFile(BitcodeKind, MB) {
896 this->ArchiveName = ArchiveName;
897
898 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
899 // (the fully resolved path of the archive) + member name + offset of the
900 // member in the archive.
901 // ThinLTO uses the MemoryBufferRef identifier to access its internal
902 // data structures and if two archives define two members with the same name,
903 // this causes a collision which result in only one of the objects being
904 // taken into consideration at LTO time (which very likely causes undefined
905 // symbols later in the link stage).
906 MemoryBufferRef MBRef(MB.getBuffer(),
907 Saver.save(ArchiveName + MB.getBufferIdentifier() +
908 utostr(OffsetInArchive)));
Rui Ueyamac0081632017-12-07 03:24:57 +0000909 Obj = CHECK(lto::InputFile::create(MBRef), this);
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000910
911 Triple T(Obj->getTargetTriple());
912 EKind = getBitcodeELFKind(T);
913 EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
Davide Italiano60976ba2016-06-29 06:12:39 +0000914}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000915
Davide Italiano786d8e32016-09-29 00:40:08 +0000916static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
917 switch (GvVisibility) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000918 case GlobalValue::DefaultVisibility:
919 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000920 case GlobalValue::HiddenVisibility:
921 return STV_HIDDEN;
922 case GlobalValue::ProtectedVisibility:
923 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000924 }
George Rimar777f9632016-03-12 08:31:34 +0000925 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000926}
927
Peter Collingbourne4f952702016-05-01 04:55:03 +0000928template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000929static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
930 const lto::InputFile::Symbol &ObjSym,
931 BitcodeFile *F) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000932 StringRef NameRef = Saver.save(ObjSym.getName());
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000933 uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000934
Davide Italiano786d8e32016-09-29 00:40:08 +0000935 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
936 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
937 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000938
Peter Collingbourne7b30f162017-03-31 04:47:07 +0000939 int C = ObjSym.getComdatIndex();
Rafael Espindola3db70212016-10-25 12:02:31 +0000940 if (C != -1 && !KeptComdats[C])
Rafael Espindolabec37652017-11-17 01:37:50 +0000941 return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
942 CanOmitFromDynSym, F);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000943
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000944 if (ObjSym.isUndefined())
Rafael Espindolabec37652017-11-17 01:37:50 +0000945 return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
946 CanOmitFromDynSym, F);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000947
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000948 if (ObjSym.isCommon())
Rafael Espindola244ef982017-07-26 18:42:48 +0000949 return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
950 ObjSym.getCommonAlignment(), Binding, Visibility,
951 STT_OBJECT, F);
Davide Italiano786d8e32016-09-29 00:40:08 +0000952
Rafael Espindola244ef982017-07-26 18:42:48 +0000953 return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
954 CanOmitFromDynSym, F);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000955}
956
Peter Collingbourne4f952702016-05-01 04:55:03 +0000957template <class ELFT>
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000958void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Rafael Espindola3db70212016-10-25 12:02:31 +0000959 std::vector<bool> KeptComdats;
Peter Collingbourne7b30f162017-03-31 04:47:07 +0000960 for (StringRef S : Obj->getComdatTable())
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000961 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
Rafael Espindola3db70212016-10-25 12:02:31 +0000962
Davide Italiano786d8e32016-09-29 00:40:08 +0000963 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000964 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000965}
966
Rui Ueyama330e52b2017-04-26 22:51:51 +0000967static ELFKind getELFKind(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000968 unsigned char Size;
969 unsigned char Endian;
970 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
Rui Ueyama330e52b2017-04-26 22:51:51 +0000971
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000972 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000973 fatal(MB.getBufferIdentifier() + ": invalid data encoding");
Rui Ueyama330e52b2017-04-26 22:51:51 +0000974 if (Size != ELFCLASS32 && Size != ELFCLASS64)
975 fatal(MB.getBufferIdentifier() + ": invalid file class");
Rui Ueyamac4b65062015-10-12 15:31:09 +0000976
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000977 size_t BufSize = MB.getBuffer().size();
978 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
979 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000980 fatal(MB.getBufferIdentifier() + ": file is too short");
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000981
Rui Ueyama330e52b2017-04-26 22:51:51 +0000982 if (Size == ELFCLASS32)
983 return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
984 return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000985}
986
Rafael Espindola093abab2016-10-27 17:45:40 +0000987template <class ELFT> void BinaryFile::parse() {
Rui Ueyamac9d82b92017-04-27 04:01:36 +0000988 ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000989 auto *Section =
990 make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
Rafael Espindola093abab2016-10-27 17:45:40 +0000991 Sections.push_back(Section);
992
Rui Ueyamac9d82b92017-04-27 04:01:36 +0000993 // For each input file foo that is embedded to a result as a binary
994 // blob, we define _binary_foo_{start,end,size} symbols, so that
995 // user programs can access blobs by name. Non-alphanumeric
996 // characters in a filename are replaced with underscore.
997 std::string S = "_binary_" + MB.getBufferIdentifier().str();
998 for (size_t I = 0; I < S.size(); ++I)
George Rimared906d82017-10-04 08:50:34 +0000999 if (!isAlnum(S[I]))
Rui Ueyamac9d82b92017-04-27 04:01:36 +00001000 S[I] = '_';
1001
Rui Ueyama23f64d22017-07-26 21:24:01 +00001002 Symtab->addRegular<ELFT>(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT,
1003 0, 0, STB_GLOBAL, Section, nullptr);
1004 Symtab->addRegular<ELFT>(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT,
1005 Data.size(), 0, STB_GLOBAL, Section, nullptr);
1006 Symtab->addRegular<ELFT>(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT,
1007 Data.size(), 0, STB_GLOBAL, nullptr, nullptr);
Michael J. Spencera9424f32016-09-09 22:08:04 +00001008}
1009
Rui Ueyama4655ea32016-04-08 00:14:55 +00001010static bool isBitcode(MemoryBufferRef MB) {
1011 using namespace sys::fs;
1012 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
1013}
1014
Rui Ueyama55518e72016-10-28 20:57:25 +00001015InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
Davide Italianobcdd6c62016-10-12 19:35:54 +00001016 uint64_t OffsetInArchive) {
Rui Ueyama330e52b2017-04-26 22:51:51 +00001017 if (isBitcode(MB))
1018 return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
1019
1020 switch (getELFKind(MB)) {
1021 case ELF32LEKind:
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001022 return make<ObjFile<ELF32LE>>(MB, ArchiveName);
Rui Ueyama330e52b2017-04-26 22:51:51 +00001023 case ELF32BEKind:
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001024 return make<ObjFile<ELF32BE>>(MB, ArchiveName);
Rui Ueyama330e52b2017-04-26 22:51:51 +00001025 case ELF64LEKind:
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001026 return make<ObjFile<ELF64LE>>(MB, ArchiveName);
Rui Ueyama330e52b2017-04-26 22:51:51 +00001027 case ELF64BEKind:
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001028 return make<ObjFile<ELF64BE>>(MB, ArchiveName);
Rui Ueyama330e52b2017-04-26 22:51:51 +00001029 default:
1030 llvm_unreachable("getELFKind");
1031 }
Rui Ueyama533c0302016-01-06 00:09:43 +00001032}
1033
Rafael Espindola3460cdd2017-04-24 21:44:20 +00001034InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
Rui Ueyama330e52b2017-04-26 22:51:51 +00001035 switch (getELFKind(MB)) {
1036 case ELF32LEKind:
1037 return make<SharedFile<ELF32LE>>(MB, DefaultSoName);
1038 case ELF32BEKind:
1039 return make<SharedFile<ELF32BE>>(MB, DefaultSoName);
1040 case ELF64LEKind:
1041 return make<SharedFile<ELF64LE>>(MB, DefaultSoName);
1042 case ELF64BEKind:
1043 return make<SharedFile<ELF64BE>>(MB, DefaultSoName);
1044 default:
1045 llvm_unreachable("getELFKind");
1046 }
Rui Ueyama533c0302016-01-06 00:09:43 +00001047}
1048
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001049MemoryBufferRef LazyObjFile::getBuffer() {
Rafael Espindola65c65ce2016-06-14 21:56:36 +00001050 if (Seen)
1051 return MemoryBufferRef();
1052 Seen = true;
1053 return MB;
1054}
1055
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001056InputFile *LazyObjFile::fetch() {
Rafael Espindola808f2d32017-05-04 14:54:48 +00001057 MemoryBufferRef MBRef = getBuffer();
1058 if (MBRef.getBuffer().empty())
1059 return nullptr;
Rafael Espindola0f6cc652017-05-05 15:17:07 +00001060 return createObjectFile(MBRef, ArchiveName, OffsetInArchive);
Rafael Espindola808f2d32017-05-04 14:54:48 +00001061}
1062
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001063template <class ELFT> void LazyObjFile::parse() {
Rafael Espindola3a8e4d92017-08-02 17:35:18 +00001064 for (StringRef Sym : getSymbolNames())
Rafael Espindola244ef982017-07-26 18:42:48 +00001065 Symtab->addLazyObject<ELFT>(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001066}
1067
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001068template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001069 typedef typename ELFT::Shdr Elf_Shdr;
1070 typedef typename ELFT::Sym Elf_Sym;
1071 typedef typename ELFT::SymRange Elf_Sym_Range;
1072
Rafael Espindolaae5e9ed2017-10-10 22:18:16 +00001073 ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(this->MB.getBuffer()));
Rui Ueyamac0081632017-12-07 03:24:57 +00001074 ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
Rafael Espindola6d18d382016-11-03 13:24:29 +00001075 for (const Elf_Shdr &Sec : Sections) {
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001076 if (Sec.sh_type != SHT_SYMTAB)
1077 continue;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001078
Rui Ueyamac0081632017-12-07 03:24:57 +00001079 Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001080 uint32_t FirstNonLocal = Sec.sh_info;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001081 StringRef StringTable =
Rui Ueyamac0081632017-12-07 03:24:57 +00001082 CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001083 std::vector<StringRef> V;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001084
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001085 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +00001086 if (Sym.st_shndx != SHN_UNDEF)
Rui Ueyamac0081632017-12-07 03:24:57 +00001087 V.push_back(CHECK(Sym.getName(StringTable), this));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001088 return V;
1089 }
1090 return {};
1091}
1092
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001093std::vector<StringRef> LazyObjFile::getBitcodeSymbols() {
Rui Ueyama37e60a52017-03-30 21:13:00 +00001094 std::unique_ptr<lto::InputFile> Obj =
Rui Ueyamac0081632017-12-07 03:24:57 +00001095 CHECK(lto::InputFile::create(this->MB), this);
Rui Ueyamad72dd1f2016-09-29 00:58:10 +00001096 std::vector<StringRef> V;
1097 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
Peter Collingbourne0d56b952017-03-28 22:31:35 +00001098 if (!Sym.isUndefined())
Rui Ueyamad72dd1f2016-09-29 00:58:10 +00001099 V.push_back(Saver.save(Sym.getName()));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001100 return V;
1101}
1102
Rui Ueyama1f492892016-04-08 20:49:31 +00001103// Returns a vector of globally-visible defined symbol names.
Rafael Espindola3a8e4d92017-08-02 17:35:18 +00001104std::vector<StringRef> LazyObjFile::getSymbolNames() {
Rui Ueyama4655ea32016-04-08 00:14:55 +00001105 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001106 return getBitcodeSymbols();
1107
Rui Ueyama330e52b2017-04-26 22:51:51 +00001108 switch (getELFKind(this->MB)) {
1109 case ELF32LEKind:
1110 return getElfSymbols<ELF32LE>();
1111 case ELF32BEKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001112 return getElfSymbols<ELF32BE>();
Rui Ueyama330e52b2017-04-26 22:51:51 +00001113 case ELF64LEKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001114 return getElfSymbols<ELF64LE>();
Rui Ueyama330e52b2017-04-26 22:51:51 +00001115 case ELF64BEKind:
1116 return getElfSymbols<ELF64BE>();
1117 default:
1118 llvm_unreachable("getELFKind");
1119 }
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001120}
1121
Peter Collingbourne4f952702016-05-01 04:55:03 +00001122template void ArchiveFile::parse<ELF32LE>();
1123template void ArchiveFile::parse<ELF32BE>();
1124template void ArchiveFile::parse<ELF64LE>();
1125template void ArchiveFile::parse<ELF64BE>();
1126
Rafael Espindola1c2baad2017-05-25 21:53:02 +00001127template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1128template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1129template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1130template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +00001131
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001132template void LazyObjFile::parse<ELF32LE>();
1133template void LazyObjFile::parse<ELF32BE>();
1134template void LazyObjFile::parse<ELF64LE>();
1135template void LazyObjFile::parse<ELF64BE>();
Peter Collingbourne4f952702016-05-01 04:55:03 +00001136
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001137template class elf::ELFFileBase<ELF32LE>;
1138template class elf::ELFFileBase<ELF32BE>;
1139template class elf::ELFFileBase<ELF64LE>;
1140template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +00001141
Rui Ueyama709fb2bb12017-07-26 22:13:32 +00001142template class elf::ObjFile<ELF32LE>;
1143template class elf::ObjFile<ELF32BE>;
1144template class elf::ObjFile<ELF64LE>;
1145template class elf::ObjFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +00001146
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001147template class elf::SharedFile<ELF32LE>;
1148template class elf::SharedFile<ELF32BE>;
1149template class elf::SharedFile<ELF64LE>;
1150template class elf::SharedFile<ELF64BE>;
Michael J. Spencera9424f32016-09-09 22:08:04 +00001151
Rafael Espindola093abab2016-10-27 17:45:40 +00001152template void BinaryFile::parse<ELF32LE>();
1153template void BinaryFile::parse<ELF32BE>();
1154template void BinaryFile::parse<ELF64LE>();
1155template void BinaryFile::parse<ELF64BE>();