ELF Library
1 Introduction
2 Reference
elf-header
program-header
section-header
read-elf
write-elf
read-elf-header
write-elf-header
elf-magic?
elf-info
read-elf-program-header
write-elf-program-header
read-section
write-section-header
elf-class-32
elf-class-64
elf-2cmpl-msb
elf-2cmpl-lsb
3 Copyright and contact
6.3.90.900

ELF Library

 (require elf) package: elf

    1 Introduction

    2 Reference

    3 Copyright and contact

1 Introduction

This library can read and write files in the exectuable and linking format common among unix like systems.

Due to the nature of overlapping binary data in ELF files, the library is strict in terms of write offsets and lengths. It is therefor the users responsibility to ensure that the length and offset fields in the headers are adjusted properly should the length of binary data be changed.

See the ELF man page elf(5) for more general information on the executable and linking format.

2 Reference

struct

(struct elf-header (class
    data
    version
    abi
    abiver
    type
    machine
    entry
    prog-headers
    section-headers
    flags
    header-size
    prog-header-size
    prog-header-count
    sec-header-size
    sec-header-count
    sec-index))
  class : integer?
  data : integer?
  version : integer?
  abi : integer?
  abiver : integer?
  type : integer?
  machine : integer?
  entry : integer?
  prog-headers : integer?
  section-headers : integer?
  flags : integer?
  header-size : integer?
  prog-header-size : integer?
  prog-header-count : integer?
  sec-header-size : integer?
  sec-header-count : integer?
  sec-index : integer?
The ELF header.

struct

(struct program-header (type
    offset
    v-addr
    p-addr
    file-size
    mem-size
    flags
    align
    data))
  type : integer?
  offset : integer?
  v-addr : integer?
  p-addr : integer?
  file-size : integer?
  mem-size : integer?
  flags : integer?
  align : integer?
  data : bytes?
An ELF program header.

The data field is unused currently.

struct

(struct section-header (name
    type
    flags
    addr
    offset
    size
    link
    info
    addralign
    entsize
    data))
  name : integer?
  type : integer?
  flags : integer?
  addr : integer?
  offset : integer?
  size : integer?
  link : integer?
  info : integer?
  addralign : integer?
  entsize : integer?
  data : bytes?
An ELF section header.

The data field is unused currently.

procedure

(read-elf stream)  list?

  stream : (file-stream-port?)
Reads an ELF from a file-stream, file stream must be seekable.

Returns a list consisting of an ELF header datastructure, a list of program-header, a list of section-header and 3 remaining elements of byte-string. One for data following the ELF header, another for data after the program headers and finally one for everything after the section headers.

procedure

(write-elf elf stream)  void?

  elf : (list?)
  stream : (output-port?)
Write an object representing ELF data to a file stream as binary.

Endianess and data (32 or 64 bit) should be set in the respective header fields, if someone needs to change them explictly.

procedure

(read-elf-header stream)  elf-header?

  stream : (file-stream-port?)
Read an ELF header from stream at current seek position.

procedure

(write-elf-header header stream)  void?

  header : (elf-header?)
  stream : (output-port?)
Writes an ELF header to stream at current seek position.

procedure

(elf-magic? bstring)  boolean?

  bstring : (bytes?)
Checks if byte-string has valid elf header magic string, each ELF file must have this string at first 4 bytes from its start.

procedure

(elf-info header)  string?

  header : (elf-header?)
Returns a string with some general human readable information about an ELF header.

procedure

(read-elf-program-header stream    
  [size    
  endianess])  program-header?
  stream : (file-stream-port?)
  size : (boolean?) = false
  endianess : (boolean?) = false
Reads a program header from current seek position.

procedure

(write-elf-program-header prog-header    
  stream    
  [data-size    
  endianess])  void?
  prog-header : (program-header?)
  stream : (output-port?)
  data-size : (boolean?) = false
  endianess : (boolean?) = false
Writes a program header to stream at current seek position.

procedure

(read-section stream [size endianess])  section-header?

  stream : (file-stream-port?)
  size : (boolean?) = false
  endianess : (boolean?) = false
Reads a section header from current seek position.

procedure

(write-section-header section-header    
  stream    
  [data-size    
  endianess])  void?
  section-header : (section-header?)
  stream : (output-port?)
  data-size : (boolean?) = false
  endianess : (boolean?) = false
Writes a section header to stream at current seek position.

value

elf-class-32 : (integer?)

Define a 32bit ELF file in the class field of the ELF header.

value

elf-class-64 : (integer?)

Define a 64bit ELF file in the class field of the ELF header.

value

elf-2cmpl-msb : (integer?)

Define a most significant bit ELF file data field of the ELF header.

value

elf-2cmpl-lsb : (integer?)

Define a least significant bit ELF file data field of the ELF header.

3 Copyright and contact

Writen by "Code_Man". Send feedback to Code_Man_AT_Cybnet.ch. See code_man.cybnet.ch for more stuff of mine.

No implied warranties or liability of any kind, use at own risk.