From c0c4f491cac0abb83f5fa0ffc3f4db67ee0c6f52 Mon Sep 17 00:00:00 2001 From: Mikael Pettersson Date: Thu, 28 May 2020 19:30:54 +0200 Subject: [PATCH] pdp10_elf36: add support for reading program header tables --- erlang/apps/lib/src/pdp10_elf36.erl | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/erlang/apps/lib/src/pdp10_elf36.erl b/erlang/apps/lib/src/pdp10_elf36.erl index 8955a98..61b1dde 100644 --- a/erlang/apps/lib/src/pdp10_elf36.erl +++ b/erlang/apps/lib/src/pdp10_elf36.erl @@ -21,6 +21,7 @@ -module(pdp10_elf36). -export([ read_Ehdr/1 + , read_PhTab/2 , read_RelaTab/2 , read_ShTab/2 , read_SymTab/2 @@ -278,6 +279,32 @@ check_Ehdr_e_shentsize(Ehdr) -> _ -> {error, {?MODULE, {wrong_e_shentsize, ShEntSize}}} end. +%% I/O of PhTab ================================================================ + +-spec read_PhTab(pdp10_stdio:file(), #elf36_Ehdr{}) + -> {ok, [#elf36_Phdr{}]} | {error, {module(), term()}}. +read_PhTab(FP, Ehdr) -> + #elf36_Ehdr{ e_phoff = PhOff + , e_phentsize = PhEntSize + , e_phnum = PhNum } = Ehdr, + if PhOff =:= 0; PhNum =:= 0 -> + {ok, []}; + true -> + true = PhEntSize =:= ?ELF36_PHDR_SIZEOF, % assert + %% FIXME: if PhNum = ?PN_XNUM the real PhNum is stored in Shdr0.sh_info + case pdp10_stdio:fseek(FP, {bof, PhOff}) of + ok -> read_PhTab(FP, PhNum, []); + {error, _Reason} = Error -> Error + end + end. + +read_PhTab(_FP, 0, Phdrs) -> {ok, lists:reverse(Phdrs)}; +read_PhTab(FP, PhNum, Phdrs) -> + case read_Phdr(FP) of + {ok, Phdr} -> read_PhTab(FP, PhNum - 1, [Phdr | Phdrs]); + {error, _Reason} = Error -> Error + end. + %% I/O of relocation tables ==================================================== -spec read_RelaTab(pdp10_stdio:file(), #elf36_Shdr{}) @@ -494,6 +521,8 @@ read_Sym_name(Sym = #elf36_Sym{st_name = StName}, StrTab) -> %% I/O of #elf36_Phdr{} ======================================================== +read_Phdr(FP) -> read_record(FP, elf36_Phdr_desc()). + -spec write_Phdr(pdp10_stdio:file(), #elf36_Phdr{}) -> ok | {error, {module(), term()}}. write_Phdr(FP, Phdr) ->