| 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" |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 12 | #include "LinkerScript.h" |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 13 | #include "OutputSections.h" |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 14 | #include "SymbolTable.h" |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 15 | #include "Target.h" |
| Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 16 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
| Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
| Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Endian.h" |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileOutputBuffer.h" |
| Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 22 | #include "llvm/Support/StringSaver.h" |
| Rafael Espindola | d0078b2 | 2016-02-06 00:06:26 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | using namespace llvm::ELF; |
| 27 | using namespace llvm::object; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 28 | using namespace llvm::support::endian; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lld; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 31 | using namespace lld::elf; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 32 | |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | // The writer writes a SymbolTable result to a file. |
| 35 | template <class ELFT> class Writer { |
| 36 | public: |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 37 | typedef typename ELFT::uint uintX_t; |
| 38 | typedef typename ELFT::Shdr Elf_Shdr; |
| 39 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 40 | typedef typename ELFT::Phdr Elf_Phdr; |
| 41 | typedef typename ELFT::Sym Elf_Sym; |
| 42 | typedef typename ELFT::SymRange Elf_Sym_Range; |
| 43 | typedef typename ELFT::Rela Elf_Rela; |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 44 | Writer(SymbolTable<ELFT> &S) : Symtab(S) {} |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 45 | void run(); |
| 46 | |
| 47 | private: |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 48 | // This describes a program header entry. |
| 49 | // Each contains type, access flags and range of output sections that will be |
| 50 | // placed in it. |
| 51 | struct Phdr { |
| 52 | Phdr(unsigned Type, unsigned Flags) { |
| 53 | H.p_type = Type; |
| 54 | H.p_flags = Flags; |
| 55 | } |
| 56 | Elf_Phdr H = {}; |
| 57 | OutputSectionBase<ELFT> *First = nullptr; |
| 58 | OutputSectionBase<ELFT> *Last = nullptr; |
| 59 | }; |
| 60 | |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 61 | void copyLocalSymbols(); |
| Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 62 | void addReservedSymbols(); |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 63 | void createSections(); |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 64 | void addPredefinedSections(); |
| Rui Ueyama | 3095148 | 2016-02-25 19:34:37 +0000 | [diff] [blame] | 65 | bool needsGot(); |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 66 | |
| Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 67 | template <class RelTy> |
| Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 68 | void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels); |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 69 | |
| Rafael Espindola | a0fa848 | 2015-11-11 15:29:50 +0000 | [diff] [blame] | 70 | void scanRelocs(InputSection<ELFT> &C); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 71 | void scanRelocs(InputSectionBase<ELFT> &S, const Elf_Shdr &RelSec); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 72 | void createPhdrs(); |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 73 | void assignAddresses(); |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 74 | void assignFileOffsets(); |
| 75 | void setPhdrs(); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 76 | void fixHeaders(); |
| Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 77 | void fixSectionAlignments(); |
| Rui Ueyama | 1a311f1 | 2015-12-26 10:52:26 +0000 | [diff] [blame] | 78 | void fixAbsoluteSymbols(); |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 79 | void openFile(); |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 80 | void writeHeader(); |
| 81 | void writeSections(); |
| Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 82 | void writeBuildId(); |
| Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 83 | bool isDiscarded(InputSectionBase<ELFT> *IS) const; |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 84 | StringRef getOutputSectionName(InputSectionBase<ELFT> *S) const; |
| Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 85 | bool needsInterpSection() const { |
| Rui Ueyama | 0d0bcf7 | 2015-10-07 21:25:39 +0000 | [diff] [blame] | 86 | return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty(); |
| Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 87 | } |
| Michael J. Spencer | f32446f | 2015-10-06 20:39:09 +0000 | [diff] [blame] | 88 | bool isOutputDynamic() const { |
| George Rimar | 786e866 | 2016-03-17 05:57:33 +0000 | [diff] [blame] | 89 | return !Symtab.getSharedFiles().empty() || Config->Pic; |
| Rafael Espindola | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 90 | } |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 91 | template <class RelTy> |
| 92 | void scanRelocsForThunks(const elf::ObjectFile<ELFT> &File, |
| Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 93 | ArrayRef<RelTy> Rels); |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 94 | |
| Rui Ueyama | b30f7356 | 2016-03-13 04:11:53 +0000 | [diff] [blame] | 95 | void ensureBss(); |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 96 | void addCommonSymbols(std::vector<DefinedCommon *> &Syms); |
| Peter Collingbourne | 4cdade6 | 2016-04-04 22:29:24 +0000 | [diff] [blame] | 97 | void addCopyRelSymbol(SharedSymbol<ELFT> *Sym); |
| Rafael Espindola | 443f50a | 2015-11-03 21:35:14 +0000 | [diff] [blame] | 98 | |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 99 | std::unique_ptr<llvm::FileOutputBuffer> Buffer; |
| Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 100 | |
| Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 101 | BumpPtrAllocator Alloc; |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 102 | std::vector<OutputSectionBase<ELFT> *> OutputSections; |
| Rui Ueyama | d4ea7dd | 2015-12-26 07:01:26 +0000 | [diff] [blame] | 103 | std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> OwningSections; |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 104 | |
| Rui Ueyama | 0168722 | 2015-12-26 09:47:57 +0000 | [diff] [blame] | 105 | void addRelIpltSymbols(); |
| Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 106 | void addStartEndSymbols(); |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 107 | void addStartStopSymbols(OutputSectionBase<ELFT> *Sec); |
| Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 108 | |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 109 | SymbolTable<ELFT> &Symtab; |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 110 | std::vector<Phdr> Phdrs; |
| Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 111 | |
| Rafael Espindola | 98f6bd0 | 2015-08-11 23:14:13 +0000 | [diff] [blame] | 112 | uintX_t FileSize; |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 113 | uintX_t SectionHeaderOff; |
| Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 114 | |
| Sean Silva | f1c5a0f | 2016-01-23 01:49:37 +0000 | [diff] [blame] | 115 | // Flag to force GOT to be in output if we have relocations |
| 116 | // that relies on its address. |
| 117 | bool HasGotOffRel = false; |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 118 | }; |
| 119 | } // anonymous namespace |
| 120 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 121 | template <class ELFT> void elf::writeResult(SymbolTable<ELFT> *Symtab) { |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 122 | typedef typename ELFT::uint uintX_t; |
| George Rimar | 687788c | 2016-04-01 17:30:52 +0000 | [diff] [blame] | 123 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 124 | |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 125 | // Create singleton output sections. |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 126 | DynamicSection<ELFT> Dynamic(*Symtab); |
| George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 127 | EhFrameHeader<ELFT> EhFrameHdr; |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 128 | GotSection<ELFT> Got; |
| 129 | InterpSection<ELFT> Interp; |
| 130 | PltSection<ELFT> Plt; |
| Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 131 | RelocationSection<ELFT> RelaDyn(Config->Rela ? ".rela.dyn" : ".rel.dyn"); |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 132 | StringTableSection<ELFT> DynStrTab(".dynstr", true); |
| 133 | StringTableSection<ELFT> ShStrTab(".shstrtab", false); |
| 134 | SymbolTableSection<ELFT> DynSymTab(*Symtab, DynStrTab); |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 135 | VersionTableSection<ELFT> VerSym; |
| 136 | VersionNeedSection<ELFT> VerNeed; |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 137 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 138 | OutputSectionBase<ELFT> ElfHeader("", 0, SHF_ALLOC); |
| George Rimar | 687788c | 2016-04-01 17:30:52 +0000 | [diff] [blame] | 139 | ElfHeader.setSize(sizeof(Elf_Ehdr)); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 140 | OutputSectionBase<ELFT> ProgramHeaders("", 0, SHF_ALLOC); |
| 141 | ProgramHeaders.updateAlign(sizeof(uintX_t)); |
| 142 | |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 143 | // Instantiate optional output sections if they are needed. |
| Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 144 | std::unique_ptr<BuildIdSection<ELFT>> BuildId; |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 145 | std::unique_ptr<GnuHashTableSection<ELFT>> GnuHashTab; |
| 146 | std::unique_ptr<GotPltSection<ELFT>> GotPlt; |
| 147 | std::unique_ptr<HashTableSection<ELFT>> HashTab; |
| 148 | std::unique_ptr<RelocationSection<ELFT>> RelaPlt; |
| 149 | std::unique_ptr<StringTableSection<ELFT>> StrTab; |
| 150 | std::unique_ptr<SymbolTableSection<ELFT>> SymTabSec; |
| Rui Ueyama | a354c5c | 2016-02-25 23:54:49 +0000 | [diff] [blame] | 151 | std::unique_ptr<OutputSection<ELFT>> MipsRldMap; |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 152 | |
| Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 153 | if (Config->BuildId == BuildIdKind::Fnv1) |
| 154 | BuildId.reset(new BuildIdFnv1<ELFT>); |
| 155 | else if (Config->BuildId == BuildIdKind::Md5) |
| 156 | BuildId.reset(new BuildIdMd5<ELFT>); |
| Rui Ueyama | d86ec30 | 2016-04-07 23:51:56 +0000 | [diff] [blame] | 157 | else if (Config->BuildId == BuildIdKind::Sha1) |
| 158 | BuildId.reset(new BuildIdSha1<ELFT>); |
| Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 159 | |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 160 | if (Config->GnuHash) |
| 161 | GnuHashTab.reset(new GnuHashTableSection<ELFT>); |
| 162 | if (Config->SysvHash) |
| 163 | HashTab.reset(new HashTableSection<ELFT>); |
| 164 | if (Target->UseLazyBinding) { |
| Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 165 | StringRef S = Config->Rela ? ".rela.plt" : ".rel.plt"; |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 166 | GotPlt.reset(new GotPltSection<ELFT>); |
| Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 167 | RelaPlt.reset(new RelocationSection<ELFT>(S)); |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 168 | } |
| 169 | if (!Config->StripAll) { |
| 170 | StrTab.reset(new StringTableSection<ELFT>(".strtab", false)); |
| 171 | SymTabSec.reset(new SymbolTableSection<ELFT>(*Symtab, *StrTab)); |
| 172 | } |
| Rui Ueyama | a354c5c | 2016-02-25 23:54:49 +0000 | [diff] [blame] | 173 | if (Config->EMachine == EM_MIPS && !Config->Shared) { |
| 174 | // This is a MIPS specific section to hold a space within the data segment |
| 175 | // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry. |
| 176 | // See "Dynamic section" in Chapter 5 in the following document: |
| 177 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 178 | MipsRldMap.reset(new OutputSection<ELFT>(".rld_map", SHT_PROGBITS, |
| 179 | SHF_ALLOC | SHF_WRITE)); |
| 180 | MipsRldMap->setSize(sizeof(uintX_t)); |
| 181 | MipsRldMap->updateAlign(sizeof(uintX_t)); |
| 182 | } |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 183 | |
| Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 184 | Out<ELFT>::BuildId = BuildId.get(); |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 185 | Out<ELFT>::DynStrTab = &DynStrTab; |
| 186 | Out<ELFT>::DynSymTab = &DynSymTab; |
| 187 | Out<ELFT>::Dynamic = &Dynamic; |
| George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 188 | Out<ELFT>::EhFrameHdr = &EhFrameHdr; |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 189 | Out<ELFT>::GnuHashTab = GnuHashTab.get(); |
| 190 | Out<ELFT>::Got = &Got; |
| 191 | Out<ELFT>::GotPlt = GotPlt.get(); |
| 192 | Out<ELFT>::HashTab = HashTab.get(); |
| 193 | Out<ELFT>::Interp = &Interp; |
| 194 | Out<ELFT>::Plt = &Plt; |
| 195 | Out<ELFT>::RelaDyn = &RelaDyn; |
| 196 | Out<ELFT>::RelaPlt = RelaPlt.get(); |
| 197 | Out<ELFT>::ShStrTab = &ShStrTab; |
| 198 | Out<ELFT>::StrTab = StrTab.get(); |
| 199 | Out<ELFT>::SymTab = SymTabSec.get(); |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 200 | Out<ELFT>::VerSym = &VerSym; |
| 201 | Out<ELFT>::VerNeed = &VerNeed; |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 202 | Out<ELFT>::Bss = nullptr; |
| Rui Ueyama | a354c5c | 2016-02-25 23:54:49 +0000 | [diff] [blame] | 203 | Out<ELFT>::MipsRldMap = MipsRldMap.get(); |
| Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 204 | Out<ELFT>::Opd = nullptr; |
| 205 | Out<ELFT>::OpdBuf = nullptr; |
| 206 | Out<ELFT>::TlsPhdr = nullptr; |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 207 | Out<ELFT>::ElfHeader = &ElfHeader; |
| 208 | Out<ELFT>::ProgramHeaders = &ProgramHeaders; |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 209 | |
| Rui Ueyama | 0d0bcf7 | 2015-10-07 21:25:39 +0000 | [diff] [blame] | 210 | Writer<ELFT>(*Symtab).run(); |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 213 | // The main function of the writer. |
| Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 214 | template <class ELFT> void Writer<ELFT>::run() { |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 215 | if (!Config->DiscardAll) |
| 216 | copyLocalSymbols(); |
| Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 217 | addReservedSymbols(); |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 218 | createSections(); |
| 219 | if (HasError) |
| Rui Ueyama | c2a0d7e | 2016-01-28 22:56:29 +0000 | [diff] [blame] | 220 | return; |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 221 | |
| 222 | if (Config->Relocatable) { |
| 223 | assignFileOffsets(); |
| 224 | } else { |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 225 | createPhdrs(); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 226 | fixHeaders(); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 227 | if (ScriptConfig->DoLayout) { |
| 228 | Script<ELFT>::X->assignAddresses(OutputSections); |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 229 | } else { |
| 230 | fixSectionAlignments(); |
| 231 | assignAddresses(); |
| 232 | } |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 233 | assignFileOffsets(); |
| 234 | setPhdrs(); |
| Rui Ueyama | a63baf1 | 2016-04-01 17:11:42 +0000 | [diff] [blame] | 235 | fixAbsoluteSymbols(); |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 236 | } |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 237 | |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 238 | openFile(); |
| 239 | if (HasError) |
| Rui Ueyama | cbe3926 | 2016-02-02 22:48:04 +0000 | [diff] [blame] | 240 | return; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 241 | writeHeader(); |
| 242 | writeSections(); |
| Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 243 | writeBuildId(); |
| Rui Ueyama | 2192399 | 2016-02-01 23:28:21 +0000 | [diff] [blame] | 244 | if (HasError) |
| 245 | return; |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 246 | check(Buffer->commit()); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 249 | namespace { |
| 250 | template <bool Is64Bits> struct SectionKey { |
| 251 | typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; |
| 252 | StringRef Name; |
| Rui Ueyama | 0fb1ee0 | 2015-10-02 21:13:19 +0000 | [diff] [blame] | 253 | uint32_t Type; |
| 254 | uintX_t Flags; |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 255 | uintX_t Alignment; |
| Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 256 | }; |
| 257 | } |
| 258 | namespace llvm { |
| 259 | template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> { |
| 260 | static SectionKey<Is64Bits> getEmptyKey() { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 261 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, |
| 262 | 0}; |
| Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 263 | } |
| 264 | static SectionKey<Is64Bits> getTombstoneKey() { |
| 265 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 266 | 0, 0}; |
| Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 267 | } |
| 268 | static unsigned getHashValue(const SectionKey<Is64Bits> &Val) { |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 269 | return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment); |
| Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 270 | } |
| 271 | static bool isEqual(const SectionKey<Is64Bits> &LHS, |
| 272 | const SectionKey<Is64Bits> &RHS) { |
| 273 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 274 | LHS.Type == RHS.Type && LHS.Flags == RHS.Flags && |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 275 | LHS.Alignment == RHS.Alignment; |
| Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 276 | } |
| 277 | }; |
| 278 | } |
| 279 | |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 280 | // Returns the number of relocations processed. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 281 | template <class ELFT> |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 282 | static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body, |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 283 | InputSectionBase<ELFT> &C, |
| 284 | typename ELFT::uint Offset, |
| 285 | typename ELFT::uint Addend, RelExpr Expr) { |
| 286 | if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC)) |
| 287 | return 0; |
| 288 | |
| Rafael Espindola | f54413c | 2016-04-20 14:52:18 +0000 | [diff] [blame] | 289 | if (!Body.isTls()) |
| 290 | return 0; |
| 291 | |
| Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 292 | typedef typename ELFT::uint uintX_t; |
| Rafael Espindola | 99c2247 | 2016-04-18 01:34:20 +0000 | [diff] [blame] | 293 | if (Expr == R_TLSLD_PC || Expr == R_TLSLD) { |
| Rafael Espindola | 2081dbc | 2016-04-20 15:01:42 +0000 | [diff] [blame] | 294 | // Local-Dynamic relocs can be relaxed to Local-Exec. |
| 295 | if (!Config->Shared) { |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 296 | C.Relocations.push_back( |
| 297 | {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); |
| Rafael Espindola | 059f3fb | 2016-04-02 00:19:22 +0000 | [diff] [blame] | 298 | return 2; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 299 | } |
| Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 300 | if (Out<ELFT>::Got->addTlsIndex()) |
| Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 301 | Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got, |
| 302 | Out<ELFT>::Got->getTlsIndexOff(), false, |
| 303 | nullptr, 0}); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 304 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 305 | return 1; |
| George Rimar | bfd29a1 | 2016-01-21 09:14:22 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| Rafael Espindola | 2081dbc | 2016-04-20 15:01:42 +0000 | [diff] [blame] | 308 | // Local-Dynamic relocs can be relaxed to Local-Exec. |
| 309 | if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) { |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 310 | C.Relocations.push_back( |
| 311 | {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); |
| 312 | return 1; |
| 313 | } |
| 314 | |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 315 | if (Target->isTlsGlobalDynamicRel(Type)) { |
| Rafael Espindola | 2081dbc | 2016-04-20 15:01:42 +0000 | [diff] [blame] | 316 | if (Config->Shared) { |
| Rafael Espindola | 56da313 | 2016-02-22 19:57:55 +0000 | [diff] [blame] | 317 | if (Out<ELFT>::Got->addDynTlsEntry(Body)) { |
| Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 318 | uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body); |
| Rafael Espindola | 56da313 | 2016-02-22 19:57:55 +0000 | [diff] [blame] | 319 | Out<ELFT>::RelaDyn->addReloc( |
| Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 320 | {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0}); |
| 321 | Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got, |
| 322 | Off + (uintX_t)sizeof(uintX_t), false, |
| 323 | &Body, 0}); |
| Rafael Espindola | 56da313 | 2016-02-22 19:57:55 +0000 | [diff] [blame] | 324 | } |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 325 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 326 | return 1; |
| George Rimar | bfd29a1 | 2016-01-21 09:14:22 +0000 | [diff] [blame] | 327 | } |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 328 | |
| Rafael Espindola | 2081dbc | 2016-04-20 15:01:42 +0000 | [diff] [blame] | 329 | // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec |
| 330 | // depending on the symbol being locally defined or not. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 331 | if (Body.isPreemptible()) { |
| Rafael Espindola | df17277 | 2016-04-18 01:29:15 +0000 | [diff] [blame] | 332 | Expr = |
| 333 | Expr == R_TLSGD_PC ? R_RELAX_TLS_GD_TO_IE_PC : R_RELAX_TLS_GD_TO_IE; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 334 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
| 335 | if (!Body.isInGot()) { |
| 336 | Out<ELFT>::Got->addEntry(Body); |
| 337 | Out<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, Out<ELFT>::Got, |
| 338 | Body.getGotOffset<ELFT>(), false, &Body, |
| 339 | 0}); |
| 340 | } |
| Rafael Espindola | cf3b04d | 2016-04-01 23:36:56 +0000 | [diff] [blame] | 341 | return 2; |
| Rafael Espindola | b97f4be | 2016-04-01 12:54:27 +0000 | [diff] [blame] | 342 | } |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 343 | C.Relocations.push_back( |
| 344 | {R_RELAX_TLS_GD_TO_LE, Type, Offset, Addend, &Body}); |
| 345 | return Target->TlsGdToLeSkip; |
| 346 | } |
| Rafael Espindola | 2081dbc | 2016-04-20 15:01:42 +0000 | [diff] [blame] | 347 | |
| 348 | // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally |
| 349 | // defined. |
| 350 | if (Target->isTlsInitialExecRel(Type) && !Config->Shared && |
| 351 | !Body.isPreemptible()) { |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 352 | C.Relocations.push_back( |
| 353 | {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body}); |
| 354 | return 1; |
| George Rimar | bfd29a1 | 2016-01-21 09:14:22 +0000 | [diff] [blame] | 355 | } |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 356 | return 0; |
| George Rimar | bfd29a1 | 2016-01-21 09:14:22 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 359 | // Some targets might require creation of thunks for relocations. Now we |
| 360 | // support only MIPS which requires LA25 thunk to call PIC code from non-PIC |
| 361 | // one. Scan relocations to find each one requires thunk. |
| 362 | template <class ELFT> |
| 363 | template <class RelTy> |
| 364 | void Writer<ELFT>::scanRelocsForThunks(const elf::ObjectFile<ELFT> &File, |
| Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 365 | ArrayRef<RelTy> Rels) { |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 366 | for (const RelTy &RI : Rels) { |
| 367 | uint32_t Type = RI.getType(Config->Mips64EL); |
| Peter Collingbourne | 676c7cd | 2016-04-26 23:52:44 +0000 | [diff] [blame] | 368 | SymbolBody &Body = File.getRelocTargetSym(RI); |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 369 | if (Body.hasThunk() || !Target->needsThunk(Type, File, Body)) |
| 370 | continue; |
| 371 | auto *D = cast<DefinedRegular<ELFT>>(&Body); |
| 372 | auto *S = cast<InputSection<ELFT>>(D->Section); |
| 373 | S->addThunk(Body); |
| 374 | } |
| 375 | } |
| 376 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 377 | template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) { |
| 378 | return read32<E>(Loc) & 0xffff; |
| 379 | } |
| 380 | |
| 381 | template <class RelTy> |
| 382 | static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) { |
| 383 | switch (Rel->getType(Config->Mips64EL)) { |
| 384 | case R_MIPS_HI16: |
| 385 | return R_MIPS_LO16; |
| 386 | case R_MIPS_GOT16: |
| 387 | return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE; |
| 388 | case R_MIPS_PCHI16: |
| 389 | return R_MIPS_PCLO16; |
| 390 | case R_MICROMIPS_HI16: |
| 391 | return R_MICROMIPS_LO16; |
| 392 | default: |
| 393 | return R_MIPS_NONE; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | template <class ELFT, class RelTy> |
| 398 | static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc, |
| 399 | SymbolBody &Sym, const RelTy *Rel, |
| 400 | const RelTy *End) { |
| 401 | uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL); |
| 402 | uint32_t Type = getMipsPairType(Rel, Sym); |
| 403 | |
| 404 | // Some MIPS relocations use addend calculated from addend of the relocation |
| 405 | // itself and addend of paired relocation. ABI requires to compute such |
| 406 | // combined addend in case of REL relocation record format only. |
| 407 | // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 408 | if (RelTy::IsRela || Type == R_MIPS_NONE) |
| 409 | return 0; |
| 410 | |
| 411 | for (const RelTy *RI = Rel; RI != End; ++RI) { |
| 412 | if (RI->getType(Config->Mips64EL) != Type) |
| 413 | continue; |
| 414 | if (RI->getSymbol(Config->Mips64EL) != SymIndex) |
| 415 | continue; |
| 416 | const endianness E = ELFT::TargetEndianness; |
| 417 | return ((read32<E>(BufLoc) & 0xffff) << 16) + |
| 418 | readSignedLo16<E>(Buf + RI->r_offset); |
| 419 | } |
| 420 | unsigned OldType = Rel->getType(Config->Mips64EL); |
| 421 | StringRef OldName = getELFRelocationTypeName(Config->EMachine, OldType); |
| 422 | StringRef NewName = getELFRelocationTypeName(Config->EMachine, Type); |
| 423 | warning("can't find matching " + NewName + " relocation for " + OldName); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| Rafael Espindola | f8f6ad7 | 2016-04-15 12:22:22 +0000 | [diff] [blame] | 427 | // True if non-preemptable symbol always has the same value regardless of where |
| 428 | // the DSO is loaded. |
| 429 | template <class ELFT> static bool isAbsolute(const SymbolBody &Body) { |
| Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 430 | Symbol *Sym = Body.Backref; |
| Rafael Espindola | 365e5f6 | 2016-04-27 11:54:07 +0000 | [diff] [blame] | 431 | if (Body.isUndefined()) { |
| 432 | if (!Sym) |
| 433 | return false; // undefined local. That is the dummy symbol 0. |
| 434 | if (Sym->isWeak()) |
| 435 | return true; // always 0 |
| 436 | } |
| Rafael Espindola | f8f6ad7 | 2016-04-15 12:22:22 +0000 | [diff] [blame] | 437 | if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body)) |
| 438 | return DR->Section == nullptr; // Absolute symbol. |
| 439 | return false; |
| 440 | } |
| 441 | |
| Rafael Espindola | 0c869a7 | 2016-04-21 16:57:32 +0000 | [diff] [blame] | 442 | namespace { |
| 443 | enum PltNeed { Plt_No, Plt_Explicit, Plt_Implicit }; |
| 444 | } |
| 445 | |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 446 | static bool needsPlt(RelExpr Expr) { |
| 447 | return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT; |
| 448 | } |
| 449 | |
| Rafael Espindola | 34b335b | 2016-04-21 16:59:25 +0000 | [diff] [blame] | 450 | static PltNeed needsPlt(RelExpr Expr, uint32_t Type, const SymbolBody &S) { |
| Rafael Espindola | 0c869a7 | 2016-04-21 16:57:32 +0000 | [diff] [blame] | 451 | if (S.isGnuIFunc()) |
| 452 | return Plt_Explicit; |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 453 | if (S.isPreemptible() && needsPlt(Expr)) |
| Rafael Espindola | 0c869a7 | 2016-04-21 16:57:32 +0000 | [diff] [blame] | 454 | return Plt_Explicit; |
| 455 | |
| 456 | // This handles a non PIC program call to function in a shared library. |
| 457 | // In an ideal world, we could just report an error saying the relocation |
| 458 | // can overflow at runtime. |
| 459 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 460 | // libc.so. |
| 461 | // |
| 462 | // The general idea on how to handle such cases is to create a PLT entry |
| 463 | // and use that as the function value. |
| 464 | // |
| 465 | // For the static linking part, we just return true and everything else |
| 466 | // will use the the PLT entry as the address. |
| 467 | // |
| 468 | // The remaining problem is making sure pointer equality still works. We |
| 469 | // need the help of the dynamic linker for that. We let it know that we have |
| 470 | // a direct reference to a so symbol by creating an undefined symbol with a |
| 471 | // non zero st_value. Seeing that, the dynamic linker resolves the symbol to |
| 472 | // the value of the symbol we created. This is true even for got entries, so |
| 473 | // pointer equality is maintained. To avoid an infinite loop, the only entry |
| 474 | // that points to the real function is a dedicated got entry used by the |
| 475 | // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 476 | // R_386_JMP_SLOT, etc). |
| Rafael Espindola | 34b335b | 2016-04-21 16:59:25 +0000 | [diff] [blame] | 477 | if (S.isShared() && !Config->Pic && S.isFunc()) |
| Rafael Espindola | 0c869a7 | 2016-04-21 16:57:32 +0000 | [diff] [blame] | 478 | if (!refersToGotEntry(Expr)) |
| 479 | return Plt_Implicit; |
| Rafael Espindola | 0c869a7 | 2016-04-21 16:57:32 +0000 | [diff] [blame] | 480 | |
| 481 | return Plt_No; |
| 482 | } |
| 483 | |
| Rafael Espindola | a6c4d2f | 2016-04-25 12:05:56 +0000 | [diff] [blame] | 484 | static bool needsCopyRel(RelExpr E, const SymbolBody &S) { |
| 485 | if (Config->Shared) |
| 486 | return false; |
| 487 | if (!S.isShared()) |
| 488 | return false; |
| 489 | if (!S.isObject()) |
| 490 | return false; |
| 491 | if (refersToGotEntry(E)) |
| 492 | return false; |
| 493 | if (needsPlt(E)) |
| 494 | return false; |
| 495 | if (E == R_SIZE) |
| 496 | return false; |
| 497 | return true; |
| 498 | } |
| 499 | |
| Rafael Espindola | 7ac9628 | 2016-04-27 12:47:30 +0000 | [diff] [blame] | 500 | template <class ELFT> |
| 501 | static bool isRelRelative(RelExpr E, uint32_t Type, const SymbolBody &Body) { |
| 502 | if (E == R_SIZE) |
| 503 | return true; |
| 504 | |
| 505 | bool AbsVal = (isAbsolute<ELFT>(Body) || Body.isTls()) && |
| 506 | !refersToGotEntry(E) && !needsPlt(E); |
| 507 | |
| 508 | bool RelE = E == R_PC || E == R_PLT_PC || E == R_GOT_PC || E == R_GOTREL || |
| 509 | E == R_PAGE_PC; |
| 510 | if (AbsVal && !RelE) |
| 511 | return true; |
| 512 | if (!AbsVal && RelE) |
| 513 | return true; |
| 514 | |
| Rafael Espindola | b8ff59a | 2016-04-28 14:34:39 +0000 | [diff] [blame] | 515 | return Target->usesOnlyLowPageBits(Type); |
| Rafael Espindola | 7ac9628 | 2016-04-27 12:47:30 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 518 | // The reason we have to do this early scan is as follows |
| 519 | // * To mmap the output file, we need to know the size |
| 520 | // * For that, we need to know how many dynamic relocs we will have. |
| 521 | // It might be possible to avoid this by outputting the file with write: |
| 522 | // * Write the allocated output sections, computing addresses. |
| 523 | // * Apply relocations, recording which ones require a dynamic reloc. |
| 524 | // * Write the dynamic relocations. |
| 525 | // * Write the rest of the file. |
| Rafael Espindola | 0df1b0b | 2016-02-02 15:45:37 +0000 | [diff] [blame] | 526 | // This would have some drawbacks. For example, we would only know if .rela.dyn |
| 527 | // is needed after applying relocations. If it is, it will go after rw and rx |
| 528 | // sections. Given that it is ro, we will need an extra PT_LOAD. This |
| 529 | // complicates things for the dynamic linker and means we would have to reserve |
| 530 | // space for the extra PT_LOAD even if we end up not using it. |
| Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 531 | template <class ELFT> |
| Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 532 | template <class RelTy> |
| Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 533 | void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) { |
| Peter Collingbourne | 0a68cf5 | 2016-04-14 01:48:11 +0000 | [diff] [blame] | 534 | uintX_t Flags = C.getSectionHdr()->sh_flags; |
| Peter Collingbourne | 0a68cf5 | 2016-04-14 01:48:11 +0000 | [diff] [blame] | 535 | bool IsWrite = Flags & SHF_WRITE; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 536 | |
| 537 | auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) { |
| Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame^] | 538 | Out<ELFT>::RelaDyn->addReloc(Reloc); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 539 | }; |
| 540 | |
| Rui Ueyama | 6b074f5 | 2016-03-11 18:56:05 +0000 | [diff] [blame] | 541 | const elf::ObjectFile<ELFT> &File = *C.getFile(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 542 | ArrayRef<uint8_t> SectionData = C.getSectionData(); |
| 543 | const uint8_t *Buf = SectionData.begin(); |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 544 | for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) { |
| 545 | const RelTy &RI = *I; |
| Peter Collingbourne | 676c7cd | 2016-04-26 23:52:44 +0000 | [diff] [blame] | 546 | SymbolBody &Body = File.getRelocTargetSym(RI); |
| Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 547 | uint32_t Type = RI.getType(Config->Mips64EL); |
| Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 548 | |
| Simon Atanasyan | 682aeea | 2016-01-14 20:42:09 +0000 | [diff] [blame] | 549 | // Ignore "hint" relocation because it is for optional code optimization. |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 550 | if (Target->isHintRel(Type)) |
| Simon Atanasyan | 682aeea | 2016-01-14 20:42:09 +0000 | [diff] [blame] | 551 | continue; |
| 552 | |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 553 | uintX_t Offset = C.getOffset(RI.r_offset); |
| 554 | if (Offset == (uintX_t)-1) |
| 555 | continue; |
| 556 | |
| Simon Atanasyan | 1ca263c | 2016-04-14 21:10:05 +0000 | [diff] [blame] | 557 | RelExpr Expr = Target->getRelExpr(Type, Body); |
| Rafael Espindola | 5bf5972 | 2016-04-18 12:31:37 +0000 | [diff] [blame] | 558 | |
| 559 | // This relocation does not require got entry, but it is relative to got and |
| 560 | // needs it to be created. Here we request for that. |
| Rafael Espindola | 520ed3a | 2016-04-27 12:21:27 +0000 | [diff] [blame] | 561 | if (Expr == R_GOTONLY_PC || Expr == R_GOTREL || Expr == R_PPC_TOC) |
| Rafael Espindola | 5bf5972 | 2016-04-18 12:31:37 +0000 | [diff] [blame] | 562 | HasGotOffRel = true; |
| 563 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 564 | uintX_t Addend = getAddend<ELFT>(RI); |
| 565 | const uint8_t *BufLoc = Buf + RI.r_offset; |
| 566 | if (!RelTy::IsRela) |
| 567 | Addend += Target->getImplicitAddend(BufLoc, Type); |
| Simon Atanasyan | 1ca263c | 2016-04-14 21:10:05 +0000 | [diff] [blame] | 568 | if (Config->EMachine == EM_MIPS) { |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 569 | Addend += findMipsPairedAddend<ELFT>(Buf, BufLoc, Body, &RI, E); |
| Simon Atanasyan | 1ca263c | 2016-04-14 21:10:05 +0000 | [diff] [blame] | 570 | if (Type == R_MIPS_LO16 && Expr == R_PC) |
| 571 | // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp |
| 572 | // symbol. In that case we should use the following formula for |
| 573 | // calculation "AHL + GP – P + 4". Let's add 4 right here. |
| 574 | // For details see p. 4-19 at |
| 575 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 576 | Addend += 4; |
| 577 | } |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 578 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 579 | if (unsigned Processed = |
| 580 | handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) { |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 581 | I += (Processed - 1); |
| George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 582 | continue; |
| Rafael Espindola | 26d239c | 2016-03-22 13:24:29 +0000 | [diff] [blame] | 583 | } |
| George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 584 | |
| Rafael Espindola | 7ac9628 | 2016-04-27 12:47:30 +0000 | [diff] [blame] | 585 | if (Expr == R_GOT && !isRelRelative<ELFT>(Expr, Type, Body) && |
| 586 | Config->Shared) |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 587 | AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body, |
| 588 | getAddend<ELFT>(RI)}); |
| George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 589 | |
| Peter Collingbourne | 0a68cf5 | 2016-04-14 01:48:11 +0000 | [diff] [blame] | 590 | // If a symbol in a DSO is referenced directly instead of through GOT |
| 591 | // in a read-only section, we need to create a copy relocation for the |
| 592 | // symbol. |
| Rui Ueyama | cb8fd3a | 2016-03-13 04:40:12 +0000 | [diff] [blame] | 593 | if (auto *B = dyn_cast<SharedSymbol<ELFT>>(&Body)) { |
| Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame^] | 594 | if (!IsWrite && needsCopyRel(Expr, *B)) { |
| Simon Atanasyan | 2615c38 | 2016-04-10 21:48:55 +0000 | [diff] [blame] | 595 | if (!B->needsCopy()) |
| 596 | addCopyRelSymbol(B); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 597 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
| Rui Ueyama | 6d3874b | 2016-02-02 06:08:08 +0000 | [diff] [blame] | 598 | continue; |
| 599 | } |
| 600 | } |
| 601 | |
| George Rimar | 5cfd306 | 2016-04-14 17:05:56 +0000 | [diff] [blame] | 602 | bool Preemptible = Body.isPreemptible(); |
| 603 | |
| Rui Ueyama | 554f273 | 2016-02-02 07:07:34 +0000 | [diff] [blame] | 604 | // If a relocation needs PLT, we create a PLT and a GOT slot |
| 605 | // for the symbol. |
| Rafael Espindola | 34b335b | 2016-04-21 16:59:25 +0000 | [diff] [blame] | 606 | PltNeed NeedPlt = needsPlt(Expr, Type, Body); |
| Rafael Espindola | 852860e | 2016-02-12 15:47:37 +0000 | [diff] [blame] | 607 | if (NeedPlt) { |
| Rafael Espindola | 0c869a7 | 2016-04-21 16:57:32 +0000 | [diff] [blame] | 608 | if (NeedPlt == Plt_Implicit) |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 609 | Body.NeedsCopyOrPltAddr = true; |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 610 | RelExpr E = Expr; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 611 | if (Expr == R_PPC_OPD) |
| 612 | E = R_PPC_PLT_OPD; |
| 613 | else if (Expr == R_PC) |
| 614 | E = R_PLT_PC; |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 615 | else if (Expr == R_ABS) |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 616 | E = R_PLT; |
| 617 | C.Relocations.push_back({E, Type, Offset, Addend, &Body}); |
| 618 | |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 619 | if (Body.isInPlt()) |
| Rui Ueyama | 554f273 | 2016-02-02 07:07:34 +0000 | [diff] [blame] | 620 | continue; |
| 621 | Out<ELFT>::Plt->addEntry(Body); |
| 622 | |
| Rafael Espindola | 31d2ada | 2016-04-01 14:14:48 +0000 | [diff] [blame] | 623 | uint32_t Rel; |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 624 | if (Body.isGnuIFunc()) |
| Rafael Espindola | 31d2ada | 2016-04-01 14:14:48 +0000 | [diff] [blame] | 625 | Rel = Preemptible ? Target->PltRel : Target->IRelativeRel; |
| 626 | else |
| 627 | Rel = Target->UseLazyBinding ? Target->PltRel : Target->GotRel; |
| 628 | |
| Rui Ueyama | 554f273 | 2016-02-02 07:07:34 +0000 | [diff] [blame] | 629 | if (Target->UseLazyBinding) { |
| 630 | Out<ELFT>::GotPlt->addEntry(Body); |
| Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame^] | 631 | Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt, |
| 632 | Body.getGotPltOffset<ELFT>(), |
| 633 | !Preemptible, &Body, 0}); |
| Rui Ueyama | 554f273 | 2016-02-02 07:07:34 +0000 | [diff] [blame] | 634 | } else { |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 635 | if (Body.isInGot()) |
| Rui Ueyama | 554f273 | 2016-02-02 07:07:34 +0000 | [diff] [blame] | 636 | continue; |
| 637 | Out<ELFT>::Got->addEntry(Body); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 638 | AddDyn({Rel, Out<ELFT>::Got, Body.getGotOffset<ELFT>(), !Preemptible, |
| 639 | &Body, 0}); |
| Rui Ueyama | 554f273 | 2016-02-02 07:07:34 +0000 | [diff] [blame] | 640 | } |
| Rui Ueyama | 554f273 | 2016-02-02 07:07:34 +0000 | [diff] [blame] | 641 | continue; |
| 642 | } |
| 643 | |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 644 | // We decided not to use a plt. Optimize a reference to the plt to a |
| 645 | // reference to the symbol itself. |
| 646 | if (Expr == R_PLT_PC) |
| 647 | Expr = R_PC; |
| 648 | if (Expr == R_PPC_PLT_OPD) |
| 649 | Expr = R_PPC_OPD; |
| 650 | if (Expr == R_PLT) |
| 651 | Expr = R_ABS; |
| 652 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 653 | if (Target->needsThunk(Type, File, Body)) { |
| 654 | C.Relocations.push_back({R_THUNK, Type, Offset, Addend, &Body}); |
| 655 | continue; |
| 656 | } |
| 657 | |
| Rui Ueyama | f263c4b | 2016-02-02 07:07:35 +0000 | [diff] [blame] | 658 | // If a relocation needs GOT, we create a GOT slot for the symbol. |
| Rafael Espindola | c6b17bd | 2016-04-20 17:30:22 +0000 | [diff] [blame] | 659 | if (refersToGotEntry(Expr)) { |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 660 | uint32_t T = Body.isTls() ? Target->getTlsGotRel(Type) : Type; |
| Rafael Espindola | 58cd5db | 2016-04-19 22:46:03 +0000 | [diff] [blame] | 661 | if (Config->EMachine == EM_MIPS && Expr == R_GOT_OFF) |
| 662 | Addend -= MipsGPOffset; |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 663 | C.Relocations.push_back({Expr, T, Offset, Addend, &Body}); |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 664 | if (Body.isInGot()) |
| Rui Ueyama | f263c4b | 2016-02-02 07:07:35 +0000 | [diff] [blame] | 665 | continue; |
| 666 | Out<ELFT>::Got->addEntry(Body); |
| Rui Ueyama | 1ac1338 | 2016-02-02 05:55:28 +0000 | [diff] [blame] | 667 | |
| Simon Atanasyan | f3ec3be | 2016-03-22 08:36:48 +0000 | [diff] [blame] | 668 | if (Config->EMachine == EM_MIPS) |
| Rui Ueyama | f263c4b | 2016-02-02 07:07:35 +0000 | [diff] [blame] | 669 | // MIPS ABI has special rules to process GOT entries |
| 670 | // and doesn't require relocation entries for them. |
| 671 | // See "Global Offset Table" in Chapter 5 in the following document |
| 672 | // for detailed description: |
| 673 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 674 | continue; |
| 675 | |
| Rafael Espindola | 69a3806 | 2016-04-15 12:44:43 +0000 | [diff] [blame] | 676 | if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) { |
| Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 677 | uint32_t DynType; |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 678 | if (Body.isTls()) |
| Rafael Espindola | 2230483 | 2016-03-11 18:33:48 +0000 | [diff] [blame] | 679 | DynType = Target->TlsGotRel; |
| Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 680 | else if (Preemptible) |
| Rafael Espindola | 2230483 | 2016-03-11 18:33:48 +0000 | [diff] [blame] | 681 | DynType = Target->GotRel; |
| Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 682 | else |
| 683 | DynType = Target->RelativeRel; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 684 | AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(), |
| 685 | !Preemptible, &Body, 0}); |
| Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 686 | } |
| Rui Ueyama | f263c4b | 2016-02-02 07:07:35 +0000 | [diff] [blame] | 687 | continue; |
| Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 688 | } |
| Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 689 | |
| Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 690 | if (Preemptible) { |
| Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 691 | // We don't know anything about the finaly symbol. Just ask the dynamic |
| 692 | // linker to handle the relocation for us. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 693 | AddDyn({Target->getDynRel(Type), C.OutSec, Offset, false, &Body, Addend}); |
| Simon Atanasyan | ca58a8f | 2016-04-20 21:40:33 +0000 | [diff] [blame] | 694 | // MIPS ABI turns using of GOT and dynamic relocations inside out. |
| 695 | // While regular ABI uses dynamic relocations to fill up GOT entries |
| 696 | // MIPS ABI requires dynamic linker to fills up GOT entries using |
| 697 | // specially sorted dynamic symbol table. This affects even dynamic |
| 698 | // relocations against symbols which do not require GOT entries |
| 699 | // creation explicitly, i.e. do not have any GOT-relocations. So if |
| 700 | // a preemptible symbol has a dynamic relocation we anyway have |
| 701 | // to create a GOT entry for it. |
| 702 | // If a non-preemptible symbol has a dynamic relocation against it, |
| 703 | // dynamic linker takes it st_value, adds offset and writes down |
| 704 | // result of the dynamic relocation. In case of preemptible symbol |
| 705 | // dynamic linker performs symbol resolution, writes the symbol value |
| 706 | // to the GOT entry and reads the GOT entry when it needs to perform |
| 707 | // a dynamic relocation. |
| 708 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 |
| 709 | if (Config->EMachine == EM_MIPS && !Body.isInGot()) |
| 710 | Out<ELFT>::Got->addEntry(Body); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 711 | continue; |
| 712 | } |
| 713 | |
| Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 714 | // We know that this is the final symbol. If the program being produced |
| 715 | // is position independent, the final value is still not known. |
| 716 | // If the relocation depends on the symbol value (not the size or distances |
| 717 | // in the output), we still need some help from the dynamic linker. |
| 718 | // We can however do better than just copying the incoming relocation. We |
| 719 | // can process some of it and and just ask the dynamic linker to add the |
| 720 | // load address. |
| Rafael Espindola | 7ac9628 | 2016-04-27 12:47:30 +0000 | [diff] [blame] | 721 | if (!Config->Pic || isRelRelative<ELFT>(Expr, Type, Body)) { |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 722 | if (Config->EMachine == EM_MIPS && Body.isLocal() && |
| Rafael Espindola | 1763dc4 | 2016-04-26 22:00:04 +0000 | [diff] [blame] | 723 | (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32)) |
| Rafael Espindola | 475dbf4 | 2016-04-20 17:20:49 +0000 | [diff] [blame] | 724 | Addend += File.getMipsGp0(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 725 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
| 726 | continue; |
| 727 | } |
| 728 | |
| Rafael Espindola | 365e5f6 | 2016-04-27 11:54:07 +0000 | [diff] [blame] | 729 | if (Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC) |
| 730 | Addend += getPPC64TocBase(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 731 | AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body, Addend}); |
| Rafael Espindola | 365e5f6 | 2016-04-27 11:54:07 +0000 | [diff] [blame] | 732 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
| Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 733 | } |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 734 | |
| 735 | // Scan relocations for necessary thunks. |
| 736 | if (Config->EMachine == EM_MIPS) |
| 737 | scanRelocsForThunks(File, Rels); |
| Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| Rafael Espindola | a0fa848 | 2015-11-11 15:29:50 +0000 | [diff] [blame] | 740 | template <class ELFT> void Writer<ELFT>::scanRelocs(InputSection<ELFT> &C) { |
| Rui Ueyama | e5c1ec4 | 2016-04-28 03:04:15 +0000 | [diff] [blame] | 741 | // Scan all relocations. Each relocation goes through a series |
| 742 | // of tests to determine if it needs special treatment, such as |
| 743 | // creating GOT, PLT, copy relocations, etc. |
| Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame^] | 744 | // Note that relocations for non-alloc sections are directly |
| 745 | // processed by InputSection::relocateNative. |
| 746 | if (C.getSectionHdr()->sh_flags & SHF_ALLOC) |
| 747 | for (const Elf_Shdr *RelSec : C.RelocSections) |
| 748 | scanRelocs(C, *RelSec); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | template <class ELFT> |
| 752 | void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &S, |
| 753 | const Elf_Shdr &RelSec) { |
| 754 | ELFFile<ELFT> &EObj = S.getFile()->getObj(); |
| 755 | if (RelSec.sh_type == SHT_RELA) |
| 756 | scanRelocs(S, EObj.relas(&RelSec)); |
| 757 | else |
| 758 | scanRelocs(S, EObj.rels(&RelSec)); |
| Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 761 | template <class ELFT> |
| Rui Ueyama | 2a65a49 | 2016-01-05 20:01:29 +0000 | [diff] [blame] | 762 | static void reportUndefined(SymbolTable<ELFT> &Symtab, SymbolBody *Sym) { |
| Peter Collingbourne | 6d01a35 | 2016-04-24 02:31:04 +0000 | [diff] [blame] | 763 | if (!Config->NoUndefined) { |
| 764 | if (Config->Relocatable) |
| 765 | return; |
| 766 | if (Config->Shared) |
| 767 | if (Sym->Backref->Visibility == STV_DEFAULT) |
| 768 | return; |
| 769 | } |
| George Rimar | ee05828 | 2015-10-01 17:24:24 +0000 | [diff] [blame] | 770 | |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 771 | std::string Msg = "undefined symbol: " + Sym->getName().str(); |
| Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 772 | if (InputFile *File = Symtab.findFile(Sym)) |
| Rui Ueyama | 2a65a49 | 2016-01-05 20:01:29 +0000 | [diff] [blame] | 773 | Msg += " in " + File->getName().str(); |
| Rui Ueyama | 3f9f092 | 2016-03-08 04:06:29 +0000 | [diff] [blame] | 774 | if (Config->NoinhibitExec) |
| Rui Ueyama | 2a65a49 | 2016-01-05 20:01:29 +0000 | [diff] [blame] | 775 | warning(Msg); |
| Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 776 | else |
| Rui Ueyama | c2a0d7e | 2016-01-28 22:56:29 +0000 | [diff] [blame] | 777 | error(Msg); |
| Rafael Espindola | 6173f84 | 2015-09-23 14:37:01 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 780 | template <class ELFT> |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 781 | static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName, |
| 782 | const SymbolBody &B) { |
| 783 | if (B.isFile()) |
| Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 784 | return false; |
| 785 | |
| George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 786 | // We keep sections in symtab for relocatable output. |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 787 | if (B.isSection()) |
| George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 788 | return Config->Relocatable; |
| 789 | |
| Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 790 | // If sym references a section in a discarded group, don't keep it. |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 791 | if (Sec == &InputSection<ELFT>::Discarded) |
| Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 792 | return false; |
| 793 | |
| 794 | if (Config->DiscardNone) |
| 795 | return true; |
| 796 | |
| 797 | // In ELF assembly .L symbols are normally discarded by the assembler. |
| 798 | // If the assembler fails to do so, the linker discards them if |
| 799 | // * --discard-locals is used. |
| 800 | // * The symbol is in a SHF_MERGE section, which is normally the reason for |
| 801 | // the assembler keeping the .L symbol. |
| 802 | if (!SymName.startswith(".L") && !SymName.empty()) |
| 803 | return true; |
| 804 | |
| 805 | if (Config->DiscardLocals) |
| 806 | return false; |
| 807 | |
| 808 | return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE); |
| 809 | } |
| 810 | |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 811 | // Local symbols are not in the linker's symbol table. This function scans |
| 812 | // each object file's symbol table to copy local symbols to the output. |
| 813 | template <class ELFT> void Writer<ELFT>::copyLocalSymbols() { |
| Rui Ueyama | 90f76fb | 2016-01-21 03:07:38 +0000 | [diff] [blame] | 814 | if (!Out<ELFT>::SymTab) |
| 815 | return; |
| Rafael Espindola | 7862097 | 2016-03-11 16:41:23 +0000 | [diff] [blame] | 816 | for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F : |
| 817 | Symtab.getObjectFiles()) { |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 818 | const char *StrTab = F->getStringTable().data(); |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 819 | for (SymbolBody *B : F->getLocalSymbols()) { |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 820 | auto *DR = dyn_cast<DefinedRegular<ELFT>>(B); |
| 821 | // No reason to keep local undefined symbol in symtab. |
| 822 | if (!DR) |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 823 | continue; |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 824 | StringRef SymName(StrTab + B->getNameOffset()); |
| 825 | InputSectionBase<ELFT> *Sec = DR->Section; |
| 826 | if (!shouldKeepInSymtab<ELFT>(Sec, SymName, *B)) |
| 827 | continue; |
| Rafael Espindola | 0b9531c | 2016-04-22 22:09:35 +0000 | [diff] [blame] | 828 | if (Sec) { |
| 829 | if (!Sec->Live) |
| 830 | continue; |
| 831 | |
| 832 | // Garbage collection is normally able to remove local symbols if they |
| 833 | // point to gced sections. In the case of SHF_MERGE sections, we want it |
| 834 | // to also be able to drop them if part of the section is gced. |
| 835 | // We could look at the section offset map to keep some of these |
| 836 | // symbols, but almost all local symbols are .L* symbols, so it |
| 837 | // is probably not worth the complexity. |
| 838 | if (Config->GcSections && isa<MergeInputSection<ELFT>>(Sec)) |
| 839 | continue; |
| 840 | } |
| Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 841 | ++Out<ELFT>::SymTab->NumLocals; |
| George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 842 | if (Config->Relocatable) |
| Rui Ueyama | 98a4b8b | 2016-03-13 20:18:12 +0000 | [diff] [blame] | 843 | B->DynsymIndex = Out<ELFT>::SymTab->NumLocals; |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 844 | F->KeptLocalSyms.push_back( |
| 845 | std::make_pair(DR, Out<ELFT>::SymTab->StrTabSec.addString(SymName))); |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 850 | // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that |
| 851 | // we would like to make sure appear is a specific order to maximize their |
| 852 | // coverage by a single signed 16-bit offset from the TOC base pointer. |
| 853 | // Conversely, the special .tocbss section should be first among all SHT_NOBITS |
| 854 | // sections. This will put it next to the loaded special PPC64 sections (and, |
| 855 | // thus, within reach of the TOC base pointer). |
| 856 | static int getPPC64SectionRank(StringRef SectionName) { |
| 857 | return StringSwitch<int>(SectionName) |
| George Rimar | ee741cf | 2016-04-14 13:23:02 +0000 | [diff] [blame] | 858 | .Case(".tocbss", 0) |
| 859 | .Case(".branch_lt", 2) |
| 860 | .Case(".toc", 3) |
| 861 | .Case(".toc1", 4) |
| 862 | .Case(".opd", 5) |
| 863 | .Default(1); |
| Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 866 | template <class ELFT> static bool isRelroSection(OutputSectionBase<ELFT> *Sec) { |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 867 | if (!Config->ZRelro) |
| 868 | return false; |
| Rui Ueyama | 389aa8e | 2016-04-27 03:04:56 +0000 | [diff] [blame] | 869 | typename ELFT::uint Flags = Sec->getFlags(); |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 870 | if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE)) |
| 871 | return false; |
| Rui Ueyama | ccfc326 | 2015-12-10 19:13:08 +0000 | [diff] [blame] | 872 | if (Flags & SHF_TLS) |
| 873 | return true; |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 874 | uint32_t Type = Sec->getType(); |
| Rui Ueyama | ccfc326 | 2015-12-10 19:13:08 +0000 | [diff] [blame] | 875 | if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY || |
| 876 | Type == SHT_PREINIT_ARRAY) |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 877 | return true; |
| 878 | if (Sec == Out<ELFT>::GotPlt) |
| 879 | return Config->ZNow; |
| 880 | if (Sec == Out<ELFT>::Dynamic || Sec == Out<ELFT>::Got) |
| 881 | return true; |
| Rui Ueyama | 01faef0 | 2015-12-10 19:19:04 +0000 | [diff] [blame] | 882 | StringRef S = Sec->getName(); |
| 883 | return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" || |
| 884 | S == ".eh_frame"; |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 887 | // Output section ordering is determined by this function. |
| 888 | template <class ELFT> |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 889 | static bool compareSections(OutputSectionBase<ELFT> *A, |
| 890 | OutputSectionBase<ELFT> *B) { |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 891 | typedef typename ELFT::uint uintX_t; |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 892 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 893 | int Comp = Script<ELFT>::X->compareSections(A->getName(), B->getName()); |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 894 | if (Comp != 0) |
| 895 | return Comp < 0; |
| 896 | |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 897 | uintX_t AFlags = A->getFlags(); |
| 898 | uintX_t BFlags = B->getFlags(); |
| 899 | |
| 900 | // Allocatable sections go first to reduce the total PT_LOAD size and |
| 901 | // so debug info doesn't change addresses in actual code. |
| 902 | bool AIsAlloc = AFlags & SHF_ALLOC; |
| 903 | bool BIsAlloc = BFlags & SHF_ALLOC; |
| 904 | if (AIsAlloc != BIsAlloc) |
| 905 | return AIsAlloc; |
| 906 | |
| 907 | // We don't have any special requirements for the relative order of |
| 908 | // two non allocatable sections. |
| 909 | if (!AIsAlloc) |
| 910 | return false; |
| 911 | |
| 912 | // We want the read only sections first so that they go in the PT_LOAD |
| 913 | // covering the program headers at the start of the file. |
| 914 | bool AIsWritable = AFlags & SHF_WRITE; |
| 915 | bool BIsWritable = BFlags & SHF_WRITE; |
| 916 | if (AIsWritable != BIsWritable) |
| 917 | return BIsWritable; |
| 918 | |
| 919 | // For a corresponding reason, put non exec sections first (the program |
| 920 | // header PT_LOAD is not executable). |
| 921 | bool AIsExec = AFlags & SHF_EXECINSTR; |
| 922 | bool BIsExec = BFlags & SHF_EXECINSTR; |
| 923 | if (AIsExec != BIsExec) |
| 924 | return BIsExec; |
| 925 | |
| Hal Finkel | 0d7e83b | 2015-10-13 17:57:46 +0000 | [diff] [blame] | 926 | // If we got here we know that both A and B are in the same PT_LOAD. |
| Michael J. Spencer | 1bf7300 | 2015-10-16 23:11:07 +0000 | [diff] [blame] | 927 | |
| 928 | // The TLS initialization block needs to be a single contiguous block in a R/W |
| 929 | // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS |
| 930 | // sections are placed here as they don't take up virtual address space in the |
| 931 | // PT_LOAD. |
| Rui Ueyama | 61805ec | 2015-12-17 00:12:03 +0000 | [diff] [blame] | 932 | bool AIsTls = AFlags & SHF_TLS; |
| 933 | bool BIsTls = BFlags & SHF_TLS; |
| 934 | if (AIsTls != BIsTls) |
| 935 | return AIsTls; |
| Michael J. Spencer | 1bf7300 | 2015-10-16 23:11:07 +0000 | [diff] [blame] | 936 | |
| Hal Finkel | 08be614 | 2015-10-13 18:55:01 +0000 | [diff] [blame] | 937 | // The next requirement we have is to put nobits sections last. The |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 938 | // reason is that the only thing the dynamic linker will see about |
| 939 | // them is a p_memsz that is larger than p_filesz. Seeing that it |
| 940 | // zeros the end of the PT_LOAD, so that has to correspond to the |
| 941 | // nobits sections. |
| Hal Finkel | 600ff14 | 2015-10-13 19:27:12 +0000 | [diff] [blame] | 942 | bool AIsNoBits = A->getType() == SHT_NOBITS; |
| 943 | bool BIsNoBits = B->getType() == SHT_NOBITS; |
| 944 | if (AIsNoBits != BIsNoBits) |
| 945 | return BIsNoBits; |
| Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 946 | |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 947 | // We place RelRo section before plain r/w ones. |
| 948 | bool AIsRelRo = isRelroSection(A); |
| 949 | bool BIsRelRo = isRelroSection(B); |
| 950 | if (AIsRelRo != BIsRelRo) |
| 951 | return AIsRelRo; |
| 952 | |
| Hal Finkel | 9abc2a5 | 2015-10-13 19:07:29 +0000 | [diff] [blame] | 953 | // Some architectures have additional ordering restrictions for sections |
| 954 | // within the same PT_LOAD. |
| 955 | if (Config->EMachine == EM_PPC64) |
| 956 | return getPPC64SectionRank(A->getName()) < |
| 957 | getPPC64SectionRank(B->getName()); |
| 958 | |
| 959 | return false; |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| Rui Ueyama | b30f7356 | 2016-03-13 04:11:53 +0000 | [diff] [blame] | 962 | // The .bss section does not exist if no input file has a .bss section. |
| 963 | // This function creates one if that's the case. |
| 964 | template <class ELFT> void Writer<ELFT>::ensureBss() { |
| 965 | if (Out<ELFT>::Bss) |
| 966 | return; |
| 967 | Out<ELFT>::Bss = |
| 968 | new OutputSection<ELFT>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); |
| 969 | OwningSections.emplace_back(Out<ELFT>::Bss); |
| 970 | OutputSections.push_back(Out<ELFT>::Bss); |
| Rafael Espindola | 443f50a | 2015-11-03 21:35:14 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 973 | // Until this function is called, common symbols do not belong to any section. |
| 974 | // This function adds them to end of BSS section. |
| 975 | template <class ELFT> |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 976 | void Writer<ELFT>::addCommonSymbols(std::vector<DefinedCommon *> &Syms) { |
| Rafael Espindola | 443f50a | 2015-11-03 21:35:14 +0000 | [diff] [blame] | 977 | if (Syms.empty()) |
| 978 | return; |
| 979 | |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 980 | // Sort the common symbols by alignment as an heuristic to pack them better. |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 981 | std::stable_sort(Syms.begin(), Syms.end(), |
| 982 | [](const DefinedCommon *A, const DefinedCommon *B) { |
| Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 983 | return A->Alignment > B->Alignment; |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 984 | }); |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 985 | |
| Rui Ueyama | b30f7356 | 2016-03-13 04:11:53 +0000 | [diff] [blame] | 986 | ensureBss(); |
| 987 | uintX_t Off = Out<ELFT>::Bss->getSize(); |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 988 | for (DefinedCommon *C : Syms) { |
| Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 989 | Off = alignTo(Off, C->Alignment); |
| 990 | Out<ELFT>::Bss->updateAlign(C->Alignment); |
| Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 991 | C->OffsetInBss = Off; |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 992 | Off += C->Size; |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | Out<ELFT>::Bss->setSize(Off); |
| 996 | } |
| 997 | |
| Rui Ueyama | f50e1d8 | 2016-03-14 22:41:08 +0000 | [diff] [blame] | 998 | template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) { |
| 999 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 1000 | |
| 1001 | uintX_t SecAlign = SS->File->getSection(SS->Sym)->sh_addralign; |
| 1002 | uintX_t SymValue = SS->Sym.st_value; |
| George Rimar | ee741cf | 2016-04-14 13:23:02 +0000 | [diff] [blame] | 1003 | int TrailingZeros = |
| 1004 | std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue)); |
| Rui Ueyama | a969218 | 2016-03-13 04:05:42 +0000 | [diff] [blame] | 1005 | return 1 << TrailingZeros; |
| 1006 | } |
| 1007 | |
| Peter Collingbourne | 4cdade6 | 2016-04-04 22:29:24 +0000 | [diff] [blame] | 1008 | // Reserve space in .bss for copy relocation. |
| George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 1009 | template <class ELFT> |
| Peter Collingbourne | 4cdade6 | 2016-04-04 22:29:24 +0000 | [diff] [blame] | 1010 | void Writer<ELFT>::addCopyRelSymbol(SharedSymbol<ELFT> *SS) { |
| Rui Ueyama | b30f7356 | 2016-03-13 04:11:53 +0000 | [diff] [blame] | 1011 | ensureBss(); |
| Peter Collingbourne | 4cdade6 | 2016-04-04 22:29:24 +0000 | [diff] [blame] | 1012 | uintX_t Align = getAlignment(SS); |
| George Rimar | 2122168 | 2016-04-14 13:56:28 +0000 | [diff] [blame] | 1013 | uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Align); |
| Peter Collingbourne | 4cdade6 | 2016-04-04 22:29:24 +0000 | [diff] [blame] | 1014 | Out<ELFT>::Bss->setSize(Off + SS->template getSize<ELFT>()); |
| 1015 | Out<ELFT>::Bss->updateAlign(Align); |
| Peter Collingbourne | 4cdade6 | 2016-04-04 22:29:24 +0000 | [diff] [blame] | 1016 | uintX_t Shndx = SS->Sym.st_shndx; |
| 1017 | uintX_t Value = SS->Sym.st_value; |
| 1018 | // Look through the DSO's dynamic symbol for aliases and create a dynamic |
| 1019 | // symbol for each one. This causes the copy relocation to correctly interpose |
| 1020 | // any aliases. |
| 1021 | for (SharedSymbol<ELFT> &S : SS->File->getSharedSymbols()) { |
| 1022 | if (S.Sym.st_shndx != Shndx || S.Sym.st_value != Value) |
| 1023 | continue; |
| 1024 | S.OffsetInBss = Off; |
| 1025 | S.NeedsCopyOrPltAddr = true; |
| Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 1026 | S.Backref->IsUsedInRegularObj = true; |
| George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 1027 | } |
| Rafael Espindola | c012db3 | 2016-04-07 14:34:15 +0000 | [diff] [blame] | 1028 | Out<ELFT>::RelaDyn->addReloc( |
| 1029 | {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0}); |
| George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 1032 | template <class ELFT> |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1033 | StringRef Writer<ELFT>::getOutputSectionName(InputSectionBase<ELFT> *S) const { |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1034 | StringRef Dest = Script<ELFT>::X->getOutputSection(S); |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1035 | if (!Dest.empty()) |
| 1036 | return Dest; |
| Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 1037 | |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1038 | StringRef Name = S->getSectionName(); |
| Rafael Espindola | 1492820 | 2016-02-16 16:05:27 +0000 | [diff] [blame] | 1039 | for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.", |
| 1040 | ".init_array.", ".fini_array.", ".ctors.", ".dtors.", |
| Rafael Espindola | 8ae1290 | 2016-02-16 16:12:06 +0000 | [diff] [blame] | 1041 | ".tbss.", ".gcc_except_table.", ".tdata."}) |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1042 | if (Name.startswith(V)) |
| Rui Ueyama | c2dca5d | 2016-02-11 01:07:18 +0000 | [diff] [blame] | 1043 | return V.drop_back(); |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1044 | return Name; |
| Rui Ueyama | 4fcadaf | 2015-10-14 19:21:25 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 1047 | template <class ELFT> |
| George Rimar | a5fbebc | 2015-12-10 09:12:18 +0000 | [diff] [blame] | 1048 | void reportDiscarded(InputSectionBase<ELFT> *IS, |
| Rafael Espindola | 36a73d2 | 2016-03-11 16:32:46 +0000 | [diff] [blame] | 1049 | const std::unique_ptr<elf::ObjectFile<ELFT>> &File) { |
| Rui Ueyama | 8fc070d | 2016-02-24 00:23:15 +0000 | [diff] [blame] | 1050 | if (!Config->PrintGcSections || !IS || IS->Live) |
| George Rimar | a5fbebc | 2015-12-10 09:12:18 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | llvm::errs() << "removing unused section from '" << IS->getSectionName() |
| 1053 | << "' in file '" << File->getName() << "'\n"; |
| 1054 | } |
| 1055 | |
| 1056 | template <class ELFT> |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 1057 | bool Writer<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) const { |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1058 | return !S || S == &InputSection<ELFT>::Discarded || !S->Live || |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1059 | Script<ELFT>::X->isDiscarded(S); |
| Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 1062 | template <class ELFT> |
| 1063 | static SymbolBody * |
| 1064 | addOptionalSynthetic(SymbolTable<ELFT> &Table, StringRef Name, |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1065 | OutputSectionBase<ELFT> &Sec, typename ELFT::uint Val) { |
| Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 1066 | if (!Table.find(Name)) |
| 1067 | return nullptr; |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1068 | return Table.addSynthetic(Name, Sec, Val); |
| Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| Rui Ueyama | 0168722 | 2015-12-26 09:47:57 +0000 | [diff] [blame] | 1071 | // The beginning and the ending of .rel[a].plt section are marked |
| 1072 | // with __rel[a]_iplt_{start,end} symbols if it is a statically linked |
| 1073 | // executable. The runtime needs these symbols in order to resolve |
| 1074 | // all IRELATIVE relocs on startup. For dynamic executables, we don't |
| 1075 | // need these symbols, since IRELATIVE relocs are resolved through GOT |
| 1076 | // and PLT. For details, see http://www.airs.com/blog/archives/403. |
| George Rimar | ee741cf | 2016-04-14 13:23:02 +0000 | [diff] [blame] | 1077 | template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() { |
| Rui Ueyama | 0168722 | 2015-12-26 09:47:57 +0000 | [diff] [blame] | 1078 | if (isOutputDynamic() || !Out<ELFT>::RelaPlt) |
| George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 1079 | return; |
| Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 1080 | StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start"; |
| Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 1081 | ElfSym<ELFT>::RelaIpltStart = |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1082 | addOptionalSynthetic(Symtab, S, *Out<ELFT>::RelaPlt, 0); |
| Rui Ueyama | 0168722 | 2015-12-26 09:47:57 +0000 | [diff] [blame] | 1083 | |
| Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 1084 | S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end"; |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1085 | ElfSym<ELFT>::RelaIpltEnd = addOptionalSynthetic( |
| 1086 | Symtab, S, *Out<ELFT>::RelaPlt, DefinedSynthetic<ELFT>::SectionEnd); |
| George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| Rafael Espindola | ae53324 | 2015-12-23 20:37:51 +0000 | [diff] [blame] | 1089 | template <class ELFT> static bool includeInSymtab(const SymbolBody &B) { |
| Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 1090 | if (!B.Backref->IsUsedInRegularObj) |
| Rafael Espindola | ae53324 | 2015-12-23 20:37:51 +0000 | [diff] [blame] | 1091 | return false; |
| 1092 | |
| Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1093 | if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) { |
| Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1094 | // Exclude symbols pointing to garbage-collected sections. |
| Rui Ueyama | 8fc070d | 2016-02-24 00:23:15 +0000 | [diff] [blame] | 1095 | if (D->Section && !D->Section->Live) |
| Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1096 | return false; |
| 1097 | } |
| Rafael Espindola | ae53324 | 2015-12-23 20:37:51 +0000 | [diff] [blame] | 1098 | return true; |
| 1099 | } |
| 1100 | |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1101 | // This class knows how to create an output section for a given |
| 1102 | // input section. Output section type is determined by various |
| 1103 | // factors, including input section's sh_flags, sh_type and |
| 1104 | // linker scripts. |
| 1105 | namespace { |
| 1106 | template <class ELFT> class OutputSectionFactory { |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 1107 | typedef typename ELFT::Shdr Elf_Shdr; |
| 1108 | typedef typename ELFT::uint uintX_t; |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1109 | |
| 1110 | public: |
| 1111 | std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C, |
| 1112 | StringRef OutsecName); |
| 1113 | |
| George Rimar | d502f47 | 2016-04-14 14:07:54 +0000 | [diff] [blame] | 1114 | OutputSectionBase<ELFT> *lookup(StringRef Name, uint32_t Type, |
| 1115 | uintX_t Flags) { |
| George Rimar | 4c2ae3a | 2016-04-14 14:24:23 +0000 | [diff] [blame] | 1116 | return Map.lookup({Name, Type, Flags, 0}); |
| George Rimar | d502f47 | 2016-04-14 14:07:54 +0000 | [diff] [blame] | 1117 | } |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1118 | |
| 1119 | private: |
| 1120 | SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C, |
| 1121 | StringRef OutsecName); |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1122 | |
| 1123 | SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSectionBase<ELFT> *> Map; |
| 1124 | }; |
| 1125 | } |
| 1126 | |
| 1127 | template <class ELFT> |
| 1128 | std::pair<OutputSectionBase<ELFT> *, bool> |
| 1129 | OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C, |
| 1130 | StringRef OutsecName) { |
| 1131 | SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName); |
| 1132 | OutputSectionBase<ELFT> *&Sec = Map[Key]; |
| 1133 | if (Sec) |
| 1134 | return {Sec, false}; |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1135 | |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 1136 | switch (C->SectionKind) { |
| 1137 | case InputSectionBase<ELFT>::Regular: |
| Rui Ueyama | f949f7f | 2016-01-11 23:50:55 +0000 | [diff] [blame] | 1138 | Sec = new OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags); |
| 1139 | break; |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 1140 | case InputSectionBase<ELFT>::EHFrame: |
| Rui Ueyama | f949f7f | 2016-01-11 23:50:55 +0000 | [diff] [blame] | 1141 | Sec = new EHOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags); |
| 1142 | break; |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 1143 | case InputSectionBase<ELFT>::Merge: |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 1144 | Sec = new MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags, |
| 1145 | Key.Alignment); |
| Rui Ueyama | f949f7f | 2016-01-11 23:50:55 +0000 | [diff] [blame] | 1146 | break; |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 1147 | case InputSectionBase<ELFT>::MipsReginfo: |
| Rui Ueyama | f949f7f | 2016-01-11 23:50:55 +0000 | [diff] [blame] | 1148 | Sec = new MipsReginfoOutputSection<ELFT>(); |
| 1149 | break; |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 1150 | } |
| Rui Ueyama | f949f7f | 2016-01-11 23:50:55 +0000 | [diff] [blame] | 1151 | return {Sec, true}; |
| Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1154 | template <class ELFT> |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1155 | SectionKey<ELFT::Is64Bits> |
| 1156 | OutputSectionFactory<ELFT>::createKey(InputSectionBase<ELFT> *C, |
| 1157 | StringRef OutsecName) { |
| 1158 | const Elf_Shdr *H = C->getSectionHdr(); |
| Rui Ueyama | 63de917 | 2015-12-26 07:13:38 +0000 | [diff] [blame] | 1159 | uintX_t Flags = H->sh_flags & ~SHF_GROUP; |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1160 | |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 1161 | // For SHF_MERGE we create different output sections for each alignment. |
| 1162 | // This makes each output section simple and keeps a single level mapping from |
| 1163 | // input to output. |
| 1164 | uintX_t Alignment = 0; |
| George Rimar | 4e8cf85 | 2016-04-14 13:00:03 +0000 | [diff] [blame] | 1165 | if (isa<MergeInputSection<ELFT>>(C)) |
| 1166 | Alignment = std::max(H->sh_addralign, H->sh_entsize); |
| Rui Ueyama | 63de917 | 2015-12-26 07:13:38 +0000 | [diff] [blame] | 1167 | |
| George Rimar | 959d180 | 2016-04-28 13:38:10 +0000 | [diff] [blame] | 1168 | // GNU as can give .eh_frame section type SHT_PROGBITS or SHT_X86_64_UNWIND |
| Rui Ueyama | 63de917 | 2015-12-26 07:13:38 +0000 | [diff] [blame] | 1169 | // depending on the construct. We want to canonicalize it so that |
| 1170 | // there is only one .eh_frame in the end. |
| 1171 | uint32_t Type = H->sh_type; |
| 1172 | if (Type == SHT_PROGBITS && Config->EMachine == EM_X86_64 && |
| 1173 | isa<EHInputSection<ELFT>>(C)) |
| 1174 | Type = SHT_X86_64_UNWIND; |
| 1175 | |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 1176 | return SectionKey<ELFT::Is64Bits>{OutsecName, Type, Flags, Alignment}; |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 1179 | // The linker is expected to define some symbols depending on |
| 1180 | // the linking result. This function defines such symbols. |
| 1181 | template <class ELFT> void Writer<ELFT>::addReservedSymbols() { |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1182 | if (Config->EMachine == EM_MIPS) { |
| Rafael Espindola | 9b3f99e | 2016-04-12 02:24:43 +0000 | [diff] [blame] | 1183 | // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer |
| 1184 | // so that it points to an absolute address which is relative to GOT. |
| 1185 | // See "Global Data Symbols" in Chapter 6 in the following document: |
| 1186 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 1187 | ElfSym<ELFT>::MipsGp = |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1188 | Symtab.addSynthetic("_gp", *Out<ELFT>::Got, MipsGPOffset); |
| Rafael Espindola | 9b3f99e | 2016-04-12 02:24:43 +0000 | [diff] [blame] | 1189 | |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1190 | // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between |
| 1191 | // start of function and 'gp' pointer into GOT. |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1192 | ElfSym<ELFT>::MipsGpDisp = |
| 1193 | addOptionalSynthetic(Symtab, "_gp_disp", *Out<ELFT>::Got, MipsGPOffset); |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1194 | // The __gnu_local_gp is a magic symbol equal to the current value of 'gp' |
| 1195 | // pointer. This symbol is used in the code generated by .cpload pseudo-op |
| 1196 | // in case of using -mno-shared option. |
| 1197 | // https://sourceware.org/ml/binutils/2004-12/msg00094.html |
| Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 1198 | ElfSym<ELFT>::MipsLocalGp = addOptionalSynthetic( |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1199 | Symtab, "__gnu_local_gp", *Out<ELFT>::Got, MipsGPOffset); |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol |
| 1203 | // is magical and is used to produce a R_386_GOTPC relocation. |
| 1204 | // The R_386_GOTPC relocation value doesn't actually depend on the |
| 1205 | // symbol value, so it could use an index of STN_UNDEF which, according |
| 1206 | // to the spec, means the symbol value is 0. |
| 1207 | // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in |
| 1208 | // the object file. |
| 1209 | // The situation is even stranger on x86_64 where the assembly doesn't |
| 1210 | // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as |
| 1211 | // an undefined symbol in the .o files. |
| 1212 | // Given that the symbol is effectively unused, we just create a dummy |
| 1213 | // hidden one to avoid the undefined symbol error. |
| 1214 | if (!Config->Relocatable) |
| 1215 | Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_"); |
| 1216 | |
| Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 1217 | // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For |
| 1218 | // static linking the linker is required to optimize away any references to |
| 1219 | // __tls_get_addr, so it's not defined anywhere. Create a hidden definition |
| 1220 | // to avoid the undefined symbol error. |
| 1221 | if (!isOutputDynamic()) |
| 1222 | Symtab.addIgnored("__tls_get_addr"); |
| 1223 | |
| Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1224 | auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1, |
| 1225 | DefinedRegular<ELFT> *&Sym2) { |
| 1226 | Sym1 = Symtab.addIgnored(S, STV_DEFAULT); |
| Rui Ueyama | 4d169bd | 2016-02-26 16:38:39 +0000 | [diff] [blame] | 1227 | |
| 1228 | // The name without the underscore is not a reserved name, |
| 1229 | // so it is defined only when there is a reference against it. |
| Rui Ueyama | 68e1555 | 2016-02-26 16:49:54 +0000 | [diff] [blame] | 1230 | assert(S.startswith("_")); |
| Rui Ueyama | 4d169bd | 2016-02-26 16:38:39 +0000 | [diff] [blame] | 1231 | S = S.substr(1); |
| 1232 | if (SymbolBody *B = Symtab.find(S)) |
| George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 1233 | if (B->isUndefined()) |
| Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1234 | Sym2 = Symtab.addAbsolute(S, STV_DEFAULT); |
| George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 1235 | }; |
| 1236 | |
| Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1237 | Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2); |
| 1238 | Define("_etext", ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2); |
| 1239 | Define("_edata", ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2); |
| Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 1242 | // Sort input sections by section name suffixes for |
| 1243 | // __attribute__((init_priority(N))). |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 1244 | template <class ELFT> static void sortInitFini(OutputSectionBase<ELFT> *S) { |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 1245 | if (S) |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 1246 | reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini(); |
| 1247 | } |
| 1248 | |
| 1249 | // Sort input sections by the special rule for .ctors and .dtors. |
| 1250 | template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) { |
| 1251 | if (S) |
| 1252 | reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors(); |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1255 | // Create output section objects and add them to OutputSections. |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 1256 | template <class ELFT> void Writer<ELFT>::createSections() { |
| Rui Ueyama | 1b2a8bf | 2015-12-26 10:22:16 +0000 | [diff] [blame] | 1257 | // Add .interp first because some loaders want to see that section |
| 1258 | // on the first page of the executable file when loaded into memory. |
| Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 1259 | if (needsInterpSection()) |
| 1260 | OutputSections.push_back(Out<ELFT>::Interp); |
| 1261 | |
| Rui Ueyama | 28286cd | 2016-03-13 01:54:48 +0000 | [diff] [blame] | 1262 | // A core file does not usually contain unmodified segments except |
| 1263 | // the first page of the executable. Add the build ID section now |
| 1264 | // so that the section is included in the first page. |
| 1265 | if (Out<ELFT>::BuildId) |
| 1266 | OutputSections.push_back(Out<ELFT>::BuildId); |
| 1267 | |
| Rui Ueyama | 1b2a8bf | 2015-12-26 10:22:16 +0000 | [diff] [blame] | 1268 | // Create output sections for input object file sections. |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1269 | std::vector<OutputSectionBase<ELFT> *> RegularSections; |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1270 | OutputSectionFactory<ELFT> Factory; |
| Rafael Espindola | 7862097 | 2016-03-11 16:41:23 +0000 | [diff] [blame] | 1271 | for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F : |
| 1272 | Symtab.getObjectFiles()) { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1273 | for (InputSectionBase<ELFT> *C : F->getSections()) { |
| George Rimar | a5fbebc | 2015-12-10 09:12:18 +0000 | [diff] [blame] | 1274 | if (isDiscarded(C)) { |
| 1275 | reportDiscarded(C, F); |
| Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 1276 | continue; |
| George Rimar | a5fbebc | 2015-12-10 09:12:18 +0000 | [diff] [blame] | 1277 | } |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1278 | OutputSectionBase<ELFT> *Sec; |
| 1279 | bool IsNew; |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1280 | std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C)); |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1281 | if (IsNew) { |
| Rui Ueyama | d4ea7dd | 2015-12-26 07:01:26 +0000 | [diff] [blame] | 1282 | OwningSections.emplace_back(Sec); |
| Rafael Espindola | d13d960 | 2015-09-25 15:08:44 +0000 | [diff] [blame] | 1283 | OutputSections.push_back(Sec); |
| Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 1284 | RegularSections.push_back(Sec); |
| Rafael Espindola | d13d960 | 2015-09-25 15:08:44 +0000 | [diff] [blame] | 1285 | } |
| Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 1286 | Sec->addSection(C); |
| Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
| Rafael Espindola | 443f50a | 2015-11-03 21:35:14 +0000 | [diff] [blame] | 1290 | Out<ELFT>::Bss = static_cast<OutputSection<ELFT> *>( |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1291 | Factory.lookup(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE)); |
| Rafael Espindola | 443f50a | 2015-11-03 21:35:14 +0000 | [diff] [blame] | 1292 | |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1293 | // If we have a .opd section (used under PPC64 for function descriptors), |
| 1294 | // store a pointer to it here so that we can use it later when processing |
| 1295 | // relocations. |
| 1296 | Out<ELFT>::Opd = Factory.lookup(".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC); |
| 1297 | |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1298 | Out<ELFT>::Dynamic->PreInitArraySec = Factory.lookup( |
| 1299 | ".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC); |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1300 | Out<ELFT>::Dynamic->InitArraySec = |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1301 | Factory.lookup(".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC); |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1302 | Out<ELFT>::Dynamic->FiniArraySec = |
| Rui Ueyama | 3a1f036 | 2015-12-26 07:01:28 +0000 | [diff] [blame] | 1303 | Factory.lookup(".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC); |
| Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 1304 | |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 1305 | // Sort section contents for __attribute__((init_priority(N)). |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 1306 | sortInitFini(Out<ELFT>::Dynamic->InitArraySec); |
| 1307 | sortInitFini(Out<ELFT>::Dynamic->FiniArraySec); |
| 1308 | sortCtorsDtors(Factory.lookup(".ctors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC)); |
| 1309 | sortCtorsDtors(Factory.lookup(".dtors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC)); |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 1310 | |
| Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 1311 | // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop |
| 1312 | // symbols for sections, so that the runtime can get the start and end |
| 1313 | // addresses of each section by section name. Add such symbols. |
| George Rimar | c1034a8 | 2016-03-01 19:12:35 +0000 | [diff] [blame] | 1314 | if (!Config->Relocatable) { |
| 1315 | addStartEndSymbols(); |
| 1316 | for (OutputSectionBase<ELFT> *Sec : RegularSections) |
| 1317 | addStartStopSymbols(Sec); |
| 1318 | } |
| Rui Ueyama | d4530c6 | 2016-03-04 18:34:14 +0000 | [diff] [blame] | 1319 | |
| 1320 | // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type. |
| 1321 | // It should be okay as no one seems to care about the type. |
| 1322 | // Even the author of gold doesn't remember why gold behaves that way. |
| 1323 | // https://sourceware.org/ml/binutils/2002-03/msg00360.html |
| George Rimar | aa4dc20 | 2016-03-01 16:23:13 +0000 | [diff] [blame] | 1324 | if (isOutputDynamic()) |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1325 | Symtab.addSynthetic("_DYNAMIC", *Out<ELFT>::Dynamic, 0); |
| Rafael Espindola | 334c3e1 | 2015-10-19 15:21:42 +0000 | [diff] [blame] | 1326 | |
| Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 1327 | // Define __rel[a]_iplt_{start,end} symbols if needed. |
| 1328 | addRelIpltSymbols(); |
| 1329 | |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1330 | if (Out<ELFT>::EhFrameHdr->Sec) |
| 1331 | Out<ELFT>::EhFrameHdr->Sec->finalize(); |
| 1332 | |
| Rafael Espindola | 334c3e1 | 2015-10-19 15:21:42 +0000 | [diff] [blame] | 1333 | // Scan relocations. This must be done after every symbol is declared so that |
| 1334 | // we can correctly decide if a dynamic relocation is needed. |
| Rafael Espindola | 3828b88 | 2016-04-07 15:50:23 +0000 | [diff] [blame] | 1335 | // Check size() each time to guard against .bss being created. |
| 1336 | for (unsigned I = 0; I < OutputSections.size(); ++I) { |
| 1337 | OutputSectionBase<ELFT> *Sec = OutputSections[I]; |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1338 | Sec->forEachInputSection([&](InputSectionBase<ELFT> *S) { |
| 1339 | if (auto *IS = dyn_cast<InputSection<ELFT>>(S)) { |
| 1340 | // Set OutSecOff so that scanRelocs can use it. |
| 1341 | uintX_t Off = alignTo(Sec->getSize(), S->Align); |
| 1342 | IS->OutSecOff = Off; |
| Rafael Espindola | 334c3e1 | 2015-10-19 15:21:42 +0000 | [diff] [blame] | 1343 | |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1344 | scanRelocs(*IS); |
| 1345 | |
| 1346 | // Now that scan relocs possibly changed the size, update the offset. |
| 1347 | Sec->setSize(Off + S->getSize()); |
| 1348 | } else if (auto *EH = dyn_cast<EHInputSection<ELFT>>(S)) { |
| 1349 | if (EH->RelocSection) |
| 1350 | scanRelocs(*EH, *EH->RelocSection); |
| 1351 | } |
| 1352 | }); |
| 1353 | } |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 1354 | |
| Rui Ueyama | 1b2a8bf | 2015-12-26 10:22:16 +0000 | [diff] [blame] | 1355 | // Now that we have defined all possible symbols including linker- |
| 1356 | // synthesized ones. Visit all symbols to give the finishing touches. |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 1357 | std::vector<DefinedCommon *> CommonSymbols; |
| Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 1358 | for (Symbol *S : Symtab.getSymbols()) { |
| 1359 | SymbolBody *Body = S->Body; |
| Rafael Espindola | 0baa73f | 2016-04-26 13:56:26 +0000 | [diff] [blame] | 1360 | |
| 1361 | // Set "used" bit for --as-needed. |
| 1362 | if (S->IsUsedInRegularObj && !S->isWeak()) |
| 1363 | if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body)) |
| 1364 | SS->File->IsUsed = true; |
| 1365 | |
| Peter Collingbourne | 892d4980 | 2016-04-27 00:05:03 +0000 | [diff] [blame] | 1366 | if (Body->isUndefined() && !S->isWeak()) |
| 1367 | reportUndefined<ELFT>(Symtab, Body); |
| Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1368 | |
| Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 1369 | if (auto *C = dyn_cast<DefinedCommon>(Body)) |
| Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1370 | CommonSymbols.push_back(C); |
| George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 1371 | |
| Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 1372 | if (!includeInSymtab<ELFT>(*Body)) |
| Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1373 | continue; |
| George Rimar | 5dad7c1 | 2015-10-24 08:52:46 +0000 | [diff] [blame] | 1374 | if (Out<ELFT>::SymTab) |
| 1375 | Out<ELFT>::SymTab->addSymbol(Body); |
| Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1376 | |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1377 | if (isOutputDynamic() && S->includeInDynsym()) { |
| Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1378 | Out<ELFT>::DynSymTab->addSymbol(Body); |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1379 | if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body)) |
| 1380 | Out<ELFT>::VerNeed->addSymbol(SS); |
| 1381 | } |
| Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1382 | } |
| Rui Ueyama | c2a0d7e | 2016-01-28 22:56:29 +0000 | [diff] [blame] | 1383 | |
| 1384 | // Do not proceed if there was an undefined symbol. |
| 1385 | if (HasError) |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 1386 | return; |
| Rui Ueyama | c2a0d7e | 2016-01-28 22:56:29 +0000 | [diff] [blame] | 1387 | |
| Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 1388 | addCommonSymbols(CommonSymbols); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1389 | |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1390 | // So far we have added sections from input object files. |
| 1391 | // This function adds linker-created Out<ELFT>::* sections. |
| 1392 | addPredefinedSections(); |
| 1393 | |
| 1394 | std::stable_sort(OutputSections.begin(), OutputSections.end(), |
| 1395 | compareSections<ELFT>); |
| 1396 | |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1397 | unsigned I = 1; |
| 1398 | for (OutputSectionBase<ELFT> *Sec : OutputSections) { |
| 1399 | Sec->SectionIndex = I++; |
| Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 1400 | Sec->setSHName(Out<ELFT>::ShStrTab->addString(Sec->getName())); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1401 | } |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1402 | |
| 1403 | // Finalizers fix each section's size. |
| Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 1404 | // .dynsym is finalized early since that may fill up .gnu.hash. |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1405 | if (isOutputDynamic()) |
| 1406 | Out<ELFT>::DynSymTab->finalize(); |
| 1407 | |
| Simon Atanasyan | 40d25f3 | 2016-02-02 09:07:47 +0000 | [diff] [blame] | 1408 | // Fill other section headers. The dynamic table is finalized |
| 1409 | // at the end because some tags like RELSZ depend on result |
| 1410 | // of finalizing other sections. The dynamic string table is |
| 1411 | // finalized once the .dynamic finalizer has added a few last |
| 1412 | // strings. See DynamicSection::finalize() |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1413 | for (OutputSectionBase<ELFT> *Sec : OutputSections) |
| Simon Atanasyan | 40d25f3 | 2016-02-02 09:07:47 +0000 | [diff] [blame] | 1414 | if (Sec != Out<ELFT>::DynStrTab && Sec != Out<ELFT>::Dynamic) |
| Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 1415 | Sec->finalize(); |
| Simon Atanasyan | 40d25f3 | 2016-02-02 09:07:47 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (isOutputDynamic()) |
| 1418 | Out<ELFT>::Dynamic->finalize(); |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| Rui Ueyama | 3095148 | 2016-02-25 19:34:37 +0000 | [diff] [blame] | 1421 | template <class ELFT> bool Writer<ELFT>::needsGot() { |
| 1422 | if (!Out<ELFT>::Got->empty()) |
| 1423 | return true; |
| 1424 | |
| 1425 | // We add the .got section to the result for dynamic MIPS target because |
| 1426 | // its address and properties are mentioned in the .dynamic section. |
| Rafael Espindola | a22b082 | 2016-04-12 13:21:13 +0000 | [diff] [blame] | 1427 | if (Config->EMachine == EM_MIPS) |
| Rui Ueyama | 3095148 | 2016-02-25 19:34:37 +0000 | [diff] [blame] | 1428 | return true; |
| 1429 | |
| 1430 | // If we have a relocation that is relative to GOT (such as GOTOFFREL), |
| 1431 | // we need to emit a GOT even if it's empty. |
| 1432 | return HasGotOffRel; |
| 1433 | } |
| 1434 | |
| Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1435 | // This function add Out<ELFT>::* sections to OutputSections. |
| 1436 | template <class ELFT> void Writer<ELFT>::addPredefinedSections() { |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1437 | auto Add = [&](OutputSectionBase<ELFT> *C) { |
| 1438 | if (C) |
| 1439 | OutputSections.push_back(C); |
| 1440 | }; |
| 1441 | |
| Rui Ueyama | c6ef3f2 | 2015-10-15 21:50:30 +0000 | [diff] [blame] | 1442 | // This order is not the same as the final output order |
| 1443 | // because we sort the sections using their attributes below. |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1444 | Add(Out<ELFT>::SymTab); |
| 1445 | Add(Out<ELFT>::ShStrTab); |
| 1446 | Add(Out<ELFT>::StrTab); |
| Michael J. Spencer | 350e5b5 | 2015-10-12 23:39:23 +0000 | [diff] [blame] | 1447 | if (isOutputDynamic()) { |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1448 | Add(Out<ELFT>::DynSymTab); |
| Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1449 | if (Out<ELFT>::VerNeed->getNeedNum() != 0) { |
| 1450 | Add(Out<ELFT>::VerSym); |
| 1451 | Add(Out<ELFT>::VerNeed); |
| 1452 | } |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1453 | Add(Out<ELFT>::GnuHashTab); |
| 1454 | Add(Out<ELFT>::HashTab); |
| 1455 | Add(Out<ELFT>::Dynamic); |
| 1456 | Add(Out<ELFT>::DynStrTab); |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1457 | if (Out<ELFT>::RelaDyn->hasRelocs()) |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1458 | Add(Out<ELFT>::RelaDyn); |
| Rui Ueyama | a354c5c | 2016-02-25 23:54:49 +0000 | [diff] [blame] | 1459 | Add(Out<ELFT>::MipsRldMap); |
| Rafael Espindola | 3f4228f | 2015-09-09 15:33:08 +0000 | [diff] [blame] | 1460 | } |
| Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 1461 | |
| George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 1462 | // We always need to add rel[a].plt to output if it has entries. |
| 1463 | // Even during static linking it can contain R_[*]_IRELATIVE relocations. |
| 1464 | if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) { |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1465 | Add(Out<ELFT>::RelaPlt); |
| George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 1466 | Out<ELFT>::RelaPlt->Static = !isOutputDynamic(); |
| 1467 | } |
| 1468 | |
| Rui Ueyama | 3095148 | 2016-02-25 19:34:37 +0000 | [diff] [blame] | 1469 | if (needsGot()) |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1470 | Add(Out<ELFT>::Got); |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 1471 | if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty()) |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1472 | Add(Out<ELFT>::GotPlt); |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1473 | if (!Out<ELFT>::Plt->empty()) |
| Rui Ueyama | 5a4ae1f | 2015-12-26 10:34:33 +0000 | [diff] [blame] | 1474 | Add(Out<ELFT>::Plt); |
| George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 1475 | if (Out<ELFT>::EhFrameHdr->Live) |
| 1476 | Add(Out<ELFT>::EhFrameHdr); |
| Rafael Espindola | abad618 | 2015-08-13 15:23:46 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 1479 | // The linker is expected to define SECNAME_start and SECNAME_end |
| 1480 | // symbols for a few sections. This function defines them. |
| 1481 | template <class ELFT> void Writer<ELFT>::addStartEndSymbols() { |
| 1482 | auto Define = [&](StringRef Start, StringRef End, |
| 1483 | OutputSectionBase<ELFT> *OS) { |
| 1484 | if (OS) { |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1485 | Symtab.addSynthetic(Start, *OS, 0); |
| 1486 | Symtab.addSynthetic(End, *OS, DefinedSynthetic<ELFT>::SectionEnd); |
| Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 1487 | } else { |
| 1488 | Symtab.addIgnored(Start); |
| 1489 | Symtab.addIgnored(End); |
| 1490 | } |
| 1491 | }; |
| 1492 | |
| 1493 | Define("__preinit_array_start", "__preinit_array_end", |
| 1494 | Out<ELFT>::Dynamic->PreInitArraySec); |
| 1495 | Define("__init_array_start", "__init_array_end", |
| 1496 | Out<ELFT>::Dynamic->InitArraySec); |
| 1497 | Define("__fini_array_start", "__fini_array_end", |
| 1498 | Out<ELFT>::Dynamic->FiniArraySec); |
| 1499 | } |
| 1500 | |
| Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 1501 | // If a section name is valid as a C identifier (which is rare because of |
| 1502 | // the leading '.'), linkers are expected to define __start_<secname> and |
| 1503 | // __stop_<secname> symbols. They are at beginning and end of the section, |
| 1504 | // respectively. This is not requested by the ELF standard, but GNU ld and |
| 1505 | // gold provide the feature, and used by many programs. |
| 1506 | template <class ELFT> |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1507 | void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) { |
| Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 1508 | StringRef S = Sec->getName(); |
| 1509 | if (!isValidCIdentifier(S)) |
| 1510 | return; |
| 1511 | StringSaver Saver(Alloc); |
| 1512 | StringRef Start = Saver.save("__start_" + S); |
| 1513 | StringRef Stop = Saver.save("__stop_" + S); |
| Rui Ueyama | 2ef58a1 | 2016-01-05 20:35:16 +0000 | [diff] [blame] | 1514 | if (SymbolBody *B = Symtab.find(Start)) |
| 1515 | if (B->isUndefined()) |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1516 | Symtab.addSynthetic(Start, *Sec, 0); |
| Rui Ueyama | 2ef58a1 | 2016-01-05 20:35:16 +0000 | [diff] [blame] | 1517 | if (SymbolBody *B = Symtab.find(Stop)) |
| 1518 | if (B->isUndefined()) |
| Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 1519 | Symtab.addSynthetic(Stop, *Sec, DefinedSynthetic<ELFT>::SectionEnd); |
| Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1522 | template <class ELFT> static bool needsPtLoad(OutputSectionBase<ELFT> *Sec) { |
| 1523 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| 1524 | return false; |
| 1525 | |
| 1526 | // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is |
| 1527 | // responsible for allocating space for them, not the PT_LOAD that |
| 1528 | // contains the TLS initialization image. |
| 1529 | if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) |
| 1530 | return false; |
| 1531 | return true; |
| Michael J. Spencer | 1d299a8 | 2015-09-09 20:48:09 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| Rui Ueyama | 9eb7ed0 | 2015-10-24 18:22:59 +0000 | [diff] [blame] | 1534 | static uint32_t toPhdrFlags(uint64_t Flags) { |
| 1535 | uint32_t Ret = PF_R; |
| 1536 | if (Flags & SHF_WRITE) |
| 1537 | Ret |= PF_W; |
| 1538 | if (Flags & SHF_EXECINSTR) |
| 1539 | Ret |= PF_X; |
| 1540 | return Ret; |
| 1541 | } |
| 1542 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1543 | // Decide which program headers to create and which sections to include in each |
| 1544 | // one. |
| 1545 | template <class ELFT> void Writer<ELFT>::createPhdrs() { |
| 1546 | auto AddHdr = [this](unsigned Type, unsigned Flags) { |
| 1547 | return &*Phdrs.emplace(Phdrs.end(), Type, Flags); |
| 1548 | }; |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 1549 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1550 | auto AddSec = [](Phdr &Hdr, OutputSectionBase<ELFT> *Sec) { |
| 1551 | Hdr.Last = Sec; |
| 1552 | if (!Hdr.First) |
| 1553 | Hdr.First = Sec; |
| 1554 | Hdr.H.p_align = std::max<uintX_t>(Hdr.H.p_align, Sec->getAlign()); |
| 1555 | }; |
| Rui Ueyama | 3486fe5 | 2015-10-11 17:44:22 +0000 | [diff] [blame] | 1556 | |
| Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1557 | // The first phdr entry is PT_PHDR which describes the program header itself. |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1558 | Phdr &Hdr = *AddHdr(PT_PHDR, PF_R); |
| 1559 | AddSec(Hdr, Out<ELFT>::ProgramHeaders); |
| Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 1560 | |
| Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1561 | // PT_INTERP must be the second entry if exists. |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1562 | if (needsInterpSection()) { |
| 1563 | Phdr &Hdr = *AddHdr(PT_INTERP, toPhdrFlags(Out<ELFT>::Interp->getFlags())); |
| 1564 | AddSec(Hdr, Out<ELFT>::Interp); |
| 1565 | } |
| Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 1566 | |
| Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1567 | // Add the first PT_LOAD segment for regular output sections. |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1568 | uintX_t Flags = PF_R; |
| 1569 | Phdr *Load = AddHdr(PT_LOAD, Flags); |
| 1570 | AddSec(*Load, Out<ELFT>::ElfHeader); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1571 | AddSec(*Load, Out<ELFT>::ProgramHeaders); |
| Rafael Espindola | 0a2e211 | 2015-09-10 15:41:34 +0000 | [diff] [blame] | 1572 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1573 | Phdr TlsHdr(PT_TLS, PF_R); |
| 1574 | Phdr RelRo(PT_GNU_RELRO, PF_R); |
| Rafael Espindola | 9907eb0 | 2016-03-01 13:23:29 +0000 | [diff] [blame] | 1575 | Phdr Note(PT_NOTE, PF_R); |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1576 | for (OutputSectionBase<ELFT> *Sec : OutputSections) { |
| Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1577 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1578 | break; |
| Rafael Espindola | 97cbe3e3 | 2015-12-23 15:20:38 +0000 | [diff] [blame] | 1579 | |
| Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1580 | // If we meet TLS section then we create TLS header |
| 1581 | // and put all TLS sections inside for futher use when |
| 1582 | // assign addresses. |
| 1583 | if (Sec->getFlags() & SHF_TLS) |
| 1584 | AddSec(TlsHdr, Sec); |
| 1585 | |
| 1586 | if (!needsPtLoad<ELFT>(Sec)) |
| 1587 | continue; |
| 1588 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1589 | // If flags changed then we want new load segment. |
| 1590 | uintX_t NewFlags = toPhdrFlags(Sec->getFlags()); |
| 1591 | if (Flags != NewFlags) { |
| Rafael Espindola | e090fb2 | 2016-03-09 21:37:22 +0000 | [diff] [blame] | 1592 | Load = AddHdr(PT_LOAD, NewFlags); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1593 | Flags = NewFlags; |
| 1594 | } |
| Michael J. Spencer | 78aa1de | 2015-11-03 00:34:39 +0000 | [diff] [blame] | 1595 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1596 | AddSec(*Load, Sec); |
| 1597 | |
| 1598 | if (isRelroSection(Sec)) |
| 1599 | AddSec(RelRo, Sec); |
| Rafael Espindola | 9907eb0 | 2016-03-01 13:23:29 +0000 | [diff] [blame] | 1600 | if (Sec->getType() == SHT_NOTE) |
| 1601 | AddSec(Note, Sec); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1602 | } |
| Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 1603 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1604 | // Add the TLS segment unless it's empty. |
| 1605 | if (TlsHdr.First) |
| 1606 | Phdrs.push_back(std::move(TlsHdr)); |
| Michael J. Spencer | 78aa1de | 2015-11-03 00:34:39 +0000 | [diff] [blame] | 1607 | |
| Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1608 | // Add an entry for .dynamic. |
| Michael J. Spencer | 350e5b5 | 2015-10-12 23:39:23 +0000 | [diff] [blame] | 1609 | if (isOutputDynamic()) { |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1610 | Phdr &H = *AddHdr(PT_DYNAMIC, toPhdrFlags(Out<ELFT>::Dynamic->getFlags())); |
| 1611 | AddSec(H, Out<ELFT>::Dynamic); |
| Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 1612 | } |
| Rafael Espindola | 91009b3 | 2015-08-12 01:45:28 +0000 | [diff] [blame] | 1613 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1614 | // PT_GNU_RELRO includes all sections that should be marked as |
| 1615 | // read-only by dynamic linker after proccessing relocations. |
| 1616 | if (RelRo.First) |
| 1617 | Phdrs.push_back(std::move(RelRo)); |
| George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 1618 | |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1619 | // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr. |
| George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 1620 | if (Out<ELFT>::EhFrameHdr->Live) { |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1621 | Phdr &Hdr = *AddHdr(PT_GNU_EH_FRAME, |
| 1622 | toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags())); |
| 1623 | AddSec(Hdr, Out<ELFT>::EhFrameHdr); |
| George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| Rui Ueyama | e79b09a | 2015-11-21 22:19:32 +0000 | [diff] [blame] | 1626 | // PT_GNU_STACK is a special section to tell the loader to make the |
| 1627 | // pages for the stack non-executable. |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1628 | if (!Config->ZExecStack) |
| 1629 | AddHdr(PT_GNU_STACK, PF_R | PF_W); |
| Rafael Espindola | 9907eb0 | 2016-03-01 13:23:29 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (Note.First) |
| 1632 | Phdrs.push_back(std::move(Note)); |
| George Rimar | 687788c | 2016-04-01 17:30:52 +0000 | [diff] [blame] | 1633 | |
| 1634 | Out<ELFT>::ProgramHeaders->setSize(sizeof(Elf_Phdr) * Phdrs.size()); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1637 | // The first section of each PT_LOAD and the first section after PT_GNU_RELRO |
| 1638 | // have to be page aligned so that the dynamic linker can set the permissions. |
| 1639 | template <class ELFT> void Writer<ELFT>::fixSectionAlignments() { |
| 1640 | for (const Phdr &P : Phdrs) |
| 1641 | if (P.H.p_type == PT_LOAD) |
| 1642 | P.First->PageAlign = true; |
| 1643 | |
| 1644 | for (const Phdr &P : Phdrs) { |
| 1645 | if (P.H.p_type != PT_GNU_RELRO) |
| 1646 | continue; |
| 1647 | // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we |
| 1648 | // have to align it to a page. |
| 1649 | auto End = OutputSections.end(); |
| 1650 | auto I = std::find(OutputSections.begin(), End, P.Last); |
| 1651 | if (I == End || (I + 1) == End) |
| 1652 | continue; |
| 1653 | OutputSectionBase<ELFT> *Sec = *(I + 1); |
| 1654 | if (needsPtLoad(Sec)) |
| 1655 | Sec->PageAlign = true; |
| 1656 | } |
| 1657 | } |
| 1658 | |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1659 | // We should set file offsets and VAs for elf header and program headers |
| 1660 | // sections. These are special, we do not include them into output sections |
| 1661 | // list, but have them to simplify the code. |
| 1662 | template <class ELFT> void Writer<ELFT>::fixHeaders() { |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1663 | uintX_t BaseVA = ScriptConfig->DoLayout ? 0 : Target->getVAStart(); |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 1664 | Out<ELFT>::ElfHeader->setVA(BaseVA); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1665 | Out<ELFT>::ElfHeader->setFileOffset(0); |
| 1666 | uintX_t Off = Out<ELFT>::ElfHeader->getSize(); |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 1667 | Out<ELFT>::ProgramHeaders->setVA(Off + BaseVA); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1668 | Out<ELFT>::ProgramHeaders->setFileOffset(Off); |
| 1669 | } |
| 1670 | |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1671 | // Assign VAs (addresses at run-time) to output sections. |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1672 | template <class ELFT> void Writer<ELFT>::assignAddresses() { |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1673 | uintX_t VA = Target->getVAStart() + Out<ELFT>::ElfHeader->getSize() + |
| 1674 | Out<ELFT>::ProgramHeaders->getSize(); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1675 | |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1676 | uintX_t ThreadBssOffset = 0; |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1677 | for (OutputSectionBase<ELFT> *Sec : OutputSections) { |
| 1678 | uintX_t Align = Sec->getAlign(); |
| Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1679 | if (Sec->PageAlign) |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1680 | Align = std::max<uintX_t>(Align, Target->PageSize); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1681 | |
| 1682 | // We only assign VAs to allocated sections. |
| Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1683 | if (needsPtLoad<ELFT>(Sec)) { |
| 1684 | VA = alignTo(VA, Align); |
| 1685 | Sec->setVA(VA); |
| 1686 | VA += Sec->getSize(); |
| 1687 | } else if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) { |
| 1688 | uintX_t TVA = VA + ThreadBssOffset; |
| 1689 | TVA = alignTo(TVA, Align); |
| 1690 | Sec->setVA(TVA); |
| 1691 | ThreadBssOffset = TVA - VA + Sec->getSize(); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1692 | } |
| Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 1693 | } |
| George Rimar | 900a260 | 2016-04-01 10:49:14 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| George Rimar | 5f85732 | 2016-04-27 09:16:28 +0000 | [diff] [blame] | 1696 | // Adjusts the file alignment for a given output section and returns |
| 1697 | // its new file offset. The file offset must be the same with its |
| 1698 | // virtual address (modulo the page size) so that the loader can load |
| 1699 | // executables without any address adjustment. |
| 1700 | template <class ELFT, class uintX_t> |
| 1701 | static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase<ELFT> *Sec) { |
| 1702 | uintX_t Align = Sec->getAlign(); |
| 1703 | if (Sec->PageAlign) |
| 1704 | Align = std::max<uintX_t>(Align, Target->PageSize); |
| 1705 | Off = alignTo(Off, Align); |
| 1706 | |
| 1707 | // Relocatable output does not have program headers |
| 1708 | // and does not need any other offset adjusting. |
| 1709 | if (Config->Relocatable || !(Sec->getFlags() & SHF_ALLOC)) |
| 1710 | return Off; |
| 1711 | return alignTo(Off, Target->PageSize, Sec->getVA()); |
| 1712 | } |
| 1713 | |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1714 | // Assign file offsets to output sections. |
| 1715 | template <class ELFT> void Writer<ELFT>::assignFileOffsets() { |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1716 | uintX_t Off = |
| 1717 | Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
| 1718 | |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1719 | for (OutputSectionBase<ELFT> *Sec : OutputSections) { |
| 1720 | if (Sec->getType() == SHT_NOBITS) { |
| 1721 | Sec->setFileOffset(Off); |
| 1722 | continue; |
| 1723 | } |
| George Rimar | 5f85732 | 2016-04-27 09:16:28 +0000 | [diff] [blame] | 1724 | |
| 1725 | Off = getFileAlignment<ELFT>(Off, Sec); |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1726 | Sec->setFileOffset(Off); |
| 1727 | Off += Sec->getSize(); |
| 1728 | } |
| 1729 | SectionHeaderOff = alignTo(Off, sizeof(uintX_t)); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1730 | FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr); |
| Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | // Finalize the program headers. We call this function after we assign |
| 1734 | // file offsets and VAs to all sections. |
| 1735 | template <class ELFT> void Writer<ELFT>::setPhdrs() { |
| Rui Ueyama | e8a45e4 | 2016-04-01 22:42:04 +0000 | [diff] [blame] | 1736 | for (Phdr &P : Phdrs) { |
| 1737 | Elf_Phdr &H = P.H; |
| 1738 | OutputSectionBase<ELFT> *First = P.First; |
| 1739 | OutputSectionBase<ELFT> *Last = P.Last; |
| 1740 | if (First) { |
| 1741 | H.p_filesz = Last->getFileOff() - First->getFileOff(); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1742 | if (Last->getType() != SHT_NOBITS) |
| 1743 | H.p_filesz += Last->getSize(); |
| Rui Ueyama | e8a45e4 | 2016-04-01 22:42:04 +0000 | [diff] [blame] | 1744 | H.p_memsz = Last->getVA() + Last->getSize() - First->getVA(); |
| 1745 | H.p_offset = First->getFileOff(); |
| 1746 | H.p_vaddr = First->getVA(); |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1747 | } |
| George Rimar | 6de3f63 | 2016-03-01 08:46:03 +0000 | [diff] [blame] | 1748 | if (H.p_type == PT_LOAD) |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1749 | H.p_align = Target->PageSize; |
| George Rimar | 6de3f63 | 2016-03-01 08:46:03 +0000 | [diff] [blame] | 1750 | else if (H.p_type == PT_GNU_RELRO) |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1751 | H.p_align = 1; |
| 1752 | H.p_paddr = H.p_vaddr; |
| 1753 | |
| 1754 | // The TLS pointer goes after PT_TLS. At least glibc will align it, |
| 1755 | // so round up the size to make sure the offsets are correct. |
| George Rimar | 6de3f63 | 2016-03-01 08:46:03 +0000 | [diff] [blame] | 1756 | if (H.p_type == PT_TLS) { |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1757 | Out<ELFT>::TlsPhdr = &H; |
| 1758 | H.p_memsz = alignTo(H.p_memsz, H.p_align); |
| Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| Rui Ueyama | 22b5d1f | 2016-03-13 19:29:17 +0000 | [diff] [blame] | 1763 | static uint32_t getMipsEFlags() { |
| Simon Atanasyan | 034c4cd | 2015-12-19 05:51:49 +0000 | [diff] [blame] | 1764 | // FIXME: In fact ELF flags depends on ELF flags of input object files |
| Simon Atanasyan | db147eb | 2016-01-12 06:24:02 +0000 | [diff] [blame] | 1765 | // and selected emulation. For now just use hard coded values. |
| Simon Atanasyan | 034c4cd | 2015-12-19 05:51:49 +0000 | [diff] [blame] | 1766 | uint32_t V = EF_MIPS_ABI_O32 | EF_MIPS_CPIC | EF_MIPS_ARCH_32R2; |
| 1767 | if (Config->Shared) |
| 1768 | V |= EF_MIPS_PIC; |
| 1769 | return V; |
| 1770 | } |
| 1771 | |
| Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 1772 | template <class ELFT> static typename ELFT::uint getEntryAddr() { |
| Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 1773 | if (Symbol *S = Config->EntrySym) |
| 1774 | return S->Body->getVA<ELFT>(); |
| Rui Ueyama | 3bfaba9 | 2015-12-24 08:37:34 +0000 | [diff] [blame] | 1775 | if (Config->EntryAddr != uint64_t(-1)) |
| 1776 | return Config->EntryAddr; |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1780 | template <class ELFT> static uint8_t getELFEncoding() { |
| 1781 | if (ELFT::TargetEndianness == llvm::support::little) |
| 1782 | return ELFDATA2LSB; |
| 1783 | return ELFDATA2MSB; |
| 1784 | } |
| 1785 | |
| 1786 | static uint16_t getELFType() { |
| George Rimar | 786e866 | 2016-03-17 05:57:33 +0000 | [diff] [blame] | 1787 | if (Config->Pic) |
| Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1788 | return ET_DYN; |
| 1789 | if (Config->Relocatable) |
| 1790 | return ET_REL; |
| 1791 | return ET_EXEC; |
| 1792 | } |
| 1793 | |
| Rui Ueyama | 1a311f1 | 2015-12-26 10:52:26 +0000 | [diff] [blame] | 1794 | // This function is called after we have assigned address and size |
| 1795 | // to each section. This function fixes some predefined absolute |
| 1796 | // symbol values that depend on section address and size. |
| 1797 | template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() { |
| Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1798 | auto Set = [](DefinedRegular<ELFT> *&S1, DefinedRegular<ELFT> *&S2, |
| 1799 | uintX_t V) { |
| 1800 | if (S1) |
| 1801 | S1->Value = V; |
| 1802 | if (S2) |
| 1803 | S2->Value = V; |
| 1804 | }; |
| 1805 | |
| George Rimar | 2abc587 | 2016-03-01 19:18:07 +0000 | [diff] [blame] | 1806 | // _etext is the first location after the last read-only loadable segment. |
| 1807 | // _edata is the first location after the last read-write loadable segment. |
| George Rimar | efded31 | 2016-04-01 10:23:32 +0000 | [diff] [blame] | 1808 | // _end is the first location after the uninitialized data region. |
| Rui Ueyama | e8a45e4 | 2016-04-01 22:42:04 +0000 | [diff] [blame] | 1809 | for (Phdr &P : Phdrs) { |
| 1810 | Elf_Phdr &H = P.H; |
| 1811 | if (H.p_type != PT_LOAD) |
| George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 1812 | continue; |
| Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1813 | Set(ElfSym<ELFT>::End, ElfSym<ELFT>::End2, H.p_vaddr + H.p_memsz); |
| Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1814 | |
| George Rimar | 8bbff7e | 2016-04-14 14:37:59 +0000 | [diff] [blame] | 1815 | uintX_t Val = H.p_vaddr + H.p_filesz; |
| 1816 | if (H.p_flags & PF_W) |
| Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1817 | Set(ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2, Val); |
| George Rimar | 8bbff7e | 2016-04-14 14:37:59 +0000 | [diff] [blame] | 1818 | else |
| Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1819 | Set(ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2, Val); |
| George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 1820 | } |
| Rui Ueyama | 1a311f1 | 2015-12-26 10:52:26 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1823 | template <class ELFT> void Writer<ELFT>::writeHeader() { |
| 1824 | uint8_t *Buf = Buffer->getBufferStart(); |
| Rui Ueyama | e08cd67 | 2015-10-23 22:44:39 +0000 | [diff] [blame] | 1825 | memcpy(Buf, "\177ELF", 4); |
| 1826 | |
| Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1827 | auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf); |
| 1828 | |
| Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1829 | // Write the ELF header. |
| Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 1830 | auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf); |
| Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 1831 | EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1832 | EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>(); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1833 | EHdr->e_ident[EI_VERSION] = EV_CURRENT; |
| Davide Italiano | aa7c533 | 2015-09-25 01:59:13 +0000 | [diff] [blame] | 1834 | EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI(); |
| Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1835 | EHdr->e_type = getELFType(); |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 1836 | EHdr->e_machine = FirstObj.getEMachine(); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1837 | EHdr->e_version = EV_CURRENT; |
| Rui Ueyama | 3bfaba9 | 2015-12-24 08:37:34 +0000 | [diff] [blame] | 1838 | EHdr->e_entry = getEntryAddr<ELFT>(); |
| Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 1839 | EHdr->e_shoff = SectionHeaderOff; |
| Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 1840 | EHdr->e_ehsize = sizeof(Elf_Ehdr); |
| Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 1841 | EHdr->e_phnum = Phdrs.size(); |
| Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 1842 | EHdr->e_shentsize = sizeof(Elf_Shdr); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1843 | EHdr->e_shnum = OutputSections.size() + 1; |
| George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 1844 | EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex; |
| Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1845 | |
| Rui Ueyama | 22b5d1f | 2016-03-13 19:29:17 +0000 | [diff] [blame] | 1846 | if (Config->EMachine == EM_MIPS) |
| 1847 | EHdr->e_flags = getMipsEFlags(); |
| 1848 | |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 1849 | if (!Config->Relocatable) { |
| 1850 | EHdr->e_phoff = sizeof(Elf_Ehdr); |
| 1851 | EHdr->e_phentsize = sizeof(Elf_Phdr); |
| 1852 | } |
| 1853 | |
| Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1854 | // Write the program header table. |
| Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1855 | auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff); |
| 1856 | for (Phdr &P : Phdrs) |
| 1857 | *HBuf++ = P.H; |
| Rafael Espindola | e438e07 | 2015-09-08 22:55:28 +0000 | [diff] [blame] | 1858 | |
| Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1859 | // Write the section header table. Note that the first table entry is null. |
| Rui Ueyama | ad59b65 | 2016-02-25 23:58:21 +0000 | [diff] [blame] | 1860 | auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff); |
| George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1861 | for (OutputSectionBase<ELFT> *Sec : OutputSections) |
| Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1862 | Sec->writeHeaderTo(++SHdrs); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 1865 | template <class ELFT> void Writer<ELFT>::openFile() { |
| Rafael Espindola | bdc8f2f | 2015-08-13 00:31:46 +0000 | [diff] [blame] | 1866 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| Rui Ueyama | cbe3926 | 2016-02-02 22:48:04 +0000 | [diff] [blame] | 1867 | FileOutputBuffer::create(Config->OutputFile, FileSize, |
| 1868 | FileOutputBuffer::F_executable); |
| Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 1869 | if (BufferOrErr) |
| 1870 | Buffer = std::move(*BufferOrErr); |
| 1871 | else |
| Rui Ueyama | 6eafa7f | 2016-03-13 04:25:41 +0000 | [diff] [blame] | 1872 | error(BufferOrErr, "failed to open " + Config->OutputFile); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | // Write section contents to a mmap'ed file. |
| 1876 | template <class ELFT> void Writer<ELFT>::writeSections() { |
| 1877 | uint8_t *Buf = Buffer->getBufferStart(); |
| Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 1878 | |
| 1879 | // PPC64 needs to process relocations in the .opd section before processing |
| 1880 | // relocations in code-containing sections. |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1881 | if (OutputSectionBase<ELFT> *Sec = Out<ELFT>::Opd) { |
| Rafael Espindola | 7a51305 | 2015-10-13 14:45:51 +0000 | [diff] [blame] | 1882 | Out<ELFT>::OpdBuf = Buf + Sec->getFileOff(); |
| 1883 | Sec->writeTo(Buf + Sec->getFileOff()); |
| 1884 | } |
| Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 1885 | |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1886 | for (OutputSectionBase<ELFT> *Sec : OutputSections) |
| Rafael Espindola | 4d91f7f | 2016-02-01 21:52:00 +0000 | [diff] [blame] | 1887 | if (Sec != Out<ELFT>::Opd) |
| Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 1888 | Sec->writeTo(Buf + Sec->getFileOff()); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1889 | } |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 1890 | |
| Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 1891 | template <class ELFT> void Writer<ELFT>::writeBuildId() { |
| 1892 | BuildIdSection<ELFT> *S = Out<ELFT>::BuildId; |
| 1893 | if (!S) |
| 1894 | return; |
| 1895 | |
| 1896 | // Compute a hash of all sections except .debug_* sections. |
| 1897 | // We skip debug sections because they tend to be very large |
| 1898 | // and their contents are very likely to be the same as long as |
| 1899 | // other sections are the same. |
| 1900 | uint8_t *Start = Buffer->getBufferStart(); |
| 1901 | uint8_t *Last = Start; |
| 1902 | for (OutputSectionBase<ELFT> *Sec : OutputSections) { |
| 1903 | uint8_t *End = Start + Sec->getFileOff(); |
| 1904 | if (!Sec->getName().startswith(".debug_")) |
| 1905 | S->update({Last, End}); |
| 1906 | Last = End; |
| 1907 | } |
| 1908 | S->update({Last, Start + FileSize}); |
| 1909 | |
| 1910 | // Fill the hash value field in the .note.gnu.build-id section. |
| 1911 | S->writeBuildId(); |
| 1912 | } |
| 1913 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 1914 | template void elf::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab); |
| 1915 | template void elf::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab); |
| 1916 | template void elf::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab); |
| 1917 | template void elf::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab); |