[lld] r212093 - [mach-o] add representation for LC_ID_DYLIB to MachONormalizedFile
Tim Northover
tnorthover at apple.com
Tue Jul 1 01:15:42 PDT 2014
Author: tnorthover
Date: Tue Jul 1 03:15:41 2014
New Revision: 212093
URL: http://llvm.org/viewvc/llvm-project?rev=212093&view=rev
Log:
[mach-o] add representation for LC_ID_DYLIB to MachONormalizedFile
It still needs to be tied into BinaryReader, but this allows reasonably
sensible creation of SharedLibrary atoms on MachO.
Added:
lld/trunk/test/mach-o/Inputs/
lld/trunk/test/mach-o/Inputs/use-dylib-install-names.yaml
lld/trunk/test/mach-o/dylib-install-names.yaml
Modified:
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
lld/trunk/test/mach-o/lit.local.cfg
lld/trunk/test/mach-o/use-simple-dylib.yaml
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp?rev=212093&r1=212092&r2=212093&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp Tue Jul 1 03:15:41 2014
@@ -386,6 +386,15 @@ readBinary(std::unique_ptr<MemoryBuffer>
}
}
}
+ if (cmd == LC_ID_DYLIB) {
+ const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
+ dylib_command tempDL;
+ if (swap) {
+ tempDL = *dl; swapStruct(tempDL); dl = &tempDL;
+ }
+
+ f->installName = lc + dl->dylib.name;
+ }
return false;
});
if (ec)
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp?rev=212093&r1=212092&r2=212093&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp Tue Jul 1 03:15:41 2014
@@ -324,6 +324,12 @@ uint32_t MachOFileLayout::loadCommandsSi
size += segCommandSize;
++count;
+ // If creating a dylib, add LC_ID_DYLIB.
+ if (_file.fileType == llvm::MachO::MH_DYLIB) {
+ size += sizeof(dylib_command) + pointerAlign(_file.installName.size() + 1);
+ ++count;
+ }
+
// Add LC_DYLD_INFO
size += sizeof(dyld_info_command);
++count;
@@ -621,6 +627,24 @@ std::error_code MachOFileLayout::writeLo
else
ec = writeSegmentLoadCommands<MachO32Trait>(lc);
+ // Add LC_ID_DYLIB command for dynamic libraries.
+ if (_file.fileType == llvm::MachO::MH_DYLIB) {
+ dylib_command *dc = reinterpret_cast<dylib_command*>(lc);
+ StringRef path = _file.installName;
+ uint32_t size = sizeof(dylib_command) + pointerAlign(path.size() + 1);
+ dc->cmd = LC_ID_DYLIB;
+ dc->cmdsize = size;
+ dc->dylib.name = sizeof(dylib_command); // offset
+ dc->dylib.timestamp = 0; // FIXME
+ dc->dylib.current_version = 0; // FIXME
+ dc->dylib.compatibility_version = 0; // FIXME
+ if (_swap)
+ swapStruct(*dc);
+ memcpy(lc + sizeof(dylib_command), path.begin(), path.size());
+ lc[sizeof(dylib_command) + path.size()] = '\0';
+ lc += size;
+ }
+
// Add LC_DYLD_INFO_ONLY.
dyld_info_command* di = reinterpret_cast<dyld_info_command*>(lc);
di->cmd = LC_DYLD_INFO_ONLY;
@@ -720,7 +744,6 @@ std::error_code MachOFileLayout::writeLo
lc[sizeof(dylib_command)+dep.path.size()] = '\0';
lc += size;
}
-
}
return ec;
}
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=212093&r1=212092&r2=212093&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Tue Jul 1 03:15:41 2014
@@ -934,6 +934,7 @@ normalizedFromAtoms(const lld::File &ato
f->arch = context.arch();
f->fileType = context.outputMachOType();
f->flags = util.fileFlags();
+ f->installName = context.installName();
util.copySegmentInfo(normFile);
util.copySections(normFile);
util.addDependentDylibs(atomFile, normFile);
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp?rev=212093&r1=212092&r2=212093&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp Tue Jul 1 03:15:41 2014
@@ -605,7 +605,8 @@ normalizedObjectToAtoms(const Normalized
ErrorOr<std::unique_ptr<lld::File>>
normalizedDylibToAtoms(const NormalizedFile &normalizedFile, StringRef path,
bool copyRefs) {
- std::unique_ptr<MachODylibFile> file(new MachODylibFile(path));
+ std::unique_ptr<MachODylibFile> file(
+ new MachODylibFile(normalizedFile.installName));
for (auto &sym : normalizedFile.globalSymbols) {
assert((sym.scope & N_EXT) && "only expect external symbols here");
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp?rev=212093&r1=212092&r2=212093&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp Tue Jul 1 03:15:41 2014
@@ -508,7 +508,6 @@ struct MappingTraits<DependentDylib> {
}
};
-
template <>
struct ScalarEnumerationTraits<RebaseType> {
static void enumeration(IO &io, RebaseType &value) {
Added: lld/trunk/test/mach-o/Inputs/use-dylib-install-names.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/Inputs/use-dylib-install-names.yaml?rev=212093&view=auto
==============================================================================
--- lld/trunk/test/mach-o/Inputs/use-dylib-install-names.yaml (added)
+++ lld/trunk/test/mach-o/Inputs/use-dylib-install-names.yaml Tue Jul 1 03:15:41 2014
@@ -0,0 +1,28 @@
+--- !mach-o
+arch: x86_64
+file-type: MH_OBJECT
+flags: [ ]
+has-UUID: false
+OS: unknown
+sections:
+ - segment: __TEXT
+ section: __text
+ type: S_REGULAR
+ attributes: [ S_ATTR_PURE_INSTRUCTIONS ]
+ address: 0x0000000000000000
+ content: [ 0x55, 0x48, 0x89, 0xE5, 0xE8, 0x00, 0x00, 0x00,
+ 0x00, 0xE8, 0x00, 0x00, 0x00, 0x00, 0xE8, 0x00,
+ 0x00, 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00, 0x00,
+ 0xE8, 0x00, 0x00, 0x00, 0x00, 0x5D, 0xE9, 0x00,
+ 0x00, 0x00, 0x00 ]
+global-symbols:
+ - name: _foo
+ type: N_SECT
+ scope: [ N_EXT ]
+ sect: 1
+ value: 0x0000000000000000
+undefined-symbols:
+ - name: _myGlobal
+ type: N_UNDF
+ scope: [ N_EXT ]
+ value: 0x0000000000000000
Added: lld/trunk/test/mach-o/dylib-install-names.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/dylib-install-names.yaml?rev=212093&view=auto
==============================================================================
--- lld/trunk/test/mach-o/dylib-install-names.yaml (added)
+++ lld/trunk/test/mach-o/dylib-install-names.yaml Tue Jul 1 03:15:41 2014
@@ -0,0 +1,41 @@
+# RUN: lld -flavor darwin -arch x86_64 -install_name libwibble.dylib -dylib %s -o %t.dylib
+# RUN: macho-dump %t.dylib | FileCheck %s --check-prefix=CHECK-BINARY-WRITE
+# RUN: lld -flavor darwin -arch x86_64 %p/Inputs/use-dylib-install-names.yaml %t.dylib -r -print_atoms | FileCheck %s --check-prefix=CHECK-BINARY-READ
+
+--- !mach-o
+arch: x86_64
+file-type: MH_OBJECT
+flags: [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+has-UUID: false
+OS: unknown
+sections:
+ - segment: __TEXT
+ section: __text
+ type: S_REGULAR
+ attributes: [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ]
+ alignment: 4
+ address: 0x0000000000000000
+ content: [ 0xCC, 0xC3, 0x90, 0xC3, 0x90, 0x90, 0xC3, 0x90,
+ 0x90, 0x90, 0xC3, 0x90, 0x90, 0x90, 0x90, 0xC3,
+ 0x31, 0xC0, 0xC3 ]
+local-symbols:
+ - name: _myStatic
+ type: N_SECT
+ sect: 1
+ value: 0x000000000000000B
+global-symbols:
+ - name: _myGlobal
+ type: N_SECT
+ scope: [ N_EXT ]
+ sect: 1
+ value: 0x0000000000000001
+...
+
+
+# CHECK-BINARY-WRITE: (('command', 13)
+# CHECK-BINARY-WRITE-NEXT: ('size', 40)
+# CHECK-BINARY-WRITE-NEXT: ('install_name', 'libwibble.dylib')
+
+# CHECK-BINARY-READ: shared-library-atoms:
+# CHECK-BINARY-READ: - name: _myGlobal
+# CHECK-BINARY-READ: load-name: libwibble.dylib
Modified: lld/trunk/test/mach-o/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/lit.local.cfg?rev=212093&r1=212092&r2=212093&view=diff
==============================================================================
--- lld/trunk/test/mach-o/lit.local.cfg (original)
+++ lld/trunk/test/mach-o/lit.local.cfg Tue Jul 1 03:15:41 2014
@@ -1,3 +1,4 @@
# mach-o test cases encode input files in yaml and use .yaml extension
config.suffixes = ['.yaml']
+config.excludes = ['Inputs']
Modified: lld/trunk/test/mach-o/use-simple-dylib.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/use-simple-dylib.yaml?rev=212093&r1=212092&r2=212093&view=diff
==============================================================================
--- lld/trunk/test/mach-o/use-simple-dylib.yaml (original)
+++ lld/trunk/test/mach-o/use-simple-dylib.yaml Tue Jul 1 03:15:41 2014
@@ -1,5 +1,5 @@
# RUN: lld -flavor darwin -arch x86_64 -print_atoms -r %s | FileCheck %s
-# lld -flavor darwin -arch x86_64 -print_atoms -r %s %p/Inputs/simple-dylib.yaml -o - | FileCheck %s
+
--- !mach-o
arch: x86_64
@@ -110,6 +110,8 @@ global-symbols:
sect: 1
desc: [ N_SYMBOL_RESOLVER ]
value: 0x0000000000000010
+
+install-name: libspecial.dylib
...
@@ -118,12 +120,12 @@ global-symbols:
# CHECK: - name: _myVariablePreviouslyKnownAsPrivateExtern
# CHECK: shared-library-atoms:
# CHECK: - name: _myHidden
-# CHECK: load-name: {{.*}}use-simple-dylib.yaml
+# CHECK: load-name: libspecial.dylib
# CHECK: - name: _myGlobal
-# CHECK: load-name: {{.*}}use-simple-dylib.yaml
+# CHECK: load-name: libspecial.dylib
# CHECK: - name: _myHiddenWeak
-# CHECK: load-name: {{.*}}use-simple-dylib.yaml
+# CHECK: load-name: libspecial.dylib
# CHECK: - name: _myGlobalWeak
-# CHECK: load-name: {{.*}}use-simple-dylib.yaml
+# CHECK: load-name: libspecial.dylib
# CHECK: - name: _myResolver
-# CHECK: load-name: {{.*}}use-simple-dylib.yaml
+# CHECK: load-name: libspecial.dylib
More information about the llvm-commits
mailing list