Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1 | //===- InputFiles.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 | |
| 10 | #include "Chunks.h" |
| 11 | #include "InputFiles.h" |
| 12 | #include "Writer.h" |
| 13 | #include "lld/Core/Error.h" |
| 14 | #include "llvm/ADT/STLExtras.h" |
| 15 | #include "llvm/Object/COFF.h" |
| 16 | #include "llvm/Support/COFF.h" |
| 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/Endian.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | using namespace llvm::object; |
| 22 | using namespace llvm::support::endian; |
| 23 | using llvm::COFF::ImportHeader; |
| 24 | using llvm::RoundUpToAlignment; |
| 25 | using llvm::sys::fs::identify_magic; |
| 26 | using llvm::sys::fs::file_magic; |
| 27 | |
| 28 | namespace lld { |
| 29 | namespace coff { |
| 30 | |
| 31 | // Returns the last element of a path, which is supposed to be a filename. |
| 32 | static StringRef getBasename(StringRef Path) { |
| 33 | size_t Pos = Path.rfind('\\'); |
| 34 | if (Pos == StringRef::npos) |
| 35 | return Path; |
| 36 | return Path.substr(Pos + 1); |
| 37 | } |
| 38 | |
| 39 | // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)". |
| 40 | std::string InputFile::getShortName() { |
| 41 | if (ParentName == "") |
| 42 | return getName().lower(); |
| 43 | std::string Res = (getBasename(ParentName) + "(" + |
| 44 | getBasename(getName()) + ")").str(); |
| 45 | return StringRef(Res).lower(); |
| 46 | } |
| 47 | |
| 48 | std::error_code ArchiveFile::parse() { |
| 49 | // Get a memory buffer. |
Rui Ueyama | d52824d | 2015-05-28 20:16:25 +0000 | [diff] [blame] | 50 | auto MBOrErr = MemoryBuffer::getFile(Filename); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 51 | if (auto EC = MBOrErr.getError()) |
| 52 | return EC; |
| 53 | MB = std::move(MBOrErr.get()); |
| 54 | |
| 55 | // Parse a memory buffer as an archive file. |
| 56 | auto ArchiveOrErr = Archive::create(MB->getMemBufferRef()); |
| 57 | if (auto EC = ArchiveOrErr.getError()) |
| 58 | return EC; |
| 59 | File = std::move(ArchiveOrErr.get()); |
| 60 | |
| 61 | // Allocate a buffer for Lazy objects. |
| 62 | size_t BufSize = File->getNumberOfSymbols() * sizeof(Lazy); |
| 63 | Lazy *Buf = (Lazy *)Alloc.Allocate(BufSize, llvm::alignOf<Lazy>()); |
| 64 | |
| 65 | // Read the symbol table to construct Lazy objects. |
| 66 | uint32_t I = 0; |
| 67 | for (const Archive::Symbol &Sym : File->symbols()) { |
| 68 | // Skip special symbol exists in import library files. |
| 69 | if (Sym.getName() == "__NULL_IMPORT_DESCRIPTOR") |
| 70 | continue; |
| 71 | SymbolBodies.push_back(new (&Buf[I++]) Lazy(this, Sym)); |
| 72 | } |
| 73 | return std::error_code(); |
| 74 | } |
| 75 | |
| 76 | // Returns a buffer pointing to a member file containing a given symbol. |
| 77 | ErrorOr<MemoryBufferRef> ArchiveFile::getMember(const Archive::Symbol *Sym) { |
| 78 | auto ItOrErr = Sym->getMember(); |
| 79 | if (auto EC = ItOrErr.getError()) |
| 80 | return EC; |
| 81 | Archive::child_iterator It = ItOrErr.get(); |
| 82 | |
| 83 | // Return an empty buffer if we have already returned the same buffer. |
| 84 | const char *StartAddr = It->getBuffer().data(); |
| 85 | auto Pair = Seen.insert(StartAddr); |
| 86 | if (!Pair.second) |
| 87 | return MemoryBufferRef(); |
| 88 | return It->getMemoryBufferRef(); |
| 89 | } |
| 90 | |
| 91 | std::error_code ObjectFile::parse() { |
| 92 | // MBRef is not initialized if this is not an archive member. |
| 93 | if (MBRef.getBuffer().empty()) { |
Rui Ueyama | d52824d | 2015-05-28 20:16:25 +0000 | [diff] [blame] | 94 | auto MBOrErr = MemoryBuffer::getFile(Filename); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 95 | if (auto EC = MBOrErr.getError()) |
| 96 | return EC; |
| 97 | MB = std::move(MBOrErr.get()); |
| 98 | MBRef = MB->getMemBufferRef(); |
| 99 | } |
| 100 | |
| 101 | // Parse a memory buffer as a COFF file. |
| 102 | auto BinOrErr = createBinary(MBRef); |
| 103 | if (auto EC = BinOrErr.getError()) |
| 104 | return EC; |
| 105 | std::unique_ptr<Binary> Bin = std::move(BinOrErr.get()); |
| 106 | |
| 107 | if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) { |
| 108 | Bin.release(); |
| 109 | COFFObj.reset(Obj); |
| 110 | } else { |
Rui Ueyama | d52824d | 2015-05-28 20:16:25 +0000 | [diff] [blame] | 111 | return make_dynamic_error_code(Twine(Filename) + " is not a COFF file."); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Read section and symbol tables. |
| 115 | if (auto EC = initializeChunks()) |
| 116 | return EC; |
| 117 | return initializeSymbols(); |
| 118 | } |
| 119 | |
| 120 | SymbolBody *ObjectFile::getSymbolBody(uint32_t SymbolIndex) { |
| 121 | return SparseSymbolBodies[SymbolIndex]->getReplacement(); |
| 122 | } |
| 123 | |
| 124 | std::error_code ObjectFile::initializeChunks() { |
| 125 | uint32_t NumSections = COFFObj->getNumberOfSections(); |
| 126 | Chunks.reserve(NumSections); |
| 127 | SparseChunks.resize(NumSections + 1); |
| 128 | for (uint32_t I = 1; I < NumSections + 1; ++I) { |
| 129 | const coff_section *Sec; |
| 130 | StringRef Name; |
| 131 | if (auto EC = COFFObj->getSection(I, Sec)) |
| 132 | return make_dynamic_error_code(Twine("getSection failed: ") + Name + |
| 133 | ": " + EC.message()); |
| 134 | if (auto EC = COFFObj->getSectionName(Sec, Name)) |
| 135 | return make_dynamic_error_code(Twine("getSectionName failed: ") + Name + |
| 136 | ": " + EC.message()); |
| 137 | if (Name == ".drectve") { |
| 138 | ArrayRef<uint8_t> Data; |
| 139 | COFFObj->getSectionContents(Sec, Data); |
| 140 | Directives = StringRef((char *)Data.data(), Data.size()).trim(); |
| 141 | continue; |
| 142 | } |
| 143 | if (Name.startswith(".debug")) |
| 144 | continue; |
| 145 | if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE) |
| 146 | continue; |
| 147 | auto *C = new (Alloc) SectionChunk(this, Sec, I); |
| 148 | Chunks.push_back(C); |
| 149 | SparseChunks[I] = C; |
| 150 | } |
| 151 | return std::error_code(); |
| 152 | } |
| 153 | |
| 154 | std::error_code ObjectFile::initializeSymbols() { |
| 155 | uint32_t NumSymbols = COFFObj->getNumberOfSymbols(); |
| 156 | SymbolBodies.reserve(NumSymbols); |
| 157 | SparseSymbolBodies.resize(NumSymbols); |
| 158 | int32_t LastSectionNumber = 0; |
| 159 | for (uint32_t I = 0; I < NumSymbols; ++I) { |
| 160 | // Get a COFFSymbolRef object. |
| 161 | auto SymOrErr = COFFObj->getSymbol(I); |
| 162 | if (auto EC = SymOrErr.getError()) |
Rui Ueyama | d52824d | 2015-05-28 20:16:25 +0000 | [diff] [blame] | 163 | return make_dynamic_error_code(Twine("broken object file: ") + Filename + |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 164 | ": " + EC.message()); |
| 165 | COFFSymbolRef Sym = SymOrErr.get(); |
| 166 | |
| 167 | // Get a symbol name. |
| 168 | StringRef SymbolName; |
| 169 | if (auto EC = COFFObj->getSymbolName(Sym, SymbolName)) |
Rui Ueyama | d52824d | 2015-05-28 20:16:25 +0000 | [diff] [blame] | 170 | return make_dynamic_error_code(Twine("broken object file: ") + Filename + |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 171 | ": " + EC.message()); |
| 172 | // Skip special symbols. |
| 173 | if (SymbolName == "@comp.id" || SymbolName == "@feat.00") |
| 174 | continue; |
| 175 | |
| 176 | const void *AuxP = nullptr; |
| 177 | if (Sym.getNumberOfAuxSymbols()) |
| 178 | AuxP = COFFObj->getSymbol(I + 1)->getRawPtr(); |
| 179 | bool IsFirst = (LastSectionNumber != Sym.getSectionNumber()); |
| 180 | |
| 181 | SymbolBody *Body = createSymbolBody(SymbolName, Sym, AuxP, IsFirst); |
| 182 | if (Body) { |
| 183 | SymbolBodies.push_back(Body); |
| 184 | SparseSymbolBodies[I] = Body; |
| 185 | } |
| 186 | I += Sym.getNumberOfAuxSymbols(); |
| 187 | LastSectionNumber = Sym.getSectionNumber(); |
| 188 | } |
| 189 | return std::error_code(); |
| 190 | } |
| 191 | |
| 192 | SymbolBody *ObjectFile::createSymbolBody(StringRef Name, COFFSymbolRef Sym, |
| 193 | const void *AuxP, bool IsFirst) { |
| 194 | if (Sym.isUndefined()) |
| 195 | return new Undefined(Name); |
| 196 | if (Sym.isCommon()) { |
| 197 | Chunk *C = new (Alloc) CommonChunk(Sym); |
| 198 | Chunks.push_back(C); |
| 199 | return new (Alloc) DefinedRegular(Name, Sym, C); |
| 200 | } |
| 201 | if (Sym.isAbsolute()) |
| 202 | return new (Alloc) DefinedAbsolute(Name, Sym.getValue()); |
| 203 | // TODO: Handle IMAGE_WEAK_EXTERN_SEARCH_ALIAS |
| 204 | if (Sym.isWeakExternal()) { |
| 205 | auto *Aux = (const coff_aux_weak_external *)AuxP; |
| 206 | return new (Alloc) Undefined(Name, &SparseSymbolBodies[Aux->TagIndex]); |
| 207 | } |
| 208 | if (IsFirst && AuxP) { |
| 209 | if (Chunk *C = SparseChunks[Sym.getSectionNumber()]) { |
| 210 | auto *Aux = (coff_aux_section_definition *)AuxP; |
| 211 | auto *Parent = |
| 212 | (SectionChunk *)(SparseChunks[Aux->getNumber(Sym.isBigObj())]); |
| 213 | if (Parent) |
| 214 | Parent->addAssociative((SectionChunk *)C); |
| 215 | } |
| 216 | } |
| 217 | if (Chunk *C = SparseChunks[Sym.getSectionNumber()]) |
| 218 | return new (Alloc) DefinedRegular(Name, Sym, C); |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
| 222 | std::error_code ImportFile::parse() { |
| 223 | const char *Buf = MBRef.getBufferStart(); |
| 224 | const char *End = MBRef.getBufferEnd(); |
| 225 | const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf); |
| 226 | |
| 227 | // Check if the total size is valid. |
| 228 | if (End - Buf != sizeof(*Hdr) + Hdr->SizeOfData) |
| 229 | return make_dynamic_error_code("broken import library"); |
| 230 | |
| 231 | // Read names and create an __imp_ symbol. |
| 232 | StringRef Name = StringAlloc.save(StringRef(Buf + sizeof(*Hdr))); |
| 233 | StringRef ImpName = StringAlloc.save(Twine("__imp_") + Name); |
| 234 | StringRef DLLName(Buf + sizeof(coff_import_header) + Name.size() + 1); |
| 235 | auto *ImpSym = new (Alloc) DefinedImportData(DLLName, ImpName, Name); |
| 236 | SymbolBodies.push_back(ImpSym); |
| 237 | |
| 238 | // If type is function, we need to create a thunk which jump to an |
| 239 | // address pointed by the __imp_ symbol. (This allows you to call |
| 240 | // DLL functions just like regular non-DLL functions.) |
| 241 | if (Hdr->getType() == llvm::COFF::IMPORT_CODE) |
| 242 | SymbolBodies.push_back(new (Alloc) DefinedImportThunk(Name, ImpSym)); |
| 243 | return std::error_code(); |
| 244 | } |
| 245 | |
| 246 | } // namespace coff |
| 247 | } // namespace lld |