blob: 8b8cf02a8de706441f2858a105acfed2c90f8aba [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
Rafael Espindolae1901cc2015-09-24 15:11:50 +000028namespace {
29class ECRAII {
Michael J. Spencer84487f12015-07-24 21:03:07 +000030 std::error_code EC;
Rafael Espindolae1901cc2015-09-24 15:11:50 +000031
32public:
33 std::error_code &getEC() { return EC; }
Rui Ueyama64cfffd2016-01-28 18:40:06 +000034 ~ECRAII() { fatal(EC); }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000035};
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000036}
37
Rafael Espindola18173d42015-09-08 15:50:05 +000038template <class ELFT>
Rafael Espindola2a4b2712015-10-13 01:17:02 +000039ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef M)
40 : InputFile(K, M), ELFObj(MB.getBuffer(), ECRAII().getEC()) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000041
42template <class ELFT>
Rui Ueyama2022e812015-11-20 02:10:52 +000043ELFKind ELFFileBase<ELFT>::getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +000044 if (ELFT::TargetEndianness == support::little)
45 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
46 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +000047}
48
49template <class ELFT>
Rafael Espindolaaf707642015-10-12 01:55:32 +000050typename ELFFileBase<ELFT>::Elf_Sym_Range
51ELFFileBase<ELFT>::getSymbolsHelper(bool Local) {
Rafael Espindola18173d42015-09-08 15:50:05 +000052 if (!Symtab)
53 return Elf_Sym_Range(nullptr, nullptr);
Rafael Espindolae1901cc2015-09-24 15:11:50 +000054 Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
Rafael Espindola18173d42015-09-08 15:50:05 +000055 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
56 uint32_t FirstNonLocal = Symtab->sh_info;
57 if (FirstNonLocal > NumSymbols)
Rui Ueyama64cfffd2016-01-28 18:40:06 +000058 fatal("Invalid sh_info in symbol table");
Davide Italiano6d328d32015-09-16 20:45:57 +000059 if (!Local)
Rui Ueyama90b3daa2015-09-30 02:37:51 +000060 return make_range(Syms.begin() + FirstNonLocal, Syms.end());
61 // +1 to skip over dummy symbol.
62 return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal);
Davide Italiano6d328d32015-09-16 20:45:57 +000063}
64
Rafael Espindola115f0f32015-11-03 14:13:40 +000065template <class ELFT>
66uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000067 uint32_t I = Sym.st_shndx;
68 if (I == ELF::SHN_XINDEX)
Rui Ueyamae69ab102016-01-06 01:14:11 +000069 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
Rafael Espindola02ce26a2015-12-24 14:22:24 +000070 if (I >= ELF::SHN_LORESERVE || I == ELF::SHN_ABS)
Rafael Espindola115f0f32015-11-03 14:13:40 +000071 return 0;
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000072 return I;
Rafael Espindola115f0f32015-11-03 14:13:40 +000073}
74
Rafael Espindolaaf707642015-10-12 01:55:32 +000075template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
Rafael Espindola3e603792015-10-01 20:26:37 +000076 if (!Symtab)
77 return;
Rafael Espindolae1901cc2015-09-24 15:11:50 +000078 ErrorOr<StringRef> StringTableOrErr = ELFObj.getStringTableForSymtab(*Symtab);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000079 fatal(StringTableOrErr);
Davide Italiano6d328d32015-09-16 20:45:57 +000080 StringTable = *StringTableOrErr;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000081}
82
83template <class ELFT>
Rafael Espindolaaf707642015-10-12 01:55:32 +000084typename ELFFileBase<ELFT>::Elf_Sym_Range
85ELFFileBase<ELFT>::getNonLocalSymbols() {
Davide Italiano6d328d32015-09-16 20:45:57 +000086 return getSymbolsHelper(false);
87}
88
89template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000090elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
Rafael Espindola2a4b2712015-10-13 01:17:02 +000091 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000092
93template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000094typename elf::ObjectFile<ELFT>::Elf_Sym_Range
95elf::ObjectFile<ELFT>::getLocalSymbols() {
Davide Italiano6d328d32015-09-16 20:45:57 +000096 return this->getSymbolsHelper(true);
Rafael Espindola18173d42015-09-08 15:50:05 +000097}
98
Rafael Espindolae0df00b2016-02-28 00:25:54 +000099template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Rui Ueyama70eed362016-01-06 22:42:43 +0000100 if (MipsReginfo)
101 return MipsReginfo->Reginfo->ri_gp_value;
102 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +0000103}
104
Rafael Espindola444576d2015-10-09 19:25:07 +0000105template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000106const typename elf::ObjectFile<ELFT>::Elf_Sym *
107elf::ObjectFile<ELFT>::getLocalSymbol(uintX_t SymIndex) {
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000108 uint32_t FirstNonLocal = this->Symtab->sh_info;
109 if (SymIndex >= FirstNonLocal)
110 return nullptr;
111 Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab);
112 return Syms.begin() + SymIndex;
113}
114
115template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000116void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000117 // Read section and symbol tables.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000118 initializeSections(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000119 initializeSymbols();
120}
121
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000122// Sections with SHT_GROUP and comdat bits define comdat section groups.
123// They are identified and deduplicated by group name. This function
124// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000125template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000126StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000127 const ELFFile<ELFT> &Obj = this->ELFObj;
128 uint32_t SymtabdSectionIndex = Sec.sh_link;
129 ErrorOr<const Elf_Shdr *> SecOrErr = Obj.getSection(SymtabdSectionIndex);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000130 fatal(SecOrErr);
Rafael Espindola444576d2015-10-09 19:25:07 +0000131 const Elf_Shdr *SymtabSec = *SecOrErr;
132 uint32_t SymIndex = Sec.sh_info;
133 const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
134 ErrorOr<StringRef> StringTableOrErr = Obj.getStringTableForSymtab(*SymtabSec);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000135 fatal(StringTableOrErr);
Rafael Espindola444576d2015-10-09 19:25:07 +0000136 ErrorOr<StringRef> SignatureOrErr = Sym->getName(*StringTableOrErr);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000137 fatal(SignatureOrErr);
Rafael Espindola444576d2015-10-09 19:25:07 +0000138 return *SignatureOrErr;
139}
140
141template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000142ArrayRef<typename elf::ObjectFile<ELFT>::uint32_X>
143elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000144 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000145 ErrorOr<ArrayRef<uint32_X>> EntriesOrErr =
146 Obj.template getSectionContentsAsArray<uint32_X>(&Sec);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000147 fatal(EntriesOrErr);
Rui Ueyama33b3f212016-01-06 20:30:02 +0000148 ArrayRef<uint32_X> Entries = *EntriesOrErr;
Rafael Espindola444576d2015-10-09 19:25:07 +0000149 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000150 fatal("Unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000151 return Entries.slice(1);
152}
153
154template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000155static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) {
156 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
157 uintX_t Flags = Sec.sh_flags;
158 if (!(Flags & SHF_MERGE))
159 return false;
160 if (Flags & SHF_WRITE)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000161 fatal("Writable SHF_MERGE sections are not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000162 uintX_t EntSize = Sec.sh_entsize;
George Rimar564da7e2015-11-09 08:40:44 +0000163 if (!EntSize || Sec.sh_size % EntSize)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000164 fatal("SHF_MERGE section size must be a multiple of sh_entsize");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000165
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000166 // Don't try to merge if the aligment is larger than the sh_entsize and this
167 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000168 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000169 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
170 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000171 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000172 if (Flags & SHF_STRINGS)
173 return true;
174
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000175 if (Sec.sh_addralign > EntSize)
176 return false;
177
178 return true;
179}
180
181template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000182void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000183 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000184 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000185 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000186 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000187 const ELFFile<ELFT> &Obj = this->ELFObj;
188 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000189 ++I;
Rui Ueyama733153d2016-02-24 18:33:35 +0000190 if (Sections[I] == InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000191 continue;
192
Rafael Espindolacde25132015-08-13 14:45:44 +0000193 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000194 case SHT_GROUP:
Rui Ueyama733153d2016-02-24 18:33:35 +0000195 Sections[I] = InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000196 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000197 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000198 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000199 if (SecIndex >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000200 fatal("Invalid section index in group");
Rui Ueyama733153d2016-02-24 18:33:35 +0000201 Sections[SecIndex] = InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000202 }
203 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000204 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000205 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000206 break;
Rafael Espindola20348222015-08-24 21:43:25 +0000207 case SHT_SYMTAB_SHNDX: {
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000208 ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000209 fatal(ErrorOrTable);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000210 this->SymtabSHNDX = *ErrorOrTable;
Rafael Espindola20348222015-08-24 21:43:25 +0000211 break;
212 }
Rafael Espindolacde25132015-08-13 14:45:44 +0000213 case SHT_STRTAB:
214 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000215 break;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000216 case SHT_RELA:
217 case SHT_REL: {
218 uint32_t RelocatedSectionIndex = Sec.sh_info;
219 if (RelocatedSectionIndex >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000220 fatal("Invalid relocated section index");
Rafael Espindolac159c962015-10-19 21:00:02 +0000221 InputSectionBase<ELFT> *RelocatedSection =
222 Sections[RelocatedSectionIndex];
Sean Silva09247f82016-02-04 21:41:07 +0000223 // Strictly speaking, a relocation section must be included in the
224 // group of the section it relocates. However, LLVM 3.3 and earlier
225 // would fail to do so, so we gracefully handle that case.
Rui Ueyama733153d2016-02-24 18:33:35 +0000226 if (RelocatedSection == InputSection<ELFT>::Discarded)
Sean Silva09247f82016-02-04 21:41:07 +0000227 continue;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000228 if (!RelocatedSection)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000229 fatal("Unsupported relocation reference");
George Rimar58941ee2016-02-25 08:23:37 +0000230 if (Config->Relocatable) {
231 // For -r, relocation sections are handled as regular input sections.
232 Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
233 } else if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000234 S->RelocSections.push_back(&Sec);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000235 } else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) {
236 if (S->RelocSection)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000237 fatal("Multiple relocation sections to .eh_frame are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000238 S->RelocSection = &Sec;
239 } else {
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000240 fatal("Relocations pointing to SHF_MERGE are not supported");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000241 }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000242 break;
243 }
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000244 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000245 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000246 }
247 }
248}
249
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000250template <class ELFT>
251InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000252elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000253 ErrorOr<StringRef> NameOrErr = this->ELFObj.getSectionName(&Sec);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000254 fatal(NameOrErr);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000255 StringRef Name = *NameOrErr;
256
257 // .note.GNU-stack is a marker section to control the presence of
258 // PT_GNU_STACK segment in outputs. Since the presence of the segment
259 // is controlled only by the command line option (-z execstack) in LLD,
260 // .note.GNU-stack is ignored.
261 if (Name == ".note.GNU-stack")
Rui Ueyama733153d2016-02-24 18:33:35 +0000262 return InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000263
264 // A MIPS object file has a special section that contains register
265 // usage info, which needs to be handled by the linker specially.
Simon Atanasyan57830b62015-12-25 13:02:13 +0000266 if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
Rui Ueyamae69ab102016-01-06 01:14:11 +0000267 MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
Simon Atanasyan57830b62015-12-25 13:02:13 +0000268 return MipsReginfo;
269 }
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000270
271 if (Name == ".eh_frame")
Rui Ueyamae69ab102016-01-06 01:14:11 +0000272 return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000273 if (shouldMerge<ELFT>(Sec))
Rui Ueyamae69ab102016-01-06 01:14:11 +0000274 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
275 return new (Alloc) InputSection<ELFT>(this, &Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000276}
277
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000278template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000279 this->initStringTable();
Rafael Espindola18173d42015-09-08 15:50:05 +0000280 Elf_Sym_Range Syms = this->getNonLocalSymbols();
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000281 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000282 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000283 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000284 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000285}
286
287template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000288InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000289elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000290 uint32_t Index = this->getSectionIndex(Sym);
291 if (Index == 0)
Rafael Espindola4cda5812015-10-16 15:29:48 +0000292 return nullptr;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000293 if (Index >= Sections.size() || !Sections[Index])
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000294 fatal("Invalid section index");
Rui Ueyama0b289522016-02-25 18:43:51 +0000295 InputSectionBase<ELFT> *S = Sections[Index];
296 if (S == InputSectionBase<ELFT>::Discarded)
297 return S;
298 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000299}
300
301template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000302SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000303 ErrorOr<StringRef> NameOrErr = Sym->getName(this->StringTable);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000304 fatal(NameOrErr);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000305 StringRef Name = *NameOrErr;
Rafael Espindola20348222015-08-24 21:43:25 +0000306
Rafael Espindola4cda5812015-10-16 15:29:48 +0000307 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000308 case SHN_UNDEF:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000309 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
Rafael Espindola51d46902015-08-28 21:26:51 +0000310 case SHN_COMMON:
Rui Ueyamae69ab102016-01-06 01:14:11 +0000311 return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
312 Sym->getBinding() == llvm::ELF::STB_WEAK,
313 Sym->getVisibility());
Rafael Espindola51d46902015-08-28 21:26:51 +0000314 }
Rafael Espindola20348222015-08-24 21:43:25 +0000315
Rafael Espindolab13df652015-08-11 17:33:02 +0000316 switch (Sym->getBinding()) {
317 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000318 fatal("unexpected binding");
Rafael Espindolab13df652015-08-11 17:33:02 +0000319 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000320 case STB_WEAK:
Rafael Espindola444576d2015-10-09 19:25:07 +0000321 case STB_GNU_UNIQUE: {
Rafael Espindolac159c962015-10-19 21:00:02 +0000322 InputSectionBase<ELFT> *Sec = getSection(*Sym);
Rui Ueyama733153d2016-02-24 18:33:35 +0000323 if (Sec == InputSection<ELFT>::Discarded)
Rui Ueyamae69ab102016-01-06 01:14:11 +0000324 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
325 return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
Rafael Espindola444576d2015-10-09 19:25:07 +0000326 }
Rafael Espindolab13df652015-08-11 17:33:02 +0000327 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000328}
329
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000330void ArchiveFile::parse() {
Rui Ueyama9b093692016-01-06 00:51:35 +0000331 ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000332 fatal(FileOrErr, "Failed to parse archive");
Rui Ueyama9b093692016-01-06 00:51:35 +0000333 File = std::move(*FileOrErr);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000334
335 // Allocate a buffer for Lazy objects.
336 size_t NumSyms = File->getNumberOfSymbols();
337 LazySymbols.reserve(NumSyms);
338
339 // Read the symbol table to construct Lazy objects.
340 for (const Archive::Symbol &Sym : File->symbols())
341 LazySymbols.emplace_back(this, Sym);
342}
343
344// Returns a buffer pointing to a member file containing a given symbol.
345MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000346 ErrorOr<Archive::Child> COrErr = Sym->getMember();
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000347 fatal(COrErr, "Could not get the member for symbol " + Sym->getName());
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000348 const Archive::Child &C = *COrErr;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000349
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000350 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000351 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000352
Rui Ueyama784b7692015-12-17 01:51:23 +0000353 ErrorOr<MemoryBufferRef> RefOrErr = C.getMemoryBufferRef();
354 if (!RefOrErr)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000355 fatal(RefOrErr, "Could not get the buffer for the member defining symbol " +
356 Sym->getName());
Rui Ueyama784b7692015-12-17 01:51:23 +0000357 return *RefOrErr;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000358}
359
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000360template <class ELFT>
361SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000362 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000363
Rafael Espindola115f0f32015-11-03 14:13:40 +0000364template <class ELFT>
365const typename ELFFile<ELFT>::Elf_Shdr *
366SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
367 uint32_t Index = this->getSectionIndex(Sym);
368 if (Index == 0)
369 return nullptr;
370 ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000371 fatal(Ret);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000372 return *Ret;
373}
374
Rui Ueyama7c713312016-01-06 01:56:36 +0000375// Partially parse the shared object file so that we can call
376// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000377template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rafael Espindolac8b15812015-10-01 15:47:50 +0000378 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
379 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
380 const Elf_Shdr *DynamicSec = nullptr;
381
382 const ELFFile<ELFT> Obj = this->ELFObj;
383 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000384 switch (Sec.sh_type) {
385 default:
386 continue;
387 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000388 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000389 break;
390 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000391 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000392 break;
393 case SHT_SYMTAB_SHNDX: {
394 ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000395 fatal(ErrorOrTable);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000396 this->SymtabSHNDX = *ErrorOrTable;
397 break;
398 }
399 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000400 }
401
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000402 this->initStringTable();
Rui Ueyamae69ab102016-01-06 01:14:11 +0000403 SoName = this->getName();
Rafael Espindolac8b15812015-10-01 15:47:50 +0000404
Rui Ueyama361d8b92015-10-12 15:49:02 +0000405 if (!DynamicSec)
406 return;
407 auto *Begin =
408 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
409 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
Rafael Espindolac8b15812015-10-01 15:47:50 +0000410
Rui Ueyama361d8b92015-10-12 15:49:02 +0000411 for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
412 if (Dyn.d_tag == DT_SONAME) {
413 uintX_t Val = Dyn.getVal();
414 if (Val >= this->StringTable.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000415 fatal("Invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000416 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000417 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000418 }
419 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000420}
Rafael Espindola18173d42015-09-08 15:50:05 +0000421
Rui Ueyama7c713312016-01-06 01:56:36 +0000422// Fully parse the shared object file. This must be called after parseSoName().
423template <class ELFT> void SharedFile<ELFT>::parseRest() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000424 Elf_Sym_Range Syms = this->getNonLocalSymbols();
Rafael Espindola18173d42015-09-08 15:50:05 +0000425 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
426 SymbolBodies.reserve(NumSymbols);
427 for (const Elf_Sym &Sym : Syms) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000428 ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable);
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000429 fatal(NameOrErr.getError());
Rafael Espindola18173d42015-09-08 15:50:05 +0000430 StringRef Name = *NameOrErr;
431
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000432 if (Sym.isUndefined())
433 Undefs.push_back(Name);
434 else
435 SymbolBodies.emplace_back(this, Name, Sym);
Rafael Espindola18173d42015-09-08 15:50:05 +0000436 }
437}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000438
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000439BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
440
441bool BitcodeFile::classof(const InputFile *F) {
442 return F->kind() == BitcodeKind;
443}
444
Rafael Espindola4de44b72016-03-02 15:43:50 +0000445void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000446 LLVMContext Context;
447 ErrorOr<std::unique_ptr<IRObjectFile>> ObjOrErr =
448 IRObjectFile::create(MB, Context);
449 fatal(ObjOrErr);
450 IRObjectFile &Obj = **ObjOrErr;
Rafael Espindola4de44b72016-03-02 15:43:50 +0000451 const Module &M = Obj.getModule();
452
453 DenseSet<const Comdat *> KeptComdats;
454 for (const auto &P : M.getComdatSymbolTable()) {
455 StringRef N = Saver.save(P.first());
456 if (ComdatGroups.insert(N).second)
457 KeptComdats.insert(&P.second);
458 }
459
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000460 for (const BasicSymbolRef &Sym : Obj.symbols()) {
Rafael Espindola4de44b72016-03-02 15:43:50 +0000461 if (const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl()))
462 if (const Comdat *C = GV->getComdat())
463 if (!KeptComdats.count(C))
464 continue;
465
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000466 SmallString<64> Name;
467 raw_svector_ostream OS(Name);
468 Sym.printName(OS);
469 StringRef NameRef = Saver.save(StringRef(Name));
Rafael Espindola6feecec2016-02-19 22:50:16 +0000470 SymbolBody *Body;
Rafael Espindola148445e2016-02-25 16:25:41 +0000471 uint32_t Flags = Sym.getFlags();
Rafael Espindola1b625532016-02-29 14:29:48 +0000472 bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
Rafael Espindola148445e2016-02-25 16:25:41 +0000473 if (Flags & BasicSymbolRef::SF_Undefined)
Rafael Espindola1b625532016-02-29 14:29:48 +0000474 Body = new (Alloc) Undefined(NameRef, IsWeak, STV_DEFAULT, false);
Rafael Espindola6feecec2016-02-19 22:50:16 +0000475 else
Rafael Espindola1b625532016-02-29 14:29:48 +0000476 Body = new (Alloc) DefinedBitcode(NameRef, IsWeak);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000477 SymbolBodies.push_back(Body);
478 }
479}
480
Rui Ueyamac4b65062015-10-12 15:31:09 +0000481template <typename T>
482static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
483 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
484
485 if (!Config->FirstElf)
486 Config->FirstElf = Ret.get();
487
Rui Ueyamae717a712015-10-13 16:20:50 +0000488 if (Config->EKind == ELFNoneKind) {
489 Config->EKind = Ret->getELFKind();
Rui Ueyamac4b65062015-10-12 15:31:09 +0000490 Config->EMachine = Ret->getEMachine();
491 }
492
493 return std::move(Ret);
494}
495
496template <template <class> class T>
Rui Ueyama533c0302016-01-06 00:09:43 +0000497static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
Rui Ueyamad94478b2015-11-20 02:19:36 +0000498 std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000499 if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000500 fatal("Invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000501
502 if (Type.first == ELF::ELFCLASS32) {
503 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000504 return createELFFileAux<T<ELF32LE>>(MB);
505 return createELFFileAux<T<ELF32BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000506 }
507 if (Type.first == ELF::ELFCLASS64) {
508 if (Type.second == ELF::ELFDATA2LSB)
Rui Ueyamad94478b2015-11-20 02:19:36 +0000509 return createELFFileAux<T<ELF64LE>>(MB);
510 return createELFFileAux<T<ELF64BE>>(MB);
Rui Ueyamac4b65062015-10-12 15:31:09 +0000511 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000512 fatal("Invalid file class: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000513}
514
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000515std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
516 StringRef ArchiveName) {
Rui Ueyamac89bff22016-02-23 18:17:11 +0000517 using namespace sys::fs;
518 std::unique_ptr<InputFile> F;
519 if (identify_magic(MB.getBuffer()) == file_magic::bitcode)
520 F.reset(new BitcodeFile(MB));
521 else
522 F = createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000523 F->ArchiveName = ArchiveName;
524 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000525}
526
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000527std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000528 return createELFFile<SharedFile>(MB);
529}
530
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000531template class elf::ELFFileBase<ELF32LE>;
532template class elf::ELFFileBase<ELF32BE>;
533template class elf::ELFFileBase<ELF64LE>;
534template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000535
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000536template class elf::ObjectFile<ELF32LE>;
537template class elf::ObjectFile<ELF32BE>;
538template class elf::ObjectFile<ELF64LE>;
539template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000540
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000541template class elf::SharedFile<ELF32LE>;
542template class elf::SharedFile<ELF32BE>;
543template class elf::SharedFile<ELF64LE>;
544template class elf::SharedFile<ELF64BE>;