blob: 4e56b0c1601a36ae51f9dfe973c83f87aea92c17 [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"
George Rimar67e3ff82016-08-12 19:56:57 +000013#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000014#include "Memory.h"
Peter Collingbourne4f952702016-05-01 04:55:03 +000015#include "SymbolTable.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "Symbols.h"
Peter Smith532bc982016-12-14 10:36:12 +000017#include "SyntheticSections.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018#include "llvm/ADT/STLExtras.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"
Eugene Leviantc4681202016-11-01 09:17:50 +000025#include "llvm/Object/ELFObjectFile.h"
Davide Italianoe02ba982016-09-08 21:18:38 +000026#include "llvm/Support/Path.h"
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000027#include "llvm/Support/TarWriter.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000028#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
Michael J. Spencer1b348a62015-09-04 22:28:10 +000030using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000032using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000033using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000034
35using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000036using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000037
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000038TarWriter *elf::Tar;
39
Rui Ueyama4e4e8662017-04-03 19:11:23 +000040InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
Rui Ueyama37e60a52017-03-30 21:13:00 +000041
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000042Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
Rui Ueyama875ae822017-07-20 18:17:55 +000043 // The --chroot option changes our virtual root directory.
44 // This is useful when you are dealing with files created by --reproduce.
45 if (!Config->Chroot.empty() && Path.startswith("/"))
46 Path = Saver.save(Config->Chroot + Path);
47
Rui Ueyamae6e206d2017-02-21 23:22:56 +000048 log(Path);
Rui Ueyama875ae822017-07-20 18:17:55 +000049
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000050 auto MBOrErr = MemoryBuffer::getFile(Path);
51 if (auto EC = MBOrErr.getError()) {
Rui Ueyamac8d3a832017-01-12 22:18:04 +000052 error("cannot open " + Path + ": " + EC.message());
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000053 return None;
54 }
Rui Ueyamae6e206d2017-02-21 23:22:56 +000055
Rui Ueyamaec1c75e2017-01-09 01:42:02 +000056 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
57 MemoryBufferRef MBRef = MB->getMemBufferRef();
58 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
59
60 if (Tar)
61 Tar->append(relativeToRoot(Path), MBRef.getBuffer());
62 return MBRef;
63}
64
Rui Ueyama7463ada2016-11-02 19:51:41 +000065template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() {
Rafael Espindola8b1afd52017-07-19 22:27:35 +000066 DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
67 const DWARFObject &Obj = Dwarf.getDWARFObj();
Paul Robinsonc60318a2017-06-29 16:52:29 +000068 DwarfLine.reset(new DWARFDebugLine);
Rafael Espindola8b1afd52017-07-19 22:27:35 +000069 DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
Paul Robinsonc60318a2017-06-29 16:52:29 +000070 Config->Wordsize);
Rui Ueyama7556f6b2016-11-02 18:42:13 +000071
Eugene Leviantb380b242016-10-26 11:07:09 +000072 // The second parameter is offset in .debug_line section
73 // for compilation unit (CU) of interest. We have only one
74 // CU (object file), so offset is always 0.
75 DwarfLine->getOrParseLineTable(LineData, 0);
76}
77
Rui Ueyama7556f6b2016-11-02 18:42:13 +000078// Returns source line information for a given offset
79// using DWARF debug info.
Eugene Leviantc4681202016-11-01 09:17:50 +000080template <class ELFT>
Rui Ueyamab8760202017-03-30 19:13:47 +000081Optional<DILineInfo> elf::ObjectFile<ELFT>::getDILineInfo(InputSectionBase *S,
82 uint64_t Offset) {
Igor Kudrin7d2b15a2017-07-21 11:26:08 +000083 llvm::call_once(InitDwarfLine, [this]() { initializeDwarfLine(); });
Eugene Leviantb380b242016-10-26 11:07:09 +000084
Rui Ueyama7556f6b2016-11-02 18:42:13 +000085 // The offset to CU is 0.
86 const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
87 if (!Tbl)
Rui Ueyamab8760202017-03-30 19:13:47 +000088 return None;
Eugene Leviantc4681202016-11-01 09:17:50 +000089
90 // Use fake address calcuated by adding section file offset and offset in
Rui Ueyama7556f6b2016-11-02 18:42:13 +000091 // section. See comments for ObjectInfo class.
92 DILineInfo Info;
George Rimar92c88a42016-12-07 19:42:25 +000093 Tbl->getFileLineInfoForAddress(
Rafael Espindola35ae65e2017-03-08 15:57:17 +000094 S->getOffsetInFile() + Offset, nullptr,
George Rimar92c88a42016-12-07 19:42:25 +000095 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
Rui Ueyama7556f6b2016-11-02 18:42:13 +000096 if (Info.Line == 0)
Rui Ueyamab8760202017-03-30 19:13:47 +000097 return None;
98 return Info;
99}
100
101// Returns source line information for a given offset
102// using DWARF debug info.
103template <class ELFT>
104std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase *S,
105 uint64_t Offset) {
106 if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
107 return Info->FileName + ":" + std::to_string(Info->Line);
108 return "";
Eugene Leviantb380b242016-10-26 11:07:09 +0000109}
110
Rui Ueyama8a3ef952017-04-28 20:00:09 +0000111// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
Rui Ueyamace039262017-01-06 10:04:08 +0000112std::string lld::toString(const InputFile *F) {
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000113 if (!F)
Rui Ueyama8a3ef952017-04-28 20:00:09 +0000114 return "<internal>";
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000115
116 if (F->ToStringCache.empty()) {
117 if (F->ArchiveName.empty())
118 F->ToStringCache = F->getName();
119 else
120 F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
121 }
122 return F->ToStringCache;
Rafael Espindola78db5a92016-05-09 21:40:06 +0000123}
124
Rui Ueyama2022e812015-11-20 02:10:52 +0000125template <class ELFT>
Rafael Espindolae19abab2016-11-03 20:44:50 +0000126ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000127 if (ELFT::TargetEndianness == support::little)
128 EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
129 else
130 EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
131
Rafael Espindolae19abab2016-11-03 20:44:50 +0000132 EMachine = getObj().getHeader()->e_machine;
133 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000134}
135
136template <class ELFT>
Rafael Espindola8e232572016-11-03 20:48:57 +0000137typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() {
George Rimar0b2d3742016-11-14 10:05:53 +0000138 return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end());
Davide Italiano6d328d32015-09-16 20:45:57 +0000139}
140
Rafael Espindola115f0f32015-11-03 14:13:40 +0000141template <class ELFT>
142uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyama37e60a52017-03-30 21:13:00 +0000143 return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000144 toString(this));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000145}
146
Rafael Espindola6d18d382016-11-03 13:24:29 +0000147template <class ELFT>
Rafael Espindola21d8be92016-11-03 15:43:47 +0000148void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
149 const Elf_Shdr *Symtab) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000150 FirstNonLocal = Symtab->sh_info;
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000151 Symbols = check(getObj().symbols(Symtab), toString(this));
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000152 if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000153 fatal(toString(this) + ": invalid sh_info in symbol table");
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000154
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000155 StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
156 toString(this));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000157}
158
159template <class ELFT>
Rui Ueyama330e52b2017-04-26 22:51:51 +0000160elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M, StringRef ArchiveName)
161 : ELFFileBase<ELFT>(Base::ObjectKind, M) {
162 this->ArchiveName = ArchiveName;
163}
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000164
165template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +0000166ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000167 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000168 return this->SymbolBodies;
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000169 return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000170}
171
172template <class ELFT>
173ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000174 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000175 return this->SymbolBodies;
176 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000177}
178
Rafael Espindola444576d2015-10-09 19:25:07 +0000179template <class ELFT>
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000180void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000181 // Read section and symbol tables.
Rafael Espindola73c3a362016-11-08 15:51:00 +0000182 initializeSections(ComdatGroups);
183 initializeSymbols();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000184}
185
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000186// Sections with SHT_GROUP and comdat bits define comdat section groups.
187// They are identified and deduplicated by group name. This function
188// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000189template <class ELFT>
Rafael Espindola7c7abaf2016-11-03 02:28:13 +0000190StringRef
191elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
192 const Elf_Shdr &Sec) {
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000193 // Group signatures are stored as symbol names in object files.
194 // sh_info contains a symbol index, so we fetch a symbol and read its name.
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000195 if (this->Symbols.empty())
Rui Ueyama37e60a52017-03-30 21:13:00 +0000196 this->initSymtab(
197 Sections,
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000198 check(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this)));
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000199
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000200 const Elf_Sym *Sym = check(
201 object::getSymbol<ELFT>(this->Symbols, Sec.sh_info), toString(this));
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000202 StringRef Signature = check(Sym->getName(this->StringTable), toString(this));
203
204 // As a special case, if a symbol is a section symbol and has no name,
205 // we use a section name as a signature.
206 //
207 // Such SHT_GROUP sections are invalid from the perspective of the ELF
208 // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
209 // older produce such sections as outputs for the -r option, so we need
210 // a bug-compatibility.
211 if (Signature.empty() && Sym->getType() == STT_SECTION)
212 return getSectionName(Sec);
213 return Signature;
Rafael Espindola444576d2015-10-09 19:25:07 +0000214}
215
216template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000217ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000218elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000219 const ELFFile<ELFT> &Obj = this->getObj();
Rui Ueyama37e60a52017-03-30 21:13:00 +0000220 ArrayRef<Elf_Word> Entries = check(
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000221 Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), toString(this));
Rafael Espindola444576d2015-10-09 19:25:07 +0000222 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000223 fatal(toString(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000224 return Entries.slice(1);
225}
226
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000227template <class ELFT>
228bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000229 // We don't merge sections if -O0 (default is -O1). This makes sometimes
230 // the linker significantly faster, although the output will be bigger.
231 if (Config->Optimize == 0)
232 return false;
233
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000234 // Do not merge sections if generating a relocatable object. It makes
235 // the code simpler because we do not need to update relocation addends
236 // to reflect changes introduced by merging. Instead of that we write
237 // such "merge" sections into separate OutputSections and keep SHF_MERGE
238 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
239 // later during a final linking.
240 if (Config->Relocatable)
241 return false;
242
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000243 // A mergeable section with size 0 is useless because they don't have
244 // any data to merge. A mergeable string section with size 0 can be
245 // argued as invalid because it doesn't end with a null character.
246 // We'll avoid a mess by handling them as if they were non-mergeable.
247 if (Sec.sh_size == 0)
248 return false;
249
Rui Ueyamac75ef852016-09-21 03:22:18 +0000250 // Check for sh_entsize. The ELF spec is not clear about the zero
251 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
252 // the section does not hold a table of fixed-size entries". We know
253 // that Rust 1.13 produces a string mergeable section with a zero
254 // sh_entsize. Here we just accept it rather than being picky about it.
Rui Ueyama9cc84382017-02-24 19:52:52 +0000255 uint64_t EntSize = Sec.sh_entsize;
Rui Ueyamac75ef852016-09-21 03:22:18 +0000256 if (EntSize == 0)
257 return false;
258 if (Sec.sh_size % EntSize)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000259 fatal(toString(this) +
Rui Ueyamac75ef852016-09-21 03:22:18 +0000260 ": SHF_MERGE section size must be a multiple of sh_entsize");
261
Rui Ueyama9cc84382017-02-24 19:52:52 +0000262 uint64_t Flags = Sec.sh_flags;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000263 if (!(Flags & SHF_MERGE))
264 return false;
265 if (Flags & SHF_WRITE)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000266 fatal(toString(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000267
Peter Smith4df2e142016-05-18 11:40:16 +0000268 // Don't try to merge if the alignment is larger than the sh_entsize and this
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000269 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000270 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000271 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
272 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000273 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000274 if (Flags & SHF_STRINGS)
275 return true;
276
George Rimardcddfb62016-06-08 12:04:59 +0000277 return Sec.sh_addralign <= EntSize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000278}
279
280template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000281void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000282 DenseSet<CachedHashStringRef> &ComdatGroups) {
Rui Ueyama240b9512017-05-26 02:17:30 +0000283 const ELFFile<ELFT> &Obj = this->getObj();
284
Rui Ueyama37e60a52017-03-30 21:13:00 +0000285 ArrayRef<Elf_Shdr> ObjSections =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000286 check(this->getObj().sections(), toString(this));
Rafael Espindola235d82c2016-11-02 14:42:20 +0000287 uint64_t Size = ObjSections.size();
George Rimar3f7c3df2017-03-21 08:44:25 +0000288 this->Sections.resize(Size);
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000289 this->SectionStringTable =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000290 check(Obj.getSectionStringTable(ObjSections), toString(this));
Rui Ueyama240b9512017-05-26 02:17:30 +0000291
292 for (size_t I = 0, E = ObjSections.size(); I < E; I++) {
George Rimar3f7c3df2017-03-21 08:44:25 +0000293 if (this->Sections[I] == &InputSection::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000294 continue;
Rui Ueyama240b9512017-05-26 02:17:30 +0000295 const Elf_Shdr &Sec = ObjSections[I];
Rafael Espindola444576d2015-10-09 19:25:07 +0000296
Rui Ueyamaaf9793d2016-10-04 16:47:49 +0000297 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
298 // if -r is given, we'll let the final link discard such sections.
299 // This is compatible with GNU.
300 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
George Rimar3f7c3df2017-03-21 08:44:25 +0000301 this->Sections[I] = &InputSection::Discarded;
Eugene Leviant27be5422016-09-28 08:42:02 +0000302 continue;
303 }
304
Rafael Espindolacde25132015-08-13 14:45:44 +0000305 switch (Sec.sh_type) {
George Rimar3b189d12017-05-29 08:37:50 +0000306 case SHT_GROUP: {
Rui Ueyamaa1ba8592017-06-09 02:42:20 +0000307 // De-duplicate section groups by their signatures.
308 StringRef Signature = getShtGroupSignature(ObjSections, Sec);
309 bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
310 this->Sections[I] = &InputSection::Discarded;
George Rimar3b189d12017-05-29 08:37:50 +0000311
Rui Ueyamaa1ba8592017-06-09 02:42:20 +0000312 // If it is a new section group, we want to keep group members.
313 // Group leader sections, which contain indices of group members, are
314 // discarded because they are useless beyond this point. The only
315 // exception is the -r option because in order to produce re-linkable
316 // object files, we want to pass through basically everything.
317 if (IsNew) {
318 if (Config->Relocatable)
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000319 this->Sections[I] = createInputSection(Sec);
Rui Ueyamaa1ba8592017-06-09 02:42:20 +0000320 continue;
321 }
322
323 // Otherwise, discard group members.
Rui Ueyama33b3f212016-01-06 20:30:02 +0000324 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000325 if (SecIndex >= Size)
George Rimar3f7c3df2017-03-21 08:44:25 +0000326 fatal(toString(this) +
327 ": invalid section index in group: " + Twine(SecIndex));
328 this->Sections[SecIndex] = &InputSection::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000329 }
330 break;
George Rimar3b189d12017-05-29 08:37:50 +0000331 }
Rafael Espindolacde25132015-08-13 14:45:44 +0000332 case SHT_SYMTAB:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000333 this->initSymtab(ObjSections, &Sec);
Rafael Espindolacde25132015-08-13 14:45:44 +0000334 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000335 case SHT_SYMTAB_SHNDX:
Rui Ueyama37e60a52017-03-30 21:13:00 +0000336 this->SymtabSHNDX =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000337 check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
Rafael Espindola20348222015-08-24 21:43:25 +0000338 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000339 case SHT_STRTAB:
340 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000341 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000342 default:
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000343 this->Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000344 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000345
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000346 // .ARM.exidx sections have a reverse dependency on the InputSection they
347 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
348 if (Sec.sh_flags & SHF_LINK_ORDER) {
George Rimarf56cadd2017-03-21 08:57:13 +0000349 if (Sec.sh_link >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000350 fatal(toString(this) + ": invalid sh_link index: " +
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000351 Twine(Sec.sh_link));
George Rimar3f7c3df2017-03-21 08:44:25 +0000352 this->Sections[Sec.sh_link]->DependentSections.push_back(
353 this->Sections[I]);
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000354 }
Peter Smith07606052016-10-10 10:10:27 +0000355 }
356}
357
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000358template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000359InputSectionBase *elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000360 uint32_t Idx = Sec.sh_info;
George Rimar3f7c3df2017-03-21 08:44:25 +0000361 if (Idx >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000362 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
George Rimar3f7c3df2017-03-21 08:44:25 +0000363 InputSectionBase *Target = this->Sections[Idx];
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000364
365 // Strictly speaking, a relocation section must be included in the
366 // group of the section it relocates. However, LLVM 3.3 and earlier
367 // would fail to do so, so we gracefully handle that case.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000368 if (Target == &InputSection::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000369 return nullptr;
370
371 if (!Target)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000372 fatal(toString(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000373 return Target;
374}
375
Rui Ueyama92a8d792017-05-01 20:49:09 +0000376// Create a regular InputSection class that has the same contents
377// as a given section.
378InputSectionBase *toRegularSection(MergeInputSection *Sec) {
379 auto *Ret = make<InputSection>(Sec->Flags, Sec->Type, Sec->Alignment,
380 Sec->Data, Sec->Name);
381 Ret->File = Sec->File;
382 return Ret;
383}
384
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000385template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000386InputSectionBase *
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000387elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
388 StringRef Name = getSectionName(Sec);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000389
Rafael Espindola042a3f22016-09-08 14:06:08 +0000390 switch (Sec.sh_type) {
391 case SHT_ARM_ATTRIBUTES:
Peter Smith532bc982016-12-14 10:36:12 +0000392 // FIXME: ARM meta-data section. Retain the first attribute section
393 // we see. The eglibc ARM dynamic loaders require the presence of an
394 // attribute section for dlopen to work.
395 // In a full implementation we would merge all attribute sections.
Rafael Espindola895aea62017-05-11 22:02:41 +0000396 if (InX::ARMAttributes == nullptr) {
397 InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
398 return InX::ARMAttributes;
Peter Smith532bc982016-12-14 10:36:12 +0000399 }
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000400 return &InputSection::Discarded;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000401 case SHT_RELA:
402 case SHT_REL: {
George Rimaree6f22c2017-02-14 16:42:38 +0000403 // Find the relocation target section and associate this
404 // section with it. Target can be discarded, for example
405 // if it is a duplicated member of SHT_GROUP section, we
406 // do not create or proccess relocatable sections then.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000407 InputSectionBase *Target = getRelocTarget(Sec);
George Rimaree6f22c2017-02-14 16:42:38 +0000408 if (!Target)
409 return nullptr;
410
Rafael Espindola042a3f22016-09-08 14:06:08 +0000411 // This section contains relocation information.
412 // If -r is given, we do not interpret or apply relocation
413 // but just copy relocation sections to output.
414 if (Config->Relocatable)
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000415 return make<InputSection>(this, &Sec, Name);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000416
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000417 if (Target->FirstRelocation)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000418 fatal(toString(this) +
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000419 ": multiple relocation sections to one section are not supported");
Rui Ueyama92a8d792017-05-01 20:49:09 +0000420
421 // Mergeable sections with relocations are tricky because relocations
422 // need to be taken into account when comparing section contents for
Rui Ueyamac3443132017-05-02 21:16:06 +0000423 // merging. It's not worth supporting such mergeable sections because
Rui Ueyamadfb1e2a2017-05-02 02:58:04 +0000424 // they are rare and it'd complicates the internal design (we usually
425 // have to determine if two sections are mergeable early in the link
426 // process much before applying relocations). We simply handle mergeable
427 // sections with relocations as non-mergeable.
Rui Ueyama92a8d792017-05-01 20:49:09 +0000428 if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
429 Target = toRegularSection(MS);
430 this->Sections[Sec.sh_info] = Target;
Davide Italianod7656382017-04-29 01:24:34 +0000431 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000432
433 size_t NumRelocations;
434 if (Sec.sh_type == SHT_RELA) {
Rui Ueyama37e60a52017-03-30 21:13:00 +0000435 ArrayRef<Elf_Rela> Rels =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000436 check(this->getObj().relas(&Sec), toString(this));
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000437 Target->FirstRelocation = Rels.begin();
438 NumRelocations = Rels.size();
439 Target->AreRelocsRela = true;
440 } else {
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000441 ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000442 Target->FirstRelocation = Rels.begin();
443 NumRelocations = Rels.size();
444 Target->AreRelocsRela = false;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000445 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000446 assert(isUInt<31>(NumRelocations));
447 Target->NumRelocations = NumRelocations;
George Rimar82bd8be2017-02-08 16:18:10 +0000448
449 // Relocation sections processed by the linker are usually removed
450 // from the output, so returning `nullptr` for the normal case.
451 // However, if -emit-relocs is given, we need to leave them in the output.
452 // (Some post link analysis tools need this information.)
George Rimar858a6592017-02-17 19:46:47 +0000453 if (Config->EmitRelocs) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000454 InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
George Rimar858a6592017-02-17 19:46:47 +0000455 // We will not emit relocation section if target was discarded.
456 Target->DependentSections.push_back(RelocSec);
457 return RelocSec;
458 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000459 return nullptr;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000460 }
461 }
462
Rui Ueyama06332732017-02-23 07:06:43 +0000463 // The GNU linker uses .note.GNU-stack section as a marker indicating
464 // that the code in the object file does not expect that the stack is
465 // executable (in terms of NX bit). If all input files have the marker,
466 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
Rui Ueyama65efe352017-02-23 07:15:46 +0000467 // make the stack non-executable. Most object files have this section as
468 // of 2017.
Rui Ueyama06332732017-02-23 07:06:43 +0000469 //
470 // But making the stack non-executable is a norm today for security
Rui Ueyama65efe352017-02-23 07:15:46 +0000471 // reasons. Failure to do so may result in a serious security issue.
472 // Therefore, we make LLD always add PT_GNU_STACK unless it is
Rui Ueyama06332732017-02-23 07:06:43 +0000473 // explicitly told to do otherwise (by -z execstack). Because the stack
474 // executable-ness is controlled solely by command line options,
475 // .note.GNU-stack sections are simply ignored.
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000476 if (Name == ".note.GNU-stack")
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000477 return &InputSection::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000478
Rui Ueyamac1a0ac22017-02-23 07:35:11 +0000479 // Split stacks is a feature to support a discontiguous stack. At least
480 // as of 2017, it seems that the feature is not being used widely.
481 // Only GNU gold supports that. We don't. For the details about that,
482 // see https://gcc.gnu.org/wiki/SplitStacks
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000483 if (Name == ".note.GNU-split-stack") {
Rui Ueyamab7f39b02017-02-23 07:35:30 +0000484 error(toString(this) +
485 ": object file compiled with -fsplit-stack is not supported");
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000486 return &InputSection::Discarded;
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000487 }
488
George Rimarf21aade2016-08-31 08:38:11 +0000489 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000490 return &InputSection::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000491
Rui Ueyamae2f11692017-04-28 22:46:55 +0000492 // If -gdb-index is given, LLD creates .gdb_index section, and that
493 // section serves the same purpose as .debug_gnu_pub{names,types} sections.
494 // If that's the case, we want to eliminate .debug_gnu_pub{names,types}
495 // because they are redundant and can waste large amount of disk space
496 // (for example, they are about 400 MiB in total for a clang debug build.)
Rafael Espindola8b1afd52017-07-19 22:27:35 +0000497 // We still create the section and mark it dead so that the gdb index code
498 // can use the InputSection to access the data.
Rui Ueyamae2f11692017-04-28 22:46:55 +0000499 if (Config->GdbIndex &&
Rafael Espindola8b1afd52017-07-19 22:27:35 +0000500 (Name == ".debug_gnu_pubnames" || Name == ".debug_gnu_pubtypes")) {
501 auto *Ret = make<InputSection>(this, &Sec, Name);
502 Script->discard({Ret});
503 return Ret;
504 }
Rui Ueyamae2f11692017-04-28 22:46:55 +0000505
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000506 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
507 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
508 // sections. Drop those sections to avoid duplicate symbol errors.
509 // FIXME: This is glibc PR20543, we should remove this hack once that has been
510 // fixed for a while.
511 if (Name.startswith(".gnu.linkonce."))
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000512 return &InputSection::Discarded;
Peter Collingbournec39e5d62017-01-09 20:26:33 +0000513
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000514 // The linker merges EH (exception handling) frames and creates a
515 // .eh_frame_hdr section for runtime. So we handle them with a special
516 // class. For relocatable outputs, they are just passed through.
517 if (Name == ".eh_frame" && !Config->Relocatable)
Rafael Espindola5c02b742017-03-06 21:17:18 +0000518 return make<EhInputSection>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000519
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000520 if (shouldMerge(Sec))
Rafael Espindola6119b862017-03-06 20:23:56 +0000521 return make<MergeInputSection>(this, &Sec, Name);
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000522 return make<InputSection>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000523}
524
Rui Ueyamaf9f69542017-06-12 18:46:33 +0000525template <class ELFT>
526StringRef elf::ObjectFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
527 return check(this->getObj().getSectionName(&Sec, SectionStringTable),
528 toString(this));
529}
530
Rafael Espindola73c3a362016-11-08 15:51:00 +0000531template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rui Ueyama27119402016-11-03 18:20:08 +0000532 SymbolBodies.reserve(this->Symbols.size());
533 for (const Elf_Sym &Sym : this->Symbols)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000534 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000535}
536
537template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000538InputSectionBase *elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000539 uint32_t Index = this->getSectionIndex(Sym);
George Rimar3f7c3df2017-03-21 08:44:25 +0000540 if (Index >= this->Sections.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000541 fatal(toString(this) + ": invalid section index: " + Twine(Index));
George Rimar3f7c3df2017-03-21 08:44:25 +0000542 InputSectionBase *S = this->Sections[Index];
George Rimar24adce92016-10-06 09:17:55 +0000543
George Rimar308752e2016-11-15 07:56:28 +0000544 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
545 // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
George Rimar683a35d2016-08-12 19:25:54 +0000546 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
George Rimar308752e2016-11-15 07:56:28 +0000547 // In this case it is fine for section to be null here as we do not
548 // allocate sections of these types.
George Rimar24adce92016-10-06 09:17:55 +0000549 if (!S) {
George Rimar308752e2016-11-15 07:56:28 +0000550 if (Index == 0 || Sym.getType() == STT_SECTION ||
551 Sym.getType() == STT_NOTYPE)
George Rimar24adce92016-10-06 09:17:55 +0000552 return nullptr;
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000553 fatal(toString(this) + ": invalid section index: " + Twine(Index));
George Rimar24adce92016-10-06 09:17:55 +0000554 }
555
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000556 if (S == &InputSection::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000557 return S;
558 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000559}
560
561template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000562SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000563 int Binding = Sym->getBinding();
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000564 InputSectionBase *Sec = getSection(*Sym);
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000565
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000566 uint8_t StOther = Sym->st_other;
567 uint8_t Type = Sym->getType();
Rui Ueyama9cc84382017-02-24 19:52:52 +0000568 uint64_t Value = Sym->st_value;
569 uint64_t Size = Sym->st_size;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000570
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000571 if (Binding == STB_LOCAL) {
Eugene Leviantb380b242016-10-26 11:07:09 +0000572 if (Sym->getType() == STT_FILE)
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000573 SourceFile = check(Sym->getName(this->StringTable), toString(this));
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000574
575 if (this->StringTable.size() <= Sym->st_name)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000576 fatal(toString(this) + ": invalid symbol name offset");
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000577
Rui Ueyama84e65a72016-11-29 19:11:39 +0000578 StringRefZ Name = this->StringTable.data() + Sym->st_name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000579 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama175e81c2017-02-28 19:36:30 +0000580 return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type, this);
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000581
Rui Ueyama175e81c2017-02-28 19:36:30 +0000582 return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
583 Size, Sec, this);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000584 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000585
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000586 StringRef Name = check(Sym->getName(this->StringTable), toString(this));
Rafael Espindola20348222015-08-24 21:43:25 +0000587
Rafael Espindola4cda5812015-10-16 15:29:48 +0000588 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000589 case SHN_UNDEF:
Rui Ueyama23f64d22017-07-26 21:24:01 +0000590 return Symtab
Rafael Espindola244ef982017-07-26 18:42:48 +0000591 ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
592 /*CanOmitFromDynSym=*/false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000593 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000594 case SHN_COMMON:
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000595 if (Value == 0 || Value >= UINT32_MAX)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000596 fatal(toString(this) + ": common symbol '" + Name +
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000597 "' has invalid alignment: " + Twine(Value));
Rui Ueyama23f64d22017-07-26 21:24:01 +0000598 return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000599 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000600 }
Rafael Espindola20348222015-08-24 21:43:25 +0000601
Rafael Espindola67d72c02016-03-11 12:06:30 +0000602 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000603 default:
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000604 fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000605 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000606 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000607 case STB_GNU_UNIQUE:
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000608 if (Sec == &InputSection::Discarded)
Rui Ueyama23f64d22017-07-26 21:24:01 +0000609 return Symtab
Rafael Espindola244ef982017-07-26 18:42:48 +0000610 ->addUndefined<ELFT>(Name, /*IsLocal=*/false, Binding, StOther, Type,
611 /*CanOmitFromDynSym=*/false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000612 ->body();
Rui Ueyama23f64d22017-07-26 21:24:01 +0000613 return Symtab
Rafael Espindola244ef982017-07-26 18:42:48 +0000614 ->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding, Sec, this)
Rui Ueyama0cbf7492016-11-23 06:59:47 +0000615 ->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000616 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000617}
618
Rui Ueyamafd7deda2017-05-03 21:03:08 +0000619ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
620 : InputFile(ArchiveKind, File->getMemoryBufferRef()),
621 File(std::move(File)) {}
622
Peter Collingbourne4f952702016-05-01 04:55:03 +0000623template <class ELFT> void ArchiveFile::parse() {
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000624 Symbols.reserve(File->getNumberOfSymbols());
Rui Ueyamafd7deda2017-05-03 21:03:08 +0000625 for (const Archive::Symbol &Sym : File->symbols())
Rafael Espindola244ef982017-07-26 18:42:48 +0000626 Symbols.push_back(Symtab->addLazyArchive<ELFT>(this, Sym));
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000627}
628
629// Returns a buffer pointing to a member file containing a given symbol.
Davide Italianobcdd6c62016-10-12 19:35:54 +0000630std::pair<MemoryBufferRef, uint64_t>
631ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000632 Archive::Child C =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000633 check(Sym->getMember(), toString(this) +
Rui Ueyama37e60a52017-03-30 21:13:00 +0000634 ": could not get the member for symbol " +
635 Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000636
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000637 if (!Seen.insert(C.getChildOffset()).second)
Davide Italianobcdd6c62016-10-12 19:35:54 +0000638 return {MemoryBufferRef(), 0};
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000639
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000640 MemoryBufferRef Ret =
641 check(C.getMemoryBufferRef(),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000642 toString(this) +
Rui Ueyama37e60a52017-03-30 21:13:00 +0000643 ": could not get the buffer for the member defining symbol " +
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000644 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000645
Rui Ueyamaec1c75e2017-01-09 01:42:02 +0000646 if (C.getParent()->isThin() && Tar)
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000647 Tar->append(relativeToRoot(check(C.getFullName(), toString(this))),
Rui Ueyama37e60a52017-03-30 21:13:00 +0000648 Ret.getBuffer());
Davide Italianobcdd6c62016-10-12 19:35:54 +0000649 if (C.getParent()->isThin())
650 return {Ret, 0};
651 return {Ret, C.getChildOffset()};
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000652}
653
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000654template <class ELFT>
Rafael Espindola3460cdd2017-04-24 21:44:20 +0000655SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
656 : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName),
657 AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000658
Rafael Espindola115f0f32015-11-03 14:13:40 +0000659template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000660const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000661SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000662 return check(
Rui Ueyama37e60a52017-03-30 21:13:00 +0000663 this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000664 toString(this));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000665}
666
Rui Ueyama7c713312016-01-06 01:56:36 +0000667// Partially parse the shared object file so that we can call
668// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000669template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rafael Espindolac8b15812015-10-01 15:47:50 +0000670 const Elf_Shdr *DynamicSec = nullptr;
Rafael Espindolae19abab2016-11-03 20:44:50 +0000671 const ELFFile<ELFT> Obj = this->getObj();
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000672 ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
Rui Ueyama3233d3e2017-04-13 00:23:32 +0000673
674 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
Rafael Espindola84d6a172016-11-03 12:21:00 +0000675 for (const Elf_Shdr &Sec : Sections) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000676 switch (Sec.sh_type) {
677 default:
678 continue;
679 case SHT_DYNSYM:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000680 this->initSymtab(Sections, &Sec);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000681 break;
682 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000683 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000684 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000685 case SHT_SYMTAB_SHNDX:
Rui Ueyama37e60a52017-03-30 21:13:00 +0000686 this->SymtabSHNDX =
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000687 check(Obj.getSHNDXTable(Sec, Sections), toString(this));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000688 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000689 case SHT_GNU_versym:
690 this->VersymSec = &Sec;
691 break;
692 case SHT_GNU_verdef:
693 this->VerdefSec = &Sec;
694 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000695 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000696 }
697
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000698 if (this->VersymSec && this->Symbols.empty())
George Rimarbcba39a2016-11-02 10:16:25 +0000699 error("SHT_GNU_versym should be associated with symbol table");
700
Rui Ueyama3233d3e2017-04-13 00:23:32 +0000701 // Search for a DT_SONAME tag to initialize this->SoName.
Rui Ueyama361d8b92015-10-12 15:49:02 +0000702 if (!DynamicSec)
703 return;
George Rimar53cf2a82016-10-07 09:01:04 +0000704 ArrayRef<Elf_Dyn> Arr =
705 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000706 toString(this));
George Rimar53cf2a82016-10-07 09:01:04 +0000707 for (const Elf_Dyn &Dyn : Arr) {
Rui Ueyama361d8b92015-10-12 15:49:02 +0000708 if (Dyn.d_tag == DT_SONAME) {
Rui Ueyama9cc84382017-02-24 19:52:52 +0000709 uint64_t Val = Dyn.getVal();
Rui Ueyama361d8b92015-10-12 15:49:02 +0000710 if (Val >= this->StringTable.size())
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000711 fatal(toString(this) + ": invalid DT_SONAME entry");
Rui Ueyamae5ad2982017-04-26 23:00:32 +0000712 SoName = this->StringTable.data() + Val;
Rui Ueyama361d8b92015-10-12 15:49:02 +0000713 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000714 }
715 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000716}
Rafael Espindola18173d42015-09-08 15:50:05 +0000717
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000718// Parse the version definitions in the object file if present. Returns a vector
719// whose nth element contains a pointer to the Elf_Verdef for version identifier
720// n. Version identifiers that are not definitions map to nullptr. The array
721// always has at least length 1.
722template <class ELFT>
723std::vector<const typename ELFT::Verdef *>
724SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
725 std::vector<const Elf_Verdef *> Verdefs(1);
726 // We only need to process symbol versions for this DSO if it has both a
727 // versym and a verdef section, which indicates that the DSO contains symbol
728 // version definitions.
729 if (!VersymSec || !VerdefSec)
730 return Verdefs;
731
732 // The location of the first global versym entry.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000733 const char *Base = this->MB.getBuffer().data();
734 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000735 this->FirstNonLocal;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000736
737 // We cannot determine the largest verdef identifier without inspecting
738 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
739 // sequentially starting from 1, so we predict that the largest identifier
740 // will be VerdefCount.
741 unsigned VerdefCount = VerdefSec->sh_info;
742 Verdefs.resize(VerdefCount + 1);
743
744 // Build the Verdefs array by following the chain of Elf_Verdef objects
745 // from the start of the .gnu.version_d section.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000746 const char *Verdef = Base + VerdefSec->sh_offset;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000747 for (unsigned I = 0; I != VerdefCount; ++I) {
748 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
749 Verdef += CurVerdef->vd_next;
750 unsigned VerdefIndex = CurVerdef->vd_ndx;
751 if (Verdefs.size() <= VerdefIndex)
752 Verdefs.resize(VerdefIndex + 1);
753 Verdefs[VerdefIndex] = CurVerdef;
754 }
755
756 return Verdefs;
757}
758
Rui Ueyama7c713312016-01-06 01:56:36 +0000759// Fully parse the shared object file. This must be called after parseSoName().
760template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000761 // Create mapping from version identifiers to Elf_Verdef entries.
762 const Elf_Versym *Versym = nullptr;
763 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
764
Rafael Espindola8e232572016-11-03 20:48:57 +0000765 Elf_Sym_Range Syms = this->getGlobalSymbols();
Rafael Espindola18173d42015-09-08 15:50:05 +0000766 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000767 unsigned VersymIndex = 0;
768 if (Versym) {
769 VersymIndex = Versym->vs_index;
770 ++Versym;
771 }
Rafael Espindola2756e042017-01-06 22:30:35 +0000772 bool Hidden = VersymIndex & VERSYM_HIDDEN;
773 VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000774
Rui Ueyama4e4e8662017-04-03 19:11:23 +0000775 StringRef Name = check(Sym.getName(this->StringTable), toString(this));
Rafael Espindola18da0e52016-04-29 16:23:31 +0000776 if (Sym.isUndefined()) {
777 Undefs.push_back(Name);
778 continue;
779 }
780
Rafael Espindola2756e042017-01-06 22:30:35 +0000781 // Ignore local symbols.
782 if (Versym && VersymIndex == VER_NDX_LOCAL)
783 continue;
Rafael Espindolad2454d62016-06-09 15:45:49 +0000784
785 const Elf_Verdef *V =
786 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
Rafael Espindola2756e042017-01-06 22:30:35 +0000787
788 if (!Hidden)
Rui Ueyama23f64d22017-07-26 21:24:01 +0000789 Symtab->addShared(this, Name, Sym, V);
Rafael Espindola2756e042017-01-06 22:30:35 +0000790
791 // Also add the symbol with the versioned name to handle undefined symbols
792 // with explicit versions.
793 if (V) {
794 StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
Rui Ueyama63d48e52017-04-27 04:01:14 +0000795 Name = Saver.save(Name + "@" + VerName);
Rui Ueyama23f64d22017-07-26 21:24:01 +0000796 Symtab->addShared(this, Name, Sym, V);
Rafael Espindola2756e042017-01-06 22:30:35 +0000797 }
Rafael Espindola18173d42015-09-08 15:50:05 +0000798 }
799}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000800
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000801static ELFKind getBitcodeELFKind(const Triple &T) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000802 if (T.isLittleEndian())
803 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
804 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000805}
806
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000807static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000808 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000809 case Triple::aarch64:
810 return EM_AARCH64;
811 case Triple::arm:
Sean Silva1d961852017-02-28 03:00:48 +0000812 case Triple::thumb:
Rui Ueyama523744d2016-07-07 02:46:30 +0000813 return EM_ARM;
Leslie Zhai49ba7952017-06-15 02:27:50 +0000814 case Triple::avr:
815 return EM_AVR;
Rui Ueyama523744d2016-07-07 02:46:30 +0000816 case Triple::mips:
817 case Triple::mipsel:
818 case Triple::mips64:
819 case Triple::mips64el:
820 return EM_MIPS;
821 case Triple::ppc:
822 return EM_PPC;
823 case Triple::ppc64:
824 return EM_PPC64;
825 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000826 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000827 case Triple::x86_64:
828 return EM_X86_64;
829 default:
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000830 fatal(Path + ": could not infer e_machine from bitcode target triple " +
831 T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000832 }
833}
834
Rafael Espindola244ef982017-07-26 18:42:48 +0000835std::vector<BitcodeFile *> BitcodeFile::Instances;
836
Peter Collingbourne8446f1f2017-04-14 02:55:06 +0000837BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
838 uint64_t OffsetInArchive)
839 : InputFile(BitcodeKind, MB) {
840 this->ArchiveName = ArchiveName;
841
842 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
843 // (the fully resolved path of the archive) + member name + offset of the
844 // member in the archive.
845 // ThinLTO uses the MemoryBufferRef identifier to access its internal
846 // data structures and if two archives define two members with the same name,
847 // this causes a collision which result in only one of the objects being
848 // taken into consideration at LTO time (which very likely causes undefined
849 // symbols later in the link stage).
850 MemoryBufferRef MBRef(MB.getBuffer(),
851 Saver.save(ArchiveName + MB.getBufferIdentifier() +
852 utostr(OffsetInArchive)));
853 Obj = check(lto::InputFile::create(MBRef), toString(this));
854
855 Triple T(Obj->getTargetTriple());
856 EKind = getBitcodeELFKind(T);
857 EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
Davide Italiano60976ba2016-06-29 06:12:39 +0000858}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000859
Davide Italiano786d8e32016-09-29 00:40:08 +0000860static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
861 switch (GvVisibility) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000862 case GlobalValue::DefaultVisibility:
863 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000864 case GlobalValue::HiddenVisibility:
865 return STV_HIDDEN;
866 case GlobalValue::ProtectedVisibility:
867 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000868 }
George Rimar777f9632016-03-12 08:31:34 +0000869 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000870}
871
Peter Collingbourne4f952702016-05-01 04:55:03 +0000872template <class ELFT>
Rafael Espindola3db70212016-10-25 12:02:31 +0000873static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
Davide Italiano786d8e32016-09-29 00:40:08 +0000874 const lto::InputFile::Symbol &ObjSym,
Rui Ueyama55518e72016-10-28 20:57:25 +0000875 BitcodeFile *F) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000876 StringRef NameRef = Saver.save(ObjSym.getName());
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000877 uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000878
Davide Italiano786d8e32016-09-29 00:40:08 +0000879 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
880 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
881 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000882
Peter Collingbourne7b30f162017-03-31 04:47:07 +0000883 int C = ObjSym.getComdatIndex();
Rafael Espindola3db70212016-10-25 12:02:31 +0000884 if (C != -1 && !KeptComdats[C])
Rafael Espindola244ef982017-07-26 18:42:48 +0000885 return Symtab->addUndefined<ELFT>(NameRef, /*IsLocal=*/false, Binding,
886 Visibility, Type, CanOmitFromDynSym, F);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000887
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000888 if (ObjSym.isUndefined())
Rafael Espindola244ef982017-07-26 18:42:48 +0000889 return Symtab->addUndefined<ELFT>(NameRef, /*IsLocal=*/false, Binding,
890 Visibility, Type, CanOmitFromDynSym, F);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000891
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000892 if (ObjSym.isCommon())
Rafael Espindola244ef982017-07-26 18:42:48 +0000893 return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
894 ObjSym.getCommonAlignment(), Binding, Visibility,
895 STT_OBJECT, F);
Davide Italiano786d8e32016-09-29 00:40:08 +0000896
Rafael Espindola244ef982017-07-26 18:42:48 +0000897 return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
898 CanOmitFromDynSym, F);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000899}
900
Peter Collingbourne4f952702016-05-01 04:55:03 +0000901template <class ELFT>
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000902void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Rafael Espindola3db70212016-10-25 12:02:31 +0000903 std::vector<bool> KeptComdats;
Peter Collingbourne7b30f162017-03-31 04:47:07 +0000904 for (StringRef S : Obj->getComdatTable())
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000905 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
Rafael Espindola3db70212016-10-25 12:02:31 +0000906
Davide Italiano786d8e32016-09-29 00:40:08 +0000907 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
Rui Ueyama55518e72016-10-28 20:57:25 +0000908 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000909}
910
Rui Ueyama330e52b2017-04-26 22:51:51 +0000911static ELFKind getELFKind(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000912 unsigned char Size;
913 unsigned char Endian;
914 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
Rui Ueyama330e52b2017-04-26 22:51:51 +0000915
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000916 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000917 fatal(MB.getBufferIdentifier() + ": invalid data encoding");
Rui Ueyama330e52b2017-04-26 22:51:51 +0000918 if (Size != ELFCLASS32 && Size != ELFCLASS64)
919 fatal(MB.getBufferIdentifier() + ": invalid file class");
Rui Ueyamac4b65062015-10-12 15:31:09 +0000920
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000921 size_t BufSize = MB.getBuffer().size();
922 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
923 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
Eugene Leviantc3a44b22016-11-23 10:07:46 +0000924 fatal(MB.getBufferIdentifier() + ": file is too short");
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000925
Rui Ueyama330e52b2017-04-26 22:51:51 +0000926 if (Size == ELFCLASS32)
927 return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
928 return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000929}
930
Rafael Espindola244ef982017-07-26 18:42:48 +0000931std::vector<BinaryFile *> BinaryFile::Instances;
932
Rafael Espindola093abab2016-10-27 17:45:40 +0000933template <class ELFT> void BinaryFile::parse() {
Rui Ueyamac9d82b92017-04-27 04:01:36 +0000934 ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000935 auto *Section =
936 make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
Rafael Espindola093abab2016-10-27 17:45:40 +0000937 Sections.push_back(Section);
938
Rui Ueyamac9d82b92017-04-27 04:01:36 +0000939 // For each input file foo that is embedded to a result as a binary
940 // blob, we define _binary_foo_{start,end,size} symbols, so that
941 // user programs can access blobs by name. Non-alphanumeric
942 // characters in a filename are replaced with underscore.
943 std::string S = "_binary_" + MB.getBufferIdentifier().str();
944 for (size_t I = 0; I < S.size(); ++I)
945 if (!isalnum(S[I]))
946 S[I] = '_';
947
Rui Ueyama23f64d22017-07-26 21:24:01 +0000948 Symtab->addRegular<ELFT>(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT,
949 0, 0, STB_GLOBAL, Section, nullptr);
950 Symtab->addRegular<ELFT>(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT,
951 Data.size(), 0, STB_GLOBAL, Section, nullptr);
952 Symtab->addRegular<ELFT>(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT,
953 Data.size(), 0, STB_GLOBAL, nullptr, nullptr);
Michael J. Spencera9424f32016-09-09 22:08:04 +0000954}
955
Rui Ueyama4655ea32016-04-08 00:14:55 +0000956static bool isBitcode(MemoryBufferRef MB) {
957 using namespace sys::fs;
958 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
959}
960
Rui Ueyama55518e72016-10-28 20:57:25 +0000961InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
Davide Italianobcdd6c62016-10-12 19:35:54 +0000962 uint64_t OffsetInArchive) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000963 if (isBitcode(MB))
964 return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
965
966 switch (getELFKind(MB)) {
967 case ELF32LEKind:
968 return make<ObjectFile<ELF32LE>>(MB, ArchiveName);
969 case ELF32BEKind:
970 return make<ObjectFile<ELF32BE>>(MB, ArchiveName);
971 case ELF64LEKind:
972 return make<ObjectFile<ELF64LE>>(MB, ArchiveName);
973 case ELF64BEKind:
974 return make<ObjectFile<ELF64BE>>(MB, ArchiveName);
975 default:
976 llvm_unreachable("getELFKind");
977 }
Rui Ueyama533c0302016-01-06 00:09:43 +0000978}
979
Rafael Espindola3460cdd2017-04-24 21:44:20 +0000980InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
Rui Ueyama330e52b2017-04-26 22:51:51 +0000981 switch (getELFKind(MB)) {
982 case ELF32LEKind:
983 return make<SharedFile<ELF32LE>>(MB, DefaultSoName);
984 case ELF32BEKind:
985 return make<SharedFile<ELF32BE>>(MB, DefaultSoName);
986 case ELF64LEKind:
987 return make<SharedFile<ELF64LE>>(MB, DefaultSoName);
988 case ELF64BEKind:
989 return make<SharedFile<ELF64BE>>(MB, DefaultSoName);
990 default:
991 llvm_unreachable("getELFKind");
992 }
Rui Ueyama533c0302016-01-06 00:09:43 +0000993}
994
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000995MemoryBufferRef LazyObjectFile::getBuffer() {
996 if (Seen)
997 return MemoryBufferRef();
998 Seen = true;
999 return MB;
1000}
1001
Rafael Espindola808f2d32017-05-04 14:54:48 +00001002InputFile *LazyObjectFile::fetch() {
1003 MemoryBufferRef MBRef = getBuffer();
1004 if (MBRef.getBuffer().empty())
1005 return nullptr;
Rafael Espindola0f6cc652017-05-05 15:17:07 +00001006 return createObjectFile(MBRef, ArchiveName, OffsetInArchive);
Rafael Espindola808f2d32017-05-04 14:54:48 +00001007}
1008
George Rimar10874f72016-10-03 11:13:55 +00001009template <class ELFT> void LazyObjectFile::parse() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001010 for (StringRef Sym : getSymbols())
Rafael Espindola244ef982017-07-26 18:42:48 +00001011 Symtab->addLazyObject<ELFT>(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001012}
1013
1014template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
1015 typedef typename ELFT::Shdr Elf_Shdr;
1016 typedef typename ELFT::Sym Elf_Sym;
1017 typedef typename ELFT::SymRange Elf_Sym_Range;
1018
Rafael Espindola22e9a8e2016-11-03 20:17:25 +00001019 const ELFFile<ELFT> Obj(this->MB.getBuffer());
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001020 ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
Rafael Espindola6d18d382016-11-03 13:24:29 +00001021 for (const Elf_Shdr &Sec : Sections) {
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001022 if (Sec.sh_type != SHT_SYMTAB)
1023 continue;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001024
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001025 Elf_Sym_Range Syms = check(Obj.symbols(&Sec), toString(this));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001026 uint32_t FirstNonLocal = Sec.sh_info;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001027 StringRef StringTable =
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001028 check(Obj.getStringTableForSymtab(Sec, Sections), toString(this));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001029 std::vector<StringRef> V;
Rui Ueyama37e60a52017-03-30 21:13:00 +00001030
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001031 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +00001032 if (Sym.st_shndx != SHN_UNDEF)
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001033 V.push_back(check(Sym.getName(StringTable), toString(this)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001034 return V;
1035 }
1036 return {};
1037}
1038
1039std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
Rui Ueyama37e60a52017-03-30 21:13:00 +00001040 std::unique_ptr<lto::InputFile> Obj =
Rui Ueyama4e4e8662017-04-03 19:11:23 +00001041 check(lto::InputFile::create(this->MB), toString(this));
Rui Ueyamad72dd1f2016-09-29 00:58:10 +00001042 std::vector<StringRef> V;
1043 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
Peter Collingbourne0d56b952017-03-28 22:31:35 +00001044 if (!Sym.isUndefined())
Rui Ueyamad72dd1f2016-09-29 00:58:10 +00001045 V.push_back(Saver.save(Sym.getName()));
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001046 return V;
1047}
1048
Rui Ueyama1f492892016-04-08 20:49:31 +00001049// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001050std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +00001051 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001052 return getBitcodeSymbols();
1053
Rui Ueyama330e52b2017-04-26 22:51:51 +00001054 switch (getELFKind(this->MB)) {
1055 case ELF32LEKind:
1056 return getElfSymbols<ELF32LE>();
1057 case ELF32BEKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001058 return getElfSymbols<ELF32BE>();
Rui Ueyama330e52b2017-04-26 22:51:51 +00001059 case ELF64LEKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001060 return getElfSymbols<ELF64LE>();
Rui Ueyama330e52b2017-04-26 22:51:51 +00001061 case ELF64BEKind:
1062 return getElfSymbols<ELF64BE>();
1063 default:
1064 llvm_unreachable("getELFKind");
1065 }
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001066}
1067
Peter Collingbourne4f952702016-05-01 04:55:03 +00001068template void ArchiveFile::parse<ELF32LE>();
1069template void ArchiveFile::parse<ELF32BE>();
1070template void ArchiveFile::parse<ELF64LE>();
1071template void ArchiveFile::parse<ELF64BE>();
1072
Rafael Espindola1c2baad2017-05-25 21:53:02 +00001073template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1074template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1075template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1076template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +00001077
1078template void LazyObjectFile::parse<ELF32LE>();
1079template void LazyObjectFile::parse<ELF32BE>();
1080template void LazyObjectFile::parse<ELF64LE>();
1081template void LazyObjectFile::parse<ELF64BE>();
1082
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001083template class elf::ELFFileBase<ELF32LE>;
1084template class elf::ELFFileBase<ELF32BE>;
1085template class elf::ELFFileBase<ELF64LE>;
1086template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +00001087
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001088template class elf::ObjectFile<ELF32LE>;
1089template class elf::ObjectFile<ELF32BE>;
1090template class elf::ObjectFile<ELF64LE>;
1091template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +00001092
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001093template class elf::SharedFile<ELF32LE>;
1094template class elf::SharedFile<ELF32BE>;
1095template class elf::SharedFile<ELF64LE>;
1096template class elf::SharedFile<ELF64BE>;
Michael J. Spencera9424f32016-09-09 22:08:04 +00001097
Rafael Espindola093abab2016-10-27 17:45:40 +00001098template void BinaryFile::parse<ELF32LE>();
1099template void BinaryFile::parse<ELF32BE>();
1100template void BinaryFile::parse<ELF64LE>();
1101template void BinaryFile::parse<ELF64BE>();