Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 1 | //===- PDB.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 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 10 | #include "PDB.h" |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 11 | #include "Chunks.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 12 | #include "Config.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 13 | #include "Error.h" |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 14 | #include "SymbolTable.h" |
| 15 | #include "Symbols.h" |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 16 | #include "Writer.h" |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/CVDebugRecord.h" |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/CodeView/SymbolSerializer.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" |
| 25 | #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/PDB/GenericError.h" |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 31 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
| 32 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 33 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/PDB/Native/NativeSession.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 36 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
Rafael Espindola | a0f30da | 2017-05-02 20:19:42 +0000 | [diff] [blame] | 37 | #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 38 | #include "llvm/DebugInfo/PDB/Native/PublicsStreamBuilder.h" |
Reid Kleckner | d91ca76 | 2017-07-19 17:26:07 +0000 | [diff] [blame] | 39 | #include "llvm/DebugInfo/PDB/Native/TpiHashing.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 40 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 41 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/PDB/PDB.h" |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 43 | #include "llvm/Object/COFF.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 44 | #include "llvm/Support/BinaryByteStream.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 46 | #include "llvm/Support/FileOutputBuffer.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Path.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ScopedPrinter.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 49 | #include <memory> |
| 50 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 51 | using namespace lld; |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 52 | using namespace lld::coff; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 53 | using namespace llvm; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 54 | using namespace llvm::codeview; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 55 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 56 | using llvm::object::coff_section; |
| 57 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 58 | static ExitOnError ExitOnErr; |
| 59 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 60 | namespace { |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 61 | /// Map from type index and item index in a type server PDB to the |
| 62 | /// corresponding index in the destination PDB. |
| 63 | struct CVIndexMap { |
| 64 | SmallVector<TypeIndex, 0> TPIMap; |
| 65 | SmallVector<TypeIndex, 0> IPIMap; |
| 66 | bool IsTypeServerMap = false; |
| 67 | }; |
| 68 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 69 | class PDBLinker { |
| 70 | public: |
| 71 | PDBLinker(SymbolTable *Symtab) |
| 72 | : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc), |
| 73 | IDTable(Alloc) {} |
| 74 | |
| 75 | /// Emit the basic PDB structure: initial streams, headers, etc. |
| 76 | void initialize(const llvm::codeview::DebugInfo *DI); |
| 77 | |
| 78 | /// Link CodeView from each object file in the symbol table into the PDB. |
| 79 | void addObjectsToPDB(); |
| 80 | |
| 81 | /// Link CodeView from a single object file into the PDB. |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 82 | void addObjFile(ObjFile *File); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 83 | |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 84 | /// Produce a mapping from the type and item indices used in the object |
| 85 | /// file to those in the destination PDB. |
| 86 | /// |
| 87 | /// If the object file uses a type server PDB (compiled with /Zi), merge TPI |
| 88 | /// and IPI from the type server PDB and return a map for it. Each unique type |
| 89 | /// server PDB is merged at most once, so this may return an existing index |
| 90 | /// mapping. |
| 91 | /// |
| 92 | /// If the object does not use a type server PDB (compiled with /Z7), we merge |
| 93 | /// all the type and item records from the .debug$S stream and fill in the |
| 94 | /// caller-provided ObjectIndexMap. |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 95 | const CVIndexMap &mergeDebugT(ObjFile *File, CVIndexMap &ObjectIndexMap); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 96 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 97 | const CVIndexMap &maybeMergeTypeServerPDB(ObjFile *File, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 98 | TypeServer2Record &TS); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 99 | |
| 100 | /// Add the section map and section contributions to the PDB. |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame^] | 101 | void addSections(ArrayRef<OutputSection *> OutputSections, |
| 102 | ArrayRef<uint8_t> SectionTable); |
| 103 | |
| 104 | void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule, |
| 105 | OutputSection *OS, Chunk *C); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 106 | |
| 107 | /// Write the PDB to disk. |
| 108 | void commit(); |
| 109 | |
| 110 | private: |
| 111 | BumpPtrAllocator Alloc; |
| 112 | |
| 113 | SymbolTable *Symtab; |
| 114 | |
| 115 | pdb::PDBFileBuilder Builder; |
| 116 | |
| 117 | /// Type records that will go into the PDB TPI stream. |
| 118 | TypeTableBuilder TypeTable; |
| 119 | |
| 120 | /// Item records that will go into the PDB IPI stream. |
| 121 | TypeTableBuilder IDTable; |
| 122 | |
| 123 | /// PDBs use a single global string table for filenames in the file checksum |
| 124 | /// table. |
| 125 | DebugStringTableSubsection PDBStrTab; |
| 126 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 127 | llvm::SmallString<128> NativePath; |
| 128 | |
| 129 | std::vector<pdb::SecMapEntry> SectionMap; |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 130 | |
| 131 | /// Type index mappings of type server PDBs that we've loaded so far. |
| 132 | std::map<GUID, CVIndexMap> TypeServerIndexMappings; |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 133 | }; |
| 134 | } |
| 135 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 136 | static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, |
| 137 | StringRef Name) { |
| 138 | for (SectionChunk *C : Sections) |
| 139 | if (C->getSectionName() == Name) |
| 140 | return C; |
| 141 | return nullptr; |
| 142 | } |
| 143 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 144 | static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data, |
| 145 | StringRef SecName) { |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 146 | // First 4 bytes are section magic. |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 147 | if (Data.size() < 4) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 148 | fatal(SecName + " too short"); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 149 | if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 150 | fatal(SecName + " has an invalid magic"); |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 151 | return Data.slice(4); |
| 152 | } |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 153 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 154 | static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) { |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 155 | if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName)) |
| 156 | return consumeDebugMagic(Sec->getContents(), SecName); |
| 157 | return {}; |
| 158 | } |
| 159 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 160 | static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 161 | TypeTableBuilder &TypeTable) { |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 162 | // Start the TPI or IPI stream header. |
| 163 | TpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 164 | |
Reid Kleckner | d91ca76 | 2017-07-19 17:26:07 +0000 | [diff] [blame] | 165 | // Flatten the in memory type table and hash each type. |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 166 | TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) { |
Reid Kleckner | d91ca76 | 2017-07-19 17:26:07 +0000 | [diff] [blame] | 167 | assert(Rec.size() >= sizeof(RecordPrefix)); |
| 168 | const RecordPrefix *P = reinterpret_cast<const RecordPrefix *>(Rec.data()); |
| 169 | CVType Type(static_cast<TypeLeafKind>(unsigned(P->RecordKind)), Rec); |
| 170 | auto Hash = pdb::hashTypeRecord(Type); |
| 171 | if (auto E = Hash.takeError()) |
| 172 | fatal("type hashing error"); |
| 173 | TpiBuilder.addTypeRecord(Rec, *Hash); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 174 | }); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 177 | static Optional<TypeServer2Record> |
| 178 | maybeReadTypeServerRecord(CVTypeArray &Types) { |
| 179 | auto I = Types.begin(); |
| 180 | if (I == Types.end()) |
| 181 | return None; |
| 182 | const CVType &Type = *I; |
| 183 | if (Type.kind() != LF_TYPESERVER2) |
| 184 | return None; |
| 185 | TypeServer2Record TS; |
| 186 | if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS)) |
| 187 | fatal(EC, "error reading type server record"); |
| 188 | return std::move(TS); |
| 189 | } |
| 190 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 191 | const CVIndexMap &PDBLinker::mergeDebugT(ObjFile *File, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 192 | CVIndexMap &ObjectIndexMap) { |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 193 | ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); |
| 194 | if (Data.empty()) |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 195 | return ObjectIndexMap; |
Reid Kleckner | 6597c28 | 2017-07-13 20:12:23 +0000 | [diff] [blame] | 196 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 197 | BinaryByteStream Stream(Data, support::little); |
| 198 | CVTypeArray Types; |
| 199 | BinaryStreamReader Reader(Stream); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 200 | if (auto EC = Reader.readArray(Types, Reader.getLength())) |
| 201 | fatal(EC, "Reader::readArray failed"); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 202 | |
| 203 | // Look through type servers. If we've already seen this type server, don't |
| 204 | // merge any type information. |
| 205 | if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types)) |
| 206 | return maybeMergeTypeServerPDB(File, *TS); |
| 207 | |
| 208 | // This is a /Z7 object. Fill in the temporary, caller-provided |
| 209 | // ObjectIndexMap. |
| 210 | if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable, |
| 211 | ObjectIndexMap.TPIMap, Types)) |
| 212 | fatal(Err, "codeview::mergeTypeAndIdRecords failed"); |
| 213 | return ObjectIndexMap; |
| 214 | } |
| 215 | |
| 216 | static Expected<std::unique_ptr<pdb::NativeSession>> |
| 217 | tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) { |
| 218 | std::unique_ptr<pdb::IPDBSession> ThisSession; |
| 219 | if (auto EC = |
| 220 | pdb::loadDataForPDB(pdb::PDB_ReaderType::Native, TSPath, ThisSession)) |
| 221 | return std::move(EC); |
| 222 | |
| 223 | std::unique_ptr<pdb::NativeSession> NS( |
| 224 | static_cast<pdb::NativeSession *>(ThisSession.release())); |
| 225 | pdb::PDBFile &File = NS->getPDBFile(); |
| 226 | auto ExpectedInfo = File.getPDBInfoStream(); |
| 227 | // All PDB Files should have an Info stream. |
| 228 | if (!ExpectedInfo) |
| 229 | return ExpectedInfo.takeError(); |
| 230 | |
| 231 | // Just because a file with a matching name was found and it was an actual |
| 232 | // PDB file doesn't mean it matches. For it to match the InfoStream's GUID |
| 233 | // must match the GUID specified in the TypeServer2 record. |
| 234 | if (ExpectedInfo->getGuid() != GuidFromObj) |
| 235 | return make_error<pdb::GenericError>( |
| 236 | pdb::generic_error_code::type_server_not_found, TSPath); |
| 237 | |
| 238 | return std::move(NS); |
| 239 | } |
| 240 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 241 | const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjFile *File, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 242 | TypeServer2Record &TS) { |
| 243 | // First, check if we already loaded a PDB with this GUID. Return the type |
| 244 | // index mapping if we have it. |
| 245 | auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()}); |
| 246 | CVIndexMap &IndexMap = Insertion.first->second; |
| 247 | if (!Insertion.second) |
| 248 | return IndexMap; |
| 249 | |
| 250 | // Mark this map as a type server map. |
| 251 | IndexMap.IsTypeServerMap = true; |
| 252 | |
| 253 | // Check for a PDB at: |
| 254 | // 1. The given file path |
| 255 | // 2. Next to the object file or archive file |
| 256 | auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName()); |
| 257 | if (!ExpectedSession) { |
| 258 | consumeError(ExpectedSession.takeError()); |
| 259 | StringRef LocalPath = |
| 260 | !File->ParentName.empty() ? File->ParentName : File->getName(); |
| 261 | SmallString<128> Path = sys::path::parent_path(LocalPath); |
Reid Kleckner | f8522d8 | 2017-07-18 00:33:53 +0000 | [diff] [blame] | 262 | sys::path::append( |
| 263 | Path, sys::path::filename(TS.getName(), sys::path::Style::windows)); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 264 | ExpectedSession = tryToLoadPDB(TS.getGuid(), Path); |
| 265 | } |
| 266 | if (auto E = ExpectedSession.takeError()) |
| 267 | fatal(E, "Type server PDB was not found"); |
| 268 | |
| 269 | // Merge TPI first, because the IPI stream will reference type indices. |
| 270 | auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream(); |
| 271 | if (auto E = ExpectedTpi.takeError()) |
| 272 | fatal(E, "Type server does not have TPI stream"); |
| 273 | if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap, |
| 274 | ExpectedTpi->typeArray())) |
| 275 | fatal(Err, "codeview::mergeTypeRecords failed"); |
| 276 | |
| 277 | // Merge IPI. |
| 278 | auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream(); |
| 279 | if (auto E = ExpectedIpi.takeError()) |
| 280 | fatal(E, "Type server does not have TPI stream"); |
| 281 | if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap, |
| 282 | ExpectedIpi->typeArray())) |
| 283 | fatal(Err, "codeview::mergeIdRecords failed"); |
| 284 | |
| 285 | return IndexMap; |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 288 | static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) { |
| 289 | if (TI.isSimple()) |
| 290 | return true; |
| 291 | if (TI.toArrayIndex() >= TypeIndexMap.size()) |
| 292 | return false; |
| 293 | TI = TypeIndexMap[TI.toArrayIndex()]; |
| 294 | return true; |
| 295 | } |
| 296 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 297 | static void remapTypesInSymbolRecord(ObjFile *File, |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 298 | MutableArrayRef<uint8_t> Contents, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 299 | const CVIndexMap &IndexMap, |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 300 | ArrayRef<TiReference> TypeRefs) { |
| 301 | for (const TiReference &Ref : TypeRefs) { |
| 302 | unsigned ByteSize = Ref.Count * sizeof(TypeIndex); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 303 | if (Contents.size() < Ref.Offset + ByteSize) |
| 304 | fatal("symbol record too short"); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 305 | |
| 306 | // This can be an item index or a type index. Choose the appropriate map. |
| 307 | ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap; |
| 308 | if (Ref.Kind == TiRefKind::IndexRef && IndexMap.IsTypeServerMap) |
| 309 | TypeOrItemMap = IndexMap.IPIMap; |
| 310 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 311 | MutableArrayRef<TypeIndex> TIs( |
| 312 | reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 313 | for (TypeIndex &TI : TIs) { |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 314 | if (!remapTypeIndex(TI, TypeOrItemMap)) { |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 315 | TI = TypeIndex(SimpleTypeKind::NotTranslated); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 316 | log("ignoring symbol record in " + File->getName() + |
| 317 | " with bad type index 0x" + utohexstr(TI.getIndex())); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 318 | continue; |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 319 | } |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame] | 320 | } |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 321 | } |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /// MSVC translates S_PROC_ID_END to S_END. |
| 325 | uint16_t canonicalizeSymbolKind(SymbolKind Kind) { |
| 326 | if (Kind == SymbolKind::S_PROC_ID_END) |
| 327 | return SymbolKind::S_END; |
| 328 | return Kind; |
| 329 | } |
| 330 | |
| 331 | /// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned. |
| 332 | /// The object file may not be aligned. |
| 333 | static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym, |
| 334 | BumpPtrAllocator &Alloc) { |
| 335 | size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb)); |
| 336 | assert(Size >= 4 && "record too short"); |
| 337 | assert(Size <= MaxRecordLength && "record too long"); |
| 338 | void *Mem = Alloc.Allocate(Size, 4); |
| 339 | |
| 340 | // Copy the symbol record and zero out any padding bytes. |
| 341 | MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size); |
| 342 | memcpy(NewData.data(), Sym.data().data(), Sym.length()); |
| 343 | memset(NewData.data() + Sym.length(), 0, Size - Sym.length()); |
| 344 | |
| 345 | // Update the record prefix length. It should point to the beginning of the |
| 346 | // next record. MSVC does some canonicalization of the record kind, so we do |
| 347 | // that as well. |
| 348 | auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem); |
| 349 | Prefix->RecordKind = canonicalizeSymbolKind(Sym.kind()); |
| 350 | Prefix->RecordLen = Size - 2; |
| 351 | return NewData; |
| 352 | } |
| 353 | |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 354 | /// Return true if this symbol opens a scope. This implies that the symbol has |
| 355 | /// "parent" and "end" fields, which contain the offset of the S_END or |
| 356 | /// S_INLINESITE_END record. |
| 357 | static bool symbolOpensScope(SymbolKind Kind) { |
| 358 | switch (Kind) { |
| 359 | case SymbolKind::S_GPROC32: |
| 360 | case SymbolKind::S_LPROC32: |
| 361 | case SymbolKind::S_LPROC32_ID: |
| 362 | case SymbolKind::S_GPROC32_ID: |
| 363 | case SymbolKind::S_BLOCK32: |
| 364 | case SymbolKind::S_SEPCODE: |
| 365 | case SymbolKind::S_THUNK32: |
| 366 | case SymbolKind::S_INLINESITE: |
| 367 | case SymbolKind::S_INLINESITE2: |
| 368 | return true; |
| 369 | default: |
| 370 | break; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | static bool symbolEndsScope(SymbolKind Kind) { |
| 376 | switch (Kind) { |
| 377 | case SymbolKind::S_END: |
| 378 | case SymbolKind::S_PROC_ID_END: |
| 379 | case SymbolKind::S_INLINESITE_END: |
| 380 | return true; |
| 381 | default: |
| 382 | break; |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | struct ScopeRecord { |
| 388 | ulittle32_t PtrParent; |
| 389 | ulittle32_t PtrEnd; |
| 390 | }; |
| 391 | |
| 392 | struct SymbolScope { |
| 393 | ScopeRecord *OpeningRecord; |
| 394 | uint32_t ScopeOffset; |
| 395 | }; |
| 396 | |
| 397 | static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack, |
| 398 | uint32_t CurOffset, CVSymbol &Sym) { |
| 399 | assert(symbolOpensScope(Sym.kind())); |
| 400 | SymbolScope S; |
| 401 | S.ScopeOffset = CurOffset; |
| 402 | S.OpeningRecord = const_cast<ScopeRecord *>( |
| 403 | reinterpret_cast<const ScopeRecord *>(Sym.content().data())); |
| 404 | S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset; |
| 405 | Stack.push_back(S); |
| 406 | } |
| 407 | |
| 408 | static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack, |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 409 | uint32_t CurOffset, ObjFile *File) { |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 410 | if (Stack.empty()) { |
| 411 | warn("symbol scopes are not balanced in " + File->getName()); |
| 412 | return; |
| 413 | } |
| 414 | SymbolScope S = Stack.pop_back_val(); |
| 415 | S.OpeningRecord->PtrEnd = CurOffset; |
| 416 | } |
| 417 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 418 | static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File, |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 419 | const CVIndexMap &IndexMap, |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 420 | BinaryStreamRef SymData) { |
| 421 | // FIXME: Improve error recovery by warning and skipping records when |
| 422 | // possible. |
| 423 | CVSymbolArray Syms; |
| 424 | BinaryStreamReader Reader(SymData); |
| 425 | ExitOnErr(Reader.readArray(Syms, Reader.getLength())); |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 426 | SmallVector<SymbolScope, 4> Scopes; |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 427 | for (const CVSymbol &Sym : Syms) { |
| 428 | // Discover type index references in the record. Skip it if we don't know |
| 429 | // where they are. |
| 430 | SmallVector<TiReference, 32> TypeRefs; |
| 431 | if (!discoverTypeIndices(Sym, TypeRefs)) { |
| 432 | log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind())); |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | // Copy the symbol record so we can mutate it. |
| 437 | MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc); |
| 438 | |
| 439 | // Re-map all the type index references. |
| 440 | MutableArrayRef<uint8_t> Contents = |
| 441 | NewData.drop_front(sizeof(RecordPrefix)); |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 442 | remapTypesInSymbolRecord(File, Contents, IndexMap, TypeRefs); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 443 | |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 444 | // Fill in "Parent" and "End" fields by maintaining a stack of scopes. |
| 445 | CVSymbol NewSym(Sym.kind(), NewData); |
| 446 | if (symbolOpensScope(Sym.kind())) |
| 447 | scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym); |
| 448 | else if (symbolEndsScope(Sym.kind())) |
| 449 | scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 450 | |
| 451 | // Add the symbol to the module. |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 452 | File->ModuleDBI->addSymbol(NewSym); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 456 | // Allocate memory for a .debug$S section and relocate it. |
| 457 | static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc, |
| 458 | SectionChunk *DebugChunk) { |
| 459 | uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize()); |
| 460 | assert(DebugChunk->OutputSectionOff == 0 && |
| 461 | "debug sections should not be in output sections"); |
| 462 | DebugChunk->writeTo(Buffer); |
| 463 | return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()), |
| 464 | ".debug$S"); |
| 465 | } |
| 466 | |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 467 | void PDBLinker::addObjFile(ObjFile *File) { |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 468 | // Add a module descriptor for every object file. We need to put an absolute |
| 469 | // path to the object into the PDB. If this is a plain object, we make its |
| 470 | // path absolute. If it's an object in an archive, we make the archive path |
| 471 | // absolute. |
| 472 | bool InArchive = !File->ParentName.empty(); |
| 473 | SmallString<128> Path = InArchive ? File->ParentName : File->getName(); |
| 474 | sys::fs::make_absolute(Path); |
| 475 | sys::path::native(Path, sys::path::Style::windows); |
| 476 | StringRef Name = InArchive ? File->getName() : StringRef(Path); |
Zachary Turner | 2897e03 | 2017-05-25 21:16:03 +0000 | [diff] [blame] | 477 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 478 | File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name)); |
| 479 | File->ModuleDBI->setObjFileName(Path); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 480 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 481 | // Before we can process symbol substreams from .debug$S, we need to process |
| 482 | // type information, file checksums, and the string table. Add type info to |
| 483 | // the PDB first, so that we can get the map from object file type and item |
| 484 | // indices to PDB type and item indices. |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 485 | CVIndexMap ObjectIndexMap; |
| 486 | const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap); |
Zachary Turner | 448dea4 | 2017-07-07 18:46:14 +0000 | [diff] [blame] | 487 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 488 | // Now do all live .debug$S sections. |
| 489 | for (SectionChunk *DebugChunk : File->getDebugChunks()) { |
| 490 | if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S") |
| 491 | continue; |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 492 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 493 | ArrayRef<uint8_t> RelocatedDebugContents = |
| 494 | relocateDebugChunk(Alloc, DebugChunk); |
| 495 | if (RelocatedDebugContents.empty()) |
| 496 | continue; |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 497 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 498 | DebugSubsectionArray Subsections; |
| 499 | BinaryStreamReader Reader(RelocatedDebugContents, support::little); |
| 500 | ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size())); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 501 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 502 | DebugStringTableSubsectionRef CVStrTab; |
| 503 | DebugChecksumsSubsectionRef Checksums; |
| 504 | for (const DebugSubsectionRecord &SS : Subsections) { |
| 505 | switch (SS.kind()) { |
| 506 | case DebugSubsectionKind::StringTable: |
| 507 | ExitOnErr(CVStrTab.initialize(SS.getRecordData())); |
| 508 | break; |
| 509 | case DebugSubsectionKind::FileChecksums: |
| 510 | ExitOnErr(Checksums.initialize(SS.getRecordData())); |
| 511 | break; |
| 512 | case DebugSubsectionKind::Lines: |
| 513 | // We can add the relocated line table directly to the PDB without |
| 514 | // modification because the file checksum offsets will stay the same. |
| 515 | File->ModuleDBI->addDebugSubsection(SS); |
| 516 | break; |
| 517 | case DebugSubsectionKind::Symbols: |
Reid Kleckner | 651db91 | 2017-07-18 00:21:25 +0000 | [diff] [blame] | 518 | mergeSymbolRecords(Alloc, File, IndexMap, SS.getRecordData()); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 519 | break; |
| 520 | default: |
| 521 | // FIXME: Process the rest of the subsections. |
| 522 | break; |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 525 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 526 | if (Checksums.valid()) { |
| 527 | // Make a new file checksum table that refers to offsets in the PDB-wide |
| 528 | // string table. Generally the string table subsection appears after the |
| 529 | // checksum table, so we have to do this after looping over all the |
| 530 | // subsections. |
| 531 | if (!CVStrTab.valid()) |
| 532 | fatal(".debug$S sections must have both a string table subsection " |
| 533 | "and a checksum subsection table or neither"); |
| 534 | auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab); |
| 535 | for (FileChecksumEntry &FC : Checksums) { |
| 536 | StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset)); |
| 537 | ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI, |
| 538 | FileName)); |
| 539 | NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum); |
| 540 | } |
| 541 | File->ModuleDBI->addDebugSubsection(std::move(NewChecksums)); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 546 | static PublicSym32 createPublic(Defined *Def) { |
| 547 | PublicSym32 Pub(SymbolKind::S_PUB32); |
| 548 | Pub.Name = Def->getName(); |
| 549 | if (auto *D = dyn_cast<DefinedCOFF>(Def)) { |
| 550 | if (D->getCOFFSymbol().isFunctionDefinition()) |
| 551 | Pub.Flags = PublicSymFlags::Function; |
| 552 | } else if (isa<DefinedImportThunk>(Def)) { |
| 553 | Pub.Flags = PublicSymFlags::Function; |
| 554 | } |
| 555 | |
| 556 | OutputSection *OS = Def->getChunk()->getOutputSection(); |
| 557 | assert(OS && "all publics should be in final image"); |
| 558 | Pub.Offset = Def->getRVA() - OS->getRVA(); |
| 559 | Pub.Segment = OS->SectionIndex; |
| 560 | return Pub; |
| 561 | } |
| 562 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 563 | // Add all object files to the PDB. Merge .debug$T sections into IpiData and |
| 564 | // TpiData. |
| 565 | void PDBLinker::addObjectsToPDB() { |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 +0000 | [diff] [blame] | 566 | for (ObjFile *File : ObjFile::Instances) |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 567 | addObjFile(File); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 568 | |
| 569 | Builder.getStringTableBuilder().setStrings(PDBStrTab); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 570 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 571 | // Construct TPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 572 | addTypeInfo(Builder.getTpiBuilder(), TypeTable); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 573 | |
| 574 | // Construct IPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 575 | addTypeInfo(Builder.getIpiBuilder(), IDTable); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 576 | |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 577 | // Compute the public symbols. |
| 578 | std::vector<PublicSym32> Publics; |
| 579 | Symtab->forEachSymbol([&Publics](Symbol *S) { |
| 580 | // Only emit defined, live symbols that have a chunk. |
| 581 | auto *Def = dyn_cast<Defined>(S->body()); |
| 582 | if (Def && Def->isLive() && Def->getChunk()) |
| 583 | Publics.push_back(createPublic(Def)); |
| 584 | }); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 585 | |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 +0000 | [diff] [blame] | 586 | if (!Publics.empty()) { |
| 587 | // Sort the public symbols and add them to the stream. |
| 588 | std::sort(Publics.begin(), Publics.end(), |
| 589 | [](const PublicSym32 &L, const PublicSym32 &R) { |
| 590 | return L.Name < R.Name; |
| 591 | }); |
| 592 | auto &PublicsBuilder = Builder.getPublicsBuilder(); |
| 593 | for (const PublicSym32 &Pub : Publics) |
| 594 | PublicsBuilder.addPublicSymbol(Pub); |
| 595 | } |
Zachary Turner | 8d927b6 | 2017-07-31 19:36:08 +0000 | [diff] [blame] | 596 | // Add globals stream. For now we don't actually write any thing useful to |
| 597 | // the globals stream, but the act of "getting" it also creates it lazily so |
| 598 | // that we write an empty stream. |
| 599 | (void)Builder.getGlobalsBuilder(); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 602 | static void addLinkerModuleSymbols(StringRef Path, |
| 603 | pdb::DbiModuleDescriptorBuilder &Mod, |
| 604 | BumpPtrAllocator &Allocator) { |
| 605 | codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb); |
| 606 | codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym); |
| 607 | codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym); |
| 608 | codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym); |
| 609 | |
| 610 | ONS.Name = "* Linker *"; |
| 611 | ONS.Signature = 0; |
| 612 | |
| 613 | CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386; |
| 614 | CS.Flags = CompileSym3Flags::None; |
| 615 | CS.VersionBackendBuild = 0; |
| 616 | CS.VersionBackendMajor = 0; |
| 617 | CS.VersionBackendMinor = 0; |
| 618 | CS.VersionBackendQFE = 0; |
| 619 | CS.VersionFrontendBuild = 0; |
| 620 | CS.VersionFrontendMajor = 0; |
| 621 | CS.VersionFrontendMinor = 0; |
| 622 | CS.VersionFrontendQFE = 0; |
| 623 | CS.Version = "LLVM Linker"; |
| 624 | CS.setLanguage(SourceLanguage::Link); |
| 625 | |
| 626 | ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front(); |
| 627 | std::string ArgStr = llvm::join(Args, " "); |
| 628 | EBS.Fields.push_back("cwd"); |
| 629 | SmallString<64> cwd; |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 630 | sys::fs::current_path(cwd); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 631 | EBS.Fields.push_back(cwd); |
| 632 | EBS.Fields.push_back("exe"); |
Zachary Turner | 77f23b9 | 2017-07-10 21:09:11 +0000 | [diff] [blame] | 633 | EBS.Fields.push_back(Config->Argv[0]); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 634 | EBS.Fields.push_back("pdb"); |
| 635 | EBS.Fields.push_back(Path); |
| 636 | EBS.Fields.push_back("cmd"); |
| 637 | EBS.Fields.push_back(ArgStr); |
| 638 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 639 | ONS, Allocator, CodeViewContainer::Pdb)); |
| 640 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 641 | CS, Allocator, CodeViewContainer::Pdb)); |
| 642 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 643 | EBS, Allocator, CodeViewContainer::Pdb)); |
| 644 | } |
| 645 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 646 | // Creates a PDB file. |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame^] | 647 | void coff::createPDB(SymbolTable *Symtab, |
| 648 | ArrayRef<OutputSection *> OutputSections, |
| 649 | ArrayRef<uint8_t> SectionTable, |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 650 | const llvm::codeview::DebugInfo *DI) { |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 651 | PDBLinker PDB(Symtab); |
| 652 | PDB.initialize(DI); |
| 653 | PDB.addObjectsToPDB(); |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame^] | 654 | PDB.addSections(OutputSections, SectionTable); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 655 | PDB.commit(); |
| 656 | } |
| 657 | |
| 658 | void PDBLinker::initialize(const llvm::codeview::DebugInfo *DI) { |
Rui Ueyama | 1297954 | 2016-09-30 20:53:45 +0000 | [diff] [blame] | 659 | ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 660 | |
Rui Ueyama | 8d3fb5d | 2016-10-05 22:08:58 +0000 | [diff] [blame] | 661 | // Create streams in MSF for predefined streams, namely |
| 662 | // PDB, TPI, DBI and IPI. |
| 663 | for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) |
| 664 | ExitOnErr(Builder.getMsfBuilder().addStream(0)); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 665 | |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 666 | // Add an Info stream. |
| 667 | auto &InfoBuilder = Builder.getInfoBuilder(); |
Saleem Abdulrasool | 0acd6dd | 2017-02-07 04:28:02 +0000 | [diff] [blame] | 668 | InfoBuilder.setAge(DI ? DI->PDB70.Age : 0); |
| 669 | |
Reid Kleckner | 67653ee | 2017-07-17 23:59:44 +0000 | [diff] [blame] | 670 | GUID uuid{}; |
Saleem Abdulrasool | 0acd6dd | 2017-02-07 04:28:02 +0000 | [diff] [blame] | 671 | if (DI) |
| 672 | memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid)); |
| 673 | InfoBuilder.setGuid(uuid); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 674 | InfoBuilder.setSignature(time(nullptr)); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 675 | InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 676 | |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 677 | // Add an empty DBI stream. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 678 | pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); |
Zachary Turner | c1e93e5 | 2017-07-07 18:45:56 +0000 | [diff] [blame] | 679 | DbiBuilder.setVersionHeader(pdb::PdbDbiV70); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 680 | ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {})); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 681 | } |
Rui Ueyama | 1343fac | 2016-10-06 22:52:01 +0000 | [diff] [blame] | 682 | |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame^] | 683 | void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule, |
| 684 | OutputSection *OS, Chunk *C) { |
| 685 | pdb::SectionContrib SC; |
| 686 | memset(&SC, 0, sizeof(SC)); |
| 687 | SC.ISect = OS->SectionIndex; |
| 688 | SC.Off = C->getRVA() - OS->getRVA(); |
| 689 | SC.Size = C->getSize(); |
| 690 | if (auto *SecChunk = dyn_cast<SectionChunk>(C)) { |
| 691 | SC.Characteristics = SecChunk->Header->Characteristics; |
| 692 | SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex(); |
| 693 | } else { |
| 694 | SC.Characteristics = OS->getCharacteristics(); |
| 695 | // FIXME: When we start creating DBI for import libraries, use those here. |
| 696 | SC.Imod = LinkerModule.getModuleIndex(); |
| 697 | } |
| 698 | SC.DataCrc = 0; // FIXME |
| 699 | SC.RelocCrc = 0; // FIXME |
| 700 | Builder.getDbiBuilder().addSectionContrib(SC); |
| 701 | } |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 702 | |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame^] | 703 | void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections, |
| 704 | ArrayRef<uint8_t> SectionTable) { |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 705 | // It's not entirely clear what this is, but the * Linker * module uses it. |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame^] | 706 | pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 707 | NativePath = Config->PDBPath; |
| 708 | sys::fs::make_absolute(NativePath); |
| 709 | sys::path::native(NativePath, sys::path::Style::windows); |
| 710 | uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath); |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 711 | auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *")); |
| 712 | LinkerModule.setPdbFilePathNI(PdbFilePathNI); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 713 | addLinkerModuleSymbols(NativePath, LinkerModule, Alloc); |
Rui Ueyama | c91f716 | 2016-11-16 01:10:46 +0000 | [diff] [blame] | 714 | |
Reid Kleckner | 175af4b | 2017-08-03 21:15:09 +0000 | [diff] [blame^] | 715 | // Add section contributions. They must be ordered by ascending RVA. |
| 716 | for (OutputSection *OS : OutputSections) |
| 717 | for (Chunk *C : OS->getChunks()) |
| 718 | addSectionContrib(LinkerModule, OS, C); |
| 719 | |
| 720 | // Add Section Map stream. |
| 721 | ArrayRef<object::coff_section> Sections = { |
| 722 | (const object::coff_section *)SectionTable.data(), |
| 723 | SectionTable.size() / sizeof(object::coff_section)}; |
| 724 | SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections); |
| 725 | DbiBuilder.setSectionMap(SectionMap); |
| 726 | |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 727 | // Add COFF section header stream. |
| 728 | ExitOnErr( |
| 729 | DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 730 | } |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 731 | |
Reid Kleckner | 0faf6d7 | 2017-07-14 00:14:58 +0000 | [diff] [blame] | 732 | void PDBLinker::commit() { |
Rui Ueyama | 3e9d6bb | 2016-09-26 23:53:55 +0000 | [diff] [blame] | 733 | // Write to a file. |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 734 | ExitOnErr(Builder.commit(Config->PDBPath)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 735 | } |