From 3f564a52d074adc5b94c08968c59f70ea8d4fb70 Mon Sep 17 00:00:00 2001 From: Pete Cooper Date: Thu, 24 Mar 2016 00:36:37 +0000 Subject: [PATCH] Parsed alignment should be a power of 2. The .o path always makes sure to store a power of 2 value in the Section alignment. However, the YAML code didn't verify this. Added verification and updated all the tests which had a 3 but meant to have 2^3. llvm-svn: 264228 --- lld/lib/ReaderWriter/MachO/MachONormalizedFile.h | 5 ++++- .../ReaderWriter/MachO/MachONormalizedFileYAML.cpp | 25 +++++++++++++++++++++- .../mach-o/arm64-relocs-errors-delta64-offset.yaml | 2 +- lld/test/mach-o/dso_handle.yaml | 2 +- lld/test/mach-o/hello-world-x86_64.yaml | 4 ++-- lld/test/mach-o/interposing-section.yaml | 2 +- lld/test/mach-o/mh_bundle_header.yaml | 2 +- lld/test/mach-o/mh_dylib_header.yaml | 2 +- lld/test/mach-o/objc_export_list.yaml | 2 +- lld/test/mach-o/parse-cfstring32.yaml | 2 +- lld/test/mach-o/parse-compact-unwind64.yaml | 2 +- lld/test/mach-o/parse-data.yaml | 4 ++-- lld/test/mach-o/parse-eh-frame.yaml | 2 +- lld/test/mach-o/unwind-info-simple-arm64.yaml | 2 +- 14 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h b/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h index 400a30b..7934a6c 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h @@ -105,6 +105,9 @@ typedef std::vector IndirectSymbols; /// A typedef so that YAML I/O can encode/decode section attributes. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionAttr) +/// A typedef so that YAML I/O can encode/decode section alignment. +LLVM_YAML_STRONG_TYPEDEF(uint16_t, SectionAlignment) + /// Mach-O has a 32-bit and 64-bit section record. This normalized form /// can support either kind. struct Section { @@ -115,7 +118,7 @@ struct Section { StringRef sectionName; SectionType type; SectionAttr attributes; - uint16_t alignment; + SectionAlignment alignment; Hex64 address; ArrayRef content; Relocations relocations; diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp index 5ae9204..aa3b042 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp @@ -237,6 +237,29 @@ struct ScalarBitSetTraits { } }; +/// This is a custom formatter for SectionAlignment. Values are +/// the power to raise by, ie, the n in 2^n. +template <> struct ScalarTraits { + static void output(const SectionAlignment &value, void *ctxt, + raw_ostream &out) { + out << llvm::format("%d", (uint32_t)value); + } + + static StringRef input(StringRef scalar, void *ctxt, + SectionAlignment &value) { + uint32_t alignment; + if (scalar.getAsInteger(0, alignment)) { + return "malformed alignment value"; + } + if (!llvm::isPowerOf2_32(alignment)) + return "alignment must be a power of 2"; + value = alignment; + return StringRef(); // returning empty string means success + } + + static bool mustQuote(StringRef) { return false; } +}; + template <> struct ScalarEnumerationTraits { static void enumeration(IO &io, NListType &value) { @@ -276,7 +299,7 @@ struct MappingTraits
{ io.mapRequired("section", sect.sectionName); io.mapRequired("type", sect.type); io.mapOptional("attributes", sect.attributes); - io.mapOptional("alignment", sect.alignment, (uint16_t)1); + io.mapOptional("alignment", sect.alignment, (SectionAlignment)1); io.mapRequired("address", sect.address); if (isZeroFillSection(sect.type)) { // S_ZEROFILL sections use "size:" instead of "content:" diff --git a/lld/test/mach-o/arm64-relocs-errors-delta64-offset.yaml b/lld/test/mach-o/arm64-relocs-errors-delta64-offset.yaml index 09c5961..08e41bc 100644 --- a/lld/test/mach-o/arm64-relocs-errors-delta64-offset.yaml +++ b/lld/test/mach-o/arm64-relocs-errors-delta64-offset.yaml @@ -24,7 +24,7 @@ sections: section: __data type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x000000000001C348 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, diff --git a/lld/test/mach-o/dso_handle.yaml b/lld/test/mach-o/dso_handle.yaml index d39d2c2..400e2c8 100644 --- a/lld/test/mach-o/dso_handle.yaml +++ b/lld/test/mach-o/dso_handle.yaml @@ -28,7 +28,7 @@ sections: section: __data type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000008 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] relocations: diff --git a/lld/test/mach-o/hello-world-x86_64.yaml b/lld/test/mach-o/hello-world-x86_64.yaml index 082ed20..8803c44 100644 --- a/lld/test/mach-o/hello-world-x86_64.yaml +++ b/lld/test/mach-o/hello-world-x86_64.yaml @@ -54,7 +54,7 @@ sections: section: __compact_unwind type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000028 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, @@ -71,7 +71,7 @@ sections: section: __eh_frame type: S_COALESCED attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000048 content: [ 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7A, 0x52, 0x00, 0x01, 0x78, 0x10, 0x01, diff --git a/lld/test/mach-o/interposing-section.yaml b/lld/test/mach-o/interposing-section.yaml index 76e3390..4f6bafc 100644 --- a/lld/test/mach-o/interposing-section.yaml +++ b/lld/test/mach-o/interposing-section.yaml @@ -31,7 +31,7 @@ sections: section: __interpose type: S_INTERPOSING attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000010 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] diff --git a/lld/test/mach-o/mh_bundle_header.yaml b/lld/test/mach-o/mh_bundle_header.yaml index 87b35e7..e440141 100644 --- a/lld/test/mach-o/mh_bundle_header.yaml +++ b/lld/test/mach-o/mh_bundle_header.yaml @@ -19,7 +19,7 @@ sections: section: __data type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000008 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] relocations: diff --git a/lld/test/mach-o/mh_dylib_header.yaml b/lld/test/mach-o/mh_dylib_header.yaml index 07429b3..96b67aa 100644 --- a/lld/test/mach-o/mh_dylib_header.yaml +++ b/lld/test/mach-o/mh_dylib_header.yaml @@ -19,7 +19,7 @@ sections: section: __data type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000008 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] relocations: diff --git a/lld/test/mach-o/objc_export_list.yaml b/lld/test/mach-o/objc_export_list.yaml index 9eb0af7..e67a4c7 100644 --- a/lld/test/mach-o/objc_export_list.yaml +++ b/lld/test/mach-o/objc_export_list.yaml @@ -15,7 +15,7 @@ sections: section: __objc_data type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000000 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, diff --git a/lld/test/mach-o/parse-cfstring32.yaml b/lld/test/mach-o/parse-cfstring32.yaml index 657e733..8e746f2 100644 --- a/lld/test/mach-o/parse-cfstring32.yaml +++ b/lld/test/mach-o/parse-cfstring32.yaml @@ -21,7 +21,7 @@ sections: section: __cfstring type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000010 content: [ 0x00, 0x00, 0x00, 0x00, 0xC8, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, diff --git a/lld/test/mach-o/parse-compact-unwind64.yaml b/lld/test/mach-o/parse-compact-unwind64.yaml index b61961a..af89674 100644 --- a/lld/test/mach-o/parse-compact-unwind64.yaml +++ b/lld/test/mach-o/parse-compact-unwind64.yaml @@ -23,7 +23,7 @@ sections: section: __compact_unwind type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000020 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, diff --git a/lld/test/mach-o/parse-data.yaml b/lld/test/mach-o/parse-data.yaml index 61d7729..3b422e0 100644 --- a/lld/test/mach-o/parse-data.yaml +++ b/lld/test/mach-o/parse-data.yaml @@ -22,7 +22,7 @@ sections: section: __data type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000000 content: [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x11, 0x12, 0x13, 0x14, 0x21, 0x22, 0x23, 0x24, @@ -31,7 +31,7 @@ sections: section: __custom type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000018 content: [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 ] - segment: __DATA diff --git a/lld/test/mach-o/parse-eh-frame.yaml b/lld/test/mach-o/parse-eh-frame.yaml index 6932480..6453474 100644 --- a/lld/test/mach-o/parse-eh-frame.yaml +++ b/lld/test/mach-o/parse-eh-frame.yaml @@ -22,7 +22,7 @@ sections: section: __eh_frame type: S_COALESCED attributes: [ ] - alignment: 3 + alignment: 8 address: 0x0000000000000058 content: [ 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7A, 0x52, 0x00, 0x01, 0x78, 0x10, 0x01, diff --git a/lld/test/mach-o/unwind-info-simple-arm64.yaml b/lld/test/mach-o/unwind-info-simple-arm64.yaml index 8e87230..2ef6dda 100644 --- a/lld/test/mach-o/unwind-info-simple-arm64.yaml +++ b/lld/test/mach-o/unwind-info-simple-arm64.yaml @@ -125,7 +125,7 @@ sections: section: __compact_unwind type: S_REGULAR attributes: [ ] - alignment: 3 + alignment: 8 address: 0x00000000000000A8 content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, -- 2.7.4