Commit 7305184203

Jacob Young <jacobly0@users.noreply.github.com>
2024-08-30 14:58:06
dwarf: share and use `std.dwarf.EH_PE` constants
1 parent f0c0f69
Changed files (3)
src/link/Elf/eh_frame.zig
@@ -482,9 +482,9 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
     const gpa = comp.gpa;
 
     try writer.writeByte(1); // version
-    try writer.writeByte(EH_PE.pcrel | EH_PE.sdata4);
-    try writer.writeByte(EH_PE.udata4);
-    try writer.writeByte(EH_PE.datarel | EH_PE.sdata4);
+    try writer.writeByte(DW_EH_PE.pcrel | DW_EH_PE.sdata4);
+    try writer.writeByte(DW_EH_PE.udata4);
+    try writer.writeByte(DW_EH_PE.datarel | DW_EH_PE.sdata4);
 
     const shdrs = elf_file.sections.items(.shdr);
     const eh_frame_shdr = shdrs[elf_file.eh_frame_section_index.?];
@@ -543,25 +543,6 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
 
 const eh_frame_hdr_header_size: usize = 12;
 
-const EH_PE = struct {
-    pub const absptr = 0x00;
-    pub const uleb128 = 0x01;
-    pub const udata2 = 0x02;
-    pub const udata4 = 0x03;
-    pub const udata8 = 0x04;
-    pub const sleb128 = 0x09;
-    pub const sdata2 = 0x0A;
-    pub const sdata4 = 0x0B;
-    pub const sdata8 = 0x0C;
-    pub const pcrel = 0x10;
-    pub const textrel = 0x20;
-    pub const datarel = 0x30;
-    pub const funcrel = 0x40;
-    pub const aligned = 0x50;
-    pub const indirect = 0x80;
-    pub const omit = 0xFF;
-};
-
 const x86_64 = struct {
     fn resolveReloc(rec: anytype, elf_file: *Elf, rel: elf.Elf64_Rela, source: i64, target: i64, data: []u8) !void {
         const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
@@ -619,6 +600,7 @@ const relocation = @import("relocation.zig");
 
 const Allocator = std.mem.Allocator;
 const Atom = @import("Atom.zig");
+const DW_EH_PE = std.dwarf.EH.PE;
 const Elf = @import("../Elf.zig");
 const Object = @import("Object.zig");
 const Symbol = @import("Symbol.zig");
src/link/MachO/eh_frame.zig
@@ -29,22 +29,22 @@ pub const Cie = struct {
         for (aug[1..]) |ch| switch (ch) {
             'R' => {
                 const enc = try reader.readByte();
-                if (enc & 0xf != EH_PE.absptr or enc & EH_PE.pcrel == 0) {
+                if (enc != DW_EH_PE.pcrel | DW_EH_PE.absptr) {
                     @panic("unexpected pointer encoding"); // TODO error
                 }
             },
             'P' => {
                 const enc = try reader.readByte();
-                if (enc != EH_PE.pcrel | EH_PE.indirect | EH_PE.sdata4) {
+                if (enc != DW_EH_PE.pcrel | DW_EH_PE.indirect | DW_EH_PE.sdata4) {
                     @panic("unexpected personality pointer encoding"); // TODO error
                 }
                 _ = try reader.readInt(u32, .little); // personality pointer
             },
             'L' => {
                 const enc = try reader.readByte();
-                switch (enc & 0xf) {
-                    EH_PE.sdata4 => cie.lsda_size = .p32,
-                    EH_PE.absptr => cie.lsda_size = .p64,
+                switch (enc & DW_EH_PE.type_mask) {
+                    DW_EH_PE.sdata4 => cie.lsda_size = .p32,
+                    DW_EH_PE.absptr => cie.lsda_size = .p64,
                     else => unreachable, // TODO error
                 }
             },
@@ -538,25 +538,6 @@ pub fn writeRelocs(macho_file: *MachO, code: []u8, relocs: []macho.relocation_in
     assert(relocs.len == i);
 }
 
-pub const EH_PE = struct {
-    pub const absptr = 0x00;
-    pub const uleb128 = 0x01;
-    pub const udata2 = 0x02;
-    pub const udata4 = 0x03;
-    pub const udata8 = 0x04;
-    pub const sleb128 = 0x09;
-    pub const sdata2 = 0x0A;
-    pub const sdata4 = 0x0B;
-    pub const sdata8 = 0x0C;
-    pub const pcrel = 0x10;
-    pub const textrel = 0x20;
-    pub const datarel = 0x30;
-    pub const funcrel = 0x40;
-    pub const aligned = 0x50;
-    pub const indirect = 0x80;
-    pub const omit = 0xFF;
-};
-
 const assert = std.debug.assert;
 const leb = std.leb;
 const macho = std.macho;
@@ -567,6 +548,7 @@ const trace = @import("../../tracy.zig").trace;
 
 const Allocator = std.mem.Allocator;
 const Atom = @import("Atom.zig");
+const DW_EH_PE = std.dwarf.EH.PE;
 const File = @import("file.zig").File;
 const MachO = @import("../MachO.zig");
 const Object = @import("Object.zig");
src/link/Dwarf.zig
@@ -3825,7 +3825,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
                         sleb128(header.fixedWriter(), dwarf.debug_frame.header.data_alignment_factor) catch unreachable;
                         uleb128(header.fixedWriter(), dwarf.debug_frame.header.return_address_register) catch unreachable;
                         uleb128(header.fixedWriter(), 1) catch unreachable;
-                        header.appendAssumeCapacity(0x10 | 0x08 | 0x03);
+                        header.appendAssumeCapacity(DW.EH.PE.pcrel | DW.EH.PE.sdata4);
                         header.appendAssumeCapacity(DW.CFA.def_cfa_sf);
                         uleb128(header.fixedWriter(), Register.rsp.dwarfNum()) catch unreachable;
                         sleb128(header.fixedWriter(), -1) catch unreachable;