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

[llvm-objdump/machO] Incorrect dumping of relative method offsets #104442

Open
alx32 opened this issue Aug 15, 2024 · 1 comment
Open

[llvm-objdump/machO] Incorrect dumping of relative method offsets #104442

alx32 opened this issue Aug 15, 2024 · 1 comment

Comments

@alx32
Copy link
Contributor

alx32 commented Aug 15, 2024

llvm-objdump cannot dump method names (and other things) for MachO binaries built with relative method lists, if the MachO binary has a non-zero base address.

Repro:

#!/bin/bash

# Set up directories
INPUT_DIR=~/llvm_project/lld/test/MachO
OUTPUT_DIR=/tmp/rel_method_list_repro
SDK_DIR=~/llvm_project/lld/test/MachO/Inputs/MacOSX.sdk
TOOLCHAIN_DIR=~/llvm_project/build/Debug/bin

# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"

# Split input file into multiple files
$TOOLCHAIN_DIR/split-file "$INPUT_DIR/objc-category-merging-complete-test.s" "$OUTPUT_DIR"

# Compile a64_file1.s to a64_file1.o
$TOOLCHAIN_DIR/llvm-mc -filetype=obj -triple=arm64-apple-macos -o "$OUTPUT_DIR/a64_file1.o" "$OUTPUT_DIR/a64_file1.s"

# Link a64_file1.o to a64_file1.dylib
$TOOLCHAIN_DIR/ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -syslibroot "$SDK_DIR" -lSystem -fatal_warnings -objc_relative_method_lists -arch arm64 "$OUTPUT_DIR/a64_file1.o" -o "$OUTPUT_DIR/a64_file1.dylib" -dylib

# Compile a64_file2.s to a64_file2.o
$TOOLCHAIN_DIR/llvm-mc -filetype=obj -triple=arm64-apple-macos -o "$OUTPUT_DIR/a64_file2.o" "$OUTPUT_DIR/a64_file2.s"

# Link a64_file1.dylib and a64_file2.o to a64_file2_merge.exe with category merging
$TOOLCHAIN_DIR/ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -syslibroot "$SDK_DIR" -lSystem -fatal_warnings -objc_relative_method_lists -arch arm64 -o "$OUTPUT_DIR/a64_file2_merge.exe" -objc_category_merging "$OUTPUT_DIR/a64_file1.dylib" "$OUTPUT_DIR/a64_file2.o"

# Dump the mach-o section which will show invalid names
$TOOLCHAIN_DIR/llvm-objdump --objc-meta-data --macho "$OUTPUT_DIR/a64_file2_merge.exe" | grep name

The output will show that the method names are missing:

              name 0x10000079b Category02|Category03
                      name 0x7950 (0x8248)
                      name 0x794c (0x8250)
                      name 0x7958 (0x8268)

Using objdump from XCode we see what the issue might be:

    instanceMethods 0x100000868 __OBJC_$_CATEGORY_INSTANCE_METHODS_MyBaseClass_$_Category03
        entsize 12 (relative)
        count   2
        name    0x7a50 (0x1000082c0 extends past end of file)
        types   0xfffffef7 (0x10000076b extends past end of file)
        imp     0xfffffe98 (0x100000710 extends past end of file)
        name    0x7a4c (0x1000082c8 extends past end of file)
        types   0xfffffeeb (0x10000076b extends past end of file)
        imp     0xfffffe90 (0x100000714 extends past end of file)

So looks like objdump is trying to read from offset 0x10000xxxx and failing - because it's failing to adjust for the 0x100000000 base offset.

@llvmbot
Copy link
Collaborator

llvmbot commented Aug 15, 2024

@llvm/issue-subscribers-tools-llvm-objdump

Author: None (alx32)

`llvm-objdump` cannot dump method names (and other things) for MachO binaries built with relative method lists, if the MachO binary has a non-zero base address.

Repro:

#!/bin/bash

# Set up directories
INPUT_DIR=~/llvm_project/lld/test/MachO
OUTPUT_DIR=/tmp/rel_method_list_repro
SDK_DIR=~/llvm_project/lld/test/MachO/Inputs/MacOSX.sdk
TOOLCHAIN_DIR=~/llvm_project/build/Debug/bin

# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"

# Split input file into multiple files
$TOOLCHAIN_DIR/split-file "$INPUT_DIR/objc-category-merging-complete-test.s" "$OUTPUT_DIR"

# Compile a64_file1.s to a64_file1.o
$TOOLCHAIN_DIR/llvm-mc -filetype=obj -triple=arm64-apple-macos -o "$OUTPUT_DIR/a64_file1.o" "$OUTPUT_DIR/a64_file1.s"

# Link a64_file1.o to a64_file1.dylib
$TOOLCHAIN_DIR/ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -syslibroot "$SDK_DIR" -lSystem -fatal_warnings -objc_relative_method_lists -arch arm64 "$OUTPUT_DIR/a64_file1.o" -o "$OUTPUT_DIR/a64_file1.dylib" -dylib

# Compile a64_file2.s to a64_file2.o
$TOOLCHAIN_DIR/llvm-mc -filetype=obj -triple=arm64-apple-macos -o "$OUTPUT_DIR/a64_file2.o" "$OUTPUT_DIR/a64_file2.s"

# Link a64_file1.dylib and a64_file2.o to a64_file2_merge.exe with category merging
$TOOLCHAIN_DIR/ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -syslibroot "$SDK_DIR" -lSystem -fatal_warnings -objc_relative_method_lists -arch arm64 -o "$OUTPUT_DIR/a64_file2_merge.exe" -objc_category_merging "$OUTPUT_DIR/a64_file1.dylib" "$OUTPUT_DIR/a64_file2.o"

# Dump the mach-o section which will show invalid names
$TOOLCHAIN_DIR/llvm-objdump --objc-meta-data --macho "$OUTPUT_DIR/a64_file2_merge.exe" | grep name

The output will show that the method names are missing:

              name 0x10000079b Category02|Category03
                      name 0x7950 (0x8248)
                      name 0x794c (0x8250)
                      name 0x7958 (0x8268)

Using objdump from XCode we see what the issue might be:

    instanceMethods 0x100000868 __OBJC_$_CATEGORY_INSTANCE_METHODS_MyBaseClass_$_Category03
        entsize 12 (relative)
        count   2
        name    0x7a50 (0x1000082c0 extends past end of file)
        types   0xfffffef7 (0x10000076b extends past end of file)
        imp     0xfffffe98 (0x100000710 extends past end of file)
        name    0x7a4c (0x1000082c8 extends past end of file)
        types   0xfffffeeb (0x10000076b extends past end of file)
        imp     0xfffffe90 (0x100000714 extends past end of file)

So looks like objdump is trying to read from offset 0x10000xxxx and failing - because it's failing to adjust for the 0x100000000 base offset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants