link/Elf: ensure we always sort all relocations by r_offset in -r mode

According to a comment in mold, this is the expected (and desired)
condition by the linkers, except for some architectures (RISCV and
Loongarch) where this condition does not have to upheld.

If you follow the changes in this patch and in particular doc comments
I have linked the comment/code in mold that explains and implements
this.

I have also modified `testEhFrameRelocatable` test to now test both
cases such that `zig ld -r a.o b.o -o c.o` and `zig ld -r b.o a.o -o
d.o`. In both cases, `c.o` and `d.o` should produce valid object
files which was not the case before this patch.
This commit is contained in:
Jakub Konka
2024-10-29 21:17:11 +01:00
committed by Andrew Kelley
parent 4661705a0e
commit 6ff267dc26
4 changed files with 72 additions and 45 deletions

View File

@@ -2744,32 +2744,52 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
,
});
obj2.linkLibCpp();
const obj3 = addObject(b, opts, .{ .name = "obj3", .cpp_source_bytes =
\\#include <iostream>
\\#include <stdexcept>
\\extern int try_again();
\\int main() {
\\ try {
\\ try_again();
\\ } catch (const std::exception &e) {
\\ std::cout << "exception=" << e.what();
\\ }
\\ return 0;
\\}
});
obj3.linkLibCpp();
const obj = addObject(b, opts, .{ .name = "obj" });
obj.addObject(obj1);
obj.addObject(obj2);
obj.linkLibCpp();
{
const obj = addObject(b, opts, .{ .name = "obj" });
obj.addObject(obj1);
obj.addObject(obj2);
obj.linkLibCpp();
const exe = addExecutable(b, opts, .{ .name = "test1" });
addCppSourceBytes(exe,
\\#include <iostream>
\\#include <stdexcept>
\\extern int try_again();
\\int main() {
\\ try {
\\ try_again();
\\ } catch (const std::exception &e) {
\\ std::cout << "exception=" << e.what();
\\ }
\\ return 0;
\\}
, &.{});
exe.addObject(obj);
exe.linkLibCpp();
const exe = addExecutable(b, opts, .{ .name = "test1" });
exe.addObject(obj3);
exe.addObject(obj);
exe.linkLibCpp();
const run = addRunArtifact(exe);
run.expectStdOutEqual("exception=Oh no!");
test_step.dependOn(&run.step);
const run = addRunArtifact(exe);
run.expectStdOutEqual("exception=Oh no!");
test_step.dependOn(&run.step);
}
{
// Flipping the order should not influence the end result.
const obj = addObject(b, opts, .{ .name = "obj" });
obj.addObject(obj2);
obj.addObject(obj1);
obj.linkLibCpp();
const exe = addExecutable(b, opts, .{ .name = "test2" });
exe.addObject(obj3);
exe.addObject(obj);
exe.linkLibCpp();
const run = addRunArtifact(exe);
run.expectStdOutEqual("exception=Oh no!");
test_step.dependOn(&run.step);
}
return test_step;
}