blob: aa6a53cdbd483752ed5711efa767a8342eb30108 [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 Espindola5217f842016-09-28 16:31:05 +000012#include "ELFCreator.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000013#include "Error.h"
Rafael Espindola9d13d042016-02-11 15:24:48 +000014#include "InputSection.h"
George Rimar67e3ff82016-08-12 19:56:57 +000015#include "LinkerScript.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"
Davide Italiano60976ba2016-06-29 06:12:39 +000019#include "llvm/Bitcode/ReaderWriter.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000020#include "llvm/CodeGen/Analysis.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000021#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000022#include "llvm/IR/Module.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000023#include "llvm/LTO/LTO.h"
Michael J. Spencera9424f32016-09-09 22:08:04 +000024#include "llvm/MC/StringTableBuilder.h"
Davide Italianoe02ba982016-09-08 21:18:38 +000025#include "llvm/Support/Path.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
Michael J. Spencer1b348a62015-09-04 22:28:10 +000028using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000030using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000031using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
33using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000035
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000036std::vector<InputFile *> InputFile::Pool;
37
38// Deletes all InputFile instances created so far.
39void InputFile::freePool() {
40 // Files are freed in reverse order so that files created
41 // from other files (e.g. object files extracted from archives)
42 // are freed in the proper order.
43 for (int I = Pool.size() - 1; I >= 0; --I)
44 delete Pool[I];
45}
46
Rafael Espindola78db5a92016-05-09 21:40:06 +000047// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Rui Ueyama429ef2a2016-07-15 20:38:28 +000048std::string elf::getFilename(const InputFile *F) {
Rafael Espindola78db5a92016-05-09 21:40:06 +000049 if (!F)
50 return "(internal)";
51 if (!F->ArchiveName.empty())
52 return (F->ArchiveName + "(" + F->getName() + ")").str();
53 return F->getName();
54}
55
George Rimar10874f72016-10-03 11:13:55 +000056template <class ELFT> static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000057 std::error_code EC;
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000058 ELFFile<ELFT> F(MB.getBuffer(), EC);
Rui Ueyamaf8292e92016-07-15 02:01:03 +000059 if (EC)
George Rimarb7aec332016-10-07 08:51:57 +000060 fatal(EC, "failed to read " + MB.getBufferIdentifier());
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000061 return F;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000062}
63
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000064template <class ELFT> static ELFKind getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +000065 if (ELFT::TargetEndianness == support::little)
66 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
67 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +000068}
69
70template <class ELFT>
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000071ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
72 : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {
73 EKind = getELFKind<ELFT>();
74 EMachine = ELFObj.getHeader()->e_machine;
75}
76
77template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000078typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
Rafael Espindola18173d42015-09-08 15:50:05 +000079 if (!Symtab)
80 return Elf_Sym_Range(nullptr, nullptr);
Rafael Espindolae1901cc2015-09-24 15:11:50 +000081 Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
Rafael Espindola18173d42015-09-08 15:50:05 +000082 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
83 uint32_t FirstNonLocal = Symtab->sh_info;
George Rimar9397cf92016-10-07 15:16:27 +000084 if (FirstNonLocal == 0 || FirstNonLocal > NumSymbols)
Rui Ueyama429ef2a2016-07-15 20:38:28 +000085 fatal(getFilename(this) + ": invalid sh_info in symbol table");
Rafael Espindola67d72c02016-03-11 12:06:30 +000086
87 if (OnlyGlobals)
Rafael Espindola0f7ccc32016-04-05 14:47:28 +000088 return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
89 return makeArrayRef(Syms.begin(), Syms.end());
Davide Italiano6d328d32015-09-16 20:45:57 +000090}
91
Rafael Espindola115f0f32015-11-03 14:13:40 +000092template <class ELFT>
93uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000094 uint32_t I = Sym.st_shndx;
95 if (I == ELF::SHN_XINDEX)
Rui Ueyamae69ab102016-01-06 01:14:11 +000096 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
Rafael Espindola972b2362016-03-09 14:31:18 +000097 if (I >= ELF::SHN_LORESERVE)
Rafael Espindola115f0f32015-11-03 14:13:40 +000098 return 0;
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000099 return I;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000100}
101
Rafael Espindolaaf707642015-10-12 01:55:32 +0000102template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
Rafael Espindola3e603792015-10-01 20:26:37 +0000103 if (!Symtab)
104 return;
Rafael Espindola75714f62016-03-03 22:24:39 +0000105 StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000106}
107
108template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000109elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
Rafael Espindola2a4b2712015-10-13 01:17:02 +0000110 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000111
112template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +0000113ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
114 if (!this->Symtab)
115 return this->SymbolBodies;
116 uint32_t FirstNonLocal = this->Symtab->sh_info;
117 return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
118}
119
120template <class ELFT>
121ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
122 if (!this->Symtab)
123 return this->SymbolBodies;
124 uint32_t FirstNonLocal = this->Symtab->sh_info;
125 return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
126}
127
128template <class ELFT>
129ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
130 if (!this->Symtab)
131 return this->SymbolBodies;
132 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000133}
134
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000135template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000136 if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo)
137 return MipsOptions->Reginfo->ri_gp_value;
138 if (!ELFT::Is64Bits && MipsReginfo && MipsReginfo->Reginfo)
Rui Ueyama70eed362016-01-06 22:42:43 +0000139 return MipsReginfo->Reginfo->ri_gp_value;
140 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +0000141}
142
Rafael Espindola444576d2015-10-09 19:25:07 +0000143template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000144void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000145 // Read section and symbol tables.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000146 initializeSections(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000147 initializeSymbols();
Peter Smith07606052016-10-10 10:10:27 +0000148 if (Config->GcSections && Config->EMachine == EM_ARM )
149 initializeReverseDependencies();
Michael J. Spencer84487f12015-07-24 21:03:07 +0000150}
151
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000152// Sections with SHT_GROUP and comdat bits define comdat section groups.
153// They are identified and deduplicated by group name. This function
154// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000155template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000156StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000157 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama188d2c32016-07-15 20:05:05 +0000158 const Elf_Shdr *Symtab = check(Obj.getSection(Sec.sh_link));
159 const Elf_Sym *Sym = Obj.getSymbol(Symtab, Sec.sh_info);
160 StringRef Strtab = check(Obj.getStringTableForSymtab(*Symtab));
161 return check(Sym->getName(Strtab));
Rafael Espindola444576d2015-10-09 19:25:07 +0000162}
163
164template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000165ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000166elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000167 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000168 ArrayRef<Elf_Word> Entries =
169 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000170 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000171 fatal(getFilename(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000172 return Entries.slice(1);
173}
174
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000175template <class ELFT>
176bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000177 // We don't merge sections if -O0 (default is -O1). This makes sometimes
178 // the linker significantly faster, although the output will be bigger.
179 if (Config->Optimize == 0)
180 return false;
181
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000182 // Do not merge sections if generating a relocatable object. It makes
183 // the code simpler because we do not need to update relocation addends
184 // to reflect changes introduced by merging. Instead of that we write
185 // such "merge" sections into separate OutputSections and keep SHF_MERGE
186 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
187 // later during a final linking.
188 if (Config->Relocatable)
189 return false;
190
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000191 // A mergeable section with size 0 is useless because they don't have
192 // any data to merge. A mergeable string section with size 0 can be
193 // argued as invalid because it doesn't end with a null character.
194 // We'll avoid a mess by handling them as if they were non-mergeable.
195 if (Sec.sh_size == 0)
196 return false;
197
Rui Ueyamac75ef852016-09-21 03:22:18 +0000198 // Check for sh_entsize. The ELF spec is not clear about the zero
199 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
200 // the section does not hold a table of fixed-size entries". We know
201 // that Rust 1.13 produces a string mergeable section with a zero
202 // sh_entsize. Here we just accept it rather than being picky about it.
203 uintX_t EntSize = Sec.sh_entsize;
204 if (EntSize == 0)
205 return false;
206 if (Sec.sh_size % EntSize)
207 fatal(getFilename(this) +
208 ": SHF_MERGE section size must be a multiple of sh_entsize");
209
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000210 uintX_t Flags = Sec.sh_flags;
211 if (!(Flags & SHF_MERGE))
212 return false;
213 if (Flags & SHF_WRITE)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000214 fatal(getFilename(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000215
Peter Smith4df2e142016-05-18 11:40:16 +0000216 // Don't try to merge if the alignment is larger than the sh_entsize and this
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000217 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000218 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000219 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
220 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000221 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000222 if (Flags & SHF_STRINGS)
223 return true;
224
George Rimardcddfb62016-06-08 12:04:59 +0000225 return Sec.sh_addralign <= EntSize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000226}
227
228template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000229void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000230 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000231 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000232 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000233 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000234 const ELFFile<ELFT> &Obj = this->ELFObj;
235 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000236 ++I;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000237 if (Sections[I] == &InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000238 continue;
239
Rui Ueyamaaf9793d2016-10-04 16:47:49 +0000240 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
241 // if -r is given, we'll let the final link discard such sections.
242 // This is compatible with GNU.
243 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
Eugene Leviant27be5422016-09-28 08:42:02 +0000244 Sections[I] = &InputSection<ELFT>::Discarded;
245 continue;
246 }
247
Rafael Espindolacde25132015-08-13 14:45:44 +0000248 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000249 case SHT_GROUP:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000250 Sections[I] = &InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000251 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000252 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000253 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000254 if (SecIndex >= Size)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000255 fatal(getFilename(this) + ": invalid section index in group: " +
256 Twine(SecIndex));
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000257 Sections[SecIndex] = &InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000258 }
259 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000260 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000261 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000262 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000263 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000264 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000265 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000266 case SHT_STRTAB:
267 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000268 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000269 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000270 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000271 }
272 }
273}
274
Peter Smith07606052016-10-10 10:10:27 +0000275// .ARM.exidx sections have a reverse dependency on the InputSection they
276// have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
277template <class ELFT>
278void elf::ObjectFile<ELFT>::initializeReverseDependencies() {
279 unsigned I = -1;
280 for (const Elf_Shdr &Sec : this->ELFObj.sections()) {
281 ++I;
282 if ((Sections[I] == &InputSection<ELFT>::Discarded) ||
283 !(Sec.sh_flags & SHF_LINK_ORDER))
284 continue;
285 if (Sec.sh_link >= Sections.size())
286 fatal(getFilename(this) + ": invalid sh_link index: " +
287 Twine(Sec.sh_link));
288 auto *IS = cast<InputSection<ELFT>>(Sections[Sec.sh_link]);
289 IS->DependentSection = Sections[I];
290 }
291}
292
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000293template <class ELFT>
294InputSectionBase<ELFT> *
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000295elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
296 uint32_t Idx = Sec.sh_info;
297 if (Idx >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000298 fatal(getFilename(this) + ": invalid relocated section index: " +
299 Twine(Idx));
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000300 InputSectionBase<ELFT> *Target = Sections[Idx];
301
302 // Strictly speaking, a relocation section must be included in the
303 // group of the section it relocates. However, LLVM 3.3 and earlier
304 // would fail to do so, so we gracefully handle that case.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000305 if (Target == &InputSection<ELFT>::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000306 return nullptr;
307
308 if (!Target)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000309 fatal(getFilename(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000310 return Target;
311}
312
313template <class ELFT>
314InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000315elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola75714f62016-03-03 22:24:39 +0000316 StringRef Name = check(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000317
Rafael Espindola042a3f22016-09-08 14:06:08 +0000318 switch (Sec.sh_type) {
319 case SHT_ARM_ATTRIBUTES:
320 // FIXME: ARM meta-data section. At present attributes are ignored,
321 // they can be used to reason about object compatibility.
322 return &InputSection<ELFT>::Discarded;
323 case SHT_MIPS_REGINFO:
324 MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec, Name));
325 return MipsReginfo.get();
326 case SHT_MIPS_OPTIONS:
327 MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec, Name));
328 return MipsOptions.get();
329 case SHT_MIPS_ABIFLAGS:
330 MipsAbiFlags.reset(new MipsAbiFlagsInputSection<ELFT>(this, &Sec, Name));
331 return MipsAbiFlags.get();
332 case SHT_RELA:
333 case SHT_REL: {
334 // This section contains relocation information.
335 // If -r is given, we do not interpret or apply relocation
336 // but just copy relocation sections to output.
337 if (Config->Relocatable)
338 return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec, Name);
339
340 // Find the relocation target section and associate this
341 // section with it.
342 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
343 if (!Target)
344 return nullptr;
345 if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
346 S->RelocSections.push_back(&Sec);
347 return nullptr;
348 }
349 if (auto *S = dyn_cast<EhInputSection<ELFT>>(Target)) {
350 if (S->RelocSection)
351 fatal(getFilename(this) +
352 ": multiple relocation sections to .eh_frame are not supported");
353 S->RelocSection = &Sec;
354 return nullptr;
355 }
356 fatal(getFilename(this) +
357 ": relocations pointing to SHF_MERGE are not supported");
358 }
359 }
360
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000361 // .note.GNU-stack is a marker section to control the presence of
362 // PT_GNU_STACK segment in outputs. Since the presence of the segment
363 // is controlled only by the command line option (-z execstack) in LLD,
364 // .note.GNU-stack is ignored.
365 if (Name == ".note.GNU-stack")
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000366 return &InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000367
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000368 if (Name == ".note.GNU-split-stack") {
George Rimar777f9632016-03-12 08:31:34 +0000369 error("objects using splitstacks are not supported");
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000370 return &InputSection<ELFT>::Discarded;
371 }
372
George Rimarf21aade2016-08-31 08:38:11 +0000373 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000374 return &InputSection<ELFT>::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000375
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000376 // The linker merges EH (exception handling) frames and creates a
377 // .eh_frame_hdr section for runtime. So we handle them with a special
378 // class. For relocatable outputs, they are just passed through.
379 if (Name == ".eh_frame" && !Config->Relocatable)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000380 return new (EHAlloc.Allocate()) EhInputSection<ELFT>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000381
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000382 if (shouldMerge(Sec))
Rafael Espindola042a3f22016-09-08 14:06:08 +0000383 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec, Name);
384 return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000385}
386
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000387template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000388 this->initStringTable();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000389 Elf_Sym_Range Syms = this->getElfSymbols(false);
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000390 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000391 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000392 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000393 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000394}
395
396template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000397InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000398elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000399 uint32_t Index = this->getSectionIndex(Sym);
George Rimar683a35d2016-08-12 19:25:54 +0000400 if (Index >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000401 fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
Rui Ueyama0b289522016-02-25 18:43:51 +0000402 InputSectionBase<ELFT> *S = Sections[Index];
George Rimar24adce92016-10-06 09:17:55 +0000403
George Rimar683a35d2016-08-12 19:25:54 +0000404 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03
405 // could generate broken objects. STT_SECTION symbols can be
406 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
407 // In this case it is fine for section to be null here as we
408 // do not allocate sections of these types.
George Rimar24adce92016-10-06 09:17:55 +0000409 if (!S) {
410 if (Index == 0 || Sym.getType() == STT_SECTION)
411 return nullptr;
412 fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
413 }
414
415 if (S == &InputSectionBase<ELFT>::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000416 return S;
417 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000418}
419
420template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000421SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000422 int Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000423 InputSectionBase<ELFT> *Sec = getSection(*Sym);
424 if (Binding == STB_LOCAL) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000425 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000426 return new (this->Alloc)
Rui Ueyama434b5612016-07-17 03:11:46 +0000427 Undefined(Sym->st_name, Sym->st_other, Sym->getType(), this);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000428 return new (this->Alloc) DefinedRegular<ELFT>(*Sym, Sec);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000429 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000430
Rafael Espindola75714f62016-03-03 22:24:39 +0000431 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000432
Rafael Espindola4cda5812015-10-16 15:29:48 +0000433 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000434 case SHN_UNDEF:
George Rimar10874f72016-10-03 11:13:55 +0000435 return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
436 Sym->getType(),
437 /*CanOmitFromDynSym*/ false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000438 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000439 case SHN_COMMON:
George Rimar0c825612016-10-04 08:49:52 +0000440 if (Sym->st_value == 0)
441 fatal(getFilename(this) + ": common symbol '" + Name +
442 "' alignment is 0");
George Rimar10874f72016-10-03 11:13:55 +0000443 return elf::Symtab<ELFT>::X->addCommon(Name, Sym->st_size, Sym->st_value,
444 Binding, Sym->st_other,
445 Sym->getType(), this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000446 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000447 }
Rafael Espindola20348222015-08-24 21:43:25 +0000448
Rafael Espindola67d72c02016-03-11 12:06:30 +0000449 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000450 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000451 fatal(getFilename(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000452 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000453 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000454 case STB_GNU_UNIQUE:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000455 if (Sec == &InputSection<ELFT>::Discarded)
George Rimar10874f72016-10-03 11:13:55 +0000456 return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
457 Sym->getType(),
458 /*CanOmitFromDynSym*/ false,
459 this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000460 ->body();
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000461 return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000462 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000463}
464
Peter Collingbourne4f952702016-05-01 04:55:03 +0000465template <class ELFT> void ArchiveFile::parse() {
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000466 File = check(Archive::create(MB), "failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000467
Rui Ueyama3d0f77b2016-09-30 17:56:20 +0000468 // Read the symbol table to construct Lazy objects.
469 for (const Archive::Symbol &Sym : File->symbols())
470 Symtab<ELFT>::X->addLazyArchive(this, Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000471}
472
473// Returns a buffer pointing to a member file containing a given symbol.
474MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000475 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000476 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000477 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000478
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000479 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000480 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000481
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000482 MemoryBufferRef Ret =
483 check(C.getMemoryBufferRef(),
484 "could not get the buffer for the member defining symbol " +
485 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000486
Rui Ueyamafe658772016-05-15 17:10:23 +0000487 if (C.getParent()->isThin() && Driver->Cpio)
488 Driver->Cpio->append(relativeToRoot(check(C.getFullName())),
489 Ret.getBuffer());
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000490
491 return Ret;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000492}
493
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000494template <class ELFT>
495SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000496 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000497
Rafael Espindola115f0f32015-11-03 14:13:40 +0000498template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000499const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000500SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
501 uint32_t Index = this->getSectionIndex(Sym);
502 if (Index == 0)
503 return nullptr;
Rafael Espindola75714f62016-03-03 22:24:39 +0000504 return check(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000505}
506
Rui Ueyama7c713312016-01-06 01:56:36 +0000507// Partially parse the shared object file so that we can call
508// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000509template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000510 typedef typename ELFT::Dyn Elf_Dyn;
511 typedef typename ELFT::uint uintX_t;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000512 const Elf_Shdr *DynamicSec = nullptr;
513
514 const ELFFile<ELFT> Obj = this->ELFObj;
515 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000516 switch (Sec.sh_type) {
517 default:
518 continue;
519 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000520 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000521 break;
522 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000523 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000524 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000525 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000526 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000527 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000528 case SHT_GNU_versym:
529 this->VersymSec = &Sec;
530 break;
531 case SHT_GNU_verdef:
532 this->VerdefSec = &Sec;
533 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000534 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000535 }
536
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000537 this->initStringTable();
Rui Ueyama478f8eb2016-09-09 21:35:38 +0000538
539 // DSOs are identified by soname, and they usually contain
540 // DT_SONAME tag in their header. But if they are missing,
541 // filenames are used as default sonames.
Davide Italianoe02ba982016-09-08 21:18:38 +0000542 SoName = sys::path::filename(this->getName());
Rafael Espindolac8b15812015-10-01 15:47:50 +0000543
Rui Ueyama361d8b92015-10-12 15:49:02 +0000544 if (!DynamicSec)
545 return;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000546
George Rimar53cf2a82016-10-07 09:01:04 +0000547 ArrayRef<Elf_Dyn> Arr =
548 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
549 getFilename(this) + ": getSectionContentsAsArray failed");
550 for (const Elf_Dyn &Dyn : Arr) {
Rui Ueyama361d8b92015-10-12 15:49:02 +0000551 if (Dyn.d_tag == DT_SONAME) {
552 uintX_t Val = Dyn.getVal();
553 if (Val >= this->StringTable.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000554 fatal(getFilename(this) + ": invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000555 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000556 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000557 }
558 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000559}
Rafael Espindola18173d42015-09-08 15:50:05 +0000560
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000561// Parse the version definitions in the object file if present. Returns a vector
562// whose nth element contains a pointer to the Elf_Verdef for version identifier
563// n. Version identifiers that are not definitions map to nullptr. The array
564// always has at least length 1.
565template <class ELFT>
566std::vector<const typename ELFT::Verdef *>
567SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
568 std::vector<const Elf_Verdef *> Verdefs(1);
569 // We only need to process symbol versions for this DSO if it has both a
570 // versym and a verdef section, which indicates that the DSO contains symbol
571 // version definitions.
572 if (!VersymSec || !VerdefSec)
573 return Verdefs;
574
575 // The location of the first global versym entry.
576 Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
577 VersymSec->sh_offset) +
578 this->Symtab->sh_info;
579
580 // We cannot determine the largest verdef identifier without inspecting
581 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
582 // sequentially starting from 1, so we predict that the largest identifier
583 // will be VerdefCount.
584 unsigned VerdefCount = VerdefSec->sh_info;
585 Verdefs.resize(VerdefCount + 1);
586
587 // Build the Verdefs array by following the chain of Elf_Verdef objects
588 // from the start of the .gnu.version_d section.
589 const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
590 for (unsigned I = 0; I != VerdefCount; ++I) {
591 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
592 Verdef += CurVerdef->vd_next;
593 unsigned VerdefIndex = CurVerdef->vd_ndx;
594 if (Verdefs.size() <= VerdefIndex)
595 Verdefs.resize(VerdefIndex + 1);
596 Verdefs[VerdefIndex] = CurVerdef;
597 }
598
599 return Verdefs;
600}
601
Rui Ueyama7c713312016-01-06 01:56:36 +0000602// Fully parse the shared object file. This must be called after parseSoName().
603template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000604 // Create mapping from version identifiers to Elf_Verdef entries.
605 const Elf_Versym *Versym = nullptr;
606 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
607
Rafael Espindola67d72c02016-03-11 12:06:30 +0000608 Elf_Sym_Range Syms = this->getElfSymbols(true);
Rafael Espindola18173d42015-09-08 15:50:05 +0000609 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000610 unsigned VersymIndex = 0;
611 if (Versym) {
612 VersymIndex = Versym->vs_index;
613 ++Versym;
614 }
615
Rafael Espindola18da0e52016-04-29 16:23:31 +0000616 StringRef Name = check(Sym.getName(this->StringTable));
617 if (Sym.isUndefined()) {
618 Undefs.push_back(Name);
619 continue;
620 }
621
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000622 if (Versym) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000623 // Ignore local symbols and non-default versions.
Rafael Espindolad2454d62016-06-09 15:45:49 +0000624 if (VersymIndex == VER_NDX_LOCAL || (VersymIndex & VERSYM_HIDDEN))
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000625 continue;
626 }
Rafael Espindolad2454d62016-06-09 15:45:49 +0000627
628 const Elf_Verdef *V =
629 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
630 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
Rafael Espindola18173d42015-09-08 15:50:05 +0000631 }
632}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000633
Rui Ueyama80356882016-08-03 20:33:17 +0000634static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000635 Triple T(getBitcodeTargetTriple(MB, Driver->Context));
636 if (T.isLittleEndian())
637 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
638 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000639}
640
Rui Ueyama80356882016-08-03 20:33:17 +0000641static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000642 Triple T(getBitcodeTargetTriple(MB, Driver->Context));
643 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000644 case Triple::aarch64:
645 return EM_AARCH64;
646 case Triple::arm:
647 return EM_ARM;
648 case Triple::mips:
649 case Triple::mipsel:
650 case Triple::mips64:
651 case Triple::mips64el:
652 return EM_MIPS;
653 case Triple::ppc:
654 return EM_PPC;
655 case Triple::ppc64:
656 return EM_PPC64;
657 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000658 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000659 case Triple::x86_64:
660 return EM_X86_64;
661 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000662 fatal(MB.getBufferIdentifier() +
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000663 ": could not infer e_machine from bitcode target triple " + T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000664 }
665}
666
Rui Ueyama523744d2016-07-07 02:46:30 +0000667BitcodeFile::BitcodeFile(MemoryBufferRef MB) : InputFile(BitcodeKind, MB) {
Rui Ueyama80356882016-08-03 20:33:17 +0000668 EKind = getBitcodeELFKind(MB);
669 EMachine = getBitcodeMachineKind(MB);
Davide Italiano60976ba2016-06-29 06:12:39 +0000670}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000671
Davide Italiano786d8e32016-09-29 00:40:08 +0000672static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
673 switch (GvVisibility) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000674 case GlobalValue::DefaultVisibility:
675 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000676 case GlobalValue::HiddenVisibility:
677 return STV_HIDDEN;
678 case GlobalValue::ProtectedVisibility:
679 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000680 }
George Rimar777f9632016-03-12 08:31:34 +0000681 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000682}
683
Peter Collingbourne4f952702016-05-01 04:55:03 +0000684template <class ELFT>
Davide Italiano786d8e32016-09-29 00:40:08 +0000685static Symbol *createBitcodeSymbol(const DenseSet<const Comdat *> &KeptComdats,
686 const lto::InputFile::Symbol &ObjSym,
687 StringSaver &Saver, BitcodeFile *F) {
688 StringRef NameRef = Saver.save(ObjSym.getName());
689 uint32_t Flags = ObjSym.getFlags();
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000690 uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000691
Davide Italiano786d8e32016-09-29 00:40:08 +0000692 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
693 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
694 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000695
Davide Italiano786d8e32016-09-29 00:40:08 +0000696 if (const Comdat *C = check(ObjSym.getComdat()))
697 if (!KeptComdats.count(C))
698 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
699 CanOmitFromDynSym, F);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000700
Davide Italiano9f8efff2016-04-22 18:26:33 +0000701 if (Flags & BasicSymbolRef::SF_Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000702 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000703 CanOmitFromDynSym, F);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000704
Davide Italiano786d8e32016-09-29 00:40:08 +0000705 if (Flags & BasicSymbolRef::SF_Common)
706 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
707 ObjSym.getCommonAlignment(), Binding,
708 Visibility, STT_OBJECT, F);
709
710 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
711 CanOmitFromDynSym, F);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000712}
713
Peter Collingbourne4f952702016-05-01 04:55:03 +0000714template <class ELFT>
Rafael Espindola4de44b72016-03-02 15:43:50 +0000715void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000716 Obj = check(lto::InputFile::create(MB));
Rafael Espindola4de44b72016-03-02 15:43:50 +0000717 DenseSet<const Comdat *> KeptComdats;
Davide Italiano786d8e32016-09-29 00:40:08 +0000718 for (const auto &P : Obj->getComdatSymbolTable()) {
Rafael Espindola4de44b72016-03-02 15:43:50 +0000719 StringRef N = Saver.save(P.first());
720 if (ComdatGroups.insert(N).second)
721 KeptComdats.insert(&P.second);
722 }
723
Davide Italiano786d8e32016-09-29 00:40:08 +0000724 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
725 Symbols.push_back(
726 createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, Saver, this));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000727}
728
Rui Ueyamac4b65062015-10-12 15:31:09 +0000729template <template <class> class T>
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000730static InputFile *createELFFile(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000731 unsigned char Size;
732 unsigned char Endian;
733 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
734 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
George Rimar777f9632016-03-12 08:31:34 +0000735 fatal("invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000736
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000737 InputFile *Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000738 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000739 Obj = new T<ELF32LE>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000740 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000741 Obj = new T<ELF32BE>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000742 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000743 Obj = new T<ELF64LE>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000744 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000745 Obj = new T<ELF64BE>(MB);
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000746 else
747 fatal("invalid file class: " + MB.getBufferIdentifier());
748
749 if (!Config->FirstElf)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000750 Config->FirstElf = Obj;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000751 return Obj;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000752}
753
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000754// Wraps a binary blob with an ELF header and footer
755// so that we can link it as a regular ELF file.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000756template <class ELFT> InputFile *BinaryFile::createELF() {
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000757 // Fill the ELF file header.
Michael J. Spencera9424f32016-09-09 22:08:04 +0000758 ELFCreator<ELFT> ELF(ET_REL, Config->EMachine);
759 auto DataSec = ELF.addSection(".data");
760 DataSec.Header->sh_flags = SHF_ALLOC;
761 DataSec.Header->sh_size = MB.getBufferSize();
762 DataSec.Header->sh_type = SHT_PROGBITS;
763 DataSec.Header->sh_addralign = 8;
764
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000765 // Replace non-alphanumeric characters with '_'.
Michael J. Spencera9424f32016-09-09 22:08:04 +0000766 std::string Filepath = MB.getBufferIdentifier();
767 std::transform(Filepath.begin(), Filepath.end(), Filepath.begin(),
768 [](char C) { return isalnum(C) ? C : '_'; });
Michael J. Spencera9424f32016-09-09 22:08:04 +0000769
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000770 // Add _start, _end and _size symbols.
771 std::string StartSym = "_binary_" + Filepath + "_start";
Michael J. Spencera9424f32016-09-09 22:08:04 +0000772 auto SSym = ELF.addSymbol(StartSym);
773 SSym.Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT);
774 SSym.Sym->st_shndx = DataSec.Index;
775
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000776 std::string EndSym = "_binary_" + Filepath + "_end";
Michael J. Spencera9424f32016-09-09 22:08:04 +0000777 auto ESym = ELF.addSymbol(EndSym);
778 ESym.Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT);
779 ESym.Sym->st_shndx = DataSec.Index;
780 ESym.Sym->st_value = MB.getBufferSize();
781
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000782 std::string SizeSym = "_binary_" + Filepath + "_size";
Michael J. Spencera9424f32016-09-09 22:08:04 +0000783 auto SZSym = ELF.addSymbol(SizeSym);
784 SZSym.Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT);
785 SZSym.Sym->st_shndx = SHN_ABS;
786 SZSym.Sym->st_value = MB.getBufferSize();
787
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000788 // Fix the ELF file layout and write it down to ELFData uint8_t vector.
Michael J. Spencera9424f32016-09-09 22:08:04 +0000789 std::size_t Size = ELF.layout();
790 ELFData.resize(Size);
Michael J. Spencera9424f32016-09-09 22:08:04 +0000791 ELF.write(ELFData.data());
792
Rui Ueyamae364d1f2016-09-29 23:04:50 +0000793 // Fill .data section with actual data.
Michael J. Spencera9424f32016-09-09 22:08:04 +0000794 std::copy(MB.getBufferStart(), MB.getBufferEnd(),
795 ELFData.data() + DataSec.Header->sh_offset);
796
797 return createELFFile<ObjectFile>(MemoryBufferRef(
798 StringRef((char *)ELFData.data(), Size), MB.getBufferIdentifier()));
799}
800
Rui Ueyama4655ea32016-04-08 00:14:55 +0000801static bool isBitcode(MemoryBufferRef MB) {
802 using namespace sys::fs;
803 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
804}
805
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000806InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName) {
807 InputFile *F =
808 isBitcode(MB) ? new BitcodeFile(MB) : createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000809 F->ArchiveName = ArchiveName;
810 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000811}
812
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000813InputFile *elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000814 return createELFFile<SharedFile>(MB);
815}
816
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000817MemoryBufferRef LazyObjectFile::getBuffer() {
818 if (Seen)
819 return MemoryBufferRef();
820 Seen = true;
821 return MB;
822}
823
George Rimar10874f72016-10-03 11:13:55 +0000824template <class ELFT> void LazyObjectFile::parse() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000825 for (StringRef Sym : getSymbols())
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000826 Symtab<ELFT>::X->addLazyObject(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000827}
828
829template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
830 typedef typename ELFT::Shdr Elf_Shdr;
831 typedef typename ELFT::Sym Elf_Sym;
832 typedef typename ELFT::SymRange Elf_Sym_Range;
833
834 const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
835 for (const Elf_Shdr &Sec : Obj.sections()) {
836 if (Sec.sh_type != SHT_SYMTAB)
837 continue;
838 Elf_Sym_Range Syms = Obj.symbols(&Sec);
839 uint32_t FirstNonLocal = Sec.sh_info;
840 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
841 std::vector<StringRef> V;
842 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +0000843 if (Sym.st_shndx != SHN_UNDEF)
844 V.push_back(check(Sym.getName(StringTable)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000845 return V;
846 }
847 return {};
848}
849
850std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
Davide Italiano786d8e32016-09-29 00:40:08 +0000851 std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB));
Rui Ueyamad72dd1f2016-09-29 00:58:10 +0000852 std::vector<StringRef> V;
853 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
854 if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined))
855 V.push_back(Saver.save(Sym.getName()));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000856 return V;
857}
858
Rui Ueyama1f492892016-04-08 20:49:31 +0000859// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000860std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +0000861 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000862 return getBitcodeSymbols();
863
Rui Ueyama4655ea32016-04-08 00:14:55 +0000864 unsigned char Size;
865 unsigned char Endian;
866 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
867 if (Size == ELFCLASS32) {
868 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000869 return getElfSymbols<ELF32LE>();
870 return getElfSymbols<ELF32BE>();
871 }
Rui Ueyama4655ea32016-04-08 00:14:55 +0000872 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000873 return getElfSymbols<ELF64LE>();
874 return getElfSymbols<ELF64BE>();
875}
876
Peter Collingbourne4f952702016-05-01 04:55:03 +0000877template void ArchiveFile::parse<ELF32LE>();
878template void ArchiveFile::parse<ELF32BE>();
879template void ArchiveFile::parse<ELF64LE>();
880template void ArchiveFile::parse<ELF64BE>();
881
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000882template void BitcodeFile::parse<ELF32LE>(DenseSet<StringRef> &);
883template void BitcodeFile::parse<ELF32BE>(DenseSet<StringRef> &);
884template void BitcodeFile::parse<ELF64LE>(DenseSet<StringRef> &);
885template void BitcodeFile::parse<ELF64BE>(DenseSet<StringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000886
887template void LazyObjectFile::parse<ELF32LE>();
888template void LazyObjectFile::parse<ELF32BE>();
889template void LazyObjectFile::parse<ELF64LE>();
890template void LazyObjectFile::parse<ELF64BE>();
891
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000892template class elf::ELFFileBase<ELF32LE>;
893template class elf::ELFFileBase<ELF32BE>;
894template class elf::ELFFileBase<ELF64LE>;
895template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000896
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000897template class elf::ObjectFile<ELF32LE>;
898template class elf::ObjectFile<ELF32BE>;
899template class elf::ObjectFile<ELF64LE>;
900template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000901
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000902template class elf::SharedFile<ELF32LE>;
903template class elf::SharedFile<ELF32BE>;
904template class elf::SharedFile<ELF64LE>;
905template class elf::SharedFile<ELF64BE>;
Michael J. Spencera9424f32016-09-09 22:08:04 +0000906
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000907template InputFile *BinaryFile::createELF<ELF32LE>();
908template InputFile *BinaryFile::createELF<ELF32BE>();
909template InputFile *BinaryFile::createELF<ELF64LE>();
910template InputFile *BinaryFile::createELF<ELF64BE>();