From fc81f546bf409fad2f33c6c66d69d4c72d9904df Mon Sep 17 00:00:00 2001 From: Mikael Pettersson Date: Thu, 14 Aug 2025 16:38:47 +0200 Subject: [PATCH] readelf: move uint36_from_s64 conversion to extint --- erlang/apps/lib/src/extint.erl | 14 ++++++++++++++ erlang/apps/readelf/src/readelf.erl | 13 ++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/erlang/apps/lib/src/extint.erl b/erlang/apps/lib/src/extint.erl index 4f49d3a..90fde17 100644 --- a/erlang/apps/lib/src/extint.erl +++ b/erlang/apps/lib/src/extint.erl @@ -32,6 +32,7 @@ , uint32_from_ext/1 , uint32_to_ext/1 , uint36_from_ext/1 + , uint36_from_s64/1 , uint36_to_c36/1 , uint36_to_ext/1 , uint64_from_ext/1 @@ -77,6 +78,19 @@ uint36_from_ext([B0, B1, B2, B3]) -> ((B2 band ?UINT9_MAX) bsl 9) bor (B3 band ?UINT9_MAX). +%% S64 is the sparse representation of a 36-bit word in 64 bits where +%% each 9-bit field is zero-extended to 16 bits, i.e. two octets. +%% The PDP-10 port of GNU binutils uses this format. +%% This converts S64-encoded data back to a 36-bit word. +-spec uint36_from_s64([uint8_t()]) -> uint36_t(). +uint36_from_s64([B0, B1, B2, B3, B4, B5, B6, B7]) -> + 0 = (B0 bor B2 bor B4 bor B6) band bnot 1, % assert + N0 = (B0 bsl 8) bor B1, + N1 = (B2 bsl 8) bor B3, + N2 = (B4 bsl 8) bor B5, + N3 = (B6 bsl 8) bor B7, + uint36_from_ext([N0, N1, N2, N3]). + -spec uint36_to_ext(uint36_t()) -> [uint9_t()]. uint36_to_ext(U36) -> [_B0 = (U36 bsr 27) band ?UINT9_MAX, diff --git a/erlang/apps/readelf/src/readelf.erl b/erlang/apps/readelf/src/readelf.erl index a1b1734..c6defc0 100644 --- a/erlang/apps/readelf/src/readelf.erl +++ b/erlang/apps/readelf/src/readelf.erl @@ -745,26 +745,17 @@ wordsize(_) -> 4. read_uint36({?ELFCLASS64, _} = FP) -> case libelf:fread(8, FP) of - {ok, [B0, B1, B2, B3, B4, B5, B6, B7]} -> - 0 = (B0 bor B2 bor B4 bor B6) band bnot 1, % assert - N0 = (B0 bsl 8) bor B1, - N1 = (B2 bsl 8) bor B3, - N2 = (B4 bsl 8) bor B5, - N3 = (B6 bsl 8) bor B7, - {ok, make_uint36(N0, N1, N2, N3)}; + {ok, Bytes} -> {ok, extint:uint36_from_s64(Bytes)}; eof -> {error, eof}; {error, _Reason} = Error -> Error end; read_uint36({?ELFCLASS36, _} = FP) -> case libelf:fread(4, FP) of - {ok, [N0, N1, N2, N3]} -> {ok, make_uint36(N0, N1, N2, N3)}; + {ok, Nonets} -> {ok, extint:uint36_from_ext(Nonets)}; eof -> {error, eof}; {error, _Reason} = Error -> Error end. -make_uint36(N0, N1, N2, N3) -> - (((((N0 bsl 9) bor N1) bsl 9) bor N2) bsl 9) bor N3. - disassemble_insn(InsnWord) -> Models = ?PDP10_KL10_271up, High13 = InsnWord bsr (36 - 13),