Skip to content

Improve ELF structure definitions and comments - #326

Merged
jserv merged 2 commits into
sysprog21:masterfrom
DrXiao:adjust-elf-struct-member-type
Aug 23, 2026
Merged

Improve ELF structure definitions and comments#326
jserv merged 2 commits into
sysprog21:masterfrom
DrXiao:adjust-elf-struct-member-type

Conversation

@DrXiao

@DrXiao DrXiao commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Since the compiler now supports handling the 'short' data type, this commit adjusts the data types of the following data members from 'char [2]' to 'short':

  • elf32_hdr_t: e_type, e_machine, e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx
  • elf32_sym_t: st_shndx

With these changes, the ELF structure definitions are closer to the ELF specification.


Summary by cubic

Aligns ELF 16-bit fields with the ELF32 spec by switching from char[2] to short and adds spec-aligned comments; simplifies header generation with no change to on-disk layout.

  • Replaced char[2] with short for elf32_hdr_t: e_type, e_machine, e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx, and for elf32_sym_t: st_shndx.
  • Updated elf_generate_header to assign shorts directly.
  • Added inline comments documenting ELF typedef sizes/alignments and noting temporary use of signed types until unsigned is supported.

Written for commit 56f60cd. Summary will update on new commits.

Review in cubic

Since the compiler now supports handling the 'short' data type, this
commit adjusts the data types of the following data members from
'char [2]' to 'short':
- elf32_hdr_t: e_type, e_machine, e_ehsize, e_phentsize, e_phnum,
               e_shentsize, e_shnum, e_shstrndx
- elf32_sym_t: st_shndx

With these changes, the ELF structure definitions are closer to the ELF
specification.
Comment thread src/defs.h Outdated
Comment on lines +640 to +641
short e_type;
short e_machine;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider to add comments, addressing ELF spec.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider to add comments, addressing ELF spec.

/* In the ELF specification, the following data types are defined
 * for data representation:
 *
 * +-------------+------+-----------+--------------------------+
 * | Name        | Size | Alignment | Purpose                  |
 * +-------------+------+-----------+--------------------------+
 * | Elf32_Addr  | 4    | 4         | Unsigned program address |
 * +-------------+------+-----------+--------------------------+
 * | Elf32_Half  | 2    | 2         | Unsigned medium integer  |
 * +-------------+------+-----------+--------------------------+
 * | Elf32_Off   | 4    | 4         | Unsigned file offset     |
 * +-------------+------+-----------+--------------------------+
 * | Elf32_Sword | 4    | 4         | Signed large integer     |
 * +-------------+------+-----------+--------------------------+
 * | Elf32_Word  | 4    | 4         | Unsigned large integer   |
 * +-------------+------+-----------+--------------------------+
 * | unsigned    | 1    | 1         | Unsigned small integer   |
 * | char        |      |           |                          |
 * +-------------+------+-----------+--------------------------+
 *
 * However, since the current implementation doesn't support unsigned
 * data type definitions, such as 'unsigned int', 'unsigned short', and
 * so on, the ELF structures are now implemented primarily using the
 * 'signed' data type.
 * - Elf32_Addr    -> int
 * - Elf32_Half    -> short
 * - Elf32_Off     -> int
 * - Elf32_Word    -> int
 * - unsigned char -> char
 *
 * TODO: Use correct unsigned types for these ELF structures after
 * the 'unsigned' specifier is supported.
 */

/* ELF header
 *
 * #define EI_NIDENT 16
 * typedef struct {
 *     unsigned char e_ident[EI_NIDENT];
 *     Elf32_Half    e_type;
 *     Elf32_Half    e_machine;
 *     Elf32_Word    e_version;
 *     Elf32_Addr    e_entry;
 *     Elf32_Off     e_phoff;
 *     Elf32_Off     e_shoff;
 *     Elf32_Word    e_flags;
 *     Elf32_Half    e_ehsize;
 *     Elf32_Half    e_phentsize
 *     Elf32_Half    e_phnum;
 *     Elf32_Half    e_shentsize;
 *     Elf32_Half    e_shnum;
 *     Elf32_Half    e_shstrndx;
 * } Elf32_Ehdr;
 */
typedef struct {
    char e_ident[16];
    short e_type;
    short e_machine;
    int e_version;
    int e_entry;
    int e_phoff;
    int e_shoff;
    int e_flags;
    short e_ehsize;
    short e_phentsize;
    short e_phnum;
    short e_shentsize;
    short e_shnum;
    short e_shstrndx;
} elf32_hdr_t;

How about adding comments like the ones above?

ELF data types are summarized in a table, and each ELF structure (e.g.: elf32_hdr_t, elf32_phdr_t ...) includes a comment showing its original definition from the ELF specification.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use inlined comments instead.

cubic-dev-ai[bot]

This comment was marked as resolved.

This commit adds the following explanations about the ELF specification:
- ELF type definitions, includeing size and alignment:
  Elf32_Addr, Elf32_Sword, Elf32_Off, Elf32_Half and Elf32_Word.
- ELF struction definitions:
  Since the compiler does not yet support the 'unsigned' specifier, the
  signed data types (int, short, char) are temporarily used to define
  the ELF structures. That is, all ELF structures, such as elf32_hdr_t,
  elf32_phdr, and so on, have additional comments appened to explain the
  original ELF data type for each field.
@DrXiao DrXiao changed the title Adjust the member data types for ELF structures Improve ELF structure definitions and comments Aug 23, 2026
@jserv
jserv merged commit 7b9e4d1 into sysprog21:master Aug 23, 2026
16 checks passed
@jserv

jserv commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

Thank @DrXiao for contributing!

@DrXiao
DrXiao deleted the adjust-elf-struct-member-type branch August 23, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants