Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- Writer.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 | |
Michael J. Spencer | f832541 | 2015-09-04 22:48:30 +0000 | [diff] [blame] | 10 | #include "Writer.h" |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 11 | #include "Config.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 12 | #include "OutputSections.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 13 | #include "SymbolTable.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 14 | #include "Target.h" |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 15 | |
Rui Ueyama | 3486fe5 | 2015-10-11 17:44:22 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileOutputBuffer.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvm::ELF; |
| 22 | using namespace llvm::object; |
| 23 | |
| 24 | using namespace lld; |
| 25 | using namespace lld::elf2; |
| 26 | |
Hal Finkel | d962ef0b | 2015-10-13 18:50:28 +0000 | [diff] [blame^] | 27 | // On freebsd x86_64 the first page cannot be mmaped. |
| 28 | // On linux that is controled by vm.mmap_min_addr. At least on some x86_64 |
| 29 | // installs that is 65536, so the first 15 pages cannot be used. |
| 30 | // Given that, the smallest value that can be used in here is 0x10000. |
| 31 | // If using 2MB pages, the smallest page aligned address that works is |
| 32 | // 0x200000, but it looks like every OS uses 4k pages for executables. |
| 33 | // FIXME: This is architecture and OS dependent. |
| 34 | static const int VAStart = 0x10000; |
| 35 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 36 | namespace { |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 37 | |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 38 | static uint32_t toPhdrFlags(uint64_t Flags) { |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 39 | uint32_t Ret = PF_R; |
| 40 | if (Flags & SHF_WRITE) |
| 41 | Ret |= PF_W; |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 42 | if (Flags & SHF_EXECINSTR) |
| 43 | Ret |= PF_X; |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 44 | return Ret; |
| 45 | } |
| 46 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 47 | // The writer writes a SymbolTable result to a file. |
| 48 | template <class ELFT> class Writer { |
| 49 | public: |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 50 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 51 | typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 52 | typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr; |
| 53 | typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr; |
| 54 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 55 | typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 56 | typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 57 | Writer(SymbolTable<ELFT> &S) : Symtab(S) {} |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 58 | void run(); |
| 59 | |
| 60 | private: |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 61 | void copyLocalSymbols(); |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 62 | void createSections(); |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 63 | template <bool isRela> |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 64 | void scanRelocs(const InputSection<ELFT> &C, |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 65 | iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels); |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 66 | void scanRelocs(const InputSection<ELFT> &C); |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 67 | void assignAddresses(); |
| 68 | void openFile(StringRef OutputPath); |
| 69 | void writeHeader(); |
| 70 | void writeSections(); |
Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 71 | bool needsInterpSection() const { |
Rui Ueyama | 0d0bcf7 | 2015-10-07 21:25:39 +0000 | [diff] [blame] | 72 | return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty(); |
Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 73 | } |
Michael J. Spencer | f32446f | 2015-10-06 20:39:09 +0000 | [diff] [blame] | 74 | bool isOutputDynamic() const { |
Rui Ueyama | 0d0bcf7 | 2015-10-07 21:25:39 +0000 | [diff] [blame] | 75 | return !Symtab.getSharedFiles().empty() || Config->Shared; |
Rafael Espindola | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 76 | } |
Hal Finkel | d962ef0b | 2015-10-13 18:50:28 +0000 | [diff] [blame^] | 77 | uintX_t getVAStart() const { return Config->Shared ? 0 : VAStart; } |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 78 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 79 | std::unique_ptr<llvm::FileOutputBuffer> Buffer; |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 80 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 81 | llvm::SpecificBumpPtrAllocator<OutputSection<ELFT>> CAlloc; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 82 | std::vector<OutputSectionBase<ELFT::Is64Bits> *> OutputSections; |
Rafael Espindola | 5f55387 | 2015-09-08 17:39:39 +0000 | [diff] [blame] | 83 | unsigned getNumSections() const { return OutputSections.size() + 1; } |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 84 | |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 85 | void setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags, uintX_t FileOff, |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 86 | uintX_t VA, uintX_t Align); |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 87 | void copyPhdr(Elf_Phdr *PH, OutputSectionBase<ELFT::Is64Bits> *From); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 88 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 89 | SymbolTable<ELFT> &Symtab; |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 90 | std::vector<Elf_Phdr> Phdrs; |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 91 | |
Rafael Espindola | 98f6bd0 | 2015-08-11 23:14:13 +0000 | [diff] [blame] | 92 | uintX_t FileSize; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 93 | uintX_t SectionHeaderOff; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 94 | }; |
| 95 | } // anonymous namespace |
| 96 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 97 | template <class ELFT> void lld::elf2::writeResult(SymbolTable<ELFT> *Symtab) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 98 | // Initialize output sections that are handled by Writer specially. |
| 99 | // Don't reorder because the order of initialization matters. |
| 100 | InterpSection<ELFT::Is64Bits> Interp; |
| 101 | Out<ELFT>::Interp = &Interp; |
| 102 | StringTableSection<ELFT::Is64Bits> StrTab(false); |
| 103 | Out<ELFT>::StrTab = &StrTab; |
| 104 | StringTableSection<ELFT::Is64Bits> DynStrTab(true); |
| 105 | Out<ELFT>::DynStrTab = &DynStrTab; |
| 106 | OutputSection<ELFT> Bss(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); |
| 107 | Out<ELFT>::Bss = &Bss; |
| 108 | GotSection<ELFT> Got; |
| 109 | Out<ELFT>::Got = &Got; |
| 110 | PltSection<ELFT> Plt; |
| 111 | Out<ELFT>::Plt = &Plt; |
| 112 | SymbolTableSection<ELFT> SymTab(*Symtab, *Out<ELFT>::StrTab); |
| 113 | Out<ELFT>::SymTab = &SymTab; |
| 114 | SymbolTableSection<ELFT> DynSymTab(*Symtab, *Out<ELFT>::DynStrTab); |
| 115 | Out<ELFT>::DynSymTab = &DynSymTab; |
| 116 | HashTableSection<ELFT> HashTab; |
| 117 | Out<ELFT>::HashTab = &HashTab; |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 118 | RelocationSection<ELFT> RelaDyn(Symtab->shouldUseRela()); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 119 | Out<ELFT>::RelaDyn = &RelaDyn; |
| 120 | DynamicSection<ELFT> Dynamic(*Symtab); |
| 121 | Out<ELFT>::Dynamic = &Dynamic; |
| 122 | |
Rui Ueyama | 0d0bcf7 | 2015-10-07 21:25:39 +0000 | [diff] [blame] | 123 | Writer<ELFT>(*Symtab).run(); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 126 | // The main function of the writer. |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 127 | template <class ELFT> void Writer<ELFT>::run() { |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 128 | if (!Config->DiscardAll) |
| 129 | copyLocalSymbols(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 130 | createSections(); |
| 131 | assignAddresses(); |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 132 | openFile(Config->OutputFile); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 133 | writeHeader(); |
| 134 | writeSections(); |
| 135 | error(Buffer->commit()); |
| 136 | } |
| 137 | |
Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 138 | namespace { |
| 139 | template <bool Is64Bits> struct SectionKey { |
| 140 | typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; |
| 141 | StringRef Name; |
Rui Ueyama | 0fb1ee0 | 2015-10-02 21:13:19 +0000 | [diff] [blame] | 142 | uint32_t Type; |
| 143 | uintX_t Flags; |
Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 144 | }; |
| 145 | } |
| 146 | namespace llvm { |
| 147 | template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> { |
| 148 | static SectionKey<Is64Bits> getEmptyKey() { |
| 149 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0}; |
| 150 | } |
| 151 | static SectionKey<Is64Bits> getTombstoneKey() { |
| 152 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, |
| 153 | 0}; |
| 154 | } |
| 155 | static unsigned getHashValue(const SectionKey<Is64Bits> &Val) { |
Rui Ueyama | 0fb1ee0 | 2015-10-02 21:13:19 +0000 | [diff] [blame] | 156 | return hash_combine(Val.Name, Val.Type, Val.Flags); |
Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 157 | } |
| 158 | static bool isEqual(const SectionKey<Is64Bits> &LHS, |
| 159 | const SectionKey<Is64Bits> &RHS) { |
| 160 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
Rui Ueyama | 0fb1ee0 | 2015-10-02 21:13:19 +0000 | [diff] [blame] | 161 | LHS.Type == RHS.Type && LHS.Flags == RHS.Flags; |
Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 162 | } |
| 163 | }; |
| 164 | } |
| 165 | |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 166 | // The reason we have to do this early scan is as follows |
| 167 | // * To mmap the output file, we need to know the size |
| 168 | // * For that, we need to know how many dynamic relocs we will have. |
| 169 | // It might be possible to avoid this by outputting the file with write: |
| 170 | // * Write the allocated output sections, computing addresses. |
| 171 | // * Apply relocations, recording which ones require a dynamic reloc. |
| 172 | // * Write the dynamic relocations. |
| 173 | // * Write the rest of the file. |
| 174 | template <class ELFT> |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 175 | template <bool isRela> |
| 176 | void Writer<ELFT>::scanRelocs( |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 177 | const InputSection<ELFT> &C, |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 178 | iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) { |
| 179 | typedef Elf_Rel_Impl<ELFT, isRela> RelType; |
| 180 | const ObjectFile<ELFT> &File = *C.getFile(); |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 181 | bool IsMips64EL = File.getObj().isMips64EL(); |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 182 | for (const RelType &RI : Rels) { |
| 183 | uint32_t SymIndex = RI.getSymbol(IsMips64EL); |
Rafael Espindola | 5c2310c | 2015-09-18 14:40:19 +0000 | [diff] [blame] | 184 | SymbolBody *Body = File.getSymbolBody(SymIndex); |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 185 | uint32_t Type = RI.getType(IsMips64EL); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 186 | |
| 187 | // Set "used" bit for --as-needed. |
| 188 | if (Body && Body->isUndefined() && !Body->isWeak()) |
| 189 | if (auto *S = dyn_cast<SharedSymbol<ELFT>>(Body->repl())) |
| 190 | S->File->IsUsed = true; |
| 191 | |
| 192 | if (Body) |
| 193 | Body = Body->repl(); |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 194 | if (Body) { |
| 195 | if (Target->relocNeedsPlt(Type, *Body)) { |
| 196 | if (Body->isInPlt()) |
| 197 | continue; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 198 | Out<ELFT>::Plt->addEntry(Body); |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 199 | } |
| 200 | if (Target->relocNeedsGot(Type, *Body)) { |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 201 | if (Body->isInGot()) |
| 202 | continue; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 203 | Out<ELFT>::Got->addEntry(Body); |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 204 | } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 205 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 206 | |
| 207 | bool CBP = canBePreempted(Body); |
| 208 | if (!CBP && (!Config->Shared || Target->isRelRelative(Type))) |
| 209 | continue; |
| 210 | if (CBP) |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 211 | Body->setUsedInDynamicReloc(); |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 212 | Out<ELFT>::RelaDyn->addReloc({C, RI}); |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | template <class ELFT> |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 217 | void Writer<ELFT>::scanRelocs(const InputSection<ELFT> &C) { |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 218 | ObjectFile<ELFT> *File = C.getFile(); |
| 219 | ELFFile<ELFT> &EObj = File->getObj(); |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 220 | |
| 221 | if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC)) |
| 222 | return; |
| 223 | |
| 224 | for (const Elf_Shdr *RelSec : C.RelocSections) { |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 225 | if (RelSec->sh_type == SHT_RELA) |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 226 | scanRelocs(C, EObj.relas(RelSec)); |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 227 | else |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 228 | scanRelocs(C, EObj.rels(RelSec)); |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 232 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 233 | static void reportUndefined(const SymbolTable<ELFT> &S, const SymbolBody &Sym) { |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 234 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 235 | typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| 236 | |
George Rimar | 57e40de | 2015-10-01 20:14:45 +0000 | [diff] [blame] | 237 | if (Config->Shared && !Config->NoUndefined) |
George Rimar | ee05828 | 2015-10-01 17:24:24 +0000 | [diff] [blame] | 238 | return; |
| 239 | |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 240 | const Elf_Sym &SymE = cast<ELFSymbolBody<ELFT>>(Sym).Sym; |
Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 241 | ELFFileBase<ELFT> *SymFile = nullptr; |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 242 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 243 | for (const std::unique_ptr<ObjectFile<ELFT>> &File : S.getObjectFiles()) { |
| 244 | Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable()); |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 245 | if (&SymE > Syms.begin() && &SymE < Syms.end()) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 246 | SymFile = File.get(); |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 247 | } |
Rafael Espindola | 551dfd8 | 2015-09-25 19:24:57 +0000 | [diff] [blame] | 248 | |
| 249 | std::string Message = "undefined symbol: " + Sym.getName().str(); |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 250 | if (SymFile) |
Rafael Espindola | 551dfd8 | 2015-09-25 19:24:57 +0000 | [diff] [blame] | 251 | Message += " in " + SymFile->getName().str(); |
| 252 | if (Config->NoInhibitExec) |
| 253 | warning(Message); |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 254 | else |
Rafael Espindola | 551dfd8 | 2015-09-25 19:24:57 +0000 | [diff] [blame] | 255 | error(Message); |
Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 258 | // Local symbols are not in the linker's symbol table. This function scans |
| 259 | // each object file's symbol table to copy local symbols to the output. |
| 260 | template <class ELFT> void Writer<ELFT>::copyLocalSymbols() { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 261 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) { |
| 262 | for (const Elf_Sym &Sym : F->getLocalSymbols()) { |
| 263 | ErrorOr<StringRef> SymNameOrErr = Sym.getName(F->getStringTable()); |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 264 | error(SymNameOrErr); |
| 265 | StringRef SymName = *SymNameOrErr; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 266 | if (!shouldKeepInSymtab<ELFT>(*F, SymName, Sym)) |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 267 | continue; |
| 268 | Out<ELFT>::SymTab->addSymbol(SymName, true); |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 273 | // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that |
| 274 | // we would like to make sure appear is a specific order to maximize their |
| 275 | // coverage by a single signed 16-bit offset from the TOC base pointer. |
| 276 | // Conversely, the special .tocbss section should be first among all SHT_NOBITS |
| 277 | // sections. This will put it next to the loaded special PPC64 sections (and, |
| 278 | // thus, within reach of the TOC base pointer). |
| 279 | static int getPPC64SectionRank(StringRef SectionName) { |
| 280 | return StringSwitch<int>(SectionName) |
| 281 | .Case(".tocbss", 0) |
| 282 | .Case(".branch_lt", 2) |
| 283 | .Case(".toc", 3) |
| 284 | .Case(".toc1", 4) |
| 285 | .Case(".opd", 5) |
| 286 | .Default(1); |
| 287 | } |
| 288 | |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 289 | // Output section ordering is determined by this function. |
| 290 | template <class ELFT> |
| 291 | static bool compareOutputSections(OutputSectionBase<ELFT::Is64Bits> *A, |
| 292 | OutputSectionBase<ELFT::Is64Bits> *B) { |
| 293 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 294 | |
| 295 | uintX_t AFlags = A->getFlags(); |
| 296 | uintX_t BFlags = B->getFlags(); |
| 297 | |
| 298 | // Allocatable sections go first to reduce the total PT_LOAD size and |
| 299 | // so debug info doesn't change addresses in actual code. |
| 300 | bool AIsAlloc = AFlags & SHF_ALLOC; |
| 301 | bool BIsAlloc = BFlags & SHF_ALLOC; |
| 302 | if (AIsAlloc != BIsAlloc) |
| 303 | return AIsAlloc; |
| 304 | |
| 305 | // We don't have any special requirements for the relative order of |
| 306 | // two non allocatable sections. |
| 307 | if (!AIsAlloc) |
| 308 | return false; |
| 309 | |
| 310 | // We want the read only sections first so that they go in the PT_LOAD |
| 311 | // covering the program headers at the start of the file. |
| 312 | bool AIsWritable = AFlags & SHF_WRITE; |
| 313 | bool BIsWritable = BFlags & SHF_WRITE; |
| 314 | if (AIsWritable != BIsWritable) |
| 315 | return BIsWritable; |
| 316 | |
| 317 | // For a corresponding reason, put non exec sections first (the program |
| 318 | // header PT_LOAD is not executable). |
| 319 | bool AIsExec = AFlags & SHF_EXECINSTR; |
| 320 | bool BIsExec = BFlags & SHF_EXECINSTR; |
| 321 | if (AIsExec != BIsExec) |
| 322 | return BIsExec; |
| 323 | |
Hal Finkel | 0d7e83b | 2015-10-13 17:57:46 +0000 | [diff] [blame] | 324 | // If we got here we know that both A and B are in the same PT_LOAD. |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 325 | // The last requirement we have is to put nobits section last. The |
| 326 | // reason is that the only thing the dynamic linker will see about |
| 327 | // them is a p_memsz that is larger than p_filesz. Seeing that it |
| 328 | // zeros the end of the PT_LOAD, so that has to correspond to the |
| 329 | // nobits sections. |
Hal Finkel | 0d7e83b | 2015-10-13 17:57:46 +0000 | [diff] [blame] | 330 | if ((A->getType() == SHT_NOBITS || B->getType() == SHT_NOBITS) && |
| 331 | A->getType() != B->getType()) |
Hal Finkel | cf58b6a | 2015-10-12 21:14:03 +0000 | [diff] [blame] | 332 | return A->getType() != SHT_NOBITS && B->getType() == SHT_NOBITS; |
Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 333 | |
| 334 | return getPPC64SectionRank(A->getName()) < getPPC64SectionRank(B->getName()); |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Until this function is called, common symbols do not belong to any section. |
| 338 | // This function adds them to end of BSS section. |
| 339 | template <class ELFT> |
| 340 | static void addCommonSymbols(std::vector<DefinedCommon<ELFT> *> &Syms) { |
| 341 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 342 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 343 | |
| 344 | // Sort the common symbols by alignment as an heuristic to pack them better. |
| 345 | std::stable_sort( |
| 346 | Syms.begin(), Syms.end(), |
| 347 | [](const DefinedCommon<ELFT> *A, const DefinedCommon<ELFT> *B) { |
| 348 | return A->MaxAlignment > B->MaxAlignment; |
| 349 | }); |
| 350 | |
| 351 | uintX_t Off = Out<ELFT>::Bss->getSize(); |
| 352 | for (DefinedCommon<ELFT> *C : Syms) { |
| 353 | const Elf_Sym &Sym = C->Sym; |
| 354 | uintX_t Align = C->MaxAlignment; |
| 355 | Off = RoundUpToAlignment(Off, Align); |
| 356 | C->OffsetInBSS = Off; |
| 357 | Off += Sym.st_size; |
| 358 | } |
| 359 | |
| 360 | Out<ELFT>::Bss->setSize(Off); |
| 361 | } |
| 362 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 363 | // Create output section objects and add them to OutputSections. |
| 364 | template <class ELFT> void Writer<ELFT>::createSections() { |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 365 | // .interp needs to be on the first page in the output file. |
| 366 | if (needsInterpSection()) |
| 367 | OutputSections.push_back(Out<ELFT>::Interp); |
| 368 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 369 | SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map; |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 370 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 371 | OutputSections.push_back(Out<ELFT>::Bss); |
| 372 | Map[{Out<ELFT>::Bss->getName(), Out<ELFT>::Bss->getType(), |
| 373 | Out<ELFT>::Bss->getFlags()}] = Out<ELFT>::Bss; |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 374 | |
Rafael Espindola | c05ad3d | 2015-10-07 02:51:44 +0000 | [diff] [blame] | 375 | // Declare linker generated symbols. |
| 376 | // This must be done before the relocation scan to make sure we can correctly |
| 377 | // decide if a dynamic relocation is needed or not. |
| 378 | // FIXME: Make this more declarative. |
| 379 | for (StringRef Name : |
| 380 | {"__preinit_array_start", "__preinit_array_end", "__init_array_start", |
| 381 | "__init_array_end", "__fini_array_start", "__fini_array_end"}) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 382 | Symtab.addIgnoredSym(Name); |
Rafael Espindola | c05ad3d | 2015-10-07 02:51:44 +0000 | [diff] [blame] | 383 | |
| 384 | // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For |
| 385 | // static linking the linker is required to optimize away any references to |
| 386 | // __tls_get_addr, so it's not defined anywhere. Create a hidden definition |
| 387 | // to avoid the undefined symbol error. |
| 388 | if (!isOutputDynamic()) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 389 | Symtab.addIgnoredSym("__tls_get_addr"); |
Rafael Espindola | c05ad3d | 2015-10-07 02:51:44 +0000 | [diff] [blame] | 390 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 391 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) { |
| 392 | for (InputSection<ELFT> *C : F->getSections()) { |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 393 | if (!C || C == &InputSection<ELFT>::Discarded) |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 394 | continue; |
| 395 | const Elf_Shdr *H = C->getSectionHdr(); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 396 | uintX_t OutFlags = H->sh_flags & ~SHF_GROUP; |
| 397 | SectionKey<ELFT::Is64Bits> Key{C->getSectionName(), H->sh_type, OutFlags}; |
Rafael Espindola | d13d960 | 2015-09-25 15:08:44 +0000 | [diff] [blame] | 398 | OutputSection<ELFT> *&Sec = Map[Key]; |
| 399 | if (!Sec) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 400 | Sec = new (CAlloc.Allocate()) |
| 401 | OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags); |
Rafael Espindola | d13d960 | 2015-09-25 15:08:44 +0000 | [diff] [blame] | 402 | OutputSections.push_back(Sec); |
| 403 | } |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 404 | Sec->addSection(C); |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 405 | scanRelocs(*C); |
| 406 | } |
| 407 | } |
| 408 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 409 | Out<ELFT>::Dynamic->PreInitArraySec = |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 410 | Map.lookup({".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC}); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 411 | Out<ELFT>::Dynamic->InitArraySec = |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 412 | Map.lookup({".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC}); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 413 | Out<ELFT>::Dynamic->FiniArraySec = |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 414 | Map.lookup({".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC}); |
| 415 | |
Rui Ueyama | 0d0bcf7 | 2015-10-07 21:25:39 +0000 | [diff] [blame] | 416 | auto AddStartEnd = [&](StringRef Start, StringRef End, |
| 417 | OutputSection<ELFT> *OS) { |
Michael J. Spencer | 95538ca | 2015-10-06 01:16:17 +0000 | [diff] [blame] | 418 | if (OS) { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 419 | Symtab.addSyntheticSym(Start, *OS, 0); |
| 420 | Symtab.addSyntheticSym(End, *OS, OS->getSize()); |
Michael J. Spencer | 95538ca | 2015-10-06 01:16:17 +0000 | [diff] [blame] | 421 | } |
| 422 | }; |
| 423 | |
Michael J. Spencer | fcacad2 | 2015-10-06 19:57:05 +0000 | [diff] [blame] | 424 | AddStartEnd("__preinit_array_start", "__preinit_array_end", |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 425 | Out<ELFT>::Dynamic->PreInitArraySec); |
Michael J. Spencer | fcacad2 | 2015-10-06 19:57:05 +0000 | [diff] [blame] | 426 | AddStartEnd("__init_array_start", "__init_array_end", |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 427 | Out<ELFT>::Dynamic->InitArraySec); |
Michael J. Spencer | fcacad2 | 2015-10-06 19:57:05 +0000 | [diff] [blame] | 428 | AddStartEnd("__fini_array_start", "__fini_array_end", |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 429 | Out<ELFT>::Dynamic->FiniArraySec); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 430 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 431 | // FIXME: Try to avoid the extra walk over all global symbols. |
| 432 | std::vector<DefinedCommon<ELFT> *> CommonSymbols; |
| 433 | for (auto &P : Symtab.getSymbols()) { |
| 434 | StringRef Name = P.first; |
| 435 | SymbolBody *Body = P.second->Body; |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 436 | if (auto *U = dyn_cast<Undefined<ELFT>>(Body)) { |
| 437 | if (!U->isWeak() && !U->canKeepUndefined()) |
| 438 | reportUndefined<ELFT>(Symtab, *Body); |
| 439 | } |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 440 | |
| 441 | if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body)) |
| 442 | CommonSymbols.push_back(C); |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 443 | if (!includeInSymtab<ELFT>(*Body)) |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 444 | continue; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 445 | Out<ELFT>::SymTab->addSymbol(Name); |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 446 | |
Michael J. Spencer | 350e5b5 | 2015-10-12 23:39:23 +0000 | [diff] [blame] | 447 | if (isOutputDynamic() && includeInDynamicSymtab(*Body)) |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 448 | Out<ELFT>::HashTab->addSymbol(Body); |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 449 | } |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 450 | addCommonSymbols(CommonSymbols); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 451 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 452 | OutputSections.push_back(Out<ELFT>::SymTab); |
Michael J. Spencer | 350e5b5 | 2015-10-12 23:39:23 +0000 | [diff] [blame] | 453 | if (isOutputDynamic()) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 454 | OutputSections.push_back(Out<ELFT>::DynSymTab); |
| 455 | OutputSections.push_back(Out<ELFT>::HashTab); |
| 456 | OutputSections.push_back(Out<ELFT>::Dynamic); |
| 457 | OutputSections.push_back(Out<ELFT>::DynStrTab); |
| 458 | if (Out<ELFT>::RelaDyn->hasRelocs()) |
| 459 | OutputSections.push_back(Out<ELFT>::RelaDyn); |
Rafael Espindola | 3f4228f | 2015-09-09 15:33:08 +0000 | [diff] [blame] | 460 | } |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 461 | if (!Out<ELFT>::Got->empty()) |
| 462 | OutputSections.push_back(Out<ELFT>::Got); |
| 463 | if (!Out<ELFT>::Plt->empty()) |
| 464 | OutputSections.push_back(Out<ELFT>::Plt); |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 465 | |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 466 | std::stable_sort(OutputSections.begin(), OutputSections.end(), |
| 467 | compareOutputSections<ELFT>); |
Rui Ueyama | 3f4ec66 | 2015-09-25 03:48:25 +0000 | [diff] [blame] | 468 | |
Michael J. Spencer | 52bf0eb | 2015-10-01 21:15:02 +0000 | [diff] [blame] | 469 | // Always put StrTabSec last so that no section names are added to it after |
| 470 | // it's finalized. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 471 | OutputSections.push_back(Out<ELFT>::StrTab); |
Michael J. Spencer | 52bf0eb | 2015-10-01 21:15:02 +0000 | [diff] [blame] | 472 | |
Rafael Espindola | b01b574 | 2015-09-08 18:08:57 +0000 | [diff] [blame] | 473 | for (unsigned I = 0, N = OutputSections.size(); I < N; ++I) |
| 474 | OutputSections[I]->setSectionIndex(I + 1); |
Michael J. Spencer | 52bf0eb | 2015-10-01 21:15:02 +0000 | [diff] [blame] | 475 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 476 | // Fill the DynStrTab early. |
| 477 | Out<ELFT>::Dynamic->finalize(); |
Rui Ueyama | af311c1 | 2015-10-09 16:03:53 +0000 | [diff] [blame] | 478 | |
| 479 | // Fix each section's header (e.g. sh_size, sh_link, etc.) |
| 480 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { |
| 481 | Out<ELFT>::StrTab->add(Sec->getName()); |
| 482 | Sec->finalize(); |
| 483 | } |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 484 | |
| 485 | // If we have a .opd section (used under PPC64 for function descriptors), |
| 486 | // store a pointer to it here so that we can use it later when processing |
| 487 | // relocations. |
| 488 | Out<ELFT>::Opd = Map.lookup({".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC}); |
Rafael Espindola | abad618 | 2015-08-13 15:23:46 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Michael J. Spencer | 1d299a8 | 2015-09-09 20:48:09 +0000 | [diff] [blame] | 491 | template <class ELFT> |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 492 | static bool needsPhdr(OutputSectionBase<ELFT::Is64Bits> *Sec) { |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 493 | return Sec->getFlags() & SHF_ALLOC; |
Michael J. Spencer | 1d299a8 | 2015-09-09 20:48:09 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 496 | // Visits all sections to assign incremental, non-overlapping RVAs and |
| 497 | // file offsets. |
| 498 | template <class ELFT> void Writer<ELFT>::assignAddresses() { |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 499 | assert(!OutputSections.empty() && "No output sections to layout!"); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 500 | uintX_t VA = getVAStart() + sizeof(Elf_Ehdr); |
| 501 | uintX_t FileOff = sizeof(Elf_Ehdr); |
Rafael Espindola | 60252d8 | 2015-09-09 22:53:55 +0000 | [diff] [blame] | 502 | |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 503 | // Reserve space for Phdrs. |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 504 | int NumPhdrs = 2; // 2 for PhdrPhdr and FileHeaderPhdr |
| 505 | if (needsInterpSection()) |
| 506 | ++NumPhdrs; |
Michael J. Spencer | 350e5b5 | 2015-10-12 23:39:23 +0000 | [diff] [blame] | 507 | if (isOutputDynamic()) |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 508 | ++NumPhdrs; |
| 509 | uintX_t Last = PF_R; |
| 510 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { |
| 511 | if (!Sec->getSize() || !needsPhdr<ELFT>(Sec)) |
| 512 | continue; |
| 513 | uintX_t Flags = toPhdrFlags(Sec->getFlags()); |
| 514 | if (Last != Flags) { |
| 515 | Last = Flags; |
| 516 | ++NumPhdrs; |
| 517 | } |
| 518 | } |
Rui Ueyama | 3486fe5 | 2015-10-11 17:44:22 +0000 | [diff] [blame] | 519 | |
| 520 | // Reserve space needed for the program header so that the array |
| 521 | // will never be resized. |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 522 | Phdrs.reserve(NumPhdrs); |
| 523 | |
| 524 | // The first Phdr entry is PT_PHDR which describes the program header itself. |
| 525 | Phdrs.emplace_back(); |
| 526 | Elf_Phdr *PhdrPhdr = &Phdrs.back(); |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 527 | setPhdr(PhdrPhdr, PT_PHDR, PF_R, FileOff, VA, /*Align=*/8); |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 528 | |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 529 | FileOff += sizeof(Elf_Phdr) * NumPhdrs; |
| 530 | VA += sizeof(Elf_Phdr) * NumPhdrs; |
Michael J. Spencer | 1d299a8 | 2015-09-09 20:48:09 +0000 | [diff] [blame] | 531 | |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 532 | Elf_Phdr *Interp = nullptr; |
| 533 | if (needsInterpSection()) { |
| 534 | Phdrs.emplace_back(); |
| 535 | Interp = &Phdrs.back(); |
| 536 | } |
Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 537 | |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 538 | // Create a Phdr for the file header. |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 539 | Phdrs.emplace_back(); |
| 540 | Elf_Phdr *FileHeader = &Phdrs.back(); |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 541 | setPhdr(FileHeader, PT_LOAD, PF_R, 0, getVAStart(), Target->getPageSize()); |
Rafael Espindola | 0a2e211 | 2015-09-10 15:41:34 +0000 | [diff] [blame] | 542 | |
Rui Ueyama | 3486fe5 | 2015-10-11 17:44:22 +0000 | [diff] [blame] | 543 | SmallPtrSet<Elf_Phdr *, 8> Closed; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 544 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 545 | if (Sec->getSize()) { |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 546 | uintX_t Flags = toPhdrFlags(Sec->getFlags()); |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 547 | Elf_Phdr *Last = &Phdrs.back(); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 548 | if (Last->p_flags != Flags || !needsPhdr<ELFT>(Sec)) { |
| 549 | // Flags changed. End current Phdr and potentially create a new one. |
| 550 | if (Closed.insert(Last).second) { |
| 551 | Last->p_filesz = FileOff - Last->p_offset; |
| 552 | Last->p_memsz = VA - Last->p_vaddr; |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 555 | if (needsPhdr<ELFT>(Sec)) { |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 556 | VA = RoundUpToAlignment(VA, Target->getPageSize()); |
| 557 | FileOff = RoundUpToAlignment(FileOff, Target->getPageSize()); |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 558 | Phdrs.emplace_back(); |
| 559 | Elf_Phdr *PH = &Phdrs.back(); |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 560 | setPhdr(PH, PT_LOAD, Flags, FileOff, VA, Target->getPageSize()); |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
Michael J. Spencer | 1d299a8 | 2015-09-09 20:48:09 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Rafael Espindola | 2db634d | 2015-08-13 20:24:18 +0000 | [diff] [blame] | 565 | uintX_t Align = Sec->getAlign(); |
| 566 | uintX_t Size = Sec->getSize(); |
Rafael Espindola | ef1ac01 | 2015-08-13 15:31:17 +0000 | [diff] [blame] | 567 | if (Sec->getFlags() & SHF_ALLOC) { |
Rafael Espindola | bfcdfb3 | 2015-09-14 19:00:35 +0000 | [diff] [blame] | 568 | VA = RoundUpToAlignment(VA, Align); |
Rafael Espindola | ef1ac01 | 2015-08-13 15:31:17 +0000 | [diff] [blame] | 569 | Sec->setVA(VA); |
Rafael Espindola | bfcdfb3 | 2015-09-14 19:00:35 +0000 | [diff] [blame] | 570 | VA += Size; |
Rafael Espindola | ef1ac01 | 2015-08-13 15:31:17 +0000 | [diff] [blame] | 571 | } |
Rafael Espindola | bfcdfb3 | 2015-09-14 19:00:35 +0000 | [diff] [blame] | 572 | FileOff = RoundUpToAlignment(FileOff, Align); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 573 | Sec->setFileOffset(FileOff); |
Rafael Espindola | 058f343 | 2015-08-31 20:23:57 +0000 | [diff] [blame] | 574 | if (Sec->getType() != SHT_NOBITS) |
Rafael Espindola | bfcdfb3 | 2015-09-14 19:00:35 +0000 | [diff] [blame] | 575 | FileOff += Size; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 576 | } |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 577 | |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 578 | if (Interp) { |
| 579 | Interp->p_type = PT_INTERP; |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 580 | copyPhdr(Interp, Out<ELFT>::Interp); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 581 | } |
Michael J. Spencer | 350e5b5 | 2015-10-12 23:39:23 +0000 | [diff] [blame] | 582 | if (isOutputDynamic()) { |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 583 | Phdrs.emplace_back(); |
| 584 | Elf_Phdr *PH = &Phdrs.back(); |
| 585 | PH->p_type = PT_DYNAMIC; |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 586 | copyPhdr(PH, Out<ELFT>::Dynamic); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 587 | } |
Rafael Espindola | 91009b3 | 2015-08-12 01:45:28 +0000 | [diff] [blame] | 588 | |
Rui Ueyama | bf3fd7c | 2015-10-09 15:31:49 +0000 | [diff] [blame] | 589 | // Fix up the first entry's size. |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 590 | PhdrPhdr->p_filesz = sizeof(Elf_Phdr) * Phdrs.size(); |
| 591 | PhdrPhdr->p_memsz = sizeof(Elf_Phdr) * Phdrs.size(); |
Rui Ueyama | bf3fd7c | 2015-10-09 15:31:49 +0000 | [diff] [blame] | 592 | |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 593 | // If nothing was merged into the file header PT_LOAD, set the size correctly. |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 594 | if (FileHeader->p_filesz == Target->getPageSize()) { |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 595 | uint64_t Size = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Phdrs.size(); |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 596 | FileHeader->p_filesz = Size; |
| 597 | FileHeader->p_memsz = Size; |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 600 | // Add space for section headers. |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 601 | FileOff = RoundUpToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4); |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 602 | SectionHeaderOff = FileOff; |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 603 | FileOff += getNumSections() * sizeof(Elf_Shdr); |
Michael J. Spencer | 1d299a8 | 2015-09-09 20:48:09 +0000 | [diff] [blame] | 604 | FileSize = FileOff; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | template <class ELFT> void Writer<ELFT>::writeHeader() { |
| 608 | uint8_t *Buf = Buffer->getBufferStart(); |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 609 | auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 610 | EHdr->e_ident[EI_MAG0] = 0x7F; |
| 611 | EHdr->e_ident[EI_MAG1] = 0x45; |
| 612 | EHdr->e_ident[EI_MAG2] = 0x4C; |
| 613 | EHdr->e_ident[EI_MAG3] = 0x46; |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 614 | EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 615 | EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little |
| 616 | ? ELFDATA2LSB |
| 617 | : ELFDATA2MSB; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 618 | EHdr->e_ident[EI_VERSION] = EV_CURRENT; |
Davide Italiano | aa7c533 | 2015-09-25 01:59:13 +0000 | [diff] [blame] | 619 | |
Rafael Espindola | b9ca7bb | 2015-10-12 11:52:31 +0000 | [diff] [blame] | 620 | auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf); |
Davide Italiano | aa7c533 | 2015-09-25 01:59:13 +0000 | [diff] [blame] | 621 | EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 622 | |
Rafael Espindola | e438e07 | 2015-09-08 22:55:28 +0000 | [diff] [blame] | 623 | // FIXME: Generalize the segment construction similar to how we create |
| 624 | // output sections. |
Rafael Espindola | e438e07 | 2015-09-08 22:55:28 +0000 | [diff] [blame] | 625 | |
Rafael Espindola | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 626 | EHdr->e_type = Config->Shared ? ET_DYN : ET_EXEC; |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 627 | EHdr->e_machine = FirstObj.getEMachine(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 628 | EHdr->e_version = EV_CURRENT; |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 629 | if (Config->EntrySym) |
| 630 | if (auto *E = dyn_cast<ELFSymbolBody<ELFT>>(Config->EntrySym->repl())) |
| 631 | EHdr->e_entry = getSymVA<ELFT>(*E); |
Rui Ueyama | 5bfd7d4 | 2015-10-10 22:36:36 +0000 | [diff] [blame] | 632 | EHdr->e_phoff = sizeof(Elf_Ehdr); |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 633 | EHdr->e_shoff = SectionHeaderOff; |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 634 | EHdr->e_ehsize = sizeof(Elf_Ehdr); |
| 635 | EHdr->e_phentsize = sizeof(Elf_Phdr); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 636 | EHdr->e_phnum = Phdrs.size(); |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 637 | EHdr->e_shentsize = sizeof(Elf_Shdr); |
Rafael Espindola | 5f55387 | 2015-09-08 17:39:39 +0000 | [diff] [blame] | 638 | EHdr->e_shnum = getNumSections(); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 639 | EHdr->e_shstrndx = Out<ELFT>::StrTab->getSectionIndex(); |
Rui Ueyama | bdca0b1 | 2015-10-11 00:10:36 +0000 | [diff] [blame] | 640 | memcpy(Buf + EHdr->e_phoff, &Phdrs[0], Phdrs.size() * sizeof(Phdrs[0])); |
Rafael Espindola | e438e07 | 2015-09-08 22:55:28 +0000 | [diff] [blame] | 641 | |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 642 | auto SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff); |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 643 | // First entry is null. |
| 644 | ++SHdrs; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 645 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 646 | Sec->setNameOffset(Out<ELFT>::StrTab->getFileOff(Sec->getName())); |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 647 | Sec->template writeHeaderTo<ELFT::TargetEndianness>(SHdrs++); |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 648 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) { |
Rafael Espindola | bdc8f2f | 2015-08-13 00:31:46 +0000 | [diff] [blame] | 652 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 653 | FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable); |
| 654 | error(BufferOrErr, Twine("failed to open ") + Path); |
| 655 | Buffer = std::move(*BufferOrErr); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | // Write section contents to a mmap'ed file. |
| 659 | template <class ELFT> void Writer<ELFT>::writeSections() { |
| 660 | uint8_t *Buf = Buffer->getBufferStart(); |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 661 | |
| 662 | // PPC64 needs to process relocations in the .opd section before processing |
| 663 | // relocations in code-containing sections. |
Rafael Espindola | 7a51305 | 2015-10-13 14:45:51 +0000 | [diff] [blame] | 664 | if (OutputSectionBase<ELFT::Is64Bits> *Sec = Out<ELFT>::Opd) { |
| 665 | Out<ELFT>::OpdBuf = Buf + Sec->getFileOff(); |
| 666 | Sec->writeTo(Buf + Sec->getFileOff()); |
| 667 | } |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 668 | |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 669 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) |
Rafael Espindola | 7a51305 | 2015-10-13 14:45:51 +0000 | [diff] [blame] | 670 | if (Sec != Out<ELFT>::Opd) |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 671 | Sec->writeTo(Buf + Sec->getFileOff()); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 672 | } |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 673 | |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 674 | template <class ELFT> |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 675 | void Writer<ELFT>::setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags, |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 676 | uintX_t FileOff, uintX_t VA, uintX_t Align) { |
| 677 | PH->p_type = Type; |
| 678 | PH->p_flags = Flags; |
| 679 | PH->p_offset = FileOff; |
| 680 | PH->p_vaddr = VA; |
| 681 | PH->p_paddr = VA; |
| 682 | PH->p_align = Align; |
| 683 | } |
| 684 | |
| 685 | template <class ELFT> |
Rui Ueyama | 536c999 | 2015-10-11 19:45:58 +0000 | [diff] [blame] | 686 | void Writer<ELFT>::copyPhdr(Elf_Phdr *PH, |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 687 | OutputSectionBase<ELFT::Is64Bits> *From) { |
| 688 | PH->p_flags = toPhdrFlags(From->getFlags()); |
| 689 | PH->p_offset = From->getFileOff(); |
| 690 | PH->p_vaddr = From->getVA(); |
| 691 | PH->p_paddr = From->getVA(); |
| 692 | PH->p_filesz = From->getSize(); |
| 693 | PH->p_memsz = From->getSize(); |
| 694 | PH->p_align = From->getAlign(); |
| 695 | } |
| 696 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 697 | template void lld::elf2::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab); |
| 698 | template void lld::elf2::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab); |
| 699 | template void lld::elf2::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab); |
| 700 | template void lld::elf2::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab); |