blob: 4d97d45f927c843e76bed8cb3373279e45537b66 [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"
Teresa Johnson1e390892016-11-11 05:35:22 +000019#include "llvm/Bitcode/BitcodeReader.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000020#include "llvm/CodeGen/Analysis.h"
Eugene Leviantb380b242016-10-26 11:07:09 +000021#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000022#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000023#include "llvm/IR/Module.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000024#include "llvm/LTO/LTO.h"
Michael J. Spencera9424f32016-09-09 22:08:04 +000025#include "llvm/MC/StringTableBuilder.h"
Eugene Leviantc4681202016-11-01 09:17:50 +000026#include "llvm/Object/ELFObjectFile.h"
Davide Italianoe02ba982016-09-08 21:18:38 +000027#include "llvm/Support/Path.h"
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000028#include "llvm/Support/TarWriter.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
Michael J. Spencer1b348a62015-09-04 22:28:10 +000031using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000033using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000034using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000035
36using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000037using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000038
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000039TarWriter *elf::Tar;
40
Eugene Leviantc4681202016-11-01 09:17:50 +000041namespace {
42// In ELF object file all section addresses are zero. If we have multiple
43// .text sections (when using -ffunction-section or comdat group) then
44// LLVM DWARF parser will not be able to parse .debug_line correctly, unless
45// we assign each section some unique address. This callback method assigns
46// each section an address equal to its offset in ELF object file.
47class ObjectInfo : public LoadedObjectInfo {
48public:
49 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override {
50 return static_cast<const ELFSectionRef &>(Sec).getOffset();
51 }
52 std::unique_ptr<LoadedObjectInfo> clone() const override {
53 return std::unique_ptr<LoadedObjectInfo>();
54 }
55};
56}
57
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000058Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
Rui Ueyamae6e206d2017-02-21 23:22:56 +000059 log(Path);
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000060 auto MBOrErr = MemoryBuffer::getFile(Path);
61 if (auto EC = MBOrErr.getError()) {
Rui Ueyamac8d3a832017-01-12 22:18:04 +000062 error("cannot open " + Path + ": " + EC.message());
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000063 return None;
64 }
Rui Ueyamae6e206d2017-02-21 23:22:56 +000065
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000066 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
67 MemoryBufferRef MBRef = MB->getMemBufferRef();
68 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
69
70 if (Tar)
71 Tar->append(relativeToRoot(Path), MBRef.getBuffer());
72 return MBRef;
73}
74
Rui Ueyama7463ada2016-11-02 19:51:41 +000075template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() {
Rui Ueyama7556f6b2016-11-02 18:42:13 +000076 std::unique_ptr<object::ObjectFile> Obj =
77 check(object::ObjectFile::createObjectFile(this->MB),
78 "createObjectFile failed");
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>
Rafael Espindolab4c9b812017-02-23 02:28:28 +000095std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase *S,
Rui Ueyama9cc84382017-02-24 19:52:52 +000096 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)
Eugene Leviantb380b242016-10-26 11:07:09 +0000103 return "";
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)
112 return "";
Rui Ueyama214a8972017-01-06 08:59:32 +0000113 return Info.FileName + ":" + std::to_string(Info.Line);
Eugene Leviantb380b242016-10-26 11:07:09 +0000114}
115
Rafael Espindola78db5a92016-05-09 21:40:06 +0000116// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Rui Ueyamace039262017-01-06 10:04:08 +0000117std::string lld::toString(const InputFile *F) {
Rafael Espindola78db5a92016-05-09 21:40:06 +0000118 if (!F)
119 return "(internal)";
120 if (!F->ArchiveName.empty())
121 return (F->ArchiveName + "(" + F->getName() + ")").str();
122 return F->getName();
123}
124
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000125template <class ELFT> static ELFKind getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000126 if (ELFT::TargetEndianness == support::little)
127 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
128 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +0000129}
130
131template <class ELFT>
Rafael Espindolae19abab2016-11-03 20:44:50 +0000132ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000133 EKind = getELFKind<ELFT>();
Rafael Espindolae19abab2016-11-03 20:44:50 +0000134 EMachine = getObj().getHeader()->e_machine;
135 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000136}
137
138template <class ELFT>
Rafael Espindola8e232572016-11-03 20:48:57 +0000139typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() {
George Rimar0b2d3742016-11-14 10:05:53 +0000140 return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end());
Davide Italiano6d328d32015-09-16 20:45:57 +0000141}
142
Rafael Espindola115f0f32015-11-03 14:13:40 +0000143template <class ELFT>
144uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000145 return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000146}
147
Rafael Espindola6d18d382016-11-03 13:24:29 +0000148template <class ELFT>
Rafael Espindola21d8be92016-11-03 15:43:47 +0000149void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
150 const Elf_Shdr *Symtab) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000151 FirstNonLocal = Symtab->sh_info;
Rafael Espindolae19abab2016-11-03 20:44:50 +0000152 Symbols = check(getObj().symbols(Symtab));
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000153 if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000154 fatal(toString(this) + ": invalid sh_info in symbol table");
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000155
Rafael Espindolae19abab2016-11-03 20:44:50 +0000156 StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000157}
158
159template <class ELFT>
Rui Ueyama55518e72016-10-28 20:57:25 +0000160elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
161 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000162
163template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +0000164ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000165 return makeArrayRef(this->SymbolBodies).slice(this->FirstNonLocal);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000166}
167
168template <class ELFT>
169ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000170 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000171 return this->SymbolBodies;
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000172 return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000173}
174
175template <class ELFT>
176ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000177 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000178 return this->SymbolBodies;
179 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000180}
181
Rafael Espindola444576d2015-10-09 19:25:07 +0000182template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000183void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000184 // Read section and symbol tables.
Rafael Espindola73c3a362016-11-08 15:51:00 +0000185 initializeSections(ComdatGroups);
186 initializeSymbols();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000187}
188
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000189// Sections with SHT_GROUP and comdat bits define comdat section groups.
190// They are identified and deduplicated by group name. This function
191// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000192template <class ELFT>
Rafael Espindola7c7abaf2016-11-03 02:28:13 +0000193StringRef
194elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
195 const Elf_Shdr &Sec) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000196 if (this->Symbols.empty())
Rafael Espindola21d8be92016-11-03 15:43:47 +0000197 this->initSymtab(Sections,
198 check(object::getSection<ELFT>(Sections, Sec.sh_link)));
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000199 const Elf_Sym *Sym =
200 check(object::getSymbol<ELFT>(this->Symbols, Sec.sh_info));
Rafael Espindola21d8be92016-11-03 15:43:47 +0000201 return check(Sym->getName(this->StringTable));
Rafael Espindola444576d2015-10-09 19:25:07 +0000202}
203
204template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000205ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000206elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000207 const ELFFile<ELFT> &Obj = this->getObj();
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000208 ArrayRef<Elf_Word> Entries =
209 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000210 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000211 fatal(toString(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000212 return Entries.slice(1);
213}
214
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000215template <class ELFT>
216bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000217 // We don't merge sections if -O0 (default is -O1). This makes sometimes
218 // the linker significantly faster, although the output will be bigger.
219 if (Config->Optimize == 0)
220 return false;
221
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000222 // Do not merge sections if generating a relocatable object. It makes
223 // the code simpler because we do not need to update relocation addends
224 // to reflect changes introduced by merging. Instead of that we write
225 // such "merge" sections into separate OutputSections and keep SHF_MERGE
226 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
227 // later during a final linking.
228 if (Config->Relocatable)
229 return false;
230
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000231 // A mergeable section with size 0 is useless because they don't have
232 // any data to merge. A mergeable string section with size 0 can be
233 // argued as invalid because it doesn't end with a null character.
234 // We'll avoid a mess by handling them as if they were non-mergeable.
235 if (Sec.sh_size == 0)
236 return false;
237
Rui Ueyamac75ef852016-09-21 03:22:18 +0000238 // Check for sh_entsize. The ELF spec is not clear about the zero
239 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
240 // the section does not hold a table of fixed-size entries". We know
241 // that Rust 1.13 produces a string mergeable section with a zero
242 // sh_entsize. Here we just accept it rather than being picky about it.
Rui Ueyama9cc84382017-02-24 19:52:52 +0000243 uint64_t EntSize = Sec.sh_entsize;
Rui Ueyamac75ef852016-09-21 03:22:18 +0000244 if (EntSize == 0)
245 return false;
246 if (Sec.sh_size % EntSize)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000247 fatal(toString(this) +
Rui Ueyamac75ef852016-09-21 03:22:18 +0000248 ": SHF_MERGE section size must be a multiple of sh_entsize");
249
Rui Ueyama9cc84382017-02-24 19:52:52 +0000250 uint64_t Flags = Sec.sh_flags;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000251 if (!(Flags & SHF_MERGE))
252 return false;
253 if (Flags & SHF_WRITE)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000254 fatal(toString(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000255
Peter Smith4df2e142016-05-18 11:40:16 +0000256 // Don't try to merge if the alignment is larger than the sh_entsize and this
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000257 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000258 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000259 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
260 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000261 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000262 if (Flags & SHF_STRINGS)
263 return true;
264
George Rimardcddfb62016-06-08 12:04:59 +0000265 return Sec.sh_addralign <= EntSize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000266}
267
268template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000269void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindola73c3a362016-11-08 15:51:00 +0000270 DenseSet<CachedHashStringRef> &ComdatGroups) {
271 ArrayRef<Elf_Shdr> ObjSections = check(this->getObj().sections());
Rafael Espindolae19abab2016-11-03 20:44:50 +0000272 const ELFFile<ELFT> &Obj = this->getObj();
Rafael Espindola235d82c2016-11-02 14:42:20 +0000273 uint64_t Size = ObjSections.size();
George Rimar3f7c3df2017-03-21 08:44:25 +0000274 this->Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000275 unsigned I = -1;
Rafael Espindola199e0042016-11-02 15:21:24 +0000276 StringRef SectionStringTable = check(Obj.getSectionStringTable(ObjSections));
Rafael Espindola235d82c2016-11-02 14:42:20 +0000277 for (const Elf_Shdr &Sec : ObjSections) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000278 ++I;
George Rimar3f7c3df2017-03-21 08:44:25 +0000279 if (this->Sections[I] == &InputSection::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000280 continue;
281
Rui Ueyamaaf9793d2016-10-04 16:47:49 +0000282 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
283 // if -r is given, we'll let the final link discard such sections.
284 // This is compatible with GNU.
285 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
George Rimar3f7c3df2017-03-21 08:44:25 +0000286 this->Sections[I] = &InputSection::Discarded;
Eugene Leviant27be5422016-09-28 08:42:02 +0000287 continue;
288 }
289
Rafael Espindolacde25132015-08-13 14:45:44 +0000290 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000291 case SHT_GROUP:
George Rimar3f7c3df2017-03-21 08:44:25 +0000292 this->Sections[I] = &InputSection::Discarded;
293 if (ComdatGroups
294 .insert(
295 CachedHashStringRef(getShtGroupSignature(ObjSections, Sec)))
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000296 .second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000297 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000298 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000299 if (SecIndex >= Size)
George Rimar3f7c3df2017-03-21 08:44:25 +0000300 fatal(toString(this) +
301 ": invalid section index in group: " + Twine(SecIndex));
302 this->Sections[SecIndex] = &InputSection::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000303 }
304 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000305 case SHT_SYMTAB:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000306 this->initSymtab(ObjSections, &Sec);
Rafael Espindolacde25132015-08-13 14:45:44 +0000307 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000308 case SHT_SYMTAB_SHNDX:
Rafael Espindola84d6a172016-11-03 12:21:00 +0000309 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, ObjSections));
Rafael Espindola20348222015-08-24 21:43:25 +0000310 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000311 case SHT_STRTAB:
312 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000313 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000314 default:
George Rimar3f7c3df2017-03-21 08:44:25 +0000315 this->Sections[I] = createInputSection(Sec, SectionStringTable);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000316 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000317
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000318 // .ARM.exidx sections have a reverse dependency on the InputSection they
319 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
320 if (Sec.sh_flags & SHF_LINK_ORDER) {
321 if (Sec.sh_link >= Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000322 fatal(toString(this) + ": invalid sh_link index: " +
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000323 Twine(Sec.sh_link));
George Rimar3f7c3df2017-03-21 08:44:25 +0000324 this->Sections[Sec.sh_link]->DependentSections.push_back(
325 this->Sections[I]);
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000326 }
Peter Smith07606052016-10-10 10:10:27 +0000327 }
328}
329
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000330template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000331InputSectionBase *elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000332 uint32_t Idx = Sec.sh_info;
George Rimar3f7c3df2017-03-21 08:44:25 +0000333 if (Idx >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000334 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
George Rimar3f7c3df2017-03-21 08:44:25 +0000335 InputSectionBase *Target = this->Sections[Idx];
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000336
337 // Strictly speaking, a relocation section must be included in the
338 // group of the section it relocates. However, LLVM 3.3 and earlier
339 // would fail to do so, so we gracefully handle that case.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000340 if (Target == &InputSection::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000341 return nullptr;
342
343 if (!Target)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000344 fatal(toString(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000345 return Target;
346}
347
348template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000349InputSectionBase *
Rafael Espindolaec05a572016-11-01 21:48:00 +0000350elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
351 StringRef SectionStringTable) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000352 StringRef Name =
353 check(this->getObj().getSectionName(&Sec, SectionStringTable));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000354
Rafael Espindola042a3f22016-09-08 14:06:08 +0000355 switch (Sec.sh_type) {
356 case SHT_ARM_ATTRIBUTES:
Peter Smith532bc982016-12-14 10:36:12 +0000357 // FIXME: ARM meta-data section. Retain the first attribute section
358 // we see. The eglibc ARM dynamic loaders require the presence of an
359 // attribute section for dlopen to work.
360 // In a full implementation we would merge all attribute sections.
361 if (In<ELFT>::ARMAttributes == nullptr) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000362 In<ELFT>::ARMAttributes = make<InputSection>(this, &Sec, Name);
Peter Smith532bc982016-12-14 10:36:12 +0000363 return In<ELFT>::ARMAttributes;
364 }
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000365 return &InputSection::Discarded;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000366 case SHT_RELA:
367 case SHT_REL: {
George Rimaree6f22c2017-02-14 16:42:38 +0000368 // Find the relocation target section and associate this
369 // section with it. Target can be discarded, for example
370 // if it is a duplicated member of SHT_GROUP section, we
371 // do not create or proccess relocatable sections then.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000372 InputSectionBase *Target = getRelocTarget(Sec);
George Rimaree6f22c2017-02-14 16:42:38 +0000373 if (!Target)
374 return nullptr;
375
Rafael Espindola042a3f22016-09-08 14:06:08 +0000376 // This section contains relocation information.
377 // If -r is given, we do not interpret or apply relocation
378 // but just copy relocation sections to output.
379 if (Config->Relocatable)
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000380 return make<InputSection>(this, &Sec, Name);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000381
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000382 if (Target->FirstRelocation)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000383 fatal(toString(this) +
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000384 ": multiple relocation sections to one section are not supported");
Rafael Espindola6119b862017-03-06 20:23:56 +0000385 if (isa<MergeInputSection>(Target))
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000386 fatal(toString(this) +
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000387 ": relocations pointing to SHF_MERGE are not supported");
388
389 size_t NumRelocations;
390 if (Sec.sh_type == SHT_RELA) {
391 ArrayRef<Elf_Rela> Rels = check(this->getObj().relas(&Sec));
392 Target->FirstRelocation = Rels.begin();
393 NumRelocations = Rels.size();
394 Target->AreRelocsRela = true;
395 } else {
396 ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec));
397 Target->FirstRelocation = Rels.begin();
398 NumRelocations = Rels.size();
399 Target->AreRelocsRela = false;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000400 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000401 assert(isUInt<31>(NumRelocations));
402 Target->NumRelocations = NumRelocations;
George Rimar82bd8be2017-02-08 16:18:10 +0000403
404 // Relocation sections processed by the linker are usually removed
405 // from the output, so returning `nullptr` for the normal case.
406 // However, if -emit-relocs is given, we need to leave them in the output.
407 // (Some post link analysis tools need this information.)
George Rimar858a6592017-02-17 19:46:47 +0000408 if (Config->EmitRelocs) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000409 InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
George Rimar858a6592017-02-17 19:46:47 +0000410 // We will not emit relocation section if target was discarded.
411 Target->DependentSections.push_back(RelocSec);
412 return RelocSec;
413 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000414 return nullptr;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000415 }
416 }
417
Rui Ueyama06332732017-02-23 07:06:43 +0000418 // The GNU linker uses .note.GNU-stack section as a marker indicating
419 // that the code in the object file does not expect that the stack is
420 // executable (in terms of NX bit). If all input files have the marker,
421 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
Rui Ueyama65efe352017-02-23 07:15:46 +0000422 // make the stack non-executable. Most object files have this section as
423 // of 2017.
Rui Ueyama06332732017-02-23 07:06:43 +0000424 //
425 // But making the stack non-executable is a norm today for security
Rui Ueyama65efe352017-02-23 07:15:46 +0000426 // reasons. Failure to do so may result in a serious security issue.
427 // Therefore, we make LLD always add PT_GNU_STACK unless it is
Rui Ueyama06332732017-02-23 07:06:43 +0000428 // explicitly told to do otherwise (by -z execstack). Because the stack
429 // executable-ness is controlled solely by command line options,
430 // .note.GNU-stack sections are simply ignored.
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000431 if (Name == ".note.GNU-stack")
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000432 return &InputSection::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000433
Rui Ueyamac1a0ac22017-02-23 07:35:11 +0000434 // Split stacks is a feature to support a discontiguous stack. At least
435 // as of 2017, it seems that the feature is not being used widely.
436 // Only GNU gold supports that. We don't. For the details about that,
437 // see https://gcc.gnu.org/wiki/SplitStacks
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000438 if (Name == ".note.GNU-split-stack") {
Rui Ueyamab7f39b02017-02-23 07:35:30 +0000439 error(toString(this) +
440 ": object file compiled with -fsplit-stack is not supported");
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000441 return &InputSection::Discarded;
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000442 }
443
George Rimarf21aade2016-08-31 08:38:11 +0000444 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000445 return &InputSection::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000446
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000447 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
448 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
449 // sections. Drop those sections to avoid duplicate symbol errors.
450 // FIXME: This is glibc PR20543, we should remove this hack once that has been
451 // fixed for a while.
452 if (Name.startswith(".gnu.linkonce."))
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000453 return &InputSection::Discarded;
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000454
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000455 // The linker merges EH (exception handling) frames and creates a
456 // .eh_frame_hdr section for runtime. So we handle them with a special
457 // class. For relocatable outputs, they are just passed through.
458 if (Name == ".eh_frame" && !Config->Relocatable)
Rafael Espindola5c02b742017-03-06 21:17:18 +0000459 return make<EhInputSection>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000460
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000461 if (shouldMerge(Sec))
Rafael Espindola6119b862017-03-06 20:23:56 +0000462 return make<MergeInputSection>(this, &Sec, Name);
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000463 return make<InputSection>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000464}
465
Rafael Espindola73c3a362016-11-08 15:51:00 +0000466template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rui Ueyama27119402016-11-03 18:20:08 +0000467 SymbolBodies.reserve(this->Symbols.size());
468 for (const Elf_Sym &Sym : this->Symbols)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000469 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000470}
471
472template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000473InputSectionBase *elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000474 uint32_t Index = this->getSectionIndex(Sym);
George Rimar3f7c3df2017-03-21 08:44:25 +0000475 if (Index >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000476 fatal(toString(this) + ": invalid section index: " + Twine(Index));
George Rimar3f7c3df2017-03-21 08:44:25 +0000477 InputSectionBase *S = this->Sections[Index];
George Rimar24adce92016-10-06 09:17:55 +0000478
George Rimar308752e2016-11-15 07:56:28 +0000479 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
480 // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
George Rimar683a35d2016-08-12 19:25:54 +0000481 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
George Rimar308752e2016-11-15 07:56:28 +0000482 // In this case it is fine for section to be null here as we do not
483 // allocate sections of these types.
George Rimar24adce92016-10-06 09:17:55 +0000484 if (!S) {
George Rimar308752e2016-11-15 07:56:28 +0000485 if (Index == 0 || Sym.getType() == STT_SECTION ||
486 Sym.getType() == STT_NOTYPE)
George Rimar24adce92016-10-06 09:17:55 +0000487 return nullptr;
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000488 fatal(toString(this) + ": invalid section index: " + Twine(Index));
George Rimar24adce92016-10-06 09:17:55 +0000489 }
490
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000491 if (S == &InputSection::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000492 return S;
493 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000494}
495
496template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000497SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000498 int Binding = Sym->getBinding();
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000499 InputSectionBase *Sec = getSection(*Sym);
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000500
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000501 uint8_t StOther = Sym->st_other;
502 uint8_t Type = Sym->getType();
Rui Ueyama9cc84382017-02-24 19:52:52 +0000503 uint64_t Value = Sym->st_value;
504 uint64_t Size = Sym->st_size;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000505
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000506 if (Binding == STB_LOCAL) {
Eugene Leviantb380b242016-10-26 11:07:09 +0000507 if (Sym->getType() == STT_FILE)
508 SourceFile = check(Sym->getName(this->StringTable));
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000509
510 if (this->StringTable.size() <= Sym->st_name)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000511 fatal(toString(this) + ": invalid symbol name offset");
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000512
Rui Ueyama84e65a72016-11-29 19:11:39 +0000513 StringRefZ Name = this->StringTable.data() + Sym->st_name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000514 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama175e81c2017-02-28 19:36:30 +0000515 return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type, this);
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000516
Rui Ueyama175e81c2017-02-28 19:36:30 +0000517 return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
518 Size, Sec, this);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000519 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000520
Rafael Espindola75714f62016-03-03 22:24:39 +0000521 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000522
Rafael Espindola4cda5812015-10-16 15:29:48 +0000523 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000524 case SHN_UNDEF:
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000525 return elf::Symtab<ELFT>::X
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000526 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000527 /*CanOmitFromDynSym=*/false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000528 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000529 case SHN_COMMON:
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000530 if (Value == 0 || Value >= UINT32_MAX)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000531 fatal(toString(this) + ": common symbol '" + Name +
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000532 "' has invalid alignment: " + Twine(Value));
533 return elf::Symtab<ELFT>::X
534 ->addCommon(Name, Size, Value, Binding, StOther, Type, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000535 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000536 }
Rafael Espindola20348222015-08-24 21:43:25 +0000537
Rafael Espindola67d72c02016-03-11 12:06:30 +0000538 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000539 default:
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000540 fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000541 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000542 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000543 case STB_GNU_UNIQUE:
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000544 if (Sec == &InputSection::Discarded)
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000545 return elf::Symtab<ELFT>::X
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000546 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000547 /*CanOmitFromDynSym=*/false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000548 ->body();
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000549 return elf::Symtab<ELFT>::X
550 ->addRegular(Name, StOther, Type, Value, Size, Binding, Sec, this)
551 ->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000552 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000553}
554
Peter Collingbourne4f952702016-05-01 04:55:03 +0000555template <class ELFT> void ArchiveFile::parse() {
Eugene Leviant7d7ff802016-11-21 09:28:07 +0000556 File = check(Archive::create(MB),
557 MB.getBufferIdentifier() + ": failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000558
Rui Ueyama3d0f77b2016-09-30 17:56:20 +0000559 // Read the symbol table to construct Lazy objects.
Bob Haarmanf790f782017-03-17 21:32:49 +0000560 for (const Archive::Symbol &Sym : File->symbols()) {
Rui Ueyama3d0f77b2016-09-30 17:56:20 +0000561 Symtab<ELFT>::X->addLazyArchive(this, Sym);
Bob Haarmanf790f782017-03-17 21:32:49 +0000562 }
563
564 if (File->symbols().begin() == File->symbols().end())
565 Config->ArchiveWithoutSymbolsSeen = true;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000566}
567
568// Returns a buffer pointing to a member file containing a given symbol.
Davide Italianobcdd6c62016-10-12 19:35:54 +0000569std::pair<MemoryBufferRef, uint64_t>
570ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000571 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000572 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000573 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000574
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000575 if (!Seen.insert(C.getChildOffset()).second)
Davide Italianobcdd6c62016-10-12 19:35:54 +0000576 return {MemoryBufferRef(), 0};
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000577
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000578 MemoryBufferRef Ret =
579 check(C.getMemoryBufferRef(),
580 "could not get the buffer for the member defining symbol " +
581 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000582
Rui Ueyamaec1c75e2017-01-09 01:42:02 +0000583 if (C.getParent()->isThin() && Tar)
584 Tar->append(relativeToRoot(check(C.getFullName())), Ret.getBuffer());
Davide Italianobcdd6c62016-10-12 19:35:54 +0000585 if (C.getParent()->isThin())
586 return {Ret, 0};
587 return {Ret, C.getChildOffset()};
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000588}
589
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000590template <class ELFT>
Rui Ueyama55518e72016-10-28 20:57:25 +0000591SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000592 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000593
Rafael Espindola115f0f32015-11-03 14:13:40 +0000594template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000595const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000596SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000597 return check(
598 this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000599}
600
Rui Ueyama7c713312016-01-06 01:56:36 +0000601// Partially parse the shared object file so that we can call
602// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000603template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rafael Espindolac8b15812015-10-01 15:47:50 +0000604 const Elf_Shdr *DynamicSec = nullptr;
605
Rafael Espindolae19abab2016-11-03 20:44:50 +0000606 const ELFFile<ELFT> Obj = this->getObj();
Rafael Espindola84d6a172016-11-03 12:21:00 +0000607 ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
608 for (const Elf_Shdr &Sec : Sections) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000609 switch (Sec.sh_type) {
610 default:
611 continue;
612 case SHT_DYNSYM:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000613 this->initSymtab(Sections, &Sec);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000614 break;
615 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000616 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000617 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000618 case SHT_SYMTAB_SHNDX:
Rafael Espindola84d6a172016-11-03 12:21:00 +0000619 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, Sections));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000620 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000621 case SHT_GNU_versym:
622 this->VersymSec = &Sec;
623 break;
624 case SHT_GNU_verdef:
625 this->VerdefSec = &Sec;
626 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000627 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000628 }
629
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000630 if (this->VersymSec && this->Symbols.empty())
George Rimarbcba39a2016-11-02 10:16:25 +0000631 error("SHT_GNU_versym should be associated with symbol table");
632
Rui Ueyama478f8eb2016-09-09 21:35:38 +0000633 // DSOs are identified by soname, and they usually contain
634 // DT_SONAME tag in their header. But if they are missing,
635 // filenames are used as default sonames.
Davide Italianoe02ba982016-09-08 21:18:38 +0000636 SoName = sys::path::filename(this->getName());
Rafael Espindolac8b15812015-10-01 15:47:50 +0000637
Rui Ueyama361d8b92015-10-12 15:49:02 +0000638 if (!DynamicSec)
639 return;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000640
George Rimar53cf2a82016-10-07 09:01:04 +0000641 ArrayRef<Elf_Dyn> Arr =
642 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000643 toString(this) + ": getSectionContentsAsArray failed");
George Rimar53cf2a82016-10-07 09:01:04 +0000644 for (const Elf_Dyn &Dyn : Arr) {
Rui Ueyama361d8b92015-10-12 15:49:02 +0000645 if (Dyn.d_tag == DT_SONAME) {
Rui Ueyama9cc84382017-02-24 19:52:52 +0000646 uint64_t Val = Dyn.getVal();
Rui Ueyama361d8b92015-10-12 15:49:02 +0000647 if (Val >= this->StringTable.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000648 fatal(toString(this) + ": invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000649 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000650 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000651 }
652 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000653}
Rafael Espindola18173d42015-09-08 15:50:05 +0000654
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000655// Parse the version definitions in the object file if present. Returns a vector
656// whose nth element contains a pointer to the Elf_Verdef for version identifier
657// n. Version identifiers that are not definitions map to nullptr. The array
658// always has at least length 1.
659template <class ELFT>
660std::vector<const typename ELFT::Verdef *>
661SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
662 std::vector<const Elf_Verdef *> Verdefs(1);
663 // We only need to process symbol versions for this DSO if it has both a
664 // versym and a verdef section, which indicates that the DSO contains symbol
665 // version definitions.
666 if (!VersymSec || !VerdefSec)
667 return Verdefs;
668
669 // The location of the first global versym entry.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000670 const char *Base = this->MB.getBuffer().data();
671 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000672 this->FirstNonLocal;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000673
674 // We cannot determine the largest verdef identifier without inspecting
675 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
676 // sequentially starting from 1, so we predict that the largest identifier
677 // will be VerdefCount.
678 unsigned VerdefCount = VerdefSec->sh_info;
679 Verdefs.resize(VerdefCount + 1);
680
681 // Build the Verdefs array by following the chain of Elf_Verdef objects
682 // from the start of the .gnu.version_d section.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000683 const char *Verdef = Base + VerdefSec->sh_offset;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000684 for (unsigned I = 0; I != VerdefCount; ++I) {
685 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
686 Verdef += CurVerdef->vd_next;
687 unsigned VerdefIndex = CurVerdef->vd_ndx;
688 if (Verdefs.size() <= VerdefIndex)
689 Verdefs.resize(VerdefIndex + 1);
690 Verdefs[VerdefIndex] = CurVerdef;
691 }
692
693 return Verdefs;
694}
695
Rui Ueyama7c713312016-01-06 01:56:36 +0000696// Fully parse the shared object file. This must be called after parseSoName().
697template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000698 // Create mapping from version identifiers to Elf_Verdef entries.
699 const Elf_Versym *Versym = nullptr;
700 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
701
Rafael Espindola8e232572016-11-03 20:48:57 +0000702 Elf_Sym_Range Syms = this->getGlobalSymbols();
Rafael Espindola18173d42015-09-08 15:50:05 +0000703 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000704 unsigned VersymIndex = 0;
705 if (Versym) {
706 VersymIndex = Versym->vs_index;
707 ++Versym;
708 }
Rafael Espindola2756e042017-01-06 22:30:35 +0000709 bool Hidden = VersymIndex & VERSYM_HIDDEN;
710 VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000711
Rafael Espindola18da0e52016-04-29 16:23:31 +0000712 StringRef Name = check(Sym.getName(this->StringTable));
713 if (Sym.isUndefined()) {
714 Undefs.push_back(Name);
715 continue;
716 }
717
Rafael Espindola2756e042017-01-06 22:30:35 +0000718 // Ignore local symbols.
719 if (Versym && VersymIndex == VER_NDX_LOCAL)
720 continue;
Rafael Espindolad2454d62016-06-09 15:45:49 +0000721
722 const Elf_Verdef *V =
723 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
Rafael Espindola2756e042017-01-06 22:30:35 +0000724
725 if (!Hidden)
726 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
727
728 // Also add the symbol with the versioned name to handle undefined symbols
729 // with explicit versions.
730 if (V) {
731 StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
732 Name = Saver.save(Twine(Name) + "@" + VerName);
733 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
734 }
Rafael Espindola18173d42015-09-08 15:50:05 +0000735 }
736}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000737
Rui Ueyama80356882016-08-03 20:33:17 +0000738static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
Peter Collingbournecd513a42016-11-11 19:50:24 +0000739 Triple T(check(getBitcodeTargetTriple(MB)));
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000740 if (T.isLittleEndian())
741 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
742 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000743}
744
Rui Ueyama80356882016-08-03 20:33:17 +0000745static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
Peter Collingbournecd513a42016-11-11 19:50:24 +0000746 Triple T(check(getBitcodeTargetTriple(MB)));
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000747 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000748 case Triple::aarch64:
749 return EM_AARCH64;
750 case Triple::arm:
Sean Silva1d961852017-02-28 03:00:48 +0000751 case Triple::thumb:
Rui Ueyama523744d2016-07-07 02:46:30 +0000752 return EM_ARM;
753 case Triple::mips:
754 case Triple::mipsel:
755 case Triple::mips64:
756 case Triple::mips64el:
757 return EM_MIPS;
758 case Triple::ppc:
759 return EM_PPC;
760 case Triple::ppc64:
761 return EM_PPC64;
762 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000763 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000764 case Triple::x86_64:
765 return EM_X86_64;
766 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000767 fatal(MB.getBufferIdentifier() +
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000768 ": could not infer e_machine from bitcode target triple " + T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000769 }
770}
771
Rafael Espindolab57e4962017-03-20 23:47:06 +0000772BitcodeFile::BitcodeFile(MemoryBufferRef MB, uint64_t OffsetInArchive)
773 : InputFile(BitcodeKind, MB), OffsetInArchive(OffsetInArchive) {
Rui Ueyama80356882016-08-03 20:33:17 +0000774 EKind = getBitcodeELFKind(MB);
775 EMachine = getBitcodeMachineKind(MB);
Davide Italiano60976ba2016-06-29 06:12:39 +0000776}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000777
Davide Italiano786d8e32016-09-29 00:40:08 +0000778static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
779 switch (GvVisibility) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000780 case GlobalValue::DefaultVisibility:
781 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000782 case GlobalValue::HiddenVisibility:
783 return STV_HIDDEN;
784 case GlobalValue::ProtectedVisibility:
785 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000786 }
George Rimar777f9632016-03-12 08:31:34 +0000787 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000788}
789
Peter Collingbourne4f952702016-05-01 04:55:03 +0000790template <class ELFT>
Rafael Espindola3db70212016-10-25 12:02:31 +0000791static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
Davide Italiano786d8e32016-09-29 00:40:08 +0000792 const lto::InputFile::Symbol &ObjSym,
Rui Ueyama55518e72016-10-28 20:57:25 +0000793 BitcodeFile *F) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000794 StringRef NameRef = Saver.save(ObjSym.getName());
795 uint32_t Flags = ObjSym.getFlags();
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000796 uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000797
Davide Italiano786d8e32016-09-29 00:40:08 +0000798 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
799 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
800 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000801
Rafael Espindola3db70212016-10-25 12:02:31 +0000802 int C = check(ObjSym.getComdatIndex());
803 if (C != -1 && !KeptComdats[C])
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000804 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
805 Visibility, Type, CanOmitFromDynSym,
806 F);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000807
Davide Italiano9f8efff2016-04-22 18:26:33 +0000808 if (Flags & BasicSymbolRef::SF_Undefined)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000809 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
810 Visibility, Type, CanOmitFromDynSym,
811 F);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000812
Davide Italiano786d8e32016-09-29 00:40:08 +0000813 if (Flags & BasicSymbolRef::SF_Common)
814 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
815 ObjSym.getCommonAlignment(), Binding,
816 Visibility, STT_OBJECT, F);
817
818 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
819 CanOmitFromDynSym, F);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000820}
821
Peter Collingbourne4f952702016-05-01 04:55:03 +0000822template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000823void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Davide Italianobcdd6c62016-10-12 19:35:54 +0000824
825 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
826 // (the fully resolved path of the archive) + member name + offset of the
827 // member in the archive.
828 // ThinLTO uses the MemoryBufferRef identifier to access its internal
829 // data structures and if two archives define two members with the same name,
830 // this causes a collision which result in only one of the objects being
831 // taken into consideration at LTO time (which very likely causes undefined
832 // symbols later in the link stage).
833 Obj = check(lto::InputFile::create(MemoryBufferRef(
834 MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() +
835 utostr(OffsetInArchive)))));
Rafael Espindola3db70212016-10-25 12:02:31 +0000836
837 std::vector<bool> KeptComdats;
838 for (StringRef S : Obj->getComdatTable()) {
839 StringRef N = Saver.save(S);
840 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(N)).second);
841 }
842
Davide Italiano786d8e32016-09-29 00:40:08 +0000843 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
Rui Ueyama55518e72016-10-28 20:57:25 +0000844 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000845}
846
Rui Ueyamac4b65062015-10-12 15:31:09 +0000847template <template <class> class T>
Rui Ueyama55518e72016-10-28 20:57:25 +0000848static InputFile *createELFFile(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000849 unsigned char Size;
850 unsigned char Endian;
851 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
852 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000853 fatal(MB.getBufferIdentifier() + ": invalid data encoding");
Rui Ueyamac4b65062015-10-12 15:31:09 +0000854
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000855 size_t BufSize = MB.getBuffer().size();
856 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
857 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000858 fatal(MB.getBufferIdentifier() + ": file is too short");
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000859
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000860 InputFile *Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000861 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000862 Obj = make<T<ELF32LE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000863 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000864 Obj = make<T<ELF32BE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000865 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000866 Obj = make<T<ELF64LE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000867 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000868 Obj = make<T<ELF64BE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000869 else
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000870 fatal(MB.getBufferIdentifier() + ": invalid file class");
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000871
872 if (!Config->FirstElf)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000873 Config->FirstElf = Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000874 return Obj;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000875}
876
Rafael Espindola093abab2016-10-27 17:45:40 +0000877template <class ELFT> void BinaryFile::parse() {
878 StringRef Buf = MB.getBuffer();
879 ArrayRef<uint8_t> Data =
880 makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size());
Rui Ueyama673c9d92016-10-20 06:44:58 +0000881
Rafael Espindola093abab2016-10-27 17:45:40 +0000882 std::string Filename = MB.getBufferIdentifier();
883 std::transform(Filename.begin(), Filename.end(), Filename.begin(),
884 [](char C) { return isalnum(C) ? C : '_'; });
885 Filename = "_binary_" + Filename;
886 StringRef StartName = Saver.save(Twine(Filename) + "_start");
887 StringRef EndName = Saver.save(Twine(Filename) + "_end");
888 StringRef SizeName = Saver.save(Twine(Filename) + "_size");
889
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000890 auto *Section =
891 make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
Rafael Espindola093abab2016-10-27 17:45:40 +0000892 Sections.push_back(Section);
893
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000894 elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, STT_OBJECT, 0, 0,
George Rimar463984d2016-11-15 08:07:14 +0000895 STB_GLOBAL, Section, nullptr);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000896 elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, STT_OBJECT,
George Rimar463984d2016-11-15 08:07:14 +0000897 Data.size(), 0, STB_GLOBAL, Section,
898 nullptr);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000899 elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, STT_OBJECT,
George Rimar463984d2016-11-15 08:07:14 +0000900 Data.size(), 0, STB_GLOBAL, nullptr,
901 nullptr);
Michael J. Spencera9424f32016-09-09 22:08:04 +0000902}
903
Rui Ueyama4655ea32016-04-08 00:14:55 +0000904static bool isBitcode(MemoryBufferRef MB) {
905 using namespace sys::fs;
906 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
907}
908
Rui Ueyama55518e72016-10-28 20:57:25 +0000909InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
Davide Italianobcdd6c62016-10-12 19:35:54 +0000910 uint64_t OffsetInArchive) {
Rafael Espindolab57e4962017-03-20 23:47:06 +0000911 InputFile *F = isBitcode(MB) ? make<BitcodeFile>(MB, OffsetInArchive)
912 : createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000913 F->ArchiveName = ArchiveName;
914 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000915}
916
Rui Ueyama55518e72016-10-28 20:57:25 +0000917InputFile *elf::createSharedFile(MemoryBufferRef MB) {
918 return createELFFile<SharedFile>(MB);
Rui Ueyama533c0302016-01-06 00:09:43 +0000919}
920
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000921MemoryBufferRef LazyObjectFile::getBuffer() {
922 if (Seen)
923 return MemoryBufferRef();
924 Seen = true;
925 return MB;
926}
927
George Rimar10874f72016-10-03 11:13:55 +0000928template <class ELFT> void LazyObjectFile::parse() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000929 for (StringRef Sym : getSymbols())
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000930 Symtab<ELFT>::X->addLazyObject(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000931}
932
933template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
934 typedef typename ELFT::Shdr Elf_Shdr;
935 typedef typename ELFT::Sym Elf_Sym;
936 typedef typename ELFT::SymRange Elf_Sym_Range;
937
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000938 const ELFFile<ELFT> Obj(this->MB.getBuffer());
Rafael Espindola6d18d382016-11-03 13:24:29 +0000939 ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
940 for (const Elf_Shdr &Sec : Sections) {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000941 if (Sec.sh_type != SHT_SYMTAB)
942 continue;
Rafael Espindoladd6abaa2016-11-03 13:43:51 +0000943 Elf_Sym_Range Syms = check(Obj.symbols(&Sec));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000944 uint32_t FirstNonLocal = Sec.sh_info;
Rafael Espindola6d18d382016-11-03 13:24:29 +0000945 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec, Sections));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000946 std::vector<StringRef> V;
947 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +0000948 if (Sym.st_shndx != SHN_UNDEF)
949 V.push_back(check(Sym.getName(StringTable)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000950 return V;
951 }
952 return {};
953}
954
955std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
Davide Italiano786d8e32016-09-29 00:40:08 +0000956 std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB));
Rui Ueyamad72dd1f2016-09-29 00:58:10 +0000957 std::vector<StringRef> V;
958 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
959 if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined))
960 V.push_back(Saver.save(Sym.getName()));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000961 return V;
962}
963
Rui Ueyama1f492892016-04-08 20:49:31 +0000964// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000965std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +0000966 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000967 return getBitcodeSymbols();
968
Rui Ueyama4655ea32016-04-08 00:14:55 +0000969 unsigned char Size;
970 unsigned char Endian;
971 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
972 if (Size == ELFCLASS32) {
973 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000974 return getElfSymbols<ELF32LE>();
975 return getElfSymbols<ELF32BE>();
976 }
Rui Ueyama4655ea32016-04-08 00:14:55 +0000977 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000978 return getElfSymbols<ELF64LE>();
979 return getElfSymbols<ELF64BE>();
980}
981
Peter Collingbourne4f952702016-05-01 04:55:03 +0000982template void ArchiveFile::parse<ELF32LE>();
983template void ArchiveFile::parse<ELF32BE>();
984template void ArchiveFile::parse<ELF64LE>();
985template void ArchiveFile::parse<ELF64BE>();
986
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000987template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
988template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
989template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
990template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000991
992template void LazyObjectFile::parse<ELF32LE>();
993template void LazyObjectFile::parse<ELF32BE>();
994template void LazyObjectFile::parse<ELF64LE>();
995template void LazyObjectFile::parse<ELF64BE>();
996
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000997template class elf::ELFFileBase<ELF32LE>;
998template class elf::ELFFileBase<ELF32BE>;
999template class elf::ELFFileBase<ELF64LE>;
1000template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +00001001
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001002template class elf::ObjectFile<ELF32LE>;
1003template class elf::ObjectFile<ELF32BE>;
1004template class elf::ObjectFile<ELF64LE>;
1005template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +00001006
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001007template class elf::SharedFile<ELF32LE>;
1008template class elf::SharedFile<ELF32BE>;
1009template class elf::SharedFile<ELF64LE>;
1010template class elf::SharedFile<ELF64BE>;
Michael J. Spencera9424f32016-09-09 22:08:04 +00001011
Rafael Espindola093abab2016-10-27 17:45:40 +00001012template void BinaryFile::parse<ELF32LE>();
1013template void BinaryFile::parse<ELF32BE>();
1014template void BinaryFile::parse<ELF64LE>();
1015template void BinaryFile::parse<ELF64BE>();