1 //===- Librarian.cpp ------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains functions for the Librarian. The librarian creates and
11 // manages libraries of the Common Object File Format (COFF) object files. It
12 // primarily is used for creating static libraries and import libraries.
14 //===----------------------------------------------------------------------===//
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/ArchiveWriter.h"
22 #include "llvm/Object/COFF.h"
23 #include "llvm/Support/Path.h"
27 using namespace lld::coff;
28 using namespace llvm::COFF;
29 using namespace llvm::object;
32 static bool is32bit() {
33 switch (Config->Machine) {
35 llvm_unreachable("unsupported machine");
36 case IMAGE_FILE_MACHINE_AMD64:
38 case IMAGE_FILE_MACHINE_ARMNT:
39 case IMAGE_FILE_MACHINE_I386:
44 static uint16_t getImgRelRelocation() {
45 switch (Config->Machine) {
47 llvm_unreachable("unsupported machine");
48 case IMAGE_FILE_MACHINE_AMD64:
49 return IMAGE_REL_AMD64_ADDR32NB;
50 case IMAGE_FILE_MACHINE_ARMNT:
51 return IMAGE_REL_ARM_ADDR32NB;
52 case IMAGE_FILE_MACHINE_I386:
53 return IMAGE_REL_I386_DIR32NB;
57 template <class T> static void append(std::vector<uint8_t> &B, const T &Data) {
59 B.resize(S + sizeof(T));
60 memcpy(&B[S], &Data, sizeof(T));
63 static void writeStringTable(std::vector<uint8_t> &B,
64 ArrayRef<const std::string> Strings) {
65 // The COFF string table consists of a 4-byte value which is the size of the
66 // table, including the length field itself. This value is followed by the
67 // string content itself, which is an array of null-terminated C-style
68 // strings. The termination is important as they are referenced to by offset
69 // by the symbol entity in the file format.
71 std::vector<uint8_t>::size_type Pos = B.size();
72 std::vector<uint8_t>::size_type Offset = B.size();
74 // Skip over the length field, we will fill it in later as we will have
75 // computed the length while emitting the string content itself.
76 Pos += sizeof(uint32_t);
78 for (const auto &S : Strings) {
79 B.resize(Pos + S.length() + 1);
80 strcpy(reinterpret_cast<char *>(&B[Pos]), S.c_str());
81 Pos += S.length() + 1;
84 // Backfill the length of the table now that it has been computed.
85 support::ulittle32_t Length(B.size() - Offset);
86 memcpy(&B[Offset], &Length, sizeof(Length));
89 static std::string getImplibPath() {
90 if (!Config->Implib.empty())
91 return Config->Implib;
92 SmallString<128> Out = StringRef(Config->OutputFile);
93 sys::path::replace_extension(Out, ".lib");
97 static ImportNameType getNameType(StringRef Sym, StringRef ExtName) {
99 return IMPORT_NAME_UNDECORATE;
100 if (Config->Machine == I386 && Sym.startswith("_"))
101 return IMPORT_NAME_NOPREFIX;
105 static std::string replace(StringRef S, StringRef From, StringRef To) {
106 size_t Pos = S.find(From);
107 assert(Pos != StringRef::npos);
108 return (Twine(S.substr(0, Pos)) + To + S.substr(Pos + From.size())).str();
111 static const std::string NullImportDescriptorSymbolName =
112 "__NULL_IMPORT_DESCRIPTOR";
115 // This class constructs various small object files necessary to support linking
116 // symbols imported from a DLL. The contents are pretty strictly defined and
117 // nearly entirely static. The details of the structures files are defined in
118 // WINNT.h and the PE/COFF specification.
119 class ObjectFactory {
120 using u16 = support::ulittle16_t;
121 using u32 = support::ulittle32_t;
123 BumpPtrAllocator Alloc;
126 std::string ImportDescriptorSymbolName;
127 std::string NullThunkSymbolName;
130 ObjectFactory(StringRef S)
131 : DLLName(S), Library(S.drop_back(4)),
132 ImportDescriptorSymbolName(("__IMPORT_DESCRIPTOR_" + Library).str()),
133 NullThunkSymbolName(("\x7f" + Library + "_NULL_THUNK_DATA").str()) {}
135 // Creates an Import Descriptor. This is a small object file which contains a
136 // reference to the terminators and contains the library name (entry) for the
137 // import name table. It will force the linker to construct the necessary
138 // structure to import symbols from the DLL.
139 NewArchiveMember createImportDescriptor(std::vector<uint8_t> &Buffer);
141 // Creates a NULL import descriptor. This is a small object file whcih
142 // contains a NULL import descriptor. It is used to terminate the imports
143 // from a specific DLL.
144 NewArchiveMember createNullImportDescriptor(std::vector<uint8_t> &Buffer);
146 // Create a NULL Thunk Entry. This is a small object file which contains a
147 // NULL Import Address Table entry and a NULL Import Lookup Table Entry. It
148 // is used to terminate the IAT and ILT.
149 NewArchiveMember createNullThunk(std::vector<uint8_t> &Buffer);
151 // Create a short import file which is described in PE/COFF spec 7. Import
153 NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal,
154 ImportNameType NameType, bool isData);
159 ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
160 static const uint32_t NumberOfSections = 2;
161 static const uint32_t NumberOfSymbols = 7;
162 static const uint32_t NumberOfRelocations = 3;
165 coff_file_header Header{
166 u16(Config->Machine), u16(NumberOfSections), u32(0),
167 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
169 sizeof(coff_import_directory_table_entry) +
170 NumberOfRelocations * sizeof(coff_relocation) +
172 (DLLName.size() + 1)),
173 u32(NumberOfSymbols), u16(0),
174 u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0),
176 append(Buffer, Header);
178 // Section Header Table
179 static const coff_section SectionTable[NumberOfSections] = {
180 {{'.', 'i', 'd', 'a', 't', 'a', '$', '2'},
183 u32(sizeof(coff_import_directory_table_entry)),
184 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
185 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
186 sizeof(coff_import_directory_table_entry)),
188 u16(NumberOfRelocations),
190 u32(IMAGE_SCN_ALIGN_4BYTES | IMAGE_SCN_CNT_INITIALIZED_DATA |
191 IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)},
192 {{'.', 'i', 'd', 'a', 't', 'a', '$', '6'},
195 u32(DLLName.size() + 1),
196 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
197 sizeof(coff_import_directory_table_entry) +
198 NumberOfRelocations * sizeof(coff_relocation)),
203 u32(IMAGE_SCN_ALIGN_2BYTES | IMAGE_SCN_CNT_INITIALIZED_DATA |
204 IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)},
206 append(Buffer, SectionTable);
209 static const coff_import_directory_table_entry ImportDescriptor{
210 u32(0), u32(0), u32(0), u32(0), u32(0),
212 append(Buffer, ImportDescriptor);
214 static const coff_relocation RelocationTable[NumberOfRelocations] = {
215 {u32(offsetof(coff_import_directory_table_entry, NameRVA)), u32(2),
216 u16(getImgRelRelocation())},
217 {u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)),
218 u32(3), u16(getImgRelRelocation())},
219 {u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)),
220 u32(4), u16(getImgRelRelocation())},
222 append(Buffer, RelocationTable);
225 auto S = Buffer.size();
226 Buffer.resize(S + DLLName.size() + 1);
227 memcpy(&Buffer[S], DLLName.data(), DLLName.size());
228 Buffer[S + DLLName.size()] = '\0';
231 coff_symbol16 SymbolTable[NumberOfSymbols] = {
232 {{{0, 0, 0, 0, 0, 0, 0, 0}},
236 IMAGE_SYM_CLASS_EXTERNAL,
238 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '2'}},
242 IMAGE_SYM_CLASS_SECTION,
244 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '6'}},
248 IMAGE_SYM_CLASS_STATIC,
250 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '4'}},
254 IMAGE_SYM_CLASS_SECTION,
256 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '5'}},
260 IMAGE_SYM_CLASS_SECTION,
262 {{{0, 0, 0, 0, 0, 0, 0, 0}},
266 IMAGE_SYM_CLASS_EXTERNAL,
268 {{{0, 0, 0, 0, 0, 0, 0, 0}},
272 IMAGE_SYM_CLASS_EXTERNAL,
275 reinterpret_cast<StringTableOffset &>(SymbolTable[0].Name).Offset =
277 reinterpret_cast<StringTableOffset &>(SymbolTable[5].Name).Offset =
278 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1;
279 reinterpret_cast<StringTableOffset &>(SymbolTable[6].Name).Offset =
280 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1 +
281 NullImportDescriptorSymbolName.length() + 1;
282 append(Buffer, SymbolTable);
285 writeStringTable(Buffer,
286 {ImportDescriptorSymbolName, NullImportDescriptorSymbolName,
287 NullThunkSymbolName});
289 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
290 return {MemoryBufferRef(F, DLLName)};
294 ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
295 static const uint32_t NumberOfSections = 1;
296 static const uint32_t NumberOfSymbols = 1;
299 coff_file_header Header{
300 u16(Config->Machine), u16(NumberOfSections), u32(0),
301 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
303 sizeof(coff_import_directory_table_entry)),
304 u32(NumberOfSymbols), u16(0),
305 u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0),
307 append(Buffer, Header);
309 // Section Header Table
310 static const coff_section SectionTable[NumberOfSections] = {
311 {{'.', 'i', 'd', 'a', 't', 'a', '$', '3'},
314 u32(sizeof(coff_import_directory_table_entry)),
315 u32(sizeof(coff_file_header) +
316 (NumberOfSections * sizeof(coff_section))),
321 u32(IMAGE_SCN_ALIGN_4BYTES | IMAGE_SCN_CNT_INITIALIZED_DATA |
322 IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)},
324 append(Buffer, SectionTable);
327 static const coff_import_directory_table_entry ImportDescriptor{
328 u32(0), u32(0), u32(0), u32(0), u32(0),
330 append(Buffer, ImportDescriptor);
333 coff_symbol16 SymbolTable[NumberOfSymbols] = {
334 {{{0, 0, 0, 0, 0, 0, 0, 0}},
338 IMAGE_SYM_CLASS_EXTERNAL,
341 reinterpret_cast<StringTableOffset &>(SymbolTable[0].Name).Offset =
343 append(Buffer, SymbolTable);
346 writeStringTable(Buffer, {NullImportDescriptorSymbolName});
348 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
349 return {MemoryBufferRef(F, DLLName)};
352 NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
353 static const uint32_t NumberOfSections = 2;
354 static const uint32_t NumberOfSymbols = 1;
355 uint32_t VASize = is32bit() ? 4 : 8;
358 coff_file_header Header{
359 u16(Config->Machine), u16(NumberOfSections), u32(0),
360 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
365 u32(NumberOfSymbols), u16(0),
366 u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0),
368 append(Buffer, Header);
370 // Section Header Table
371 static const coff_section SectionTable[NumberOfSections] = {
372 {{'.', 'i', 'd', 'a', 't', 'a', '$', '5'},
376 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
381 u32((is32bit() ? IMAGE_SCN_ALIGN_4BYTES : IMAGE_SCN_ALIGN_8BYTES) |
382 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
383 IMAGE_SCN_MEM_WRITE)},
384 {{'.', 'i', 'd', 'a', 't', 'a', '$', '4'},
388 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
394 u32((is32bit() ? IMAGE_SCN_ALIGN_4BYTES : IMAGE_SCN_ALIGN_8BYTES) |
395 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
396 IMAGE_SCN_MEM_WRITE)},
398 append(Buffer, SectionTable);
401 append(Buffer, u32(0));
403 append(Buffer, u32(0));
406 append(Buffer, u32(0));
408 append(Buffer, u32(0));
411 coff_symbol16 SymbolTable[NumberOfSymbols] = {
412 {{{0, 0, 0, 0, 0, 0, 0, 0}},
416 IMAGE_SYM_CLASS_EXTERNAL,
419 reinterpret_cast<StringTableOffset &>(SymbolTable[0].Name).Offset =
421 append(Buffer, SymbolTable);
424 writeStringTable(Buffer, {NullThunkSymbolName});
426 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
427 return {MemoryBufferRef{F, DLLName}};
430 NewArchiveMember ObjectFactory::createShortImport(StringRef Sym,
432 ImportNameType NameType,
434 size_t ImpSize = DLLName.size() + Sym.size() + 2; // +2 for NULs
435 size_t Size = sizeof(coff_import_header) + ImpSize;
436 char *Buf = Alloc.Allocate<char>(Size);
437 memset(Buf, 0, Size);
440 // Write short import library.
441 auto *Imp = reinterpret_cast<coff_import_header *>(P);
444 Imp->Machine = Config->Machine;
445 Imp->SizeOfData = ImpSize;
447 Imp->OrdinalHint = Ordinal;
448 Imp->TypeInfo = (isData ? IMPORT_DATA : IMPORT_CODE);
449 Imp->TypeInfo |= NameType << 2;
451 // Write symbol name and DLL name.
452 memcpy(P, Sym.data(), Sym.size());
454 memcpy(P, DLLName.data(), DLLName.size());
456 return {MemoryBufferRef(StringRef(Buf, Size), DLLName)};
459 // Creates an import library for a DLL. In this function, we first
460 // create an empty import library using lib.exe and then adds short
461 // import files to that file.
462 void lld::coff::writeImportLibrary() {
463 std::vector<NewArchiveMember> Members;
465 std::string Path = getImplibPath();
466 std::string DLLName = sys::path::filename(Config->OutputFile);
467 ObjectFactory OF(DLLName);
469 std::vector<uint8_t> ImportDescriptor;
470 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
472 std::vector<uint8_t> NullImportDescriptor;
473 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
475 std::vector<uint8_t> NullThunk;
476 Members.push_back(OF.createNullThunk(NullThunk));
478 for (Export &E : Config->Exports) {
482 ImportNameType Type = getNameType(E.SymbolName, E.Name);
483 std::string Name = E.ExtName.empty()
484 ? std::string(E.SymbolName)
485 : replace(E.SymbolName, E.Name, E.ExtName);
486 Members.push_back(OF.createShortImport(Name, E.Ordinal, Type, E.Data));
489 std::pair<StringRef, std::error_code> Result =
490 writeArchive(Path, Members, /*WriteSymtab*/ true, object::Archive::K_GNU,
491 /*Deterministic*/ true, /*Thin*/ false);
492 if (auto EC = Result.second)
493 fatal(EC, "failed to write " + Path);