blob: 47e4b33c22ae19be085b38870084fed5c51f4267 [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"
Peter Collingbourne4f952702016-05-01 04:55:03 +000014#include "SymbolTable.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000015#include "Symbols.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "llvm/ADT/STLExtras.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000017#include "llvm/CodeGen/Analysis.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000018#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000019#include "llvm/IR/Module.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000020#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
Michael J. Spencer1b348a62015-09-04 22:28:10 +000022using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000024using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000025using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000028using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
Rafael Espindola78db5a92016-05-09 21:40:06 +000030// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
31std::string elf::getFilename(InputFile *F) {
32 if (!F)
33 return "(internal)";
34 if (!F->ArchiveName.empty())
35 return (F->ArchiveName + "(" + F->getName() + ")").str();
36 return F->getName();
37}
38
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000039template <class ELFT>
40static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000041 std::error_code EC;
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000042 ELFFile<ELFT> F(MB.getBuffer(), EC);
Rafael Espindola75714f62016-03-03 22:24:39 +000043 check(EC);
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000044 return F;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000045}
46
Rafael Espindola18173d42015-09-08 15:50:05 +000047template <class ELFT>
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000048ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
49 : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000050
51template <class ELFT>
Rui Ueyama2022e812015-11-20 02:10:52 +000052ELFKind ELFFileBase<ELFT>::getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +000053 if (ELFT::TargetEndianness == support::little)
54 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
55 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +000056}
57
58template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000059typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
Rafael Espindola18173d42015-09-08 15:50:05 +000060 if (!Symtab)
61 return Elf_Sym_Range(nullptr, nullptr);
Rafael Espindolae1901cc2015-09-24 15:11:50 +000062 Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
Rafael Espindola18173d42015-09-08 15:50:05 +000063 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
64 uint32_t FirstNonLocal = Symtab->sh_info;
65 if (FirstNonLocal > NumSymbols)
George Rimar777f9632016-03-12 08:31:34 +000066 fatal("invalid sh_info in symbol table");
Rafael Espindola67d72c02016-03-11 12:06:30 +000067
68 if (OnlyGlobals)
Rafael Espindola0f7ccc32016-04-05 14:47:28 +000069 return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
70 return makeArrayRef(Syms.begin(), Syms.end());
Davide Italiano6d328d32015-09-16 20:45:57 +000071}
72
Rafael Espindola115f0f32015-11-03 14:13:40 +000073template <class ELFT>
74uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000075 uint32_t I = Sym.st_shndx;
76 if (I == ELF::SHN_XINDEX)
Rui Ueyamae69ab102016-01-06 01:14:11 +000077 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
Rafael Espindola972b2362016-03-09 14:31:18 +000078 if (I >= ELF::SHN_LORESERVE)
Rafael Espindola115f0f32015-11-03 14:13:40 +000079 return 0;
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000080 return I;
Rafael Espindola115f0f32015-11-03 14:13:40 +000081}
82
Rafael Espindolaaf707642015-10-12 01:55:32 +000083template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
Rafael Espindola3e603792015-10-01 20:26:37 +000084 if (!Symtab)
85 return;
Rafael Espindola75714f62016-03-03 22:24:39 +000086 StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000087}
88
89template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000090elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
Rafael Espindola2a4b2712015-10-13 01:17:02 +000091 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000092
93template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +000094ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
95 if (!this->Symtab)
96 return this->SymbolBodies;
97 uint32_t FirstNonLocal = this->Symtab->sh_info;
98 return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
99}
100
101template <class ELFT>
102ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
103 if (!this->Symtab)
104 return this->SymbolBodies;
105 uint32_t FirstNonLocal = this->Symtab->sh_info;
106 return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
107}
108
109template <class ELFT>
110ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
111 if (!this->Symtab)
112 return this->SymbolBodies;
113 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000114}
115
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000116template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000117 if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo)
118 return MipsOptions->Reginfo->ri_gp_value;
119 if (!ELFT::Is64Bits && MipsReginfo && MipsReginfo->Reginfo)
Rui Ueyama70eed362016-01-06 22:42:43 +0000120 return MipsReginfo->Reginfo->ri_gp_value;
121 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +0000122}
123
Rafael Espindola444576d2015-10-09 19:25:07 +0000124template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000125void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000126 // Read section and symbol tables.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000127 initializeSections(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000128 initializeSymbols();
129}
130
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000131// Sections with SHT_GROUP and comdat bits define comdat section groups.
132// They are identified and deduplicated by group name. This function
133// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000134template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000135StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000136 const ELFFile<ELFT> &Obj = this->ELFObj;
137 uint32_t SymtabdSectionIndex = Sec.sh_link;
Rafael Espindola75714f62016-03-03 22:24:39 +0000138 const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex));
Rafael Espindola444576d2015-10-09 19:25:07 +0000139 uint32_t SymIndex = Sec.sh_info;
140 const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
Rafael Espindola75714f62016-03-03 22:24:39 +0000141 StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec));
142 return check(Sym->getName(StringTable));
Rafael Espindola444576d2015-10-09 19:25:07 +0000143}
144
145template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000146ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000147elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000148 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000149 ArrayRef<Elf_Word> Entries =
150 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000151 if (Entries.empty() || Entries[0] != GRP_COMDAT)
George Rimar777f9632016-03-12 08:31:34 +0000152 fatal("unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000153 return Entries.slice(1);
154}
155
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000156template <class ELFT> static bool shouldMerge(const typename ELFT::Shdr &Sec) {
157 typedef typename ELFT::uint uintX_t;
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000158
159 // We don't merge sections if -O0 (default is -O1). This makes sometimes
160 // the linker significantly faster, although the output will be bigger.
161 if (Config->Optimize == 0)
162 return false;
163
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000164 uintX_t Flags = Sec.sh_flags;
165 if (!(Flags & SHF_MERGE))
166 return false;
167 if (Flags & SHF_WRITE)
George Rimar777f9632016-03-12 08:31:34 +0000168 fatal("writable SHF_MERGE sections are not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000169 uintX_t EntSize = Sec.sh_entsize;
Rafael Espindola0d2ad422016-03-21 14:57:20 +0000170 if (!EntSize || Sec.sh_size % EntSize)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000171 fatal("SHF_MERGE section size must be a multiple of sh_entsize");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000172
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000173 // Don't try to merge if the aligment is larger than the sh_entsize and this
174 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000175 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000176 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
177 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000178 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000179 if (Flags & SHF_STRINGS)
180 return true;
181
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000182 if (Sec.sh_addralign > EntSize)
183 return false;
184
185 return true;
186}
187
188template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000189void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000190 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000191 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000192 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000193 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000194 const ELFFile<ELFT> &Obj = this->ELFObj;
195 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000196 ++I;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000197 if (Sections[I] == &InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000198 continue;
199
Rafael Espindolacde25132015-08-13 14:45:44 +0000200 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000201 case SHT_GROUP:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000202 Sections[I] = &InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000203 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000204 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000205 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000206 if (SecIndex >= Size)
George Rimar777f9632016-03-12 08:31:34 +0000207 fatal("invalid section index in group");
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000208 Sections[SecIndex] = &InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000209 }
210 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000211 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000212 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000213 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000214 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000215 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000216 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000217 case SHT_STRTAB:
218 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000219 break;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000220 case SHT_RELA:
221 case SHT_REL: {
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000222 // This section contains relocation information.
223 // If -r is given, we do not interpret or apply relocation
224 // but just copy relocation sections to output.
George Rimar58941ee2016-02-25 08:23:37 +0000225 if (Config->Relocatable) {
Ivan Krasinbfc91312016-04-06 01:11:10 +0000226 Sections[I] = new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000227 break;
228 }
229
230 // Find the relocation target section and associate this
231 // section with it.
232 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
233 if (!Target)
234 break;
235 if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000236 S->RelocSections.push_back(&Sec);
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000237 break;
238 }
239 if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000240 if (S->RelocSection)
George Rimar777f9632016-03-12 08:31:34 +0000241 fatal("multiple relocation sections to .eh_frame are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000242 S->RelocSection = &Sec;
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000243 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000244 }
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000245 fatal("relocations pointing to SHF_MERGE are not supported");
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000246 }
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000247 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000248 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000249 }
250 }
251}
252
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000253template <class ELFT>
254InputSectionBase<ELFT> *
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000255elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
256 uint32_t Idx = Sec.sh_info;
257 if (Idx >= Sections.size())
258 fatal("invalid relocated section index");
259 InputSectionBase<ELFT> *Target = Sections[Idx];
260
261 // Strictly speaking, a relocation section must be included in the
262 // group of the section it relocates. However, LLVM 3.3 and earlier
263 // would fail to do so, so we gracefully handle that case.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000264 if (Target == &InputSection<ELFT>::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000265 return nullptr;
266
267 if (!Target)
268 fatal("unsupported relocation reference");
269 return Target;
270}
271
272template <class ELFT>
273InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000274elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola75714f62016-03-03 22:24:39 +0000275 StringRef Name = check(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000276
277 // .note.GNU-stack is a marker section to control the presence of
278 // PT_GNU_STACK segment in outputs. Since the presence of the segment
279 // is controlled only by the command line option (-z execstack) in LLD,
280 // .note.GNU-stack is ignored.
281 if (Name == ".note.GNU-stack")
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000282 return &InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000283
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000284 if (Name == ".note.GNU-split-stack") {
George Rimar777f9632016-03-12 08:31:34 +0000285 error("objects using splitstacks are not supported");
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000286 return &InputSection<ELFT>::Discarded;
287 }
288
289 if (Config->StripDebug && Name.startswith(".debug"))
290 return &InputSection<ELFT>::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000291
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000292 // A MIPS object file has a special sections that contain register
293 // usage info, which need to be handled by the linker specially.
294 if (Config->EMachine == EM_MIPS) {
295 if (Name == ".reginfo") {
296 MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec));
297 return MipsReginfo.get();
298 }
299 if (Name == ".MIPS.options") {
300 MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec));
301 return MipsOptions.get();
302 }
Simon Atanasyan57830b62015-12-25 13:02:13 +0000303 }
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000304
George Rimar4cfe5722016-03-03 07:49:35 +0000305 // We dont need special handling of .eh_frame sections if relocatable
306 // output was choosen. Proccess them as usual input sections.
307 if (!Config->Relocatable && Name == ".eh_frame")
Rui Ueyamae69ab102016-01-06 01:14:11 +0000308 return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000309 if (shouldMerge<ELFT>(Sec))
Rui Ueyamae69ab102016-01-06 01:14:11 +0000310 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
Ivan Krasinbfc91312016-04-06 01:11:10 +0000311 return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000312}
313
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000314template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000315 this->initStringTable();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000316 Elf_Sym_Range Syms = this->getElfSymbols(false);
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000317 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000318 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000319 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000320 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000321}
322
323template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000324InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000325elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000326 uint32_t Index = this->getSectionIndex(Sym);
327 if (Index == 0)
Rafael Espindola4cda5812015-10-16 15:29:48 +0000328 return nullptr;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000329 if (Index >= Sections.size() || !Sections[Index])
George Rimar777f9632016-03-12 08:31:34 +0000330 fatal("invalid section index");
Rui Ueyama0b289522016-02-25 18:43:51 +0000331 InputSectionBase<ELFT> *S = Sections[Index];
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000332 if (S == &InputSectionBase<ELFT>::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000333 return S;
334 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000335}
336
337template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000338SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama376ab7c2016-04-05 17:47:29 +0000339 unsigned char Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000340 InputSectionBase<ELFT> *Sec = getSection(*Sym);
341 if (Binding == STB_LOCAL) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000342 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000343 return new (Alloc) Undefined(Sym->st_name, Sym->st_other, Sym->getType());
Rafael Espindolad9a17172016-04-05 11:47:46 +0000344 return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000345 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000346
Rafael Espindola75714f62016-03-03 22:24:39 +0000347 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000348
Rafael Espindola4cda5812015-10-16 15:29:48 +0000349 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000350 case SHN_UNDEF:
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000351 return elf::Symtab<ELFT>::X
Peter Collingbourne4f952702016-05-01 04:55:03 +0000352 ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(), this)
353 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000354 case SHN_COMMON:
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000355 return elf::Symtab<ELFT>::X
Peter Collingbourne4f952702016-05-01 04:55:03 +0000356 ->addCommon(Name, Sym->st_size, Sym->st_value, Binding, Sym->st_other,
357 Sym->getType(), this)
358 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000359 }
Rafael Espindola20348222015-08-24 21:43:25 +0000360
Rafael Espindola67d72c02016-03-11 12:06:30 +0000361 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000362 default:
George Rimar57610422016-03-11 14:43:02 +0000363 fatal("unexpected binding");
Rafael Espindolab13df652015-08-11 17:33:02 +0000364 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000365 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000366 case STB_GNU_UNIQUE:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000367 if (Sec == &InputSection<ELFT>::Discarded)
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000368 return elf::Symtab<ELFT>::X
Peter Collingbourne4f952702016-05-01 04:55:03 +0000369 ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(), this)
370 ->body();
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000371 return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000372 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000373}
374
Peter Collingbourne4f952702016-05-01 04:55:03 +0000375template <class ELFT> void ArchiveFile::parse() {
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000376 File = check(Archive::create(MB), "failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000377
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000378 // Read the symbol table to construct Lazy objects.
379 for (const Archive::Symbol &Sym : File->symbols())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000380 Symtab<ELFT>::X->addLazyArchive(this, Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000381}
382
383// Returns a buffer pointing to a member file containing a given symbol.
384MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000385 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000386 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000387 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000388
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000389 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000390 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000391
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000392 MemoryBufferRef Ret =
393 check(C.getMemoryBufferRef(),
394 "could not get the buffer for the member defining symbol " +
395 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000396
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000397 if (C.getParent()->isThin())
398 maybeCopyInputFile(check(C.getFullName()), Ret.getBuffer());
399
400 return Ret;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000401}
402
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000403template <class ELFT>
404SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000405 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000406
Rafael Espindola115f0f32015-11-03 14:13:40 +0000407template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000408const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000409SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
410 uint32_t Index = this->getSectionIndex(Sym);
411 if (Index == 0)
412 return nullptr;
Rafael Espindola75714f62016-03-03 22:24:39 +0000413 return check(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000414}
415
Rui Ueyama7c713312016-01-06 01:56:36 +0000416// Partially parse the shared object file so that we can call
417// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000418template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000419 typedef typename ELFT::Dyn Elf_Dyn;
420 typedef typename ELFT::uint uintX_t;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000421 const Elf_Shdr *DynamicSec = nullptr;
422
423 const ELFFile<ELFT> Obj = this->ELFObj;
424 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000425 switch (Sec.sh_type) {
426 default:
427 continue;
428 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000429 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000430 break;
431 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000432 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000433 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000434 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000435 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000436 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000437 case SHT_GNU_versym:
438 this->VersymSec = &Sec;
439 break;
440 case SHT_GNU_verdef:
441 this->VerdefSec = &Sec;
442 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000443 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000444 }
445
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000446 this->initStringTable();
Rui Ueyamae69ab102016-01-06 01:14:11 +0000447 SoName = this->getName();
Rafael Espindolac8b15812015-10-01 15:47:50 +0000448
Rui Ueyama361d8b92015-10-12 15:49:02 +0000449 if (!DynamicSec)
450 return;
451 auto *Begin =
452 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
453 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
Rafael Espindolac8b15812015-10-01 15:47:50 +0000454
Rui Ueyama361d8b92015-10-12 15:49:02 +0000455 for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
456 if (Dyn.d_tag == DT_SONAME) {
457 uintX_t Val = Dyn.getVal();
458 if (Val >= this->StringTable.size())
George Rimar777f9632016-03-12 08:31:34 +0000459 fatal("invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000460 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000461 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000462 }
463 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000464}
Rafael Espindola18173d42015-09-08 15:50:05 +0000465
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000466// Parse the version definitions in the object file if present. Returns a vector
467// whose nth element contains a pointer to the Elf_Verdef for version identifier
468// n. Version identifiers that are not definitions map to nullptr. The array
469// always has at least length 1.
470template <class ELFT>
471std::vector<const typename ELFT::Verdef *>
472SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
473 std::vector<const Elf_Verdef *> Verdefs(1);
474 // We only need to process symbol versions for this DSO if it has both a
475 // versym and a verdef section, which indicates that the DSO contains symbol
476 // version definitions.
477 if (!VersymSec || !VerdefSec)
478 return Verdefs;
479
480 // The location of the first global versym entry.
481 Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
482 VersymSec->sh_offset) +
483 this->Symtab->sh_info;
484
485 // We cannot determine the largest verdef identifier without inspecting
486 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
487 // sequentially starting from 1, so we predict that the largest identifier
488 // will be VerdefCount.
489 unsigned VerdefCount = VerdefSec->sh_info;
490 Verdefs.resize(VerdefCount + 1);
491
492 // Build the Verdefs array by following the chain of Elf_Verdef objects
493 // from the start of the .gnu.version_d section.
494 const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
495 for (unsigned I = 0; I != VerdefCount; ++I) {
496 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
497 Verdef += CurVerdef->vd_next;
498 unsigned VerdefIndex = CurVerdef->vd_ndx;
499 if (Verdefs.size() <= VerdefIndex)
500 Verdefs.resize(VerdefIndex + 1);
501 Verdefs[VerdefIndex] = CurVerdef;
502 }
503
504 return Verdefs;
505}
506
Rui Ueyama7c713312016-01-06 01:56:36 +0000507// Fully parse the shared object file. This must be called after parseSoName().
508template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000509 // Create mapping from version identifiers to Elf_Verdef entries.
510 const Elf_Versym *Versym = nullptr;
511 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
512
Rafael Espindola67d72c02016-03-11 12:06:30 +0000513 Elf_Sym_Range Syms = this->getElfSymbols(true);
Rafael Espindola18173d42015-09-08 15:50:05 +0000514 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000515 unsigned VersymIndex = 0;
516 if (Versym) {
517 VersymIndex = Versym->vs_index;
518 ++Versym;
519 }
520
Rafael Espindola18da0e52016-04-29 16:23:31 +0000521 StringRef Name = check(Sym.getName(this->StringTable));
522 if (Sym.isUndefined()) {
523 Undefs.push_back(Name);
524 continue;
525 }
526
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000527 if (Versym) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000528 // Ignore local symbols and non-default versions.
529 if (VersymIndex == 0 || (VersymIndex & VERSYM_HIDDEN))
530 continue;
531 }
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000532 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, Verdefs[VersymIndex]);
Rafael Espindola18173d42015-09-08 15:50:05 +0000533 }
534}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000535
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000536BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
537
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000538static uint8_t getGvVisibility(const GlobalValue *GV) {
539 switch (GV->getVisibility()) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000540 case GlobalValue::DefaultVisibility:
541 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000542 case GlobalValue::HiddenVisibility:
543 return STV_HIDDEN;
544 case GlobalValue::ProtectedVisibility:
545 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000546 }
George Rimar777f9632016-03-12 08:31:34 +0000547 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000548}
549
Peter Collingbourne4f952702016-05-01 04:55:03 +0000550template <class ELFT>
551Symbol *BitcodeFile::createSymbol(const DenseSet<const Comdat *> &KeptComdats,
552 const IRObjectFile &Obj,
553 const BasicSymbolRef &Sym) {
554 const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
555
Davide Italiano9f8efff2016-04-22 18:26:33 +0000556 SmallString<64> Name;
557 raw_svector_ostream OS(Name);
558 Sym.printName(OS);
559 StringRef NameRef = Saver.save(StringRef(Name));
Rui Ueyamaf7149552016-03-11 18:46:51 +0000560
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000561 uint32_t Flags = Sym.getFlags();
Davide Italiano9f8efff2016-04-22 18:26:33 +0000562 bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000563 uint32_t Binding = IsWeak ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000564
Peter Collingbourne4f952702016-05-01 04:55:03 +0000565 uint8_t Type = STT_NOTYPE;
566 bool CanOmitFromDynSym = false;
567 // FIXME: Expose a thread-local flag for module asm symbols.
568 if (GV) {
569 if (GV->isThreadLocal())
570 Type = STT_TLS;
571 CanOmitFromDynSym = canBeOmittedFromSymbolTable(GV);
572 }
573
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000574 uint8_t Visibility;
575 if (GV)
576 Visibility = getGvVisibility(GV);
577 else
578 // FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose
579 // protected visibility.
580 Visibility = STV_DEFAULT;
Rui Ueyamaf7149552016-03-11 18:46:51 +0000581
Davide Italiano9f8efff2016-04-22 18:26:33 +0000582 if (GV)
583 if (const Comdat *C = GV->getComdat())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000584 if (!KeptComdats.count(C))
585 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
586 this);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000587
588 const Module &M = Obj.getModule();
Davide Italiano9f8efff2016-04-22 18:26:33 +0000589 if (Flags & BasicSymbolRef::SF_Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000590 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
591 this);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000592 if (Flags & BasicSymbolRef::SF_Common) {
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000593 // FIXME: Set SF_Common flag correctly for module asm symbols, and expose
594 // size and alignment.
595 assert(GV);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000596 const DataLayout &DL = M.getDataLayout();
597 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000598 return Symtab<ELFT>::X->addCommon(NameRef, Size, GV->getAlignment(),
599 Binding, Visibility, STT_OBJECT, this);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000600 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000601 return Symtab<ELFT>::X->addBitcode(NameRef, IsWeak, Visibility, Type,
602 CanOmitFromDynSym, this);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000603}
604
Peter Collingbourne4f952702016-05-01 04:55:03 +0000605bool BitcodeFile::shouldSkip(uint32_t Flags) {
Rui Ueyamaf7149552016-03-11 18:46:51 +0000606 if (!(Flags & BasicSymbolRef::SF_Global))
607 return true;
608 if (Flags & BasicSymbolRef::SF_FormatSpecific)
609 return true;
610 return false;
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000611}
612
Peter Collingbourne4f952702016-05-01 04:55:03 +0000613template <class ELFT>
Rafael Espindola4de44b72016-03-02 15:43:50 +0000614void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Rafael Espindola156f4ee2016-04-28 19:30:41 +0000615 Obj = check(IRObjectFile::create(MB, Driver->Context));
Rafael Espindola1130935c2016-03-03 16:21:44 +0000616 const Module &M = Obj->getModule();
Rafael Espindola4de44b72016-03-02 15:43:50 +0000617
618 DenseSet<const Comdat *> KeptComdats;
619 for (const auto &P : M.getComdatSymbolTable()) {
620 StringRef N = Saver.save(P.first());
621 if (ComdatGroups.insert(N).second)
622 KeptComdats.insert(&P.second);
623 }
624
Rui Ueyamaf7149552016-03-11 18:46:51 +0000625 for (const BasicSymbolRef &Sym : Obj->symbols())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000626 if (!shouldSkip(Sym.getFlags()))
627 Symbols.push_back(createSymbol<ELFT>(KeptComdats, *Obj, Sym));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000628}
629
Rui Ueyamac4b65062015-10-12 15:31:09 +0000630template <typename T>
631static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
632 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
633
634 if (!Config->FirstElf)
635 Config->FirstElf = Ret.get();
636
Rui Ueyamae717a712015-10-13 16:20:50 +0000637 if (Config->EKind == ELFNoneKind) {
638 Config->EKind = Ret->getELFKind();
Rui Ueyamac4b65062015-10-12 15:31:09 +0000639 Config->EMachine = Ret->getEMachine();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000640 if (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind)
641 Config->Mips64EL = true;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000642 }
643
644 return std::move(Ret);
645}
646
647template <template <class> class T>
Rui Ueyama533c0302016-01-06 00:09:43 +0000648static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000649 unsigned char Size;
650 unsigned char Endian;
651 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
652 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
George Rimar777f9632016-03-12 08:31:34 +0000653 fatal("invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000654
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000655 if (Size == ELFCLASS32) {
656 if (Endian == ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000657 return createELFFileAux<T<ELF32LE>>(MB);
658 return createELFFileAux<T<ELF32BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000659 }
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000660 if (Size == ELFCLASS64) {
661 if (Endian == ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000662 return createELFFileAux<T<ELF64LE>>(MB);
663 return createELFFileAux<T<ELF64BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000664 }
George Rimar777f9632016-03-12 08:31:34 +0000665 fatal("invalid file class: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000666}
667
Rui Ueyama4655ea32016-04-08 00:14:55 +0000668static bool isBitcode(MemoryBufferRef MB) {
669 using namespace sys::fs;
670 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
671}
672
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000673std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
674 StringRef ArchiveName) {
Rui Ueyamac89bff22016-02-23 18:17:11 +0000675 std::unique_ptr<InputFile> F;
Rui Ueyama4655ea32016-04-08 00:14:55 +0000676 if (isBitcode(MB))
Rui Ueyamac89bff22016-02-23 18:17:11 +0000677 F.reset(new BitcodeFile(MB));
678 else
679 F = createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000680 F->ArchiveName = ArchiveName;
681 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000682}
683
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000684std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000685 return createELFFile<SharedFile>(MB);
686}
687
Peter Collingbourne4f952702016-05-01 04:55:03 +0000688template <class ELFT>
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000689void LazyObjectFile::parse() {
690 for (StringRef Sym : getSymbols())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000691 Symtab<ELFT>::X->addLazyObject(Sym, this->MB);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000692}
693
694template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
695 typedef typename ELFT::Shdr Elf_Shdr;
696 typedef typename ELFT::Sym Elf_Sym;
697 typedef typename ELFT::SymRange Elf_Sym_Range;
698
699 const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
700 for (const Elf_Shdr &Sec : Obj.sections()) {
701 if (Sec.sh_type != SHT_SYMTAB)
702 continue;
703 Elf_Sym_Range Syms = Obj.symbols(&Sec);
704 uint32_t FirstNonLocal = Sec.sh_info;
705 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
706 std::vector<StringRef> V;
707 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +0000708 if (Sym.st_shndx != SHN_UNDEF)
709 V.push_back(check(Sym.getName(StringTable)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000710 return V;
711 }
712 return {};
713}
714
715std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
716 LLVMContext Context;
717 std::unique_ptr<IRObjectFile> Obj =
718 check(IRObjectFile::create(this->MB, Context));
719 std::vector<StringRef> V;
720 for (const BasicSymbolRef &Sym : Obj->symbols()) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000721 uint32_t Flags = Sym.getFlags();
722 if (BitcodeFile::shouldSkip(Flags))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000723 continue;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000724 if (Flags & BasicSymbolRef::SF_Undefined)
Rui Ueyama1f492892016-04-08 20:49:31 +0000725 continue;
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000726 SmallString<64> Name;
727 raw_svector_ostream OS(Name);
728 Sym.printName(OS);
729 V.push_back(Saver.save(StringRef(Name)));
730 }
731 return V;
732}
733
Rui Ueyama1f492892016-04-08 20:49:31 +0000734// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000735std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +0000736 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000737 return getBitcodeSymbols();
738
Rui Ueyama4655ea32016-04-08 00:14:55 +0000739 unsigned char Size;
740 unsigned char Endian;
741 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
742 if (Size == ELFCLASS32) {
743 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000744 return getElfSymbols<ELF32LE>();
745 return getElfSymbols<ELF32BE>();
746 }
Rui Ueyama4655ea32016-04-08 00:14:55 +0000747 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000748 return getElfSymbols<ELF64LE>();
749 return getElfSymbols<ELF64BE>();
750}
751
Peter Collingbourne4f952702016-05-01 04:55:03 +0000752template void ArchiveFile::parse<ELF32LE>();
753template void ArchiveFile::parse<ELF32BE>();
754template void ArchiveFile::parse<ELF64LE>();
755template void ArchiveFile::parse<ELF64BE>();
756
757template void
758BitcodeFile::parse<ELF32LE>(llvm::DenseSet<StringRef> &ComdatGroups);
759template void
760BitcodeFile::parse<ELF32BE>(llvm::DenseSet<StringRef> &ComdatGroups);
761template void
762BitcodeFile::parse<ELF64LE>(llvm::DenseSet<StringRef> &ComdatGroups);
763template void
764BitcodeFile::parse<ELF64BE>(llvm::DenseSet<StringRef> &ComdatGroups);
765
766template void LazyObjectFile::parse<ELF32LE>();
767template void LazyObjectFile::parse<ELF32BE>();
768template void LazyObjectFile::parse<ELF64LE>();
769template void LazyObjectFile::parse<ELF64BE>();
770
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000771template class elf::ELFFileBase<ELF32LE>;
772template class elf::ELFFileBase<ELF32BE>;
773template class elf::ELFFileBase<ELF64LE>;
774template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000775
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000776template class elf::ObjectFile<ELF32LE>;
777template class elf::ObjectFile<ELF32BE>;
778template class elf::ObjectFile<ELF64LE>;
779template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000780
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000781template class elf::SharedFile<ELF32LE>;
782template class elf::SharedFile<ELF32BE>;
783template class elf::SharedFile<ELF64LE>;
784template class elf::SharedFile<ELF64BE>;