blob: bfe358566d219f6ce25a2deef80b512e8e794e24 [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);
32 fatal(EC);
33 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
49ELFFileBase<ELFT>::getSymbolsHelper(bool Local) {
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");
Davide Italiano6d328d32015-09-16 20:45:57 +000057 if (!Local)
Rui Ueyama90b3daa2015-09-30 02:37:51 +000058 return make_range(Syms.begin() + FirstNonLocal, Syms.end());
59 // +1 to skip over dummy symbol.
60 return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal);
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 Espindola02ce26a2015-12-24 14:22:24 +000068 if (I >= ELF::SHN_LORESERVE || I == ELF::SHN_ABS)
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 Espindola1130935c2016-03-03 16:21:44 +000076 StringTable = fatal(ELFObj.getStringTableForSymtab(*Symtab));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000077}
78
79template <class ELFT>
Rafael Espindolaaf707642015-10-12 01:55:32 +000080typename ELFFileBase<ELFT>::Elf_Sym_Range
81ELFFileBase<ELFT>::getNonLocalSymbols() {
Davide Italiano6d328d32015-09-16 20:45:57 +000082 return getSymbolsHelper(false);
83}
84
85template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000086elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
Rafael Espindola2a4b2712015-10-13 01:17:02 +000087 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000088
89template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000090typename elf::ObjectFile<ELFT>::Elf_Sym_Range
91elf::ObjectFile<ELFT>::getLocalSymbols() {
Davide Italiano6d328d32015-09-16 20:45:57 +000092 return this->getSymbolsHelper(true);
Rafael Espindola18173d42015-09-08 15:50:05 +000093}
94
Rafael Espindolae0df00b2016-02-28 00:25:54 +000095template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Rui Ueyama70eed362016-01-06 22:42:43 +000096 if (MipsReginfo)
97 return MipsReginfo->Reginfo->ri_gp_value;
98 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +000099}
100
Rafael Espindola444576d2015-10-09 19:25:07 +0000101template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000102const typename elf::ObjectFile<ELFT>::Elf_Sym *
103elf::ObjectFile<ELFT>::getLocalSymbol(uintX_t SymIndex) {
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000104 uint32_t FirstNonLocal = this->Symtab->sh_info;
105 if (SymIndex >= FirstNonLocal)
106 return nullptr;
107 Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab);
108 return Syms.begin() + SymIndex;
109}
110
111template <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 Espindola1130935c2016-03-03 16:21:44 +0000125 const Elf_Shdr *SymtabSec = fatal(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 Espindola1130935c2016-03-03 16:21:44 +0000128 StringRef StringTable = fatal(Obj.getStringTableForSymtab(*SymtabSec));
129 return fatal(Sym->getName(StringTable));
Rafael Espindola444576d2015-10-09 19:25:07 +0000130}
131
132template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000133ArrayRef<typename elf::ObjectFile<ELFT>::uint32_X>
134elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000135 const ELFFile<ELFT> &Obj = this->ELFObj;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000136 ArrayRef<uint32_X> Entries =
137 fatal(Obj.template getSectionContentsAsArray<uint32_X>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000138 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000139 fatal("Unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000140 return Entries.slice(1);
141}
142
143template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000144static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) {
145 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
146 uintX_t Flags = Sec.sh_flags;
147 if (!(Flags & SHF_MERGE))
148 return false;
149 if (Flags & SHF_WRITE)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000150 fatal("Writable SHF_MERGE sections are not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000151 uintX_t EntSize = Sec.sh_entsize;
George Rimar564da7e2015-11-09 08:40:44 +0000152 if (!EntSize || Sec.sh_size % EntSize)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000153 fatal("SHF_MERGE section size must be a multiple of sh_entsize");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000154
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000155 // Don't try to merge if the aligment is larger than the sh_entsize and this
156 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000157 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000158 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
159 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000160 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000161 if (Flags & SHF_STRINGS)
162 return true;
163
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000164 if (Sec.sh_addralign > EntSize)
165 return false;
166
167 return true;
168}
169
170template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000171void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000172 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000173 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000174 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000175 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000176 const ELFFile<ELFT> &Obj = this->ELFObj;
177 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000178 ++I;
Rui Ueyama733153d2016-02-24 18:33:35 +0000179 if (Sections[I] == InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000180 continue;
181
Rafael Espindolacde25132015-08-13 14:45:44 +0000182 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000183 case SHT_GROUP:
Rui Ueyama733153d2016-02-24 18:33:35 +0000184 Sections[I] = InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000185 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000186 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000187 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000188 if (SecIndex >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000189 fatal("Invalid section index in group");
Rui Ueyama733153d2016-02-24 18:33:35 +0000190 Sections[SecIndex] = InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000191 }
192 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000193 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000194 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000195 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000196 case SHT_SYMTAB_SHNDX:
197 this->SymtabSHNDX = fatal(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000198 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000199 case SHT_STRTAB:
200 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000201 break;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000202 case SHT_RELA:
203 case SHT_REL: {
204 uint32_t RelocatedSectionIndex = Sec.sh_info;
205 if (RelocatedSectionIndex >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000206 fatal("Invalid relocated section index");
Rafael Espindolac159c962015-10-19 21:00:02 +0000207 InputSectionBase<ELFT> *RelocatedSection =
208 Sections[RelocatedSectionIndex];
Sean Silva09247f82016-02-04 21:41:07 +0000209 // Strictly speaking, a relocation section must be included in the
210 // group of the section it relocates. However, LLVM 3.3 and earlier
211 // would fail to do so, so we gracefully handle that case.
Rui Ueyama733153d2016-02-24 18:33:35 +0000212 if (RelocatedSection == InputSection<ELFT>::Discarded)
Sean Silva09247f82016-02-04 21:41:07 +0000213 continue;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000214 if (!RelocatedSection)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000215 fatal("Unsupported relocation reference");
George Rimar58941ee2016-02-25 08:23:37 +0000216 if (Config->Relocatable) {
217 // For -r, relocation sections are handled as regular input sections.
218 Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
219 } else if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000220 S->RelocSections.push_back(&Sec);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000221 } else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) {
222 if (S->RelocSection)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000223 fatal("Multiple relocation sections to .eh_frame are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000224 S->RelocSection = &Sec;
225 } else {
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000226 fatal("Relocations pointing to SHF_MERGE are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000227 }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000228 break;
229 }
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000230 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000231 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000232 }
233 }
234}
235
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000236template <class ELFT>
237InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000238elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000239 StringRef Name = fatal(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000240
241 // .note.GNU-stack is a marker section to control the presence of
242 // PT_GNU_STACK segment in outputs. Since the presence of the segment
243 // is controlled only by the command line option (-z execstack) in LLD,
244 // .note.GNU-stack is ignored.
245 if (Name == ".note.GNU-stack")
Rui Ueyama733153d2016-02-24 18:33:35 +0000246 return InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000247
248 // A MIPS object file has a special section that contains register
249 // usage info, which needs to be handled by the linker specially.
Simon Atanasyan57830b62015-12-25 13:02:13 +0000250 if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
Rui Ueyamae69ab102016-01-06 01:14:11 +0000251 MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
Simon Atanasyan57830b62015-12-25 13:02:13 +0000252 return MipsReginfo;
253 }
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000254
George Rimar4cfe5722016-03-03 07:49:35 +0000255 // We dont need special handling of .eh_frame sections if relocatable
256 // output was choosen. Proccess them as usual input sections.
257 if (!Config->Relocatable && Name == ".eh_frame")
Rui Ueyamae69ab102016-01-06 01:14:11 +0000258 return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000259 if (shouldMerge<ELFT>(Sec))
Rui Ueyamae69ab102016-01-06 01:14:11 +0000260 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
261 return new (Alloc) InputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000262}
263
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000264template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000265 this->initStringTable();
Rafael Espindola18173d42015-09-08 15:50:05 +0000266 Elf_Sym_Range Syms = this->getNonLocalSymbols();
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000267 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000268 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000269 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000270 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000271}
272
273template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000274InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000275elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000276 uint32_t Index = this->getSectionIndex(Sym);
277 if (Index == 0)
Rafael Espindola4cda5812015-10-16 15:29:48 +0000278 return nullptr;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000279 if (Index >= Sections.size() || !Sections[Index])
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000280 fatal("Invalid section index");
Rui Ueyama0b289522016-02-25 18:43:51 +0000281 InputSectionBase<ELFT> *S = Sections[Index];
282 if (S == InputSectionBase<ELFT>::Discarded)
283 return S;
284 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000285}
286
287template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000288SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000289 StringRef Name = fatal(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000290
Rafael Espindola4cda5812015-10-16 15:29:48 +0000291 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000292 case SHN_UNDEF:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000293 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
Rafael Espindola51d46902015-08-28 21:26:51 +0000294 case SHN_COMMON:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000295 return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
296 Sym->getBinding() == llvm::ELF::STB_WEAK,
297 Sym->getVisibility());
Rafael Espindola51d46902015-08-28 21:26:51 +0000298 }
Rafael Espindola20348222015-08-24 21:43:25 +0000299
Rafael Espindolab13df652015-08-11 17:33:02 +0000300 switch (Sym->getBinding()) {
301 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000302 fatal("unexpected binding");
Rafael Espindolab13df652015-08-11 17:33:02 +0000303 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000304 case STB_WEAK:
Rafael Espindola444576d2015-10-09 19:25:07 +0000305 case STB_GNU_UNIQUE: {
Rafael Espindolac159c962015-10-19 21:00:02 +0000306 InputSectionBase<ELFT> *Sec = getSection(*Sym);
Rui Ueyama733153d2016-02-24 18:33:35 +0000307 if (Sec == InputSection<ELFT>::Discarded)
Rui Ueyamae69ab102016-01-06 01:14:11 +0000308 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
309 return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
Rafael Espindola444576d2015-10-09 19:25:07 +0000310 }
Rafael Espindolab13df652015-08-11 17:33:02 +0000311 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000312}
313
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000314void ArchiveFile::parse() {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000315 File = fatal(Archive::create(MB), "Failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000316
317 // Allocate a buffer for Lazy objects.
318 size_t NumSyms = File->getNumberOfSymbols();
319 LazySymbols.reserve(NumSyms);
320
321 // Read the symbol table to construct Lazy objects.
322 for (const Archive::Symbol &Sym : File->symbols())
323 LazySymbols.emplace_back(this, Sym);
324}
325
326// Returns a buffer pointing to a member file containing a given symbol.
327MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000328 Archive::Child C =
329 fatal(Sym->getMember(),
330 "Could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000331
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000332 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000333 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000334
Rafael Espindola1130935c2016-03-03 16:21:44 +0000335 return fatal(C.getMemoryBufferRef(),
336 "Could not get the buffer for the member defining symbol " +
337 Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000338}
339
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000340template <class ELFT>
341SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000342 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000343
Rafael Espindola115f0f32015-11-03 14:13:40 +0000344template <class ELFT>
345const typename ELFFile<ELFT>::Elf_Shdr *
346SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
347 uint32_t Index = this->getSectionIndex(Sym);
348 if (Index == 0)
349 return nullptr;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000350 return fatal(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000351}
352
Rui Ueyama7c713312016-01-06 01:56:36 +0000353// Partially parse the shared object file so that we can call
354// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000355template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rafael Espindolac8b15812015-10-01 15:47:50 +0000356 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
357 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
358 const Elf_Shdr *DynamicSec = nullptr;
359
360 const ELFFile<ELFT> Obj = this->ELFObj;
361 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000362 switch (Sec.sh_type) {
363 default:
364 continue;
365 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000366 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000367 break;
368 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000369 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000370 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000371 case SHT_SYMTAB_SHNDX:
372 this->SymtabSHNDX = fatal(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000373 break;
374 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000375 }
376
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000377 this->initStringTable();
Rui Ueyamae69ab102016-01-06 01:14:11 +0000378 SoName = this->getName();
Rafael Espindolac8b15812015-10-01 15:47:50 +0000379
Rui Ueyama361d8b92015-10-12 15:49:02 +0000380 if (!DynamicSec)
381 return;
382 auto *Begin =
383 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
384 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
Rafael Espindolac8b15812015-10-01 15:47:50 +0000385
Rui Ueyama361d8b92015-10-12 15:49:02 +0000386 for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
387 if (Dyn.d_tag == DT_SONAME) {
388 uintX_t Val = Dyn.getVal();
389 if (Val >= this->StringTable.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000390 fatal("Invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000391 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000392 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000393 }
394 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000395}
Rafael Espindola18173d42015-09-08 15:50:05 +0000396
Rui Ueyama7c713312016-01-06 01:56:36 +0000397// Fully parse the shared object file. This must be called after parseSoName().
398template <class ELFT> void SharedFile<ELFT>::parseRest() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000399 Elf_Sym_Range Syms = this->getNonLocalSymbols();
Rafael Espindola18173d42015-09-08 15:50:05 +0000400 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
401 SymbolBodies.reserve(NumSymbols);
402 for (const Elf_Sym &Sym : Syms) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000403 ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000404 fatal(NameOrErr.getError());
Rafael Espindola18173d42015-09-08 15:50:05 +0000405 StringRef Name = *NameOrErr;
406
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000407 if (Sym.isUndefined())
408 Undefs.push_back(Name);
409 else
410 SymbolBodies.emplace_back(this, Name, Sym);
Rafael Espindola18173d42015-09-08 15:50:05 +0000411 }
412}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000413
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000414BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
415
416bool BitcodeFile::classof(const InputFile *F) {
417 return F->kind() == BitcodeKind;
418}
419
Rafael Espindola4de44b72016-03-02 15:43:50 +0000420void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000421 LLVMContext Context;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000422 std::unique_ptr<IRObjectFile> Obj = fatal(IRObjectFile::create(MB, Context));
423 const Module &M = Obj->getModule();
Rafael Espindola4de44b72016-03-02 15:43:50 +0000424
425 DenseSet<const Comdat *> KeptComdats;
426 for (const auto &P : M.getComdatSymbolTable()) {
427 StringRef N = Saver.save(P.first());
428 if (ComdatGroups.insert(N).second)
429 KeptComdats.insert(&P.second);
430 }
431
Rafael Espindola1130935c2016-03-03 16:21:44 +0000432 for (const BasicSymbolRef &Sym : Obj->symbols()) {
433 if (const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl()))
Rafael Espindola4de44b72016-03-02 15:43:50 +0000434 if (const Comdat *C = GV->getComdat())
435 if (!KeptComdats.count(C))
436 continue;
437
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000438 SmallString<64> Name;
439 raw_svector_ostream OS(Name);
440 Sym.printName(OS);
441 StringRef NameRef = Saver.save(StringRef(Name));
Rafael Espindola6feecec2016-02-19 22:50:16 +0000442 SymbolBody *Body;
Rafael Espindola148445e2016-02-25 16:25:41 +0000443 uint32_t Flags = Sym.getFlags();
Rafael Espindola1b625532016-02-29 14:29:48 +0000444 bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
Rafael Espindola148445e2016-02-25 16:25:41 +0000445 if (Flags & BasicSymbolRef::SF_Undefined)
Rafael Espindola1b625532016-02-29 14:29:48 +0000446 Body = new (Alloc) Undefined(NameRef, IsWeak, STV_DEFAULT, false);
Rafael Espindola6feecec2016-02-19 22:50:16 +0000447 else
Rafael Espindola1b625532016-02-29 14:29:48 +0000448 Body = new (Alloc) DefinedBitcode(NameRef, IsWeak);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000449 SymbolBodies.push_back(Body);
450 }
451}
452
Rui Ueyamac4b65062015-10-12 15:31:09 +0000453template <typename T>
454static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
455 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
456
457 if (!Config->FirstElf)
458 Config->FirstElf = Ret.get();
459
Rui Ueyamae717a712015-10-13 16:20:50 +0000460 if (Config->EKind == ELFNoneKind) {
461 Config->EKind = Ret->getELFKind();
Rui Ueyamac4b65062015-10-12 15:31:09 +0000462 Config->EMachine = Ret->getEMachine();
463 }
464
465 return std::move(Ret);
466}
467
468template <template <class> class T>
Rui Ueyama533c0302016-01-06 00:09:43 +0000469static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
Rui Ueyamad94478b2015-11-20 02:19:36 +0000470 std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000471 if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000472 fatal("Invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000473
474 if (Type.first == ELF::ELFCLASS32) {
475 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000476 return createELFFileAux<T<ELF32LE>>(MB);
477 return createELFFileAux<T<ELF32BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000478 }
479 if (Type.first == ELF::ELFCLASS64) {
480 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000481 return createELFFileAux<T<ELF64LE>>(MB);
482 return createELFFileAux<T<ELF64BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000483 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000484 fatal("Invalid file class: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000485}
486
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000487std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
488 StringRef ArchiveName) {
Rui Ueyamac89bff22016-02-23 18:17:11 +0000489 using namespace sys::fs;
490 std::unique_ptr<InputFile> F;
491 if (identify_magic(MB.getBuffer()) == file_magic::bitcode)
492 F.reset(new BitcodeFile(MB));
493 else
494 F = createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000495 F->ArchiveName = ArchiveName;
496 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000497}
498
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000499std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000500 return createELFFile<SharedFile>(MB);
501}
502
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000503template class elf::ELFFileBase<ELF32LE>;
504template class elf::ELFFileBase<ELF32BE>;
505template class elf::ELFFileBase<ELF64LE>;
506template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000507
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000508template class elf::ObjectFile<ELF32LE>;
509template class elf::ObjectFile<ELF32BE>;
510template class elf::ObjectFile<ELF64LE>;
511template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000512
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000513template class elf::SharedFile<ELF32LE>;
514template class elf::SharedFile<ELF32BE>;
515template class elf::SharedFile<ELF64LE>;
516template class elf::SharedFile<ELF64BE>;