authorgravatar for readcuttingt@gmail.comTom Read Cutting <readcuttingt@gmail.com> 2022-04-04 13:33:24+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-04 15:33:24+03:00
logcdcb34cdf42fa3eb7a399106ccd57efc87643032
treedfe6f49534679b4eb5aaae77292345e03db11b0a
parent6d04ab6d5be1eaa47a920aaa3989bd3515fec5d6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Pull elf magic string out to re-used constant


7 files changed, 9 insertions(+), 7 deletions(-)

lib/std/debug.zig+1-1
...@@ -842,7 +842,7 @@ pub fn readElfDebugInfo(allocator: mem.Allocator, elf_file: File) !ModuleDebugIn...@@ -842,7 +842,7 @@ pub fn readElfDebugInfo(allocator: mem.Allocator, elf_file: File) !ModuleDebugIn
842 nosuspend {842 nosuspend {
843 const mapped_mem = try mapWholeFile(elf_file);843 const mapped_mem = try mapWholeFile(elf_file);
844 const hdr = @ptrCast(*const elf.Ehdr, &mapped_mem[0]);844 const hdr = @ptrCast(*const elf.Ehdr, &mapped_mem[0]);
845 if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;845 if (!mem.eql(u8, hdr.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic;
846 if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;846 if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;
847847
848 const endian: std.builtin.Endian = switch (hdr.e_ident[elf.EI_DATA]) {848 const endian: std.builtin.Endian = switch (hdr.e_ident[elf.EI_DATA]) {
lib/std/dynamic_library.zig+1-1
...@@ -133,7 +133,7 @@ pub const ElfDynLib = struct {...@@ -133,7 +133,7 @@ pub const ElfDynLib = struct {
133 defer os.munmap(file_bytes);133 defer os.munmap(file_bytes);
134134
135 const eh = @ptrCast(*elf.Ehdr, file_bytes.ptr);135 const eh = @ptrCast(*elf.Ehdr, file_bytes.ptr);
136 if (!mem.eql(u8, eh.e_ident[0..4], "\x7fELF")) return error.NotElfFile;136 if (!mem.eql(u8, eh.e_ident[0..4], elf.MAGIC)) return error.NotElfFile;
137 if (eh.e_type != elf.ET.DYN) return error.NotDynamicLibrary;137 if (eh.e_type != elf.ET.DYN) return error.NotDynamicLibrary;
138138
139 const elf_addr = @ptrToInt(file_bytes.ptr);139 const elf_addr = @ptrToInt(file_bytes.ptr);
lib/std/elf.zig+3-1
...@@ -305,6 +305,8 @@ pub const STT_ARM_16BIT = STT_HIPROC;...@@ -305,6 +305,8 @@ pub const STT_ARM_16BIT = STT_HIPROC;
305pub const VER_FLG_BASE = 0x1;305pub const VER_FLG_BASE = 0x1;
306pub const VER_FLG_WEAK = 0x2;306pub const VER_FLG_WEAK = 0x2;
307307
308pub const MAGIC = "\x7fELF";
309
308/// File types310/// File types
309pub const ET = enum(u16) {311pub const ET = enum(u16) {
310 /// No file type312 /// No file type
...@@ -367,7 +369,7 @@ pub const Header = struct {...@@ -367,7 +369,7 @@ pub const Header = struct {
367 pub fn parse(hdr_buf: *align(@alignOf(Elf64_Ehdr)) const [@sizeOf(Elf64_Ehdr)]u8) !Header {369 pub fn parse(hdr_buf: *align(@alignOf(Elf64_Ehdr)) const [@sizeOf(Elf64_Ehdr)]u8) !Header {
368 const hdr32 = @ptrCast(*const Elf32_Ehdr, hdr_buf);370 const hdr32 = @ptrCast(*const Elf32_Ehdr, hdr_buf);
369 const hdr64 = @ptrCast(*const Elf64_Ehdr, hdr_buf);371 const hdr64 = @ptrCast(*const Elf64_Ehdr, hdr_buf);
370 if (!mem.eql(u8, hdr32.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;372 if (!mem.eql(u8, hdr32.e_ident[0..4], MAGIC)) return error.InvalidElfMagic;
371 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;373 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;
372374
373 const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {375 const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {
lib/std/os.zig+1-1
...@@ -5325,7 +5325,7 @@ pub fn dl_iterate_phdr(...@@ -5325,7 +5325,7 @@ pub fn dl_iterate_phdr(
5325 const elf_base = std.process.getBaseAddress();5325 const elf_base = std.process.getBaseAddress();
5326 const ehdr = @intToPtr(*elf.Ehdr, elf_base);5326 const ehdr = @intToPtr(*elf.Ehdr, elf_base);
5327 // Make sure the base address points to an ELF image.5327 // Make sure the base address points to an ELF image.
5328 assert(mem.eql(u8, ehdr.e_ident[0..4], "\x7fELF"));5328 assert(mem.eql(u8, ehdr.e_ident[0..4], elf.MAGIC));
5329 const n_phdr = ehdr.e_phnum;5329 const n_phdr = ehdr.e_phnum;
5330 const phdrs = (@intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff))[0..n_phdr];5330 const phdrs = (@intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff))[0..n_phdr];
53315331
lib/std/os/test.zig+1-1
...@@ -447,7 +447,7 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {...@@ -447,7 +447,7 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
447 // Find the ELF header447 // Find the ELF header
448 const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);448 const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);
449 // Validate the magic449 // Validate the magic
450 if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return error.BadElfMagic;450 if (!mem.eql(u8, elf_header.e_ident[0..4], elf.MAGIC)) return error.BadElfMagic;
451 // Consistency check451 // Consistency check
452 if (elf_header.e_phnum != info.dlpi_phnum) return error.FailedConsistencyCheck;452 if (elf_header.e_phnum != info.dlpi_phnum) return error.FailedConsistencyCheck;
453453
lib/std/zig/system/NativeTargetInfo.zig+1-1
...@@ -473,7 +473,7 @@ pub fn abiAndDynamicLinkerFromFile(...@@ -473,7 +473,7 @@ pub fn abiAndDynamicLinkerFromFile(
473 _ = try preadMin(file, &hdr_buf, 0, hdr_buf.len);473 _ = try preadMin(file, &hdr_buf, 0, hdr_buf.len);
474 const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf);474 const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf);
475 const hdr64 = @ptrCast(*elf.Elf64_Ehdr, &hdr_buf);475 const hdr64 = @ptrCast(*elf.Elf64_Ehdr, &hdr_buf);
476 if (!mem.eql(u8, hdr32.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;476 if (!mem.eql(u8, hdr32.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic;
477 const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) {477 const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) {
478 elf.ELFDATA2LSB => .Little,478 elf.ELFDATA2LSB => .Little,
479 elf.ELFDATA2MSB => .Big,479 elf.ELFDATA2MSB => .Big,
src/link/Elf.zig+1-1
...@@ -1827,7 +1827,7 @@ fn writeElfHeader(self: *Elf) !void {...@@ -1827,7 +1827,7 @@ fn writeElfHeader(self: *Elf) !void {
1827 var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined;1827 var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined;
18281828
1829 var index: usize = 0;1829 var index: usize = 0;
1830 hdr_buf[0..4].* = "\x7fELF".*;1830 hdr_buf[0..4].* = elf.MAGIC.*;
1831 index += 4;1831 index += 4;
18321832
1833 hdr_buf[index] = switch (self.ptr_width) {1833 hdr_buf[index] = switch (self.ptr_width) {