blob: 35bddb777a95314460c2620a95012f8852698ff3 [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 Espindola192e1fa2015-08-06 15:08:23 +000011#include "Error.h"
Rafael Espindola9d13d042016-02-11 15:24:48 +000012#include "InputSection.h"
George Rimar67e3ff82016-08-12 19:56:57 +000013#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000014#include "Memory.h"
Peter Collingbourne4f952702016-05-01 04:55:03 +000015#include "SymbolTable.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "Symbols.h"
Peter Smith532bc982016-12-14 10:36:12 +000017#include "SyntheticSections.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"
Davide Italianoe02ba982016-09-08 21:18:38 +000026#include "llvm/Support/Path.h"
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000027#include "llvm/Support/TarWriter.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000028#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
Michael J. Spencer1b348a62015-09-04 22:28:10 +000030using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000032using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000033using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000034
35using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000036using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000037
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000038TarWriter *elf::Tar;
39
Rui Ueyama4e4e8662017-04-03 19:11:23 +000040InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
Rui Ueyama37e60a52017-03-30 21:13:00 +000041
Eugene Leviantc4681202016-11-01 09:17:50 +000042namespace {
43// In ELF object file all section addresses are zero. If we have multiple
44// .text sections (when using -ffunction-section or comdat group) then
45// LLVM DWARF parser will not be able to parse .debug_line correctly, unless
46// we assign each section some unique address. This callback method assigns
47// each section an address equal to its offset in ELF object file.
48class ObjectInfo : public LoadedObjectInfo {
49public:
50 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override {
51 return static_cast<const ELFSectionRef &>(Sec).getOffset();
52 }
53 std::unique_ptr<LoadedObjectInfo> clone() const override {
54 return std::unique_ptr<LoadedObjectInfo>();
55 }
56};
57}
58
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000059Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
Rui Ueyamae6e206d2017-02-21 23:22:56 +000060 log(Path);
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000061 auto MBOrErr = MemoryBuffer::getFile(Path);
62 if (auto EC = MBOrErr.getError()) {
Rui Ueyamac8d3a832017-01-12 22:18:04 +000063 error("cannot open " + Path + ": " + EC.message());
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000064 return None;
65 }
Rui Ueyamae6e206d2017-02-21 23:22:56 +000066
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000067 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
68 MemoryBufferRef MBRef = MB->getMemBufferRef();
69 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
70
71 if (Tar)
72 Tar->append(relativeToRoot(Path), MBRef.getBuffer());
73 return MBRef;
74}
75
Rui Ueyama7463ada2016-11-02 19:51:41 +000076template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() {
Rui Ueyama7556f6b2016-11-02 18:42:13 +000077 std::unique_ptr<object::ObjectFile> Obj =
Rui Ueyama4e4e8662017-04-03 19:11:23 +000078 check(object::ObjectFile::createObjectFile(this->MB), toString(this));
Eugene Leviantb380b242016-10-26 11:07:09 +000079
Eugene Leviantc4681202016-11-01 09:17:50 +000080 ObjectInfo ObjInfo;
Rui Ueyama9a97acc2016-12-07 23:26:39 +000081 DWARFContextInMemory Dwarf(*Obj, &ObjInfo);
Eugene Leviantb380b242016-10-26 11:07:09 +000082 DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
Rui Ueyamad57e74b72017-03-17 23:29:01 +000083 DataExtractor LineData(Dwarf.getLineSection().Data, Config->IsLE,
84 Config->Wordsize);
Rui Ueyama7556f6b2016-11-02 18:42:13 +000085
Eugene Leviantb380b242016-10-26 11:07:09 +000086 // The second parameter is offset in .debug_line section
87 // for compilation unit (CU) of interest. We have only one
88 // CU (object file), so offset is always 0.
89 DwarfLine->getOrParseLineTable(LineData, 0);
90}
91
Rui Ueyama7556f6b2016-11-02 18:42:13 +000092// Returns source line information for a given offset
93// using DWARF debug info.
Eugene Leviantc4681202016-11-01 09:17:50 +000094template <class ELFT>
Rui Ueyamab8760202017-03-30 19:13:47 +000095Optional<DILineInfo> elf::ObjectFile<ELFT>::getDILineInfo(InputSectionBase *S,
96 uint64_t Offset) {
Eugene Leviantb380b242016-10-26 11:07:09 +000097 if (!DwarfLine)
Rui Ueyama7556f6b2016-11-02 18:42:13 +000098 initializeDwarfLine();
Eugene Leviantb380b242016-10-26 11:07:09 +000099
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000100 // The offset to CU is 0.
101 const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
102 if (!Tbl)
Rui Ueyamab8760202017-03-30 19:13:47 +0000103 return None;
Eugene Leviantc4681202016-11-01 09:17:50 +0000104
105 // Use fake address calcuated by adding section file offset and offset in
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000106 // section. See comments for ObjectInfo class.
107 DILineInfo Info;
George Rimar92c88a42016-12-07 19:42:25 +0000108 Tbl->getFileLineInfoForAddress(
Rafael Espindola35ae65e2017-03-08 15:57:17 +0000109 S->getOffsetInFile() + Offset, nullptr,
George Rimar92c88a42016-12-07 19:42:25 +0000110 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000111 if (Info.Line == 0)
Rui Ueyamab8760202017-03-30 19:13:47 +0000112 return None;
113 return Info;
114}
115
116// Returns source line information for a given offset
117// using DWARF debug info.
118template <class ELFT>
119std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase *S,
120 uint64_t Offset) {
121 if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
122 return Info->FileName + ":" + std::to_string(Info->Line);
123 return "";
Eugene Leviantb380b242016-10-26 11:07:09 +0000124}
125
Rui Ueyama8a3ef952017-04-28 20:00:09 +0000126// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
Rui Ueyamace039262017-01-06 10:04:08 +0000127std::string lld::toString(const InputFile *F) {
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000128 if (!F)
Rui Ueyama8a3ef952017-04-28 20:00:09 +0000129 return "<internal>";
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000130
131 if (F->ToStringCache.empty()) {
132 if (F->ArchiveName.empty())
133 F->ToStringCache = F->getName();
134 else
135 F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
136 }
137 return F->ToStringCache;
Rafael Espindola78db5a92016-05-09 21:40:06 +0000138}
139
Rui Ueyama2022e812015-11-20 02:10:52 +0000140template <class ELFT>
Rafael Espindolae19abab2016-11-03 20:44:50 +0000141ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000142 if (ELFT::TargetEndianness == support::little)
143 EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
144 else
145 EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
146
Rafael Espindolae19abab2016-11-03 20:44:50 +0000147 EMachine = getObj().getHeader()->e_machine;
148 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000149}
150
151template <class ELFT>
Rafael Espindola8e232572016-11-03 20:48:57 +0000152typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() {
George Rimar0b2d3742016-11-14 10:05:53 +0000153 return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end());
Davide Italiano6d328d32015-09-16 20:45:57 +0000154}
155
Rafael Espindola115f0f32015-11-03 14:13:40 +0000156template <class ELFT>
157uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyama37e60a52017-03-30 21:13:00 +0000158 return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000159 toString(this));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000160}
161
Rafael Espindola6d18d382016-11-03 13:24:29 +0000162template <class ELFT>
Rafael Espindola21d8be92016-11-03 15:43:47 +0000163void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
164 const Elf_Shdr *Symtab) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000165 FirstNonLocal = Symtab->sh_info;
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000166 Symbols = check(getObj().symbols(Symtab), toString(this));
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000167 if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000168 fatal(toString(this) + ": invalid sh_info in symbol table");
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000169
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000170 StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
171 toString(this));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000172}
173
174template <class ELFT>
Rui Ueyama330e52b2017-04-26 22:51:51 +0000175elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M, StringRef ArchiveName)
176 : ELFFileBase<ELFT>(Base::ObjectKind, M) {
177 this->ArchiveName = ArchiveName;
178}
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000179
180template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +0000181ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000182 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000183 return this->SymbolBodies;
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000184 return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000185}
186
187template <class ELFT>
188ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000189 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000190 return this->SymbolBodies;
191 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000192}
193
Rafael Espindola444576d2015-10-09 19:25:07 +0000194template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000195void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000196 // Read section and symbol tables.
Rafael Espindola73c3a362016-11-08 15:51:00 +0000197 initializeSections(ComdatGroups);
198 initializeSymbols();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199}
200
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000201// Sections with SHT_GROUP and comdat bits define comdat section groups.
202// They are identified and deduplicated by group name. This function
203// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000204template <class ELFT>
Rafael Espindola7c7abaf2016-11-03 02:28:13 +0000205StringRef
206elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
207 const Elf_Shdr &Sec) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000208 if (this->Symbols.empty())
Rui Ueyama37e60a52017-03-30 21:13:00 +0000209 this->initSymtab(
210 Sections,
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000211 check(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this)));
212 const Elf_Sym *Sym = check(
213 object::getSymbol<ELFT>(this->Symbols, Sec.sh_info), toString(this));
214 return check(Sym->getName(this->StringTable), toString(this));
Rafael Espindola444576d2015-10-09 19:25:07 +0000215}
216
217template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000218ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000219elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000220 const ELFFile<ELFT> &Obj = this->getObj();
Rui Ueyama37e60a52017-03-30 21:13:00 +0000221 ArrayRef<Elf_Word> Entries = check(
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000222 Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), toString(this));
Rafael Espindola444576d2015-10-09 19:25:07 +0000223 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000224 fatal(toString(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000225 return Entries.slice(1);
226}
227
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000228template <class ELFT>
229bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000230 // We don't merge sections if -O0 (default is -O1). This makes sometimes
231 // the linker significantly faster, although the output will be bigger.
232 if (Config->Optimize == 0)
233 return false;
234
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000235 // Do not merge sections if generating a relocatable object. It makes
236 // the code simpler because we do not need to update relocation addends
237 // to reflect changes introduced by merging. Instead of that we write
238 // such "merge" sections into separate OutputSections and keep SHF_MERGE
239 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
240 // later during a final linking.
241 if (Config->Relocatable)
242 return false;
243
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000244 // A mergeable section with size 0 is useless because they don't have
245 // any data to merge. A mergeable string section with size 0 can be
246 // argued as invalid because it doesn't end with a null character.
247 // We'll avoid a mess by handling them as if they were non-mergeable.
248 if (Sec.sh_size == 0)
249 return false;
250
Rui Ueyamac75ef852016-09-21 03:22:18 +0000251 // Check for sh_entsize. The ELF spec is not clear about the zero
252 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
253 // the section does not hold a table of fixed-size entries". We know
254 // that Rust 1.13 produces a string mergeable section with a zero
255 // sh_entsize. Here we just accept it rather than being picky about it.
Rui Ueyama9cc84382017-02-24 19:52:52 +0000256 uint64_t EntSize = Sec.sh_entsize;
Rui Ueyamac75ef852016-09-21 03:22:18 +0000257 if (EntSize == 0)
258 return false;
259 if (Sec.sh_size % EntSize)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000260 fatal(toString(this) +
Rui Ueyamac75ef852016-09-21 03:22:18 +0000261 ": SHF_MERGE section size must be a multiple of sh_entsize");
262
Rui Ueyama9cc84382017-02-24 19:52:52 +0000263 uint64_t Flags = Sec.sh_flags;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000264 if (!(Flags & SHF_MERGE))
265 return false;
266 if (Flags & SHF_WRITE)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000267 fatal(toString(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000268
Peter Smith4df2e142016-05-18 11:40:16 +0000269 // Don't try to merge if the alignment is larger than the sh_entsize and this
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000270 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000271 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000272 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
273 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000274 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000275 if (Flags & SHF_STRINGS)
276 return true;
277
George Rimardcddfb62016-06-08 12:04:59 +0000278 return Sec.sh_addralign <= EntSize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000279}
280
281template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000282void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindola73c3a362016-11-08 15:51:00 +0000283 DenseSet<CachedHashStringRef> &ComdatGroups) {
Rui Ueyama37e60a52017-03-30 21:13:00 +0000284 ArrayRef<Elf_Shdr> ObjSections =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000285 check(this->getObj().sections(), toString(this));
Rafael Espindolae19abab2016-11-03 20:44:50 +0000286 const ELFFile<ELFT> &Obj = this->getObj();
Rafael Espindola235d82c2016-11-02 14:42:20 +0000287 uint64_t Size = ObjSections.size();
George Rimar3f7c3df2017-03-21 08:44:25 +0000288 this->Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000289 unsigned I = -1;
Rui Ueyama37e60a52017-03-30 21:13:00 +0000290 StringRef SectionStringTable =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000291 check(Obj.getSectionStringTable(ObjSections), toString(this));
Rafael Espindola235d82c2016-11-02 14:42:20 +0000292 for (const Elf_Shdr &Sec : ObjSections) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000293 ++I;
George Rimar3f7c3df2017-03-21 08:44:25 +0000294 if (this->Sections[I] == &InputSection::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000295 continue;
296
Rui Ueyamaaf9793d2016-10-04 16:47:49 +0000297 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
298 // if -r is given, we'll let the final link discard such sections.
299 // This is compatible with GNU.
300 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
George Rimar3f7c3df2017-03-21 08:44:25 +0000301 this->Sections[I] = &InputSection::Discarded;
Eugene Leviant27be5422016-09-28 08:42:02 +0000302 continue;
303 }
304
Rafael Espindolacde25132015-08-13 14:45:44 +0000305 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000306 case SHT_GROUP:
George Rimar3f7c3df2017-03-21 08:44:25 +0000307 this->Sections[I] = &InputSection::Discarded;
308 if (ComdatGroups
309 .insert(
310 CachedHashStringRef(getShtGroupSignature(ObjSections, Sec)))
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000311 .second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000312 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000313 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000314 if (SecIndex >= Size)
George Rimar3f7c3df2017-03-21 08:44:25 +0000315 fatal(toString(this) +
316 ": invalid section index in group: " + Twine(SecIndex));
317 this->Sections[SecIndex] = &InputSection::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000318 }
319 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000320 case SHT_SYMTAB:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000321 this->initSymtab(ObjSections, &Sec);
Rafael Espindolacde25132015-08-13 14:45:44 +0000322 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000323 case SHT_SYMTAB_SHNDX:
Rui Ueyama37e60a52017-03-30 21:13:00 +0000324 this->SymtabSHNDX =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000325 check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
Rafael Espindola20348222015-08-24 21:43:25 +0000326 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000327 case SHT_STRTAB:
328 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000329 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000330 default:
George Rimar3f7c3df2017-03-21 08:44:25 +0000331 this->Sections[I] = createInputSection(Sec, SectionStringTable);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000332 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000333
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000334 // .ARM.exidx sections have a reverse dependency on the InputSection they
335 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
336 if (Sec.sh_flags & SHF_LINK_ORDER) {
George Rimarf56cadd2017-03-21 08:57:13 +0000337 if (Sec.sh_link >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000338 fatal(toString(this) + ": invalid sh_link index: " +
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000339 Twine(Sec.sh_link));
George Rimar3f7c3df2017-03-21 08:44:25 +0000340 this->Sections[Sec.sh_link]->DependentSections.push_back(
341 this->Sections[I]);
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000342 }
Peter Smith07606052016-10-10 10:10:27 +0000343 }
344}
345
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000346template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000347InputSectionBase *elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000348 uint32_t Idx = Sec.sh_info;
George Rimar3f7c3df2017-03-21 08:44:25 +0000349 if (Idx >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000350 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
George Rimar3f7c3df2017-03-21 08:44:25 +0000351 InputSectionBase *Target = this->Sections[Idx];
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000352
353 // Strictly speaking, a relocation section must be included in the
354 // group of the section it relocates. However, LLVM 3.3 and earlier
355 // would fail to do so, so we gracefully handle that case.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000356 if (Target == &InputSection::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000357 return nullptr;
358
359 if (!Target)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000360 fatal(toString(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000361 return Target;
362}
363
Rui Ueyama92a8d792017-05-01 20:49:09 +0000364// Create a regular InputSection class that has the same contents
365// as a given section.
366InputSectionBase *toRegularSection(MergeInputSection *Sec) {
367 auto *Ret = make<InputSection>(Sec->Flags, Sec->Type, Sec->Alignment,
368 Sec->Data, Sec->Name);
369 Ret->File = Sec->File;
370 return Ret;
371}
372
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000373template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000374InputSectionBase *
Rafael Espindolaec05a572016-11-01 21:48:00 +0000375elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
376 StringRef SectionStringTable) {
Rui Ueyama37e60a52017-03-30 21:13:00 +0000377 StringRef Name = check(
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000378 this->getObj().getSectionName(&Sec, SectionStringTable), toString(this));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000379
Rafael Espindola042a3f22016-09-08 14:06:08 +0000380 switch (Sec.sh_type) {
381 case SHT_ARM_ATTRIBUTES:
Peter Smith532bc982016-12-14 10:36:12 +0000382 // FIXME: ARM meta-data section. Retain the first attribute section
383 // we see. The eglibc ARM dynamic loaders require the presence of an
384 // attribute section for dlopen to work.
385 // In a full implementation we would merge all attribute sections.
386 if (In<ELFT>::ARMAttributes == nullptr) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000387 In<ELFT>::ARMAttributes = make<InputSection>(this, &Sec, Name);
Peter Smith532bc982016-12-14 10:36:12 +0000388 return In<ELFT>::ARMAttributes;
389 }
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000390 return &InputSection::Discarded;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000391 case SHT_RELA:
392 case SHT_REL: {
George Rimaree6f22c2017-02-14 16:42:38 +0000393 // Find the relocation target section and associate this
394 // section with it. Target can be discarded, for example
395 // if it is a duplicated member of SHT_GROUP section, we
396 // do not create or proccess relocatable sections then.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000397 InputSectionBase *Target = getRelocTarget(Sec);
George Rimaree6f22c2017-02-14 16:42:38 +0000398 if (!Target)
399 return nullptr;
400
Rafael Espindola042a3f22016-09-08 14:06:08 +0000401 // This section contains relocation information.
402 // If -r is given, we do not interpret or apply relocation
403 // but just copy relocation sections to output.
404 if (Config->Relocatable)
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000405 return make<InputSection>(this, &Sec, Name);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000406
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000407 if (Target->FirstRelocation)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000408 fatal(toString(this) +
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000409 ": multiple relocation sections to one section are not supported");
Rui Ueyama92a8d792017-05-01 20:49:09 +0000410
411 // Mergeable sections with relocations are tricky because relocations
412 // need to be taken into account when comparing section contents for
Rui Ueyamac3443132017-05-02 21:16:06 +0000413 // merging. It's not worth supporting such mergeable sections because
Rui Ueyamadfb1e2a2017-05-02 02:58:04 +0000414 // they are rare and it'd complicates the internal design (we usually
415 // have to determine if two sections are mergeable early in the link
416 // process much before applying relocations). We simply handle mergeable
417 // sections with relocations as non-mergeable.
Rui Ueyama92a8d792017-05-01 20:49:09 +0000418 if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
419 Target = toRegularSection(MS);
420 this->Sections[Sec.sh_info] = Target;
Davide Italianod7656382017-04-29 01:24:34 +0000421 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000422
423 size_t NumRelocations;
424 if (Sec.sh_type == SHT_RELA) {
Rui Ueyama37e60a52017-03-30 21:13:00 +0000425 ArrayRef<Elf_Rela> Rels =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000426 check(this->getObj().relas(&Sec), toString(this));
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000427 Target->FirstRelocation = Rels.begin();
428 NumRelocations = Rels.size();
429 Target->AreRelocsRela = true;
430 } else {
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000431 ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000432 Target->FirstRelocation = Rels.begin();
433 NumRelocations = Rels.size();
434 Target->AreRelocsRela = false;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000435 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000436 assert(isUInt<31>(NumRelocations));
437 Target->NumRelocations = NumRelocations;
George Rimar82bd8be2017-02-08 16:18:10 +0000438
439 // Relocation sections processed by the linker are usually removed
440 // from the output, so returning `nullptr` for the normal case.
441 // However, if -emit-relocs is given, we need to leave them in the output.
442 // (Some post link analysis tools need this information.)
George Rimar858a6592017-02-17 19:46:47 +0000443 if (Config->EmitRelocs) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000444 InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
George Rimar858a6592017-02-17 19:46:47 +0000445 // We will not emit relocation section if target was discarded.
446 Target->DependentSections.push_back(RelocSec);
447 return RelocSec;
448 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000449 return nullptr;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000450 }
451 }
452
Rui Ueyama06332732017-02-23 07:06:43 +0000453 // The GNU linker uses .note.GNU-stack section as a marker indicating
454 // that the code in the object file does not expect that the stack is
455 // executable (in terms of NX bit). If all input files have the marker,
456 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
Rui Ueyama65efe352017-02-23 07:15:46 +0000457 // make the stack non-executable. Most object files have this section as
458 // of 2017.
Rui Ueyama06332732017-02-23 07:06:43 +0000459 //
460 // But making the stack non-executable is a norm today for security
Rui Ueyama65efe352017-02-23 07:15:46 +0000461 // reasons. Failure to do so may result in a serious security issue.
462 // Therefore, we make LLD always add PT_GNU_STACK unless it is
Rui Ueyama06332732017-02-23 07:06:43 +0000463 // explicitly told to do otherwise (by -z execstack). Because the stack
464 // executable-ness is controlled solely by command line options,
465 // .note.GNU-stack sections are simply ignored.
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000466 if (Name == ".note.GNU-stack")
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000467 return &InputSection::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000468
Rui Ueyamac1a0ac22017-02-23 07:35:11 +0000469 // Split stacks is a feature to support a discontiguous stack. At least
470 // as of 2017, it seems that the feature is not being used widely.
471 // Only GNU gold supports that. We don't. For the details about that,
472 // see https://gcc.gnu.org/wiki/SplitStacks
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000473 if (Name == ".note.GNU-split-stack") {
Rui Ueyamab7f39b02017-02-23 07:35:30 +0000474 error(toString(this) +
475 ": object file compiled with -fsplit-stack is not supported");
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000476 return &InputSection::Discarded;
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000477 }
478
George Rimarf21aade2016-08-31 08:38:11 +0000479 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000480 return &InputSection::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000481
Rui Ueyamae2f11692017-04-28 22:46:55 +0000482 // If -gdb-index is given, LLD creates .gdb_index section, and that
483 // section serves the same purpose as .debug_gnu_pub{names,types} sections.
484 // If that's the case, we want to eliminate .debug_gnu_pub{names,types}
485 // because they are redundant and can waste large amount of disk space
486 // (for example, they are about 400 MiB in total for a clang debug build.)
487 if (Config->GdbIndex &&
488 (Name == ".debug_gnu_pubnames" || Name == ".debug_gnu_pubtypes"))
489 return &InputSection::Discarded;
490
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000491 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
492 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
493 // sections. Drop those sections to avoid duplicate symbol errors.
494 // FIXME: This is glibc PR20543, we should remove this hack once that has been
495 // fixed for a while.
496 if (Name.startswith(".gnu.linkonce."))
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000497 return &InputSection::Discarded;
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000498
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000499 // The linker merges EH (exception handling) frames and creates a
500 // .eh_frame_hdr section for runtime. So we handle them with a special
501 // class. For relocatable outputs, they are just passed through.
502 if (Name == ".eh_frame" && !Config->Relocatable)
Rafael Espindola5c02b742017-03-06 21:17:18 +0000503 return make<EhInputSection>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000504
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000505 if (shouldMerge(Sec))
Rafael Espindola6119b862017-03-06 20:23:56 +0000506 return make<MergeInputSection>(this, &Sec, Name);
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000507 return make<InputSection>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000508}
509
Rafael Espindola73c3a362016-11-08 15:51:00 +0000510template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rui Ueyama27119402016-11-03 18:20:08 +0000511 SymbolBodies.reserve(this->Symbols.size());
512 for (const Elf_Sym &Sym : this->Symbols)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000513 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000514}
515
516template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000517InputSectionBase *elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000518 uint32_t Index = this->getSectionIndex(Sym);
George Rimar3f7c3df2017-03-21 08:44:25 +0000519 if (Index >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000520 fatal(toString(this) + ": invalid section index: " + Twine(Index));
George Rimar3f7c3df2017-03-21 08:44:25 +0000521 InputSectionBase *S = this->Sections[Index];
George Rimar24adce92016-10-06 09:17:55 +0000522
George Rimar308752e2016-11-15 07:56:28 +0000523 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
524 // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
George Rimar683a35d2016-08-12 19:25:54 +0000525 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
George Rimar308752e2016-11-15 07:56:28 +0000526 // In this case it is fine for section to be null here as we do not
527 // allocate sections of these types.
George Rimar24adce92016-10-06 09:17:55 +0000528 if (!S) {
George Rimar308752e2016-11-15 07:56:28 +0000529 if (Index == 0 || Sym.getType() == STT_SECTION ||
530 Sym.getType() == STT_NOTYPE)
George Rimar24adce92016-10-06 09:17:55 +0000531 return nullptr;
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000532 fatal(toString(this) + ": invalid section index: " + Twine(Index));
George Rimar24adce92016-10-06 09:17:55 +0000533 }
534
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000535 if (S == &InputSection::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000536 return S;
537 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000538}
539
540template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000541SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000542 int Binding = Sym->getBinding();
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000543 InputSectionBase *Sec = getSection(*Sym);
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000544
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000545 uint8_t StOther = Sym->st_other;
546 uint8_t Type = Sym->getType();
Rui Ueyama9cc84382017-02-24 19:52:52 +0000547 uint64_t Value = Sym->st_value;
548 uint64_t Size = Sym->st_size;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000549
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000550 if (Binding == STB_LOCAL) {
Eugene Leviantb380b242016-10-26 11:07:09 +0000551 if (Sym->getType() == STT_FILE)
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000552 SourceFile = check(Sym->getName(this->StringTable), toString(this));
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000553
554 if (this->StringTable.size() <= Sym->st_name)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000555 fatal(toString(this) + ": invalid symbol name offset");
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000556
Rui Ueyama84e65a72016-11-29 19:11:39 +0000557 StringRefZ Name = this->StringTable.data() + Sym->st_name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000558 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama175e81c2017-02-28 19:36:30 +0000559 return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type, this);
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000560
Rui Ueyama175e81c2017-02-28 19:36:30 +0000561 return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
562 Size, Sec, this);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000563 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000564
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000565 StringRef Name = check(Sym->getName(this->StringTable), toString(this));
Rafael Espindola20348222015-08-24 21:43:25 +0000566
Rafael Espindola4cda5812015-10-16 15:29:48 +0000567 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000568 case SHN_UNDEF:
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000569 return elf::Symtab<ELFT>::X
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000570 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000571 /*CanOmitFromDynSym=*/false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000572 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000573 case SHN_COMMON:
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000574 if (Value == 0 || Value >= UINT32_MAX)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000575 fatal(toString(this) + ": common symbol '" + Name +
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000576 "' has invalid alignment: " + Twine(Value));
577 return elf::Symtab<ELFT>::X
578 ->addCommon(Name, Size, Value, Binding, StOther, Type, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000579 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000580 }
Rafael Espindola20348222015-08-24 21:43:25 +0000581
Rafael Espindola67d72c02016-03-11 12:06:30 +0000582 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000583 default:
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000584 fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000585 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000586 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000587 case STB_GNU_UNIQUE:
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000588 if (Sec == &InputSection::Discarded)
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000589 return elf::Symtab<ELFT>::X
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000590 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000591 /*CanOmitFromDynSym=*/false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000592 ->body();
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000593 return elf::Symtab<ELFT>::X
594 ->addRegular(Name, StOther, Type, Value, Size, Binding, Sec, this)
595 ->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000596 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000597}
598
Rui Ueyamafd7deda2017-05-03 21:03:08 +0000599ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
600 : InputFile(ArchiveKind, File->getMemoryBufferRef()),
601 File(std::move(File)) {}
602
Peter Collingbourne4f952702016-05-01 04:55:03 +0000603template <class ELFT> void ArchiveFile::parse() {
Rui Ueyamafd7deda2017-05-03 21:03:08 +0000604 for (const Archive::Symbol &Sym : File->symbols())
Rui Ueyama3d0f77b2016-09-30 17:56:20 +0000605 Symtab<ELFT>::X->addLazyArchive(this, Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000606}
607
608// Returns a buffer pointing to a member file containing a given symbol.
Davide Italianobcdd6c62016-10-12 19:35:54 +0000609std::pair<MemoryBufferRef, uint64_t>
610ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000611 Archive::Child C =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000612 check(Sym->getMember(), toString(this) +
Rui Ueyama37e60a52017-03-30 21:13:00 +0000613 ": could not get the member for symbol " +
614 Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000615
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000616 if (!Seen.insert(C.getChildOffset()).second)
Davide Italianobcdd6c62016-10-12 19:35:54 +0000617 return {MemoryBufferRef(), 0};
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000618
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000619 MemoryBufferRef Ret =
620 check(C.getMemoryBufferRef(),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000621 toString(this) +
Rui Ueyama37e60a52017-03-30 21:13:00 +0000622 ": could not get the buffer for the member defining symbol " +
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000623 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000624
Rui Ueyamaec1c75e2017-01-09 01:42:02 +0000625 if (C.getParent()->isThin() && Tar)
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000626 Tar->append(relativeToRoot(check(C.getFullName(), toString(this))),
Rui Ueyama37e60a52017-03-30 21:13:00 +0000627 Ret.getBuffer());
Davide Italianobcdd6c62016-10-12 19:35:54 +0000628 if (C.getParent()->isThin())
629 return {Ret, 0};
630 return {Ret, C.getChildOffset()};
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000631}
632
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000633template <class ELFT>
Rafael Espindola3460cdd2017-04-24 21:44:20 +0000634SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
635 : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName),
636 AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000637
Rafael Espindola115f0f32015-11-03 14:13:40 +0000638template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000639const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000640SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000641 return check(
Rui Ueyama37e60a52017-03-30 21:13:00 +0000642 this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000643 toString(this));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000644}
645
Rui Ueyama7c713312016-01-06 01:56:36 +0000646// Partially parse the shared object file so that we can call
647// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000648template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rafael Espindolac8b15812015-10-01 15:47:50 +0000649 const Elf_Shdr *DynamicSec = nullptr;
Rafael Espindolae19abab2016-11-03 20:44:50 +0000650 const ELFFile<ELFT> Obj = this->getObj();
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000651 ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
Rui Ueyama3233d3e2017-04-13 00:23:32 +0000652
653 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
Rafael Espindola84d6a172016-11-03 12:21:00 +0000654 for (const Elf_Shdr &Sec : Sections) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000655 switch (Sec.sh_type) {
656 default:
657 continue;
658 case SHT_DYNSYM:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000659 this->initSymtab(Sections, &Sec);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000660 break;
661 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000662 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000663 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000664 case SHT_SYMTAB_SHNDX:
Rui Ueyama37e60a52017-03-30 21:13:00 +0000665 this->SymtabSHNDX =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000666 check(Obj.getSHNDXTable(Sec, Sections), toString(this));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000667 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000668 case SHT_GNU_versym:
669 this->VersymSec = &Sec;
670 break;
671 case SHT_GNU_verdef:
672 this->VerdefSec = &Sec;
673 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000674 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000675 }
676
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000677 if (this->VersymSec && this->Symbols.empty())
George Rimarbcba39a2016-11-02 10:16:25 +0000678 error("SHT_GNU_versym should be associated with symbol table");
679
Rui Ueyama3233d3e2017-04-13 00:23:32 +0000680 // Search for a DT_SONAME tag to initialize this->SoName.
Rui Ueyama361d8b92015-10-12 15:49:02 +0000681 if (!DynamicSec)
682 return;
George Rimar53cf2a82016-10-07 09:01:04 +0000683 ArrayRef<Elf_Dyn> Arr =
684 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000685 toString(this));
George Rimar53cf2a82016-10-07 09:01:04 +0000686 for (const Elf_Dyn &Dyn : Arr) {
Rui Ueyama361d8b92015-10-12 15:49:02 +0000687 if (Dyn.d_tag == DT_SONAME) {
Rui Ueyama9cc84382017-02-24 19:52:52 +0000688 uint64_t Val = Dyn.getVal();
Rui Ueyama361d8b92015-10-12 15:49:02 +0000689 if (Val >= this->StringTable.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000690 fatal(toString(this) + ": invalid DT_SONAME entry");
Rui Ueyamae5ad2982017-04-26 23:00:32 +0000691 SoName = this->StringTable.data() + Val;
Rui Ueyama361d8b92015-10-12 15:49:02 +0000692 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000693 }
694 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000695}
Rafael Espindola18173d42015-09-08 15:50:05 +0000696
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000697// Parse the version definitions in the object file if present. Returns a vector
698// whose nth element contains a pointer to the Elf_Verdef for version identifier
699// n. Version identifiers that are not definitions map to nullptr. The array
700// always has at least length 1.
701template <class ELFT>
702std::vector<const typename ELFT::Verdef *>
703SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
704 std::vector<const Elf_Verdef *> Verdefs(1);
705 // We only need to process symbol versions for this DSO if it has both a
706 // versym and a verdef section, which indicates that the DSO contains symbol
707 // version definitions.
708 if (!VersymSec || !VerdefSec)
709 return Verdefs;
710
711 // The location of the first global versym entry.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000712 const char *Base = this->MB.getBuffer().data();
713 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000714 this->FirstNonLocal;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000715
716 // We cannot determine the largest verdef identifier without inspecting
717 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
718 // sequentially starting from 1, so we predict that the largest identifier
719 // will be VerdefCount.
720 unsigned VerdefCount = VerdefSec->sh_info;
721 Verdefs.resize(VerdefCount + 1);
722
723 // Build the Verdefs array by following the chain of Elf_Verdef objects
724 // from the start of the .gnu.version_d section.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000725 const char *Verdef = Base + VerdefSec->sh_offset;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000726 for (unsigned I = 0; I != VerdefCount; ++I) {
727 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
728 Verdef += CurVerdef->vd_next;
729 unsigned VerdefIndex = CurVerdef->vd_ndx;
730 if (Verdefs.size() <= VerdefIndex)
731 Verdefs.resize(VerdefIndex + 1);
732 Verdefs[VerdefIndex] = CurVerdef;
733 }
734
735 return Verdefs;
736}
737
Rui Ueyama7c713312016-01-06 01:56:36 +0000738// Fully parse the shared object file. This must be called after parseSoName().
739template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000740 // Create mapping from version identifiers to Elf_Verdef entries.
741 const Elf_Versym *Versym = nullptr;
742 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
743
Rafael Espindola8e232572016-11-03 20:48:57 +0000744 Elf_Sym_Range Syms = this->getGlobalSymbols();
Rafael Espindola18173d42015-09-08 15:50:05 +0000745 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000746 unsigned VersymIndex = 0;
747 if (Versym) {
748 VersymIndex = Versym->vs_index;
749 ++Versym;
750 }
Rafael Espindola2756e042017-01-06 22:30:35 +0000751 bool Hidden = VersymIndex & VERSYM_HIDDEN;
752 VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000753
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000754 StringRef Name = check(Sym.getName(this->StringTable), toString(this));
Rafael Espindola18da0e52016-04-29 16:23:31 +0000755 if (Sym.isUndefined()) {
756 Undefs.push_back(Name);
757 continue;
758 }
759
Rafael Espindola2756e042017-01-06 22:30:35 +0000760 // Ignore local symbols.
761 if (Versym && VersymIndex == VER_NDX_LOCAL)
762 continue;
Rafael Espindolad2454d62016-06-09 15:45:49 +0000763
764 const Elf_Verdef *V =
765 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
Rafael Espindola2756e042017-01-06 22:30:35 +0000766
767 if (!Hidden)
768 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
769
770 // Also add the symbol with the versioned name to handle undefined symbols
771 // with explicit versions.
772 if (V) {
773 StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
Rui Ueyama63d48e52017-04-27 04:01:14 +0000774 Name = Saver.save(Name + "@" + VerName);
Rafael Espindola2756e042017-01-06 22:30:35 +0000775 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
776 }
Rafael Espindola18173d42015-09-08 15:50:05 +0000777 }
778}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000779
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000780static ELFKind getBitcodeELFKind(const Triple &T) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000781 if (T.isLittleEndian())
782 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
783 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000784}
785
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000786static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000787 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000788 case Triple::aarch64:
789 return EM_AARCH64;
790 case Triple::arm:
Sean Silva1d961852017-02-28 03:00:48 +0000791 case Triple::thumb:
Rui Ueyama523744d2016-07-07 02:46:30 +0000792 return EM_ARM;
793 case Triple::mips:
794 case Triple::mipsel:
795 case Triple::mips64:
796 case Triple::mips64el:
797 return EM_MIPS;
798 case Triple::ppc:
799 return EM_PPC;
800 case Triple::ppc64:
801 return EM_PPC64;
802 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000803 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000804 case Triple::x86_64:
805 return EM_X86_64;
806 default:
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000807 fatal(Path + ": could not infer e_machine from bitcode target triple " +
808 T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000809 }
810}
811
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000812BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
813 uint64_t OffsetInArchive)
814 : InputFile(BitcodeKind, MB) {
815 this->ArchiveName = ArchiveName;
816
817 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
818 // (the fully resolved path of the archive) + member name + offset of the
819 // member in the archive.
820 // ThinLTO uses the MemoryBufferRef identifier to access its internal
821 // data structures and if two archives define two members with the same name,
822 // this causes a collision which result in only one of the objects being
823 // taken into consideration at LTO time (which very likely causes undefined
824 // symbols later in the link stage).
825 MemoryBufferRef MBRef(MB.getBuffer(),
826 Saver.save(ArchiveName + MB.getBufferIdentifier() +
827 utostr(OffsetInArchive)));
828 Obj = check(lto::InputFile::create(MBRef), toString(this));
829
830 Triple T(Obj->getTargetTriple());
831 EKind = getBitcodeELFKind(T);
832 EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
Davide Italiano60976ba2016-06-29 06:12:39 +0000833}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000834
Davide Italiano786d8e32016-09-29 00:40:08 +0000835static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
836 switch (GvVisibility) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000837 case GlobalValue::DefaultVisibility:
838 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000839 case GlobalValue::HiddenVisibility:
840 return STV_HIDDEN;
841 case GlobalValue::ProtectedVisibility:
842 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000843 }
George Rimar777f9632016-03-12 08:31:34 +0000844 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000845}
846
Peter Collingbourne4f952702016-05-01 04:55:03 +0000847template <class ELFT>
Rafael Espindola3db70212016-10-25 12:02:31 +0000848static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
Davide Italiano786d8e32016-09-29 00:40:08 +0000849 const lto::InputFile::Symbol &ObjSym,
Rui Ueyama55518e72016-10-28 20:57:25 +0000850 BitcodeFile *F) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000851 StringRef NameRef = Saver.save(ObjSym.getName());
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000852 uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000853
Davide Italiano786d8e32016-09-29 00:40:08 +0000854 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
855 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
856 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000857
Peter Collingbourne7b30f162017-03-31 04:47:07 +0000858 int C = ObjSym.getComdatIndex();
Rafael Espindola3db70212016-10-25 12:02:31 +0000859 if (C != -1 && !KeptComdats[C])
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000860 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
861 Visibility, Type, CanOmitFromDynSym,
862 F);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000863
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000864 if (ObjSym.isUndefined())
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000865 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
866 Visibility, Type, CanOmitFromDynSym,
867 F);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000868
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000869 if (ObjSym.isCommon())
Davide Italiano786d8e32016-09-29 00:40:08 +0000870 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
871 ObjSym.getCommonAlignment(), Binding,
872 Visibility, STT_OBJECT, F);
873
874 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
875 CanOmitFromDynSym, F);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000876}
877
Peter Collingbourne4f952702016-05-01 04:55:03 +0000878template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000879void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Rafael Espindola3db70212016-10-25 12:02:31 +0000880 std::vector<bool> KeptComdats;
Peter Collingbourne7b30f162017-03-31 04:47:07 +0000881 for (StringRef S : Obj->getComdatTable())
882 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
Rafael Espindola3db70212016-10-25 12:02:31 +0000883
Davide Italiano786d8e32016-09-29 00:40:08 +0000884 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
Rui Ueyama55518e72016-10-28 20:57:25 +0000885 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000886}
887
Rui Ueyama330e52b2017-04-26 22:51:51 +0000888static ELFKind getELFKind(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000889 unsigned char Size;
890 unsigned char Endian;
891 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
Rui Ueyama330e52b2017-04-26 22:51:51 +0000892
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000893 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000894 fatal(MB.getBufferIdentifier() + ": invalid data encoding");
Rui Ueyama330e52b2017-04-26 22:51:51 +0000895 if (Size != ELFCLASS32 && Size != ELFCLASS64)
896 fatal(MB.getBufferIdentifier() + ": invalid file class");
Rui Ueyamac4b65062015-10-12 15:31:09 +0000897
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000898 size_t BufSize = MB.getBuffer().size();
899 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
900 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000901 fatal(MB.getBufferIdentifier() + ": file is too short");
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000902
Rui Ueyama330e52b2017-04-26 22:51:51 +0000903 if (Size == ELFCLASS32)
904 return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
905 return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000906}
907
Rafael Espindola093abab2016-10-27 17:45:40 +0000908template <class ELFT> void BinaryFile::parse() {
Rui Ueyamac9d82b92017-04-27 04:01:36 +0000909 ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000910 auto *Section =
911 make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
Rafael Espindola093abab2016-10-27 17:45:40 +0000912 Sections.push_back(Section);
913
Rui Ueyamac9d82b92017-04-27 04:01:36 +0000914 // For each input file foo that is embedded to a result as a binary
915 // blob, we define _binary_foo_{start,end,size} symbols, so that
916 // user programs can access blobs by name. Non-alphanumeric
917 // characters in a filename are replaced with underscore.
918 std::string S = "_binary_" + MB.getBufferIdentifier().str();
919 for (size_t I = 0; I < S.size(); ++I)
920 if (!isalnum(S[I]))
921 S[I] = '_';
922
923 elf::Symtab<ELFT>::X->addRegular(Saver.save(S + "_start"), STV_DEFAULT,
924 STT_OBJECT, 0, 0, STB_GLOBAL, Section,
George Rimar463984d2016-11-15 08:07:14 +0000925 nullptr);
Rui Ueyamac9d82b92017-04-27 04:01:36 +0000926 elf::Symtab<ELFT>::X->addRegular(Saver.save(S + "_end"), STV_DEFAULT,
927 STT_OBJECT, Data.size(), 0, STB_GLOBAL,
928 Section, nullptr);
929 elf::Symtab<ELFT>::X->addRegular(Saver.save(S + "_size"), STV_DEFAULT,
930 STT_OBJECT, Data.size(), 0, STB_GLOBAL,
931 nullptr, nullptr);
Michael J. Spencera9424f32016-09-09 22:08:04 +0000932}
933
Rui Ueyama4655ea32016-04-08 00:14:55 +0000934static bool isBitcode(MemoryBufferRef MB) {
935 using namespace sys::fs;
936 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
937}
938
Rui Ueyama55518e72016-10-28 20:57:25 +0000939InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
Davide Italianobcdd6c62016-10-12 19:35:54 +0000940 uint64_t OffsetInArchive) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000941 if (isBitcode(MB))
942 return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
943
944 switch (getELFKind(MB)) {
945 case ELF32LEKind:
946 return make<ObjectFile<ELF32LE>>(MB, ArchiveName);
947 case ELF32BEKind:
948 return make<ObjectFile<ELF32BE>>(MB, ArchiveName);
949 case ELF64LEKind:
950 return make<ObjectFile<ELF64LE>>(MB, ArchiveName);
951 case ELF64BEKind:
952 return make<ObjectFile<ELF64BE>>(MB, ArchiveName);
953 default:
954 llvm_unreachable("getELFKind");
955 }
Rui Ueyama533c0302016-01-06 00:09:43 +0000956}
957
Rafael Espindola3460cdd2017-04-24 21:44:20 +0000958InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000959 switch (getELFKind(MB)) {
960 case ELF32LEKind:
961 return make<SharedFile<ELF32LE>>(MB, DefaultSoName);
962 case ELF32BEKind:
963 return make<SharedFile<ELF32BE>>(MB, DefaultSoName);
964 case ELF64LEKind:
965 return make<SharedFile<ELF64LE>>(MB, DefaultSoName);
966 case ELF64BEKind:
967 return make<SharedFile<ELF64BE>>(MB, DefaultSoName);
968 default:
969 llvm_unreachable("getELFKind");
970 }
Rui Ueyama533c0302016-01-06 00:09:43 +0000971}
972
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000973MemoryBufferRef LazyObjectFile::getBuffer() {
974 if (Seen)
975 return MemoryBufferRef();
976 Seen = true;
977 return MB;
978}
979
Rafael Espindola808f2d32017-05-04 14:54:48 +0000980InputFile *LazyObjectFile::fetch() {
981 MemoryBufferRef MBRef = getBuffer();
982 if (MBRef.getBuffer().empty())
983 return nullptr;
Rafael Espindola80ae8ae2017-05-05 13:55:51 +0000984 return createObjectFile(MBRef, ArchiveName);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000985}
986
George Rimar10874f72016-10-03 11:13:55 +0000987template <class ELFT> void LazyObjectFile::parse() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000988 for (StringRef Sym : getSymbols())
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000989 Symtab<ELFT>::X->addLazyObject(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000990}
991
992template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
993 typedef typename ELFT::Shdr Elf_Shdr;
994 typedef typename ELFT::Sym Elf_Sym;
995 typedef typename ELFT::SymRange Elf_Sym_Range;
996
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000997 const ELFFile<ELFT> Obj(this->MB.getBuffer());
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000998 ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
Rafael Espindola6d18d382016-11-03 13:24:29 +0000999 for (const Elf_Shdr &Sec : Sections) {
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001000 if (Sec.sh_type != SHT_SYMTAB)
1001 continue;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001002
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001003 Elf_Sym_Range Syms = check(Obj.symbols(&Sec), toString(this));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001004 uint32_t FirstNonLocal = Sec.sh_info;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001005 StringRef StringTable =
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001006 check(Obj.getStringTableForSymtab(Sec, Sections), toString(this));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001007 std::vector<StringRef> V;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001008
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001009 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +00001010 if (Sym.st_shndx != SHN_UNDEF)
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001011 V.push_back(check(Sym.getName(StringTable), toString(this)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001012 return V;
1013 }
1014 return {};
1015}
1016
1017std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
Rui Ueyama37e60a52017-03-30 21:13:00 +00001018 std::unique_ptr<lto::InputFile> Obj =
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001019 check(lto::InputFile::create(this->MB), toString(this));
Rui Ueyamad72dd1f2016-09-29 00:58:10 +00001020 std::vector<StringRef> V;
1021 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
Peter Collingbourne0d56b952017-03-28 22:31:35 +00001022 if (!Sym.isUndefined())
Rui Ueyamad72dd1f2016-09-29 00:58:10 +00001023 V.push_back(Saver.save(Sym.getName()));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001024 return V;
1025}
1026
Rui Ueyama1f492892016-04-08 20:49:31 +00001027// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001028std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +00001029 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001030 return getBitcodeSymbols();
1031
Rui Ueyama330e52b2017-04-26 22:51:51 +00001032 switch (getELFKind(this->MB)) {
1033 case ELF32LEKind:
1034 return getElfSymbols<ELF32LE>();
1035 case ELF32BEKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001036 return getElfSymbols<ELF32BE>();
Rui Ueyama330e52b2017-04-26 22:51:51 +00001037 case ELF64LEKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001038 return getElfSymbols<ELF64LE>();
Rui Ueyama330e52b2017-04-26 22:51:51 +00001039 case ELF64BEKind:
1040 return getElfSymbols<ELF64BE>();
1041 default:
1042 llvm_unreachable("getELFKind");
1043 }
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001044}
1045
Peter Collingbourne4f952702016-05-01 04:55:03 +00001046template void ArchiveFile::parse<ELF32LE>();
1047template void ArchiveFile::parse<ELF32BE>();
1048template void ArchiveFile::parse<ELF64LE>();
1049template void ArchiveFile::parse<ELF64BE>();
1050
Rafael Espindola8b2c85362016-10-21 19:49:42 +00001051template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1052template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1053template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1054template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +00001055
1056template void LazyObjectFile::parse<ELF32LE>();
1057template void LazyObjectFile::parse<ELF32BE>();
1058template void LazyObjectFile::parse<ELF64LE>();
1059template void LazyObjectFile::parse<ELF64BE>();
1060
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001061template class elf::ELFFileBase<ELF32LE>;
1062template class elf::ELFFileBase<ELF32BE>;
1063template class elf::ELFFileBase<ELF64LE>;
1064template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +00001065
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001066template class elf::ObjectFile<ELF32LE>;
1067template class elf::ObjectFile<ELF32BE>;
1068template class elf::ObjectFile<ELF64LE>;
1069template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +00001070
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001071template class elf::SharedFile<ELF32LE>;
1072template class elf::SharedFile<ELF32BE>;
1073template class elf::SharedFile<ELF64LE>;
1074template class elf::SharedFile<ELF64BE>;
Michael J. Spencera9424f32016-09-09 22:08:04 +00001075
Rafael Espindola093abab2016-10-27 17:45:40 +00001076template void BinaryFile::parse<ELF32LE>();
1077template void BinaryFile::parse<ELF32BE>();
1078template void BinaryFile::parse<ELF64LE>();
1079template void BinaryFile::parse<ELF64BE>();