blob: fcc05c2a2c6c69f448b78d25808fbf9107208951 [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>
Rafael Espindolaaf707642015-10-12 01:55:32 +000048typename ELFFileBase<ELFT>::Elf_Sym_Range
Rafael Espindola67d72c02016-03-11 12:06:30 +000049ELFFileBase<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)
Rui Ueyama64cfffd2016-01-28 18:40:06 +000056 fatal("Invalid sh_info in symbol table");
Rafael Espindola67d72c02016-03-11 12:06:30 +000057
58 if (OnlyGlobals)
Rui Ueyama90b3daa2015-09-30 02:37:51 +000059 return make_range(Syms.begin() + FirstNonLocal, Syms.end());
Rafael Espindola67d72c02016-03-11 12:06:30 +000060 return make_range(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>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000134ArrayRef<typename elf::ObjectFile<ELFT>::uint32_X>
135elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000136 const ELFFile<ELFT> &Obj = this->ELFObj;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000137 ArrayRef<uint32_X> Entries =
Rafael Espindola75714f62016-03-03 22:24:39 +0000138 check(Obj.template getSectionContentsAsArray<uint32_X>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000139 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000140 fatal("Unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000141 return Entries.slice(1);
142}
143
144template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000145static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) {
146 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
147 uintX_t Flags = Sec.sh_flags;
148 if (!(Flags & SHF_MERGE))
149 return false;
150 if (Flags & SHF_WRITE)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000151 fatal("Writable SHF_MERGE sections are not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000152 uintX_t EntSize = Sec.sh_entsize;
George Rimar564da7e2015-11-09 08:40:44 +0000153 if (!EntSize || Sec.sh_size % EntSize)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000154 fatal("SHF_MERGE section size must be a multiple of sh_entsize");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000155
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000156 // Don't try to merge if the aligment is larger than the sh_entsize and this
157 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000158 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000159 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
160 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000161 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000162 if (Flags & SHF_STRINGS)
163 return true;
164
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000165 if (Sec.sh_addralign > EntSize)
166 return false;
167
168 return true;
169}
170
171template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000172void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000173 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000174 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000175 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000176 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000177 const ELFFile<ELFT> &Obj = this->ELFObj;
178 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000179 ++I;
Rui Ueyama733153d2016-02-24 18:33:35 +0000180 if (Sections[I] == InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000181 continue;
182
Rafael Espindolacde25132015-08-13 14:45:44 +0000183 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000184 case SHT_GROUP:
Rui Ueyama733153d2016-02-24 18:33:35 +0000185 Sections[I] = InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000186 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000187 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000188 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000189 if (SecIndex >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000190 fatal("Invalid section index in group");
Rui Ueyama733153d2016-02-24 18:33:35 +0000191 Sections[SecIndex] = InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000192 }
193 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000194 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000195 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000196 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000197 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000198 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000199 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000200 case SHT_STRTAB:
201 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000202 break;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000203 case SHT_RELA:
204 case SHT_REL: {
205 uint32_t RelocatedSectionIndex = Sec.sh_info;
206 if (RelocatedSectionIndex >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000207 fatal("Invalid relocated section index");
Rafael Espindolac159c962015-10-19 21:00:02 +0000208 InputSectionBase<ELFT> *RelocatedSection =
209 Sections[RelocatedSectionIndex];
Sean Silva09247f82016-02-04 21:41:07 +0000210 // Strictly speaking, a relocation section must be included in the
211 // group of the section it relocates. However, LLVM 3.3 and earlier
212 // would fail to do so, so we gracefully handle that case.
Rui Ueyama733153d2016-02-24 18:33:35 +0000213 if (RelocatedSection == InputSection<ELFT>::Discarded)
Sean Silva09247f82016-02-04 21:41:07 +0000214 continue;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000215 if (!RelocatedSection)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000216 fatal("Unsupported relocation reference");
George Rimar58941ee2016-02-25 08:23:37 +0000217 if (Config->Relocatable) {
218 // For -r, relocation sections are handled as regular input sections.
219 Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
220 } else if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000221 S->RelocSections.push_back(&Sec);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000222 } else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) {
223 if (S->RelocSection)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000224 fatal("Multiple relocation sections to .eh_frame are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000225 S->RelocSection = &Sec;
226 } else {
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000227 fatal("Relocations pointing to SHF_MERGE are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000228 }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000229 break;
230 }
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000231 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000232 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000233 }
234 }
235}
236
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000237template <class ELFT>
238InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000239elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola75714f62016-03-03 22:24:39 +0000240 StringRef Name = check(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000241
242 // .note.GNU-stack is a marker section to control the presence of
243 // PT_GNU_STACK segment in outputs. Since the presence of the segment
244 // is controlled only by the command line option (-z execstack) in LLD,
245 // .note.GNU-stack is ignored.
246 if (Name == ".note.GNU-stack")
Rui Ueyama733153d2016-02-24 18:33:35 +0000247 return InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000248
George Rimar3c45ed22016-03-09 18:01:45 +0000249 if (Name == ".note.GNU-split-stack")
250 error("Objects using splitstacks are not supported");
251
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000252 // A MIPS object file has a special section that contains register
253 // usage info, which needs to be handled by the linker specially.
Simon Atanasyan57830b62015-12-25 13:02:13 +0000254 if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
Rui Ueyamae69ab102016-01-06 01:14:11 +0000255 MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
Simon Atanasyan57830b62015-12-25 13:02:13 +0000256 return MipsReginfo;
257 }
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000258
George Rimar4cfe5722016-03-03 07:49:35 +0000259 // We dont need special handling of .eh_frame sections if relocatable
260 // output was choosen. Proccess them as usual input sections.
261 if (!Config->Relocatable && Name == ".eh_frame")
Rui Ueyamae69ab102016-01-06 01:14:11 +0000262 return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000263 if (shouldMerge<ELFT>(Sec))
Rui Ueyamae69ab102016-01-06 01:14:11 +0000264 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
265 return new (Alloc) InputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000266}
267
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000268template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000269 this->initStringTable();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000270 Elf_Sym_Range Syms = this->getElfSymbols(false);
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000271 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000272 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000273 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000274 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000275}
276
277template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000278InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000279elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000280 uint32_t Index = this->getSectionIndex(Sym);
281 if (Index == 0)
Rafael Espindola4cda5812015-10-16 15:29:48 +0000282 return nullptr;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000283 if (Index >= Sections.size() || !Sections[Index])
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000284 fatal("Invalid section index");
Rui Ueyama0b289522016-02-25 18:43:51 +0000285 InputSectionBase<ELFT> *S = Sections[Index];
286 if (S == InputSectionBase<ELFT>::Discarded)
287 return S;
288 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000289}
290
291template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000292SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rafael Espindola67d72c02016-03-11 12:06:30 +0000293 unsigned char Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000294 InputSectionBase<ELFT> *Sec = getSection(*Sym);
295 if (Binding == STB_LOCAL) {
296 if (Sec == InputSection<ELFT>::Discarded)
297 Sec = nullptr;
298 return new (Alloc) DefinedRegular<ELFT>("", *Sym, Sec);
299 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000300
Rafael Espindola75714f62016-03-03 22:24:39 +0000301 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000302
Rafael Espindola4cda5812015-10-16 15:29:48 +0000303 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000304 case SHN_UNDEF:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000305 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
Rafael Espindola51d46902015-08-28 21:26:51 +0000306 case SHN_COMMON:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000307 return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
308 Sym->getBinding() == llvm::ELF::STB_WEAK,
309 Sym->getVisibility());
Rafael Espindola51d46902015-08-28 21:26:51 +0000310 }
Rafael Espindola20348222015-08-24 21:43:25 +0000311
Rafael Espindola67d72c02016-03-11 12:06:30 +0000312 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000313 default:
George Rimar57610422016-03-11 14:43:02 +0000314 fatal("unexpected binding");
Rafael Espindolab13df652015-08-11 17:33:02 +0000315 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000316 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000317 case STB_GNU_UNIQUE:
Rui Ueyama733153d2016-02-24 18:33:35 +0000318 if (Sec == InputSection<ELFT>::Discarded)
Rui Ueyamae69ab102016-01-06 01:14:11 +0000319 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
320 return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
Rafael Espindola444576d2015-10-09 19:25:07 +0000321 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000322}
323
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000324void ArchiveFile::parse() {
Rafael Espindola75714f62016-03-03 22:24:39 +0000325 File = check(Archive::create(MB), "Failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000326
327 // Allocate a buffer for Lazy objects.
328 size_t NumSyms = File->getNumberOfSymbols();
329 LazySymbols.reserve(NumSyms);
330
331 // Read the symbol table to construct Lazy objects.
332 for (const Archive::Symbol &Sym : File->symbols())
333 LazySymbols.emplace_back(this, Sym);
334}
335
336// Returns a buffer pointing to a member file containing a given symbol.
337MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000338 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000339 check(Sym->getMember(),
Rafael Espindola1130935c2016-03-03 16:21:44 +0000340 "Could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000341
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000342 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000343 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000344
Rafael Espindola75714f62016-03-03 22:24:39 +0000345 return check(C.getMemoryBufferRef(),
Rafael Espindola1130935c2016-03-03 16:21:44 +0000346 "Could not get the buffer for the member defining symbol " +
347 Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000348}
349
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000350template <class ELFT>
351SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000352 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000353
Rafael Espindola115f0f32015-11-03 14:13:40 +0000354template <class ELFT>
355const typename ELFFile<ELFT>::Elf_Shdr *
356SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
357 uint32_t Index = this->getSectionIndex(Sym);
358 if (Index == 0)
359 return nullptr;
Rafael Espindola75714f62016-03-03 22:24:39 +0000360 return check(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000361}
362
Rui Ueyama7c713312016-01-06 01:56:36 +0000363// Partially parse the shared object file so that we can call
364// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000365template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rafael Espindolac8b15812015-10-01 15:47:50 +0000366 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
367 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
368 const Elf_Shdr *DynamicSec = nullptr;
369
370 const ELFFile<ELFT> Obj = this->ELFObj;
371 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000372 switch (Sec.sh_type) {
373 default:
374 continue;
375 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000376 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000377 break;
378 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000379 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000380 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000381 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000382 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000383 break;
384 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000385 }
386
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000387 this->initStringTable();
Rui Ueyamae69ab102016-01-06 01:14:11 +0000388 SoName = this->getName();
Rafael Espindolac8b15812015-10-01 15:47:50 +0000389
Rui Ueyama361d8b92015-10-12 15:49:02 +0000390 if (!DynamicSec)
391 return;
392 auto *Begin =
393 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
394 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
Rafael Espindolac8b15812015-10-01 15:47:50 +0000395
Rui Ueyama361d8b92015-10-12 15:49:02 +0000396 for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
397 if (Dyn.d_tag == DT_SONAME) {
398 uintX_t Val = Dyn.getVal();
399 if (Val >= this->StringTable.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000400 fatal("Invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000401 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000402 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000403 }
404 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000405}
Rafael Espindola18173d42015-09-08 15:50:05 +0000406
Rui Ueyama7c713312016-01-06 01:56:36 +0000407// Fully parse the shared object file. This must be called after parseSoName().
408template <class ELFT> void SharedFile<ELFT>::parseRest() {
Rafael Espindola67d72c02016-03-11 12:06:30 +0000409 Elf_Sym_Range Syms = this->getElfSymbols(true);
Rafael Espindola18173d42015-09-08 15:50:05 +0000410 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
411 SymbolBodies.reserve(NumSymbols);
412 for (const Elf_Sym &Sym : Syms) {
Rui Ueyamaa3cb80a2016-03-04 01:56:52 +0000413 StringRef Name = check(Sym.getName(this->StringTable));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000414 if (Sym.isUndefined())
415 Undefs.push_back(Name);
416 else
417 SymbolBodies.emplace_back(this, Name, Sym);
Rafael Espindola18173d42015-09-08 15:50:05 +0000418 }
419}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000420
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000421BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
422
423bool BitcodeFile::classof(const InputFile *F) {
424 return F->kind() == BitcodeKind;
425}
426
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000427static uint8_t getGvVisibility(const GlobalValue *GV) {
428 switch (GV->getVisibility()) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000429 case GlobalValue::DefaultVisibility:
430 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000431 case GlobalValue::HiddenVisibility:
432 return STV_HIDDEN;
433 case GlobalValue::ProtectedVisibility:
434 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000435 }
George Rimar599d7722016-03-09 18:05:34 +0000436 llvm_unreachable("Unknown visibility");
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000437}
438
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000439SymbolBody *
440BitcodeFile::createSymbolBody(const DenseSet<const Comdat *> &KeptComdats,
441 const IRObjectFile &Obj,
442 const BasicSymbolRef &Sym) {
443 const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
444 assert(GV);
445 if (const Comdat *C = GV->getComdat())
446 if (!KeptComdats.count(C))
447 return nullptr;
448
449 uint8_t Visibility = getGvVisibility(GV);
450
451 SmallString<64> Name;
452 raw_svector_ostream OS(Name);
453 Sym.printName(OS);
454 StringRef NameRef = Saver.save(StringRef(Name));
455
456 const Module &M = Obj.getModule();
457 SymbolBody *Body;
458 uint32_t Flags = Sym.getFlags();
459 bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
460 if (Flags & BasicSymbolRef::SF_Undefined) {
461 Body = new (Alloc) Undefined(NameRef, IsWeak, Visibility, false);
462 } else if (Flags & BasicSymbolRef::SF_Common) {
463 const DataLayout &DL = M.getDataLayout();
464 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
465 Body = new (Alloc)
466 DefinedCommon(NameRef, Size, GV->getAlignment(), IsWeak, Visibility);
467 } else {
468 Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
469 }
470 Body->IsTls = GV->isThreadLocal();
471 return Body;
472}
473
474bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) {
475 uint32_t Flags = Sym.getFlags();
476 if (!(Flags & BasicSymbolRef::SF_Global))
477 return true;
478 if (Flags & BasicSymbolRef::SF_FormatSpecific)
479 return true;
480 return false;
481}
482
Rafael Espindola4de44b72016-03-02 15:43:50 +0000483void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000484 LLVMContext Context;
Rafael Espindola75714f62016-03-03 22:24:39 +0000485 std::unique_ptr<IRObjectFile> Obj = check(IRObjectFile::create(MB, Context));
Rafael Espindola1130935c2016-03-03 16:21:44 +0000486 const Module &M = Obj->getModule();
Rafael Espindola4de44b72016-03-02 15:43:50 +0000487
488 DenseSet<const Comdat *> KeptComdats;
489 for (const auto &P : M.getComdatSymbolTable()) {
490 StringRef N = Saver.save(P.first());
491 if (ComdatGroups.insert(N).second)
492 KeptComdats.insert(&P.second);
493 }
494
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000495 for (const BasicSymbolRef &Sym : Obj->symbols())
496 if (!shouldSkip(Sym))
497 SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000498}
499
Rui Ueyamac4b65062015-10-12 15:31:09 +0000500template <typename T>
501static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
502 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
503
504 if (!Config->FirstElf)
505 Config->FirstElf = Ret.get();
506
Rui Ueyamae717a712015-10-13 16:20:50 +0000507 if (Config->EKind == ELFNoneKind) {
508 Config->EKind = Ret->getELFKind();
Rui Ueyamac4b65062015-10-12 15:31:09 +0000509 Config->EMachine = Ret->getEMachine();
510 }
511
512 return std::move(Ret);
513}
514
515template <template <class> class T>
Rui Ueyama533c0302016-01-06 00:09:43 +0000516static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
Rui Ueyamad94478b2015-11-20 02:19:36 +0000517 std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000518 if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000519 fatal("Invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000520
521 if (Type.first == ELF::ELFCLASS32) {
522 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000523 return createELFFileAux<T<ELF32LE>>(MB);
524 return createELFFileAux<T<ELF32BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000525 }
526 if (Type.first == ELF::ELFCLASS64) {
527 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000528 return createELFFileAux<T<ELF64LE>>(MB);
529 return createELFFileAux<T<ELF64BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000530 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000531 fatal("Invalid file class: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000532}
533
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000534std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
535 StringRef ArchiveName) {
Rui Ueyamac89bff22016-02-23 18:17:11 +0000536 using namespace sys::fs;
537 std::unique_ptr<InputFile> F;
538 if (identify_magic(MB.getBuffer()) == file_magic::bitcode)
539 F.reset(new BitcodeFile(MB));
540 else
541 F = createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000542 F->ArchiveName = ArchiveName;
543 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000544}
545
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000546std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000547 return createELFFile<SharedFile>(MB);
548}
549
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000550template class elf::ELFFileBase<ELF32LE>;
551template class elf::ELFFileBase<ELF32BE>;
552template class elf::ELFFileBase<ELF64LE>;
553template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000554
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000555template class elf::ObjectFile<ELF32LE>;
556template class elf::ObjectFile<ELF32BE>;
557template class elf::ObjectFile<ELF64LE>;
558template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000559
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000560template class elf::SharedFile<ELF32LE>;
561template class elf::SharedFile<ELF32BE>;
562template class elf::SharedFile<ELF64LE>;
563template class elf::SharedFile<ELF64BE>;