Improve ELF structure definitions and comments - #326
Merged
Conversation
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.
jserv
reviewed
Aug 10, 2026
Comment on lines
+640
to
+641
| short e_type; | ||
| short e_machine; |
Collaborator
There was a problem hiding this comment.
Consider to add comments, addressing ELF spec.
Collaborator
Author
There was a problem hiding this comment.
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.
Collaborator
There was a problem hiding this comment.
Use inlined comments instead.
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.
Collaborator
|
Thank @DrXiao for contributing! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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':
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.
Written for commit 56f60cd. Summary will update on new commits.