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