Commit cdcb34cdf4

Tom Read Cutting <moosichu@users.noreply.github.com>
2022-04-04 14:33:24
Pull elf magic string out to re-used constant
1 parent 6d04ab6
lib/std/os/test.zig
@@ -447,7 +447,7 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
         // Find the ELF header
         const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);
         // Validate the magic
-        if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return error.BadElfMagic;
+        if (!mem.eql(u8, elf_header.e_ident[0..4], elf.MAGIC)) return error.BadElfMagic;
         // Consistency check
         if (elf_header.e_phnum != info.dlpi_phnum) return error.FailedConsistencyCheck;
 
lib/std/zig/system/NativeTargetInfo.zig
@@ -473,7 +473,7 @@ pub fn abiAndDynamicLinkerFromFile(
     _ = try preadMin(file, &hdr_buf, 0, hdr_buf.len);
     const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf);
     const hdr64 = @ptrCast(*elf.Elf64_Ehdr, &hdr_buf);
-    if (!mem.eql(u8, hdr32.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;
+    if (!mem.eql(u8, hdr32.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic;
     const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) {
         elf.ELFDATA2LSB => .Little,
         elf.ELFDATA2MSB => .Big,
lib/std/debug.zig
@@ -842,7 +842,7 @@ pub fn readElfDebugInfo(allocator: mem.Allocator, elf_file: File) !ModuleDebugIn
     nosuspend {
         const mapped_mem = try mapWholeFile(elf_file);
         const hdr = @ptrCast(*const elf.Ehdr, &mapped_mem[0]);
-        if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;
+        if (!mem.eql(u8, hdr.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic;
         if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;
 
         const endian: std.builtin.Endian = switch (hdr.e_ident[elf.EI_DATA]) {
lib/std/dynamic_library.zig
@@ -133,7 +133,7 @@ pub const ElfDynLib = struct {
         defer os.munmap(file_bytes);
 
         const eh = @ptrCast(*elf.Ehdr, file_bytes.ptr);
-        if (!mem.eql(u8, eh.e_ident[0..4], "\x7fELF")) return error.NotElfFile;
+        if (!mem.eql(u8, eh.e_ident[0..4], elf.MAGIC)) return error.NotElfFile;
         if (eh.e_type != elf.ET.DYN) return error.NotDynamicLibrary;
 
         const elf_addr = @ptrToInt(file_bytes.ptr);
lib/std/elf.zig
@@ -305,6 +305,8 @@ pub const STT_ARM_16BIT = STT_HIPROC;
 pub const VER_FLG_BASE = 0x1;
 pub const VER_FLG_WEAK = 0x2;
 
+pub const MAGIC = "\x7fELF";
+
 /// File types
 pub const ET = enum(u16) {
     /// No file type
@@ -367,7 +369,7 @@ pub const Header = struct {
     pub fn parse(hdr_buf: *align(@alignOf(Elf64_Ehdr)) const [@sizeOf(Elf64_Ehdr)]u8) !Header {
         const hdr32 = @ptrCast(*const Elf32_Ehdr, hdr_buf);
         const hdr64 = @ptrCast(*const Elf64_Ehdr, hdr_buf);
-        if (!mem.eql(u8, hdr32.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;
+        if (!mem.eql(u8, hdr32.e_ident[0..4], MAGIC)) return error.InvalidElfMagic;
         if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;
 
         const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {
lib/std/os.zig
@@ -5325,7 +5325,7 @@ pub fn dl_iterate_phdr(
     const elf_base = std.process.getBaseAddress();
     const ehdr = @intToPtr(*elf.Ehdr, elf_base);
     // Make sure the base address points to an ELF image.
-    assert(mem.eql(u8, ehdr.e_ident[0..4], "\x7fELF"));
+    assert(mem.eql(u8, ehdr.e_ident[0..4], elf.MAGIC));
     const n_phdr = ehdr.e_phnum;
     const phdrs = (@intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff))[0..n_phdr];
 
src/link/Elf.zig
@@ -1827,7 +1827,7 @@ fn writeElfHeader(self: *Elf) !void {
     var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined;
 
     var index: usize = 0;
-    hdr_buf[0..4].* = "\x7fELF".*;
+    hdr_buf[0..4].* = elf.MAGIC.*;
     index += 4;
 
     hdr_buf[index] = switch (self.ptr_width) {