Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.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 | /// |
| 11 | /// \file Converts from in-memory normalized mach-o to in-memory Atoms. |
| 12 | /// |
| 13 | /// +------------+ |
| 14 | /// | normalized | |
| 15 | /// +------------+ |
| 16 | /// | |
| 17 | /// | |
| 18 | /// v |
| 19 | /// +-------+ |
| 20 | /// | Atoms | |
| 21 | /// +-------+ |
| 22 | |
| 23 | #include "MachONormalizedFile.h" |
| 24 | #include "File.h" |
| 25 | #include "Atoms.h" |
| 26 | |
| 27 | #include "lld/Core/LLVM.h" |
| 28 | |
| 29 | #include "llvm/Support/MachO.h" |
| 30 | |
| 31 | using namespace llvm::MachO; |
| 32 | |
| 33 | namespace lld { |
| 34 | namespace mach_o { |
| 35 | namespace normalized { |
| 36 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 37 | static uint64_t nextSymbolAddress(const NormalizedFile &normalizedFile, |
Nick Kledzik | 36baa33 | 2014-01-08 02:52:58 +0000 | [diff] [blame] | 38 | const Symbol &symbol) { |
| 39 | uint64_t symbolAddr = symbol.value; |
| 40 | uint8_t symbolSectionIndex = symbol.sect; |
| 41 | const Section §ion = normalizedFile.sections[symbolSectionIndex - 1]; |
| 42 | // If no symbol after this address, use end of section address. |
| 43 | uint64_t closestAddr = section.address + section.content.size(); |
| 44 | for (const Symbol &s : normalizedFile.globalSymbols) { |
| 45 | if (s.sect != symbolSectionIndex) |
| 46 | continue; |
| 47 | uint64_t sValue = s.value; |
| 48 | if (sValue <= symbolAddr) |
| 49 | continue; |
| 50 | if (sValue < closestAddr) |
| 51 | closestAddr = s.value; |
| 52 | } |
Joey Gouly | d221537 | 2014-01-13 22:28:02 +0000 | [diff] [blame] | 53 | for (const Symbol &s : normalizedFile.localSymbols) { |
| 54 | if (s.sect != symbolSectionIndex) |
| 55 | continue; |
| 56 | uint64_t sValue = s.value; |
| 57 | if (sValue <= symbolAddr) |
| 58 | continue; |
| 59 | if (sValue < closestAddr) |
| 60 | closestAddr = s.value; |
| 61 | } |
Nick Kledzik | 36baa33 | 2014-01-08 02:52:58 +0000 | [diff] [blame] | 62 | return closestAddr; |
| 63 | } |
| 64 | |
Joey Gouly | d221537 | 2014-01-13 22:28:02 +0000 | [diff] [blame] | 65 | static Atom::Scope atomScope(uint8_t scope) { |
| 66 | switch (scope) { |
| 67 | case N_EXT: |
| 68 | return Atom::scopeGlobal; |
| 69 | case N_PEXT | N_EXT: |
| 70 | return Atom::scopeLinkageUnit; |
| 71 | case 0: |
| 72 | return Atom::scopeTranslationUnit; |
| 73 | } |
| 74 | llvm_unreachable("unknown scope value!"); |
| 75 | } |
| 76 | |
Joey Gouly | 9c826c10 | 2014-01-12 22:55:49 +0000 | [diff] [blame] | 77 | static void processSymbol(const NormalizedFile &normalizedFile, MachOFile &file, |
| 78 | const Symbol &sym, bool copyRefs) { |
| 79 | // Mach-O symbol table does have size in it, so need to scan ahead |
| 80 | // to find symbol with next highest address. |
| 81 | const Section §ion = normalizedFile.sections[sym.sect - 1]; |
| 82 | uint64_t offset = sym.value - section.address; |
| 83 | uint64_t size = nextSymbolAddress(normalizedFile, sym) - sym.value; |
| 84 | ArrayRef<uint8_t> atomContent = section.content.slice(offset, size); |
Joey Gouly | d221537 | 2014-01-13 22:28:02 +0000 | [diff] [blame] | 85 | file.addDefinedAtom(sym.name, atomContent, atomScope(sym.scope), copyRefs); |
Joey Gouly | 9c826c10 | 2014-01-12 22:55:49 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 88 | |
| 89 | static void processUndefindeSymbol(MachOFile &file, const Symbol &sym, |
| 90 | bool copyRefs) { |
| 91 | // Undefinded symbols with n_value!=0 are actually tentative definitions. |
| 92 | if (sym.value == Hex64(0)) { |
| 93 | file.addUndefinedAtom(sym.name, copyRefs); |
| 94 | } else { |
| 95 | file.addTentativeDefAtom(sym.name, atomScope(sym.scope), sym.value, |
| 96 | DefinedAtom::Alignment(sym.desc >> 8), copyRefs); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 101 | static ErrorOr<std::unique_ptr<lld::File>> |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 102 | normalizedObjectToAtoms(const NormalizedFile &normalizedFile, StringRef path, |
| 103 | bool copyRefs) { |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 104 | std::unique_ptr<MachOFile> file(new MachOFile(path)); |
| 105 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 106 | for (const Symbol &sym : normalizedFile.globalSymbols) { |
Joey Gouly | 9c826c10 | 2014-01-12 22:55:49 +0000 | [diff] [blame] | 107 | processSymbol(normalizedFile, *file, sym, copyRefs); |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Joey Gouly | d221537 | 2014-01-13 22:28:02 +0000 | [diff] [blame] | 110 | for (const Symbol &sym : normalizedFile.localSymbols) { |
| 111 | processSymbol(normalizedFile, *file, sym, copyRefs); |
| 112 | } |
| 113 | |
Joey Gouly | cf46680 | 2014-02-02 19:34:55 +0000 | [diff] [blame] | 114 | for (auto &sym : normalizedFile.undefinedSymbols) { |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 115 | processUndefindeSymbol(*file, sym, copyRefs); |
Joey Gouly | cf46680 | 2014-02-02 19:34:55 +0000 | [diff] [blame] | 116 | } |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 117 | |
| 118 | return std::unique_ptr<File>(std::move(file)); |
| 119 | } |
| 120 | |
| 121 | ErrorOr<std::unique_ptr<lld::File>> |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 122 | normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path, |
| 123 | bool copyRefs) { |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 124 | switch (normalizedFile.fileType) { |
| 125 | case MH_OBJECT: |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 126 | return normalizedObjectToAtoms(normalizedFile, path, copyRefs); |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 127 | default: |
| 128 | llvm_unreachable("unhandled MachO file type!"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace normalized |
| 133 | } // namespace mach_o |
| 134 | } // namespace lld |