blob: 578d5a29fe6ff5f3b95be4105ef3afb7ed8f18bf [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 Espindola156f4ee2016-04-28 19:30:41 +000011#include "Driver.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000012#include "Error.h"
Rafael Espindola9d13d042016-02-11 15:24:48 +000013#include "InputSection.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014#include "Symbols.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000015#include "llvm/ADT/STLExtras.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000016#include "llvm/CodeGen/Analysis.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000017#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000018#include "llvm/IR/Module.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000019#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
Michael J. Spencer1b348a62015-09-04 22:28:10 +000021using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000022using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000023using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000024using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000027using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000029template <class ELFT>
30static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000031 std::error_code EC;
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000032 ELFFile<ELFT> F(MB.getBuffer(), EC);
Rafael Espindola75714f62016-03-03 22:24:39 +000033 check(EC);
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000034 return F;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000035}
36
Rafael Espindola18173d42015-09-08 15:50:05 +000037template <class ELFT>
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000038ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
39 : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000040
41template <class ELFT>
Rui Ueyama2022e812015-11-20 02:10:52 +000042ELFKind ELFFileBase<ELFT>::getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +000043 if (ELFT::TargetEndianness == support::little)
44 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
45 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +000046}
47
48template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000049typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
Rafael Espindola18173d42015-09-08 15:50:05 +000050 if (!Symtab)
51 return Elf_Sym_Range(nullptr, nullptr);
Rafael Espindolae1901cc2015-09-24 15:11:50 +000052 Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
Rafael Espindola18173d42015-09-08 15:50:05 +000053 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
54 uint32_t FirstNonLocal = Symtab->sh_info;
55 if (FirstNonLocal > NumSymbols)
George Rimar777f9632016-03-12 08:31:34 +000056 fatal("invalid sh_info in symbol table");
Rafael Espindola67d72c02016-03-11 12:06:30 +000057
58 if (OnlyGlobals)
Rafael Espindola0f7ccc32016-04-05 14:47:28 +000059 return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
60 return makeArrayRef(Syms.begin(), Syms.end());
Davide Italiano6d328d32015-09-16 20:45:57 +000061}
62
Rafael Espindola115f0f32015-11-03 14:13:40 +000063template <class ELFT>
64uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000065 uint32_t I = Sym.st_shndx;
66 if (I == ELF::SHN_XINDEX)
Rui Ueyamae69ab102016-01-06 01:14:11 +000067 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
Rafael Espindola972b2362016-03-09 14:31:18 +000068 if (I >= ELF::SHN_LORESERVE)
Rafael Espindola115f0f32015-11-03 14:13:40 +000069 return 0;
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000070 return I;
Rafael Espindola115f0f32015-11-03 14:13:40 +000071}
72
Rafael Espindolaaf707642015-10-12 01:55:32 +000073template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
Rafael Espindola3e603792015-10-01 20:26:37 +000074 if (!Symtab)
75 return;
Rafael Espindola75714f62016-03-03 22:24:39 +000076 StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000077}
78
79template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000080elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
Rafael Espindola2a4b2712015-10-13 01:17:02 +000081 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000082
83template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +000084ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
85 if (!this->Symtab)
86 return this->SymbolBodies;
87 uint32_t FirstNonLocal = this->Symtab->sh_info;
88 return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
89}
90
91template <class ELFT>
92ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
93 if (!this->Symtab)
94 return this->SymbolBodies;
95 uint32_t FirstNonLocal = this->Symtab->sh_info;
96 return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
97}
98
99template <class ELFT>
100ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
101 if (!this->Symtab)
102 return this->SymbolBodies;
103 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000104}
105
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000106template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Rui Ueyama70eed362016-01-06 22:42:43 +0000107 if (MipsReginfo)
108 return MipsReginfo->Reginfo->ri_gp_value;
109 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +0000110}
111
Rafael Espindola444576d2015-10-09 19:25:07 +0000112template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000113void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114 // Read section and symbol tables.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000115 initializeSections(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000116 initializeSymbols();
117}
118
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000119// Sections with SHT_GROUP and comdat bits define comdat section groups.
120// They are identified and deduplicated by group name. This function
121// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000122template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000123StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000124 const ELFFile<ELFT> &Obj = this->ELFObj;
125 uint32_t SymtabdSectionIndex = Sec.sh_link;
Rafael Espindola75714f62016-03-03 22:24:39 +0000126 const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex));
Rafael Espindola444576d2015-10-09 19:25:07 +0000127 uint32_t SymIndex = Sec.sh_info;
128 const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
Rafael Espindola75714f62016-03-03 22:24:39 +0000129 StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec));
130 return check(Sym->getName(StringTable));
Rafael Espindola444576d2015-10-09 19:25:07 +0000131}
132
133template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000134ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000135elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000136 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000137 ArrayRef<Elf_Word> Entries =
138 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000139 if (Entries.empty() || Entries[0] != GRP_COMDAT)
George Rimar777f9632016-03-12 08:31:34 +0000140 fatal("unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000141 return Entries.slice(1);
142}
143
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000144template <class ELFT> static bool shouldMerge(const typename ELFT::Shdr &Sec) {
145 typedef typename ELFT::uint uintX_t;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000146 uintX_t Flags = Sec.sh_flags;
147 if (!(Flags & SHF_MERGE))
148 return false;
149 if (Flags & SHF_WRITE)
George Rimar777f9632016-03-12 08:31:34 +0000150 fatal("writable SHF_MERGE sections are not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000151 uintX_t EntSize = Sec.sh_entsize;
Rafael Espindola0d2ad422016-03-21 14:57:20 +0000152 if (!EntSize || Sec.sh_size % EntSize)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000153 fatal("SHF_MERGE section size must be a multiple of sh_entsize");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000154
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000155 // Don't try to merge if the aligment is larger than the sh_entsize and this
156 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000157 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000158 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
159 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000160 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000161 if (Flags & SHF_STRINGS)
162 return true;
163
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000164 if (Sec.sh_addralign > EntSize)
165 return false;
166
167 return true;
168}
169
170template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000171void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000172 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000173 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000174 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000175 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000176 const ELFFile<ELFT> &Obj = this->ELFObj;
177 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000178 ++I;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000179 if (Sections[I] == &InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000180 continue;
181
Rafael Espindolacde25132015-08-13 14:45:44 +0000182 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000183 case SHT_GROUP:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000184 Sections[I] = &InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000185 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000186 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000187 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000188 if (SecIndex >= Size)
George Rimar777f9632016-03-12 08:31:34 +0000189 fatal("invalid section index in group");
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000190 Sections[SecIndex] = &InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000191 }
192 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000193 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000194 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000195 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000196 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000197 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000198 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000199 case SHT_STRTAB:
200 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000201 break;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000202 case SHT_RELA:
203 case SHT_REL: {
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000204 // This section contains relocation information.
205 // If -r is given, we do not interpret or apply relocation
206 // but just copy relocation sections to output.
George Rimar58941ee2016-02-25 08:23:37 +0000207 if (Config->Relocatable) {
Ivan Krasinbfc91312016-04-06 01:11:10 +0000208 Sections[I] = new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000209 break;
210 }
211
212 // Find the relocation target section and associate this
213 // section with it.
214 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
215 if (!Target)
216 break;
217 if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000218 S->RelocSections.push_back(&Sec);
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000219 break;
220 }
221 if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000222 if (S->RelocSection)
George Rimar777f9632016-03-12 08:31:34 +0000223 fatal("multiple relocation sections to .eh_frame are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000224 S->RelocSection = &Sec;
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000225 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000226 }
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000227 fatal("relocations pointing to SHF_MERGE are not supported");
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000228 }
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000229 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000230 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000231 }
232 }
233}
234
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000235template <class ELFT>
236InputSectionBase<ELFT> *
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000237elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
238 uint32_t Idx = Sec.sh_info;
239 if (Idx >= Sections.size())
240 fatal("invalid relocated section index");
241 InputSectionBase<ELFT> *Target = Sections[Idx];
242
243 // Strictly speaking, a relocation section must be included in the
244 // group of the section it relocates. However, LLVM 3.3 and earlier
245 // would fail to do so, so we gracefully handle that case.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000246 if (Target == &InputSection<ELFT>::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000247 return nullptr;
248
249 if (!Target)
250 fatal("unsupported relocation reference");
251 return Target;
252}
253
254template <class ELFT>
255InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000256elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola75714f62016-03-03 22:24:39 +0000257 StringRef Name = check(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000258
259 // .note.GNU-stack is a marker section to control the presence of
260 // PT_GNU_STACK segment in outputs. Since the presence of the segment
261 // is controlled only by the command line option (-z execstack) in LLD,
262 // .note.GNU-stack is ignored.
263 if (Name == ".note.GNU-stack")
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000264 return &InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000265
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000266 if (Name == ".note.GNU-split-stack") {
George Rimar777f9632016-03-12 08:31:34 +0000267 error("objects using splitstacks are not supported");
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000268 return &InputSection<ELFT>::Discarded;
269 }
270
271 if (Config->StripDebug && Name.startswith(".debug"))
272 return &InputSection<ELFT>::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000273
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000274 // A MIPS object file has a special section that contains register
275 // usage info, which needs to be handled by the linker specially.
Simon Atanasyan57830b62015-12-25 13:02:13 +0000276 if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
Rui Ueyamaec6aee02016-04-06 02:52:47 +0000277 MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec));
278 return MipsReginfo.get();
Simon Atanasyan57830b62015-12-25 13:02:13 +0000279 }
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000280
George Rimar4cfe5722016-03-03 07:49:35 +0000281 // We dont need special handling of .eh_frame sections if relocatable
282 // output was choosen. Proccess them as usual input sections.
283 if (!Config->Relocatable && Name == ".eh_frame")
Rui Ueyamae69ab102016-01-06 01:14:11 +0000284 return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000285 if (shouldMerge<ELFT>(Sec))
Rui Ueyamae69ab102016-01-06 01:14:11 +0000286 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
Ivan Krasinbfc91312016-04-06 01:11:10 +0000287 return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000288}
289
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000290template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000291 this->initStringTable();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000292 Elf_Sym_Range Syms = this->getElfSymbols(false);
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000293 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000294 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000295 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000296 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000297}
298
299template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000300InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000301elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000302 uint32_t Index = this->getSectionIndex(Sym);
303 if (Index == 0)
Rafael Espindola4cda5812015-10-16 15:29:48 +0000304 return nullptr;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000305 if (Index >= Sections.size() || !Sections[Index])
George Rimar777f9632016-03-12 08:31:34 +0000306 fatal("invalid section index");
Rui Ueyama0b289522016-02-25 18:43:51 +0000307 InputSectionBase<ELFT> *S = Sections[Index];
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000308 if (S == &InputSectionBase<ELFT>::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000309 return S;
310 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000311}
312
313template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000314SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama376ab7c2016-04-05 17:47:29 +0000315 unsigned char Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000316 InputSectionBase<ELFT> *Sec = getSection(*Sym);
317 if (Binding == STB_LOCAL) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000318 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000319 return new (Alloc) Undefined(Sym->st_name, Sym->st_other, Sym->getType());
Rafael Espindolad9a17172016-04-05 11:47:46 +0000320 return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000321 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000322
Rafael Espindola75714f62016-03-03 22:24:39 +0000323 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000324
Rafael Espindola4cda5812015-10-16 15:29:48 +0000325 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000326 case SHN_UNDEF:
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000327 return new (Alloc) Undefined(Name, Binding, Sym->st_other, Sym->getType(),
328 /*IsBitcode*/ false);
Rafael Espindola51d46902015-08-28 21:26:51 +0000329 case SHN_COMMON:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000330 return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value, Binding,
331 Sym->st_other, Sym->getType());
Rafael Espindola51d46902015-08-28 21:26:51 +0000332 }
Rafael Espindola20348222015-08-24 21:43:25 +0000333
Rafael Espindola67d72c02016-03-11 12:06:30 +0000334 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000335 default:
George Rimar57610422016-03-11 14:43:02 +0000336 fatal("unexpected binding");
Rafael Espindolab13df652015-08-11 17:33:02 +0000337 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000338 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000339 case STB_GNU_UNIQUE:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000340 if (Sec == &InputSection<ELFT>::Discarded)
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000341 return new (Alloc) Undefined(Name, Binding, Sym->st_other, Sym->getType(),
342 /*IsBitcode*/ false);
Rui Ueyamae69ab102016-01-06 01:14:11 +0000343 return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
Rafael Espindola444576d2015-10-09 19:25:07 +0000344 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000345}
346
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000347void ArchiveFile::parse() {
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000348 File = check(Archive::create(MB), "failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000349
350 // Allocate a buffer for Lazy objects.
351 size_t NumSyms = File->getNumberOfSymbols();
352 LazySymbols.reserve(NumSyms);
353
354 // Read the symbol table to construct Lazy objects.
355 for (const Archive::Symbol &Sym : File->symbols())
356 LazySymbols.emplace_back(this, Sym);
357}
358
359// Returns a buffer pointing to a member file containing a given symbol.
360MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000361 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000362 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000363 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000364
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000365 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000366 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000367
Rafael Espindola75714f62016-03-03 22:24:39 +0000368 return check(C.getMemoryBufferRef(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000369 "could not get the buffer for the member defining symbol " +
Rafael Espindola1130935c2016-03-03 16:21:44 +0000370 Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000371}
372
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000373template <class ELFT>
374SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000375 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000376
Rafael Espindola115f0f32015-11-03 14:13:40 +0000377template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000378const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000379SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
380 uint32_t Index = this->getSectionIndex(Sym);
381 if (Index == 0)
382 return nullptr;
Rafael Espindola75714f62016-03-03 22:24:39 +0000383 return check(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000384}
385
Rui Ueyama7c713312016-01-06 01:56:36 +0000386// Partially parse the shared object file so that we can call
387// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000388template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000389 typedef typename ELFT::Dyn Elf_Dyn;
390 typedef typename ELFT::uint uintX_t;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000391 const Elf_Shdr *DynamicSec = nullptr;
392
393 const ELFFile<ELFT> Obj = this->ELFObj;
394 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000395 switch (Sec.sh_type) {
396 default:
397 continue;
398 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000399 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000400 break;
401 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000402 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000403 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000404 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000405 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000406 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000407 case SHT_GNU_versym:
408 this->VersymSec = &Sec;
409 break;
410 case SHT_GNU_verdef:
411 this->VerdefSec = &Sec;
412 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000413 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000414 }
415
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000416 this->initStringTable();
Rui Ueyamae69ab102016-01-06 01:14:11 +0000417 SoName = this->getName();
Rafael Espindolac8b15812015-10-01 15:47:50 +0000418
Rui Ueyama361d8b92015-10-12 15:49:02 +0000419 if (!DynamicSec)
420 return;
421 auto *Begin =
422 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
423 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
Rafael Espindolac8b15812015-10-01 15:47:50 +0000424
Rui Ueyama361d8b92015-10-12 15:49:02 +0000425 for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
426 if (Dyn.d_tag == DT_SONAME) {
427 uintX_t Val = Dyn.getVal();
428 if (Val >= this->StringTable.size())
George Rimar777f9632016-03-12 08:31:34 +0000429 fatal("invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000430 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000431 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000432 }
433 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000434}
Rafael Espindola18173d42015-09-08 15:50:05 +0000435
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000436// Parse the version definitions in the object file if present. Returns a vector
437// whose nth element contains a pointer to the Elf_Verdef for version identifier
438// n. Version identifiers that are not definitions map to nullptr. The array
439// always has at least length 1.
440template <class ELFT>
441std::vector<const typename ELFT::Verdef *>
442SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
443 std::vector<const Elf_Verdef *> Verdefs(1);
444 // We only need to process symbol versions for this DSO if it has both a
445 // versym and a verdef section, which indicates that the DSO contains symbol
446 // version definitions.
447 if (!VersymSec || !VerdefSec)
448 return Verdefs;
449
450 // The location of the first global versym entry.
451 Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
452 VersymSec->sh_offset) +
453 this->Symtab->sh_info;
454
455 // We cannot determine the largest verdef identifier without inspecting
456 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
457 // sequentially starting from 1, so we predict that the largest identifier
458 // will be VerdefCount.
459 unsigned VerdefCount = VerdefSec->sh_info;
460 Verdefs.resize(VerdefCount + 1);
461
462 // Build the Verdefs array by following the chain of Elf_Verdef objects
463 // from the start of the .gnu.version_d section.
464 const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
465 for (unsigned I = 0; I != VerdefCount; ++I) {
466 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
467 Verdef += CurVerdef->vd_next;
468 unsigned VerdefIndex = CurVerdef->vd_ndx;
469 if (Verdefs.size() <= VerdefIndex)
470 Verdefs.resize(VerdefIndex + 1);
471 Verdefs[VerdefIndex] = CurVerdef;
472 }
473
474 return Verdefs;
475}
476
Rui Ueyama7c713312016-01-06 01:56:36 +0000477// Fully parse the shared object file. This must be called after parseSoName().
478template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000479 // Create mapping from version identifiers to Elf_Verdef entries.
480 const Elf_Versym *Versym = nullptr;
481 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
482
Rafael Espindola67d72c02016-03-11 12:06:30 +0000483 Elf_Sym_Range Syms = this->getElfSymbols(true);
Rafael Espindola18173d42015-09-08 15:50:05 +0000484 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
485 SymbolBodies.reserve(NumSymbols);
486 for (const Elf_Sym &Sym : Syms) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000487 unsigned VersymIndex = 0;
488 if (Versym) {
489 VersymIndex = Versym->vs_index;
490 ++Versym;
491 // Ignore local symbols and non-default versions.
492 if (VersymIndex == 0 || (VersymIndex & VERSYM_HIDDEN))
493 continue;
494 }
Rui Ueyamaa3cb80a2016-03-04 01:56:52 +0000495 StringRef Name = check(Sym.getName(this->StringTable));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000496 if (Sym.isUndefined())
497 Undefs.push_back(Name);
498 else
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000499 SymbolBodies.emplace_back(this, Name, Sym, Verdefs[VersymIndex]);
Rafael Espindola18173d42015-09-08 15:50:05 +0000500 }
501}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000502
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000503BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
504
505bool BitcodeFile::classof(const InputFile *F) {
506 return F->kind() == BitcodeKind;
507}
508
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000509static uint8_t getGvVisibility(const GlobalValue *GV) {
510 switch (GV->getVisibility()) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000511 case GlobalValue::DefaultVisibility:
512 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000513 case GlobalValue::HiddenVisibility:
514 return STV_HIDDEN;
515 case GlobalValue::ProtectedVisibility:
516 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000517 }
George Rimar777f9632016-03-12 08:31:34 +0000518 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000519}
520
521SymbolBody *
Davide Italiano9f8efff2016-04-22 18:26:33 +0000522BitcodeFile::createBody(const DenseSet<const Comdat *> &KeptComdats,
Rui Ueyamaf7149552016-03-11 18:46:51 +0000523 const IRObjectFile &Obj,
Davide Italiano9f8efff2016-04-22 18:26:33 +0000524 const BasicSymbolRef &Sym,
525 const GlobalValue *GV) {
526 SmallString<64> Name;
527 raw_svector_ostream OS(Name);
528 Sym.printName(OS);
529 StringRef NameRef = Saver.save(StringRef(Name));
530 SymbolBody *Body;
Rui Ueyamaf7149552016-03-11 18:46:51 +0000531
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000532 uint32_t Flags = Sym.getFlags();
Davide Italiano9f8efff2016-04-22 18:26:33 +0000533 bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000534 uint32_t Binding = IsWeak ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000535
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000536 uint8_t Visibility;
537 if (GV)
538 Visibility = getGvVisibility(GV);
539 else
540 // FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose
541 // protected visibility.
542 Visibility = STV_DEFAULT;
Rui Ueyamaf7149552016-03-11 18:46:51 +0000543
Davide Italiano9f8efff2016-04-22 18:26:33 +0000544 if (GV)
545 if (const Comdat *C = GV->getComdat())
546 if (!KeptComdats.count(C)) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000547 Body = new (Alloc) Undefined(NameRef, Binding, Visibility, /*Type*/ 0,
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000548 /*IsBitcode*/ true);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000549 return Body;
550 }
Rui Ueyamaf7149552016-03-11 18:46:51 +0000551
552 const Module &M = Obj.getModule();
Davide Italiano9f8efff2016-04-22 18:26:33 +0000553 if (Flags & BasicSymbolRef::SF_Undefined)
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000554 return new (Alloc) Undefined(NameRef, Binding, Visibility, /*Type*/ 0,
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000555 /*IsBitcode*/ true);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000556 if (Flags & BasicSymbolRef::SF_Common) {
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000557 // FIXME: Set SF_Common flag correctly for module asm symbols, and expose
558 // size and alignment.
559 assert(GV);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000560 const DataLayout &DL = M.getDataLayout();
561 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000562 return new (Alloc) DefinedCommon(NameRef, Size, GV->getAlignment(), Binding,
563 Visibility, /*Type*/ 0);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000564 }
Davide Italiano9f8efff2016-04-22 18:26:33 +0000565 return new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
566}
567
568SymbolBody *
569BitcodeFile::createSymbolBody(const DenseSet<const Comdat *> &KeptComdats,
570 const IRObjectFile &Obj,
571 const BasicSymbolRef &Sym) {
572 const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
573 SymbolBody *Body = createBody(KeptComdats, Obj, Sym, GV);
574
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000575 // FIXME: Expose a thread-local flag for module asm symbols.
Rafael Espindola4d480ed2016-04-21 21:44:25 +0000576 if (GV) {
577 if (GV->isThreadLocal())
578 Body->Type = STT_TLS;
579 Body->CanOmitFromDynSym = canBeOmittedFromSymbolTable(GV);
580 }
Rui Ueyamaf7149552016-03-11 18:46:51 +0000581 return Body;
582}
583
584bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) {
585 uint32_t Flags = Sym.getFlags();
586 if (!(Flags & BasicSymbolRef::SF_Global))
587 return true;
588 if (Flags & BasicSymbolRef::SF_FormatSpecific)
589 return true;
590 return false;
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000591}
592
Rafael Espindola4de44b72016-03-02 15:43:50 +0000593void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Rafael Espindola156f4ee2016-04-28 19:30:41 +0000594 Obj = check(IRObjectFile::create(MB, Driver->Context));
Rafael Espindola1130935c2016-03-03 16:21:44 +0000595 const Module &M = Obj->getModule();
Rafael Espindola4de44b72016-03-02 15:43:50 +0000596
597 DenseSet<const Comdat *> KeptComdats;
598 for (const auto &P : M.getComdatSymbolTable()) {
599 StringRef N = Saver.save(P.first());
600 if (ComdatGroups.insert(N).second)
601 KeptComdats.insert(&P.second);
602 }
603
Rui Ueyamaf7149552016-03-11 18:46:51 +0000604 for (const BasicSymbolRef &Sym : Obj->symbols())
605 if (!shouldSkip(Sym))
606 SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000607}
608
Rui Ueyamac4b65062015-10-12 15:31:09 +0000609template <typename T>
610static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
611 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
612
613 if (!Config->FirstElf)
614 Config->FirstElf = Ret.get();
615
Rui Ueyamae717a712015-10-13 16:20:50 +0000616 if (Config->EKind == ELFNoneKind) {
617 Config->EKind = Ret->getELFKind();
Rui Ueyamac4b65062015-10-12 15:31:09 +0000618 Config->EMachine = Ret->getEMachine();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000619 if (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind)
620 Config->Mips64EL = true;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000621 }
622
623 return std::move(Ret);
624}
625
626template <template <class> class T>
Rui Ueyama533c0302016-01-06 00:09:43 +0000627static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000628 unsigned char Size;
629 unsigned char Endian;
630 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
631 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
George Rimar777f9632016-03-12 08:31:34 +0000632 fatal("invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000633
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000634 if (Size == ELFCLASS32) {
635 if (Endian == ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000636 return createELFFileAux<T<ELF32LE>>(MB);
637 return createELFFileAux<T<ELF32BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000638 }
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000639 if (Size == ELFCLASS64) {
640 if (Endian == ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000641 return createELFFileAux<T<ELF64LE>>(MB);
642 return createELFFileAux<T<ELF64BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000643 }
George Rimar777f9632016-03-12 08:31:34 +0000644 fatal("invalid file class: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000645}
646
Rui Ueyama4655ea32016-04-08 00:14:55 +0000647static bool isBitcode(MemoryBufferRef MB) {
648 using namespace sys::fs;
649 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
650}
651
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000652std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
653 StringRef ArchiveName) {
Rui Ueyamac89bff22016-02-23 18:17:11 +0000654 std::unique_ptr<InputFile> F;
Rui Ueyama4655ea32016-04-08 00:14:55 +0000655 if (isBitcode(MB))
Rui Ueyamac89bff22016-02-23 18:17:11 +0000656 F.reset(new BitcodeFile(MB));
657 else
658 F = createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000659 F->ArchiveName = ArchiveName;
660 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000661}
662
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000663std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000664 return createELFFile<SharedFile>(MB);
665}
666
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000667void LazyObjectFile::parse() {
668 for (StringRef Sym : getSymbols())
669 LazySymbols.emplace_back(Sym, this->MB);
670}
671
672template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
673 typedef typename ELFT::Shdr Elf_Shdr;
674 typedef typename ELFT::Sym Elf_Sym;
675 typedef typename ELFT::SymRange Elf_Sym_Range;
676
677 const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
678 for (const Elf_Shdr &Sec : Obj.sections()) {
679 if (Sec.sh_type != SHT_SYMTAB)
680 continue;
681 Elf_Sym_Range Syms = Obj.symbols(&Sec);
682 uint32_t FirstNonLocal = Sec.sh_info;
683 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
684 std::vector<StringRef> V;
685 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +0000686 if (Sym.st_shndx != SHN_UNDEF)
687 V.push_back(check(Sym.getName(StringTable)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000688 return V;
689 }
690 return {};
691}
692
693std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
694 LLVMContext Context;
695 std::unique_ptr<IRObjectFile> Obj =
696 check(IRObjectFile::create(this->MB, Context));
697 std::vector<StringRef> V;
698 for (const BasicSymbolRef &Sym : Obj->symbols()) {
699 if (BitcodeFile::shouldSkip(Sym))
700 continue;
Rui Ueyama029d89e2016-04-08 21:38:22 +0000701 if (Sym.getFlags() & BasicSymbolRef::SF_Undefined)
Rui Ueyama1f492892016-04-08 20:49:31 +0000702 continue;
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000703 SmallString<64> Name;
704 raw_svector_ostream OS(Name);
705 Sym.printName(OS);
706 V.push_back(Saver.save(StringRef(Name)));
707 }
708 return V;
709}
710
Rui Ueyama1f492892016-04-08 20:49:31 +0000711// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000712std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +0000713 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000714 return getBitcodeSymbols();
715
Rui Ueyama4655ea32016-04-08 00:14:55 +0000716 unsigned char Size;
717 unsigned char Endian;
718 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
719 if (Size == ELFCLASS32) {
720 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000721 return getElfSymbols<ELF32LE>();
722 return getElfSymbols<ELF32BE>();
723 }
Rui Ueyama4655ea32016-04-08 00:14:55 +0000724 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000725 return getElfSymbols<ELF64LE>();
726 return getElfSymbols<ELF64BE>();
727}
728
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000729template class elf::ELFFileBase<ELF32LE>;
730template class elf::ELFFileBase<ELF32BE>;
731template class elf::ELFFileBase<ELF64LE>;
732template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000733
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000734template class elf::ObjectFile<ELF32LE>;
735template class elf::ObjectFile<ELF32BE>;
736template class elf::ObjectFile<ELF64LE>;
737template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000738
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000739template class elf::SharedFile<ELF32LE>;
740template class elf::SharedFile<ELF32BE>;
741template class elf::SharedFile<ELF64LE>;
742template class elf::SharedFile<ELF64BE>;