Re: [PATCH 02/16] doc: Add elf_flag*.3

2025-07-04 Thread Mark Wielaard
Hi Aaron,

On Mon, Jun 30, 2025 at 11:12:19PM -0400, Aaron Merey wrote:
> Signed-off-by: Aaron Merey 
> ---
>  doc/Makefile.am|   6 ++
>  doc/elf_flagdata.3 |   1 +
>  doc/elf_flagehdr.3 |   1 +
>  doc/elf_flagelf.3  | 145 +
>  doc/elf_flagphdr.3 |   1 +
>  doc/elf_flagscn.3  |   1 +
>  doc/elf_flagshdr.3 |   1 +
>  7 files changed, 156 insertions(+)
>  create mode 100644 doc/elf_flagdata.3
>  create mode 100644 doc/elf_flagehdr.3
>  create mode 100644 doc/elf_flagelf.3
>  create mode 100644 doc/elf_flagphdr.3
>  create mode 100644 doc/elf_flagscn.3
>  create mode 100644 doc/elf_flagshdr.3
> 
> diff --git a/doc/Makefile.am b/doc/Makefile.am
> index e5b12133..3b63dc38 100644
> --- a/doc/Makefile.am
> +++ b/doc/Makefile.am
> @@ -58,6 +58,12 @@ notrans_dist_man3_MANS= elf32_checksum.3 \
>   elf_errmsg.3 \
>   elf_errno.3 \
>   elf_fill.3 \
> + elf_flagelf.3 \
> + elf_flagehdr.3 \
> + elf_flagphdr.3 \
> + elf_flagscn.3 \
> + elf_flagdata.3 \
> + elf_flagshdr.3 \
>   elf_getbase.3 \
>   elf_getdata.3 \
>   elf_getscn.3 \

OK.

> diff --git a/doc/elf_flagdata.3 b/doc/elf_flagdata.3
> new file mode 100644
> index ..ee84d431
> --- /dev/null
> +++ b/doc/elf_flagdata.3
> @@ -0,0 +1 @@
> +.so man3/elf_flagelf.3
> diff --git a/doc/elf_flagehdr.3 b/doc/elf_flagehdr.3
> new file mode 100644
> index ..ee84d431
> --- /dev/null
> +++ b/doc/elf_flagehdr.3
> @@ -0,0 +1 @@
> +.so man3/elf_flagelf.3

OK.

> diff --git a/doc/elf_flagelf.3 b/doc/elf_flagelf.3
> new file mode 100644
> index ..1118deaa
> --- /dev/null
> +++ b/doc/elf_flagelf.3
> @@ -0,0 +1,145 @@
> +.TH ELF_FLAGELF 3 2025-06-23 "Libelf" "Libelf Programmer's Manual"
> +
> +.SH NAME
> +elf_flagelf, elf_flagdata, elf_flagehdr, elf_flagphdr, elf_flagscn, 
> elf_flagshdr \- set or clear flags on ELF descriptors and components
> +.SH SYNOPSIS
> +.nf
> +#include 
> +
> +.B unsigned int elf_flagelf(Elf *" elf ", Elf_Cmd" cmd ", unsigned int" 
> flags ");
> +.B unsigned int elf_flagehdr(Elf *" elf ", "Elf_Cmd" cmd ", unsigned int" 
> flags ");
> +.B unsigned int elf_flagphdr(Elf *" elf ", Elf_Cmd" cmd ", unsigned int" 
> flags ");
> +.B unsigned int elf_flagscn(Elf_Scn *" scn ", Elf_Cmd" cmd ", unsigned int" 
> flags ");
> +.B unsigned int elf_flagshdr("Elf_Scn *" scn ", "Elf_Cmd" cmd ", unsigned 
> int" flags ");
> +.B unsigned int elf_flagdata(Elf_Data *" data ", Elf_Cmd" cmd ", unsigned 
> int" flags ");
> +.fi

OK.

> +.SH DESCRIPTION
> +These functions allow the caller to set or clear flags on various ELF
> +components managed by
> +.B libelf .
> +They are typically used to indicate that a component has been modified
> +(dirty), or to control layout behavior during output.
> +
> +.BR elf_flagelf ()
> +modifies flags on the top-level
> +.B Elf
> +descriptor.
> +
> +.BR elf_flagdata ()
> +modifies flags on an
> +.B Elf_Data
> +descriptor, typically retrieved using
> +.BR elf_getdata (3)
> +or created with
> +.BR elf_newdata (3).
> +
> +.BR elf_flagehdr ()
> +modifies flags on the ELF header for a ELF descriptor (e.g., the result of
> +.BR gelf_getehdr (3)).
> +
> +.BR elf_flagphdr ()
> +modifies flags on the program header table of an ELF descriptor.
> +
> +.BR elf_flagscn ()
> +modifies flags on an
> +.B Elf_Scn
> +(section) descriptor.
> +
> +.BR elf_flagshdr ()
> +modifies flags on the section header of a given
> +.B Elf_Scn .

OK

> +The
> +.I cmd
> +argument determines whether the given flags should be set (
> +.B ELF_C_SET
> +) or cleared (
> +.B ELF_C_CLR
> +).

OK.

> +The following flag values may be passed via the
> +.I flags
> +parameter:
> +
> +.TP
> +.B ELF_F_DIRTY
> +Marks the object as modified. This ensures changes to the component is 
> written
> +out during
> +.BR elf_update (3).

OK.

> +.TP
> +.B ELF_F_LAYOUT
> +Indicates that the layout of the ELF component is managed manually. Offsets 
> and
> +sizes will be preserved as provided.

Only effective with elf_flagelf.  Specifically this requires the user
to set the Ehdr e_phoff and e_shoff, the Elf_Data d_off and the Shdr
sh_offset. When not set elf_update will (re)calculate these offsets.

> +.TP
> +.B ELF_F_PERMISSIVE
> +Enables relaxed validation of object structure. This flag permits 
> nonstandard layouts
> +without strict checks.

Only effective with elf_flagelf.  Note that this is an elfutils libelf
extension.  Specifically this removes the check in elf_update that the
section sh_size is a multiple of the section sh_entsize (if not zero).

> +.SH PARAMETERS
> +.TP
> +.I obj
> +The object to be modified:
> +For
> +.BR elf_flagelf ,
> +.BR elf_flagehdr ,
> +.BR elf_flagphdr ,
> +this is an
> +.B Elf *
> +descriptor.
> +For
> +.B elf_flagscn
> +and
> +.B elf_flagshdr ,
> +this is an
> +.B Elf

Re: [PATCH 01/16] doc: Add elf_memory.3

2025-07-04 Thread Mark Wielaard
Hi Aaron,

On Mon, Jun 30, 2025 at 11:12:18PM -0400, Aaron Merey wrote:
> Signed-off-by: Aaron Merey 
> ---
>  doc/Makefile.am  |  1 +
>  doc/elf_memory.3 | 84 
>  2 files changed, 85 insertions(+)
>  create mode 100644 doc/elf_memory.3
> 
> diff --git a/doc/Makefile.am b/doc/Makefile.am
> index 3ed04cd7..e5b12133 100644
> --- a/doc/Makefile.am
> +++ b/doc/Makefile.am
> @@ -63,6 +63,7 @@ notrans_dist_man3_MANS= elf32_checksum.3 \
>   elf_getscn.3 \
>   elf_hash.3 \
>   elf_kind.3 \
> + elf_memory.3 \
>   elf_ndxscn.3 \
>   elf_update.3 \
>   elf_version.3 \

OK.

> diff --git a/doc/elf_memory.3 b/doc/elf_memory.3
> new file mode 100644
> index ..88d259ca
> --- /dev/null
> +++ b/doc/elf_memory.3
> @@ -0,0 +1,84 @@
> +.TH ELF_MEMORY 3 2025-06-23 "Libelf" "Libelf Programmer's Manual"
> +
> +.SH NAME
> +elf_memory \- create an ELF descriptor from a memory buffer
> +.SH SYNOPSIS
> +.nf
> +#include 
> +
> +.B Elf * elf_memory(char *" image ", size_t" size ");

OK.

> +
> +.fi
> +.SH DESCRIPTION
> +The
> +.BR elf_memory ()
> +function creates a new ELF descriptor from a memory region containing an ELF
> +object.  This function is used when the ELF data is already present in memory
> +rather than being read from a file descriptor.
> +

OK.

> +The memory pointed to by
> +.I image
> +must contain a complete ELF file or archive. The contents must remain valid
> +and unmodified for the lifetime of the resulting ELF descriptor.
> +
> +The ELF descriptor returned by
> +.BR elf_memory ()
> +is opened in read-only mode using ELF_C_READ_MMAP_PRIVATE (see
> +.BR elf_begin ).
> +Writing, updating, or modifying this ELF descriptor is not permitted.

Yeah, hmmm. There has been some debate on that.  Originally it was
like ELF_C_READ, then ELF_C_READ_MMAP and now ELF_C_READ_MMAP_PRIVATE.

See https://sourceware.org/bugzilla/show_bug.cgi?id=31225
Crash when using elf_memory() on a compressed section; fixed with 
s/ELF_C_READ/ELF_C_READ_MMAP/
commit cc44ac6740797a23cd0af0cb22bd828d569224b8
libelf: Treat elf_memory as if using ELF_C_READ_MMAP
commit 81981fdfd8f776508579da60d29e068359c61e6a
libelf: Treat elf_memory image as writable

So while it is still treated as "read-only" it also allows
modifications in place. It is complicated.

> +.PP
> +Internally,
> +.BR elf_memory ()
> +is equivalent to calling
> +.BR elf_begin ()
> +to create a descriptor from an open memory buffer with command
> +.B ELF_C_READ_MMAP_PRIVATE
> +and a NULL parent.

Open memory buffer? Do you mean from an open file descriptor?

> +.SH PARAMETERS
> +.TP
> +.I image
> +A pointer to a memory buffer that contains the complete contents of an ELF 
> file
> +or archive. Should not be NULL.

Fails and returns NULL when image is NULL.

> +
> +.TP
> +.I size
> +The size in bytes of the memory region pointed to by
> +.IR image .
> +Must be greater than zero and cover the entire ELF object.

Size must be at least a full ELF header.

> +.SH RETURN VALUE
> +On success,
> +.BR elf_memory ()
> +returns a pointer to an
> +.B Elf
> +descriptor representing the archive or ELF file contained in
> +.IR image .
> +
> +On failure, it returns NULL and sets an error code retrievable by
> +.BR elf_errmsg (3).

OK.

> +
> +.SH SEE ALSO
> +.BR elf_begin (3),
> +.BR elf_errmsg (3),
> +.BR libelf (3),
> +.BR elf (5) 

OK.

> +.SH ATTRIBUTES
> +.TS
> +allbox;
> +lbx lb lb
> +l l l.
> +Interface   Attribute   Value
> +T{
> +.na
> +.nh
> +.BR elf_memory ()
> +T}  Thread safety   MT-Safe
> +.TE
> +
> +.SH REPORTING BUGS
> +Report bugs to  or 
> https://sourceware.org/bugzilla/.
> +
> -- 
> 2.49.0
>