From 63adb08c2ca3b8a4c119643e225fe45c856571b9 Mon Sep 17 00:00:00 2001 From: Nick Kledzik Date: Tue, 18 Nov 2014 00:30:25 +0000 Subject: [PATCH] [mach-o] extract atom alignment information from mach-o files llvm-svn: 222201 --- lld/lib/ReaderWriter/MachO/Atoms.h | 17 ++++++---- lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp | 2 +- lld/lib/ReaderWriter/MachO/File.h | 13 ++++++-- lld/test/mach-o/align_text.yaml | 45 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 lld/test/mach-o/align_text.yaml diff --git a/lld/lib/ReaderWriter/MachO/Atoms.h b/lld/lib/ReaderWriter/MachO/Atoms.h index 24318a0..8d60c1a 100644 --- a/lld/lib/ReaderWriter/MachO/Atoms.h +++ b/lld/lib/ReaderWriter/MachO/Atoms.h @@ -18,16 +18,16 @@ class MachODefinedAtom : public SimpleDefinedAtom { public: MachODefinedAtom(const File &f, const StringRef name, Scope scope, ContentType type, Merge merge, bool thumb, bool noDeadStrip, - const ArrayRef content) + const ArrayRef content, Alignment align) : SimpleDefinedAtom(f), _name(name), _content(content), - _contentType(type), _scope(scope), _merge(merge), _thumb(thumb), - _noDeadStrip(noDeadStrip) {} + _align(align), _contentType(type), _scope(scope), _merge(merge), + _thumb(thumb), _noDeadStrip(noDeadStrip) {} // Constructor for zero-fill content MachODefinedAtom(const File &f, const StringRef name, Scope scope, - uint64_t size, bool noDeadStrip) + uint64_t size, bool noDeadStrip, Alignment align) : SimpleDefinedAtom(f), _name(name), - _content(ArrayRef(nullptr, size)), + _content(ArrayRef(nullptr, size)), _align(align), _contentType(DefinedAtom::typeZeroFill), _scope(scope), _merge(mergeNo), _thumb(false), _noDeadStrip(noDeadStrip) {} @@ -36,6 +36,8 @@ public: ContentType contentType() const override { return _contentType; } + Alignment alignment() const override { return _align; } + StringRef name() const override { return _name; } Scope scope() const override { return _scope; } @@ -71,6 +73,7 @@ public: private: const StringRef _name; const ArrayRef _content; + const DefinedAtom::Alignment _align; const ContentType _contentType; const Scope _scope; const Merge _merge; @@ -84,9 +87,9 @@ public: Scope scope, ContentType type, Merge merge, bool thumb, bool noDeadStrip, const ArrayRef content, - StringRef sectionName) + StringRef sectionName, Alignment align) : MachODefinedAtom(f, name, scope, type, merge, thumb, noDeadStrip, - content), + content, align), _sectionName(sectionName) {} SectionChoice sectionChoice() const override { diff --git a/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp b/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp index 693a428..ba2df6c 100644 --- a/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp +++ b/lld/lib/ReaderWriter/MachO/ExecutableAtoms.hpp @@ -100,7 +100,7 @@ public: _definedAtoms._atoms.push_back(new (_alloc) MachODefinedAtom( *this, sym, DefinedAtom::scopeLinkageUnit, DefinedAtom::typeMachHeader, DefinedAtom::mergeNo, false, false, - ArrayRef())); + ArrayRef(), DefinedAtom::Alignment(12,0))); return this; } return nullptr; diff --git a/lld/lib/ReaderWriter/MachO/File.h b/lld/lib/ReaderWriter/MachO/File.h index 666bab6..831de24 100644 --- a/lld/lib/ReaderWriter/MachO/File.h +++ b/lld/lib/ReaderWriter/MachO/File.h @@ -39,9 +39,11 @@ public: name = name.copy(_allocator); content = content.copy(_allocator); } + DefinedAtom::Alignment align(inSection->alignment, + sectionOffset % (1 << inSection->alignment)); MachODefinedAtom *atom = new (_allocator) MachODefinedAtom(*this, name, scope, type, merge, - thumb, noDeadStrip, content); + thumb, noDeadStrip, content, align); addAtomForSection(inSection, atom, sectionOffset); } @@ -59,11 +61,13 @@ public: content = content.copy(_allocator); sectionName = sectionName.copy(_allocator); } + DefinedAtom::Alignment align(inSection->alignment, + sectionOffset % (1 << inSection->alignment)); MachODefinedCustomSectionAtom *atom = new (_allocator) MachODefinedCustomSectionAtom(*this, name, scope, type, merge, thumb, noDeadStrip, content, - sectionName); + sectionName, align); addAtomForSection(inSection, atom, sectionOffset); } @@ -75,8 +79,11 @@ public: // Make a copy of the atom's name and content that is owned by this file. name = name.copy(_allocator); } + DefinedAtom::Alignment align(inSection->alignment, + sectionOffset % (1 << inSection->alignment)); MachODefinedAtom *atom = - new (_allocator) MachODefinedAtom(*this, name, scope, size, noDeadStrip); + new (_allocator) MachODefinedAtom(*this, name, scope, size, noDeadStrip, + align); addAtomForSection(inSection, atom, sectionOffset); } diff --git a/lld/test/mach-o/align_text.yaml b/lld/test/mach-o/align_text.yaml new file mode 100644 index 0000000..5ddbf91 --- /dev/null +++ b/lld/test/mach-o/align_text.yaml @@ -0,0 +1,45 @@ +# RUN: lld -flavor darwin -arch x86_64 -r %s -o %t -print_atoms | FileCheck %s +# RUN: lld -flavor darwin -arch x86_64 -r %t -o %t2 -print_atoms | FileCheck %s +# +# Test that alignment info round trips through -r +# + +--- !mach-o +arch: x86_64 +file-type: MH_OBJECT +flags: [ MH_SUBSECTIONS_VIA_SYMBOLS ] +sections: + - segment: __TEXT + section: __text + type: S_REGULAR + attributes: [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ] + alignment: 4 + address: 0x0000000000000000 + content: [ 0x90, 0x90, 0x90, 0xC3, 0xC3, 0xC3 ] +local-symbols: + - name: _f1 + type: N_SECT + sect: 1 + value: 0x0000000000000003 + - name: _f2 + type: N_SECT + sect: 1 + value: 0x0000000000000004 + - name: _f3 + type: N_SECT + sect: 1 + value: 0x0000000000000005 +... + +# CHECK: defined-atoms: +# CHECK: - content: [ 90, 90, 90 ] +# CHECK: alignment: 2^4 +# CHECK: - name: _f1 +# CHECK: content: [ C3 ] +# CHECK: alignment: 3 mod 2^4 +# CHECK: - name: _f2 +# CHECK: content: [ C3 ] +# CHECK: alignment: 4 mod 2^4 +# CHECK: - name: _f3 +# CHECK: content: [ C3 ] +# CHECK: alignment: 5 mod 2^4 -- 2.7.4