blob: 7d58ee942f08fbcf0d72991450e9c7e147815dc8 [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"
Rui Ueyama55518e72016-10-28 20:57:25 +000015#include "Memory.h"
Peter Collingbourne4f952702016-05-01 04:55:03 +000016#include "SymbolTable.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000017#include "Symbols.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018#include "llvm/ADT/STLExtras.h"
Teresa Johnson1e390892016-11-11 05:35:22 +000019#include "llvm/Bitcode/BitcodeReader.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000020#include "llvm/CodeGen/Analysis.h"
Eugene Leviantb380b242016-10-26 11:07:09 +000021#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000022#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000023#include "llvm/IR/Module.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000024#include "llvm/LTO/LTO.h"
Michael J. Spencera9424f32016-09-09 22:08:04 +000025#include "llvm/MC/StringTableBuilder.h"
Eugene Leviantc4681202016-11-01 09:17:50 +000026#include "llvm/Object/ELFObjectFile.h"
Davide Italianoe02ba982016-09-08 21:18:38 +000027#include "llvm/Support/Path.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
Eugene Leviantc4681202016-11-01 09:17:50 +000038namespace {
39// In ELF object file all section addresses are zero. If we have multiple
40// .text sections (when using -ffunction-section or comdat group) then
41// LLVM DWARF parser will not be able to parse .debug_line correctly, unless
42// we assign each section some unique address. This callback method assigns
43// each section an address equal to its offset in ELF object file.
44class ObjectInfo : public LoadedObjectInfo {
45public:
46 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override {
47 return static_cast<const ELFSectionRef &>(Sec).getOffset();
48 }
49 std::unique_ptr<LoadedObjectInfo> clone() const override {
50 return std::unique_ptr<LoadedObjectInfo>();
51 }
52};
53}
54
Rui Ueyama7463ada2016-11-02 19:51:41 +000055template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() {
Rui Ueyama7556f6b2016-11-02 18:42:13 +000056 std::unique_ptr<object::ObjectFile> Obj =
57 check(object::ObjectFile::createObjectFile(this->MB),
58 "createObjectFile failed");
Eugene Leviantb380b242016-10-26 11:07:09 +000059
Eugene Leviantc4681202016-11-01 09:17:50 +000060 ObjectInfo ObjInfo;
61 DWARFContextInMemory Dwarf(*Obj.get(), &ObjInfo);
Eugene Leviantb380b242016-10-26 11:07:09 +000062 DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
63 DataExtractor LineData(Dwarf.getLineSection().Data,
64 ELFT::TargetEndianness == support::little,
65 ELFT::Is64Bits ? 8 : 4);
Rui Ueyama7556f6b2016-11-02 18:42:13 +000066
Eugene Leviantb380b242016-10-26 11:07:09 +000067 // The second parameter is offset in .debug_line section
68 // for compilation unit (CU) of interest. We have only one
69 // CU (object file), so offset is always 0.
70 DwarfLine->getOrParseLineTable(LineData, 0);
71}
72
Rui Ueyama7556f6b2016-11-02 18:42:13 +000073// Returns source line information for a given offset
74// using DWARF debug info.
Eugene Leviantc4681202016-11-01 09:17:50 +000075template <class ELFT>
Rui Ueyama7463ada2016-11-02 19:51:41 +000076std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase<ELFT> *S,
77 uintX_t Offset) {
Eugene Leviantb380b242016-10-26 11:07:09 +000078 if (!DwarfLine)
Rui Ueyama7556f6b2016-11-02 18:42:13 +000079 initializeDwarfLine();
Eugene Leviantb380b242016-10-26 11:07:09 +000080
Rui Ueyama7556f6b2016-11-02 18:42:13 +000081 // The offset to CU is 0.
82 const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
83 if (!Tbl)
Eugene Leviantb380b242016-10-26 11:07:09 +000084 return "";
Eugene Leviantc4681202016-11-01 09:17:50 +000085
86 // Use fake address calcuated by adding section file offset and offset in
Rui Ueyama7556f6b2016-11-02 18:42:13 +000087 // section. See comments for ObjectInfo class.
88 DILineInfo Info;
89 DILineInfoSpecifier Spec;
90 Tbl->getFileLineInfoForAddress(S->Offset + Offset, nullptr, Spec.FLIKind,
91 Info);
92 if (Info.Line == 0)
93 return "";
Rui Ueyamae8785ba2016-11-21 13:49:57 +000094 return Info.FileName + ":" + std::to_string(Info.Line);
Eugene Leviantb380b242016-10-26 11:07:09 +000095}
96
Rafael Espindola78db5a92016-05-09 21:40:06 +000097// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Rui Ueyama429ef2a2016-07-15 20:38:28 +000098std::string elf::getFilename(const InputFile *F) {
Rafael Espindola78db5a92016-05-09 21:40:06 +000099 if (!F)
100 return "(internal)";
101 if (!F->ArchiveName.empty())
102 return (F->ArchiveName + "(" + F->getName() + ")").str();
103 return F->getName();
104}
105
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000106template <class ELFT> static ELFKind getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000107 if (ELFT::TargetEndianness == support::little)
108 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
109 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +0000110}
111
112template <class ELFT>
Rafael Espindolae19abab2016-11-03 20:44:50 +0000113ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000114 EKind = getELFKind<ELFT>();
Rafael Espindolae19abab2016-11-03 20:44:50 +0000115 EMachine = getObj().getHeader()->e_machine;
116 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000117}
118
119template <class ELFT>
Rafael Espindola8e232572016-11-03 20:48:57 +0000120typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() {
George Rimar0b2d3742016-11-14 10:05:53 +0000121 return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end());
Davide Italiano6d328d32015-09-16 20:45:57 +0000122}
123
Rafael Espindola115f0f32015-11-03 14:13:40 +0000124template <class ELFT>
125uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000126 return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000127}
128
Rafael Espindola6d18d382016-11-03 13:24:29 +0000129template <class ELFT>
Rafael Espindola21d8be92016-11-03 15:43:47 +0000130void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
131 const Elf_Shdr *Symtab) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000132 FirstNonLocal = Symtab->sh_info;
Rafael Espindolae19abab2016-11-03 20:44:50 +0000133 Symbols = check(getObj().symbols(Symtab));
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000134 if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
135 fatal(getFilename(this) + ": invalid sh_info in symbol table");
136
Rafael Espindolae19abab2016-11-03 20:44:50 +0000137 StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000138}
139
140template <class ELFT>
Rui Ueyama55518e72016-10-28 20:57:25 +0000141elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
142 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000143
144template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +0000145ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000146 return makeArrayRef(this->SymbolBodies).slice(this->FirstNonLocal);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000147}
148
149template <class ELFT>
150ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000151 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000152 return this->SymbolBodies;
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000153 return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000154}
155
156template <class ELFT>
157ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000158 if (this->SymbolBodies.empty())
Rafael Espindola67d72c02016-03-11 12:06:30 +0000159 return this->SymbolBodies;
160 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000161}
162
Rafael Espindola444576d2015-10-09 19:25:07 +0000163template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000164void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000165 // Read section and symbol tables.
Rafael Espindola73c3a362016-11-08 15:51:00 +0000166 initializeSections(ComdatGroups);
167 initializeSymbols();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000168}
169
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000170// Sections with SHT_GROUP and comdat bits define comdat section groups.
171// They are identified and deduplicated by group name. This function
172// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000173template <class ELFT>
Rafael Espindola7c7abaf2016-11-03 02:28:13 +0000174StringRef
175elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
176 const Elf_Shdr &Sec) {
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000177 if (this->Symbols.empty())
Rafael Espindola21d8be92016-11-03 15:43:47 +0000178 this->initSymtab(Sections,
179 check(object::getSection<ELFT>(Sections, Sec.sh_link)));
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000180 const Elf_Sym *Sym =
181 check(object::getSymbol<ELFT>(this->Symbols, Sec.sh_info));
Rafael Espindola21d8be92016-11-03 15:43:47 +0000182 return check(Sym->getName(this->StringTable));
Rafael Espindola444576d2015-10-09 19:25:07 +0000183}
184
185template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000186ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000187elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000188 const ELFFile<ELFT> &Obj = this->getObj();
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000189 ArrayRef<Elf_Word> Entries =
190 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000191 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000192 fatal(getFilename(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000193 return Entries.slice(1);
194}
195
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000196template <class ELFT>
197bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000198 // We don't merge sections if -O0 (default is -O1). This makes sometimes
199 // the linker significantly faster, although the output will be bigger.
200 if (Config->Optimize == 0)
201 return false;
202
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000203 // Do not merge sections if generating a relocatable object. It makes
204 // the code simpler because we do not need to update relocation addends
205 // to reflect changes introduced by merging. Instead of that we write
206 // such "merge" sections into separate OutputSections and keep SHF_MERGE
207 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
208 // later during a final linking.
209 if (Config->Relocatable)
210 return false;
211
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000212 // A mergeable section with size 0 is useless because they don't have
213 // any data to merge. A mergeable string section with size 0 can be
214 // argued as invalid because it doesn't end with a null character.
215 // We'll avoid a mess by handling them as if they were non-mergeable.
216 if (Sec.sh_size == 0)
217 return false;
218
Rui Ueyamac75ef852016-09-21 03:22:18 +0000219 // Check for sh_entsize. The ELF spec is not clear about the zero
220 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
221 // the section does not hold a table of fixed-size entries". We know
222 // that Rust 1.13 produces a string mergeable section with a zero
223 // sh_entsize. Here we just accept it rather than being picky about it.
224 uintX_t EntSize = Sec.sh_entsize;
225 if (EntSize == 0)
226 return false;
227 if (Sec.sh_size % EntSize)
228 fatal(getFilename(this) +
229 ": SHF_MERGE section size must be a multiple of sh_entsize");
230
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000231 uintX_t Flags = Sec.sh_flags;
232 if (!(Flags & SHF_MERGE))
233 return false;
234 if (Flags & SHF_WRITE)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000235 fatal(getFilename(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000236
Peter Smith4df2e142016-05-18 11:40:16 +0000237 // Don't try to merge if the alignment is larger than the sh_entsize and this
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000238 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000239 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000240 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
241 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000242 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000243 if (Flags & SHF_STRINGS)
244 return true;
245
George Rimardcddfb62016-06-08 12:04:59 +0000246 return Sec.sh_addralign <= EntSize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000247}
248
249template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000250void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindola73c3a362016-11-08 15:51:00 +0000251 DenseSet<CachedHashStringRef> &ComdatGroups) {
252 ArrayRef<Elf_Shdr> ObjSections = check(this->getObj().sections());
Rafael Espindolae19abab2016-11-03 20:44:50 +0000253 const ELFFile<ELFT> &Obj = this->getObj();
Rafael Espindola235d82c2016-11-02 14:42:20 +0000254 uint64_t Size = ObjSections.size();
Rafael Espindola71675852015-09-22 00:16:19 +0000255 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000256 unsigned I = -1;
Rafael Espindola199e0042016-11-02 15:21:24 +0000257 StringRef SectionStringTable = check(Obj.getSectionStringTable(ObjSections));
Rafael Espindola235d82c2016-11-02 14:42:20 +0000258 for (const Elf_Shdr &Sec : ObjSections) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000259 ++I;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000260 if (Sections[I] == &InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000261 continue;
262
Rui Ueyamaaf9793d2016-10-04 16:47:49 +0000263 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
264 // if -r is given, we'll let the final link discard such sections.
265 // This is compatible with GNU.
266 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
Eugene Leviant27be5422016-09-28 08:42:02 +0000267 Sections[I] = &InputSection<ELFT>::Discarded;
268 continue;
269 }
270
Rafael Espindolacde25132015-08-13 14:45:44 +0000271 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000272 case SHT_GROUP:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000273 Sections[I] = &InputSection<ELFT>::Discarded;
George Rimar0b2d3742016-11-14 10:05:53 +0000274 if (ComdatGroups.insert(CachedHashStringRef(
275 getShtGroupSignature(ObjSections, Sec)))
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000276 .second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000277 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000278 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000279 if (SecIndex >= Size)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000280 fatal(getFilename(this) + ": invalid section index in group: " +
281 Twine(SecIndex));
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000282 Sections[SecIndex] = &InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000283 }
284 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000285 case SHT_SYMTAB:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000286 this->initSymtab(ObjSections, &Sec);
Rafael Espindolacde25132015-08-13 14:45:44 +0000287 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000288 case SHT_SYMTAB_SHNDX:
Rafael Espindola84d6a172016-11-03 12:21:00 +0000289 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, ObjSections));
Rafael Espindola20348222015-08-24 21:43:25 +0000290 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000291 case SHT_STRTAB:
292 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000293 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000294 default:
Rafael Espindolaec05a572016-11-01 21:48:00 +0000295 Sections[I] = createInputSection(Sec, SectionStringTable);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000296 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000297
Rafael Espindolac17e0b62016-11-02 13:36:31 +0000298 // .ARM.exidx sections have a reverse dependency on the InputSection they
299 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
300 if (Sec.sh_flags & SHF_LINK_ORDER) {
301 if (Sec.sh_link >= Sections.size())
302 fatal(getFilename(this) + ": invalid sh_link index: " +
303 Twine(Sec.sh_link));
304 auto *IS = cast<InputSection<ELFT>>(Sections[Sec.sh_link]);
305 IS->DependentSection = Sections[I];
306 }
Peter Smith07606052016-10-10 10:10:27 +0000307 }
308}
309
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000310template <class ELFT>
311InputSectionBase<ELFT> *
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000312elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
313 uint32_t Idx = Sec.sh_info;
314 if (Idx >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000315 fatal(getFilename(this) + ": invalid relocated section index: " +
316 Twine(Idx));
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000317 InputSectionBase<ELFT> *Target = Sections[Idx];
318
319 // Strictly speaking, a relocation section must be included in the
320 // group of the section it relocates. However, LLVM 3.3 and earlier
321 // would fail to do so, so we gracefully handle that case.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000322 if (Target == &InputSection<ELFT>::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000323 return nullptr;
324
325 if (!Target)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000326 fatal(getFilename(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000327 return Target;
328}
329
330template <class ELFT>
331InputSectionBase<ELFT> *
Rafael Espindolaec05a572016-11-01 21:48:00 +0000332elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
333 StringRef SectionStringTable) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000334 StringRef Name =
335 check(this->getObj().getSectionName(&Sec, SectionStringTable));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000336
Rafael Espindola042a3f22016-09-08 14:06:08 +0000337 switch (Sec.sh_type) {
338 case SHT_ARM_ATTRIBUTES:
339 // FIXME: ARM meta-data section. At present attributes are ignored,
340 // they can be used to reason about object compatibility.
341 return &InputSection<ELFT>::Discarded;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000342 case SHT_RELA:
343 case SHT_REL: {
344 // This section contains relocation information.
345 // If -r is given, we do not interpret or apply relocation
346 // but just copy relocation sections to output.
347 if (Config->Relocatable)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000348 return make<InputSection<ELFT>>(this, &Sec, Name);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000349
350 // Find the relocation target section and associate this
351 // section with it.
352 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
353 if (!Target)
354 return nullptr;
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000355 if (Target->FirstRelocation)
356 fatal(getFilename(this) +
357 ": multiple relocation sections to one section are not supported");
358 if (!isa<InputSection<ELFT>>(Target) && !isa<EhInputSection<ELFT>>(Target))
359 fatal(getFilename(this) +
360 ": relocations pointing to SHF_MERGE are not supported");
361
362 size_t NumRelocations;
363 if (Sec.sh_type == SHT_RELA) {
364 ArrayRef<Elf_Rela> Rels = check(this->getObj().relas(&Sec));
365 Target->FirstRelocation = Rels.begin();
366 NumRelocations = Rels.size();
367 Target->AreRelocsRela = true;
368 } else {
369 ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec));
370 Target->FirstRelocation = Rels.begin();
371 NumRelocations = Rels.size();
372 Target->AreRelocsRela = false;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000373 }
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000374 assert(isUInt<31>(NumRelocations));
375 Target->NumRelocations = NumRelocations;
376 return nullptr;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000377 }
378 }
379
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000380 // .note.GNU-stack is a marker section to control the presence of
381 // PT_GNU_STACK segment in outputs. Since the presence of the segment
382 // is controlled only by the command line option (-z execstack) in LLD,
383 // .note.GNU-stack is ignored.
384 if (Name == ".note.GNU-stack")
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000385 return &InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000386
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000387 if (Name == ".note.GNU-split-stack") {
George Rimar777f9632016-03-12 08:31:34 +0000388 error("objects using splitstacks are not supported");
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000389 return &InputSection<ELFT>::Discarded;
390 }
391
George Rimarf21aade2016-08-31 08:38:11 +0000392 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000393 return &InputSection<ELFT>::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000394
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000395 // The linker merges EH (exception handling) frames and creates a
396 // .eh_frame_hdr section for runtime. So we handle them with a special
397 // class. For relocatable outputs, they are just passed through.
398 if (Name == ".eh_frame" && !Config->Relocatable)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000399 return make<EhInputSection<ELFT>>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000400
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000401 if (shouldMerge(Sec))
Rui Ueyamad52adb32016-11-01 22:53:18 +0000402 return make<MergeInputSection<ELFT>>(this, &Sec, Name);
403 return make<InputSection<ELFT>>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000404}
405
Rafael Espindola73c3a362016-11-08 15:51:00 +0000406template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rui Ueyama27119402016-11-03 18:20:08 +0000407 SymbolBodies.reserve(this->Symbols.size());
408 for (const Elf_Sym &Sym : this->Symbols)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000409 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000410}
411
412template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000413InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000414elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000415 uint32_t Index = this->getSectionIndex(Sym);
George Rimar683a35d2016-08-12 19:25:54 +0000416 if (Index >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000417 fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
Rui Ueyama0b289522016-02-25 18:43:51 +0000418 InputSectionBase<ELFT> *S = Sections[Index];
George Rimar24adce92016-10-06 09:17:55 +0000419
George Rimar308752e2016-11-15 07:56:28 +0000420 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
421 // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
George Rimar683a35d2016-08-12 19:25:54 +0000422 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
George Rimar308752e2016-11-15 07:56:28 +0000423 // In this case it is fine for section to be null here as we do not
424 // allocate sections of these types.
George Rimar24adce92016-10-06 09:17:55 +0000425 if (!S) {
George Rimar308752e2016-11-15 07:56:28 +0000426 if (Index == 0 || Sym.getType() == STT_SECTION ||
427 Sym.getType() == STT_NOTYPE)
George Rimar24adce92016-10-06 09:17:55 +0000428 return nullptr;
429 fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
430 }
431
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000432 if (S == &InputSection<ELFT>::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000433 return S;
434 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000435}
436
437template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000438SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000439 int Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000440 InputSectionBase<ELFT> *Sec = getSection(*Sym);
441 if (Binding == STB_LOCAL) {
Eugene Leviantb380b242016-10-26 11:07:09 +0000442 if (Sym->getType() == STT_FILE)
443 SourceFile = check(Sym->getName(this->StringTable));
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000444
445 if (this->StringTable.size() <= Sym->st_name)
446 fatal(getFilename(this) + ": invalid symbol name offset");
447
448 const char *Name = this->StringTable.data() + Sym->st_name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000449 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000450 return new (BAlloc) Undefined(Name, Sym->st_other, Sym->getType(), this);
451 return new (BAlloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000452 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000453
Rafael Espindola75714f62016-03-03 22:24:39 +0000454 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000455
Rafael Espindola4cda5812015-10-16 15:29:48 +0000456 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000457 case SHN_UNDEF:
George Rimar10874f72016-10-03 11:13:55 +0000458 return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
459 Sym->getType(),
460 /*CanOmitFromDynSym*/ false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000461 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000462 case SHN_COMMON:
George Rimar27e651d2016-10-10 10:31:03 +0000463 if (Sym->st_value == 0 || Sym->st_value >= UINT32_MAX)
George Rimar0c825612016-10-04 08:49:52 +0000464 fatal(getFilename(this) + ": common symbol '" + Name +
George Rimar27e651d2016-10-10 10:31:03 +0000465 "' has invalid alignment: " + Twine(Sym->st_value));
George Rimar10874f72016-10-03 11:13:55 +0000466 return elf::Symtab<ELFT>::X->addCommon(Name, Sym->st_size, Sym->st_value,
467 Binding, Sym->st_other,
468 Sym->getType(), this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000469 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000470 }
Rafael Espindola20348222015-08-24 21:43:25 +0000471
Rafael Espindola67d72c02016-03-11 12:06:30 +0000472 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000473 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000474 fatal(getFilename(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000475 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000476 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000477 case STB_GNU_UNIQUE:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000478 if (Sec == &InputSection<ELFT>::Discarded)
George Rimar10874f72016-10-03 11:13:55 +0000479 return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
480 Sym->getType(),
481 /*CanOmitFromDynSym*/ false,
482 this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000483 ->body();
George Rimar463984d2016-11-15 08:07:14 +0000484 return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec, this)->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000485 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000486}
487
Peter Collingbourne4f952702016-05-01 04:55:03 +0000488template <class ELFT> void ArchiveFile::parse() {
Eugene Leviant7d7ff802016-11-21 09:28:07 +0000489 File = check(Archive::create(MB),
490 MB.getBufferIdentifier() + ": failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000491
Rui Ueyama3d0f77b2016-09-30 17:56:20 +0000492 // Read the symbol table to construct Lazy objects.
493 for (const Archive::Symbol &Sym : File->symbols())
494 Symtab<ELFT>::X->addLazyArchive(this, Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000495}
496
497// Returns a buffer pointing to a member file containing a given symbol.
Davide Italianobcdd6c62016-10-12 19:35:54 +0000498std::pair<MemoryBufferRef, uint64_t>
499ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000500 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000501 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000502 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000503
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000504 if (!Seen.insert(C.getChildOffset()).second)
Davide Italianobcdd6c62016-10-12 19:35:54 +0000505 return {MemoryBufferRef(), 0};
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000506
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000507 MemoryBufferRef Ret =
508 check(C.getMemoryBufferRef(),
509 "could not get the buffer for the member defining symbol " +
510 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000511
Rui Ueyamafe658772016-05-15 17:10:23 +0000512 if (C.getParent()->isThin() && Driver->Cpio)
513 Driver->Cpio->append(relativeToRoot(check(C.getFullName())),
514 Ret.getBuffer());
Davide Italianobcdd6c62016-10-12 19:35:54 +0000515 if (C.getParent()->isThin())
516 return {Ret, 0};
517 return {Ret, C.getChildOffset()};
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000518}
519
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000520template <class ELFT>
Rui Ueyama55518e72016-10-28 20:57:25 +0000521SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000522 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000523
Rafael Espindola115f0f32015-11-03 14:13:40 +0000524template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000525const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000526SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000527 return check(
528 this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000529}
530
Rui Ueyama7c713312016-01-06 01:56:36 +0000531// Partially parse the shared object file so that we can call
532// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000533template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000534 typedef typename ELFT::Dyn Elf_Dyn;
535 typedef typename ELFT::uint uintX_t;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000536 const Elf_Shdr *DynamicSec = nullptr;
537
Rafael Espindolae19abab2016-11-03 20:44:50 +0000538 const ELFFile<ELFT> Obj = this->getObj();
Rafael Espindola84d6a172016-11-03 12:21:00 +0000539 ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
540 for (const Elf_Shdr &Sec : Sections) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000541 switch (Sec.sh_type) {
542 default:
543 continue;
544 case SHT_DYNSYM:
Rafael Espindola21d8be92016-11-03 15:43:47 +0000545 this->initSymtab(Sections, &Sec);
Rafael Espindola115f0f32015-11-03 14:13:40 +0000546 break;
547 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000548 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000549 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000550 case SHT_SYMTAB_SHNDX:
Rafael Espindola84d6a172016-11-03 12:21:00 +0000551 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, Sections));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000552 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000553 case SHT_GNU_versym:
554 this->VersymSec = &Sec;
555 break;
556 case SHT_GNU_verdef:
557 this->VerdefSec = &Sec;
558 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000559 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000560 }
561
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000562 if (this->VersymSec && this->Symbols.empty())
George Rimarbcba39a2016-11-02 10:16:25 +0000563 error("SHT_GNU_versym should be associated with symbol table");
564
Rui Ueyama478f8eb2016-09-09 21:35:38 +0000565 // DSOs are identified by soname, and they usually contain
566 // DT_SONAME tag in their header. But if they are missing,
567 // filenames are used as default sonames.
Davide Italianoe02ba982016-09-08 21:18:38 +0000568 SoName = sys::path::filename(this->getName());
Rafael Espindolac8b15812015-10-01 15:47:50 +0000569
Rui Ueyama361d8b92015-10-12 15:49:02 +0000570 if (!DynamicSec)
571 return;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000572
George Rimar53cf2a82016-10-07 09:01:04 +0000573 ArrayRef<Elf_Dyn> Arr =
574 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
575 getFilename(this) + ": getSectionContentsAsArray failed");
576 for (const Elf_Dyn &Dyn : Arr) {
Rui Ueyama361d8b92015-10-12 15:49:02 +0000577 if (Dyn.d_tag == DT_SONAME) {
578 uintX_t Val = Dyn.getVal();
579 if (Val >= this->StringTable.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000580 fatal(getFilename(this) + ": invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000581 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000582 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000583 }
584 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000585}
Rafael Espindola18173d42015-09-08 15:50:05 +0000586
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000587// Parse the version definitions in the object file if present. Returns a vector
588// whose nth element contains a pointer to the Elf_Verdef for version identifier
589// n. Version identifiers that are not definitions map to nullptr. The array
590// always has at least length 1.
591template <class ELFT>
592std::vector<const typename ELFT::Verdef *>
593SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
594 std::vector<const Elf_Verdef *> Verdefs(1);
595 // We only need to process symbol versions for this DSO if it has both a
596 // versym and a verdef section, which indicates that the DSO contains symbol
597 // version definitions.
598 if (!VersymSec || !VerdefSec)
599 return Verdefs;
600
601 // The location of the first global versym entry.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000602 const char *Base = this->MB.getBuffer().data();
603 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
Rafael Espindola6dcf4c62016-11-03 16:55:44 +0000604 this->FirstNonLocal;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000605
606 // We cannot determine the largest verdef identifier without inspecting
607 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
608 // sequentially starting from 1, so we predict that the largest identifier
609 // will be VerdefCount.
610 unsigned VerdefCount = VerdefSec->sh_info;
611 Verdefs.resize(VerdefCount + 1);
612
613 // Build the Verdefs array by following the chain of Elf_Verdef objects
614 // from the start of the .gnu.version_d section.
Rafael Espindolae19abab2016-11-03 20:44:50 +0000615 const char *Verdef = Base + VerdefSec->sh_offset;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000616 for (unsigned I = 0; I != VerdefCount; ++I) {
617 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
618 Verdef += CurVerdef->vd_next;
619 unsigned VerdefIndex = CurVerdef->vd_ndx;
620 if (Verdefs.size() <= VerdefIndex)
621 Verdefs.resize(VerdefIndex + 1);
622 Verdefs[VerdefIndex] = CurVerdef;
623 }
624
625 return Verdefs;
626}
627
Rui Ueyama7c713312016-01-06 01:56:36 +0000628// Fully parse the shared object file. This must be called after parseSoName().
629template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000630 // Create mapping from version identifiers to Elf_Verdef entries.
631 const Elf_Versym *Versym = nullptr;
632 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
633
Rafael Espindola8e232572016-11-03 20:48:57 +0000634 Elf_Sym_Range Syms = this->getGlobalSymbols();
Rafael Espindola18173d42015-09-08 15:50:05 +0000635 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000636 unsigned VersymIndex = 0;
637 if (Versym) {
638 VersymIndex = Versym->vs_index;
639 ++Versym;
640 }
641
Rafael Espindola18da0e52016-04-29 16:23:31 +0000642 StringRef Name = check(Sym.getName(this->StringTable));
643 if (Sym.isUndefined()) {
644 Undefs.push_back(Name);
645 continue;
646 }
647
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000648 if (Versym) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000649 // Ignore local symbols and non-default versions.
Rafael Espindolad2454d62016-06-09 15:45:49 +0000650 if (VersymIndex == VER_NDX_LOCAL || (VersymIndex & VERSYM_HIDDEN))
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000651 continue;
652 }
Rafael Espindolad2454d62016-06-09 15:45:49 +0000653
654 const Elf_Verdef *V =
655 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
656 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
Rafael Espindola18173d42015-09-08 15:50:05 +0000657 }
658}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000659
Rui Ueyama80356882016-08-03 20:33:17 +0000660static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
Peter Collingbournecd513a42016-11-11 19:50:24 +0000661 Triple T(check(getBitcodeTargetTriple(MB)));
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000662 if (T.isLittleEndian())
663 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
664 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000665}
666
Rui Ueyama80356882016-08-03 20:33:17 +0000667static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
Peter Collingbournecd513a42016-11-11 19:50:24 +0000668 Triple T(check(getBitcodeTargetTriple(MB)));
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000669 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000670 case Triple::aarch64:
671 return EM_AARCH64;
672 case Triple::arm:
673 return EM_ARM;
674 case Triple::mips:
675 case Triple::mipsel:
676 case Triple::mips64:
677 case Triple::mips64el:
678 return EM_MIPS;
679 case Triple::ppc:
680 return EM_PPC;
681 case Triple::ppc64:
682 return EM_PPC64;
683 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000684 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000685 case Triple::x86_64:
686 return EM_X86_64;
687 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000688 fatal(MB.getBufferIdentifier() +
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000689 ": could not infer e_machine from bitcode target triple " + T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000690 }
691}
692
Rui Ueyama523744d2016-07-07 02:46:30 +0000693BitcodeFile::BitcodeFile(MemoryBufferRef MB) : InputFile(BitcodeKind, MB) {
Rui Ueyama80356882016-08-03 20:33:17 +0000694 EKind = getBitcodeELFKind(MB);
695 EMachine = getBitcodeMachineKind(MB);
Davide Italiano60976ba2016-06-29 06:12:39 +0000696}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000697
Davide Italiano786d8e32016-09-29 00:40:08 +0000698static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
699 switch (GvVisibility) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000700 case GlobalValue::DefaultVisibility:
701 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000702 case GlobalValue::HiddenVisibility:
703 return STV_HIDDEN;
704 case GlobalValue::ProtectedVisibility:
705 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000706 }
George Rimar777f9632016-03-12 08:31:34 +0000707 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000708}
709
Peter Collingbourne4f952702016-05-01 04:55:03 +0000710template <class ELFT>
Rafael Espindola3db70212016-10-25 12:02:31 +0000711static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
Davide Italiano786d8e32016-09-29 00:40:08 +0000712 const lto::InputFile::Symbol &ObjSym,
Rui Ueyama55518e72016-10-28 20:57:25 +0000713 BitcodeFile *F) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000714 StringRef NameRef = Saver.save(ObjSym.getName());
715 uint32_t Flags = ObjSym.getFlags();
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000716 uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000717
Davide Italiano786d8e32016-09-29 00:40:08 +0000718 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
719 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
720 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000721
Rafael Espindola3db70212016-10-25 12:02:31 +0000722 int C = check(ObjSym.getComdatIndex());
723 if (C != -1 && !KeptComdats[C])
724 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
725 CanOmitFromDynSym, F);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000726
Davide Italiano9f8efff2016-04-22 18:26:33 +0000727 if (Flags & BasicSymbolRef::SF_Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000728 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000729 CanOmitFromDynSym, F);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000730
Davide Italiano786d8e32016-09-29 00:40:08 +0000731 if (Flags & BasicSymbolRef::SF_Common)
732 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
733 ObjSym.getCommonAlignment(), Binding,
734 Visibility, STT_OBJECT, F);
735
736 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
737 CanOmitFromDynSym, F);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000738}
739
Peter Collingbourne4f952702016-05-01 04:55:03 +0000740template <class ELFT>
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000741void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
Davide Italianobcdd6c62016-10-12 19:35:54 +0000742
743 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
744 // (the fully resolved path of the archive) + member name + offset of the
745 // member in the archive.
746 // ThinLTO uses the MemoryBufferRef identifier to access its internal
747 // data structures and if two archives define two members with the same name,
748 // this causes a collision which result in only one of the objects being
749 // taken into consideration at LTO time (which very likely causes undefined
750 // symbols later in the link stage).
751 Obj = check(lto::InputFile::create(MemoryBufferRef(
752 MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() +
753 utostr(OffsetInArchive)))));
Rafael Espindola3db70212016-10-25 12:02:31 +0000754
755 std::vector<bool> KeptComdats;
756 for (StringRef S : Obj->getComdatTable()) {
757 StringRef N = Saver.save(S);
758 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(N)).second);
759 }
760
Davide Italiano786d8e32016-09-29 00:40:08 +0000761 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
Rui Ueyama55518e72016-10-28 20:57:25 +0000762 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000763}
764
Rui Ueyamac4b65062015-10-12 15:31:09 +0000765template <template <class> class T>
Rui Ueyama55518e72016-10-28 20:57:25 +0000766static InputFile *createELFFile(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000767 unsigned char Size;
768 unsigned char Endian;
769 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
770 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
George Rimar777f9632016-03-12 08:31:34 +0000771 fatal("invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000772
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000773 size_t BufSize = MB.getBuffer().size();
774 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
775 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
776 fatal("file is too short");
777
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000778 InputFile *Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000779 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000780 Obj = make<T<ELF32LE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000781 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000782 Obj = make<T<ELF32BE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000783 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000784 Obj = make<T<ELF64LE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000785 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
Rui Ueyamad52adb32016-11-01 22:53:18 +0000786 Obj = make<T<ELF64BE>>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000787 else
788 fatal("invalid file class: " + MB.getBufferIdentifier());
789
790 if (!Config->FirstElf)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000791 Config->FirstElf = Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000792 return Obj;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000793}
794
Rafael Espindola093abab2016-10-27 17:45:40 +0000795template <class ELFT> void BinaryFile::parse() {
796 StringRef Buf = MB.getBuffer();
797 ArrayRef<uint8_t> Data =
798 makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size());
Rui Ueyama673c9d92016-10-20 06:44:58 +0000799
Rafael Espindola093abab2016-10-27 17:45:40 +0000800 std::string Filename = MB.getBufferIdentifier();
801 std::transform(Filename.begin(), Filename.end(), Filename.begin(),
802 [](char C) { return isalnum(C) ? C : '_'; });
803 Filename = "_binary_" + Filename;
804 StringRef StartName = Saver.save(Twine(Filename) + "_start");
805 StringRef EndName = Saver.save(Twine(Filename) + "_end");
806 StringRef SizeName = Saver.save(Twine(Filename) + "_size");
807
808 auto *Section =
Davide Italiano089f0e72016-11-08 01:42:38 +0000809 make<InputSection<ELFT>>(SHF_ALLOC, SHT_PROGBITS, 8, Data, ".data");
Rafael Espindola093abab2016-10-27 17:45:40 +0000810 Sections.push_back(Section);
811
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000812 elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, STT_OBJECT, 0, 0,
George Rimar463984d2016-11-15 08:07:14 +0000813 STB_GLOBAL, Section, nullptr);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000814 elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, STT_OBJECT,
George Rimar463984d2016-11-15 08:07:14 +0000815 Data.size(), 0, STB_GLOBAL, Section,
816 nullptr);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000817 elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, STT_OBJECT,
George Rimar463984d2016-11-15 08:07:14 +0000818 Data.size(), 0, STB_GLOBAL, nullptr,
819 nullptr);
Michael J. Spencera9424f32016-09-09 22:08:04 +0000820}
821
Rui Ueyama4655ea32016-04-08 00:14:55 +0000822static bool isBitcode(MemoryBufferRef MB) {
823 using namespace sys::fs;
824 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
825}
826
Rui Ueyama55518e72016-10-28 20:57:25 +0000827InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
Davide Italianobcdd6c62016-10-12 19:35:54 +0000828 uint64_t OffsetInArchive) {
Rui Ueyama55518e72016-10-28 20:57:25 +0000829 InputFile *F =
Davide Italianoba8761b2016-11-08 01:46:02 +0000830 isBitcode(MB) ? make<BitcodeFile>(MB) : createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000831 F->ArchiveName = ArchiveName;
Davide Italianobcdd6c62016-10-12 19:35:54 +0000832 F->OffsetInArchive = OffsetInArchive;
Rui Ueyama71c066d2016-02-02 08:22:41 +0000833 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000834}
835
Rui Ueyama55518e72016-10-28 20:57:25 +0000836InputFile *elf::createSharedFile(MemoryBufferRef MB) {
837 return createELFFile<SharedFile>(MB);
Rui Ueyama533c0302016-01-06 00:09:43 +0000838}
839
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000840MemoryBufferRef LazyObjectFile::getBuffer() {
841 if (Seen)
842 return MemoryBufferRef();
843 Seen = true;
844 return MB;
845}
846
George Rimar10874f72016-10-03 11:13:55 +0000847template <class ELFT> void LazyObjectFile::parse() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000848 for (StringRef Sym : getSymbols())
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000849 Symtab<ELFT>::X->addLazyObject(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000850}
851
852template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
853 typedef typename ELFT::Shdr Elf_Shdr;
854 typedef typename ELFT::Sym Elf_Sym;
855 typedef typename ELFT::SymRange Elf_Sym_Range;
856
Rafael Espindola22e9a8e2016-11-03 20:17:25 +0000857 const ELFFile<ELFT> Obj(this->MB.getBuffer());
Rafael Espindola6d18d382016-11-03 13:24:29 +0000858 ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
859 for (const Elf_Shdr &Sec : Sections) {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000860 if (Sec.sh_type != SHT_SYMTAB)
861 continue;
Rafael Espindoladd6abaa2016-11-03 13:43:51 +0000862 Elf_Sym_Range Syms = check(Obj.symbols(&Sec));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000863 uint32_t FirstNonLocal = Sec.sh_info;
Rafael Espindola6d18d382016-11-03 13:24:29 +0000864 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec, Sections));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000865 std::vector<StringRef> V;
866 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +0000867 if (Sym.st_shndx != SHN_UNDEF)
868 V.push_back(check(Sym.getName(StringTable)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000869 return V;
870 }
871 return {};
872}
873
874std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
Davide Italiano786d8e32016-09-29 00:40:08 +0000875 std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB));
Rui Ueyamad72dd1f2016-09-29 00:58:10 +0000876 std::vector<StringRef> V;
877 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
878 if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined))
879 V.push_back(Saver.save(Sym.getName()));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000880 return V;
881}
882
Rui Ueyama1f492892016-04-08 20:49:31 +0000883// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000884std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +0000885 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000886 return getBitcodeSymbols();
887
Rui Ueyama4655ea32016-04-08 00:14:55 +0000888 unsigned char Size;
889 unsigned char Endian;
890 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
891 if (Size == ELFCLASS32) {
892 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000893 return getElfSymbols<ELF32LE>();
894 return getElfSymbols<ELF32BE>();
895 }
Rui Ueyama4655ea32016-04-08 00:14:55 +0000896 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000897 return getElfSymbols<ELF64LE>();
898 return getElfSymbols<ELF64BE>();
899}
900
Peter Collingbourne4f952702016-05-01 04:55:03 +0000901template void ArchiveFile::parse<ELF32LE>();
902template void ArchiveFile::parse<ELF32BE>();
903template void ArchiveFile::parse<ELF64LE>();
904template void ArchiveFile::parse<ELF64BE>();
905
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000906template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
907template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
908template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
909template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000910
911template void LazyObjectFile::parse<ELF32LE>();
912template void LazyObjectFile::parse<ELF32BE>();
913template void LazyObjectFile::parse<ELF64LE>();
914template void LazyObjectFile::parse<ELF64BE>();
915
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000916template class elf::ELFFileBase<ELF32LE>;
917template class elf::ELFFileBase<ELF32BE>;
918template class elf::ELFFileBase<ELF64LE>;
919template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000920
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000921template class elf::ObjectFile<ELF32LE>;
922template class elf::ObjectFile<ELF32BE>;
923template class elf::ObjectFile<ELF64LE>;
924template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000925
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000926template class elf::SharedFile<ELF32LE>;
927template class elf::SharedFile<ELF32BE>;
928template class elf::SharedFile<ELF64LE>;
929template class elf::SharedFile<ELF64BE>;
Michael J. Spencera9424f32016-09-09 22:08:04 +0000930
Rafael Espindola093abab2016-10-27 17:45:40 +0000931template void BinaryFile::parse<ELF32LE>();
932template void BinaryFile::parse<ELF32BE>();
933template void BinaryFile::parse<ELF64LE>();
934template void BinaryFile::parse<ELF64BE>();