Rafael Espindola | beee25e | 2015-08-14 14:12:54 | [diff] [blame] | 1 | //===- InputFiles.h ---------------------------------------------*- C++ -*-===// |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLD_COFF_INPUT_FILES_H |
| 10 | #define LLD_COFF_INPUT_FILES_H |
| 11 | |
Rui Ueyama | 9aa82f7 | 2017-05-24 22:30:06 | [diff] [blame] | 12 | #include "Config.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 13 | #include "lld/Common/LLVM.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
Martin Storsjo | 21858a9b | 2018-08-06 21:26:09 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMap.h" |
Peter Collingbourne | 8155dfa | 2016-12-12 03:16:14 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 17 | #include "llvm/BinaryFormat/Magic.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 18 | #include "llvm/Object/Archive.h" |
| 19 | #include "llvm/Object/COFF.h" |
Rafael Espindola | 9bd82e9 | 2015-06-13 12:50:13 | [diff] [blame] | 20 | #include "llvm/Support/StringSaver.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <set> |
| 23 | #include <vector> |
| 24 | |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 | [diff] [blame] | 25 | namespace llvm { |
Martin Storsjo | 65b1c49 | 2019-10-21 08:01:59 | [diff] [blame] | 26 | struct DILineInfo; |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 | [diff] [blame] | 27 | namespace pdb { |
| 28 | class DbiModuleDescriptorBuilder; |
Reid Kleckner | 54a335a | 2020-05-09 13:58:15 | [diff] [blame] | 29 | class NativeSession; |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 | [diff] [blame] | 30 | } |
Reid Kleckner | f24c335 | 2019-11-14 21:46:00 | [diff] [blame] | 31 | namespace lto { |
| 32 | class InputFile; |
| 33 | } |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 | [diff] [blame] | 34 | } |
| 35 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 36 | namespace lld { |
Reid Kleckner | adfad4d | 2019-11-14 22:16:21 | [diff] [blame] | 37 | class DWARFCache; |
| 38 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 39 | namespace coff { |
| 40 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 41 | std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *file); |
Rui Ueyama | f59b709 | 2017-08-30 20:55:18 | [diff] [blame] | 42 | |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 | [diff] [blame] | 43 | using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN; |
| 44 | using llvm::COFF::MachineTypes; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 45 | using llvm::object::Archive; |
| 46 | using llvm::object::COFFObjectFile; |
Chandler Carruth | be6e80b | 2015-06-29 18:50:11 | [diff] [blame] | 47 | using llvm::object::COFFSymbolRef; |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 | [diff] [blame] | 48 | using llvm::object::coff_import_header; |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 | [diff] [blame] | 49 | using llvm::object::coff_section; |
Chandler Carruth | be6e80b | 2015-06-29 18:50:11 | [diff] [blame] | 50 | |
| 51 | class Chunk; |
Rui Ueyama | dae1661 | 2015-06-29 22:16:21 | [diff] [blame] | 52 | class Defined; |
Rui Ueyama | e73e418 | 2015-08-17 07:27:45 | [diff] [blame] | 53 | class DefinedImportData; |
| 54 | class DefinedImportThunk; |
Nico Weber | 04db8cb | 2019-02-14 03:16:44 | [diff] [blame] | 55 | class DefinedRegular; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 | [diff] [blame] | 56 | class SectionChunk; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 57 | class Symbol; |
Rui Ueyama | dae1661 | 2015-06-29 22:16:21 | [diff] [blame] | 58 | class Undefined; |
Alexandre Ganea | bf55c4e | 2019-04-01 13:36:59 | [diff] [blame] | 59 | class TpiSource; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 60 | |
| 61 | // The root class of input files. |
| 62 | class InputFile { |
| 63 | public: |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 64 | enum Kind { |
| 65 | ArchiveKind, |
| 66 | ObjectKind, |
| 67 | LazyObjectKind, |
Reid Kleckner | 54a335a | 2020-05-09 13:58:15 | [diff] [blame] | 68 | PDBKind, |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 69 | ImportKind, |
| 70 | BitcodeKind |
| 71 | }; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 72 | Kind kind() const { return fileKind; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 73 | virtual ~InputFile() {} |
| 74 | |
| 75 | // Returns the filename. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 76 | StringRef getName() const { return mb.getBufferIdentifier(); } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 77 | |
Rui Ueyama | 1574b8e | 2015-08-06 19:57:21 | [diff] [blame] | 78 | // Reads a file (the constructor doesn't do that). |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 | [diff] [blame] | 79 | virtual void parse() = 0; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 80 | |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 | [diff] [blame] | 81 | // Returns the CPU type this file was compiled to. |
| 82 | virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; } |
| 83 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 84 | MemoryBufferRef mb; |
Bob Haarman | 4110816 | 2017-04-21 21:38:01 | [diff] [blame] | 85 | |
Rui Ueyama | a45d45e | 2016-12-07 23:17:02 | [diff] [blame] | 86 | // An archive file name if this file is created from an archive. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 87 | StringRef parentName; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 88 | |
Rui Ueyama | eeae5dd | 2015-06-08 06:00:10 | [diff] [blame] | 89 | // Returns .drectve section contents if exist. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 90 | StringRef getDirectives() { return directives; } |
Rui Ueyama | eeae5dd | 2015-06-08 06:00:10 | [diff] [blame] | 91 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 92 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 93 | InputFile(Kind k, MemoryBufferRef m) : mb(m), fileKind(k) {} |
Rui Ueyama | 65813ed | 2015-07-02 20:33:48 | [diff] [blame] | 94 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 95 | StringRef directives; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 96 | |
| 97 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 98 | const Kind fileKind; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // .lib or .a file. |
| 102 | class ArchiveFile : public InputFile { |
| 103 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 104 | explicit ArchiveFile(MemoryBufferRef m); |
| 105 | static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; } |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 | [diff] [blame] | 106 | void parse() override; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 107 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 | [diff] [blame] | 108 | // Enqueues an archive member load for the given symbol. If we've already |
| 109 | // enqueued a load for the same archive member, this function does nothing, |
| 110 | // which ensures that we don't load the same member more than once. |
Nico Weber | cb2c500 | 2019-07-19 13:29:10 | [diff] [blame] | 111 | void addMember(const Archive::Symbol &sym); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 112 | |
| 113 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 114 | std::unique_ptr<Archive> file; |
| 115 | llvm::DenseSet<uint64_t> seen; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 116 | }; |
| 117 | |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 118 | // .obj or .o file between -start-lib and -end-lib. |
| 119 | class LazyObjFile : public InputFile { |
| 120 | public: |
| 121 | explicit LazyObjFile(MemoryBufferRef m) : InputFile(LazyObjectKind, m) {} |
| 122 | static bool classof(const InputFile *f) { |
| 123 | return f->kind() == LazyObjectKind; |
| 124 | } |
| 125 | // Makes this object file part of the link. |
| 126 | void fetch(); |
| 127 | // Adds the symbols in this file to the symbol table as LazyObject symbols. |
| 128 | void parse() override; |
| 129 | |
| 130 | private: |
| 131 | std::vector<Symbol *> symbols; |
| 132 | }; |
| 133 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 134 | // .obj or .o file. This may be a member of an archive file. |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 | [diff] [blame] | 135 | class ObjFile : public InputFile { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 136 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 137 | explicit ObjFile(MemoryBufferRef m) : InputFile(ObjectKind, m) {} |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 138 | explicit ObjFile(MemoryBufferRef m, std::vector<Symbol *> &&symbols) |
| 139 | : InputFile(ObjectKind, m), symbols(std::move(symbols)) {} |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 140 | static bool classof(const InputFile *f) { return f->kind() == ObjectKind; } |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 | [diff] [blame] | 141 | void parse() override; |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 | [diff] [blame] | 142 | MachineTypes getMachineType() override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 143 | ArrayRef<Chunk *> getChunks() { return chunks; } |
| 144 | ArrayRef<SectionChunk *> getDebugChunks() { return debugChunks; } |
Reid Kleckner | 3774bcf | 2020-05-14 18:21:53 | [diff] [blame] | 145 | ArrayRef<SectionChunk *> getSXDataChunks() { return sxDataChunks; } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 146 | ArrayRef<SectionChunk *> getGuardFidChunks() { return guardFidChunks; } |
| 147 | ArrayRef<SectionChunk *> getGuardLJmpChunks() { return guardLJmpChunks; } |
| 148 | ArrayRef<Symbol *> getSymbols() { return symbols; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 149 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 150 | ArrayRef<uint8_t> getDebugSection(StringRef secName); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 | [diff] [blame] | 151 | |
Fangrui Song | 2e2038b | 2019-07-16 08:26:38 | [diff] [blame] | 152 | // Returns a Symbol object for the symbolIndex'th symbol in the |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 153 | // underlying object file. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 154 | Symbol *getSymbol(uint32_t symbolIndex) { |
| 155 | return symbols[symbolIndex]; |
Chandler Carruth | 52eb355 | 2015-06-27 03:40:10 | [diff] [blame] | 156 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 157 | |
Alexandre Ganea | 71c43ce | 2018-11-05 19:20:47 | [diff] [blame] | 158 | // Returns the underlying COFF file. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 159 | COFFObjectFile *getCOFFObj() { return coffObj.get(); } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 160 | |
Reid Kleckner | 1600490 | 2019-03-28 18:30:03 | [diff] [blame] | 161 | // Add a symbol for a range extension thunk. Return the new symbol table |
| 162 | // index. This index can be used to modify a relocation. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 163 | uint32_t addRangeThunkSymbol(Symbol *thunk) { |
| 164 | symbols.push_back(thunk); |
| 165 | return symbols.size() - 1; |
Reid Kleckner | 1600490 | 2019-03-28 18:30:03 | [diff] [blame] | 166 | } |
| 167 | |
Martin Storsjo | 3d3a9b3 | 2019-08-30 06:56:33 | [diff] [blame] | 168 | void includeResourceChunks(); |
| 169 | |
| 170 | bool isResourceObjFile() const { return !resourceChunks.empty(); } |
| 171 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 172 | static std::vector<ObjFile *> instances; |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 | [diff] [blame] | 173 | |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 174 | // Flags in the absolute @feat.00 symbol if it is present. These usually |
| 175 | // indicate if an object was compiled with certain security features enabled |
| 176 | // like stack guard, safeseh, /guard:cf, or other things. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 177 | uint32_t feat00Flags = 0; |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 | [diff] [blame] | 178 | |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 179 | // True if this object file is compatible with SEH. COFF-specific and |
| 180 | // x86-only. COFF spec 5.10.1. The .sxdata section. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 181 | bool hasSafeSEH() { return feat00Flags & 0x1; } |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 182 | |
| 183 | // True if this file was compiled with /guard:cf. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 184 | bool hasGuardCF() { return feat00Flags & 0x800; } |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 | [diff] [blame] | 185 | |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 | [diff] [blame] | 186 | // Pointer to the PDB module descriptor builder. Various debug info records |
| 187 | // will reference object files by "module index", which is here. Things like |
| 188 | // source files and section contributions are also recorded here. Will be null |
| 189 | // if we are not producing a PDB. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 190 | llvm::pdb::DbiModuleDescriptorBuilder *moduleDBI = nullptr; |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 | [diff] [blame] | 191 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 192 | const coff_section *addrsigSec = nullptr; |
Peter Collingbourne | ab03802 | 2018-08-23 17:44:42 | [diff] [blame] | 193 | |
Zequan Wu | 763671f | 2020-07-21 20:46:11 | [diff] [blame^] | 194 | const coff_section *callgraphSec = nullptr; |
| 195 | |
Alexandre Ganea | 71c43ce | 2018-11-05 19:20:47 | [diff] [blame] | 196 | // When using Microsoft precompiled headers, this is the PCH's key. |
| 197 | // The same key is used by both the precompiled object, and objects using the |
| 198 | // precompiled object. Any difference indicates out-of-date objects. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 199 | llvm::Optional<uint32_t> pchSignature; |
Alexandre Ganea | 71c43ce | 2018-11-05 19:20:47 | [diff] [blame] | 200 | |
Nico Weber | af6bc65 | 2019-06-11 15:22:28 | [diff] [blame] | 201 | // Whether this file was compiled with /hotpatch. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 202 | bool hotPatchable = false; |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 | [diff] [blame] | 203 | |
Nico Weber | af6bc65 | 2019-06-11 15:22:28 | [diff] [blame] | 204 | // Whether the object was already merged into the final PDB. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 205 | bool mergedIntoPDB = false; |
Alexandre Ganea | 74d5b33 | 2019-03-22 22:07:27 | [diff] [blame] | 206 | |
Alexandre Ganea | bf55c4e | 2019-04-01 13:36:59 | [diff] [blame] | 207 | // If the OBJ has a .debug$T stream, this tells how it will be handled. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 208 | TpiSource *debugTypesObj = nullptr; |
Alexandre Ganea | bf55c4e | 2019-04-01 13:36:59 | [diff] [blame] | 209 | |
Reid Kleckner | de3fb1e | 2019-11-14 22:27:48 | [diff] [blame] | 210 | // The .debug$P or .debug$T section data if present. Empty otherwise. |
| 211 | ArrayRef<uint8_t> debugTypes; |
Alexandre Ganea | bf55c4e | 2019-04-01 13:36:59 | [diff] [blame] | 212 | |
Martin Storsjo | b38f577 | 2019-10-18 10:43:15 | [diff] [blame] | 213 | llvm::Optional<std::pair<StringRef, uint32_t>> |
| 214 | getVariableLocation(StringRef var); |
| 215 | |
Martin Storsjo | 65b1c49 | 2019-10-21 08:01:59 | [diff] [blame] | 216 | llvm::Optional<llvm::DILineInfo> getDILineInfo(uint32_t offset, |
| 217 | uint32_t sectionIndex); |
| 218 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 219 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 220 | const coff_section* getSection(uint32_t i); |
| 221 | const coff_section *getSection(COFFSymbolRef sym) { |
| 222 | return getSection(sym.getSectionNumber()); |
Nico Weber | 04db8cb | 2019-02-14 03:16:44 | [diff] [blame] | 223 | } |
Nico Weber | 48dc110 | 2019-01-30 02:17:27 | [diff] [blame] | 224 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 | [diff] [blame] | 225 | void initializeChunks(); |
| 226 | void initializeSymbols(); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 | [diff] [blame] | 227 | void initializeFlags(); |
Alexandre Ganea | bf55c4e | 2019-04-01 13:36:59 | [diff] [blame] | 228 | void initializeDependencies(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 229 | |
Peter Collingbourne | 1621c20 | 2017-11-28 01:30:07 | [diff] [blame] | 230 | SectionChunk * |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 231 | readSection(uint32_t sectionNumber, |
| 232 | const llvm::object::coff_aux_section_definition *def, |
| 233 | StringRef leaderName); |
Peter Collingbourne | 1621c20 | 2017-11-28 01:30:07 | [diff] [blame] | 234 | |
| 235 | void readAssociativeDefinition( |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 236 | COFFSymbolRef coffSym, |
| 237 | const llvm::object::coff_aux_section_definition *def); |
Peter Collingbourne | 1621c20 | 2017-11-28 01:30:07 | [diff] [blame] | 238 | |
Martin Storsjo | 21858a9b | 2018-08-06 21:26:09 | [diff] [blame] | 239 | void readAssociativeDefinition( |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 240 | COFFSymbolRef coffSym, |
| 241 | const llvm::object::coff_aux_section_definition *def, |
| 242 | uint32_t parentSection); |
Martin Storsjo | 21858a9b | 2018-08-06 21:26:09 | [diff] [blame] | 243 | |
| 244 | void recordPrevailingSymbolForMingw( |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 245 | COFFSymbolRef coffSym, |
| 246 | llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap); |
Martin Storsjo | 21858a9b | 2018-08-06 21:26:09 | [diff] [blame] | 247 | |
| 248 | void maybeAssociateSEHForMingw( |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 249 | COFFSymbolRef sym, const llvm::object::coff_aux_section_definition *def, |
| 250 | const llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap); |
Martin Storsjo | 21858a9b | 2018-08-06 21:26:09 | [diff] [blame] | 251 | |
Nico Weber | 04db8cb | 2019-02-14 03:16:44 | [diff] [blame] | 252 | // Given a new symbol Sym with comdat selection Selection, if the new |
| 253 | // symbol is not (yet) Prevailing and the existing comdat leader set to |
| 254 | // Leader, emits a diagnostic if the new symbol and its selection doesn't |
| 255 | // match the existing symbol and its selection. If either old or new |
| 256 | // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace |
| 257 | // the existing leader. In that case, Prevailing is set to true. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 258 | void handleComdatSelection(COFFSymbolRef sym, |
| 259 | llvm::COFF::COMDATType &selection, |
| 260 | bool &prevailing, DefinedRegular *leader); |
Nico Weber | 04db8cb | 2019-02-14 03:16:44 | [diff] [blame] | 261 | |
Peter Collingbourne | 1621c20 | 2017-11-28 01:30:07 | [diff] [blame] | 262 | llvm::Optional<Symbol *> |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 263 | createDefined(COFFSymbolRef sym, |
Peter Collingbourne | 1621c20 | 2017-11-28 01:30:07 | [diff] [blame] | 264 | std::vector<const llvm::object::coff_aux_section_definition *> |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 265 | &comdatDefs, |
| 266 | bool &prevailingComdat); |
| 267 | Symbol *createRegular(COFFSymbolRef sym); |
| 268 | Symbol *createUndefined(COFFSymbolRef sym); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 269 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 270 | std::unique_ptr<COFFObjectFile> coffObj; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 271 | |
| 272 | // List of all chunks defined by this file. This includes both section |
| 273 | // chunks and non-section chunks for common symbols. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 274 | std::vector<Chunk *> chunks; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 275 | |
Martin Storsjo | 3d3a9b3 | 2019-08-30 06:56:33 | [diff] [blame] | 276 | std::vector<SectionChunk *> resourceChunks; |
| 277 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 | [diff] [blame] | 278 | // CodeView debug info sections. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 279 | std::vector<SectionChunk *> debugChunks; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 | [diff] [blame] | 280 | |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 281 | // Chunks containing symbol table indices of exception handlers. Only used for |
| 282 | // 32-bit x86. |
Reid Kleckner | 3774bcf | 2020-05-14 18:21:53 | [diff] [blame] | 283 | std::vector<SectionChunk *> sxDataChunks; |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 284 | |
Reid Kleckner | fd52096 | 2018-02-13 20:32:53 | [diff] [blame] | 285 | // Chunks containing symbol table indices of address taken symbols and longjmp |
| 286 | // targets. These are not linked into the final binary when /guard:cf is set. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 287 | std::vector<SectionChunk *> guardFidChunks; |
| 288 | std::vector<SectionChunk *> guardLJmpChunks; |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 289 | |
Peter Collingbourne | 80f961a | 2017-11-20 18:52:53 | [diff] [blame] | 290 | // This vector contains a list of all symbols defined or referenced by this |
| 291 | // file. They are indexed such that you can get a Symbol by symbol |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 292 | // index. Nonexistent indices (which are occupied by auxiliary |
| 293 | // symbols in the real symbol table) are filled with null pointers. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 294 | std::vector<Symbol *> symbols; |
Martin Storsjo | b38f577 | 2019-10-18 10:43:15 | [diff] [blame] | 295 | |
Reid Kleckner | 11d1aa0 | 2020-06-02 01:46:51 | [diff] [blame] | 296 | // This vector contains the same chunks as Chunks, but they are |
| 297 | // indexed such that you can get a SectionChunk by section index. |
| 298 | // Nonexistent section indices are filled with null pointers. |
| 299 | // (Because section number is 1-based, the first slot is always a |
| 300 | // null pointer.) This vector is only valid during initialization. |
| 301 | std::vector<SectionChunk *> sparseChunks; |
| 302 | |
Martin Storsjo | 150a9ad | 2019-10-21 09:35:34 | [diff] [blame] | 303 | DWARFCache *dwarf = nullptr; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 304 | }; |
| 305 | |
Reid Kleckner | 54a335a | 2020-05-09 13:58:15 | [diff] [blame] | 306 | // This is a PDB type server dependency, that is not a input file per se, but |
| 307 | // needs to be treated like one. Such files are discovered from the debug type |
| 308 | // stream. |
| 309 | class PDBInputFile : public InputFile { |
| 310 | public: |
| 311 | explicit PDBInputFile(MemoryBufferRef m); |
| 312 | ~PDBInputFile(); |
| 313 | static bool classof(const InputFile *f) { return f->kind() == PDBKind; } |
| 314 | void parse() override; |
| 315 | |
| 316 | static void enqueue(StringRef path, ObjFile *fromFile); |
| 317 | |
| 318 | static PDBInputFile *findFromRecordPath(StringRef path, ObjFile *fromFile); |
| 319 | |
| 320 | static std::map<std::string, PDBInputFile *> instances; |
| 321 | |
| 322 | // Record possible errors while opening the PDB file |
| 323 | llvm::Optional<Error> loadErr; |
| 324 | |
| 325 | // This is the actual interface to the PDB (if it was opened successfully) |
| 326 | std::unique_ptr<llvm::pdb::NativeSession> session; |
| 327 | |
| 328 | // If the PDB has a .debug$T stream, this tells how it will be handled. |
| 329 | TpiSource *debugTypesObj = nullptr; |
| 330 | }; |
| 331 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 332 | // This type represents import library members that contain DLL names |
| 333 | // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7 |
| 334 | // for details about the format. |
| 335 | class ImportFile : public InputFile { |
| 336 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 337 | explicit ImportFile(MemoryBufferRef m) : InputFile(ImportKind, m) {} |
Rui Ueyama | 9aa82f7 | 2017-05-24 22:30:06 | [diff] [blame] | 338 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 339 | static bool classof(const InputFile *f) { return f->kind() == ImportKind; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 340 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 341 | static std::vector<ImportFile *> instances; |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 | [diff] [blame] | 342 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 343 | Symbol *impSym = nullptr; |
| 344 | Symbol *thunkSym = nullptr; |
| 345 | std::string dllName; |
Rui Ueyama | e73e418 | 2015-08-17 07:27:45 | [diff] [blame] | 346 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 347 | private: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 | [diff] [blame] | 348 | void parse() override; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 349 | |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 | [diff] [blame] | 350 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 351 | StringRef externalName; |
| 352 | const coff_import_header *hdr; |
| 353 | Chunk *location = nullptr; |
Rui Ueyama | 9aa82f7 | 2017-05-24 22:30:06 | [diff] [blame] | 354 | |
| 355 | // We want to eliminate dllimported symbols if no one actually refers them. |
Peter Collingbourne | e28faed | 2018-05-10 19:01:28 | [diff] [blame] | 356 | // These "Live" bits are used to keep track of which import library members |
Rui Ueyama | 9aa82f7 | 2017-05-24 22:30:06 | [diff] [blame] | 357 | // are actually in use. |
| 358 | // |
| 359 | // If the Live bit is turned off by MarkLive, Writer will ignore dllimported |
Peter Collingbourne | e28faed | 2018-05-10 19:01:28 | [diff] [blame] | 360 | // symbols provided by this import library member. We also track whether the |
| 361 | // imported symbol is used separately from whether the thunk is used in order |
| 362 | // to avoid creating unnecessary thunks. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 363 | bool live = !config->doGC; |
| 364 | bool thunkLive = !config->doGC; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 365 | }; |
| 366 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 | [diff] [blame] | 367 | // Used for LTO. |
| 368 | class BitcodeFile : public InputFile { |
| 369 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 370 | BitcodeFile(MemoryBufferRef mb, StringRef archiveName, |
Reid Kleckner | f24c335 | 2019-11-14 21:46:00 | [diff] [blame] | 371 | uint64_t offsetInArchive); |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 372 | explicit BitcodeFile(MemoryBufferRef m, StringRef archiveName, |
| 373 | uint64_t offsetInArchive, |
| 374 | std::vector<Symbol *> &&symbols); |
Reid Kleckner | f24c335 | 2019-11-14 21:46:00 | [diff] [blame] | 375 | ~BitcodeFile(); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 376 | static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; } |
| 377 | ArrayRef<Symbol *> getSymbols() { return symbols; } |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 | [diff] [blame] | 378 | MachineTypes getMachineType() override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 379 | static std::vector<BitcodeFile *> instances; |
| 380 | std::unique_ptr<llvm::lto::InputFile> obj; |
Rui Ueyama | 34b60f1 | 2016-04-14 21:41:44 | [diff] [blame] | 381 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 | [diff] [blame] | 382 | private: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 | [diff] [blame] | 383 | void parse() override; |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 | [diff] [blame] | 384 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 385 | std::vector<Symbol *> symbols; |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 | [diff] [blame] | 386 | }; |
Bob Haarman | 5011b83 | 2019-07-11 18:48:58 | [diff] [blame] | 387 | |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 388 | inline bool isBitcode(MemoryBufferRef mb) { |
| 389 | return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode; |
| 390 | } |
| 391 | |
Bob Haarman | 5011b83 | 2019-07-11 18:48:58 | [diff] [blame] | 392 | std::string replaceThinLTOSuffix(StringRef path); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 393 | } // namespace coff |
Rui Ueyama | ce03926 | 2017-01-06 10:04:08 | [diff] [blame] | 394 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 395 | std::string toString(const coff::InputFile *file); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 396 | } // namespace lld |
| 397 | |
| 398 | #endif |