ELF specification

ELF specification for MOS-compatible processors
This is version 0.2.1 of this specification.

Comments and improvements are solicited at johnwbyrd at gmail dot com.

Overview
This is a specification to extend the Executable and Linking Format (ELF) to encompass the MOS family of processors and their relatives. These include, but are not limited to, the MOS 6502, the MOS 65816, and comparable processors.

This specification considers the following documents to be normative, and incorporates them by reference:


 * Key words for use in RFCs to Indicate Requirement Levels
 * Tool Interface Standard (TIS), Executable and Linking Format (ELF) Specification, Version 1.2
 * Semantic Versioning 2.0.0

ELF header
The EI_CLASS field of the e_ident[] identification index, as defined in the ELF header, should be set to ELFCLASS32, which has a value of 1. This identifies ELF files as having (at most) 32 bits of address space.

Although the ELF format encompasses 32-bit address types, it does not require them. MOS processors can access 16 bits by default and 24 bits at most. When an 8-bit, 16-bit, or 24-bit address is stored in a 32-bit ELF address type, the unused high bits should be zeroed.

Unused bits in any 32-bit ELF address field should be set to zero. If these unused bits are set in any ELF file, then tool behavior is undefined. However, if a tool detects that these unused bits are set, then this condition may be flagged as a warning or as an error by the tool.

Machine type field
The e_machine field should have a constant name of EM_MOS. The constant of EM_MOS shall be set to 6502 (decimal; equivalent to 0x1966 in hexadecimal).

The e_flags field bits should have the following meanings. These values may be logically OR'ed to indicate that multiple features are present.

Behavior of a binary resulting from linking incompatible combinations of e_flags values, is undefined. Linkers are suggested, but not required, to emit warnings when a flag mismatch exists between ELF objects, libraries, and/or executables.

Section flags
Each ELF section header contains a 32-bit word named sh_flags. For MOS-format ELF files, these flags have special meanings.

Relocation types
Relocations should behave as specified in the Relocations section of the ELF specification as described earlier. Each relocation entry in a MOS ELF file describes a particular MOS relocation. The lowest byte in the r_info field in an Elf32_Rel or Elf32_Rela relocation entry, is treated as a MOS-specific relocation type. (The ELF specification refers to this as the ELF32_R_TYPE macro.)

The exact types of MOS-specific relocations for ELF can be viewed in MOS.def as part of the llvm-mos distribution.