authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-17 11:29:06+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-17 11:29:06+01:00
log975862aca9a68f1d6fa786b6cef19bcc7b1aec2a
treebd0c4783671214faa562fd29956ff00bae8b3e5b
parent601aa10b82a43a9d566e8de3cb784929185411b4

elf: add riscv dynamic relocs


6 files changed, 172 insertions(+), 142 deletions(-)

lib/std/elf.zig+3
......@@ -1891,6 +1891,7 @@ pub const R_X86_64 = enum(u32) {
18911891 R_X86_64_GOTPCRELX = 41,
18921892 /// Load from 32 bit signed PC relative offset to GOT entry with REX prefix, relaxable
18931893 R_X86_64_REX_GOTPCRELX = 42,
1894 _,
18941895};
18951896
18961897/// AArch64 relocs.
......@@ -2163,6 +2164,7 @@ pub const R_AARCH64 = enum(u32) {
21632164 R_AARCH64_TLSDESC = 1031,
21642165 /// STT_GNU_IFUNC relocation.
21652166 R_AARCH64_IRELATIVE = 1032,
2167 _,
21662168};
21672169
21682170/// RISC-V relocations.
......@@ -2225,6 +2227,7 @@ pub const R_RISCV = enum(u32) {
22252227 R_RISCV_PLT32 = 59,
22262228 R_RISCV_SET_ULEB128 = 60,
22272229 R_RISCV_SUB_ULEB128 = 61,
2230 _,
22282231};
22292232
22302233pub const STV = enum(u2) {
src/arch/x86_64/Emit.zig+13-9
......@@ -43,9 +43,10 @@ pub fn emitMir(emit: *Emit) Error!void {
4343 .linker_extern_fn => |symbol| if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| {
4444 // Add relocation to the decl.
4545 const atom_ptr = elf_file.symbol(symbol.atom_index).atom(elf_file).?;
46 const r_type = @intFromEnum(std.elf.R_X86_64.R_X86_64_PLT32);
4647 try atom_ptr.addReloc(elf_file, .{
4748 .r_offset = end_offset - 4,
48 .r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | std.elf.R_X86_64_PLT32,
49 .r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | r_type,
4950 .r_addend = -4,
5051 });
5152 } else if (emit.lower.bin_file.cast(link.File.MachO)) |macho_file| {
......@@ -88,18 +89,20 @@ pub fn emitMir(emit: *Emit) Error!void {
8889 .linker_tlsld => |data| {
8990 const elf_file = emit.lower.bin_file.cast(link.File.Elf).?;
9091 const atom = elf_file.symbol(data.atom_index).atom(elf_file).?;
92 const r_type = @intFromEnum(std.elf.R_X86_64.R_X86_64_TLSLD);
9193 try atom.addReloc(elf_file, .{
9294 .r_offset = end_offset - 4,
93 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | std.elf.R_X86_64_TLSLD,
95 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
9496 .r_addend = -4,
9597 });
9698 },
9799 .linker_dtpoff => |data| {
98100 const elf_file = emit.lower.bin_file.cast(link.File.Elf).?;
99101 const atom = elf_file.symbol(data.atom_index).atom(elf_file).?;
102 const r_type = @intFromEnum(std.elf.R_X86_64.R_X86_64_DTPOFF32);
100103 try atom.addReloc(elf_file, .{
101104 .r_offset = end_offset - 4,
102 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | std.elf.R_X86_64_DTPOFF32,
105 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
103106 .r_addend = 0,
104107 });
105108 },
......@@ -119,9 +122,9 @@ pub fn emitMir(emit: *Emit) Error!void {
119122 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib)
120123 link.File.Elf.R_X86_64_ZIG_GOTPCREL
121124 else if (sym.flags.needs_got)
122 std.elf.R_X86_64_GOTPCREL
125 @intFromEnum(std.elf.R_X86_64.R_X86_64_GOTPCREL)
123126 else
124 std.elf.R_X86_64_PC32;
127 @intFromEnum(std.elf.R_X86_64.R_X86_64_PC32);
125128 try atom.addReloc(elf_file, .{
126129 .r_offset = end_offset - 4,
127130 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
......@@ -129,20 +132,21 @@ pub fn emitMir(emit: *Emit) Error!void {
129132 });
130133 } else {
131134 if (lowered_inst.encoding.mnemonic == .call and sym.flags.needs_zig_got and is_obj_or_static_lib) {
135 const r_type = @intFromEnum(std.elf.R_X86_64.R_X86_64_PC32);
132136 try atom.addReloc(elf_file, .{
133137 .r_offset = end_offset - 4,
134 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | std.elf.R_X86_64_PC32,
138 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
135139 .r_addend = -4,
136140 });
137141 } else {
138142 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib)
139143 link.File.Elf.R_X86_64_ZIG_GOT32
140144 else if (sym.flags.needs_got)
141 std.elf.R_X86_64_GOT32
145 @intFromEnum(std.elf.R_X86_64.R_X86_64_GOT32)
142146 else if (sym.flags.is_tls)
143 std.elf.R_X86_64_TPOFF32
147 @intFromEnum(std.elf.R_X86_64.R_X86_64_TPOFF32)
144148 else
145 std.elf.R_X86_64_32;
149 @intFromEnum(std.elf.R_X86_64.R_X86_64_32);
146150 try atom.addReloc(elf_file, .{
147151 .r_offset = end_offset - 4,
148152 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
src/link/Elf.zig+2-2
......@@ -6065,8 +6065,8 @@ const RelaSection = struct {
60656065};
60666066const RelaSectionTable = std.AutoArrayHashMapUnmanaged(u32, RelaSection);
60676067
6068pub const R_X86_64_ZIG_GOT32 = elf.R_X86_64_NUM + 1;
6069pub const R_X86_64_ZIG_GOTPCREL = elf.R_X86_64_NUM + 2;
6068pub const R_X86_64_ZIG_GOT32: u32 = 0xff00;
6069pub const R_X86_64_ZIG_GOTPCREL: u32 = 0xff01;
60706070
60716071fn defaultEntrySymbolName(cpu_arch: std.Target.Cpu.Arch) []const u8 {
60726072 return switch (cpu_arch) {
src/link/Elf/Atom.zig+116-105
......@@ -384,7 +384,10 @@ pub fn scanRelocsRequiresCode(self: Atom, elf_file: *Elf) bool {
384384 const cpu_arch = elf_file.getTarget().cpu.arch;
385385 for (self.relocs(elf_file)) |rel| {
386386 switch (cpu_arch) {
387 .x86_64 => if (rel.r_type() == elf.R_X86_64_GOTTPOFF) return true,
387 .x86_64 => {
388 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
389 if (r_type == .R_X86_64_GOTTPOFF) return true;
390 },
388391 else => {},
389392 }
390393 }
......@@ -836,8 +839,9 @@ const x86_64 = struct {
836839 var i: usize = 0;
837840 while (i < rels.len) : (i += 1) {
838841 const rel = rels[i];
842 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
839843
840 if (rel.r_type() == elf.R_X86_64_NONE) continue;
844 if (r_type == .R_X86_64_NONE) continue;
841845
842846 const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
843847
......@@ -869,41 +873,41 @@ const x86_64 = struct {
869873
870874 // While traversing relocations, mark symbols that require special handling such as
871875 // pointer indirection via GOT, or a stub trampoline via PLT.
872 switch (rel.r_type()) {
873 elf.R_X86_64_64 => {
876 switch (r_type) {
877 .R_X86_64_64 => {
874878 try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file);
875879 },
876880
877 elf.R_X86_64_32,
878 elf.R_X86_64_32S,
881 .R_X86_64_32,
882 .R_X86_64_32S,
879883 => {
880884 try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file);
881885 },
882886
883 elf.R_X86_64_GOT32,
884 elf.R_X86_64_GOTPC32,
885 elf.R_X86_64_GOTPC64,
886 elf.R_X86_64_GOTPCREL,
887 elf.R_X86_64_GOTPCREL64,
888 elf.R_X86_64_GOTPCRELX,
889 elf.R_X86_64_REX_GOTPCRELX,
887 .R_X86_64_GOT32,
888 .R_X86_64_GOTPC32,
889 .R_X86_64_GOTPC64,
890 .R_X86_64_GOTPCREL,
891 .R_X86_64_GOTPCREL64,
892 .R_X86_64_GOTPCRELX,
893 .R_X86_64_REX_GOTPCRELX,
890894 => {
891895 symbol.flags.needs_got = true;
892896 },
893897
894 elf.R_X86_64_PLT32,
895 elf.R_X86_64_PLTOFF64,
898 .R_X86_64_PLT32,
899 .R_X86_64_PLTOFF64,
896900 => {
897901 if (symbol.flags.import) {
898902 symbol.flags.needs_plt = true;
899903 }
900904 },
901905
902 elf.R_X86_64_PC32 => {
906 .R_X86_64_PC32 => {
903907 try atom.scanReloc(symbol, rel, pcRelocAction(symbol, elf_file), elf_file);
904908 },
905909
906 elf.R_X86_64_TLSGD => {
910 .R_X86_64_TLSGD => {
907911 // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr
908912
909913 if (is_static or (!symbol.flags.import and !is_dyn_lib)) {
......@@ -918,7 +922,7 @@ const x86_64 = struct {
918922 }
919923 },
920924
921 elf.R_X86_64_TLSLD => {
925 .R_X86_64_TLSLD => {
922926 // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr
923927
924928 if (is_static or !is_dyn_lib) {
......@@ -930,7 +934,7 @@ const x86_64 = struct {
930934 }
931935 },
932936
933 elf.R_X86_64_GOTTPOFF => {
937 .R_X86_64_GOTTPOFF => {
934938 const should_relax = blk: {
935939 if (is_dyn_lib or symbol.flags.import) break :blk false;
936940 if (!x86_64.canRelaxGotTpOff(code.?[r_offset - 3 ..])) break :blk false;
......@@ -941,35 +945,37 @@ const x86_64 = struct {
941945 }
942946 },
943947
944 elf.R_X86_64_GOTPC32_TLSDESC => {
948 .R_X86_64_GOTPC32_TLSDESC => {
945949 const should_relax = is_static or (!is_dyn_lib and !symbol.flags.import);
946950 if (!should_relax) {
947951 symbol.flags.needs_tlsdesc = true;
948952 }
949953 },
950954
951 elf.R_X86_64_TPOFF32,
952 elf.R_X86_64_TPOFF64,
955 .R_X86_64_TPOFF32,
956 .R_X86_64_TPOFF64,
953957 => {
954958 if (is_dyn_lib) try atom.reportPicError(symbol, rel, elf_file);
955959 },
956960
957 elf.R_X86_64_GOTOFF64,
958 elf.R_X86_64_DTPOFF32,
959 elf.R_X86_64_DTPOFF64,
960 elf.R_X86_64_SIZE32,
961 elf.R_X86_64_SIZE64,
962 elf.R_X86_64_TLSDESC_CALL,
961 .R_X86_64_GOTOFF64,
962 .R_X86_64_DTPOFF32,
963 .R_X86_64_DTPOFF64,
964 .R_X86_64_SIZE32,
965 .R_X86_64_SIZE64,
966 .R_X86_64_TLSDESC_CALL,
963967 => {},
964968
965 // Zig custom relocations
966 Elf.R_X86_64_ZIG_GOT32,
967 Elf.R_X86_64_ZIG_GOTPCREL,
968 => {
969 assert(symbol.flags.has_zig_got);
970 },
969 else => |x| switch (@intFromEnum(x)) {
970 // Zig custom relocations
971 Elf.R_X86_64_ZIG_GOT32,
972 Elf.R_X86_64_ZIG_GOTPCREL,
973 => {
974 assert(symbol.flags.has_zig_got);
975 },
971976
972 else => try atom.reportUnhandledRelocError(rel, elf_file),
977 else => try atom.reportUnhandledRelocError(rel, elf_file),
978 },
973979 }
974980 }
975981 }
......@@ -983,8 +989,8 @@ const x86_64 = struct {
983989 var i: usize = 0;
984990 while (i < rels.len) : (i += 1) {
985991 const rel = rels[i];
986 const r_type = rel.r_type();
987 if (r_type == elf.R_X86_64_NONE) continue;
992 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
993 if (r_type == .R_X86_64_NONE) continue;
988994
989995 const target = switch (file_ptr) {
990996 .zig_object => |x| elf_file.symbol(x.symbol(rel.r_sym())),
......@@ -1022,7 +1028,7 @@ const x86_64 = struct {
10221028 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
10231029
10241030 relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{
1025 relocation.fmtRelocType(r_type, .x86_64),
1031 @tagName(r_type),
10261032 r_offset,
10271033 P,
10281034 S + A,
......@@ -1033,10 +1039,10 @@ const x86_64 = struct {
10331039
10341040 try stream.seekTo(r_offset);
10351041
1036 switch (rel.r_type()) {
1037 elf.R_X86_64_NONE => unreachable,
1042 switch (r_type) {
1043 .R_X86_64_NONE => unreachable,
10381044
1039 elf.R_X86_64_64 => {
1045 .R_X86_64_64 => {
10401046 try atom.resolveDynAbsReloc(
10411047 target,
10421048 rel,
......@@ -1046,15 +1052,15 @@ const x86_64 = struct {
10461052 );
10471053 },
10481054
1049 elf.R_X86_64_PLT32,
1050 elf.R_X86_64_PC32,
1055 .R_X86_64_PLT32,
1056 .R_X86_64_PC32,
10511057 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little),
10521058
1053 elf.R_X86_64_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little),
1054 elf.R_X86_64_GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .little),
1055 elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .little),
1059 .R_X86_64_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little),
1060 .R_X86_64_GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .little),
1061 .R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .little),
10561062
1057 elf.R_X86_64_GOTPCRELX => {
1063 .R_X86_64_GOTPCRELX => {
10581064 if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: {
10591065 x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk;
10601066 try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little);
......@@ -1063,7 +1069,7 @@ const x86_64 = struct {
10631069 try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little);
10641070 },
10651071
1066 elf.R_X86_64_REX_GOTPCRELX => {
1072 .R_X86_64_REX_GOTPCRELX => {
10671073 if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: {
10681074 x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk;
10691075 try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little);
......@@ -1072,16 +1078,16 @@ const x86_64 = struct {
10721078 try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little);
10731079 },
10741080
1075 elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little),
1076 elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little),
1081 .R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little),
1082 .R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little),
10771083
1078 elf.R_X86_64_TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little),
1079 elf.R_X86_64_TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .little),
1084 .R_X86_64_TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little),
1085 .R_X86_64_TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .little),
10801086
1081 elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .little),
1082 elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
1087 .R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .little),
1088 .R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
10831089
1084 elf.R_X86_64_TLSGD => {
1090 .R_X86_64_TLSGD => {
10851091 if (target.flags.has_tlsgd) {
10861092 const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file)));
10871093 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
......@@ -1101,7 +1107,7 @@ const x86_64 = struct {
11011107 }
11021108 },
11031109
1104 elf.R_X86_64_TLSLD => {
1110 .R_X86_64_TLSLD => {
11051111 if (elf_file.got.tlsld_index) |entry_index| {
11061112 const tlsld_entry = elf_file.got.entries.items[entry_index];
11071113 const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file)));
......@@ -1118,7 +1124,7 @@ const x86_64 = struct {
11181124 }
11191125 },
11201126
1121 elf.R_X86_64_GOTPC32_TLSDESC => {
1127 .R_X86_64_GOTPC32_TLSDESC => {
11221128 if (target.flags.has_tlsdesc) {
11231129 const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file)));
11241130 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
......@@ -1128,12 +1134,12 @@ const x86_64 = struct {
11281134 }
11291135 },
11301136
1131 elf.R_X86_64_TLSDESC_CALL => if (!target.flags.has_tlsdesc) {
1137 .R_X86_64_TLSDESC_CALL => if (!target.flags.has_tlsdesc) {
11321138 // call -> nop
11331139 try cwriter.writeAll(&.{ 0x66, 0x90 });
11341140 },
11351141
1136 elf.R_X86_64_GOTTPOFF => {
1142 .R_X86_64_GOTTPOFF => {
11371143 if (target.flags.has_gottp) {
11381144 const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file)));
11391145 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
......@@ -1143,13 +1149,15 @@ const x86_64 = struct {
11431149 }
11441150 },
11451151
1146 elf.R_X86_64_GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .little),
1152 .R_X86_64_GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .little),
11471153
1148 // Zig custom relocations
1149 Elf.R_X86_64_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .little),
1150 Elf.R_X86_64_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .little),
1154 else => |x| switch (@intFromEnum(x)) {
1155 // Zig custom relocations
1156 Elf.R_X86_64_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .little),
1157 Elf.R_X86_64_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .little),
11511158
1152 else => {},
1159 else => {},
1160 },
11531161 }
11541162 }
11551163 }
......@@ -1163,8 +1171,8 @@ const x86_64 = struct {
11631171 var i: usize = 0;
11641172 while (i < rels.len) : (i += 1) {
11651173 const rel = rels[i];
1166 const r_type = rel.r_type();
1167 if (r_type == elf.R_X86_64_NONE) continue;
1174 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
1175 if (r_type == .R_X86_64_NONE) continue;
11681176
11691177 const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
11701178
......@@ -1211,7 +1219,7 @@ const x86_64 = struct {
12111219 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
12121220
12131221 relocs_log.debug(" {s}: {x}: [{x} => {x}] ({s})", .{
1214 relocation.fmtRelocType(r_type, .x86_64),
1222 @tagName(r_type),
12151223 rel.r_offset,
12161224 P,
12171225 S + A,
......@@ -1221,21 +1229,21 @@ const x86_64 = struct {
12211229 try stream.seekTo(r_offset);
12221230
12231231 switch (r_type) {
1224 elf.R_X86_64_NONE => unreachable,
1225 elf.R_X86_64_8 => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .little),
1226 elf.R_X86_64_16 => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little),
1227 elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little),
1228 elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little),
1229 elf.R_X86_64_64 => try cwriter.writeInt(i64, S + A, .little),
1230 elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little),
1231 elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
1232 elf.R_X86_64_GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little),
1233 elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little),
1234 elf.R_X86_64_SIZE32 => {
1232 .R_X86_64_NONE => unreachable,
1233 .R_X86_64_8 => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .little),
1234 .R_X86_64_16 => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little),
1235 .R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little),
1236 .R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little),
1237 .R_X86_64_64 => try cwriter.writeInt(i64, S + A, .little),
1238 .R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little),
1239 .R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
1240 .R_X86_64_GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little),
1241 .R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little),
1242 .R_X86_64_SIZE32 => {
12351243 const size = @as(i64, @intCast(target.elfSym(elf_file).st_size));
12361244 try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A)))), .little);
12371245 },
1238 elf.R_X86_64_SIZE64 => {
1246 .R_X86_64_SIZE64 => {
12391247 const size = @as(i64, @intCast(target.elfSym(elf_file).st_size));
12401248 try cwriter.writeInt(i64, @as(i64, @intCast(size + A)), .little);
12411249 },
......@@ -1283,9 +1291,11 @@ const x86_64 = struct {
12831291 ) !void {
12841292 assert(rels.len == 2);
12851293 const writer = stream.writer();
1286 switch (rels[1].r_type()) {
1287 elf.R_X86_64_PC32,
1288 elf.R_X86_64_PLT32,
1294 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());
1295 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1296 switch (rel_1) {
1297 .R_X86_64_PC32,
1298 .R_X86_64_PLT32,
12891299 => {
12901300 var insts = [_]u8{
12911301 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax
......@@ -1298,9 +1308,9 @@ const x86_64 = struct {
12981308
12991309 else => {
13001310 var err = try elf_file.addErrorWithNotes(1);
1301 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1302 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1303 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1311 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{
1312 @tagName(rel_0),
1313 @tagName(rel_1),
13041314 });
13051315 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
13061316 self.file(elf_file).?.fmtPath(),
......@@ -1320,9 +1330,11 @@ const x86_64 = struct {
13201330 ) !void {
13211331 assert(rels.len == 2);
13221332 const writer = stream.writer();
1323 switch (rels[1].r_type()) {
1324 elf.R_X86_64_PC32,
1325 elf.R_X86_64_PLT32,
1333 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());
1334 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1335 switch (rel_1) {
1336 .R_X86_64_PC32,
1337 .R_X86_64_PLT32,
13261338 => {
13271339 var insts = [_]u8{
13281340 0x31, 0xc0, // xor %eax, %eax
......@@ -1334,8 +1346,8 @@ const x86_64 = struct {
13341346 try writer.writeAll(&insts);
13351347 },
13361348
1337 elf.R_X86_64_GOTPCREL,
1338 elf.R_X86_64_GOTPCRELX,
1349 .R_X86_64_GOTPCREL,
1350 .R_X86_64_GOTPCRELX,
13391351 => {
13401352 var insts = [_]u8{
13411353 0x31, 0xc0, // xor %eax, %eax
......@@ -1350,9 +1362,9 @@ const x86_64 = struct {
13501362
13511363 else => {
13521364 var err = try elf_file.addErrorWithNotes(1);
1353 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1354 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1355 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1365 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{
1366 @tagName(rel_0),
1367 @tagName(rel_1),
13561368 });
13571369 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
13581370 self.file(elf_file).?.fmtPath(),
......@@ -1419,11 +1431,13 @@ const x86_64 = struct {
14191431 ) !void {
14201432 assert(rels.len == 2);
14211433 const writer = stream.writer();
1422 switch (rels[1].r_type()) {
1423 elf.R_X86_64_PC32,
1424 elf.R_X86_64_PLT32,
1425 elf.R_X86_64_GOTPCREL,
1426 elf.R_X86_64_GOTPCRELX,
1434 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());
1435 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1436 switch (rel_1) {
1437 .R_X86_64_PC32,
1438 .R_X86_64_PLT32,
1439 .R_X86_64_GOTPCREL,
1440 .R_X86_64_GOTPCRELX,
14271441 => {
14281442 var insts = [_]u8{
14291443 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax
......@@ -1432,17 +1446,14 @@ const x86_64 = struct {
14321446 std.mem.writeInt(i32, insts[12..][0..4], value, .little);
14331447 try stream.seekBy(-4);
14341448 try writer.writeAll(&insts);
1435 relocs_log.debug(" relaxing {} and {}", .{
1436 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1437 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1438 });
1449 relocs_log.debug(" relaxing {s} and {s}", .{ @tagName(rel_0), @tagName(rel_1) });
14391450 },
14401451
14411452 else => {
14421453 var err = try elf_file.addErrorWithNotes(1);
1443 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1444 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1445 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1454 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{
1455 @tagName(rel_0),
1456 @tagName(rel_1),
14461457 });
14471458 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
14481459 self.file(elf_file).?.fmtPath(),
src/link/Elf/eh_frame.zig+6-5
......@@ -541,11 +541,12 @@ const EH_PE = struct {
541541
542542const x86_64 = struct {
543543 fn resolveReloc(rel: elf.Elf64_Rela, source: i64, target: i64, data: []u8) void {
544 switch (rel.r_type()) {
545 elf.R_X86_64_32 => std.mem.writeInt(i32, data[0..4], @as(i32, @truncate(target)), .little),
546 elf.R_X86_64_64 => std.mem.writeInt(i64, data[0..8], target, .little),
547 elf.R_X86_64_PC32 => std.mem.writeInt(i32, data[0..4], @as(i32, @intCast(target - source)), .little),
548 elf.R_X86_64_PC64 => std.mem.writeInt(i64, data[0..8], target - source, .little),
544 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
545 switch (r_type) {
546 .R_X86_64_32 => std.mem.writeInt(i32, data[0..4], @as(i32, @truncate(target)), .little),
547 .R_X86_64_64 => std.mem.writeInt(i64, data[0..8], target, .little),
548 .R_X86_64_PC32 => std.mem.writeInt(i32, data[0..4], @as(i32, @intCast(target - source)), .little),
549 .R_X86_64_PC64 => std.mem.writeInt(i64, data[0..8], target - source, .little),
549550 else => unreachable,
550551 }
551552 }
src/link/Elf/relocation.zig+32-21
......@@ -11,7 +11,25 @@ pub const Kind = enum {
1111 tlsdesc,
1212};
1313
14const x86_64_relocs = [_]struct { Kind, elf.R_X86_64 }{
14fn Table(comptime len: comptime_int, comptime RelType: type, comptime mapping: [len]struct { Kind, RelType }) type {
15 return struct {
16 fn decode(r_type: u32) ?Kind {
17 inline for (mapping) |entry| {
18 if (@intFromEnum(entry[1]) == r_type) return entry[0];
19 }
20 return null;
21 }
22
23 fn encode(comptime kind: Kind) u32 {
24 inline for (mapping) |entry| {
25 if (entry[0] == kind) return @intFromEnum(entry[1]);
26 }
27 unreachable;
28 }
29 };
30}
31
32const x86_64_relocs = Table(10, elf.R_X86_64, .{
1533 .{ .abs, .R_X86_64_64 },
1634 .{ .copy, .R_X86_64_COPY },
1735 .{ .rel, .R_X86_64_RELATIVE },
......@@ -22,9 +40,9 @@ const x86_64_relocs = [_]struct { Kind, elf.R_X86_64 }{
2240 .{ .dtpoff, .R_X86_64_DTPOFF64 },
2341 .{ .tpoff, .R_X86_64_TPOFF64 },
2442 .{ .tlsdesc, .R_X86_64_TLSDESC },
25};
43});
2644
27const aarch64_relocs = [_]struct { Kind, elf.R_AARCH64 }{
45const aarch64_relocs = Table(10, elf.R_AARCH64, .{
2846 .{ .abs, .R_AARCH64_ABS64 },
2947 .{ .copy, .R_AARCH64_COPY },
3048 .{ .rel, .R_AARCH64_RELATIVE },
......@@ -35,9 +53,9 @@ const aarch64_relocs = [_]struct { Kind, elf.R_AARCH64 }{
3553 .{ .dtpoff, .R_AARCH64_TLS_DTPREL },
3654 .{ .tpoff, .R_AARCH64_TLS_TPREL },
3755 .{ .tlsdesc, .R_AARCH64_TLSDESC },
38};
56});
3957
40const riscv64_relocs = [_]struct { Kind, elf.R_RISCV }{
58const riscv64_relocs = Table(8, elf.R_RISCV, .{
4159 .{ .abs, .R_RISCV_64 },
4260 .{ .copy, .R_RISCV_COPY },
4361 .{ .rel, .R_RISCV_RELATIVE },
......@@ -46,31 +64,24 @@ const riscv64_relocs = [_]struct { Kind, elf.R_RISCV }{
4664 .{ .dtpmod, .R_RISCV_TLS_DTPMOD64 },
4765 .{ .dtpoff, .R_RISCV_TLS_DTPREL64 },
4866 .{ .tpoff, .R_RISCV_TLS_TPREL64 },
49 .{ .tpoff, .R_RISCV_TLS_TPREL64 },
50};
67});
5168
5269pub fn decode(r_type: u32, cpu_arch: std.Target.Cpu.Arch) ?Kind {
53 const relocs = switch (cpu_arch) {
54 .x86_64 => &x86_64_relocs,
55 .aarch64 => &aarch64_relocs,
70 return switch (cpu_arch) {
71 .x86_64 => x86_64_relocs.decode(r_type),
72 .aarch64 => aarch64_relocs.decode(r_type),
73 .riscv64 => riscv64_relocs.decode(r_type),
5674 else => @panic("TODO unhandled cpu arch"),
5775 };
58 inline for (relocs) |entry| {
59 if (entry[1] == r_type) return entry[0];
60 }
61 return null;
6276}
6377
6478pub fn encode(comptime kind: Kind, cpu_arch: std.Target.Cpu.Arch) u32 {
65 const relocs = switch (cpu_arch) {
66 .x86_64 => &x86_64_relocs,
67 .aarch64 => &aarch64_relocs,
79 return switch (cpu_arch) {
80 .x86_64 => x86_64_relocs.encode(kind),
81 .aarch64 => aarch64_relocs.encode(kind),
82 .riscv64 => riscv64_relocs.encode(kind),
6883 else => @panic("TODO unhandled cpu arch"),
6984 };
70 inline for (relocs) |entry| {
71 if (entry[0] == kind) return entry[1];
72 }
73 unreachable;
7485}
7586
7687const FormatRelocTypeCtx = struct {