Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3712] Mach-O files use non-aligned pointers #46297

Closed
swift-ci opened this issue Jan 24, 2017 · 4 comments
Closed

[SR-3712] Mach-O files use non-aligned pointers #46297

swift-ci opened this issue Jan 24, 2017 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself IRGen LLVM IR generation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3712
Radar None
Original Reporter mpetrov (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1)
ld64-274.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, IRGen
Assignee mpetrov (JIRA)
Priority Medium

md5: 8a872b674041ac969b54d108104bff60

Issue Description:

Generated Mach-O binaries use non word-aligned references that can break some Mach-O parsing libraries.

For example, the pointer for a Swift class' class_ro_t* is at 0x1000042d9, when in practice it starts at 0x1000042d8. This may just be a linker bug, but I've only seen this happen with Swift classes (and not with ObjC).

Repro case is below:

~> cat hello.swift
class Foo {}
class Bar {}
class Baz {}

~> swiftc hello.swift -v -Xlinker -v
Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file hello.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name hello -o /var/folders/r7/q5y959494nl8rg1yy6th6gd40045kr/T/hello-522055.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld /var/folders/r7/q5y959494nl8rg1yy6th6gd40045kr/T/hello-522055.o -force_load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -framework CoreFoundation -v -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -lobjc -lSystem -arch x86_64 -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -rpath /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -macosx_version_min 10.9.0 -no_objc_category_merging -o hello
@(#)PROGRAM:ld PROJECT:ld64-274.1
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/lib
Framework search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/

~> otool hello -o | grep "Swift class"
data 0x1000042d9 (struct class_ro_t *) Swift class
data 0x100004369 (struct class_ro_t *) Swift class
data 0x1000043f9 (struct class_ro_t *) Swift class

@belkadan
Copy link
Contributor

This is correct and necessary behavior for Swift class metadata records, which have information located at negative offsets. That said I'm a little concerned about not respecting alignment within the class_ro_t.

@belkadan
Copy link
Contributor

@rjmccall, @jckarter, thoughts?

@jckarter
Copy link
Member

The low bit of class_ro_t is the "is Swift, using an app-packaged runtime" bit. The rodata pointer itself is still aligned, but the alignment bits (and on 64-bit platforms, unused address space high bits) are reserved to the ObjC runtime.

@rjmccall
Copy link
Member

Right. The pointer is not "unaligned". It's a pointer to a correctly-aligned ro_t, and a low bit is being used to encode extra information in that pointer. Those Mach-O parsing libraries should just be fixed to ignore sub-word offsets.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself IRGen LLVM IR generation
Projects
None yet
Development

No branches or pull requests

4 participants