blob: 9589a50f7a5fa96fb7e8a0d91b6685ab17b9b539 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- InputFiles.cpp -----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "InputFiles.h"
Rafael Espindola156f4ee2016-04-28 19:30:41 +000011#include "Driver.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000012#include "Error.h"
Rafael Espindola9d13d042016-02-11 15:24:48 +000013#include "InputSection.h"
George Rimar67e3ff82016-08-12 19:56:57 +000014#include "LinkerScript.h"
Peter Collingbourne4f952702016-05-01 04:55:03 +000015#include "SymbolTable.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "Symbols.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000017#include "llvm/ADT/STLExtras.h"
Davide Italiano60976ba2016-06-29 06:12:39 +000018#include "llvm/Bitcode/ReaderWriter.h"
Rafael Espindola4d480ed2016-04-21 21:44:25 +000019#include "llvm/CodeGen/Analysis.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000020#include "llvm/IR/LLVMContext.h"
Rafael Espindola4de44b72016-03-02 15:43:50 +000021#include "llvm/IR/Module.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000022#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
Michael J. Spencer1b348a62015-09-04 22:28:10 +000024using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025using namespace llvm::ELF;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000026using namespace llvm::object;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000027using namespace llvm::sys::fs;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
29using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000030using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
Rafael Espindola78db5a92016-05-09 21:40:06 +000032// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Rui Ueyama429ef2a2016-07-15 20:38:28 +000033std::string elf::getFilename(const InputFile *F) {
Rafael Espindola78db5a92016-05-09 21:40:06 +000034 if (!F)
35 return "(internal)";
36 if (!F->ArchiveName.empty())
37 return (F->ArchiveName + "(" + F->getName() + ")").str();
38 return F->getName();
39}
40
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000041template <class ELFT>
42static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000043 std::error_code EC;
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000044 ELFFile<ELFT> F(MB.getBuffer(), EC);
Rui Ueyamaf8292e92016-07-15 02:01:03 +000045 if (EC)
46 error(EC, "failed to read " + MB.getBufferIdentifier());
Rui Ueyamaeb3413e2016-03-03 06:22:29 +000047 return F;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000048}
49
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000050template <class ELFT> static ELFKind getELFKind() {
Rui Ueyamaf588ac42016-01-06 00:09:41 +000051 if (ELFT::TargetEndianness == support::little)
52 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
53 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
Rui Ueyama2022e812015-11-20 02:10:52 +000054}
55
56template <class ELFT>
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000057ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
58 : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {
59 EKind = getELFKind<ELFT>();
60 EMachine = ELFObj.getHeader()->e_machine;
61}
62
63template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000064typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
Rafael Espindola18173d42015-09-08 15:50:05 +000065 if (!Symtab)
66 return Elf_Sym_Range(nullptr, nullptr);
Rafael Espindolae1901cc2015-09-24 15:11:50 +000067 Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
Rafael Espindola18173d42015-09-08 15:50:05 +000068 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
69 uint32_t FirstNonLocal = Symtab->sh_info;
70 if (FirstNonLocal > NumSymbols)
Rui Ueyama429ef2a2016-07-15 20:38:28 +000071 fatal(getFilename(this) + ": invalid sh_info in symbol table");
Rafael Espindola67d72c02016-03-11 12:06:30 +000072
73 if (OnlyGlobals)
Rafael Espindola0f7ccc32016-04-05 14:47:28 +000074 return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
75 return makeArrayRef(Syms.begin(), Syms.end());
Davide Italiano6d328d32015-09-16 20:45:57 +000076}
77
Rafael Espindola115f0f32015-11-03 14:13:40 +000078template <class ELFT>
79uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000080 uint32_t I = Sym.st_shndx;
81 if (I == ELF::SHN_XINDEX)
Rui Ueyamae69ab102016-01-06 01:14:11 +000082 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
Rafael Espindola972b2362016-03-09 14:31:18 +000083 if (I >= ELF::SHN_LORESERVE)
Rafael Espindola115f0f32015-11-03 14:13:40 +000084 return 0;
Rui Ueyamadc8d3a22015-12-24 08:36:56 +000085 return I;
Rafael Espindola115f0f32015-11-03 14:13:40 +000086}
87
Rafael Espindolaaf707642015-10-12 01:55:32 +000088template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
Rafael Espindola3e603792015-10-01 20:26:37 +000089 if (!Symtab)
90 return;
Rafael Espindola75714f62016-03-03 22:24:39 +000091 StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000092}
93
94template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +000095elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
Rafael Espindola2a4b2712015-10-13 01:17:02 +000096 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
Rafael Espindolae1901cc2015-09-24 15:11:50 +000097
98template <class ELFT>
Rafael Espindola67d72c02016-03-11 12:06:30 +000099ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
100 if (!this->Symtab)
101 return this->SymbolBodies;
102 uint32_t FirstNonLocal = this->Symtab->sh_info;
103 return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
104}
105
106template <class ELFT>
107ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
108 if (!this->Symtab)
109 return this->SymbolBodies;
110 uint32_t FirstNonLocal = this->Symtab->sh_info;
111 return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
112}
113
114template <class ELFT>
115ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
116 if (!this->Symtab)
117 return this->SymbolBodies;
118 return makeArrayRef(this->SymbolBodies).slice(1);
Rafael Espindola18173d42015-09-08 15:50:05 +0000119}
120
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000121template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000122 if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo)
123 return MipsOptions->Reginfo->ri_gp_value;
124 if (!ELFT::Is64Bits && MipsReginfo && MipsReginfo->Reginfo)
Rui Ueyama70eed362016-01-06 22:42:43 +0000125 return MipsReginfo->Reginfo->ri_gp_value;
126 return 0;
Simon Atanasyan57830b62015-12-25 13:02:13 +0000127}
128
Rafael Espindola444576d2015-10-09 19:25:07 +0000129template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000130void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000131 // Read section and symbol tables.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000132 initializeSections(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000133 initializeSymbols();
134}
135
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000136// Sections with SHT_GROUP and comdat bits define comdat section groups.
137// They are identified and deduplicated by group name. This function
138// returns a group name.
Rafael Espindola444576d2015-10-09 19:25:07 +0000139template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000140StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000141 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama188d2c32016-07-15 20:05:05 +0000142 const Elf_Shdr *Symtab = check(Obj.getSection(Sec.sh_link));
143 const Elf_Sym *Sym = Obj.getSymbol(Symtab, Sec.sh_info);
144 StringRef Strtab = check(Obj.getStringTableForSymtab(*Symtab));
145 return check(Sym->getName(Strtab));
Rafael Espindola444576d2015-10-09 19:25:07 +0000146}
147
148template <class ELFT>
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000149ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000150elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000151 const ELFFile<ELFT> &Obj = this->ELFObj;
Rui Ueyama368e1ea2016-03-13 22:02:04 +0000152 ArrayRef<Elf_Word> Entries =
153 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
Rafael Espindola444576d2015-10-09 19:25:07 +0000154 if (Entries.empty() || Entries[0] != GRP_COMDAT)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000155 fatal(getFilename(this) + ": unsupported SHT_GROUP format");
Rafael Espindola444576d2015-10-09 19:25:07 +0000156 return Entries.slice(1);
157}
158
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000159template <class ELFT>
160bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
Rui Ueyamafb6d4992016-04-29 16:12:29 +0000161 // We don't merge sections if -O0 (default is -O1). This makes sometimes
162 // the linker significantly faster, although the output will be bigger.
163 if (Config->Optimize == 0)
164 return false;
165
George Rimar67e3ff82016-08-12 19:56:57 +0000166 // We don't merge if linker script has SECTIONS command. When script
167 // do layout it can merge several sections with different attributes
168 // into single output sections. We currently do not support adding
169 // mergeable input sections to regular output ones as well as adding
170 // regular input sections to mergeable output.
171 if (ScriptConfig->HasContents)
172 return false;
173
Rui Ueyama3ebc71e2016-08-03 05:28:02 +0000174 // A mergeable section with size 0 is useless because they don't have
175 // any data to merge. A mergeable string section with size 0 can be
176 // argued as invalid because it doesn't end with a null character.
177 // We'll avoid a mess by handling them as if they were non-mergeable.
178 if (Sec.sh_size == 0)
179 return false;
180
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000181 uintX_t Flags = Sec.sh_flags;
182 if (!(Flags & SHF_MERGE))
183 return false;
184 if (Flags & SHF_WRITE)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000185 fatal(getFilename(this) + ": writable SHF_MERGE section is not supported");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000186 uintX_t EntSize = Sec.sh_entsize;
Rafael Espindola0d2ad422016-03-21 14:57:20 +0000187 if (!EntSize || Sec.sh_size % EntSize)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000188 fatal(getFilename(this) +
189 ": SHF_MERGE section size must be a multiple of sh_entsize");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000190
Peter Smith4df2e142016-05-18 11:40:16 +0000191 // Don't try to merge if the alignment is larger than the sh_entsize and this
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000192 // is not SHF_STRINGS.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000193 //
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000194 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
195 // It would be equivalent for the producer of the .o to just set a larger
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000196 // sh_entsize.
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000197 if (Flags & SHF_STRINGS)
198 return true;
199
George Rimardcddfb62016-06-08 12:04:59 +0000200 return Sec.sh_addralign <= EntSize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000201}
202
203template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000204void elf::ObjectFile<ELFT>::initializeSections(
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000205 DenseSet<StringRef> &ComdatGroups) {
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000206 uint64_t Size = this->ELFObj.getNumSections();
Rafael Espindola71675852015-09-22 00:16:19 +0000207 Sections.resize(Size);
Rafael Espindola444576d2015-10-09 19:25:07 +0000208 unsigned I = -1;
Rafael Espindolad42f4e52015-10-08 12:02:38 +0000209 const ELFFile<ELFT> &Obj = this->ELFObj;
210 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000211 ++I;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000212 if (Sections[I] == &InputSection<ELFT>::Discarded)
Rafael Espindola444576d2015-10-09 19:25:07 +0000213 continue;
214
Rafael Espindolacde25132015-08-13 14:45:44 +0000215 switch (Sec.sh_type) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000216 case SHT_GROUP:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000217 Sections[I] = &InputSection<ELFT>::Discarded;
Rui Ueyama52d3b672016-01-06 02:06:33 +0000218 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
Rafael Espindola444576d2015-10-09 19:25:07 +0000219 continue;
Rui Ueyama33b3f212016-01-06 20:30:02 +0000220 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
Rafael Espindola444576d2015-10-09 19:25:07 +0000221 if (SecIndex >= Size)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000222 fatal(getFilename(this) + ": invalid section index in group: " +
223 Twine(SecIndex));
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000224 Sections[SecIndex] = &InputSection<ELFT>::Discarded;
Rafael Espindola444576d2015-10-09 19:25:07 +0000225 }
226 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000227 case SHT_SYMTAB:
Rafael Espindola18173d42015-09-08 15:50:05 +0000228 this->Symtab = &Sec;
Rafael Espindolacde25132015-08-13 14:45:44 +0000229 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000230 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000231 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola20348222015-08-24 21:43:25 +0000232 break;
Rafael Espindolacde25132015-08-13 14:45:44 +0000233 case SHT_STRTAB:
234 case SHT_NULL:
Rafael Espindolacde25132015-08-13 14:45:44 +0000235 break;
Rui Ueyamae79b09a2015-11-21 22:19:32 +0000236 default:
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000237 Sections[I] = createInputSection(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000238 }
239 }
240}
241
Rafael Espindolaf1d598c2016-02-12 21:17:10 +0000242template <class ELFT>
243InputSectionBase<ELFT> *
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000244elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
245 uint32_t Idx = Sec.sh_info;
246 if (Idx >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000247 fatal(getFilename(this) + ": invalid relocated section index: " +
248 Twine(Idx));
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000249 InputSectionBase<ELFT> *Target = Sections[Idx];
250
251 // Strictly speaking, a relocation section must be included in the
252 // group of the section it relocates. However, LLVM 3.3 and earlier
253 // would fail to do so, so we gracefully handle that case.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000254 if (Target == &InputSection<ELFT>::Discarded)
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000255 return nullptr;
256
257 if (!Target)
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000258 fatal(getFilename(this) + ": unsupported relocation reference");
Rui Ueyamae270c0a2016-03-13 21:52:57 +0000259 return Target;
260}
261
262template <class ELFT>
263InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000264elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
Rafael Espindola75714f62016-03-03 22:24:39 +0000265 StringRef Name = check(this->ELFObj.getSectionName(&Sec));
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000266
Rafael Espindola042a3f22016-09-08 14:06:08 +0000267 switch (Sec.sh_type) {
268 case SHT_ARM_ATTRIBUTES:
269 // FIXME: ARM meta-data section. At present attributes are ignored,
270 // they can be used to reason about object compatibility.
271 return &InputSection<ELFT>::Discarded;
272 case SHT_MIPS_REGINFO:
273 MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec, Name));
274 return MipsReginfo.get();
275 case SHT_MIPS_OPTIONS:
276 MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec, Name));
277 return MipsOptions.get();
278 case SHT_MIPS_ABIFLAGS:
279 MipsAbiFlags.reset(new MipsAbiFlagsInputSection<ELFT>(this, &Sec, Name));
280 return MipsAbiFlags.get();
281 case SHT_RELA:
282 case SHT_REL: {
283 // This section contains relocation information.
284 // If -r is given, we do not interpret or apply relocation
285 // but just copy relocation sections to output.
286 if (Config->Relocatable)
287 return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec, Name);
288
289 // Find the relocation target section and associate this
290 // section with it.
291 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
292 if (!Target)
293 return nullptr;
294 if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
295 S->RelocSections.push_back(&Sec);
296 return nullptr;
297 }
298 if (auto *S = dyn_cast<EhInputSection<ELFT>>(Target)) {
299 if (S->RelocSection)
300 fatal(getFilename(this) +
301 ": multiple relocation sections to .eh_frame are not supported");
302 S->RelocSection = &Sec;
303 return nullptr;
304 }
305 fatal(getFilename(this) +
306 ": relocations pointing to SHF_MERGE are not supported");
307 }
308 }
309
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000310 // .note.GNU-stack is a marker section to control the presence of
311 // PT_GNU_STACK segment in outputs. Since the presence of the segment
312 // is controlled only by the command line option (-z execstack) in LLD,
313 // .note.GNU-stack is ignored.
314 if (Name == ".note.GNU-stack")
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000315 return &InputSection<ELFT>::Discarded;
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000316
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000317 if (Name == ".note.GNU-split-stack") {
George Rimar777f9632016-03-12 08:31:34 +0000318 error("objects using splitstacks are not supported");
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000319 return &InputSection<ELFT>::Discarded;
320 }
321
George Rimarf21aade2016-08-31 08:38:11 +0000322 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000323 return &InputSection<ELFT>::Discarded;
George Rimar3c45ed22016-03-09 18:01:45 +0000324
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000325 // The linker merges EH (exception handling) frames and creates a
326 // .eh_frame_hdr section for runtime. So we handle them with a special
327 // class. For relocatable outputs, they are just passed through.
328 if (Name == ".eh_frame" && !Config->Relocatable)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000329 return new (EHAlloc.Allocate()) EhInputSection<ELFT>(this, &Sec, Name);
Rui Ueyamaeba9b632016-07-15 04:57:44 +0000330
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000331 if (shouldMerge(Sec))
Rafael Espindola042a3f22016-09-08 14:06:08 +0000332 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec, Name);
333 return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec, Name);
Rui Ueyama3f11c8c2015-12-24 08:41:12 +0000334}
335
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000336template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000337 this->initStringTable();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000338 Elf_Sym_Range Syms = this->getElfSymbols(false);
Reid Klecknerf7b85e02015-08-11 20:06:51 +0000339 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
Rui Ueyamae69ab102016-01-06 01:14:11 +0000340 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +0000341 for (const Elf_Sym &Sym : Syms)
Rui Ueyamac5e372d2016-01-21 02:10:12 +0000342 SymbolBodies.push_back(createSymbolBody(&Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000343}
344
345template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000346InputSectionBase<ELFT> *
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000347elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000348 uint32_t Index = this->getSectionIndex(Sym);
349 if (Index == 0)
Rafael Espindola4cda5812015-10-16 15:29:48 +0000350 return nullptr;
George Rimar683a35d2016-08-12 19:25:54 +0000351 if (Index >= Sections.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000352 fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
Rui Ueyama0b289522016-02-25 18:43:51 +0000353 InputSectionBase<ELFT> *S = Sections[Index];
George Rimar683a35d2016-08-12 19:25:54 +0000354 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03
355 // could generate broken objects. STT_SECTION symbols can be
356 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
357 // In this case it is fine for section to be null here as we
358 // do not allocate sections of these types.
359 if (!S || S == &InputSectionBase<ELFT>::Discarded)
Rui Ueyama0b289522016-02-25 18:43:51 +0000360 return S;
361 return S->Repl;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000362}
363
364template <class ELFT>
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000365SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000366 int Binding = Sym->getBinding();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000367 InputSectionBase<ELFT> *Sec = getSection(*Sym);
368 if (Binding == STB_LOCAL) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000369 if (Sym->st_shndx == SHN_UNDEF)
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000370 return new (this->Alloc)
Rui Ueyama434b5612016-07-17 03:11:46 +0000371 Undefined(Sym->st_name, Sym->st_other, Sym->getType(), this);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000372 return new (this->Alloc) DefinedRegular<ELFT>(*Sym, Sec);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000373 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000374
Rafael Espindola75714f62016-03-03 22:24:39 +0000375 StringRef Name = check(Sym->getName(this->StringTable));
Rafael Espindola20348222015-08-24 21:43:25 +0000376
Rafael Espindola4cda5812015-10-16 15:29:48 +0000377 switch (Sym->st_shndx) {
Rafael Espindola51d46902015-08-28 21:26:51 +0000378 case SHN_UNDEF:
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000379 return elf::Symtab<ELFT>::X
Rafael Espindolacc70da32016-06-15 17:56:10 +0000380 ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(),
Davide Italiano35af5b32016-08-30 20:15:03 +0000381 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
382 this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000383 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000384 case SHN_COMMON:
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000385 return elf::Symtab<ELFT>::X
Peter Collingbourne4f952702016-05-01 04:55:03 +0000386 ->addCommon(Name, Sym->st_size, Sym->st_value, Binding, Sym->st_other,
Davide Italiano35af5b32016-08-30 20:15:03 +0000387 Sym->getType(), /*HasUnnamedAddr*/ false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000388 ->body();
Rafael Espindola51d46902015-08-28 21:26:51 +0000389 }
Rafael Espindola20348222015-08-24 21:43:25 +0000390
Rafael Espindola67d72c02016-03-11 12:06:30 +0000391 switch (Binding) {
Rafael Espindolab13df652015-08-11 17:33:02 +0000392 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000393 fatal(getFilename(this) + ": unexpected binding: " + Twine(Binding));
Rafael Espindolab13df652015-08-11 17:33:02 +0000394 case STB_GLOBAL:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000395 case STB_WEAK:
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000396 case STB_GNU_UNIQUE:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000397 if (Sec == &InputSection<ELFT>::Discarded)
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000398 return elf::Symtab<ELFT>::X
Rafael Espindolacc70da32016-06-15 17:56:10 +0000399 ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(),
Davide Italiano35af5b32016-08-30 20:15:03 +0000400 /*CanOmitFromDynSym*/ false,
401 /*HasUnnamedAddr*/ false, this)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000402 ->body();
Peter Collingbourne3db410e2016-05-01 06:00:09 +0000403 return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
Rafael Espindola444576d2015-10-09 19:25:07 +0000404 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000405}
406
Peter Collingbourne4f952702016-05-01 04:55:03 +0000407template <class ELFT> void ArchiveFile::parse() {
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000408 File = check(Archive::create(MB), "failed to parse archive");
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000409
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000410 // Read the symbol table to construct Lazy objects.
411 for (const Archive::Symbol &Sym : File->symbols())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000412 Symtab<ELFT>::X->addLazyArchive(this, Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000413}
414
415// Returns a buffer pointing to a member file containing a given symbol.
416MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
Rafael Espindola1130935c2016-03-03 16:21:44 +0000417 Archive::Child C =
Rafael Espindola75714f62016-03-03 22:24:39 +0000418 check(Sym->getMember(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +0000419 "could not get the member for symbol " + Sym->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000420
Rafael Espindola8f3a6ae2015-11-05 14:40:28 +0000421 if (!Seen.insert(C.getChildOffset()).second)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000422 return MemoryBufferRef();
Michael J. Spencer88f0d632015-09-08 20:36:20 +0000423
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000424 MemoryBufferRef Ret =
425 check(C.getMemoryBufferRef(),
426 "could not get the buffer for the member defining symbol " +
427 Sym->getName());
Rafael Espindolad1cbe4d2016-05-02 13:54:10 +0000428
Rui Ueyamafe658772016-05-15 17:10:23 +0000429 if (C.getParent()->isThin() && Driver->Cpio)
430 Driver->Cpio->append(relativeToRoot(check(C.getFullName())),
431 Ret.getBuffer());
Rafael Espindola1dd2b3d2016-05-03 17:30:44 +0000432
433 return Ret;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000434}
435
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000436template <class ELFT>
437SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000438 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000439
Rafael Espindola115f0f32015-11-03 14:13:40 +0000440template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000441const typename ELFT::Shdr *
Rafael Espindola115f0f32015-11-03 14:13:40 +0000442SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
443 uint32_t Index = this->getSectionIndex(Sym);
444 if (Index == 0)
445 return nullptr;
Rafael Espindola75714f62016-03-03 22:24:39 +0000446 return check(this->ELFObj.getSection(Index));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000447}
448
Rui Ueyama7c713312016-01-06 01:56:36 +0000449// Partially parse the shared object file so that we can call
450// getSoName on this object.
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000451template <class ELFT> void SharedFile<ELFT>::parseSoName() {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000452 typedef typename ELFT::Dyn Elf_Dyn;
453 typedef typename ELFT::uint uintX_t;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000454 const Elf_Shdr *DynamicSec = nullptr;
455
456 const ELFFile<ELFT> Obj = this->ELFObj;
457 for (const Elf_Shdr &Sec : Obj.sections()) {
Rafael Espindola115f0f32015-11-03 14:13:40 +0000458 switch (Sec.sh_type) {
459 default:
460 continue;
461 case SHT_DYNSYM:
Rafael Espindola18173d42015-09-08 15:50:05 +0000462 this->Symtab = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000463 break;
464 case SHT_DYNAMIC:
Rafael Espindolac8b15812015-10-01 15:47:50 +0000465 DynamicSec = &Sec;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000466 break;
Rafael Espindola1130935c2016-03-03 16:21:44 +0000467 case SHT_SYMTAB_SHNDX:
Rafael Espindola75714f62016-03-03 22:24:39 +0000468 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
Rafael Espindola115f0f32015-11-03 14:13:40 +0000469 break;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000470 case SHT_GNU_versym:
471 this->VersymSec = &Sec;
472 break;
473 case SHT_GNU_verdef:
474 this->VerdefSec = &Sec;
475 break;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000476 }
Rafael Espindolac8b15812015-10-01 15:47:50 +0000477 }
478
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000479 this->initStringTable();
Rui Ueyamae69ab102016-01-06 01:14:11 +0000480 SoName = this->getName();
Rafael Espindolac8b15812015-10-01 15:47:50 +0000481
Rui Ueyama361d8b92015-10-12 15:49:02 +0000482 if (!DynamicSec)
483 return;
484 auto *Begin =
485 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
486 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
Rafael Espindolac8b15812015-10-01 15:47:50 +0000487
Rui Ueyama361d8b92015-10-12 15:49:02 +0000488 for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
489 if (Dyn.d_tag == DT_SONAME) {
490 uintX_t Val = Dyn.getVal();
491 if (Val >= this->StringTable.size())
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000492 fatal(getFilename(this) + ": invalid DT_SONAME entry");
Rui Ueyamae69ab102016-01-06 01:14:11 +0000493 SoName = StringRef(this->StringTable.data() + Val);
Rui Ueyama361d8b92015-10-12 15:49:02 +0000494 return;
Rafael Espindola18173d42015-09-08 15:50:05 +0000495 }
496 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000497}
Rafael Espindola18173d42015-09-08 15:50:05 +0000498
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000499// Parse the version definitions in the object file if present. Returns a vector
500// whose nth element contains a pointer to the Elf_Verdef for version identifier
501// n. Version identifiers that are not definitions map to nullptr. The array
502// always has at least length 1.
503template <class ELFT>
504std::vector<const typename ELFT::Verdef *>
505SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
506 std::vector<const Elf_Verdef *> Verdefs(1);
507 // We only need to process symbol versions for this DSO if it has both a
508 // versym and a verdef section, which indicates that the DSO contains symbol
509 // version definitions.
510 if (!VersymSec || !VerdefSec)
511 return Verdefs;
512
513 // The location of the first global versym entry.
514 Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
515 VersymSec->sh_offset) +
516 this->Symtab->sh_info;
517
518 // We cannot determine the largest verdef identifier without inspecting
519 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
520 // sequentially starting from 1, so we predict that the largest identifier
521 // will be VerdefCount.
522 unsigned VerdefCount = VerdefSec->sh_info;
523 Verdefs.resize(VerdefCount + 1);
524
525 // Build the Verdefs array by following the chain of Elf_Verdef objects
526 // from the start of the .gnu.version_d section.
527 const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
528 for (unsigned I = 0; I != VerdefCount; ++I) {
529 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
530 Verdef += CurVerdef->vd_next;
531 unsigned VerdefIndex = CurVerdef->vd_ndx;
532 if (Verdefs.size() <= VerdefIndex)
533 Verdefs.resize(VerdefIndex + 1);
534 Verdefs[VerdefIndex] = CurVerdef;
535 }
536
537 return Verdefs;
538}
539
Rui Ueyama7c713312016-01-06 01:56:36 +0000540// Fully parse the shared object file. This must be called after parseSoName().
541template <class ELFT> void SharedFile<ELFT>::parseRest() {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000542 // Create mapping from version identifiers to Elf_Verdef entries.
543 const Elf_Versym *Versym = nullptr;
544 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
545
Rafael Espindola67d72c02016-03-11 12:06:30 +0000546 Elf_Sym_Range Syms = this->getElfSymbols(true);
Rafael Espindola18173d42015-09-08 15:50:05 +0000547 for (const Elf_Sym &Sym : Syms) {
Rafael Espindolafb4f2fe2016-04-29 17:46:07 +0000548 unsigned VersymIndex = 0;
549 if (Versym) {
550 VersymIndex = Versym->vs_index;
551 ++Versym;
552 }
553
Rafael Espindola18da0e52016-04-29 16:23:31 +0000554 StringRef Name = check(Sym.getName(this->StringTable));
555 if (Sym.isUndefined()) {
556 Undefs.push_back(Name);
557 continue;
558 }
559
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000560 if (Versym) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000561 // Ignore local symbols and non-default versions.
Rafael Espindolad2454d62016-06-09 15:45:49 +0000562 if (VersymIndex == VER_NDX_LOCAL || (VersymIndex & VERSYM_HIDDEN))
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000563 continue;
564 }
Rafael Espindolad2454d62016-06-09 15:45:49 +0000565
566 const Elf_Verdef *V =
567 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
568 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
Rafael Espindola18173d42015-09-08 15:50:05 +0000569 }
570}
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000571
Rui Ueyama80356882016-08-03 20:33:17 +0000572static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000573 Triple T(getBitcodeTargetTriple(MB, Driver->Context));
574 if (T.isLittleEndian())
575 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
576 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
Davide Italiano60976ba2016-06-29 06:12:39 +0000577}
578
Rui Ueyama80356882016-08-03 20:33:17 +0000579static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000580 Triple T(getBitcodeTargetTriple(MB, Driver->Context));
581 switch (T.getArch()) {
Rui Ueyama523744d2016-07-07 02:46:30 +0000582 case Triple::aarch64:
583 return EM_AARCH64;
584 case Triple::arm:
585 return EM_ARM;
586 case Triple::mips:
587 case Triple::mipsel:
588 case Triple::mips64:
589 case Triple::mips64el:
590 return EM_MIPS;
591 case Triple::ppc:
592 return EM_PPC;
593 case Triple::ppc64:
594 return EM_PPC64;
595 case Triple::x86:
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000596 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
Rui Ueyama523744d2016-07-07 02:46:30 +0000597 case Triple::x86_64:
598 return EM_X86_64;
599 default:
Rui Ueyama429ef2a2016-07-15 20:38:28 +0000600 fatal(MB.getBufferIdentifier() +
Rui Ueyama7fdb4382016-08-03 20:25:29 +0000601 ": could not infer e_machine from bitcode target triple " + T.str());
Davide Italiano60976ba2016-06-29 06:12:39 +0000602 }
603}
604
Rui Ueyama523744d2016-07-07 02:46:30 +0000605BitcodeFile::BitcodeFile(MemoryBufferRef MB) : InputFile(BitcodeKind, MB) {
Rui Ueyama80356882016-08-03 20:33:17 +0000606 EKind = getBitcodeELFKind(MB);
607 EMachine = getBitcodeMachineKind(MB);
Davide Italiano60976ba2016-06-29 06:12:39 +0000608}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000609
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000610static uint8_t getGvVisibility(const GlobalValue *GV) {
611 switch (GV->getVisibility()) {
Rui Ueyama68fae232016-03-07 19:06:14 +0000612 case GlobalValue::DefaultVisibility:
613 return STV_DEFAULT;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000614 case GlobalValue::HiddenVisibility:
615 return STV_HIDDEN;
616 case GlobalValue::ProtectedVisibility:
617 return STV_PROTECTED;
Rui Ueyamafd4fee52016-03-07 00:54:17 +0000618 }
George Rimar777f9632016-03-12 08:31:34 +0000619 llvm_unreachable("unknown visibility");
Rui Ueyamaf7149552016-03-11 18:46:51 +0000620}
621
Peter Collingbourne4f952702016-05-01 04:55:03 +0000622template <class ELFT>
623Symbol *BitcodeFile::createSymbol(const DenseSet<const Comdat *> &KeptComdats,
624 const IRObjectFile &Obj,
625 const BasicSymbolRef &Sym) {
626 const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
627
Davide Italiano9f8efff2016-04-22 18:26:33 +0000628 SmallString<64> Name;
629 raw_svector_ostream OS(Name);
630 Sym.printName(OS);
631 StringRef NameRef = Saver.save(StringRef(Name));
Rui Ueyamaf7149552016-03-11 18:46:51 +0000632
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000633 uint32_t Flags = Sym.getFlags();
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000634 uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
Davide Italiano9f8efff2016-04-22 18:26:33 +0000635
Peter Collingbourne4f952702016-05-01 04:55:03 +0000636 uint8_t Type = STT_NOTYPE;
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000637 uint8_t Visibility;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000638 bool CanOmitFromDynSym = false;
Davide Italiano29fa6ab2016-08-31 12:27:47 +0000639 bool HasUnnamedAddr = false;
640
Peter Collingbourne4f952702016-05-01 04:55:03 +0000641 // FIXME: Expose a thread-local flag for module asm symbols.
642 if (GV) {
643 if (GV->isThreadLocal())
644 Type = STT_TLS;
645 CanOmitFromDynSym = canBeOmittedFromSymbolTable(GV);
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000646 Visibility = getGvVisibility(GV);
Davide Italiano35af5b32016-08-30 20:15:03 +0000647 HasUnnamedAddr =
648 GV->getUnnamedAddr() == llvm::GlobalValue::UnnamedAddr::Global;
649 } else {
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000650 // FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose
651 // protected visibility.
652 Visibility = STV_DEFAULT;
Davide Italiano35af5b32016-08-30 20:15:03 +0000653 }
Rui Ueyamaf7149552016-03-11 18:46:51 +0000654
Davide Italiano9f8efff2016-04-22 18:26:33 +0000655 if (GV)
656 if (const Comdat *C = GV->getComdat())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000657 if (!KeptComdats.count(C))
658 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000659 CanOmitFromDynSym, HasUnnamedAddr,
660 this);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000661
662 const Module &M = Obj.getModule();
Davide Italiano9f8efff2016-04-22 18:26:33 +0000663 if (Flags & BasicSymbolRef::SF_Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000664 return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000665 CanOmitFromDynSym, HasUnnamedAddr,
666 this);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000667 if (Flags & BasicSymbolRef::SF_Common) {
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +0000668 // FIXME: Set SF_Common flag correctly for module asm symbols, and expose
669 // size and alignment.
670 assert(GV);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000671 const DataLayout &DL = M.getDataLayout();
672 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000673 return Symtab<ELFT>::X->addCommon(NameRef, Size, GV->getAlignment(),
Davide Italiano35af5b32016-08-30 20:15:03 +0000674 Binding, Visibility, STT_OBJECT,
675 HasUnnamedAddr, this);
Rui Ueyamaf7149552016-03-11 18:46:51 +0000676 }
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000677 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000678 CanOmitFromDynSym, HasUnnamedAddr, this);
Davide Italiano9f8efff2016-04-22 18:26:33 +0000679}
680
Peter Collingbourne4f952702016-05-01 04:55:03 +0000681bool BitcodeFile::shouldSkip(uint32_t Flags) {
Rui Ueyamad5738442016-07-02 06:08:44 +0000682 return !(Flags & BasicSymbolRef::SF_Global) ||
683 (Flags & BasicSymbolRef::SF_FormatSpecific);
Rafael Espindola9b3acf92016-03-11 16:11:47 +0000684}
685
Peter Collingbourne4f952702016-05-01 04:55:03 +0000686template <class ELFT>
Rafael Espindola4de44b72016-03-02 15:43:50 +0000687void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
Rafael Espindola156f4ee2016-04-28 19:30:41 +0000688 Obj = check(IRObjectFile::create(MB, Driver->Context));
Rafael Espindola1130935c2016-03-03 16:21:44 +0000689 const Module &M = Obj->getModule();
Rafael Espindola4de44b72016-03-02 15:43:50 +0000690
691 DenseSet<const Comdat *> KeptComdats;
692 for (const auto &P : M.getComdatSymbolTable()) {
693 StringRef N = Saver.save(P.first());
694 if (ComdatGroups.insert(N).second)
695 KeptComdats.insert(&P.second);
696 }
697
Rui Ueyamaf7149552016-03-11 18:46:51 +0000698 for (const BasicSymbolRef &Sym : Obj->symbols())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000699 if (!shouldSkip(Sym.getFlags()))
700 Symbols.push_back(createSymbol<ELFT>(KeptComdats, *Obj, Sym));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000701}
702
Rui Ueyamac4b65062015-10-12 15:31:09 +0000703template <template <class> class T>
Rui Ueyama533c0302016-01-06 00:09:43 +0000704static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
Rui Ueyama57bbdaf2016-04-08 00:18:25 +0000705 unsigned char Size;
706 unsigned char Endian;
707 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
708 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
George Rimar777f9632016-03-12 08:31:34 +0000709 fatal("invalid data encoding: " + MB.getBufferIdentifier());
Rui Ueyamac4b65062015-10-12 15:31:09 +0000710
Rui Ueyama5e64d3f2016-06-29 01:30:50 +0000711 std::unique_ptr<InputFile> Obj;
712 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
713 Obj.reset(new T<ELF32LE>(MB));
714 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
715 Obj.reset(new T<ELF32BE>(MB));
716 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
717 Obj.reset(new T<ELF64LE>(MB));
718 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
719 Obj.reset(new T<ELF64BE>(MB));
720 else
721 fatal("invalid file class: " + MB.getBufferIdentifier());
722
723 if (!Config->FirstElf)
724 Config->FirstElf = Obj.get();
725 return Obj;
Rui Ueyamac4b65062015-10-12 15:31:09 +0000726}
727
Rui Ueyama4655ea32016-04-08 00:14:55 +0000728static bool isBitcode(MemoryBufferRef MB) {
729 using namespace sys::fs;
730 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
731}
732
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000733std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
734 StringRef ArchiveName) {
Rui Ueyamac89bff22016-02-23 18:17:11 +0000735 std::unique_ptr<InputFile> F;
Rui Ueyama4655ea32016-04-08 00:14:55 +0000736 if (isBitcode(MB))
Rui Ueyamac89bff22016-02-23 18:17:11 +0000737 F.reset(new BitcodeFile(MB));
738 else
739 F = createELFFile<ObjectFile>(MB);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000740 F->ArchiveName = ArchiveName;
741 return F;
Rui Ueyama533c0302016-01-06 00:09:43 +0000742}
743
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000744std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
Rui Ueyama533c0302016-01-06 00:09:43 +0000745 return createELFFile<SharedFile>(MB);
746}
747
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000748MemoryBufferRef LazyObjectFile::getBuffer() {
749 if (Seen)
750 return MemoryBufferRef();
751 Seen = true;
752 return MB;
753}
754
Peter Collingbourne4f952702016-05-01 04:55:03 +0000755template <class ELFT>
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000756void LazyObjectFile::parse() {
757 for (StringRef Sym : getSymbols())
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000758 Symtab<ELFT>::X->addLazyObject(Sym, *this);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000759}
760
761template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
762 typedef typename ELFT::Shdr Elf_Shdr;
763 typedef typename ELFT::Sym Elf_Sym;
764 typedef typename ELFT::SymRange Elf_Sym_Range;
765
766 const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
767 for (const Elf_Shdr &Sec : Obj.sections()) {
768 if (Sec.sh_type != SHT_SYMTAB)
769 continue;
770 Elf_Sym_Range Syms = Obj.symbols(&Sec);
771 uint32_t FirstNonLocal = Sec.sh_info;
772 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
773 std::vector<StringRef> V;
774 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
Rui Ueyama1f492892016-04-08 20:49:31 +0000775 if (Sym.st_shndx != SHN_UNDEF)
776 V.push_back(check(Sym.getName(StringTable)));
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000777 return V;
778 }
779 return {};
780}
781
782std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
783 LLVMContext Context;
784 std::unique_ptr<IRObjectFile> Obj =
785 check(IRObjectFile::create(this->MB, Context));
786 std::vector<StringRef> V;
787 for (const BasicSymbolRef &Sym : Obj->symbols()) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000788 uint32_t Flags = Sym.getFlags();
789 if (BitcodeFile::shouldSkip(Flags))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000790 continue;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000791 if (Flags & BasicSymbolRef::SF_Undefined)
Rui Ueyama1f492892016-04-08 20:49:31 +0000792 continue;
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000793 SmallString<64> Name;
794 raw_svector_ostream OS(Name);
795 Sym.printName(OS);
796 V.push_back(Saver.save(StringRef(Name)));
797 }
798 return V;
799}
800
Rui Ueyama1f492892016-04-08 20:49:31 +0000801// Returns a vector of globally-visible defined symbol names.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000802std::vector<StringRef> LazyObjectFile::getSymbols() {
Rui Ueyama4655ea32016-04-08 00:14:55 +0000803 if (isBitcode(this->MB))
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000804 return getBitcodeSymbols();
805
Rui Ueyama4655ea32016-04-08 00:14:55 +0000806 unsigned char Size;
807 unsigned char Endian;
808 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
809 if (Size == ELFCLASS32) {
810 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000811 return getElfSymbols<ELF32LE>();
812 return getElfSymbols<ELF32BE>();
813 }
Rui Ueyama4655ea32016-04-08 00:14:55 +0000814 if (Endian == ELFDATA2LSB)
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000815 return getElfSymbols<ELF64LE>();
816 return getElfSymbols<ELF64BE>();
817}
818
Peter Collingbourne4f952702016-05-01 04:55:03 +0000819template void ArchiveFile::parse<ELF32LE>();
820template void ArchiveFile::parse<ELF32BE>();
821template void ArchiveFile::parse<ELF64LE>();
822template void ArchiveFile::parse<ELF64BE>();
823
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000824template void BitcodeFile::parse<ELF32LE>(DenseSet<StringRef> &);
825template void BitcodeFile::parse<ELF32BE>(DenseSet<StringRef> &);
826template void BitcodeFile::parse<ELF64LE>(DenseSet<StringRef> &);
827template void BitcodeFile::parse<ELF64BE>(DenseSet<StringRef> &);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000828
829template void LazyObjectFile::parse<ELF32LE>();
830template void LazyObjectFile::parse<ELF32BE>();
831template void LazyObjectFile::parse<ELF64LE>();
832template void LazyObjectFile::parse<ELF64BE>();
833
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000834template class elf::ELFFileBase<ELF32LE>;
835template class elf::ELFFileBase<ELF32BE>;
836template class elf::ELFFileBase<ELF64LE>;
837template class elf::ELFFileBase<ELF64BE>;
Davide Italiano6d328d32015-09-16 20:45:57 +0000838
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000839template class elf::ObjectFile<ELF32LE>;
840template class elf::ObjectFile<ELF32BE>;
841template class elf::ObjectFile<ELF64LE>;
842template class elf::ObjectFile<ELF64BE>;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000843
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000844template class elf::SharedFile<ELF32LE>;
845template class elf::SharedFile<ELF32BE>;
846template class elf::SharedFile<ELF64LE>;
847template class elf::SharedFile<ELF64BE>;