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
842842 nosuspend {
843843 const mapped_mem = try mapWholeFile(elf_file);
844844 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;
846846 if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;
847847
848848 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 {
133133 defer os.munmap(file_bytes);
134134
135135 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;
137137 if (eh.e_type != elf.ET.DYN) return error.NotDynamicLibrary;
138138
139139 const elf_addr = @ptrToInt(file_bytes.ptr);
lib/std/elf.zig+3-1
......@@ -305,6 +305,8 @@ pub const STT_ARM_16BIT = STT_HIPROC;
305305pub const VER_FLG_BASE = 0x1;
306306pub const VER_FLG_WEAK = 0x2;
307307
308pub const MAGIC = "\x7fELF";
309
308310/// File types
309311pub const ET = enum(u16) {
310312 /// No file type
......@@ -367,7 +369,7 @@ pub const Header = struct {
367369 pub fn parse(hdr_buf: *align(@alignOf(Elf64_Ehdr)) const [@sizeOf(Elf64_Ehdr)]u8) !Header {
368370 const hdr32 = @ptrCast(*const Elf32_Ehdr, hdr_buf);
369371 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;
371373 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;
372374
373375 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(
53255325 const elf_base = std.process.getBaseAddress();
53265326 const ehdr = @intToPtr(*elf.Ehdr, elf_base);
53275327 // 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));
53295329 const n_phdr = ehdr.e_phnum;
53305330 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 {
447447 // Find the ELF header
448448 const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);
449449 // 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;
451451 // Consistency check
452452 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(
473473 _ = try preadMin(file, &hdr_buf, 0, hdr_buf.len);
474474 const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf);
475475 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;
477477 const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) {
478478 elf.ELFDATA2LSB => .Little,
479479 elf.ELFDATA2MSB => .Big,
src/link/Elf.zig+1-1
......@@ -1827,7 +1827,7 @@ fn writeElfHeader(self: *Elf) !void {
18271827 var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined;
18281828
18291829 var index: usize = 0;
1830 hdr_buf[0..4].* = "\x7fELF".*;
1830 hdr_buf[0..4].* = elf.MAGIC.*;
18311831 index += 4;
18321832
18331833 hdr_buf[index] = switch (self.ptr_width) {