blob: 355d406a83839eebd39812b778f468101a90d811 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- InputFiles.cpp -----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "InputFiles.h"
Rafael Espindola156f4ee2016-04-28 19:30:41 +000011#include "Driver.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000012#include "Error.h"
Rafael Espindola9d13d042016-02-11 15:24:48 +000013#include "InputSection.h"
George Rimar67e3ff82016-08-12 19:56:57 +000014#include "LinkerScript.h"
Peter Collingbourne4f952702016-05-01 04:55:03 +000015#include "SymbolTable.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "Symbols.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000017#include "llvm/ADT/STLExtras.h"
Davide Italiano60976ba2016-06-29 06:12:39 +000018#include "llvm/Bitcode/ReaderWriter.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000019#include "llvm/CodeGen/Analysis.h"
Eugene Leviantb380b242016-10-26 11:07:09 +000020#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000021#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000022#include "llvm/IR/Module.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000023#include "llvm/LTO/LTO.h"
Michael J. Spencera9424f32016-09-09 22:08:04 +000024#include "llvm/MC/StringTableBuilder.h"
Davide Italianoe02ba982016-09-08 21:18:38 +000025#include "llvm/Support/Path.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
Michael J. Spencer1b348a62015-09-04 22:28:10 +000028using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000030using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000031using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
33using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000035
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000036std::vector<InputFile *> InputFile::Pool;
37
Eugene Leviantb380b242016-10-26 11:07:09 +000038template <class ELFT> DIHelper<ELFT>::DIHelper(elf::InputFile *F) {
39 Expected<std::unique_ptr<object::ObjectFile>> Obj =
40 object::ObjectFile::createObjectFile(F->MB);
41 if (!Obj)
42 return;
43
44 DWARFContextInMemory Dwarf(*Obj.get());
45 DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
46 DataExtractor LineData(Dwarf.getLineSection().Data,
47 ELFT::TargetEndianness == support::little,
48 ELFT::Is64Bits ? 8 : 4);
49 // The second parameter is offset in .debug_line section
50 // for compilation unit (CU) of interest. We have only one
51 // CU (object file), so offset is always 0.
52 DwarfLine->getOrParseLineTable(LineData, 0);
53}
54
55template <class ELFT> std::string DIHelper<ELFT>::getLineInfo(uintX_t Offset) {
56 if (!DwarfLine)
57 return "";
58
59 DILineInfo LineInfo;
60 DILineInfoSpecifier Spec;
61 // The offset to CU is 0 (see DIHelper constructor).
62 const DWARFDebugLine::LineTable *LineTbl = DwarfLine->getLineTable(0);
63 if (!LineTbl)
64 return "";
65 LineTbl->getFileLineInfoForAddress(Offset, nullptr, Spec.FLIKind, LineInfo);
66 return LineInfo.Line != 0
67 ? LineInfo.FileName + " (" + std::to_string(LineInfo.Line) + ")"
68 : "";
69}
70
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000071// Deletes all InputFile instances created so far.
72void InputFile::freePool() {
73 // Files are freed in reverse order so that files created
74 // from other files (e.g. object files extracted from archives)
75 // are freed in the proper order.
76 for (int I = Pool.size() - 1; I >= 0; --I)
77 delete Pool[I];
78}
79
Rafael Espindola78db5a92016-05-09 21:40:06 +000080// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Rui Ueyama429ef2a2016-07-15 20:38:28 +000081std::string elf::getFilename(const InputFile *F) {
Rafael Espindola78db5a92016-05-09 21:40:06 +000082 if (!F)
83 return "(internal)";
84 if (!F->ArchiveName.empty())
85 return (F->ArchiveName + "(" + F->getName() + ")").str();
86 return F->getName();
87}
88
George Rimar10874f72016-10-03 11:13:55 +000089template <class ELFT> static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000090 std::error_code EC;
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000091 ELFFile<ELFT> F(MB.getBuffer(), EC);
Rui Ueyamaf8292e92016-07-15 02:01:03 +000092 if (EC)
George Rimarb7aec332016-10-07 08:51:57 +000093 fatal(EC, "failed to read " + MB.getBufferIdentifier());
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000094 return F;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000095}
96
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000097template <class ELFT> static ELFKind getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +000098 if (ELFT::TargetEndianness == support::little)
99 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
100 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +0000101}
102
103template <class ELFT>
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000104ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
105 : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {
106 EKind = getELFKind<ELFT>();
107 EMachine = ELFObj.getHeader()->e_machine;
Rafael Espindola7cc713a2016-10-27 14:00:51 +0000108 OSABI = ELFObj.getHeader()->e_ident[llvm::ELF::EI_OSABI];
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000109}
110
111template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000112typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000113 if (!Symtab)
114 return Elf_Sym_Range(nullptr, nullptr);
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000115 Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
Rafael Espindola18173d42015-09-08 15:50:05 +0000116 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
117 uint32_t FirstNonLocal = Symtab->sh_info;
George Rimar9397cf92016-10-07 15:16:27 +0000118 if (FirstNonLocal == 0 || FirstNonLocal > NumSymbols)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000119 fatal(getFilename(this) + ": invalid sh_info in symbol table");
Rafael Espindola67d72c02016-03-11 12:06:30 +0000120
121 if (OnlyGlobals)
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000122 return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
123 return makeArrayRef(Syms.begin(), Syms.end());
Davide Italiano6d328d32015-09-16 20:45:57 +0000124}
125
Rafael Espindola115f0f32015-11-03 14:13:40 +0000126template <class ELFT>
127uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamadc8d3a22015-12-24 08:36:56 +0000128 uint32_t I = Sym.st_shndx;
129 if (I == ELF::SHN_XINDEX)
Rui Ueyamae69ab102016-01-06 01:14:11 +0000130 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
Rafael Espindola972b2362016-03-09 14:31:18 +0000131 if (I >= ELF::SHN_LORESERVE)
Rafael Espindola115f0f32015-11-03 14:13:40 +0000132 return 0;
Rui Ueyamadc8d3a22015-12-24 08:36:56 +0000133 return I;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000134}
135
Rafael Espindolaaf707642015-10-12 01:55:32 +0000136template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
Rafael Espindola3e603792015-10-01 20:26:37 +0000137 if (!Symtab)
138 return;
Rafael Espindola75714f62016-03-03 22:24:39 +0000139 StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000140}
141
142template <class ELFT>
Rafael Espindola5da1d882016-10-26 15:34:24 +0000143elf::ObjectFile<ELFT>::ObjectFile(BumpPtrAllocator &Alloc, MemoryBufferRef M)
144 : ELFFileBase<ELFT>(Base::ObjectKind, M), Alloc(Alloc) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000145
146template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +0000147ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
148 if (!this->Symtab)
149 return this->SymbolBodies;
150 uint32_t FirstNonLocal = this->Symtab->sh_info;
151 return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
152}
153
154template <class ELFT>
155ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
156 if (!this->Symtab)
157 return this->SymbolBodies;
158 uint32_t FirstNonLocal = this->Symtab->sh_info;
159 return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
160}
161
162template <class ELFT>
163ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
164 if (!this->Symtab)
165 return this->SymbolBodies;
166 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000167}
168
Eugene Leviantb380b242016-10-26 11:07:09 +0000169template <class ELFT> DIHelper<ELFT> *elf::ObjectFile<ELFT>::getDIHelper() {
170 if (!DIH)
171 DIH.reset(new DIHelper<ELFT>(this));
172
173 return DIH.get();
174}
175
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000176template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000177 if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo)
178 return MipsOptions->Reginfo->ri_gp_value;
179 if (!ELFT::Is64Bits && MipsReginfo && MipsReginfo->Reginfo)
Rui Ueyama70eed362016-01-06 22:42:43 +0000180 return MipsReginfo->Reginfo->ri_gp_value;
181 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +0000182}
183
Rafael Espindola444576d2015-10-09 19:25:07 +0000184template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000185void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000186 // Read section and symbol tables.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000187 initializeSections(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000188 initializeSymbols();
Peter Smithbafd3c42016-10-10 10:18:58 +0000189 if (Config->GcSections && Config->EMachine == EM_ARM)
Peter Smith07606052016-10-10 10:10:27 +0000190 initializeReverseDependencies();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000191}
192
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000193// Sections with SHT_GROUP and comdat bits define comdat section groups.
194// They are identified and deduplicated by group name. This function
195// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000196template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000197StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000198 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama188d2c32016-07-15 20:05:05 +0000199 const Elf_Shdr *Symtab = check(Obj.getSection(Sec.sh_link));
200 const Elf_Sym *Sym = Obj.getSymbol(Symtab, Sec.sh_info);
201 StringRef Strtab = check(Obj.getStringTableForSymtab(*Symtab));
202 return check(Sym->getName(Strtab));
Rafael Espindola444576d2015-10-09 19:25:07 +0000203}
204
205template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000206ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000207elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000208 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000209 ArrayRef<Elf_Word> Entries =
210 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000211 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000212 fatal(getFilename(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000213 return Entries.slice(1);
214}
215
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000216template <class ELFT>
217bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000218 // We don't merge sections if -O0 (default is -O1). This makes sometimes
219 // the linker significantly faster, although the output will be bigger.
220 if (Config->Optimize == 0)
221 return false;
222
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000223 // Do not merge sections if generating a relocatable object. It makes
224 // the code simpler because we do not need to update relocation addends
225 // to reflect changes introduced by merging. Instead of that we write
226 // such "merge" sections into separate OutputSections and keep SHF_MERGE
227 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
228 // later during a final linking.
229 if (Config->Relocatable)
230 return false;
231
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000232 // A mergeable section with size 0 is useless because they don't have
233 // any data to merge. A mergeable string section with size 0 can be
234 // argued as invalid because it doesn't end with a null character.
235 // We'll avoid a mess by handling them as if they were non-mergeable.
236 if (Sec.sh_size == 0)
237 return false;
238
Rui Ueyamac75ef852016-09-21 03:22:18 +0000239 // Check for sh_entsize. The ELF spec is not clear about the zero
240 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
241 // the section does not hold a table of fixed-size entries". We know
242 // that Rust 1.13 produces a string mergeable section with a zero
243 // sh_entsize. Here we just accept it rather than being picky about it.
244 uintX_t EntSize = Sec.sh_entsize;
245 if (EntSize == 0)
246 return false;
247 if (Sec.sh_size % EntSize)
248 fatal(getFilename(this) +
249 ": SHF_MERGE section size must be a multiple of sh_entsize");
250
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000251 uintX_t Flags = Sec.sh_flags;
252 if (!(Flags & SHF_MERGE))
253 return false;
254 if (Flags & SHF_WRITE)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000255 fatal(getFilename(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000256
Peter Smith4df2e142016-05-18 11:40:16 +0000257 // Don't try to merge if the alignment is larger than the sh_entsize and this
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000258 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000259 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000260 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
261 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000262 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000263 if (Flags & SHF_STRINGS)
264 return true;
265
George Rimardcddfb62016-06-08 12:04:59 +0000266 return Sec.sh_addralign <= EntSize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000267}
268
269template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000270void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000271 DenseSet<CachedHashStringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000272 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000273 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000274 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000275 const ELFFile<ELFT> &Obj = this->ELFObj;
276 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000277 ++I;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000278 if (Sections[I] == &InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000279 continue;
280
Rui Ueyamaaf9793d2016-10-04 16:47:49 +0000281 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
282 // if -r is given, we'll let the final link discard such sections.
283 // This is compatible with GNU.
284 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
Eugene Leviant27be5422016-09-28 08:42:02 +0000285 Sections[I] = &InputSection<ELFT>::Discarded;
286 continue;
287 }
288
Rafael Espindolacde25132015-08-13 14:45:44 +0000289 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000290 case SHT_GROUP:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000291 Sections[I] = &InputSection<ELFT>::Discarded;
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000292 if (ComdatGroups.insert(CachedHashStringRef(getShtGroupSignature(Sec)))
293 .second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000294 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000295 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000296 if (SecIndex >= Size)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000297 fatal(getFilename(this) + ": invalid section index in group: " +
298 Twine(SecIndex));
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000299 Sections[SecIndex] = &InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000300 }
301 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000302 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000303 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000304 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000305 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000306 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000307 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000308 case SHT_STRTAB:
309 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000310 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000311 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000312 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000313 }
314 }
315}
316
Peter Smith07606052016-10-10 10:10:27 +0000317// .ARM.exidx sections have a reverse dependency on the InputSection they
318// have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
319template <class ELFT>
320void elf::ObjectFile<ELFT>::initializeReverseDependencies() {
321 unsigned I = -1;
322 for (const Elf_Shdr &Sec : this->ELFObj.sections()) {
323 ++I;
324 if ((Sections[I] == &InputSection<ELFT>::Discarded) ||
325 !(Sec.sh_flags & SHF_LINK_ORDER))
326 continue;
327 if (Sec.sh_link >= Sections.size())
328 fatal(getFilename(this) + ": invalid sh_link index: " +
329 Twine(Sec.sh_link));
330 auto *IS = cast<InputSection<ELFT>>(Sections[Sec.sh_link]);
331 IS->DependentSection = Sections[I];
332 }
333}
334
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000335template <class ELFT>
336InputSectionBase<ELFT> *
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000337elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
338 uint32_t Idx = Sec.sh_info;
339 if (Idx >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000340 fatal(getFilename(this) + ": invalid relocated section index: " +
341 Twine(Idx));
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000342 InputSectionBase<ELFT> *Target = Sections[Idx];
343
344 // Strictly speaking, a relocation section must be included in the
345 // group of the section it relocates. However, LLVM 3.3 and earlier
346 // would fail to do so, so we gracefully handle that case.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000347 if (Target == &InputSection<ELFT>::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000348 return nullptr;
349
350 if (!Target)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000351 fatal(getFilename(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000352 return Target;
353}
354
355template <class ELFT>
356InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000357elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola75714f62016-03-03 22:24:39 +0000358 StringRef Name = check(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000359
Rafael Espindola042a3f22016-09-08 14:06:08 +0000360 switch (Sec.sh_type) {
361 case SHT_ARM_ATTRIBUTES:
362 // FIXME: ARM meta-data section. At present attributes are ignored,
363 // they can be used to reason about object compatibility.
364 return &InputSection<ELFT>::Discarded;
365 case SHT_MIPS_REGINFO:
George Rimar17d566d2016-10-17 11:31:46 +0000366 if (MipsReginfo)
367 fatal(getFilename(this) +
368 ": multiple SHT_MIPS_REGINFO sections are not allowed");
Rafael Espindola042a3f22016-09-08 14:06:08 +0000369 MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec, Name));
370 return MipsReginfo.get();
371 case SHT_MIPS_OPTIONS:
George Rimar85e61062016-10-14 11:10:36 +0000372 if (MipsOptions)
373 fatal(getFilename(this) +
374 ": multiple SHT_MIPS_OPTIONS sections are not allowed");
Rafael Espindola042a3f22016-09-08 14:06:08 +0000375 MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec, Name));
376 return MipsOptions.get();
377 case SHT_MIPS_ABIFLAGS:
George Rimar17d566d2016-10-17 11:31:46 +0000378 if (MipsAbiFlags)
379 fatal(getFilename(this) +
380 ": multiple SHT_MIPS_ABIFLAGS sections are not allowed");
Rafael Espindola042a3f22016-09-08 14:06:08 +0000381 MipsAbiFlags.reset(new MipsAbiFlagsInputSection<ELFT>(this, &Sec, Name));
382 return MipsAbiFlags.get();
383 case SHT_RELA:
384 case SHT_REL: {
385 // This section contains relocation information.
386 // If -r is given, we do not interpret or apply relocation
387 // but just copy relocation sections to output.
388 if (Config->Relocatable)
Rafael Espindolaa8631e32016-10-27 13:32:32 +0000389 return new (GAlloc<ELFT>::IAlloc.Allocate())
390 InputSection<ELFT>(this, &Sec, Name);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000391
392 // Find the relocation target section and associate this
393 // section with it.
394 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
395 if (!Target)
396 return nullptr;
397 if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
398 S->RelocSections.push_back(&Sec);
399 return nullptr;
400 }
401 if (auto *S = dyn_cast<EhInputSection<ELFT>>(Target)) {
402 if (S->RelocSection)
403 fatal(getFilename(this) +
404 ": multiple relocation sections to .eh_frame are not supported");
405 S->RelocSection = &Sec;
406 return nullptr;
407 }
408 fatal(getFilename(this) +
409 ": relocations pointing to SHF_MERGE are not supported");
410 }
411 }
412
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000413 // .note.GNU-stack is a marker section to control the presence of
414 // PT_GNU_STACK segment in outputs. Since the presence of the segment
415 // is controlled only by the command line option (-z execstack) in LLD,
416 // .note.GNU-stack is ignored.
417 if (Name == ".note.GNU-stack")
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000418 return &InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000419
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000420 if (Name == ".note.GNU-split-stack") {
George Rimar777f9632016-03-12 08:31:34 +0000421 error("objects using splitstacks are not supported");
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000422 return &InputSection<ELFT>::Discarded;
423 }
424
George Rimarf21aade2016-08-31 08:38:11 +0000425 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000426 return &InputSection<ELFT>::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000427
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000428 // The linker merges EH (exception handling) frames and creates a
429 // .eh_frame_hdr section for runtime. So we handle them with a special
430 // class. For relocatable outputs, they are just passed through.
431 if (Name == ".eh_frame" && !Config->Relocatable)
Rafael Espindolaa8631e32016-10-27 13:32:32 +0000432 return new (GAlloc<ELFT>::EHAlloc.Allocate())
433 EhInputSection<ELFT>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000434
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000435 if (shouldMerge(Sec))
Rafael Espindolaa8631e32016-10-27 13:32:32 +0000436 return new (GAlloc<ELFT>::MAlloc.Allocate())
437 MergeInputSection<ELFT>(this, &Sec, Name);
438 return new (GAlloc<ELFT>::IAlloc.Allocate())
439 InputSection<ELFT>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000440}
441
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000442template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000443 this->initStringTable();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000444 Elf_Sym_Range Syms = this->getElfSymbols(false);
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000445 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000446 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000447 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000448 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000449}
450
451template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000452InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000453elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000454 uint32_t Index = this->getSectionIndex(Sym);
George Rimar683a35d2016-08-12 19:25:54 +0000455 if (Index >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000456 fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
Rui Ueyama0b289522016-02-25 18:43:51 +0000457 InputSectionBase<ELFT> *S = Sections[Index];
George Rimar24adce92016-10-06 09:17:55 +0000458
George Rimar683a35d2016-08-12 19:25:54 +0000459 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03
460 // could generate broken objects. STT_SECTION symbols can be
461 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
462 // In this case it is fine for section to be null here as we
463 // do not allocate sections of these types.
George Rimar24adce92016-10-06 09:17:55 +0000464 if (!S) {
465 if (Index == 0 || Sym.getType() == STT_SECTION)
466 return nullptr;
467 fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
468 }
469
470 if (S == &InputSectionBase<ELFT>::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000471 return S;
472 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000473}
474
475template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000476SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000477 int Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000478 InputSectionBase<ELFT> *Sec = getSection(*Sym);
479 if (Binding == STB_LOCAL) {
Eugene Leviantb380b242016-10-26 11:07:09 +0000480 if (Sym->getType() == STT_FILE)
481 SourceFile = check(Sym->getName(this->StringTable));
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000482 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000483 return new (this->Alloc)
Rui Ueyama434b5612016-07-17 03:11:46 +0000484 Undefined(Sym->st_name, Sym->st_other, Sym->getType(), this);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000485 return new (this->Alloc) DefinedRegular<ELFT>(*Sym, Sec);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000486 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000487
Rafael Espindola75714f62016-03-03 22:24:39 +0000488 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000489
Rafael Espindola4cda5812015-10-16 15:29:48 +0000490 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000491 case SHN_UNDEF:
George Rimar10874f72016-10-03 11:13:55 +0000492 return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
493 Sym->getType(),
494 /*CanOmitFromDynSym*/ false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000496 case SHN_COMMON:
George Rimar27e651d2016-10-10 10:31:03 +0000497 if (Sym->st_value == 0 || Sym->st_value >= UINT32_MAX)
George Rimar0c825612016-10-04 08:49:52 +0000498 fatal(getFilename(this) + ": common symbol '" + Name +
George Rimar27e651d2016-10-10 10:31:03 +0000499 "' has invalid alignment: " + Twine(Sym->st_value));
George Rimar10874f72016-10-03 11:13:55 +0000500 return elf::Symtab<ELFT>::X->addCommon(Name, Sym->st_size, Sym->st_value,
501 Binding, Sym->st_other,
502 Sym->getType(), this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000503 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000504 }
Rafael Espindola20348222015-08-24 21:43:25 +0000505
Rafael Espindola67d72c02016-03-11 12:06:30 +0000506 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000507 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000508 fatal(getFilename(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000509 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000510 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000511 case STB_GNU_UNIQUE:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000512 if (Sec == &InputSection<ELFT>::Discarded)
George Rimar10874f72016-10-03 11:13:55 +0000513 return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
514 Sym->getType(),
515 /*CanOmitFromDynSym*/ false,
516 this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000517 ->body();
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000518 return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000519 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000520}
521
Peter Collingbourne4f952702016-05-01 04:55:03 +0000522template <class ELFT> void ArchiveFile::parse() {
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000523 File = check(Archive::create(MB), "failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000524
Rui Ueyama3d0f77b2016-09-30 17:56:20 +0000525 // Read the symbol table to construct Lazy objects.
526 for (const Archive::Symbol &Sym : File->symbols())
527 Symtab<ELFT>::X->addLazyArchive(this, Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000528}
529
530// Returns a buffer pointing to a member file containing a given symbol.
Davide Italianobcdd6c62016-10-12 19:35:54 +0000531std::pair<MemoryBufferRef, uint64_t>
532ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000533 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000534 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000535 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000536
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000537 if (!Seen.insert(C.getChildOffset()).second)
Davide Italianobcdd6c62016-10-12 19:35:54 +0000538 return {MemoryBufferRef(), 0};
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000539
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000540 MemoryBufferRef Ret =
541 check(C.getMemoryBufferRef(),
542 "could not get the buffer for the member defining symbol " +
543 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000544
Rui Ueyamafe658772016-05-15 17:10:23 +0000545 if (C.getParent()->isThin() && Driver->Cpio)
546 Driver->Cpio->append(relativeToRoot(check(C.getFullName())),
547 Ret.getBuffer());
Davide Italianobcdd6c62016-10-12 19:35:54 +0000548 if (C.getParent()->isThin())
549 return {Ret, 0};
550 return {Ret, C.getChildOffset()};
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000551}
552
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000553template <class ELFT>
Rafael Espindola5da1d882016-10-26 15:34:24 +0000554SharedFile<ELFT>::SharedFile(BumpPtrAllocator &Alloc, MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000555 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000556
Rafael Espindola115f0f32015-11-03 14:13:40 +0000557template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000558const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000559SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
560 uint32_t Index = this->getSectionIndex(Sym);
561 if (Index == 0)
562 return nullptr;
Rafael Espindola75714f62016-03-03 22:24:39 +0000563 return check(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000564}
565
Rui Ueyama7c713312016-01-06 01:56:36 +0000566// Partially parse the shared object file so that we can call
567// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000568template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000569 typedef typename ELFT::Dyn Elf_Dyn;
570 typedef typename ELFT::uint uintX_t;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000571 const Elf_Shdr *DynamicSec = nullptr;
572
573 const ELFFile<ELFT> Obj = this->ELFObj;
574 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000575 switch (Sec.sh_type) {
576 default:
577 continue;
578 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000579 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000580 break;
581 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000582 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000583 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000584 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000585 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000586 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000587 case SHT_GNU_versym:
588 this->VersymSec = &Sec;
589 break;
590 case SHT_GNU_verdef:
591 this->VerdefSec = &Sec;
592 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000593 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000594 }
595
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000596 this->initStringTable();
Rui Ueyama478f8eb2016-09-09 21:35:38 +0000597
598 // DSOs are identified by soname, and they usually contain
599 // DT_SONAME tag in their header. But if they are missing,
600 // filenames are used as default sonames.
Davide Italianoe02ba982016-09-08 21:18:38 +0000601 SoName = sys::path::filename(this->getName());
Rafael Espindolac8b15812015-10-01 15:47:50 +0000602
Rui Ueyama361d8b92015-10-12 15:49:02 +0000603 if (!DynamicSec)
604 return;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000605
George Rimar53cf2a82016-10-07 09:01:04 +0000606 ArrayRef<Elf_Dyn> Arr =
607 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
608 getFilename(this) + ": getSectionContentsAsArray failed");
609 for (const Elf_Dyn &Dyn : Arr) {
Rui Ueyama361d8b92015-10-12 15:49:02 +0000610 if (Dyn.d_tag == DT_SONAME) {
611 uintX_t Val = Dyn.getVal();
612 if (Val >= this->StringTable.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000613 fatal(getFilename(this) + ": invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000614 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000615 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000616 }
617 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000618}
Rafael Espindola18173d42015-09-08 15:50:05 +0000619
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000620// Parse the version definitions in the object file if present. Returns a vector
621// whose nth element contains a pointer to the Elf_Verdef for version identifier
622// n. Version identifiers that are not definitions map to nullptr. The array
623// always has at least length 1.
624template <class ELFT>
625std::vector<const typename ELFT::Verdef *>
626SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
627 std::vector<const Elf_Verdef *> Verdefs(1);
628 // We only need to process symbol versions for this DSO if it has both a
629 // versym and a verdef section, which indicates that the DSO contains symbol
630 // version definitions.
631 if (!VersymSec || !VerdefSec)
632 return Verdefs;
633
634 // The location of the first global versym entry.
635 Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
636 VersymSec->sh_offset) +
637 this->Symtab->sh_info;
638
639 // We cannot determine the largest verdef identifier without inspecting
640 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
641 // sequentially starting from 1, so we predict that the largest identifier
642 // will be VerdefCount.
643 unsigned VerdefCount = VerdefSec->sh_info;
644 Verdefs.resize(VerdefCount + 1);
645
646 // Build the Verdefs array by following the chain of Elf_Verdef objects
647 // from the start of the .gnu.version_d section.
648 const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
649 for (unsigned I = 0; I != VerdefCount; ++I) {
650 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
651 Verdef += CurVerdef->vd_next;
652 unsigned VerdefIndex = CurVerdef->vd_ndx;
653 if (Verdefs.size() <= VerdefIndex)
654 Verdefs.resize(VerdefIndex + 1);
655 Verdefs[VerdefIndex] = CurVerdef;
656 }
657
658 return Verdefs;
659}
660
Rui Ueyama7c713312016-01-06 01:56:36 +0000661// Fully parse the shared object file. This must be called after parseSoName().
662template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000663 // Create mapping from version identifiers to Elf_Verdef entries.
664 const Elf_Versym *Versym = nullptr;
665 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
666
Rafael Espindola67d72c02016-03-11 12:06:30 +0000667 Elf_Sym_Range Syms = this->getElfSymbols(true);
Rafael Espindola18173d42015-09-08 15:50:05 +0000668 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000669 unsigned VersymIndex = 0;
670 if (Versym) {
671 VersymIndex = Versym->vs_index;
672 ++Versym;
673 }
674
Rafael Espindola18da0e52016-04-29 16:23:31 +0000675 StringRef Name = check(Sym.getName(this->StringTable));
676 if (Sym.isUndefined()) {
677 Undefs.push_back(Name);
678 continue;
679 }
680
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000681 if (Versym) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000682 // Ignore local symbols and non-default versions.
Rafael Espindolad2454d62016-06-09 15:45:49 +0000683 if (VersymIndex == VER_NDX_LOCAL || (VersymIndex & VERSYM_HIDDEN))
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000684 continue;
685 }
Rafael Espindolad2454d62016-06-09 15:45:49 +0000686
687 const Elf_Verdef *V =
688 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
689 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
Rafael Espindola18173d42015-09-08 15:50:05 +0000690 }
691}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000692
Rui Ueyama80356882016-08-03 20:33:17 +0000693static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000694 Triple T(getBitcodeTargetTriple(MB, Driver->Context));
695 if (T.isLittleEndian())
696 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
697 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000698}
699
Rui Ueyama80356882016-08-03 20:33:17 +0000700static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000701 Triple T(getBitcodeTargetTriple(MB, Driver->Context));
702 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000703 case Triple::aarch64:
704 return EM_AARCH64;
705 case Triple::arm:
706 return EM_ARM;
707 case Triple::mips:
708 case Triple::mipsel:
709 case Triple::mips64:
710 case Triple::mips64el:
711 return EM_MIPS;
712 case Triple::ppc:
713 return EM_PPC;
714 case Triple::ppc64:
715 return EM_PPC64;
716 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000717 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000718 case Triple::x86_64:
719 return EM_X86_64;
720 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000721 fatal(MB.getBufferIdentifier() +
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000722 ": could not infer e_machine from bitcode target triple " + T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000723 }
724}
725
Rui Ueyama523744d2016-07-07 02:46:30 +0000726BitcodeFile::BitcodeFile(MemoryBufferRef MB) : InputFile(BitcodeKind, MB) {
Rui Ueyama80356882016-08-03 20:33:17 +0000727 EKind = getBitcodeELFKind(MB);
728 EMachine = getBitcodeMachineKind(MB);
Davide Italiano60976ba2016-06-29 06:12:39 +0000729}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000730
Davide Italiano786d8e32016-09-29 00:40:08 +0000731static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
732 switch (GvVisibility) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000733 case GlobalValue::DefaultVisibility:
734 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000735 case GlobalValue::HiddenVisibility:
736 return STV_HIDDEN;
737 case GlobalValue::ProtectedVisibility:
738 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000739 }
George Rimar777f9632016-03-12 08:31:34 +0000740 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000741}
742
Peter Collingbourne4f952702016-05-01 04:55:03 +0000743template <class ELFT>
Rafael Espindola3db70212016-10-25 12:02:31 +0000744static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
Davide Italiano786d8e32016-09-29 00:40:08 +0000745 const lto::InputFile::Symbol &ObjSym,
746 StringSaver &Saver, BitcodeFile *F) {
747 StringRef NameRef = Saver.save(ObjSym.getName());
748 uint32_t Flags = ObjSym.getFlags();
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000749 uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000750
Davide Italiano786d8e32016-09-29 00:40:08 +0000751 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
752 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
753 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000754
Rafael Espindola3db70212016-10-25 12:02:31 +0000755 int C = check(ObjSym.getComdatIndex());
756 if (C != -1 && !KeptComdats[C])
757 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
758 CanOmitFromDynSym, F);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000759
Davide Italiano9f8efff2016-04-22 18:26:33 +0000760 if (Flags & BasicSymbolRef::SF_Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000761 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000762 CanOmitFromDynSym, F);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000763
Davide Italiano786d8e32016-09-29 00:40:08 +0000764 if (Flags & BasicSymbolRef::SF_Common)
765 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
766 ObjSym.getCommonAlignment(), Binding,
767 Visibility, STT_OBJECT, F);
768
769 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
770 CanOmitFromDynSym, F);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000771}
772
Peter Collingbourne4f952702016-05-01 04:55:03 +0000773template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000774void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Davide Italianobcdd6c62016-10-12 19:35:54 +0000775
776 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
777 // (the fully resolved path of the archive) + member name + offset of the
778 // member in the archive.
779 // ThinLTO uses the MemoryBufferRef identifier to access its internal
780 // data structures and if two archives define two members with the same name,
781 // this causes a collision which result in only one of the objects being
782 // taken into consideration at LTO time (which very likely causes undefined
783 // symbols later in the link stage).
784 Obj = check(lto::InputFile::create(MemoryBufferRef(
785 MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() +
786 utostr(OffsetInArchive)))));
Rafael Espindola3db70212016-10-25 12:02:31 +0000787
788 std::vector<bool> KeptComdats;
789 for (StringRef S : Obj->getComdatTable()) {
790 StringRef N = Saver.save(S);
791 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(N)).second);
792 }
793
Davide Italiano786d8e32016-09-29 00:40:08 +0000794 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
Rafael Espindola3db70212016-10-25 12:02:31 +0000795 Symbols.push_back(
796 createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, Saver, this));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000797}
798
Rui Ueyamac4b65062015-10-12 15:31:09 +0000799template <template <class> class T>
Rafael Espindola5da1d882016-10-26 15:34:24 +0000800static InputFile *createELFFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000801 unsigned char Size;
802 unsigned char Endian;
803 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
804 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
George Rimar777f9632016-03-12 08:31:34 +0000805 fatal("invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000806
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000807 InputFile *Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000808 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
Rafael Espindola5da1d882016-10-26 15:34:24 +0000809 Obj = new T<ELF32LE>(Alloc, MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000810 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
Rafael Espindola5da1d882016-10-26 15:34:24 +0000811 Obj = new T<ELF32BE>(Alloc, MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000812 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
Rafael Espindola5da1d882016-10-26 15:34:24 +0000813 Obj = new T<ELF64LE>(Alloc, MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000814 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
Rafael Espindola5da1d882016-10-26 15:34:24 +0000815 Obj = new T<ELF64BE>(Alloc, MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000816 else
817 fatal("invalid file class: " + MB.getBufferIdentifier());
818
819 if (!Config->FirstElf)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000820 Config->FirstElf = Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000821 return Obj;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000822}
823
Rafael Espindola093abab2016-10-27 17:45:40 +0000824template <class ELFT> void BinaryFile::parse() {
825 StringRef Buf = MB.getBuffer();
826 ArrayRef<uint8_t> Data =
827 makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size());
Rui Ueyama673c9d92016-10-20 06:44:58 +0000828
Rafael Espindola093abab2016-10-27 17:45:40 +0000829 std::string Filename = MB.getBufferIdentifier();
830 std::transform(Filename.begin(), Filename.end(), Filename.begin(),
831 [](char C) { return isalnum(C) ? C : '_'; });
832 Filename = "_binary_" + Filename;
833 StringRef StartName = Saver.save(Twine(Filename) + "_start");
834 StringRef EndName = Saver.save(Twine(Filename) + "_end");
835 StringRef SizeName = Saver.save(Twine(Filename) + "_size");
836
837 auto *Section =
838 new InputSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 8, Data, ".data");
839 Sections.push_back(Section);
840
841 elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, Section, STB_GLOBAL,
842 STT_OBJECT, 0);
843 elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, Section, STB_GLOBAL,
844 STT_OBJECT, Data.size());
845 elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, nullptr, STB_GLOBAL,
846 STT_OBJECT, Data.size());
Michael J. Spencera9424f32016-09-09 22:08:04 +0000847}
848
Rui Ueyama4655ea32016-04-08 00:14:55 +0000849static bool isBitcode(MemoryBufferRef MB) {
850 using namespace sys::fs;
851 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
852}
853
Rafael Espindola5da1d882016-10-26 15:34:24 +0000854InputFile *elf::createObjectFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB,
855 StringRef ArchiveName,
Davide Italianobcdd6c62016-10-12 19:35:54 +0000856 uint64_t OffsetInArchive) {
Rafael Espindola5da1d882016-10-26 15:34:24 +0000857 InputFile *F = isBitcode(MB) ? new BitcodeFile(MB)
858 : createELFFile<ObjectFile>(Alloc, MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000859 F->ArchiveName = ArchiveName;
Davide Italianobcdd6c62016-10-12 19:35:54 +0000860 F->OffsetInArchive = OffsetInArchive;
Rui Ueyama71c066d2016-02-02 08:22:41 +0000861 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000862}
863
Rafael Espindola5da1d882016-10-26 15:34:24 +0000864InputFile *elf::createSharedFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB) {
865 return createELFFile<SharedFile>(Alloc, MB);
Rui Ueyama533c0302016-01-06 00:09:43 +0000866}
867
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000868MemoryBufferRef LazyObjectFile::getBuffer() {
869 if (Seen)
870 return MemoryBufferRef();
871 Seen = true;
872 return MB;
873}
874
George Rimar10874f72016-10-03 11:13:55 +0000875template <class ELFT> void LazyObjectFile::parse() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000876 for (StringRef Sym : getSymbols())
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000877 Symtab<ELFT>::X->addLazyObject(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000878}
879
880template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
881 typedef typename ELFT::Shdr Elf_Shdr;
882 typedef typename ELFT::Sym Elf_Sym;
883 typedef typename ELFT::SymRange Elf_Sym_Range;
884
885 const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
886 for (const Elf_Shdr &Sec : Obj.sections()) {
887 if (Sec.sh_type != SHT_SYMTAB)
888 continue;
889 Elf_Sym_Range Syms = Obj.symbols(&Sec);
890 uint32_t FirstNonLocal = Sec.sh_info;
891 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
892 std::vector<StringRef> V;
893 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +0000894 if (Sym.st_shndx != SHN_UNDEF)
895 V.push_back(check(Sym.getName(StringTable)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000896 return V;
897 }
898 return {};
899}
900
901std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
Davide Italiano786d8e32016-09-29 00:40:08 +0000902 std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB));
Rui Ueyamad72dd1f2016-09-29 00:58:10 +0000903 std::vector<StringRef> V;
904 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
905 if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined))
906 V.push_back(Saver.save(Sym.getName()));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000907 return V;
908}
909
Rui Ueyama1f492892016-04-08 20:49:31 +0000910// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000911std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +0000912 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000913 return getBitcodeSymbols();
914
Rui Ueyama4655ea32016-04-08 00:14:55 +0000915 unsigned char Size;
916 unsigned char Endian;
917 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
918 if (Size == ELFCLASS32) {
919 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000920 return getElfSymbols<ELF32LE>();
921 return getElfSymbols<ELF32BE>();
922 }
Rui Ueyama4655ea32016-04-08 00:14:55 +0000923 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000924 return getElfSymbols<ELF64LE>();
925 return getElfSymbols<ELF64BE>();
926}
927
Peter Collingbourne4f952702016-05-01 04:55:03 +0000928template void ArchiveFile::parse<ELF32LE>();
929template void ArchiveFile::parse<ELF32BE>();
930template void ArchiveFile::parse<ELF64LE>();
931template void ArchiveFile::parse<ELF64BE>();
932
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000933template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
934template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
935template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
936template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000937
938template void LazyObjectFile::parse<ELF32LE>();
939template void LazyObjectFile::parse<ELF32BE>();
940template void LazyObjectFile::parse<ELF64LE>();
941template void LazyObjectFile::parse<ELF64BE>();
942
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000943template class elf::ELFFileBase<ELF32LE>;
944template class elf::ELFFileBase<ELF32BE>;
945template class elf::ELFFileBase<ELF64LE>;
946template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000947
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000948template class elf::ObjectFile<ELF32LE>;
949template class elf::ObjectFile<ELF32BE>;
950template class elf::ObjectFile<ELF64LE>;
951template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000952
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000953template class elf::SharedFile<ELF32LE>;
954template class elf::SharedFile<ELF32BE>;
955template class elf::SharedFile<ELF64LE>;
956template class elf::SharedFile<ELF64BE>;
Michael J. Spencera9424f32016-09-09 22:08:04 +0000957
Rafael Espindola093abab2016-10-27 17:45:40 +0000958template void BinaryFile::parse<ELF32LE>();
959template void BinaryFile::parse<ELF32BE>();
960template void BinaryFile::parse<ELF64LE>();
961template void BinaryFile::parse<ELF64BE>();
Eugene Leviantb380b242016-10-26 11:07:09 +0000962
963template class elf::DIHelper<ELF32LE>;
964template class elf::DIHelper<ELF32BE>;
965template class elf::DIHelper<ELF64LE>;
966template class elf::DIHelper<ELF64BE>;