1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLD_MACHO_INPUT_FILES_H 10 #define LLD_MACHO_INPUT_FILES_H 11 12 #include "lld/Common/LLVM.h" 13 #include "llvm/ADT/DenseSet.h" 14 #include "llvm/BinaryFormat/MachO.h" 15 #include "llvm/Object/Archive.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include <vector> 18 19 namespace lld { 20 namespace macho { 21 22 class InputSection; 23 class Symbol; 24 struct Reloc; 25 26 class InputFile { 27 public: 28 enum Kind { 29 ObjKind, 30 DylibKind, 31 }; 32 33 virtual ~InputFile() = default; 34 Kind kind() const { return fileKind; } 35 StringRef getName() const { return mb.getBufferIdentifier(); } 36 37 MemoryBufferRef mb; 38 std::vector<Symbol *> symbols; 39 std::vector<InputSection *> sections; 40 41 protected: 42 InputFile(Kind kind, MemoryBufferRef mb) : mb(mb), fileKind(kind) {} 43 44 std::vector<InputSection *> parseSections(ArrayRef<llvm::MachO::section_64>); 45 46 void parseRelocations(const llvm::MachO::section_64 &, 47 std::vector<Reloc> &relocs); 48 49 private: 50 const Kind fileKind; 51 }; 52 53 // .o file 54 class ObjFile : public InputFile { 55 public: 56 explicit ObjFile(MemoryBufferRef mb); 57 static bool classof(const InputFile *f) { return f->kind() == ObjKind; } 58 }; 59 60 // .dylib file 61 class DylibFile : public InputFile { 62 public: 63 explicit DylibFile(MemoryBufferRef mb); 64 static bool classof(const InputFile *f) { return f->kind() == DylibKind; } 65 66 StringRef dylibName; 67 uint64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel 68 }; 69 70 extern std::vector<InputFile *> inputFiles; 71 72 llvm::Optional<MemoryBufferRef> readFile(StringRef path); 73 74 } // namespace macho 75 76 std::string toString(const macho::InputFile *file); 77 } // namespace lld 78 79 #endif 80