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