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 | } |
| 53 | return closestAddr; |
| 54 | } |
| 55 | |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 56 | static ErrorOr<std::unique_ptr<lld::File>> |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame^] | 57 | normalizedObjectToAtoms(const NormalizedFile &normalizedFile, StringRef path, |
| 58 | bool copyRefs) { |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 59 | std::unique_ptr<MachOFile> file(new MachOFile(path)); |
| 60 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame^] | 61 | for (const Symbol &sym : normalizedFile.globalSymbols) { |
Nick Kledzik | 36baa33 | 2014-01-08 02:52:58 +0000 | [diff] [blame] | 62 | // Mach-O symbol table does have size in it, so need to scan ahead |
| 63 | // to find symbol with next highest address. |
| 64 | const Section §ion = normalizedFile.sections[sym.sect - 1]; |
| 65 | uint64_t offset = sym.value - section.address; |
| 66 | uint64_t size = nextSymbolAddress(normalizedFile, sym) - sym.value; |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame^] | 67 | ArrayRef<uint8_t> atomContent = section.content.slice(offset, size); |
| 68 | file->addDefinedAtom(sym.name, atomContent, copyRefs); |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | assert(normalizedFile.localSymbols.empty() && |
| 72 | "local symbols not supported yet!"); |
| 73 | assert(normalizedFile.undefinedSymbols.empty() && |
| 74 | "undefined symbols not supported yet!"); |
| 75 | |
| 76 | return std::unique_ptr<File>(std::move(file)); |
| 77 | } |
| 78 | |
| 79 | ErrorOr<std::unique_ptr<lld::File>> |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame^] | 80 | normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path, |
| 81 | bool copyRefs) { |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 82 | switch (normalizedFile.fileType) { |
| 83 | case MH_OBJECT: |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame^] | 84 | return normalizedObjectToAtoms(normalizedFile, path, copyRefs); |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 85 | default: |
| 86 | llvm_unreachable("unhandled MachO file type!"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } // namespace normalized |
| 91 | } // namespace mach_o |
| 92 | } // namespace lld |