Skip to content

[LLD][eh_frame] Relocation overlaps with FDE length in .eh_frame #158011

@MaooJian

Description

@MaooJian

When compiling the following test case, I observed that relocations in .rela.eh_frame overwrite the length field of a Frame Description Entry (FDE) in .eh_frame. This only happens when linking with lld; using GNU ld does not produce this issue.

test1.cpp

#include <stdexcept>
#include <iostream>
int foo1(int x)
{
    try {
        if (x < 0)
            throw std::invalid_argument("std::invalid_argument");
        return 0;
    } catch (const std::invalid_argument &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return -1;
    }
}

test2.cpp

#include <stdexcept>
#include <iostream>
int foo2(int x)
{
    try {
        if (x == 0)
            throw std::runtime_error("std::runtime_error");
        return x;
    } catch (const std::runtime_error &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return -1;
    }
}

Build commands (with lld):

clang++ -O2 -fPIC -mcmodel=large -o ./test1.o -c ./test1.cpp
clang++ -O2 -fPIC -mcmodel=large -o ./test2.o -c ./test2.cpp
clang++ -fuse-ld=lld -rdynamic -Wl,--emit-relocs -shared -o test.so test1.o test2.o

Hex dump of .eh_frame:

0x00000d20 14000000 00000000 017a5200 01781001 .........zR..x..
0x00000d30 1c0c0708 90010000 1c000000 1c000000 ................
0x00000d40 50160000 00000000 2f000000 00000000 P......./.......

Relocation entries (readelf -rW test.so):

0000000000000d33  0000001700000018 R_X86_64_PC64          0000000000004680 DW.ref.__gxx_personality_v0 + 0

This relocation corresponds to bytes inside:

0x00000d30  1c0c0708 90010000 1c000000 1c000000
                  ^^^^^^^^^^^^^^^^^^

where 1c000000 encodes the FDE length field.

Normally, relocations should not overlap with the next entry header in .eh_frame.
This seems to be a bug in lld’s .eh_frame relocation handling.

Environment:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions