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

[lld][AArch64][ELF][PAC] Support .relr.auth.dyn section #87635

Merged
merged 7 commits into from
May 16, 2024

Conversation

kovdan01
Copy link
Contributor

@kovdan01 kovdan01 commented Apr 4, 2024

@kovdan01
Copy link
Contributor Author

@MaskRay Would be glad to see your feedback on this. It would be nice to have RELR support merged since it's present in the spec and CREL is not widely adopted and accepted yet, and having some way to reduce relative relocs size is a desirable feature.

@kovdan01
Copy link
Contributor Author

@MaskRay A kind reminder regarding the PR

// .relr.auth.dyn to .rela.dyn, and the addend write is not needed.
//
// If val fits in 32 bits, we have two potential scenarios:
// * True RELR: Write the 32-bit `val`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing period

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, see e68efcf

@@ -544,7 +544,9 @@ class RelocationBaseSection : public SyntheticSection {
static bool classof(const SectionBase *d) {
return SyntheticSection::classof(d) &&
(d->type == llvm::ELF::SHT_RELA || d->type == llvm::ELF::SHT_REL ||
d->type == llvm::ELF::SHT_RELR);
d->type == llvm::ELF::SHT_RELR ||
(config->emachine == llvm::ELF::EM_AARCH64 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check d->type before config->emachine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks, see e68efcf

reloc.offset,
DynamicReloc::AddendOnlyWithTargetVA,
*reloc.sym, reloc.addend, R_ABS});
// See also AArch64::relocate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be moved to the previous one or just dropped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the previous one, see e68efcf

@kovdan01 kovdan01 requested a review from MaskRay May 14, 2024 19:42

# EMPTY-RELA: Section Headers:
# EMPTY-RELA-NEXT: Name Type Address Off Size ES Flg Lk Inf Al
# EMPTY-RELA: .rela.dyn RELA {{0*}}[[ADDR1:.*]] {{0*}}[[ADDR1]] 000000 18 A 0 0 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[[#%x,ADDR1]] and {{0*}}[[#ADDR1]] below.

Better to avoid .*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can use numeric variables here - see the following limitation https://www.llvm.org/docs/CommandGuide/FileCheck.html#filecheck-numeric-substitution-blocks

Important note: In its current implementation, an expression cannot use a numeric variable defined earlier in the same CHECK directive.

We need to use ADDR1 and ADDR2 on the same check lines where they are defined. FileCheck fails with error: numeric variable 'ADDR1' defined earlier in the same CHECK directive.

Do I miss something or we can leave this "as is" due to this FileCheck limitation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it is fine. .+ is still probably better than .* (which can match spaces)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed .* to .+, thanks for suggestion, see 8a83e05

@kovdan01 kovdan01 merged commit ca1f0d4 into llvm:main May 16, 2024
3 of 4 checks passed
@gulfemsavrun
Copy link
Contributor

We started seeing an lld relocation issue, and I bisected to this commit.

[164519/322444](64) SOLINK arm64-shared/libmagma_shared.so
FAILED: arm64-shared/libmagma_shared.so arm64-shared/lib.unstripped/libmagma_shared.so arm64-shared/libmagma_shared.so.build-id.stamp arm64-shared/libmagma_shared.ifs arm64-shared/link_stub/libmagma_shared.so 
mkdir -p arm64-shared/lib.unstripped &&  ../../prebuilt/third_party/clang/custom/bin/clang++ -shared -Wl,-soname="libmagma_shared.so" -Wl,--whole-archive arm64-shared/obj/src/graphics/lib/magma/src/libmagma/libmagma_client.a -Wl,--no-whole-archive -fcolor-diagnostics -Wl,--color-diagnostics -fcrash-diagnostics-dir=clang-crashreports -fcrash-diagnostics=all -gen-reproducer=error --sysroot=arm64-shared/gen/zircon/public/sysroot/cpp --target=aarch64-unknown-fuchsia -ffuchsia-api-level=4293918720 -march=armv8-a+simd+crc+crypto -mtune=generic -ffile-compilation-dir=. -no-canonical-prefixes -fomit-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections -Os -Wl,-O2 -flto -Wl,-mllvm,-wholeprogramdevirt-branch-funnel-threshold=0 -ffat-lto-objects -g3 -grecord-gcc-switches -gdwarf-5 -gz=zstd -fno-exceptions -fno-rtti -Wl,--icf=all -fPIC -Wl,--no-undefined -Lgen/zircon/system/ulib/c/c.aarch64 -L../../build/config/zircon/libc-dummy -o "arm64-shared/lib.unstripped/libmagma_shared.so" -Wl,--dependency-file="arm64-shared/libmagma_shared.so.d" -Wl,--Map="arm64-shared/lib.unstripped/libmagma_shared.so.map" "@arm64-shared/libmagma_shared.so.rsp"  gen/zircon/system/ulib/c/c.aarch64/libc.so && ../../prebuilt/third_party/clang/custom/bin/llvm-objcopy --strip-all "arm64-shared/lib.unstripped/libmagma_shared.so" "arm64-shared/libmagma_shared.so" && ../../prebuilt/tools/buildidtool/linux-x64/buildidtool -build-id-dir .build-id -stamp "arm64-shared/libmagma_shared.so.build-id.stamp" -entry "=arm64-shared/libmagma_shared.so" -entry ".debug=arm64-shared/lib.unstripped/libmagma_shared.so" &&  ../../prebuilt/third_party/clang/custom/bin/llvm-ifs --write-if-changed --output-ifs=arm64-shared/libmagma_shared.ifs --output-elf=arm64-shared/link_stub/libmagma_shared.so arm64-shared/lib.unstripped/libmagma_shared.so
ld.lld: error: arm64-shared/obj/src/graphics/lib/magma/src/libmagma/libmagma_client.a(magma_client.o): relocation section (index 44207) has invalid sh_info (0)
clang++: error: ld.lld command failed with exit code 1 (use -v to see invocation)

https://luci-milo.appspot.com/ui/p/fuchsia/builders/ci/clang_toolchain.ci.core.arm64-release/b8746515880088952657/overview

This is just heads up, and I'll try to come up with a small reproducer. I'm just posting early in case you have any idea.

@MaskRay
Copy link
Member

MaskRay commented May 30, 2024

We started seeing an lld relocation issue, and I bisected to this commit.

[164519/322444](64) SOLINK arm64-shared/libmagma_shared.so
FAILED: arm64-shared/libmagma_shared.so arm64-shared/lib.unstripped/libmagma_shared.so arm64-shared/libmagma_shared.so.build-id.stamp arm64-shared/libmagma_shared.ifs arm64-shared/link_stub/libmagma_shared.so 
mkdir -p arm64-shared/lib.unstripped &&  ../../prebuilt/third_party/clang/custom/bin/clang++ -shared -Wl,-soname="libmagma_shared.so" -Wl,--whole-archive arm64-shared/obj/src/graphics/lib/magma/src/libmagma/libmagma_client.a -Wl,--no-whole-archive -fcolor-diagnostics -Wl,--color-diagnostics -fcrash-diagnostics-dir=clang-crashreports -fcrash-diagnostics=all -gen-reproducer=error --sysroot=arm64-shared/gen/zircon/public/sysroot/cpp --target=aarch64-unknown-fuchsia -ffuchsia-api-level=4293918720 -march=armv8-a+simd+crc+crypto -mtune=generic -ffile-compilation-dir=. -no-canonical-prefixes -fomit-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections -Os -Wl,-O2 -flto -Wl,-mllvm,-wholeprogramdevirt-branch-funnel-threshold=0 -ffat-lto-objects -g3 -grecord-gcc-switches -gdwarf-5 -gz=zstd -fno-exceptions -fno-rtti -Wl,--icf=all -fPIC -Wl,--no-undefined -Lgen/zircon/system/ulib/c/c.aarch64 -L../../build/config/zircon/libc-dummy -o "arm64-shared/lib.unstripped/libmagma_shared.so" -Wl,--dependency-file="arm64-shared/libmagma_shared.so.d" -Wl,--Map="arm64-shared/lib.unstripped/libmagma_shared.so.map" "@arm64-shared/libmagma_shared.so.rsp"  gen/zircon/system/ulib/c/c.aarch64/libc.so && ../../prebuilt/third_party/clang/custom/bin/llvm-objcopy --strip-all "arm64-shared/lib.unstripped/libmagma_shared.so" "arm64-shared/libmagma_shared.so" && ../../prebuilt/tools/buildidtool/linux-x64/buildidtool -build-id-dir .build-id -stamp "arm64-shared/libmagma_shared.so.build-id.stamp" -entry "=arm64-shared/libmagma_shared.so" -entry ".debug=arm64-shared/lib.unstripped/libmagma_shared.so" &&  ../../prebuilt/third_party/clang/custom/bin/llvm-ifs --write-if-changed --output-ifs=arm64-shared/libmagma_shared.ifs --output-elf=arm64-shared/link_stub/libmagma_shared.so arm64-shared/lib.unstripped/libmagma_shared.so
ld.lld: error: arm64-shared/obj/src/graphics/lib/magma/src/libmagma/libmagma_client.a(magma_client.o): relocation section (index 44207) has invalid sh_info (0)
clang++: error: ld.lld command failed with exit code 1 (use -v to see invocation)

luci-milo.appspot.com/ui/p/fuchsia/builders/ci/clang_toolchain.ci.core.arm64-release/b8746515880088952657/overview

This is just heads up, and I'll try to come up with a small reproducer. I'm just posting early in case you have any idea.

@gulfemsavrun With limited information, I suspect the root cause is something else. The lld error is to disallow a relocation section with an invalid target . You can run llvm-readelf -S on the relocatable file and figure out how it is built.

@zeroomega
Copy link
Contributor

We started seeing an lld relocation issue, and I bisected to this commit.

[164519/322444](64) SOLINK arm64-shared/libmagma_shared.so
FAILED: arm64-shared/libmagma_shared.so arm64-shared/lib.unstripped/libmagma_shared.so arm64-shared/libmagma_shared.so.build-id.stamp arm64-shared/libmagma_shared.ifs arm64-shared/link_stub/libmagma_shared.so 
mkdir -p arm64-shared/lib.unstripped &&  ../../prebuilt/third_party/clang/custom/bin/clang++ -shared -Wl,-soname="libmagma_shared.so" -Wl,--whole-archive arm64-shared/obj/src/graphics/lib/magma/src/libmagma/libmagma_client.a -Wl,--no-whole-archive -fcolor-diagnostics -Wl,--color-diagnostics -fcrash-diagnostics-dir=clang-crashreports -fcrash-diagnostics=all -gen-reproducer=error --sysroot=arm64-shared/gen/zircon/public/sysroot/cpp --target=aarch64-unknown-fuchsia -ffuchsia-api-level=4293918720 -march=armv8-a+simd+crc+crypto -mtune=generic -ffile-compilation-dir=. -no-canonical-prefixes -fomit-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections -Os -Wl,-O2 -flto -Wl,-mllvm,-wholeprogramdevirt-branch-funnel-threshold=0 -ffat-lto-objects -g3 -grecord-gcc-switches -gdwarf-5 -gz=zstd -fno-exceptions -fno-rtti -Wl,--icf=all -fPIC -Wl,--no-undefined -Lgen/zircon/system/ulib/c/c.aarch64 -L../../build/config/zircon/libc-dummy -o "arm64-shared/lib.unstripped/libmagma_shared.so" -Wl,--dependency-file="arm64-shared/libmagma_shared.so.d" -Wl,--Map="arm64-shared/lib.unstripped/libmagma_shared.so.map" "@arm64-shared/libmagma_shared.so.rsp"  gen/zircon/system/ulib/c/c.aarch64/libc.so && ../../prebuilt/third_party/clang/custom/bin/llvm-objcopy --strip-all "arm64-shared/lib.unstripped/libmagma_shared.so" "arm64-shared/libmagma_shared.so" && ../../prebuilt/tools/buildidtool/linux-x64/buildidtool -build-id-dir .build-id -stamp "arm64-shared/libmagma_shared.so.build-id.stamp" -entry "=arm64-shared/libmagma_shared.so" -entry ".debug=arm64-shared/lib.unstripped/libmagma_shared.so" &&  ../../prebuilt/third_party/clang/custom/bin/llvm-ifs --write-if-changed --output-ifs=arm64-shared/libmagma_shared.ifs --output-elf=arm64-shared/link_stub/libmagma_shared.so arm64-shared/lib.unstripped/libmagma_shared.so
ld.lld: error: arm64-shared/obj/src/graphics/lib/magma/src/libmagma/libmagma_client.a(magma_client.o): relocation section (index 44207) has invalid sh_info (0)
clang++: error: ld.lld command failed with exit code 1 (use -v to see invocation)

luci-milo.appspot.com/ui/p/fuchsia/builders/ci/clang_toolchain.ci.core.arm64-release/b8746515880088952657/overview
This is just heads up, and I'll try to come up with a small reproducer. I'm just posting early in case you have any idea.

@gulfemsavrun With limited information, I suspect the root cause is something else. The lld error is to disallow a relocation section with an invalid target . You can run llvm-readelf -S on the relocatable file and figure out how it is built.

I did a few more testing yesterday and I can comfirm, with this change, lld generated an object file with an empty REL section with sh_info set to 0. This object file later triggered the lld error @gulfemsavrun reported.

Without this change:

➜  default git:(1f0f5e6ffac) ✗ /usr/local/google/home/haowei/SRC/clang/clang_revert/bin/clang++ -o user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.debug -r -static -Wl,--build-id=none -Wl,--no-eh-frame-hdr -Wl,--undefined=strlen -Wl,--undefined=__libc_strlen -Wl,--undefined=__asan_strlen -Wl,--undefined=__hwasan_strlen -Wno-unused-command-line-argument -g3 -grecord-gcc-switches -gdwarf-5 -gz=zstd -fno-omit-frame-pointer -momit-leaf-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections -O2 -fno-exceptions -fno-rtti -ffile-compilation-dir=. -no-canonical-prefixes -fuse-ld=lld -Wl,-z,relro --target=aarch64-fuchsia -march=armv8-a+simd+crc+crypto -mtune=generic -fcolor-diagnostics -Wl,--color-diagnostics -fcrash-diagnostics-dir=clang-crashreports -fcrash-diagnostics=all -ffuchsia-api-level=4293918720 -nostartfiles -nolibc -nostdlib++ -Wl,--build-id=none -Wl,--no-eh-frame-hdr -Wl,--no-dynamic-linker -nostartfiles -nolibc -static-libstdc++ -L../../build/config/zircon/libc++-stub -Wl,--no-undefined -L../../build/config/zircon/libc-dummy -Lgen/ifs-as-needed -Wl,--dependency-file=user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.d -Wl,-Map,user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.map -Wl,--start-group '@user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.rsp' -lc++ -Wl,--end-group
➜  default git:(1f0f5e6ffac) ✗ ../../prebuilt/third_party/clang/linux-x64/bin/llvm-readelf -S user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.debug
There are 20 section headers, starting at offset 0x1020:

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .text.strlen      PROGBITS        0000000000000000 000040 00002c 00  AX  0   0  4
  [ 2] .debug_loclists   PROGBITS        0000000000000000 00006c 000054 00      0   0  1
  [ 3] .debug_abbrev     PROGBITS        0000000000000000 0000c0 0000ab 00   C  0   0  1
  [ 4] .debug_info       PROGBITS        0000000000000000 00016b 0000a5 00   C  0   0  1
  [ 5] .rela.debug_info  RELA            0000000000000000 000210 000078 18   I 17   4  8
  [ 6] .debug_str_offsets PROGBITS       0000000000000000 000288 00002d 00   C  0   0  1
  [ 7] .rela.debug_str_offsets RELA      0000000000000000 0002b8 000180 18   I 17   6  8
  [ 8] .debug_str        PROGBITS        0000000000000000 000438 000515 01 MSC  0   0  1
  [ 9] .debug_addr       PROGBITS        0000000000000000 00094d 000010 00      0   0  1
  [10] .rela.debug_addr  RELA            0000000000000000 000960 000018 18   I 17   9  8
  [11] .comment          PROGBITS        0000000000000000 000978 000077 01  MS  0   0  1
  [12] .debug_line       PROGBITS        0000000000000000 0009ef 0000b5 00      0   0  1
  [13] .rela.debug_line  RELA            0000000000000000 000aa8 0000a8 18   I 17  12  8
  [14] .debug_line_str   PROGBITS        0000000000000000 000b50 000090 01 MSC  0   0  1
  [15] .llvm_addrsig     LLVM_ADDRSIG    0000000000000000 000be0 000001 00   E  0   0  1
  [16] .note.GNU-stack   PROGBITS        0000000000000000 000be1 000000 00      0   0  1
  [17] .symtab           SYMTAB          0000000000000000 000be8 0002a0 18     19  23  8
  [18] .shstrtab         STRTAB          0000000000000000 000e88 00010a 00      0   0  1
  [19] .strtab           STRTAB          0000000000000000 000f92 00008e 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  R (retain), p (processor specific)

With this change:

➜  default git:(1f0f5e6ffac) ✗ /usr/local/google/home/haowei/SRC/clang/clang_git_af57ad6536c7df19e6df7217d9d976067fdaf882/bin/clang++ -o user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.debug -r -static -Wl,--build-id=none -Wl,--no-eh-frame-hdr -Wl,--undefined=strlen -Wl,--undefined=__libc_strlen -Wl,--undefined=__asan_strlen -Wl,--undefined=__hwasan_strlen -Wno-unused-command-line-argument -g3 -grecord-gcc-switches -gdwarf-5 -gz=zstd -fno-omit-frame-pointer -momit-leaf-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections -O2 -fno-exceptions -fno-rtti -ffile-compilation-dir=. -no-canonical-prefixes -fuse-ld=lld -Wl,-z,relro --target=aarch64-fuchsia -march=armv8-a+simd+crc+crypto -mtune=generic -fcolor-diagnostics -Wl,--color-diagnostics -fcrash-diagnostics-dir=clang-crashreports -fcrash-diagnostics=all -ffuchsia-api-level=4293918720 -nostartfiles -nolibc -nostdlib++ -Wl,--build-id=none -Wl,--no-eh-frame-hdr -Wl,--no-dynamic-linker -nostartfiles -nolibc -static-libstdc++ -L../../build/config/zircon/libc++-stub -Wl,--no-undefined -L../../build/config/zircon/libc-dummy -Lgen/ifs-as-needed -Wl,--dependency-file=user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.d -Wl,-Map,user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.map -Wl,--start-group '@user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.rsp' -lc++ -Wl,--end-group
➜  default git:(1f0f5e6ffac) ✗ ../../prebuilt/third_party/clang/linux-x64/bin/llvm-readelf -S user.basic_arm64/obj/zircon/system/ulib/c/string/strlen.link.o.debug
There are 21 section headers, starting at offset 0x1078:

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .text.strlen      PROGBITS        0000000000000000 000040 00002c 00  AX  0   0  4
  [ 2] .debug_loclists   PROGBITS        0000000000000000 000070 00006c 00   C  0   0  1
  [ 3] .debug_abbrev     PROGBITS        0000000000000000 0000dc 0000ab 00   C  0   0  1
  [ 4] .debug_info       PROGBITS        0000000000000000 000187 0000a5 00   C  0   0  1
  [ 5] .rela.debug_info  RELA            0000000000000000 000230 000078 18   I 18   4  8
  [ 6] .debug_str_offsets PROGBITS       0000000000000000 0002a8 00002d 00   C  0   0  1
  [ 7] .rela.debug_str_offsets RELA      0000000000000000 0002d8 000180 18   I 18   6  8
  [ 8] .debug_str        PROGBITS        0000000000000000 000458 000515 01 MSC  0   0  1
  [ 9] .debug_addr       PROGBITS        0000000000000000 00096d 000031 00   C  0   0  1
  [10] .rela.debug_addr  RELA            0000000000000000 0009a0 000018 18   I 18   9  8
  [11] .comment          PROGBITS        0000000000000000 0009b8 000077 01  MS  0   0  1
  [12] .debug_line       PROGBITS        0000000000000000 000a2f 0000bb 00   C  0   0  1
  [13] .rela.debug_line  RELA            0000000000000000 000af0 0000a8 18   I 18  12  8
  [14] .debug_line_str   PROGBITS        0000000000000000 000b98 000090 01 MSC  0   0  1
  [15] .llvm_addrsig     LLVM_ADDRSIG    0000000000000000 000c28 000001 00   E  0   0  1
  [16] .rel.dyn          REL             0000000000000000 000070 000000 10   A  0   0  8
  [17] .note.GNU-stack   PROGBITS        0000000000000000 000c29 000000 00      0   0  1
  [18] .symtab           SYMTAB          0000000000000000 000c30 0002a0 18     20  23  8
  [19] .shstrtab         STRTAB          0000000000000000 000ed0 000113 00      0   0  1
  [20] .strtab           STRTAB          0000000000000000 000fe3 00008e 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  R (retain), p (processor specific)
➜  default git:(1f0f5e6ffac) ✗

You can see index 16, .rel.dyn section is empty with sh_info(0).
It will be a bit difficult to construct an end to end reproducer but I will have a try.

@kovdan01
Copy link
Contributor Author

kovdan01 commented Jun 7, 2024

@zeroomega Thanks for a detailed report!

You can see index 16, .rel.dyn section is empty

Having rel(a) or relr section empty in some rare cases is expected behavior with this patch applied, we have tests for that (unfortunately, it's pretty hard to avoid that at current lld state). But, if such files cause issues when passed later as input for lld, we need to investigate that. If it does not contradict with specs, lld should be able to properly process input files with empty relocation sections, and it looks like that it isn't able to do that now.

@zeroomega
Copy link
Contributor

zeroomega commented Jun 7, 2024

@zeroomega Thanks for a detailed report!

You can see index 16, .rel.dyn section is empty

Having rel(a) or relr section empty in some rare cases is expected behavior with this patch applied, we have tests for that (unfortunately, it's pretty hard to avoid that at current lld state). But, if such files cause issues when passed later as input for lld, we need to investigate that. If it does not contradict with specs, lld should be able to properly process input files with empty relocation sections, and it looks like that it isn't able to do that now.

I think the main issue is not an empty REL(A) section. This patch causes lld to generate a REL section with sh_info set to 0. IIUC this shouldn't happen for relocatable linking. And that is what lld complains in the later stages.

@zeroomega
Copy link
Contributor

I have generated a small reproducer package:
lld_rel_sh_info_bug.tar.gz

Steps to reproduce:
Use tip of tree lld:

$ tar -xf lld_rel_sh_info_bug.tar.gz
$ cd lld_rel_sh_info_bug
$ ld.lld @response.txt
$ llvm-readelf -S strlen.link.o.debug

You should see the .rel.dyn section has a 0 value sh_info, which should not happen and will cause lld link failure if this output object file is usd.

Since this patch introduced an issue that lld fail to link the file it generated. Could we revert this change? Other people who uses relocatable linking on aarch64 will likely hit the same issue once they upgrade their lld.

@asl
Copy link
Collaborator

asl commented Jun 7, 2024

Since this patch introduced an issue that lld fail to link the file it generated. Could we revert this change? Other people who uses relocatable linking on aarch64 will likely hit the same issue once they upgrade their lld.

Well, it is great that this issue was found. So, it should be treated as normal bug / missed functionality. Normally per developer policy we do not revert unless it breaks the bot or is known regression wrt some test existing in the mainline.

@frobtech
Copy link
Contributor

frobtech commented Jun 7, 2024

This is not at all the policy normally observed in LLVM. Breakages that are not found by existing tests cannot be allowed to persist just because the test didn't exist before the breakage was landed. It is quite normal for downstream reports of an actual regression in a real world use to be reason to revert. It's not a sign that your change wasn't broken. It's a sign that there was already insufficient testing, and your change did not add sufficient testing, to catch the real regression.

It is invalid output ever to produce an ET_REL file with any SHT_REL or SHT_RELA section whose sh_info and sh_link fields are not valid section indices for the symbol table and target sections. This is why LLD refuses this as input.

There is a lack of existing testing coverage for ld -r output being correct in these ways, and a good test to add would be that ld -r output is accepted as ld input. A general scheme of doing that test scenario for various kinds of links would ensure that bugs like the one introduced here are not easily overlooked.

@zeroomega
Copy link
Contributor

Since this patch introduced an issue that lld fail to link the file it generated. Could we revert this change? Other people who uses relocatable linking on aarch64 will likely hit the same issue once they upgrade their lld.

Well, it is great that this issue was found. So, it should be treated as normal bug / missed functionality. Normally per developer policy we do not revert unless it breaks the bot or is known regression wrt some test existing in the mainline.

I don't think the revert policy you mentioned is correct.
Per https://www.llvm.org/docs/DeveloperPolicy.html

The changes should not cause performance or correctness regressions in code compiled by LLVM on all applicable targets.

In this particular case. lld now generates invalid output (it rejects its output as its input) on AArch64 (a supported target) with relocatable linking (a supported feature). We happened to discovered it in field. This should have covered by lld tests but it such tests did not exist in the current LLVM. I think it is a valid request to revert this change, fix it, combined with additional testing and re-land it. It is a correctness issue which is now observed and validated.

kovdan01 added a commit that referenced this pull request Jun 8, 2024
kovdan01 added a commit that referenced this pull request Jun 8, 2024
…94843)

Reverts #87635

On some corner cases, lld generated an object file with an empty REL
section with `sh_info` set to 0. This file triggers an lld error when
used as its input. See
#87635 (comment)
for details.
@kovdan01
Copy link
Contributor Author

kovdan01 commented Jun 8, 2024

@zeroomega @gulfemsavrun Reverted changes, see #94843. @zeroomega Thanks for providing a reproducer

nekoshirro pushed a commit to nekoshirro/Alchemist-LLVM that referenced this pull request Jun 9, 2024
…lvm#94843)

Reverts llvm#87635

On some corner cases, lld generated an object file with an empty REL
section with `sh_info` set to 0. This file triggers an lld error when
used as its input. See
llvm#87635 (comment)
for details.

Signed-off-by: Hafidz Muzakky <ais.muzakky@gmail.com>
kovdan01 added a commit to kovdan01/llvm-project that referenced this pull request Jun 24, 2024
This re-applies llvm#87635 with fix of issue described in
llvm#87635 (comment).

We need to discard `rel(a).dyn` section when output is a relocatable object
file. The section is always empty in such a case (as well as both auth and
regular relr sections), and emitting that resulted in its `sh_info` equal to 0.
Section with zero index is always a NULL section according to ELF spec,
and lld is unable to run `ObjFile<ELFT>::getRelocTarget` against
relocation section with `sh_info` equal to zero (the ELF spec does not
seem to define behavior for such a corner case). This is only an issue
with relocatable object file output - having `sh_info` equal to zero for
shared objects is OK.

Original commit description below.

Support `R_AARCH64_AUTH_RELATIVE` relocation compression as described in
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#relocation-compression.
kovdan01 added a commit that referenced this pull request Jun 29, 2024
This re-applies #87635 after the issue described in
#87635 (comment)
is fixed in ebc123e. A corresponding
test is also added.

Original PR description below.

Support `R_AARCH64_AUTH_RELATIVE` relocation compression as described in
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#relocation-compression.
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
This re-applies llvm#87635 after the issue described in
llvm#87635 (comment)
is fixed in ebc123e. A corresponding
test is also added.

Original PR description below.

Support `R_AARCH64_AUTH_RELATIVE` relocation compression as described in
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#relocation-compression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants