blob: 3622dc2b8c37f6aa5d0c2a4741fe9684ccaa7e43 [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000013#include "Symbols.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014#include "llvm/ADT/STLExtras.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000015#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000016#include "llvm/IR/Module.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000017#include "llvm/Object/IRObjectFile.h"
18#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000019
Michael J. Spencer1b348a62015-09-04 22:28:10 +000020using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000021using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000022using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000023using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
25using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000026using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000028template <class ELFT>
29static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000030 std::error_code EC;
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000031 ELFFile<ELFT> F(MB.getBuffer(), EC);
Rafael Espindola75714f62016-03-03 22:24:39 +000032 check(EC);
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000033 return F;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000034}
35
Rafael Espindola18173d42015-09-08 15:50:05 +000036template <class ELFT>
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000037ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
38 : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000039
40template <class ELFT>
Rui Ueyama2022e812015-11-20 02:10:52 +000041ELFKind ELFFileBase<ELFT>::getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +000042 if (ELFT::TargetEndianness == support::little)
43 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
44 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +000045}
46
47template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000048typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
Rafael Espindola18173d42015-09-08 15:50:05 +000049 if (!Symtab)
50 return Elf_Sym_Range(nullptr, nullptr);
Rafael Espindolae1901cc2015-09-24 15:11:50 +000051 Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
Rafael Espindola18173d42015-09-08 15:50:05 +000052 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
53 uint32_t FirstNonLocal = Symtab->sh_info;
54 if (FirstNonLocal > NumSymbols)
George Rimar777f9632016-03-12 08:31:34 +000055 fatal("invalid sh_info in symbol table");
Rafael Espindola67d72c02016-03-11 12:06:30 +000056
57 if (OnlyGlobals)
Rui Ueyama90b3daa2015-09-30 02:37:51 +000058 return make_range(Syms.begin() + FirstNonLocal, Syms.end());
Rafael Espindola67d72c02016-03-11 12:06:30 +000059 return make_range(Syms.begin(), Syms.end());
Davide Italiano6d328d32015-09-16 20:45:57 +000060}
61
Rafael Espindola115f0f32015-11-03 14:13:40 +000062template <class ELFT>
63uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000064 uint32_t I = Sym.st_shndx;
65 if (I == ELF::SHN_XINDEX)
Rui Ueyamae69ab102016-01-06 01:14:11 +000066 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
Rafael Espindola972b2362016-03-09 14:31:18 +000067 if (I >= ELF::SHN_LORESERVE)
Rafael Espindola115f0f32015-11-03 14:13:40 +000068 return 0;
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000069 return I;
Rafael Espindola115f0f32015-11-03 14:13:40 +000070}
71
Rafael Espindolaaf707642015-10-12 01:55:32 +000072template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
Rafael Espindola3e603792015-10-01 20:26:37 +000073 if (!Symtab)
74 return;
Rafael Espindola75714f62016-03-03 22:24:39 +000075 StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000076}
77
78template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000079elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
Rafael Espindola2a4b2712015-10-13 01:17:02 +000080 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000081
82template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +000083ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
84 if (!this->Symtab)
85 return this->SymbolBodies;
86 uint32_t FirstNonLocal = this->Symtab->sh_info;
87 return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
88}
89
90template <class ELFT>
91ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
92 if (!this->Symtab)
93 return this->SymbolBodies;
94 uint32_t FirstNonLocal = this->Symtab->sh_info;
95 return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
96}
97
98template <class ELFT>
99ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
100 if (!this->Symtab)
101 return this->SymbolBodies;
102 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000103}
104
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000105template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Rui Ueyama70eed362016-01-06 22:42:43 +0000106 if (MipsReginfo)
107 return MipsReginfo->Reginfo->ri_gp_value;
108 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +0000109}
110
Rafael Espindola444576d2015-10-09 19:25:07 +0000111template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000112void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000113 // Read section and symbol tables.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000114 initializeSections(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000115 initializeSymbols();
116}
117
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000118// Sections with SHT_GROUP and comdat bits define comdat section groups.
119// They are identified and deduplicated by group name. This function
120// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000121template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000122StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000123 const ELFFile<ELFT> &Obj = this->ELFObj;
124 uint32_t SymtabdSectionIndex = Sec.sh_link;
Rafael Espindola75714f62016-03-03 22:24:39 +0000125 const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex));
Rafael Espindola444576d2015-10-09 19:25:07 +0000126 uint32_t SymIndex = Sec.sh_info;
127 const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
Rafael Espindola75714f62016-03-03 22:24:39 +0000128 StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec));
129 return check(Sym->getName(StringTable));
Rafael Espindola444576d2015-10-09 19:25:07 +0000130}
131
132template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000133ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000134elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000135 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000136 ArrayRef<Elf_Word> Entries =
137 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000138 if (Entries.empty() || Entries[0] != GRP_COMDAT)
George Rimar777f9632016-03-12 08:31:34 +0000139 fatal("unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000140 return Entries.slice(1);
141}
142
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000143template <class ELFT> static bool shouldMerge(const typename ELFT::Shdr &Sec) {
144 typedef typename ELFT::uint uintX_t;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000145 uintX_t Flags = Sec.sh_flags;
146 if (!(Flags & SHF_MERGE))
147 return false;
148 if (Flags & SHF_WRITE)
George Rimar777f9632016-03-12 08:31:34 +0000149 fatal("writable SHF_MERGE sections are not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000150 uintX_t EntSize = Sec.sh_entsize;
Rafael Espindola0d2ad422016-03-21 14:57:20 +0000151 if (!EntSize || Sec.sh_size % EntSize)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000152 fatal("SHF_MERGE section size must be a multiple of sh_entsize");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000153
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000154 // Don't try to merge if the aligment is larger than the sh_entsize and this
155 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000156 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000157 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
158 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000159 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000160 if (Flags & SHF_STRINGS)
161 return true;
162
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000163 if (Sec.sh_addralign > EntSize)
164 return false;
165
166 return true;
167}
168
169template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000170void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000171 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000172 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000173 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000174 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000175 const ELFFile<ELFT> &Obj = this->ELFObj;
176 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000177 ++I;
Rui Ueyama733153d2016-02-24 18:33:35 +0000178 if (Sections[I] == InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000179 continue;
180
Rafael Espindolacde25132015-08-13 14:45:44 +0000181 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000182 case SHT_GROUP:
Rui Ueyama733153d2016-02-24 18:33:35 +0000183 Sections[I] = InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000184 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000185 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000186 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000187 if (SecIndex >= Size)
George Rimar777f9632016-03-12 08:31:34 +0000188 fatal("invalid section index in group");
Rui Ueyama733153d2016-02-24 18:33:35 +0000189 Sections[SecIndex] = InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000190 }
191 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000192 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000193 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000194 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000195 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000196 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000197 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000198 case SHT_STRTAB:
199 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000200 break;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000201 case SHT_RELA:
202 case SHT_REL: {
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000203 // This section contains relocation information.
204 // If -r is given, we do not interpret or apply relocation
205 // but just copy relocation sections to output.
George Rimar58941ee2016-02-25 08:23:37 +0000206 if (Config->Relocatable) {
George Rimar58941ee2016-02-25 08:23:37 +0000207 Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000208 break;
209 }
210
211 // Find the relocation target section and associate this
212 // section with it.
213 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
214 if (!Target)
215 break;
216 if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000217 S->RelocSections.push_back(&Sec);
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000218 break;
219 }
220 if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000221 if (S->RelocSection)
George Rimar777f9632016-03-12 08:31:34 +0000222 fatal("multiple relocation sections to .eh_frame are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000223 S->RelocSection = &Sec;
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000224 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000225 }
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000226 fatal("relocations pointing to SHF_MERGE are not supported");
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000227 }
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000228 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000229 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000230 }
231 }
232}
233
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000234template <class ELFT>
235InputSectionBase<ELFT> *
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000236elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
237 uint32_t Idx = Sec.sh_info;
238 if (Idx >= Sections.size())
239 fatal("invalid relocated section index");
240 InputSectionBase<ELFT> *Target = Sections[Idx];
241
242 // Strictly speaking, a relocation section must be included in the
243 // group of the section it relocates. However, LLVM 3.3 and earlier
244 // would fail to do so, so we gracefully handle that case.
245 if (Target == InputSection<ELFT>::Discarded)
246 return nullptr;
247
248 if (!Target)
249 fatal("unsupported relocation reference");
250 return Target;
251}
252
253template <class ELFT>
254InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000255elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola75714f62016-03-03 22:24:39 +0000256 StringRef Name = check(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000257
258 // .note.GNU-stack is a marker section to control the presence of
259 // PT_GNU_STACK segment in outputs. Since the presence of the segment
260 // is controlled only by the command line option (-z execstack) in LLD,
261 // .note.GNU-stack is ignored.
262 if (Name == ".note.GNU-stack")
Rui Ueyama733153d2016-02-24 18:33:35 +0000263 return InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000264
George Rimar3c45ed22016-03-09 18:01:45 +0000265 if (Name == ".note.GNU-split-stack")
George Rimar777f9632016-03-12 08:31:34 +0000266 error("objects using splitstacks are not supported");
George Rimar3c45ed22016-03-09 18:01:45 +0000267
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000268 // A MIPS object file has a special section that contains register
269 // usage info, which needs to be handled by the linker specially.
Simon Atanasyan57830b62015-12-25 13:02:13 +0000270 if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
Rui Ueyamae69ab102016-01-06 01:14:11 +0000271 MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
Simon Atanasyan57830b62015-12-25 13:02:13 +0000272 return MipsReginfo;
273 }
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000274
George Rimar4cfe5722016-03-03 07:49:35 +0000275 // We dont need special handling of .eh_frame sections if relocatable
276 // output was choosen. Proccess them as usual input sections.
277 if (!Config->Relocatable && Name == ".eh_frame")
Rui Ueyamae69ab102016-01-06 01:14:11 +0000278 return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000279 if (shouldMerge<ELFT>(Sec))
Rui Ueyamae69ab102016-01-06 01:14:11 +0000280 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
281 return new (Alloc) InputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000282}
283
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000284template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000285 this->initStringTable();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000286 Elf_Sym_Range Syms = this->getElfSymbols(false);
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000287 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000288 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000289 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000290 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000291}
292
293template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000294InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000295elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000296 uint32_t Index = this->getSectionIndex(Sym);
297 if (Index == 0)
Rafael Espindola4cda5812015-10-16 15:29:48 +0000298 return nullptr;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000299 if (Index >= Sections.size() || !Sections[Index])
George Rimar777f9632016-03-12 08:31:34 +0000300 fatal("invalid section index");
Rui Ueyama0b289522016-02-25 18:43:51 +0000301 InputSectionBase<ELFT> *S = Sections[Index];
302 if (S == InputSectionBase<ELFT>::Discarded)
303 return S;
304 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000305}
306
307template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000308SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rafael Espindola67d72c02016-03-11 12:06:30 +0000309 unsigned char Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000310 InputSectionBase<ELFT> *Sec = getSection(*Sym);
311 if (Binding == STB_LOCAL) {
312 if (Sec == InputSection<ELFT>::Discarded)
313 Sec = nullptr;
314 return new (Alloc) DefinedRegular<ELFT>("", *Sym, Sec);
315 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000316
Rafael Espindola75714f62016-03-03 22:24:39 +0000317 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000318
Rafael Espindola4cda5812015-10-16 15:29:48 +0000319 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000320 case SHN_UNDEF:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000321 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
Rafael Espindola51d46902015-08-28 21:26:51 +0000322 case SHN_COMMON:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000323 return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
324 Sym->getBinding() == llvm::ELF::STB_WEAK,
325 Sym->getVisibility());
Rafael Espindola51d46902015-08-28 21:26:51 +0000326 }
Rafael Espindola20348222015-08-24 21:43:25 +0000327
Rafael Espindola67d72c02016-03-11 12:06:30 +0000328 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000329 default:
George Rimar57610422016-03-11 14:43:02 +0000330 fatal("unexpected binding");
Rafael Espindolab13df652015-08-11 17:33:02 +0000331 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000332 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000333 case STB_GNU_UNIQUE:
Rui Ueyama733153d2016-02-24 18:33:35 +0000334 if (Sec == InputSection<ELFT>::Discarded)
Rui Ueyamae69ab102016-01-06 01:14:11 +0000335 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
336 return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
Rafael Espindola444576d2015-10-09 19:25:07 +0000337 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000338}
339
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000340void ArchiveFile::parse() {
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000341 File = check(Archive::create(MB), "failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000342
343 // Allocate a buffer for Lazy objects.
344 size_t NumSyms = File->getNumberOfSymbols();
345 LazySymbols.reserve(NumSyms);
346
347 // Read the symbol table to construct Lazy objects.
348 for (const Archive::Symbol &Sym : File->symbols())
349 LazySymbols.emplace_back(this, Sym);
350}
351
352// Returns a buffer pointing to a member file containing a given symbol.
353MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000354 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000355 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000356 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000357
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000358 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000359 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000360
Rafael Espindola75714f62016-03-03 22:24:39 +0000361 return check(C.getMemoryBufferRef(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000362 "could not get the buffer for the member defining symbol " +
Rafael Espindola1130935c2016-03-03 16:21:44 +0000363 Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000364}
365
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000366template <class ELFT>
367SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000368 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000369
Rafael Espindola115f0f32015-11-03 14:13:40 +0000370template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000371const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000372SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
373 uint32_t Index = this->getSectionIndex(Sym);
374 if (Index == 0)
375 return nullptr;
Rafael Espindola75714f62016-03-03 22:24:39 +0000376 return check(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000377}
378
Rui Ueyama7c713312016-01-06 01:56:36 +0000379// Partially parse the shared object file so that we can call
380// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000381template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000382 typedef typename ELFT::Dyn Elf_Dyn;
383 typedef typename ELFT::uint uintX_t;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000384 const Elf_Shdr *DynamicSec = nullptr;
385
386 const ELFFile<ELFT> Obj = this->ELFObj;
387 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000388 switch (Sec.sh_type) {
389 default:
390 continue;
391 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000392 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000393 break;
394 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000395 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000396 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000397 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000398 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000399 break;
400 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000401 }
402
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000403 this->initStringTable();
Rui Ueyamae69ab102016-01-06 01:14:11 +0000404 SoName = this->getName();
Rafael Espindolac8b15812015-10-01 15:47:50 +0000405
Rui Ueyama361d8b92015-10-12 15:49:02 +0000406 if (!DynamicSec)
407 return;
408 auto *Begin =
409 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
410 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
Rafael Espindolac8b15812015-10-01 15:47:50 +0000411
Rui Ueyama361d8b92015-10-12 15:49:02 +0000412 for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
413 if (Dyn.d_tag == DT_SONAME) {
414 uintX_t Val = Dyn.getVal();
415 if (Val >= this->StringTable.size())
George Rimar777f9632016-03-12 08:31:34 +0000416 fatal("invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000417 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000418 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000419 }
420 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000421}
Rafael Espindola18173d42015-09-08 15:50:05 +0000422
Rui Ueyama7c713312016-01-06 01:56:36 +0000423// Fully parse the shared object file. This must be called after parseSoName().
424template <class ELFT> void SharedFile<ELFT>::parseRest() {
Rafael Espindola67d72c02016-03-11 12:06:30 +0000425 Elf_Sym_Range Syms = this->getElfSymbols(true);
Rafael Espindola18173d42015-09-08 15:50:05 +0000426 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
427 SymbolBodies.reserve(NumSymbols);
428 for (const Elf_Sym &Sym : Syms) {
Rui Ueyamaa3cb80a2016-03-04 01:56:52 +0000429 StringRef Name = check(Sym.getName(this->StringTable));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000430 if (Sym.isUndefined())
431 Undefs.push_back(Name);
432 else
433 SymbolBodies.emplace_back(this, Name, Sym);
Rafael Espindola18173d42015-09-08 15:50:05 +0000434 }
435}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000436
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000437BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
438
439bool BitcodeFile::classof(const InputFile *F) {
440 return F->kind() == BitcodeKind;
441}
442
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000443static uint8_t getGvVisibility(const GlobalValue *GV) {
444 switch (GV->getVisibility()) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000445 case GlobalValue::DefaultVisibility:
446 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000447 case GlobalValue::HiddenVisibility:
448 return STV_HIDDEN;
449 case GlobalValue::ProtectedVisibility:
450 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000451 }
George Rimar777f9632016-03-12 08:31:34 +0000452 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000453}
454
455SymbolBody *
456BitcodeFile::createSymbolBody(const DenseSet<const Comdat *> &KeptComdats,
457 const IRObjectFile &Obj,
458 const BasicSymbolRef &Sym) {
459 const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
460 assert(GV);
461 if (const Comdat *C = GV->getComdat())
462 if (!KeptComdats.count(C))
463 return nullptr;
464
465 uint8_t Visibility = getGvVisibility(GV);
466
467 SmallString<64> Name;
468 raw_svector_ostream OS(Name);
469 Sym.printName(OS);
470 StringRef NameRef = Saver.save(StringRef(Name));
471
472 const Module &M = Obj.getModule();
473 SymbolBody *Body;
474 uint32_t Flags = Sym.getFlags();
475 bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
476 if (Flags & BasicSymbolRef::SF_Undefined) {
477 Body = new (Alloc) Undefined(NameRef, IsWeak, Visibility, false);
478 } else if (Flags & BasicSymbolRef::SF_Common) {
479 const DataLayout &DL = M.getDataLayout();
480 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
481 Body = new (Alloc)
482 DefinedCommon(NameRef, Size, GV->getAlignment(), IsWeak, Visibility);
483 } else {
484 Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
485 }
486 Body->IsTls = GV->isThreadLocal();
487 return Body;
488}
489
490bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) {
491 uint32_t Flags = Sym.getFlags();
492 if (!(Flags & BasicSymbolRef::SF_Global))
493 return true;
494 if (Flags & BasicSymbolRef::SF_FormatSpecific)
495 return true;
496 return false;
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000497}
498
Rafael Espindola4de44b72016-03-02 15:43:50 +0000499void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000500 LLVMContext Context;
Rafael Espindola75714f62016-03-03 22:24:39 +0000501 std::unique_ptr<IRObjectFile> Obj = check(IRObjectFile::create(MB, Context));
Rafael Espindola1130935c2016-03-03 16:21:44 +0000502 const Module &M = Obj->getModule();
Rafael Espindola4de44b72016-03-02 15:43:50 +0000503
504 DenseSet<const Comdat *> KeptComdats;
505 for (const auto &P : M.getComdatSymbolTable()) {
506 StringRef N = Saver.save(P.first());
507 if (ComdatGroups.insert(N).second)
508 KeptComdats.insert(&P.second);
509 }
510
Rui Ueyamaf7149552016-03-11 18:46:51 +0000511 for (const BasicSymbolRef &Sym : Obj->symbols())
512 if (!shouldSkip(Sym))
513 SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000514}
515
Rui Ueyamac4b65062015-10-12 15:31:09 +0000516template <typename T>
517static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
518 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
519
520 if (!Config->FirstElf)
521 Config->FirstElf = Ret.get();
522
Rui Ueyamae717a712015-10-13 16:20:50 +0000523 if (Config->EKind == ELFNoneKind) {
524 Config->EKind = Ret->getELFKind();
Rui Ueyamac4b65062015-10-12 15:31:09 +0000525 Config->EMachine = Ret->getEMachine();
526 }
527
528 return std::move(Ret);
529}
530
531template <template <class> class T>
Rui Ueyama533c0302016-01-06 00:09:43 +0000532static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
Rui Ueyamad94478b2015-11-20 02:19:36 +0000533 std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000534 if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
George Rimar777f9632016-03-12 08:31:34 +0000535 fatal("invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000536
537 if (Type.first == ELF::ELFCLASS32) {
538 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000539 return createELFFileAux<T<ELF32LE>>(MB);
540 return createELFFileAux<T<ELF32BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000541 }
542 if (Type.first == ELF::ELFCLASS64) {
543 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000544 return createELFFileAux<T<ELF64LE>>(MB);
545 return createELFFileAux<T<ELF64BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000546 }
George Rimar777f9632016-03-12 08:31:34 +0000547 fatal("invalid file class: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000548}
549
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000550std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
551 StringRef ArchiveName) {
Rui Ueyamac89bff22016-02-23 18:17:11 +0000552 using namespace sys::fs;
553 std::unique_ptr<InputFile> F;
554 if (identify_magic(MB.getBuffer()) == file_magic::bitcode)
555 F.reset(new BitcodeFile(MB));
556 else
557 F = createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000558 F->ArchiveName = ArchiveName;
559 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000560}
561
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000562std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000563 return createELFFile<SharedFile>(MB);
564}
565
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000566template class elf::ELFFileBase<ELF32LE>;
567template class elf::ELFFileBase<ELF32BE>;
568template class elf::ELFFileBase<ELF64LE>;
569template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000570
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000571template class elf::ObjectFile<ELF32LE>;
572template class elf::ObjectFile<ELF32BE>;
573template class elf::ObjectFile<ELF64LE>;
574template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000575
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000576template class elf::SharedFile<ELF32LE>;
577template class elf::SharedFile<ELF32BE>;
578template class elf::SharedFile<ELF64LE>;
579template class elf::SharedFile<ELF64BE>;