pub const gnu_hash = struct pub fn calculate(name: []const u8) u32Calculate the hash value for a name
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307pub const gnu_hash = struct {
// See https://flapenguin.me/elf-dt-gnu-hash
pub const Header = extern struct {
nbuckets: u32,
symoffset: u32,
bloom_size: u32,
bloom_shift: u32,
};
pub const ChainEntry = packed struct(u32) {
end_of_chain: bool,
/// Contains the top bits of the hash value.
hash: u31,
};
/// Calculate the hash value for a name
pub fn calculate(name: []const u8) u32 {
var h: u32 = 5381;
for (name) |char| {
h = (h << 5) +% h +% char;
}
return h;
}
test calculate {
try std.testing.expectEqual(0x00001505, calculate(""));
try std.testing.expectEqual(0x156b2bb8, calculate("printf"));
try std.testing.expectEqual(0x7c967e3f, calculate("exit"));
try std.testing.expectEqual(0xbac212a0, calculate("syscall"));
try std.testing.expectEqual(0x8ae9f18e, calculate("flapenguin.me"));
}
}